ipmmu-vmsa.c 22 KB

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