omap-iommu.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * omap iommu: tlb and pagetable primitives
  4. *
  5. * Copyright (C) 2008-2010 Nokia Corporation
  6. * Copyright (C) 2013-2017 Texas Instruments Incorporated - https://www.ti.com/
  7. *
  8. * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>,
  9. * Paul Mundt and Toshihiro Kobayashi
  10. */
  11. #include <linux/dma-mapping.h>
  12. #include <linux/err.h>
  13. #include <linux/slab.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/ioport.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/iommu.h>
  18. #include <linux/omap-iommu.h>
  19. #include <linux/mutex.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/io.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/of.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/of_platform.h>
  26. #include <linux/regmap.h>
  27. #include <linux/mfd/syscon.h>
  28. #include <linux/platform_data/iommu-omap.h>
  29. #include "omap-iopgtable.h"
  30. #include "omap-iommu.h"
  31. static const struct iommu_ops omap_iommu_ops;
  32. #define to_iommu(dev) ((struct omap_iommu *)dev_get_drvdata(dev))
  33. /* bitmap of the page sizes currently supported */
  34. #define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
  35. #define MMU_LOCK_BASE_SHIFT 10
  36. #define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
  37. #define MMU_LOCK_BASE(x) \
  38. ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
  39. #define MMU_LOCK_VICT_SHIFT 4
  40. #define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
  41. #define MMU_LOCK_VICT(x) \
  42. ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
  43. static struct platform_driver omap_iommu_driver;
  44. static struct kmem_cache *iopte_cachep;
  45. /**
  46. * to_omap_domain - Get struct omap_iommu_domain from generic iommu_domain
  47. * @dom: generic iommu domain handle
  48. **/
  49. static struct omap_iommu_domain *to_omap_domain(struct iommu_domain *dom)
  50. {
  51. return container_of(dom, struct omap_iommu_domain, domain);
  52. }
  53. /**
  54. * omap_iommu_save_ctx - Save registers for pm off-mode support
  55. * @dev: client device
  56. *
  57. * This should be treated as an deprecated API. It is preserved only
  58. * to maintain existing functionality for OMAP3 ISP driver.
  59. **/
  60. void omap_iommu_save_ctx(struct device *dev)
  61. {
  62. struct omap_iommu_arch_data *arch_data = dev_iommu_priv_get(dev);
  63. struct omap_iommu *obj;
  64. u32 *p;
  65. int i;
  66. if (!arch_data)
  67. return;
  68. while (arch_data->iommu_dev) {
  69. obj = arch_data->iommu_dev;
  70. p = obj->ctx;
  71. for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
  72. p[i] = iommu_read_reg(obj, i * sizeof(u32));
  73. dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i,
  74. p[i]);
  75. }
  76. arch_data++;
  77. }
  78. }
  79. EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
  80. /**
  81. * omap_iommu_restore_ctx - Restore registers for pm off-mode support
  82. * @dev: client device
  83. *
  84. * This should be treated as an deprecated API. It is preserved only
  85. * to maintain existing functionality for OMAP3 ISP driver.
  86. **/
  87. void omap_iommu_restore_ctx(struct device *dev)
  88. {
  89. struct omap_iommu_arch_data *arch_data = dev_iommu_priv_get(dev);
  90. struct omap_iommu *obj;
  91. u32 *p;
  92. int i;
  93. if (!arch_data)
  94. return;
  95. while (arch_data->iommu_dev) {
  96. obj = arch_data->iommu_dev;
  97. p = obj->ctx;
  98. for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
  99. iommu_write_reg(obj, p[i], i * sizeof(u32));
  100. dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i,
  101. p[i]);
  102. }
  103. arch_data++;
  104. }
  105. }
  106. EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
  107. static void dra7_cfg_dspsys_mmu(struct omap_iommu *obj, bool enable)
  108. {
  109. u32 val, mask;
  110. if (!obj->syscfg)
  111. return;
  112. mask = (1 << (obj->id * DSP_SYS_MMU_CONFIG_EN_SHIFT));
  113. val = enable ? mask : 0;
  114. regmap_update_bits(obj->syscfg, DSP_SYS_MMU_CONFIG, mask, val);
  115. }
  116. static void __iommu_set_twl(struct omap_iommu *obj, bool on)
  117. {
  118. u32 l = iommu_read_reg(obj, MMU_CNTL);
  119. if (on)
  120. iommu_write_reg(obj, MMU_IRQ_TWL_MASK, MMU_IRQENABLE);
  121. else
  122. iommu_write_reg(obj, MMU_IRQ_TLB_MISS_MASK, MMU_IRQENABLE);
  123. l &= ~MMU_CNTL_MASK;
  124. if (on)
  125. l |= (MMU_CNTL_MMU_EN | MMU_CNTL_TWL_EN);
  126. else
  127. l |= (MMU_CNTL_MMU_EN);
  128. iommu_write_reg(obj, l, MMU_CNTL);
  129. }
  130. static int omap2_iommu_enable(struct omap_iommu *obj)
  131. {
  132. u32 l, pa;
  133. if (!obj->iopgd || !IS_ALIGNED((unsigned long)obj->iopgd, SZ_16K))
  134. return -EINVAL;
  135. pa = virt_to_phys(obj->iopgd);
  136. if (!IS_ALIGNED(pa, SZ_16K))
  137. return -EINVAL;
  138. l = iommu_read_reg(obj, MMU_REVISION);
  139. dev_info(obj->dev, "%s: version %d.%d\n", obj->name,
  140. (l >> 4) & 0xf, l & 0xf);
  141. iommu_write_reg(obj, pa, MMU_TTB);
  142. dra7_cfg_dspsys_mmu(obj, true);
  143. if (obj->has_bus_err_back)
  144. iommu_write_reg(obj, MMU_GP_REG_BUS_ERR_BACK_EN, MMU_GP_REG);
  145. __iommu_set_twl(obj, true);
  146. return 0;
  147. }
  148. static void omap2_iommu_disable(struct omap_iommu *obj)
  149. {
  150. u32 l = iommu_read_reg(obj, MMU_CNTL);
  151. l &= ~MMU_CNTL_MASK;
  152. iommu_write_reg(obj, l, MMU_CNTL);
  153. dra7_cfg_dspsys_mmu(obj, false);
  154. dev_dbg(obj->dev, "%s is shutting down\n", obj->name);
  155. }
  156. static int iommu_enable(struct omap_iommu *obj)
  157. {
  158. int ret;
  159. ret = pm_runtime_get_sync(obj->dev);
  160. if (ret < 0)
  161. pm_runtime_put_noidle(obj->dev);
  162. return ret < 0 ? ret : 0;
  163. }
  164. static void iommu_disable(struct omap_iommu *obj)
  165. {
  166. pm_runtime_put_sync(obj->dev);
  167. }
  168. /*
  169. * TLB operations
  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. 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. 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. 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_obj(*cr);
  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(%zx)\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. /*
  365. * H/W pagetable operations
  366. */
  367. static void flush_iopte_range(struct device *dev, dma_addr_t dma,
  368. unsigned long offset, int num_entries)
  369. {
  370. size_t size = num_entries * sizeof(u32);
  371. dma_sync_single_range_for_device(dev, dma, offset, size, DMA_TO_DEVICE);
  372. }
  373. static void iopte_free(struct omap_iommu *obj, u32 *iopte, bool dma_valid)
  374. {
  375. dma_addr_t pt_dma;
  376. /* Note: freed iopte's must be clean ready for re-use */
  377. if (iopte) {
  378. if (dma_valid) {
  379. pt_dma = virt_to_phys(iopte);
  380. dma_unmap_single(obj->dev, pt_dma, IOPTE_TABLE_SIZE,
  381. DMA_TO_DEVICE);
  382. }
  383. kmem_cache_free(iopte_cachep, iopte);
  384. }
  385. }
  386. static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd,
  387. dma_addr_t *pt_dma, u32 da)
  388. {
  389. u32 *iopte;
  390. unsigned long offset = iopgd_index(da) * sizeof(da);
  391. /* a table has already existed */
  392. if (*iopgd)
  393. goto pte_ready;
  394. /*
  395. * do the allocation outside the page table lock
  396. */
  397. spin_unlock(&obj->page_table_lock);
  398. iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
  399. spin_lock(&obj->page_table_lock);
  400. if (!*iopgd) {
  401. if (!iopte)
  402. return ERR_PTR(-ENOMEM);
  403. *pt_dma = dma_map_single(obj->dev, iopte, IOPTE_TABLE_SIZE,
  404. DMA_TO_DEVICE);
  405. if (dma_mapping_error(obj->dev, *pt_dma)) {
  406. dev_err(obj->dev, "DMA map error for L2 table\n");
  407. iopte_free(obj, iopte, false);
  408. return ERR_PTR(-ENOMEM);
  409. }
  410. /*
  411. * we rely on dma address and the physical address to be
  412. * the same for mapping the L2 table
  413. */
  414. if (WARN_ON(*pt_dma != virt_to_phys(iopte))) {
  415. dev_err(obj->dev, "DMA translation error for L2 table\n");
  416. dma_unmap_single(obj->dev, *pt_dma, IOPTE_TABLE_SIZE,
  417. DMA_TO_DEVICE);
  418. iopte_free(obj, iopte, false);
  419. return ERR_PTR(-ENOMEM);
  420. }
  421. *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
  422. flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
  423. dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
  424. } else {
  425. /* We raced, free the reduniovant table */
  426. iopte_free(obj, iopte, false);
  427. }
  428. pte_ready:
  429. iopte = iopte_offset(iopgd, da);
  430. *pt_dma = iopgd_page_paddr(iopgd);
  431. dev_vdbg(obj->dev,
  432. "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
  433. __func__, da, iopgd, *iopgd, iopte, *iopte);
  434. return iopte;
  435. }
  436. static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  437. {
  438. u32 *iopgd = iopgd_offset(obj, da);
  439. unsigned long offset = iopgd_index(da) * sizeof(da);
  440. if ((da | pa) & ~IOSECTION_MASK) {
  441. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  442. __func__, da, pa, IOSECTION_SIZE);
  443. return -EINVAL;
  444. }
  445. *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
  446. flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
  447. return 0;
  448. }
  449. static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  450. {
  451. u32 *iopgd = iopgd_offset(obj, da);
  452. unsigned long offset = iopgd_index(da) * sizeof(da);
  453. int i;
  454. if ((da | pa) & ~IOSUPER_MASK) {
  455. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  456. __func__, da, pa, IOSUPER_SIZE);
  457. return -EINVAL;
  458. }
  459. for (i = 0; i < 16; i++)
  460. *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
  461. flush_iopte_range(obj->dev, obj->pd_dma, offset, 16);
  462. return 0;
  463. }
  464. static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  465. {
  466. u32 *iopgd = iopgd_offset(obj, da);
  467. dma_addr_t pt_dma;
  468. u32 *iopte = iopte_alloc(obj, iopgd, &pt_dma, da);
  469. unsigned long offset = iopte_index(da) * sizeof(da);
  470. if (IS_ERR(iopte))
  471. return PTR_ERR(iopte);
  472. *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
  473. flush_iopte_range(obj->dev, pt_dma, offset, 1);
  474. dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
  475. __func__, da, pa, iopte, *iopte);
  476. return 0;
  477. }
  478. static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  479. {
  480. u32 *iopgd = iopgd_offset(obj, da);
  481. dma_addr_t pt_dma;
  482. u32 *iopte = iopte_alloc(obj, iopgd, &pt_dma, da);
  483. unsigned long offset = iopte_index(da) * sizeof(da);
  484. int i;
  485. if ((da | pa) & ~IOLARGE_MASK) {
  486. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  487. __func__, da, pa, IOLARGE_SIZE);
  488. return -EINVAL;
  489. }
  490. if (IS_ERR(iopte))
  491. return PTR_ERR(iopte);
  492. for (i = 0; i < 16; i++)
  493. *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
  494. flush_iopte_range(obj->dev, pt_dma, offset, 16);
  495. return 0;
  496. }
  497. static int
  498. iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
  499. {
  500. int (*fn)(struct omap_iommu *, u32, u32, u32);
  501. u32 prot;
  502. int err;
  503. if (!obj || !e)
  504. return -EINVAL;
  505. switch (e->pgsz) {
  506. case MMU_CAM_PGSZ_16M:
  507. fn = iopgd_alloc_super;
  508. break;
  509. case MMU_CAM_PGSZ_1M:
  510. fn = iopgd_alloc_section;
  511. break;
  512. case MMU_CAM_PGSZ_64K:
  513. fn = iopte_alloc_large;
  514. break;
  515. case MMU_CAM_PGSZ_4K:
  516. fn = iopte_alloc_page;
  517. break;
  518. default:
  519. fn = NULL;
  520. break;
  521. }
  522. if (WARN_ON(!fn))
  523. return -EINVAL;
  524. prot = get_iopte_attr(e);
  525. spin_lock(&obj->page_table_lock);
  526. err = fn(obj, e->da, e->pa, prot);
  527. spin_unlock(&obj->page_table_lock);
  528. return err;
  529. }
  530. /**
  531. * omap_iopgtable_store_entry - Make an iommu pte entry
  532. * @obj: target iommu
  533. * @e: an iommu tlb entry info
  534. **/
  535. static int
  536. omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
  537. {
  538. int err;
  539. flush_iotlb_page(obj, e->da);
  540. err = iopgtable_store_entry_core(obj, e);
  541. if (!err)
  542. prefetch_iotlb_entry(obj, e);
  543. return err;
  544. }
  545. /**
  546. * iopgtable_lookup_entry - Lookup an iommu pte entry
  547. * @obj: target iommu
  548. * @da: iommu device virtual address
  549. * @ppgd: iommu pgd entry pointer to be returned
  550. * @ppte: iommu pte entry pointer to be returned
  551. **/
  552. static void
  553. iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
  554. {
  555. u32 *iopgd, *iopte = NULL;
  556. iopgd = iopgd_offset(obj, da);
  557. if (!*iopgd)
  558. goto out;
  559. if (iopgd_is_table(*iopgd))
  560. iopte = iopte_offset(iopgd, da);
  561. out:
  562. *ppgd = iopgd;
  563. *ppte = iopte;
  564. }
  565. static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
  566. {
  567. size_t bytes;
  568. u32 *iopgd = iopgd_offset(obj, da);
  569. int nent = 1;
  570. dma_addr_t pt_dma;
  571. unsigned long pd_offset = iopgd_index(da) * sizeof(da);
  572. unsigned long pt_offset = iopte_index(da) * sizeof(da);
  573. if (!*iopgd)
  574. return 0;
  575. if (iopgd_is_table(*iopgd)) {
  576. int i;
  577. u32 *iopte = iopte_offset(iopgd, da);
  578. bytes = IOPTE_SIZE;
  579. if (*iopte & IOPTE_LARGE) {
  580. nent *= 16;
  581. /* rewind to the 1st entry */
  582. iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
  583. }
  584. bytes *= nent;
  585. memset(iopte, 0, nent * sizeof(*iopte));
  586. pt_dma = iopgd_page_paddr(iopgd);
  587. flush_iopte_range(obj->dev, pt_dma, pt_offset, nent);
  588. /*
  589. * do table walk to check if this table is necessary or not
  590. */
  591. iopte = iopte_offset(iopgd, 0);
  592. for (i = 0; i < PTRS_PER_IOPTE; i++)
  593. if (iopte[i])
  594. goto out;
  595. iopte_free(obj, iopte, true);
  596. nent = 1; /* for the next L1 entry */
  597. } else {
  598. bytes = IOPGD_SIZE;
  599. if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
  600. nent *= 16;
  601. /* rewind to the 1st entry */
  602. iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
  603. }
  604. bytes *= nent;
  605. }
  606. memset(iopgd, 0, nent * sizeof(*iopgd));
  607. flush_iopte_range(obj->dev, obj->pd_dma, pd_offset, nent);
  608. out:
  609. return bytes;
  610. }
  611. /**
  612. * iopgtable_clear_entry - Remove an iommu pte entry
  613. * @obj: target iommu
  614. * @da: iommu device virtual address
  615. **/
  616. static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
  617. {
  618. size_t bytes;
  619. spin_lock(&obj->page_table_lock);
  620. bytes = iopgtable_clear_entry_core(obj, da);
  621. flush_iotlb_page(obj, da);
  622. spin_unlock(&obj->page_table_lock);
  623. return bytes;
  624. }
  625. static void iopgtable_clear_entry_all(struct omap_iommu *obj)
  626. {
  627. unsigned long offset;
  628. int i;
  629. spin_lock(&obj->page_table_lock);
  630. for (i = 0; i < PTRS_PER_IOPGD; i++) {
  631. u32 da;
  632. u32 *iopgd;
  633. da = i << IOPGD_SHIFT;
  634. iopgd = iopgd_offset(obj, da);
  635. offset = iopgd_index(da) * sizeof(da);
  636. if (!*iopgd)
  637. continue;
  638. if (iopgd_is_table(*iopgd))
  639. iopte_free(obj, iopte_offset(iopgd, 0), true);
  640. *iopgd = 0;
  641. flush_iopte_range(obj->dev, obj->pd_dma, offset, 1);
  642. }
  643. flush_iotlb_all(obj);
  644. spin_unlock(&obj->page_table_lock);
  645. }
  646. /*
  647. * Device IOMMU generic operations
  648. */
  649. static irqreturn_t iommu_fault_handler(int irq, void *data)
  650. {
  651. u32 da, errs;
  652. u32 *iopgd, *iopte;
  653. struct omap_iommu *obj = data;
  654. struct iommu_domain *domain = obj->domain;
  655. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  656. if (!omap_domain->dev)
  657. return IRQ_NONE;
  658. errs = iommu_report_fault(obj, &da);
  659. if (errs == 0)
  660. return IRQ_HANDLED;
  661. /* Fault callback or TLB/PTE Dynamic loading */
  662. if (!report_iommu_fault(domain, obj->dev, da, 0))
  663. return IRQ_HANDLED;
  664. iommu_write_reg(obj, 0, MMU_IRQENABLE);
  665. iopgd = iopgd_offset(obj, da);
  666. if (!iopgd_is_table(*iopgd)) {
  667. dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n",
  668. obj->name, errs, da, iopgd, *iopgd);
  669. return IRQ_NONE;
  670. }
  671. iopte = iopte_offset(iopgd, da);
  672. dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x pte:0x%p *pte:0x%08x\n",
  673. obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
  674. return IRQ_NONE;
  675. }
  676. /**
  677. * omap_iommu_attach() - attach iommu device to an iommu domain
  678. * @obj: target omap iommu device
  679. * @iopgd: page table
  680. **/
  681. static int omap_iommu_attach(struct omap_iommu *obj, u32 *iopgd)
  682. {
  683. int err;
  684. spin_lock(&obj->iommu_lock);
  685. obj->pd_dma = dma_map_single(obj->dev, iopgd, IOPGD_TABLE_SIZE,
  686. DMA_TO_DEVICE);
  687. if (dma_mapping_error(obj->dev, obj->pd_dma)) {
  688. dev_err(obj->dev, "DMA map error for L1 table\n");
  689. err = -ENOMEM;
  690. goto out_err;
  691. }
  692. obj->iopgd = iopgd;
  693. err = iommu_enable(obj);
  694. if (err)
  695. goto out_err;
  696. flush_iotlb_all(obj);
  697. spin_unlock(&obj->iommu_lock);
  698. dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
  699. return 0;
  700. out_err:
  701. spin_unlock(&obj->iommu_lock);
  702. return err;
  703. }
  704. /**
  705. * omap_iommu_detach - release iommu device
  706. * @obj: target iommu
  707. **/
  708. static void omap_iommu_detach(struct omap_iommu *obj)
  709. {
  710. if (!obj || IS_ERR(obj))
  711. return;
  712. spin_lock(&obj->iommu_lock);
  713. dma_unmap_single(obj->dev, obj->pd_dma, IOPGD_TABLE_SIZE,
  714. DMA_TO_DEVICE);
  715. obj->pd_dma = 0;
  716. obj->iopgd = NULL;
  717. iommu_disable(obj);
  718. spin_unlock(&obj->iommu_lock);
  719. dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
  720. }
  721. static void omap_iommu_save_tlb_entries(struct omap_iommu *obj)
  722. {
  723. struct iotlb_lock lock;
  724. struct cr_regs cr;
  725. struct cr_regs *tmp;
  726. int i;
  727. /* check if there are any locked tlbs to save */
  728. iotlb_lock_get(obj, &lock);
  729. obj->num_cr_ctx = lock.base;
  730. if (!obj->num_cr_ctx)
  731. return;
  732. tmp = obj->cr_ctx;
  733. for_each_iotlb_cr(obj, obj->num_cr_ctx, i, cr)
  734. * tmp++ = cr;
  735. }
  736. static void omap_iommu_restore_tlb_entries(struct omap_iommu *obj)
  737. {
  738. struct iotlb_lock l;
  739. struct cr_regs *tmp;
  740. int i;
  741. /* no locked tlbs to restore */
  742. if (!obj->num_cr_ctx)
  743. return;
  744. l.base = 0;
  745. tmp = obj->cr_ctx;
  746. for (i = 0; i < obj->num_cr_ctx; i++, tmp++) {
  747. l.vict = i;
  748. iotlb_lock_set(obj, &l);
  749. iotlb_load_cr(obj, tmp);
  750. }
  751. l.base = obj->num_cr_ctx;
  752. l.vict = i;
  753. iotlb_lock_set(obj, &l);
  754. }
  755. /**
  756. * omap_iommu_domain_deactivate - deactivate attached iommu devices
  757. * @domain: iommu domain attached to the target iommu device
  758. *
  759. * This API allows the client devices of IOMMU devices to suspend
  760. * the IOMMUs they control at runtime, after they are idled and
  761. * suspended all activity. System Suspend will leverage the PM
  762. * driver late callbacks.
  763. **/
  764. int omap_iommu_domain_deactivate(struct iommu_domain *domain)
  765. {
  766. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  767. struct omap_iommu_device *iommu;
  768. struct omap_iommu *oiommu;
  769. int i;
  770. if (!omap_domain->dev)
  771. return 0;
  772. iommu = omap_domain->iommus;
  773. iommu += (omap_domain->num_iommus - 1);
  774. for (i = 0; i < omap_domain->num_iommus; i++, iommu--) {
  775. oiommu = iommu->iommu_dev;
  776. pm_runtime_put_sync(oiommu->dev);
  777. }
  778. return 0;
  779. }
  780. EXPORT_SYMBOL_GPL(omap_iommu_domain_deactivate);
  781. /**
  782. * omap_iommu_domain_activate - activate attached iommu devices
  783. * @domain: iommu domain attached to the target iommu device
  784. *
  785. * This API allows the client devices of IOMMU devices to resume the
  786. * IOMMUs they control at runtime, before they can resume operations.
  787. * System Resume will leverage the PM driver late callbacks.
  788. **/
  789. int omap_iommu_domain_activate(struct iommu_domain *domain)
  790. {
  791. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  792. struct omap_iommu_device *iommu;
  793. struct omap_iommu *oiommu;
  794. int i;
  795. if (!omap_domain->dev)
  796. return 0;
  797. iommu = omap_domain->iommus;
  798. for (i = 0; i < omap_domain->num_iommus; i++, iommu++) {
  799. oiommu = iommu->iommu_dev;
  800. pm_runtime_get_sync(oiommu->dev);
  801. }
  802. return 0;
  803. }
  804. EXPORT_SYMBOL_GPL(omap_iommu_domain_activate);
  805. /**
  806. * omap_iommu_runtime_suspend - disable an iommu device
  807. * @dev: iommu device
  808. *
  809. * This function performs all that is necessary to disable an
  810. * IOMMU device, either during final detachment from a client
  811. * device, or during system/runtime suspend of the device. This
  812. * includes programming all the appropriate IOMMU registers, and
  813. * managing the associated omap_hwmod's state and the device's
  814. * reset line. This function also saves the context of any
  815. * locked TLBs if suspending.
  816. **/
  817. static __maybe_unused int omap_iommu_runtime_suspend(struct device *dev)
  818. {
  819. struct platform_device *pdev = to_platform_device(dev);
  820. struct iommu_platform_data *pdata = dev_get_platdata(dev);
  821. struct omap_iommu *obj = to_iommu(dev);
  822. int ret;
  823. /* save the TLBs only during suspend, and not for power down */
  824. if (obj->domain && obj->iopgd)
  825. omap_iommu_save_tlb_entries(obj);
  826. omap2_iommu_disable(obj);
  827. if (pdata && pdata->device_idle)
  828. pdata->device_idle(pdev);
  829. if (pdata && pdata->assert_reset)
  830. pdata->assert_reset(pdev, pdata->reset_name);
  831. if (pdata && pdata->set_pwrdm_constraint) {
  832. ret = pdata->set_pwrdm_constraint(pdev, false, &obj->pwrst);
  833. if (ret) {
  834. dev_warn(obj->dev, "pwrdm_constraint failed to be reset, status = %d\n",
  835. ret);
  836. }
  837. }
  838. return 0;
  839. }
  840. /**
  841. * omap_iommu_runtime_resume - enable an iommu device
  842. * @dev: iommu device
  843. *
  844. * This function performs all that is necessary to enable an
  845. * IOMMU device, either during initial attachment to a client
  846. * device, or during system/runtime resume of the device. This
  847. * includes programming all the appropriate IOMMU registers, and
  848. * managing the associated omap_hwmod's state and the device's
  849. * reset line. The function also restores any locked TLBs if
  850. * resuming after a suspend.
  851. **/
  852. static __maybe_unused int omap_iommu_runtime_resume(struct device *dev)
  853. {
  854. struct platform_device *pdev = to_platform_device(dev);
  855. struct iommu_platform_data *pdata = dev_get_platdata(dev);
  856. struct omap_iommu *obj = to_iommu(dev);
  857. int ret = 0;
  858. if (pdata && pdata->set_pwrdm_constraint) {
  859. ret = pdata->set_pwrdm_constraint(pdev, true, &obj->pwrst);
  860. if (ret) {
  861. dev_warn(obj->dev, "pwrdm_constraint failed to be set, status = %d\n",
  862. ret);
  863. }
  864. }
  865. if (pdata && pdata->deassert_reset) {
  866. ret = pdata->deassert_reset(pdev, pdata->reset_name);
  867. if (ret) {
  868. dev_err(dev, "deassert_reset failed: %d\n", ret);
  869. return ret;
  870. }
  871. }
  872. if (pdata && pdata->device_enable)
  873. pdata->device_enable(pdev);
  874. /* restore the TLBs only during resume, and not for power up */
  875. if (obj->domain)
  876. omap_iommu_restore_tlb_entries(obj);
  877. ret = omap2_iommu_enable(obj);
  878. return ret;
  879. }
  880. /**
  881. * omap_iommu_prepare - prepare() dev_pm_ops implementation
  882. * @dev: iommu device
  883. *
  884. * This function performs the necessary checks to determine if the IOMMU
  885. * device needs suspending or not. The function checks if the runtime_pm
  886. * status of the device is suspended, and returns 1 in that case. This
  887. * results in the PM core to skip invoking any of the Sleep PM callbacks
  888. * (suspend, suspend_late, resume, resume_early etc).
  889. */
  890. static int omap_iommu_prepare(struct device *dev)
  891. {
  892. if (pm_runtime_status_suspended(dev))
  893. return 1;
  894. return 0;
  895. }
  896. static bool omap_iommu_can_register(struct platform_device *pdev)
  897. {
  898. struct device_node *np = pdev->dev.of_node;
  899. if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu"))
  900. return true;
  901. /*
  902. * restrict IOMMU core registration only for processor-port MDMA MMUs
  903. * on DRA7 DSPs
  904. */
  905. if ((!strcmp(dev_name(&pdev->dev), "40d01000.mmu")) ||
  906. (!strcmp(dev_name(&pdev->dev), "41501000.mmu")))
  907. return true;
  908. return false;
  909. }
  910. static int omap_iommu_dra7_get_dsp_system_cfg(struct platform_device *pdev,
  911. struct omap_iommu *obj)
  912. {
  913. struct device_node *np = pdev->dev.of_node;
  914. if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu"))
  915. return 0;
  916. obj->syscfg = syscon_regmap_lookup_by_phandle_args(np, "ti,syscon-mmuconfig",
  917. 1, &obj->id);
  918. if (IS_ERR(obj->syscfg))
  919. return dev_err_probe(&pdev->dev, PTR_ERR(obj->syscfg),
  920. "ti,syscon-mmuconfig property is missing\n");
  921. if (obj->id != 0 && obj->id != 1) {
  922. dev_err(&pdev->dev, "invalid IOMMU instance id\n");
  923. return -EINVAL;
  924. }
  925. return 0;
  926. }
  927. /*
  928. * OMAP Device MMU(IOMMU) detection
  929. */
  930. static int omap_iommu_probe(struct platform_device *pdev)
  931. {
  932. int err = -ENODEV;
  933. int irq;
  934. struct omap_iommu *obj;
  935. struct resource *res;
  936. struct device_node *of = pdev->dev.of_node;
  937. if (!of) {
  938. pr_err("%s: only DT-based devices are supported\n", __func__);
  939. return -ENODEV;
  940. }
  941. obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
  942. if (!obj)
  943. return -ENOMEM;
  944. /*
  945. * self-manage the ordering dependencies between omap_device_enable/idle
  946. * and omap_device_assert/deassert_hardreset API
  947. */
  948. if (pdev->dev.pm_domain) {
  949. dev_dbg(&pdev->dev, "device pm_domain is being reset\n");
  950. pdev->dev.pm_domain = NULL;
  951. }
  952. obj->name = dev_name(&pdev->dev);
  953. obj->nr_tlb_entries = 32;
  954. err = of_property_read_u32(of, "ti,#tlb-entries", &obj->nr_tlb_entries);
  955. if (err && err != -EINVAL)
  956. return err;
  957. if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
  958. return -EINVAL;
  959. if (of_property_read_bool(of, "ti,iommu-bus-err-back"))
  960. obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
  961. obj->dev = &pdev->dev;
  962. obj->ctx = (void *)obj + sizeof(*obj);
  963. obj->cr_ctx = devm_kzalloc(&pdev->dev,
  964. sizeof(*obj->cr_ctx) * obj->nr_tlb_entries,
  965. GFP_KERNEL);
  966. if (!obj->cr_ctx)
  967. return -ENOMEM;
  968. spin_lock_init(&obj->iommu_lock);
  969. spin_lock_init(&obj->page_table_lock);
  970. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  971. obj->regbase = devm_ioremap_resource(obj->dev, res);
  972. if (IS_ERR(obj->regbase))
  973. return PTR_ERR(obj->regbase);
  974. err = omap_iommu_dra7_get_dsp_system_cfg(pdev, obj);
  975. if (err)
  976. return err;
  977. irq = platform_get_irq(pdev, 0);
  978. if (irq < 0)
  979. return -ENODEV;
  980. err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
  981. dev_name(obj->dev), obj);
  982. if (err < 0)
  983. return err;
  984. platform_set_drvdata(pdev, obj);
  985. if (omap_iommu_can_register(pdev)) {
  986. err = iommu_device_sysfs_add(&obj->iommu, obj->dev, NULL,
  987. obj->name);
  988. if (err)
  989. return err;
  990. obj->has_iommu_driver = true;
  991. }
  992. err = iommu_device_register(&obj->iommu, &omap_iommu_ops, &pdev->dev);
  993. if (err)
  994. goto out_sysfs;
  995. pm_runtime_enable(obj->dev);
  996. omap_iommu_debugfs_add(obj);
  997. dev_info(&pdev->dev, "%s registered\n", obj->name);
  998. return 0;
  999. out_sysfs:
  1000. if (obj->has_iommu_driver)
  1001. iommu_device_sysfs_remove(&obj->iommu);
  1002. return err;
  1003. }
  1004. static void omap_iommu_remove(struct platform_device *pdev)
  1005. {
  1006. struct omap_iommu *obj = platform_get_drvdata(pdev);
  1007. if (obj->has_iommu_driver)
  1008. iommu_device_sysfs_remove(&obj->iommu);
  1009. iommu_device_unregister(&obj->iommu);
  1010. omap_iommu_debugfs_remove(obj);
  1011. pm_runtime_disable(obj->dev);
  1012. dev_info(&pdev->dev, "%s removed\n", obj->name);
  1013. }
  1014. static const struct dev_pm_ops omap_iommu_pm_ops = {
  1015. .prepare = omap_iommu_prepare,
  1016. SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  1017. pm_runtime_force_resume)
  1018. SET_RUNTIME_PM_OPS(omap_iommu_runtime_suspend,
  1019. omap_iommu_runtime_resume, NULL)
  1020. };
  1021. static const struct of_device_id omap_iommu_of_match[] = {
  1022. { .compatible = "ti,omap2-iommu" },
  1023. { .compatible = "ti,omap4-iommu" },
  1024. { .compatible = "ti,dra7-iommu" },
  1025. { .compatible = "ti,dra7-dsp-iommu" },
  1026. {},
  1027. };
  1028. static struct platform_driver omap_iommu_driver = {
  1029. .probe = omap_iommu_probe,
  1030. .remove = omap_iommu_remove,
  1031. .driver = {
  1032. .name = "omap-iommu",
  1033. .pm = &omap_iommu_pm_ops,
  1034. .of_match_table = of_match_ptr(omap_iommu_of_match),
  1035. },
  1036. };
  1037. static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz)
  1038. {
  1039. memset(e, 0, sizeof(*e));
  1040. e->da = da;
  1041. e->pa = pa;
  1042. e->valid = MMU_CAM_V;
  1043. e->pgsz = pgsz;
  1044. e->endian = MMU_RAM_ENDIAN_LITTLE;
  1045. e->elsz = MMU_RAM_ELSZ_8;
  1046. e->mixed = 0;
  1047. return iopgsz_to_bytes(e->pgsz);
  1048. }
  1049. static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
  1050. phys_addr_t pa, size_t bytes, size_t count,
  1051. int prot, gfp_t gfp, size_t *mapped)
  1052. {
  1053. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  1054. struct device *dev = omap_domain->dev;
  1055. struct omap_iommu_device *iommu;
  1056. struct omap_iommu *oiommu;
  1057. struct iotlb_entry e;
  1058. int ret = -EINVAL;
  1059. int omap_pgsz;
  1060. int i;
  1061. omap_pgsz = bytes_to_iopgsz(bytes);
  1062. if (omap_pgsz < 0) {
  1063. dev_err(dev, "invalid size to map: %zu\n", bytes);
  1064. return -EINVAL;
  1065. }
  1066. dev_dbg(dev, "mapping da 0x%lx to pa %pa size 0x%zx\n", da, &pa, bytes);
  1067. iotlb_init_entry(&e, da, pa, omap_pgsz);
  1068. iommu = omap_domain->iommus;
  1069. for (i = 0; i < omap_domain->num_iommus; i++, iommu++) {
  1070. oiommu = iommu->iommu_dev;
  1071. ret = omap_iopgtable_store_entry(oiommu, &e);
  1072. if (ret) {
  1073. dev_err(dev, "omap_iopgtable_store_entry failed: %d\n",
  1074. ret);
  1075. break;
  1076. }
  1077. }
  1078. if (ret) {
  1079. while (i--) {
  1080. iommu--;
  1081. oiommu = iommu->iommu_dev;
  1082. iopgtable_clear_entry(oiommu, da);
  1083. }
  1084. } else {
  1085. *mapped = bytes;
  1086. }
  1087. return ret;
  1088. }
  1089. static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
  1090. size_t size, size_t count, struct iommu_iotlb_gather *gather)
  1091. {
  1092. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  1093. struct device *dev = omap_domain->dev;
  1094. struct omap_iommu_device *iommu;
  1095. struct omap_iommu *oiommu;
  1096. bool error = false;
  1097. size_t bytes = 0;
  1098. int i;
  1099. dev_dbg(dev, "unmapping da 0x%lx size %zu\n", da, size);
  1100. iommu = omap_domain->iommus;
  1101. for (i = 0; i < omap_domain->num_iommus; i++, iommu++) {
  1102. oiommu = iommu->iommu_dev;
  1103. bytes = iopgtable_clear_entry(oiommu, da);
  1104. if (!bytes)
  1105. error = true;
  1106. }
  1107. /*
  1108. * simplify return - we are only checking if any of the iommus
  1109. * reported an error, but not if all of them are unmapping the
  1110. * same number of entries. This should not occur due to the
  1111. * mirror programming.
  1112. */
  1113. return error ? 0 : bytes;
  1114. }
  1115. static int omap_iommu_count(struct device *dev)
  1116. {
  1117. struct omap_iommu_arch_data *arch_data = dev_iommu_priv_get(dev);
  1118. int count = 0;
  1119. while (arch_data->iommu_dev) {
  1120. count++;
  1121. arch_data++;
  1122. }
  1123. return count;
  1124. }
  1125. /* caller should call cleanup if this function fails */
  1126. static int omap_iommu_attach_init(struct device *dev,
  1127. struct omap_iommu_domain *odomain)
  1128. {
  1129. struct omap_iommu_device *iommu;
  1130. int i;
  1131. odomain->num_iommus = omap_iommu_count(dev);
  1132. if (!odomain->num_iommus)
  1133. return -ENODEV;
  1134. odomain->iommus = kzalloc_objs(*iommu, odomain->num_iommus, GFP_ATOMIC);
  1135. if (!odomain->iommus)
  1136. return -ENOMEM;
  1137. iommu = odomain->iommus;
  1138. for (i = 0; i < odomain->num_iommus; i++, iommu++) {
  1139. iommu->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_ATOMIC);
  1140. if (!iommu->pgtable)
  1141. return -ENOMEM;
  1142. /*
  1143. * should never fail, but please keep this around to ensure
  1144. * we keep the hardware happy
  1145. */
  1146. if (WARN_ON(!IS_ALIGNED((long)iommu->pgtable,
  1147. IOPGD_TABLE_SIZE)))
  1148. return -EINVAL;
  1149. }
  1150. return 0;
  1151. }
  1152. static void omap_iommu_detach_fini(struct omap_iommu_domain *odomain)
  1153. {
  1154. int i;
  1155. struct omap_iommu_device *iommu = odomain->iommus;
  1156. for (i = 0; iommu && i < odomain->num_iommus; i++, iommu++)
  1157. kfree(iommu->pgtable);
  1158. kfree(odomain->iommus);
  1159. odomain->num_iommus = 0;
  1160. odomain->iommus = NULL;
  1161. }
  1162. static int omap_iommu_attach_dev(struct iommu_domain *domain,
  1163. struct device *dev, struct iommu_domain *old)
  1164. {
  1165. struct omap_iommu_arch_data *arch_data = dev_iommu_priv_get(dev);
  1166. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  1167. struct omap_iommu_device *iommu;
  1168. struct omap_iommu *oiommu;
  1169. int ret = 0;
  1170. int i;
  1171. if (!arch_data || !arch_data->iommu_dev) {
  1172. dev_err(dev, "device doesn't have an associated iommu\n");
  1173. return -ENODEV;
  1174. }
  1175. spin_lock(&omap_domain->lock);
  1176. /* only a single client device can be attached to a domain */
  1177. if (omap_domain->dev) {
  1178. dev_err(dev, "iommu domain is already attached\n");
  1179. ret = -EINVAL;
  1180. goto out;
  1181. }
  1182. ret = omap_iommu_attach_init(dev, omap_domain);
  1183. if (ret) {
  1184. dev_err(dev, "failed to allocate required iommu data %d\n",
  1185. ret);
  1186. goto init_fail;
  1187. }
  1188. iommu = omap_domain->iommus;
  1189. for (i = 0; i < omap_domain->num_iommus; i++, iommu++, arch_data++) {
  1190. /* configure and enable the omap iommu */
  1191. oiommu = arch_data->iommu_dev;
  1192. ret = omap_iommu_attach(oiommu, iommu->pgtable);
  1193. if (ret) {
  1194. dev_err(dev, "can't get omap iommu: %d\n", ret);
  1195. goto attach_fail;
  1196. }
  1197. oiommu->domain = domain;
  1198. iommu->iommu_dev = oiommu;
  1199. }
  1200. omap_domain->dev = dev;
  1201. goto out;
  1202. attach_fail:
  1203. while (i--) {
  1204. iommu--;
  1205. arch_data--;
  1206. oiommu = iommu->iommu_dev;
  1207. omap_iommu_detach(oiommu);
  1208. iommu->iommu_dev = NULL;
  1209. oiommu->domain = NULL;
  1210. }
  1211. init_fail:
  1212. omap_iommu_detach_fini(omap_domain);
  1213. out:
  1214. spin_unlock(&omap_domain->lock);
  1215. return ret;
  1216. }
  1217. static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
  1218. struct device *dev)
  1219. {
  1220. struct omap_iommu_arch_data *arch_data = dev_iommu_priv_get(dev);
  1221. struct omap_iommu_device *iommu = omap_domain->iommus;
  1222. struct omap_iommu *oiommu;
  1223. int i;
  1224. if (!omap_domain->dev) {
  1225. dev_err(dev, "domain has no attached device\n");
  1226. return;
  1227. }
  1228. /* only a single device is supported per domain for now */
  1229. if (omap_domain->dev != dev) {
  1230. dev_err(dev, "invalid attached device\n");
  1231. return;
  1232. }
  1233. /*
  1234. * cleanup in the reverse order of attachment - this addresses
  1235. * any h/w dependencies between multiple instances, if any
  1236. */
  1237. iommu += (omap_domain->num_iommus - 1);
  1238. arch_data += (omap_domain->num_iommus - 1);
  1239. for (i = 0; i < omap_domain->num_iommus; i++, iommu--, arch_data--) {
  1240. oiommu = iommu->iommu_dev;
  1241. iopgtable_clear_entry_all(oiommu);
  1242. omap_iommu_detach(oiommu);
  1243. iommu->iommu_dev = NULL;
  1244. oiommu->domain = NULL;
  1245. }
  1246. omap_iommu_detach_fini(omap_domain);
  1247. omap_domain->dev = NULL;
  1248. }
  1249. static int omap_iommu_identity_attach(struct iommu_domain *identity_domain,
  1250. struct device *dev,
  1251. struct iommu_domain *old)
  1252. {
  1253. struct omap_iommu_domain *omap_domain;
  1254. if (old == identity_domain || !old)
  1255. return 0;
  1256. omap_domain = to_omap_domain(old);
  1257. spin_lock(&omap_domain->lock);
  1258. _omap_iommu_detach_dev(omap_domain, dev);
  1259. spin_unlock(&omap_domain->lock);
  1260. return 0;
  1261. }
  1262. static struct iommu_domain_ops omap_iommu_identity_ops = {
  1263. .attach_dev = omap_iommu_identity_attach,
  1264. };
  1265. static struct iommu_domain omap_iommu_identity_domain = {
  1266. .type = IOMMU_DOMAIN_IDENTITY,
  1267. .ops = &omap_iommu_identity_ops,
  1268. };
  1269. static struct iommu_domain *omap_iommu_domain_alloc_paging(struct device *dev)
  1270. {
  1271. struct omap_iommu_domain *omap_domain;
  1272. omap_domain = kzalloc_obj(*omap_domain);
  1273. if (!omap_domain)
  1274. return NULL;
  1275. spin_lock_init(&omap_domain->lock);
  1276. omap_domain->domain.pgsize_bitmap = OMAP_IOMMU_PGSIZES;
  1277. omap_domain->domain.geometry.aperture_start = 0;
  1278. omap_domain->domain.geometry.aperture_end = (1ULL << 32) - 1;
  1279. omap_domain->domain.geometry.force_aperture = true;
  1280. return &omap_domain->domain;
  1281. }
  1282. static void omap_iommu_domain_free(struct iommu_domain *domain)
  1283. {
  1284. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  1285. /*
  1286. * An iommu device is still attached
  1287. * (currently, only one device can be attached) ?
  1288. */
  1289. if (omap_domain->dev)
  1290. _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
  1291. kfree(omap_domain);
  1292. }
  1293. static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
  1294. dma_addr_t da)
  1295. {
  1296. struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
  1297. struct omap_iommu_device *iommu = omap_domain->iommus;
  1298. struct omap_iommu *oiommu = iommu->iommu_dev;
  1299. struct device *dev = oiommu->dev;
  1300. u32 *pgd, *pte;
  1301. phys_addr_t ret = 0;
  1302. /*
  1303. * all the iommus within the domain will have identical programming,
  1304. * so perform the lookup using just the first iommu
  1305. */
  1306. iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
  1307. if (pte) {
  1308. if (iopte_is_small(*pte))
  1309. ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
  1310. else if (iopte_is_large(*pte))
  1311. ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
  1312. else
  1313. dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
  1314. (unsigned long long)da);
  1315. } else {
  1316. if (iopgd_is_section(*pgd))
  1317. ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
  1318. else if (iopgd_is_super(*pgd))
  1319. ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
  1320. else
  1321. dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
  1322. (unsigned long long)da);
  1323. }
  1324. return ret;
  1325. }
  1326. static struct iommu_device *omap_iommu_probe_device(struct device *dev)
  1327. {
  1328. struct omap_iommu_arch_data *arch_data, *tmp;
  1329. struct platform_device *pdev;
  1330. struct omap_iommu *oiommu;
  1331. struct device_node *np;
  1332. int num_iommus, i;
  1333. /*
  1334. * Allocate the per-device iommu structure for DT-based devices.
  1335. *
  1336. * TODO: Simplify this when removing non-DT support completely from the
  1337. * IOMMU users.
  1338. */
  1339. if (!dev->of_node)
  1340. return ERR_PTR(-ENODEV);
  1341. /*
  1342. * retrieve the count of IOMMU nodes using phandle size as element size
  1343. * since #iommu-cells = 0 for OMAP
  1344. */
  1345. num_iommus = of_property_count_elems_of_size(dev->of_node, "iommus",
  1346. sizeof(phandle));
  1347. if (num_iommus < 0)
  1348. return ERR_PTR(-ENODEV);
  1349. arch_data = kzalloc_objs(*arch_data, num_iommus + 1);
  1350. if (!arch_data)
  1351. return ERR_PTR(-ENOMEM);
  1352. for (i = 0, tmp = arch_data; i < num_iommus; i++, tmp++) {
  1353. np = of_parse_phandle(dev->of_node, "iommus", i);
  1354. if (!np) {
  1355. kfree(arch_data);
  1356. return ERR_PTR(-EINVAL);
  1357. }
  1358. pdev = of_find_device_by_node(np);
  1359. of_node_put(np);
  1360. if (!pdev) {
  1361. kfree(arch_data);
  1362. return ERR_PTR(-ENODEV);
  1363. }
  1364. oiommu = platform_get_drvdata(pdev);
  1365. put_device(&pdev->dev);
  1366. if (!oiommu) {
  1367. kfree(arch_data);
  1368. return ERR_PTR(-EINVAL);
  1369. }
  1370. tmp->iommu_dev = oiommu;
  1371. }
  1372. dev_iommu_priv_set(dev, arch_data);
  1373. /*
  1374. * use the first IOMMU alone for the sysfs device linking.
  1375. * TODO: Evaluate if a single iommu_group needs to be
  1376. * maintained for both IOMMUs
  1377. */
  1378. oiommu = arch_data->iommu_dev;
  1379. return &oiommu->iommu;
  1380. }
  1381. static void omap_iommu_release_device(struct device *dev)
  1382. {
  1383. struct omap_iommu_arch_data *arch_data = dev_iommu_priv_get(dev);
  1384. if (!dev->of_node || !arch_data)
  1385. return;
  1386. kfree(arch_data);
  1387. }
  1388. static int omap_iommu_of_xlate(struct device *dev, const struct of_phandle_args *args)
  1389. {
  1390. /* TODO: collect args->np to save re-parsing in probe above */
  1391. return 0;
  1392. }
  1393. static const struct iommu_ops omap_iommu_ops = {
  1394. .identity_domain = &omap_iommu_identity_domain,
  1395. .domain_alloc_paging = omap_iommu_domain_alloc_paging,
  1396. .probe_device = omap_iommu_probe_device,
  1397. .release_device = omap_iommu_release_device,
  1398. .device_group = generic_single_device_group,
  1399. .of_xlate = omap_iommu_of_xlate,
  1400. .default_domain_ops = &(const struct iommu_domain_ops) {
  1401. .attach_dev = omap_iommu_attach_dev,
  1402. .map_pages = omap_iommu_map,
  1403. .unmap_pages = omap_iommu_unmap,
  1404. .iova_to_phys = omap_iommu_iova_to_phys,
  1405. .free = omap_iommu_domain_free,
  1406. }
  1407. };
  1408. static int __init omap_iommu_init(void)
  1409. {
  1410. struct kmem_cache *p;
  1411. const slab_flags_t flags = SLAB_HWCACHE_ALIGN;
  1412. size_t align = 1 << 10; /* L2 pagetable alignement */
  1413. struct device_node *np;
  1414. int ret;
  1415. np = of_find_matching_node(NULL, omap_iommu_of_match);
  1416. if (!np)
  1417. return 0;
  1418. of_node_put(np);
  1419. p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
  1420. NULL);
  1421. if (!p)
  1422. return -ENOMEM;
  1423. iopte_cachep = p;
  1424. omap_iommu_debugfs_init();
  1425. ret = platform_driver_register(&omap_iommu_driver);
  1426. if (ret) {
  1427. pr_err("%s: failed to register driver\n", __func__);
  1428. goto fail_driver;
  1429. }
  1430. return 0;
  1431. fail_driver:
  1432. kmem_cache_destroy(iopte_cachep);
  1433. return ret;
  1434. }
  1435. subsys_initcall(omap_iommu_init);
  1436. /* must be ready before omap3isp is probed */