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. struct mutex mutex; /* 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, bool leaf,
  235. 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. /*
  267. * TODO: Add support for coherent walk through CCI with DVM and remove
  268. * cache handling. For now, delegate it to the io-pgtable code.
  269. */
  270. domain->cfg.iommu_dev = domain->mmu->dev;
  271. domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg,
  272. domain);
  273. if (!domain->iop)
  274. return -EINVAL;
  275. /*
  276. * TODO: When adding support for multiple contexts, find an unused
  277. * context.
  278. */
  279. domain->context_id = 0;
  280. /* TTBR0 */
  281. ttbr = domain->cfg.arm_lpae_s1_cfg.ttbr[0];
  282. ipmmu_ctx_write(domain, IMTTLBR0, ttbr);
  283. ipmmu_ctx_write(domain, IMTTUBR0, ttbr >> 32);
  284. /*
  285. * TTBCR
  286. * We use long descriptors with inner-shareable WBWA tables and allocate
  287. * the whole 32-bit VA space to TTBR0.
  288. */
  289. ipmmu_ctx_write(domain, IMTTBCR, IMTTBCR_EAE |
  290. IMTTBCR_SH0_INNER_SHAREABLE | IMTTBCR_ORGN0_WB_WA |
  291. IMTTBCR_IRGN0_WB_WA | IMTTBCR_SL0_LVL_1);
  292. /* MAIR0 */
  293. ipmmu_ctx_write(domain, IMMAIR0, domain->cfg.arm_lpae_s1_cfg.mair[0]);
  294. /* IMBUSCR */
  295. ipmmu_ctx_write(domain, IMBUSCR,
  296. ipmmu_ctx_read(domain, IMBUSCR) &
  297. ~(IMBUSCR_DVM | IMBUSCR_BUSSEL_MASK));
  298. /*
  299. * IMSTR
  300. * Clear all interrupt flags.
  301. */
  302. ipmmu_ctx_write(domain, IMSTR, ipmmu_ctx_read(domain, IMSTR));
  303. /*
  304. * IMCTR
  305. * Enable the MMU and interrupt generation. The long-descriptor
  306. * translation table format doesn't use TEX remapping. Don't enable AF
  307. * software management as we have no use for it. Flush the TLB as
  308. * required when modifying the context registers.
  309. */
  310. ipmmu_ctx_write(domain, IMCTR, IMCTR_INTEN | IMCTR_FLUSH | IMCTR_MMUEN);
  311. return 0;
  312. }
  313. static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain)
  314. {
  315. if (!domain->mmu)
  316. return;
  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. mutex_init(&domain->mutex);
  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 int i;
  411. int ret = 0;
  412. if (!mmu) {
  413. dev_err(dev, "Cannot attach to IPMMU\n");
  414. return -ENXIO;
  415. }
  416. mutex_lock(&domain->mutex);
  417. if (!domain->mmu) {
  418. /* The domain hasn't been used yet, initialize it. */
  419. domain->mmu = mmu;
  420. ret = ipmmu_domain_init_context(domain);
  421. } else if (domain->mmu != mmu) {
  422. /*
  423. * Something is wrong, we can't attach two devices using
  424. * different IOMMUs to the same domain.
  425. */
  426. dev_err(dev, "Can't attach IPMMU %s to domain on IPMMU %s\n",
  427. dev_name(mmu->dev), dev_name(domain->mmu->dev));
  428. ret = -EINVAL;
  429. }
  430. mutex_unlock(&domain->mutex);
  431. if (ret < 0)
  432. return ret;
  433. for (i = 0; i < archdata->num_utlbs; ++i)
  434. ipmmu_utlb_enable(domain, archdata->utlbs[i]);
  435. return 0;
  436. }
  437. static void ipmmu_detach_device(struct iommu_domain *io_domain,
  438. struct device *dev)
  439. {
  440. struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu;
  441. struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
  442. unsigned int i;
  443. for (i = 0; i < archdata->num_utlbs; ++i)
  444. ipmmu_utlb_disable(domain, archdata->utlbs[i]);
  445. /*
  446. * TODO: Optimize by disabling the context when no device is attached.
  447. */
  448. }
  449. static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova,
  450. phys_addr_t paddr, size_t size, int prot)
  451. {
  452. struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
  453. if (!domain)
  454. return -ENODEV;
  455. return domain->iop->map(domain->iop, iova, paddr, size, prot);
  456. }
  457. static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova,
  458. size_t size)
  459. {
  460. struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
  461. return domain->iop->unmap(domain->iop, iova, size);
  462. }
  463. static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain,
  464. dma_addr_t iova)
  465. {
  466. struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
  467. /* TODO: Is locking needed ? */
  468. return domain->iop->iova_to_phys(domain->iop, iova);
  469. }
  470. static int ipmmu_find_utlbs(struct ipmmu_vmsa_device *mmu, struct device *dev,
  471. unsigned int *utlbs, unsigned int num_utlbs)
  472. {
  473. unsigned int i;
  474. for (i = 0; i < num_utlbs; ++i) {
  475. struct of_phandle_args args;
  476. int ret;
  477. ret = of_parse_phandle_with_args(dev->of_node, "iommus",
  478. "#iommu-cells", i, &args);
  479. if (ret < 0)
  480. return ret;
  481. of_node_put(args.np);
  482. if (args.np != mmu->dev->of_node || args.args_count != 1)
  483. return -EINVAL;
  484. utlbs[i] = args.args[0];
  485. }
  486. return 0;
  487. }
  488. static int ipmmu_add_device(struct device *dev)
  489. {
  490. struct ipmmu_vmsa_archdata *archdata;
  491. struct ipmmu_vmsa_device *mmu;
  492. struct iommu_group *group = NULL;
  493. unsigned int *utlbs;
  494. unsigned int i;
  495. int num_utlbs;
  496. int ret = -ENODEV;
  497. if (dev->archdata.iommu) {
  498. dev_warn(dev, "IOMMU driver already assigned to device %s\n",
  499. dev_name(dev));
  500. return -EINVAL;
  501. }
  502. /* Find the master corresponding to the device. */
  503. num_utlbs = of_count_phandle_with_args(dev->of_node, "iommus",
  504. "#iommu-cells");
  505. if (num_utlbs < 0)
  506. return -ENODEV;
  507. utlbs = kcalloc(num_utlbs, sizeof(*utlbs), GFP_KERNEL);
  508. if (!utlbs)
  509. return -ENOMEM;
  510. spin_lock(&ipmmu_devices_lock);
  511. list_for_each_entry(mmu, &ipmmu_devices, list) {
  512. ret = ipmmu_find_utlbs(mmu, dev, utlbs, num_utlbs);
  513. if (!ret) {
  514. /*
  515. * TODO Take a reference to the MMU to protect
  516. * against device removal.
  517. */
  518. break;
  519. }
  520. }
  521. spin_unlock(&ipmmu_devices_lock);
  522. if (ret < 0)
  523. return -ENODEV;
  524. for (i = 0; i < num_utlbs; ++i) {
  525. if (utlbs[i] >= mmu->num_utlbs) {
  526. ret = -EINVAL;
  527. goto error;
  528. }
  529. }
  530. /* Create a device group and add the device to it. */
  531. group = iommu_group_alloc();
  532. if (IS_ERR(group)) {
  533. dev_err(dev, "Failed to allocate IOMMU group\n");
  534. ret = PTR_ERR(group);
  535. goto error;
  536. }
  537. ret = iommu_group_add_device(group, dev);
  538. iommu_group_put(group);
  539. if (ret < 0) {
  540. dev_err(dev, "Failed to add device to IPMMU group\n");
  541. group = NULL;
  542. goto error;
  543. }
  544. archdata = kzalloc(sizeof(*archdata), GFP_KERNEL);
  545. if (!archdata) {
  546. ret = -ENOMEM;
  547. goto error;
  548. }
  549. archdata->mmu = mmu;
  550. archdata->utlbs = utlbs;
  551. archdata->num_utlbs = num_utlbs;
  552. dev->archdata.iommu = archdata;
  553. /*
  554. * Create the ARM mapping, used by the ARM DMA mapping core to allocate
  555. * VAs. This will allocate a corresponding IOMMU domain.
  556. *
  557. * TODO:
  558. * - Create one mapping per context (TLB).
  559. * - Make the mapping size configurable ? We currently use a 2GB mapping
  560. * at a 1GB offset to ensure that NULL VAs will fault.
  561. */
  562. if (!mmu->mapping) {
  563. struct dma_iommu_mapping *mapping;
  564. mapping = arm_iommu_create_mapping(&platform_bus_type,
  565. SZ_1G, SZ_2G);
  566. if (IS_ERR(mapping)) {
  567. dev_err(mmu->dev, "failed to create ARM IOMMU mapping\n");
  568. ret = PTR_ERR(mapping);
  569. goto error;
  570. }
  571. mmu->mapping = mapping;
  572. }
  573. /* Attach the ARM VA mapping to the device. */
  574. ret = arm_iommu_attach_device(dev, mmu->mapping);
  575. if (ret < 0) {
  576. dev_err(dev, "Failed to attach device to VA mapping\n");
  577. goto error;
  578. }
  579. return 0;
  580. error:
  581. arm_iommu_release_mapping(mmu->mapping);
  582. kfree(dev->archdata.iommu);
  583. kfree(utlbs);
  584. dev->archdata.iommu = NULL;
  585. if (!IS_ERR_OR_NULL(group))
  586. iommu_group_remove_device(dev);
  587. return ret;
  588. }
  589. static void ipmmu_remove_device(struct device *dev)
  590. {
  591. struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu;
  592. arm_iommu_detach_device(dev);
  593. iommu_group_remove_device(dev);
  594. kfree(archdata->utlbs);
  595. kfree(archdata);
  596. dev->archdata.iommu = NULL;
  597. }
  598. static const struct iommu_ops ipmmu_ops = {
  599. .domain_alloc = ipmmu_domain_alloc,
  600. .domain_free = ipmmu_domain_free,
  601. .attach_dev = ipmmu_attach_device,
  602. .detach_dev = ipmmu_detach_device,
  603. .map = ipmmu_map,
  604. .unmap = ipmmu_unmap,
  605. .map_sg = default_iommu_map_sg,
  606. .iova_to_phys = ipmmu_iova_to_phys,
  607. .add_device = ipmmu_add_device,
  608. .remove_device = ipmmu_remove_device,
  609. .pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K,
  610. };
  611. /* -----------------------------------------------------------------------------
  612. * Probe/remove and init
  613. */
  614. static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
  615. {
  616. unsigned int i;
  617. /* Disable all contexts. */
  618. for (i = 0; i < 4; ++i)
  619. ipmmu_write(mmu, i * IM_CTX_SIZE + IMCTR, 0);
  620. }
  621. static int ipmmu_probe(struct platform_device *pdev)
  622. {
  623. struct ipmmu_vmsa_device *mmu;
  624. struct resource *res;
  625. int irq;
  626. int ret;
  627. if (!IS_ENABLED(CONFIG_OF) && !pdev->dev.platform_data) {
  628. dev_err(&pdev->dev, "missing platform data\n");
  629. return -EINVAL;
  630. }
  631. mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL);
  632. if (!mmu) {
  633. dev_err(&pdev->dev, "cannot allocate device data\n");
  634. return -ENOMEM;
  635. }
  636. mmu->dev = &pdev->dev;
  637. mmu->num_utlbs = 32;
  638. /* Map I/O memory and request IRQ. */
  639. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  640. mmu->base = devm_ioremap_resource(&pdev->dev, res);
  641. if (IS_ERR(mmu->base))
  642. return PTR_ERR(mmu->base);
  643. /*
  644. * The IPMMU has two register banks, for secure and non-secure modes.
  645. * The bank mapped at the beginning of the IPMMU address space
  646. * corresponds to the running mode of the CPU. When running in secure
  647. * mode the non-secure register bank is also available at an offset.
  648. *
  649. * Secure mode operation isn't clearly documented and is thus currently
  650. * not implemented in the driver. Furthermore, preliminary tests of
  651. * non-secure operation with the main register bank were not successful.
  652. * Offset the registers base unconditionally to point to the non-secure
  653. * alias space for now.
  654. */
  655. mmu->base += IM_NS_ALIAS_OFFSET;
  656. irq = platform_get_irq(pdev, 0);
  657. if (irq < 0) {
  658. dev_err(&pdev->dev, "no IRQ found\n");
  659. return irq;
  660. }
  661. ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0,
  662. dev_name(&pdev->dev), mmu);
  663. if (ret < 0) {
  664. dev_err(&pdev->dev, "failed to request IRQ %d\n", irq);
  665. return ret;
  666. }
  667. ipmmu_device_reset(mmu);
  668. /*
  669. * We can't create the ARM mapping here as it requires the bus to have
  670. * an IOMMU, which only happens when bus_set_iommu() is called in
  671. * ipmmu_init() after the probe function returns.
  672. */
  673. spin_lock(&ipmmu_devices_lock);
  674. list_add(&mmu->list, &ipmmu_devices);
  675. spin_unlock(&ipmmu_devices_lock);
  676. platform_set_drvdata(pdev, mmu);
  677. return 0;
  678. }
  679. static int ipmmu_remove(struct platform_device *pdev)
  680. {
  681. struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev);
  682. spin_lock(&ipmmu_devices_lock);
  683. list_del(&mmu->list);
  684. spin_unlock(&ipmmu_devices_lock);
  685. arm_iommu_release_mapping(mmu->mapping);
  686. ipmmu_device_reset(mmu);
  687. return 0;
  688. }
  689. static const struct of_device_id ipmmu_of_ids[] = {
  690. { .compatible = "renesas,ipmmu-vmsa", },
  691. { }
  692. };
  693. static struct platform_driver ipmmu_driver = {
  694. .driver = {
  695. .name = "ipmmu-vmsa",
  696. .of_match_table = of_match_ptr(ipmmu_of_ids),
  697. },
  698. .probe = ipmmu_probe,
  699. .remove = ipmmu_remove,
  700. };
  701. static int __init ipmmu_init(void)
  702. {
  703. int ret;
  704. ret = platform_driver_register(&ipmmu_driver);
  705. if (ret < 0)
  706. return ret;
  707. if (!iommu_present(&platform_bus_type))
  708. bus_set_iommu(&platform_bus_type, &ipmmu_ops);
  709. return 0;
  710. }
  711. static void __exit ipmmu_exit(void)
  712. {
  713. return platform_driver_unregister(&ipmmu_driver);
  714. }
  715. subsys_initcall(ipmmu_init);
  716. module_exit(ipmmu_exit);
  717. MODULE_DESCRIPTION("IOMMU API for Renesas VMSA-compatible IPMMU");
  718. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  719. MODULE_LICENSE("GPL v2");