ipmmu-vmsa.c 22 KB

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