18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * linux/arch/alpha/mm/numa.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * DISCONTIGMEM NUMA alpha support. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/types.h> 118c2ecf20Sopenharmony_ci#include <linux/kernel.h> 128c2ecf20Sopenharmony_ci#include <linux/mm.h> 138c2ecf20Sopenharmony_ci#include <linux/memblock.h> 148c2ecf20Sopenharmony_ci#include <linux/swap.h> 158c2ecf20Sopenharmony_ci#include <linux/initrd.h> 168c2ecf20Sopenharmony_ci#include <linux/pfn.h> 178c2ecf20Sopenharmony_ci#include <linux/module.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#include <asm/hwrpb.h> 208c2ecf20Sopenharmony_ci#include <asm/sections.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_cipg_data_t node_data[MAX_NUMNODES]; 238c2ecf20Sopenharmony_ciEXPORT_SYMBOL(node_data); 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci#undef DEBUG_DISCONTIG 268c2ecf20Sopenharmony_ci#ifdef DEBUG_DISCONTIG 278c2ecf20Sopenharmony_ci#define DBGDCONT(args...) printk(args) 288c2ecf20Sopenharmony_ci#else 298c2ecf20Sopenharmony_ci#define DBGDCONT(args...) 308c2ecf20Sopenharmony_ci#endif 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci#define for_each_mem_cluster(memdesc, _cluster, i) \ 338c2ecf20Sopenharmony_ci for ((_cluster) = (memdesc)->cluster, (i) = 0; \ 348c2ecf20Sopenharmony_ci (i) < (memdesc)->numclusters; (i)++, (_cluster)++) 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_cistatic void __init show_mem_layout(void) 378c2ecf20Sopenharmony_ci{ 388c2ecf20Sopenharmony_ci struct memclust_struct * cluster; 398c2ecf20Sopenharmony_ci struct memdesc_struct * memdesc; 408c2ecf20Sopenharmony_ci int i; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci /* Find free clusters, and init and free the bootmem accordingly. */ 438c2ecf20Sopenharmony_ci memdesc = (struct memdesc_struct *) 448c2ecf20Sopenharmony_ci (hwrpb->mddt_offset + (unsigned long) hwrpb); 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci printk("Raw memory layout:\n"); 478c2ecf20Sopenharmony_ci for_each_mem_cluster(memdesc, cluster, i) { 488c2ecf20Sopenharmony_ci printk(" memcluster %2d, usage %1lx, start %8lu, end %8lu\n", 498c2ecf20Sopenharmony_ci i, cluster->usage, cluster->start_pfn, 508c2ecf20Sopenharmony_ci cluster->start_pfn + cluster->numpages); 518c2ecf20Sopenharmony_ci } 528c2ecf20Sopenharmony_ci} 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_cistatic void __init 558c2ecf20Sopenharmony_cisetup_memory_node(int nid, void *kernel_end) 568c2ecf20Sopenharmony_ci{ 578c2ecf20Sopenharmony_ci extern unsigned long mem_size_limit; 588c2ecf20Sopenharmony_ci struct memclust_struct * cluster; 598c2ecf20Sopenharmony_ci struct memdesc_struct * memdesc; 608c2ecf20Sopenharmony_ci unsigned long start_kernel_pfn, end_kernel_pfn; 618c2ecf20Sopenharmony_ci unsigned long start, end; 628c2ecf20Sopenharmony_ci unsigned long node_pfn_start, node_pfn_end; 638c2ecf20Sopenharmony_ci unsigned long node_min_pfn, node_max_pfn; 648c2ecf20Sopenharmony_ci int i; 658c2ecf20Sopenharmony_ci int show_init = 0; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci /* Find the bounds of current node */ 688c2ecf20Sopenharmony_ci node_pfn_start = (node_mem_start(nid)) >> PAGE_SHIFT; 698c2ecf20Sopenharmony_ci node_pfn_end = node_pfn_start + (node_mem_size(nid) >> PAGE_SHIFT); 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci /* Find free clusters, and init and free the bootmem accordingly. */ 728c2ecf20Sopenharmony_ci memdesc = (struct memdesc_struct *) 738c2ecf20Sopenharmony_ci (hwrpb->mddt_offset + (unsigned long) hwrpb); 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci /* find the bounds of this node (node_min_pfn/node_max_pfn) */ 768c2ecf20Sopenharmony_ci node_min_pfn = ~0UL; 778c2ecf20Sopenharmony_ci node_max_pfn = 0UL; 788c2ecf20Sopenharmony_ci for_each_mem_cluster(memdesc, cluster, i) { 798c2ecf20Sopenharmony_ci /* Bit 0 is console/PALcode reserved. Bit 1 is 808c2ecf20Sopenharmony_ci non-volatile memory -- we might want to mark 818c2ecf20Sopenharmony_ci this for later. */ 828c2ecf20Sopenharmony_ci if (cluster->usage & 3) 838c2ecf20Sopenharmony_ci continue; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci start = cluster->start_pfn; 868c2ecf20Sopenharmony_ci end = start + cluster->numpages; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci if (start >= node_pfn_end || end <= node_pfn_start) 898c2ecf20Sopenharmony_ci continue; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci if (!show_init) { 928c2ecf20Sopenharmony_ci show_init = 1; 938c2ecf20Sopenharmony_ci printk("Initializing bootmem allocator on Node ID %d\n", nid); 948c2ecf20Sopenharmony_ci } 958c2ecf20Sopenharmony_ci printk(" memcluster %2d, usage %1lx, start %8lu, end %8lu\n", 968c2ecf20Sopenharmony_ci i, cluster->usage, cluster->start_pfn, 978c2ecf20Sopenharmony_ci cluster->start_pfn + cluster->numpages); 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci if (start < node_pfn_start) 1008c2ecf20Sopenharmony_ci start = node_pfn_start; 1018c2ecf20Sopenharmony_ci if (end > node_pfn_end) 1028c2ecf20Sopenharmony_ci end = node_pfn_end; 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci if (start < node_min_pfn) 1058c2ecf20Sopenharmony_ci node_min_pfn = start; 1068c2ecf20Sopenharmony_ci if (end > node_max_pfn) 1078c2ecf20Sopenharmony_ci node_max_pfn = end; 1088c2ecf20Sopenharmony_ci } 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci if (mem_size_limit && node_max_pfn > mem_size_limit) { 1118c2ecf20Sopenharmony_ci static int msg_shown = 0; 1128c2ecf20Sopenharmony_ci if (!msg_shown) { 1138c2ecf20Sopenharmony_ci msg_shown = 1; 1148c2ecf20Sopenharmony_ci printk("setup: forcing memory size to %ldK (from %ldK).\n", 1158c2ecf20Sopenharmony_ci mem_size_limit << (PAGE_SHIFT - 10), 1168c2ecf20Sopenharmony_ci node_max_pfn << (PAGE_SHIFT - 10)); 1178c2ecf20Sopenharmony_ci } 1188c2ecf20Sopenharmony_ci node_max_pfn = mem_size_limit; 1198c2ecf20Sopenharmony_ci } 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci if (node_min_pfn >= node_max_pfn) 1228c2ecf20Sopenharmony_ci return; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci /* Update global {min,max}_low_pfn from node information. */ 1258c2ecf20Sopenharmony_ci if (node_min_pfn < min_low_pfn) 1268c2ecf20Sopenharmony_ci min_low_pfn = node_min_pfn; 1278c2ecf20Sopenharmony_ci if (node_max_pfn > max_low_pfn) 1288c2ecf20Sopenharmony_ci max_pfn = max_low_pfn = node_max_pfn; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci#if 0 /* we'll try this one again in a little while */ 1318c2ecf20Sopenharmony_ci /* Cute trick to make sure our local node data is on local memory */ 1328c2ecf20Sopenharmony_ci node_data[nid] = (pg_data_t *)(__va(node_min_pfn << PAGE_SHIFT)); 1338c2ecf20Sopenharmony_ci#endif 1348c2ecf20Sopenharmony_ci printk(" Detected node memory: start %8lu, end %8lu\n", 1358c2ecf20Sopenharmony_ci node_min_pfn, node_max_pfn); 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci DBGDCONT(" DISCONTIG: node_data[%d] is at 0x%p\n", nid, NODE_DATA(nid)); 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci /* Find the bounds of kernel memory. */ 1408c2ecf20Sopenharmony_ci start_kernel_pfn = PFN_DOWN(KERNEL_START_PHYS); 1418c2ecf20Sopenharmony_ci end_kernel_pfn = PFN_UP(virt_to_phys(kernel_end)); 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci if (!nid && (node_max_pfn < end_kernel_pfn || node_min_pfn > start_kernel_pfn)) 1448c2ecf20Sopenharmony_ci panic("kernel loaded out of ram"); 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci memblock_add_node(PFN_PHYS(node_min_pfn), 1478c2ecf20Sopenharmony_ci (node_max_pfn - node_min_pfn) << PAGE_SHIFT, nid); 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci /* Zone start phys-addr must be 2^(MAX_ORDER-1) aligned. 1508c2ecf20Sopenharmony_ci Note that we round this down, not up - node memory 1518c2ecf20Sopenharmony_ci has much larger alignment than 8Mb, so it's safe. */ 1528c2ecf20Sopenharmony_ci node_min_pfn &= ~((1UL << (MAX_ORDER-1))-1); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci NODE_DATA(nid)->node_start_pfn = node_min_pfn; 1558c2ecf20Sopenharmony_ci NODE_DATA(nid)->node_present_pages = node_max_pfn - node_min_pfn; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci node_set_online(nid); 1588c2ecf20Sopenharmony_ci} 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_civoid __init 1618c2ecf20Sopenharmony_cisetup_memory(void *kernel_end) 1628c2ecf20Sopenharmony_ci{ 1638c2ecf20Sopenharmony_ci unsigned long kernel_size; 1648c2ecf20Sopenharmony_ci int nid; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci show_mem_layout(); 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci nodes_clear(node_online_map); 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci min_low_pfn = ~0UL; 1718c2ecf20Sopenharmony_ci max_low_pfn = 0UL; 1728c2ecf20Sopenharmony_ci for (nid = 0; nid < MAX_NUMNODES; nid++) 1738c2ecf20Sopenharmony_ci setup_memory_node(nid, kernel_end); 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci kernel_size = virt_to_phys(kernel_end) - KERNEL_START_PHYS; 1768c2ecf20Sopenharmony_ci memblock_reserve(KERNEL_START_PHYS, kernel_size); 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci#ifdef CONFIG_BLK_DEV_INITRD 1798c2ecf20Sopenharmony_ci initrd_start = INITRD_START; 1808c2ecf20Sopenharmony_ci if (initrd_start) { 1818c2ecf20Sopenharmony_ci extern void *move_initrd(unsigned long); 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci initrd_end = initrd_start+INITRD_SIZE; 1848c2ecf20Sopenharmony_ci printk("Initial ramdisk at: 0x%p (%lu bytes)\n", 1858c2ecf20Sopenharmony_ci (void *) initrd_start, INITRD_SIZE); 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci if ((void *)initrd_end > phys_to_virt(PFN_PHYS(max_low_pfn))) { 1888c2ecf20Sopenharmony_ci if (!move_initrd(PFN_PHYS(max_low_pfn))) 1898c2ecf20Sopenharmony_ci printk("initrd extends beyond end of memory " 1908c2ecf20Sopenharmony_ci "(0x%08lx > 0x%p)\ndisabling initrd\n", 1918c2ecf20Sopenharmony_ci initrd_end, 1928c2ecf20Sopenharmony_ci phys_to_virt(PFN_PHYS(max_low_pfn))); 1938c2ecf20Sopenharmony_ci } else { 1948c2ecf20Sopenharmony_ci nid = kvaddr_to_nid(initrd_start); 1958c2ecf20Sopenharmony_ci memblock_reserve(virt_to_phys((void *)initrd_start), 1968c2ecf20Sopenharmony_ci INITRD_SIZE); 1978c2ecf20Sopenharmony_ci } 1988c2ecf20Sopenharmony_ci } 1998c2ecf20Sopenharmony_ci#endif /* CONFIG_BLK_DEV_INITRD */ 2008c2ecf20Sopenharmony_ci} 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_civoid __init paging_init(void) 2038c2ecf20Sopenharmony_ci{ 2048c2ecf20Sopenharmony_ci unsigned long max_zone_pfn[MAX_NR_ZONES] = {0, }; 2058c2ecf20Sopenharmony_ci unsigned long dma_local_pfn; 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci /* 2088c2ecf20Sopenharmony_ci * The old global MAX_DMA_ADDRESS per-arch API doesn't fit 2098c2ecf20Sopenharmony_ci * in the NUMA model, for now we convert it to a pfn and 2108c2ecf20Sopenharmony_ci * we interpret this pfn as a local per-node information. 2118c2ecf20Sopenharmony_ci * This issue isn't very important since none of these machines 2128c2ecf20Sopenharmony_ci * have legacy ISA slots anyways. 2138c2ecf20Sopenharmony_ci */ 2148c2ecf20Sopenharmony_ci dma_local_pfn = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT; 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci max_zone_pfn[ZONE_DMA] = dma_local_pfn; 2178c2ecf20Sopenharmony_ci max_zone_pfn[ZONE_NORMAL] = max_pfn; 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci free_area_init(max_zone_pfn); 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci /* Initialize the kernel's ZERO_PGE. */ 2228c2ecf20Sopenharmony_ci memset((void *)ZERO_PGE, 0, PAGE_SIZE); 2238c2ecf20Sopenharmony_ci} 224