omap-iommu.c 31 KB

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