iommu.c 20 KB

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