blob: 40ae6e87cb8802232b58d7b29f84d3f82930d7c2 [file] [log] [blame]
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001/*
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
Magnus Dammdbb70692017-05-17 19:06:38 +090011#include <linux/bitmap.h>
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020012#include <linux/delay.h>
Magnus Damm3ae47292017-05-17 19:07:10 +090013#include <linux/dma-iommu.h>
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020014#include <linux/dma-mapping.h>
15#include <linux/err.h>
16#include <linux/export.h>
17#include <linux/interrupt.h>
18#include <linux/io.h>
19#include <linux/iommu.h>
20#include <linux/module.h>
Laurent Pinchart275f5052014-03-17 01:02:46 +010021#include <linux/of.h>
Magnus Damm33f3ac92017-10-16 21:29:25 +090022#include <linux/of_device.h>
Magnus Dammcda52fc2017-10-16 21:29:57 +090023#include <linux/of_iommu.h>
Magnus Damm7b2d5962017-07-17 22:05:41 +090024#include <linux/of_platform.h>
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020025#include <linux/platform_device.h>
26#include <linux/sizes.h>
27#include <linux/slab.h>
Magnus Damm58b8e8b2017-10-16 21:30:50 +090028#include <linux/sys_soc.h>
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020029
Magnus Damm3ae47292017-05-17 19:07:10 +090030#if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020031#include <asm/dma-iommu.h>
32#include <asm/pgalloc.h>
Robin Murphy49c875f2017-10-13 19:23:42 +010033#else
34#define arm_iommu_create_mapping(...) NULL
35#define arm_iommu_attach_device(...) -ENODEV
36#define arm_iommu_release_mapping(...) do {} while (0)
37#define arm_iommu_detach_device(...) do {} while (0)
Magnus Damm3ae47292017-05-17 19:07:10 +090038#endif
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020039
Laurent Pinchartf20ed392015-01-20 18:30:04 +020040#include "io-pgtable.h"
41
Magnus Damm5fd16342017-10-16 21:29:46 +090042#define IPMMU_CTX_MAX 8
Magnus Dammdbb70692017-05-17 19:06:38 +090043
Magnus Damm33f3ac92017-10-16 21:29:25 +090044struct ipmmu_features {
45 bool use_ns_alias_offset;
Magnus Dammfd5140e2017-10-16 21:29:36 +090046 bool has_cache_leaf_nodes;
Magnus Damm5fd16342017-10-16 21:29:46 +090047 unsigned int number_of_contexts;
Magnus Dammf5c85892017-10-16 21:30:28 +090048 bool setup_imbuscr;
Magnus Dammc295f502017-10-16 21:30:39 +090049 bool twobit_imttbcr_sl0;
Magnus Damm33f3ac92017-10-16 21:29:25 +090050};
51
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020052struct ipmmu_vmsa_device {
53 struct device *dev;
54 void __iomem *base;
Magnus Damm01da21e2017-07-17 22:05:10 +090055 struct iommu_device iommu;
Magnus Dammfd5140e2017-10-16 21:29:36 +090056 struct ipmmu_vmsa_device *root;
Magnus Damm33f3ac92017-10-16 21:29:25 +090057 const struct ipmmu_features *features;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020058 unsigned int num_utlbs;
Magnus Damm5fd16342017-10-16 21:29:46 +090059 unsigned int num_ctx;
Magnus Dammdbb70692017-05-17 19:06:38 +090060 spinlock_t lock; /* Protects ctx and domains[] */
61 DECLARE_BITMAP(ctx, IPMMU_CTX_MAX);
62 struct ipmmu_vmsa_domain *domains[IPMMU_CTX_MAX];
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020063
Robin Murphyb354c732017-10-13 19:23:40 +010064 struct iommu_group *group;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020065 struct dma_iommu_mapping *mapping;
66};
67
68struct ipmmu_vmsa_domain {
69 struct ipmmu_vmsa_device *mmu;
Joerg Roedel5914c5f2015-03-26 13:43:16 +010070 struct iommu_domain io_domain;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020071
Laurent Pinchartf20ed392015-01-20 18:30:04 +020072 struct io_pgtable_cfg cfg;
73 struct io_pgtable_ops *iop;
74
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020075 unsigned int context_id;
76 spinlock_t lock; /* Protects mappings */
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020077};
78
Joerg Roedel5914c5f2015-03-26 13:43:16 +010079static struct ipmmu_vmsa_domain *to_vmsa_domain(struct iommu_domain *dom)
80{
81 return container_of(dom, struct ipmmu_vmsa_domain, io_domain);
82}
83
Robin Murphye4efe4a2017-10-13 19:23:41 +010084static struct ipmmu_vmsa_device *to_ipmmu(struct device *dev)
Magnus Damm0fbc8b02017-05-17 19:07:20 +090085{
Robin Murphy3c49ed32017-07-17 22:05:31 +090086 return dev->iommu_fwspec ? dev->iommu_fwspec->iommu_priv : NULL;
Magnus Damm0fbc8b02017-05-17 19:07:20 +090087}
88
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020089#define TLB_LOOP_TIMEOUT 100 /* 100us */
90
91/* -----------------------------------------------------------------------------
92 * Registers Definition
93 */
94
Laurent Pinchart275f5052014-03-17 01:02:46 +010095#define IM_NS_ALIAS_OFFSET 0x800
96
Laurent Pinchartd25a2a12014-04-02 12:47:37 +020097#define IM_CTX_SIZE 0x40
98
99#define IMCTR 0x0000
100#define IMCTR_TRE (1 << 17)
101#define IMCTR_AFE (1 << 16)
102#define IMCTR_RTSEL_MASK (3 << 4)
103#define IMCTR_RTSEL_SHIFT 4
104#define IMCTR_TREN (1 << 3)
105#define IMCTR_INTEN (1 << 2)
106#define IMCTR_FLUSH (1 << 1)
107#define IMCTR_MMUEN (1 << 0)
108
109#define IMCAAR 0x0004
110
111#define IMTTBCR 0x0008
112#define IMTTBCR_EAE (1 << 31)
113#define IMTTBCR_PMB (1 << 30)
114#define IMTTBCR_SH1_NON_SHAREABLE (0 << 28)
115#define IMTTBCR_SH1_OUTER_SHAREABLE (2 << 28)
116#define IMTTBCR_SH1_INNER_SHAREABLE (3 << 28)
117#define IMTTBCR_SH1_MASK (3 << 28)
118#define IMTTBCR_ORGN1_NC (0 << 26)
119#define IMTTBCR_ORGN1_WB_WA (1 << 26)
120#define IMTTBCR_ORGN1_WT (2 << 26)
121#define IMTTBCR_ORGN1_WB (3 << 26)
122#define IMTTBCR_ORGN1_MASK (3 << 26)
123#define IMTTBCR_IRGN1_NC (0 << 24)
124#define IMTTBCR_IRGN1_WB_WA (1 << 24)
125#define IMTTBCR_IRGN1_WT (2 << 24)
126#define IMTTBCR_IRGN1_WB (3 << 24)
127#define IMTTBCR_IRGN1_MASK (3 << 24)
128#define IMTTBCR_TSZ1_MASK (7 << 16)
129#define IMTTBCR_TSZ1_SHIFT 16
130#define IMTTBCR_SH0_NON_SHAREABLE (0 << 12)
131#define IMTTBCR_SH0_OUTER_SHAREABLE (2 << 12)
132#define IMTTBCR_SH0_INNER_SHAREABLE (3 << 12)
133#define IMTTBCR_SH0_MASK (3 << 12)
134#define IMTTBCR_ORGN0_NC (0 << 10)
135#define IMTTBCR_ORGN0_WB_WA (1 << 10)
136#define IMTTBCR_ORGN0_WT (2 << 10)
137#define IMTTBCR_ORGN0_WB (3 << 10)
138#define IMTTBCR_ORGN0_MASK (3 << 10)
139#define IMTTBCR_IRGN0_NC (0 << 8)
140#define IMTTBCR_IRGN0_WB_WA (1 << 8)
141#define IMTTBCR_IRGN0_WT (2 << 8)
142#define IMTTBCR_IRGN0_WB (3 << 8)
143#define IMTTBCR_IRGN0_MASK (3 << 8)
144#define IMTTBCR_SL0_LVL_2 (0 << 4)
145#define IMTTBCR_SL0_LVL_1 (1 << 4)
146#define IMTTBCR_TSZ0_MASK (7 << 0)
147#define IMTTBCR_TSZ0_SHIFT O
148
Magnus Dammc295f502017-10-16 21:30:39 +0900149#define IMTTBCR_SL0_TWOBIT_LVL_3 (0 << 6)
150#define IMTTBCR_SL0_TWOBIT_LVL_2 (1 << 6)
151#define IMTTBCR_SL0_TWOBIT_LVL_1 (2 << 6)
152
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200153#define IMBUSCR 0x000c
154#define IMBUSCR_DVM (1 << 2)
155#define IMBUSCR_BUSSEL_SYS (0 << 0)
156#define IMBUSCR_BUSSEL_CCI (1 << 0)
157#define IMBUSCR_BUSSEL_IMCAAR (2 << 0)
158#define IMBUSCR_BUSSEL_CCI_IMCAAR (3 << 0)
159#define IMBUSCR_BUSSEL_MASK (3 << 0)
160
161#define IMTTLBR0 0x0010
162#define IMTTUBR0 0x0014
163#define IMTTLBR1 0x0018
164#define IMTTUBR1 0x001c
165
166#define IMSTR 0x0020
167#define IMSTR_ERRLVL_MASK (3 << 12)
168#define IMSTR_ERRLVL_SHIFT 12
169#define IMSTR_ERRCODE_TLB_FORMAT (1 << 8)
170#define IMSTR_ERRCODE_ACCESS_PERM (4 << 8)
171#define IMSTR_ERRCODE_SECURE_ACCESS (5 << 8)
172#define IMSTR_ERRCODE_MASK (7 << 8)
173#define IMSTR_MHIT (1 << 4)
174#define IMSTR_ABORT (1 << 2)
175#define IMSTR_PF (1 << 1)
176#define IMSTR_TF (1 << 0)
177
178#define IMMAIR0 0x0028
179#define IMMAIR1 0x002c
180#define IMMAIR_ATTR_MASK 0xff
181#define IMMAIR_ATTR_DEVICE 0x04
182#define IMMAIR_ATTR_NC 0x44
183#define IMMAIR_ATTR_WBRWA 0xff
184#define IMMAIR_ATTR_SHIFT(n) ((n) << 3)
185#define IMMAIR_ATTR_IDX_NC 0
186#define IMMAIR_ATTR_IDX_WBRWA 1
187#define IMMAIR_ATTR_IDX_DEV 2
188
189#define IMEAR 0x0030
190
191#define IMPCTR 0x0200
192#define IMPSTR 0x0208
193#define IMPEAR 0x020c
194#define IMPMBA(n) (0x0280 + ((n) * 4))
195#define IMPMBD(n) (0x02c0 + ((n) * 4))
196
197#define IMUCTR(n) (0x0300 + ((n) * 16))
198#define IMUCTR_FIXADDEN (1 << 31)
199#define IMUCTR_FIXADD_MASK (0xff << 16)
200#define IMUCTR_FIXADD_SHIFT 16
201#define IMUCTR_TTSEL_MMU(n) ((n) << 4)
202#define IMUCTR_TTSEL_PMB (8 << 4)
203#define IMUCTR_TTSEL_MASK (15 << 4)
204#define IMUCTR_FLUSH (1 << 1)
205#define IMUCTR_MMUEN (1 << 0)
206
207#define IMUASID(n) (0x0308 + ((n) * 16))
208#define IMUASID_ASID8_MASK (0xff << 8)
209#define IMUASID_ASID8_SHIFT 8
210#define IMUASID_ASID0_MASK (0xff << 0)
211#define IMUASID_ASID0_SHIFT 0
212
213/* -----------------------------------------------------------------------------
Magnus Dammfd5140e2017-10-16 21:29:36 +0900214 * Root device handling
215 */
216
217static struct platform_driver ipmmu_driver;
218
219static bool ipmmu_is_root(struct ipmmu_vmsa_device *mmu)
220{
221 return mmu->root == mmu;
222}
223
224static int __ipmmu_check_device(struct device *dev, void *data)
225{
226 struct ipmmu_vmsa_device *mmu = dev_get_drvdata(dev);
227 struct ipmmu_vmsa_device **rootp = data;
228
229 if (ipmmu_is_root(mmu))
230 *rootp = mmu;
231
232 return 0;
233}
234
235static struct ipmmu_vmsa_device *ipmmu_find_root(void)
236{
237 struct ipmmu_vmsa_device *root = NULL;
238
239 return driver_for_each_device(&ipmmu_driver.driver, NULL, &root,
240 __ipmmu_check_device) == 0 ? root : NULL;
241}
242
243/* -----------------------------------------------------------------------------
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200244 * Read/Write Access
245 */
246
247static u32 ipmmu_read(struct ipmmu_vmsa_device *mmu, unsigned int offset)
248{
249 return ioread32(mmu->base + offset);
250}
251
252static void ipmmu_write(struct ipmmu_vmsa_device *mmu, unsigned int offset,
253 u32 data)
254{
255 iowrite32(data, mmu->base + offset);
256}
257
Magnus Dammd5748932017-10-16 21:30:18 +0900258static u32 ipmmu_ctx_read_root(struct ipmmu_vmsa_domain *domain,
259 unsigned int reg)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200260{
Magnus Dammfd5140e2017-10-16 21:29:36 +0900261 return ipmmu_read(domain->mmu->root,
262 domain->context_id * IM_CTX_SIZE + reg);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200263}
264
Magnus Dammd5748932017-10-16 21:30:18 +0900265static void ipmmu_ctx_write_root(struct ipmmu_vmsa_domain *domain,
266 unsigned int reg, u32 data)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200267{
Magnus Dammfd5140e2017-10-16 21:29:36 +0900268 ipmmu_write(domain->mmu->root,
269 domain->context_id * IM_CTX_SIZE + reg, data);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200270}
271
Magnus Dammd5748932017-10-16 21:30:18 +0900272static void ipmmu_ctx_write_all(struct ipmmu_vmsa_domain *domain,
273 unsigned int reg, u32 data)
274{
275 if (domain->mmu != domain->mmu->root)
276 ipmmu_write(domain->mmu,
277 domain->context_id * IM_CTX_SIZE + reg, data);
278
279 ipmmu_write(domain->mmu->root,
280 domain->context_id * IM_CTX_SIZE + reg, data);
281}
282
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200283/* -----------------------------------------------------------------------------
284 * TLB and microTLB Management
285 */
286
287/* Wait for any pending TLB invalidations to complete */
288static void ipmmu_tlb_sync(struct ipmmu_vmsa_domain *domain)
289{
290 unsigned int count = 0;
291
Magnus Dammd5748932017-10-16 21:30:18 +0900292 while (ipmmu_ctx_read_root(domain, IMCTR) & IMCTR_FLUSH) {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200293 cpu_relax();
294 if (++count == TLB_LOOP_TIMEOUT) {
295 dev_err_ratelimited(domain->mmu->dev,
296 "TLB sync timed out -- MMU may be deadlocked\n");
297 return;
298 }
299 udelay(1);
300 }
301}
302
303static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain)
304{
305 u32 reg;
306
Magnus Dammd5748932017-10-16 21:30:18 +0900307 reg = ipmmu_ctx_read_root(domain, IMCTR);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200308 reg |= IMCTR_FLUSH;
Magnus Dammd5748932017-10-16 21:30:18 +0900309 ipmmu_ctx_write_all(domain, IMCTR, reg);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200310
311 ipmmu_tlb_sync(domain);
312}
313
314/*
315 * Enable MMU translation for the microTLB.
316 */
317static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
Laurent Pinchart192d2042014-05-15 12:40:42 +0200318 unsigned int utlb)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200319{
320 struct ipmmu_vmsa_device *mmu = domain->mmu;
321
Laurent Pinchart192d2042014-05-15 12:40:42 +0200322 /*
323 * TODO: Reference-count the microTLB as several bus masters can be
324 * connected to the same microTLB.
325 */
326
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200327 /* TODO: What should we set the ASID to ? */
Laurent Pinchart192d2042014-05-15 12:40:42 +0200328 ipmmu_write(mmu, IMUASID(utlb), 0);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200329 /* TODO: Do we need to flush the microTLB ? */
Laurent Pinchart192d2042014-05-15 12:40:42 +0200330 ipmmu_write(mmu, IMUCTR(utlb),
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200331 IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_FLUSH |
332 IMUCTR_MMUEN);
333}
334
335/*
336 * Disable MMU translation for the microTLB.
337 */
338static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain,
Laurent Pinchart192d2042014-05-15 12:40:42 +0200339 unsigned int utlb)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200340{
341 struct ipmmu_vmsa_device *mmu = domain->mmu;
342
Laurent Pinchart192d2042014-05-15 12:40:42 +0200343 ipmmu_write(mmu, IMUCTR(utlb), 0);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200344}
345
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200346static void ipmmu_tlb_flush_all(void *cookie)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200347{
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200348 struct ipmmu_vmsa_domain *domain = cookie;
349
350 ipmmu_tlb_invalidate(domain);
351}
352
Robin Murphy06c610e2015-12-07 18:18:53 +0000353static void ipmmu_tlb_add_flush(unsigned long iova, size_t size,
354 size_t granule, bool leaf, void *cookie)
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200355{
356 /* The hardware doesn't support selective TLB flush. */
357}
358
Bhumika Goyal8da4af92017-08-28 23:47:27 +0530359static const struct iommu_gather_ops ipmmu_gather_ops = {
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200360 .tlb_flush_all = ipmmu_tlb_flush_all,
361 .tlb_add_flush = ipmmu_tlb_add_flush,
362 .tlb_sync = ipmmu_tlb_flush_all,
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200363};
364
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200365/* -----------------------------------------------------------------------------
366 * Domain/Context Management
367 */
368
Magnus Dammdbb70692017-05-17 19:06:38 +0900369static int ipmmu_domain_allocate_context(struct ipmmu_vmsa_device *mmu,
370 struct ipmmu_vmsa_domain *domain)
371{
372 unsigned long flags;
373 int ret;
374
375 spin_lock_irqsave(&mmu->lock, flags);
376
Magnus Damm5fd16342017-10-16 21:29:46 +0900377 ret = find_first_zero_bit(mmu->ctx, mmu->num_ctx);
378 if (ret != mmu->num_ctx) {
Magnus Dammdbb70692017-05-17 19:06:38 +0900379 mmu->domains[ret] = domain;
380 set_bit(ret, mmu->ctx);
Magnus Damm5fd16342017-10-16 21:29:46 +0900381 } else
382 ret = -EBUSY;
Magnus Dammdbb70692017-05-17 19:06:38 +0900383
384 spin_unlock_irqrestore(&mmu->lock, flags);
385
386 return ret;
387}
388
Oleksandr Tyshchenkoa175a672017-08-23 17:31:42 +0300389static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu,
390 unsigned int context_id)
391{
392 unsigned long flags;
393
394 spin_lock_irqsave(&mmu->lock, flags);
395
396 clear_bit(context_id, mmu->ctx);
397 mmu->domains[context_id] = NULL;
398
399 spin_unlock_irqrestore(&mmu->lock, flags);
400}
401
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200402static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
403{
Geert Uytterhoevenf64232e2015-12-22 20:01:06 +0100404 u64 ttbr;
Magnus Dammc295f502017-10-16 21:30:39 +0900405 u32 tmp;
Magnus Dammdbb70692017-05-17 19:06:38 +0900406 int ret;
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200407
408 /*
409 * Allocate the page table operations.
410 *
411 * VMSA states in section B3.6.3 "Control of Secure or Non-secure memory
412 * access, Long-descriptor format" that the NStable bit being set in a
413 * table descriptor will result in the NStable and NS bits of all child
414 * entries being ignored and considered as being set. The IPMMU seems
415 * not to comply with this, as it generates a secure access page fault
416 * if any of the NStable and NS bits isn't set when running in
417 * non-secure mode.
418 */
419 domain->cfg.quirks = IO_PGTABLE_QUIRK_ARM_NS;
Magnus Damm26b6aec2017-05-17 19:07:41 +0900420 domain->cfg.pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K;
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200421 domain->cfg.ias = 32;
422 domain->cfg.oas = 40;
423 domain->cfg.tlb = &ipmmu_gather_ops;
Geert Uytterhoeven3b6bb5b2017-01-31 12:17:07 +0100424 domain->io_domain.geometry.aperture_end = DMA_BIT_MASK(32);
425 domain->io_domain.geometry.force_aperture = true;
Robin Murphyff2ed962015-07-29 19:46:08 +0100426 /*
427 * TODO: Add support for coherent walk through CCI with DVM and remove
428 * cache handling. For now, delegate it to the io-pgtable code.
429 */
Magnus Dammfd5140e2017-10-16 21:29:36 +0900430 domain->cfg.iommu_dev = domain->mmu->root->dev;
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200431
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200432 /*
Magnus Dammdbb70692017-05-17 19:06:38 +0900433 * Find an unused context.
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200434 */
Magnus Dammfd5140e2017-10-16 21:29:36 +0900435 ret = ipmmu_domain_allocate_context(domain->mmu->root, domain);
Magnus Damm5fd16342017-10-16 21:29:46 +0900436 if (ret < 0)
437 return ret;
Magnus Dammdbb70692017-05-17 19:06:38 +0900438
439 domain->context_id = ret;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200440
Oleksandr Tyshchenkoa175a672017-08-23 17:31:42 +0300441 domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg,
442 domain);
443 if (!domain->iop) {
Magnus Dammfd5140e2017-10-16 21:29:36 +0900444 ipmmu_domain_free_context(domain->mmu->root,
445 domain->context_id);
Oleksandr Tyshchenkoa175a672017-08-23 17:31:42 +0300446 return -EINVAL;
447 }
448
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200449 /* TTBR0 */
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200450 ttbr = domain->cfg.arm_lpae_s1_cfg.ttbr[0];
Magnus Dammd5748932017-10-16 21:30:18 +0900451 ipmmu_ctx_write_root(domain, IMTTLBR0, ttbr);
452 ipmmu_ctx_write_root(domain, IMTTUBR0, ttbr >> 32);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200453
454 /*
455 * TTBCR
456 * We use long descriptors with inner-shareable WBWA tables and allocate
457 * the whole 32-bit VA space to TTBR0.
458 */
Magnus Dammc295f502017-10-16 21:30:39 +0900459 if (domain->mmu->features->twobit_imttbcr_sl0)
460 tmp = IMTTBCR_SL0_TWOBIT_LVL_1;
461 else
462 tmp = IMTTBCR_SL0_LVL_1;
463
Magnus Dammd5748932017-10-16 21:30:18 +0900464 ipmmu_ctx_write_root(domain, IMTTBCR, IMTTBCR_EAE |
465 IMTTBCR_SH0_INNER_SHAREABLE | IMTTBCR_ORGN0_WB_WA |
Magnus Dammc295f502017-10-16 21:30:39 +0900466 IMTTBCR_IRGN0_WB_WA | tmp);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200467
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200468 /* MAIR0 */
Magnus Dammd5748932017-10-16 21:30:18 +0900469 ipmmu_ctx_write_root(domain, IMMAIR0,
470 domain->cfg.arm_lpae_s1_cfg.mair[0]);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200471
472 /* IMBUSCR */
Magnus Dammf5c85892017-10-16 21:30:28 +0900473 if (domain->mmu->features->setup_imbuscr)
474 ipmmu_ctx_write_root(domain, IMBUSCR,
475 ipmmu_ctx_read_root(domain, IMBUSCR) &
476 ~(IMBUSCR_DVM | IMBUSCR_BUSSEL_MASK));
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200477
478 /*
479 * IMSTR
480 * Clear all interrupt flags.
481 */
Magnus Dammd5748932017-10-16 21:30:18 +0900482 ipmmu_ctx_write_root(domain, IMSTR, ipmmu_ctx_read_root(domain, IMSTR));
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200483
484 /*
485 * IMCTR
486 * Enable the MMU and interrupt generation. The long-descriptor
487 * translation table format doesn't use TEX remapping. Don't enable AF
488 * software management as we have no use for it. Flush the TLB as
489 * required when modifying the context registers.
490 */
Magnus Dammd5748932017-10-16 21:30:18 +0900491 ipmmu_ctx_write_all(domain, IMCTR,
492 IMCTR_INTEN | IMCTR_FLUSH | IMCTR_MMUEN);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200493
494 return 0;
495}
496
497static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain)
498{
499 /*
500 * Disable the context. Flush the TLB as required when modifying the
501 * context registers.
502 *
503 * TODO: Is TLB flush really needed ?
504 */
Magnus Dammd5748932017-10-16 21:30:18 +0900505 ipmmu_ctx_write_all(domain, IMCTR, IMCTR_FLUSH);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200506 ipmmu_tlb_sync(domain);
Magnus Dammfd5140e2017-10-16 21:29:36 +0900507 ipmmu_domain_free_context(domain->mmu->root, domain->context_id);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200508}
509
510/* -----------------------------------------------------------------------------
511 * Fault Handling
512 */
513
514static irqreturn_t ipmmu_domain_irq(struct ipmmu_vmsa_domain *domain)
515{
516 const u32 err_mask = IMSTR_MHIT | IMSTR_ABORT | IMSTR_PF | IMSTR_TF;
517 struct ipmmu_vmsa_device *mmu = domain->mmu;
518 u32 status;
519 u32 iova;
520
Magnus Dammd5748932017-10-16 21:30:18 +0900521 status = ipmmu_ctx_read_root(domain, IMSTR);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200522 if (!(status & err_mask))
523 return IRQ_NONE;
524
Magnus Dammd5748932017-10-16 21:30:18 +0900525 iova = ipmmu_ctx_read_root(domain, IMEAR);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200526
527 /*
528 * Clear the error status flags. Unlike traditional interrupt flag
529 * registers that must be cleared by writing 1, this status register
530 * seems to require 0. The error address register must be read before,
531 * otherwise its value will be 0.
532 */
Magnus Dammd5748932017-10-16 21:30:18 +0900533 ipmmu_ctx_write_root(domain, IMSTR, 0);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200534
535 /* Log fatal errors. */
536 if (status & IMSTR_MHIT)
537 dev_err_ratelimited(mmu->dev, "Multiple TLB hits @0x%08x\n",
538 iova);
539 if (status & IMSTR_ABORT)
540 dev_err_ratelimited(mmu->dev, "Page Table Walk Abort @0x%08x\n",
541 iova);
542
543 if (!(status & (IMSTR_PF | IMSTR_TF)))
544 return IRQ_NONE;
545
546 /*
547 * Try to handle page faults and translation faults.
548 *
549 * TODO: We need to look up the faulty device based on the I/O VA. Use
550 * the IOMMU device for now.
551 */
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100552 if (!report_iommu_fault(&domain->io_domain, mmu->dev, iova, 0))
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200553 return IRQ_HANDLED;
554
555 dev_err_ratelimited(mmu->dev,
556 "Unhandled fault: status 0x%08x iova 0x%08x\n",
557 status, iova);
558
559 return IRQ_HANDLED;
560}
561
562static irqreturn_t ipmmu_irq(int irq, void *dev)
563{
564 struct ipmmu_vmsa_device *mmu = dev;
Magnus Dammdbb70692017-05-17 19:06:38 +0900565 irqreturn_t status = IRQ_NONE;
566 unsigned int i;
567 unsigned long flags;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200568
Magnus Dammdbb70692017-05-17 19:06:38 +0900569 spin_lock_irqsave(&mmu->lock, flags);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200570
Magnus Dammdbb70692017-05-17 19:06:38 +0900571 /*
572 * Check interrupts for all active contexts.
573 */
Magnus Damm5fd16342017-10-16 21:29:46 +0900574 for (i = 0; i < mmu->num_ctx; i++) {
Magnus Dammdbb70692017-05-17 19:06:38 +0900575 if (!mmu->domains[i])
576 continue;
577 if (ipmmu_domain_irq(mmu->domains[i]) == IRQ_HANDLED)
578 status = IRQ_HANDLED;
579 }
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200580
Magnus Dammdbb70692017-05-17 19:06:38 +0900581 spin_unlock_irqrestore(&mmu->lock, flags);
582
583 return status;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200584}
585
586/* -----------------------------------------------------------------------------
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200587 * IOMMU Operations
588 */
589
Magnus Damm8e73bf62017-05-17 19:06:59 +0900590static struct iommu_domain *__ipmmu_domain_alloc(unsigned type)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200591{
592 struct ipmmu_vmsa_domain *domain;
593
594 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
595 if (!domain)
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100596 return NULL;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200597
598 spin_lock_init(&domain->lock);
599
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100600 return &domain->io_domain;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200601}
602
Robin Murphy1c7e7c02017-10-13 19:23:39 +0100603static struct iommu_domain *ipmmu_domain_alloc(unsigned type)
604{
605 struct iommu_domain *io_domain = NULL;
606
607 switch (type) {
608 case IOMMU_DOMAIN_UNMANAGED:
609 io_domain = __ipmmu_domain_alloc(type);
610 break;
611
612 case IOMMU_DOMAIN_DMA:
613 io_domain = __ipmmu_domain_alloc(type);
614 if (io_domain && iommu_get_dma_cookie(io_domain)) {
615 kfree(io_domain);
616 io_domain = NULL;
617 }
618 break;
619 }
620
621 return io_domain;
622}
623
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100624static void ipmmu_domain_free(struct iommu_domain *io_domain)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200625{
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100626 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200627
628 /*
629 * Free the domain resources. We assume that all devices have already
630 * been detached.
631 */
Robin Murphy1c7e7c02017-10-13 19:23:39 +0100632 iommu_put_dma_cookie(io_domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200633 ipmmu_domain_destroy_context(domain);
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200634 free_io_pgtable_ops(domain->iop);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200635 kfree(domain);
636}
637
638static int ipmmu_attach_device(struct iommu_domain *io_domain,
639 struct device *dev)
640{
Magnus Damm7b2d5962017-07-17 22:05:41 +0900641 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
Robin Murphye4efe4a2017-10-13 19:23:41 +0100642 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100643 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200644 unsigned long flags;
Laurent Pincharta166d312014-07-24 01:36:43 +0200645 unsigned int i;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200646 int ret = 0;
647
Robin Murphye4efe4a2017-10-13 19:23:41 +0100648 if (!mmu) {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200649 dev_err(dev, "Cannot attach to IPMMU\n");
650 return -ENXIO;
651 }
652
653 spin_lock_irqsave(&domain->lock, flags);
654
655 if (!domain->mmu) {
656 /* The domain hasn't been used yet, initialize it. */
657 domain->mmu = mmu;
658 ret = ipmmu_domain_init_context(domain);
Magnus Damm5fd16342017-10-16 21:29:46 +0900659 if (ret < 0) {
660 dev_err(dev, "Unable to initialize IPMMU context\n");
661 domain->mmu = NULL;
662 } else {
663 dev_info(dev, "Using IPMMU context %u\n",
664 domain->context_id);
665 }
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200666 } else if (domain->mmu != mmu) {
667 /*
668 * Something is wrong, we can't attach two devices using
669 * different IOMMUs to the same domain.
670 */
671 dev_err(dev, "Can't attach IPMMU %s to domain on IPMMU %s\n",
672 dev_name(mmu->dev), dev_name(domain->mmu->dev));
673 ret = -EINVAL;
Magnus Damm3ae47292017-05-17 19:07:10 +0900674 } else
675 dev_info(dev, "Reusing IPMMU context %u\n", domain->context_id);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200676
677 spin_unlock_irqrestore(&domain->lock, flags);
678
679 if (ret < 0)
680 return ret;
681
Magnus Damm7b2d5962017-07-17 22:05:41 +0900682 for (i = 0; i < fwspec->num_ids; ++i)
683 ipmmu_utlb_enable(domain, fwspec->ids[i]);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200684
685 return 0;
686}
687
688static void ipmmu_detach_device(struct iommu_domain *io_domain,
689 struct device *dev)
690{
Magnus Damm7b2d5962017-07-17 22:05:41 +0900691 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100692 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pincharta166d312014-07-24 01:36:43 +0200693 unsigned int i;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200694
Magnus Damm7b2d5962017-07-17 22:05:41 +0900695 for (i = 0; i < fwspec->num_ids; ++i)
696 ipmmu_utlb_disable(domain, fwspec->ids[i]);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200697
698 /*
699 * TODO: Optimize by disabling the context when no device is attached.
700 */
701}
702
703static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova,
704 phys_addr_t paddr, size_t size, int prot)
705{
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100706 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200707
708 if (!domain)
709 return -ENODEV;
710
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200711 return domain->iop->map(domain->iop, iova, paddr, size, prot);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200712}
713
714static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova,
715 size_t size)
716{
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100717 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200718
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200719 return domain->iop->unmap(domain->iop, iova, size);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200720}
721
Robin Murphy32b12442017-09-28 15:55:01 +0100722static void ipmmu_iotlb_sync(struct iommu_domain *io_domain)
723{
724 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
725
726 if (domain->mmu)
727 ipmmu_tlb_flush_all(domain);
728}
729
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200730static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain,
731 dma_addr_t iova)
732{
Joerg Roedel5914c5f2015-03-26 13:43:16 +0100733 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200734
735 /* TODO: Is locking needed ? */
736
Laurent Pinchartf20ed392015-01-20 18:30:04 +0200737 return domain->iop->iova_to_phys(domain->iop, iova);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200738}
739
Magnus Damm7b2d5962017-07-17 22:05:41 +0900740static int ipmmu_init_platform_device(struct device *dev,
741 struct of_phandle_args *args)
Laurent Pinchart192d2042014-05-15 12:40:42 +0200742{
Magnus Damm7b2d5962017-07-17 22:05:41 +0900743 struct platform_device *ipmmu_pdev;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200744
Magnus Damm7b2d5962017-07-17 22:05:41 +0900745 ipmmu_pdev = of_find_device_by_node(args->np);
746 if (!ipmmu_pdev)
Laurent Pinchartbb590c92015-01-24 23:13:50 +0200747 return -ENODEV;
748
Robin Murphye4efe4a2017-10-13 19:23:41 +0100749 dev->iommu_fwspec->iommu_priv = platform_get_drvdata(ipmmu_pdev);
Magnus Damm383fef5f2017-05-17 19:06:48 +0900750 return 0;
Magnus Damm383fef5f2017-05-17 19:06:48 +0900751}
752
Magnus Damm58b8e8b2017-10-16 21:30:50 +0900753static bool ipmmu_slave_whitelist(struct device *dev)
754{
755 /* By default, do not allow use of IPMMU */
756 return false;
757}
758
759static const struct soc_device_attribute soc_r8a7795[] = {
760 { .soc_id = "r8a7795", },
761 { /* sentinel */ }
762};
763
Magnus Damm49558da2017-07-17 22:05:20 +0900764static int ipmmu_of_xlate(struct device *dev,
765 struct of_phandle_args *spec)
766{
Magnus Damm58b8e8b2017-10-16 21:30:50 +0900767 /* For R-Car Gen3 use a white list to opt-in slave devices */
768 if (soc_device_match(soc_r8a7795) && !ipmmu_slave_whitelist(dev))
769 return -ENODEV;
770
Magnus Damm7b2d5962017-07-17 22:05:41 +0900771 iommu_fwspec_add_ids(dev, spec->args, 1);
772
Magnus Damm49558da2017-07-17 22:05:20 +0900773 /* Initialize once - xlate() will call multiple times */
Robin Murphye4efe4a2017-10-13 19:23:41 +0100774 if (to_ipmmu(dev))
Magnus Damm49558da2017-07-17 22:05:20 +0900775 return 0;
776
Magnus Damm7b2d5962017-07-17 22:05:41 +0900777 return ipmmu_init_platform_device(dev, spec);
Magnus Damm49558da2017-07-17 22:05:20 +0900778}
779
Robin Murphy49c875f2017-10-13 19:23:42 +0100780static int ipmmu_init_arm_mapping(struct device *dev)
Magnus Damm383fef5f2017-05-17 19:06:48 +0900781{
Robin Murphye4efe4a2017-10-13 19:23:41 +0100782 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
Magnus Damm383fef5f2017-05-17 19:06:48 +0900783 struct iommu_group *group;
784 int ret;
785
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200786 /* Create a device group and add the device to it. */
787 group = iommu_group_alloc();
788 if (IS_ERR(group)) {
789 dev_err(dev, "Failed to allocate IOMMU group\n");
Robin Murphy49c875f2017-10-13 19:23:42 +0100790 return PTR_ERR(group);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200791 }
792
793 ret = iommu_group_add_device(group, dev);
794 iommu_group_put(group);
795
796 if (ret < 0) {
797 dev_err(dev, "Failed to add device to IPMMU group\n");
Robin Murphy49c875f2017-10-13 19:23:42 +0100798 return ret;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200799 }
800
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200801 /*
802 * Create the ARM mapping, used by the ARM DMA mapping core to allocate
803 * VAs. This will allocate a corresponding IOMMU domain.
804 *
805 * TODO:
806 * - Create one mapping per context (TLB).
807 * - Make the mapping size configurable ? We currently use a 2GB mapping
808 * at a 1GB offset to ensure that NULL VAs will fault.
809 */
810 if (!mmu->mapping) {
811 struct dma_iommu_mapping *mapping;
812
813 mapping = arm_iommu_create_mapping(&platform_bus_type,
Joerg Roedel720b0ce2014-05-26 13:07:01 +0200814 SZ_1G, SZ_2G);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200815 if (IS_ERR(mapping)) {
816 dev_err(mmu->dev, "failed to create ARM IOMMU mapping\n");
Laurent Pinchartb8f80bf2014-03-14 14:00:56 +0100817 ret = PTR_ERR(mapping);
818 goto error;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200819 }
820
821 mmu->mapping = mapping;
822 }
823
824 /* Attach the ARM VA mapping to the device. */
825 ret = arm_iommu_attach_device(dev, mmu->mapping);
826 if (ret < 0) {
827 dev_err(dev, "Failed to attach device to VA mapping\n");
828 goto error;
829 }
830
831 return 0;
832
833error:
Robin Murphy49c875f2017-10-13 19:23:42 +0100834 iommu_group_remove_device(dev);
835 if (mmu->mapping)
Magnus Damm383fef5f2017-05-17 19:06:48 +0900836 arm_iommu_release_mapping(mmu->mapping);
Laurent Pincharta166d312014-07-24 01:36:43 +0200837
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200838 return ret;
839}
840
Robin Murphy49c875f2017-10-13 19:23:42 +0100841static int ipmmu_add_device(struct device *dev)
Magnus Damm3ae47292017-05-17 19:07:10 +0900842{
Magnus Damm3ae47292017-05-17 19:07:10 +0900843 struct iommu_group *group;
844
Magnus Damm0fbc8b02017-05-17 19:07:20 +0900845 /*
846 * Only let through devices that have been verified in xlate()
Magnus Damm0fbc8b02017-05-17 19:07:20 +0900847 */
Robin Murphye4efe4a2017-10-13 19:23:41 +0100848 if (!to_ipmmu(dev))
Magnus Damm3ae47292017-05-17 19:07:10 +0900849 return -ENODEV;
850
Robin Murphy49c875f2017-10-13 19:23:42 +0100851 if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_IOMMU_DMA))
852 return ipmmu_init_arm_mapping(dev);
853
Magnus Damm3ae47292017-05-17 19:07:10 +0900854 group = iommu_group_get_for_dev(dev);
855 if (IS_ERR(group))
856 return PTR_ERR(group);
857
Robin Murphy49c875f2017-10-13 19:23:42 +0100858 iommu_group_put(group);
Magnus Damm3ae47292017-05-17 19:07:10 +0900859 return 0;
860}
861
Robin Murphy49c875f2017-10-13 19:23:42 +0100862static void ipmmu_remove_device(struct device *dev)
Magnus Damm3ae47292017-05-17 19:07:10 +0900863{
Robin Murphy49c875f2017-10-13 19:23:42 +0100864 arm_iommu_detach_device(dev);
Magnus Damm3ae47292017-05-17 19:07:10 +0900865 iommu_group_remove_device(dev);
866}
867
Robin Murphyb354c732017-10-13 19:23:40 +0100868static struct iommu_group *ipmmu_find_group(struct device *dev)
Magnus Damm3ae47292017-05-17 19:07:10 +0900869{
Robin Murphye4efe4a2017-10-13 19:23:41 +0100870 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
Magnus Damm3ae47292017-05-17 19:07:10 +0900871 struct iommu_group *group;
Magnus Damm3ae47292017-05-17 19:07:10 +0900872
Robin Murphye4efe4a2017-10-13 19:23:41 +0100873 if (mmu->group)
874 return iommu_group_ref_get(mmu->group);
Robin Murphyb354c732017-10-13 19:23:40 +0100875
876 group = iommu_group_alloc();
877 if (!IS_ERR(group))
Robin Murphye4efe4a2017-10-13 19:23:41 +0100878 mmu->group = group;
Magnus Damm3ae47292017-05-17 19:07:10 +0900879
880 return group;
881}
882
Magnus Damm3ae47292017-05-17 19:07:10 +0900883static const struct iommu_ops ipmmu_ops = {
Robin Murphy1c7e7c02017-10-13 19:23:39 +0100884 .domain_alloc = ipmmu_domain_alloc,
885 .domain_free = ipmmu_domain_free,
Magnus Damm3ae47292017-05-17 19:07:10 +0900886 .attach_dev = ipmmu_attach_device,
887 .detach_dev = ipmmu_detach_device,
888 .map = ipmmu_map,
889 .unmap = ipmmu_unmap,
Robin Murphy32b12442017-09-28 15:55:01 +0100890 .flush_iotlb_all = ipmmu_iotlb_sync,
891 .iotlb_sync = ipmmu_iotlb_sync,
Magnus Damm3ae47292017-05-17 19:07:10 +0900892 .map_sg = default_iommu_map_sg,
893 .iova_to_phys = ipmmu_iova_to_phys,
Robin Murphy49c875f2017-10-13 19:23:42 +0100894 .add_device = ipmmu_add_device,
895 .remove_device = ipmmu_remove_device,
Robin Murphyb354c732017-10-13 19:23:40 +0100896 .device_group = ipmmu_find_group,
Magnus Damm3ae47292017-05-17 19:07:10 +0900897 .pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K,
Magnus Damm49558da2017-07-17 22:05:20 +0900898 .of_xlate = ipmmu_of_xlate,
Magnus Damm3ae47292017-05-17 19:07:10 +0900899};
900
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200901/* -----------------------------------------------------------------------------
902 * Probe/remove and init
903 */
904
905static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
906{
907 unsigned int i;
908
909 /* Disable all contexts. */
Magnus Damm5fd16342017-10-16 21:29:46 +0900910 for (i = 0; i < mmu->num_ctx; ++i)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200911 ipmmu_write(mmu, i * IM_CTX_SIZE + IMCTR, 0);
912}
913
Magnus Damm33f3ac92017-10-16 21:29:25 +0900914static const struct ipmmu_features ipmmu_features_default = {
915 .use_ns_alias_offset = true,
Magnus Dammfd5140e2017-10-16 21:29:36 +0900916 .has_cache_leaf_nodes = false,
Magnus Damm5fd16342017-10-16 21:29:46 +0900917 .number_of_contexts = 1, /* software only tested with one context */
Magnus Dammf5c85892017-10-16 21:30:28 +0900918 .setup_imbuscr = true,
Magnus Dammc295f502017-10-16 21:30:39 +0900919 .twobit_imttbcr_sl0 = false,
Magnus Damm33f3ac92017-10-16 21:29:25 +0900920};
921
Magnus Damm58b8e8b2017-10-16 21:30:50 +0900922static const struct ipmmu_features ipmmu_features_r8a7795 = {
923 .use_ns_alias_offset = false,
924 .has_cache_leaf_nodes = true,
925 .number_of_contexts = 8,
926 .setup_imbuscr = false,
927 .twobit_imttbcr_sl0 = true,
928};
929
Magnus Damm33f3ac92017-10-16 21:29:25 +0900930static const struct of_device_id ipmmu_of_ids[] = {
931 {
932 .compatible = "renesas,ipmmu-vmsa",
933 .data = &ipmmu_features_default,
934 }, {
Magnus Damm58b8e8b2017-10-16 21:30:50 +0900935 .compatible = "renesas,ipmmu-r8a7795",
936 .data = &ipmmu_features_r8a7795,
937 }, {
Magnus Damm33f3ac92017-10-16 21:29:25 +0900938 /* Terminator */
939 },
940};
941
942MODULE_DEVICE_TABLE(of, ipmmu_of_ids);
943
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200944static int ipmmu_probe(struct platform_device *pdev)
945{
946 struct ipmmu_vmsa_device *mmu;
947 struct resource *res;
948 int irq;
949 int ret;
950
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200951 mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL);
952 if (!mmu) {
953 dev_err(&pdev->dev, "cannot allocate device data\n");
954 return -ENOMEM;
955 }
956
957 mmu->dev = &pdev->dev;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200958 mmu->num_utlbs = 32;
Magnus Dammdbb70692017-05-17 19:06:38 +0900959 spin_lock_init(&mmu->lock);
960 bitmap_zero(mmu->ctx, IPMMU_CTX_MAX);
Magnus Damm33f3ac92017-10-16 21:29:25 +0900961 mmu->features = of_device_get_match_data(&pdev->dev);
Magnus Damm1c894222017-10-16 21:30:07 +0900962 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40));
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200963
964 /* Map I/O memory and request IRQ. */
965 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
966 mmu->base = devm_ioremap_resource(&pdev->dev, res);
967 if (IS_ERR(mmu->base))
968 return PTR_ERR(mmu->base);
969
Laurent Pinchart275f5052014-03-17 01:02:46 +0100970 /*
971 * The IPMMU has two register banks, for secure and non-secure modes.
972 * The bank mapped at the beginning of the IPMMU address space
973 * corresponds to the running mode of the CPU. When running in secure
974 * mode the non-secure register bank is also available at an offset.
975 *
976 * Secure mode operation isn't clearly documented and is thus currently
977 * not implemented in the driver. Furthermore, preliminary tests of
978 * non-secure operation with the main register bank were not successful.
979 * Offset the registers base unconditionally to point to the non-secure
980 * alias space for now.
981 */
Magnus Damm33f3ac92017-10-16 21:29:25 +0900982 if (mmu->features->use_ns_alias_offset)
983 mmu->base += IM_NS_ALIAS_OFFSET;
Laurent Pinchart275f5052014-03-17 01:02:46 +0100984
Magnus Damm5fd16342017-10-16 21:29:46 +0900985 mmu->num_ctx = min_t(unsigned int, IPMMU_CTX_MAX,
986 mmu->features->number_of_contexts);
987
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200988 irq = platform_get_irq(pdev, 0);
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200989
Magnus Dammfd5140e2017-10-16 21:29:36 +0900990 /*
991 * Determine if this IPMMU instance is a root device by checking for
992 * the lack of has_cache_leaf_nodes flag or renesas,ipmmu-main property.
993 */
994 if (!mmu->features->has_cache_leaf_nodes ||
995 !of_find_property(pdev->dev.of_node, "renesas,ipmmu-main", NULL))
996 mmu->root = mmu;
997 else
998 mmu->root = ipmmu_find_root();
Laurent Pinchartd25a2a12014-04-02 12:47:37 +0200999
Magnus Dammfd5140e2017-10-16 21:29:36 +09001000 /*
1001 * Wait until the root device has been registered for sure.
1002 */
1003 if (!mmu->root)
1004 return -EPROBE_DEFER;
1005
1006 /* Root devices have mandatory IRQs */
1007 if (ipmmu_is_root(mmu)) {
1008 if (irq < 0) {
1009 dev_err(&pdev->dev, "no IRQ found\n");
1010 return irq;
1011 }
1012
1013 ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0,
1014 dev_name(&pdev->dev), mmu);
1015 if (ret < 0) {
1016 dev_err(&pdev->dev, "failed to request IRQ %d\n", irq);
1017 return ret;
1018 }
1019
1020 ipmmu_device_reset(mmu);
1021 }
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001022
Magnus Dammcda52fc2017-10-16 21:29:57 +09001023 /*
1024 * Register the IPMMU to the IOMMU subsystem in the following cases:
1025 * - R-Car Gen2 IPMMU (all devices registered)
1026 * - R-Car Gen3 IPMMU (leaf devices only - skip root IPMMU-MM device)
1027 */
1028 if (!mmu->features->has_cache_leaf_nodes || !ipmmu_is_root(mmu)) {
1029 ret = iommu_device_sysfs_add(&mmu->iommu, &pdev->dev, NULL,
1030 dev_name(&pdev->dev));
1031 if (ret)
1032 return ret;
Magnus Damm7af9a5f2017-08-21 14:53:35 +09001033
Magnus Dammcda52fc2017-10-16 21:29:57 +09001034 iommu_device_set_ops(&mmu->iommu, &ipmmu_ops);
1035 iommu_device_set_fwnode(&mmu->iommu,
1036 &pdev->dev.of_node->fwnode);
Magnus Damm01da21e2017-07-17 22:05:10 +09001037
Magnus Dammcda52fc2017-10-16 21:29:57 +09001038 ret = iommu_device_register(&mmu->iommu);
1039 if (ret)
1040 return ret;
1041
1042#if defined(CONFIG_IOMMU_DMA)
1043 if (!iommu_present(&platform_bus_type))
1044 bus_set_iommu(&platform_bus_type, &ipmmu_ops);
1045#endif
1046 }
Magnus Damm01da21e2017-07-17 22:05:10 +09001047
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001048 /*
1049 * We can't create the ARM mapping here as it requires the bus to have
1050 * an IOMMU, which only happens when bus_set_iommu() is called in
1051 * ipmmu_init() after the probe function returns.
1052 */
1053
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001054 platform_set_drvdata(pdev, mmu);
1055
1056 return 0;
1057}
1058
1059static int ipmmu_remove(struct platform_device *pdev)
1060{
1061 struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev);
1062
Magnus Damm7af9a5f2017-08-21 14:53:35 +09001063 iommu_device_sysfs_remove(&mmu->iommu);
Magnus Damm01da21e2017-07-17 22:05:10 +09001064 iommu_device_unregister(&mmu->iommu);
1065
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001066 arm_iommu_release_mapping(mmu->mapping);
1067
1068 ipmmu_device_reset(mmu);
1069
1070 return 0;
1071}
1072
1073static struct platform_driver ipmmu_driver = {
1074 .driver = {
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001075 .name = "ipmmu-vmsa",
Laurent Pinchart275f5052014-03-17 01:02:46 +01001076 .of_match_table = of_match_ptr(ipmmu_of_ids),
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001077 },
1078 .probe = ipmmu_probe,
1079 .remove = ipmmu_remove,
1080};
1081
1082static int __init ipmmu_init(void)
1083{
Magnus Dammcda52fc2017-10-16 21:29:57 +09001084 static bool setup_done;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001085 int ret;
1086
Magnus Dammcda52fc2017-10-16 21:29:57 +09001087 if (setup_done)
1088 return 0;
1089
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001090 ret = platform_driver_register(&ipmmu_driver);
1091 if (ret < 0)
1092 return ret;
1093
Magnus Dammcda52fc2017-10-16 21:29:57 +09001094#if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA)
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001095 if (!iommu_present(&platform_bus_type))
1096 bus_set_iommu(&platform_bus_type, &ipmmu_ops);
Magnus Dammcda52fc2017-10-16 21:29:57 +09001097#endif
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001098
Magnus Dammcda52fc2017-10-16 21:29:57 +09001099 setup_done = true;
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001100 return 0;
1101}
1102
1103static void __exit ipmmu_exit(void)
1104{
1105 return platform_driver_unregister(&ipmmu_driver);
1106}
1107
1108subsys_initcall(ipmmu_init);
1109module_exit(ipmmu_exit);
1110
Robin Murphyb0c560f2018-01-09 16:17:27 +00001111IOMMU_OF_DECLARE(ipmmu_vmsa_iommu_of, "renesas,ipmmu-vmsa");
1112IOMMU_OF_DECLARE(ipmmu_r8a7795_iommu_of, "renesas,ipmmu-r8a7795");
Magnus Dammcda52fc2017-10-16 21:29:57 +09001113
Laurent Pinchartd25a2a12014-04-02 12:47:37 +02001114MODULE_DESCRIPTION("IOMMU API for Renesas VMSA-compatible IPMMU");
1115MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
1116MODULE_LICENSE("GPL v2");