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