ipmmu-vmsa.c 29 KB

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