omap-iommu.c 30 KB

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