xref: /openbmc/linux/arch/hexagon/mm/init.c (revision 4419617e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Memory subsystem initialization for Hexagon
4  *
5  * Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
6  */
7 
8 #include <linux/init.h>
9 #include <linux/mm.h>
10 #include <linux/memblock.h>
11 #include <asm/atomic.h>
12 #include <linux/highmem.h>
13 #include <asm/tlb.h>
14 #include <asm/sections.h>
15 #include <asm/vm_mmu.h>
16 
17 /*
18  * Define a startpg just past the end of the kernel image and a lastpg
19  * that corresponds to the end of real or simulated platform memory.
20  */
21 #define bootmem_startpg (PFN_UP(((unsigned long) _end) - PAGE_OFFSET + PHYS_OFFSET))
22 
23 unsigned long bootmem_lastpg;	/*  Should be set by platform code  */
24 unsigned long __phys_offset;	/*  physical kernel offset >> 12  */
25 
26 /*  Set as variable to limit PMD copies  */
27 int max_kernel_seg = 0x303;
28 
29 /*  indicate pfn's of high memory  */
30 unsigned long highstart_pfn, highend_pfn;
31 
32 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
33 
34 /* Default cache attribute for newly created page tables */
35 unsigned long _dflt_cache_att = CACHEDEF;
36 
37 /*
38  * The current "generation" of kernel map, which should not roll
39  * over until Hell freezes over.  Actual bound in years needs to be
40  * calculated to confirm.
41  */
42 DEFINE_SPINLOCK(kmap_gen_lock);
43 
44 /*  checkpatch says don't init this to 0.  */
45 unsigned long long kmap_generation;
46 
47 /*
48  * mem_init - initializes memory
49  *
50  * Frees up bootmem
51  * Fixes up more stuff for HIGHMEM
52  * Calculates and displays memory available/used
53  */
54 void __init mem_init(void)
55 {
56 	/*  No idea where this is actually declared.  Seems to evade LXR.  */
57 	memblock_free_all();
58 	mem_init_print_info(NULL);
59 
60 	/*
61 	 *  To-Do:  someone somewhere should wipe out the bootmem map
62 	 *  after we're done?
63 	 */
64 
65 	/*
66 	 * This can be moved to some more virtual-memory-specific
67 	 * initialization hook at some point.  Set the init_mm
68 	 * descriptors "context" value to point to the initial
69 	 * kernel segment table's physical address.
70 	 */
71 	init_mm.context.ptbase = __pa(init_mm.pgd);
72 }
73 
74 /*
75  * free_initrd_mem - frees...  initrd memory.
76  * @start - start of init memory
77  * @end - end of init memory
78  *
79  * Apparently has to be passed the address of the initrd memory.
80  *
81  * Wrapped by #ifdef CONFIG_BLKDEV_INITRD
82  */
83 void free_initrd_mem(unsigned long start, unsigned long end)
84 {
85 }
86 
87 void sync_icache_dcache(pte_t pte)
88 {
89 	unsigned long addr;
90 	struct page *page;
91 
92 	page = pte_page(pte);
93 	addr = (unsigned long) page_address(page);
94 
95 	__vmcache_idsync(addr, PAGE_SIZE);
96 }
97 
98 /*
99  * In order to set up page allocator "nodes",
100  * somebody has to call free_area_init() for UMA.
101  *
102  * In this mode, we only have one pg_data_t
103  * structure: contig_mem_data.
104  */
105 void __init paging_init(void)
106 {
107 	unsigned long zones_sizes[MAX_NR_ZONES] = {0, };
108 
109 	/*
110 	 *  This is not particularly well documented anywhere, but
111 	 *  give ZONE_NORMAL all the memory, including the big holes
112 	 *  left by the kernel+bootmem_map which are already left as reserved
113 	 *  in the bootmem_map; free_area_init should see those bits and
114 	 *  adjust accordingly.
115 	 */
116 
117 	zones_sizes[ZONE_NORMAL] = max_low_pfn;
118 
119 	free_area_init(zones_sizes);  /*  sets up the zonelists and mem_map  */
120 
121 	/*
122 	 * Start of high memory area.  Will probably need something more
123 	 * fancy if we...  get more fancy.
124 	 */
125 	high_memory = (void *)((bootmem_lastpg + 1) << PAGE_SHIFT);
126 }
127 
128 #ifndef DMA_RESERVE
129 #define DMA_RESERVE		(4)
130 #endif
131 
132 #define DMA_CHUNKSIZE		(1<<22)
133 #define DMA_RESERVED_BYTES	(DMA_RESERVE * DMA_CHUNKSIZE)
134 
135 /*
136  * Pick out the memory size.  We look for mem=size,
137  * where size is "size[KkMm]"
138  */
139 static int __init early_mem(char *p)
140 {
141 	unsigned long size;
142 	char *endp;
143 
144 	size = memparse(p, &endp);
145 
146 	bootmem_lastpg = PFN_DOWN(size);
147 
148 	return 0;
149 }
150 early_param("mem", early_mem);
151 
152 size_t hexagon_coherent_pool_size = (size_t) (DMA_RESERVE << 22);
153 
154 void __init setup_arch_memory(void)
155 {
156 	/*  XXX Todo: this probably should be cleaned up  */
157 	u32 *segtable = (u32 *) &swapper_pg_dir[0];
158 	u32 *segtable_end;
159 
160 	/*
161 	 * Set up boot memory allocator
162 	 *
163 	 * The Gorman book also talks about these functions.
164 	 * This needs to change for highmem setups.
165 	 */
166 
167 	/*  Prior to this, bootmem_lastpg is actually mem size  */
168 	bootmem_lastpg += ARCH_PFN_OFFSET;
169 
170 	/* Memory size needs to be a multiple of 16M */
171 	bootmem_lastpg = PFN_DOWN((bootmem_lastpg << PAGE_SHIFT) &
172 		~((BIG_KERNEL_PAGE_SIZE) - 1));
173 
174 	memblock_add(PHYS_OFFSET,
175 		     (bootmem_lastpg - ARCH_PFN_OFFSET) << PAGE_SHIFT);
176 
177 	/* Reserve kernel text/data/bss */
178 	memblock_reserve(PHYS_OFFSET,
179 			 (bootmem_startpg - ARCH_PFN_OFFSET) << PAGE_SHIFT);
180 	/*
181 	 * Reserve the top DMA_RESERVE bytes of RAM for DMA (uncached)
182 	 * memory allocation
183 	 */
184 	max_low_pfn = bootmem_lastpg - PFN_DOWN(DMA_RESERVED_BYTES);
185 	min_low_pfn = ARCH_PFN_OFFSET;
186 	memblock_reserve(PFN_PHYS(max_low_pfn), DMA_RESERVED_BYTES);
187 
188 	printk(KERN_INFO "bootmem_startpg:  0x%08lx\n", bootmem_startpg);
189 	printk(KERN_INFO "bootmem_lastpg:  0x%08lx\n", bootmem_lastpg);
190 	printk(KERN_INFO "min_low_pfn:  0x%08lx\n", min_low_pfn);
191 	printk(KERN_INFO "max_low_pfn:  0x%08lx\n", max_low_pfn);
192 
193 	/*
194 	 * The default VM page tables (will be) populated with
195 	 * VA=PA+PAGE_OFFSET mapping.  We go in and invalidate entries
196 	 * higher than what we have memory for.
197 	 */
198 
199 	/*  this is pointer arithmetic; each entry covers 4MB  */
200 	segtable = segtable + (PAGE_OFFSET >> 22);
201 
202 	/*  this actually only goes to the end of the first gig  */
203 	segtable_end = segtable + (1<<(30-22));
204 
205 	/*
206 	 * Move forward to the start of empty pages; take into account
207 	 * phys_offset shift.
208 	 */
209 
210 	segtable += (bootmem_lastpg-ARCH_PFN_OFFSET)>>(22-PAGE_SHIFT);
211 	{
212 		int i;
213 
214 		for (i = 1 ; i <= DMA_RESERVE ; i++)
215 			segtable[-i] = ((segtable[-i] & __HVM_PTE_PGMASK_4MB)
216 				| __HVM_PTE_R | __HVM_PTE_W | __HVM_PTE_X
217 				| __HEXAGON_C_UNC << 6
218 				| __HVM_PDE_S_4MB);
219 	}
220 
221 	printk(KERN_INFO "clearing segtable from %p to %p\n", segtable,
222 		segtable_end);
223 	while (segtable < (segtable_end-8))
224 		*(segtable++) = __HVM_PDE_S_INVALID;
225 	/* stop the pointer at the device I/O 4MB page  */
226 
227 	printk(KERN_INFO "segtable = %p (should be equal to _K_io_map)\n",
228 		segtable);
229 
230 #if 0
231 	/*  Other half of the early device table from vm_init_segtable. */
232 	printk(KERN_INFO "&_K_init_devicetable = 0x%08x\n",
233 		(unsigned long) _K_init_devicetable-PAGE_OFFSET);
234 	*segtable = ((u32) (unsigned long) _K_init_devicetable-PAGE_OFFSET) |
235 		__HVM_PDE_S_4KB;
236 	printk(KERN_INFO "*segtable = 0x%08x\n", *segtable);
237 #endif
238 
239 	/*
240 	 *  The bootmem allocator seemingly just lives to feed memory
241 	 *  to the paging system
242 	 */
243 	printk(KERN_INFO "PAGE_SIZE=%lu\n", PAGE_SIZE);
244 	paging_init();  /*  See Gorman Book, 2.3  */
245 
246 	/*
247 	 *  At this point, the page allocator is kind of initialized, but
248 	 *  apparently no pages are available (just like with the bootmem
249 	 *  allocator), and need to be freed themselves via mem_init(),
250 	 *  which is called by start_kernel() later on in the process
251 	 */
252 }
253