iommu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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. #if defined(CONFIG_OMAP_IOMMU_DEBUG_MODULE)
  302. ssize_t iommu_dump_ctx(struct iommu *obj, char *buf, ssize_t bytes)
  303. {
  304. if (!obj || !buf)
  305. return -EINVAL;
  306. clk_enable(obj->clk);
  307. bytes = arch_iommu->dump_ctx(obj, buf, bytes);
  308. clk_disable(obj->clk);
  309. return bytes;
  310. }
  311. EXPORT_SYMBOL_GPL(iommu_dump_ctx);
  312. static int __dump_tlb_entries(struct iommu *obj, struct cr_regs *crs, int num)
  313. {
  314. int i;
  315. struct iotlb_lock saved;
  316. struct cr_regs tmp;
  317. struct cr_regs *p = crs;
  318. clk_enable(obj->clk);
  319. iotlb_lock_get(obj, &saved);
  320. for_each_iotlb_cr(obj, num, i, tmp) {
  321. if (!iotlb_cr_valid(&tmp))
  322. continue;
  323. *p++ = tmp;
  324. }
  325. iotlb_lock_set(obj, &saved);
  326. clk_disable(obj->clk);
  327. return p - crs;
  328. }
  329. /**
  330. * dump_tlb_entries - dump cr arrays to given buffer
  331. * @obj: target iommu
  332. * @buf: output buffer
  333. **/
  334. size_t dump_tlb_entries(struct iommu *obj, char *buf, ssize_t bytes)
  335. {
  336. int i, num;
  337. struct cr_regs *cr;
  338. char *p = buf;
  339. num = bytes / sizeof(*cr);
  340. num = min(obj->nr_tlb_entries, num);
  341. cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
  342. if (!cr)
  343. return 0;
  344. num = __dump_tlb_entries(obj, cr, num);
  345. for (i = 0; i < num; i++)
  346. p += iotlb_dump_cr(obj, cr + i, p);
  347. kfree(cr);
  348. return p - buf;
  349. }
  350. EXPORT_SYMBOL_GPL(dump_tlb_entries);
  351. int foreach_iommu_device(void *data, int (*fn)(struct device *, void *))
  352. {
  353. return driver_for_each_device(&omap_iommu_driver.driver,
  354. NULL, data, fn);
  355. }
  356. EXPORT_SYMBOL_GPL(foreach_iommu_device);
  357. #endif /* CONFIG_OMAP_IOMMU_DEBUG_MODULE */
  358. /*
  359. * H/W pagetable operations
  360. */
  361. static void flush_iopgd_range(u32 *first, u32 *last)
  362. {
  363. /* FIXME: L2 cache should be taken care of if it exists */
  364. do {
  365. asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
  366. : : "r" (first));
  367. first += L1_CACHE_BYTES / sizeof(*first);
  368. } while (first <= last);
  369. }
  370. static void flush_iopte_range(u32 *first, u32 *last)
  371. {
  372. /* FIXME: L2 cache should be taken care of if it exists */
  373. do {
  374. asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
  375. : : "r" (first));
  376. first += L1_CACHE_BYTES / sizeof(*first);
  377. } while (first <= last);
  378. }
  379. static void iopte_free(u32 *iopte)
  380. {
  381. /* Note: freed iopte's must be clean ready for re-use */
  382. kmem_cache_free(iopte_cachep, iopte);
  383. }
  384. static u32 *iopte_alloc(struct iommu *obj, u32 *iopgd, u32 da)
  385. {
  386. u32 *iopte;
  387. /* a table has already existed */
  388. if (*iopgd)
  389. goto pte_ready;
  390. /*
  391. * do the allocation outside the page table lock
  392. */
  393. spin_unlock(&obj->page_table_lock);
  394. iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
  395. spin_lock(&obj->page_table_lock);
  396. if (!*iopgd) {
  397. if (!iopte)
  398. return ERR_PTR(-ENOMEM);
  399. *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
  400. flush_iopgd_range(iopgd, iopgd);
  401. dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
  402. } else {
  403. /* We raced, free the reduniovant table */
  404. iopte_free(iopte);
  405. }
  406. pte_ready:
  407. iopte = iopte_offset(iopgd, da);
  408. dev_vdbg(obj->dev,
  409. "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
  410. __func__, da, iopgd, *iopgd, iopte, *iopte);
  411. return iopte;
  412. }
  413. static int iopgd_alloc_section(struct iommu *obj, u32 da, u32 pa, u32 prot)
  414. {
  415. u32 *iopgd = iopgd_offset(obj, da);
  416. if ((da | pa) & ~IOSECTION_MASK) {
  417. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  418. __func__, da, pa, IOSECTION_SIZE);
  419. return -EINVAL;
  420. }
  421. *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
  422. flush_iopgd_range(iopgd, iopgd);
  423. return 0;
  424. }
  425. static int iopgd_alloc_super(struct iommu *obj, u32 da, u32 pa, u32 prot)
  426. {
  427. u32 *iopgd = iopgd_offset(obj, da);
  428. int i;
  429. if ((da | pa) & ~IOSUPER_MASK) {
  430. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  431. __func__, da, pa, IOSUPER_SIZE);
  432. return -EINVAL;
  433. }
  434. for (i = 0; i < 16; i++)
  435. *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
  436. flush_iopgd_range(iopgd, iopgd + 15);
  437. return 0;
  438. }
  439. static int iopte_alloc_page(struct iommu *obj, u32 da, u32 pa, u32 prot)
  440. {
  441. u32 *iopgd = iopgd_offset(obj, da);
  442. u32 *iopte = iopte_alloc(obj, iopgd, da);
  443. if (IS_ERR(iopte))
  444. return PTR_ERR(iopte);
  445. *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
  446. flush_iopte_range(iopte, iopte);
  447. dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
  448. __func__, da, pa, iopte, *iopte);
  449. return 0;
  450. }
  451. static int iopte_alloc_large(struct iommu *obj, u32 da, u32 pa, u32 prot)
  452. {
  453. u32 *iopgd = iopgd_offset(obj, da);
  454. u32 *iopte = iopte_alloc(obj, iopgd, da);
  455. int i;
  456. if ((da | pa) & ~IOLARGE_MASK) {
  457. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  458. __func__, da, pa, IOLARGE_SIZE);
  459. return -EINVAL;
  460. }
  461. if (IS_ERR(iopte))
  462. return PTR_ERR(iopte);
  463. for (i = 0; i < 16; i++)
  464. *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
  465. flush_iopte_range(iopte, iopte + 15);
  466. return 0;
  467. }
  468. static int iopgtable_store_entry_core(struct iommu *obj, struct iotlb_entry *e)
  469. {
  470. int (*fn)(struct iommu *, u32, u32, u32);
  471. u32 prot;
  472. int err;
  473. if (!obj || !e)
  474. return -EINVAL;
  475. switch (e->pgsz) {
  476. case MMU_CAM_PGSZ_16M:
  477. fn = iopgd_alloc_super;
  478. break;
  479. case MMU_CAM_PGSZ_1M:
  480. fn = iopgd_alloc_section;
  481. break;
  482. case MMU_CAM_PGSZ_64K:
  483. fn = iopte_alloc_large;
  484. break;
  485. case MMU_CAM_PGSZ_4K:
  486. fn = iopte_alloc_page;
  487. break;
  488. default:
  489. fn = NULL;
  490. BUG();
  491. break;
  492. }
  493. prot = get_iopte_attr(e);
  494. spin_lock(&obj->page_table_lock);
  495. err = fn(obj, e->da, e->pa, prot);
  496. spin_unlock(&obj->page_table_lock);
  497. return err;
  498. }
  499. /**
  500. * iopgtable_store_entry - Make an iommu pte entry
  501. * @obj: target iommu
  502. * @e: an iommu tlb entry info
  503. **/
  504. int iopgtable_store_entry(struct iommu *obj, struct iotlb_entry *e)
  505. {
  506. int err;
  507. flush_iotlb_page(obj, e->da);
  508. err = iopgtable_store_entry_core(obj, e);
  509. #ifdef PREFETCH_IOTLB
  510. if (!err)
  511. load_iotlb_entry(obj, e);
  512. #endif
  513. return err;
  514. }
  515. EXPORT_SYMBOL_GPL(iopgtable_store_entry);
  516. /**
  517. * iopgtable_lookup_entry - Lookup an iommu pte entry
  518. * @obj: target iommu
  519. * @da: iommu device virtual address
  520. * @ppgd: iommu pgd entry pointer to be returned
  521. * @ppte: iommu pte entry pointer to be returned
  522. **/
  523. void iopgtable_lookup_entry(struct iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
  524. {
  525. u32 *iopgd, *iopte = NULL;
  526. iopgd = iopgd_offset(obj, da);
  527. if (!*iopgd)
  528. goto out;
  529. if (*iopgd & IOPGD_TABLE)
  530. iopte = iopte_offset(iopgd, da);
  531. out:
  532. *ppgd = iopgd;
  533. *ppte = iopte;
  534. }
  535. EXPORT_SYMBOL_GPL(iopgtable_lookup_entry);
  536. static size_t iopgtable_clear_entry_core(struct iommu *obj, u32 da)
  537. {
  538. size_t bytes;
  539. u32 *iopgd = iopgd_offset(obj, da);
  540. int nent = 1;
  541. if (!*iopgd)
  542. return 0;
  543. if (*iopgd & IOPGD_TABLE) {
  544. int i;
  545. u32 *iopte = iopte_offset(iopgd, da);
  546. bytes = IOPTE_SIZE;
  547. if (*iopte & IOPTE_LARGE) {
  548. nent *= 16;
  549. /* rewind to the 1st entry */
  550. iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
  551. }
  552. bytes *= nent;
  553. memset(iopte, 0, nent * sizeof(*iopte));
  554. flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
  555. /*
  556. * do table walk to check if this table is necessary or not
  557. */
  558. iopte = iopte_offset(iopgd, 0);
  559. for (i = 0; i < PTRS_PER_IOPTE; i++)
  560. if (iopte[i])
  561. goto out;
  562. iopte_free(iopte);
  563. nent = 1; /* for the next L1 entry */
  564. } else {
  565. bytes = IOPGD_SIZE;
  566. if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
  567. nent *= 16;
  568. /* rewind to the 1st entry */
  569. iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
  570. }
  571. bytes *= nent;
  572. }
  573. memset(iopgd, 0, nent * sizeof(*iopgd));
  574. flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
  575. out:
  576. return bytes;
  577. }
  578. /**
  579. * iopgtable_clear_entry - Remove an iommu pte entry
  580. * @obj: target iommu
  581. * @da: iommu device virtual address
  582. **/
  583. size_t iopgtable_clear_entry(struct iommu *obj, u32 da)
  584. {
  585. size_t bytes;
  586. spin_lock(&obj->page_table_lock);
  587. bytes = iopgtable_clear_entry_core(obj, da);
  588. flush_iotlb_page(obj, da);
  589. spin_unlock(&obj->page_table_lock);
  590. return bytes;
  591. }
  592. EXPORT_SYMBOL_GPL(iopgtable_clear_entry);
  593. static void iopgtable_clear_entry_all(struct iommu *obj)
  594. {
  595. int i;
  596. spin_lock(&obj->page_table_lock);
  597. for (i = 0; i < PTRS_PER_IOPGD; i++) {
  598. u32 da;
  599. u32 *iopgd;
  600. da = i << IOPGD_SHIFT;
  601. iopgd = iopgd_offset(obj, da);
  602. if (!*iopgd)
  603. continue;
  604. if (*iopgd & IOPGD_TABLE)
  605. iopte_free(iopte_offset(iopgd, 0));
  606. *iopgd = 0;
  607. flush_iopgd_range(iopgd, iopgd);
  608. }
  609. flush_iotlb_all(obj);
  610. spin_unlock(&obj->page_table_lock);
  611. }
  612. /*
  613. * Device IOMMU generic operations
  614. */
  615. static irqreturn_t iommu_fault_handler(int irq, void *data)
  616. {
  617. u32 stat, da;
  618. u32 *iopgd, *iopte;
  619. int err = -EIO;
  620. struct iommu *obj = data;
  621. if (!obj->refcount)
  622. return IRQ_NONE;
  623. /* Dynamic loading TLB or PTE */
  624. if (obj->isr)
  625. err = obj->isr(obj);
  626. if (!err)
  627. return IRQ_HANDLED;
  628. clk_enable(obj->clk);
  629. stat = iommu_report_fault(obj, &da);
  630. clk_disable(obj->clk);
  631. if (!stat)
  632. return IRQ_HANDLED;
  633. iopgd = iopgd_offset(obj, da);
  634. if (!(*iopgd & IOPGD_TABLE)) {
  635. dev_err(obj->dev, "%s: da:%08x pgd:%p *pgd:%08x\n", __func__,
  636. da, iopgd, *iopgd);
  637. return IRQ_NONE;
  638. }
  639. iopte = iopte_offset(iopgd, da);
  640. dev_err(obj->dev, "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
  641. __func__, da, iopgd, *iopgd, iopte, *iopte);
  642. return IRQ_NONE;
  643. }
  644. static int device_match_by_alias(struct device *dev, void *data)
  645. {
  646. struct iommu *obj = to_iommu(dev);
  647. const char *name = data;
  648. pr_debug("%s: %s %s\n", __func__, obj->name, name);
  649. return strcmp(obj->name, name) == 0;
  650. }
  651. /**
  652. * iommu_get - Get iommu handler
  653. * @name: target iommu name
  654. **/
  655. struct iommu *iommu_get(const char *name)
  656. {
  657. int err = -ENOMEM;
  658. struct device *dev;
  659. struct iommu *obj;
  660. dev = driver_find_device(&omap_iommu_driver.driver, NULL, (void *)name,
  661. device_match_by_alias);
  662. if (!dev)
  663. return ERR_PTR(-ENODEV);
  664. obj = to_iommu(dev);
  665. mutex_lock(&obj->iommu_lock);
  666. if (obj->refcount++ == 0) {
  667. err = iommu_enable(obj);
  668. if (err)
  669. goto err_enable;
  670. flush_iotlb_all(obj);
  671. }
  672. if (!try_module_get(obj->owner))
  673. goto err_module;
  674. mutex_unlock(&obj->iommu_lock);
  675. dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
  676. return obj;
  677. err_module:
  678. if (obj->refcount == 1)
  679. iommu_disable(obj);
  680. err_enable:
  681. obj->refcount--;
  682. mutex_unlock(&obj->iommu_lock);
  683. return ERR_PTR(err);
  684. }
  685. EXPORT_SYMBOL_GPL(iommu_get);
  686. /**
  687. * iommu_put - Put back iommu handler
  688. * @obj: target iommu
  689. **/
  690. void iommu_put(struct iommu *obj)
  691. {
  692. if (!obj || IS_ERR(obj))
  693. return;
  694. mutex_lock(&obj->iommu_lock);
  695. if (--obj->refcount == 0)
  696. iommu_disable(obj);
  697. module_put(obj->owner);
  698. mutex_unlock(&obj->iommu_lock);
  699. dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
  700. }
  701. EXPORT_SYMBOL_GPL(iommu_put);
  702. /*
  703. * OMAP Device MMU(IOMMU) detection
  704. */
  705. static int __devinit omap_iommu_probe(struct platform_device *pdev)
  706. {
  707. int err = -ENODEV;
  708. void *p;
  709. int irq;
  710. struct iommu *obj;
  711. struct resource *res;
  712. struct iommu_platform_data *pdata = pdev->dev.platform_data;
  713. if (pdev->num_resources != 2)
  714. return -EINVAL;
  715. obj = kzalloc(sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
  716. if (!obj)
  717. return -ENOMEM;
  718. obj->clk = clk_get(&pdev->dev, pdata->clk_name);
  719. if (IS_ERR(obj->clk))
  720. goto err_clk;
  721. obj->nr_tlb_entries = pdata->nr_tlb_entries;
  722. obj->name = pdata->name;
  723. obj->dev = &pdev->dev;
  724. obj->ctx = (void *)obj + sizeof(*obj);
  725. mutex_init(&obj->iommu_lock);
  726. mutex_init(&obj->mmap_lock);
  727. spin_lock_init(&obj->page_table_lock);
  728. INIT_LIST_HEAD(&obj->mmap);
  729. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  730. if (!res) {
  731. err = -ENODEV;
  732. goto err_mem;
  733. }
  734. obj->regbase = ioremap(res->start, resource_size(res));
  735. if (!obj->regbase) {
  736. err = -ENOMEM;
  737. goto err_mem;
  738. }
  739. res = request_mem_region(res->start, resource_size(res),
  740. dev_name(&pdev->dev));
  741. if (!res) {
  742. err = -EIO;
  743. goto err_mem;
  744. }
  745. irq = platform_get_irq(pdev, 0);
  746. if (irq < 0) {
  747. err = -ENODEV;
  748. goto err_irq;
  749. }
  750. err = request_irq(irq, iommu_fault_handler, IRQF_SHARED,
  751. dev_name(&pdev->dev), obj);
  752. if (err < 0)
  753. goto err_irq;
  754. platform_set_drvdata(pdev, obj);
  755. p = (void *)__get_free_pages(GFP_KERNEL, get_order(IOPGD_TABLE_SIZE));
  756. if (!p) {
  757. err = -ENOMEM;
  758. goto err_pgd;
  759. }
  760. memset(p, 0, IOPGD_TABLE_SIZE);
  761. clean_dcache_area(p, IOPGD_TABLE_SIZE);
  762. obj->iopgd = p;
  763. BUG_ON(!IS_ALIGNED((unsigned long)obj->iopgd, IOPGD_TABLE_SIZE));
  764. dev_info(&pdev->dev, "%s registered\n", obj->name);
  765. return 0;
  766. err_pgd:
  767. free_irq(irq, obj);
  768. err_irq:
  769. release_mem_region(res->start, resource_size(res));
  770. iounmap(obj->regbase);
  771. err_mem:
  772. clk_put(obj->clk);
  773. err_clk:
  774. kfree(obj);
  775. return err;
  776. }
  777. static int __devexit omap_iommu_remove(struct platform_device *pdev)
  778. {
  779. int irq;
  780. struct resource *res;
  781. struct iommu *obj = platform_get_drvdata(pdev);
  782. platform_set_drvdata(pdev, NULL);
  783. iopgtable_clear_entry_all(obj);
  784. free_pages((unsigned long)obj->iopgd, get_order(IOPGD_TABLE_SIZE));
  785. irq = platform_get_irq(pdev, 0);
  786. free_irq(irq, obj);
  787. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  788. release_mem_region(res->start, resource_size(res));
  789. iounmap(obj->regbase);
  790. clk_put(obj->clk);
  791. dev_info(&pdev->dev, "%s removed\n", obj->name);
  792. kfree(obj);
  793. return 0;
  794. }
  795. static struct platform_driver omap_iommu_driver = {
  796. .probe = omap_iommu_probe,
  797. .remove = __devexit_p(omap_iommu_remove),
  798. .driver = {
  799. .name = "omap-iommu",
  800. },
  801. };
  802. static void iopte_cachep_ctor(void *iopte)
  803. {
  804. clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
  805. }
  806. static int __init omap_iommu_init(void)
  807. {
  808. struct kmem_cache *p;
  809. const unsigned long flags = SLAB_HWCACHE_ALIGN;
  810. size_t align = 1 << 10; /* L2 pagetable alignement */
  811. p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
  812. iopte_cachep_ctor);
  813. if (!p)
  814. return -ENOMEM;
  815. iopte_cachep = p;
  816. return platform_driver_register(&omap_iommu_driver);
  817. }
  818. module_init(omap_iommu_init);
  819. static void __exit omap_iommu_exit(void)
  820. {
  821. kmem_cache_destroy(iopte_cachep);
  822. platform_driver_unregister(&omap_iommu_driver);
  823. }
  824. module_exit(omap_iommu_exit);
  825. MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives");
  826. MODULE_ALIAS("platform:omap-iommu");
  827. MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
  828. MODULE_LICENSE("GPL v2");