ipmmu-vmsa.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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. /*
  423. * Disable the context. Flush the TLB as required when modifying the
  424. * context registers.
  425. *
  426. * TODO: Is TLB flush really needed ?
  427. */
  428. ipmmu_ctx_write_all(domain, IMCTR, IMCTR_FLUSH);
  429. ipmmu_tlb_sync(domain);
  430. ipmmu_domain_free_context(domain->mmu->root, domain->context_id);
  431. }
  432. /* -----------------------------------------------------------------------------
  433. * Fault Handling
  434. */
  435. static irqreturn_t ipmmu_domain_irq(struct ipmmu_vmsa_domain *domain)
  436. {
  437. const u32 err_mask = IMSTR_MHIT | IMSTR_ABORT | IMSTR_PF | IMSTR_TF;
  438. struct ipmmu_vmsa_device *mmu = domain->mmu;
  439. u32 status;
  440. u32 iova;
  441. status = ipmmu_ctx_read_root(domain, IMSTR);
  442. if (!(status & err_mask))
  443. return IRQ_NONE;
  444. iova = ipmmu_ctx_read_root(domain, IMEAR);
  445. /*
  446. * Clear the error status flags. Unlike traditional interrupt flag
  447. * registers that must be cleared by writing 1, this status register
  448. * seems to require 0. The error address register must be read before,
  449. * otherwise its value will be 0.
  450. */
  451. ipmmu_ctx_write_root(domain, IMSTR, 0);
  452. /* Log fatal errors. */
  453. if (status & IMSTR_MHIT)
  454. dev_err_ratelimited(mmu->dev, "Multiple TLB hits @0x%08x\n",
  455. iova);
  456. if (status & IMSTR_ABORT)
  457. dev_err_ratelimited(mmu->dev, "Page Table Walk Abort @0x%08x\n",
  458. iova);
  459. if (!(status & (IMSTR_PF | IMSTR_TF)))
  460. return IRQ_NONE;
  461. /*
  462. * Try to handle page faults and translation faults.
  463. *
  464. * TODO: We need to look up the faulty device based on the I/O VA. Use
  465. * the IOMMU device for now.
  466. */
  467. if (!report_iommu_fault(&domain->io_domain, mmu->dev, iova, 0))
  468. return IRQ_HANDLED;
  469. dev_err_ratelimited(mmu->dev,
  470. "Unhandled fault: status 0x%08x iova 0x%08x\n",
  471. status, iova);
  472. return IRQ_HANDLED;
  473. }
  474. static irqreturn_t ipmmu_irq(int irq, void *dev)
  475. {
  476. struct ipmmu_vmsa_device *mmu = dev;
  477. irqreturn_t status = IRQ_NONE;
  478. unsigned int i;
  479. unsigned long flags;
  480. spin_lock_irqsave(&mmu->lock, flags);
  481. /*
  482. * Check interrupts for all active contexts.
  483. */
  484. for (i = 0; i < mmu->num_ctx; i++) {
  485. if (!mmu->domains[i])
  486. continue;
  487. if (ipmmu_domain_irq(mmu->domains[i]) == IRQ_HANDLED)
  488. status = IRQ_HANDLED;
  489. }
  490. spin_unlock_irqrestore(&mmu->lock, flags);
  491. return status;
  492. }
  493. /* -----------------------------------------------------------------------------
  494. * IOMMU Operations
  495. */
  496. static struct iommu_domain *__ipmmu_domain_alloc(unsigned type)
  497. {
  498. struct ipmmu_vmsa_domain *domain;
  499. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  500. if (!domain)
  501. return NULL;
  502. mutex_init(&domain->mutex);
  503. return &domain->io_domain;
  504. }
  505. static struct iommu_domain *ipmmu_domain_alloc(unsigned type)
  506. {
  507. struct iommu_domain *io_domain = NULL;
  508. switch (type) {
  509. case IOMMU_DOMAIN_UNMANAGED:
  510. io_domain = __ipmmu_domain_alloc(type);
  511. break;
  512. case IOMMU_DOMAIN_DMA:
  513. io_domain = __ipmmu_domain_alloc(type);
  514. if (io_domain && iommu_get_dma_cookie(io_domain)) {
  515. kfree(io_domain);
  516. io_domain = NULL;
  517. }
  518. break;
  519. }
  520. return io_domain;
  521. }
  522. static void ipmmu_domain_free(struct iommu_domain *io_domain)
  523. {
  524. struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
  525. /*
  526. * Free the domain resources. We assume that all devices have already
  527. * been detached.
  528. */
  529. iommu_put_dma_cookie(io_domain);
  530. ipmmu_domain_destroy_context(domain);
  531. free_io_pgtable_ops(domain->iop);
  532. kfree(domain);
  533. }
  534. static int ipmmu_attach_device(struct iommu_domain *io_domain,
  535. struct device *dev)
  536. {
  537. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  538. struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
  539. struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
  540. unsigned int i;
  541. int ret = 0;
  542. if (!mmu) {
  543. dev_err(dev, "Cannot attach to IPMMU\n");
  544. return -ENXIO;
  545. }
  546. mutex_lock(&domain->mutex);
  547. if (!domain->mmu) {
  548. /* The domain hasn't been used yet, initialize it. */
  549. domain->mmu = mmu;
  550. ret = ipmmu_domain_init_context(domain);
  551. if (ret < 0) {
  552. dev_err(dev, "Unable to initialize IPMMU context\n");
  553. domain->mmu = NULL;
  554. } else {
  555. dev_info(dev, "Using IPMMU context %u\n",
  556. domain->context_id);
  557. }
  558. } else if (domain->mmu != mmu) {
  559. /*
  560. * Something is wrong, we can't attach two devices using
  561. * different IOMMUs to the same domain.
  562. */
  563. dev_err(dev, "Can't attach IPMMU %s to domain on IPMMU %s\n",
  564. dev_name(mmu->dev), dev_name(domain->mmu->dev));
  565. ret = -EINVAL;
  566. } else
  567. dev_info(dev, "Reusing IPMMU context %u\n", domain->context_id);
  568. mutex_unlock(&domain->mutex);
  569. if (ret < 0)
  570. return ret;
  571. for (i = 0; i < fwspec->num_ids; ++i)
  572. ipmmu_utlb_enable(domain, fwspec->ids[i]);
  573. return 0;
  574. }
  575. static void ipmmu_detach_device(struct iommu_domain *io_domain,
  576. struct device *dev)
  577. {
  578. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  579. struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
  580. unsigned int i;
  581. for (i = 0; i < fwspec->num_ids; ++i)
  582. ipmmu_utlb_disable(domain, fwspec->ids[i]);
  583. /*
  584. * TODO: Optimize by disabling the context when no device is attached.
  585. */
  586. }
  587. static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova,
  588. phys_addr_t paddr, size_t size, int prot)
  589. {
  590. struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
  591. if (!domain)
  592. return -ENODEV;
  593. return domain->iop->map(domain->iop, iova, paddr, size, prot);
  594. }
  595. static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova,
  596. size_t size)
  597. {
  598. struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
  599. return domain->iop->unmap(domain->iop, iova, size);
  600. }
  601. static void ipmmu_iotlb_sync(struct iommu_domain *io_domain)
  602. {
  603. struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
  604. if (domain->mmu)
  605. ipmmu_tlb_flush_all(domain);
  606. }
  607. static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain,
  608. dma_addr_t iova)
  609. {
  610. struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
  611. /* TODO: Is locking needed ? */
  612. return domain->iop->iova_to_phys(domain->iop, iova);
  613. }
  614. static int ipmmu_init_platform_device(struct device *dev,
  615. struct of_phandle_args *args)
  616. {
  617. struct platform_device *ipmmu_pdev;
  618. ipmmu_pdev = of_find_device_by_node(args->np);
  619. if (!ipmmu_pdev)
  620. return -ENODEV;
  621. dev->iommu_fwspec->iommu_priv = platform_get_drvdata(ipmmu_pdev);
  622. return 0;
  623. }
  624. static bool ipmmu_slave_whitelist(struct device *dev)
  625. {
  626. /* By default, do not allow use of IPMMU */
  627. return false;
  628. }
  629. static const struct soc_device_attribute soc_rcar_gen3[] = {
  630. { .soc_id = "r8a7795", },
  631. { .soc_id = "r8a7796", },
  632. { .soc_id = "r8a77965", },
  633. { .soc_id = "r8a77970", },
  634. { .soc_id = "r8a77995", },
  635. { /* sentinel */ }
  636. };
  637. static int ipmmu_of_xlate(struct device *dev,
  638. struct of_phandle_args *spec)
  639. {
  640. /* For R-Car Gen3 use a white list to opt-in slave devices */
  641. if (soc_device_match(soc_rcar_gen3) && !ipmmu_slave_whitelist(dev))
  642. return -ENODEV;
  643. iommu_fwspec_add_ids(dev, spec->args, 1);
  644. /* Initialize once - xlate() will call multiple times */
  645. if (to_ipmmu(dev))
  646. return 0;
  647. return ipmmu_init_platform_device(dev, spec);
  648. }
  649. static int ipmmu_init_arm_mapping(struct device *dev)
  650. {
  651. struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
  652. struct iommu_group *group;
  653. int ret;
  654. /* Create a device group and add the device to it. */
  655. group = iommu_group_alloc();
  656. if (IS_ERR(group)) {
  657. dev_err(dev, "Failed to allocate IOMMU group\n");
  658. return PTR_ERR(group);
  659. }
  660. ret = iommu_group_add_device(group, dev);
  661. iommu_group_put(group);
  662. if (ret < 0) {
  663. dev_err(dev, "Failed to add device to IPMMU group\n");
  664. return ret;
  665. }
  666. /*
  667. * Create the ARM mapping, used by the ARM DMA mapping core to allocate
  668. * VAs. This will allocate a corresponding IOMMU domain.
  669. *
  670. * TODO:
  671. * - Create one mapping per context (TLB).
  672. * - Make the mapping size configurable ? We currently use a 2GB mapping
  673. * at a 1GB offset to ensure that NULL VAs will fault.
  674. */
  675. if (!mmu->mapping) {
  676. struct dma_iommu_mapping *mapping;
  677. mapping = arm_iommu_create_mapping(&platform_bus_type,
  678. SZ_1G, SZ_2G);
  679. if (IS_ERR(mapping)) {
  680. dev_err(mmu->dev, "failed to create ARM IOMMU mapping\n");
  681. ret = PTR_ERR(mapping);
  682. goto error;
  683. }
  684. mmu->mapping = mapping;
  685. }
  686. /* Attach the ARM VA mapping to the device. */
  687. ret = arm_iommu_attach_device(dev, mmu->mapping);
  688. if (ret < 0) {
  689. dev_err(dev, "Failed to attach device to VA mapping\n");
  690. goto error;
  691. }
  692. return 0;
  693. error:
  694. iommu_group_remove_device(dev);
  695. if (mmu->mapping)
  696. arm_iommu_release_mapping(mmu->mapping);
  697. return ret;
  698. }
  699. static int ipmmu_add_device(struct device *dev)
  700. {
  701. struct iommu_group *group;
  702. /*
  703. * Only let through devices that have been verified in xlate()
  704. */
  705. if (!to_ipmmu(dev))
  706. return -ENODEV;
  707. if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_IOMMU_DMA))
  708. return ipmmu_init_arm_mapping(dev);
  709. group = iommu_group_get_for_dev(dev);
  710. if (IS_ERR(group))
  711. return PTR_ERR(group);
  712. iommu_group_put(group);
  713. return 0;
  714. }
  715. static void ipmmu_remove_device(struct device *dev)
  716. {
  717. arm_iommu_detach_device(dev);
  718. iommu_group_remove_device(dev);
  719. }
  720. static struct iommu_group *ipmmu_find_group(struct device *dev)
  721. {
  722. struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
  723. struct iommu_group *group;
  724. if (mmu->group)
  725. return iommu_group_ref_get(mmu->group);
  726. group = iommu_group_alloc();
  727. if (!IS_ERR(group))
  728. mmu->group = group;
  729. return group;
  730. }
  731. static const struct iommu_ops ipmmu_ops = {
  732. .domain_alloc = ipmmu_domain_alloc,
  733. .domain_free = ipmmu_domain_free,
  734. .attach_dev = ipmmu_attach_device,
  735. .detach_dev = ipmmu_detach_device,
  736. .map = ipmmu_map,
  737. .unmap = ipmmu_unmap,
  738. .flush_iotlb_all = ipmmu_iotlb_sync,
  739. .iotlb_sync = ipmmu_iotlb_sync,
  740. .iova_to_phys = ipmmu_iova_to_phys,
  741. .add_device = ipmmu_add_device,
  742. .remove_device = ipmmu_remove_device,
  743. .device_group = ipmmu_find_group,
  744. .pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K,
  745. .of_xlate = ipmmu_of_xlate,
  746. };
  747. /* -----------------------------------------------------------------------------
  748. * Probe/remove and init
  749. */
  750. static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
  751. {
  752. unsigned int i;
  753. /* Disable all contexts. */
  754. for (i = 0; i < mmu->num_ctx; ++i)
  755. ipmmu_write(mmu, i * IM_CTX_SIZE + IMCTR, 0);
  756. }
  757. static const struct ipmmu_features ipmmu_features_default = {
  758. .use_ns_alias_offset = true,
  759. .has_cache_leaf_nodes = false,
  760. .number_of_contexts = 1, /* software only tested with one context */
  761. .setup_imbuscr = true,
  762. .twobit_imttbcr_sl0 = false,
  763. .reserved_context = false,
  764. };
  765. static const struct ipmmu_features ipmmu_features_rcar_gen3 = {
  766. .use_ns_alias_offset = false,
  767. .has_cache_leaf_nodes = true,
  768. .number_of_contexts = 8,
  769. .setup_imbuscr = false,
  770. .twobit_imttbcr_sl0 = true,
  771. .reserved_context = true,
  772. };
  773. static const struct of_device_id ipmmu_of_ids[] = {
  774. {
  775. .compatible = "renesas,ipmmu-vmsa",
  776. .data = &ipmmu_features_default,
  777. }, {
  778. .compatible = "renesas,ipmmu-r8a7795",
  779. .data = &ipmmu_features_rcar_gen3,
  780. }, {
  781. .compatible = "renesas,ipmmu-r8a7796",
  782. .data = &ipmmu_features_rcar_gen3,
  783. }, {
  784. .compatible = "renesas,ipmmu-r8a77965",
  785. .data = &ipmmu_features_rcar_gen3,
  786. }, {
  787. .compatible = "renesas,ipmmu-r8a77970",
  788. .data = &ipmmu_features_rcar_gen3,
  789. }, {
  790. .compatible = "renesas,ipmmu-r8a77995",
  791. .data = &ipmmu_features_rcar_gen3,
  792. }, {
  793. /* Terminator */
  794. },
  795. };
  796. MODULE_DEVICE_TABLE(of, ipmmu_of_ids);
  797. static int ipmmu_probe(struct platform_device *pdev)
  798. {
  799. struct ipmmu_vmsa_device *mmu;
  800. struct resource *res;
  801. int irq;
  802. int ret;
  803. mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL);
  804. if (!mmu) {
  805. dev_err(&pdev->dev, "cannot allocate device data\n");
  806. return -ENOMEM;
  807. }
  808. mmu->dev = &pdev->dev;
  809. mmu->num_utlbs = 48;
  810. spin_lock_init(&mmu->lock);
  811. bitmap_zero(mmu->ctx, IPMMU_CTX_MAX);
  812. mmu->features = of_device_get_match_data(&pdev->dev);
  813. dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40));
  814. /* Map I/O memory and request IRQ. */
  815. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  816. mmu->base = devm_ioremap_resource(&pdev->dev, res);
  817. if (IS_ERR(mmu->base))
  818. return PTR_ERR(mmu->base);
  819. /*
  820. * The IPMMU has two register banks, for secure and non-secure modes.
  821. * The bank mapped at the beginning of the IPMMU address space
  822. * corresponds to the running mode of the CPU. When running in secure
  823. * mode the non-secure register bank is also available at an offset.
  824. *
  825. * Secure mode operation isn't clearly documented and is thus currently
  826. * not implemented in the driver. Furthermore, preliminary tests of
  827. * non-secure operation with the main register bank were not successful.
  828. * Offset the registers base unconditionally to point to the non-secure
  829. * alias space for now.
  830. */
  831. if (mmu->features->use_ns_alias_offset)
  832. mmu->base += IM_NS_ALIAS_OFFSET;
  833. mmu->num_ctx = min_t(unsigned int, IPMMU_CTX_MAX,
  834. mmu->features->number_of_contexts);
  835. irq = platform_get_irq(pdev, 0);
  836. /*
  837. * Determine if this IPMMU instance is a root device by checking for
  838. * the lack of has_cache_leaf_nodes flag or renesas,ipmmu-main property.
  839. */
  840. if (!mmu->features->has_cache_leaf_nodes ||
  841. !of_find_property(pdev->dev.of_node, "renesas,ipmmu-main", NULL))
  842. mmu->root = mmu;
  843. else
  844. mmu->root = ipmmu_find_root();
  845. /*
  846. * Wait until the root device has been registered for sure.
  847. */
  848. if (!mmu->root)
  849. return -EPROBE_DEFER;
  850. /* Root devices have mandatory IRQs */
  851. if (ipmmu_is_root(mmu)) {
  852. if (irq < 0) {
  853. dev_err(&pdev->dev, "no IRQ found\n");
  854. return irq;
  855. }
  856. ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0,
  857. dev_name(&pdev->dev), mmu);
  858. if (ret < 0) {
  859. dev_err(&pdev->dev, "failed to request IRQ %d\n", irq);
  860. return ret;
  861. }
  862. ipmmu_device_reset(mmu);
  863. if (mmu->features->reserved_context) {
  864. dev_info(&pdev->dev, "IPMMU context 0 is reserved\n");
  865. set_bit(0, mmu->ctx);
  866. }
  867. }
  868. /*
  869. * Register the IPMMU to the IOMMU subsystem in the following cases:
  870. * - R-Car Gen2 IPMMU (all devices registered)
  871. * - R-Car Gen3 IPMMU (leaf devices only - skip root IPMMU-MM device)
  872. */
  873. if (!mmu->features->has_cache_leaf_nodes || !ipmmu_is_root(mmu)) {
  874. ret = iommu_device_sysfs_add(&mmu->iommu, &pdev->dev, NULL,
  875. dev_name(&pdev->dev));
  876. if (ret)
  877. return ret;
  878. iommu_device_set_ops(&mmu->iommu, &ipmmu_ops);
  879. iommu_device_set_fwnode(&mmu->iommu,
  880. &pdev->dev.of_node->fwnode);
  881. ret = iommu_device_register(&mmu->iommu);
  882. if (ret)
  883. return ret;
  884. #if defined(CONFIG_IOMMU_DMA)
  885. if (!iommu_present(&platform_bus_type))
  886. bus_set_iommu(&platform_bus_type, &ipmmu_ops);
  887. #endif
  888. }
  889. /*
  890. * We can't create the ARM mapping here as it requires the bus to have
  891. * an IOMMU, which only happens when bus_set_iommu() is called in
  892. * ipmmu_init() after the probe function returns.
  893. */
  894. platform_set_drvdata(pdev, mmu);
  895. return 0;
  896. }
  897. static int ipmmu_remove(struct platform_device *pdev)
  898. {
  899. struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev);
  900. iommu_device_sysfs_remove(&mmu->iommu);
  901. iommu_device_unregister(&mmu->iommu);
  902. arm_iommu_release_mapping(mmu->mapping);
  903. ipmmu_device_reset(mmu);
  904. return 0;
  905. }
  906. static struct platform_driver ipmmu_driver = {
  907. .driver = {
  908. .name = "ipmmu-vmsa",
  909. .of_match_table = of_match_ptr(ipmmu_of_ids),
  910. },
  911. .probe = ipmmu_probe,
  912. .remove = ipmmu_remove,
  913. };
  914. static int __init ipmmu_init(void)
  915. {
  916. struct device_node *np;
  917. static bool setup_done;
  918. int ret;
  919. if (setup_done)
  920. return 0;
  921. np = of_find_matching_node(NULL, ipmmu_of_ids);
  922. if (!np)
  923. return 0;
  924. of_node_put(np);
  925. ret = platform_driver_register(&ipmmu_driver);
  926. if (ret < 0)
  927. return ret;
  928. #if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA)
  929. if (!iommu_present(&platform_bus_type))
  930. bus_set_iommu(&platform_bus_type, &ipmmu_ops);
  931. #endif
  932. setup_done = true;
  933. return 0;
  934. }
  935. static void __exit ipmmu_exit(void)
  936. {
  937. return platform_driver_unregister(&ipmmu_driver);
  938. }
  939. subsys_initcall(ipmmu_init);
  940. module_exit(ipmmu_exit);
  941. MODULE_DESCRIPTION("IOMMU API for Renesas VMSA-compatible IPMMU");
  942. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  943. MODULE_LICENSE("GPL v2");