iommu.c 22 KB

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