omap-iommu.c 30 KB

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