iommu.c 16 KB

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