omap-iommu.c 28 KB

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