msm_iommu.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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 "msm_iommu_hw-8xxx.h"
  32. #include "msm_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. clk_disable(drvdata->clk);
  64. clk_disable(drvdata->pclk);
  65. }
  66. static int __flush_iotlb(struct iommu_domain *domain)
  67. {
  68. struct msm_priv *priv = domain->priv;
  69. struct msm_iommu_drvdata *iommu_drvdata;
  70. struct msm_iommu_ctx_drvdata *ctx_drvdata;
  71. int ret = 0;
  72. #ifndef CONFIG_IOMMU_PGTABLES_L2
  73. unsigned long *fl_table = priv->pgtable;
  74. int i;
  75. if (!list_empty(&priv->list_attached)) {
  76. dmac_flush_range(fl_table, fl_table + SZ_16K);
  77. for (i = 0; i < NUM_FL_PTE; i++)
  78. if ((fl_table[i] & 0x03) == FL_TYPE_TABLE) {
  79. void *sl_table = __va(fl_table[i] &
  80. FL_BASE_MASK);
  81. dmac_flush_range(sl_table, sl_table + SZ_4K);
  82. }
  83. }
  84. #endif
  85. list_for_each_entry(ctx_drvdata, &priv->list_attached, attached_elm) {
  86. if (!ctx_drvdata->pdev || !ctx_drvdata->pdev->dev.parent)
  87. BUG();
  88. iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent);
  89. BUG_ON(!iommu_drvdata);
  90. ret = __enable_clocks(iommu_drvdata);
  91. if (ret)
  92. goto fail;
  93. SET_CTX_TLBIALL(iommu_drvdata->base, ctx_drvdata->num, 0);
  94. __disable_clocks(iommu_drvdata);
  95. }
  96. fail:
  97. return ret;
  98. }
  99. static void __reset_context(void __iomem *base, int ctx)
  100. {
  101. SET_BPRCOSH(base, ctx, 0);
  102. SET_BPRCISH(base, ctx, 0);
  103. SET_BPRCNSH(base, ctx, 0);
  104. SET_BPSHCFG(base, ctx, 0);
  105. SET_BPMTCFG(base, ctx, 0);
  106. SET_ACTLR(base, ctx, 0);
  107. SET_SCTLR(base, ctx, 0);
  108. SET_FSRRESTORE(base, ctx, 0);
  109. SET_TTBR0(base, ctx, 0);
  110. SET_TTBR1(base, ctx, 0);
  111. SET_TTBCR(base, ctx, 0);
  112. SET_BFBCR(base, ctx, 0);
  113. SET_PAR(base, ctx, 0);
  114. SET_FAR(base, ctx, 0);
  115. SET_CTX_TLBIALL(base, ctx, 0);
  116. SET_TLBFLPTER(base, ctx, 0);
  117. SET_TLBSLPTER(base, ctx, 0);
  118. SET_TLBLKCR(base, ctx, 0);
  119. SET_PRRR(base, ctx, 0);
  120. SET_NMRR(base, ctx, 0);
  121. }
  122. static void __program_context(void __iomem *base, int ctx, phys_addr_t pgtable)
  123. {
  124. unsigned int prrr, nmrr;
  125. __reset_context(base, ctx);
  126. /* Set up HTW mode */
  127. /* TLB miss configuration: perform HTW on miss */
  128. SET_TLBMCFG(base, ctx, 0x3);
  129. /* V2P configuration: HTW for access */
  130. SET_V2PCFG(base, ctx, 0x3);
  131. SET_TTBCR(base, ctx, 0);
  132. SET_TTBR0_PA(base, ctx, (pgtable >> 14));
  133. /* Invalidate the TLB for this context */
  134. SET_CTX_TLBIALL(base, ctx, 0);
  135. /* Set interrupt number to "secure" interrupt */
  136. SET_IRPTNDX(base, ctx, 0);
  137. /* Enable context fault interrupt */
  138. SET_CFEIE(base, ctx, 1);
  139. /* Stall access on a context fault and let the handler deal with it */
  140. SET_CFCFG(base, ctx, 1);
  141. /* Redirect all cacheable requests to L2 slave port. */
  142. SET_RCISH(base, ctx, 1);
  143. SET_RCOSH(base, ctx, 1);
  144. SET_RCNSH(base, ctx, 1);
  145. /* Turn on TEX Remap */
  146. SET_TRE(base, ctx, 1);
  147. /* Set TEX remap attributes */
  148. RCP15_PRRR(prrr);
  149. RCP15_NMRR(nmrr);
  150. SET_PRRR(base, ctx, prrr);
  151. SET_NMRR(base, ctx, nmrr);
  152. /* Turn on BFB prefetch */
  153. SET_BFBDFE(base, ctx, 1);
  154. #ifdef CONFIG_IOMMU_PGTABLES_L2
  155. /* Configure page tables as inner-cacheable and shareable to reduce
  156. * the TLB miss penalty.
  157. */
  158. SET_TTBR0_SH(base, ctx, 1);
  159. SET_TTBR1_SH(base, ctx, 1);
  160. SET_TTBR0_NOS(base, ctx, 1);
  161. SET_TTBR1_NOS(base, ctx, 1);
  162. SET_TTBR0_IRGNH(base, ctx, 0); /* WB, WA */
  163. SET_TTBR0_IRGNL(base, ctx, 1);
  164. SET_TTBR1_IRGNH(base, ctx, 0); /* WB, WA */
  165. SET_TTBR1_IRGNL(base, ctx, 1);
  166. SET_TTBR0_ORGN(base, ctx, 1); /* WB, WA */
  167. SET_TTBR1_ORGN(base, ctx, 1); /* WB, WA */
  168. #endif
  169. /* Enable the MMU */
  170. SET_M(base, ctx, 1);
  171. }
  172. static int msm_iommu_domain_init(struct iommu_domain *domain)
  173. {
  174. struct msm_priv *priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  175. if (!priv)
  176. goto fail_nomem;
  177. INIT_LIST_HEAD(&priv->list_attached);
  178. priv->pgtable = (unsigned long *)__get_free_pages(GFP_KERNEL,
  179. get_order(SZ_16K));
  180. if (!priv->pgtable)
  181. goto fail_nomem;
  182. memset(priv->pgtable, 0, SZ_16K);
  183. domain->priv = priv;
  184. domain->geometry.aperture_start = 0;
  185. domain->geometry.aperture_end = (1ULL << 32) - 1;
  186. domain->geometry.force_aperture = true;
  187. return 0;
  188. fail_nomem:
  189. kfree(priv);
  190. return -ENOMEM;
  191. }
  192. static void msm_iommu_domain_destroy(struct iommu_domain *domain)
  193. {
  194. struct msm_priv *priv;
  195. unsigned long flags;
  196. unsigned long *fl_table;
  197. int i;
  198. spin_lock_irqsave(&msm_iommu_lock, flags);
  199. priv = domain->priv;
  200. domain->priv = NULL;
  201. if (priv) {
  202. fl_table = priv->pgtable;
  203. for (i = 0; i < NUM_FL_PTE; i++)
  204. if ((fl_table[i] & 0x03) == FL_TYPE_TABLE)
  205. free_page((unsigned long) __va(((fl_table[i]) &
  206. FL_BASE_MASK)));
  207. free_pages((unsigned long)priv->pgtable, get_order(SZ_16K));
  208. priv->pgtable = NULL;
  209. }
  210. kfree(priv);
  211. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  212. }
  213. static int msm_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
  214. {
  215. struct msm_priv *priv;
  216. struct msm_iommu_ctx_dev *ctx_dev;
  217. struct msm_iommu_drvdata *iommu_drvdata;
  218. struct msm_iommu_ctx_drvdata *ctx_drvdata;
  219. struct msm_iommu_ctx_drvdata *tmp_drvdata;
  220. int ret = 0;
  221. unsigned long flags;
  222. spin_lock_irqsave(&msm_iommu_lock, flags);
  223. priv = domain->priv;
  224. if (!priv || !dev) {
  225. ret = -EINVAL;
  226. goto fail;
  227. }
  228. iommu_drvdata = dev_get_drvdata(dev->parent);
  229. ctx_drvdata = dev_get_drvdata(dev);
  230. ctx_dev = dev->platform_data;
  231. if (!iommu_drvdata || !ctx_drvdata || !ctx_dev) {
  232. ret = -EINVAL;
  233. goto fail;
  234. }
  235. if (!list_empty(&ctx_drvdata->attached_elm)) {
  236. ret = -EBUSY;
  237. goto fail;
  238. }
  239. list_for_each_entry(tmp_drvdata, &priv->list_attached, attached_elm)
  240. if (tmp_drvdata == ctx_drvdata) {
  241. ret = -EBUSY;
  242. goto fail;
  243. }
  244. ret = __enable_clocks(iommu_drvdata);
  245. if (ret)
  246. goto fail;
  247. __program_context(iommu_drvdata->base, ctx_dev->num,
  248. __pa(priv->pgtable));
  249. __disable_clocks(iommu_drvdata);
  250. list_add(&(ctx_drvdata->attached_elm), &priv->list_attached);
  251. ret = __flush_iotlb(domain);
  252. fail:
  253. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  254. return ret;
  255. }
  256. static void msm_iommu_detach_dev(struct iommu_domain *domain,
  257. struct device *dev)
  258. {
  259. struct msm_priv *priv;
  260. struct msm_iommu_ctx_dev *ctx_dev;
  261. struct msm_iommu_drvdata *iommu_drvdata;
  262. struct msm_iommu_ctx_drvdata *ctx_drvdata;
  263. unsigned long flags;
  264. int ret;
  265. spin_lock_irqsave(&msm_iommu_lock, flags);
  266. priv = domain->priv;
  267. if (!priv || !dev)
  268. goto fail;
  269. iommu_drvdata = dev_get_drvdata(dev->parent);
  270. ctx_drvdata = dev_get_drvdata(dev);
  271. ctx_dev = dev->platform_data;
  272. if (!iommu_drvdata || !ctx_drvdata || !ctx_dev)
  273. goto fail;
  274. ret = __flush_iotlb(domain);
  275. if (ret)
  276. goto fail;
  277. ret = __enable_clocks(iommu_drvdata);
  278. if (ret)
  279. goto fail;
  280. __reset_context(iommu_drvdata->base, ctx_dev->num);
  281. __disable_clocks(iommu_drvdata);
  282. list_del_init(&ctx_drvdata->attached_elm);
  283. fail:
  284. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  285. }
  286. static int msm_iommu_map(struct iommu_domain *domain, unsigned long va,
  287. phys_addr_t pa, size_t len, int prot)
  288. {
  289. struct msm_priv *priv;
  290. unsigned long flags;
  291. unsigned long *fl_table;
  292. unsigned long *fl_pte;
  293. unsigned long fl_offset;
  294. unsigned long *sl_table;
  295. unsigned long *sl_pte;
  296. unsigned long sl_offset;
  297. unsigned int pgprot;
  298. int ret = 0, tex, sh;
  299. spin_lock_irqsave(&msm_iommu_lock, flags);
  300. sh = (prot & MSM_IOMMU_ATTR_SH) ? 1 : 0;
  301. tex = msm_iommu_tex_class[prot & MSM_IOMMU_CP_MASK];
  302. if (tex < 0 || tex > NUM_TEX_CLASS - 1) {
  303. ret = -EINVAL;
  304. goto fail;
  305. }
  306. priv = domain->priv;
  307. if (!priv) {
  308. ret = -EINVAL;
  309. goto fail;
  310. }
  311. fl_table = priv->pgtable;
  312. if (len != SZ_16M && len != SZ_1M &&
  313. len != SZ_64K && len != SZ_4K) {
  314. pr_debug("Bad size: %d\n", len);
  315. ret = -EINVAL;
  316. goto fail;
  317. }
  318. if (!fl_table) {
  319. pr_debug("Null page table\n");
  320. ret = -EINVAL;
  321. goto fail;
  322. }
  323. if (len == SZ_16M || len == SZ_1M) {
  324. pgprot = sh ? FL_SHARED : 0;
  325. pgprot |= tex & 0x01 ? FL_BUFFERABLE : 0;
  326. pgprot |= tex & 0x02 ? FL_CACHEABLE : 0;
  327. pgprot |= tex & 0x04 ? FL_TEX0 : 0;
  328. } else {
  329. pgprot = sh ? SL_SHARED : 0;
  330. pgprot |= tex & 0x01 ? SL_BUFFERABLE : 0;
  331. pgprot |= tex & 0x02 ? SL_CACHEABLE : 0;
  332. pgprot |= tex & 0x04 ? SL_TEX0 : 0;
  333. }
  334. fl_offset = FL_OFFSET(va); /* Upper 12 bits */
  335. fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */
  336. if (len == SZ_16M) {
  337. int i = 0;
  338. for (i = 0; i < 16; i++)
  339. *(fl_pte+i) = (pa & 0xFF000000) | FL_SUPERSECTION |
  340. FL_AP_READ | FL_AP_WRITE | FL_TYPE_SECT |
  341. FL_SHARED | FL_NG | pgprot;
  342. }
  343. if (len == SZ_1M)
  344. *fl_pte = (pa & 0xFFF00000) | FL_AP_READ | FL_AP_WRITE | FL_NG |
  345. FL_TYPE_SECT | FL_SHARED | pgprot;
  346. /* Need a 2nd level table */
  347. if ((len == SZ_4K || len == SZ_64K) && (*fl_pte) == 0) {
  348. unsigned long *sl;
  349. sl = (unsigned long *) __get_free_pages(GFP_ATOMIC,
  350. get_order(SZ_4K));
  351. if (!sl) {
  352. pr_debug("Could not allocate second level table\n");
  353. ret = -ENOMEM;
  354. goto fail;
  355. }
  356. memset(sl, 0, SZ_4K);
  357. *fl_pte = ((((int)__pa(sl)) & FL_BASE_MASK) | FL_TYPE_TABLE);
  358. }
  359. sl_table = (unsigned long *) __va(((*fl_pte) & FL_BASE_MASK));
  360. sl_offset = SL_OFFSET(va);
  361. sl_pte = sl_table + sl_offset;
  362. if (len == SZ_4K)
  363. *sl_pte = (pa & SL_BASE_MASK_SMALL) | SL_AP0 | SL_AP1 | SL_NG |
  364. SL_SHARED | SL_TYPE_SMALL | pgprot;
  365. if (len == SZ_64K) {
  366. int i;
  367. for (i = 0; i < 16; i++)
  368. *(sl_pte+i) = (pa & SL_BASE_MASK_LARGE) | SL_AP0 |
  369. SL_NG | SL_AP1 | SL_SHARED | SL_TYPE_LARGE | pgprot;
  370. }
  371. ret = __flush_iotlb(domain);
  372. fail:
  373. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  374. return ret;
  375. }
  376. static size_t msm_iommu_unmap(struct iommu_domain *domain, unsigned long va,
  377. size_t len)
  378. {
  379. struct msm_priv *priv;
  380. unsigned long flags;
  381. unsigned long *fl_table;
  382. unsigned long *fl_pte;
  383. unsigned long fl_offset;
  384. unsigned long *sl_table;
  385. unsigned long *sl_pte;
  386. unsigned long sl_offset;
  387. int i, ret = 0;
  388. spin_lock_irqsave(&msm_iommu_lock, flags);
  389. priv = domain->priv;
  390. if (!priv)
  391. goto fail;
  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. goto fail;
  397. }
  398. if (!fl_table) {
  399. pr_debug("Null page table\n");
  400. goto fail;
  401. }
  402. fl_offset = FL_OFFSET(va); /* Upper 12 bits */
  403. fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */
  404. if (*fl_pte == 0) {
  405. pr_debug("First level PTE is 0\n");
  406. goto fail;
  407. }
  408. /* Unmap supersection */
  409. if (len == SZ_16M)
  410. for (i = 0; i < 16; i++)
  411. *(fl_pte+i) = 0;
  412. if (len == SZ_1M)
  413. *fl_pte = 0;
  414. sl_table = (unsigned long *) __va(((*fl_pte) & FL_BASE_MASK));
  415. sl_offset = SL_OFFSET(va);
  416. sl_pte = sl_table + sl_offset;
  417. if (len == SZ_64K) {
  418. for (i = 0; i < 16; i++)
  419. *(sl_pte+i) = 0;
  420. }
  421. if (len == SZ_4K)
  422. *sl_pte = 0;
  423. if (len == SZ_4K || len == SZ_64K) {
  424. int used = 0;
  425. for (i = 0; i < NUM_SL_PTE; i++)
  426. if (sl_table[i])
  427. used = 1;
  428. if (!used) {
  429. free_page((unsigned long)sl_table);
  430. *fl_pte = 0;
  431. }
  432. }
  433. ret = __flush_iotlb(domain);
  434. fail:
  435. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  436. /* the IOMMU API requires us to return how many bytes were unmapped */
  437. len = ret ? 0 : len;
  438. return len;
  439. }
  440. static phys_addr_t msm_iommu_iova_to_phys(struct iommu_domain *domain,
  441. dma_addr_t va)
  442. {
  443. struct msm_priv *priv;
  444. struct msm_iommu_drvdata *iommu_drvdata;
  445. struct msm_iommu_ctx_drvdata *ctx_drvdata;
  446. unsigned int par;
  447. unsigned long flags;
  448. void __iomem *base;
  449. phys_addr_t ret = 0;
  450. int ctx;
  451. spin_lock_irqsave(&msm_iommu_lock, flags);
  452. priv = domain->priv;
  453. if (list_empty(&priv->list_attached))
  454. goto fail;
  455. ctx_drvdata = list_entry(priv->list_attached.next,
  456. struct msm_iommu_ctx_drvdata, attached_elm);
  457. iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent);
  458. base = iommu_drvdata->base;
  459. ctx = ctx_drvdata->num;
  460. ret = __enable_clocks(iommu_drvdata);
  461. if (ret)
  462. goto fail;
  463. /* Invalidate context TLB */
  464. SET_CTX_TLBIALL(base, ctx, 0);
  465. SET_V2PPR(base, ctx, va & V2Pxx_VA);
  466. par = GET_PAR(base, ctx);
  467. /* We are dealing with a supersection */
  468. if (GET_NOFAULT_SS(base, ctx))
  469. ret = (par & 0xFF000000) | (va & 0x00FFFFFF);
  470. else /* Upper 20 bits from PAR, lower 12 from VA */
  471. ret = (par & 0xFFFFF000) | (va & 0x00000FFF);
  472. if (GET_FAULT(base, ctx))
  473. ret = 0;
  474. __disable_clocks(iommu_drvdata);
  475. fail:
  476. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  477. return ret;
  478. }
  479. static bool msm_iommu_capable(enum iommu_cap cap)
  480. {
  481. return false;
  482. }
  483. static void print_ctx_regs(void __iomem *base, int ctx)
  484. {
  485. unsigned int fsr = GET_FSR(base, ctx);
  486. pr_err("FAR = %08x PAR = %08x\n",
  487. GET_FAR(base, ctx), GET_PAR(base, ctx));
  488. pr_err("FSR = %08x [%s%s%s%s%s%s%s%s%s%s]\n", fsr,
  489. (fsr & 0x02) ? "TF " : "",
  490. (fsr & 0x04) ? "AFF " : "",
  491. (fsr & 0x08) ? "APF " : "",
  492. (fsr & 0x10) ? "TLBMF " : "",
  493. (fsr & 0x20) ? "HTWDEEF " : "",
  494. (fsr & 0x40) ? "HTWSEEF " : "",
  495. (fsr & 0x80) ? "MHF " : "",
  496. (fsr & 0x10000) ? "SL " : "",
  497. (fsr & 0x40000000) ? "SS " : "",
  498. (fsr & 0x80000000) ? "MULTI " : "");
  499. pr_err("FSYNR0 = %08x FSYNR1 = %08x\n",
  500. GET_FSYNR0(base, ctx), GET_FSYNR1(base, ctx));
  501. pr_err("TTBR0 = %08x TTBR1 = %08x\n",
  502. GET_TTBR0(base, ctx), GET_TTBR1(base, ctx));
  503. pr_err("SCTLR = %08x ACTLR = %08x\n",
  504. GET_SCTLR(base, ctx), GET_ACTLR(base, ctx));
  505. pr_err("PRRR = %08x NMRR = %08x\n",
  506. GET_PRRR(base, ctx), GET_NMRR(base, ctx));
  507. }
  508. irqreturn_t msm_iommu_fault_handler(int irq, void *dev_id)
  509. {
  510. struct msm_iommu_drvdata *drvdata = dev_id;
  511. void __iomem *base;
  512. unsigned int fsr;
  513. int i, ret;
  514. spin_lock(&msm_iommu_lock);
  515. if (!drvdata) {
  516. pr_err("Invalid device ID in context interrupt handler\n");
  517. goto fail;
  518. }
  519. base = drvdata->base;
  520. pr_err("Unexpected IOMMU page fault!\n");
  521. pr_err("base = %08x\n", (unsigned int) base);
  522. ret = __enable_clocks(drvdata);
  523. if (ret)
  524. goto fail;
  525. for (i = 0; i < drvdata->ncb; i++) {
  526. fsr = GET_FSR(base, i);
  527. if (fsr) {
  528. pr_err("Fault occurred in context %d.\n", i);
  529. pr_err("Interesting registers:\n");
  530. print_ctx_regs(base, i);
  531. SET_FSR(base, i, 0x4000000F);
  532. }
  533. }
  534. __disable_clocks(drvdata);
  535. fail:
  536. spin_unlock(&msm_iommu_lock);
  537. return 0;
  538. }
  539. static const struct iommu_ops msm_iommu_ops = {
  540. .capable = msm_iommu_capable,
  541. .domain_init = msm_iommu_domain_init,
  542. .domain_destroy = msm_iommu_domain_destroy,
  543. .attach_dev = msm_iommu_attach_dev,
  544. .detach_dev = msm_iommu_detach_dev,
  545. .map = msm_iommu_map,
  546. .unmap = msm_iommu_unmap,
  547. .map_sg = default_iommu_map_sg,
  548. .iova_to_phys = msm_iommu_iova_to_phys,
  549. .pgsize_bitmap = MSM_IOMMU_PGSIZES,
  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. bus_set_iommu(&platform_bus_type, &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>");