ipmmu-vmsa.c 28 KB

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