Skip to content

Commit d7293f7

Browse files
author
Christoph Hellwig
committed
Merge branch 'for-next/zone-dma' of git://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux into dma-mapping-for-next
Pull in a stable branch from the arm64 tree that adds the zone_dma_bits variable to avoid creating hard to resolve conflicts with that addition.
2 parents 68a33b1 + bff3b04 commit d7293f7

9 files changed

Lines changed: 107 additions & 66 deletions

File tree

‎arch/arm64/Kconfig‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ config GENERIC_CSUM
265265
config GENERIC_CALIBRATE_DELAY
266266
def_bool y
267267

268+
config ZONE_DMA
269+
bool "Support DMA zone" if EXPERT
270+
default y
271+
268272
config ZONE_DMA32
269273
bool "Support DMA32 zone" if EXPERT
270274
default y

‎arch/arm64/mm/init.c‎

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/sort.h>
2121
#include <linux/of.h>
2222
#include <linux/of_fdt.h>
23+
#include <linux/dma-direct.h>
2324
#include <linux/dma-mapping.h>
2425
#include <linux/dma-contiguous.h>
2526
#include <linux/efi.h>
@@ -41,6 +42,8 @@
4142
#include <asm/tlb.h>
4243
#include <asm/alternative.h>
4344

45+
#define ARM64_ZONE_DMA_BITS 30
46+
4447
/*
4548
* We need to be able to catch inadvertent references to memstart_addr
4649
* that occur (potentially in generic code) before arm64_memblock_init()
@@ -56,7 +59,14 @@ EXPORT_SYMBOL(physvirt_offset);
5659
struct page *vmemmap __ro_after_init;
5760
EXPORT_SYMBOL(vmemmap);
5861

62+
/*
63+
* We create both ZONE_DMA and ZONE_DMA32. ZONE_DMA covers the first 1G of
64+
* memory as some devices, namely the Raspberry Pi 4, have peripherals with
65+
* this limited view of the memory. ZONE_DMA32 will cover the rest of the 32
66+
* bit addressable memory area.
67+
*/
5968
phys_addr_t arm64_dma_phys_limit __ro_after_init;
69+
static phys_addr_t arm64_dma32_phys_limit __ro_after_init;
6070

