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 
sync_icache_dcache(pte_t pte)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 /*
58  * In order to set up page allocator "nodes",
59  * somebody has to call free_area_init() for UMA.
60  *
61  * In this mode, we only have one pg_data_t
62  * structure: contig_mem_data.
63  */
paging_init(void)64 static void __init paging_init(void)
65 {
66 	unsigned long max_zone_pfn[MAX_NR_ZONES] = {0, };
67 
68 	/*
69 	 *  This is not particularly well documented anywhere, but
70 	 *  give ZONE_NORMAL all the memory, including the big holes
71 	 *  left by the kernel+bootmem_map which are already left as reserved
72 	 *  in the bootmem_map; free_area_init should see those bits and
73 	 *  adjust accordingly.
74 	 */
75 
76 	max_zone_pfn[ZONE_NORMAL] = max_low_pfn;
77 
78 	free_area_init(max_zone_pfn);  /*  sets up the zonelists and mem_map  */
79 
80 	/*
81 	 * Set the init_mm descriptors "context" value to point to the
82 	 * initial kernel segment table's physical address.
83 	 */
84 	init_mm.context.ptbase = __pa(init_mm.pgd);
85 }
86 
87 #ifndef DMA_RESERVE
88 #define DMA_RESERVE		(4)
89 #endif
90 
91 #define DMA_CHUNKSIZE		(1<<22)
92 #define DMA_RESERVED_BYTES	(DMA_RESERVE * DMA_CHUNKSIZE)
93 
94 /*
95  * Pick out the memory size.  We look for mem=size,
96  * where size is "size[KkMm]"
97  */
early_mem(char * p)98 static int __init early_mem(char *p)
99 {
100 	unsigned long size;
101 	char *endp;
102 
103 	size = memparse(p, &endp);
104 
105 	bootmem_lastpg = PFN_DOWN(size);
106 
107 	return 0;
108 }
109 early_param("mem", early_mem);
110 
111 size_t hexagon_coherent_pool_size = (size_t) (DMA_RESERVE << 22);
112 
setup_arch_memory(void)113 void __init setup_arch_memory(void)
114 {
115 	/*  XXX Todo: this probably should be cleaned up  */
116 	u32 *segtable = (u32 *) &swapper_pg_dir[0];
117 	u32 *segtable_end;
118 
119 	/*
120 	 * Set up boot memory allocator
121 	 *
122 	 * The Gorman book also talks about these functions.
123 	 * This needs to change for highmem setups.
124 	 */
125 
126 	/*  Prior to this, bootmem_lastpg is actually mem size  */
127 	bootmem_lastpg += ARCH_PFN_OFFSET;
128 
129 	/* Memory size needs to be a multiple of 16M */
130 	bootmem_lastpg = PFN_DOWN((bootmem_lastpg << PAGE_SHIFT) &
131 		~((BIG_KERNEL_PAGE_SIZE) - 1));
132 
133 	memblock_add(PHYS_OFFSET,
134 		     (bootmem_lastpg - ARCH_PFN_OFFSET) << PAGE_SHIFT);
135 
136 	/* Reserve kernel text/data/bss */
137 	memblock_reserve(PHYS_OFFSET,
138 			 (bootmem_startpg - ARCH_PFN_OFFSET) << PAGE_SHIFT);
139 	/*
140 	 * Reserve the top DMA_RESERVE bytes of RAM for DMA (uncached)
141 	 * memory allocation
142 	 */
143 	max_low_pfn = bootmem_lastpg - PFN_DOWN(DMA_RESERVED_BYTES);
144 	min_low_pfn = ARCH_PFN_OFFSET;
145 	memblock_reserve(PFN_PHYS(max_low_pfn), DMA_RESERVED_BYTES);
146 
147 	printk(KERN_INFO "bootmem_startpg:  0x%08lx\n", bootmem_startpg);
148 	printk(KERN_INFO "bootmem_lastpg:  0x%08lx\n", bootmem_lastpg);
149 	printk(KERN_INFO "min_low_pfn:  0x%08lx\n", min_low_pfn);
150 	printk(KERN_INFO "max_low_pfn:  0x%08lx\n", max_low_pfn);
151 
152 	/*
153 	 * The default VM page tables (will be) populated with
154 	 * VA=PA+PAGE_OFFSET mapping.  We go in and invalidate entries
155 	 * higher than what we have memory for.
156 	 */
157 
158 	/*  this is pointer arithmetic; each entry covers 4MB  */
159 	segtable = segtable + (PAGE_OFFSET >> 22);
160 
161 	/*  this actually only goes to the end of the first gig  */
162 	segtable_end = segtable + (1<<(30-22));
163 
164 	/*
165 	 * Move forward to the start of empty pages; take into account
166 	 * phys_offset shift.
167 	 */
168 
169 	segtable += (bootmem_lastpg-ARCH_PFN_OFFSET)>>(22-PAGE_SHIFT);
170 	{
171 		int i;
172 
173 		for (i = 1 ; i <= DMA_RESERVE ; i++)
174 			segtable[-i] = ((segtable[-i] & __HVM_PTE_PGMASK_4MB)
175 				| __HVM_PTE_R | __HVM_PTE_W | __HVM_PTE_X
176 				| __HEXAGON_C_UNC << 6
177 				| __HVM_PDE_S_4MB);
178 	}
179 
180 	printk(KERN_INFO "clearing segtable from %p to %p\n", segtable,
181 		segtable_end);
182 	while (segtable < (segtable_end-8))
183 		*(segtable++) = __HVM_PDE_S_INVALID;
184 	/* stop the pointer at the device I/O 4MB page  */
185 
186 	printk(KERN_INFO "segtable = %p (should be equal to _K_io_map)\n",
187 		segtable);
188 
189 #if 0
190 	/*  Other half of the early device table from vm_init_segtable. */
191 	printk(KERN_INFO "&_K_init_devicetable = 0x%08x\n",
192 		(unsigned long) _K_init_devicetable-PAGE_OFFSET);
193 	*segtable = ((u32) (unsigned long) _K_init_devicetable-PAGE_OFFSET) |
194 		__HVM_PDE_S_4KB;
195 	printk(KERN_INFO "*segtable = 0x%08x\n", *segtable);
196 #endif
197 
198 	/*
199 	 *  The bootmem allocator seemingly just lives to feed memory
200 	 *  to the paging system
201 	 */
202 	printk(KERN_INFO "PAGE_SIZE=%lu\n", PAGE_SIZE);
203 	paging_init();  /*  See Gorman Book, 2.3  */
204 
205 	/*
206 	 *  At this point, the page allocator is kind of initialized, but
207 	 *  apparently no pages are available (just like with the bootmem
208 	 *  allocator), and need to be freed themselves via mem_init(),
209 	 *  which is called by start_kernel() later on in the process
210 	 */
211 }
212 
213 static const pgprot_t protection_map[16] = {
214 	[VM_NONE]					= __pgprot(_PAGE_PRESENT | _PAGE_USER |
215 								   CACHEDEF),
216 	[VM_READ]					= __pgprot(_PAGE_PRESENT | _PAGE_USER |
217 								   _PAGE_READ | CACHEDEF),
218 	[VM_WRITE]					= __pgprot(_PAGE_PRESENT | _PAGE_USER |
219 								   CACHEDEF),
220 	[VM_WRITE | VM_READ]				= __pgprot(_PAGE_PRESENT | _PAGE_USER |
221 								   _PAGE_READ | CACHEDEF),
222 	[VM_EXEC]					= __pgprot(_PAGE_PRESENT | _PAGE_USER |
223 								   _PAGE_EXECUTE | CACHEDEF),
224 	[VM_EXEC | VM_READ]				= __pgprot(_PAGE_PRESENT | _PAGE_USER |
225 								   _PAGE_EXECUTE | _PAGE_READ |
226 								   CACHEDEF),
227 	[VM_EXEC | VM_WRITE]				= __pgprot(_PAGE_PRESENT | _PAGE_USER |
228 								   _PAGE_EXECUTE | CACHEDEF),
229 	[VM_EXEC | VM_WRITE | VM_READ]			= __pgprot(_PAGE_PRESENT | _PAGE_USER |
230 								   _PAGE_EXECUTE | _PAGE_READ |
231 								   CACHEDEF),
232 	[VM_SHARED]                                     = __pgprot(_PAGE_PRESENT | _PAGE_USER |
233 								   CACHEDEF),
234 	[VM_SHARED | VM_READ]				= __pgprot(_PAGE_PRESENT | _PAGE_USER |
235 								   _PAGE_READ | CACHEDEF),
236 	[VM_SHARED | VM_WRITE]				= __pgprot(_PAGE_PRESENT | _PAGE_USER |
237 								   _PAGE_WRITE | CACHEDEF),
238 	[VM_SHARED | VM_WRITE | VM_READ]		= __pgprot(_PAGE_PRESENT | _PAGE_USER |
239 								   _PAGE_READ | _PAGE_WRITE |
240 								   CACHEDEF),
241 	[VM_SHARED | VM_EXEC]				= __pgprot(_PAGE_PRESENT | _PAGE_USER |
242 								   _PAGE_EXECUTE | CACHEDEF),
243 	[VM_SHARED | VM_EXEC | VM_READ]			= __pgprot(_PAGE_PRESENT | _PAGE_USER |
244 								   _PAGE_EXECUTE | _PAGE_READ |
245 								   CACHEDEF),
246 	[VM_SHARED | VM_EXEC | VM_WRITE]		= __pgprot(_PAGE_PRESENT | _PAGE_USER |
247 								   _PAGE_EXECUTE | _PAGE_WRITE |
248 								   CACHEDEF),
249 	[VM_SHARED | VM_EXEC | VM_WRITE | VM_READ]	= __pgprot(_PAGE_PRESENT | _PAGE_USER |
250 								   _PAGE_READ | _PAGE_EXECUTE |
251 								   _PAGE_WRITE | CACHEDEF)
252 };
253 DECLARE_VM_GET_PAGE_PROT
254