iommu.c 20 KB

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