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