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