iommu.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. * 02110-1301, USA.
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/errno.h>
  22. #include <linux/io.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/list.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/slab.h>
  27. #include <linux/iommu.h>
  28. #include <linux/clk.h>
  29. #include <asm/cacheflush.h>
  30. #include <asm/sizes.h>
  31. #include <mach/iommu_hw-8xxx.h>
  32. #include <mach/iommu.h>
  33. #define MRC(reg, processor, op1, crn, crm, op2) \
  34. __asm__ __volatile__ ( \
  35. " mrc " #processor "," #op1 ", %0," #crn "," #crm "," #op2 "\n" \
  36. : "=r" (reg))
  37. #define RCP15_PRRR(reg) MRC(reg, p15, 0, c10, c2, 0)
  38. #define RCP15_NMRR(reg) MRC(reg, p15, 0, c10, c2, 1)
  39. static int msm_iommu_tex_class[4];
  40. DEFINE_SPINLOCK(msm_iommu_lock);
  41. struct msm_priv {
  42. unsigned long *pgtable;
  43. struct list_head list_attached;
  44. };
  45. static int __enable_clocks(struct msm_iommu_drvdata *drvdata)
  46. {
  47. int ret;
  48. ret = clk_enable(drvdata->pclk);
  49. if (ret)
  50. goto fail;
  51. if (drvdata->clk) {
  52. ret = clk_enable(drvdata->clk);
  53. if (ret)
  54. clk_disable(drvdata->pclk);
  55. }
  56. fail:
  57. return ret;
  58. }
  59. static void __disable_clocks(struct msm_iommu_drvdata *drvdata)
  60. {
  61. if (drvdata->clk)
  62. clk_disable(drvdata->clk);
  63. clk_disable(drvdata->pclk);
  64. }
  65. static int __flush_iotlb(struct iommu_domain *domain)
  66. {
  67. struct msm_priv *priv = domain->priv;
  68. struct msm_iommu_drvdata *iommu_drvdata;
  69. struct msm_iommu_ctx_drvdata *ctx_drvdata;
  70. int ret = 0;
  71. #ifndef CONFIG_IOMMU_PGTABLES_L2
  72. unsigned long *fl_table = priv->pgtable;
  73. int i;
  74. if (!list_empty(&priv->list_attached)) {
  75. dmac_flush_range(fl_table, fl_table + SZ_16K);
  76. for (i = 0; i < NUM_FL_PTE; i++)
  77. if ((fl_table[i] & 0x03) == FL_TYPE_TABLE) {
  78. void *sl_table = __va(fl_table[i] &
  79. FL_BASE_MASK);
  80. dmac_flush_range(sl_table, sl_table + SZ_4K);
  81. }
  82. }
  83. #endif
  84. list_for_each_entry(ctx_drvdata, &priv->list_attached, attached_elm) {
  85. if (!ctx_drvdata->pdev || !ctx_drvdata->pdev->dev.parent)
  86. BUG();
  87. iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent);
  88. BUG_ON(!iommu_drvdata);
  89. ret = __enable_clocks(iommu_drvdata);
  90. if (ret)
  91. goto fail;
  92. SET_CTX_TLBIALL(iommu_drvdata->base, ctx_drvdata->num, 0);
  93. __disable_clocks(iommu_drvdata);
  94. }
  95. fail:
  96. return ret;
  97. }
  98. static void __reset_context(void __iomem *base, int ctx)
  99. {
  100. SET_BPRCOSH(base, ctx, 0);
  101. SET_BPRCISH(base, ctx, 0);
  102. SET_BPRCNSH(base, ctx, 0);
  103. SET_BPSHCFG(base, ctx, 0);
  104. SET_BPMTCFG(base, ctx, 0);
  105. SET_ACTLR(base, ctx, 0);
  106. SET_SCTLR(base, ctx, 0);
  107. SET_FSRRESTORE(base, ctx, 0);
  108. SET_TTBR0(base, ctx, 0);
  109. SET_TTBR1(base, ctx, 0);
  110. SET_TTBCR(base, ctx, 0);
  111. SET_BFBCR(base, ctx, 0);
  112. SET_PAR(base, ctx, 0);
  113. SET_FAR(base, ctx, 0);
  114. SET_CTX_TLBIALL(base, ctx, 0);
  115. SET_TLBFLPTER(base, ctx, 0);
  116. SET_TLBSLPTER(base, ctx, 0);
  117. SET_TLBLKCR(base, ctx, 0);
  118. SET_PRRR(base, ctx, 0);
  119. SET_NMRR(base, ctx, 0);
  120. }
  121. static void __program_context(void __iomem *base, int ctx, phys_addr_t pgtable)
  122. {
  123. unsigned int prrr, nmrr;
  124. __reset_context(base, ctx);
  125. /* Set up HTW mode */
  126. /* TLB miss configuration: perform HTW on miss */
  127. SET_TLBMCFG(base, ctx, 0x3);
  128. /* V2P configuration: HTW for access */
  129. SET_V2PCFG(base, ctx, 0x3);
  130. SET_TTBCR(base, ctx, 0);
  131. SET_TTBR0_PA(base, ctx, (pgtable >> 14));
  132. /* Invalidate the TLB for this context */
  133. SET_CTX_TLBIALL(base, ctx, 0);
  134. /* Set interrupt number to "secure" interrupt */
  135. SET_IRPTNDX(base, ctx, 0);
  136. /* Enable context fault interrupt */
  137. SET_CFEIE(base, ctx, 1);
  138. /* Stall access on a context fault and let the handler deal with it */
  139. SET_CFCFG(base, ctx, 1);
  140. /* Redirect all cacheable requests to L2 slave port. */
  141. SET_RCISH(base, ctx, 1);
  142. SET_RCOSH(base, ctx, 1);
  143. SET_RCNSH(base, ctx, 1);
  144. /* Turn on TEX Remap */
  145. SET_TRE(base, ctx, 1);
  146. /* Set TEX remap attributes */
  147. RCP15_PRRR(prrr);
  148. RCP15_NMRR(nmrr);
  149. SET_PRRR(base, ctx, prrr);
  150. SET_NMRR(base, ctx, nmrr);
  151. /* Turn on BFB prefetch */
  152. SET_BFBDFE(base, ctx, 1);
  153. #ifdef CONFIG_IOMMU_PGTABLES_L2
  154. /* Configure page tables as inner-cacheable and shareable to reduce
  155. * the TLB miss penalty.
  156. */
  157. SET_TTBR0_SH(base, ctx, 1);
  158. SET_TTBR1_SH(base, ctx, 1);
  159. SET_TTBR0_NOS(base, ctx, 1);
  160. SET_TTBR1_NOS(base, ctx, 1);
  161. SET_TTBR0_IRGNH(base, ctx, 0); /* WB, WA */
  162. SET_TTBR0_IRGNL(base, ctx, 1);
  163. SET_TTBR1_IRGNH(base, ctx, 0); /* WB, WA */
  164. SET_TTBR1_IRGNL(base, ctx, 1);
  165. SET_TTBR0_ORGN(base, ctx, 1); /* WB, WA */
  166. SET_TTBR1_ORGN(base, ctx, 1); /* WB, WA */
  167. #endif
  168. /* Enable the MMU */
  169. SET_M(base, ctx, 1);
  170. }
  171. static int msm_iommu_domain_init(struct iommu_domain *domain)
  172. {
  173. struct msm_priv *priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  174. if (!priv)
  175. goto fail_nomem;
  176. INIT_LIST_HEAD(&priv->list_attached);
  177. priv->pgtable = (unsigned long *)__get_free_pages(GFP_KERNEL,
  178. get_order(SZ_16K));
  179. if (!priv->pgtable)
  180. goto fail_nomem;
  181. memset(priv->pgtable, 0, SZ_16K);
  182. domain->priv = priv;
  183. return 0;
  184. fail_nomem:
  185. kfree(priv);
  186. return -ENOMEM;
  187. }
  188. static void msm_iommu_domain_destroy(struct iommu_domain *domain)
  189. {
  190. struct msm_priv *priv;
  191. unsigned long flags;
  192. unsigned long *fl_table;
  193. int i;
  194. spin_lock_irqsave(&msm_iommu_lock, flags);
  195. priv = domain->priv;
  196. domain->priv = NULL;
  197. if (priv) {
  198. fl_table = priv->pgtable;
  199. for (i = 0; i < NUM_FL_PTE; i++)
  200. if ((fl_table[i] & 0x03) == FL_TYPE_TABLE)
  201. free_page((unsigned long) __va(((fl_table[i]) &
  202. FL_BASE_MASK)));
  203. free_pages((unsigned long)priv->pgtable, get_order(SZ_16K));
  204. priv->pgtable = NULL;
  205. }
  206. kfree(priv);
  207. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  208. }
  209. static int msm_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
  210. {
  211. struct msm_priv *priv;
  212. struct msm_iommu_ctx_dev *ctx_dev;
  213. struct msm_iommu_drvdata *iommu_drvdata;
  214. struct msm_iommu_ctx_drvdata *ctx_drvdata;
  215. struct msm_iommu_ctx_drvdata *tmp_drvdata;
  216. int ret = 0;
  217. unsigned long flags;
  218. spin_lock_irqsave(&msm_iommu_lock, flags);
  219. priv = domain->priv;
  220. if (!priv || !dev) {
  221. ret = -EINVAL;
  222. goto fail;
  223. }
  224. iommu_drvdata = dev_get_drvdata(dev->parent);
  225. ctx_drvdata = dev_get_drvdata(dev);
  226. ctx_dev = dev->platform_data;
  227. if (!iommu_drvdata || !ctx_drvdata || !ctx_dev) {
  228. ret = -EINVAL;
  229. goto fail;
  230. }
  231. if (!list_empty(&ctx_drvdata->attached_elm)) {
  232. ret = -EBUSY;
  233. goto fail;
  234. }
  235. list_for_each_entry(tmp_drvdata, &priv->list_attached, attached_elm)
  236. if (tmp_drvdata == ctx_drvdata) {
  237. ret = -EBUSY;
  238. goto fail;
  239. }
  240. ret = __enable_clocks(iommu_drvdata);
  241. if (ret)
  242. goto fail;
  243. __program_context(iommu_drvdata->base, ctx_dev->num,
  244. __pa(priv->pgtable));
  245. __disable_clocks(iommu_drvdata);
  246. list_add(&(ctx_drvdata->attached_elm), &priv->list_attached);
  247. ret = __flush_iotlb(domain);
  248. fail:
  249. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  250. return ret;
  251. }
  252. static void msm_iommu_detach_dev(struct iommu_domain *domain,
  253. struct device *dev)
  254. {
  255. struct msm_priv *priv;
  256. struct msm_iommu_ctx_dev *ctx_dev;
  257. struct msm_iommu_drvdata *iommu_drvdata;
  258. struct msm_iommu_ctx_drvdata *ctx_drvdata;
  259. unsigned long flags;
  260. int ret;
  261. spin_lock_irqsave(&msm_iommu_lock, flags);
  262. priv = domain->priv;
  263. if (!priv || !dev)
  264. goto fail;
  265. iommu_drvdata = dev_get_drvdata(dev->parent);
  266. ctx_drvdata = dev_get_drvdata(dev);
  267. ctx_dev = dev->platform_data;
  268. if (!iommu_drvdata || !ctx_drvdata || !ctx_dev)
  269. goto fail;
  270. ret = __flush_iotlb(domain);
  271. if (ret)
  272. goto fail;
  273. ret = __enable_clocks(iommu_drvdata);
  274. if (ret)
  275. goto fail;
  276. __reset_context(iommu_drvdata->base, ctx_dev->num);
  277. __disable_clocks(iommu_drvdata);
  278. list_del_init(&ctx_drvdata->attached_elm);
  279. fail:
  280. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  281. }
  282. static int msm_iommu_map(struct iommu_domain *domain, unsigned long va,
  283. phys_addr_t pa, int order, int prot)
  284. {
  285. struct msm_priv *priv;
  286. unsigned long flags;
  287. unsigned long *fl_table;
  288. unsigned long *fl_pte;
  289. unsigned long fl_offset;
  290. unsigned long *sl_table;
  291. unsigned long *sl_pte;
  292. unsigned long sl_offset;
  293. unsigned int pgprot;
  294. size_t len = 0x1000UL << order;
  295. int ret = 0, tex, sh;
  296. spin_lock_irqsave(&msm_iommu_lock, flags);
  297. sh = (prot & MSM_IOMMU_ATTR_SH) ? 1 : 0;
  298. tex = msm_iommu_tex_class[prot & MSM_IOMMU_CP_MASK];
  299. if (tex < 0 || tex > NUM_TEX_CLASS - 1) {
  300. ret = -EINVAL;
  301. goto fail;
  302. }
  303. priv = domain->priv;
  304. if (!priv) {
  305. ret = -EINVAL;
  306. goto fail;
  307. }
  308. fl_table = priv->pgtable;
  309. if (len != SZ_16M && len != SZ_1M &&
  310. len != SZ_64K && len != SZ_4K) {
  311. pr_debug("Bad size: %d\n", len);
  312. ret = -EINVAL;
  313. goto fail;
  314. }
  315. if (!fl_table) {
  316. pr_debug("Null page table\n");
  317. ret = -EINVAL;
  318. goto fail;
  319. }
  320. if (len == SZ_16M || len == SZ_1M) {
  321. pgprot = sh ? FL_SHARED : 0;
  322. pgprot |= tex & 0x01 ? FL_BUFFERABLE : 0;
  323. pgprot |= tex & 0x02 ? FL_CACHEABLE : 0;
  324. pgprot |= tex & 0x04 ? FL_TEX0 : 0;
  325. } else {
  326. pgprot = sh ? SL_SHARED : 0;
  327. pgprot |= tex & 0x01 ? SL_BUFFERABLE : 0;
  328. pgprot |= tex & 0x02 ? SL_CACHEABLE : 0;
  329. pgprot |= tex & 0x04 ? SL_TEX0 : 0;
  330. }
  331. fl_offset = FL_OFFSET(va); /* Upper 12 bits */
  332. fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */
  333. if (len == SZ_16M) {
  334. int i = 0;
  335. for (i = 0; i < 16; i++)
  336. *(fl_pte+i) = (pa & 0xFF000000) | FL_SUPERSECTION |
  337. FL_AP_READ | FL_AP_WRITE | FL_TYPE_SECT |
  338. FL_SHARED | FL_NG | pgprot;
  339. }
  340. if (len == SZ_1M)
  341. *fl_pte = (pa & 0xFFF00000) | FL_AP_READ | FL_AP_WRITE | FL_NG |
  342. FL_TYPE_SECT | FL_SHARED | pgprot;
  343. /* Need a 2nd level table */
  344. if ((len == SZ_4K || len == SZ_64K) && (*fl_pte) == 0) {
  345. unsigned long *sl;
  346. sl = (unsigned long *) __get_free_pages(GFP_ATOMIC,
  347. get_order(SZ_4K));
  348. if (!sl) {
  349. pr_debug("Could not allocate second level table\n");
  350. ret = -ENOMEM;
  351. goto fail;
  352. }
  353. memset(sl, 0, SZ_4K);
  354. *fl_pte = ((((int)__pa(sl)) & FL_BASE_MASK) | FL_TYPE_TABLE);
  355. }
  356. sl_table = (unsigned long *) __va(((*fl_pte) & FL_BASE_MASK));
  357. sl_offset = SL_OFFSET(va);
  358. sl_pte = sl_table + sl_offset;
  359. if (len == SZ_4K)
  360. *sl_pte = (pa & SL_BASE_MASK_SMALL) | SL_AP0 | SL_AP1 | SL_NG |
  361. SL_SHARED | SL_TYPE_SMALL | pgprot;
  362. if (len == SZ_64K) {
  363. int i;
  364. for (i = 0; i < 16; i++)
  365. *(sl_pte+i) = (pa & SL_BASE_MASK_LARGE) | SL_AP0 |
  366. SL_NG | SL_AP1 | SL_SHARED | SL_TYPE_LARGE | pgprot;
  367. }
  368. ret = __flush_iotlb(domain);
  369. fail:
  370. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  371. return ret;
  372. }
  373. static int msm_iommu_unmap(struct iommu_domain *domain, unsigned long va,
  374. int order)
  375. {
  376. struct msm_priv *priv;
  377. unsigned long flags;
  378. unsigned long *fl_table;
  379. unsigned long *fl_pte;
  380. unsigned long fl_offset;
  381. unsigned long *sl_table;
  382. unsigned long *sl_pte;
  383. unsigned long sl_offset;
  384. size_t len = 0x1000UL << order;
  385. int i, ret = 0;
  386. spin_lock_irqsave(&msm_iommu_lock, flags);
  387. priv = domain->priv;
  388. if (!priv) {
  389. ret = -ENODEV;
  390. goto fail;
  391. }
  392. fl_table = priv->pgtable;
  393. if (len != SZ_16M && len != SZ_1M &&
  394. len != SZ_64K && len != SZ_4K) {
  395. pr_debug("Bad length: %d\n", len);
  396. ret = -EINVAL;
  397. goto fail;
  398. }
  399. if (!fl_table) {
  400. pr_debug("Null page table\n");
  401. ret = -EINVAL;
  402. goto fail;
  403. }
  404. fl_offset = FL_OFFSET(va); /* Upper 12 bits */
  405. fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */
  406. if (*fl_pte == 0) {
  407. pr_debug("First level PTE is 0\n");
  408. ret = -ENODEV;
  409. goto fail;
  410. }
  411. /* Unmap supersection */
  412. if (len == SZ_16M)
  413. for (i = 0; i < 16; i++)
  414. *(fl_pte+i) = 0;
  415. if (len == SZ_1M)
  416. *fl_pte = 0;
  417. sl_table = (unsigned long *) __va(((*fl_pte) & FL_BASE_MASK));
  418. sl_offset = SL_OFFSET(va);
  419. sl_pte = sl_table + sl_offset;
  420. if (len == SZ_64K) {
  421. for (i = 0; i < 16; i++)
  422. *(sl_pte+i) = 0;
  423. }
  424. if (len == SZ_4K)
  425. *sl_pte = 0;
  426. if (len == SZ_4K || len == SZ_64K) {
  427. int used = 0;
  428. for (i = 0; i < NUM_SL_PTE; i++)
  429. if (sl_table[i])
  430. used = 1;
  431. if (!used) {
  432. free_page((unsigned long)sl_table);
  433. *fl_pte = 0;
  434. }
  435. }
  436. ret = __flush_iotlb(domain);
  437. fail:
  438. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  439. return ret;
  440. }
  441. static phys_addr_t msm_iommu_iova_to_phys(struct iommu_domain *domain,
  442. unsigned long va)
  443. {
  444. struct msm_priv *priv;
  445. struct msm_iommu_drvdata *iommu_drvdata;
  446. struct msm_iommu_ctx_drvdata *ctx_drvdata;
  447. unsigned int par;
  448. unsigned long flags;
  449. void __iomem *base;
  450. phys_addr_t ret = 0;
  451. int ctx;
  452. spin_lock_irqsave(&msm_iommu_lock, flags);
  453. priv = domain->priv;
  454. if (list_empty(&priv->list_attached))
  455. goto fail;
  456. ctx_drvdata = list_entry(priv->list_attached.next,
  457. struct msm_iommu_ctx_drvdata, attached_elm);
  458. iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent);
  459. base = iommu_drvdata->base;
  460. ctx = ctx_drvdata->num;
  461. ret = __enable_clocks(iommu_drvdata);
  462. if (ret)
  463. goto fail;
  464. /* Invalidate context TLB */
  465. SET_CTX_TLBIALL(base, ctx, 0);
  466. SET_V2PPR(base, ctx, va & V2Pxx_VA);
  467. par = GET_PAR(base, ctx);
  468. /* We are dealing with a supersection */
  469. if (GET_NOFAULT_SS(base, ctx))
  470. ret = (par & 0xFF000000) | (va & 0x00FFFFFF);
  471. else /* Upper 20 bits from PAR, lower 12 from VA */
  472. ret = (par & 0xFFFFF000) | (va & 0x00000FFF);
  473. if (GET_FAULT(base, ctx))
  474. ret = 0;
  475. __disable_clocks(iommu_drvdata);
  476. fail:
  477. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  478. return ret;
  479. }
  480. static int msm_iommu_domain_has_cap(struct iommu_domain *domain,
  481. unsigned long cap)
  482. {
  483. return 0;
  484. }
  485. static void print_ctx_regs(void __iomem *base, int ctx)
  486. {
  487. unsigned int fsr = GET_FSR(base, ctx);
  488. pr_err("FAR = %08x PAR = %08x\n",
  489. GET_FAR(base, ctx), GET_PAR(base, ctx));
  490. pr_err("FSR = %08x [%s%s%s%s%s%s%s%s%s%s]\n", fsr,
  491. (fsr & 0x02) ? "TF " : "",
  492. (fsr & 0x04) ? "AFF " : "",
  493. (fsr & 0x08) ? "APF " : "",
  494. (fsr & 0x10) ? "TLBMF " : "",
  495. (fsr & 0x20) ? "HTWDEEF " : "",
  496. (fsr & 0x40) ? "HTWSEEF " : "",
  497. (fsr & 0x80) ? "MHF " : "",
  498. (fsr & 0x10000) ? "SL " : "",
  499. (fsr & 0x40000000) ? "SS " : "",
  500. (fsr & 0x80000000) ? "MULTI " : "");
  501. pr_err("FSYNR0 = %08x FSYNR1 = %08x\n",
  502. GET_FSYNR0(base, ctx), GET_FSYNR1(base, ctx));
  503. pr_err("TTBR0 = %08x TTBR1 = %08x\n",
  504. GET_TTBR0(base, ctx), GET_TTBR1(base, ctx));
  505. pr_err("SCTLR = %08x ACTLR = %08x\n",
  506. GET_SCTLR(base, ctx), GET_ACTLR(base, ctx));
  507. pr_err("PRRR = %08x NMRR = %08x\n",
  508. GET_PRRR(base, ctx), GET_NMRR(base, ctx));
  509. }
  510. irqreturn_t msm_iommu_fault_handler(int irq, void *dev_id)
  511. {
  512. struct msm_iommu_drvdata *drvdata = dev_id;
  513. void __iomem *base;
  514. unsigned int fsr;
  515. int i, ret;
  516. spin_lock(&msm_iommu_lock);
  517. if (!drvdata) {
  518. pr_err("Invalid device ID in context interrupt handler\n");
  519. goto fail;
  520. }
  521. base = drvdata->base;
  522. pr_err("Unexpected IOMMU page fault!\n");
  523. pr_err("base = %08x\n", (unsigned int) base);
  524. ret = __enable_clocks(drvdata);
  525. if (ret)
  526. goto fail;
  527. for (i = 0; i < drvdata->ncb; i++) {
  528. fsr = GET_FSR(base, i);
  529. if (fsr) {
  530. pr_err("Fault occurred in context %d.\n", i);
  531. pr_err("Interesting registers:\n");
  532. print_ctx_regs(base, i);
  533. SET_FSR(base, i, 0x4000000F);
  534. }
  535. }
  536. __disable_clocks(drvdata);
  537. fail:
  538. spin_unlock(&msm_iommu_lock);
  539. return 0;
  540. }
  541. static struct iommu_ops msm_iommu_ops = {
  542. .domain_init = msm_iommu_domain_init,
  543. .domain_destroy = msm_iommu_domain_destroy,
  544. .attach_dev = msm_iommu_attach_dev,
  545. .detach_dev = msm_iommu_detach_dev,
  546. .map = msm_iommu_map,
  547. .unmap = msm_iommu_unmap,
  548. .iova_to_phys = msm_iommu_iova_to_phys,
  549. .domain_has_cap = msm_iommu_domain_has_cap
  550. };
  551. static int __init get_tex_class(int icp, int ocp, int mt, int nos)
  552. {
  553. int i = 0;
  554. unsigned int prrr = 0;
  555. unsigned int nmrr = 0;
  556. int c_icp, c_ocp, c_mt, c_nos;
  557. RCP15_PRRR(prrr);
  558. RCP15_NMRR(nmrr);
  559. for (i = 0; i < NUM_TEX_CLASS; i++) {
  560. c_nos = PRRR_NOS(prrr, i);
  561. c_mt = PRRR_MT(prrr, i);
  562. c_icp = NMRR_ICP(nmrr, i);
  563. c_ocp = NMRR_OCP(nmrr, i);
  564. if (icp == c_icp && ocp == c_ocp && c_mt == mt && c_nos == nos)
  565. return i;
  566. }
  567. return -ENODEV;
  568. }
  569. static void __init setup_iommu_tex_classes(void)
  570. {
  571. msm_iommu_tex_class[MSM_IOMMU_ATTR_NONCACHED] =
  572. get_tex_class(CP_NONCACHED, CP_NONCACHED, MT_NORMAL, 1);
  573. msm_iommu_tex_class[MSM_IOMMU_ATTR_CACHED_WB_WA] =
  574. get_tex_class(CP_WB_WA, CP_WB_WA, MT_NORMAL, 1);
  575. msm_iommu_tex_class[MSM_IOMMU_ATTR_CACHED_WB_NWA] =
  576. get_tex_class(CP_WB_NWA, CP_WB_NWA, MT_NORMAL, 1);
  577. msm_iommu_tex_class[MSM_IOMMU_ATTR_CACHED_WT] =
  578. get_tex_class(CP_WT, CP_WT, MT_NORMAL, 1);
  579. }
  580. static int __init msm_iommu_init(void)
  581. {
  582. setup_iommu_tex_classes();
  583. register_iommu(&msm_iommu_ops);
  584. return 0;
  585. }
  586. subsys_initcall(msm_iommu_init);
  587. MODULE_LICENSE("GPL v2");
  588. MODULE_AUTHOR("Stepan Moskovchenko <stepanm@codeaurora.org>");