iommu.c 22 KB

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