ipmmu-vmsa.c 27 KB

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