omap-iommu.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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 <asm/cacheflush.h>
  29. #include <linux/platform_data/iommu-omap.h>
  30. #include "omap-iopgtable.h"
  31. #include "omap-iommu.h"
  32. #define to_iommu(dev) \
  33. ((struct omap_iommu *)platform_get_drvdata(to_platform_device(dev)))
  34. /* bitmap of the page sizes currently supported */
  35. #define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
  36. /**
  37. * struct omap_iommu_domain - omap iommu domain
  38. * @pgtable: the page table
  39. * @iommu_dev: an omap iommu device attached to this domain. only a single
  40. * iommu device can be attached for now.
  41. * @dev: Device using this domain.
  42. * @lock: domain lock, should be taken when attaching/detaching
  43. */
  44. struct omap_iommu_domain {
  45. u32 *pgtable;
  46. struct omap_iommu *iommu_dev;
  47. struct device *dev;
  48. spinlock_t lock;
  49. struct iommu_domain domain;
  50. };
  51. #define MMU_LOCK_BASE_SHIFT 10
  52. #define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
  53. #define MMU_LOCK_BASE(x) \
  54. ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
  55. #define MMU_LOCK_VICT_SHIFT 4
  56. #define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
  57. #define MMU_LOCK_VICT(x) \
  58. ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
  59. static struct platform_driver omap_iommu_driver;
  60. static struct kmem_cache *iopte_cachep;
  61. /**
  62. * to_omap_domain - Get struct omap_iommu_domain from generic iommu_domain
  63. * @dom: generic iommu domain handle
  64. **/
  65. static struct omap_iommu_domain *to_omap_domain(struct iommu_domain *dom)
  66. {
  67. return container_of(dom, struct omap_iommu_domain, domain);
  68. }
  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 u32 iotlb_cr_to_virt(struct cr_regs *cr)
  166. {
  167. u32 page_size = cr->cam & MMU_CAM_PGSZ_MASK;
  168. u32 mask = get_cam_va_mask(cr->cam & page_size);
  169. return cr->cam & mask;
  170. }
  171. static u32 get_iopte_attr(struct iotlb_entry *e)
  172. {
  173. u32 attr;
  174. attr = e->mixed << 5;
  175. attr |= e->endian;
  176. attr |= e->elsz >> 3;
  177. attr <<= (((e->pgsz == MMU_CAM_PGSZ_4K) ||
  178. (e->pgsz == MMU_CAM_PGSZ_64K)) ? 0 : 6);
  179. return attr;
  180. }
  181. static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
  182. {
  183. u32 status, fault_addr;
  184. status = iommu_read_reg(obj, MMU_IRQSTATUS);
  185. status &= MMU_IRQ_MASK;
  186. if (!status) {
  187. *da = 0;
  188. return 0;
  189. }
  190. fault_addr = iommu_read_reg(obj, MMU_FAULT_AD);
  191. *da = fault_addr;
  192. iommu_write_reg(obj, status, MMU_IRQSTATUS);
  193. return status;
  194. }
  195. void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
  196. {
  197. u32 val;
  198. val = iommu_read_reg(obj, MMU_LOCK);
  199. l->base = MMU_LOCK_BASE(val);
  200. l->vict = MMU_LOCK_VICT(val);
  201. }
  202. void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
  203. {
  204. u32 val;
  205. val = (l->base << MMU_LOCK_BASE_SHIFT);
  206. val |= (l->vict << MMU_LOCK_VICT_SHIFT);
  207. iommu_write_reg(obj, val, MMU_LOCK);
  208. }
  209. static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
  210. {
  211. cr->cam = iommu_read_reg(obj, MMU_READ_CAM);
  212. cr->ram = iommu_read_reg(obj, MMU_READ_RAM);
  213. }
  214. static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
  215. {
  216. iommu_write_reg(obj, cr->cam | MMU_CAM_V, MMU_CAM);
  217. iommu_write_reg(obj, cr->ram, MMU_RAM);
  218. iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
  219. iommu_write_reg(obj, 1, MMU_LD_TLB);
  220. }
  221. /* only used in iotlb iteration for-loop */
  222. struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
  223. {
  224. struct cr_regs cr;
  225. struct iotlb_lock l;
  226. iotlb_lock_get(obj, &l);
  227. l.vict = n;
  228. iotlb_lock_set(obj, &l);
  229. iotlb_read_cr(obj, &cr);
  230. return cr;
  231. }
  232. #ifdef PREFETCH_IOTLB
  233. static struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
  234. struct iotlb_entry *e)
  235. {
  236. struct cr_regs *cr;
  237. if (!e)
  238. return NULL;
  239. if (e->da & ~(get_cam_va_mask(e->pgsz))) {
  240. dev_err(obj->dev, "%s:\twrong alignment: %08x\n", __func__,
  241. e->da);
  242. return ERR_PTR(-EINVAL);
  243. }
  244. cr = kmalloc(sizeof(*cr), GFP_KERNEL);
  245. if (!cr)
  246. return ERR_PTR(-ENOMEM);
  247. cr->cam = (e->da & MMU_CAM_VATAG_MASK) | e->prsvd | e->pgsz | e->valid;
  248. cr->ram = e->pa | e->endian | e->elsz | e->mixed;
  249. return cr;
  250. }
  251. /**
  252. * load_iotlb_entry - Set an iommu tlb entry
  253. * @obj: target iommu
  254. * @e: an iommu tlb entry info
  255. **/
  256. static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
  257. {
  258. int err = 0;
  259. struct iotlb_lock l;
  260. struct cr_regs *cr;
  261. if (!obj || !obj->nr_tlb_entries || !e)
  262. return -EINVAL;
  263. pm_runtime_get_sync(obj->dev);
  264. iotlb_lock_get(obj, &l);
  265. if (l.base == obj->nr_tlb_entries) {
  266. dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
  267. err = -EBUSY;
  268. goto out;
  269. }
  270. if (!e->prsvd) {
  271. int i;
  272. struct cr_regs tmp;
  273. for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
  274. if (!iotlb_cr_valid(&tmp))
  275. break;
  276. if (i == obj->nr_tlb_entries) {
  277. dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
  278. err = -EBUSY;
  279. goto out;
  280. }
  281. iotlb_lock_get(obj, &l);
  282. } else {
  283. l.vict = l.base;
  284. iotlb_lock_set(obj, &l);
  285. }
  286. cr = iotlb_alloc_cr(obj, e);
  287. if (IS_ERR(cr)) {
  288. pm_runtime_put_sync(obj->dev);
  289. return PTR_ERR(cr);
  290. }
  291. iotlb_load_cr(obj, cr);
  292. kfree(cr);
  293. if (e->prsvd)
  294. l.base++;
  295. /* increment victim for next tlb load */
  296. if (++l.vict == obj->nr_tlb_entries)
  297. l.vict = l.base;
  298. iotlb_lock_set(obj, &l);
  299. out:
  300. pm_runtime_put_sync(obj->dev);
  301. return err;
  302. }
  303. #else /* !PREFETCH_IOTLB */
  304. static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
  305. {
  306. return 0;
  307. }
  308. #endif /* !PREFETCH_IOTLB */
  309. static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
  310. {
  311. return load_iotlb_entry(obj, e);
  312. }
  313. /**
  314. * flush_iotlb_page - Clear an iommu tlb entry
  315. * @obj: target iommu
  316. * @da: iommu device virtual address
  317. *
  318. * Clear an iommu tlb entry which includes 'da' address.
  319. **/
  320. static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
  321. {
  322. int i;
  323. struct cr_regs cr;
  324. pm_runtime_get_sync(obj->dev);
  325. for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
  326. u32 start;
  327. size_t bytes;
  328. if (!iotlb_cr_valid(&cr))
  329. continue;
  330. start = iotlb_cr_to_virt(&cr);
  331. bytes = iopgsz_to_bytes(cr.cam & 3);
  332. if ((start <= da) && (da < start + bytes)) {
  333. dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
  334. __func__, start, da, bytes);
  335. iotlb_load_cr(obj, &cr);
  336. iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
  337. break;
  338. }
  339. }
  340. pm_runtime_put_sync(obj->dev);
  341. if (i == obj->nr_tlb_entries)
  342. dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
  343. }
  344. /**
  345. * flush_iotlb_all - Clear all iommu tlb entries
  346. * @obj: target iommu
  347. **/
  348. static void flush_iotlb_all(struct omap_iommu *obj)
  349. {
  350. struct iotlb_lock l;
  351. pm_runtime_get_sync(obj->dev);
  352. l.base = 0;
  353. l.vict = 0;
  354. iotlb_lock_set(obj, &l);
  355. iommu_write_reg(obj, 1, MMU_GFLUSH);
  356. pm_runtime_put_sync(obj->dev);
  357. }
  358. /*
  359. * H/W pagetable operations
  360. */
  361. static void flush_iopgd_range(u32 *first, u32 *last)
  362. {
  363. /* FIXME: L2 cache should be taken care of if it exists */
  364. do {
  365. asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
  366. : : "r" (first));
  367. first += L1_CACHE_BYTES / sizeof(*first);
  368. } while (first <= last);
  369. }
  370. static void flush_iopte_range(u32 *first, u32 *last)
  371. {
  372. /* FIXME: L2 cache should be taken care of if it exists */
  373. do {
  374. asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
  375. : : "r" (first));
  376. first += L1_CACHE_BYTES / sizeof(*first);
  377. } while (first <= last);
  378. }
  379. static void iopte_free(u32 *iopte)
  380. {
  381. /* Note: freed iopte's must be clean ready for re-use */
  382. if (iopte)
  383. kmem_cache_free(iopte_cachep, iopte);
  384. }
  385. static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
  386. {
  387. u32 *iopte;
  388. /* a table has already existed */
  389. if (*iopgd)
  390. goto pte_ready;
  391. /*
  392. * do the allocation outside the page table lock
  393. */
  394. spin_unlock(&obj->page_table_lock);
  395. iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
  396. spin_lock(&obj->page_table_lock);
  397. if (!*iopgd) {
  398. if (!iopte)
  399. return ERR_PTR(-ENOMEM);
  400. *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
  401. flush_iopgd_range(iopgd, iopgd);
  402. dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
  403. } else {
  404. /* We raced, free the reduniovant table */
  405. iopte_free(iopte);
  406. }
  407. pte_ready:
  408. iopte = iopte_offset(iopgd, da);
  409. dev_vdbg(obj->dev,
  410. "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
  411. __func__, da, iopgd, *iopgd, iopte, *iopte);
  412. return iopte;
  413. }
  414. static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  415. {
  416. u32 *iopgd = iopgd_offset(obj, da);
  417. if ((da | pa) & ~IOSECTION_MASK) {
  418. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  419. __func__, da, pa, IOSECTION_SIZE);
  420. return -EINVAL;
  421. }
  422. *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
  423. flush_iopgd_range(iopgd, iopgd);
  424. return 0;
  425. }
  426. static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  427. {
  428. u32 *iopgd = iopgd_offset(obj, da);
  429. int i;
  430. if ((da | pa) & ~IOSUPER_MASK) {
  431. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  432. __func__, da, pa, IOSUPER_SIZE);
  433. return -EINVAL;
  434. }
  435. for (i = 0; i < 16; i++)
  436. *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
  437. flush_iopgd_range(iopgd, iopgd + 15);
  438. return 0;
  439. }
  440. static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  441. {
  442. u32 *iopgd = iopgd_offset(obj, da);
  443. u32 *iopte = iopte_alloc(obj, iopgd, da);
  444. if (IS_ERR(iopte))
  445. return PTR_ERR(iopte);
  446. *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
  447. flush_iopte_range(iopte, iopte);
  448. dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
  449. __func__, da, pa, iopte, *iopte);
  450. return 0;
  451. }
  452. static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  453. {
  454. u32 *iopgd = iopgd_offset(obj, da);
  455. u32 *iopte = iopte_alloc(obj, iopgd, da);
  456. int i;
  457. if ((da | pa) & ~IOLARGE_MASK) {
  458. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  459. __func__, da, pa, IOLARGE_SIZE);
  460. return -EINVAL;
  461. }
  462. if (IS_ERR(iopte))
  463. return PTR_ERR(iopte);
  464. for (i = 0; i < 16; i++)
  465. *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
  466. flush_iopte_range(iopte, iopte + 15);
  467. return 0;
  468. }
  469. static int
  470. iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
  471. {
  472. int (*fn)(struct omap_iommu *, u32, u32, u32);
  473. u32 prot;
  474. int err;
  475. if (!obj || !e)
  476. return -EINVAL;
  477. switch (e->pgsz) {
  478. case MMU_CAM_PGSZ_16M:
  479. fn = iopgd_alloc_super;
  480. break;
  481. case MMU_CAM_PGSZ_1M:
  482. fn = iopgd_alloc_section;
  483. break;
  484. case MMU_CAM_PGSZ_64K:
  485. fn = iopte_alloc_large;
  486. break;
  487. case MMU_CAM_PGSZ_4K:
  488. fn = iopte_alloc_page;
  489. break;
  490. default:
  491. fn = NULL;
  492. BUG();
  493. break;
  494. }
  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. static int device_match_by_alias(struct device *dev, void *data)
  642. {
  643. struct omap_iommu *obj = to_iommu(dev);
  644. const char *name = data;
  645. pr_debug("%s: %s %s\n", __func__, obj->name, name);
  646. return strcmp(obj->name, name) == 0;
  647. }
  648. /**
  649. * omap_iommu_attach() - attach iommu device to an iommu domain
  650. * @name: name of target omap iommu device
  651. * @iopgd: page table
  652. **/
  653. static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd)
  654. {
  655. int err;
  656. struct device *dev;
  657. struct omap_iommu *obj;
  658. dev = driver_find_device(&omap_iommu_driver.driver, NULL, (void *)name,
  659. device_match_by_alias);
  660. if (!dev)
  661. return ERR_PTR(-ENODEV);
  662. obj = to_iommu(dev);
  663. spin_lock(&obj->iommu_lock);
  664. obj->iopgd = iopgd;
  665. err = iommu_enable(obj);
  666. if (err)
  667. goto err_enable;
  668. flush_iotlb_all(obj);
  669. spin_unlock(&obj->iommu_lock);
  670. dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
  671. return obj;
  672. err_enable:
  673. spin_unlock(&obj->iommu_lock);
  674. return ERR_PTR(err);
  675. }
  676. /**
  677. * omap_iommu_detach - release iommu device
  678. * @obj: target iommu
  679. **/
  680. static void omap_iommu_detach(struct omap_iommu *obj)
  681. {
  682. if (!obj || IS_ERR(obj))
  683. return;
  684. spin_lock(&obj->iommu_lock);
  685. iommu_disable(obj);
  686. obj->iopgd = NULL;
  687. spin_unlock(&obj->iommu_lock);
  688. dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
  689. }
  690. /*
  691. * OMAP Device MMU(IOMMU) detection
  692. */
  693. static int omap_iommu_probe(struct platform_device *pdev)
  694. {
  695. int err = -ENODEV;
  696. int irq;
  697. struct omap_iommu *obj;
  698. struct resource *res;
  699. struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
  700. struct device_node *of = pdev->dev.of_node;
  701. obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
  702. if (!obj)
  703. return -ENOMEM;
  704. if (of) {
  705. obj->name = dev_name(&pdev->dev);
  706. obj->nr_tlb_entries = 32;
  707. err = of_property_read_u32(of, "ti,#tlb-entries",
  708. &obj->nr_tlb_entries);
  709. if (err && err != -EINVAL)
  710. return err;
  711. if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
  712. return -EINVAL;
  713. if (of_find_property(of, "ti,iommu-bus-err-back", NULL))
  714. obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
  715. } else {
  716. obj->nr_tlb_entries = pdata->nr_tlb_entries;
  717. obj->name = pdata->name;
  718. }
  719. obj->dev = &pdev->dev;
  720. obj->ctx = (void *)obj + sizeof(*obj);
  721. spin_lock_init(&obj->iommu_lock);
  722. spin_lock_init(&obj->page_table_lock);
  723. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  724. obj->regbase = devm_ioremap_resource(obj->dev, res);
  725. if (IS_ERR(obj->regbase))
  726. return PTR_ERR(obj->regbase);
  727. irq = platform_get_irq(pdev, 0);
  728. if (irq < 0)
  729. return -ENODEV;
  730. err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
  731. dev_name(obj->dev), obj);
  732. if (err < 0)
  733. return err;
  734. platform_set_drvdata(pdev, obj);
  735. pm_runtime_irq_safe(obj->dev);
  736. pm_runtime_enable(obj->dev);
  737. omap_iommu_debugfs_add(obj);
  738. dev_info(&pdev->dev, "%s registered\n", obj->name);
  739. return 0;
  740. }
  741. static int omap_iommu_remove(struct platform_device *pdev)
  742. {
  743. struct omap_iommu *obj = platform_get_drvdata(pdev);
  744. iopgtable_clear_entry_all(obj);
  745. omap_iommu_debugfs_remove(obj);
  746. pm_runtime_disable(obj->dev);
  747. dev_info(&pdev->dev, "%s removed\n", obj->name);
  748. return 0;
  749. }
  750. static const struct of_device_id omap_iommu_of_match[] = {
  751. { .compatible = "ti,omap2-iommu" },
  752. { .compatible = "ti,omap4-iommu" },
  753. { .compatible = "ti,dra7-iommu" },
  754. {},
  755. };
  756. static struct platform_driver omap_iommu_driver = {
  757. .probe = omap_iommu_probe,
  758. .remove = omap_iommu_remove,
  759. .driver = {
  760. .name = "omap-iommu",
  761. .of_match_table = of_match_ptr(omap_iommu_of_match),
  762. },
  763. };
  764. static void iopte_cachep_ctor(void *iopte)
  765. {
  766. clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
  767. }
  768. static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz)
  769. {
  770. memset(e, 0, sizeof(*e));
  771. e->da = da;
  772. e->pa = pa;
  773. e->valid = MMU_CAM_V;
  774. e->pgsz = pgsz;
  775. e->endian = MMU_RAM_ENDIAN_LITTLE;
  776. e->elsz = MMU_RAM_ELSZ_8;
  777. e->mixed = 0;
  778. return iopgsz_to_bytes(e->pgsz);
  779. }
  780. static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
  781. phys_addr_t pa, size_t bytes, int prot)
  782. {
  783. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  784. struct omap_iommu *oiommu = omap_domain->iommu_dev;
  785. struct device *dev = oiommu->dev;
  786. struct iotlb_entry e;
  787. int omap_pgsz;
  788. u32 ret;
  789. omap_pgsz = bytes_to_iopgsz(bytes);
  790. if (omap_pgsz < 0) {
  791. dev_err(dev, "invalid size to map: %d\n", bytes);
  792. return -EINVAL;
  793. }
  794. dev_dbg(dev, "mapping da 0x%lx to pa %pa size 0x%x\n", da, &pa, bytes);
  795. iotlb_init_entry(&e, da, pa, omap_pgsz);
  796. ret = omap_iopgtable_store_entry(oiommu, &e);
  797. if (ret)
  798. dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
  799. return ret;
  800. }
  801. static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
  802. size_t size)
  803. {
  804. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  805. struct omap_iommu *oiommu = omap_domain->iommu_dev;
  806. struct device *dev = oiommu->dev;
  807. dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
  808. return iopgtable_clear_entry(oiommu, da);
  809. }
  810. static int
  811. omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
  812. {
  813. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  814. struct omap_iommu *oiommu;
  815. struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
  816. int ret = 0;
  817. if (!arch_data || !arch_data->name) {
  818. dev_err(dev, "device doesn't have an associated iommu\n");
  819. return -EINVAL;
  820. }
  821. spin_lock(&omap_domain->lock);
  822. /* only a single device is supported per domain for now */
  823. if (omap_domain->iommu_dev) {
  824. dev_err(dev, "iommu domain is already attached\n");
  825. ret = -EBUSY;
  826. goto out;
  827. }
  828. /* get a handle to and enable the omap iommu */
  829. oiommu = omap_iommu_attach(arch_data->name, omap_domain->pgtable);
  830. if (IS_ERR(oiommu)) {
  831. ret = PTR_ERR(oiommu);
  832. dev_err(dev, "can't get omap iommu: %d\n", ret);
  833. goto out;
  834. }
  835. omap_domain->iommu_dev = arch_data->iommu_dev = oiommu;
  836. omap_domain->dev = dev;
  837. oiommu->domain = domain;
  838. out:
  839. spin_unlock(&omap_domain->lock);
  840. return ret;
  841. }
  842. static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
  843. struct device *dev)
  844. {
  845. struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
  846. struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
  847. /* only a single device is supported per domain for now */
  848. if (omap_domain->iommu_dev != oiommu) {
  849. dev_err(dev, "invalid iommu device\n");
  850. return;
  851. }
  852. iopgtable_clear_entry_all(oiommu);
  853. omap_iommu_detach(oiommu);
  854. omap_domain->iommu_dev = arch_data->iommu_dev = NULL;
  855. omap_domain->dev = NULL;
  856. oiommu->domain = NULL;
  857. }
  858. static void omap_iommu_detach_dev(struct iommu_domain *domain,
  859. struct device *dev)
  860. {
  861. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  862. spin_lock(&omap_domain->lock);
  863. _omap_iommu_detach_dev(omap_domain, dev);
  864. spin_unlock(&omap_domain->lock);
  865. }
  866. static struct iommu_domain *omap_iommu_domain_alloc(unsigned type)
  867. {
  868. struct omap_iommu_domain *omap_domain;
  869. if (type != IOMMU_DOMAIN_UNMANAGED)
  870. return NULL;
  871. omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
  872. if (!omap_domain)
  873. goto out;
  874. omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
  875. if (!omap_domain->pgtable)
  876. goto fail_nomem;
  877. /*
  878. * should never fail, but please keep this around to ensure
  879. * we keep the hardware happy
  880. */
  881. BUG_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE));
  882. clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE);
  883. spin_lock_init(&omap_domain->lock);
  884. omap_domain->domain.geometry.aperture_start = 0;
  885. omap_domain->domain.geometry.aperture_end = (1ULL << 32) - 1;
  886. omap_domain->domain.geometry.force_aperture = true;
  887. return &omap_domain->domain;
  888. fail_nomem:
  889. kfree(omap_domain);
  890. out:
  891. return NULL;
  892. }
  893. static void omap_iommu_domain_free(struct iommu_domain *domain)
  894. {
  895. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  896. /*
  897. * An iommu device is still attached
  898. * (currently, only one device can be attached) ?
  899. */
  900. if (omap_domain->iommu_dev)
  901. _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
  902. kfree(omap_domain->pgtable);
  903. kfree(omap_domain);
  904. }
  905. static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
  906. dma_addr_t da)
  907. {
  908. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  909. struct omap_iommu *oiommu = omap_domain->iommu_dev;
  910. struct device *dev = oiommu->dev;
  911. u32 *pgd, *pte;
  912. phys_addr_t ret = 0;
  913. iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
  914. if (pte) {
  915. if (iopte_is_small(*pte))
  916. ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
  917. else if (iopte_is_large(*pte))
  918. ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
  919. else
  920. dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
  921. (unsigned long long)da);
  922. } else {
  923. if (iopgd_is_section(*pgd))
  924. ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
  925. else if (iopgd_is_super(*pgd))
  926. ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
  927. else
  928. dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
  929. (unsigned long long)da);
  930. }
  931. return ret;
  932. }
  933. static int omap_iommu_add_device(struct device *dev)
  934. {
  935. struct omap_iommu_arch_data *arch_data;
  936. struct device_node *np;
  937. struct platform_device *pdev;
  938. /*
  939. * Allocate the archdata iommu structure for DT-based devices.
  940. *
  941. * TODO: Simplify this when removing non-DT support completely from the
  942. * IOMMU users.
  943. */
  944. if (!dev->of_node)
  945. return 0;
  946. np = of_parse_phandle(dev->of_node, "iommus", 0);
  947. if (!np)
  948. return 0;
  949. pdev = of_find_device_by_node(np);
  950. if (WARN_ON(!pdev)) {
  951. of_node_put(np);
  952. return -EINVAL;
  953. }
  954. arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
  955. if (!arch_data) {
  956. of_node_put(np);
  957. return -ENOMEM;
  958. }
  959. arch_data->name = kstrdup(dev_name(&pdev->dev), GFP_KERNEL);
  960. dev->archdata.iommu = arch_data;
  961. of_node_put(np);
  962. return 0;
  963. }
  964. static void omap_iommu_remove_device(struct device *dev)
  965. {
  966. struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
  967. if (!dev->of_node || !arch_data)
  968. return;
  969. kfree(arch_data->name);
  970. kfree(arch_data);
  971. }
  972. static const struct iommu_ops omap_iommu_ops = {
  973. .domain_alloc = omap_iommu_domain_alloc,
  974. .domain_free = omap_iommu_domain_free,
  975. .attach_dev = omap_iommu_attach_dev,
  976. .detach_dev = omap_iommu_detach_dev,
  977. .map = omap_iommu_map,
  978. .unmap = omap_iommu_unmap,
  979. .map_sg = default_iommu_map_sg,
  980. .iova_to_phys = omap_iommu_iova_to_phys,
  981. .add_device = omap_iommu_add_device,
  982. .remove_device = omap_iommu_remove_device,
  983. .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
  984. };
  985. static int __init omap_iommu_init(void)
  986. {
  987. struct kmem_cache *p;
  988. const unsigned long flags = SLAB_HWCACHE_ALIGN;
  989. size_t align = 1 << 10; /* L2 pagetable alignement */
  990. struct device_node *np;
  991. np = of_find_matching_node(NULL, omap_iommu_of_match);
  992. if (!np)
  993. return 0;
  994. of_node_put(np);
  995. p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
  996. iopte_cachep_ctor);
  997. if (!p)
  998. return -ENOMEM;
  999. iopte_cachep = p;
  1000. bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
  1001. omap_iommu_debugfs_init();
  1002. return platform_driver_register(&omap_iommu_driver);
  1003. }
  1004. subsys_initcall(omap_iommu_init);
  1005. /* must be ready before omap3isp is probed */