omap-iommu.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  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 <linux/iommu.h>
  21. #include <linux/mutex.h>
  22. #include <linux/spinlock.h>
  23. #include <asm/cacheflush.h>
  24. #include <plat/iommu.h>
  25. #include <plat/iopgtable.h>
  26. #define for_each_iotlb_cr(obj, n, __i, cr) \
  27. for (__i = 0; \
  28. (__i < (n)) && (cr = __iotlb_read_cr((obj), __i), true); \
  29. __i++)
  30. /* bitmap of the page sizes currently supported */
  31. #define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
  32. /**
  33. * struct omap_iommu_domain - omap iommu domain
  34. * @pgtable: the page table
  35. * @iommu_dev: an omap iommu device attached to this domain. only a single
  36. * iommu device can be attached for now.
  37. * @lock: domain lock, should be taken when attaching/detaching
  38. */
  39. struct omap_iommu_domain {
  40. u32 *pgtable;
  41. struct omap_iommu *iommu_dev;
  42. spinlock_t lock;
  43. };
  44. /* accommodate the difference between omap1 and omap2/3 */
  45. static const struct iommu_functions *arch_iommu;
  46. static struct platform_driver omap_iommu_driver;
  47. static struct kmem_cache *iopte_cachep;
  48. /**
  49. * omap_install_iommu_arch - Install archtecure specific iommu functions
  50. * @ops: a pointer to architecture specific iommu functions
  51. *
  52. * There are several kind of iommu algorithm(tlb, pagetable) among
  53. * omap series. This interface installs such an iommu algorighm.
  54. **/
  55. int omap_install_iommu_arch(const struct iommu_functions *ops)
  56. {
  57. if (arch_iommu)
  58. return -EBUSY;
  59. arch_iommu = ops;
  60. return 0;
  61. }
  62. EXPORT_SYMBOL_GPL(omap_install_iommu_arch);
  63. /**
  64. * omap_uninstall_iommu_arch - Uninstall archtecure specific iommu functions
  65. * @ops: a pointer to architecture specific iommu functions
  66. *
  67. * This interface uninstalls the iommu algorighm installed previously.
  68. **/
  69. void omap_uninstall_iommu_arch(const struct iommu_functions *ops)
  70. {
  71. if (arch_iommu != ops)
  72. pr_err("%s: not your arch\n", __func__);
  73. arch_iommu = NULL;
  74. }
  75. EXPORT_SYMBOL_GPL(omap_uninstall_iommu_arch);
  76. /**
  77. * omap_iommu_save_ctx - Save registers for pm off-mode support
  78. * @dev: client device
  79. **/
  80. void omap_iommu_save_ctx(struct device *dev)
  81. {
  82. struct omap_iommu *obj = dev_to_omap_iommu(dev);
  83. arch_iommu->save_ctx(obj);
  84. }
  85. EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
  86. /**
  87. * omap_iommu_restore_ctx - Restore registers for pm off-mode support
  88. * @dev: client device
  89. **/
  90. void omap_iommu_restore_ctx(struct device *dev)
  91. {
  92. struct omap_iommu *obj = dev_to_omap_iommu(dev);
  93. arch_iommu->restore_ctx(obj);
  94. }
  95. EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
  96. /**
  97. * omap_iommu_arch_version - Return running iommu arch version
  98. **/
  99. u32 omap_iommu_arch_version(void)
  100. {
  101. return arch_iommu->version;
  102. }
  103. EXPORT_SYMBOL_GPL(omap_iommu_arch_version);
  104. static int iommu_enable(struct omap_iommu *obj)
  105. {
  106. int err;
  107. if (!obj)
  108. return -EINVAL;
  109. if (!arch_iommu)
  110. return -ENODEV;
  111. clk_enable(obj->clk);
  112. err = arch_iommu->enable(obj);
  113. clk_disable(obj->clk);
  114. return err;
  115. }
  116. static void iommu_disable(struct omap_iommu *obj)
  117. {
  118. if (!obj)
  119. return;
  120. clk_enable(obj->clk);
  121. arch_iommu->disable(obj);
  122. clk_disable(obj->clk);
  123. }
  124. /*
  125. * TLB operations
  126. */
  127. void omap_iotlb_cr_to_e(struct cr_regs *cr, struct iotlb_entry *e)
  128. {
  129. BUG_ON(!cr || !e);
  130. arch_iommu->cr_to_e(cr, e);
  131. }
  132. EXPORT_SYMBOL_GPL(omap_iotlb_cr_to_e);
  133. static inline int iotlb_cr_valid(struct cr_regs *cr)
  134. {
  135. if (!cr)
  136. return -EINVAL;
  137. return arch_iommu->cr_valid(cr);
  138. }
  139. static inline struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
  140. struct iotlb_entry *e)
  141. {
  142. if (!e)
  143. return NULL;
  144. return arch_iommu->alloc_cr(obj, e);
  145. }
  146. static u32 iotlb_cr_to_virt(struct cr_regs *cr)
  147. {
  148. return arch_iommu->cr_to_virt(cr);
  149. }
  150. static u32 get_iopte_attr(struct iotlb_entry *e)
  151. {
  152. return arch_iommu->get_pte_attr(e);
  153. }
  154. static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
  155. {
  156. return arch_iommu->fault_isr(obj, da);
  157. }
  158. static void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
  159. {
  160. u32 val;
  161. val = iommu_read_reg(obj, MMU_LOCK);
  162. l->base = MMU_LOCK_BASE(val);
  163. l->vict = MMU_LOCK_VICT(val);
  164. }
  165. static void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
  166. {
  167. u32 val;
  168. val = (l->base << MMU_LOCK_BASE_SHIFT);
  169. val |= (l->vict << MMU_LOCK_VICT_SHIFT);
  170. iommu_write_reg(obj, val, MMU_LOCK);
  171. }
  172. static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
  173. {
  174. arch_iommu->tlb_read_cr(obj, cr);
  175. }
  176. static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
  177. {
  178. arch_iommu->tlb_load_cr(obj, cr);
  179. iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
  180. iommu_write_reg(obj, 1, MMU_LD_TLB);
  181. }
  182. /**
  183. * iotlb_dump_cr - Dump an iommu tlb entry into buf
  184. * @obj: target iommu
  185. * @cr: contents of cam and ram register
  186. * @buf: output buffer
  187. **/
  188. static inline ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr,
  189. char *buf)
  190. {
  191. BUG_ON(!cr || !buf);
  192. return arch_iommu->dump_cr(obj, cr, buf);
  193. }
  194. /* only used in iotlb iteration for-loop */
  195. static struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
  196. {
  197. struct cr_regs cr;
  198. struct iotlb_lock l;
  199. iotlb_lock_get(obj, &l);
  200. l.vict = n;
  201. iotlb_lock_set(obj, &l);
  202. iotlb_read_cr(obj, &cr);
  203. return cr;
  204. }
  205. /**
  206. * load_iotlb_entry - Set an iommu tlb entry
  207. * @obj: target iommu
  208. * @e: an iommu tlb entry info
  209. **/
  210. #ifdef PREFETCH_IOTLB
  211. static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
  212. {
  213. int err = 0;
  214. struct iotlb_lock l;
  215. struct cr_regs *cr;
  216. if (!obj || !obj->nr_tlb_entries || !e)
  217. return -EINVAL;
  218. clk_enable(obj->clk);
  219. iotlb_lock_get(obj, &l);
  220. if (l.base == obj->nr_tlb_entries) {
  221. dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
  222. err = -EBUSY;
  223. goto out;
  224. }
  225. if (!e->prsvd) {
  226. int i;
  227. struct cr_regs tmp;
  228. for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
  229. if (!iotlb_cr_valid(&tmp))
  230. break;
  231. if (i == obj->nr_tlb_entries) {
  232. dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
  233. err = -EBUSY;
  234. goto out;
  235. }
  236. iotlb_lock_get(obj, &l);
  237. } else {
  238. l.vict = l.base;
  239. iotlb_lock_set(obj, &l);
  240. }
  241. cr = iotlb_alloc_cr(obj, e);
  242. if (IS_ERR(cr)) {
  243. clk_disable(obj->clk);
  244. return PTR_ERR(cr);
  245. }
  246. iotlb_load_cr(obj, cr);
  247. kfree(cr);
  248. if (e->prsvd)
  249. l.base++;
  250. /* increment victim for next tlb load */
  251. if (++l.vict == obj->nr_tlb_entries)
  252. l.vict = l.base;
  253. iotlb_lock_set(obj, &l);
  254. out:
  255. clk_disable(obj->clk);
  256. return err;
  257. }
  258. #else /* !PREFETCH_IOTLB */
  259. static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
  260. {
  261. return 0;
  262. }
  263. #endif /* !PREFETCH_IOTLB */
  264. static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
  265. {
  266. return load_iotlb_entry(obj, e);
  267. }
  268. /**
  269. * flush_iotlb_page - Clear an iommu tlb entry
  270. * @obj: target iommu
  271. * @da: iommu device virtual address
  272. *
  273. * Clear an iommu tlb entry which includes 'da' address.
  274. **/
  275. static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
  276. {
  277. int i;
  278. struct cr_regs cr;
  279. clk_enable(obj->clk);
  280. for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
  281. u32 start;
  282. size_t bytes;
  283. if (!iotlb_cr_valid(&cr))
  284. continue;
  285. start = iotlb_cr_to_virt(&cr);
  286. bytes = iopgsz_to_bytes(cr.cam & 3);
  287. if ((start <= da) && (da < start + bytes)) {
  288. dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
  289. __func__, start, da, bytes);
  290. iotlb_load_cr(obj, &cr);
  291. iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
  292. }
  293. }
  294. clk_disable(obj->clk);
  295. if (i == obj->nr_tlb_entries)
  296. dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
  297. }
  298. /**
  299. * flush_iotlb_all - Clear all iommu tlb entries
  300. * @obj: target iommu
  301. **/
  302. static void flush_iotlb_all(struct omap_iommu *obj)
  303. {
  304. struct iotlb_lock l;
  305. clk_enable(obj->clk);
  306. l.base = 0;
  307. l.vict = 0;
  308. iotlb_lock_set(obj, &l);
  309. iommu_write_reg(obj, 1, MMU_GFLUSH);
  310. clk_disable(obj->clk);
  311. }
  312. #if defined(CONFIG_OMAP_IOMMU_DEBUG) || defined(CONFIG_OMAP_IOMMU_DEBUG_MODULE)
  313. ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t bytes)
  314. {
  315. if (!obj || !buf)
  316. return -EINVAL;
  317. clk_enable(obj->clk);
  318. bytes = arch_iommu->dump_ctx(obj, buf, bytes);
  319. clk_disable(obj->clk);
  320. return bytes;
  321. }
  322. EXPORT_SYMBOL_GPL(omap_iommu_dump_ctx);
  323. static int
  324. __dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num)
  325. {
  326. int i;
  327. struct iotlb_lock saved;
  328. struct cr_regs tmp;
  329. struct cr_regs *p = crs;
  330. clk_enable(obj->clk);
  331. iotlb_lock_get(obj, &saved);
  332. for_each_iotlb_cr(obj, num, i, tmp) {
  333. if (!iotlb_cr_valid(&tmp))
  334. continue;
  335. *p++ = tmp;
  336. }
  337. iotlb_lock_set(obj, &saved);
  338. clk_disable(obj->clk);
  339. return p - crs;
  340. }
  341. /**
  342. * omap_dump_tlb_entries - dump cr arrays to given buffer
  343. * @obj: target iommu
  344. * @buf: output buffer
  345. **/
  346. size_t omap_dump_tlb_entries(struct omap_iommu *obj, char *buf, ssize_t bytes)
  347. {
  348. int i, num;
  349. struct cr_regs *cr;
  350. char *p = buf;
  351. num = bytes / sizeof(*cr);
  352. num = min(obj->nr_tlb_entries, num);
  353. cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
  354. if (!cr)
  355. return 0;
  356. num = __dump_tlb_entries(obj, cr, num);
  357. for (i = 0; i < num; i++)
  358. p += iotlb_dump_cr(obj, cr + i, p);
  359. kfree(cr);
  360. return p - buf;
  361. }
  362. EXPORT_SYMBOL_GPL(omap_dump_tlb_entries);
  363. int omap_foreach_iommu_device(void *data, int (*fn)(struct device *, void *))
  364. {
  365. return driver_for_each_device(&omap_iommu_driver.driver,
  366. NULL, data, fn);
  367. }
  368. EXPORT_SYMBOL_GPL(omap_foreach_iommu_device);
  369. #endif /* CONFIG_OMAP_IOMMU_DEBUG_MODULE */
  370. /*
  371. * H/W pagetable operations
  372. */
  373. static void flush_iopgd_range(u32 *first, u32 *last)
  374. {
  375. /* FIXME: L2 cache should be taken care of if it exists */
  376. do {
  377. asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
  378. : : "r" (first));
  379. first += L1_CACHE_BYTES / sizeof(*first);
  380. } while (first <= last);
  381. }
  382. static void flush_iopte_range(u32 *first, u32 *last)
  383. {
  384. /* FIXME: L2 cache should be taken care of if it exists */
  385. do {
  386. asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
  387. : : "r" (first));
  388. first += L1_CACHE_BYTES / sizeof(*first);
  389. } while (first <= last);
  390. }
  391. static void iopte_free(u32 *iopte)
  392. {
  393. /* Note: freed iopte's must be clean ready for re-use */
  394. kmem_cache_free(iopte_cachep, iopte);
  395. }
  396. static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
  397. {
  398. u32 *iopte;
  399. /* a table has already existed */
  400. if (*iopgd)
  401. goto pte_ready;
  402. /*
  403. * do the allocation outside the page table lock
  404. */
  405. spin_unlock(&obj->page_table_lock);
  406. iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
  407. spin_lock(&obj->page_table_lock);
  408. if (!*iopgd) {
  409. if (!iopte)
  410. return ERR_PTR(-ENOMEM);
  411. *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
  412. flush_iopgd_range(iopgd, iopgd);
  413. dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
  414. } else {
  415. /* We raced, free the reduniovant table */
  416. iopte_free(iopte);
  417. }
  418. pte_ready:
  419. iopte = iopte_offset(iopgd, da);
  420. dev_vdbg(obj->dev,
  421. "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
  422. __func__, da, iopgd, *iopgd, iopte, *iopte);
  423. return iopte;
  424. }
  425. static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  426. {
  427. u32 *iopgd = iopgd_offset(obj, da);
  428. if ((da | pa) & ~IOSECTION_MASK) {
  429. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  430. __func__, da, pa, IOSECTION_SIZE);
  431. return -EINVAL;
  432. }
  433. *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
  434. flush_iopgd_range(iopgd, iopgd);
  435. return 0;
  436. }
  437. static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  438. {
  439. u32 *iopgd = iopgd_offset(obj, da);
  440. int i;
  441. if ((da | pa) & ~IOSUPER_MASK) {
  442. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  443. __func__, da, pa, IOSUPER_SIZE);
  444. return -EINVAL;
  445. }
  446. for (i = 0; i < 16; i++)
  447. *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
  448. flush_iopgd_range(iopgd, iopgd + 15);
  449. return 0;
  450. }
  451. static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  452. {
  453. u32 *iopgd = iopgd_offset(obj, da);
  454. u32 *iopte = iopte_alloc(obj, iopgd, da);
  455. if (IS_ERR(iopte))
  456. return PTR_ERR(iopte);
  457. *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
  458. flush_iopte_range(iopte, iopte);
  459. dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
  460. __func__, da, pa, iopte, *iopte);
  461. return 0;
  462. }
  463. static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  464. {
  465. u32 *iopgd = iopgd_offset(obj, da);
  466. u32 *iopte = iopte_alloc(obj, iopgd, da);
  467. int i;
  468. if ((da | pa) & ~IOLARGE_MASK) {
  469. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  470. __func__, da, pa, IOLARGE_SIZE);
  471. return -EINVAL;
  472. }
  473. if (IS_ERR(iopte))
  474. return PTR_ERR(iopte);
  475. for (i = 0; i < 16; i++)
  476. *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
  477. flush_iopte_range(iopte, iopte + 15);
  478. return 0;
  479. }
  480. static int
  481. iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
  482. {
  483. int (*fn)(struct omap_iommu *, u32, u32, u32);
  484. u32 prot;
  485. int err;
  486. if (!obj || !e)
  487. return -EINVAL;
  488. switch (e->pgsz) {
  489. case MMU_CAM_PGSZ_16M:
  490. fn = iopgd_alloc_super;
  491. break;
  492. case MMU_CAM_PGSZ_1M:
  493. fn = iopgd_alloc_section;
  494. break;
  495. case MMU_CAM_PGSZ_64K:
  496. fn = iopte_alloc_large;
  497. break;
  498. case MMU_CAM_PGSZ_4K:
  499. fn = iopte_alloc_page;
  500. break;
  501. default:
  502. fn = NULL;
  503. BUG();
  504. break;
  505. }
  506. prot = get_iopte_attr(e);
  507. spin_lock(&obj->page_table_lock);
  508. err = fn(obj, e->da, e->pa, prot);
  509. spin_unlock(&obj->page_table_lock);
  510. return err;
  511. }
  512. /**
  513. * omap_iopgtable_store_entry - Make an iommu pte entry
  514. * @obj: target iommu
  515. * @e: an iommu tlb entry info
  516. **/
  517. int omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
  518. {
  519. int err;
  520. flush_iotlb_page(obj, e->da);
  521. err = iopgtable_store_entry_core(obj, e);
  522. if (!err)
  523. prefetch_iotlb_entry(obj, e);
  524. return err;
  525. }
  526. EXPORT_SYMBOL_GPL(omap_iopgtable_store_entry);
  527. /**
  528. * iopgtable_lookup_entry - Lookup an iommu pte entry
  529. * @obj: target iommu
  530. * @da: iommu device virtual address
  531. * @ppgd: iommu pgd entry pointer to be returned
  532. * @ppte: iommu pte entry pointer to be returned
  533. **/
  534. static void
  535. iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
  536. {
  537. u32 *iopgd, *iopte = NULL;
  538. iopgd = iopgd_offset(obj, da);
  539. if (!*iopgd)
  540. goto out;
  541. if (iopgd_is_table(*iopgd))
  542. iopte = iopte_offset(iopgd, da);
  543. out:
  544. *ppgd = iopgd;
  545. *ppte = iopte;
  546. }
  547. static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
  548. {
  549. size_t bytes;
  550. u32 *iopgd = iopgd_offset(obj, da);
  551. int nent = 1;
  552. if (!*iopgd)
  553. return 0;
  554. if (iopgd_is_table(*iopgd)) {
  555. int i;
  556. u32 *iopte = iopte_offset(iopgd, da);
  557. bytes = IOPTE_SIZE;
  558. if (*iopte & IOPTE_LARGE) {
  559. nent *= 16;
  560. /* rewind to the 1st entry */
  561. iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
  562. }
  563. bytes *= nent;
  564. memset(iopte, 0, nent * sizeof(*iopte));
  565. flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
  566. /*
  567. * do table walk to check if this table is necessary or not
  568. */
  569. iopte = iopte_offset(iopgd, 0);
  570. for (i = 0; i < PTRS_PER_IOPTE; i++)
  571. if (iopte[i])
  572. goto out;
  573. iopte_free(iopte);
  574. nent = 1; /* for the next L1 entry */
  575. } else {
  576. bytes = IOPGD_SIZE;
  577. if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
  578. nent *= 16;
  579. /* rewind to the 1st entry */
  580. iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
  581. }
  582. bytes *= nent;
  583. }
  584. memset(iopgd, 0, nent * sizeof(*iopgd));
  585. flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
  586. out:
  587. return bytes;
  588. }
  589. /**
  590. * iopgtable_clear_entry - Remove an iommu pte entry
  591. * @obj: target iommu
  592. * @da: iommu device virtual address
  593. **/
  594. static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
  595. {
  596. size_t bytes;
  597. spin_lock(&obj->page_table_lock);
  598. bytes = iopgtable_clear_entry_core(obj, da);
  599. flush_iotlb_page(obj, da);
  600. spin_unlock(&obj->page_table_lock);
  601. return bytes;
  602. }
  603. static void iopgtable_clear_entry_all(struct omap_iommu *obj)
  604. {
  605. int i;
  606. spin_lock(&obj->page_table_lock);
  607. for (i = 0; i < PTRS_PER_IOPGD; i++) {
  608. u32 da;
  609. u32 *iopgd;
  610. da = i << IOPGD_SHIFT;
  611. iopgd = iopgd_offset(obj, da);
  612. if (!*iopgd)
  613. continue;
  614. if (iopgd_is_table(*iopgd))
  615. iopte_free(iopte_offset(iopgd, 0));
  616. *iopgd = 0;
  617. flush_iopgd_range(iopgd, iopgd);
  618. }
  619. flush_iotlb_all(obj);
  620. spin_unlock(&obj->page_table_lock);
  621. }
  622. /*
  623. * Device IOMMU generic operations
  624. */
  625. static irqreturn_t iommu_fault_handler(int irq, void *data)
  626. {
  627. u32 da, errs;
  628. u32 *iopgd, *iopte;
  629. struct omap_iommu *obj = data;
  630. struct iommu_domain *domain = obj->domain;
  631. if (!obj->refcount)
  632. return IRQ_NONE;
  633. clk_enable(obj->clk);
  634. errs = iommu_report_fault(obj, &da);
  635. clk_disable(obj->clk);
  636. if (errs == 0)
  637. return IRQ_HANDLED;
  638. /* Fault callback or TLB/PTE Dynamic loading */
  639. if (!report_iommu_fault(domain, obj->dev, da, 0))
  640. return IRQ_HANDLED;
  641. iommu_disable(obj);
  642. iopgd = iopgd_offset(obj, da);
  643. if (!iopgd_is_table(*iopgd)) {
  644. dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p "
  645. "*pgd:px%08x\n", obj->name, errs, da, iopgd, *iopgd);
  646. return IRQ_NONE;
  647. }
  648. iopte = iopte_offset(iopgd, da);
  649. dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x "
  650. "pte:0x%p *pte:0x%08x\n", obj->name, errs, da, iopgd, *iopgd,
  651. iopte, *iopte);
  652. return IRQ_NONE;
  653. }
  654. static int device_match_by_alias(struct device *dev, void *data)
  655. {
  656. struct omap_iommu *obj = to_iommu(dev);
  657. const char *name = data;
  658. pr_debug("%s: %s %s\n", __func__, obj->name, name);
  659. return strcmp(obj->name, name) == 0;
  660. }
  661. /**
  662. * omap_iommu_attach() - attach iommu device to an iommu domain
  663. * @name: name of target omap iommu device
  664. * @iopgd: page table
  665. **/
  666. static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd)
  667. {
  668. int err = -ENOMEM;
  669. struct device *dev;
  670. struct omap_iommu *obj;
  671. dev = driver_find_device(&omap_iommu_driver.driver, NULL,
  672. (void *)name,
  673. device_match_by_alias);
  674. if (!dev)
  675. return NULL;
  676. obj = to_iommu(dev);
  677. spin_lock(&obj->iommu_lock);
  678. /* an iommu device can only be attached once */
  679. if (++obj->refcount > 1) {
  680. dev_err(dev, "%s: already attached!\n", obj->name);
  681. err = -EBUSY;
  682. goto err_enable;
  683. }
  684. obj->iopgd = iopgd;
  685. err = iommu_enable(obj);
  686. if (err)
  687. goto err_enable;
  688. flush_iotlb_all(obj);
  689. if (!try_module_get(obj->owner))
  690. goto err_module;
  691. spin_unlock(&obj->iommu_lock);
  692. dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
  693. return obj;
  694. err_module:
  695. if (obj->refcount == 1)
  696. iommu_disable(obj);
  697. err_enable:
  698. obj->refcount--;
  699. spin_unlock(&obj->iommu_lock);
  700. return ERR_PTR(err);
  701. }
  702. /**
  703. * omap_iommu_detach - release iommu device
  704. * @obj: target iommu
  705. **/
  706. static void omap_iommu_detach(struct omap_iommu *obj)
  707. {
  708. if (!obj || IS_ERR(obj))
  709. return;
  710. spin_lock(&obj->iommu_lock);
  711. if (--obj->refcount == 0)
  712. iommu_disable(obj);
  713. module_put(obj->owner);
  714. obj->iopgd = NULL;
  715. spin_unlock(&obj->iommu_lock);
  716. dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
  717. }
  718. /*
  719. * OMAP Device MMU(IOMMU) detection
  720. */
  721. static int __devinit omap_iommu_probe(struct platform_device *pdev)
  722. {
  723. int err = -ENODEV;
  724. int irq;
  725. struct omap_iommu *obj;
  726. struct resource *res;
  727. struct iommu_platform_data *pdata = pdev->dev.platform_data;
  728. if (pdev->num_resources != 2)
  729. return -EINVAL;
  730. obj = kzalloc(sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
  731. if (!obj)
  732. return -ENOMEM;
  733. obj->clk = clk_get(&pdev->dev, pdata->clk_name);
  734. if (IS_ERR(obj->clk))
  735. goto err_clk;
  736. obj->nr_tlb_entries = pdata->nr_tlb_entries;
  737. obj->name = pdata->name;
  738. obj->dev = &pdev->dev;
  739. obj->ctx = (void *)obj + sizeof(*obj);
  740. obj->da_start = pdata->da_start;
  741. obj->da_end = pdata->da_end;
  742. spin_lock_init(&obj->iommu_lock);
  743. mutex_init(&obj->mmap_lock);
  744. spin_lock_init(&obj->page_table_lock);
  745. INIT_LIST_HEAD(&obj->mmap);
  746. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  747. if (!res) {
  748. err = -ENODEV;
  749. goto err_mem;
  750. }
  751. res = request_mem_region(res->start, resource_size(res),
  752. dev_name(&pdev->dev));
  753. if (!res) {
  754. err = -EIO;
  755. goto err_mem;
  756. }
  757. obj->regbase = ioremap(res->start, resource_size(res));
  758. if (!obj->regbase) {
  759. err = -ENOMEM;
  760. goto err_ioremap;
  761. }
  762. irq = platform_get_irq(pdev, 0);
  763. if (irq < 0) {
  764. err = -ENODEV;
  765. goto err_irq;
  766. }
  767. err = request_irq(irq, iommu_fault_handler, IRQF_SHARED,
  768. dev_name(&pdev->dev), obj);
  769. if (err < 0)
  770. goto err_irq;
  771. platform_set_drvdata(pdev, obj);
  772. dev_info(&pdev->dev, "%s registered\n", obj->name);
  773. return 0;
  774. err_irq:
  775. iounmap(obj->regbase);
  776. err_ioremap:
  777. release_mem_region(res->start, resource_size(res));
  778. err_mem:
  779. clk_put(obj->clk);
  780. err_clk:
  781. kfree(obj);
  782. return err;
  783. }
  784. static int __devexit omap_iommu_remove(struct platform_device *pdev)
  785. {
  786. int irq;
  787. struct resource *res;
  788. struct omap_iommu *obj = platform_get_drvdata(pdev);
  789. platform_set_drvdata(pdev, NULL);
  790. iopgtable_clear_entry_all(obj);
  791. irq = platform_get_irq(pdev, 0);
  792. free_irq(irq, obj);
  793. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  794. release_mem_region(res->start, resource_size(res));
  795. iounmap(obj->regbase);
  796. clk_put(obj->clk);
  797. dev_info(&pdev->dev, "%s removed\n", obj->name);
  798. kfree(obj);
  799. return 0;
  800. }
  801. static struct platform_driver omap_iommu_driver = {
  802. .probe = omap_iommu_probe,
  803. .remove = __devexit_p(omap_iommu_remove),
  804. .driver = {
  805. .name = "omap-iommu",
  806. },
  807. };
  808. static void iopte_cachep_ctor(void *iopte)
  809. {
  810. clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
  811. }
  812. static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
  813. phys_addr_t pa, size_t bytes, int prot)
  814. {
  815. struct omap_iommu_domain *omap_domain = domain->priv;
  816. struct omap_iommu *oiommu = omap_domain->iommu_dev;
  817. struct device *dev = oiommu->dev;
  818. struct iotlb_entry e;
  819. int omap_pgsz;
  820. u32 ret, flags;
  821. /* we only support mapping a single iommu page for now */
  822. omap_pgsz = bytes_to_iopgsz(bytes);
  823. if (omap_pgsz < 0) {
  824. dev_err(dev, "invalid size to map: %d\n", bytes);
  825. return -EINVAL;
  826. }
  827. dev_dbg(dev, "mapping da 0x%lx to pa 0x%x size 0x%x\n", da, pa, bytes);
  828. flags = omap_pgsz | prot;
  829. iotlb_init_entry(&e, da, pa, flags);
  830. ret = omap_iopgtable_store_entry(oiommu, &e);
  831. if (ret)
  832. dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
  833. return ret;
  834. }
  835. static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
  836. size_t size)
  837. {
  838. struct omap_iommu_domain *omap_domain = domain->priv;
  839. struct omap_iommu *oiommu = omap_domain->iommu_dev;
  840. struct device *dev = oiommu->dev;
  841. dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
  842. return iopgtable_clear_entry(oiommu, da);
  843. }
  844. static int
  845. omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
  846. {
  847. struct omap_iommu_domain *omap_domain = domain->priv;
  848. struct omap_iommu *oiommu;
  849. struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
  850. int ret = 0;
  851. spin_lock(&omap_domain->lock);
  852. /* only a single device is supported per domain for now */
  853. if (omap_domain->iommu_dev) {
  854. dev_err(dev, "iommu domain is already attached\n");
  855. ret = -EBUSY;
  856. goto out;
  857. }
  858. /* get a handle to and enable the omap iommu */
  859. oiommu = omap_iommu_attach(arch_data->name, omap_domain->pgtable);
  860. if (IS_ERR(oiommu)) {
  861. ret = PTR_ERR(oiommu);
  862. dev_err(dev, "can't get omap iommu: %d\n", ret);
  863. goto out;
  864. }
  865. omap_domain->iommu_dev = arch_data->iommu_dev = oiommu;
  866. oiommu->domain = domain;
  867. out:
  868. spin_unlock(&omap_domain->lock);
  869. return ret;
  870. }
  871. static void omap_iommu_detach_dev(struct iommu_domain *domain,
  872. struct device *dev)
  873. {
  874. struct omap_iommu_domain *omap_domain = domain->priv;
  875. struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
  876. struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
  877. spin_lock(&omap_domain->lock);
  878. /* only a single device is supported per domain for now */
  879. if (omap_domain->iommu_dev != oiommu) {
  880. dev_err(dev, "invalid iommu device\n");
  881. goto out;
  882. }
  883. iopgtable_clear_entry_all(oiommu);
  884. omap_iommu_detach(oiommu);
  885. omap_domain->iommu_dev = arch_data->iommu_dev = NULL;
  886. out:
  887. spin_unlock(&omap_domain->lock);
  888. }
  889. static int omap_iommu_domain_init(struct iommu_domain *domain)
  890. {
  891. struct omap_iommu_domain *omap_domain;
  892. omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
  893. if (!omap_domain) {
  894. pr_err("kzalloc failed\n");
  895. goto out;
  896. }
  897. omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
  898. if (!omap_domain->pgtable) {
  899. pr_err("kzalloc failed\n");
  900. goto fail_nomem;
  901. }
  902. /*
  903. * should never fail, but please keep this around to ensure
  904. * we keep the hardware happy
  905. */
  906. BUG_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE));
  907. clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE);
  908. spin_lock_init(&omap_domain->lock);
  909. domain->priv = omap_domain;
  910. return 0;
  911. fail_nomem:
  912. kfree(omap_domain);
  913. out:
  914. return -ENOMEM;
  915. }
  916. /* assume device was already detached */
  917. static void omap_iommu_domain_destroy(struct iommu_domain *domain)
  918. {
  919. struct omap_iommu_domain *omap_domain = domain->priv;
  920. domain->priv = NULL;
  921. kfree(omap_domain->pgtable);
  922. kfree(omap_domain);
  923. }
  924. static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
  925. unsigned long da)
  926. {
  927. struct omap_iommu_domain *omap_domain = domain->priv;
  928. struct omap_iommu *oiommu = omap_domain->iommu_dev;
  929. struct device *dev = oiommu->dev;
  930. u32 *pgd, *pte;
  931. phys_addr_t ret = 0;
  932. iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
  933. if (pte) {
  934. if (iopte_is_small(*pte))
  935. ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
  936. else if (iopte_is_large(*pte))
  937. ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
  938. else
  939. dev_err(dev, "bogus pte 0x%x, da 0x%lx", *pte, da);
  940. } else {
  941. if (iopgd_is_section(*pgd))
  942. ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
  943. else if (iopgd_is_super(*pgd))
  944. ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
  945. else
  946. dev_err(dev, "bogus pgd 0x%x, da 0x%lx", *pgd, da);
  947. }
  948. return ret;
  949. }
  950. static int omap_iommu_domain_has_cap(struct iommu_domain *domain,
  951. unsigned long cap)
  952. {
  953. return 0;
  954. }
  955. static struct iommu_ops omap_iommu_ops = {
  956. .domain_init = omap_iommu_domain_init,
  957. .domain_destroy = omap_iommu_domain_destroy,
  958. .attach_dev = omap_iommu_attach_dev,
  959. .detach_dev = omap_iommu_detach_dev,
  960. .map = omap_iommu_map,
  961. .unmap = omap_iommu_unmap,
  962. .iova_to_phys = omap_iommu_iova_to_phys,
  963. .domain_has_cap = omap_iommu_domain_has_cap,
  964. .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
  965. };
  966. static int __init omap_iommu_init(void)
  967. {
  968. struct kmem_cache *p;
  969. const unsigned long flags = SLAB_HWCACHE_ALIGN;
  970. size_t align = 1 << 10; /* L2 pagetable alignement */
  971. p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
  972. iopte_cachep_ctor);
  973. if (!p)
  974. return -ENOMEM;
  975. iopte_cachep = p;
  976. bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
  977. return platform_driver_register(&omap_iommu_driver);
  978. }
  979. /* must be ready before omap3isp is probed */
  980. subsys_initcall(omap_iommu_init);
  981. static void __exit omap_iommu_exit(void)
  982. {
  983. kmem_cache_destroy(iopte_cachep);
  984. platform_driver_unregister(&omap_iommu_driver);
  985. }
  986. module_exit(omap_iommu_exit);
  987. MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives");
  988. MODULE_ALIAS("platform:omap-iommu");
  989. MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
  990. MODULE_LICENSE("GPL v2");