msm_iommu.c 17 KB

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