omap-iommu.c 28 KB

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