omap-iommu.c 28 KB

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