6171
#ifdef CONFIG_KEXEC_CORE
6272
/*
@@ -81,7 +91,7 @@ static void __init reserve_crashkernel(void)
8191

8292
if (crash_base == 0) {
8393
/* Current arm64 boot protocol requires 2MB alignment */
84-
crash_base = memblock_find_in_range(0, ARCH_LOW_ADDRESS_LIMIT,
94+
crash_base = memblock_find_in_range(0, arm64_dma32_phys_limit,
8595
crash_size, SZ_2M);
8696
if (crash_base == 0) {
8797
pr_warn("cannot allocate crashkernel (size:0x%llx)\n",
@@ -169,15 +179,16 @@ static void __init reserve_elfcorehdr(void)
169179
{
170180
}
171181
#endif /* CONFIG_CRASH_DUMP */
182+
172183
/*
173-
* Return the maximum physical address for ZONE_DMA32 (DMA_BIT_MASK(32)). It
174-
* currently assumes that for memory starting above 4G, 32-bit devices will
175-
* use a DMA offset.
184+
* Return the maximum physical address for a zone with a given address size
185+
* limit. It currently assumes that for memory starting above 4G, 32-bit
186+
* devices will use a DMA offset.
176187
*/
177-
static phys_addr_t __init max_zone_dma_phys(void)
188+
static phys_addr_t __init max_zone_phys(unsigned int zone_bits)
178189
{
179-
phys_addr_t offset = memblock_start_of_DRAM() & GENMASK_ULL(63, 32);
180-
return min(offset + (1ULL << 32), memblock_end_of_DRAM());
190+
phys_addr_t offset = memblock_start_of_DRAM() & GENMASK_ULL(63, zone_bits);
191+
return min(offset + (1ULL << zone_bits), memblock_end_of_DRAM());
181192
}
182193

183194
#ifdef CONFIG_NUMA
@@ -186,8 +197,11 @@ static void __init zone_sizes_init(unsigned long min, unsigned long max)
186197
{
187198
unsigned long max_zone_pfns[MAX_NR_ZONES] = {0};
188199

200+
#ifdef CONFIG_ZONE_DMA
201+
max_zone_pfns[ZONE_DMA] = PFN_DOWN(arm64_dma_phys_limit);
202+
#endif
189203
#ifdef CONFIG_ZONE_DMA32
190-
max_zone_pfns[ZONE_DMA32] = PFN_DOWN(max_zone_dma_phys());
204+
max_zone_pfns[ZONE_DMA32] = PFN_DOWN(arm64_dma32_phys_limit);
191205
#endif
192206
max_zone_pfns[ZONE_NORMAL] = max;
193207

@@ -200,16 +214,21 @@ static void __init zone_sizes_init(unsigned long min, unsigned long max)
200214
{
201215
struct memblock_region *reg;
202216
unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
203-
unsigned long max_dma = min;
217+
unsigned long max_dma32 = min;
218+
unsigned long __maybe_unused max_dma = min;
204219

205220
memset(zone_size, 0, sizeof(zone_size));
206221

207-
/* 4GB maximum for 32-bit only capable devices */
208-
#ifdef CONFIG_ZONE_DMA32
222+
#ifdef CONFIG_ZONE_DMA
209223
max_dma = PFN_DOWN(arm64_dma_phys_limit);
210-
zone_size[ZONE_DMA32] = max_dma - min;
224+
zone_size[ZONE_DMA] = max_dma - min;
225+
max_dma32 = max_dma;
226+
#endif
227+
#ifdef CONFIG_ZONE_DMA32
228+
max_dma32 = PFN_DOWN(arm64_dma32_phys_limit);
229+
zone_size[ZONE_DMA32] = max_dma32 - max_dma;
211230
#endif
212-
zone_size[ZONE_NORMAL] = max - max_dma;
231+
zone_size[ZONE_NORMAL] = max - max_dma32;
213232

214233
memcpy(zhole_size, zone_size, sizeof(zhole_size));
215234

@@ -219,16 +238,22 @@ static void __init zone_sizes_init(unsigned long min, unsigned long max)
219238

220239
if (start >= max)
221240
continue;
222-
223-
#ifdef CONFIG_ZONE_DMA32
241+
#ifdef CONFIG_ZONE_DMA
224242
if (start < max_dma) {
225-
unsigned long dma_end = min(end, max_dma);
226-
zhole_size[ZONE_DMA32] -= dma_end - start;
243+
unsigned long dma_end = min_not_zero(end, max_dma);
244+
zhole_size[ZONE_DMA] -= dma_end - start;
227245
}
228246
#endif
229-
if (end > max_dma) {
247+
#ifdef CONFIG_ZONE_DMA32
248+
if (start < max_dma32) {
249+
unsigned long dma32_end = min(end, max_dma32);
250+
unsigned long dma32_start = max(start, max_dma);
251+
zhole_size[ZONE_DMA32] -= dma32_end - dma32_start;
252+
}
253+
#endif
254+
if (end > max_dma32) {
230255
unsigned long normal_end = min(end, max);
231-
unsigned long normal_start = max(start, max_dma);
256+
unsigned long normal_start = max(start, max_dma32);
232257
zhole_size[ZONE_NORMAL] -= normal_end - normal_start;
233258
}
234259
}
@@ -418,19 +443,23 @@ void __init arm64_memblock_init(void)
418443

419444
early_init_fdt_scan_reserved_mem();
420445

421-
/* 4GB maximum for 32-bit only capable devices */
446+
if (IS_ENABLED(CONFIG_ZONE_DMA)) {
447+
zone_dma_bits = ARM64_ZONE_DMA_BITS;
448+
arm64_dma_phys_limit = max_zone_phys(ARM64_ZONE_DMA_BITS);
449+
}
450+
422451
if (IS_ENABLED(CONFIG_ZONE_DMA32))
423-
arm64_dma_phys_limit = max_zone_dma_phys();
452+
arm64_dma32_phys_limit = max_zone_phys(32);
424453
else
425-
arm64_dma_phys_limit = PHYS_MASK + 1;
454+
arm64_dma32_phys_limit = PHYS_MASK + 1;
426455

427456
reserve_crashkernel();
428457

429458
reserve_elfcorehdr();
430459

431460
high_memory = __va(memblock_end_of_DRAM() - 1) + 1;
432461

433-
dma_contiguous_reserve(arm64_dma_phys_limit);
462+
dma_contiguous_reserve(arm64_dma32_phys_limit);
434463
}
435464

436465
void __init bootmem_init(void)
@@ -534,7 +563,7 @@ static void __init free_unused_memmap(void)
534563
void __init mem_init(void)
535564
{
536565
if (swiotlb_force == SWIOTLB_FORCE ||
537-
max_pfn > (arm64_dma_phys_limit >> PAGE_SHIFT))
566+
max_pfn > PFN_DOWN(arm64_dma_phys_limit ? : arm64_dma32_phys_limit))
538567
swiotlb_init(1);
539568
else
540569
swiotlb_force = SWIOTLB_NO_FORCE;

‎arch/powerpc/include/asm/page.h‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -329,13 +329,4 @@ struct vm_area_struct;
329329
#endif /* __ASSEMBLY__ */
330330
#include <asm/slice.h>
331331

332-
/*
333-
* Allow 30-bit DMA for very limited Broadcom wifi chips on many powerbooks.
334-
*/
335-
#ifdef CONFIG_PPC32
336-
#define ARCH_ZONE_DMA_BITS 30
337-
#else
338-
#define ARCH_ZONE_DMA_BITS 31
339-
#endif
340-
341332
#endif /* _ASM_POWERPC_PAGE_H */

‎arch/powerpc/mm/mem.c‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <linux/slab.h>
3232
#include <linux/vmalloc.h>
3333
#include <linux/memremap.h>
34+
#include <linux/dma-direct.h>
3435

3536
#include <asm/pgalloc.h>
3637
#include <asm/prom.h>
@@ -201,10 +202,10 @@ static int __init mark_nonram_nosave(void)
201202
* everything else. GFP_DMA32 page allocations automatically fall back to
202203
* ZONE_DMA.
203204
*
204-
* By using 31-bit unconditionally, we can exploit ARCH_ZONE_DMA_BITS to
205-
* inform the generic DMA mapping code. 32-bit only devices (if not handled
206-
* by an IOMMU anyway) will take a first dip into ZONE_NORMAL and get
207-
* otherwise served by ZONE_DMA.
205+
* By using 31-bit unconditionally, we can exploit zone_dma_bits to inform the
206+
* generic DMA mapping code. 32-bit only devices (if not handled by an IOMMU
207+
* anyway) will take a first dip into ZONE_NORMAL and get otherwise served by
208+
* ZONE_DMA.
208209
*/
209210
static unsigned long max_zone_pfns[MAX_NR_ZONES];
210211

@@ -237,9 +238,18 @@ void __init paging_init(void)
237238
printk(KERN_DEBUG "Memory hole size: %ldMB\n",
238239
(long int)((top_of_ram - total_ram) >> 20));
239240

241+
/*
242+
* Allow 30-bit DMA for very limited Broadcom wifi chips on many
243+
* powerbooks.
244+
*/
245+
if (IS_ENABLED(CONFIG_PPC32))
246+
zone_dma_bits = 30;
247+
else
248+
zone_dma_bits = 31;
249+
240250
#ifdef CONFIG_ZONE_DMA
241251
max_zone_pfns[ZONE_DMA] = min(max_low_pfn,
242-
1UL << (ARCH_ZONE_DMA_BITS - PAGE_SHIFT));
252+
1UL << (zone_dma_bits - PAGE_SHIFT));
243253
#endif
244254
max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
245255
#ifdef CONFIG_HIGHMEM

‎arch/s390/include/asm/page.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,6 @@ static inline int devmem_is_allowed(unsigned long pfn)
177177
#define VM_DATA_DEFAULT_FLAGS (VM_READ | VM_WRITE | \
178178
VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC)
179179

180-
#define ARCH_ZONE_DMA_BITS 31
181-
182180
#include <asm-generic/memory_model.h>
183181
#include <asm-generic/getorder.h>
184182

‎arch/s390/mm/init.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ void __init paging_init(void)
118118

119119
sparse_memory_present_with_active_regions(MAX_NUMNODES);
120120
sparse_init();
121+
zone_dma_bits = 31;
121122
memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
122123
max_zone_pfns[ZONE_DMA] = PFN_DOWN(MAX_DMA_ADDRESS);
123124
max_zone_pfns[ZONE_NORMAL] = max_low_pfn;

‎include/linux/dma-direct.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <linux/memblock.h> /* for min_low_pfn */
77
#include <linux/mem_encrypt.h>
88

9+
extern unsigned int zone_dma_bits;
10+
911
#ifdef CONFIG_ARCH_HAS_PHYS_TO_DMA
1012
#include <asm/dma-direct.h>
1113
#else

‎include/linux/mmzone.h‎

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -359,33 +359,40 @@ struct per_cpu_nodestat {
359359
#endif /* !__GENERATING_BOUNDS.H */
360360

361361
enum zone_type {
362-
#ifdef CONFIG_ZONE_DMA
363362
/*
364-
* ZONE_DMA is used when there are devices that are not able
365-
* to do DMA to all of addressable memory (ZONE_NORMAL). Then we
366-
* carve out the portion of memory that is needed for these devices.
367-
* The range is arch specific.
363+
* ZONE_DMA and ZONE_DMA32 are used when there are peripherals not able
364+
* to DMA to all of the addressable memory (ZONE_NORMAL).
365+
* On architectures where this area covers the whole 32 bit address
366+
* space ZONE_DMA32 is used. ZONE_DMA is left for the ones with smaller
367+
* DMA addressing constraints. This distinction is important as a 32bit
368+
* DMA mask is assumed when ZONE_DMA32 is defined. Some 64-bit
369+
* platforms may need both zones as they support peripherals with
370+
* different DMA addressing limitations.
371+
*
372+
* Some examples:
373+
*
374+
* - i386 and x86_64 have a fixed 16M ZONE_DMA and ZONE_DMA32 for the
375+
* rest of the lower 4G.
376+
*
377+
* - arm only uses ZONE_DMA, the size, up to 4G, may vary depending on
378+
* the specific device.
379+
*
380+
* - arm64 has a fixed 1G ZONE_DMA and ZONE_DMA32 for the rest of the
381+
* lower 4G.
368382
*
369-
* Some examples
383+
* - powerpc only uses ZONE_DMA, the size, up to 2G, may vary
384+
* depending on the specific device.
370385
*
371-
* Architecture Limit
372-
* ---------------------------
373-
* parisc, ia64, sparc <4G
374-
* s390, powerpc <2G
375-
* arm Various
376-
* alpha Unlimited or 0-16MB.
386+
* - s390 uses ZONE_DMA fixed to the lower 2G.
377387
*
378-
* i386, x86_64 and multiple other arches
379-
* <16M.
388+
* - ia64 and riscv only use ZONE_DMA32.
389+
*
390+
* - parisc uses neither.
380391
*/
392+
#ifdef CONFIG_ZONE_DMA
381393
ZONE_DMA,
382394
#endif
383395
#ifdef CONFIG_ZONE_DMA32
384-
/*
385-
* x86_64 needs two ZONE_DMAs because it supports devices that are
386-
* only able to do DMA to the lower 16M but also 32 bit devices that
387-
* can only do DMA areas below 4G.
388-
*/
389396
ZONE_DMA32,
390397
#endif
391398
/*

‎kernel/dma/direct.c‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@
1717
#include <linux/swiotlb.h>
1818

1919
/*
20-
* Most architectures use ZONE_DMA for the first 16 Megabytes, but
21-
* some use it for entirely different regions:
20+
* Most architectures use ZONE_DMA for the first 16 Megabytes, but some use it
21+
* it for entirely different regions. In that case the arch code needs to
22+
* override the variable below for dma-direct to work properly.
2223
*/
23-
#ifndef ARCH_ZONE_DMA_BITS
24-
#define ARCH_ZONE_DMA_BITS 24
25-
#endif
24+
unsigned int zone_dma_bits __ro_after_init = 24;
2625

2726
static void report_addr(struct device *dev, dma_addr_t dma_addr, size_t size)
2827
{
@@ -76,7 +75,7 @@ static gfp_t __dma_direct_optimal_gfp_mask(struct device *dev, u64 dma_mask,
7675
* Note that GFP_DMA32 and GFP_DMA are no ops without the corresponding
7776
* zones.
7877
*/
79-
if (*phys_mask <= DMA_BIT_MASK(ARCH_ZONE_DMA_BITS))
78+
if (*phys_mask <= DMA_BIT_MASK(zone_dma_bits))
8079
return GFP_DMA;
8180
if (*phys_mask <= DMA_BIT_MASK(32))
8281
return GFP_DMA32;
@@ -485,7 +484,7 @@ int dma_direct_supported(struct device *dev, u64 mask)
485484
u64 min_mask;
486485

487486
if (IS_ENABLED(CONFIG_ZONE_DMA))
488-
min_mask = DMA_BIT_MASK(ARCH_ZONE_DMA_BITS);
487+
min_mask = DMA_BIT_MASK(zone_dma_bits);
489488
else
490489
min_mask = DMA_BIT_MASK(32);
491490

0 commit comments

Comments
 (0)