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 unsigned char __node_distances[MAX_NUMNODES][MAX_NUMNODES]; 31 EXPORT_SYMBOL(__node_distances); 32 33 cpumask_t __node_cpumask[MAX_NUMNODES]; 34 EXPORT_SYMBOL(__node_cpumask); 35 36 static void cpu_node_probe(void) 37 { 38 int i; 39 40 nodes_clear(node_possible_map); 41 nodes_clear(node_online_map); 42 for (i = 0; i < loongson_sysconf.nr_nodes; i++) { 43 node_set_state(num_online_nodes(), N_POSSIBLE); 44 node_set_online(num_online_nodes()); 45 } 46 47 pr_info("NUMA: Discovered %d cpus on %d nodes\n", 48 loongson_sysconf.nr_cpus, num_online_nodes()); 49 } 50 51 static int __init compute_node_distance(int row, int col) 52 { 53 int package_row = row * loongson_sysconf.cores_per_node / 54 loongson_sysconf.cores_per_package; 55 int package_col = col * loongson_sysconf.cores_per_node / 56 loongson_sysconf.cores_per_package; 57 58 if (col == row) 59 return LOCAL_DISTANCE; 60 else if (package_row == package_col) 61 return 40; 62 else 63 return 100; 64 } 65 66 static void __init init_topology_matrix(void) 67 { 68 int row, col; 69 70 for (row = 0; row < MAX_NUMNODES; row++) 71 for (col = 0; col < MAX_NUMNODES; col++) 72 __node_distances[row][col] = -1; 73 74 for_each_online_node(row) { 75 for_each_online_node(col) { 76 __node_distances[row][col] = 77 compute_node_distance(row, col); 78 } 79 } 80 } 81 82 static void __init node_mem_init(unsigned int node) 83 { 84 unsigned long node_addrspace_offset; 85 unsigned long start_pfn, end_pfn; 86 87 node_addrspace_offset = nid_to_addrbase(node); 88 pr_info("Node%d's addrspace_offset is 0x%lx\n", 89 node, node_addrspace_offset); 90 91 get_pfn_range_for_nid(node, &start_pfn, &end_pfn); 92 pr_info("Node%d: start_pfn=0x%lx, end_pfn=0x%lx\n", 93 node, start_pfn, end_pfn); 94 95 alloc_node_data(node); 96 97 NODE_DATA(node)->node_start_pfn = start_pfn; 98 NODE_DATA(node)->node_spanned_pages = end_pfn - start_pfn; 99 100 if (node == 0) { 101 /* kernel start address */ 102 unsigned long kernel_start_pfn = PFN_DOWN(__pa_symbol(&_text)); 103 104 /* kernel end address */ 105 unsigned long kernel_end_pfn = PFN_UP(__pa_symbol(&_end)); 106 107 /* used by finalize_initrd() */ 108 max_low_pfn = end_pfn; 109 110 /* Reserve the kernel text/data/bss */ 111 memblock_reserve(kernel_start_pfn << PAGE_SHIFT, 112 ((kernel_end_pfn - kernel_start_pfn) << PAGE_SHIFT)); 113 114 /* Reserve 0xfe000000~0xffffffff for RS780E integrated GPU */ 115 if (node_end_pfn(0) >= (0xffffffff >> PAGE_SHIFT)) 116 memblock_reserve((node_addrspace_offset | 0xfe000000), 117 32 << 20); 118 119 /* Reserve pfn range 0~node[0]->node_start_pfn */ 120 memblock_reserve(0, PAGE_SIZE * start_pfn); 121 /* set nid for reserved memory on node 0 */ 122 memblock_set_node(0, 1ULL << 44, &memblock.reserved, 0); 123 } 124 } 125 126 static __init void prom_meminit(void) 127 { 128 unsigned int node, cpu, active_cpu = 0; 129 130 cpu_node_probe(); 131 init_topology_matrix(); 132 133 for (node = 0; node < loongson_sysconf.nr_nodes; node++) { 134 if (node_online(node)) { 135 szmem(node); 136 node_mem_init(node); 137 cpumask_clear(&__node_cpumask[node]); 138 } 139 } 140 max_low_pfn = PHYS_PFN(memblock_end_of_DRAM()); 141 142 for (cpu = 0; cpu < loongson_sysconf.nr_cpus; cpu++) { 143 node = cpu / loongson_sysconf.cores_per_node; 144 if (node >= num_online_nodes()) 145 node = 0; 146 147 if (loongson_sysconf.reserved_cpus_mask & (1<<cpu)) 148 continue; 149 150 cpumask_set_cpu(active_cpu, &__node_cpumask[node]); 151 pr_info("NUMA: set cpumask cpu %d on node %d\n", active_cpu, node); 152 153 active_cpu++; 154 } 155 } 156 157 void __init paging_init(void) 158 { 159 unsigned long zones_size[MAX_NR_ZONES] = {0, }; 160 161 pagetable_init(); 162 zones_size[ZONE_DMA32] = MAX_DMA32_PFN; 163 zones_size[ZONE_NORMAL] = max_low_pfn; 164 free_area_init(zones_size); 165 } 166 167 void __init mem_init(void) 168 { 169 high_memory = (void *) __va(get_num_physpages() << PAGE_SHIFT); 170 memblock_free_all(); 171 setup_zero_pages(); /* This comes from node 0 */ 172 } 173 174 /* All PCI device belongs to logical Node-0 */ 175 int pcibus_to_node(struct pci_bus *bus) 176 { 177 return 0; 178 } 179 EXPORT_SYMBOL(pcibus_to_node); 180 181 void __init prom_init_numa_memory(void) 182 { 183 pr_info("CP0_Config3: CP0 16.3 (0x%x)\n", read_c0_config3()); 184 pr_info("CP0_PageGrain: CP0 5.1 (0x%x)\n", read_c0_pagegrain()); 185 prom_meminit(); 186 } 187