ipmmu-vmsa.c 29 KB

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