18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Memory subsystem initialization for Hexagon 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2010-2013, The Linux Foundation. All rights reserved. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/init.h> 98c2ecf20Sopenharmony_ci#include <linux/mm.h> 108c2ecf20Sopenharmony_ci#include <linux/memblock.h> 118c2ecf20Sopenharmony_ci#include <asm/atomic.h> 128c2ecf20Sopenharmony_ci#include <linux/highmem.h> 138c2ecf20Sopenharmony_ci#include <asm/tlb.h> 148c2ecf20Sopenharmony_ci#include <asm/sections.h> 158c2ecf20Sopenharmony_ci#include <asm/vm_mmu.h> 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci/* 188c2ecf20Sopenharmony_ci * Define a startpg just past the end of the kernel image and a lastpg 198c2ecf20Sopenharmony_ci * that corresponds to the end of real or simulated platform memory. 208c2ecf20Sopenharmony_ci */ 218c2ecf20Sopenharmony_ci#define bootmem_startpg (PFN_UP(((unsigned long) _end) - PAGE_OFFSET + PHYS_OFFSET)) 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ciunsigned long bootmem_lastpg; /* Should be set by platform code */ 248c2ecf20Sopenharmony_ciunsigned long __phys_offset; /* physical kernel offset >> 12 */ 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci/* Set as variable to limit PMD copies */ 278c2ecf20Sopenharmony_ciint max_kernel_seg = 0x303; 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci/* indicate pfn's of high memory */ 308c2ecf20Sopenharmony_ciunsigned long highstart_pfn, highend_pfn; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ciDEFINE_PER_CPU(struct mmu_gather, mmu_gathers); 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci/* Default cache attribute for newly created page tables */ 358c2ecf20Sopenharmony_ciunsigned long _dflt_cache_att = CACHEDEF; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci/* 388c2ecf20Sopenharmony_ci * The current "generation" of kernel map, which should not roll 398c2ecf20Sopenharmony_ci * over until Hell freezes over. Actual bound in years needs to be 408c2ecf20Sopenharmony_ci * calculated to confirm. 418c2ecf20Sopenharmony_ci */ 428c2ecf20Sopenharmony_ciDEFINE_SPINLOCK(kmap_gen_lock); 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci/* checkpatch says don't init this to 0. */ 458c2ecf20Sopenharmony_ciunsigned long long kmap_generation; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci/* 488c2ecf20Sopenharmony_ci * mem_init - initializes memory 498c2ecf20Sopenharmony_ci * 508c2ecf20Sopenharmony_ci * Frees up bootmem 518c2ecf20Sopenharmony_ci * Fixes up more stuff for HIGHMEM 528c2ecf20Sopenharmony_ci * Calculates and displays memory available/used 538c2ecf20Sopenharmony_ci */ 548c2ecf20Sopenharmony_civoid __init mem_init(void) 558c2ecf20Sopenharmony_ci{ 568c2ecf20Sopenharmony_ci /* No idea where this is actually declared. Seems to evade LXR. */ 578c2ecf20Sopenharmony_ci memblock_free_all(); 588c2ecf20Sopenharmony_ci mem_init_print_info(NULL); 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci /* 618c2ecf20Sopenharmony_ci * To-Do: someone somewhere should wipe out the bootmem map 628c2ecf20Sopenharmony_ci * after we're done? 638c2ecf20Sopenharmony_ci */ 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci /* 668c2ecf20Sopenharmony_ci * This can be moved to some more virtual-memory-specific 678c2ecf20Sopenharmony_ci * initialization hook at some point. Set the init_mm 688c2ecf20Sopenharmony_ci * descriptors "context" value to point to the initial 698c2ecf20Sopenharmony_ci * kernel segment table's physical address. 708c2ecf20Sopenharmony_ci */ 718c2ecf20Sopenharmony_ci init_mm.context.ptbase = __pa(init_mm.pgd); 728c2ecf20Sopenharmony_ci} 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_civoid sync_icache_dcache(pte_t pte) 758c2ecf20Sopenharmony_ci{ 768c2ecf20Sopenharmony_ci unsigned long addr; 778c2ecf20Sopenharmony_ci struct page *page; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci page = pte_page(pte); 808c2ecf20Sopenharmony_ci addr = (unsigned long) page_address(page); 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci __vmcache_idsync(addr, PAGE_SIZE); 838c2ecf20Sopenharmony_ci} 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci/* 868c2ecf20Sopenharmony_ci * In order to set up page allocator "nodes", 878c2ecf20Sopenharmony_ci * somebody has to call free_area_init() for UMA. 888c2ecf20Sopenharmony_ci * 898c2ecf20Sopenharmony_ci * In this mode, we only have one pg_data_t 908c2ecf20Sopenharmony_ci * structure: contig_mem_data. 918c2ecf20Sopenharmony_ci */ 928c2ecf20Sopenharmony_civoid __init paging_init(void) 938c2ecf20Sopenharmony_ci{ 948c2ecf20Sopenharmony_ci unsigned long max_zone_pfn[MAX_NR_ZONES] = {0, }; 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci /* 978c2ecf20Sopenharmony_ci * This is not particularly well documented anywhere, but 988c2ecf20Sopenharmony_ci * give ZONE_NORMAL all the memory, including the big holes 998c2ecf20Sopenharmony_ci * left by the kernel+bootmem_map which are already left as reserved 1008c2ecf20Sopenharmony_ci * in the bootmem_map; free_area_init should see those bits and 1018c2ecf20Sopenharmony_ci * adjust accordingly. 1028c2ecf20Sopenharmony_ci */ 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci max_zone_pfn[ZONE_NORMAL] = max_low_pfn; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci free_area_init(max_zone_pfn); /* sets up the zonelists and mem_map */ 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci /* 1098c2ecf20Sopenharmony_ci * Start of high memory area. Will probably need something more 1108c2ecf20Sopenharmony_ci * fancy if we... get more fancy. 1118c2ecf20Sopenharmony_ci */ 1128c2ecf20Sopenharmony_ci high_memory = (void *)((bootmem_lastpg + 1) << PAGE_SHIFT); 1138c2ecf20Sopenharmony_ci} 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci#ifndef DMA_RESERVE 1168c2ecf20Sopenharmony_ci#define DMA_RESERVE (4) 1178c2ecf20Sopenharmony_ci#endif 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci#define DMA_CHUNKSIZE (1<<22) 1208c2ecf20Sopenharmony_ci#define DMA_RESERVED_BYTES (DMA_RESERVE * DMA_CHUNKSIZE) 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci/* 1238c2ecf20Sopenharmony_ci * Pick out the memory size. We look for mem=size, 1248c2ecf20Sopenharmony_ci * where size is "size[KkMm]" 1258c2ecf20Sopenharmony_ci */ 1268c2ecf20Sopenharmony_cistatic int __init early_mem(char *p) 1278c2ecf20Sopenharmony_ci{ 1288c2ecf20Sopenharmony_ci unsigned long size; 1298c2ecf20Sopenharmony_ci char *endp; 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci size = memparse(p, &endp); 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci bootmem_lastpg = PFN_DOWN(size); 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci return 0; 1368c2ecf20Sopenharmony_ci} 1378c2ecf20Sopenharmony_ciearly_param("mem", early_mem); 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_cisize_t hexagon_coherent_pool_size = (size_t) (DMA_RESERVE << 22); 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_civoid __init setup_arch_memory(void) 1428c2ecf20Sopenharmony_ci{ 1438c2ecf20Sopenharmony_ci /* XXX Todo: this probably should be cleaned up */ 1448c2ecf20Sopenharmony_ci u32 *segtable = (u32 *) &swapper_pg_dir[0]; 1458c2ecf20Sopenharmony_ci u32 *segtable_end; 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci /* 1488c2ecf20Sopenharmony_ci * Set up boot memory allocator 1498c2ecf20Sopenharmony_ci * 1508c2ecf20Sopenharmony_ci * The Gorman book also talks about these functions. 1518c2ecf20Sopenharmony_ci * This needs to change for highmem setups. 1528c2ecf20Sopenharmony_ci */ 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci /* Prior to this, bootmem_lastpg is actually mem size */ 1558c2ecf20Sopenharmony_ci bootmem_lastpg += ARCH_PFN_OFFSET; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci /* Memory size needs to be a multiple of 16M */ 1588c2ecf20Sopenharmony_ci bootmem_lastpg = PFN_DOWN((bootmem_lastpg << PAGE_SHIFT) & 1598c2ecf20Sopenharmony_ci ~((BIG_KERNEL_PAGE_SIZE) - 1)); 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci memblock_add(PHYS_OFFSET, 1628c2ecf20Sopenharmony_ci (bootmem_lastpg - ARCH_PFN_OFFSET) << PAGE_SHIFT); 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci /* Reserve kernel text/data/bss */ 1658c2ecf20Sopenharmony_ci memblock_reserve(PHYS_OFFSET, 1668c2ecf20Sopenharmony_ci (bootmem_startpg - ARCH_PFN_OFFSET) << PAGE_SHIFT); 1678c2ecf20Sopenharmony_ci /* 1688c2ecf20Sopenharmony_ci * Reserve the top DMA_RESERVE bytes of RAM for DMA (uncached) 1698c2ecf20Sopenharmony_ci * memory allocation 1708c2ecf20Sopenharmony_ci */ 1718c2ecf20Sopenharmony_ci max_low_pfn = bootmem_lastpg - PFN_DOWN(DMA_RESERVED_BYTES); 1728c2ecf20Sopenharmony_ci min_low_pfn = ARCH_PFN_OFFSET; 1738c2ecf20Sopenharmony_ci memblock_reserve(PFN_PHYS(max_low_pfn), DMA_RESERVED_BYTES); 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci printk(KERN_INFO "bootmem_startpg: 0x%08lx\n", bootmem_startpg); 1768c2ecf20Sopenharmony_ci printk(KERN_INFO "bootmem_lastpg: 0x%08lx\n", bootmem_lastpg); 1778c2ecf20Sopenharmony_ci printk(KERN_INFO "min_low_pfn: 0x%08lx\n", min_low_pfn); 1788c2ecf20Sopenharmony_ci printk(KERN_INFO "max_low_pfn: 0x%08lx\n", max_low_pfn); 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci /* 1818c2ecf20Sopenharmony_ci * The default VM page tables (will be) populated with 1828c2ecf20Sopenharmony_ci * VA=PA+PAGE_OFFSET mapping. We go in and invalidate entries 1838c2ecf20Sopenharmony_ci * higher than what we have memory for. 1848c2ecf20Sopenharmony_ci */ 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci /* this is pointer arithmetic; each entry covers 4MB */ 1878c2ecf20Sopenharmony_ci segtable = segtable + (PAGE_OFFSET >> 22); 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci /* this actually only goes to the end of the first gig */ 1908c2ecf20Sopenharmony_ci segtable_end = segtable + (1<<(30-22)); 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci /* 1938c2ecf20Sopenharmony_ci * Move forward to the start of empty pages; take into account 1948c2ecf20Sopenharmony_ci * phys_offset shift. 1958c2ecf20Sopenharmony_ci */ 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci segtable += (bootmem_lastpg-ARCH_PFN_OFFSET)>>(22-PAGE_SHIFT); 1988c2ecf20Sopenharmony_ci { 1998c2ecf20Sopenharmony_ci int i; 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci for (i = 1 ; i <= DMA_RESERVE ; i++) 2028c2ecf20Sopenharmony_ci segtable[-i] = ((segtable[-i] & __HVM_PTE_PGMASK_4MB) 2038c2ecf20Sopenharmony_ci | __HVM_PTE_R | __HVM_PTE_W | __HVM_PTE_X 2048c2ecf20Sopenharmony_ci | __HEXAGON_C_UNC << 6 2058c2ecf20Sopenharmony_ci | __HVM_PDE_S_4MB); 2068c2ecf20Sopenharmony_ci } 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci printk(KERN_INFO "clearing segtable from %p to %p\n", segtable, 2098c2ecf20Sopenharmony_ci segtable_end); 2108c2ecf20Sopenharmony_ci while (segtable < (segtable_end-8)) 2118c2ecf20Sopenharmony_ci *(segtable++) = __HVM_PDE_S_INVALID; 2128c2ecf20Sopenharmony_ci /* stop the pointer at the device I/O 4MB page */ 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci printk(KERN_INFO "segtable = %p (should be equal to _K_io_map)\n", 2158c2ecf20Sopenharmony_ci segtable); 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci#if 0 2188c2ecf20Sopenharmony_ci /* Other half of the early device table from vm_init_segtable. */ 2198c2ecf20Sopenharmony_ci printk(KERN_INFO "&_K_init_devicetable = 0x%08x\n", 2208c2ecf20Sopenharmony_ci (unsigned long) _K_init_devicetable-PAGE_OFFSET); 2218c2ecf20Sopenharmony_ci *segtable = ((u32) (unsigned long) _K_init_devicetable-PAGE_OFFSET) | 2228c2ecf20Sopenharmony_ci __HVM_PDE_S_4KB; 2238c2ecf20Sopenharmony_ci printk(KERN_INFO "*segtable = 0x%08x\n", *segtable); 2248c2ecf20Sopenharmony_ci#endif 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci /* 2278c2ecf20Sopenharmony_ci * The bootmem allocator seemingly just lives to feed memory 2288c2ecf20Sopenharmony_ci * to the paging system 2298c2ecf20Sopenharmony_ci */ 2308c2ecf20Sopenharmony_ci printk(KERN_INFO "PAGE_SIZE=%lu\n", PAGE_SIZE); 2318c2ecf20Sopenharmony_ci paging_init(); /* See Gorman Book, 2.3 */ 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci /* 2348c2ecf20Sopenharmony_ci * At this point, the page allocator is kind of initialized, but 2358c2ecf20Sopenharmony_ci * apparently no pages are available (just like with the bootmem 2368c2ecf20Sopenharmony_ci * allocator), and need to be freed themselves via mem_init(), 2378c2ecf20Sopenharmony_ci * which is called by start_kernel() later on in the process 2388c2ecf20Sopenharmony_ci */ 2398c2ecf20Sopenharmony_ci} 240