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>
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);
5659struct page * vmemmap __ro_after_init ;
5760EXPORT_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+ */
5968phys_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
436465void __init bootmem_init (void )
@@ -534,7 +563,7 @@ static void __init free_unused_memmap(void)
534563void __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 ;
0 commit comments