xref: /OK3568_Linux_fs/kernel/arch/arc/mm/init.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #include <linux/kernel.h>
7*4882a593Smuzhiyun #include <linux/mm.h>
8*4882a593Smuzhiyun #include <linux/memblock.h>
9*4882a593Smuzhiyun #ifdef CONFIG_BLK_DEV_INITRD
10*4882a593Smuzhiyun #include <linux/initrd.h>
11*4882a593Smuzhiyun #endif
12*4882a593Smuzhiyun #include <linux/of_fdt.h>
13*4882a593Smuzhiyun #include <linux/swap.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/highmem.h>
16*4882a593Smuzhiyun #include <asm/page.h>
17*4882a593Smuzhiyun #include <asm/sections.h>
18*4882a593Smuzhiyun #include <asm/arcregs.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun pgd_t swapper_pg_dir[PTRS_PER_PGD] __aligned(PAGE_SIZE);
21*4882a593Smuzhiyun char empty_zero_page[PAGE_SIZE] __aligned(PAGE_SIZE);
22*4882a593Smuzhiyun EXPORT_SYMBOL(empty_zero_page);
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun static const unsigned long low_mem_start = CONFIG_LINUX_RAM_BASE;
25*4882a593Smuzhiyun static unsigned long low_mem_sz;
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #ifdef CONFIG_HIGHMEM
28*4882a593Smuzhiyun static unsigned long min_high_pfn, max_high_pfn;
29*4882a593Smuzhiyun static phys_addr_t high_mem_start;
30*4882a593Smuzhiyun static phys_addr_t high_mem_sz;
31*4882a593Smuzhiyun #endif
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #ifdef CONFIG_DISCONTIGMEM
34*4882a593Smuzhiyun struct pglist_data node_data[MAX_NUMNODES] __read_mostly;
35*4882a593Smuzhiyun EXPORT_SYMBOL(node_data);
36*4882a593Smuzhiyun #endif
37*4882a593Smuzhiyun 
arc_get_mem_sz(void)38*4882a593Smuzhiyun long __init arc_get_mem_sz(void)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	return low_mem_sz;
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun /* User can over-ride above with "mem=nnn[KkMm]" in cmdline */
setup_mem_sz(char * str)44*4882a593Smuzhiyun static int __init setup_mem_sz(char *str)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun 	low_mem_sz = memparse(str, NULL) & PAGE_MASK;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	/* early console might not be setup yet - it will show up later */
49*4882a593Smuzhiyun 	pr_info("\"mem=%s\": mem sz set to %ldM\n", str, TO_MB(low_mem_sz));
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	return 0;
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun early_param("mem", setup_mem_sz);
54*4882a593Smuzhiyun 
early_init_dt_add_memory_arch(u64 base,u64 size)55*4882a593Smuzhiyun void __init early_init_dt_add_memory_arch(u64 base, u64 size)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun 	int in_use = 0;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	if (!low_mem_sz) {
60*4882a593Smuzhiyun 		if (base != low_mem_start)
61*4882a593Smuzhiyun 			panic("CONFIG_LINUX_RAM_BASE != DT memory { }");
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 		low_mem_sz = size;
64*4882a593Smuzhiyun 		in_use = 1;
65*4882a593Smuzhiyun 		memblock_add_node(base, size, 0);
66*4882a593Smuzhiyun 	} else {
67*4882a593Smuzhiyun #ifdef CONFIG_HIGHMEM
68*4882a593Smuzhiyun 		high_mem_start = base;
69*4882a593Smuzhiyun 		high_mem_sz = size;
70*4882a593Smuzhiyun 		in_use = 1;
71*4882a593Smuzhiyun 		memblock_add_node(base, size, 1);
72*4882a593Smuzhiyun 		memblock_reserve(base, size);
73*4882a593Smuzhiyun #endif
74*4882a593Smuzhiyun 	}
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	pr_info("Memory @ %llx [%lldM] %s\n",
77*4882a593Smuzhiyun 		base, TO_MB(size), !in_use ? "Not used":"");
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun 
arch_has_descending_max_zone_pfns(void)80*4882a593Smuzhiyun bool arch_has_descending_max_zone_pfns(void)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun 	return !IS_ENABLED(CONFIG_ARC_HAS_PAE40);
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun /*
86*4882a593Smuzhiyun  * First memory setup routine called from setup_arch()
87*4882a593Smuzhiyun  * 1. setup swapper's mm @init_mm
88*4882a593Smuzhiyun  * 2. Count the pages we have and setup bootmem allocator
89*4882a593Smuzhiyun  * 3. zone setup
90*4882a593Smuzhiyun  */
setup_arch_memory(void)91*4882a593Smuzhiyun void __init setup_arch_memory(void)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun 	unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	init_mm.start_code = (unsigned long)_text;
96*4882a593Smuzhiyun 	init_mm.end_code = (unsigned long)_etext;
97*4882a593Smuzhiyun 	init_mm.end_data = (unsigned long)_edata;
98*4882a593Smuzhiyun 	init_mm.brk = (unsigned long)_end;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	/* first page of system - kernel .vector starts here */
101*4882a593Smuzhiyun 	min_low_pfn = ARCH_PFN_OFFSET;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	/* Last usable page of low mem */
104*4882a593Smuzhiyun 	max_low_pfn = max_pfn = PFN_DOWN(low_mem_start + low_mem_sz);
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun #ifdef CONFIG_FLATMEM
107*4882a593Smuzhiyun 	/* pfn_valid() uses this */
108*4882a593Smuzhiyun 	max_mapnr = max_low_pfn - min_low_pfn;
109*4882a593Smuzhiyun #endif
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	/*------------- bootmem allocator setup -----------------------*/
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	/*
114*4882a593Smuzhiyun 	 * seed the bootmem allocator after any DT memory node parsing or
115*4882a593Smuzhiyun 	 * "mem=xxx" cmdline overrides have potentially updated @arc_mem_sz
116*4882a593Smuzhiyun 	 *
117*4882a593Smuzhiyun 	 * Only low mem is added, otherwise we have crashes when allocating
118*4882a593Smuzhiyun 	 * mem_map[] itself. NO_BOOTMEM allocates mem_map[] at the end of
119*4882a593Smuzhiyun 	 * avail memory, ending in highmem with a > 32-bit address. However
120*4882a593Smuzhiyun 	 * it then tries to memset it with a truncaed 32-bit handle, causing
121*4882a593Smuzhiyun 	 * the crash
122*4882a593Smuzhiyun 	 */
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	memblock_reserve(CONFIG_LINUX_LINK_BASE,
125*4882a593Smuzhiyun 			 __pa(_end) - CONFIG_LINUX_LINK_BASE);
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun #ifdef CONFIG_BLK_DEV_INITRD
128*4882a593Smuzhiyun 	if (phys_initrd_size) {
129*4882a593Smuzhiyun 		memblock_reserve(phys_initrd_start, phys_initrd_size);
130*4882a593Smuzhiyun 		initrd_start = (unsigned long)__va(phys_initrd_start);
131*4882a593Smuzhiyun 		initrd_end = initrd_start + phys_initrd_size;
132*4882a593Smuzhiyun 	}
133*4882a593Smuzhiyun #endif
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	early_init_fdt_reserve_self();
136*4882a593Smuzhiyun 	early_init_fdt_scan_reserved_mem();
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	memblock_dump_all();
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	/*----------------- node/zones setup --------------------------*/
141*4882a593Smuzhiyun 	max_zone_pfn[ZONE_NORMAL] = max_low_pfn;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun #ifdef CONFIG_HIGHMEM
144*4882a593Smuzhiyun 	/*
145*4882a593Smuzhiyun 	 * Populate a new node with highmem
146*4882a593Smuzhiyun 	 *
147*4882a593Smuzhiyun 	 * On ARC (w/o PAE) HIGHMEM addresses are actually smaller (0 based)
148*4882a593Smuzhiyun 	 * than addresses in normal ala low memory (0x8000_0000 based).
149*4882a593Smuzhiyun 	 * Even with PAE, the huge peripheral space hole would waste a lot of
150*4882a593Smuzhiyun 	 * mem with single mem_map[]. This warrants a mem_map per region design.
151*4882a593Smuzhiyun 	 * Thus HIGHMEM on ARC is imlemented with DISCONTIGMEM.
152*4882a593Smuzhiyun 	 *
153*4882a593Smuzhiyun 	 * DISCONTIGMEM in turns requires multiple nodes. node 0 above is
154*4882a593Smuzhiyun 	 * populated with normal memory zone while node 1 only has highmem
155*4882a593Smuzhiyun 	 */
156*4882a593Smuzhiyun 	node_set_online(1);
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	min_high_pfn = PFN_DOWN(high_mem_start);
159*4882a593Smuzhiyun 	max_high_pfn = PFN_DOWN(high_mem_start + high_mem_sz);
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	/*
162*4882a593Smuzhiyun 	 * max_high_pfn should be ok here for both HIGHMEM and HIGHMEM+PAE.
163*4882a593Smuzhiyun 	 * For HIGHMEM without PAE max_high_pfn should be less than
164*4882a593Smuzhiyun 	 * min_low_pfn to guarantee that these two regions don't overlap.
165*4882a593Smuzhiyun 	 * For PAE case highmem is greater than lowmem, so it is natural
166*4882a593Smuzhiyun 	 * to use max_high_pfn.
167*4882a593Smuzhiyun 	 *
168*4882a593Smuzhiyun 	 * In both cases, holes should be handled by pfn_valid().
169*4882a593Smuzhiyun 	 */
170*4882a593Smuzhiyun 	max_zone_pfn[ZONE_HIGHMEM] = max_high_pfn;
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	high_memory = (void *)(min_high_pfn << PAGE_SHIFT);
173*4882a593Smuzhiyun 	kmap_init();
174*4882a593Smuzhiyun #endif
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	free_area_init(max_zone_pfn);
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun 
highmem_init(void)179*4882a593Smuzhiyun static void __init highmem_init(void)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun #ifdef CONFIG_HIGHMEM
182*4882a593Smuzhiyun 	unsigned long tmp;
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	memblock_free(high_mem_start, high_mem_sz);
185*4882a593Smuzhiyun 	for (tmp = min_high_pfn; tmp < max_high_pfn; tmp++)
186*4882a593Smuzhiyun 		free_highmem_page(pfn_to_page(tmp));
187*4882a593Smuzhiyun #endif
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun /*
191*4882a593Smuzhiyun  * mem_init - initializes memory
192*4882a593Smuzhiyun  *
193*4882a593Smuzhiyun  * Frees up bootmem
194*4882a593Smuzhiyun  * Calculates and displays memory available/used
195*4882a593Smuzhiyun  */
mem_init(void)196*4882a593Smuzhiyun void __init mem_init(void)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun 	memblock_free_all();
199*4882a593Smuzhiyun 	highmem_init();
200*4882a593Smuzhiyun 	mem_init_print_info(NULL);
201*4882a593Smuzhiyun }
202