msm_iommu.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. /* bitmap of the page sizes currently supported */
  40. #define MSM_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
  41. static int msm_iommu_tex_class[4];
  42. DEFINE_SPINLOCK(msm_iommu_lock);
  43. struct msm_priv {
  44. unsigned long *pgtable;
  45. struct list_head list_attached;
  46. };
  47. static int __enable_clocks(struct msm_iommu_drvdata *drvdata)
  48. {
  49. int ret;
  50. ret = clk_enable(drvdata->pclk);
  51. if (ret)
  52. goto fail;
  53. if (drvdata->clk) {
  54. ret = clk_enable(drvdata->clk);
  55. if (ret)
  56. clk_disable(drvdata->pclk);
  57. }
  58. fail:
  59. return ret;
  60. }
  61. static void __disable_clocks(struct msm_iommu_drvdata *drvdata)
  62. {
  63. if (drvdata->clk)
  64. clk_disable(drvdata->clk);
  65. clk_disable(drvdata->pclk);
  66. }
  67. static int __flush_iotlb(struct iommu_domain *domain)
  68. {
  69. struct msm_priv *priv = domain->priv;
  70. struct msm_iommu_drvdata *iommu_drvdata;
  71. struct msm_iommu_ctx_drvdata *ctx_drvdata;
  72. int ret = 0;
  73. #ifndef CONFIG_IOMMU_PGTABLES_L2
  74. unsigned long *fl_table = priv->pgtable;
  75. int i;
  76. if (!list_empty(&priv->list_attached)) {
  77. dmac_flush_range(fl_table, fl_table + SZ_16K);
  78. for (i = 0; i < NUM_FL_PTE; i++)
  79. if ((fl_table[i] & 0x03) == FL_TYPE_TABLE) {
  80. void *sl_table = __va(fl_table[i] &
  81. FL_BASE_MASK);
  82. dmac_flush_range(sl_table, sl_table + SZ_4K);
  83. }
  84. }
  85. #endif
  86. list_for_each_entry(ctx_drvdata, &priv->list_attached, attached_elm) {
  87. if (!ctx_drvdata->pdev || !ctx_drvdata->pdev->dev.parent)
  88. BUG();
  89. iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent);
  90. BUG_ON(!iommu_drvdata);
  91. ret = __enable_clocks(iommu_drvdata);
  92. if (ret)
  93. goto fail;
  94. SET_CTX_TLBIALL(iommu_drvdata->base, ctx_drvdata->num, 0);
  95. __disable_clocks(iommu_drvdata);
  96. }
  97. fail:
  98. return ret;
  99. }
  100. static void __reset_context(void __iomem *base, int ctx)
  101. {
  102. SET_BPRCOSH(base, ctx, 0);
  103. SET_BPRCISH(base, ctx, 0);
  104. SET_BPRCNSH(base, ctx, 0);
  105. SET_BPSHCFG(base, ctx, 0);
  106. SET_BPMTCFG(base, ctx, 0);
  107. SET_ACTLR(base, ctx, 0);
  108. SET_SCTLR(base, ctx, 0);
  109. SET_FSRRESTORE(base, ctx, 0);
  110. SET_TTBR0(base, ctx, 0);
  111. SET_TTBR1(base, ctx, 0);
  112. SET_TTBCR(base, ctx, 0);
  113. SET_BFBCR(base, ctx, 0);
  114. SET_PAR(base, ctx, 0);
  115. SET_FAR(base, ctx, 0);
  116. SET_CTX_TLBIALL(base, ctx, 0);
  117. SET_TLBFLPTER(base, ctx, 0);
  118. SET_TLBSLPTER(base, ctx, 0);
  119. SET_TLBLKCR(base, ctx, 0);
  120. SET_PRRR(base, ctx, 0);
  121. SET_NMRR(base, ctx, 0);
  122. }
  123. static void __program_context(void __iomem *base, int ctx, phys_addr_t pgtable)
  124. {
  125. unsigned int prrr, nmrr;
  126. __reset_context(base, ctx);
  127. /* Set up HTW mode */
  128. /* TLB miss configuration: perform HTW on miss */
  129. SET_TLBMCFG(base, ctx, 0x3);
  130. /* V2P configuration: HTW for access */
  131. SET_V2PCFG(base, ctx, 0x3);
  132. SET_TTBCR(base, ctx, 0);
  133. SET_TTBR0_PA(base, ctx, (pgtable >> 14));
  134. /* Invalidate the TLB for this context */
  135. SET_CTX_TLBIALL(base, ctx, 0);
  136. /* Set interrupt number to "secure" interrupt */
  137. SET_IRPTNDX(base, ctx, 0);
  138. /* Enable context fault interrupt */
  139. SET_CFEIE(base, ctx, 1);
  140. /* Stall access on a context fault and let the handler deal with it */
  141. SET_CFCFG(base, ctx, 1);
  142. /* Redirect all cacheable requests to L2 slave port. */
  143. SET_RCISH(base, ctx, 1);
  144. SET_RCOSH(base, ctx, 1);
  145. SET_RCNSH(base, ctx, 1);
  146. /* Turn on TEX Remap */
  147. SET_TRE(base, ctx, 1);
  148. /* Set TEX remap attributes */
  149. RCP15_PRRR(prrr);
  150. RCP15_NMRR(nmrr);
  151. SET_PRRR(base, ctx, prrr);
  152. SET_NMRR(base, ctx, nmrr);
  153. /* Turn on BFB prefetch */
  154. SET_BFBDFE(base, ctx, 1);
  155. #ifdef CONFIG_IOMMU_PGTABLES_L2
  156. /* Configure page tables as inner-cacheable and shareable to reduce
  157. * the TLB miss penalty.
  158. */
  159. SET_TTBR0_SH(base, ctx, 1);
  160. SET_TTBR1_SH(base, ctx, 1);
  161. SET_TTBR0_NOS(base, ctx, 1);
  162. SET_TTBR1_NOS(base, ctx, 1);
  163. SET_TTBR0_IRGNH(base, ctx, 0); /* WB, WA */
  164. SET_TTBR0_IRGNL(base, ctx, 1);
  165. SET_TTBR1_IRGNH(base, ctx, 0); /* WB, WA */
  166. SET_TTBR1_IRGNL(base, ctx, 1);
  167. SET_TTBR0_ORGN(base, ctx, 1); /* WB, WA */
  168. SET_TTBR1_ORGN(base, ctx, 1); /* WB, WA */
  169. #endif
  170. /* Enable the MMU */
  171. SET_M(base, ctx, 1);
  172. }
  173. static int msm_iommu_domain_init(struct iommu_domain *domain)
  174. {
  175. struct msm_priv *priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  176. if (!priv)
  177. goto fail_nomem;
  178. INIT_LIST_HEAD(&priv->list_attached);
  179. priv->pgtable = (unsigned long *)__get_free_pages(GFP_KERNEL,
  180. get_order(SZ_16K));
  181. if (!priv->pgtable)
  182. goto fail_nomem;
  183. memset(priv->pgtable, 0, SZ_16K);
  184. domain->priv = priv;
  185. domain->geometry.aperture_start = 0;
  186. domain->geometry.aperture_end = (1ULL << 32) - 1;
  187. domain->geometry.force_aperture = true;
  188. return 0;
  189. fail_nomem:
  190. kfree(priv);
  191. return -ENOMEM;
  192. }
  193. static void msm_iommu_domain_destroy(struct iommu_domain *domain)
  194. {
  195. struct msm_priv *priv;
  196. unsigned long flags;
  197. unsigned long *fl_table;
  198. int i;
  199. spin_lock_irqsave(&msm_iommu_lock, flags);
  200. priv = domain->priv;
  201. domain->priv = NULL;
  202. if (priv) {
  203. fl_table = priv->pgtable;
  204. for (i = 0; i < NUM_FL_PTE; i++)
  205. if ((fl_table[i] & 0x03) == FL_TYPE_TABLE)
  206. free_page((unsigned long) __va(((fl_table[i]) &
  207. FL_BASE_MASK)));
  208. free_pages((unsigned long)priv->pgtable, get_order(SZ_16K));
  209. priv->pgtable = NULL;
  210. }
  211. kfree(priv);
  212. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  213. }
  214. static int msm_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
  215. {
  216. struct msm_priv *priv;
  217. struct msm_iommu_ctx_dev *ctx_dev;
  218. struct msm_iommu_drvdata *iommu_drvdata;
  219. struct msm_iommu_ctx_drvdata *ctx_drvdata;
  220. struct msm_iommu_ctx_drvdata *tmp_drvdata;
  221. int ret = 0;
  222. unsigned long flags;
  223. spin_lock_irqsave(&msm_iommu_lock, flags);
  224. priv = domain->priv;
  225. if (!priv || !dev) {
  226. ret = -EINVAL;
  227. goto fail;
  228. }
  229. iommu_drvdata = dev_get_drvdata(dev->parent);
  230. ctx_drvdata = dev_get_drvdata(dev);
  231. ctx_dev = dev->platform_data;
  232. if (!iommu_drvdata || !ctx_drvdata || !ctx_dev) {
  233. ret = -EINVAL;
  234. goto fail;
  235. }
  236. if (!list_empty(&ctx_drvdata->attached_elm)) {
  237. ret = -EBUSY;
  238. goto fail;
  239. }
  240. list_for_each_entry(tmp_drvdata, &priv->list_attached, attached_elm)
  241. if (tmp_drvdata == ctx_drvdata) {
  242. ret = -EBUSY;
  243. goto fail;
  244. }
  245. ret = __enable_clocks(iommu_drvdata);
  246. if (ret)
  247. goto fail;
  248. __program_context(iommu_drvdata->base, ctx_dev->num,
  249. __pa(priv->pgtable));
  250. __disable_clocks(iommu_drvdata);
  251. list_add(&(ctx_drvdata->attached_elm), &priv->list_attached);
  252. ret = __flush_iotlb(domain);
  253. fail:
  254. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  255. return ret;
  256. }
  257. static void msm_iommu_detach_dev(struct iommu_domain *domain,
  258. struct device *dev)
  259. {
  260. struct msm_priv *priv;
  261. struct msm_iommu_ctx_dev *ctx_dev;
  262. struct msm_iommu_drvdata *iommu_drvdata;
  263. struct msm_iommu_ctx_drvdata *ctx_drvdata;
  264. unsigned long flags;
  265. int ret;
  266. spin_lock_irqsave(&msm_iommu_lock, flags);
  267. priv = domain->priv;
  268. if (!priv || !dev)
  269. goto fail;
  270. iommu_drvdata = dev_get_drvdata(dev->parent);
  271. ctx_drvdata = dev_get_drvdata(dev);
  272. ctx_dev = dev->platform_data;
  273. if (!iommu_drvdata || !ctx_drvdata || !ctx_dev)
  274. goto fail;
  275. ret = __flush_iotlb(domain);
  276. if (ret)
  277. goto fail;
  278. ret = __enable_clocks(iommu_drvdata);
  279. if (ret)
  280. goto fail;
  281. __reset_context(iommu_drvdata->base, ctx_dev->num);
  282. __disable_clocks(iommu_drvdata);
  283. list_del_init(&ctx_drvdata->attached_elm);
  284. fail:
  285. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  286. }
  287. static int msm_iommu_map(struct iommu_domain *domain, unsigned long va,
  288. phys_addr_t pa, size_t len, int prot)
  289. {
  290. struct msm_priv *priv;
  291. unsigned long flags;
  292. unsigned long *fl_table;
  293. unsigned long *fl_pte;
  294. unsigned long fl_offset;
  295. unsigned long *sl_table;
  296. unsigned long *sl_pte;
  297. unsigned long sl_offset;
  298. unsigned int pgprot;
  299. int ret = 0, tex, sh;
  300. spin_lock_irqsave(&msm_iommu_lock, flags);
  301. sh = (prot & MSM_IOMMU_ATTR_SH) ? 1 : 0;
  302. tex = msm_iommu_tex_class[prot & MSM_IOMMU_CP_MASK];
  303. if (tex < 0 || tex > NUM_TEX_CLASS - 1) {
  304. ret = -EINVAL;
  305. goto fail;
  306. }
  307. priv = domain->priv;
  308. if (!priv) {
  309. ret = -EINVAL;
  310. goto fail;
  311. }
  312. fl_table = priv->pgtable;
  313. if (len != SZ_16M && len != SZ_1M &&
  314. len != SZ_64K && len != SZ_4K) {
  315. pr_debug("Bad size: %d\n", len);
  316. ret = -EINVAL;
  317. goto fail;
  318. }
  319. if (!fl_table) {
  320. pr_debug("Null page table\n");
  321. ret = -EINVAL;
  322. goto fail;
  323. }
  324. if (len == SZ_16M || len == SZ_1M) {
  325. pgprot = sh ? FL_SHARED : 0;
  326. pgprot |= tex & 0x01 ? FL_BUFFERABLE : 0;
  327. pgprot |= tex & 0x02 ? FL_CACHEABLE : 0;
  328. pgprot |= tex & 0x04 ? FL_TEX0 : 0;
  329. } else {
  330. pgprot = sh ? SL_SHARED : 0;
  331. pgprot |= tex & 0x01 ? SL_BUFFERABLE : 0;
  332. pgprot |= tex & 0x02 ? SL_CACHEABLE : 0;
  333. pgprot |= tex & 0x04 ? SL_TEX0 : 0;
  334. }
  335. fl_offset = FL_OFFSET(va); /* Upper 12 bits */
  336. fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */
  337. if (len == SZ_16M) {
  338. int i = 0;
  339. for (i = 0; i < 16; i++)
  340. *(fl_pte+i) = (pa & 0xFF000000) | FL_SUPERSECTION |
  341. FL_AP_READ | FL_AP_WRITE | FL_TYPE_SECT |
  342. FL_SHARED | FL_NG | pgprot;
  343. }
  344. if (len == SZ_1M)
  345. *fl_pte = (pa & 0xFFF00000) | FL_AP_READ | FL_AP_WRITE | FL_NG |
  346. FL_TYPE_SECT | FL_SHARED | pgprot;
  347. /* Need a 2nd level table */
  348. if ((len == SZ_4K || len == SZ_64K) && (*fl_pte) == 0) {
  349. unsigned long *sl;
  350. sl = (unsigned long *) __get_free_pages(GFP_ATOMIC,
  351. get_order(SZ_4K));
  352. if (!sl) {
  353. pr_debug("Could not allocate second level table\n");
  354. ret = -ENOMEM;
  355. goto fail;
  356. }
  357. memset(sl, 0, SZ_4K);
  358. *fl_pte = ((((int)__pa(sl)) & FL_BASE_MASK) | FL_TYPE_TABLE);
  359. }
  360. sl_table = (unsigned long *) __va(((*fl_pte) & FL_BASE_MASK));
  361. sl_offset = SL_OFFSET(va);
  362. sl_pte = sl_table + sl_offset;
  363. if (len == SZ_4K)
  364. *sl_pte = (pa & SL_BASE_MASK_SMALL) | SL_AP0 | SL_AP1 | SL_NG |
  365. SL_SHARED | SL_TYPE_SMALL | pgprot;
  366. if (len == SZ_64K) {
  367. int i;
  368. for (i = 0; i < 16; i++)
  369. *(sl_pte+i) = (pa & SL_BASE_MASK_LARGE) | SL_AP0 |
  370. SL_NG | SL_AP1 | SL_SHARED | SL_TYPE_LARGE | pgprot;
  371. }
  372. ret = __flush_iotlb(domain);
  373. fail:
  374. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  375. return ret;
  376. }
  377. static size_t msm_iommu_unmap(struct iommu_domain *domain, unsigned long va,
  378. size_t len)
  379. {
  380. struct msm_priv *priv;
  381. unsigned long flags;
  382. unsigned long *fl_table;
  383. unsigned long *fl_pte;
  384. unsigned long fl_offset;
  385. unsigned long *sl_table;
  386. unsigned long *sl_pte;
  387. unsigned long sl_offset;
  388. int i, ret = 0;
  389. spin_lock_irqsave(&msm_iommu_lock, flags);
  390. priv = domain->priv;
  391. if (!priv)
  392. goto fail;
  393. fl_table = priv->pgtable;
  394. if (len != SZ_16M && len != SZ_1M &&
  395. len != SZ_64K && len != SZ_4K) {
  396. pr_debug("Bad length: %d\n", len);
  397. goto fail;
  398. }
  399. if (!fl_table) {
  400. pr_debug("Null page table\n");
  401. goto fail;
  402. }
  403. fl_offset = FL_OFFSET(va); /* Upper 12 bits */
  404. fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */
  405. if (*fl_pte == 0) {
  406. pr_debug("First level PTE is 0\n");
  407. goto fail;
  408. }
  409. /* Unmap supersection */
  410. if (len == SZ_16M)
  411. for (i = 0; i < 16; i++)
  412. *(fl_pte+i) = 0;
  413. if (len == SZ_1M)
  414. *fl_pte = 0;
  415. sl_table = (unsigned long *) __va(((*fl_pte) & FL_BASE_MASK));
  416. sl_offset = SL_OFFSET(va);
  417. sl_pte = sl_table + sl_offset;
  418. if (len == SZ_64K) {
  419. for (i = 0; i < 16; i++)
  420. *(sl_pte+i) = 0;
  421. }
  422. if (len == SZ_4K)
  423. *sl_pte = 0;
  424. if (len == SZ_4K || len == SZ_64K) {
  425. int used = 0;
  426. for (i = 0; i < NUM_SL_PTE; i++)
  427. if (sl_table[i])
  428. used = 1;
  429. if (!used) {
  430. free_page((unsigned long)sl_table);
  431. *fl_pte = 0;
  432. }
  433. }
  434. ret = __flush_iotlb(domain);
  435. fail:
  436. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  437. /* the IOMMU API requires us to return how many bytes were unmapped */
  438. len = ret ? 0 : len;
  439. return len;
  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. .pgsize_bitmap = MSM_IOMMU_PGSIZES,
  551. };
  552. static int __init get_tex_class(int icp, int ocp, int mt, int nos)
  553. {
  554. int i = 0;
  555. unsigned int prrr = 0;
  556. unsigned int nmrr = 0;
  557. int c_icp, c_ocp, c_mt, c_nos;
  558. RCP15_PRRR(prrr);
  559. RCP15_NMRR(nmrr);
  560. for (i = 0; i < NUM_TEX_CLASS; i++) {
  561. c_nos = PRRR_NOS(prrr, i);
  562. c_mt = PRRR_MT(prrr, i);
  563. c_icp = NMRR_ICP(nmrr, i);
  564. c_ocp = NMRR_OCP(nmrr, i);
  565. if (icp == c_icp && ocp == c_ocp && c_mt == mt && c_nos == nos)
  566. return i;
  567. }
  568. return -ENODEV;
  569. }
  570. static void __init setup_iommu_tex_classes(void)
  571. {
  572. msm_iommu_tex_class[MSM_IOMMU_ATTR_NONCACHED] =
  573. get_tex_class(CP_NONCACHED, CP_NONCACHED, MT_NORMAL, 1);
  574. msm_iommu_tex_class[MSM_IOMMU_ATTR_CACHED_WB_WA] =
  575. get_tex_class(CP_WB_WA, CP_WB_WA, MT_NORMAL, 1);
  576. msm_iommu_tex_class[MSM_IOMMU_ATTR_CACHED_WB_NWA] =
  577. get_tex_class(CP_WB_NWA, CP_WB_NWA, MT_NORMAL, 1);
  578. msm_iommu_tex_class[MSM_IOMMU_ATTR_CACHED_WT] =
  579. get_tex_class(CP_WT, CP_WT, MT_NORMAL, 1);
  580. }
  581. static int __init msm_iommu_init(void)
  582. {
  583. setup_iommu_tex_classes();
  584. bus_set_iommu(&platform_bus_type, &msm_iommu_ops);
  585. return 0;
  586. }
  587. subsys_initcall(msm_iommu_init);
  588. MODULE_LICENSE("GPL v2");
  589. MODULE_AUTHOR("Stepan Moskovchenko <stepanm@codeaurora.org>");