omap-iommu.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  1. /*
  2. * omap iommu: tlb and pagetable primitives
  3. *
  4. * Copyright (C) 2008-2010 Nokia Corporation
  5. * Copyright (C) 2013-2017 Texas Instruments Incorporated - http://www.ti.com/
  6. *
  7. * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>,
  8. * Paul Mundt and Toshihiro Kobayashi
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/dma-mapping.h>
  15. #include <linux/err.h>
  16. #include <linux/slab.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/ioport.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/iommu.h>
  21. #include <linux/omap-iommu.h>
  22. #include <linux/mutex.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/io.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/of.h>
  27. #include <linux/of_iommu.h>
  28. #include <linux/of_irq.h>
  29. #include <linux/of_platform.h>
  30. #include <linux/regmap.h>
  31. #include <linux/mfd/syscon.h>
  32. #include <linux/platform_data/iommu-omap.h>
  33. #include "omap-iopgtable.h"
  34. #include "omap-iommu.h"
  35. static const struct iommu_ops omap_iommu_ops;
  36. #define to_iommu(dev) \
  37. ((struct omap_iommu *)platform_get_drvdata(to_platform_device(dev)))
  38. /* bitmap of the page sizes currently supported */
  39. #define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
  40. #define MMU_LOCK_BASE_SHIFT 10
  41. #define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
  42. #define MMU_LOCK_BASE(x) \
  43. ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
  44. #define MMU_LOCK_VICT_SHIFT 4
  45. #define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
  46. #define MMU_LOCK_VICT(x) \
  47. ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
  48. static struct platform_driver omap_iommu_driver;
  49. static struct kmem_cache *iopte_cachep;
  50. /**
  51. * to_omap_domain - Get struct omap_iommu_domain from generic iommu_domain
  52. * @dom: generic iommu domain handle
  53. **/
  54. static struct omap_iommu_domain *to_omap_domain(struct iommu_domain *dom)
  55. {
  56. return container_of(dom, struct omap_iommu_domain, domain);
  57. }
  58. /**
  59. * omap_iommu_save_ctx - Save registers for pm off-mode support
  60. * @dev: client device
  61. **/
  62. void omap_iommu_save_ctx(struct device *dev)
  63. {
  64. struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
  65. struct omap_iommu *obj;
  66. u32 *p;
  67. int i;
  68. if (!arch_data)
  69. return;
  70. while (arch_data->iommu_dev) {
  71. obj = arch_data->iommu_dev;
  72. p = obj->ctx;
  73. for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
  74. p[i] = iommu_read_reg(obj, i * sizeof(u32));
  75. dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i,
  76. p[i]);
  77. }
  78. arch_data++;
  79. }
  80. }
  81. EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
  82. /**
  83. * omap_iommu_restore_ctx - Restore registers for pm off-mode support
  84. * @dev: client device
  85. **/
  86. void omap_iommu_restore_ctx(struct device *dev)
  87. {
  88. struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
  89. struct omap_iommu *obj;
  90. u32 *p;
  91. int i;
  92. if (!arch_data)
  93. return;
  94. while (arch_data->iommu_dev) {
  95. obj = arch_data->iommu_dev;
  96. p = obj->ctx;
  97. for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
  98. iommu_write_reg(obj, p[i], i * sizeof(u32));
  99. dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i,
  100. p[i]);
  101. }
  102. arch_data++;
  103. }
  104. }
  105. EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
  106. static void dra7_cfg_dspsys_mmu(struct omap_iommu *obj, bool enable)
  107. {
  108. u32 val, mask;
  109. if (!obj->syscfg)
  110. return;
  111. mask = (1 << (obj->id * DSP_SYS_MMU_CONFIG_EN_SHIFT));
  112. val = enable ? mask : 0;
  113. regmap_update_bits(obj->syscfg, DSP_SYS_MMU_CONFIG, mask, val);
  114. }
  115. static void __iommu_set_twl(struct omap_iommu *obj, bool on)
  116. {
  117. u32 l = iommu_read_reg(obj, MMU_CNTL);
  118. if (on)
  119. iommu_write_reg(obj, MMU_IRQ_TWL_MASK, MMU_IRQENABLE);
  120. else
  121. iommu_write_reg(obj, MMU_IRQ_TLB_MISS_MASK, MMU_IRQENABLE);
  122. l &= ~MMU_CNTL_MASK;
  123. if (on)
  124. l |= (MMU_CNTL_MMU_EN | MMU_CNTL_TWL_EN);
  125. else
  126. l |= (MMU_CNTL_MMU_EN);
  127. iommu_write_reg(obj, l, MMU_CNTL);
  128. }
  129. static int omap2_iommu_enable(struct omap_iommu *obj)
  130. {
  131. u32 l, pa;
  132. if (!obj->iopgd || !IS_ALIGNED((u32)obj->iopgd, SZ_16K))
  133. return -EINVAL;
  134. pa = virt_to_phys(obj->iopgd);
  135. if (!IS_ALIGNED(pa, SZ_16K))
  136. return -EINVAL;
  137. l = iommu_read_reg(obj, MMU_REVISION);
  138. dev_info(obj->dev, "%s: version %d.%d\n", obj->name,
  139. (l >> 4) & 0xf, l & 0xf);
  140. iommu_write_reg(obj, pa, MMU_TTB);
  141. dra7_cfg_dspsys_mmu(obj, true);
  142. if (obj->has_bus_err_back)
  143. iommu_write_reg(obj, MMU_GP_REG_BUS_ERR_BACK_EN, MMU_GP_REG);
  144. __iommu_set_twl(obj, true);
  145. return 0;
  146. }
  147. static void omap2_iommu_disable(struct omap_iommu *obj)
  148. {
  149. u32 l = iommu_read_reg(obj, MMU_CNTL);
  150. l &= ~MMU_CNTL_MASK;
  151. iommu_write_reg(obj, l, MMU_CNTL);
  152. dra7_cfg_dspsys_mmu(obj, false);
  153. dev_dbg(obj->dev, "%s is shutting down\n", obj->name);
  154. }
  155. static int iommu_enable(struct omap_iommu *obj)
  156. {
  157. int err;
  158. struct platform_device *pdev = to_platform_device(obj->dev);
  159. struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
  160. if (pdata && pdata->deassert_reset) {
  161. err = pdata->deassert_reset(pdev, pdata->reset_name);
  162. if (err) {
  163. dev_err(obj->dev, "deassert_reset failed: %d\n", err);
  164. return err;
  165. }
  166. }
  167. pm_runtime_get_sync(obj->dev);
  168. err = omap2_iommu_enable(obj);
  169. return err;
  170. }
  171. static void iommu_disable(struct omap_iommu *obj)
  172. {
  173. struct platform_device *pdev = to_platform_device(obj->dev);
  174. struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
  175. omap2_iommu_disable(obj);
  176. pm_runtime_put_sync(obj->dev);
  177. if (pdata && pdata->assert_reset)
  178. pdata->assert_reset(pdev, pdata->reset_name);
  179. }
  180. /*
  181. * TLB operations
  182. */
  183. static u32 iotlb_cr_to_virt(struct cr_regs *cr)
  184. {
  185. u32 page_size = cr->cam & MMU_CAM_PGSZ_MASK;
  186. u32 mask = get_cam_va_mask(cr->cam & page_size);
  187. return cr->cam & mask;
  188. }
  189. static u32 get_iopte_attr(struct iotlb_entry *e)
  190. {
  191. u32 attr;
  192. attr = e->mixed << 5;
  193. attr |= e->endian;
  194. attr |= e->elsz >> 3;
  195. attr <<= (((e->pgsz == MMU_CAM_PGSZ_4K) ||
  196. (e->pgsz == MMU_CAM_PGSZ_64K)) ? 0 : 6);
  197. return attr;
  198. }
  199. static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
  200. {
  201. u32 status, fault_addr;
  202. status = iommu_read_reg(obj, MMU_IRQSTATUS);
  203. status &= MMU_IRQ_MASK;
  204. if (!status) {
  205. *da = 0;
  206. return 0;
  207. }
  208. fault_addr = iommu_read_reg(obj, MMU_FAULT_AD);
  209. *da = fault_addr;
  210. iommu_write_reg(obj, status, MMU_IRQSTATUS);
  211. return status;
  212. }
  213. void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
  214. {
  215. u32 val;
  216. val = iommu_read_reg(obj, MMU_LOCK);
  217. l->base = MMU_LOCK_BASE(val);
  218. l->vict = MMU_LOCK_VICT(val);
  219. }
  220. void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
  221. {
  222. u32 val;
  223. val = (l->base << MMU_LOCK_BASE_SHIFT);
  224. val |= (l->vict << MMU_LOCK_VICT_SHIFT);
  225. iommu_write_reg(obj, val, MMU_LOCK);
  226. }
  227. static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
  228. {
  229. cr->cam = iommu_read_reg(obj, MMU_READ_CAM);
  230. cr->ram = iommu_read_reg(obj, MMU_READ_RAM);
  231. }
  232. static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
  233. {
  234. iommu_write_reg(obj, cr->cam | MMU_CAM_V, MMU_CAM);
  235. iommu_write_reg(obj, cr->ram, MMU_RAM);
  236. iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
  237. iommu_write_reg(obj, 1, MMU_LD_TLB);
  238. }
  239. /* only used in iotlb iteration for-loop */
  240. struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
  241. {
  242. struct cr_regs cr;
  243. struct iotlb_lock l;
  244. iotlb_lock_get(obj, &l);
  245. l.vict = n;
  246. iotlb_lock_set(obj, &l);
  247. iotlb_read_cr(obj, &cr);
  248. return cr;
  249. }
  250. #ifdef PREFETCH_IOTLB
  251. static struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
  252. struct iotlb_entry *e)
  253. {
  254. struct cr_regs *cr;
  255. if (!e)
  256. return NULL;
  257. if (e->da & ~(get_cam_va_mask(e->pgsz))) {
  258. dev_err(obj->dev, "%s:\twrong alignment: %08x\n", __func__,
  259. e->da);
  260. return ERR_PTR(-EINVAL);
  261. }
  262. cr = kmalloc(sizeof(*cr), GFP_KERNEL);
  263. if (!cr)
  264. return ERR_PTR(-ENOMEM);
  265. cr->cam = (e->da & MMU_CAM_VATAG_MASK) | e->prsvd | e->pgsz | e->valid;
  266. cr->ram = e->pa | e->endian | e->elsz | e->mixed;
  267. return cr;
  268. }
  269. /**
  270. * load_iotlb_entry - Set an iommu tlb entry
  271. * @obj: target iommu
  272. * @e: an iommu tlb entry info
  273. **/
  274. static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
  275. {
  276. int err = 0;
  277. struct iotlb_lock l;
  278. struct cr_regs *cr;
  279. if (!obj || !obj->nr_tlb_entries || !e)
  280. return -EINVAL;
  281. pm_runtime_get_sync(obj->dev);
  282. iotlb_lock_get(obj, &l);
  283. if (l.base == obj->nr_tlb_entries) {
  284. dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
  285. err = -EBUSY;
  286. goto out;
  287. }
  288. if (!e->prsvd) {
  289. int i;
  290. struct cr_regs tmp;
  291. for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
  292. if (!iotlb_cr_valid(&tmp))
  293. break;
  294. if (i == obj->nr_tlb_entries) {
  295. dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
  296. err = -EBUSY;
  297. goto out;
  298. }
  299. iotlb_lock_get(obj, &l);
  300. } else {
  301. l.vict = l.base;
  302. iotlb_lock_set(obj, &l);
  303. }
  304. cr = iotlb_alloc_cr(obj, e);
  305. if (IS_ERR(cr)) {
  306. pm_runtime_put_sync(obj->dev);
  307. return PTR_ERR(cr);
  308. }
  309. iotlb_load_cr(obj, cr);
  310. kfree(cr);
  311. if (e->prsvd)
  312. l.base++;
  313. /* increment victim for next tlb load */
  314. if (++l.vict == obj->nr_tlb_entries)
  315. l.vict = l.base;
  316. iotlb_lock_set(obj, &l);
  317. out:
  318. pm_runtime_put_sync(obj->dev);
  319. return err;
  320. }
  321. #else /* !PREFETCH_IOTLB */
  322. static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
  323. {
  324. return 0;
  325. }
  326. #endif /* !PREFETCH_IOTLB */
  327. static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
  328. {
  329. return load_iotlb_entry(obj, e);
  330. }
  331. /**
  332. * flush_iotlb_page - Clear an iommu tlb entry
  333. * @obj: target iommu
  334. * @da: iommu device virtual address
  335. *
  336. * Clear an iommu tlb entry which includes 'da' address.
  337. **/
  338. static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
  339. {
  340. int i;
  341. struct cr_regs cr;
  342. pm_runtime_get_sync(obj->dev);
  343. for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
  344. u32 start;
  345. size_t bytes;
  346. if (!iotlb_cr_valid(&cr))
  347. continue;
  348. start = iotlb_cr_to_virt(&cr);
  349. bytes = iopgsz_to_bytes(cr.cam & 3);
  350. if ((start <= da) && (da < start + bytes)) {
  351. dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
  352. __func__, start, da, bytes);
  353. iotlb_load_cr(obj, &cr);
  354. iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
  355. break;
  356. }
  357. }
  358. pm_runtime_put_sync(obj->dev);
  359. if (i == obj->nr_tlb_entries)
  360. dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
  361. }
  362. /**
  363. * flush_iotlb_all - Clear all iommu tlb entries
  364. * @obj: target iommu
  365. **/
  366. static void flush_iotlb_all(struct omap_iommu *obj)
  367. {
  368. struct iotlb_lock l;
  369. pm_runtime_get_sync(obj->dev);
  370. l.base = 0;
  371. l.vict = 0;
  372. iotlb_lock_set(obj, &l);
  373. iommu_write_reg(obj, 1, MMU_GFLUSH);
  374. pm_runtime_put_sync(obj->dev);
  375. }
  376. /*
  377. * H/W pagetable operations
  378. */
  379. static void flush_iopte_range(struct device *dev, dma_addr_t dma,
  380. unsigned long offset, int num_entries)
  381. {
  382. size_t size = num_entries * sizeof(u32);
  383. dma_sync_single_range_for_device(dev, dma, offset, size, DMA_TO_DEVICE);
  384. }
  385. static void iopte_free(struct omap_iommu *obj, u32 *iopte, bool dma_valid)
  386. {
  387. dma_addr_t pt_dma;
  388. /* Note: freed iopte's must be clean ready for re-use */
  389. if (iopte) {
  390. if (dma_valid) {
  391. pt_dma = virt_to_phys(iopte);
  392. dma_unmap_single(obj->dev, pt_dma, IOPTE_TABLE_SIZE,
  393. DMA_TO_DEVICE);
  394. }
  395. kmem_cache_free(iopte_cachep, iopte);
  396. }
  397. }
  398. static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd,
  399. dma_addr_t *pt_dma, u32 da)
  400. {
  401. u32 *iopte;
  402. unsigned long offset = iopgd_index(da) * sizeof(da);
  403. /* a table has already existed */
  404. if (*iopgd)
  405. goto pte_ready;
  406. /*
  407. * do the allocation outside the page table lock
  408. */
  409. spin_unlock(&obj->page_table_lock);
  410. iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
  411. spin_lock(&obj->page_table_lock);
  412. if (!*iopgd) {
  413. if (!iopte)
  414. return ERR_PTR(-ENOMEM);
  415. *pt_dma = dma_map_single(obj->dev, iopte, IOPTE_TABLE_SIZE,
  416. DMA_TO_DEVICE);
  417. if (dma_mapping_error(obj->dev, *pt_dma)) {
  418. dev_err(obj->dev, "DMA map error for L2 table\n");
  419. iopte_free(obj, iopte, false);
  420. return ERR_PTR(-ENOMEM);
  421. }
  422. /*
  423. * we rely on dma address and the physical address to be
  424. * the same for mapping the L2 table
  425. */
  426. if (WARN_ON(*pt_dma != virt_to_phys(iopte))) {
  427. dev_err(obj->dev, "DMA translation error for L2 table\n");
  428. dma_unmap_single(obj->dev, *pt_dma, IOPTE_TABLE_SIZE,
  429. DMA_TO_DEVICE);
  430. iopte_free(obj, iopte, false);
  431. return ERR_PTR(-ENOMEM);
  432. }
  433. *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
  434. flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
  435. dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
  436. } else {
  437. /* We raced, free the reduniovant table */
  438. iopte_free(obj, iopte, false);
  439. }
  440. pte_ready:
  441. iopte = iopte_offset(iopgd, da);
  442. *pt_dma = iopgd_page_paddr(iopgd);
  443. dev_vdbg(obj->dev,
  444. "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
  445. __func__, da, iopgd, *iopgd, iopte, *iopte);
  446. return iopte;
  447. }
  448. static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  449. {
  450. u32 *iopgd = iopgd_offset(obj, da);
  451. unsigned long offset = iopgd_index(da) * sizeof(da);
  452. if ((da | pa) & ~IOSECTION_MASK) {
  453. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  454. __func__, da, pa, IOSECTION_SIZE);
  455. return -EINVAL;
  456. }
  457. *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
  458. flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
  459. return 0;
  460. }
  461. static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  462. {
  463. u32 *iopgd = iopgd_offset(obj, da);
  464. unsigned long offset = iopgd_index(da) * sizeof(da);
  465. int i;
  466. if ((da | pa) & ~IOSUPER_MASK) {
  467. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  468. __func__, da, pa, IOSUPER_SIZE);
  469. return -EINVAL;
  470. }
  471. for (i = 0; i < 16; i++)
  472. *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
  473. flush_iopte_range(obj->dev, obj->pd_dma, offset, 16);
  474. return 0;
  475. }
  476. static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  477. {
  478. u32 *iopgd = iopgd_offset(obj, da);
  479. dma_addr_t pt_dma;
  480. u32 *iopte = iopte_alloc(obj, iopgd, &pt_dma, da);
  481. unsigned long offset = iopte_index(da) * sizeof(da);
  482. if (IS_ERR(iopte))
  483. return PTR_ERR(iopte);
  484. *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
  485. flush_iopte_range(obj->dev, pt_dma, offset, 1);
  486. dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
  487. __func__, da, pa, iopte, *iopte);
  488. return 0;
  489. }
  490. static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  491. {
  492. u32 *iopgd = iopgd_offset(obj, da);
  493. dma_addr_t pt_dma;
  494. u32 *iopte = iopte_alloc(obj, iopgd, &pt_dma, da);
  495. unsigned long offset = iopte_index(da) * sizeof(da);
  496. int i;
  497. if ((da | pa) & ~IOLARGE_MASK) {
  498. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  499. __func__, da, pa, IOLARGE_SIZE);
  500. return -EINVAL;
  501. }
  502. if (IS_ERR(iopte))
  503. return PTR_ERR(iopte);
  504. for (i = 0; i < 16; i++)
  505. *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
  506. flush_iopte_range(obj->dev, pt_dma, offset, 16);
  507. return 0;
  508. }
  509. static int
  510. iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
  511. {
  512. int (*fn)(struct omap_iommu *, u32, u32, u32);
  513. u32 prot;
  514. int err;
  515. if (!obj || !e)
  516. return -EINVAL;
  517. switch (e->pgsz) {
  518. case MMU_CAM_PGSZ_16M:
  519. fn = iopgd_alloc_super;
  520. break;
  521. case MMU_CAM_PGSZ_1M:
  522. fn = iopgd_alloc_section;
  523. break;
  524. case MMU_CAM_PGSZ_64K:
  525. fn = iopte_alloc_large;
  526. break;
  527. case MMU_CAM_PGSZ_4K:
  528. fn = iopte_alloc_page;
  529. break;
  530. default:
  531. fn = NULL;
  532. break;
  533. }
  534. if (WARN_ON(!fn))
  535. return -EINVAL;
  536. prot = get_iopte_attr(e);
  537. spin_lock(&obj->page_table_lock);
  538. err = fn(obj, e->da, e->pa, prot);
  539. spin_unlock(&obj->page_table_lock);
  540. return err;
  541. }
  542. /**
  543. * omap_iopgtable_store_entry - Make an iommu pte entry
  544. * @obj: target iommu
  545. * @e: an iommu tlb entry info
  546. **/
  547. static int
  548. omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
  549. {
  550. int err;
  551. flush_iotlb_page(obj, e->da);
  552. err = iopgtable_store_entry_core(obj, e);
  553. if (!err)
  554. prefetch_iotlb_entry(obj, e);
  555. return err;
  556. }
  557. /**
  558. * iopgtable_lookup_entry - Lookup an iommu pte entry
  559. * @obj: target iommu
  560. * @da: iommu device virtual address
  561. * @ppgd: iommu pgd entry pointer to be returned
  562. * @ppte: iommu pte entry pointer to be returned
  563. **/
  564. static void
  565. iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
  566. {
  567. u32 *iopgd, *iopte = NULL;
  568. iopgd = iopgd_offset(obj, da);
  569. if (!*iopgd)
  570. goto out;
  571. if (iopgd_is_table(*iopgd))
  572. iopte = iopte_offset(iopgd, da);
  573. out:
  574. *ppgd = iopgd;
  575. *ppte = iopte;
  576. }
  577. static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
  578. {
  579. size_t bytes;
  580. u32 *iopgd = iopgd_offset(obj, da);
  581. int nent = 1;
  582. dma_addr_t pt_dma;
  583. unsigned long pd_offset = iopgd_index(da) * sizeof(da);
  584. unsigned long pt_offset = iopte_index(da) * sizeof(da);
  585. if (!*iopgd)
  586. return 0;
  587. if (iopgd_is_table(*iopgd)) {
  588. int i;
  589. u32 *iopte = iopte_offset(iopgd, da);
  590. bytes = IOPTE_SIZE;
  591. if (*iopte & IOPTE_LARGE) {
  592. nent *= 16;
  593. /* rewind to the 1st entry */
  594. iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
  595. }
  596. bytes *= nent;
  597. memset(iopte, 0, nent * sizeof(*iopte));
  598. pt_dma = iopgd_page_paddr(iopgd);
  599. flush_iopte_range(obj->dev, pt_dma, pt_offset, nent);
  600. /*
  601. * do table walk to check if this table is necessary or not
  602. */
  603. iopte = iopte_offset(iopgd, 0);
  604. for (i = 0; i < PTRS_PER_IOPTE; i++)
  605. if (iopte[i])
  606. goto out;
  607. iopte_free(obj, iopte, true);
  608. nent = 1; /* for the next L1 entry */
  609. } else {
  610. bytes = IOPGD_SIZE;
  611. if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
  612. nent *= 16;
  613. /* rewind to the 1st entry */
  614. iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
  615. }
  616. bytes *= nent;
  617. }
  618. memset(iopgd, 0, nent * sizeof(*iopgd));
  619. flush_iopte_range(obj->dev, obj->pd_dma, pd_offset, nent);
  620. out:
  621. return bytes;
  622. }
  623. /**
  624. * iopgtable_clear_entry - Remove an iommu pte entry
  625. * @obj: target iommu
  626. * @da: iommu device virtual address
  627. **/
  628. static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
  629. {
  630. size_t bytes;
  631. spin_lock(&obj->page_table_lock);
  632. bytes = iopgtable_clear_entry_core(obj, da);
  633. flush_iotlb_page(obj, da);
  634. spin_unlock(&obj->page_table_lock);
  635. return bytes;
  636. }
  637. static void iopgtable_clear_entry_all(struct omap_iommu *obj)
  638. {
  639. unsigned long offset;
  640. int i;
  641. spin_lock(&obj->page_table_lock);
  642. for (i = 0; i < PTRS_PER_IOPGD; i++) {
  643. u32 da;
  644. u32 *iopgd;
  645. da = i << IOPGD_SHIFT;
  646. iopgd = iopgd_offset(obj, da);
  647. offset = iopgd_index(da) * sizeof(da);
  648. if (!*iopgd)
  649. continue;
  650. if (iopgd_is_table(*iopgd))
  651. iopte_free(obj, iopte_offset(iopgd, 0), true);
  652. *iopgd = 0;
  653. flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
  654. }
  655. flush_iotlb_all(obj);
  656. spin_unlock(&obj->page_table_lock);
  657. }
  658. /*
  659. * Device IOMMU generic operations
  660. */
  661. static irqreturn_t iommu_fault_handler(int irq, void *data)
  662. {
  663. u32 da, errs;
  664. u32 *iopgd, *iopte;
  665. struct omap_iommu *obj = data;
  666. struct iommu_domain *domain = obj->domain;
  667. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  668. if (!omap_domain->dev)
  669. return IRQ_NONE;
  670. errs = iommu_report_fault(obj, &da);
  671. if (errs == 0)
  672. return IRQ_HANDLED;
  673. /* Fault callback or TLB/PTE Dynamic loading */
  674. if (!report_iommu_fault(domain, obj->dev, da, 0))
  675. return IRQ_HANDLED;
  676. iommu_write_reg(obj, 0, MMU_IRQENABLE);
  677. iopgd = iopgd_offset(obj, da);
  678. if (!iopgd_is_table(*iopgd)) {
  679. dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n",
  680. obj->name, errs, da, iopgd, *iopgd);
  681. return IRQ_NONE;
  682. }
  683. iopte = iopte_offset(iopgd, da);
  684. dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x pte:0x%p *pte:0x%08x\n",
  685. obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
  686. return IRQ_NONE;
  687. }
  688. /**
  689. * omap_iommu_attach() - attach iommu device to an iommu domain
  690. * @obj: target omap iommu device
  691. * @iopgd: page table
  692. **/
  693. static int omap_iommu_attach(struct omap_iommu *obj, u32 *iopgd)
  694. {
  695. int err;
  696. spin_lock(&obj->iommu_lock);
  697. obj->pd_dma = dma_map_single(obj->dev, iopgd, IOPGD_TABLE_SIZE,
  698. DMA_TO_DEVICE);
  699. if (dma_mapping_error(obj->dev, obj->pd_dma)) {
  700. dev_err(obj->dev, "DMA map error for L1 table\n");
  701. err = -ENOMEM;
  702. goto out_err;
  703. }
  704. obj->iopgd = iopgd;
  705. err = iommu_enable(obj);
  706. if (err)
  707. goto out_err;
  708. flush_iotlb_all(obj);
  709. spin_unlock(&obj->iommu_lock);
  710. dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
  711. return 0;
  712. out_err:
  713. spin_unlock(&obj->iommu_lock);
  714. return err;
  715. }
  716. /**
  717. * omap_iommu_detach - release iommu device
  718. * @obj: target iommu
  719. **/
  720. static void omap_iommu_detach(struct omap_iommu *obj)
  721. {
  722. if (!obj || IS_ERR(obj))
  723. return;
  724. spin_lock(&obj->iommu_lock);
  725. dma_unmap_single(obj->dev, obj->pd_dma, IOPGD_TABLE_SIZE,
  726. DMA_TO_DEVICE);
  727. iommu_disable(obj);
  728. obj->pd_dma = 0;
  729. obj->iopgd = NULL;
  730. spin_unlock(&obj->iommu_lock);
  731. dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
  732. }
  733. static bool omap_iommu_can_register(struct platform_device *pdev)
  734. {
  735. struct device_node *np = pdev->dev.of_node;
  736. if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu"))
  737. return true;
  738. /*
  739. * restrict IOMMU core registration only for processor-port MDMA MMUs
  740. * on DRA7 DSPs
  741. */
  742. if ((!strcmp(dev_name(&pdev->dev), "40d01000.mmu")) ||
  743. (!strcmp(dev_name(&pdev->dev), "41501000.mmu")))
  744. return true;
  745. return false;
  746. }
  747. static int omap_iommu_dra7_get_dsp_system_cfg(struct platform_device *pdev,
  748. struct omap_iommu *obj)
  749. {
  750. struct device_node *np = pdev->dev.of_node;
  751. int ret;
  752. if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu"))
  753. return 0;
  754. if (!of_property_read_bool(np, "ti,syscon-mmuconfig")) {
  755. dev_err(&pdev->dev, "ti,syscon-mmuconfig property is missing\n");
  756. return -EINVAL;
  757. }
  758. obj->syscfg =
  759. syscon_regmap_lookup_by_phandle(np, "ti,syscon-mmuconfig");
  760. if (IS_ERR(obj->syscfg)) {
  761. /* can fail with -EPROBE_DEFER */
  762. ret = PTR_ERR(obj->syscfg);
  763. return ret;
  764. }
  765. if (of_property_read_u32_index(np, "ti,syscon-mmuconfig", 1,
  766. &obj->id)) {
  767. dev_err(&pdev->dev, "couldn't get the IOMMU instance id within subsystem\n");
  768. return -EINVAL;
  769. }
  770. if (obj->id != 0 && obj->id != 1) {
  771. dev_err(&pdev->dev, "invalid IOMMU instance id\n");
  772. return -EINVAL;
  773. }
  774. return 0;
  775. }
  776. /*
  777. * OMAP Device MMU(IOMMU) detection
  778. */
  779. static int omap_iommu_probe(struct platform_device *pdev)
  780. {
  781. int err = -ENODEV;
  782. int irq;
  783. struct omap_iommu *obj;
  784. struct resource *res;
  785. struct device_node *of = pdev->dev.of_node;
  786. if (!of) {
  787. pr_err("%s: only DT-based devices are supported\n", __func__);
  788. return -ENODEV;
  789. }
  790. obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
  791. if (!obj)
  792. return -ENOMEM;
  793. obj->name = dev_name(&pdev->dev);
  794. obj->nr_tlb_entries = 32;
  795. err = of_property_read_u32(of, "ti,#tlb-entries", &obj->nr_tlb_entries);
  796. if (err && err != -EINVAL)
  797. return err;
  798. if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
  799. return -EINVAL;
  800. if (of_find_property(of, "ti,iommu-bus-err-back", NULL))
  801. obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
  802. obj->dev = &pdev->dev;
  803. obj->ctx = (void *)obj + sizeof(*obj);
  804. spin_lock_init(&obj->iommu_lock);
  805. spin_lock_init(&obj->page_table_lock);
  806. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  807. obj->regbase = devm_ioremap_resource(obj->dev, res);
  808. if (IS_ERR(obj->regbase))
  809. return PTR_ERR(obj->regbase);
  810. err = omap_iommu_dra7_get_dsp_system_cfg(pdev, obj);
  811. if (err)
  812. return err;
  813. irq = platform_get_irq(pdev, 0);
  814. if (irq < 0)
  815. return -ENODEV;
  816. err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
  817. dev_name(obj->dev), obj);
  818. if (err < 0)
  819. return err;
  820. platform_set_drvdata(pdev, obj);
  821. if (omap_iommu_can_register(pdev)) {
  822. obj->group = iommu_group_alloc();
  823. if (IS_ERR(obj->group))
  824. return PTR_ERR(obj->group);
  825. err = iommu_device_sysfs_add(&obj->iommu, obj->dev, NULL,
  826. obj->name);
  827. if (err)
  828. goto out_group;
  829. iommu_device_set_ops(&obj->iommu, &omap_iommu_ops);
  830. err = iommu_device_register(&obj->iommu);
  831. if (err)
  832. goto out_sysfs;
  833. }
  834. pm_runtime_irq_safe(obj->dev);
  835. pm_runtime_enable(obj->dev);
  836. omap_iommu_debugfs_add(obj);
  837. dev_info(&pdev->dev, "%s registered\n", obj->name);
  838. return 0;
  839. out_sysfs:
  840. iommu_device_sysfs_remove(&obj->iommu);
  841. out_group:
  842. iommu_group_put(obj->group);
  843. return err;
  844. }
  845. static int omap_iommu_remove(struct platform_device *pdev)
  846. {
  847. struct omap_iommu *obj = platform_get_drvdata(pdev);
  848. if (obj->group) {
  849. iommu_group_put(obj->group);
  850. obj->group = NULL;
  851. iommu_device_sysfs_remove(&obj->iommu);
  852. iommu_device_unregister(&obj->iommu);
  853. }
  854. omap_iommu_debugfs_remove(obj);
  855. pm_runtime_disable(obj->dev);
  856. dev_info(&pdev->dev, "%s removed\n", obj->name);
  857. return 0;
  858. }
  859. static const struct of_device_id omap_iommu_of_match[] = {
  860. { .compatible = "ti,omap2-iommu" },
  861. { .compatible = "ti,omap4-iommu" },
  862. { .compatible = "ti,dra7-iommu" },
  863. { .compatible = "ti,dra7-dsp-iommu" },
  864. {},
  865. };
  866. static struct platform_driver omap_iommu_driver = {
  867. .probe = omap_iommu_probe,
  868. .remove = omap_iommu_remove,
  869. .driver = {
  870. .name = "omap-iommu",
  871. .of_match_table = of_match_ptr(omap_iommu_of_match),
  872. },
  873. };
  874. static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz)
  875. {
  876. memset(e, 0, sizeof(*e));
  877. e->da = da;
  878. e->pa = pa;
  879. e->valid = MMU_CAM_V;
  880. e->pgsz = pgsz;
  881. e->endian = MMU_RAM_ENDIAN_LITTLE;
  882. e->elsz = MMU_RAM_ELSZ_8;
  883. e->mixed = 0;
  884. return iopgsz_to_bytes(e->pgsz);
  885. }
  886. static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
  887. phys_addr_t pa, size_t bytes, int prot)
  888. {
  889. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  890. struct device *dev = omap_domain->dev;
  891. struct omap_iommu_device *iommu;
  892. struct omap_iommu *oiommu;
  893. struct iotlb_entry e;
  894. int omap_pgsz;
  895. u32 ret = -EINVAL;
  896. int i;
  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. iommu = omap_domain->iommus;
  905. for (i = 0; i < omap_domain->num_iommus; i++, iommu++) {
  906. oiommu = iommu->iommu_dev;
  907. ret = omap_iopgtable_store_entry(oiommu, &e);
  908. if (ret) {
  909. dev_err(dev, "omap_iopgtable_store_entry failed: %d\n",
  910. ret);
  911. break;
  912. }
  913. }
  914. if (ret) {
  915. while (i--) {
  916. iommu--;
  917. oiommu = iommu->iommu_dev;
  918. iopgtable_clear_entry(oiommu, da);
  919. }
  920. }
  921. return ret;
  922. }
  923. static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
  924. size_t size)
  925. {
  926. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  927. struct device *dev = omap_domain->dev;
  928. struct omap_iommu_device *iommu;
  929. struct omap_iommu *oiommu;
  930. bool error = false;
  931. size_t bytes = 0;
  932. int i;
  933. dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
  934. iommu = omap_domain->iommus;
  935. for (i = 0; i < omap_domain->num_iommus; i++, iommu++) {
  936. oiommu = iommu->iommu_dev;
  937. bytes = iopgtable_clear_entry(oiommu, da);
  938. if (!bytes)
  939. error = true;
  940. }
  941. /*
  942. * simplify return - we are only checking if any of the iommus
  943. * reported an error, but not if all of them are unmapping the
  944. * same number of entries. This should not occur due to the
  945. * mirror programming.
  946. */
  947. return error ? 0 : bytes;
  948. }
  949. static int omap_iommu_count(struct device *dev)
  950. {
  951. struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
  952. int count = 0;
  953. while (arch_data->iommu_dev) {
  954. count++;
  955. arch_data++;
  956. }
  957. return count;
  958. }
  959. /* caller should call cleanup if this function fails */
  960. static int omap_iommu_attach_init(struct device *dev,
  961. struct omap_iommu_domain *odomain)
  962. {
  963. struct omap_iommu_device *iommu;
  964. int i;
  965. odomain->num_iommus = omap_iommu_count(dev);
  966. if (!odomain->num_iommus)
  967. return -EINVAL;
  968. odomain->iommus = kcalloc(odomain->num_iommus, sizeof(*iommu),
  969. GFP_ATOMIC);
  970. if (!odomain->iommus)
  971. return -ENOMEM;
  972. iommu = odomain->iommus;
  973. for (i = 0; i < odomain->num_iommus; i++, iommu++) {
  974. iommu->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_ATOMIC);
  975. if (!iommu->pgtable)
  976. return -ENOMEM;
  977. /*
  978. * should never fail, but please keep this around to ensure
  979. * we keep the hardware happy
  980. */
  981. if (WARN_ON(!IS_ALIGNED((long)iommu->pgtable,
  982. IOPGD_TABLE_SIZE)))
  983. return -EINVAL;
  984. }
  985. return 0;
  986. }
  987. static void omap_iommu_detach_fini(struct omap_iommu_domain *odomain)
  988. {
  989. int i;
  990. struct omap_iommu_device *iommu = odomain->iommus;
  991. for (i = 0; iommu && i < odomain->num_iommus; i++, iommu++)
  992. kfree(iommu->pgtable);
  993. kfree(odomain->iommus);
  994. odomain->num_iommus = 0;
  995. odomain->iommus = NULL;
  996. }
  997. static int
  998. omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
  999. {
  1000. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  1001. struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
  1002. struct omap_iommu_device *iommu;
  1003. struct omap_iommu *oiommu;
  1004. int ret = 0;
  1005. int i;
  1006. if (!arch_data || !arch_data->iommu_dev) {
  1007. dev_err(dev, "device doesn't have an associated iommu\n");
  1008. return -EINVAL;
  1009. }
  1010. spin_lock(&omap_domain->lock);
  1011. /* only a single client device can be attached to a domain */
  1012. if (omap_domain->dev) {
  1013. dev_err(dev, "iommu domain is already attached\n");
  1014. ret = -EBUSY;
  1015. goto out;
  1016. }
  1017. ret = omap_iommu_attach_init(dev, omap_domain);
  1018. if (ret) {
  1019. dev_err(dev, "failed to allocate required iommu data %d\n",
  1020. ret);
  1021. goto init_fail;
  1022. }
  1023. iommu = omap_domain->iommus;
  1024. for (i = 0; i < omap_domain->num_iommus; i++, iommu++, arch_data++) {
  1025. /* configure and enable the omap iommu */
  1026. oiommu = arch_data->iommu_dev;
  1027. ret = omap_iommu_attach(oiommu, iommu->pgtable);
  1028. if (ret) {
  1029. dev_err(dev, "can't get omap iommu: %d\n", ret);
  1030. goto attach_fail;
  1031. }
  1032. oiommu->domain = domain;
  1033. iommu->iommu_dev = oiommu;
  1034. }
  1035. omap_domain->dev = dev;
  1036. goto out;
  1037. attach_fail:
  1038. while (i--) {
  1039. iommu--;
  1040. arch_data--;
  1041. oiommu = iommu->iommu_dev;
  1042. omap_iommu_detach(oiommu);
  1043. iommu->iommu_dev = NULL;
  1044. oiommu->domain = NULL;
  1045. }
  1046. init_fail:
  1047. omap_iommu_detach_fini(omap_domain);
  1048. out:
  1049. spin_unlock(&omap_domain->lock);
  1050. return ret;
  1051. }
  1052. static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
  1053. struct device *dev)
  1054. {
  1055. struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
  1056. struct omap_iommu_device *iommu = omap_domain->iommus;
  1057. struct omap_iommu *oiommu;
  1058. int i;
  1059. if (!omap_domain->dev) {
  1060. dev_err(dev, "domain has no attached device\n");
  1061. return;
  1062. }
  1063. /* only a single device is supported per domain for now */
  1064. if (omap_domain->dev != dev) {
  1065. dev_err(dev, "invalid attached device\n");
  1066. return;
  1067. }
  1068. /*
  1069. * cleanup in the reverse order of attachment - this addresses
  1070. * any h/w dependencies between multiple instances, if any
  1071. */
  1072. iommu += (omap_domain->num_iommus - 1);
  1073. arch_data += (omap_domain->num_iommus - 1);
  1074. for (i = 0; i < omap_domain->num_iommus; i++, iommu--, arch_data--) {
  1075. oiommu = iommu->iommu_dev;
  1076. iopgtable_clear_entry_all(oiommu);
  1077. omap_iommu_detach(oiommu);
  1078. iommu->iommu_dev = NULL;
  1079. oiommu->domain = NULL;
  1080. }
  1081. omap_iommu_detach_fini(omap_domain);
  1082. omap_domain->dev = NULL;
  1083. }
  1084. static void omap_iommu_detach_dev(struct iommu_domain *domain,
  1085. struct device *dev)
  1086. {
  1087. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  1088. spin_lock(&omap_domain->lock);
  1089. _omap_iommu_detach_dev(omap_domain, dev);
  1090. spin_unlock(&omap_domain->lock);
  1091. }
  1092. static struct iommu_domain *omap_iommu_domain_alloc(unsigned type)
  1093. {
  1094. struct omap_iommu_domain *omap_domain;
  1095. if (type != IOMMU_DOMAIN_UNMANAGED)
  1096. return NULL;
  1097. omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
  1098. if (!omap_domain)
  1099. return NULL;
  1100. spin_lock_init(&omap_domain->lock);
  1101. omap_domain->domain.geometry.aperture_start = 0;
  1102. omap_domain->domain.geometry.aperture_end = (1ULL << 32) - 1;
  1103. omap_domain->domain.geometry.force_aperture = true;
  1104. return &omap_domain->domain;
  1105. }
  1106. static void omap_iommu_domain_free(struct iommu_domain *domain)
  1107. {
  1108. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  1109. /*
  1110. * An iommu device is still attached
  1111. * (currently, only one device can be attached) ?
  1112. */
  1113. if (omap_domain->dev)
  1114. _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
  1115. kfree(omap_domain);
  1116. }
  1117. static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
  1118. dma_addr_t da)
  1119. {
  1120. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  1121. struct omap_iommu_device *iommu = omap_domain->iommus;
  1122. struct omap_iommu *oiommu = iommu->iommu_dev;
  1123. struct device *dev = oiommu->dev;
  1124. u32 *pgd, *pte;
  1125. phys_addr_t ret = 0;
  1126. /*
  1127. * all the iommus within the domain will have identical programming,
  1128. * so perform the lookup using just the first iommu
  1129. */
  1130. iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
  1131. if (pte) {
  1132. if (iopte_is_small(*pte))
  1133. ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
  1134. else if (iopte_is_large(*pte))
  1135. ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
  1136. else
  1137. dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
  1138. (unsigned long long)da);
  1139. } else {
  1140. if (iopgd_is_section(*pgd))
  1141. ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
  1142. else if (iopgd_is_super(*pgd))
  1143. ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
  1144. else
  1145. dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
  1146. (unsigned long long)da);
  1147. }
  1148. return ret;
  1149. }
  1150. static int omap_iommu_add_device(struct device *dev)
  1151. {
  1152. struct omap_iommu_arch_data *arch_data, *tmp;
  1153. struct omap_iommu *oiommu;
  1154. struct iommu_group *group;
  1155. struct device_node *np;
  1156. struct platform_device *pdev;
  1157. int num_iommus, i;
  1158. int ret;
  1159. /*
  1160. * Allocate the archdata iommu structure for DT-based devices.
  1161. *
  1162. * TODO: Simplify this when removing non-DT support completely from the
  1163. * IOMMU users.
  1164. */
  1165. if (!dev->of_node)
  1166. return 0;
  1167. /*
  1168. * retrieve the count of IOMMU nodes using phandle size as element size
  1169. * since #iommu-cells = 0 for OMAP
  1170. */
  1171. num_iommus = of_property_count_elems_of_size(dev->of_node, "iommus",
  1172. sizeof(phandle));
  1173. if (num_iommus < 0)
  1174. return 0;
  1175. arch_data = kcalloc(num_iommus + 1, sizeof(*arch_data), GFP_KERNEL);
  1176. if (!arch_data)
  1177. return -ENOMEM;
  1178. for (i = 0, tmp = arch_data; i < num_iommus; i++, tmp++) {
  1179. np = of_parse_phandle(dev->of_node, "iommus", i);
  1180. if (!np) {
  1181. kfree(arch_data);
  1182. return -EINVAL;
  1183. }
  1184. pdev = of_find_device_by_node(np);
  1185. if (WARN_ON(!pdev)) {
  1186. of_node_put(np);
  1187. kfree(arch_data);
  1188. return -EINVAL;
  1189. }
  1190. oiommu = platform_get_drvdata(pdev);
  1191. if (!oiommu) {
  1192. of_node_put(np);
  1193. kfree(arch_data);
  1194. return -EINVAL;
  1195. }
  1196. tmp->iommu_dev = oiommu;
  1197. of_node_put(np);
  1198. }
  1199. /*
  1200. * use the first IOMMU alone for the sysfs device linking.
  1201. * TODO: Evaluate if a single iommu_group needs to be
  1202. * maintained for both IOMMUs
  1203. */
  1204. oiommu = arch_data->iommu_dev;
  1205. ret = iommu_device_link(&oiommu->iommu, dev);
  1206. if (ret) {
  1207. kfree(arch_data);
  1208. return ret;
  1209. }
  1210. dev->archdata.iommu = arch_data;
  1211. /*
  1212. * IOMMU group initialization calls into omap_iommu_device_group, which
  1213. * needs a valid dev->archdata.iommu pointer
  1214. */
  1215. group = iommu_group_get_for_dev(dev);
  1216. if (IS_ERR(group)) {
  1217. iommu_device_unlink(&oiommu->iommu, dev);
  1218. dev->archdata.iommu = NULL;
  1219. kfree(arch_data);
  1220. return PTR_ERR(group);
  1221. }
  1222. iommu_group_put(group);
  1223. return 0;
  1224. }
  1225. static void omap_iommu_remove_device(struct device *dev)
  1226. {
  1227. struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
  1228. if (!dev->of_node || !arch_data)
  1229. return;
  1230. iommu_device_unlink(&arch_data->iommu_dev->iommu, dev);
  1231. iommu_group_remove_device(dev);
  1232. dev->archdata.iommu = NULL;
  1233. kfree(arch_data);
  1234. }
  1235. static struct iommu_group *omap_iommu_device_group(struct device *dev)
  1236. {
  1237. struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
  1238. struct iommu_group *group = ERR_PTR(-EINVAL);
  1239. if (arch_data->iommu_dev)
  1240. group = iommu_group_ref_get(arch_data->iommu_dev->group);
  1241. return group;
  1242. }
  1243. static const struct iommu_ops omap_iommu_ops = {
  1244. .domain_alloc = omap_iommu_domain_alloc,
  1245. .domain_free = omap_iommu_domain_free,
  1246. .attach_dev = omap_iommu_attach_dev,
  1247. .detach_dev = omap_iommu_detach_dev,
  1248. .map = omap_iommu_map,
  1249. .unmap = omap_iommu_unmap,
  1250. .iova_to_phys = omap_iommu_iova_to_phys,
  1251. .add_device = omap_iommu_add_device,
  1252. .remove_device = omap_iommu_remove_device,
  1253. .device_group = omap_iommu_device_group,
  1254. .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
  1255. };
  1256. static int __init omap_iommu_init(void)
  1257. {
  1258. struct kmem_cache *p;
  1259. const unsigned long flags = SLAB_HWCACHE_ALIGN;
  1260. size_t align = 1 << 10; /* L2 pagetable alignement */
  1261. struct device_node *np;
  1262. int ret;
  1263. np = of_find_matching_node(NULL, omap_iommu_of_match);
  1264. if (!np)
  1265. return 0;
  1266. of_node_put(np);
  1267. p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
  1268. NULL);
  1269. if (!p)
  1270. return -ENOMEM;
  1271. iopte_cachep = p;
  1272. omap_iommu_debugfs_init();
  1273. ret = platform_driver_register(&omap_iommu_driver);
  1274. if (ret) {
  1275. pr_err("%s: failed to register driver\n", __func__);
  1276. goto fail_driver;
  1277. }
  1278. ret = bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
  1279. if (ret)
  1280. goto fail_bus;
  1281. return 0;
  1282. fail_bus:
  1283. platform_driver_unregister(&omap_iommu_driver);
  1284. fail_driver:
  1285. kmem_cache_destroy(iopte_cachep);
  1286. return ret;
  1287. }
  1288. subsys_initcall(omap_iommu_init);
  1289. /* must be ready before omap3isp is probed */