omap-iommu.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  1. /*
  2. * omap iommu: tlb and pagetable primitives
  3. *
  4. * Copyright (C) 2008-2010 Nokia Corporation
  5. *
  6. * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>,
  7. * Paul Mundt and Toshihiro Kobayashi
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/dma-mapping.h>
  14. #include <linux/err.h>
  15. #include <linux/slab.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/ioport.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/iommu.h>
  20. #include <linux/omap-iommu.h>
  21. #include <linux/mutex.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/io.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/of.h>
  26. #include <linux/of_iommu.h>
  27. #include <linux/of_irq.h>
  28. #include <linux/of_platform.h>
  29. #include <linux/regmap.h>
  30. #include <linux/mfd/syscon.h>
  31. #include <linux/platform_data/iommu-omap.h>
  32. #include "omap-iopgtable.h"
  33. #include "omap-iommu.h"
  34. static const struct iommu_ops omap_iommu_ops;
  35. #define to_iommu(dev) \
  36. ((struct omap_iommu *)platform_get_drvdata(to_platform_device(dev)))
  37. /* bitmap of the page sizes currently supported */
  38. #define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
  39. #define MMU_LOCK_BASE_SHIFT 10
  40. #define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
  41. #define MMU_LOCK_BASE(x) \
  42. ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
  43. #define MMU_LOCK_VICT_SHIFT 4
  44. #define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
  45. #define MMU_LOCK_VICT(x) \
  46. ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
  47. static struct platform_driver omap_iommu_driver;
  48. static struct kmem_cache *iopte_cachep;
  49. /**
  50. * to_omap_domain - Get struct omap_iommu_domain from generic iommu_domain
  51. * @dom: generic iommu domain handle
  52. **/
  53. static struct omap_iommu_domain *to_omap_domain(struct iommu_domain *dom)
  54. {
  55. return container_of(dom, struct omap_iommu_domain, domain);
  56. }
  57. /**
  58. * omap_iommu_save_ctx - Save registers for pm off-mode support
  59. * @dev: client device
  60. **/
  61. void omap_iommu_save_ctx(struct device *dev)
  62. {
  63. struct omap_iommu *obj = dev_to_omap_iommu(dev);
  64. u32 *p = obj->ctx;
  65. int i;
  66. for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
  67. p[i] = iommu_read_reg(obj, i * sizeof(u32));
  68. dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
  69. }
  70. }
  71. EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
  72. /**
  73. * omap_iommu_restore_ctx - Restore registers for pm off-mode support
  74. * @dev: client device
  75. **/
  76. void omap_iommu_restore_ctx(struct device *dev)
  77. {
  78. struct omap_iommu *obj = dev_to_omap_iommu(dev);
  79. u32 *p = obj->ctx;
  80. int i;
  81. for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
  82. iommu_write_reg(obj, p[i], i * sizeof(u32));
  83. dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
  84. }
  85. }
  86. EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
  87. static void dra7_cfg_dspsys_mmu(struct omap_iommu *obj, bool enable)
  88. {
  89. u32 val, mask;
  90. if (!obj->syscfg)
  91. return;
  92. mask = (1 << (obj->id * DSP_SYS_MMU_CONFIG_EN_SHIFT));
  93. val = enable ? mask : 0;
  94. regmap_update_bits(obj->syscfg, DSP_SYS_MMU_CONFIG, mask, val);
  95. }
  96. static void __iommu_set_twl(struct omap_iommu *obj, bool on)
  97. {
  98. u32 l = iommu_read_reg(obj, MMU_CNTL);
  99. if (on)
  100. iommu_write_reg(obj, MMU_IRQ_TWL_MASK, MMU_IRQENABLE);
  101. else
  102. iommu_write_reg(obj, MMU_IRQ_TLB_MISS_MASK, MMU_IRQENABLE);
  103. l &= ~MMU_CNTL_MASK;
  104. if (on)
  105. l |= (MMU_CNTL_MMU_EN | MMU_CNTL_TWL_EN);
  106. else
  107. l |= (MMU_CNTL_MMU_EN);
  108. iommu_write_reg(obj, l, MMU_CNTL);
  109. }
  110. static int omap2_iommu_enable(struct omap_iommu *obj)
  111. {
  112. u32 l, pa;
  113. if (!obj->iopgd || !IS_ALIGNED((u32)obj->iopgd, SZ_16K))
  114. return -EINVAL;
  115. pa = virt_to_phys(obj->iopgd);
  116. if (!IS_ALIGNED(pa, SZ_16K))
  117. return -EINVAL;
  118. l = iommu_read_reg(obj, MMU_REVISION);
  119. dev_info(obj->dev, "%s: version %d.%d\n", obj->name,
  120. (l >> 4) & 0xf, l & 0xf);
  121. iommu_write_reg(obj, pa, MMU_TTB);
  122. dra7_cfg_dspsys_mmu(obj, true);
  123. if (obj->has_bus_err_back)
  124. iommu_write_reg(obj, MMU_GP_REG_BUS_ERR_BACK_EN, MMU_GP_REG);
  125. __iommu_set_twl(obj, true);
  126. return 0;
  127. }
  128. static void omap2_iommu_disable(struct omap_iommu *obj)
  129. {
  130. u32 l = iommu_read_reg(obj, MMU_CNTL);
  131. l &= ~MMU_CNTL_MASK;
  132. iommu_write_reg(obj, l, MMU_CNTL);
  133. dra7_cfg_dspsys_mmu(obj, false);
  134. dev_dbg(obj->dev, "%s is shutting down\n", obj->name);
  135. }
  136. static int iommu_enable(struct omap_iommu *obj)
  137. {
  138. int err;
  139. struct platform_device *pdev = to_platform_device(obj->dev);
  140. struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
  141. if (pdata && pdata->deassert_reset) {
  142. err = pdata->deassert_reset(pdev, pdata->reset_name);
  143. if (err) {
  144. dev_err(obj->dev, "deassert_reset failed: %d\n", err);
  145. return err;
  146. }
  147. }
  148. pm_runtime_get_sync(obj->dev);
  149. err = omap2_iommu_enable(obj);
  150. return err;
  151. }
  152. static void iommu_disable(struct omap_iommu *obj)
  153. {
  154. struct platform_device *pdev = to_platform_device(obj->dev);
  155. struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
  156. omap2_iommu_disable(obj);
  157. pm_runtime_put_sync(obj->dev);
  158. if (pdata && pdata->assert_reset)
  159. pdata->assert_reset(pdev, pdata->reset_name);
  160. }
  161. /*
  162. * TLB operations
  163. */
  164. static u32 iotlb_cr_to_virt(struct cr_regs *cr)
  165. {
  166. u32 page_size = cr->cam & MMU_CAM_PGSZ_MASK;
  167. u32 mask = get_cam_va_mask(cr->cam & page_size);
  168. return cr->cam & mask;
  169. }
  170. static u32 get_iopte_attr(struct iotlb_entry *e)
  171. {
  172. u32 attr;
  173. attr = e->mixed << 5;
  174. attr |= e->endian;
  175. attr |= e->elsz >> 3;
  176. attr <<= (((e->pgsz == MMU_CAM_PGSZ_4K) ||
  177. (e->pgsz == MMU_CAM_PGSZ_64K)) ? 0 : 6);
  178. return attr;
  179. }
  180. static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
  181. {
  182. u32 status, fault_addr;
  183. status = iommu_read_reg(obj, MMU_IRQSTATUS);
  184. status &= MMU_IRQ_MASK;
  185. if (!status) {
  186. *da = 0;
  187. return 0;
  188. }
  189. fault_addr = iommu_read_reg(obj, MMU_FAULT_AD);
  190. *da = fault_addr;
  191. iommu_write_reg(obj, status, MMU_IRQSTATUS);
  192. return status;
  193. }
  194. void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
  195. {
  196. u32 val;
  197. val = iommu_read_reg(obj, MMU_LOCK);
  198. l->base = MMU_LOCK_BASE(val);
  199. l->vict = MMU_LOCK_VICT(val);
  200. }
  201. void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
  202. {
  203. u32 val;
  204. val = (l->base << MMU_LOCK_BASE_SHIFT);
  205. val |= (l->vict << MMU_LOCK_VICT_SHIFT);
  206. iommu_write_reg(obj, val, MMU_LOCK);
  207. }
  208. static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
  209. {
  210. cr->cam = iommu_read_reg(obj, MMU_READ_CAM);
  211. cr->ram = iommu_read_reg(obj, MMU_READ_RAM);
  212. }
  213. static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
  214. {
  215. iommu_write_reg(obj, cr->cam | MMU_CAM_V, MMU_CAM);
  216. iommu_write_reg(obj, cr->ram, MMU_RAM);
  217. iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
  218. iommu_write_reg(obj, 1, MMU_LD_TLB);
  219. }
  220. /* only used in iotlb iteration for-loop */
  221. struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
  222. {
  223. struct cr_regs cr;
  224. struct iotlb_lock l;
  225. iotlb_lock_get(obj, &l);
  226. l.vict = n;
  227. iotlb_lock_set(obj, &l);
  228. iotlb_read_cr(obj, &cr);
  229. return cr;
  230. }
  231. #ifdef PREFETCH_IOTLB
  232. static struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
  233. struct iotlb_entry *e)
  234. {
  235. struct cr_regs *cr;
  236. if (!e)
  237. return NULL;
  238. if (e->da & ~(get_cam_va_mask(e->pgsz))) {
  239. dev_err(obj->dev, "%s:\twrong alignment: %08x\n", __func__,
  240. e->da);
  241. return ERR_PTR(-EINVAL);
  242. }
  243. cr = kmalloc(sizeof(*cr), GFP_KERNEL);
  244. if (!cr)
  245. return ERR_PTR(-ENOMEM);
  246. cr->cam = (e->da & MMU_CAM_VATAG_MASK) | e->prsvd | e->pgsz | e->valid;
  247. cr->ram = e->pa | e->endian | e->elsz | e->mixed;
  248. return cr;
  249. }
  250. /**
  251. * load_iotlb_entry - Set an iommu tlb entry
  252. * @obj: target iommu
  253. * @e: an iommu tlb entry info
  254. **/
  255. static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
  256. {
  257. int err = 0;
  258. struct iotlb_lock l;
  259. struct cr_regs *cr;
  260. if (!obj || !obj->nr_tlb_entries || !e)
  261. return -EINVAL;
  262. pm_runtime_get_sync(obj->dev);
  263. iotlb_lock_get(obj, &l);
  264. if (l.base == obj->nr_tlb_entries) {
  265. dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
  266. err = -EBUSY;
  267. goto out;
  268. }
  269. if (!e->prsvd) {
  270. int i;
  271. struct cr_regs tmp;
  272. for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
  273. if (!iotlb_cr_valid(&tmp))
  274. break;
  275. if (i == obj->nr_tlb_entries) {
  276. dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
  277. err = -EBUSY;
  278. goto out;
  279. }
  280. iotlb_lock_get(obj, &l);
  281. } else {
  282. l.vict = l.base;
  283. iotlb_lock_set(obj, &l);
  284. }
  285. cr = iotlb_alloc_cr(obj, e);
  286. if (IS_ERR(cr)) {
  287. pm_runtime_put_sync(obj->dev);
  288. return PTR_ERR(cr);
  289. }
  290. iotlb_load_cr(obj, cr);
  291. kfree(cr);
  292. if (e->prsvd)
  293. l.base++;
  294. /* increment victim for next tlb load */
  295. if (++l.vict == obj->nr_tlb_entries)
  296. l.vict = l.base;
  297. iotlb_lock_set(obj, &l);
  298. out:
  299. pm_runtime_put_sync(obj->dev);
  300. return err;
  301. }
  302. #else /* !PREFETCH_IOTLB */
  303. static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
  304. {
  305. return 0;
  306. }
  307. #endif /* !PREFETCH_IOTLB */
  308. static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
  309. {
  310. return load_iotlb_entry(obj, e);
  311. }
  312. /**
  313. * flush_iotlb_page - Clear an iommu tlb entry
  314. * @obj: target iommu
  315. * @da: iommu device virtual address
  316. *
  317. * Clear an iommu tlb entry which includes 'da' address.
  318. **/
  319. static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
  320. {
  321. int i;
  322. struct cr_regs cr;
  323. pm_runtime_get_sync(obj->dev);
  324. for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
  325. u32 start;
  326. size_t bytes;
  327. if (!iotlb_cr_valid(&cr))
  328. continue;
  329. start = iotlb_cr_to_virt(&cr);
  330. bytes = iopgsz_to_bytes(cr.cam & 3);
  331. if ((start <= da) && (da < start + bytes)) {
  332. dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
  333. __func__, start, da, bytes);
  334. iotlb_load_cr(obj, &cr);
  335. iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
  336. break;
  337. }
  338. }
  339. pm_runtime_put_sync(obj->dev);
  340. if (i == obj->nr_tlb_entries)
  341. dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
  342. }
  343. /**
  344. * flush_iotlb_all - Clear all iommu tlb entries
  345. * @obj: target iommu
  346. **/
  347. static void flush_iotlb_all(struct omap_iommu *obj)
  348. {
  349. struct iotlb_lock l;
  350. pm_runtime_get_sync(obj->dev);
  351. l.base = 0;
  352. l.vict = 0;
  353. iotlb_lock_set(obj, &l);
  354. iommu_write_reg(obj, 1, MMU_GFLUSH);
  355. pm_runtime_put_sync(obj->dev);
  356. }
  357. /*
  358. * H/W pagetable operations
  359. */
  360. static void flush_iopte_range(struct device *dev, dma_addr_t dma,
  361. unsigned long offset, int num_entries)
  362. {
  363. size_t size = num_entries * sizeof(u32);
  364. dma_sync_single_range_for_device(dev, dma, offset, size, DMA_TO_DEVICE);
  365. }
  366. static void iopte_free(struct omap_iommu *obj, u32 *iopte, bool dma_valid)
  367. {
  368. dma_addr_t pt_dma;
  369. /* Note: freed iopte's must be clean ready for re-use */
  370. if (iopte) {
  371. if (dma_valid) {
  372. pt_dma = virt_to_phys(iopte);
  373. dma_unmap_single(obj->dev, pt_dma, IOPTE_TABLE_SIZE,
  374. DMA_TO_DEVICE);
  375. }
  376. kmem_cache_free(iopte_cachep, iopte);
  377. }
  378. }
  379. static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd,
  380. dma_addr_t *pt_dma, u32 da)
  381. {
  382. u32 *iopte;
  383. unsigned long offset = iopgd_index(da) * sizeof(da);
  384. /* a table has already existed */
  385. if (*iopgd)
  386. goto pte_ready;
  387. /*
  388. * do the allocation outside the page table lock
  389. */
  390. spin_unlock(&obj->page_table_lock);
  391. iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
  392. spin_lock(&obj->page_table_lock);
  393. if (!*iopgd) {
  394. if (!iopte)
  395. return ERR_PTR(-ENOMEM);
  396. *pt_dma = dma_map_single(obj->dev, iopte, IOPTE_TABLE_SIZE,
  397. DMA_TO_DEVICE);
  398. if (dma_mapping_error(obj->dev, *pt_dma)) {
  399. dev_err(obj->dev, "DMA map error for L2 table\n");
  400. iopte_free(obj, iopte, false);
  401. return ERR_PTR(-ENOMEM);
  402. }
  403. /*
  404. * we rely on dma address and the physical address to be
  405. * the same for mapping the L2 table
  406. */
  407. if (WARN_ON(*pt_dma != virt_to_phys(iopte))) {
  408. dev_err(obj->dev, "DMA translation error for L2 table\n");
  409. dma_unmap_single(obj->dev, *pt_dma, IOPTE_TABLE_SIZE,
  410. DMA_TO_DEVICE);
  411. iopte_free(obj, iopte, false);
  412. return ERR_PTR(-ENOMEM);
  413. }
  414. *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
  415. flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
  416. dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
  417. } else {
  418. /* We raced, free the reduniovant table */
  419. iopte_free(obj, iopte, false);
  420. }
  421. pte_ready:
  422. iopte = iopte_offset(iopgd, da);
  423. *pt_dma = virt_to_phys(iopte);
  424. dev_vdbg(obj->dev,
  425. "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
  426. __func__, da, iopgd, *iopgd, iopte, *iopte);
  427. return iopte;
  428. }
  429. static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  430. {
  431. u32 *iopgd = iopgd_offset(obj, da);
  432. unsigned long offset = iopgd_index(da) * sizeof(da);
  433. if ((da | pa) & ~IOSECTION_MASK) {
  434. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  435. __func__, da, pa, IOSECTION_SIZE);
  436. return -EINVAL;
  437. }
  438. *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
  439. flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
  440. return 0;
  441. }
  442. static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  443. {
  444. u32 *iopgd = iopgd_offset(obj, da);
  445. unsigned long offset = iopgd_index(da) * sizeof(da);
  446. int i;
  447. if ((da | pa) & ~IOSUPER_MASK) {
  448. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  449. __func__, da, pa, IOSUPER_SIZE);
  450. return -EINVAL;
  451. }
  452. for (i = 0; i < 16; i++)
  453. *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
  454. flush_iopte_range(obj->dev, obj->pd_dma, offset, 16);
  455. return 0;
  456. }
  457. static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  458. {
  459. u32 *iopgd = iopgd_offset(obj, da);
  460. dma_addr_t pt_dma;
  461. u32 *iopte = iopte_alloc(obj, iopgd, &pt_dma, da);
  462. unsigned long offset = iopte_index(da) * sizeof(da);
  463. if (IS_ERR(iopte))
  464. return PTR_ERR(iopte);
  465. *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
  466. flush_iopte_range(obj->dev, pt_dma, offset, 1);
  467. dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
  468. __func__, da, pa, iopte, *iopte);
  469. return 0;
  470. }
  471. static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  472. {
  473. u32 *iopgd = iopgd_offset(obj, da);
  474. dma_addr_t pt_dma;
  475. u32 *iopte = iopte_alloc(obj, iopgd, &pt_dma, da);
  476. unsigned long offset = iopte_index(da) * sizeof(da);
  477. int i;
  478. if ((da | pa) & ~IOLARGE_MASK) {
  479. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  480. __func__, da, pa, IOLARGE_SIZE);
  481. return -EINVAL;
  482. }
  483. if (IS_ERR(iopte))
  484. return PTR_ERR(iopte);
  485. for (i = 0; i < 16; i++)
  486. *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
  487. flush_iopte_range(obj->dev, pt_dma, offset, 16);
  488. return 0;
  489. }
  490. static int
  491. iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
  492. {
  493. int (*fn)(struct omap_iommu *, u32, u32, u32);
  494. u32 prot;
  495. int err;
  496. if (!obj || !e)
  497. return -EINVAL;
  498. switch (e->pgsz) {
  499. case MMU_CAM_PGSZ_16M:
  500. fn = iopgd_alloc_super;
  501. break;
  502. case MMU_CAM_PGSZ_1M:
  503. fn = iopgd_alloc_section;
  504. break;
  505. case MMU_CAM_PGSZ_64K:
  506. fn = iopte_alloc_large;
  507. break;
  508. case MMU_CAM_PGSZ_4K:
  509. fn = iopte_alloc_page;
  510. break;
  511. default:
  512. fn = NULL;
  513. break;
  514. }
  515. if (WARN_ON(!fn))
  516. return -EINVAL;
  517. prot = get_iopte_attr(e);
  518. spin_lock(&obj->page_table_lock);
  519. err = fn(obj, e->da, e->pa, prot);
  520. spin_unlock(&obj->page_table_lock);
  521. return err;
  522. }
  523. /**
  524. * omap_iopgtable_store_entry - Make an iommu pte entry
  525. * @obj: target iommu
  526. * @e: an iommu tlb entry info
  527. **/
  528. static int
  529. omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
  530. {
  531. int err;
  532. flush_iotlb_page(obj, e->da);
  533. err = iopgtable_store_entry_core(obj, e);
  534. if (!err)
  535. prefetch_iotlb_entry(obj, e);
  536. return err;
  537. }
  538. /**
  539. * iopgtable_lookup_entry - Lookup an iommu pte entry
  540. * @obj: target iommu
  541. * @da: iommu device virtual address
  542. * @ppgd: iommu pgd entry pointer to be returned
  543. * @ppte: iommu pte entry pointer to be returned
  544. **/
  545. static void
  546. iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
  547. {
  548. u32 *iopgd, *iopte = NULL;
  549. iopgd = iopgd_offset(obj, da);
  550. if (!*iopgd)
  551. goto out;
  552. if (iopgd_is_table(*iopgd))
  553. iopte = iopte_offset(iopgd, da);
  554. out:
  555. *ppgd = iopgd;
  556. *ppte = iopte;
  557. }
  558. static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
  559. {
  560. size_t bytes;
  561. u32 *iopgd = iopgd_offset(obj, da);
  562. int nent = 1;
  563. dma_addr_t pt_dma;
  564. unsigned long pd_offset = iopgd_index(da) * sizeof(da);
  565. unsigned long pt_offset = iopte_index(da) * sizeof(da);
  566. if (!*iopgd)
  567. return 0;
  568. if (iopgd_is_table(*iopgd)) {
  569. int i;
  570. u32 *iopte = iopte_offset(iopgd, da);
  571. bytes = IOPTE_SIZE;
  572. if (*iopte & IOPTE_LARGE) {
  573. nent *= 16;
  574. /* rewind to the 1st entry */
  575. iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
  576. }
  577. bytes *= nent;
  578. memset(iopte, 0, nent * sizeof(*iopte));
  579. pt_dma = virt_to_phys(iopte);
  580. flush_iopte_range(obj->dev, pt_dma, pt_offset, nent);
  581. /*
  582. * do table walk to check if this table is necessary or not
  583. */
  584. iopte = iopte_offset(iopgd, 0);
  585. for (i = 0; i < PTRS_PER_IOPTE; i++)
  586. if (iopte[i])
  587. goto out;
  588. iopte_free(obj, iopte, true);
  589. nent = 1; /* for the next L1 entry */
  590. } else {
  591. bytes = IOPGD_SIZE;
  592. if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
  593. nent *= 16;
  594. /* rewind to the 1st entry */
  595. iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
  596. }
  597. bytes *= nent;
  598. }
  599. memset(iopgd, 0, nent * sizeof(*iopgd));
  600. flush_iopte_range(obj->dev, obj->pd_dma, pd_offset, nent);
  601. out:
  602. return bytes;
  603. }
  604. /**
  605. * iopgtable_clear_entry - Remove an iommu pte entry
  606. * @obj: target iommu
  607. * @da: iommu device virtual address
  608. **/
  609. static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
  610. {
  611. size_t bytes;
  612. spin_lock(&obj->page_table_lock);
  613. bytes = iopgtable_clear_entry_core(obj, da);
  614. flush_iotlb_page(obj, da);
  615. spin_unlock(&obj->page_table_lock);
  616. return bytes;
  617. }
  618. static void iopgtable_clear_entry_all(struct omap_iommu *obj)
  619. {
  620. unsigned long offset;
  621. int i;
  622. spin_lock(&obj->page_table_lock);
  623. for (i = 0; i < PTRS_PER_IOPGD; i++) {
  624. u32 da;
  625. u32 *iopgd;
  626. da = i << IOPGD_SHIFT;
  627. iopgd = iopgd_offset(obj, da);
  628. offset = iopgd_index(da) * sizeof(da);
  629. if (!*iopgd)
  630. continue;
  631. if (iopgd_is_table(*iopgd))
  632. iopte_free(obj, iopte_offset(iopgd, 0), true);
  633. *iopgd = 0;
  634. flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
  635. }
  636. flush_iotlb_all(obj);
  637. spin_unlock(&obj->page_table_lock);
  638. }
  639. /*
  640. * Device IOMMU generic operations
  641. */
  642. static irqreturn_t iommu_fault_handler(int irq, void *data)
  643. {
  644. u32 da, errs;
  645. u32 *iopgd, *iopte;
  646. struct omap_iommu *obj = data;
  647. struct iommu_domain *domain = obj->domain;
  648. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  649. if (!omap_domain->iommu_dev)
  650. return IRQ_NONE;
  651. errs = iommu_report_fault(obj, &da);
  652. if (errs == 0)
  653. return IRQ_HANDLED;
  654. /* Fault callback or TLB/PTE Dynamic loading */
  655. if (!report_iommu_fault(domain, obj->dev, da, 0))
  656. return IRQ_HANDLED;
  657. iommu_write_reg(obj, 0, MMU_IRQENABLE);
  658. iopgd = iopgd_offset(obj, da);
  659. if (!iopgd_is_table(*iopgd)) {
  660. dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n",
  661. obj->name, errs, da, iopgd, *iopgd);
  662. return IRQ_NONE;
  663. }
  664. iopte = iopte_offset(iopgd, da);
  665. dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x pte:0x%p *pte:0x%08x\n",
  666. obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
  667. return IRQ_NONE;
  668. }
  669. /**
  670. * omap_iommu_attach() - attach iommu device to an iommu domain
  671. * @obj: target omap iommu device
  672. * @iopgd: page table
  673. **/
  674. static int omap_iommu_attach(struct omap_iommu *obj, u32 *iopgd)
  675. {
  676. int err;
  677. spin_lock(&obj->iommu_lock);
  678. obj->pd_dma = dma_map_single(obj->dev, iopgd, IOPGD_TABLE_SIZE,
  679. DMA_TO_DEVICE);
  680. if (dma_mapping_error(obj->dev, obj->pd_dma)) {
  681. dev_err(obj->dev, "DMA map error for L1 table\n");
  682. err = -ENOMEM;
  683. goto out_err;
  684. }
  685. obj->iopgd = iopgd;
  686. err = iommu_enable(obj);
  687. if (err)
  688. goto out_err;
  689. flush_iotlb_all(obj);
  690. spin_unlock(&obj->iommu_lock);
  691. dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
  692. return 0;
  693. out_err:
  694. spin_unlock(&obj->iommu_lock);
  695. return err;
  696. }
  697. /**
  698. * omap_iommu_detach - release iommu device
  699. * @obj: target iommu
  700. **/
  701. static void omap_iommu_detach(struct omap_iommu *obj)
  702. {
  703. if (!obj || IS_ERR(obj))
  704. return;
  705. spin_lock(&obj->iommu_lock);
  706. dma_unmap_single(obj->dev, obj->pd_dma, IOPGD_TABLE_SIZE,
  707. DMA_TO_DEVICE);
  708. iommu_disable(obj);
  709. obj->pd_dma = 0;
  710. obj->iopgd = NULL;
  711. spin_unlock(&obj->iommu_lock);
  712. dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
  713. }
  714. static int omap_iommu_dra7_get_dsp_system_cfg(struct platform_device *pdev,
  715. struct omap_iommu *obj)
  716. {
  717. struct device_node *np = pdev->dev.of_node;
  718. int ret;
  719. if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu"))
  720. return 0;
  721. if (!of_property_read_bool(np, "ti,syscon-mmuconfig")) {
  722. dev_err(&pdev->dev, "ti,syscon-mmuconfig property is missing\n");
  723. return -EINVAL;
  724. }
  725. obj->syscfg =
  726. syscon_regmap_lookup_by_phandle(np, "ti,syscon-mmuconfig");
  727. if (IS_ERR(obj->syscfg)) {
  728. /* can fail with -EPROBE_DEFER */
  729. ret = PTR_ERR(obj->syscfg);
  730. return ret;
  731. }
  732. if (of_property_read_u32_index(np, "ti,syscon-mmuconfig", 1,
  733. &obj->id)) {
  734. dev_err(&pdev->dev, "couldn't get the IOMMU instance id within subsystem\n");
  735. return -EINVAL;
  736. }
  737. if (obj->id != 0 && obj->id != 1) {
  738. dev_err(&pdev->dev, "invalid IOMMU instance id\n");
  739. return -EINVAL;
  740. }
  741. return 0;
  742. }
  743. /*
  744. * OMAP Device MMU(IOMMU) detection
  745. */
  746. static int omap_iommu_probe(struct platform_device *pdev)
  747. {
  748. int err = -ENODEV;
  749. int irq;
  750. struct omap_iommu *obj;
  751. struct resource *res;
  752. struct device_node *of = pdev->dev.of_node;
  753. if (!of) {
  754. pr_err("%s: only DT-based devices are supported\n", __func__);
  755. return -ENODEV;
  756. }
  757. obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
  758. if (!obj)
  759. return -ENOMEM;
  760. obj->name = dev_name(&pdev->dev);
  761. obj->nr_tlb_entries = 32;
  762. err = of_property_read_u32(of, "ti,#tlb-entries", &obj->nr_tlb_entries);
  763. if (err && err != -EINVAL)
  764. return err;
  765. if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
  766. return -EINVAL;
  767. if (of_find_property(of, "ti,iommu-bus-err-back", NULL))
  768. obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
  769. obj->dev = &pdev->dev;
  770. obj->ctx = (void *)obj + sizeof(*obj);
  771. spin_lock_init(&obj->iommu_lock);
  772. spin_lock_init(&obj->page_table_lock);
  773. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  774. obj->regbase = devm_ioremap_resource(obj->dev, res);
  775. if (IS_ERR(obj->regbase))
  776. return PTR_ERR(obj->regbase);
  777. err = omap_iommu_dra7_get_dsp_system_cfg(pdev, obj);
  778. if (err)
  779. return err;
  780. irq = platform_get_irq(pdev, 0);
  781. if (irq < 0)
  782. return -ENODEV;
  783. err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
  784. dev_name(obj->dev), obj);
  785. if (err < 0)
  786. return err;
  787. platform_set_drvdata(pdev, obj);
  788. obj->group = iommu_group_alloc();
  789. if (IS_ERR(obj->group))
  790. return PTR_ERR(obj->group);
  791. err = iommu_device_sysfs_add(&obj->iommu, obj->dev, NULL, obj->name);
  792. if (err)
  793. goto out_group;
  794. iommu_device_set_ops(&obj->iommu, &omap_iommu_ops);
  795. err = iommu_device_register(&obj->iommu);
  796. if (err)
  797. goto out_sysfs;
  798. pm_runtime_irq_safe(obj->dev);
  799. pm_runtime_enable(obj->dev);
  800. omap_iommu_debugfs_add(obj);
  801. dev_info(&pdev->dev, "%s registered\n", obj->name);
  802. return 0;
  803. out_sysfs:
  804. iommu_device_sysfs_remove(&obj->iommu);
  805. out_group:
  806. iommu_group_put(obj->group);
  807. return err;
  808. }
  809. static int omap_iommu_remove(struct platform_device *pdev)
  810. {
  811. struct omap_iommu *obj = platform_get_drvdata(pdev);
  812. iommu_group_put(obj->group);
  813. obj->group = NULL;
  814. iommu_device_sysfs_remove(&obj->iommu);
  815. iommu_device_unregister(&obj->iommu);
  816. omap_iommu_debugfs_remove(obj);
  817. pm_runtime_disable(obj->dev);
  818. dev_info(&pdev->dev, "%s removed\n", obj->name);
  819. return 0;
  820. }
  821. static const struct of_device_id omap_iommu_of_match[] = {
  822. { .compatible = "ti,omap2-iommu" },
  823. { .compatible = "ti,omap4-iommu" },
  824. { .compatible = "ti,dra7-iommu" },
  825. { .compatible = "ti,dra7-dsp-iommu" },
  826. {},
  827. };
  828. static struct platform_driver omap_iommu_driver = {
  829. .probe = omap_iommu_probe,
  830. .remove = omap_iommu_remove,
  831. .driver = {
  832. .name = "omap-iommu",
  833. .of_match_table = of_match_ptr(omap_iommu_of_match),
  834. },
  835. };
  836. static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz)
  837. {
  838. memset(e, 0, sizeof(*e));
  839. e->da = da;
  840. e->pa = pa;
  841. e->valid = MMU_CAM_V;
  842. e->pgsz = pgsz;
  843. e->endian = MMU_RAM_ENDIAN_LITTLE;
  844. e->elsz = MMU_RAM_ELSZ_8;
  845. e->mixed = 0;
  846. return iopgsz_to_bytes(e->pgsz);
  847. }
  848. static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
  849. phys_addr_t pa, size_t bytes, int prot)
  850. {
  851. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  852. struct omap_iommu *oiommu = omap_domain->iommu_dev;
  853. struct device *dev = oiommu->dev;
  854. struct iotlb_entry e;
  855. int omap_pgsz;
  856. u32 ret;
  857. omap_pgsz = bytes_to_iopgsz(bytes);
  858. if (omap_pgsz < 0) {
  859. dev_err(dev, "invalid size to map: %d\n", bytes);
  860. return -EINVAL;
  861. }
  862. dev_dbg(dev, "mapping da 0x%lx to pa %pa size 0x%x\n", da, &pa, bytes);
  863. iotlb_init_entry(&e, da, pa, omap_pgsz);
  864. ret = omap_iopgtable_store_entry(oiommu, &e);
  865. if (ret)
  866. dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
  867. return ret;
  868. }
  869. static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
  870. size_t size)
  871. {
  872. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  873. struct omap_iommu *oiommu = omap_domain->iommu_dev;
  874. struct device *dev = oiommu->dev;
  875. dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
  876. return iopgtable_clear_entry(oiommu, da);
  877. }
  878. static int
  879. omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
  880. {
  881. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  882. struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
  883. struct omap_iommu *oiommu;
  884. int ret = 0;
  885. if (!arch_data || !arch_data->iommu_dev) {
  886. dev_err(dev, "device doesn't have an associated iommu\n");
  887. return -EINVAL;
  888. }
  889. spin_lock(&omap_domain->lock);
  890. /* only a single device is supported per domain for now */
  891. if (omap_domain->iommu_dev) {
  892. dev_err(dev, "iommu domain is already attached\n");
  893. ret = -EBUSY;
  894. goto out;
  895. }
  896. oiommu = arch_data->iommu_dev;
  897. /* get a handle to and enable the omap iommu */
  898. ret = omap_iommu_attach(oiommu, omap_domain->pgtable);
  899. if (ret) {
  900. dev_err(dev, "can't get omap iommu: %d\n", ret);
  901. goto out;
  902. }
  903. omap_domain->iommu_dev = oiommu;
  904. omap_domain->dev = dev;
  905. oiommu->domain = domain;
  906. out:
  907. spin_unlock(&omap_domain->lock);
  908. return ret;
  909. }
  910. static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
  911. struct device *dev)
  912. {
  913. struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
  914. /* only a single device is supported per domain for now */
  915. if (omap_domain->iommu_dev != oiommu) {
  916. dev_err(dev, "invalid iommu device\n");
  917. return;
  918. }
  919. iopgtable_clear_entry_all(oiommu);
  920. omap_iommu_detach(oiommu);
  921. omap_domain->iommu_dev = NULL;
  922. omap_domain->dev = NULL;
  923. oiommu->domain = NULL;
  924. }
  925. static void omap_iommu_detach_dev(struct iommu_domain *domain,
  926. struct device *dev)
  927. {
  928. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  929. spin_lock(&omap_domain->lock);
  930. _omap_iommu_detach_dev(omap_domain, dev);
  931. spin_unlock(&omap_domain->lock);
  932. }
  933. static struct iommu_domain *omap_iommu_domain_alloc(unsigned type)
  934. {
  935. struct omap_iommu_domain *omap_domain;
  936. if (type != IOMMU_DOMAIN_UNMANAGED)
  937. return NULL;
  938. omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
  939. if (!omap_domain)
  940. goto out;
  941. omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
  942. if (!omap_domain->pgtable)
  943. goto fail_nomem;
  944. /*
  945. * should never fail, but please keep this around to ensure
  946. * we keep the hardware happy
  947. */
  948. if (WARN_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE)))
  949. goto fail_align;
  950. spin_lock_init(&omap_domain->lock);
  951. omap_domain->domain.geometry.aperture_start = 0;
  952. omap_domain->domain.geometry.aperture_end = (1ULL << 32) - 1;
  953. omap_domain->domain.geometry.force_aperture = true;
  954. return &omap_domain->domain;
  955. fail_align:
  956. kfree(omap_domain->pgtable);
  957. fail_nomem:
  958. kfree(omap_domain);
  959. out:
  960. return NULL;
  961. }
  962. static void omap_iommu_domain_free(struct iommu_domain *domain)
  963. {
  964. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  965. /*
  966. * An iommu device is still attached
  967. * (currently, only one device can be attached) ?
  968. */
  969. if (omap_domain->iommu_dev)
  970. _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
  971. kfree(omap_domain->pgtable);
  972. kfree(omap_domain);
  973. }
  974. static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
  975. dma_addr_t da)
  976. {
  977. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  978. struct omap_iommu *oiommu = omap_domain->iommu_dev;
  979. struct device *dev = oiommu->dev;
  980. u32 *pgd, *pte;
  981. phys_addr_t ret = 0;
  982. iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
  983. if (pte) {
  984. if (iopte_is_small(*pte))
  985. ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
  986. else if (iopte_is_large(*pte))
  987. ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
  988. else
  989. dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
  990. (unsigned long long)da);
  991. } else {
  992. if (iopgd_is_section(*pgd))
  993. ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
  994. else if (iopgd_is_super(*pgd))
  995. ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
  996. else
  997. dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
  998. (unsigned long long)da);
  999. }
  1000. return ret;
  1001. }
  1002. static int omap_iommu_add_device(struct device *dev)
  1003. {
  1004. struct omap_iommu_arch_data *arch_data;
  1005. struct omap_iommu *oiommu;
  1006. struct iommu_group *group;
  1007. struct device_node *np;
  1008. struct platform_device *pdev;
  1009. int ret;
  1010. /*
  1011. * Allocate the archdata iommu structure for DT-based devices.
  1012. *
  1013. * TODO: Simplify this when removing non-DT support completely from the
  1014. * IOMMU users.
  1015. */
  1016. if (!dev->of_node)
  1017. return 0;
  1018. np = of_parse_phandle(dev->of_node, "iommus", 0);
  1019. if (!np)
  1020. return 0;
  1021. pdev = of_find_device_by_node(np);
  1022. if (WARN_ON(!pdev)) {
  1023. of_node_put(np);
  1024. return -EINVAL;
  1025. }
  1026. oiommu = platform_get_drvdata(pdev);
  1027. if (!oiommu) {
  1028. of_node_put(np);
  1029. return -EINVAL;
  1030. }
  1031. arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
  1032. if (!arch_data) {
  1033. of_node_put(np);
  1034. return -ENOMEM;
  1035. }
  1036. ret = iommu_device_link(&oiommu->iommu, dev);
  1037. if (ret) {
  1038. kfree(arch_data);
  1039. of_node_put(np);
  1040. return ret;
  1041. }
  1042. arch_data->iommu_dev = oiommu;
  1043. dev->archdata.iommu = arch_data;
  1044. /*
  1045. * IOMMU group initialization calls into omap_iommu_device_group, which
  1046. * needs a valid dev->archdata.iommu pointer
  1047. */
  1048. group = iommu_group_get_for_dev(dev);
  1049. if (IS_ERR(group)) {
  1050. iommu_device_unlink(&oiommu->iommu, dev);
  1051. dev->archdata.iommu = NULL;
  1052. kfree(arch_data);
  1053. return PTR_ERR(group);
  1054. }
  1055. iommu_group_put(group);
  1056. of_node_put(np);
  1057. return 0;
  1058. }
  1059. static void omap_iommu_remove_device(struct device *dev)
  1060. {
  1061. struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
  1062. if (!dev->of_node || !arch_data)
  1063. return;
  1064. iommu_device_unlink(&arch_data->iommu_dev->iommu, dev);
  1065. iommu_group_remove_device(dev);
  1066. dev->archdata.iommu = NULL;
  1067. kfree(arch_data);
  1068. }
  1069. static struct iommu_group *omap_iommu_device_group(struct device *dev)
  1070. {
  1071. struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
  1072. struct iommu_group *group = ERR_PTR(-EINVAL);
  1073. if (arch_data->iommu_dev)
  1074. group = arch_data->iommu_dev->group;
  1075. return group;
  1076. }
  1077. static const struct iommu_ops omap_iommu_ops = {
  1078. .domain_alloc = omap_iommu_domain_alloc,
  1079. .domain_free = omap_iommu_domain_free,
  1080. .attach_dev = omap_iommu_attach_dev,
  1081. .detach_dev = omap_iommu_detach_dev,
  1082. .map = omap_iommu_map,
  1083. .unmap = omap_iommu_unmap,
  1084. .map_sg = default_iommu_map_sg,
  1085. .iova_to_phys = omap_iommu_iova_to_phys,
  1086. .add_device = omap_iommu_add_device,
  1087. .remove_device = omap_iommu_remove_device,
  1088. .device_group = omap_iommu_device_group,
  1089. .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
  1090. };
  1091. static int __init omap_iommu_init(void)
  1092. {
  1093. struct kmem_cache *p;
  1094. const unsigned long flags = SLAB_HWCACHE_ALIGN;
  1095. size_t align = 1 << 10; /* L2 pagetable alignement */
  1096. struct device_node *np;
  1097. int ret;
  1098. np = of_find_matching_node(NULL, omap_iommu_of_match);
  1099. if (!np)
  1100. return 0;
  1101. of_node_put(np);
  1102. p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
  1103. NULL);
  1104. if (!p)
  1105. return -ENOMEM;
  1106. iopte_cachep = p;
  1107. omap_iommu_debugfs_init();
  1108. ret = platform_driver_register(&omap_iommu_driver);
  1109. if (ret) {
  1110. pr_err("%s: failed to register driver\n", __func__);
  1111. goto fail_driver;
  1112. }
  1113. ret = bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
  1114. if (ret)
  1115. goto fail_bus;
  1116. return 0;
  1117. fail_bus:
  1118. platform_driver_unregister(&omap_iommu_driver);
  1119. fail_driver:
  1120. kmem_cache_destroy(iopte_cachep);
  1121. return ret;
  1122. }
  1123. subsys_initcall(omap_iommu_init);
  1124. /* must be ready before omap3isp is probed */