Skip to content

Commit 4dcfa60

Browse files
author
Russell King
committed
ARM: DMA-API: better handing of DMA masks for coherent allocations
We need to start treating DMA masks as something which is specific to the bus that the device resides on, otherwise we're going to hit all sorts of nasty issues with LPAE and 32-bit DMA controllers in >32-bit systems, where memory is offset from PFN 0. In order to start doing this, we convert the DMA mask to a PFN using the device specific dma_to_pfn() macro. This is the reverse of the pfn_to_dma() macro which is used to get the DMA address for the device. This gives us a PFN mask, which we can then check against the PFN limit of the DMA zone. Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
1 parent 42536b9 commit 4dcfa60

3 files changed

Lines changed: 49 additions & 6 deletions

File tree

‎arch/arm/mm/dma-mapping.c‎

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ EXPORT_SYMBOL(arm_coherent_dma_ops);
159159

160160
static u64 get_coherent_dma_mask(struct device *dev)
161161
{
162-
u64 mask = (u64)arm_dma_limit;
162+
u64 mask = (u64)DMA_BIT_MASK(32);
163163

164164
if (dev) {
165165
mask = dev->coherent_dma_mask;
@@ -173,10 +173,30 @@ static u64 get_coherent_dma_mask(struct device *dev)
173173
return 0;
174174
}
175175

176-
if ((~mask) & (u64)arm_dma_limit) {
177-
dev_warn(dev, "coherent DMA mask %#llx is smaller "
178-
"than system GFP_DMA mask %#llx\n",
179-
mask, (u64)arm_dma_limit);
176+
/*
177+
* If the mask allows for more memory than we can address,
178+
* and we actually have that much memory, then fail the
179+
* allocation.
180+
*/
181+
if (sizeof(mask) != sizeof(dma_addr_t) &&
182+
mask > (dma_addr_t)~0 &&
183+
dma_to_pfn(dev, ~0) > arm_dma_pfn_limit) {
184+
dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
185+
mask);
186+
dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
187+
return 0;
188+
}
189+
190+
/*
191+
* Now check that the mask, when translated to a PFN,
192+
* fits within the allowable addresses which we can
193+
* allocate.
194+
*/
195+
if (dma_to_pfn(dev, mask) < arm_dma_pfn_limit) {
196+
dev_warn(dev, "Coherent DMA mask %#llx (pfn %#lx-%#lx) covers a smaller range of system memory than the DMA zone pfn 0x0-%#lx\n",
197+
mask,
198+
dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
199+
arm_dma_pfn_limit + 1);
180200
return 0;
181201
}
182202
}
@@ -1007,8 +1027,27 @@ void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
10071027
*/
10081028
int dma_supported(struct device *dev, u64 mask)
10091029
{
1010-
if (mask < (u64)arm_dma_limit)
1030+
unsigned long limit;
1031+
1032+
/*
1033+
* If the mask allows for more memory than we can address,
1034+
* and we actually have that much memory, then we must
1035+
* indicate that DMA to this device is not supported.
1036+
*/
1037+
if (sizeof(mask) != sizeof(dma_addr_t) &&
1038+
mask > (dma_addr_t)~0 &&
1039+
dma_to_pfn(dev, ~0) > arm_dma_pfn_limit)
1040+
return 0;
1041+
1042+
/*
1043+
* Translate the device's DMA mask to a PFN limit. This
1044+
* PFN number includes the page which we can DMA to.
1045+
*/
1046+
limit = dma_to_pfn(dev, mask);
1047+
1048+
if (limit < arm_dma_pfn_limit)
10111049
return 0;
1050+
10121051
return 1;
10131052
}
10141053
EXPORT_SYMBOL(dma_supported);

‎arch/arm/mm/init.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ EXPORT_SYMBOL(arm_dma_zone_size);
218218
* so a successful GFP_DMA allocation will always satisfy this.
219219
*/
220220
phys_addr_t arm_dma_limit;
221+
unsigned long arm_dma_pfn_limit;
221222

222223
static void __init arm_adjust_dma_zone(unsigned long *size, unsigned long *hole,
223224
unsigned long dma_size)
@@ -240,6 +241,7 @@ void __init setup_dma_zone(const struct machine_desc *mdesc)
240241
arm_dma_limit = PHYS_OFFSET + arm_dma_zone_size - 1;
241242
} else
242243
arm_dma_limit = 0xffffffff;
244+
arm_dma_pfn_limit = arm_dma_limit >> PAGE_SHIFT;
243245
#endif
244246
}
245247

‎arch/arm/mm/mm.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ extern __init void add_static_vm_early(struct static_vm *svm);
8181

8282
#ifdef CONFIG_ZONE_DMA
8383
extern phys_addr_t arm_dma_limit;
84+
extern unsigned long arm_dma_pfn_limit;
8485
#else
8586
#define arm_dma_limit ((phys_addr_t)~0)
87+
#define arm_dma_pfn_limit (~0ul >> PAGE_SHIFT)
8688
#endif
8789

8890
extern phys_addr_t arm_lowmem_limit;

0 commit comments

Comments
 (0)