ipmmu-vmsa.c 30 KB

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