108dbd0f8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2499236d9SRichard Kuo /*
3499236d9SRichard Kuo * Memory subsystem initialization for Hexagon
4499236d9SRichard Kuo *
57c6a5df4SRichard Kuo * Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
6499236d9SRichard Kuo */
7499236d9SRichard Kuo
8499236d9SRichard Kuo #include <linux/init.h>
9499236d9SRichard Kuo #include <linux/mm.h>
10426dcd4bSMike Rapoport #include <linux/memblock.h>
11499236d9SRichard Kuo #include <asm/atomic.h>
12499236d9SRichard Kuo #include <linux/highmem.h>
13499236d9SRichard Kuo #include <asm/tlb.h>
14499236d9SRichard Kuo #include <asm/sections.h>
15499236d9SRichard Kuo #include <asm/vm_mmu.h>
16499236d9SRichard Kuo
17499236d9SRichard Kuo /*
18499236d9SRichard Kuo * Define a startpg just past the end of the kernel image and a lastpg
19499236d9SRichard Kuo * that corresponds to the end of real or simulated platform memory.
20499236d9SRichard Kuo */
218f5a0b9dSRichard Kuo #define bootmem_startpg (PFN_UP(((unsigned long) _end) - PAGE_OFFSET + PHYS_OFFSET))
22499236d9SRichard Kuo
23499236d9SRichard Kuo unsigned long bootmem_lastpg; /* Should be set by platform code */
248f5a0b9dSRichard Kuo unsigned long __phys_offset; /* physical kernel offset >> 12 */
25499236d9SRichard Kuo
26499236d9SRichard Kuo /* Set as variable to limit PMD copies */
27499236d9SRichard Kuo int max_kernel_seg = 0x303;
28499236d9SRichard Kuo
29499236d9SRichard Kuo /* indicate pfn's of high memory */
30499236d9SRichard Kuo unsigned long highstart_pfn, highend_pfn;
31499236d9SRichard Kuo
32499236d9SRichard Kuo /* Default cache attribute for newly created page tables */
33499236d9SRichard Kuo unsigned long _dflt_cache_att = CACHEDEF;
34499236d9SRichard Kuo
35499236d9SRichard Kuo /*
36499236d9SRichard Kuo * The current "generation" of kernel map, which should not roll
37499236d9SRichard Kuo * over until Hell freezes over. Actual bound in years needs to be
38499236d9SRichard Kuo * calculated to confirm.
39499236d9SRichard Kuo */
40499236d9SRichard Kuo DEFINE_SPINLOCK(kmap_gen_lock);
41499236d9SRichard Kuo
42499236d9SRichard Kuo /* checkpatch says don't init this to 0. */
43499236d9SRichard Kuo unsigned long long kmap_generation;
44499236d9SRichard Kuo
45499236d9SRichard Kuo /*
46499236d9SRichard Kuo * mem_init - initializes memory
47499236d9SRichard Kuo *
48499236d9SRichard Kuo * Frees up bootmem
49499236d9SRichard Kuo * Fixes up more stuff for HIGHMEM
50499236d9SRichard Kuo * Calculates and displays memory available/used
51499236d9SRichard Kuo */
mem_init(void)52499236d9SRichard Kuo void __init mem_init(void)
53499236d9SRichard Kuo {
54499236d9SRichard Kuo /* No idea where this is actually declared. Seems to evade LXR. */
55c6ffc5caSMike Rapoport memblock_free_all();
56499236d9SRichard Kuo
57499236d9SRichard Kuo /*
58499236d9SRichard Kuo * To-Do: someone somewhere should wipe out the bootmem map
59499236d9SRichard Kuo * after we're done?
60499236d9SRichard Kuo */
61499236d9SRichard Kuo
62499236d9SRichard Kuo /*
63499236d9SRichard Kuo * This can be moved to some more virtual-memory-specific
64499236d9SRichard Kuo * initialization hook at some point. Set the init_mm
65499236d9SRichard Kuo * descriptors "context" value to point to the initial
66499236d9SRichard Kuo * kernel segment table's physical address.
67499236d9SRichard Kuo */
68499236d9SRichard Kuo init_mm.context.ptbase = __pa(init_mm.pgd);
69499236d9SRichard Kuo }
70499236d9SRichard Kuo
sync_icache_dcache(pte_t pte)71499236d9SRichard Kuo void sync_icache_dcache(pte_t pte)
72499236d9SRichard Kuo {
73499236d9SRichard Kuo unsigned long addr;
74499236d9SRichard Kuo struct page *page;
75499236d9SRichard Kuo
76499236d9SRichard Kuo page = pte_page(pte);
77499236d9SRichard Kuo addr = (unsigned long) page_address(page);
78499236d9SRichard Kuo
79499236d9SRichard Kuo __vmcache_idsync(addr, PAGE_SIZE);
80499236d9SRichard Kuo }
81499236d9SRichard Kuo
82499236d9SRichard Kuo /*
83499236d9SRichard Kuo * In order to set up page allocator "nodes",
84499236d9SRichard Kuo * somebody has to call free_area_init() for UMA.
85499236d9SRichard Kuo *
86499236d9SRichard Kuo * In this mode, we only have one pg_data_t
87499236d9SRichard Kuo * structure: contig_mem_data.
88499236d9SRichard Kuo */
paging_init(void)89499236d9SRichard Kuo void __init paging_init(void)
90499236d9SRichard Kuo {
91fa3354e4SMike Rapoport unsigned long max_zone_pfn[MAX_NR_ZONES] = {0, };
92499236d9SRichard Kuo
93499236d9SRichard Kuo /*
94499236d9SRichard Kuo * This is not particularly well documented anywhere, but
95499236d9SRichard Kuo * give ZONE_NORMAL all the memory, including the big holes
96499236d9SRichard Kuo * left by the kernel+bootmem_map which are already left as reserved
97499236d9SRichard Kuo * in the bootmem_map; free_area_init should see those bits and
98499236d9SRichard Kuo * adjust accordingly.
99499236d9SRichard Kuo */
100499236d9SRichard Kuo
101fa3354e4SMike Rapoport max_zone_pfn[ZONE_NORMAL] = max_low_pfn;
102499236d9SRichard Kuo
103fa3354e4SMike Rapoport free_area_init(max_zone_pfn); /* sets up the zonelists and mem_map */
104499236d9SRichard Kuo
105499236d9SRichard Kuo /*
106499236d9SRichard Kuo * Start of high memory area. Will probably need something more
107499236d9SRichard Kuo * fancy if we... get more fancy.
108499236d9SRichard Kuo */
109499236d9SRichard Kuo high_memory = (void *)((bootmem_lastpg + 1) << PAGE_SHIFT);
110499236d9SRichard Kuo }
111499236d9SRichard Kuo
112499236d9SRichard Kuo #ifndef DMA_RESERVE
113499236d9SRichard Kuo #define DMA_RESERVE (4)
114499236d9SRichard Kuo #endif
115499236d9SRichard Kuo
116499236d9SRichard Kuo #define DMA_CHUNKSIZE (1<<22)
117499236d9SRichard Kuo #define DMA_RESERVED_BYTES (DMA_RESERVE * DMA_CHUNKSIZE)
118499236d9SRichard Kuo
119499236d9SRichard Kuo /*
120499236d9SRichard Kuo * Pick out the memory size. We look for mem=size,
121499236d9SRichard Kuo * where size is "size[KkMm]"
122499236d9SRichard Kuo */
early_mem(char * p)123499236d9SRichard Kuo static int __init early_mem(char *p)
124499236d9SRichard Kuo {
125499236d9SRichard Kuo unsigned long size;
126499236d9SRichard Kuo char *endp;
127499236d9SRichard Kuo
128499236d9SRichard Kuo size = memparse(p, &endp);
129499236d9SRichard Kuo
130499236d9SRichard Kuo bootmem_lastpg = PFN_DOWN(size);
131499236d9SRichard Kuo
132499236d9SRichard Kuo return 0;
133499236d9SRichard Kuo }
134499236d9SRichard Kuo early_param("mem", early_mem);
135499236d9SRichard Kuo
136499236d9SRichard Kuo size_t hexagon_coherent_pool_size = (size_t) (DMA_RESERVE << 22);
137499236d9SRichard Kuo
setup_arch_memory(void)138499236d9SRichard Kuo void __init setup_arch_memory(void)
139499236d9SRichard Kuo {
140499236d9SRichard Kuo /* XXX Todo: this probably should be cleaned up */
141499236d9SRichard Kuo u32 *segtable = (u32 *) &swapper_pg_dir[0];
142499236d9SRichard Kuo u32 *segtable_end;
143499236d9SRichard Kuo
144499236d9SRichard Kuo /*
145499236d9SRichard Kuo * Set up boot memory allocator
146499236d9SRichard Kuo *
147499236d9SRichard Kuo * The Gorman book also talks about these functions.
148499236d9SRichard Kuo * This needs to change for highmem setups.
149499236d9SRichard Kuo */
150499236d9SRichard Kuo
1518f5a0b9dSRichard Kuo /* Prior to this, bootmem_lastpg is actually mem size */
1528f5a0b9dSRichard Kuo bootmem_lastpg += ARCH_PFN_OFFSET;
1538f5a0b9dSRichard Kuo
154499236d9SRichard Kuo /* Memory size needs to be a multiple of 16M */
155499236d9SRichard Kuo bootmem_lastpg = PFN_DOWN((bootmem_lastpg << PAGE_SHIFT) &
156499236d9SRichard Kuo ~((BIG_KERNEL_PAGE_SIZE) - 1));
157499236d9SRichard Kuo
158426dcd4bSMike Rapoport memblock_add(PHYS_OFFSET,
159426dcd4bSMike Rapoport (bootmem_lastpg - ARCH_PFN_OFFSET) << PAGE_SHIFT);
160426dcd4bSMike Rapoport
161426dcd4bSMike Rapoport /* Reserve kernel text/data/bss */
162426dcd4bSMike Rapoport memblock_reserve(PHYS_OFFSET,
163426dcd4bSMike Rapoport (bootmem_startpg - ARCH_PFN_OFFSET) << PAGE_SHIFT);
164499236d9SRichard Kuo /*
165499236d9SRichard Kuo * Reserve the top DMA_RESERVE bytes of RAM for DMA (uncached)
166499236d9SRichard Kuo * memory allocation
167499236d9SRichard Kuo */
1688f5a0b9dSRichard Kuo max_low_pfn = bootmem_lastpg - PFN_DOWN(DMA_RESERVED_BYTES);
1698f5a0b9dSRichard Kuo min_low_pfn = ARCH_PFN_OFFSET;
170426dcd4bSMike Rapoport memblock_reserve(PFN_PHYS(max_low_pfn), DMA_RESERVED_BYTES);
171499236d9SRichard Kuo
172499236d9SRichard Kuo printk(KERN_INFO "bootmem_startpg: 0x%08lx\n", bootmem_startpg);
173499236d9SRichard Kuo printk(KERN_INFO "bootmem_lastpg: 0x%08lx\n", bootmem_lastpg);
1748f5a0b9dSRichard Kuo printk(KERN_INFO "min_low_pfn: 0x%08lx\n", min_low_pfn);
175499236d9SRichard Kuo printk(KERN_INFO "max_low_pfn: 0x%08lx\n", max_low_pfn);
176499236d9SRichard Kuo
177499236d9SRichard Kuo /*
178499236d9SRichard Kuo * The default VM page tables (will be) populated with
179499236d9SRichard Kuo * VA=PA+PAGE_OFFSET mapping. We go in and invalidate entries
180499236d9SRichard Kuo * higher than what we have memory for.
181499236d9SRichard Kuo */
182499236d9SRichard Kuo
183499236d9SRichard Kuo /* this is pointer arithmetic; each entry covers 4MB */
184499236d9SRichard Kuo segtable = segtable + (PAGE_OFFSET >> 22);
185499236d9SRichard Kuo
186499236d9SRichard Kuo /* this actually only goes to the end of the first gig */
187499236d9SRichard Kuo segtable_end = segtable + (1<<(30-22));
188499236d9SRichard Kuo
1898f5a0b9dSRichard Kuo /*
1908f5a0b9dSRichard Kuo * Move forward to the start of empty pages; take into account
1918f5a0b9dSRichard Kuo * phys_offset shift.
1928f5a0b9dSRichard Kuo */
193499236d9SRichard Kuo
1948f5a0b9dSRichard Kuo segtable += (bootmem_lastpg-ARCH_PFN_OFFSET)>>(22-PAGE_SHIFT);
195499236d9SRichard Kuo {
196499236d9SRichard Kuo int i;
197499236d9SRichard Kuo
198499236d9SRichard Kuo for (i = 1 ; i <= DMA_RESERVE ; i++)
199499236d9SRichard Kuo segtable[-i] = ((segtable[-i] & __HVM_PTE_PGMASK_4MB)
200499236d9SRichard Kuo | __HVM_PTE_R | __HVM_PTE_W | __HVM_PTE_X
201499236d9SRichard Kuo | __HEXAGON_C_UNC << 6
202499236d9SRichard Kuo | __HVM_PDE_S_4MB);
203499236d9SRichard Kuo }
204499236d9SRichard Kuo
205499236d9SRichard Kuo printk(KERN_INFO "clearing segtable from %p to %p\n", segtable,
206499236d9SRichard Kuo segtable_end);
207499236d9SRichard Kuo while (segtable < (segtable_end-8))
208499236d9SRichard Kuo *(segtable++) = __HVM_PDE_S_INVALID;
209499236d9SRichard Kuo /* stop the pointer at the device I/O 4MB page */
210499236d9SRichard Kuo
211499236d9SRichard Kuo printk(KERN_INFO "segtable = %p (should be equal to _K_io_map)\n",
212499236d9SRichard Kuo segtable);
213499236d9SRichard Kuo
214499236d9SRichard Kuo #if 0
215499236d9SRichard Kuo /* Other half of the early device table from vm_init_segtable. */
216499236d9SRichard Kuo printk(KERN_INFO "&_K_init_devicetable = 0x%08x\n",
217499236d9SRichard Kuo (unsigned long) _K_init_devicetable-PAGE_OFFSET);
218499236d9SRichard Kuo *segtable = ((u32) (unsigned long) _K_init_devicetable-PAGE_OFFSET) |
219499236d9SRichard Kuo __HVM_PDE_S_4KB;
220499236d9SRichard Kuo printk(KERN_INFO "*segtable = 0x%08x\n", *segtable);
221499236d9SRichard Kuo #endif
222499236d9SRichard Kuo
223499236d9SRichard Kuo /*
224499236d9SRichard Kuo * The bootmem allocator seemingly just lives to feed memory
225499236d9SRichard Kuo * to the paging system
226499236d9SRichard Kuo */
227499236d9SRichard Kuo printk(KERN_INFO "PAGE_SIZE=%lu\n", PAGE_SIZE);
228499236d9SRichard Kuo paging_init(); /* See Gorman Book, 2.3 */
229499236d9SRichard Kuo
230499236d9SRichard Kuo /*
231499236d9SRichard Kuo * At this point, the page allocator is kind of initialized, but
232499236d9SRichard Kuo * apparently no pages are available (just like with the bootmem
233499236d9SRichard Kuo * allocator), and need to be freed themselves via mem_init(),
234499236d9SRichard Kuo * which is called by start_kernel() later on in the process
235499236d9SRichard Kuo */
236499236d9SRichard Kuo }
237*b2022dcfSAnshuman Khandual
238*b2022dcfSAnshuman Khandual static const pgprot_t protection_map[16] = {
239*b2022dcfSAnshuman Khandual [VM_NONE] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
240*b2022dcfSAnshuman Khandual CACHEDEF),
241*b2022dcfSAnshuman Khandual [VM_READ] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
242*b2022dcfSAnshuman Khandual _PAGE_READ | CACHEDEF),
243*b2022dcfSAnshuman Khandual [VM_WRITE] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
244*b2022dcfSAnshuman Khandual CACHEDEF),
245*b2022dcfSAnshuman Khandual [VM_WRITE | VM_READ] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
246*b2022dcfSAnshuman Khandual _PAGE_READ | CACHEDEF),
247*b2022dcfSAnshuman Khandual [VM_EXEC] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
248*b2022dcfSAnshuman Khandual _PAGE_EXECUTE | CACHEDEF),
249*b2022dcfSAnshuman Khandual [VM_EXEC | VM_READ] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
250*b2022dcfSAnshuman Khandual _PAGE_EXECUTE | _PAGE_READ |
251*b2022dcfSAnshuman Khandual CACHEDEF),
252*b2022dcfSAnshuman Khandual [VM_EXEC | VM_WRITE] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
253*b2022dcfSAnshuman Khandual _PAGE_EXECUTE | CACHEDEF),
254*b2022dcfSAnshuman Khandual [VM_EXEC | VM_WRITE | VM_READ] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
255*b2022dcfSAnshuman Khandual _PAGE_EXECUTE | _PAGE_READ |
256*b2022dcfSAnshuman Khandual CACHEDEF),
257*b2022dcfSAnshuman Khandual [VM_SHARED] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
258*b2022dcfSAnshuman Khandual CACHEDEF),
259*b2022dcfSAnshuman Khandual [VM_SHARED | VM_READ] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
260*b2022dcfSAnshuman Khandual _PAGE_READ | CACHEDEF),
261*b2022dcfSAnshuman Khandual [VM_SHARED | VM_WRITE] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
262*b2022dcfSAnshuman Khandual _PAGE_WRITE | CACHEDEF),
263*b2022dcfSAnshuman Khandual [VM_SHARED | VM_WRITE | VM_READ] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
264*b2022dcfSAnshuman Khandual _PAGE_READ | _PAGE_WRITE |
265*b2022dcfSAnshuman Khandual CACHEDEF),
266*b2022dcfSAnshuman Khandual [VM_SHARED | VM_EXEC] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
267*b2022dcfSAnshuman Khandual _PAGE_EXECUTE | CACHEDEF),
268*b2022dcfSAnshuman Khandual [VM_SHARED | VM_EXEC | VM_READ] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
269*b2022dcfSAnshuman Khandual _PAGE_EXECUTE | _PAGE_READ |
270*b2022dcfSAnshuman Khandual CACHEDEF),
271*b2022dcfSAnshuman Khandual [VM_SHARED | VM_EXEC | VM_WRITE] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
272*b2022dcfSAnshuman Khandual _PAGE_EXECUTE | _PAGE_WRITE |
273*b2022dcfSAnshuman Khandual CACHEDEF),
274*b2022dcfSAnshuman Khandual [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
275*b2022dcfSAnshuman Khandual _PAGE_READ | _PAGE_EXECUTE |
276*b2022dcfSAnshuman Khandual _PAGE_WRITE | CACHEDEF)
277*b2022dcfSAnshuman Khandual };
278*b2022dcfSAnshuman Khandual DECLARE_VM_GET_PAGE_PROT
279