omap-iommu.c 41 KB

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