1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2010 Loongson Inc. & Lemote Inc. & 4 * Institute of Computing Technology 5 * Author: Xiang Gao, gaoxiang@ict.ac.cn 6 * Huacai Chen, chenhc@lemote.com 7 * Xiaofu Meng, Shuangshuang Zhang 8 */ 9 #include <linux/init.h> 10 #include <linux/kernel.h> 11 #include <linux/mm.h> 12 #include <linux/mmzone.h> 13 #include <linux/export.h> 14 #include <linux/nodemask.h> 15 #include <linux/swap.h> 16 #include <linux/memblock.h> 17 #include <linux/pfn.h> 18 #include <linux/highmem.h> 19 #include <asm/page.h> 20 #include <asm/pgalloc.h> 21 #include <asm/sections.h> 22 #include <linux/irq.h> 23 #include <asm/bootinfo.h> 24 #include <asm/mc146818-time.h> 25 #include <asm/time.h> 26 #include <asm/wbflush.h> 27 #include <boot_param.h> 28 #include <loongson.h> 29 30 static struct pglist_data prealloc__node_data[MAX_NUMNODES]; 31 unsigned char __node_distances[MAX_NUMNODES][MAX_NUMNODES]; 32 EXPORT_SYMBOL(__node_distances); 33 struct pglist_data *__node_data[MAX_NUMNODES]; 34 EXPORT_SYMBOL(__node_data); 35 36 cpumask_t __node_cpumask[MAX_NUMNODES]; 37 EXPORT_SYMBOL(__node_cpumask); 38 39 static void cpu_node_probe(void) 40 { 41 int i; 42 43 nodes_clear(node_possible_map); 44 nodes_clear(node_online_map); 45 for (i = 0; i < loongson_sysconf.nr_nodes; i++) { 46 node_set_state(num_online_nodes(), N_POSSIBLE); 47 node_set_online(num_online_nodes()); 48 } 49 50 pr_info("NUMA: Discovered %d cpus on %d nodes\n", 51 loongson_sysconf.nr_cpus, num_online_nodes()); 52 } 53 54 static int __init compute_node_distance(int row, int col) 55 { 56 int package_row = row * loongson_sysconf.cores_per_node / 57 loongson_sysconf.cores_per_package; 58 int package_col = col * loongson_sysconf.cores_per_node / 59 loongson_sysconf.cores_per_package; 60 61 if (col == row) 62 return LOCAL_DISTANCE; 63 else if (package_row == package_col) 64 return 40; 65 else 66 return 100; 67 } 68 69 static void __init init_topology_matrix(void) 70 { 71 int row, col; 72 73 for (row = 0; row < MAX_NUMNODES; row++) 74 for (col = 0; col < MAX_NUMNODES; col++) 75 __node_distances[row][col] = -1; 76 77 for_each_online_node(row) { 78 for_each_online_node(col) { 79 __node_distances[row][col] = 80 compute_node_distance(row, col); 81 } 82 } 83 } 84 85 static void __init node_mem_init(unsigned int node) 86 { 87 unsigned long node_addrspace_offset; 88 unsigned long start_pfn, end_pfn; 89 90 node_addrspace_offset = nid_to_addrbase(node); 91 pr_info("Node%d's addrspace_offset is 0x%lx\n", 92 node, node_addrspace_offset); 93 94 get_pfn_range_for_nid(node, &start_pfn, &end_pfn); 95 pr_info("Node%d: start_pfn=0x%lx, end_pfn=0x%lx\n", 96 node, start_pfn, end_pfn); 97 98 __node_data[node] = prealloc__node_data + node; 99 100 NODE_DATA(node)->node_start_pfn = start_pfn; 101 NODE_DATA(node)->node_spanned_pages = end_pfn - start_pfn; 102 103 if (node == 0) { 104 /* kernel start address */ 105 unsigned long kernel_start_pfn = PFN_DOWN(__pa_symbol(&_text)); 106 107 /* kernel end address */ 108 unsigned long kernel_end_pfn = PFN_UP(__pa_symbol(&_end)); 109 110 /* used by finalize_initrd() */ 111 max_low_pfn = end_pfn; 112 113 /* Reserve the kernel text/data/bss */ 114 memblock_reserve(kernel_start_pfn << PAGE_SHIFT, 115 ((kernel_end_pfn - kernel_start_pfn) << PAGE_SHIFT)); 116 117 /* Reserve 0xfe000000~0xffffffff for RS780E integrated GPU */ 118 if (node_end_pfn(0) >= (0xffffffff >> PAGE_SHIFT)) 119 memblock_reserve((node_addrspace_offset | 0xfe000000), 120 32 << 20); 121 } 122 } 123 124 static __init void prom_meminit(void) 125 { 126 unsigned int node, cpu, active_cpu = 0; 127 128 cpu_node_probe(); 129 init_topology_matrix(); 130 131 for (node = 0; node < loongson_sysconf.nr_nodes; node++) { 132 if (node_online(node)) { 133 szmem(node); 134 node_mem_init(node); 135 cpumask_clear(&__node_cpumask[node]); 136 } 137 } 138 max_low_pfn = PHYS_PFN(memblock_end_of_DRAM()); 139 140 for (cpu = 0; cpu < loongson_sysconf.nr_cpus; cpu++) { 141 node = cpu / loongson_sysconf.cores_per_node; 142 if (node >= num_online_nodes()) 143 node = 0; 144 145 if (loongson_sysconf.reserved_cpus_mask & (1<<cpu)) 146 continue; 147 148 cpumask_set_cpu(active_cpu, &__node_cpumask[node]); 149 pr_info("NUMA: set cpumask cpu %d on node %d\n", active_cpu, node); 150 151 active_cpu++; 152 } 153 } 154 155 void __init paging_init(void) 156 { 157 unsigned long zones_size[MAX_NR_ZONES] = {0, }; 158 159 pagetable_init(); 160 zones_size[ZONE_DMA32] = MAX_DMA32_PFN; 161 zones_size[ZONE_NORMAL] = max_low_pfn; 162 free_area_init(zones_size); 163 } 164 165 void __init mem_init(void) 166 { 167 high_memory = (void *) __va(get_num_physpages() << PAGE_SHIFT); 168 memblock_free_all(); 169 setup_zero_pages(); /* This comes from node 0 */ 170 mem_init_print_info(NULL); 171 } 172 173 /* All PCI device belongs to logical Node-0 */ 174 int pcibus_to_node(struct pci_bus *bus) 175 { 176 return 0; 177 } 178 EXPORT_SYMBOL(pcibus_to_node); 179 180 void __init prom_init_numa_memory(void) 181 { 182 pr_info("CP0_Config3: CP0 16.3 (0x%x)\n", read_c0_config3()); 183 pr_info("CP0_PageGrain: CP0 5.1 (0x%x)\n", read_c0_pagegrain()); 184 prom_meminit(); 185 } 186 EXPORT_SYMBOL(prom_init_numa_memory); 187