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. struct iommu_domain domain;
  47. };
  48. static struct msm_priv *to_msm_priv(struct iommu_domain *dom)
  49. {
  50. return container_of(dom, struct msm_priv, domain);
  51. }
  52. static int __enable_clocks(struct msm_iommu_drvdata *drvdata)
  53. {
  54. int ret;
  55. ret = clk_enable(drvdata->pclk);
  56. if (ret)
  57. goto fail;
  58. if (drvdata->clk) {
  59. ret = clk_enable(drvdata->clk);
  60. if (ret)
  61. clk_disable(drvdata->pclk);
  62. }
  63. fail:
  64. return ret;
  65. }
  66. static void __disable_clocks(struct msm_iommu_drvdata *drvdata)
  67. {
  68. clk_disable(drvdata->clk);
  69. clk_disable(drvdata->pclk);
  70. }
  71. static int __flush_iotlb(struct iommu_domain *domain)
  72. {
  73. struct msm_priv *priv = to_msm_priv(domain);
  74. struct msm_iommu_drvdata *iommu_drvdata;
  75. struct msm_iommu_ctx_drvdata *ctx_drvdata;
  76. int ret = 0;
  77. #ifndef CONFIG_IOMMU_PGTABLES_L2
  78. unsigned long *fl_table = priv->pgtable;
  79. int i;
  80. if (!list_empty(&priv->list_attached)) {
  81. dmac_flush_range(fl_table, fl_table + SZ_16K);
  82. for (i = 0; i < NUM_FL_PTE; i++)
  83. if ((fl_table[i] & 0x03) == FL_TYPE_TABLE) {
  84. void *sl_table = __va(fl_table[i] &
  85. FL_BASE_MASK);
  86. dmac_flush_range(sl_table, sl_table + SZ_4K);
  87. }
  88. }
  89. #endif
  90. list_for_each_entry(ctx_drvdata, &priv->list_attached, attached_elm) {
  91. if (!ctx_drvdata->pdev || !ctx_drvdata->pdev->dev.parent)
  92. BUG();
  93. iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent);
  94. BUG_ON(!iommu_drvdata);
  95. ret = __enable_clocks(iommu_drvdata);
  96. if (ret)
  97. goto fail;
  98. SET_CTX_TLBIALL(iommu_drvdata->base, ctx_drvdata->num, 0);
  99. __disable_clocks(iommu_drvdata);
  100. }
  101. fail:
  102. return ret;
  103. }
  104. static void __reset_context(void __iomem *base, int ctx)
  105. {
  106. SET_BPRCOSH(base, ctx, 0);
  107. SET_BPRCISH(base, ctx, 0);
  108. SET_BPRCNSH(base, ctx, 0);
  109. SET_BPSHCFG(base, ctx, 0);
  110. SET_BPMTCFG(base, ctx, 0);
  111. SET_ACTLR(base, ctx, 0);
  112. SET_SCTLR(base, ctx, 0);
  113. SET_FSRRESTORE(base, ctx, 0);
  114. SET_TTBR0(base, ctx, 0);
  115. SET_TTBR1(base, ctx, 0);
  116. SET_TTBCR(base, ctx, 0);
  117. SET_BFBCR(base, ctx, 0);
  118. SET_PAR(base, ctx, 0);
  119. SET_FAR(base, ctx, 0);
  120. SET_CTX_TLBIALL(base, ctx, 0);
  121. SET_TLBFLPTER(base, ctx, 0);
  122. SET_TLBSLPTER(base, ctx, 0);
  123. SET_TLBLKCR(base, ctx, 0);
  124. SET_PRRR(base, ctx, 0);
  125. SET_NMRR(base, ctx, 0);
  126. }
  127. static void __program_context(void __iomem *base, int ctx, phys_addr_t pgtable)
  128. {
  129. unsigned int prrr, nmrr;
  130. __reset_context(base, ctx);
  131. /* Set up HTW mode */
  132. /* TLB miss configuration: perform HTW on miss */
  133. SET_TLBMCFG(base, ctx, 0x3);
  134. /* V2P configuration: HTW for access */
  135. SET_V2PCFG(base, ctx, 0x3);
  136. SET_TTBCR(base, ctx, 0);
  137. SET_TTBR0_PA(base, ctx, (pgtable >> 14));
  138. /* Invalidate the TLB for this context */
  139. SET_CTX_TLBIALL(base, ctx, 0);
  140. /* Set interrupt number to "secure" interrupt */
  141. SET_IRPTNDX(base, ctx, 0);
  142. /* Enable context fault interrupt */
  143. SET_CFEIE(base, ctx, 1);
  144. /* Stall access on a context fault and let the handler deal with it */
  145. SET_CFCFG(base, ctx, 1);
  146. /* Redirect all cacheable requests to L2 slave port. */
  147. SET_RCISH(base, ctx, 1);
  148. SET_RCOSH(base, ctx, 1);
  149. SET_RCNSH(base, ctx, 1);
  150. /* Turn on TEX Remap */
  151. SET_TRE(base, ctx, 1);
  152. /* Set TEX remap attributes */
  153. RCP15_PRRR(prrr);
  154. RCP15_NMRR(nmrr);
  155. SET_PRRR(base, ctx, prrr);
  156. SET_NMRR(base, ctx, nmrr);
  157. /* Turn on BFB prefetch */
  158. SET_BFBDFE(base, ctx, 1);
  159. #ifdef CONFIG_IOMMU_PGTABLES_L2
  160. /* Configure page tables as inner-cacheable and shareable to reduce
  161. * the TLB miss penalty.
  162. */
  163. SET_TTBR0_SH(base, ctx, 1);
  164. SET_TTBR1_SH(base, ctx, 1);
  165. SET_TTBR0_NOS(base, ctx, 1);
  166. SET_TTBR1_NOS(base, ctx, 1);
  167. SET_TTBR0_IRGNH(base, ctx, 0); /* WB, WA */
  168. SET_TTBR0_IRGNL(base, ctx, 1);
  169. SET_TTBR1_IRGNH(base, ctx, 0); /* WB, WA */
  170. SET_TTBR1_IRGNL(base, ctx, 1);
  171. SET_TTBR0_ORGN(base, ctx, 1); /* WB, WA */
  172. SET_TTBR1_ORGN(base, ctx, 1); /* WB, WA */
  173. #endif
  174. /* Enable the MMU */
  175. SET_M(base, ctx, 1);
  176. }
  177. static struct iommu_domain *msm_iommu_domain_alloc(unsigned type)
  178. {
  179. struct msm_priv *priv;
  180. if (type != IOMMU_DOMAIN_UNMANAGED)
  181. return NULL;
  182. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  183. if (!priv)
  184. goto fail_nomem;
  185. INIT_LIST_HEAD(&priv->list_attached);
  186. priv->pgtable = (unsigned long *)__get_free_pages(GFP_KERNEL,
  187. get_order(SZ_16K));
  188. if (!priv->pgtable)
  189. goto fail_nomem;
  190. memset(priv->pgtable, 0, SZ_16K);
  191. priv->domain.geometry.aperture_start = 0;
  192. priv->domain.geometry.aperture_end = (1ULL << 32) - 1;
  193. priv->domain.geometry.force_aperture = true;
  194. return &priv->domain;
  195. fail_nomem:
  196. kfree(priv);
  197. return NULL;
  198. }
  199. static void msm_iommu_domain_free(struct iommu_domain *domain)
  200. {
  201. struct msm_priv *priv;
  202. unsigned long flags;
  203. unsigned long *fl_table;
  204. int i;
  205. spin_lock_irqsave(&msm_iommu_lock, flags);
  206. priv = to_msm_priv(domain);
  207. fl_table = priv->pgtable;
  208. for (i = 0; i < NUM_FL_PTE; i++)
  209. if ((fl_table[i] & 0x03) == FL_TYPE_TABLE)
  210. free_page((unsigned long) __va(((fl_table[i]) &
  211. FL_BASE_MASK)));
  212. free_pages((unsigned long)priv->pgtable, get_order(SZ_16K));
  213. priv->pgtable = NULL;
  214. kfree(priv);
  215. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  216. }
  217. static int msm_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
  218. {
  219. struct msm_priv *priv;
  220. struct msm_iommu_ctx_dev *ctx_dev;
  221. struct msm_iommu_drvdata *iommu_drvdata;
  222. struct msm_iommu_ctx_drvdata *ctx_drvdata;
  223. struct msm_iommu_ctx_drvdata *tmp_drvdata;
  224. int ret = 0;
  225. unsigned long flags;
  226. spin_lock_irqsave(&msm_iommu_lock, flags);
  227. priv = to_msm_priv(domain);
  228. if (!dev) {
  229. ret = -EINVAL;
  230. goto fail;
  231. }
  232. iommu_drvdata = dev_get_drvdata(dev->parent);
  233. ctx_drvdata = dev_get_drvdata(dev);
  234. ctx_dev = dev->platform_data;
  235. if (!iommu_drvdata || !ctx_drvdata || !ctx_dev) {
  236. ret = -EINVAL;
  237. goto fail;
  238. }
  239. if (!list_empty(&ctx_drvdata->attached_elm)) {
  240. ret = -EBUSY;
  241. goto fail;
  242. }
  243. list_for_each_entry(tmp_drvdata, &priv->list_attached, attached_elm)
  244. if (tmp_drvdata == ctx_drvdata) {
  245. ret = -EBUSY;
  246. goto fail;
  247. }
  248. ret = __enable_clocks(iommu_drvdata);
  249. if (ret)
  250. goto fail;
  251. __program_context(iommu_drvdata->base, ctx_dev->num,
  252. __pa(priv->pgtable));
  253. __disable_clocks(iommu_drvdata);
  254. list_add(&(ctx_drvdata->attached_elm), &priv->list_attached);
  255. ret = __flush_iotlb(domain);
  256. fail:
  257. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  258. return ret;
  259. }
  260. static void msm_iommu_detach_dev(struct iommu_domain *domain,
  261. struct device *dev)
  262. {
  263. struct msm_priv *priv;
  264. struct msm_iommu_ctx_dev *ctx_dev;
  265. struct msm_iommu_drvdata *iommu_drvdata;
  266. struct msm_iommu_ctx_drvdata *ctx_drvdata;
  267. unsigned long flags;
  268. int ret;
  269. spin_lock_irqsave(&msm_iommu_lock, flags);
  270. priv = to_msm_priv(domain);
  271. if (!dev)
  272. goto fail;
  273. iommu_drvdata = dev_get_drvdata(dev->parent);
  274. ctx_drvdata = dev_get_drvdata(dev);
  275. ctx_dev = dev->platform_data;
  276. if (!iommu_drvdata || !ctx_drvdata || !ctx_dev)
  277. goto fail;
  278. ret = __flush_iotlb(domain);
  279. if (ret)
  280. goto fail;
  281. ret = __enable_clocks(iommu_drvdata);
  282. if (ret)
  283. goto fail;
  284. __reset_context(iommu_drvdata->base, ctx_dev->num);
  285. __disable_clocks(iommu_drvdata);
  286. list_del_init(&ctx_drvdata->attached_elm);
  287. fail:
  288. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  289. }
  290. static int msm_iommu_map(struct iommu_domain *domain, unsigned long va,
  291. phys_addr_t pa, size_t len, int prot)
  292. {
  293. struct msm_priv *priv;
  294. unsigned long flags;
  295. unsigned long *fl_table;
  296. unsigned long *fl_pte;
  297. unsigned long fl_offset;
  298. unsigned long *sl_table;
  299. unsigned long *sl_pte;
  300. unsigned long sl_offset;
  301. unsigned int pgprot;
  302. int ret = 0, tex, sh;
  303. spin_lock_irqsave(&msm_iommu_lock, flags);
  304. sh = (prot & MSM_IOMMU_ATTR_SH) ? 1 : 0;
  305. tex = msm_iommu_tex_class[prot & MSM_IOMMU_CP_MASK];
  306. if (tex < 0 || tex > NUM_TEX_CLASS - 1) {
  307. ret = -EINVAL;
  308. goto fail;
  309. }
  310. priv = to_msm_priv(domain);
  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 = to_msm_priv(domain);
  390. fl_table = priv->pgtable;
  391. if (len != SZ_16M && len != SZ_1M &&
  392. len != SZ_64K && len != SZ_4K) {
  393. pr_debug("Bad length: %d\n", len);
  394. goto fail;
  395. }
  396. if (!fl_table) {
  397. pr_debug("Null page table\n");
  398. goto fail;
  399. }
  400. fl_offset = FL_OFFSET(va); /* Upper 12 bits */
  401. fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */
  402. if (*fl_pte == 0) {
  403. pr_debug("First level PTE is 0\n");
  404. goto fail;
  405. }
  406. /* Unmap supersection */
  407. if (len == SZ_16M)
  408. for (i = 0; i < 16; i++)
  409. *(fl_pte+i) = 0;
  410. if (len == SZ_1M)
  411. *fl_pte = 0;
  412. sl_table = (unsigned long *) __va(((*fl_pte) & FL_BASE_MASK));
  413. sl_offset = SL_OFFSET(va);
  414. sl_pte = sl_table + sl_offset;
  415. if (len == SZ_64K) {
  416. for (i = 0; i < 16; i++)
  417. *(sl_pte+i) = 0;
  418. }
  419. if (len == SZ_4K)
  420. *sl_pte = 0;
  421. if (len == SZ_4K || len == SZ_64K) {
  422. int used = 0;
  423. for (i = 0; i < NUM_SL_PTE; i++)
  424. if (sl_table[i])
  425. used = 1;
  426. if (!used) {
  427. free_page((unsigned long)sl_table);
  428. *fl_pte = 0;
  429. }
  430. }
  431. ret = __flush_iotlb(domain);
  432. fail:
  433. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  434. /* the IOMMU API requires us to return how many bytes were unmapped */
  435. len = ret ? 0 : len;
  436. return len;
  437. }
  438. static phys_addr_t msm_iommu_iova_to_phys(struct iommu_domain *domain,
  439. dma_addr_t va)
  440. {
  441. struct msm_priv *priv;
  442. struct msm_iommu_drvdata *iommu_drvdata;
  443. struct msm_iommu_ctx_drvdata *ctx_drvdata;
  444. unsigned int par;
  445. unsigned long flags;
  446. void __iomem *base;
  447. phys_addr_t ret = 0;
  448. int ctx;
  449. spin_lock_irqsave(&msm_iommu_lock, flags);
  450. priv = to_msm_priv(domain);
  451. if (list_empty(&priv->list_attached))
  452. goto fail;
  453. ctx_drvdata = list_entry(priv->list_attached.next,
  454. struct msm_iommu_ctx_drvdata, attached_elm);
  455. iommu_drvdata = dev_get_drvdata(ctx_drvdata->pdev->dev.parent);
  456. base = iommu_drvdata->base;
  457. ctx = ctx_drvdata->num;
  458. ret = __enable_clocks(iommu_drvdata);
  459. if (ret)
  460. goto fail;
  461. /* Invalidate context TLB */
  462. SET_CTX_TLBIALL(base, ctx, 0);
  463. SET_V2PPR(base, ctx, va & V2Pxx_VA);
  464. par = GET_PAR(base, ctx);
  465. /* We are dealing with a supersection */
  466. if (GET_NOFAULT_SS(base, ctx))
  467. ret = (par & 0xFF000000) | (va & 0x00FFFFFF);
  468. else /* Upper 20 bits from PAR, lower 12 from VA */
  469. ret = (par & 0xFFFFF000) | (va & 0x00000FFF);
  470. if (GET_FAULT(base, ctx))
  471. ret = 0;
  472. __disable_clocks(iommu_drvdata);
  473. fail:
  474. spin_unlock_irqrestore(&msm_iommu_lock, flags);
  475. return ret;
  476. }
  477. static bool msm_iommu_capable(enum iommu_cap cap)
  478. {
  479. return false;
  480. }
  481. static void print_ctx_regs(void __iomem *base, int ctx)
  482. {
  483. unsigned int fsr = GET_FSR(base, ctx);
  484. pr_err("FAR = %08x PAR = %08x\n",
  485. GET_FAR(base, ctx), GET_PAR(base, ctx));
  486. pr_err("FSR = %08x [%s%s%s%s%s%s%s%s%s%s]\n", fsr,
  487. (fsr & 0x02) ? "TF " : "",
  488. (fsr & 0x04) ? "AFF " : "",
  489. (fsr & 0x08) ? "APF " : "",
  490. (fsr & 0x10) ? "TLBMF " : "",
  491. (fsr & 0x20) ? "HTWDEEF " : "",
  492. (fsr & 0x40) ? "HTWSEEF " : "",
  493. (fsr & 0x80) ? "MHF " : "",
  494. (fsr & 0x10000) ? "SL " : "",
  495. (fsr & 0x40000000) ? "SS " : "",
  496. (fsr & 0x80000000) ? "MULTI " : "");
  497. pr_err("FSYNR0 = %08x FSYNR1 = %08x\n",
  498. GET_FSYNR0(base, ctx), GET_FSYNR1(base, ctx));
  499. pr_err("TTBR0 = %08x TTBR1 = %08x\n",
  500. GET_TTBR0(base, ctx), GET_TTBR1(base, ctx));
  501. pr_err("SCTLR = %08x ACTLR = %08x\n",
  502. GET_SCTLR(base, ctx), GET_ACTLR(base, ctx));
  503. pr_err("PRRR = %08x NMRR = %08x\n",
  504. GET_PRRR(base, ctx), GET_NMRR(base, ctx));
  505. }
  506. irqreturn_t msm_iommu_fault_handler(int irq, void *dev_id)
  507. {
  508. struct msm_iommu_drvdata *drvdata = dev_id;
  509. void __iomem *base;
  510. unsigned int fsr;
  511. int i, ret;
  512. spin_lock(&msm_iommu_lock);
  513. if (!drvdata) {
  514. pr_err("Invalid device ID in context interrupt handler\n");
  515. goto fail;
  516. }
  517. base = drvdata->base;
  518. pr_err("Unexpected IOMMU page fault!\n");
  519. pr_err("base = %08x\n", (unsigned int) base);
  520. ret = __enable_clocks(drvdata);
  521. if (ret)
  522. goto fail;
  523. for (i = 0; i < drvdata->ncb; i++) {
  524. fsr = GET_FSR(base, i);
  525. if (fsr) {
  526. pr_err("Fault occurred in context %d.\n", i);
  527. pr_err("Interesting registers:\n");
  528. print_ctx_regs(base, i);
  529. SET_FSR(base, i, 0x4000000F);
  530. }
  531. }
  532. __disable_clocks(drvdata);
  533. fail:
  534. spin_unlock(&msm_iommu_lock);
  535. return 0;
  536. }
  537. static const struct iommu_ops msm_iommu_ops = {
  538. .capable = msm_iommu_capable,
  539. .domain_alloc = msm_iommu_domain_alloc,
  540. .domain_free = msm_iommu_domain_free,
  541. .attach_dev = msm_iommu_attach_dev,
  542. .detach_dev = msm_iommu_detach_dev,
  543. .map = msm_iommu_map,
  544. .unmap = msm_iommu_unmap,
  545. .map_sg = default_iommu_map_sg,
  546. .iova_to_phys = msm_iommu_iova_to_phys,
  547. .pgsize_bitmap = MSM_IOMMU_PGSIZES,
  548. };
  549. static int __init get_tex_class(int icp, int ocp, int mt, int nos)
  550. {
  551. int i = 0;
  552. unsigned int prrr = 0;
  553. unsigned int nmrr = 0;
  554. int c_icp, c_ocp, c_mt, c_nos;
  555. RCP15_PRRR(prrr);
  556. RCP15_NMRR(nmrr);
  557. for (i = 0; i < NUM_TEX_CLASS; i++) {
  558. c_nos = PRRR_NOS(prrr, i);
  559. c_mt = PRRR_MT(prrr, i);
  560. c_icp = NMRR_ICP(nmrr, i);
  561. c_ocp = NMRR_OCP(nmrr, i);
  562. if (icp == c_icp && ocp == c_ocp && c_mt == mt && c_nos == nos)
  563. return i;
  564. }
  565. return -ENODEV;
  566. }
  567. static void __init setup_iommu_tex_classes(void)
  568. {
  569. msm_iommu_tex_class[MSM_IOMMU_ATTR_NONCACHED] =
  570. get_tex_class(CP_NONCACHED, CP_NONCACHED, MT_NORMAL, 1);
  571. msm_iommu_tex_class[MSM_IOMMU_ATTR_CACHED_WB_WA] =
  572. get_tex_class(CP_WB_WA, CP_WB_WA, MT_NORMAL, 1);
  573. msm_iommu_tex_class[MSM_IOMMU_ATTR_CACHED_WB_NWA] =
  574. get_tex_class(CP_WB_NWA, CP_WB_NWA, MT_NORMAL, 1);
  575. msm_iommu_tex_class[MSM_IOMMU_ATTR_CACHED_WT] =
  576. get_tex_class(CP_WT, CP_WT, MT_NORMAL, 1);
  577. }
  578. static int __init msm_iommu_init(void)
  579. {
  580. setup_iommu_tex_classes();
  581. bus_set_iommu(&platform_bus_type, &msm_iommu_ops);
  582. return 0;
  583. }
  584. subsys_initcall(msm_iommu_init);
  585. MODULE_LICENSE("GPL v2");
  586. MODULE_AUTHOR("Stepan Moskovchenko <stepanm@codeaurora.org>");