ipmmu-vmsa.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  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/platform_data/ipmmu-vmsa.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. struct ipmmu_vmsa_device {
  25. struct device *dev;
  26. void __iomem *base;
  27. struct list_head list;
  28. const struct ipmmu_vmsa_platform_data *pdata;
  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. unsigned int context_id;
  36. spinlock_t lock; /* Protects mappings */
  37. pgd_t *pgd;
  38. };
  39. struct ipmmu_vmsa_archdata {
  40. struct ipmmu_vmsa_device *mmu;
  41. unsigned int utlb;
  42. };
  43. static DEFINE_SPINLOCK(ipmmu_devices_lock);
  44. static LIST_HEAD(ipmmu_devices);
  45. #define TLB_LOOP_TIMEOUT 100 /* 100us */
  46. /* -----------------------------------------------------------------------------
  47. * Registers Definition
  48. */
  49. #define IM_CTX_SIZE 0x40
  50. #define IMCTR 0x0000
  51. #define IMCTR_TRE (1 << 17)
  52. #define IMCTR_AFE (1 << 16)
  53. #define IMCTR_RTSEL_MASK (3 << 4)
  54. #define IMCTR_RTSEL_SHIFT 4
  55. #define IMCTR_TREN (1 << 3)
  56. #define IMCTR_INTEN (1 << 2)
  57. #define IMCTR_FLUSH (1 << 1)
  58. #define IMCTR_MMUEN (1 << 0)
  59. #define IMCAAR 0x0004
  60. #define IMTTBCR 0x0008
  61. #define IMTTBCR_EAE (1 << 31)
  62. #define IMTTBCR_PMB (1 << 30)
  63. #define IMTTBCR_SH1_NON_SHAREABLE (0 << 28)
  64. #define IMTTBCR_SH1_OUTER_SHAREABLE (2 << 28)
  65. #define IMTTBCR_SH1_INNER_SHAREABLE (3 << 28)
  66. #define IMTTBCR_SH1_MASK (3 << 28)
  67. #define IMTTBCR_ORGN1_NC (0 << 26)
  68. #define IMTTBCR_ORGN1_WB_WA (1 << 26)
  69. #define IMTTBCR_ORGN1_WT (2 << 26)
  70. #define IMTTBCR_ORGN1_WB (3 << 26)
  71. #define IMTTBCR_ORGN1_MASK (3 << 26)
  72. #define IMTTBCR_IRGN1_NC (0 << 24)
  73. #define IMTTBCR_IRGN1_WB_WA (1 << 24)
  74. #define IMTTBCR_IRGN1_WT (2 << 24)
  75. #define IMTTBCR_IRGN1_WB (3 << 24)
  76. #define IMTTBCR_IRGN1_MASK (3 << 24)
  77. #define IMTTBCR_TSZ1_MASK (7 << 16)
  78. #define IMTTBCR_TSZ1_SHIFT 16
  79. #define IMTTBCR_SH0_NON_SHAREABLE (0 << 12)
  80. #define IMTTBCR_SH0_OUTER_SHAREABLE (2 << 12)
  81. #define IMTTBCR_SH0_INNER_SHAREABLE (3 << 12)
  82. #define IMTTBCR_SH0_MASK (3 << 12)
  83. #define IMTTBCR_ORGN0_NC (0 << 10)
  84. #define IMTTBCR_ORGN0_WB_WA (1 << 10)
  85. #define IMTTBCR_ORGN0_WT (2 << 10)
  86. #define IMTTBCR_ORGN0_WB (3 << 10)
  87. #define IMTTBCR_ORGN0_MASK (3 << 10)
  88. #define IMTTBCR_IRGN0_NC (0 << 8)
  89. #define IMTTBCR_IRGN0_WB_WA (1 << 8)
  90. #define IMTTBCR_IRGN0_WT (2 << 8)
  91. #define IMTTBCR_IRGN0_WB (3 << 8)
  92. #define IMTTBCR_IRGN0_MASK (3 << 8)
  93. #define IMTTBCR_SL0_LVL_2 (0 << 4)
  94. #define IMTTBCR_SL0_LVL_1 (1 << 4)
  95. #define IMTTBCR_TSZ0_MASK (7 << 0)
  96. #define IMTTBCR_TSZ0_SHIFT O
  97. #define IMBUSCR 0x000c
  98. #define IMBUSCR_DVM (1 << 2)
  99. #define IMBUSCR_BUSSEL_SYS (0 << 0)
  100. #define IMBUSCR_BUSSEL_CCI (1 << 0)
  101. #define IMBUSCR_BUSSEL_IMCAAR (2 << 0)
  102. #define IMBUSCR_BUSSEL_CCI_IMCAAR (3 << 0)
  103. #define IMBUSCR_BUSSEL_MASK (3 << 0)
  104. #define IMTTLBR0 0x0010
  105. #define IMTTUBR0 0x0014
  106. #define IMTTLBR1 0x0018
  107. #define IMTTUBR1 0x001c
  108. #define IMSTR 0x0020
  109. #define IMSTR_ERRLVL_MASK (3 << 12)
  110. #define IMSTR_ERRLVL_SHIFT 12
  111. #define IMSTR_ERRCODE_TLB_FORMAT (1 << 8)
  112. #define IMSTR_ERRCODE_ACCESS_PERM (4 << 8)
  113. #define IMSTR_ERRCODE_SECURE_ACCESS (5 << 8)
  114. #define IMSTR_ERRCODE_MASK (7 << 8)
  115. #define IMSTR_MHIT (1 << 4)
  116. #define IMSTR_ABORT (1 << 2)
  117. #define IMSTR_PF (1 << 1)
  118. #define IMSTR_TF (1 << 0)
  119. #define IMMAIR0 0x0028
  120. #define IMMAIR1 0x002c
  121. #define IMMAIR_ATTR_MASK 0xff
  122. #define IMMAIR_ATTR_DEVICE 0x04
  123. #define IMMAIR_ATTR_NC 0x44
  124. #define IMMAIR_ATTR_WBRWA 0xff
  125. #define IMMAIR_ATTR_SHIFT(n) ((n) << 3)
  126. #define IMMAIR_ATTR_IDX_NC 0
  127. #define IMMAIR_ATTR_IDX_WBRWA 1
  128. #define IMMAIR_ATTR_IDX_DEV 2
  129. #define IMEAR 0x0030
  130. #define IMPCTR 0x0200
  131. #define IMPSTR 0x0208
  132. #define IMPEAR 0x020c
  133. #define IMPMBA(n) (0x0280 + ((n) * 4))
  134. #define IMPMBD(n) (0x02c0 + ((n) * 4))
  135. #define IMUCTR(n) (0x0300 + ((n) * 16))
  136. #define IMUCTR_FIXADDEN (1 << 31)
  137. #define IMUCTR_FIXADD_MASK (0xff << 16)
  138. #define IMUCTR_FIXADD_SHIFT 16
  139. #define IMUCTR_TTSEL_MMU(n) ((n) << 4)
  140. #define IMUCTR_TTSEL_PMB (8 << 4)
  141. #define IMUCTR_TTSEL_MASK (15 << 4)
  142. #define IMUCTR_FLUSH (1 << 1)
  143. #define IMUCTR_MMUEN (1 << 0)
  144. #define IMUASID(n) (0x0308 + ((n) * 16))
  145. #define IMUASID_ASID8_MASK (0xff << 8)
  146. #define IMUASID_ASID8_SHIFT 8
  147. #define IMUASID_ASID0_MASK (0xff << 0)
  148. #define IMUASID_ASID0_SHIFT 0
  149. /* -----------------------------------------------------------------------------
  150. * Page Table Bits
  151. */
  152. /*
  153. * VMSA states in section B3.6.3 "Control of Secure or Non-secure memory access,
  154. * Long-descriptor format" that the NStable bit being set in a table descriptor
  155. * will result in the NStable and NS bits of all child entries being ignored and
  156. * considered as being set. The IPMMU seems not to comply with this, as it
  157. * generates a secure access page fault if any of the NStable and NS bits isn't
  158. * set when running in non-secure mode.
  159. */
  160. #ifndef PMD_NSTABLE
  161. #define PMD_NSTABLE (_AT(pmdval_t, 1) << 63)
  162. #endif
  163. #define ARM_VMSA_PTE_XN (((pteval_t)3) << 53)
  164. #define ARM_VMSA_PTE_CONT (((pteval_t)1) << 52)
  165. #define ARM_VMSA_PTE_AF (((pteval_t)1) << 10)
  166. #define ARM_VMSA_PTE_SH_NS (((pteval_t)0) << 8)
  167. #define ARM_VMSA_PTE_SH_OS (((pteval_t)2) << 8)
  168. #define ARM_VMSA_PTE_SH_IS (((pteval_t)3) << 8)
  169. #define ARM_VMSA_PTE_SH_MASK (((pteval_t)3) << 8)
  170. #define ARM_VMSA_PTE_NS (((pteval_t)1) << 5)
  171. #define ARM_VMSA_PTE_PAGE (((pteval_t)3) << 0)
  172. /* Stage-1 PTE */
  173. #define ARM_VMSA_PTE_nG (((pteval_t)1) << 11)
  174. #define ARM_VMSA_PTE_AP_UNPRIV (((pteval_t)1) << 6)
  175. #define ARM_VMSA_PTE_AP_RDONLY (((pteval_t)2) << 6)
  176. #define ARM_VMSA_PTE_AP_MASK (((pteval_t)3) << 6)
  177. #define ARM_VMSA_PTE_ATTRINDX_MASK (((pteval_t)3) << 2)
  178. #define ARM_VMSA_PTE_ATTRINDX_SHIFT 2
  179. #define ARM_VMSA_PTE_ATTRS_MASK \
  180. (ARM_VMSA_PTE_XN | ARM_VMSA_PTE_CONT | ARM_VMSA_PTE_nG | \
  181. ARM_VMSA_PTE_AF | ARM_VMSA_PTE_SH_MASK | ARM_VMSA_PTE_AP_MASK | \
  182. ARM_VMSA_PTE_NS | ARM_VMSA_PTE_ATTRINDX_MASK)
  183. #define ARM_VMSA_PTE_CONT_ENTRIES 16
  184. #define ARM_VMSA_PTE_CONT_SIZE (PAGE_SIZE * ARM_VMSA_PTE_CONT_ENTRIES)
  185. #define IPMMU_PTRS_PER_PTE 512
  186. #define IPMMU_PTRS_PER_PMD 512
  187. #define IPMMU_PTRS_PER_PGD 4
  188. /* -----------------------------------------------------------------------------
  189. * Read/Write Access
  190. */
  191. static u32 ipmmu_read(struct ipmmu_vmsa_device *mmu, unsigned int offset)
  192. {
  193. return ioread32(mmu->base + offset);
  194. }
  195. static void ipmmu_write(struct ipmmu_vmsa_device *mmu, unsigned int offset,
  196. u32 data)
  197. {
  198. iowrite32(data, mmu->base + offset);
  199. }
  200. static u32 ipmmu_ctx_read(struct ipmmu_vmsa_domain *domain, unsigned int reg)
  201. {
  202. return ipmmu_read(domain->mmu, domain->context_id * IM_CTX_SIZE + reg);
  203. }
  204. static void ipmmu_ctx_write(struct ipmmu_vmsa_domain *domain, unsigned int reg,
  205. u32 data)
  206. {
  207. ipmmu_write(domain->mmu, domain->context_id * IM_CTX_SIZE + reg, data);
  208. }
  209. /* -----------------------------------------------------------------------------
  210. * TLB and microTLB Management
  211. */
  212. /* Wait for any pending TLB invalidations to complete */
  213. static void ipmmu_tlb_sync(struct ipmmu_vmsa_domain *domain)
  214. {
  215. unsigned int count = 0;
  216. while (ipmmu_ctx_read(domain, IMCTR) & IMCTR_FLUSH) {
  217. cpu_relax();
  218. if (++count == TLB_LOOP_TIMEOUT) {
  219. dev_err_ratelimited(domain->mmu->dev,
  220. "TLB sync timed out -- MMU may be deadlocked\n");
  221. return;
  222. }
  223. udelay(1);
  224. }
  225. }
  226. static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain)
  227. {
  228. u32 reg;
  229. reg = ipmmu_ctx_read(domain, IMCTR);
  230. reg |= IMCTR_FLUSH;
  231. ipmmu_ctx_write(domain, IMCTR, reg);
  232. ipmmu_tlb_sync(domain);
  233. }
  234. /*
  235. * Enable MMU translation for the microTLB.
  236. */
  237. static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
  238. unsigned int utlb)
  239. {
  240. struct ipmmu_vmsa_device *mmu = domain->mmu;
  241. /*
  242. * TODO: Reference-count the microTLB as several bus masters can be
  243. * connected to the same microTLB.
  244. */
  245. /* TODO: What should we set the ASID to ? */
  246. ipmmu_write(mmu, IMUASID(utlb), 0);
  247. /* TODO: Do we need to flush the microTLB ? */
  248. ipmmu_write(mmu, IMUCTR(utlb),
  249. IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_FLUSH |
  250. IMUCTR_MMUEN);
  251. }
  252. /*
  253. * Disable MMU translation for the microTLB.
  254. */
  255. static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain,
  256. unsigned int utlb)
  257. {
  258. struct ipmmu_vmsa_device *mmu = domain->mmu;
  259. ipmmu_write(mmu, IMUCTR(utlb), 0);
  260. }
  261. static void ipmmu_flush_pgtable(struct ipmmu_vmsa_device *mmu, void *addr,
  262. size_t size)
  263. {
  264. unsigned long offset = (unsigned long)addr & ~PAGE_MASK;
  265. /*
  266. * TODO: Add support for coherent walk through CCI with DVM and remove
  267. * cache handling.
  268. */
  269. dma_map_page(mmu->dev, virt_to_page(addr), offset, size, DMA_TO_DEVICE);
  270. }
  271. /* -----------------------------------------------------------------------------
  272. * Domain/Context Management
  273. */
  274. static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
  275. {
  276. phys_addr_t ttbr;
  277. u32 reg;
  278. /*
  279. * TODO: When adding support for multiple contexts, find an unused
  280. * context.
  281. */
  282. domain->context_id = 0;
  283. /* TTBR0 */
  284. ipmmu_flush_pgtable(domain->mmu, domain->pgd,
  285. IPMMU_PTRS_PER_PGD * sizeof(*domain->pgd));
  286. ttbr = __pa(domain->pgd);
  287. ipmmu_ctx_write(domain, IMTTLBR0, ttbr);
  288. ipmmu_ctx_write(domain, IMTTUBR0, ttbr >> 32);
  289. /*
  290. * TTBCR
  291. * We use long descriptors with inner-shareable WBWA tables and allocate
  292. * the whole 32-bit VA space to TTBR0.
  293. */
  294. ipmmu_ctx_write(domain, IMTTBCR, IMTTBCR_EAE |
  295. IMTTBCR_SH0_INNER_SHAREABLE | IMTTBCR_ORGN0_WB_WA |
  296. IMTTBCR_IRGN0_WB_WA | IMTTBCR_SL0_LVL_1);
  297. /*
  298. * MAIR0
  299. * We need three attributes only, non-cacheable, write-back read/write
  300. * allocate and device memory.
  301. */
  302. reg = (IMMAIR_ATTR_NC << IMMAIR_ATTR_SHIFT(IMMAIR_ATTR_IDX_NC))
  303. | (IMMAIR_ATTR_WBRWA << IMMAIR_ATTR_SHIFT(IMMAIR_ATTR_IDX_WBRWA))
  304. | (IMMAIR_ATTR_DEVICE << IMMAIR_ATTR_SHIFT(IMMAIR_ATTR_IDX_DEV));
  305. ipmmu_ctx_write(domain, IMMAIR0, reg);
  306. /* IMBUSCR */
  307. ipmmu_ctx_write(domain, IMBUSCR,
  308. ipmmu_ctx_read(domain, IMBUSCR) &
  309. ~(IMBUSCR_DVM | IMBUSCR_BUSSEL_MASK));
  310. /*
  311. * IMSTR
  312. * Clear all interrupt flags.
  313. */
  314. ipmmu_ctx_write(domain, IMSTR, ipmmu_ctx_read(domain, IMSTR));
  315. /*
  316. * IMCTR
  317. * Enable the MMU and interrupt generation. The long-descriptor
  318. * translation table format doesn't use TEX remapping. Don't enable AF
  319. * software management as we have no use for it. Flush the TLB as
  320. * required when modifying the context registers.
  321. */
  322. ipmmu_ctx_write(domain, IMCTR, IMCTR_INTEN | IMCTR_FLUSH | IMCTR_MMUEN);
  323. return 0;
  324. }
  325. static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain)
  326. {
  327. /*
  328. * Disable the context. Flush the TLB as required when modifying the
  329. * context registers.
  330. *
  331. * TODO: Is TLB flush really needed ?
  332. */
  333. ipmmu_ctx_write(domain, IMCTR, IMCTR_FLUSH);
  334. ipmmu_tlb_sync(domain);
  335. }
  336. /* -----------------------------------------------------------------------------
  337. * Fault Handling
  338. */
  339. static irqreturn_t ipmmu_domain_irq(struct ipmmu_vmsa_domain *domain)
  340. {
  341. const u32 err_mask = IMSTR_MHIT | IMSTR_ABORT | IMSTR_PF | IMSTR_TF;
  342. struct ipmmu_vmsa_device *mmu = domain->mmu;
  343. u32 status;
  344. u32 iova;
  345. status = ipmmu_ctx_read(domain, IMSTR);
  346. if (!(status & err_mask))
  347. return IRQ_NONE;
  348. iova = ipmmu_ctx_read(domain, IMEAR);
  349. /*
  350. * Clear the error status flags. Unlike traditional interrupt flag
  351. * registers that must be cleared by writing 1, this status register
  352. * seems to require 0. The error address register must be read before,
  353. * otherwise its value will be 0.
  354. */
  355. ipmmu_ctx_write(domain, IMSTR, 0);
  356. /* Log fatal errors. */
  357. if (status & IMSTR_MHIT)
  358. dev_err_ratelimited(mmu->dev, "Multiple TLB hits @0x%08x\n",
  359. iova);
  360. if (status & IMSTR_ABORT)
  361. dev_err_ratelimited(mmu->dev, "Page Table Walk Abort @0x%08x\n",
  362. iova);
  363. if (!(status & (IMSTR_PF | IMSTR_TF)))
  364. return IRQ_NONE;
  365. /*
  366. * Try to handle page faults and translation faults.
  367. *
  368. * TODO: We need to look up the faulty device based on the I/O VA. Use
  369. * the IOMMU device for now.
  370. */
  371. if (!report_iommu_fault(domain->io_domain, mmu->dev, iova, 0))
  372. return IRQ_HANDLED;
  373. dev_err_ratelimited(mmu->dev,
  374. "Unhandled fault: status 0x%08x iova 0x%08x\n",
  375. status, iova);
  376. return IRQ_HANDLED;
  377. }
  378. static irqreturn_t ipmmu_irq(int irq, void *dev)
  379. {
  380. struct ipmmu_vmsa_device *mmu = dev;
  381. struct iommu_domain *io_domain;
  382. struct ipmmu_vmsa_domain *domain;
  383. if (!mmu->mapping)
  384. return IRQ_NONE;
  385. io_domain = mmu->mapping->domain;
  386. domain = io_domain->priv;
  387. return ipmmu_domain_irq(domain);
  388. }
  389. /* -----------------------------------------------------------------------------
  390. * Page Table Management
  391. */
  392. #define pud_pgtable(pud) pfn_to_page(__phys_to_pfn(pud_val(pud) & PHYS_MASK))
  393. static void ipmmu_free_ptes(pmd_t *pmd)
  394. {
  395. pgtable_t table = pmd_pgtable(*pmd);
  396. __free_page(table);
  397. }
  398. static void ipmmu_free_pmds(pud_t *pud)
  399. {
  400. pmd_t *pmd = pmd_offset(pud, 0);
  401. pgtable_t table;
  402. unsigned int i;
  403. for (i = 0; i < IPMMU_PTRS_PER_PMD; ++i) {
  404. if (!pmd_table(*pmd))
  405. continue;
  406. ipmmu_free_ptes(pmd);
  407. pmd++;
  408. }
  409. table = pud_pgtable(*pud);
  410. __free_page(table);
  411. }
  412. static void ipmmu_free_pgtables(struct ipmmu_vmsa_domain *domain)
  413. {
  414. pgd_t *pgd, *pgd_base = domain->pgd;
  415. unsigned int i;
  416. /*
  417. * Recursively free the page tables for this domain. We don't care about
  418. * speculative TLB filling, because the TLB will be nuked next time this
  419. * context bank is re-allocated and no devices currently map to these
  420. * tables.
  421. */
  422. pgd = pgd_base;
  423. for (i = 0; i < IPMMU_PTRS_PER_PGD; ++i) {
  424. if (pgd_none(*pgd))
  425. continue;
  426. ipmmu_free_pmds((pud_t *)pgd);
  427. pgd++;
  428. }
  429. kfree(pgd_base);
  430. }
  431. /*
  432. * We can't use the (pgd|pud|pmd|pte)_populate or the set_(pgd|pud|pmd|pte)
  433. * functions as they would flush the CPU TLB.
  434. */
  435. static pte_t *ipmmu_alloc_pte(struct ipmmu_vmsa_device *mmu, pmd_t *pmd,
  436. unsigned long iova)
  437. {
  438. pte_t *pte;
  439. if (!pmd_none(*pmd))
  440. return pte_offset_kernel(pmd, iova);
  441. pte = (pte_t *)get_zeroed_page(GFP_ATOMIC);
  442. if (!pte)
  443. return NULL;
  444. ipmmu_flush_pgtable(mmu, pte, PAGE_SIZE);
  445. *pmd = __pmd(__pa(pte) | PMD_NSTABLE | PMD_TYPE_TABLE);
  446. ipmmu_flush_pgtable(mmu, pmd, sizeof(*pmd));
  447. return pte + pte_index(iova);
  448. }
  449. static pmd_t *ipmmu_alloc_pmd(struct ipmmu_vmsa_device *mmu, pgd_t *pgd,
  450. unsigned long iova)
  451. {
  452. pud_t *pud = (pud_t *)pgd;
  453. pmd_t *pmd;
  454. if (!pud_none(*pud))
  455. return pmd_offset(pud, iova);
  456. pmd = (pmd_t *)get_zeroed_page(GFP_ATOMIC);
  457. if (!pmd)
  458. return NULL;
  459. ipmmu_flush_pgtable(mmu, pmd, PAGE_SIZE);
  460. *pud = __pud(__pa(pmd) | PMD_NSTABLE | PMD_TYPE_TABLE);
  461. ipmmu_flush_pgtable(mmu, pud, sizeof(*pud));
  462. return pmd + pmd_index(iova);
  463. }
  464. static u64 ipmmu_page_prot(unsigned int prot, u64 type)
  465. {
  466. u64 pgprot = ARM_VMSA_PTE_XN | ARM_VMSA_PTE_nG | ARM_VMSA_PTE_AF
  467. | ARM_VMSA_PTE_SH_IS | ARM_VMSA_PTE_AP_UNPRIV
  468. | ARM_VMSA_PTE_NS | type;
  469. if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
  470. pgprot |= ARM_VMSA_PTE_AP_RDONLY;
  471. if (prot & IOMMU_CACHE)
  472. pgprot |= IMMAIR_ATTR_IDX_WBRWA << ARM_VMSA_PTE_ATTRINDX_SHIFT;
  473. if (prot & IOMMU_EXEC)
  474. pgprot &= ~ARM_VMSA_PTE_XN;
  475. else if (!(prot & (IOMMU_READ | IOMMU_WRITE)))
  476. /* If no access create a faulting entry to avoid TLB fills. */
  477. pgprot &= ~ARM_VMSA_PTE_PAGE;
  478. return pgprot;
  479. }
  480. static int ipmmu_alloc_init_pte(struct ipmmu_vmsa_device *mmu, pmd_t *pmd,
  481. unsigned long iova, unsigned long pfn,
  482. size_t size, int prot)
  483. {
  484. pteval_t pteval = ipmmu_page_prot(prot, ARM_VMSA_PTE_PAGE);
  485. unsigned int num_ptes = 1;
  486. pte_t *pte, *start;
  487. unsigned int i;
  488. pte = ipmmu_alloc_pte(mmu, pmd, iova);
  489. if (!pte)
  490. return -ENOMEM;
  491. start = pte;
  492. /*
  493. * Install the page table entries. We can be called both for a single
  494. * page or for a block of 16 physically contiguous pages. In the latter
  495. * case set the PTE contiguous hint.
  496. */
  497. if (size == SZ_64K) {
  498. pteval |= ARM_VMSA_PTE_CONT;
  499. num_ptes = ARM_VMSA_PTE_CONT_ENTRIES;
  500. }
  501. for (i = num_ptes; i; --i)
  502. *pte++ = pfn_pte(pfn++, __pgprot(pteval));
  503. ipmmu_flush_pgtable(mmu, start, sizeof(*pte) * num_ptes);
  504. return 0;
  505. }
  506. static int ipmmu_alloc_init_pmd(struct ipmmu_vmsa_device *mmu, pmd_t *pmd,
  507. unsigned long iova, unsigned long pfn,
  508. int prot)
  509. {
  510. pmdval_t pmdval = ipmmu_page_prot(prot, PMD_TYPE_SECT);
  511. *pmd = pfn_pmd(pfn, __pgprot(pmdval));
  512. ipmmu_flush_pgtable(mmu, pmd, sizeof(*pmd));
  513. return 0;
  514. }
  515. static int ipmmu_create_mapping(struct ipmmu_vmsa_domain *domain,
  516. unsigned long iova, phys_addr_t paddr,
  517. size_t size, int prot)
  518. {
  519. struct ipmmu_vmsa_device *mmu = domain->mmu;
  520. pgd_t *pgd = domain->pgd;
  521. unsigned long flags;
  522. unsigned long pfn;
  523. pmd_t *pmd;
  524. int ret;
  525. if (!pgd)
  526. return -EINVAL;
  527. if (size & ~PAGE_MASK)
  528. return -EINVAL;
  529. if (paddr & ~((1ULL << 40) - 1))
  530. return -ERANGE;
  531. pfn = __phys_to_pfn(paddr);
  532. pgd += pgd_index(iova);
  533. /* Update the page tables. */
  534. spin_lock_irqsave(&domain->lock, flags);
  535. pmd = ipmmu_alloc_pmd(mmu, pgd, iova);
  536. if (!pmd) {
  537. ret = -ENOMEM;
  538. goto done;
  539. }
  540. switch (size) {
  541. case SZ_2M:
  542. ret = ipmmu_alloc_init_pmd(mmu, pmd, iova, pfn, prot);
  543. break;
  544. case SZ_64K:
  545. case SZ_4K:
  546. ret = ipmmu_alloc_init_pte(mmu, pmd, iova, pfn, size, prot);
  547. break;
  548. default:
  549. ret = -EINVAL;
  550. break;
  551. }
  552. done:
  553. spin_unlock_irqrestore(&domain->lock, flags);
  554. if (!ret)
  555. ipmmu_tlb_invalidate(domain);
  556. return ret;
  557. }
  558. static void ipmmu_clear_pud(struct ipmmu_vmsa_device *mmu, pud_t *pud)
  559. {
  560. /* Free the page table. */
  561. pgtable_t table = pud_pgtable(*pud);
  562. __free_page(table);
  563. /* Clear the PUD. */
  564. *pud = __pud(0);
  565. ipmmu_flush_pgtable(mmu, pud, sizeof(*pud));
  566. }
  567. static void ipmmu_clear_pmd(struct ipmmu_vmsa_device *mmu, pud_t *pud,
  568. pmd_t *pmd)
  569. {
  570. unsigned int i;
  571. /* Free the page table. */
  572. if (pmd_table(*pmd)) {
  573. pgtable_t table = pmd_pgtable(*pmd);
  574. __free_page(table);
  575. }
  576. /* Clear the PMD. */
  577. *pmd = __pmd(0);
  578. ipmmu_flush_pgtable(mmu, pmd, sizeof(*pmd));
  579. /* Check whether the PUD is still needed. */
  580. pmd = pmd_offset(pud, 0);
  581. for (i = 0; i < IPMMU_PTRS_PER_PMD; ++i) {
  582. if (!pmd_none(pmd[i]))
  583. return;
  584. }
  585. /* Clear the parent PUD. */
  586. ipmmu_clear_pud(mmu, pud);
  587. }
  588. static void ipmmu_clear_pte(struct ipmmu_vmsa_device *mmu, pud_t *pud,
  589. pmd_t *pmd, pte_t *pte, unsigned int num_ptes)
  590. {
  591. unsigned int i;
  592. /* Clear the PTE. */
  593. for (i = num_ptes; i; --i)
  594. pte[i-1] = __pte(0);
  595. ipmmu_flush_pgtable(mmu, pte, sizeof(*pte) * num_ptes);
  596. /* Check whether the PMD is still needed. */
  597. pte = pte_offset_kernel(pmd, 0);
  598. for (i = 0; i < IPMMU_PTRS_PER_PTE; ++i) {
  599. if (!pte_none(pte[i]))
  600. return;
  601. }
  602. /* Clear the parent PMD. */
  603. ipmmu_clear_pmd(mmu, pud, pmd);
  604. }
  605. static int ipmmu_split_pmd(struct ipmmu_vmsa_device *mmu, pmd_t *pmd)
  606. {
  607. pte_t *pte, *start;
  608. pteval_t pteval;
  609. unsigned long pfn;
  610. unsigned int i;
  611. pte = (pte_t *)get_zeroed_page(GFP_ATOMIC);
  612. if (!pte)
  613. return -ENOMEM;
  614. /* Copy the PMD attributes. */
  615. pteval = (pmd_val(*pmd) & ARM_VMSA_PTE_ATTRS_MASK)
  616. | ARM_VMSA_PTE_CONT | ARM_VMSA_PTE_PAGE;
  617. pfn = pmd_pfn(*pmd);
  618. start = pte;
  619. for (i = IPMMU_PTRS_PER_PTE; i; --i)
  620. *pte++ = pfn_pte(pfn++, __pgprot(pteval));
  621. ipmmu_flush_pgtable(mmu, start, PAGE_SIZE);
  622. *pmd = __pmd(__pa(start) | PMD_NSTABLE | PMD_TYPE_TABLE);
  623. ipmmu_flush_pgtable(mmu, pmd, sizeof(*pmd));
  624. return 0;
  625. }
  626. static void ipmmu_split_pte(struct ipmmu_vmsa_device *mmu, pte_t *pte)
  627. {
  628. unsigned int i;
  629. for (i = ARM_VMSA_PTE_CONT_ENTRIES; i; --i)
  630. pte[i-1] = __pte(pte_val(*pte) & ~ARM_VMSA_PTE_CONT);
  631. ipmmu_flush_pgtable(mmu, pte, sizeof(*pte) * ARM_VMSA_PTE_CONT_ENTRIES);
  632. }
  633. static int ipmmu_clear_mapping(struct ipmmu_vmsa_domain *domain,
  634. unsigned long iova, size_t size)
  635. {
  636. struct ipmmu_vmsa_device *mmu = domain->mmu;
  637. unsigned long flags;
  638. pgd_t *pgd = domain->pgd;
  639. pud_t *pud;
  640. pmd_t *pmd;
  641. pte_t *pte;
  642. int ret = 0;
  643. if (!pgd)
  644. return -EINVAL;
  645. if (size & ~PAGE_MASK)
  646. return -EINVAL;
  647. pgd += pgd_index(iova);
  648. pud = (pud_t *)pgd;
  649. spin_lock_irqsave(&domain->lock, flags);
  650. /* If there's no PUD or PMD we're done. */
  651. if (pud_none(*pud))
  652. goto done;
  653. pmd = pmd_offset(pud, iova);
  654. if (pmd_none(*pmd))
  655. goto done;
  656. /*
  657. * When freeing a 2MB block just clear the PMD. In the unlikely case the
  658. * block is mapped as individual pages this will free the corresponding
  659. * PTE page table.
  660. */
  661. if (size == SZ_2M) {
  662. ipmmu_clear_pmd(mmu, pud, pmd);
  663. goto done;
  664. }
  665. /*
  666. * If the PMD has been mapped as a section remap it as pages to allow
  667. * freeing individual pages.
  668. */
  669. if (pmd_sect(*pmd))
  670. ipmmu_split_pmd(mmu, pmd);
  671. pte = pte_offset_kernel(pmd, iova);
  672. /*
  673. * When freeing a 64kB block just clear the PTE entries. We don't have
  674. * to care about the contiguous hint of the surrounding entries.
  675. */
  676. if (size == SZ_64K) {
  677. ipmmu_clear_pte(mmu, pud, pmd, pte, ARM_VMSA_PTE_CONT_ENTRIES);
  678. goto done;
  679. }
  680. /*
  681. * If the PTE has been mapped with the contiguous hint set remap it and
  682. * its surrounding PTEs to allow unmapping a single page.
  683. */
  684. if (pte_val(*pte) & ARM_VMSA_PTE_CONT)
  685. ipmmu_split_pte(mmu, pte);
  686. /* Clear the PTE. */
  687. ipmmu_clear_pte(mmu, pud, pmd, pte, 1);
  688. done:
  689. spin_unlock_irqrestore(&domain->lock, flags);
  690. if (ret)
  691. ipmmu_tlb_invalidate(domain);
  692. return 0;
  693. }
  694. /* -----------------------------------------------------------------------------
  695. * IOMMU Operations
  696. */
  697. static int ipmmu_domain_init(struct iommu_domain *io_domain)
  698. {
  699. struct ipmmu_vmsa_domain *domain;
  700. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  701. if (!domain)
  702. return -ENOMEM;
  703. spin_lock_init(&domain->lock);
  704. domain->pgd = kzalloc(IPMMU_PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL);
  705. if (!domain->pgd) {
  706. kfree(domain);
  707. return -ENOMEM;
  708. }
  709. io_domain->priv = domain;
  710. domain->io_domain = io_domain;
  711. return 0;
  712. }
  713. static void ipmmu_domain_destroy(struct iommu_domain *io_domain)
  714. {
  715. struct ipmmu_vmsa_domain *domain = io_domain->priv;
  716. /*
  717. * Free the domain resources. We assume that all devices have already
  718. * been detached.
  719. */
  720. ipmmu_domain_destroy_context(domain);
  721. ipmmu_free_pgtables(domain);
  722. kfree(domain);
  723. }
  724. static int ipmmu_attach_device(struct iommu_domain *io_domain,
  725. struct device *dev)
  726. {
  727. struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu;
  728. struct ipmmu_vmsa_device *mmu = archdata->mmu;
  729. struct ipmmu_vmsa_domain *domain = io_domain->priv;
  730. unsigned long flags;
  731. int ret = 0;
  732. if (!mmu) {
  733. dev_err(dev, "Cannot attach to IPMMU\n");
  734. return -ENXIO;
  735. }
  736. spin_lock_irqsave(&domain->lock, flags);
  737. if (!domain->mmu) {
  738. /* The domain hasn't been used yet, initialize it. */
  739. domain->mmu = mmu;
  740. ret = ipmmu_domain_init_context(domain);
  741. } else if (domain->mmu != mmu) {
  742. /*
  743. * Something is wrong, we can't attach two devices using
  744. * different IOMMUs to the same domain.
  745. */
  746. dev_err(dev, "Can't attach IPMMU %s to domain on IPMMU %s\n",
  747. dev_name(mmu->dev), dev_name(domain->mmu->dev));
  748. ret = -EINVAL;
  749. }
  750. spin_unlock_irqrestore(&domain->lock, flags);
  751. if (ret < 0)
  752. return ret;
  753. ipmmu_utlb_enable(domain, archdata->utlb);
  754. return 0;
  755. }
  756. static void ipmmu_detach_device(struct iommu_domain *io_domain,
  757. struct device *dev)
  758. {
  759. struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu;
  760. struct ipmmu_vmsa_domain *domain = io_domain->priv;
  761. ipmmu_utlb_disable(domain, archdata->utlb);
  762. /*
  763. * TODO: Optimize by disabling the context when no device is attached.
  764. */
  765. }
  766. static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova,
  767. phys_addr_t paddr, size_t size, int prot)
  768. {
  769. struct ipmmu_vmsa_domain *domain = io_domain->priv;
  770. if (!domain)
  771. return -ENODEV;
  772. return ipmmu_create_mapping(domain, iova, paddr, size, prot);
  773. }
  774. static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova,
  775. size_t size)
  776. {
  777. struct ipmmu_vmsa_domain *domain = io_domain->priv;
  778. int ret;
  779. ret = ipmmu_clear_mapping(domain, iova, size);
  780. return ret ? 0 : size;
  781. }
  782. static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain,
  783. dma_addr_t iova)
  784. {
  785. struct ipmmu_vmsa_domain *domain = io_domain->priv;
  786. pgd_t pgd;
  787. pud_t pud;
  788. pmd_t pmd;
  789. pte_t pte;
  790. /* TODO: Is locking needed ? */
  791. if (!domain->pgd)
  792. return 0;
  793. pgd = *(domain->pgd + pgd_index(iova));
  794. if (pgd_none(pgd))
  795. return 0;
  796. pud = *pud_offset(&pgd, iova);
  797. if (pud_none(pud))
  798. return 0;
  799. pmd = *pmd_offset(&pud, iova);
  800. if (pmd_none(pmd))
  801. return 0;
  802. if (pmd_sect(pmd))
  803. return __pfn_to_phys(pmd_pfn(pmd)) | (iova & ~PMD_MASK);
  804. pte = *(pmd_page_vaddr(pmd) + pte_index(iova));
  805. if (pte_none(pte))
  806. return 0;
  807. return __pfn_to_phys(pte_pfn(pte)) | (iova & ~PAGE_MASK);
  808. }
  809. static int ipmmu_find_utlb(struct ipmmu_vmsa_device *mmu, struct device *dev)
  810. {
  811. const struct ipmmu_vmsa_master *master = mmu->pdata->masters;
  812. const char *devname = dev_name(dev);
  813. unsigned int i;
  814. for (i = 0; i < mmu->pdata->num_masters; ++i, ++master) {
  815. if (strcmp(master->name, devname) == 0)
  816. return master->utlb;
  817. }
  818. return -1;
  819. }
  820. static int ipmmu_add_device(struct device *dev)
  821. {
  822. struct ipmmu_vmsa_archdata *archdata;
  823. struct ipmmu_vmsa_device *mmu;
  824. struct iommu_group *group;
  825. int utlb = -1;
  826. int ret;
  827. if (dev->archdata.iommu) {
  828. dev_warn(dev, "IOMMU driver already assigned to device %s\n",
  829. dev_name(dev));
  830. return -EINVAL;
  831. }
  832. /* Find the master corresponding to the device. */
  833. spin_lock(&ipmmu_devices_lock);
  834. list_for_each_entry(mmu, &ipmmu_devices, list) {
  835. utlb = ipmmu_find_utlb(mmu, dev);
  836. if (utlb >= 0) {
  837. /*
  838. * TODO Take a reference to the MMU to protect
  839. * against device removal.
  840. */
  841. break;
  842. }
  843. }
  844. spin_unlock(&ipmmu_devices_lock);
  845. if (utlb < 0)
  846. return -ENODEV;
  847. if (utlb >= mmu->num_utlbs)
  848. return -EINVAL;
  849. /* Create a device group and add the device to it. */
  850. group = iommu_group_alloc();
  851. if (IS_ERR(group)) {
  852. dev_err(dev, "Failed to allocate IOMMU group\n");
  853. return PTR_ERR(group);
  854. }
  855. ret = iommu_group_add_device(group, dev);
  856. iommu_group_put(group);
  857. if (ret < 0) {
  858. dev_err(dev, "Failed to add device to IPMMU group\n");
  859. return ret;
  860. }
  861. archdata = kzalloc(sizeof(*archdata), GFP_KERNEL);
  862. if (!archdata) {
  863. ret = -ENOMEM;
  864. goto error;
  865. }
  866. archdata->mmu = mmu;
  867. archdata->utlb = utlb;
  868. dev->archdata.iommu = archdata;
  869. /*
  870. * Create the ARM mapping, used by the ARM DMA mapping core to allocate
  871. * VAs. This will allocate a corresponding IOMMU domain.
  872. *
  873. * TODO:
  874. * - Create one mapping per context (TLB).
  875. * - Make the mapping size configurable ? We currently use a 2GB mapping
  876. * at a 1GB offset to ensure that NULL VAs will fault.
  877. */
  878. if (!mmu->mapping) {
  879. struct dma_iommu_mapping *mapping;
  880. mapping = arm_iommu_create_mapping(&platform_bus_type,
  881. SZ_1G, SZ_2G);
  882. if (IS_ERR(mapping)) {
  883. dev_err(mmu->dev, "failed to create ARM IOMMU mapping\n");
  884. return PTR_ERR(mapping);
  885. }
  886. mmu->mapping = mapping;
  887. }
  888. /* Attach the ARM VA mapping to the device. */
  889. ret = arm_iommu_attach_device(dev, mmu->mapping);
  890. if (ret < 0) {
  891. dev_err(dev, "Failed to attach device to VA mapping\n");
  892. goto error;
  893. }
  894. return 0;
  895. error:
  896. kfree(dev->archdata.iommu);
  897. dev->archdata.iommu = NULL;
  898. iommu_group_remove_device(dev);
  899. return ret;
  900. }
  901. static void ipmmu_remove_device(struct device *dev)
  902. {
  903. arm_iommu_detach_device(dev);
  904. iommu_group_remove_device(dev);
  905. kfree(dev->archdata.iommu);
  906. dev->archdata.iommu = NULL;
  907. }
  908. static struct iommu_ops ipmmu_ops = {
  909. .domain_init = ipmmu_domain_init,
  910. .domain_destroy = ipmmu_domain_destroy,
  911. .attach_dev = ipmmu_attach_device,
  912. .detach_dev = ipmmu_detach_device,
  913. .map = ipmmu_map,
  914. .unmap = ipmmu_unmap,
  915. .iova_to_phys = ipmmu_iova_to_phys,
  916. .add_device = ipmmu_add_device,
  917. .remove_device = ipmmu_remove_device,
  918. .pgsize_bitmap = SZ_2M | SZ_64K | SZ_4K,
  919. };
  920. /* -----------------------------------------------------------------------------
  921. * Probe/remove and init
  922. */
  923. static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
  924. {
  925. unsigned int i;
  926. /* Disable all contexts. */
  927. for (i = 0; i < 4; ++i)
  928. ipmmu_write(mmu, i * IM_CTX_SIZE + IMCTR, 0);
  929. }
  930. static int ipmmu_probe(struct platform_device *pdev)
  931. {
  932. struct ipmmu_vmsa_device *mmu;
  933. struct resource *res;
  934. int irq;
  935. int ret;
  936. if (!pdev->dev.platform_data) {
  937. dev_err(&pdev->dev, "missing platform data\n");
  938. return -EINVAL;
  939. }
  940. mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL);
  941. if (!mmu) {
  942. dev_err(&pdev->dev, "cannot allocate device data\n");
  943. return -ENOMEM;
  944. }
  945. mmu->dev = &pdev->dev;
  946. mmu->pdata = pdev->dev.platform_data;
  947. mmu->num_utlbs = 32;
  948. /* Map I/O memory and request IRQ. */
  949. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  950. mmu->base = devm_ioremap_resource(&pdev->dev, res);
  951. if (IS_ERR(mmu->base))
  952. return PTR_ERR(mmu->base);
  953. irq = platform_get_irq(pdev, 0);
  954. if (irq < 0) {
  955. dev_err(&pdev->dev, "no IRQ found\n");
  956. return irq;
  957. }
  958. ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0,
  959. dev_name(&pdev->dev), mmu);
  960. if (ret < 0) {
  961. dev_err(&pdev->dev, "failed to request IRQ %d\n", irq);
  962. return irq;
  963. }
  964. ipmmu_device_reset(mmu);
  965. /*
  966. * We can't create the ARM mapping here as it requires the bus to have
  967. * an IOMMU, which only happens when bus_set_iommu() is called in
  968. * ipmmu_init() after the probe function returns.
  969. */
  970. spin_lock(&ipmmu_devices_lock);
  971. list_add(&mmu->list, &ipmmu_devices);
  972. spin_unlock(&ipmmu_devices_lock);
  973. platform_set_drvdata(pdev, mmu);
  974. return 0;
  975. }
  976. static int ipmmu_remove(struct platform_device *pdev)
  977. {
  978. struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev);
  979. spin_lock(&ipmmu_devices_lock);
  980. list_del(&mmu->list);
  981. spin_unlock(&ipmmu_devices_lock);
  982. arm_iommu_release_mapping(mmu->mapping);
  983. ipmmu_device_reset(mmu);
  984. return 0;
  985. }
  986. static struct platform_driver ipmmu_driver = {
  987. .driver = {
  988. .owner = THIS_MODULE,
  989. .name = "ipmmu-vmsa",
  990. },
  991. .probe = ipmmu_probe,
  992. .remove = ipmmu_remove,
  993. };
  994. static int __init ipmmu_init(void)
  995. {
  996. int ret;
  997. ret = platform_driver_register(&ipmmu_driver);
  998. if (ret < 0)
  999. return ret;
  1000. if (!iommu_present(&platform_bus_type))
  1001. bus_set_iommu(&platform_bus_type, &ipmmu_ops);
  1002. return 0;
  1003. }
  1004. static void __exit ipmmu_exit(void)
  1005. {
  1006. return platform_driver_unregister(&ipmmu_driver);
  1007. }
  1008. subsys_initcall(ipmmu_init);
  1009. module_exit(ipmmu_exit);
  1010. MODULE_DESCRIPTION("IOMMU API for Renesas VMSA-compatible IPMMU");
  1011. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  1012. MODULE_LICENSE("GPL v2");