iommu.c 14 KB

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