omap-iommu.c 31 KB

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