ipmmu-vmsa.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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 = SZ_1G | SZ_2M | SZ_4K;
  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(sizeof(*domain), GFP_KERNEL);
  463. if (!domain)
  464. return NULL;
  465. mutex_init(&domain->mutex);
  466. return &domain->io_domain;
  467. }
  468. static void ipmmu_domain_free(struct iommu_domain *io_domain)
  469. {
  470. struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
  471. /*
  472. * Free the domain resources. We assume that all devices have already
  473. * been detached.
  474. */
  475. ipmmu_domain_destroy_context(domain);
  476. free_io_pgtable_ops(domain->iop);
  477. kfree(domain);
  478. }
  479. static int ipmmu_attach_device(struct iommu_domain *io_domain,
  480. struct device *dev)
  481. {
  482. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  483. struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
  484. struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
  485. unsigned int i;
  486. int ret = 0;
  487. if (!mmu) {
  488. dev_err(dev, "Cannot attach to IPMMU\n");
  489. return -ENXIO;
  490. }
  491. mutex_lock(&domain->mutex);
  492. if (!domain->mmu) {
  493. /* The domain hasn't been used yet, initialize it. */
  494. domain->mmu = mmu;
  495. ret = ipmmu_domain_init_context(domain);
  496. if (ret < 0) {
  497. dev_err(dev, "Unable to initialize IPMMU context\n");
  498. domain->mmu = NULL;
  499. } else {
  500. dev_info(dev, "Using IPMMU context %u\n",
  501. domain->context_id);
  502. }
  503. } else if (domain->mmu != mmu) {
  504. /*
  505. * Something is wrong, we can't attach two devices using
  506. * different IOMMUs to the same domain.
  507. */
  508. ret = -EINVAL;
  509. } else
  510. dev_info(dev, "Reusing IPMMU context %u\n", domain->context_id);
  511. mutex_unlock(&domain->mutex);
  512. if (ret < 0)
  513. return ret;
  514. for (i = 0; i < fwspec->num_ids; ++i)
  515. ipmmu_utlb_enable(domain, fwspec->ids[i]);
  516. return 0;
  517. }
  518. static int ipmmu_iommu_identity_attach(struct iommu_domain *identity_domain,
  519. struct device *dev)
  520. {
  521. struct iommu_domain *io_domain = iommu_get_domain_for_dev(dev);
  522. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  523. struct ipmmu_vmsa_domain *domain;
  524. unsigned int i;
  525. if (io_domain == identity_domain || !io_domain)
  526. return 0;
  527. domain = to_vmsa_domain(io_domain);
  528. for (i = 0; i < fwspec->num_ids; ++i)
  529. ipmmu_utlb_disable(domain, fwspec->ids[i]);
  530. /*
  531. * TODO: Optimize by disabling the context when no device is attached.
  532. */
  533. return 0;
  534. }
  535. static struct iommu_domain_ops ipmmu_iommu_identity_ops = {
  536. .attach_dev = ipmmu_iommu_identity_attach,
  537. };
  538. static struct iommu_domain ipmmu_iommu_identity_domain = {
  539. .type = IOMMU_DOMAIN_IDENTITY,
  540. .ops = &ipmmu_iommu_identity_ops,
  541. };
  542. static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova,
  543. phys_addr_t paddr, size_t pgsize, size_t pgcount,
  544. int prot, gfp_t gfp, size_t *mapped)
  545. {
  546. struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
  547. return domain->iop->map_pages(domain->iop, iova, paddr, pgsize, pgcount,
  548. prot, gfp, mapped);
  549. }
  550. static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova,
  551. size_t pgsize, size_t pgcount,
  552. struct iommu_iotlb_gather *gather)
  553. {
  554. struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
  555. return domain->iop->unmap_pages(domain->iop, iova, pgsize, pgcount, gather);
  556. }
  557. static void ipmmu_flush_iotlb_all(struct iommu_domain *io_domain)
  558. {
  559. struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
  560. if (domain->mmu)
  561. ipmmu_tlb_flush_all(domain);
  562. }
  563. static void ipmmu_iotlb_sync(struct iommu_domain *io_domain,
  564. struct iommu_iotlb_gather *gather)
  565. {
  566. ipmmu_flush_iotlb_all(io_domain);
  567. }
  568. static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain,
  569. dma_addr_t iova)
  570. {
  571. struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
  572. /* TODO: Is locking needed ? */
  573. return domain->iop->iova_to_phys(domain->iop, iova);
  574. }
  575. static int ipmmu_init_platform_device(struct device *dev,
  576. const struct of_phandle_args *args)
  577. {
  578. struct platform_device *ipmmu_pdev;
  579. ipmmu_pdev = of_find_device_by_node(args->np);
  580. if (!ipmmu_pdev)
  581. return -ENODEV;
  582. dev_iommu_priv_set(dev, platform_get_drvdata(ipmmu_pdev));
  583. return 0;
  584. }
  585. static const struct soc_device_attribute soc_needs_opt_in[] = {
  586. { .family = "R-Car Gen3", },
  587. { .family = "R-Car Gen4", },
  588. { .family = "RZ/G2", },
  589. { /* sentinel */ }
  590. };
  591. static const struct soc_device_attribute soc_denylist[] = {
  592. { .soc_id = "r8a774a1", },
  593. { .soc_id = "r8a7795", .revision = "ES2.*" },
  594. { .soc_id = "r8a7796", },
  595. { /* sentinel */ }
  596. };
  597. static const char * const devices_allowlist[] = {
  598. "ee100000.mmc",
  599. "ee120000.mmc",
  600. "ee140000.mmc",
  601. "ee160000.mmc"
  602. };
  603. static bool ipmmu_device_is_allowed(struct device *dev)
  604. {
  605. unsigned int i;
  606. /*
  607. * R-Car Gen3/4 and RZ/G2 use the allow list to opt-in devices.
  608. * For Other SoCs, this returns true anyway.
  609. */
  610. if (!soc_device_match(soc_needs_opt_in))
  611. return true;
  612. /* Check whether this SoC can use the IPMMU correctly or not */
  613. if (soc_device_match(soc_denylist))
  614. return false;
  615. /* Check whether this device is a PCI device */
  616. if (dev_is_pci(dev))
  617. return true;
  618. /* Check whether this device can work with the IPMMU */
  619. for (i = 0; i < ARRAY_SIZE(devices_allowlist); i++) {
  620. if (!strcmp(dev_name(dev), devices_allowlist[i]))
  621. return true;
  622. }
  623. /* Otherwise, do not allow use of IPMMU */
  624. return false;
  625. }
  626. static int ipmmu_of_xlate(struct device *dev,
  627. const struct of_phandle_args *spec)
  628. {
  629. if (!ipmmu_device_is_allowed(dev))
  630. return -ENODEV;
  631. iommu_fwspec_add_ids(dev, spec->args, 1);
  632. /* Initialize once - xlate() will call multiple times */
  633. if (to_ipmmu(dev))
  634. return 0;
  635. return ipmmu_init_platform_device(dev, spec);
  636. }
  637. static int ipmmu_init_arm_mapping(struct device *dev)
  638. {
  639. struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
  640. int ret;
  641. /*
  642. * Create the ARM mapping, used by the ARM DMA mapping core to allocate
  643. * VAs. This will allocate a corresponding IOMMU domain.
  644. *
  645. * TODO:
  646. * - Create one mapping per context (TLB).
  647. * - Make the mapping size configurable ? We currently use a 2GB mapping
  648. * at a 1GB offset to ensure that NULL VAs will fault.
  649. */
  650. if (!mmu->mapping) {
  651. struct dma_iommu_mapping *mapping;
  652. mapping = arm_iommu_create_mapping(dev, SZ_1G, SZ_2G);
  653. if (IS_ERR(mapping)) {
  654. dev_err(mmu->dev, "failed to create ARM IOMMU mapping\n");
  655. ret = PTR_ERR(mapping);
  656. goto error;
  657. }
  658. mmu->mapping = mapping;
  659. }
  660. /* Attach the ARM VA mapping to the device. */
  661. ret = arm_iommu_attach_device(dev, mmu->mapping);
  662. if (ret < 0) {
  663. dev_err(dev, "Failed to attach device to VA mapping\n");
  664. goto error;
  665. }
  666. return 0;
  667. error:
  668. if (mmu->mapping)
  669. arm_iommu_release_mapping(mmu->mapping);
  670. return ret;
  671. }
  672. static struct iommu_device *ipmmu_probe_device(struct device *dev)
  673. {
  674. struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
  675. /*
  676. * Only let through devices that have been verified in xlate()
  677. */
  678. if (!mmu)
  679. return ERR_PTR(-ENODEV);
  680. return &mmu->iommu;
  681. }
  682. static void ipmmu_probe_finalize(struct device *dev)
  683. {
  684. int ret = 0;
  685. if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_IOMMU_DMA))
  686. ret = ipmmu_init_arm_mapping(dev);
  687. if (ret)
  688. dev_err(dev, "Can't create IOMMU mapping - DMA-OPS will not work\n");
  689. }
  690. static void ipmmu_release_device(struct device *dev)
  691. {
  692. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  693. struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
  694. unsigned int i;
  695. for (i = 0; i < fwspec->num_ids; ++i) {
  696. unsigned int utlb = fwspec->ids[i];
  697. ipmmu_imuctr_write(mmu, utlb, 0);
  698. mmu->utlb_ctx[utlb] = IPMMU_CTX_INVALID;
  699. }
  700. arm_iommu_release_mapping(mmu->mapping);
  701. }
  702. static const struct iommu_ops ipmmu_ops = {
  703. .identity_domain = &ipmmu_iommu_identity_domain,
  704. .domain_alloc_paging = ipmmu_domain_alloc_paging,
  705. .probe_device = ipmmu_probe_device,
  706. .release_device = ipmmu_release_device,
  707. .probe_finalize = ipmmu_probe_finalize,
  708. /*
  709. * FIXME: The device grouping is a fixed property of the hardware's
  710. * ability to isolate and control DMA, it should not depend on kconfig.
  711. */
  712. .device_group = IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_IOMMU_DMA)
  713. ? generic_device_group : generic_single_device_group,
  714. .pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K,
  715. .of_xlate = ipmmu_of_xlate,
  716. .default_domain_ops = &(const struct iommu_domain_ops) {
  717. .attach_dev = ipmmu_attach_device,
  718. .map_pages = ipmmu_map,
  719. .unmap_pages = ipmmu_unmap,
  720. .flush_iotlb_all = ipmmu_flush_iotlb_all,
  721. .iotlb_sync = ipmmu_iotlb_sync,
  722. .iova_to_phys = ipmmu_iova_to_phys,
  723. .free = ipmmu_domain_free,
  724. }
  725. };
  726. /* -----------------------------------------------------------------------------
  727. * Probe/remove and init
  728. */
  729. static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
  730. {
  731. unsigned int i;
  732. /* Disable all contexts. */
  733. for (i = 0; i < mmu->num_ctx; ++i)
  734. ipmmu_ctx_write(mmu, i, IMCTR, 0);
  735. }
  736. static const struct ipmmu_features ipmmu_features_default = {
  737. .use_ns_alias_offset = true,
  738. .has_cache_leaf_nodes = false,
  739. .number_of_contexts = 1, /* software only tested with one context */
  740. .num_utlbs = 32,
  741. .setup_imbuscr = true,
  742. .twobit_imttbcr_sl0 = false,
  743. .reserved_context = false,
  744. .cache_snoop = true,
  745. .ctx_offset_base = 0,
  746. .ctx_offset_stride = 0x40,
  747. .utlb_offset_base = 0,
  748. };
  749. static const struct ipmmu_features ipmmu_features_rcar_gen3 = {
  750. .use_ns_alias_offset = false,
  751. .has_cache_leaf_nodes = true,
  752. .number_of_contexts = 8,
  753. .num_utlbs = 48,
  754. .setup_imbuscr = false,
  755. .twobit_imttbcr_sl0 = true,
  756. .reserved_context = true,
  757. .cache_snoop = false,
  758. .ctx_offset_base = 0,
  759. .ctx_offset_stride = 0x40,
  760. .utlb_offset_base = 0,
  761. };
  762. static const struct ipmmu_features ipmmu_features_rcar_gen4 = {
  763. .use_ns_alias_offset = false,
  764. .has_cache_leaf_nodes = true,
  765. .number_of_contexts = 16,
  766. .num_utlbs = 64,
  767. .setup_imbuscr = false,
  768. .twobit_imttbcr_sl0 = true,
  769. .reserved_context = true,
  770. .cache_snoop = false,
  771. .ctx_offset_base = 0x10000,
  772. .ctx_offset_stride = 0x1040,
  773. .utlb_offset_base = 0x3000,
  774. };
  775. static const struct of_device_id ipmmu_of_ids[] = {
  776. {
  777. .compatible = "renesas,ipmmu-vmsa",
  778. .data = &ipmmu_features_default,
  779. }, {
  780. .compatible = "renesas,ipmmu-r8a774a1",
  781. .data = &ipmmu_features_rcar_gen3,
  782. }, {
  783. .compatible = "renesas,ipmmu-r8a774b1",
  784. .data = &ipmmu_features_rcar_gen3,
  785. }, {
  786. .compatible = "renesas,ipmmu-r8a774c0",
  787. .data = &ipmmu_features_rcar_gen3,
  788. }, {
  789. .compatible = "renesas,ipmmu-r8a774e1",
  790. .data = &ipmmu_features_rcar_gen3,
  791. }, {
  792. .compatible = "renesas,ipmmu-r8a7795",
  793. .data = &ipmmu_features_rcar_gen3,
  794. }, {
  795. .compatible = "renesas,ipmmu-r8a7796",
  796. .data = &ipmmu_features_rcar_gen3,
  797. }, {
  798. .compatible = "renesas,ipmmu-r8a77961",
  799. .data = &ipmmu_features_rcar_gen3,
  800. }, {
  801. .compatible = "renesas,ipmmu-r8a77965",
  802. .data = &ipmmu_features_rcar_gen3,
  803. }, {
  804. .compatible = "renesas,ipmmu-r8a77970",
  805. .data = &ipmmu_features_rcar_gen3,
  806. }, {
  807. .compatible = "renesas,ipmmu-r8a77980",
  808. .data = &ipmmu_features_rcar_gen3,
  809. }, {
  810. .compatible = "renesas,ipmmu-r8a77990",
  811. .data = &ipmmu_features_rcar_gen3,
  812. }, {
  813. .compatible = "renesas,ipmmu-r8a77995",
  814. .data = &ipmmu_features_rcar_gen3,
  815. }, {
  816. .compatible = "renesas,ipmmu-r8a779a0",
  817. .data = &ipmmu_features_rcar_gen4,
  818. }, {
  819. .compatible = "renesas,rcar-gen4-ipmmu-vmsa",
  820. .data = &ipmmu_features_rcar_gen4,
  821. }, {
  822. /* Terminator */
  823. },
  824. };
  825. static int ipmmu_probe(struct platform_device *pdev)
  826. {
  827. struct ipmmu_vmsa_device *mmu;
  828. int irq;
  829. int ret;
  830. mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL);
  831. if (!mmu) {
  832. dev_err(&pdev->dev, "cannot allocate device data\n");
  833. return -ENOMEM;
  834. }
  835. mmu->dev = &pdev->dev;
  836. spin_lock_init(&mmu->lock);
  837. bitmap_zero(mmu->ctx, IPMMU_CTX_MAX);
  838. mmu->features = of_device_get_match_data(&pdev->dev);
  839. memset(mmu->utlb_ctx, IPMMU_CTX_INVALID, mmu->features->num_utlbs);
  840. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40));
  841. if (ret)
  842. return ret;
  843. /* Map I/O memory and request IRQ. */
  844. mmu->base = devm_platform_ioremap_resource(pdev, 0);
  845. if (IS_ERR(mmu->base))
  846. return PTR_ERR(mmu->base);
  847. /*
  848. * The IPMMU has two register banks, for secure and non-secure modes.
  849. * The bank mapped at the beginning of the IPMMU address space
  850. * corresponds to the running mode of the CPU. When running in secure
  851. * mode the non-secure register bank is also available at an offset.
  852. *
  853. * Secure mode operation isn't clearly documented and is thus currently
  854. * not implemented in the driver. Furthermore, preliminary tests of
  855. * non-secure operation with the main register bank were not successful.
  856. * Offset the registers base unconditionally to point to the non-secure
  857. * alias space for now.
  858. */
  859. if (mmu->features->use_ns_alias_offset)
  860. mmu->base += IM_NS_ALIAS_OFFSET;
  861. mmu->num_ctx = min(IPMMU_CTX_MAX, mmu->features->number_of_contexts);
  862. /*
  863. * Determine if this IPMMU instance is a root device by checking for
  864. * the lack of has_cache_leaf_nodes flag or renesas,ipmmu-main property.
  865. */
  866. if (!mmu->features->has_cache_leaf_nodes ||
  867. !of_property_present(pdev->dev.of_node, "renesas,ipmmu-main"))
  868. mmu->root = mmu;
  869. else
  870. mmu->root = ipmmu_find_root();
  871. /*
  872. * Wait until the root device has been registered for sure.
  873. */
  874. if (!mmu->root)
  875. return -EPROBE_DEFER;
  876. /* Root devices have mandatory IRQs */
  877. if (ipmmu_is_root(mmu)) {
  878. irq = platform_get_irq(pdev, 0);
  879. if (irq < 0)
  880. return irq;
  881. ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0,
  882. dev_name(&pdev->dev), mmu);
  883. if (ret < 0) {
  884. dev_err(&pdev->dev, "failed to request IRQ %d\n", irq);
  885. return ret;
  886. }
  887. ipmmu_device_reset(mmu);
  888. if (mmu->features->reserved_context) {
  889. dev_info(&pdev->dev, "IPMMU context 0 is reserved\n");
  890. set_bit(0, mmu->ctx);
  891. }
  892. }
  893. /*
  894. * Register the IPMMU to the IOMMU subsystem in the following cases:
  895. * - R-Car Gen2 IPMMU (all devices registered)
  896. * - R-Car Gen3 IPMMU (leaf devices only - skip root IPMMU-MM device)
  897. */
  898. if (!mmu->features->has_cache_leaf_nodes || !ipmmu_is_root(mmu)) {
  899. ret = iommu_device_sysfs_add(&mmu->iommu, &pdev->dev, NULL,
  900. dev_name(&pdev->dev));
  901. if (ret)
  902. return ret;
  903. ret = iommu_device_register(&mmu->iommu, &ipmmu_ops, &pdev->dev);
  904. if (ret)
  905. return ret;
  906. }
  907. /*
  908. * We can't create the ARM mapping here as it requires the bus to have
  909. * an IOMMU, which only happens when bus_set_iommu() is called in
  910. * ipmmu_init() after the probe function returns.
  911. */
  912. platform_set_drvdata(pdev, mmu);
  913. return 0;
  914. }
  915. static void ipmmu_remove(struct platform_device *pdev)
  916. {
  917. struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev);
  918. iommu_device_sysfs_remove(&mmu->iommu);
  919. iommu_device_unregister(&mmu->iommu);
  920. arm_iommu_release_mapping(mmu->mapping);
  921. ipmmu_device_reset(mmu);
  922. }
  923. static int ipmmu_resume_noirq(struct device *dev)
  924. {
  925. struct ipmmu_vmsa_device *mmu = dev_get_drvdata(dev);
  926. unsigned int i;
  927. /* Reset root MMU and restore contexts */
  928. if (ipmmu_is_root(mmu)) {
  929. ipmmu_device_reset(mmu);
  930. for (i = 0; i < mmu->num_ctx; i++) {
  931. if (!mmu->domains[i])
  932. continue;
  933. ipmmu_domain_setup_context(mmu->domains[i]);
  934. }
  935. }
  936. /* Re-enable active micro-TLBs */
  937. for (i = 0; i < mmu->features->num_utlbs; i++) {
  938. if (mmu->utlb_ctx[i] == IPMMU_CTX_INVALID)
  939. continue;
  940. ipmmu_utlb_enable(mmu->root->domains[mmu->utlb_ctx[i]], i);
  941. }
  942. return 0;
  943. }
  944. static const struct dev_pm_ops ipmmu_pm = {
  945. NOIRQ_SYSTEM_SLEEP_PM_OPS(NULL, ipmmu_resume_noirq)
  946. };
  947. static struct platform_driver ipmmu_driver = {
  948. .driver = {
  949. .name = "ipmmu-vmsa",
  950. .of_match_table = ipmmu_of_ids,
  951. .pm = pm_sleep_ptr(&ipmmu_pm),
  952. },
  953. .probe = ipmmu_probe,
  954. .remove_new = ipmmu_remove,
  955. };
  956. builtin_platform_driver(ipmmu_driver);