ipmmu-vmsa.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * IOMMU API for Renesas VMSA-compatible IPMMU
  4. * Author: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  5. *
  6. * Copyright (C) 2014-2020 Renesas Electronics Corporation
  7. */
  8. #include <linux/bitmap.h>
  9. #include <linux/delay.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/err.h>
  12. #include <linux/export.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/iopoll.h>
  17. #include <linux/io-pgtable.h>
  18. #include <linux/iommu.h>
  19. #include <linux/of.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/pci.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/sizes.h>
  24. #include <linux/slab.h>
  25. #include <linux/sys_soc.h>
  26. #if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA)
  27. #include <asm/dma-iommu.h>
  28. #else
  29. #define arm_iommu_create_mapping(...) NULL
  30. #define arm_iommu_attach_device(...) -ENODEV
  31. #define arm_iommu_release_mapping(...) do {} while (0)
  32. #endif
  33. #define IPMMU_CTX_MAX 16U
  34. #define IPMMU_CTX_INVALID -1
  35. #define IPMMU_UTLB_MAX 64U
  36. struct ipmmu_features {
  37. bool use_ns_alias_offset;
  38. bool has_cache_leaf_nodes;
  39. unsigned int number_of_contexts;
  40. unsigned int num_utlbs;
  41. bool setup_imbuscr;
  42. bool twobit_imttbcr_sl0;
  43. bool reserved_context;
  44. bool cache_snoop;
  45. unsigned int ctx_offset_base;
  46. unsigned int ctx_offset_stride;
  47. unsigned int utlb_offset_base;
  48. };
  49. struct ipmmu_vmsa_device {
  50. struct device *dev;
  51. void __iomem *base;
  52. struct iommu_device iommu;
  53. struct ipmmu_vmsa_device *root;
  54. const struct ipmmu_features *features;
  55. unsigned int num_ctx;
  56. spinlock_t lock; /* Protects ctx and domains[] */
  57. DECLARE_BITMAP(ctx, IPMMU_CTX_MAX);
  58. struct ipmmu_vmsa_domain *domains[IPMMU_CTX_MAX];
  59. s8 utlb_ctx[IPMMU_UTLB_MAX];
  60. struct dma_iommu_mapping *mapping;
  61. };
  62. struct ipmmu_vmsa_domain {
  63. struct ipmmu_vmsa_device *mmu;
  64. struct iommu_domain io_domain;
  65. struct io_pgtable_cfg cfg;
  66. struct io_pgtable_ops *iop;
  67. unsigned int context_id;
  68. struct mutex mutex; /* Protects mappings */
  69. };
  70. static struct ipmmu_vmsa_domain *to_vmsa_domain(struct iommu_domain *dom)
  71. {
  72. return container_of(dom, struct ipmmu_vmsa_domain, io_domain);
  73. }
  74. static struct ipmmu_vmsa_device *to_ipmmu(struct device *dev)
  75. {
  76. return dev_iommu_priv_get(dev);
  77. }
  78. #define TLB_LOOP_TIMEOUT 100 /* 100us */
  79. /* -----------------------------------------------------------------------------
  80. * Registers Definition
  81. */
  82. #define IM_NS_ALIAS_OFFSET 0x800
  83. /* MMU "context" registers */
  84. #define IMCTR 0x0000 /* R-Car Gen2/3 */
  85. #define IMCTR_INTEN (1 << 2) /* R-Car Gen2/3 */
  86. #define IMCTR_FLUSH (1 << 1) /* R-Car Gen2/3 */
  87. #define IMCTR_MMUEN (1 << 0) /* R-Car Gen2/3 */
  88. #define IMTTBCR 0x0008 /* R-Car Gen2/3 */
  89. #define IMTTBCR_EAE (1 << 31) /* R-Car Gen2/3 */
  90. #define IMTTBCR_SH0_INNER_SHAREABLE (3 << 12) /* R-Car Gen2 only */
  91. #define IMTTBCR_ORGN0_WB_WA (1 << 10) /* R-Car Gen2 only */
  92. #define IMTTBCR_IRGN0_WB_WA (1 << 8) /* R-Car Gen2 only */
  93. #define IMTTBCR_SL0_TWOBIT_LVL_1 (2 << 6) /* R-Car Gen3 only */
  94. #define IMTTBCR_SL0_LVL_1 (1 << 4) /* R-Car Gen2 only */
  95. #define IMBUSCR 0x000c /* R-Car Gen2 only */
  96. #define IMBUSCR_DVM (1 << 2) /* R-Car Gen2 only */
  97. #define IMBUSCR_BUSSEL_MASK (3 << 0) /* R-Car Gen2 only */
  98. #define IMTTLBR0 0x0010 /* R-Car Gen2/3 */
  99. #define IMTTUBR0 0x0014 /* R-Car Gen2/3 */
  100. #define IMSTR 0x0020 /* R-Car Gen2/3 */
  101. #define IMSTR_MHIT (1 << 4) /* R-Car Gen2/3 */
  102. #define IMSTR_ABORT (1 << 2) /* R-Car Gen2/3 */
  103. #define IMSTR_PF (1 << 1) /* R-Car Gen2/3 */
  104. #define IMSTR_TF (1 << 0) /* R-Car Gen2/3 */
  105. #define IMMAIR0 0x0028 /* R-Car Gen2/3 */
  106. #define IMELAR 0x0030 /* R-Car Gen2/3, IMEAR on R-Car Gen2 */
  107. #define IMEUAR 0x0034 /* R-Car Gen3 only */
  108. /* uTLB registers */
  109. #define IMUCTR(n) ((n) < 32 ? IMUCTR0(n) : IMUCTR32(n))
  110. #define IMUCTR0(n) (0x0300 + ((n) * 16)) /* R-Car Gen2/3 */
  111. #define IMUCTR32(n) (0x0600 + (((n) - 32) * 16)) /* R-Car Gen3 only */
  112. #define IMUCTR_TTSEL_MMU(n) ((n) << 4) /* R-Car Gen2/3 */
  113. #define IMUCTR_FLUSH (1 << 1) /* R-Car Gen2/3 */
  114. #define IMUCTR_MMUEN (1 << 0) /* R-Car Gen2/3 */
  115. #define IMUASID(n) ((n) < 32 ? IMUASID0(n) : IMUASID32(n))
  116. #define IMUASID0(n) (0x0308 + ((n) * 16)) /* R-Car Gen2/3 */
  117. #define IMUASID32(n) (0x0608 + (((n) - 32) * 16)) /* R-Car Gen3 only */
  118. /* -----------------------------------------------------------------------------
  119. * Root device handling
  120. */
  121. static struct platform_driver ipmmu_driver;
  122. static bool ipmmu_is_root(struct ipmmu_vmsa_device *mmu)
  123. {
  124. return mmu->root == mmu;
  125. }
  126. static int __ipmmu_check_device(struct device *dev, void *data)
  127. {
  128. struct ipmmu_vmsa_device *mmu = dev_get_drvdata(dev);
  129. struct ipmmu_vmsa_device **rootp = data;
  130. if (ipmmu_is_root(mmu))
  131. *rootp = mmu;
  132. return 0;
  133. }
  134. static struct ipmmu_vmsa_device *ipmmu_find_root(void)
  135. {
  136. struct ipmmu_vmsa_device *root = NULL;
  137. return driver_for_each_device(&ipmmu_driver.driver, NULL, &root,
  138. __ipmmu_check_device) == 0 ? root : NULL;
  139. }
  140. /* -----------------------------------------------------------------------------
  141. * Read/Write Access
  142. */
  143. static u32 ipmmu_read(struct ipmmu_vmsa_device *mmu, unsigned int offset)
  144. {
  145. return ioread32(mmu->base + offset);
  146. }
  147. static void ipmmu_write(struct ipmmu_vmsa_device *mmu, unsigned int offset,
  148. u32 data)
  149. {
  150. iowrite32(data, mmu->base + offset);
  151. }
  152. static unsigned int ipmmu_ctx_reg(struct ipmmu_vmsa_device *mmu,
  153. unsigned int context_id, unsigned int reg)
  154. {
  155. unsigned int base = mmu->features->ctx_offset_base;
  156. if (context_id > 7)
  157. base += 0x800 - 8 * 0x40;
  158. return base + context_id * mmu->features->ctx_offset_stride + reg;
  159. }
  160. static u32 ipmmu_ctx_read(struct ipmmu_vmsa_device *mmu,
  161. unsigned int context_id, unsigned int reg)
  162. {
  163. return ipmmu_read(mmu, ipmmu_ctx_reg(mmu, context_id, reg));
  164. }
  165. static void ipmmu_ctx_write(struct ipmmu_vmsa_device *mmu,
  166. unsigned int context_id, unsigned int reg, u32 data)
  167. {
  168. ipmmu_write(mmu, ipmmu_ctx_reg(mmu, context_id, reg), data);
  169. }
  170. static u32 ipmmu_ctx_read_root(struct ipmmu_vmsa_domain *domain,
  171. unsigned int reg)
  172. {
  173. return ipmmu_ctx_read(domain->mmu->root, domain->context_id, reg);
  174. }
  175. static void ipmmu_ctx_write_root(struct ipmmu_vmsa_domain *domain,
  176. unsigned int reg, u32 data)
  177. {
  178. ipmmu_ctx_write(domain->mmu->root, domain->context_id, reg, data);
  179. }
  180. static void ipmmu_ctx_write_all(struct ipmmu_vmsa_domain *domain,
  181. unsigned int reg, u32 data)
  182. {
  183. if (domain->mmu != domain->mmu->root)
  184. ipmmu_ctx_write(domain->mmu, domain->context_id, reg, data);
  185. ipmmu_ctx_write(domain->mmu->root, domain->context_id, reg, data);
  186. }
  187. static u32 ipmmu_utlb_reg(struct ipmmu_vmsa_device *mmu, unsigned int reg)
  188. {
  189. return mmu->features->utlb_offset_base + reg;
  190. }
  191. static void ipmmu_imuasid_write(struct ipmmu_vmsa_device *mmu,
  192. unsigned int utlb, u32 data)
  193. {
  194. ipmmu_write(mmu, ipmmu_utlb_reg(mmu, IMUASID(utlb)), data);
  195. }
  196. static void ipmmu_imuctr_write(struct ipmmu_vmsa_device *mmu,
  197. unsigned int utlb, u32 data)
  198. {
  199. ipmmu_write(mmu, ipmmu_utlb_reg(mmu, IMUCTR(utlb)), data);
  200. }
  201. /* -----------------------------------------------------------------------------
  202. * TLB and microTLB Management
  203. */
  204. /* Wait for any pending TLB invalidations to complete */
  205. static void ipmmu_tlb_sync(struct ipmmu_vmsa_domain *domain)
  206. {
  207. u32 val;
  208. if (read_poll_timeout_atomic(ipmmu_ctx_read_root, val,
  209. !(val & IMCTR_FLUSH), 1, TLB_LOOP_TIMEOUT,
  210. false, domain, IMCTR))
  211. dev_err_ratelimited(domain->mmu->dev,
  212. "TLB sync timed out -- MMU may be deadlocked\n");
  213. }
  214. static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain)
  215. {
  216. u32 reg;
  217. reg = ipmmu_ctx_read_root(domain, IMCTR);
  218. reg |= IMCTR_FLUSH;
  219. ipmmu_ctx_write_all(domain, IMCTR, reg);
  220. ipmmu_tlb_sync(domain);
  221. }
  222. /*
  223. * Enable MMU translation for the microTLB.
  224. */
  225. static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
  226. unsigned int utlb)
  227. {
  228. struct ipmmu_vmsa_device *mmu = domain->mmu;
  229. /*
  230. * TODO: Reference-count the microTLB as several bus masters can be
  231. * connected to the same microTLB.
  232. */
  233. /* TODO: What should we set the ASID to ? */
  234. ipmmu_imuasid_write(mmu, utlb, 0);
  235. /* TODO: Do we need to flush the microTLB ? */
  236. ipmmu_imuctr_write(mmu, utlb, IMUCTR_TTSEL_MMU(domain->context_id) |
  237. IMUCTR_FLUSH | IMUCTR_MMUEN);
  238. mmu->utlb_ctx[utlb] = domain->context_id;
  239. }
  240. /*
  241. * Disable MMU translation for the microTLB.
  242. */
  243. static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain,
  244. unsigned int utlb)
  245. {
  246. struct ipmmu_vmsa_device *mmu = domain->mmu;
  247. ipmmu_imuctr_write(mmu, utlb, 0);
  248. mmu->utlb_ctx[utlb] = IPMMU_CTX_INVALID;
  249. }
  250. static void ipmmu_tlb_flush_all(void *cookie)
  251. {
  252. struct ipmmu_vmsa_domain *domain = cookie;
  253. ipmmu_tlb_invalidate(domain);
  254. }
  255. static void ipmmu_tlb_flush(unsigned long iova, size_t size,
  256. size_t granule, void *cookie)
  257. {
  258. ipmmu_tlb_flush_all(cookie);
  259. }
  260. static const struct iommu_flush_ops ipmmu_flush_ops = {
  261. .tlb_flush_all = ipmmu_tlb_flush_all,
  262. .tlb_flush_walk = ipmmu_tlb_flush,
  263. };
  264. /* -----------------------------------------------------------------------------
  265. * Domain/Context Management
  266. */
  267. static int ipmmu_domain_allocate_context(struct ipmmu_vmsa_device *mmu,
  268. struct ipmmu_vmsa_domain *domain)
  269. {
  270. unsigned long flags;
  271. int ret;
  272. spin_lock_irqsave(&mmu->lock, flags);
  273. ret = find_first_zero_bit(mmu->ctx, mmu->num_ctx);
  274. if (ret != mmu->num_ctx) {
  275. mmu->domains[ret] = domain;
  276. set_bit(ret, mmu->ctx);
  277. } else
  278. ret = -EBUSY;
  279. spin_unlock_irqrestore(&mmu->lock, flags);
  280. return ret;
  281. }
  282. static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu,
  283. unsigned int context_id)
  284. {
  285. unsigned long flags;
  286. spin_lock_irqsave(&mmu->lock, flags);
  287. clear_bit(context_id, mmu->ctx);
  288. mmu->domains[context_id] = NULL;
  289. spin_unlock_irqrestore(&mmu->lock, flags);
  290. }
  291. static void ipmmu_domain_setup_context(struct ipmmu_vmsa_domain *domain)
  292. {
  293. u64 ttbr;
  294. u32 tmp;
  295. /* TTBR0 */
  296. ttbr = domain->cfg.arm_lpae_s1_cfg.ttbr;
  297. ipmmu_ctx_write_root(domain, IMTTLBR0, ttbr);
  298. ipmmu_ctx_write_root(domain, IMTTUBR0, ttbr >> 32);
  299. /*
  300. * TTBCR
  301. * We use long descriptors and allocate the whole 32-bit VA space to
  302. * TTBR0.
  303. */
  304. if (domain->mmu->features->twobit_imttbcr_sl0)
  305. tmp = IMTTBCR_SL0_TWOBIT_LVL_1;
  306. else
  307. tmp = IMTTBCR_SL0_LVL_1;
  308. if (domain->mmu->features->cache_snoop)
  309. tmp |= IMTTBCR_SH0_INNER_SHAREABLE | IMTTBCR_ORGN0_WB_WA |
  310. IMTTBCR_IRGN0_WB_WA;
  311. ipmmu_ctx_write_root(domain, IMTTBCR, IMTTBCR_EAE | tmp);
  312. /* MAIR0 */
  313. ipmmu_ctx_write_root(domain, IMMAIR0,
  314. domain->cfg.arm_lpae_s1_cfg.mair);
  315. /* IMBUSCR */
  316. if (domain->mmu->features->setup_imbuscr)
  317. ipmmu_ctx_write_root(domain, IMBUSCR,
  318. ipmmu_ctx_read_root(domain, IMBUSCR) &
  319. ~(IMBUSCR_DVM | IMBUSCR_BUSSEL_MASK));
  320. /*
  321. * IMSTR
  322. * Clear all interrupt flags.
  323. */
  324. ipmmu_ctx_write_root(domain, IMSTR, ipmmu_ctx_read_root(domain, IMSTR));
  325. /*
  326. * IMCTR
  327. * Enable the MMU and interrupt generation. The long-descriptor
  328. * translation table format doesn't use TEX remapping. Don't enable AF
  329. * software management as we have no use for it. Flush the TLB as
  330. * required when modifying the context registers.
  331. */
  332. ipmmu_ctx_write_all(domain, IMCTR,
  333. IMCTR_INTEN | IMCTR_FLUSH | IMCTR_MMUEN);
  334. }
  335. static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
  336. {
  337. int ret;
  338. /*
  339. * Allocate the page table operations.
  340. *
  341. * VMSA states in section B3.6.3 "Control of Secure or Non-secure memory
  342. * access, Long-descriptor format" that the NStable bit being set in a
  343. * table descriptor will result in the NStable and NS bits of all child
  344. * entries being ignored and considered as being set. The IPMMU seems
  345. * not to comply with this, as it generates a secure access page fault
  346. * if any of the NStable and NS bits isn't set when running in
  347. * non-secure mode.
  348. */
  349. domain->cfg.quirks = IO_PGTABLE_QUIRK_ARM_NS;
  350. domain->cfg.pgsize_bitmap = domain->io_domain.pgsize_bitmap;
  351. domain->cfg.ias = 32;
  352. domain->cfg.oas = 40;
  353. domain->cfg.tlb = &ipmmu_flush_ops;
  354. domain->io_domain.geometry.aperture_end = DMA_BIT_MASK(32);
  355. domain->io_domain.geometry.force_aperture = true;
  356. /*
  357. * TODO: Add support for coherent walk through CCI with DVM and remove
  358. * cache handling. For now, delegate it to the io-pgtable code.
  359. */
  360. domain->cfg.coherent_walk = false;
  361. domain->cfg.iommu_dev = domain->mmu->root->dev;
  362. /*
  363. * Find an unused context.
  364. */
  365. ret = ipmmu_domain_allocate_context(domain->mmu->root, domain);
  366. if (ret < 0)
  367. return ret;
  368. domain->context_id = ret;
  369. domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg,
  370. domain);
  371. if (!domain->iop) {
  372. ipmmu_domain_free_context(domain->mmu->root,
  373. domain->context_id);
  374. return -EINVAL;
  375. }
  376. ipmmu_domain_setup_context(domain);
  377. return 0;
  378. }
  379. static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain)
  380. {
  381. if (!domain->mmu)
  382. return;
  383. /*
  384. * Disable the context. Flush the TLB as required when modifying the
  385. * context registers.
  386. *
  387. * TODO: Is TLB flush really needed ?
  388. */
  389. ipmmu_ctx_write_all(domain, IMCTR, IMCTR_FLUSH);
  390. ipmmu_tlb_sync(domain);
  391. ipmmu_domain_free_context(domain->mmu->root, domain->context_id);
  392. }
  393. /* -----------------------------------------------------------------------------
  394. * Fault Handling
  395. */
  396. static irqreturn_t ipmmu_domain_irq(struct ipmmu_vmsa_domain *domain)
  397. {
  398. const u32 err_mask = IMSTR_MHIT | IMSTR_ABORT | IMSTR_PF | IMSTR_TF;
  399. struct ipmmu_vmsa_device *mmu = domain->mmu;
  400. unsigned long iova;
  401. u32 status;
  402. status = ipmmu_ctx_read_root(domain, IMSTR);
  403. if (!(status & err_mask))
  404. return IRQ_NONE;
  405. iova = ipmmu_ctx_read_root(domain, IMELAR);
  406. if (IS_ENABLED(CONFIG_64BIT))
  407. iova |= (u64)ipmmu_ctx_read_root(domain, IMEUAR) << 32;
  408. /*
  409. * Clear the error status flags. Unlike traditional interrupt flag
  410. * registers that must be cleared by writing 1, this status register
  411. * seems to require 0. The error address register must be read before,
  412. * otherwise its value will be 0.
  413. */
  414. ipmmu_ctx_write_root(domain, IMSTR, 0);
  415. /* Log fatal errors. */
  416. if (status & IMSTR_MHIT)
  417. dev_err_ratelimited(mmu->dev, "Multiple TLB hits @0x%lx\n",
  418. iova);
  419. if (status & IMSTR_ABORT)
  420. dev_err_ratelimited(mmu->dev, "Page Table Walk Abort @0x%lx\n",
  421. iova);
  422. if (!(status & (IMSTR_PF | IMSTR_TF)))
  423. return IRQ_NONE;
  424. /*
  425. * Try to handle page faults and translation faults.
  426. *
  427. * TODO: We need to look up the faulty device based on the I/O VA. Use
  428. * the IOMMU device for now.
  429. */
  430. if (!report_iommu_fault(&domain->io_domain, mmu->dev, iova, 0))
  431. return IRQ_HANDLED;
  432. dev_err_ratelimited(mmu->dev,
  433. "Unhandled fault: status 0x%08x iova 0x%lx\n",
  434. status, iova);
  435. return IRQ_HANDLED;
  436. }
  437. static irqreturn_t ipmmu_irq(int irq, void *dev)
  438. {
  439. struct ipmmu_vmsa_device *mmu = dev;
  440. irqreturn_t status = IRQ_NONE;
  441. unsigned int i;
  442. unsigned long flags;
  443. spin_lock_irqsave(&mmu->lock, flags);
  444. /*
  445. * Check interrupts for all active contexts.
  446. */
  447. for (i = 0; i < mmu->num_ctx; i++) {
  448. if (!mmu->domains[i])
  449. continue;
  450. if (ipmmu_domain_irq(mmu->domains[i]) == IRQ_HANDLED)
  451. status = IRQ_HANDLED;
  452. }
  453. spin_unlock_irqrestore(&mmu->lock, flags);
  454. return status;
  455. }
  456. /* -----------------------------------------------------------------------------
  457. * IOMMU Operations
  458. */
  459. static struct iommu_domain *ipmmu_domain_alloc_paging(struct device *dev)
  460. {
  461. struct ipmmu_vmsa_domain *domain;
  462. domain = kzalloc_obj(*domain);
  463. if (!domain)
  464. return NULL;
  465. mutex_init(&domain->mutex);
  466. domain->io_domain.pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K;
  467. return &domain->io_domain;
  468. }
  469. static void ipmmu_domain_free(struct iommu_domain *io_domain)
  470. {
  471. struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
  472. /*
  473. * Free the domain resources. We assume that all devices have already
  474. * been detached.
  475. */
  476. ipmmu_domain_destroy_context(domain);
  477. free_io_pgtable_ops(domain->iop);
  478. kfree(domain);
  479. }
  480. static int ipmmu_attach_device(struct iommu_domain *io_domain,
  481. struct device *dev, struct iommu_domain *old)
  482. {
  483. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  484. struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
  485. struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
  486. unsigned int i;
  487. int ret = 0;
  488. if (!mmu) {
  489. dev_err(dev, "Cannot attach to IPMMU\n");
  490. return -ENXIO;
  491. }
  492. mutex_lock(&domain->mutex);
  493. if (!domain->mmu) {
  494. /* The domain hasn't been used yet, initialize it. */
  495. domain->mmu = mmu;
  496. ret = ipmmu_domain_init_context(domain);
  497. if (ret < 0) {
  498. dev_err(dev, "Unable to initialize IPMMU context\n");
  499. domain->mmu = NULL;
  500. } else {
  501. dev_info(dev, "Using IPMMU context %u\n",
  502. domain->context_id);
  503. }
  504. } else if (domain->mmu != mmu) {
  505. /*
  506. * Something is wrong, we can't attach two devices using
  507. * different IOMMUs to the same domain.
  508. */
  509. ret = -EINVAL;
  510. } else
  511. dev_info(dev, "Reusing IPMMU context %u\n", domain->context_id);
  512. mutex_unlock(&domain->mutex);
  513. if (ret < 0)
  514. return ret;
  515. for (i = 0; i < fwspec->num_ids; ++i)
  516. ipmmu_utlb_enable(domain, fwspec->ids[i]);
  517. return 0;
  518. }
  519. static int ipmmu_iommu_identity_attach(struct iommu_domain *identity_domain,
  520. struct device *dev,
  521. struct iommu_domain *old)
  522. {
  523. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  524. struct ipmmu_vmsa_domain *domain;
  525. unsigned int i;
  526. if (old == identity_domain || !old)
  527. return 0;
  528. domain = to_vmsa_domain(old);
  529. for (i = 0; i < fwspec->num_ids; ++i)
  530. ipmmu_utlb_disable(domain, fwspec->ids[i]);
  531. /*
  532. * TODO: Optimize by disabling the context when no device is attached.
  533. */
  534. return 0;
  535. }
  536. static struct iommu_domain_ops ipmmu_iommu_identity_ops = {
  537. .attach_dev = ipmmu_iommu_identity_attach,
  538. };
  539. static struct iommu_domain ipmmu_iommu_identity_domain = {
  540. .type = IOMMU_DOMAIN_IDENTITY,
  541. .ops = &ipmmu_iommu_identity_ops,
  542. };
  543. static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova,
  544. phys_addr_t paddr, size_t pgsize, size_t pgcount,
  545. int prot, gfp_t gfp, size_t *mapped)
  546. {
  547. struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
  548. return domain->iop->map_pages(domain->iop, iova, paddr, pgsize, pgcount,
  549. prot, gfp, mapped);
  550. }
  551. static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova,
  552. size_t pgsize, size_t pgcount,
  553. struct iommu_iotlb_gather *gather)
  554. {
  555. struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
  556. return domain->iop->unmap_pages(domain->iop, iova, pgsize, pgcount, gather);
  557. }
  558. static void ipmmu_flush_iotlb_all(struct iommu_domain *io_domain)
  559. {
  560. struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
  561. if (domain->mmu)
  562. ipmmu_tlb_flush_all(domain);
  563. }
  564. static void ipmmu_iotlb_sync(struct iommu_domain *io_domain,
  565. struct iommu_iotlb_gather *gather)
  566. {
  567. ipmmu_flush_iotlb_all(io_domain);
  568. }
  569. static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain,
  570. dma_addr_t iova)
  571. {
  572. struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
  573. /* TODO: Is locking needed ? */
  574. return domain->iop->iova_to_phys(domain->iop, iova);
  575. }
  576. static int ipmmu_init_platform_device(struct device *dev,
  577. const struct of_phandle_args *args)
  578. {
  579. struct platform_device *ipmmu_pdev;
  580. ipmmu_pdev = of_find_device_by_node(args->np);
  581. if (!ipmmu_pdev)
  582. return -ENODEV;
  583. dev_iommu_priv_set(dev, platform_get_drvdata(ipmmu_pdev));
  584. put_device(&ipmmu_pdev->dev);
  585. return 0;
  586. }
  587. static const struct soc_device_attribute soc_needs_opt_in[] = {
  588. { .family = "R-Car Gen3", },
  589. { .family = "R-Car Gen4", },
  590. { .family = "RZ/G2", },
  591. { /* sentinel */ }
  592. };
  593. static const struct soc_device_attribute soc_denylist[] = {
  594. { .soc_id = "r8a774a1", },
  595. { .soc_id = "r8a7795", .revision = "ES2.*" },
  596. { .soc_id = "r8a7796", },
  597. { /* sentinel */ }
  598. };
  599. static const char * const devices_allowlist[] = {
  600. "ee100000.mmc",
  601. "ee120000.mmc",
  602. "ee140000.mmc",
  603. "ee160000.mmc"
  604. };
  605. static bool ipmmu_device_is_allowed(struct device *dev)
  606. {
  607. unsigned int i;
  608. /*
  609. * R-Car Gen3/4 and RZ/G2 use the allow list to opt-in devices.
  610. * For Other SoCs, this returns true anyway.
  611. */
  612. if (!soc_device_match(soc_needs_opt_in))
  613. return true;
  614. /* Check whether this SoC can use the IPMMU correctly or not */
  615. if (soc_device_match(soc_denylist))
  616. return false;
  617. /* Check whether this device is a PCI device */
  618. if (dev_is_pci(dev))
  619. return true;
  620. /* Check whether this device can work with the IPMMU */
  621. for (i = 0; i < ARRAY_SIZE(devices_allowlist); i++) {
  622. if (!strcmp(dev_name(dev), devices_allowlist[i]))
  623. return true;
  624. }
  625. /* Otherwise, do not allow use of IPMMU */
  626. return false;
  627. }
  628. static int ipmmu_of_xlate(struct device *dev,
  629. const struct of_phandle_args *spec)
  630. {
  631. if (!ipmmu_device_is_allowed(dev))
  632. return -ENODEV;
  633. iommu_fwspec_add_ids(dev, spec->args, 1);
  634. /* Initialize once - xlate() will call multiple times */
  635. if (to_ipmmu(dev))
  636. return 0;
  637. return ipmmu_init_platform_device(dev, spec);
  638. }
  639. static int ipmmu_init_arm_mapping(struct device *dev)
  640. {
  641. struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
  642. int ret;
  643. /*
  644. * Create the ARM mapping, used by the ARM DMA mapping core to allocate
  645. * VAs. This will allocate a corresponding IOMMU domain.
  646. *
  647. * TODO:
  648. * - Create one mapping per context (TLB).
  649. * - Make the mapping size configurable ? We currently use a 2GB mapping
  650. * at a 1GB offset to ensure that NULL VAs will fault.
  651. */
  652. if (!mmu->mapping) {
  653. struct dma_iommu_mapping *mapping;
  654. mapping = arm_iommu_create_mapping(dev, SZ_1G, SZ_2G);
  655. if (IS_ERR(mapping)) {
  656. dev_err(mmu->dev, "failed to create ARM IOMMU mapping\n");
  657. ret = PTR_ERR(mapping);
  658. goto error;
  659. }
  660. mmu->mapping = mapping;
  661. }
  662. /* Attach the ARM VA mapping to the device. */
  663. ret = arm_iommu_attach_device(dev, mmu->mapping);
  664. if (ret < 0) {
  665. dev_err(dev, "Failed to attach device to VA mapping\n");
  666. goto error;
  667. }
  668. return 0;
  669. error:
  670. if (mmu->mapping)
  671. arm_iommu_release_mapping(mmu->mapping);
  672. return ret;
  673. }
  674. static struct iommu_device *ipmmu_probe_device(struct device *dev)
  675. {
  676. struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
  677. /*
  678. * Only let through devices that have been verified in xlate()
  679. */
  680. if (!mmu)
  681. return ERR_PTR(-ENODEV);
  682. return &mmu->iommu;
  683. }
  684. static void ipmmu_probe_finalize(struct device *dev)
  685. {
  686. int ret = 0;
  687. if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_IOMMU_DMA))
  688. ret = ipmmu_init_arm_mapping(dev);
  689. if (ret)
  690. dev_err(dev, "Can't create IOMMU mapping - DMA-OPS will not work\n");
  691. }
  692. static void ipmmu_release_device(struct device *dev)
  693. {
  694. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  695. struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
  696. unsigned int i;
  697. for (i = 0; i < fwspec->num_ids; ++i) {
  698. unsigned int utlb = fwspec->ids[i];
  699. ipmmu_imuctr_write(mmu, utlb, 0);
  700. mmu->utlb_ctx[utlb] = IPMMU_CTX_INVALID;
  701. }
  702. arm_iommu_release_mapping(mmu->mapping);
  703. }
  704. static const struct iommu_ops ipmmu_ops = {
  705. .identity_domain = &ipmmu_iommu_identity_domain,
  706. .domain_alloc_paging = ipmmu_domain_alloc_paging,
  707. .probe_device = ipmmu_probe_device,
  708. .release_device = ipmmu_release_device,
  709. .probe_finalize = ipmmu_probe_finalize,
  710. /*
  711. * FIXME: The device grouping is a fixed property of the hardware's
  712. * ability to isolate and control DMA, it should not depend on kconfig.
  713. */
  714. .device_group = IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_IOMMU_DMA)
  715. ? generic_device_group : generic_single_device_group,
  716. .of_xlate = ipmmu_of_xlate,
  717. .default_domain_ops = &(const struct iommu_domain_ops) {
  718. .attach_dev = ipmmu_attach_device,
  719. .map_pages = ipmmu_map,
  720. .unmap_pages = ipmmu_unmap,
  721. .flush_iotlb_all = ipmmu_flush_iotlb_all,
  722. .iotlb_sync = ipmmu_iotlb_sync,
  723. .iova_to_phys = ipmmu_iova_to_phys,
  724. .free = ipmmu_domain_free,
  725. }
  726. };
  727. /* -----------------------------------------------------------------------------
  728. * Probe/remove and init
  729. */
  730. static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
  731. {
  732. unsigned int i;
  733. /* Disable all contexts. */
  734. for (i = 0; i < mmu->num_ctx; ++i)
  735. ipmmu_ctx_write(mmu, i, IMCTR, 0);
  736. }
  737. static const struct ipmmu_features ipmmu_features_default = {
  738. .use_ns_alias_offset = true,
  739. .has_cache_leaf_nodes = false,
  740. .number_of_contexts = 1, /* software only tested with one context */
  741. .num_utlbs = 32,
  742. .setup_imbuscr = true,
  743. .twobit_imttbcr_sl0 = false,
  744. .reserved_context = false,
  745. .cache_snoop = true,
  746. .ctx_offset_base = 0,
  747. .ctx_offset_stride = 0x40,
  748. .utlb_offset_base = 0,
  749. };
  750. static const struct ipmmu_features ipmmu_features_rcar_gen3 = {
  751. .use_ns_alias_offset = false,
  752. .has_cache_leaf_nodes = true,
  753. .number_of_contexts = 8,
  754. .num_utlbs = 48,
  755. .setup_imbuscr = false,
  756. .twobit_imttbcr_sl0 = true,
  757. .reserved_context = true,
  758. .cache_snoop = false,
  759. .ctx_offset_base = 0,
  760. .ctx_offset_stride = 0x40,
  761. .utlb_offset_base = 0,
  762. };
  763. static const struct ipmmu_features ipmmu_features_rcar_gen4 = {
  764. .use_ns_alias_offset = false,
  765. .has_cache_leaf_nodes = true,
  766. .number_of_contexts = 16,
  767. .num_utlbs = 64,
  768. .setup_imbuscr = false,
  769. .twobit_imttbcr_sl0 = true,
  770. .reserved_context = true,
  771. .cache_snoop = false,
  772. .ctx_offset_base = 0x10000,
  773. .ctx_offset_stride = 0x1040,
  774. .utlb_offset_base = 0x3000,
  775. };
  776. static const struct of_device_id ipmmu_of_ids[] = {
  777. {
  778. .compatible = "renesas,ipmmu-vmsa",
  779. .data = &ipmmu_features_default,
  780. }, {
  781. .compatible = "renesas,ipmmu-r8a774a1",
  782. .data = &ipmmu_features_rcar_gen3,
  783. }, {
  784. .compatible = "renesas,ipmmu-r8a774b1",
  785. .data = &ipmmu_features_rcar_gen3,
  786. }, {
  787. .compatible = "renesas,ipmmu-r8a774c0",
  788. .data = &ipmmu_features_rcar_gen3,
  789. }, {
  790. .compatible = "renesas,ipmmu-r8a774e1",
  791. .data = &ipmmu_features_rcar_gen3,
  792. }, {
  793. .compatible = "renesas,ipmmu-r8a7795",
  794. .data = &ipmmu_features_rcar_gen3,
  795. }, {
  796. .compatible = "renesas,ipmmu-r8a7796",
  797. .data = &ipmmu_features_rcar_gen3,
  798. }, {
  799. .compatible = "renesas,ipmmu-r8a77961",
  800. .data = &ipmmu_features_rcar_gen3,
  801. }, {
  802. .compatible = "renesas,ipmmu-r8a77965",
  803. .data = &ipmmu_features_rcar_gen3,
  804. }, {
  805. .compatible = "renesas,ipmmu-r8a77970",
  806. .data = &ipmmu_features_rcar_gen3,
  807. }, {
  808. .compatible = "renesas,ipmmu-r8a77980",
  809. .data = &ipmmu_features_rcar_gen3,
  810. }, {
  811. .compatible = "renesas,ipmmu-r8a77990",
  812. .data = &ipmmu_features_rcar_gen3,
  813. }, {
  814. .compatible = "renesas,ipmmu-r8a77995",
  815. .data = &ipmmu_features_rcar_gen3,
  816. }, {
  817. .compatible = "renesas,ipmmu-r8a779a0",
  818. .data = &ipmmu_features_rcar_gen4,
  819. }, {
  820. .compatible = "renesas,rcar-gen4-ipmmu-vmsa",
  821. .data = &ipmmu_features_rcar_gen4,
  822. }, {
  823. /* Terminator */
  824. },
  825. };
  826. static int ipmmu_probe(struct platform_device *pdev)
  827. {
  828. struct ipmmu_vmsa_device *mmu;
  829. int irq;
  830. int ret;
  831. mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL);
  832. if (!mmu) {
  833. dev_err(&pdev->dev, "cannot allocate device data\n");
  834. return -ENOMEM;
  835. }
  836. mmu->dev = &pdev->dev;
  837. spin_lock_init(&mmu->lock);
  838. bitmap_zero(mmu->ctx, IPMMU_CTX_MAX);
  839. mmu->features = of_device_get_match_data(&pdev->dev);
  840. memset(mmu->utlb_ctx, IPMMU_CTX_INVALID, mmu->features->num_utlbs);
  841. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40));
  842. if (ret)
  843. return ret;
  844. /* Map I/O memory and request IRQ. */
  845. mmu->base = devm_platform_ioremap_resource(pdev, 0);
  846. if (IS_ERR(mmu->base))
  847. return PTR_ERR(mmu->base);
  848. /*
  849. * The IPMMU has two register banks, for secure and non-secure modes.
  850. * The bank mapped at the beginning of the IPMMU address space
  851. * corresponds to the running mode of the CPU. When running in secure
  852. * mode the non-secure register bank is also available at an offset.
  853. *
  854. * Secure mode operation isn't clearly documented and is thus currently
  855. * not implemented in the driver. Furthermore, preliminary tests of
  856. * non-secure operation with the main register bank were not successful.
  857. * Offset the registers base unconditionally to point to the non-secure
  858. * alias space for now.
  859. */
  860. if (mmu->features->use_ns_alias_offset)
  861. mmu->base += IM_NS_ALIAS_OFFSET;
  862. mmu->num_ctx = min(IPMMU_CTX_MAX, mmu->features->number_of_contexts);
  863. /*
  864. * Determine if this IPMMU instance is a root device by checking for
  865. * the lack of has_cache_leaf_nodes flag or renesas,ipmmu-main property.
  866. */
  867. if (!mmu->features->has_cache_leaf_nodes ||
  868. !of_property_present(pdev->dev.of_node, "renesas,ipmmu-main"))
  869. mmu->root = mmu;
  870. else
  871. mmu->root = ipmmu_find_root();
  872. /*
  873. * Wait until the root device has been registered for sure.
  874. */
  875. if (!mmu->root)
  876. return -EPROBE_DEFER;
  877. /* Root devices have mandatory IRQs */
  878. if (ipmmu_is_root(mmu)) {
  879. irq = platform_get_irq(pdev, 0);
  880. if (irq < 0)
  881. return irq;
  882. ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0,
  883. dev_name(&pdev->dev), mmu);
  884. if (ret < 0) {
  885. dev_err(&pdev->dev, "failed to request IRQ %d\n", irq);
  886. return ret;
  887. }
  888. ipmmu_device_reset(mmu);
  889. if (mmu->features->reserved_context) {
  890. dev_info(&pdev->dev, "IPMMU context 0 is reserved\n");
  891. set_bit(0, mmu->ctx);
  892. }
  893. }
  894. platform_set_drvdata(pdev, mmu);
  895. /*
  896. * Register the IPMMU to the IOMMU subsystem in the following cases:
  897. * - R-Car Gen2 IPMMU (all devices registered)
  898. * - R-Car Gen3 IPMMU (leaf devices only - skip root IPMMU-MM device)
  899. */
  900. if (mmu->features->has_cache_leaf_nodes && ipmmu_is_root(mmu))
  901. return 0;
  902. ret = iommu_device_sysfs_add(&mmu->iommu, &pdev->dev, NULL, "%s",
  903. dev_name(&pdev->dev));
  904. if (ret)
  905. return ret;
  906. ret = iommu_device_register(&mmu->iommu, &ipmmu_ops, &pdev->dev);
  907. if (ret)
  908. iommu_device_sysfs_remove(&mmu->iommu);
  909. return ret;
  910. }
  911. static void ipmmu_remove(struct platform_device *pdev)
  912. {
  913. struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev);
  914. iommu_device_sysfs_remove(&mmu->iommu);
  915. iommu_device_unregister(&mmu->iommu);
  916. arm_iommu_release_mapping(mmu->mapping);
  917. ipmmu_device_reset(mmu);
  918. }
  919. static int ipmmu_resume_noirq(struct device *dev)
  920. {
  921. struct ipmmu_vmsa_device *mmu = dev_get_drvdata(dev);
  922. unsigned int i;
  923. /* Reset root MMU and restore contexts */
  924. if (ipmmu_is_root(mmu)) {
  925. ipmmu_device_reset(mmu);
  926. for (i = 0; i < mmu->num_ctx; i++) {
  927. if (!mmu->domains[i])
  928. continue;
  929. ipmmu_domain_setup_context(mmu->domains[i]);
  930. }
  931. }
  932. /* Re-enable active micro-TLBs */
  933. for (i = 0; i < mmu->features->num_utlbs; i++) {
  934. if (mmu->utlb_ctx[i] == IPMMU_CTX_INVALID)
  935. continue;
  936. ipmmu_utlb_enable(mmu->root->domains[mmu->utlb_ctx[i]], i);
  937. }
  938. return 0;
  939. }
  940. static const struct dev_pm_ops ipmmu_pm = {
  941. NOIRQ_SYSTEM_SLEEP_PM_OPS(NULL, ipmmu_resume_noirq)
  942. };
  943. static struct platform_driver ipmmu_driver = {
  944. .driver = {
  945. .name = "ipmmu-vmsa",
  946. .of_match_table = ipmmu_of_ids,
  947. .pm = pm_sleep_ptr(&ipmmu_pm),
  948. },
  949. .probe = ipmmu_probe,
  950. .remove = ipmmu_remove,
  951. };
  952. builtin_platform_driver(ipmmu_driver);