Skip to content

Commit ce273db

Browse files
committed
Merge branch 'iommu/iommu-priv' of git://git.kernel.org/pub/scm/linux/kernel/git/will/linux into arm/core
2 parents 93fa6cf + 14b4dba commit ce273db

File tree

12 files changed

+82
-48
lines changed

12 files changed

+82
-48
lines changed

Documentation/DMA-attributes.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,13 @@ So, this provides a way for drivers to avoid those error messages on calls
143143
where allocation failures are not a problem, and shouldn't bother the logs.
144144

145145
NOTE: At the moment DMA_ATTR_NO_WARN is only implemented on PowerPC.
146+
147+
DMA_ATTR_PRIVILEGED
148+
------------------------------
149+
150+
Some advanced peripherals such as remote processors and GPUs perform
151+
accesses to DMA buffers in both privileged "supervisor" and unprivileged
152+
"user" modes. This attribute is used to indicate to the DMA-mapping
153+
subsystem that the buffer is fully accessible at the elevated privilege
154+
level (and ideally inaccessible or at least read-only at the
155+
lesser-privileged levels).

arch/arm/mm/dma-mapping.c

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,25 @@ core_initcall(dma_debug_do_init);
11711171

11721172
#ifdef CONFIG_ARM_DMA_USE_IOMMU
11731173

1174+
static int __dma_info_to_prot(enum dma_data_direction dir, unsigned long attrs)
1175+
{
1176+
int prot = 0;
1177+
1178+
if (attrs & DMA_ATTR_PRIVILEGED)
1179+
prot |= IOMMU_PRIV;
1180+
1181+
switch (dir) {
1182+
case DMA_BIDIRECTIONAL:
1183+
return prot | IOMMU_READ | IOMMU_WRITE;
1184+
case DMA_TO_DEVICE:
1185+
return prot | IOMMU_READ;
1186+
case DMA_FROM_DEVICE:
1187+
return prot | IOMMU_WRITE;
1188+
default:
1189+
return prot;
1190+
}
1191+
}
1192+
11741193
/* IOMMU */
11751194

11761195
static int extend_iommu_mapping(struct dma_iommu_mapping *mapping);
@@ -1394,7 +1413,8 @@ __iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
13941413
* Create a mapping in device IO address space for specified pages
13951414
*/
13961415
static dma_addr_t
1397-
__iommu_create_mapping(struct device *dev, struct page **pages, size_t size)
1416+
__iommu_create_mapping(struct device *dev, struct page **pages, size_t size,
1417+
unsigned long attrs)
13981418
{
13991419
struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
14001420
unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
@@ -1419,7 +1439,7 @@ __iommu_create_mapping(struct device *dev, struct page **pages, size_t size)
14191439

14201440
len = (j - i) << PAGE_SHIFT;
14211441
ret = iommu_map(mapping->domain, iova, phys, len,
1422-
IOMMU_READ|IOMMU_WRITE);
1442+
__dma_info_to_prot(DMA_BIDIRECTIONAL, attrs));
14231443
if (ret < 0)
14241444
goto fail;
14251445
iova += len;
@@ -1476,7 +1496,8 @@ static struct page **__iommu_get_pages(void *cpu_addr, unsigned long attrs)
14761496
}
14771497

