iommu.c 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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 <asm/cacheflush.h>
  21. #include <plat/iommu.h>
  22. #include "iopgtable.h"
  23. #define for_each_iotlb_cr(obj, n, __i, cr) \
  24. for (__i = 0; \
  25. (__i < (n)) && (cr = __iotlb_read_cr((obj), __i), true); \
  26. __i++)
  27. /* accommodate the difference between omap1 and omap2/3 */
  28. static const struct iommu_functions *arch_iommu;
  29. static struct platform_driver omap_iommu_driver;
  30. static struct kmem_cache *iopte_cachep;
  31. /**
  32. * install_iommu_arch - Install archtecure specific iommu functions
  33. * @ops: a pointer to architecture specific iommu functions
  34. *
  35. * There are several kind of iommu algorithm(tlb, pagetable) among
  36. * omap series. This interface installs such an iommu algorighm.
  37. **/
  38. int install_iommu_arch(const struct iommu_functions *ops)
  39. {
  40. if (arch_iommu)
  41. return -EBUSY;
  42. arch_iommu = ops;
  43. return 0;
  44. }
  45. EXPORT_SYMBOL_GPL(install_iommu_arch);
  46. /**
  47. * uninstall_iommu_arch - Uninstall archtecure specific iommu functions
  48. * @ops: a pointer to architecture specific iommu functions
  49. *
  50. * This interface uninstalls the iommu algorighm installed previously.
  51. **/
  52. void uninstall_iommu_arch(const struct iommu_functions *ops)
  53. {
  54. if (arch_iommu != ops)
  55. pr_err("%s: not your arch\n", __func__);
  56. arch_iommu = NULL;
  57. }
  58. EXPORT_SYMBOL_GPL(uninstall_iommu_arch);
  59. /**
  60. * iommu_save_ctx - Save registers for pm off-mode support
  61. * @obj: target iommu
  62. **/
  63. void iommu_save_ctx(struct iommu *obj)
  64. {
  65. arch_iommu->save_ctx(obj);
  66. }
  67. EXPORT_SYMBOL_GPL(iommu_save_ctx);
  68. /**
  69. * iommu_restore_ctx - Restore registers for pm off-mode support
  70. * @obj: target iommu
  71. **/
  72. void iommu_restore_ctx(struct iommu *obj)
  73. {
  74. arch_iommu->restore_ctx(obj);
  75. }
  76. EXPORT_SYMBOL_GPL(iommu_restore_ctx);
  77. /**
  78. * iommu_arch_version - Return running iommu arch version
  79. **/
  80. u32 iommu_arch_version(void)
  81. {
  82. return arch_iommu->version;
  83. }
  84. EXPORT_SYMBOL_GPL(iommu_arch_version);
  85. static int iommu_enable(struct iommu *obj)
  86. {
  87. int err;
  88. if (!obj)
  89. return -EINVAL;
  90. clk_enable(obj->clk);
  91. err = arch_iommu->enable(obj);
  92. clk_disable(obj->clk);
  93. return err;
  94. }
  95. static void iommu_disable(struct iommu *obj)
  96. {
  97. if (!obj)
  98. return;
  99. clk_enable(obj->clk);
  100. arch_iommu->disable(obj);
  101. clk_disable(obj->clk);
  102. }
  103. /*
  104. * TLB operations
  105. */
  106. void iotlb_cr_to_e(struct cr_regs *cr, struct iotlb_entry *e)
  107. {
  108. BUG_ON(!cr || !e);
  109. arch_iommu->cr_to_e(cr, e);
  110. }
  111. EXPORT_SYMBOL_GPL(iotlb_cr_to_e);
  112. static inline int iotlb_cr_valid(struct cr_regs *cr)
  113. {
  114. if (!cr)
  115. return -EINVAL;
  116. return arch_iommu->cr_valid(cr);
  117. }
  118. static inline struct cr_regs *iotlb_alloc_cr(struct iommu *obj,
  119. struct iotlb_entry *e)
  120. {
  121. if (!e)
  122. return NULL;
  123. return arch_iommu->alloc_cr(obj, e);
  124. }
  125. u32 iotlb_cr_to_virt(struct cr_regs *cr)
  126. {
  127. return arch_iommu->cr_to_virt(cr);
  128. }
  129. EXPORT_SYMBOL_GPL(iotlb_cr_to_virt);
  130. static u32 get_iopte_attr(struct iotlb_entry *e)
  131. {
  132. return arch_iommu->get_pte_attr(e);
  133. }
  134. static u32 iommu_report_fault(struct iommu *obj, u32 *da)
  135. {
  136. return arch_iommu->fault_isr(obj, da);
  137. }
  138. static void iotlb_lock_get(struct iommu *obj, struct iotlb_lock *l)
  139. {
  140. u32 val;
  141. val = iommu_read_reg(obj, MMU_LOCK);
  142. l->base = MMU_LOCK_BASE(val);
  143. l->vict = MMU_LOCK_VICT(val);
  144. }
  145. static void iotlb_lock_set(struct iommu *obj, struct iotlb_lock *l)
  146. {
  147. u32 val;
  148. val = (l->base << MMU_LOCK_BASE_SHIFT);
  149. val |= (l->vict << MMU_LOCK_VICT_SHIFT);
  150. iommu_write_reg(obj, val, MMU_LOCK);
  151. }
  152. static void iotlb_read_cr(struct iommu *obj, struct cr_regs *cr)
  153. {
  154. arch_iommu->tlb_read_cr(obj, cr);
  155. }
  156. static void iotlb_load_cr(struct iommu *obj, struct cr_regs *cr)
  157. {
  158. arch_iommu->tlb_load_cr(obj, cr);
  159. iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
  160. iommu_write_reg(obj, 1, MMU_LD_TLB);
  161. }
  162. /**
  163. * iotlb_dump_cr - Dump an iommu tlb entry into buf
  164. * @obj: target iommu
  165. * @cr: contents of cam and ram register
  166. * @buf: output buffer
  167. **/
  168. static inline ssize_t iotlb_dump_cr(struct iommu *obj, struct cr_regs *cr,
  169. char *buf)
  170. {
  171. BUG_ON(!cr || !buf);
  172. return arch_iommu->dump_cr(obj, cr, buf);
  173. }
  174. /* only used in iotlb iteration for-loop */
  175. static struct cr_regs __iotlb_read_cr(struct iommu *obj, int n)
  176. {
  177. struct cr_regs cr;
  178. struct iotlb_lock l;
  179. iotlb_lock_get(obj, &l);
  180. l.vict = n;
  181. iotlb_lock_set(obj, &l);
  182. iotlb_read_cr(obj, &cr);
  183. return cr;
  184. }
  185. /**
  186. * load_iotlb_entry - Set an iommu tlb entry
  187. * @obj: target iommu
  188. * @e: an iommu tlb entry info
  189. **/
  190. int load_iotlb_entry(struct iommu *obj, struct iotlb_entry *e)
  191. {
  192. int err = 0;
  193. struct iotlb_lock l;
  194. struct cr_regs *cr;
  195. if (!obj || !obj->nr_tlb_entries || !e)
  196. return -EINVAL;
  197. clk_enable(obj->clk);
  198. iotlb_lock_get(obj, &l);
  199. if (l.base == obj->nr_tlb_entries) {
  200. dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
  201. err = -EBUSY;
  202. goto out;
  203. }
  204. if (!e->prsvd) {
  205. int i;
  206. struct cr_regs tmp;
  207. for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
  208. if (!iotlb_cr_valid(&tmp))
  209. break;
  210. if (i == obj->nr_tlb_entries) {
  211. dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
  212. err = -EBUSY;
  213. goto out;
  214. }
  215. iotlb_lock_get(obj, &l);
  216. } else {
  217. l.vict = l.base;
  218. iotlb_lock_set(obj, &l);
  219. }
  220. cr = iotlb_alloc_cr(obj, e);
  221. if (IS_ERR(cr)) {
  222. clk_disable(obj->clk);
  223. return PTR_ERR(cr);
  224. }
  225. iotlb_load_cr(obj, cr);
  226. kfree(cr);
  227. if (e->prsvd)
  228. l.base++;
  229. /* increment victim for next tlb load */
  230. if (++l.vict == obj->nr_tlb_entries)
  231. l.vict = l.base;
  232. iotlb_lock_set(obj, &l);
  233. out:
  234. clk_disable(obj->clk);
  235. return err;
  236. }
  237. EXPORT_SYMBOL_GPL(load_iotlb_entry);
  238. /**
  239. * flush_iotlb_page - Clear an iommu tlb entry
  240. * @obj: target iommu
  241. * @da: iommu device virtual address
  242. *
  243. * Clear an iommu tlb entry which includes 'da' address.
  244. **/
  245. void flush_iotlb_page(struct iommu *obj, u32 da)
  246. {
  247. int i;
  248. struct cr_regs cr;
  249. clk_enable(obj->clk);
  250. for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
  251. u32 start;
  252. size_t bytes;
  253. if (!iotlb_cr_valid(&cr))
  254. continue;
  255. start = iotlb_cr_to_virt(&cr);
  256. bytes = iopgsz_to_bytes(cr.cam & 3);
  257. if ((start <= da) && (da < start + bytes)) {
  258. dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
  259. __func__, start, da, bytes);
  260. iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
  261. }
  262. }
  263. clk_disable(obj->clk);
  264. if (i == obj->nr_tlb_entries)
  265. dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
  266. }
  267. EXPORT_SYMBOL_GPL(flush_iotlb_page);
  268. /**
  269. * flush_iotlb_range - Clear an iommu tlb entries
  270. * @obj: target iommu
  271. * @start: iommu device virtual address(start)
  272. * @end: iommu device virtual address(end)
  273. *
  274. * Clear an iommu tlb entry which includes 'da' address.
  275. **/
  276. void flush_iotlb_range(struct iommu *obj, u32 start, u32 end)
  277. {
  278. u32 da = start;
  279. while (da < end) {
  280. flush_iotlb_page(obj, da);
  281. /* FIXME: Optimize for multiple page size */
  282. da += IOPTE_SIZE;
  283. }
  284. }
  285. EXPORT_SYMBOL_GPL(flush_iotlb_range);
  286. /**
  287. * flush_iotlb_all - Clear all iommu tlb entries
  288. * @obj: target iommu
  289. **/
  290. void flush_iotlb_all(struct iommu *obj)
  291. {
  292. struct iotlb_lock l;
  293. clk_enable(obj->clk);
  294. l.base = 0;
  295. l.vict = 0;
  296. iotlb_lock_set(obj, &l);
  297. iommu_write_reg(obj, 1, MMU_GFLUSH);
  298. clk_disable(obj->clk);
  299. }
  300. EXPORT_SYMBOL_GPL(flush_iotlb_all);
  301. /**
  302. * iommu_set_twl - enable/disable table walking logic
  303. * @obj: target iommu
  304. * @on: enable/disable
  305. *
  306. * Function used to enable/disable TWL. If one wants to work
  307. * exclusively with locked TLB entries and receive notifications
  308. * for TLB miss then call this function to disable TWL.
  309. */
  310. void iommu_set_twl(struct iommu *obj, bool on)
  311. {
  312. clk_enable(obj->clk);
  313. arch_iommu->set_twl(obj, on);
  314. clk_disable(obj->clk);
  315. }
  316. EXPORT_SYMBOL_GPL(iommu_set_twl);
  317. #if defined(CONFIG_OMAP_IOMMU_DEBUG_MODULE)
  318. ssize_t iommu_dump_ctx(struct iommu *obj, char *buf, ssize_t bytes)
  319. {
  320. if (!obj || !buf)
  321. return -EINVAL;
  322. clk_enable(obj->clk);
  323. bytes = arch_iommu->dump_ctx(obj, buf, bytes);
  324. clk_disable(obj->clk);
  325. return bytes;
  326. }
  327. EXPORT_SYMBOL_GPL(iommu_dump_ctx);
  328. static int __dump_tlb_entries(struct iommu *obj, struct cr_regs *crs, int num)
  329. {
  330. int i;
  331. struct iotlb_lock saved;
  332. struct cr_regs tmp;
  333. struct cr_regs *p = crs;
  334. clk_enable(obj->clk);
  335. iotlb_lock_get(obj, &saved);
  336. for_each_iotlb_cr(obj, num, i, tmp) {
  337. if (!iotlb_cr_valid(&tmp))
  338. continue;
  339. *p++ = tmp;
  340. }
  341. iotlb_lock_set(obj, &saved);
  342. clk_disable(obj->clk);
  343. return p - crs;
  344. }
  345. /**
  346. * dump_tlb_entries - dump cr arrays to given buffer
  347. * @obj: target iommu
  348. * @buf: output buffer
  349. **/
  350. size_t dump_tlb_entries(struct iommu *obj, char *buf, ssize_t bytes)
  351. {
  352. int i, num;
  353. struct cr_regs *cr;
  354. char *p = buf;
  355. num = bytes / sizeof(*cr);
  356. num = min(obj->nr_tlb_entries, num);
  357. cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
  358. if (!cr)
  359. return 0;
  360. num = __dump_tlb_entries(obj, cr, num);
  361. for (i = 0; i < num; i++)
  362. p += iotlb_dump_cr(obj, cr + i, p);
  363. kfree(cr);
  364. return p - buf;
  365. }
  366. EXPORT_SYMBOL_GPL(dump_tlb_entries);
  367. int foreach_iommu_device(void *data, int (*fn)(struct device *, void *))
  368. {
  369. return driver_for_each_device(&omap_iommu_driver.driver,
  370. NULL, data, fn);
  371. }
  372. EXPORT_SYMBOL_GPL(foreach_iommu_device);
  373. #endif /* CONFIG_OMAP_IOMMU_DEBUG_MODULE */
  374. /*
  375. * H/W pagetable operations
  376. */
  377. static void flush_iopgd_range(u32 *first, u32 *last)
  378. {
  379. /* FIXME: L2 cache should be taken care of if it exists */
  380. do {
  381. asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
  382. : : "r" (first));
  383. first += L1_CACHE_BYTES / sizeof(*first);
  384. } while (first <= last);
  385. }
  386. static void flush_iopte_range(u32 *first, u32 *last)
  387. {
  388. /* FIXME: L2 cache should be taken care of if it exists */
  389. do {
  390. asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
  391. : : "r" (first));
  392. first += L1_CACHE_BYTES / sizeof(*first);
  393. } while (first <= last);
  394. }
  395. static void iopte_free(u32 *iopte)
  396. {
  397. /* Note: freed iopte's must be clean ready for re-use */
  398. kmem_cache_free(iopte_cachep, iopte);
  399. }
  400. static u32 *iopte_alloc(struct iommu *obj, u32 *iopgd, u32 da)
  401. {
  402. u32 *iopte;
  403. /* a table has already existed */
  404. if (*iopgd)
  405. goto pte_ready;
  406. /*
  407. * do the allocation outside the page table lock
  408. */
  409. spin_unlock(&obj->page_table_lock);
  410. iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
  411. spin_lock(&obj->page_table_lock);
  412. if (!*iopgd) {
  413. if (!iopte)
  414. return ERR_PTR(-ENOMEM);
  415. *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
  416. flush_iopgd_range(iopgd, iopgd);
  417. dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
  418. } else {
  419. /* We raced, free the reduniovant table */
  420. iopte_free(iopte);
  421. }
  422. pte_ready:
  423. iopte = iopte_offset(iopgd, da);
  424. dev_vdbg(obj->dev,
  425. "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
  426. __func__, da, iopgd, *iopgd, iopte, *iopte);
  427. return iopte;
  428. }
  429. static int iopgd_alloc_section(struct iommu *obj, u32 da, u32 pa, u32 prot)
  430. {
  431. u32 *iopgd = iopgd_offset(obj, da);
  432. if ((da | pa) & ~IOSECTION_MASK) {
  433. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  434. __func__, da, pa, IOSECTION_SIZE);
  435. return -EINVAL;
  436. }
  437. *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
  438. flush_iopgd_range(iopgd, iopgd);
  439. return 0;
  440. }
  441. static int iopgd_alloc_super(struct iommu *obj, u32 da, u32 pa, u32 prot)
  442. {
  443. u32 *iopgd = iopgd_offset(obj, da);
  444. int i;
  445. if ((da | pa) & ~IOSUPER_MASK) {
  446. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  447. __func__, da, pa, IOSUPER_SIZE);
  448. return -EINVAL;
  449. }
  450. for (i = 0; i < 16; i++)
  451. *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
  452. flush_iopgd_range(iopgd, iopgd + 15);
  453. return 0;
  454. }
  455. static int iopte_alloc_page(struct iommu *obj, u32 da, u32 pa, u32 prot)
  456. {
  457. u32 *iopgd = iopgd_offset(obj, da);
  458. u32 *iopte = iopte_alloc(obj, iopgd, da);
  459. if (IS_ERR(iopte))
  460. return PTR_ERR(iopte);
  461. *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
  462. flush_iopte_range(iopte, iopte);
  463. dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
  464. __func__, da, pa, iopte, *iopte);
  465. return 0;
  466. }
  467. static int iopte_alloc_large(struct iommu *obj, u32 da, u32 pa, u32 prot)
  468. {
  469. u32 *iopgd = iopgd_offset(obj, da);
  470. u32 *iopte = iopte_alloc(obj, iopgd, da);
  471. int i;
  472. if ((da | pa) & ~IOLARGE_MASK) {
  473. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  474. __func__, da, pa, IOLARGE_SIZE);
  475. return -EINVAL;
  476. }
  477. if (IS_ERR(iopte))
  478. return PTR_ERR(iopte);
  479. for (i = 0; i < 16; i++)
  480. *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
  481. flush_iopte_range(iopte, iopte + 15);
  482. return 0;
  483. }
  484. static int iopgtable_store_entry_core(struct iommu *obj, struct iotlb_entry *e)
  485. {
  486. int (*fn)(struct iommu *, u32, u32, u32);
  487. u32 prot;
  488. int err;
  489. if (!obj || !e)
  490. return -EINVAL;
  491. switch (e->pgsz) {
  492. case MMU_CAM_PGSZ_16M:
  493. fn = iopgd_alloc_super;
  494. break;
  495. case MMU_CAM_PGSZ_1M:
  496. fn = iopgd_alloc_section;
  497. break;
  498. case MMU_CAM_PGSZ_64K:
  499. fn = iopte_alloc_large;
  500. break;
  501. case MMU_CAM_PGSZ_4K:
  502. fn = iopte_alloc_page;
  503. break;
  504. default:
  505. fn = NULL;
  506. BUG();
  507. break;
  508. }
  509. prot = get_iopte_attr(e);
  510. spin_lock(&obj->page_table_lock);
  511. err = fn(obj, e->da, e->pa, prot);
  512. spin_unlock(&obj->page_table_lock);
  513. return err;
  514. }
  515. /**
  516. * iopgtable_store_entry - Make an iommu pte entry
  517. * @obj: target iommu
  518. * @e: an iommu tlb entry info
  519. **/
  520. int iopgtable_store_entry(struct iommu *obj, struct iotlb_entry *e)
  521. {
  522. int err;
  523. flush_iotlb_page(obj, e->da);
  524. err = iopgtable_store_entry_core(obj, e);
  525. #ifdef PREFETCH_IOTLB
  526. if (!err)
  527. load_iotlb_entry(obj, e);
  528. #endif
  529. return err;
  530. }
  531. EXPORT_SYMBOL_GPL(iopgtable_store_entry);
  532. /**
  533. * iopgtable_lookup_entry - Lookup an iommu pte entry
  534. * @obj: target iommu
  535. * @da: iommu device virtual address
  536. * @ppgd: iommu pgd entry pointer to be returned
  537. * @ppte: iommu pte entry pointer to be returned
  538. **/
  539. void iopgtable_lookup_entry(struct iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
  540. {
  541. u32 *iopgd, *iopte = NULL;
  542. iopgd = iopgd_offset(obj, da);
  543. if (!*iopgd)
  544. goto out;
  545. if (iopgd_is_table(*iopgd))
  546. iopte = iopte_offset(iopgd, da);
  547. out:
  548. *ppgd = iopgd;
  549. *ppte = iopte;
  550. }
  551. EXPORT_SYMBOL_GPL(iopgtable_lookup_entry);
  552. static size_t iopgtable_clear_entry_core(struct iommu *obj, u32 da)
  553. {
  554. size_t bytes;
  555. u32 *iopgd = iopgd_offset(obj, da);
  556. int nent = 1;
  557. if (!*iopgd)
  558. return 0;
  559. if (iopgd_is_table(*iopgd)) {
  560. int i;
  561. u32 *iopte = iopte_offset(iopgd, da);
  562. bytes = IOPTE_SIZE;
  563. if (*iopte & IOPTE_LARGE) {
  564. nent *= 16;
  565. /* rewind to the 1st entry */
  566. iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
  567. }
  568. bytes *= nent;
  569. memset(iopte, 0, nent * sizeof(*iopte));
  570. flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
  571. /*
  572. * do table walk to check if this table is necessary or not
  573. */
  574. iopte = iopte_offset(iopgd, 0);
  575. for (i = 0; i < PTRS_PER_IOPTE; i++)
  576. if (iopte[i])
  577. goto out;
  578. iopte_free(iopte);
  579. nent = 1; /* for the next L1 entry */
  580. } else {
  581. bytes = IOPGD_SIZE;
  582. if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
  583. nent *= 16;
  584. /* rewind to the 1st entry */
  585. iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
  586. }
  587. bytes *= nent;
  588. }
  589. memset(iopgd, 0, nent * sizeof(*iopgd));
  590. flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
  591. out:
  592. return bytes;
  593. }
  594. /**
  595. * iopgtable_clear_entry - Remove an iommu pte entry
  596. * @obj: target iommu
  597. * @da: iommu device virtual address
  598. **/
  599. size_t iopgtable_clear_entry(struct iommu *obj, u32 da)
  600. {
  601. size_t bytes;
  602. spin_lock(&obj->page_table_lock);
  603. bytes = iopgtable_clear_entry_core(obj, da);
  604. flush_iotlb_page(obj, da);
  605. spin_unlock(&obj->page_table_lock);
  606. return bytes;
  607. }
  608. EXPORT_SYMBOL_GPL(iopgtable_clear_entry);
  609. static void iopgtable_clear_entry_all(struct iommu *obj)
  610. {
  611. int i;
  612. spin_lock(&obj->page_table_lock);
  613. for (i = 0; i < PTRS_PER_IOPGD; i++) {
  614. u32 da;
  615. u32 *iopgd;
  616. da = i << IOPGD_SHIFT;
  617. iopgd = iopgd_offset(obj, da);
  618. if (!*iopgd)
  619. continue;
  620. if (iopgd_is_table(*iopgd))
  621. iopte_free(iopte_offset(iopgd, 0));
  622. *iopgd = 0;
  623. flush_iopgd_range(iopgd, iopgd);
  624. }
  625. flush_iotlb_all(obj);
  626. spin_unlock(&obj->page_table_lock);
  627. }
  628. /*
  629. * Device IOMMU generic operations
  630. */
  631. static irqreturn_t iommu_fault_handler(int irq, void *data)
  632. {
  633. u32 stat, da;
  634. u32 *iopgd, *iopte;
  635. int err = -EIO;
  636. struct iommu *obj = data;
  637. if (!obj->refcount)
  638. return IRQ_NONE;
  639. /* Dynamic loading TLB or PTE */
  640. if (obj->isr)
  641. err = obj->isr(obj);
  642. if (!err)
  643. return IRQ_HANDLED;
  644. clk_enable(obj->clk);
  645. stat = iommu_report_fault(obj, &da);
  646. clk_disable(obj->clk);
  647. if (!stat)
  648. return IRQ_HANDLED;
  649. iommu_disable(obj);
  650. iopgd = iopgd_offset(obj, da);
  651. if (!iopgd_is_table(*iopgd)) {
  652. dev_err(obj->dev, "%s: da:%08x pgd:%p *pgd:%08x\n", __func__,
  653. da, iopgd, *iopgd);
  654. return IRQ_NONE;
  655. }
  656. iopte = iopte_offset(iopgd, da);
  657. dev_err(obj->dev, "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
  658. __func__, da, iopgd, *iopgd, iopte, *iopte);
  659. return IRQ_NONE;
  660. }
  661. static int device_match_by_alias(struct device *dev, void *data)
  662. {
  663. struct iommu *obj = to_iommu(dev);
  664. const char *name = data;
  665. pr_debug("%s: %s %s\n", __func__, obj->name, name);
  666. return strcmp(obj->name, name) == 0;
  667. }
  668. /**
  669. * iommu_get - Get iommu handler
  670. * @name: target iommu name
  671. **/
  672. struct iommu *iommu_get(const char *name)
  673. {
  674. int err = -ENOMEM;
  675. struct device *dev;
  676. struct iommu *obj;
  677. dev = driver_find_device(&omap_iommu_driver.driver, NULL, (void *)name,
  678. device_match_by_alias);
  679. if (!dev)
  680. return ERR_PTR(-ENODEV);
  681. obj = to_iommu(dev);
  682. mutex_lock(&obj->iommu_lock);
  683. if (obj->refcount++ == 0) {
  684. err = iommu_enable(obj);
  685. if (err)
  686. goto err_enable;
  687. flush_iotlb_all(obj);
  688. }
  689. if (!try_module_get(obj->owner))
  690. goto err_module;
  691. mutex_unlock(&obj->iommu_lock);
  692. dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
  693. return obj;
  694. err_module:
  695. if (obj->refcount == 1)
  696. iommu_disable(obj);
  697. err_enable:
  698. obj->refcount--;
  699. mutex_unlock(&obj->iommu_lock);
  700. return ERR_PTR(err);
  701. }
  702. EXPORT_SYMBOL_GPL(iommu_get);
  703. /**
  704. * iommu_put - Put back iommu handler
  705. * @obj: target iommu
  706. **/
  707. void iommu_put(struct iommu *obj)
  708. {
  709. if (!obj || IS_ERR(obj))
  710. return;
  711. mutex_lock(&obj->iommu_lock);
  712. if (--obj->refcount == 0)
  713. iommu_disable(obj);
  714. module_put(obj->owner);
  715. mutex_unlock(&obj->iommu_lock);
  716. dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
  717. }
  718. EXPORT_SYMBOL_GPL(iommu_put);
  719. /*
  720. * OMAP Device MMU(IOMMU) detection
  721. */
  722. static int __devinit omap_iommu_probe(struct platform_device *pdev)
  723. {
  724. int err = -ENODEV;
  725. void *p;
  726. int irq;
  727. struct iommu *obj;
  728. struct resource *res;
  729. struct iommu_platform_data *pdata = pdev->dev.platform_data;
  730. if (pdev->num_resources != 2)
  731. return -EINVAL;
  732. obj = kzalloc(sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
  733. if (!obj)
  734. return -ENOMEM;
  735. obj->clk = clk_get(&pdev->dev, pdata->clk_name);
  736. if (IS_ERR(obj->clk))
  737. goto err_clk;
  738. obj->nr_tlb_entries = pdata->nr_tlb_entries;
  739. obj->name = pdata->name;
  740. obj->dev = &pdev->dev;
  741. obj->ctx = (void *)obj + sizeof(*obj);
  742. mutex_init(&obj->iommu_lock);
  743. mutex_init(&obj->mmap_lock);
  744. spin_lock_init(&obj->page_table_lock);
  745. INIT_LIST_HEAD(&obj->mmap);
  746. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  747. if (!res) {
  748. err = -ENODEV;
  749. goto err_mem;
  750. }
  751. obj->regbase = ioremap(res->start, resource_size(res));
  752. if (!obj->regbase) {
  753. err = -ENOMEM;
  754. goto err_mem;
  755. }
  756. res = request_mem_region(res->start, resource_size(res),
  757. dev_name(&pdev->dev));
  758. if (!res) {
  759. err = -EIO;
  760. goto err_mem;
  761. }
  762. irq = platform_get_irq(pdev, 0);
  763. if (irq < 0) {
  764. err = -ENODEV;
  765. goto err_irq;
  766. }
  767. err = request_irq(irq, iommu_fault_handler, IRQF_SHARED,
  768. dev_name(&pdev->dev), obj);
  769. if (err < 0)
  770. goto err_irq;
  771. platform_set_drvdata(pdev, obj);
  772. p = (void *)__get_free_pages(GFP_KERNEL, get_order(IOPGD_TABLE_SIZE));
  773. if (!p) {
  774. err = -ENOMEM;
  775. goto err_pgd;
  776. }
  777. memset(p, 0, IOPGD_TABLE_SIZE);
  778. clean_dcache_area(p, IOPGD_TABLE_SIZE);
  779. obj->iopgd = p;
  780. BUG_ON(!IS_ALIGNED((unsigned long)obj->iopgd, IOPGD_TABLE_SIZE));
  781. dev_info(&pdev->dev, "%s registered\n", obj->name);
  782. return 0;
  783. err_pgd:
  784. free_irq(irq, obj);
  785. err_irq:
  786. release_mem_region(res->start, resource_size(res));
  787. iounmap(obj->regbase);
  788. err_mem:
  789. clk_put(obj->clk);
  790. err_clk:
  791. kfree(obj);
  792. return err;
  793. }
  794. static int __devexit omap_iommu_remove(struct platform_device *pdev)
  795. {
  796. int irq;
  797. struct resource *res;
  798. struct iommu *obj = platform_get_drvdata(pdev);
  799. platform_set_drvdata(pdev, NULL);
  800. iopgtable_clear_entry_all(obj);
  801. free_pages((unsigned long)obj->iopgd, get_order(IOPGD_TABLE_SIZE));
  802. irq = platform_get_irq(pdev, 0);
  803. free_irq(irq, obj);
  804. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  805. release_mem_region(res->start, resource_size(res));
  806. iounmap(obj->regbase);
  807. clk_put(obj->clk);
  808. dev_info(&pdev->dev, "%s removed\n", obj->name);
  809. kfree(obj);
  810. return 0;
  811. }
  812. static struct platform_driver omap_iommu_driver = {
  813. .probe = omap_iommu_probe,
  814. .remove = __devexit_p(omap_iommu_remove),
  815. .driver = {
  816. .name = "omap-iommu",
  817. },
  818. };
  819. static void iopte_cachep_ctor(void *iopte)
  820. {
  821. clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
  822. }
  823. static int __init omap_iommu_init(void)
  824. {
  825. struct kmem_cache *p;
  826. const unsigned long flags = SLAB_HWCACHE_ALIGN;
  827. size_t align = 1 << 10; /* L2 pagetable alignement */
  828. p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
  829. iopte_cachep_ctor);
  830. if (!p)
  831. return -ENOMEM;
  832. iopte_cachep = p;
  833. return platform_driver_register(&omap_iommu_driver);
  834. }
  835. module_init(omap_iommu_init);
  836. static void __exit omap_iommu_exit(void)
  837. {
  838. kmem_cache_destroy(iopte_cachep);
  839. platform_driver_unregister(&omap_iommu_driver);
  840. }
  841. module_exit(omap_iommu_exit);
  842. MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives");
  843. MODULE_ALIAS("platform:omap-iommu");
  844. MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
  845. MODULE_LICENSE("GPL v2");