ipmmu-vmsa.c 26 KB

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