omap-iommu.c 29 KB

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