omap-iommu.c 36 KB

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