omap-iommu.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  1. /*
  2. * omap iommu: tlb and pagetable primitives
  3. *
  4. * Copyright (C) 2008-2010 Nokia Corporation
  5. *
  6. * Written by Hiroshi DOYU <[email protected]>,
  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/slab.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/ioport.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/iommu.h>
  19. #include <linux/omap-iommu.h>
  20. #include <linux/mutex.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/io.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/of.h>
  25. #include <linux/of_iommu.h>
  26. #include <linux/of_irq.h>
  27. #include <linux/of_platform.h>
  28. #include <linux/regmap.h>
  29. #include <linux/mfd/syscon.h>
  30. #include <asm/cacheflush.h>
  31. #include <linux/platform_data/iommu-omap.h>
  32. #include "omap-iopgtable.h"
  33. #include "omap-iommu.h"
  34. #define to_iommu(dev) \
  35. ((struct omap_iommu *)platform_get_drvdata(to_platform_device(dev)))
  36. /* bitmap of the page sizes currently supported */
  37. #define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
  38. /**
  39. * struct omap_iommu_domain - omap iommu domain
  40. * @pgtable: the page table
  41. * @iommu_dev: an omap iommu device attached to this domain. only a single
  42. * iommu device can be attached for now.
  43. * @dev: Device using this domain.
  44. * @lock: domain lock, should be taken when attaching/detaching
  45. */
  46. struct omap_iommu_domain {
  47. u32 *pgtable;
  48. struct omap_iommu *iommu_dev;
  49. struct device *dev;
  50. spinlock_t lock;
  51. struct iommu_domain domain;
  52. };
  53. #define MMU_LOCK_BASE_SHIFT 10
  54. #define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
  55. #define MMU_LOCK_BASE(x) \
  56. ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
  57. #define MMU_LOCK_VICT_SHIFT 4
  58. #define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
  59. #define MMU_LOCK_VICT(x) \
  60. ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
  61. static struct platform_driver omap_iommu_driver;
  62. static struct kmem_cache *iopte_cachep;
  63. /**
  64. * to_omap_domain - Get struct omap_iommu_domain from generic iommu_domain
  65. * @dom: generic iommu domain handle
  66. **/
  67. static struct omap_iommu_domain *to_omap_domain(struct iommu_domain *dom)
  68. {
  69. return container_of(dom, struct omap_iommu_domain, domain);
  70. }
  71. /**
  72. * omap_iommu_save_ctx - Save registers for pm off-mode support
  73. * @dev: client device
  74. **/
  75. void omap_iommu_save_ctx(struct device *dev)
  76. {
  77. struct omap_iommu *obj = dev_to_omap_iommu(dev);
  78. u32 *p = obj->ctx;
  79. int i;
  80. for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
  81. p[i] = iommu_read_reg(obj, i * sizeof(u32));
  82. dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
  83. }
  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. u32 *p = obj->ctx;
  94. int i;
  95. for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
  96. iommu_write_reg(obj, p[i], i * sizeof(u32));
  97. dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
  98. }
  99. }
  100. EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
  101. static void dra7_cfg_dspsys_mmu(struct omap_iommu *obj, bool enable)
  102. {
  103. u32 val, mask;
  104. if (!obj->syscfg)
  105. return;
  106. mask = (1 << (obj->id * DSP_SYS_MMU_CONFIG_EN_SHIFT));
  107. val = enable ? mask : 0;
  108. regmap_update_bits(obj->syscfg, DSP_SYS_MMU_CONFIG, mask, val);
  109. }
  110. static void __iommu_set_twl(struct omap_iommu *obj, bool on)
  111. {
  112. u32 l = iommu_read_reg(obj, MMU_CNTL);
  113. if (on)
  114. iommu_write_reg(obj, MMU_IRQ_TWL_MASK, MMU_IRQENABLE);
  115. else
  116. iommu_write_reg(obj, MMU_IRQ_TLB_MISS_MASK, MMU_IRQENABLE);
  117. l &= ~MMU_CNTL_MASK;
  118. if (on)
  119. l |= (MMU_CNTL_MMU_EN | MMU_CNTL_TWL_EN);
  120. else
  121. l |= (MMU_CNTL_MMU_EN);
  122. iommu_write_reg(obj, l, MMU_CNTL);
  123. }
  124. static int omap2_iommu_enable(struct omap_iommu *obj)
  125. {
  126. u32 l, pa;
  127. if (!obj->iopgd || !IS_ALIGNED((u32)obj->iopgd, SZ_16K))
  128. return -EINVAL;
  129. pa = virt_to_phys(obj->iopgd);
  130. if (!IS_ALIGNED(pa, SZ_16K))
  131. return -EINVAL;
  132. l = iommu_read_reg(obj, MMU_REVISION);
  133. dev_info(obj->dev, "%s: version %d.%d\n", obj->name,
  134. (l >> 4) & 0xf, l & 0xf);
  135. iommu_write_reg(obj, pa, MMU_TTB);
  136. dra7_cfg_dspsys_mmu(obj, true);
  137. if (obj->has_bus_err_back)
  138. iommu_write_reg(obj, MMU_GP_REG_BUS_ERR_BACK_EN, MMU_GP_REG);
  139. __iommu_set_twl(obj, true);
  140. return 0;
  141. }
  142. static void omap2_iommu_disable(struct omap_iommu *obj)
  143. {
  144. u32 l = iommu_read_reg(obj, MMU_CNTL);
  145. l &= ~MMU_CNTL_MASK;
  146. iommu_write_reg(obj, l, MMU_CNTL);
  147. dra7_cfg_dspsys_mmu(obj, false);
  148. dev_dbg(obj->dev, "%s is shutting down\n", obj->name);
  149. }
  150. static int iommu_enable(struct omap_iommu *obj)
  151. {
  152. int err;
  153. struct platform_device *pdev = to_platform_device(obj->dev);
  154. struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
  155. if (pdata && pdata->deassert_reset) {
  156. err = pdata->deassert_reset(pdev, pdata->reset_name);
  157. if (err) {
  158. dev_err(obj->dev, "deassert_reset failed: %d\n", err);
  159. return err;
  160. }
  161. }
  162. pm_runtime_get_sync(obj->dev);
  163. err = omap2_iommu_enable(obj);
  164. return err;
  165. }
  166. static void iommu_disable(struct omap_iommu *obj)
  167. {
  168. struct platform_device *pdev = to_platform_device(obj->dev);
  169. struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
  170. omap2_iommu_disable(obj);
  171. pm_runtime_put_sync(obj->dev);
  172. if (pdata && pdata->assert_reset)
  173. pdata->assert_reset(pdev, pdata->reset_name);
  174. }
  175. /*
  176. * TLB operations
  177. */
  178. static u32 iotlb_cr_to_virt(struct cr_regs *cr)
  179. {
  180. u32 page_size = cr->cam & MMU_CAM_PGSZ_MASK;
  181. u32 mask = get_cam_va_mask(cr->cam & page_size);
  182. return cr->cam & mask;
  183. }
  184. static u32 get_iopte_attr(struct iotlb_entry *e)
  185. {
  186. u32 attr;
  187. attr = e->mixed << 5;
  188. attr |= e->endian;
  189. attr |= e->elsz >> 3;
  190. attr <<= (((e->pgsz == MMU_CAM_PGSZ_4K) ||
  191. (e->pgsz == MMU_CAM_PGSZ_64K)) ? 0 : 6);
  192. return attr;
  193. }
  194. static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
  195. {
  196. u32 status, fault_addr;
  197. status = iommu_read_reg(obj, MMU_IRQSTATUS);
  198. status &= MMU_IRQ_MASK;
  199. if (!status) {
  200. *da = 0;
  201. return 0;
  202. }
  203. fault_addr = iommu_read_reg(obj, MMU_FAULT_AD);
  204. *da = fault_addr;
  205. iommu_write_reg(obj, status, MMU_IRQSTATUS);
  206. return status;
  207. }
  208. void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
  209. {
  210. u32 val;
  211. val = iommu_read_reg(obj, MMU_LOCK);
  212. l->base = MMU_LOCK_BASE(val);
  213. l->vict = MMU_LOCK_VICT(val);
  214. }
  215. void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
  216. {
  217. u32 val;
  218. val = (l->base << MMU_LOCK_BASE_SHIFT);
  219. val |= (l->vict << MMU_LOCK_VICT_SHIFT);
  220. iommu_write_reg(obj, val, MMU_LOCK);
  221. }
  222. static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
  223. {
  224. cr->cam = iommu_read_reg(obj, MMU_READ_CAM);
  225. cr->ram = iommu_read_reg(obj, MMU_READ_RAM);
  226. }
  227. static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
  228. {
  229. iommu_write_reg(obj, cr->cam | MMU_CAM_V, MMU_CAM);
  230. iommu_write_reg(obj, cr->ram, MMU_RAM);
  231. iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
  232. iommu_write_reg(obj, 1, MMU_LD_TLB);
  233. }
  234. /* only used in iotlb iteration for-loop */
  235. struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
  236. {
  237. struct cr_regs cr;
  238. struct iotlb_lock l;
  239. iotlb_lock_get(obj, &l);
  240. l.vict = n;
  241. iotlb_lock_set(obj, &l);
  242. iotlb_read_cr(obj, &cr);
  243. return cr;
  244. }
  245. #ifdef PREFETCH_IOTLB
  246. static struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
  247. struct iotlb_entry *e)
  248. {
  249. struct cr_regs *cr;
  250. if (!e)
  251. return NULL;
  252. if (e->da & ~(get_cam_va_mask(e->pgsz))) {
  253. dev_err(obj->dev, "%s:\twrong alignment: %08x\n", __func__,
  254. e->da);
  255. return ERR_PTR(-EINVAL);
  256. }
  257. cr = kmalloc(sizeof(*cr), GFP_KERNEL);
  258. if (!cr)
  259. return ERR_PTR(-ENOMEM);
  260. cr->cam = (e->da & MMU_CAM_VATAG_MASK) | e->prsvd | e->pgsz | e->valid;
  261. cr->ram = e->pa | e->endian | e->elsz | e->mixed;
  262. return cr;
  263. }
  264. /**
  265. * load_iotlb_entry - Set an iommu tlb entry
  266. * @obj: target iommu
  267. * @e: an iommu tlb entry info
  268. **/
  269. static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
  270. {
  271. int err = 0;
  272. struct iotlb_lock l;
  273. struct cr_regs *cr;
  274. if (!obj || !obj->nr_tlb_entries || !e)
  275. return -EINVAL;
  276. pm_runtime_get_sync(obj->dev);
  277. iotlb_lock_get(obj, &l);
  278. if (l.base == obj->nr_tlb_entries) {
  279. dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
  280. err = -EBUSY;
  281. goto out;
  282. }
  283. if (!e->prsvd) {
  284. int i;
  285. struct cr_regs tmp;
  286. for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
  287. if (!iotlb_cr_valid(&tmp))
  288. break;
  289. if (i == obj->nr_tlb_entries) {
  290. dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
  291. err = -EBUSY;
  292. goto out;
  293. }
  294. iotlb_lock_get(obj, &l);
  295. } else {
  296. l.vict = l.base;
  297. iotlb_lock_set(obj, &l);
  298. }
  299. cr = iotlb_alloc_cr(obj, e);
  300. if (IS_ERR(cr)) {
  301. pm_runtime_put_sync(obj->dev);
  302. return PTR_ERR(cr);
  303. }
  304. iotlb_load_cr(obj, cr);
  305. kfree(cr);
  306. if (e->prsvd)
  307. l.base++;
  308. /* increment victim for next tlb load */
  309. if (++l.vict == obj->nr_tlb_entries)
  310. l.vict = l.base;
  311. iotlb_lock_set(obj, &l);
  312. out:
  313. pm_runtime_put_sync(obj->dev);
  314. return err;
  315. }
  316. #else /* !PREFETCH_IOTLB */
  317. static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
  318. {
  319. return 0;
  320. }
  321. #endif /* !PREFETCH_IOTLB */
  322. static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
  323. {
  324. return load_iotlb_entry(obj, e);
  325. }
  326. /**
  327. * flush_iotlb_page - Clear an iommu tlb entry
  328. * @obj: target iommu
  329. * @da: iommu device virtual address
  330. *
  331. * Clear an iommu tlb entry which includes 'da' address.
  332. **/
  333. static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
  334. {
  335. int i;
  336. struct cr_regs cr;
  337. pm_runtime_get_sync(obj->dev);
  338. for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
  339. u32 start;
  340. size_t bytes;
  341. if (!iotlb_cr_valid(&cr))
  342. continue;
  343. start = iotlb_cr_to_virt(&cr);
  344. bytes = iopgsz_to_bytes(cr.cam & 3);
  345. if ((start <= da) && (da < start + bytes)) {
  346. dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
  347. __func__, start, da, bytes);
  348. iotlb_load_cr(obj, &cr);
  349. iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
  350. break;
  351. }
  352. }
  353. pm_runtime_put_sync(obj->dev);
  354. if (i == obj->nr_tlb_entries)
  355. dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
  356. }
  357. /**
  358. * flush_iotlb_all - Clear all iommu tlb entries
  359. * @obj: target iommu
  360. **/
  361. static void flush_iotlb_all(struct omap_iommu *obj)
  362. {
  363. struct iotlb_lock l;
  364. pm_runtime_get_sync(obj->dev);
  365. l.base = 0;
  366. l.vict = 0;
  367. iotlb_lock_set(obj, &l);
  368. iommu_write_reg(obj, 1, MMU_GFLUSH);
  369. pm_runtime_put_sync(obj->dev);
  370. }
  371. /*
  372. * H/W pagetable operations
  373. */
  374. static void flush_iopgd_range(u32 *first, u32 *last)
  375. {
  376. /* FIXME: L2 cache should be taken care of if it exists */
  377. do {
  378. asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
  379. : : "r" (first));
  380. first += L1_CACHE_BYTES / sizeof(*first);
  381. } while (first <= last);
  382. }
  383. static void flush_iopte_range(u32 *first, u32 *last)
  384. {
  385. /* FIXME: L2 cache should be taken care of if it exists */
  386. do {
  387. asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
  388. : : "r" (first));
  389. first += L1_CACHE_BYTES / sizeof(*first);
  390. } while (first <= last);
  391. }
  392. static void iopte_free(u32 *iopte)
  393. {
  394. /* Note: freed iopte's must be clean ready for re-use */
  395. if (iopte)
  396. kmem_cache_free(iopte_cachep, iopte);
  397. }
  398. static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
  399. {
  400. u32 *iopte;
  401. /* a table has already existed */
  402. if (*iopgd)
  403. goto pte_ready;
  404. /*
  405. * do the allocation outside the page table lock
  406. */
  407. spin_unlock(&obj->page_table_lock);
  408. iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
  409. spin_lock(&obj->page_table_lock);
  410. if (!*iopgd) {
  411. if (!iopte)
  412. return ERR_PTR(-ENOMEM);
  413. *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
  414. flush_iopgd_range(iopgd, iopgd);
  415. dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
  416. } else {
  417. /* We raced, free the reduniovant table */
  418. iopte_free(iopte);
  419. }
  420. pte_ready:
  421. iopte = iopte_offset(iopgd, da);
  422. dev_vdbg(obj->dev,
  423. "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
  424. __func__, da, iopgd, *iopgd, iopte, *iopte);
  425. return iopte;
  426. }
  427. static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  428. {
  429. u32 *iopgd = iopgd_offset(obj, da);
  430. if ((da | pa) & ~IOSECTION_MASK) {
  431. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  432. __func__, da, pa, IOSECTION_SIZE);
  433. return -EINVAL;
  434. }
  435. *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
  436. flush_iopgd_range(iopgd, iopgd);
  437. return 0;
  438. }
  439. static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  440. {
  441. u32 *iopgd = iopgd_offset(obj, da);
  442. int i;
  443. if ((da | pa) & ~IOSUPER_MASK) {
  444. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  445. __func__, da, pa, IOSUPER_SIZE);
  446. return -EINVAL;
  447. }
  448. for (i = 0; i < 16; i++)
  449. *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
  450. flush_iopgd_range(iopgd, iopgd + 15);
  451. return 0;
  452. }
  453. static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  454. {
  455. u32 *iopgd = iopgd_offset(obj, da);
  456. u32 *iopte = iopte_alloc(obj, iopgd, da);
  457. if (IS_ERR(iopte))
  458. return PTR_ERR(iopte);
  459. *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
  460. flush_iopte_range(iopte, iopte);
  461. dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
  462. __func__, da, pa, iopte, *iopte);
  463. return 0;
  464. }
  465. static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  466. {
  467. u32 *iopgd = iopgd_offset(obj, da);
  468. u32 *iopte = iopte_alloc(obj, iopgd, da);
  469. int i;
  470. if ((da | pa) & ~IOLARGE_MASK) {
  471. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  472. __func__, da, pa, IOLARGE_SIZE);
  473. return -EINVAL;
  474. }
  475. if (IS_ERR(iopte))
  476. return PTR_ERR(iopte);
  477. for (i = 0; i < 16; i++)
  478. *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
  479. flush_iopte_range(iopte, iopte + 15);
  480. return 0;
  481. }
  482. static int
  483. iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
  484. {
  485. int (*fn)(struct omap_iommu *, u32, u32, u32);
  486. u32 prot;
  487. int err;
  488. if (!obj || !e)
  489. return -EINVAL;
  490. switch (e->pgsz) {
  491. case MMU_CAM_PGSZ_16M:
  492. fn = iopgd_alloc_super;
  493. break;
  494. case MMU_CAM_PGSZ_1M:
  495. fn = iopgd_alloc_section;
  496. break;
  497. case MMU_CAM_PGSZ_64K:
  498. fn = iopte_alloc_large;
  499. break;
  500. case MMU_CAM_PGSZ_4K:
  501. fn = iopte_alloc_page;
  502. break;
  503. default:
  504. fn = NULL;
  505. break;
  506. }
  507. if (WARN_ON(!fn))
  508. return -EINVAL;
  509. prot = get_iopte_attr(e);
  510. spin_lock(&obj->page_table_lock);
  511. err = fn(obj, e->da, e->pa, prot);
  512. spin_unlock(&obj->page_table_lock);
  513. return err;
  514. }
  515. /**
  516. * omap_iopgtable_store_entry - Make an iommu pte entry
  517. * @obj: target iommu
  518. * @e: an iommu tlb entry info
  519. **/
  520. static int
  521. omap_iopgtable_store_entry(struct omap_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. if (!err)
  527. prefetch_iotlb_entry(obj, e);
  528. return err;
  529. }
  530. /**
  531. * iopgtable_lookup_entry - Lookup an iommu pte entry
  532. * @obj: target iommu
  533. * @da: iommu device virtual address
  534. * @ppgd: iommu pgd entry pointer to be returned
  535. * @ppte: iommu pte entry pointer to be returned
  536. **/
  537. static void
  538. iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
  539. {
  540. u32 *iopgd, *iopte = NULL;
  541. iopgd = iopgd_offset(obj, da);
  542. if (!*iopgd)
  543. goto out;
  544. if (iopgd_is_table(*iopgd))
  545. iopte = iopte_offset(iopgd, da);
  546. out:
  547. *ppgd = iopgd;
  548. *ppte = iopte;
  549. }
  550. static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
  551. {
  552. size_t bytes;
  553. u32 *iopgd = iopgd_offset(obj, da);
  554. int nent = 1;
  555. if (!*iopgd)
  556. return 0;
  557. if (iopgd_is_table(*iopgd)) {
  558. int i;
  559. u32 *iopte = iopte_offset(iopgd, da);
  560. bytes = IOPTE_SIZE;
  561. if (*iopte & IOPTE_LARGE) {
  562. nent *= 16;
  563. /* rewind to the 1st entry */
  564. iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
  565. }
  566. bytes *= nent;
  567. memset(iopte, 0, nent * sizeof(*iopte));
  568. flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
  569. /*
  570. * do table walk to check if this table is necessary or not
  571. */
  572. iopte = iopte_offset(iopgd, 0);
  573. for (i = 0; i < PTRS_PER_IOPTE; i++)
  574. if (iopte[i])
  575. goto out;
  576. iopte_free(iopte);
  577. nent = 1; /* for the next L1 entry */
  578. } else {
  579. bytes = IOPGD_SIZE;
  580. if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
  581. nent *= 16;
  582. /* rewind to the 1st entry */
  583. iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
  584. }
  585. bytes *= nent;
  586. }
  587. memset(iopgd, 0, nent * sizeof(*iopgd));
  588. flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
  589. out:
  590. return bytes;
  591. }
  592. /**
  593. * iopgtable_clear_entry - Remove an iommu pte entry
  594. * @obj: target iommu
  595. * @da: iommu device virtual address
  596. **/
  597. static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
  598. {
  599. size_t bytes;
  600. spin_lock(&obj->page_table_lock);
  601. bytes = iopgtable_clear_entry_core(obj, da);
  602. flush_iotlb_page(obj, da);
  603. spin_unlock(&obj->page_table_lock);
  604. return bytes;
  605. }
  606. static void iopgtable_clear_entry_all(struct omap_iommu *obj)
  607. {
  608. int i;
  609. spin_lock(&obj->page_table_lock);
  610. for (i = 0; i < PTRS_PER_IOPGD; i++) {
  611. u32 da;
  612. u32 *iopgd;
  613. da = i << IOPGD_SHIFT;
  614. iopgd = iopgd_offset(obj, da);
  615. if (!*iopgd)
  616. continue;
  617. if (iopgd_is_table(*iopgd))
  618. iopte_free(iopte_offset(iopgd, 0));
  619. *iopgd = 0;
  620. flush_iopgd_range(iopgd, iopgd);
  621. }
  622. flush_iotlb_all(obj);
  623. spin_unlock(&obj->page_table_lock);
  624. }
  625. /*
  626. * Device IOMMU generic operations
  627. */
  628. static irqreturn_t iommu_fault_handler(int irq, void *data)
  629. {
  630. u32 da, errs;
  631. u32 *iopgd, *iopte;
  632. struct omap_iommu *obj = data;
  633. struct iommu_domain *domain = obj->domain;
  634. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  635. if (!omap_domain->iommu_dev)
  636. return IRQ_NONE;
  637. errs = iommu_report_fault(obj, &da);
  638. if (errs == 0)
  639. return IRQ_HANDLED;
  640. /* Fault callback or TLB/PTE Dynamic loading */
  641. if (!report_iommu_fault(domain, obj->dev, da, 0))
  642. return IRQ_HANDLED;
  643. iommu_disable(obj);
  644. iopgd = iopgd_offset(obj, da);
  645. if (!iopgd_is_table(*iopgd)) {
  646. dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n",
  647. obj->name, errs, da, iopgd, *iopgd);
  648. return IRQ_NONE;
  649. }
  650. iopte = iopte_offset(iopgd, da);
  651. dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x pte:0x%p *pte:0x%08x\n",
  652. obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
  653. return IRQ_NONE;
  654. }
  655. static int device_match_by_alias(struct device *dev, void *data)
  656. {
  657. struct omap_iommu *obj = to_iommu(dev);
  658. const char *name = data;
  659. pr_debug("%s: %s %s\n", __func__, obj->name, name);
  660. return strcmp(obj->name, name) == 0;
  661. }
  662. /**
  663. * omap_iommu_attach() - attach iommu device to an iommu domain
  664. * @name: name of target omap iommu device
  665. * @iopgd: page table
  666. **/
  667. static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd)
  668. {
  669. int err;
  670. struct device *dev;
  671. struct omap_iommu *obj;
  672. dev = driver_find_device(&omap_iommu_driver.driver, NULL, (void *)name,
  673. device_match_by_alias);
  674. if (!dev)
  675. return ERR_PTR(-ENODEV);
  676. obj = to_iommu(dev);
  677. spin_lock(&obj->iommu_lock);
  678. obj->iopgd = iopgd;
  679. err = iommu_enable(obj);
  680. if (err)
  681. goto err_enable;
  682. flush_iotlb_all(obj);
  683. spin_unlock(&obj->iommu_lock);
  684. dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
  685. return obj;
  686. err_enable:
  687. spin_unlock(&obj->iommu_lock);
  688. return ERR_PTR(err);
  689. }
  690. /**
  691. * omap_iommu_detach - release iommu device
  692. * @obj: target iommu
  693. **/
  694. static void omap_iommu_detach(struct omap_iommu *obj)
  695. {
  696. if (!obj || IS_ERR(obj))
  697. return;
  698. spin_lock(&obj->iommu_lock);
  699. iommu_disable(obj);
  700. obj->iopgd = NULL;
  701. spin_unlock(&obj->iommu_lock);
  702. dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
  703. }
  704. static int omap_iommu_dra7_get_dsp_system_cfg(struct platform_device *pdev,
  705. struct omap_iommu *obj)
  706. {
  707. struct device_node *np = pdev->dev.of_node;
  708. int ret;
  709. if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu"))
  710. return 0;
  711. if (!of_property_read_bool(np, "ti,syscon-mmuconfig")) {
  712. dev_err(&pdev->dev, "ti,syscon-mmuconfig property is missing\n");
  713. return -EINVAL;
  714. }
  715. obj->syscfg =
  716. syscon_regmap_lookup_by_phandle(np, "ti,syscon-mmuconfig");
  717. if (IS_ERR(obj->syscfg)) {
  718. /* can fail with -EPROBE_DEFER */
  719. ret = PTR_ERR(obj->syscfg);
  720. return ret;
  721. }
  722. if (of_property_read_u32_index(np, "ti,syscon-mmuconfig", 1,
  723. &obj->id)) {
  724. dev_err(&pdev->dev, "couldn't get the IOMMU instance id within subsystem\n");
  725. return -EINVAL;
  726. }
  727. if (obj->id != 0 && obj->id != 1) {
  728. dev_err(&pdev->dev, "invalid IOMMU instance id\n");
  729. return -EINVAL;
  730. }
  731. return 0;
  732. }
  733. /*
  734. * OMAP Device MMU(IOMMU) detection
  735. */
  736. static int omap_iommu_probe(struct platform_device *pdev)
  737. {
  738. int err = -ENODEV;
  739. int irq;
  740. struct omap_iommu *obj;
  741. struct resource *res;
  742. struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
  743. struct device_node *of = pdev->dev.of_node;
  744. obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
  745. if (!obj)
  746. return -ENOMEM;
  747. if (of) {
  748. obj->name = dev_name(&pdev->dev);
  749. obj->nr_tlb_entries = 32;
  750. err = of_property_read_u32(of, "ti,#tlb-entries",
  751. &obj->nr_tlb_entries);
  752. if (err && err != -EINVAL)
  753. return err;
  754. if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
  755. return -EINVAL;
  756. if (of_find_property(of, "ti,iommu-bus-err-back", NULL))
  757. obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
  758. } else {
  759. obj->nr_tlb_entries = pdata->nr_tlb_entries;
  760. obj->name = pdata->name;
  761. }
  762. obj->dev = &pdev->dev;
  763. obj->ctx = (void *)obj + sizeof(*obj);
  764. spin_lock_init(&obj->iommu_lock);
  765. spin_lock_init(&obj->page_table_lock);
  766. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  767. obj->regbase = devm_ioremap_resource(obj->dev, res);
  768. if (IS_ERR(obj->regbase))
  769. return PTR_ERR(obj->regbase);
  770. err = omap_iommu_dra7_get_dsp_system_cfg(pdev, obj);
  771. if (err)
  772. return err;
  773. irq = platform_get_irq(pdev, 0);
  774. if (irq < 0)
  775. return -ENODEV;
  776. err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
  777. dev_name(obj->dev), obj);
  778. if (err < 0)
  779. return err;
  780. platform_set_drvdata(pdev, obj);
  781. pm_runtime_irq_safe(obj->dev);
  782. pm_runtime_enable(obj->dev);
  783. omap_iommu_debugfs_add(obj);
  784. dev_info(&pdev->dev, "%s registered\n", obj->name);
  785. return 0;
  786. }
  787. static int omap_iommu_remove(struct platform_device *pdev)
  788. {
  789. struct omap_iommu *obj = platform_get_drvdata(pdev);
  790. omap_iommu_debugfs_remove(obj);
  791. pm_runtime_disable(obj->dev);
  792. dev_info(&pdev->dev, "%s removed\n", obj->name);
  793. return 0;
  794. }
  795. static const struct of_device_id omap_iommu_of_match[] = {
  796. { .compatible = "ti,omap2-iommu" },
  797. { .compatible = "ti,omap4-iommu" },
  798. { .compatible = "ti,dra7-iommu" },
  799. { .compatible = "ti,dra7-dsp-iommu" },
  800. {},
  801. };
  802. static struct platform_driver omap_iommu_driver = {
  803. .probe = omap_iommu_probe,
  804. .remove = omap_iommu_remove,
  805. .driver = {
  806. .name = "omap-iommu",
  807. .of_match_table = of_match_ptr(omap_iommu_of_match),
  808. },
  809. };
  810. static void iopte_cachep_ctor(void *iopte)
  811. {
  812. clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
  813. }
  814. static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz)
  815. {
  816. memset(e, 0, sizeof(*e));
  817. e->da = da;
  818. e->pa = pa;
  819. e->valid = MMU_CAM_V;
  820. e->pgsz = pgsz;
  821. e->endian = MMU_RAM_ENDIAN_LITTLE;
  822. e->elsz = MMU_RAM_ELSZ_8;
  823. e->mixed = 0;
  824. return iopgsz_to_bytes(e->pgsz);
  825. }
  826. static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
  827. phys_addr_t pa, size_t bytes, int prot)
  828. {
  829. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  830. struct omap_iommu *oiommu = omap_domain->iommu_dev;
  831. struct device *dev = oiommu->dev;
  832. struct iotlb_entry e;
  833. int omap_pgsz;
  834. u32 ret;
  835. omap_pgsz = bytes_to_iopgsz(bytes);
  836. if (omap_pgsz < 0) {
  837. dev_err(dev, "invalid size to map: %d\n", bytes);
  838. return -EINVAL;
  839. }
  840. dev_dbg(dev, "mapping da 0x%lx to pa %pa size 0x%x\n", da, &pa, bytes);
  841. iotlb_init_entry(&e, da, pa, omap_pgsz);
  842. ret = omap_iopgtable_store_entry(oiommu, &e);
  843. if (ret)
  844. dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
  845. return ret;
  846. }
  847. static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
  848. size_t size)
  849. {
  850. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  851. struct omap_iommu *oiommu = omap_domain->iommu_dev;
  852. struct device *dev = oiommu->dev;
  853. dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
  854. return iopgtable_clear_entry(oiommu, da);
  855. }
  856. static int
  857. omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
  858. {
  859. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  860. struct omap_iommu *oiommu;
  861. struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
  862. int ret = 0;
  863. if (!arch_data || !arch_data->name) {
  864. dev_err(dev, "device doesn't have an associated iommu\n");
  865. return -EINVAL;
  866. }
  867. spin_lock(&omap_domain->lock);
  868. /* only a single device is supported per domain for now */
  869. if (omap_domain->iommu_dev) {
  870. dev_err(dev, "iommu domain is already attached\n");
  871. ret = -EBUSY;
  872. goto out;
  873. }
  874. /* get a handle to and enable the omap iommu */
  875. oiommu = omap_iommu_attach(arch_data->name, omap_domain->pgtable);
  876. if (IS_ERR(oiommu)) {
  877. ret = PTR_ERR(oiommu);
  878. dev_err(dev, "can't get omap iommu: %d\n", ret);
  879. goto out;
  880. }
  881. omap_domain->iommu_dev = arch_data->iommu_dev = oiommu;
  882. omap_domain->dev = dev;
  883. oiommu->domain = domain;
  884. out:
  885. spin_unlock(&omap_domain->lock);
  886. return ret;
  887. }
  888. static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
  889. struct device *dev)
  890. {
  891. struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
  892. struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
  893. /* only a single device is supported per domain for now */
  894. if (omap_domain->iommu_dev != oiommu) {
  895. dev_err(dev, "invalid iommu device\n");
  896. return;
  897. }
  898. iopgtable_clear_entry_all(oiommu);
  899. omap_iommu_detach(oiommu);
  900. omap_domain->iommu_dev = arch_data->iommu_dev = NULL;
  901. omap_domain->dev = NULL;
  902. oiommu->domain = NULL;
  903. }
  904. static void omap_iommu_detach_dev(struct iommu_domain *domain,
  905. struct device *dev)
  906. {
  907. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  908. spin_lock(&omap_domain->lock);
  909. _omap_iommu_detach_dev(omap_domain, dev);
  910. spin_unlock(&omap_domain->lock);
  911. }
  912. static struct iommu_domain *omap_iommu_domain_alloc(unsigned type)
  913. {
  914. struct omap_iommu_domain *omap_domain;
  915. if (type != IOMMU_DOMAIN_UNMANAGED)
  916. return NULL;
  917. omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
  918. if (!omap_domain)
  919. goto out;
  920. omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
  921. if (!omap_domain->pgtable)
  922. goto fail_nomem;
  923. /*
  924. * should never fail, but please keep this around to ensure
  925. * we keep the hardware happy
  926. */
  927. if (WARN_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE)))
  928. goto fail_align;
  929. clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE);
  930. spin_lock_init(&omap_domain->lock);
  931. omap_domain->domain.geometry.aperture_start = 0;
  932. omap_domain->domain.geometry.aperture_end = (1ULL << 32) - 1;
  933. omap_domain->domain.geometry.force_aperture = true;
  934. return &omap_domain->domain;
  935. fail_align:
  936. kfree(omap_domain->pgtable);
  937. fail_nomem:
  938. kfree(omap_domain);
  939. out:
  940. return NULL;
  941. }
  942. static void omap_iommu_domain_free(struct iommu_domain *domain)
  943. {
  944. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  945. /*
  946. * An iommu device is still attached
  947. * (currently, only one device can be attached) ?
  948. */
  949. if (omap_domain->iommu_dev)
  950. _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
  951. kfree(omap_domain->pgtable);
  952. kfree(omap_domain);
  953. }
  954. static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
  955. dma_addr_t da)
  956. {
  957. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  958. struct omap_iommu *oiommu = omap_domain->iommu_dev;
  959. struct device *dev = oiommu->dev;
  960. u32 *pgd, *pte;
  961. phys_addr_t ret = 0;
  962. iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
  963. if (pte) {
  964. if (iopte_is_small(*pte))
  965. ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
  966. else if (iopte_is_large(*pte))
  967. ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
  968. else
  969. dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
  970. (unsigned long long)da);
  971. } else {
  972. if (iopgd_is_section(*pgd))
  973. ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
  974. else if (iopgd_is_super(*pgd))
  975. ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
  976. else
  977. dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
  978. (unsigned long long)da);
  979. }
  980. return ret;
  981. }
  982. static int omap_iommu_add_device(struct device *dev)
  983. {
  984. struct omap_iommu_arch_data *arch_data;
  985. struct device_node *np;
  986. struct platform_device *pdev;
  987. /*
  988. * Allocate the archdata iommu structure for DT-based devices.
  989. *
  990. * TODO: Simplify this when removing non-DT support completely from the
  991. * IOMMU users.
  992. */
  993. if (!dev->of_node)
  994. return 0;
  995. np = of_parse_phandle(dev->of_node, "iommus", 0);
  996. if (!np)
  997. return 0;
  998. pdev = of_find_device_by_node(np);
  999. if (WARN_ON(!pdev)) {
  1000. of_node_put(np);
  1001. return -EINVAL;
  1002. }
  1003. arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
  1004. if (!arch_data) {
  1005. of_node_put(np);
  1006. return -ENOMEM;
  1007. }
  1008. arch_data->name = kstrdup(dev_name(&pdev->dev), GFP_KERNEL);
  1009. dev->archdata.iommu = arch_data;
  1010. of_node_put(np);
  1011. return 0;
  1012. }
  1013. static void omap_iommu_remove_device(struct device *dev)
  1014. {
  1015. struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
  1016. if (!dev->of_node || !arch_data)
  1017. return;
  1018. kfree(arch_data->name);
  1019. kfree(arch_data);
  1020. }
  1021. static const struct iommu_ops omap_iommu_ops = {
  1022. .domain_alloc = omap_iommu_domain_alloc,
  1023. .domain_free = omap_iommu_domain_free,
  1024. .attach_dev = omap_iommu_attach_dev,
  1025. .detach_dev = omap_iommu_detach_dev,
  1026. .map = omap_iommu_map,
  1027. .unmap = omap_iommu_unmap,
  1028. .map_sg = default_iommu_map_sg,
  1029. .iova_to_phys = omap_iommu_iova_to_phys,
  1030. .add_device = omap_iommu_add_device,
  1031. .remove_device = omap_iommu_remove_device,
  1032. .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
  1033. };
  1034. static int __init omap_iommu_init(void)
  1035. {
  1036. struct kmem_cache *p;
  1037. const unsigned long flags = SLAB_HWCACHE_ALIGN;
  1038. size_t align = 1 << 10; /* L2 pagetable alignement */
  1039. struct device_node *np;
  1040. int ret;
  1041. np = of_find_matching_node(NULL, omap_iommu_of_match);
  1042. if (!np)
  1043. return 0;
  1044. of_node_put(np);
  1045. p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
  1046. iopte_cachep_ctor);
  1047. if (!p)
  1048. return -ENOMEM;
  1049. iopte_cachep = p;
  1050. omap_iommu_debugfs_init();
  1051. ret = platform_driver_register(&omap_iommu_driver);
  1052. if (ret) {
  1053. pr_err("%s: failed to register driver\n", __func__);
  1054. goto fail_driver;
  1055. }
  1056. ret = bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
  1057. if (ret)
  1058. goto fail_bus;
  1059. return 0;
  1060. fail_bus:
  1061. platform_driver_unregister(&omap_iommu_driver);
  1062. fail_driver:
  1063. kmem_cache_destroy(iopte_cachep);
  1064. return ret;
  1065. }
  1066. subsys_initcall(omap_iommu_init);
  1067. /* must be ready before omap3isp is probed */