14781498
static void *__iommu_alloc_simple(struct device *dev, size_t size, gfp_t gfp,
1479-
dma_addr_t *handle, int coherent_flag)
1499+
dma_addr_t *handle, int coherent_flag,
1500+
unsigned long attrs)
14801501
{
14811502
struct page *page;
14821503
void *addr;
@@ -1488,7 +1509,7 @@ static void *__iommu_alloc_simple(struct device *dev, size_t size, gfp_t gfp,
14881509
if (!addr)
14891510
return NULL;
14901511

1491-
*handle = __iommu_create_mapping(dev, &page, size);
1512+
*handle = __iommu_create_mapping(dev, &page, size, attrs);
14921513
if (*handle == DMA_ERROR_CODE)
14931514
goto err_mapping;
14941515

@@ -1522,7 +1543,7 @@ static void *__arm_iommu_alloc_attrs(struct device *dev, size_t size,
15221543

15231544
if (coherent_flag == COHERENT || !gfpflags_allow_blocking(gfp))
15241545
return __iommu_alloc_simple(dev, size, gfp, handle,
1525-
coherent_flag);
1546+
coherent_flag, attrs);
15261547

15271548
/*
15281549
* Following is a work-around (a.k.a. hack) to prevent pages
@@ -1537,7 +1558,7 @@ static void *__arm_iommu_alloc_attrs(struct device *dev, size_t size,
15371558
if (!pages)
15381559
return NULL;
15391560

1540-
*handle = __iommu_create_mapping(dev, pages, size);
1561+
*handle = __iommu_create_mapping(dev, pages, size, attrs);
15411562
if (*handle == DMA_ERROR_CODE)
15421563
goto err_buffer;
15431564

@@ -1672,27 +1693,6 @@ static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
16721693
GFP_KERNEL);
16731694
}
16741695

1675-
static int __dma_direction_to_prot(enum dma_data_direction dir)
1676-
{
1677-
int prot;
1678-
1679-
switch (dir) {
1680-
case DMA_BIDIRECTIONAL:
1681-
prot = IOMMU_READ | IOMMU_WRITE;
1682-
break;
1683-
case DMA_TO_DEVICE:
1684-
prot = IOMMU_READ;
1685-
break;
1686-
case DMA_FROM_DEVICE:
1687-
prot = IOMMU_WRITE;
1688-
break;
1689-
default:
1690-
prot = 0;
1691-
}
1692-
1693-
return prot;
1694-
}
1695-
16961696
/*
16971697
* Map a part of the scatter-gather list into contiguous io address space
16981698
*/
@@ -1722,7 +1722,7 @@ static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
17221722
if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
17231723
__dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
17241724

1725-
prot = __dma_direction_to_prot(dir);
1725+
prot = __dma_info_to_prot(dir, attrs);
17261726

17271727
ret = iommu_map(mapping->domain, iova, phys, len, prot);
17281728
if (ret < 0)
@@ -1930,7 +1930,7 @@ static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *p
19301930
if (dma_addr == DMA_ERROR_CODE)
19311931
return dma_addr;
19321932

1933-
prot = __dma_direction_to_prot(dir);
1933+
prot = __dma_info_to_prot(dir, attrs);
19341934

19351935
ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, prot);
19361936
if (ret < 0)
@@ -2036,7 +2036,7 @@ static dma_addr_t arm_iommu_map_resource(struct device *dev,
20362036
if (dma_addr == DMA_ERROR_CODE)
20372037
return dma_addr;
20382038

2039-
prot = __dma_direction_to_prot(dir) | IOMMU_MMIO;
2039+
prot = __dma_info_to_prot(dir, attrs) | IOMMU_MMIO;
20402040

20412041
ret = iommu_map(mapping->domain, dma_addr, addr, len, prot);
20422042
if (ret < 0)

arch/arm64/mm/dma-mapping.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ static void *__iommu_alloc_attrs(struct device *dev, size_t size,
558558
unsigned long attrs)
559559
{
560560
bool coherent = is_device_dma_coherent(dev);
561-
int ioprot = dma_direction_to_prot(DMA_BIDIRECTIONAL, coherent);
561+
int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs);
562562
size_t iosize = size;
563563
void *addr;
564564

@@ -712,7 +712,7 @@ static dma_addr_t __iommu_map_page(struct device *dev, struct page *page,
712712
unsigned long attrs)
713713
{
714714
bool coherent = is_device_dma_coherent(dev);
715-
int prot = dma_direction_to_prot(dir, coherent);
715+
int prot = dma_info_to_prot(dir, coherent, attrs);
716716
dma_addr_t dev_addr = iommu_dma_map_page(dev, page, offset, size, prot);
717717

718718
if (!iommu_dma_mapping_error(dev, dev_addr) &&
@@ -770,7 +770,7 @@ static int __iommu_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
770770
__iommu_sync_sg_for_device(dev, sgl, nelems, dir);
771771

772772
return iommu_dma_map_sg(dev, sgl, nelems,
773-
dma_direction_to_prot(dir, coherent));
773+
dma_info_to_prot(dir, coherent, attrs));
774774
}
775775

776776
static void __iommu_unmap_sg_attrs(struct device *dev,

drivers/dma/pl330.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,9 +1867,10 @@ static int dmac_alloc_resources(struct pl330_dmac *pl330)
18671867
* Alloc MicroCode buffer for 'chans' Channel threads.
18681868
* A channel's buffer offset is (Channel_Id * MCODE_BUFF_PERCHAN)
18691869
*/
1870-
pl330->mcode_cpu = dma_alloc_coherent(pl330->ddma.dev,
1870+
pl330->mcode_cpu = dma_alloc_attrs(pl330->ddma.dev,
18711871
chans * pl330->mcbufsz,
1872-
&pl330->mcode_bus, GFP_KERNEL);
1872+
&pl330->mcode_bus, GFP_KERNEL,
1873+
DMA_ATTR_PRIVILEGED);
18731874
if (!pl330->mcode_cpu) {
18741875
dev_err(pl330->ddma.dev, "%s:%d Can't allocate memory!\n",
18751876
__func__, __LINE__);

drivers/iommu/arm-smmu-v3.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,6 @@
269269
#define STRTAB_STE_1_SHCFG_INCOMING 1UL
270270
#define STRTAB_STE_1_SHCFG_SHIFT 44
271271

272-
#define STRTAB_STE_1_PRIVCFG_UNPRIV 2UL
273-
#define STRTAB_STE_1_PRIVCFG_SHIFT 48
274-
275272
#define STRTAB_STE_2_S2VMID_SHIFT 0
276273
#define STRTAB_STE_2_S2VMID_MASK 0xffffUL
277274
#define STRTAB_STE_2_VTCR_SHIFT 32
@@ -1076,9 +1073,7 @@ static void arm_smmu_write_strtab_ent(struct arm_smmu_device *smmu, u32 sid,
10761073
#ifdef CONFIG_PCI_ATS
10771074
STRTAB_STE_1_EATS_TRANS << STRTAB_STE_1_EATS_SHIFT |
10781075
#endif
1079-
STRTAB_STE_1_STRW_NSEL1 << STRTAB_STE_1_STRW_SHIFT |
1080-
STRTAB_STE_1_PRIVCFG_UNPRIV <<
1081-
STRTAB_STE_1_PRIVCFG_SHIFT);
1076+
STRTAB_STE_1_STRW_NSEL1 << STRTAB_STE_1_STRW_SHIFT);
10821077

10831078
if (smmu->features & ARM_SMMU_FEAT_STALLS)
10841079
dst[1] |= cpu_to_le64(STRTAB_STE_1_S1STALLD);

drivers/iommu/arm-smmu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,7 @@ static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
12171217
continue;
12181218

12191219
s2cr[idx].type = type;
1220-
s2cr[idx].privcfg = S2CR_PRIVCFG_UNPRIV;
1220+
s2cr[idx].privcfg = S2CR_PRIVCFG_DEFAULT;
12211221
s2cr[idx].cbndx = cbndx;
12221222
arm_smmu_write_s2cr(smmu, idx);
12231223
}

drivers/iommu/dma-iommu.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,16 +245,22 @@ int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base,
245245
EXPORT_SYMBOL(iommu_dma_init_domain);
246246

247247
/**
248-
* dma_direction_to_prot - Translate DMA API directions to IOMMU API page flags
248+
* dma_info_to_prot - Translate DMA API directions and attributes to IOMMU API
249+
* page flags.
249250
* @dir: Direction of DMA transfer
250251
* @coherent: Is the DMA master cache-coherent?
252+
* @attrs: DMA attributes for the mapping
251253
*
252254
* Return: corresponding IOMMU API page protection flags
253255
*/
254-
int dma_direction_to_prot(enum dma_data_direction dir, bool coherent)
256+
int dma_info_to_prot(enum dma_data_direction dir, bool coherent,
257+
unsigned long attrs)
255258
{
256259
int prot = coherent ? IOMMU_CACHE : 0;
257260

261+
if (attrs & DMA_ATTR_PRIVILEGED)
262+
prot |= IOMMU_PRIV;
263+
258264
switch (dir) {
259265
case DMA_BIDIRECTIONAL:
260266
return prot | IOMMU_READ | IOMMU_WRITE;
@@ -697,7 +703,7 @@ dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys,
697703
size_t size, enum dma_data_direction dir, unsigned long attrs)
698704
{
699705
return __iommu_dma_map(dev, phys, size,
700-
dma_direction_to_prot(dir, false) | IOMMU_MMIO);
706+
dma_info_to_prot(dir, false, attrs) | IOMMU_MMIO);
701707
}
702708

703709
void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle,

drivers/iommu/io-pgtable-arm-v7s.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,9 @@ static arm_v7s_iopte arm_v7s_prot_to_pte(int prot, int lvl,
265265
if (!(prot & IOMMU_MMIO))
266266
pte |= ARM_V7S_ATTR_TEX(1);
267267
if (ap) {
268-
pte |= ARM_V7S_PTE_AF | ARM_V7S_PTE_AP_UNPRIV;
268+
pte |= ARM_V7S_PTE_AF;
269+
if (!(prot & IOMMU_PRIV))
270+
pte |= ARM_V7S_PTE_AP_UNPRIV;
269271
if (!(prot & IOMMU_WRITE))
270272
pte |= ARM_V7S_PTE_AP_RDONLY;
271273
}
@@ -288,6 +290,8 @@ static int arm_v7s_pte_to_prot(arm_v7s_iopte pte, int lvl)
288290

289291
if (!(attr & ARM_V7S_PTE_AP_RDONLY))
290292
prot |= IOMMU_WRITE;
293+
if (!(attr & ARM_V7S_PTE_AP_UNPRIV))
294+
prot |= IOMMU_PRIV;
291295
if ((attr & (ARM_V7S_TEX_MASK << ARM_V7S_TEX_SHIFT)) == 0)
292296
prot |= IOMMU_MMIO;
293297
else if (pte & ARM_V7S_ATTR_C)

drivers/iommu/io-pgtable-arm.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,14 @@ static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data,
350350

351351
if (data->iop.fmt == ARM_64_LPAE_S1 ||
352352
data->iop.fmt == ARM_32_LPAE_S1) {
353-
pte = ARM_LPAE_PTE_AP_UNPRIV | ARM_LPAE_PTE_nG;
353+
pte = ARM_LPAE_PTE_nG;
354354

355355
if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
356356
pte |= ARM_LPAE_PTE_AP_RDONLY;
357357

358+
if (!(prot & IOMMU_PRIV))
359+
pte |= ARM_LPAE_PTE_AP_UNPRIV;
360+
358361
if (prot & IOMMU_MMIO)
359362
pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV
360363
<< ARM_LPAE_PTE_ATTRINDX_SHIFT);

include/linux/dma-iommu.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base,
3535
u64 size, struct device *dev);
3636

3737
/* General helpers for DMA-API <-> IOMMU-API interaction */
38-
int dma_direction_to_prot(enum dma_data_direction dir, bool coherent);
38+
int dma_info_to_prot(enum dma_data_direction dir, bool coherent,
39+
unsigned long attrs);
3940

4041
/*
4142
* These implement the bulk of the relevant DMA mapping callbacks, but require

0 commit comments

Comments
 (0)