fsl_pamu.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (C) 2013 Freescale Semiconductor, Inc.
  5. */
  6. #define pr_fmt(fmt) "fsl-pamu: %s: " fmt, __func__
  7. #include "fsl_pamu.h"
  8. #include <linux/fsl/guts.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/genalloc.h>
  11. #include <linux/of_address.h>
  12. #include <linux/of_irq.h>
  13. #include <linux/platform_device.h>
  14. #include <asm/mpc85xx.h>
  15. /* define indexes for each operation mapping scenario */
  16. #define OMI_QMAN 0x00
  17. #define OMI_FMAN 0x01
  18. #define OMI_QMAN_PRIV 0x02
  19. #define OMI_CAAM 0x03
  20. #define make64(high, low) (((u64)(high) << 32) | (low))
  21. struct pamu_isr_data {
  22. void __iomem *pamu_reg_base; /* Base address of PAMU regs */
  23. unsigned int count; /* The number of PAMUs */
  24. };
  25. static struct paace *ppaact;
  26. static struct paace *spaact;
  27. static bool probed; /* Has PAMU been probed? */
  28. /*
  29. * Table for matching compatible strings, for device tree
  30. * guts node, for QorIQ SOCs.
  31. * "fsl,qoriq-device-config-2.0" corresponds to T4 & B4
  32. * SOCs. For the older SOCs "fsl,qoriq-device-config-1.0"
  33. * string would be used.
  34. */
  35. static const struct of_device_id guts_device_ids[] = {
  36. { .compatible = "fsl,qoriq-device-config-1.0", },
  37. { .compatible = "fsl,qoriq-device-config-2.0", },
  38. {}
  39. };
  40. /*
  41. * Table for matching compatible strings, for device tree
  42. * L3 cache controller node.
  43. * "fsl,t4240-l3-cache-controller" corresponds to T4,
  44. * "fsl,b4860-l3-cache-controller" corresponds to B4 &
  45. * "fsl,p4080-l3-cache-controller" corresponds to other,
  46. * SOCs.
  47. */
  48. static const struct of_device_id l3_device_ids[] = {
  49. { .compatible = "fsl,t4240-l3-cache-controller", },
  50. { .compatible = "fsl,b4860-l3-cache-controller", },
  51. { .compatible = "fsl,p4080-l3-cache-controller", },
  52. {}
  53. };
  54. /* maximum subwindows permitted per liodn */
  55. static u32 max_subwindow_count;
  56. /**
  57. * pamu_get_ppaace() - Return the primary PACCE
  58. * @liodn: liodn PAACT index for desired PAACE
  59. *
  60. * Returns the ppace pointer upon success else return
  61. * null.
  62. */
  63. static struct paace *pamu_get_ppaace(int liodn)
  64. {
  65. if (!ppaact || liodn >= PAACE_NUMBER_ENTRIES) {
  66. pr_debug("PPAACT doesn't exist\n");
  67. return NULL;
  68. }
  69. return &ppaact[liodn];
  70. }
  71. /**
  72. * pamu_enable_liodn() - Set valid bit of PACCE
  73. * @liodn: liodn PAACT index for desired PAACE
  74. *
  75. * Returns 0 upon success else error code < 0 returned
  76. */
  77. int pamu_enable_liodn(int liodn)
  78. {
  79. struct paace *ppaace;
  80. ppaace = pamu_get_ppaace(liodn);
  81. if (!ppaace) {
  82. pr_debug("Invalid primary paace entry\n");
  83. return -ENOENT;
  84. }
  85. if (!get_bf(ppaace->addr_bitfields, PPAACE_AF_WSE)) {
  86. pr_debug("liodn %d not configured\n", liodn);
  87. return -EINVAL;
  88. }
  89. /* Ensure that all other stores to the ppaace complete first */
  90. mb();
  91. set_bf(ppaace->addr_bitfields, PAACE_AF_V, PAACE_V_VALID);
  92. mb();
  93. return 0;
  94. }
  95. /**
  96. * pamu_disable_liodn() - Clears valid bit of PACCE
  97. * @liodn: liodn PAACT index for desired PAACE
  98. *
  99. * Returns 0 upon success else error code < 0 returned
  100. */
  101. int pamu_disable_liodn(int liodn)
  102. {
  103. struct paace *ppaace;
  104. ppaace = pamu_get_ppaace(liodn);
  105. if (!ppaace) {
  106. pr_debug("Invalid primary paace entry\n");
  107. return -ENOENT;
  108. }
  109. set_bf(ppaace->addr_bitfields, PAACE_AF_V, PAACE_V_INVALID);
  110. mb();
  111. return 0;
  112. }
  113. /* Derive the window size encoding for a particular PAACE entry */
  114. static unsigned int map_addrspace_size_to_wse(phys_addr_t addrspace_size)
  115. {
  116. /* Bug if not a power of 2 */
  117. BUG_ON(addrspace_size & (addrspace_size - 1));
  118. /* window size is 2^(WSE+1) bytes */
  119. return fls64(addrspace_size) - 2;
  120. }
  121. /*
  122. * Set the PAACE type as primary and set the coherency required domain
  123. * attribute
  124. */
  125. static void pamu_init_ppaace(struct paace *ppaace)
  126. {
  127. set_bf(ppaace->addr_bitfields, PAACE_AF_PT, PAACE_PT_PRIMARY);
  128. set_bf(ppaace->domain_attr.to_host.coherency_required, PAACE_DA_HOST_CR,
  129. PAACE_M_COHERENCE_REQ);
  130. }
  131. /*
  132. * Function used for updating stash destination for the coressponding
  133. * LIODN.
  134. */
  135. int pamu_update_paace_stash(int liodn, u32 value)
  136. {
  137. struct paace *paace;
  138. paace = pamu_get_ppaace(liodn);
  139. if (!paace) {
  140. pr_debug("Invalid liodn entry\n");
  141. return -ENOENT;
  142. }
  143. set_bf(paace->impl_attr, PAACE_IA_CID, value);
  144. mb();
  145. return 0;
  146. }
  147. /**
  148. * pamu_config_paace() - Sets up PPAACE entry for specified liodn
  149. *
  150. * @liodn: Logical IO device number
  151. * @omi: Operation mapping index -- if ~omi == 0 then omi not defined
  152. * @stashid: cache stash id for associated cpu -- if ~stashid == 0 then
  153. * stashid not defined
  154. * @prot: window permissions
  155. *
  156. * Returns 0 upon success else error code < 0 returned
  157. */
  158. int pamu_config_ppaace(int liodn, u32 omi, u32 stashid, int prot)
  159. {
  160. struct paace *ppaace;
  161. ppaace = pamu_get_ppaace(liodn);
  162. if (!ppaace)
  163. return -ENOENT;
  164. /* window size is 2^(WSE+1) bytes */
  165. set_bf(ppaace->addr_bitfields, PPAACE_AF_WSE,
  166. map_addrspace_size_to_wse(1ULL << 36));
  167. pamu_init_ppaace(ppaace);
  168. ppaace->wbah = 0;
  169. set_bf(ppaace->addr_bitfields, PPAACE_AF_WBAL, 0);
  170. /* set up operation mapping if it's configured */
  171. if (omi < OME_NUMBER_ENTRIES) {
  172. set_bf(ppaace->impl_attr, PAACE_IA_OTM, PAACE_OTM_INDEXED);
  173. ppaace->op_encode.index_ot.omi = omi;
  174. } else if (~omi != 0) {
  175. pr_debug("bad operation mapping index: %d\n", omi);
  176. return -EINVAL;
  177. }
  178. /* configure stash id */
  179. if (~stashid != 0)
  180. set_bf(ppaace->impl_attr, PAACE_IA_CID, stashid);
  181. set_bf(ppaace->impl_attr, PAACE_IA_ATM, PAACE_ATM_WINDOW_XLATE);
  182. ppaace->twbah = 0;
  183. set_bf(ppaace->win_bitfields, PAACE_WIN_TWBAL, 0);
  184. set_bf(ppaace->addr_bitfields, PAACE_AF_AP, prot);
  185. set_bf(ppaace->impl_attr, PAACE_IA_WCE, 0);
  186. set_bf(ppaace->addr_bitfields, PPAACE_AF_MW, 0);
  187. mb();
  188. return 0;
  189. }
  190. /**
  191. * get_ome_index() - Returns the index in the operation mapping table
  192. * for device.
  193. * @*omi_index: pointer for storing the index value
  194. *
  195. */
  196. void get_ome_index(u32 *omi_index, struct device *dev)
  197. {
  198. if (of_device_is_compatible(dev->of_node, "fsl,qman-portal"))
  199. *omi_index = OMI_QMAN;
  200. if (of_device_is_compatible(dev->of_node, "fsl,qman"))
  201. *omi_index = OMI_QMAN_PRIV;
  202. }
  203. /**
  204. * get_stash_id - Returns stash destination id corresponding to a
  205. * cache type and vcpu.
  206. * @stash_dest_hint: L1, L2 or L3
  207. * @vcpu: vpcu target for a particular cache type.
  208. *
  209. * Returs stash on success or ~(u32)0 on failure.
  210. *
  211. */
  212. u32 get_stash_id(u32 stash_dest_hint, u32 vcpu)
  213. {
  214. const u32 *prop;
  215. struct device_node *node;
  216. u32 cache_level;
  217. int len, found = 0;
  218. int i;
  219. /* Fastpath, exit early if L3/CPC cache is target for stashing */
  220. if (stash_dest_hint == PAMU_ATTR_CACHE_L3) {
  221. node = of_find_matching_node(NULL, l3_device_ids);
  222. if (node) {
  223. prop = of_get_property(node, "cache-stash-id", NULL);
  224. if (!prop) {
  225. pr_debug("missing cache-stash-id at %pOF\n",
  226. node);
  227. of_node_put(node);
  228. return ~(u32)0;
  229. }
  230. of_node_put(node);
  231. return be32_to_cpup(prop);
  232. }
  233. return ~(u32)0;
  234. }
  235. for_each_of_cpu_node(node) {
  236. prop = of_get_property(node, "reg", &len);
  237. for (i = 0; i < len / sizeof(u32); i++) {
  238. if (be32_to_cpup(&prop[i]) == vcpu) {
  239. found = 1;
  240. goto found_cpu_node;
  241. }
  242. }
  243. }
  244. found_cpu_node:
  245. /* find the hwnode that represents the cache */
  246. for (cache_level = PAMU_ATTR_CACHE_L1; (cache_level < PAMU_ATTR_CACHE_L3) && found; cache_level++) {
  247. if (stash_dest_hint == cache_level) {
  248. prop = of_get_property(node, "cache-stash-id", NULL);
  249. if (!prop) {
  250. pr_debug("missing cache-stash-id at %pOF\n",
  251. node);
  252. of_node_put(node);
  253. return ~(u32)0;
  254. }
  255. of_node_put(node);
  256. return be32_to_cpup(prop);
  257. }
  258. prop = of_get_property(node, "next-level-cache", NULL);
  259. if (!prop) {
  260. pr_debug("can't find next-level-cache at %pOF\n", node);
  261. of_node_put(node);
  262. return ~(u32)0; /* can't traverse any further */
  263. }
  264. of_node_put(node);
  265. /* advance to next node in cache hierarchy */
  266. node = of_find_node_by_phandle(*prop);
  267. if (!node) {
  268. pr_debug("Invalid node for cache hierarchy\n");
  269. return ~(u32)0;
  270. }
  271. }
  272. pr_debug("stash dest not found for %d on vcpu %d\n",
  273. stash_dest_hint, vcpu);
  274. return ~(u32)0;
  275. }
  276. /* Identify if the PAACT table entry belongs to QMAN, BMAN or QMAN Portal */
  277. #define QMAN_PAACE 1
  278. #define QMAN_PORTAL_PAACE 2
  279. #define BMAN_PAACE 3
  280. /**
  281. * Setup operation mapping and stash destinations for QMAN and QMAN portal.
  282. * Memory accesses to QMAN and BMAN private memory need not be coherent, so
  283. * clear the PAACE entry coherency attribute for them.
  284. */
  285. static void setup_qbman_paace(struct paace *ppaace, int paace_type)
  286. {
  287. switch (paace_type) {
  288. case QMAN_PAACE:
  289. set_bf(ppaace->impl_attr, PAACE_IA_OTM, PAACE_OTM_INDEXED);
  290. ppaace->op_encode.index_ot.omi = OMI_QMAN_PRIV;
  291. /* setup QMAN Private data stashing for the L3 cache */
  292. set_bf(ppaace->impl_attr, PAACE_IA_CID, get_stash_id(PAMU_ATTR_CACHE_L3, 0));
  293. set_bf(ppaace->domain_attr.to_host.coherency_required, PAACE_DA_HOST_CR,
  294. 0);
  295. break;
  296. case QMAN_PORTAL_PAACE:
  297. set_bf(ppaace->impl_attr, PAACE_IA_OTM, PAACE_OTM_INDEXED);
  298. ppaace->op_encode.index_ot.omi = OMI_QMAN;
  299. /* Set DQRR and Frame stashing for the L3 cache */
  300. set_bf(ppaace->impl_attr, PAACE_IA_CID, get_stash_id(PAMU_ATTR_CACHE_L3, 0));
  301. break;
  302. case BMAN_PAACE:
  303. set_bf(ppaace->domain_attr.to_host.coherency_required, PAACE_DA_HOST_CR,
  304. 0);
  305. break;
  306. }
  307. }
  308. /**
  309. * Setup the operation mapping table for various devices. This is a static
  310. * table where each table index corresponds to a particular device. PAMU uses
  311. * this table to translate device transaction to appropriate corenet
  312. * transaction.
  313. */
  314. static void setup_omt(struct ome *omt)
  315. {
  316. struct ome *ome;
  317. /* Configure OMI_QMAN */
  318. ome = &omt[OMI_QMAN];
  319. ome->moe[IOE_READ_IDX] = EOE_VALID | EOE_READ;
  320. ome->moe[IOE_EREAD0_IDX] = EOE_VALID | EOE_RSA;
  321. ome->moe[IOE_WRITE_IDX] = EOE_VALID | EOE_WRITE;
  322. ome->moe[IOE_EWRITE0_IDX] = EOE_VALID | EOE_WWSAO;
  323. ome->moe[IOE_DIRECT0_IDX] = EOE_VALID | EOE_LDEC;
  324. ome->moe[IOE_DIRECT1_IDX] = EOE_VALID | EOE_LDECPE;
  325. /* Configure OMI_FMAN */
  326. ome = &omt[OMI_FMAN];
  327. ome->moe[IOE_READ_IDX] = EOE_VALID | EOE_READI;
  328. ome->moe[IOE_WRITE_IDX] = EOE_VALID | EOE_WRITE;
  329. /* Configure OMI_QMAN private */
  330. ome = &omt[OMI_QMAN_PRIV];
  331. ome->moe[IOE_READ_IDX] = EOE_VALID | EOE_READ;
  332. ome->moe[IOE_WRITE_IDX] = EOE_VALID | EOE_WRITE;
  333. ome->moe[IOE_EREAD0_IDX] = EOE_VALID | EOE_RSA;
  334. ome->moe[IOE_EWRITE0_IDX] = EOE_VALID | EOE_WWSA;
  335. /* Configure OMI_CAAM */
  336. ome = &omt[OMI_CAAM];
  337. ome->moe[IOE_READ_IDX] = EOE_VALID | EOE_READI;
  338. ome->moe[IOE_WRITE_IDX] = EOE_VALID | EOE_WRITE;
  339. }
  340. /*
  341. * Get the maximum number of PAACT table entries
  342. * and subwindows supported by PAMU
  343. */
  344. static void get_pamu_cap_values(unsigned long pamu_reg_base)
  345. {
  346. u32 pc_val;
  347. pc_val = in_be32((u32 *)(pamu_reg_base + PAMU_PC3));
  348. /* Maximum number of subwindows per liodn */
  349. max_subwindow_count = 1 << (1 + PAMU_PC3_MWCE(pc_val));
  350. }
  351. /* Setup PAMU registers pointing to PAACT, SPAACT and OMT */
  352. static int setup_one_pamu(unsigned long pamu_reg_base, unsigned long pamu_reg_size,
  353. phys_addr_t ppaact_phys, phys_addr_t spaact_phys,
  354. phys_addr_t omt_phys)
  355. {
  356. u32 *pc;
  357. struct pamu_mmap_regs *pamu_regs;
  358. pc = (u32 *) (pamu_reg_base + PAMU_PC);
  359. pamu_regs = (struct pamu_mmap_regs *)
  360. (pamu_reg_base + PAMU_MMAP_REGS_BASE);
  361. /* set up pointers to corenet control blocks */
  362. out_be32(&pamu_regs->ppbah, upper_32_bits(ppaact_phys));
  363. out_be32(&pamu_regs->ppbal, lower_32_bits(ppaact_phys));
  364. ppaact_phys = ppaact_phys + PAACT_SIZE;
  365. out_be32(&pamu_regs->pplah, upper_32_bits(ppaact_phys));
  366. out_be32(&pamu_regs->pplal, lower_32_bits(ppaact_phys));
  367. out_be32(&pamu_regs->spbah, upper_32_bits(spaact_phys));
  368. out_be32(&pamu_regs->spbal, lower_32_bits(spaact_phys));
  369. spaact_phys = spaact_phys + SPAACT_SIZE;
  370. out_be32(&pamu_regs->splah, upper_32_bits(spaact_phys));
  371. out_be32(&pamu_regs->splal, lower_32_bits(spaact_phys));
  372. out_be32(&pamu_regs->obah, upper_32_bits(omt_phys));
  373. out_be32(&pamu_regs->obal, lower_32_bits(omt_phys));
  374. omt_phys = omt_phys + OMT_SIZE;
  375. out_be32(&pamu_regs->olah, upper_32_bits(omt_phys));
  376. out_be32(&pamu_regs->olal, lower_32_bits(omt_phys));
  377. /*
  378. * set PAMU enable bit,
  379. * allow ppaact & omt to be cached
  380. * & enable PAMU access violation interrupts.
  381. */
  382. out_be32((u32 *)(pamu_reg_base + PAMU_PICS),
  383. PAMU_ACCESS_VIOLATION_ENABLE);
  384. out_be32(pc, PAMU_PC_PE | PAMU_PC_OCE | PAMU_PC_SPCC | PAMU_PC_PPCC);
  385. return 0;
  386. }
  387. /* Enable all device LIODNS */
  388. static void setup_liodns(void)
  389. {
  390. int i, len;
  391. struct paace *ppaace;
  392. struct device_node *node = NULL;
  393. const u32 *prop;
  394. for_each_node_with_property(node, "fsl,liodn") {
  395. prop = of_get_property(node, "fsl,liodn", &len);
  396. for (i = 0; i < len / sizeof(u32); i++) {
  397. int liodn;
  398. liodn = be32_to_cpup(&prop[i]);
  399. if (liodn >= PAACE_NUMBER_ENTRIES) {
  400. pr_debug("Invalid LIODN value %d\n", liodn);
  401. continue;
  402. }
  403. ppaace = pamu_get_ppaace(liodn);
  404. pamu_init_ppaace(ppaace);
  405. /* window size is 2^(WSE+1) bytes */
  406. set_bf(ppaace->addr_bitfields, PPAACE_AF_WSE, 35);
  407. ppaace->wbah = 0;
  408. set_bf(ppaace->addr_bitfields, PPAACE_AF_WBAL, 0);
  409. set_bf(ppaace->impl_attr, PAACE_IA_ATM,
  410. PAACE_ATM_NO_XLATE);
  411. set_bf(ppaace->addr_bitfields, PAACE_AF_AP,
  412. PAACE_AP_PERMS_ALL);
  413. if (of_device_is_compatible(node, "fsl,qman-portal"))
  414. setup_qbman_paace(ppaace, QMAN_PORTAL_PAACE);
  415. if (of_device_is_compatible(node, "fsl,qman"))
  416. setup_qbman_paace(ppaace, QMAN_PAACE);
  417. if (of_device_is_compatible(node, "fsl,bman"))
  418. setup_qbman_paace(ppaace, BMAN_PAACE);
  419. mb();
  420. pamu_enable_liodn(liodn);
  421. }
  422. }
  423. }
  424. static irqreturn_t pamu_av_isr(int irq, void *arg)
  425. {
  426. struct pamu_isr_data *data = arg;
  427. phys_addr_t phys;
  428. unsigned int i, j, ret;
  429. pr_emerg("access violation interrupt\n");
  430. for (i = 0; i < data->count; i++) {
  431. void __iomem *p = data->pamu_reg_base + i * PAMU_OFFSET;
  432. u32 pics = in_be32(p + PAMU_PICS);
  433. if (pics & PAMU_ACCESS_VIOLATION_STAT) {
  434. u32 avs1 = in_be32(p + PAMU_AVS1);
  435. struct paace *paace;
  436. pr_emerg("POES1=%08x\n", in_be32(p + PAMU_POES1));
  437. pr_emerg("POES2=%08x\n", in_be32(p + PAMU_POES2));
  438. pr_emerg("AVS1=%08x\n", avs1);
  439. pr_emerg("AVS2=%08x\n", in_be32(p + PAMU_AVS2));
  440. pr_emerg("AVA=%016llx\n",
  441. make64(in_be32(p + PAMU_AVAH),
  442. in_be32(p + PAMU_AVAL)));
  443. pr_emerg("UDAD=%08x\n", in_be32(p + PAMU_UDAD));
  444. pr_emerg("POEA=%016llx\n",
  445. make64(in_be32(p + PAMU_POEAH),
  446. in_be32(p + PAMU_POEAL)));
  447. phys = make64(in_be32(p + PAMU_POEAH),
  448. in_be32(p + PAMU_POEAL));
  449. /* Assume that POEA points to a PAACE */
  450. if (phys) {
  451. u32 *paace = phys_to_virt(phys);
  452. /* Only the first four words are relevant */
  453. for (j = 0; j < 4; j++)
  454. pr_emerg("PAACE[%u]=%08x\n",
  455. j, in_be32(paace + j));
  456. }
  457. /* clear access violation condition */
  458. out_be32(p + PAMU_AVS1, avs1 & PAMU_AV_MASK);
  459. paace = pamu_get_ppaace(avs1 >> PAMU_AVS1_LIODN_SHIFT);
  460. BUG_ON(!paace);
  461. /* check if we got a violation for a disabled LIODN */
  462. if (!get_bf(paace->addr_bitfields, PAACE_AF_V)) {
  463. /*
  464. * As per hardware erratum A-003638, access
  465. * violation can be reported for a disabled
  466. * LIODN. If we hit that condition, disable
  467. * access violation reporting.
  468. */
  469. pics &= ~PAMU_ACCESS_VIOLATION_ENABLE;
  470. } else {
  471. /* Disable the LIODN */
  472. ret = pamu_disable_liodn(avs1 >> PAMU_AVS1_LIODN_SHIFT);
  473. BUG_ON(ret);
  474. pr_emerg("Disabling liodn %x\n",
  475. avs1 >> PAMU_AVS1_LIODN_SHIFT);
  476. }
  477. out_be32((p + PAMU_PICS), pics);
  478. }
  479. }
  480. return IRQ_HANDLED;
  481. }
  482. #define LAWAR_EN 0x80000000
  483. #define LAWAR_TARGET_MASK 0x0FF00000
  484. #define LAWAR_TARGET_SHIFT 20
  485. #define LAWAR_SIZE_MASK 0x0000003F
  486. #define LAWAR_CSDID_MASK 0x000FF000
  487. #define LAWAR_CSDID_SHIFT 12
  488. #define LAW_SIZE_4K 0xb
  489. struct ccsr_law {
  490. u32 lawbarh; /* LAWn base address high */
  491. u32 lawbarl; /* LAWn base address low */
  492. u32 lawar; /* LAWn attributes */
  493. u32 reserved;
  494. };
  495. /*
  496. * Create a coherence subdomain for a given memory block.
  497. */
  498. static int create_csd(phys_addr_t phys, size_t size, u32 csd_port_id)
  499. {
  500. struct device_node *np;
  501. const __be32 *iprop;
  502. void __iomem *lac = NULL; /* Local Access Control registers */
  503. struct ccsr_law __iomem *law;
  504. void __iomem *ccm = NULL;
  505. u32 __iomem *csdids;
  506. unsigned int i, num_laws, num_csds;
  507. u32 law_target = 0;
  508. u32 csd_id = 0;
  509. int ret = 0;
  510. np = of_find_compatible_node(NULL, NULL, "fsl,corenet-law");
  511. if (!np)
  512. return -ENODEV;
  513. iprop = of_get_property(np, "fsl,num-laws", NULL);
  514. if (!iprop) {
  515. ret = -ENODEV;
  516. goto error;
  517. }
  518. num_laws = be32_to_cpup(iprop);
  519. if (!num_laws) {
  520. ret = -ENODEV;
  521. goto error;
  522. }
  523. lac = of_iomap(np, 0);
  524. if (!lac) {
  525. ret = -ENODEV;
  526. goto error;
  527. }
  528. /* LAW registers are at offset 0xC00 */
  529. law = lac + 0xC00;
  530. of_node_put(np);
  531. np = of_find_compatible_node(NULL, NULL, "fsl,corenet-cf");
  532. if (!np) {
  533. ret = -ENODEV;
  534. goto error;
  535. }
  536. iprop = of_get_property(np, "fsl,ccf-num-csdids", NULL);
  537. if (!iprop) {
  538. ret = -ENODEV;
  539. goto error;
  540. }
  541. num_csds = be32_to_cpup(iprop);
  542. if (!num_csds) {
  543. ret = -ENODEV;
  544. goto error;
  545. }
  546. ccm = of_iomap(np, 0);
  547. if (!ccm) {
  548. ret = -ENOMEM;
  549. goto error;
  550. }
  551. /* The undocumented CSDID registers are at offset 0x600 */
  552. csdids = ccm + 0x600;
  553. of_node_put(np);
  554. np = NULL;
  555. /* Find an unused coherence subdomain ID */
  556. for (csd_id = 0; csd_id < num_csds; csd_id++) {
  557. if (!csdids[csd_id])
  558. break;
  559. }
  560. /* Store the Port ID in the (undocumented) proper CIDMRxx register */
  561. csdids[csd_id] = csd_port_id;
  562. /* Find the DDR LAW that maps to our buffer. */
  563. for (i = 0; i < num_laws; i++) {
  564. if (law[i].lawar & LAWAR_EN) {
  565. phys_addr_t law_start, law_end;
  566. law_start = make64(law[i].lawbarh, law[i].lawbarl);
  567. law_end = law_start +
  568. (2ULL << (law[i].lawar & LAWAR_SIZE_MASK));
  569. if (law_start <= phys && phys < law_end) {
  570. law_target = law[i].lawar & LAWAR_TARGET_MASK;
  571. break;
  572. }
  573. }
  574. }
  575. if (i == 0 || i == num_laws) {
  576. /* This should never happen */
  577. ret = -ENOENT;
  578. goto error;
  579. }
  580. /* Find a free LAW entry */
  581. while (law[--i].lawar & LAWAR_EN) {
  582. if (i == 0) {
  583. /* No higher priority LAW slots available */
  584. ret = -ENOENT;
  585. goto error;
  586. }
  587. }
  588. law[i].lawbarh = upper_32_bits(phys);
  589. law[i].lawbarl = lower_32_bits(phys);
  590. wmb();
  591. law[i].lawar = LAWAR_EN | law_target | (csd_id << LAWAR_CSDID_SHIFT) |
  592. (LAW_SIZE_4K + get_order(size));
  593. wmb();
  594. error:
  595. if (ccm)
  596. iounmap(ccm);
  597. if (lac)
  598. iounmap(lac);
  599. if (np)
  600. of_node_put(np);
  601. return ret;
  602. }
  603. /*
  604. * Table of SVRs and the corresponding PORT_ID values. Port ID corresponds to a
  605. * bit map of snoopers for a given range of memory mapped by a LAW.
  606. *
  607. * All future CoreNet-enabled SOCs will have this erratum(A-004510) fixed, so this
  608. * table should never need to be updated. SVRs are guaranteed to be unique, so
  609. * there is no worry that a future SOC will inadvertently have one of these
  610. * values.
  611. */
  612. static const struct {
  613. u32 svr;
  614. u32 port_id;
  615. } port_id_map[] = {
  616. {(SVR_P2040 << 8) | 0x10, 0xFF000000}, /* P2040 1.0 */
  617. {(SVR_P2040 << 8) | 0x11, 0xFF000000}, /* P2040 1.1 */
  618. {(SVR_P2041 << 8) | 0x10, 0xFF000000}, /* P2041 1.0 */
  619. {(SVR_P2041 << 8) | 0x11, 0xFF000000}, /* P2041 1.1 */
  620. {(SVR_P3041 << 8) | 0x10, 0xFF000000}, /* P3041 1.0 */
  621. {(SVR_P3041 << 8) | 0x11, 0xFF000000}, /* P3041 1.1 */
  622. {(SVR_P4040 << 8) | 0x20, 0xFFF80000}, /* P4040 2.0 */
  623. {(SVR_P4080 << 8) | 0x20, 0xFFF80000}, /* P4080 2.0 */
  624. {(SVR_P5010 << 8) | 0x10, 0xFC000000}, /* P5010 1.0 */
  625. {(SVR_P5010 << 8) | 0x20, 0xFC000000}, /* P5010 2.0 */
  626. {(SVR_P5020 << 8) | 0x10, 0xFC000000}, /* P5020 1.0 */
  627. {(SVR_P5021 << 8) | 0x10, 0xFF800000}, /* P5021 1.0 */
  628. {(SVR_P5040 << 8) | 0x10, 0xFF800000}, /* P5040 1.0 */
  629. };
  630. #define SVR_SECURITY 0x80000 /* The Security (E) bit */
  631. static int fsl_pamu_probe(struct platform_device *pdev)
  632. {
  633. struct device *dev = &pdev->dev;
  634. void __iomem *pamu_regs = NULL;
  635. struct ccsr_guts __iomem *guts_regs = NULL;
  636. u32 pamubypenr, pamu_counter;
  637. unsigned long pamu_reg_off;
  638. unsigned long pamu_reg_base;
  639. struct pamu_isr_data *data = NULL;
  640. struct device_node *guts_node;
  641. u64 size;
  642. struct page *p;
  643. int ret = 0;
  644. int irq;
  645. phys_addr_t ppaact_phys;
  646. phys_addr_t spaact_phys;
  647. struct ome *omt;
  648. phys_addr_t omt_phys;
  649. size_t mem_size = 0;
  650. unsigned int order = 0;
  651. u32 csd_port_id = 0;
  652. unsigned i;
  653. /*
  654. * enumerate all PAMUs and allocate and setup PAMU tables
  655. * for each of them,
  656. * NOTE : All PAMUs share the same LIODN tables.
  657. */
  658. if (WARN_ON(probed))
  659. return -EBUSY;
  660. pamu_regs = of_iomap(dev->of_node, 0);
  661. if (!pamu_regs) {
  662. dev_err(dev, "ioremap of PAMU node failed\n");
  663. return -ENOMEM;
  664. }
  665. of_get_address(dev->of_node, 0, &size, NULL);
  666. irq = irq_of_parse_and_map(dev->of_node, 0);
  667. if (irq == NO_IRQ) {
  668. dev_warn(dev, "no interrupts listed in PAMU node\n");
  669. goto error;
  670. }
  671. data = kzalloc(sizeof(*data), GFP_KERNEL);
  672. if (!data) {
  673. ret = -ENOMEM;
  674. goto error;
  675. }
  676. data->pamu_reg_base = pamu_regs;
  677. data->count = size / PAMU_OFFSET;
  678. /* The ISR needs access to the regs, so we won't iounmap them */
  679. ret = request_irq(irq, pamu_av_isr, 0, "pamu", data);
  680. if (ret < 0) {
  681. dev_err(dev, "error %i installing ISR for irq %i\n", ret, irq);
  682. goto error;
  683. }
  684. guts_node = of_find_matching_node(NULL, guts_device_ids);
  685. if (!guts_node) {
  686. dev_err(dev, "could not find GUTS node %pOF\n", dev->of_node);
  687. ret = -ENODEV;
  688. goto error;
  689. }
  690. guts_regs = of_iomap(guts_node, 0);
  691. of_node_put(guts_node);
  692. if (!guts_regs) {
  693. dev_err(dev, "ioremap of GUTS node failed\n");
  694. ret = -ENODEV;
  695. goto error;
  696. }
  697. /* read in the PAMU capability registers */
  698. get_pamu_cap_values((unsigned long)pamu_regs);
  699. /*
  700. * To simplify the allocation of a coherency domain, we allocate the
  701. * PAACT and the OMT in the same memory buffer. Unfortunately, this
  702. * wastes more memory compared to allocating the buffers separately.
  703. */
  704. /* Determine how much memory we need */
  705. mem_size = (PAGE_SIZE << get_order(PAACT_SIZE)) +
  706. (PAGE_SIZE << get_order(SPAACT_SIZE)) +
  707. (PAGE_SIZE << get_order(OMT_SIZE));
  708. order = get_order(mem_size);
  709. p = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
  710. if (!p) {
  711. dev_err(dev, "unable to allocate PAACT/SPAACT/OMT block\n");
  712. ret = -ENOMEM;
  713. goto error;
  714. }
  715. ppaact = page_address(p);
  716. ppaact_phys = page_to_phys(p);
  717. /* Make sure the memory is naturally aligned */
  718. if (ppaact_phys & ((PAGE_SIZE << order) - 1)) {
  719. dev_err(dev, "PAACT/OMT block is unaligned\n");
  720. ret = -ENOMEM;
  721. goto error;
  722. }
  723. spaact = (void *)ppaact + (PAGE_SIZE << get_order(PAACT_SIZE));
  724. omt = (void *)spaact + (PAGE_SIZE << get_order(SPAACT_SIZE));
  725. dev_dbg(dev, "ppaact virt=%p phys=%pa\n", ppaact, &ppaact_phys);
  726. /* Check to see if we need to implement the work-around on this SOC */
  727. /* Determine the Port ID for our coherence subdomain */
  728. for (i = 0; i < ARRAY_SIZE(port_id_map); i++) {
  729. if (port_id_map[i].svr == (mfspr(SPRN_SVR) & ~SVR_SECURITY)) {
  730. csd_port_id = port_id_map[i].port_id;
  731. dev_dbg(dev, "found matching SVR %08x\n",
  732. port_id_map[i].svr);
  733. break;
  734. }
  735. }
  736. if (csd_port_id) {
  737. dev_dbg(dev, "creating coherency subdomain at address %pa, size %zu, port id 0x%08x",
  738. &ppaact_phys, mem_size, csd_port_id);
  739. ret = create_csd(ppaact_phys, mem_size, csd_port_id);
  740. if (ret) {
  741. dev_err(dev, "could not create coherence subdomain\n");
  742. goto error;
  743. }
  744. }
  745. spaact_phys = virt_to_phys(spaact);
  746. omt_phys = virt_to_phys(omt);
  747. pamubypenr = in_be32(&guts_regs->pamubypenr);
  748. for (pamu_reg_off = 0, pamu_counter = 0x80000000; pamu_reg_off < size;
  749. pamu_reg_off += PAMU_OFFSET, pamu_counter >>= 1) {
  750. pamu_reg_base = (unsigned long)pamu_regs + pamu_reg_off;
  751. setup_one_pamu(pamu_reg_base, pamu_reg_off, ppaact_phys,
  752. spaact_phys, omt_phys);
  753. /* Disable PAMU bypass for this PAMU */
  754. pamubypenr &= ~pamu_counter;
  755. }
  756. setup_omt(omt);
  757. /* Enable all relevant PAMU(s) */
  758. out_be32(&guts_regs->pamubypenr, pamubypenr);
  759. iounmap(guts_regs);
  760. /* Enable DMA for the LIODNs in the device tree */
  761. setup_liodns();
  762. probed = true;
  763. return 0;
  764. error:
  765. if (irq != NO_IRQ)
  766. free_irq(irq, data);
  767. kfree_sensitive(data);
  768. if (pamu_regs)
  769. iounmap(pamu_regs);
  770. if (guts_regs)
  771. iounmap(guts_regs);
  772. if (ppaact)
  773. free_pages((unsigned long)ppaact, order);
  774. ppaact = NULL;
  775. return ret;
  776. }
  777. static struct platform_driver fsl_of_pamu_driver = {
  778. .driver = {
  779. .name = "fsl-of-pamu",
  780. },
  781. .probe = fsl_pamu_probe,
  782. };
  783. static __init int fsl_pamu_init(void)
  784. {
  785. struct platform_device *pdev = NULL;
  786. struct device_node *np;
  787. int ret;
  788. /*
  789. * The normal OF process calls the probe function at some
  790. * indeterminate later time, after most drivers have loaded. This is
  791. * too late for us, because PAMU clients (like the Qman driver)
  792. * depend on PAMU being initialized early.
  793. *
  794. * So instead, we "manually" call our probe function by creating the
  795. * platform devices ourselves.
  796. */
  797. /*
  798. * We assume that there is only one PAMU node in the device tree. A
  799. * single PAMU node represents all of the PAMU devices in the SOC
  800. * already. Everything else already makes that assumption, and the
  801. * binding for the PAMU nodes doesn't allow for any parent-child
  802. * relationships anyway. In other words, support for more than one
  803. * PAMU node would require significant changes to a lot of code.
  804. */
  805. np = of_find_compatible_node(NULL, NULL, "fsl,pamu");
  806. if (!np) {
  807. pr_err("could not find a PAMU node\n");
  808. return -ENODEV;
  809. }
  810. ret = platform_driver_register(&fsl_of_pamu_driver);
  811. if (ret) {
  812. pr_err("could not register driver (err=%i)\n", ret);
  813. goto error_driver_register;
  814. }
  815. pdev = platform_device_alloc("fsl-of-pamu", 0);
  816. if (!pdev) {
  817. pr_err("could not allocate device %pOF\n", np);
  818. ret = -ENOMEM;
  819. goto error_device_alloc;
  820. }
  821. pdev->dev.of_node = of_node_get(np);
  822. ret = pamu_domain_init();
  823. if (ret)
  824. goto error_device_add;
  825. ret = platform_device_add(pdev);
  826. if (ret) {
  827. pr_err("could not add device %pOF (err=%i)\n", np, ret);
  828. goto error_device_add;
  829. }
  830. return 0;
  831. error_device_add:
  832. of_node_put(pdev->dev.of_node);
  833. pdev->dev.of_node = NULL;
  834. platform_device_put(pdev);
  835. error_device_alloc:
  836. platform_driver_unregister(&fsl_of_pamu_driver);
  837. error_driver_register:
  838. of_node_put(np);
  839. return ret;
  840. }
  841. arch_initcall(fsl_pamu_init);