omap-iommu.c 27 KB

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