2 * Copyright (C) 2010 Loongson Inc. & Lemote Inc. &
3 * Institute of Computing Technology
6 * Xiaofu Meng, Shuangshuang Zhang
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
13 #include <linux/init.h>
14 #include <linux/kernel.h>
16 #include <linux/mmzone.h>
17 #include <linux/export.h>
18 #include <linux/nodemask.h>
19 #include <linux/swap.h>
20 #include <linux/memblock.h>
21 #include <linux/pfn.h>
22 #include <linux/highmem.h>
24 #include <asm/pgalloc.h>
25 #include <asm/sections.h>
26 #include <linux/irq.h>
27 #include <asm/bootinfo.h>
28 #include <asm/mc146818-time.h>
30 #include <asm/wbflush.h>
31 #include <boot_param.h>
33 static struct node_data prealloc__node_data[MAX_NUMNODES];
34 unsigned char __node_distances[MAX_NUMNODES][MAX_NUMNODES];
35 EXPORT_SYMBOL(__node_distances);
36 struct node_data *__node_data[MAX_NUMNODES];
37 EXPORT_SYMBOL(__node_data);
39 static void enable_lpa(void)
43 value = __read_32bit_c0_register($16, 3);
45 __write_32bit_c0_register($16, 3, value);
46 value = __read_32bit_c0_register($16, 3);
47 pr_info("CP0_Config3: CP0 16.3 (0x%lx)\n", value);
49 value = __read_32bit_c0_register($5, 1);
51 __write_32bit_c0_register($5, 1, value);
52 value = __read_32bit_c0_register($5, 1);
53 pr_info("CP0_PageGrain: CP0 5.1 (0x%lx)\n", value);
56 static void cpu_node_probe(void)
60 nodes_clear(node_possible_map);
61 nodes_clear(node_online_map);
62 for (i = 0; i < loongson_sysconf.nr_nodes; i++) {
63 node_set_state(num_online_nodes(), N_POSSIBLE);
64 node_set_online(num_online_nodes());
67 pr_info("NUMA: Discovered %d cpus on %d nodes\n",
68 loongson_sysconf.nr_cpus, num_online_nodes());
71 static int __init compute_node_distance(int row, int col)
73 int package_row = row * loongson_sysconf.cores_per_node /
74 loongson_sysconf.cores_per_package;
75 int package_col = col * loongson_sysconf.cores_per_node /
76 loongson_sysconf.cores_per_package;
80 else if (package_row == package_col)
86 static void __init init_topology_matrix(void)
90 for (row = 0; row < MAX_NUMNODES; row++)
91 for (col = 0; col < MAX_NUMNODES; col++)
92 __node_distances[row][col] = -1;
94 for_each_online_node(row) {
95 for_each_online_node(col) {
96 __node_distances[row][col] =
97 compute_node_distance(row, col);
102 static unsigned long nid_to_addroffset(unsigned int nid)
104 unsigned long result;
108 result = NODE0_ADDRSPACE_OFFSET;
111 result = NODE1_ADDRSPACE_OFFSET;
114 result = NODE2_ADDRSPACE_OFFSET;
117 result = NODE3_ADDRSPACE_OFFSET;
123 static void __init szmem(unsigned int node)
126 static unsigned long num_physpages = 0;
127 u64 node_id, node_psize, start_pfn, end_pfn, mem_start, mem_size;
129 /* Parse memory information and activate */
130 for (i = 0; i < loongson_memmap->nr_map; i++) {
131 node_id = loongson_memmap->map[i].node_id;
135 mem_type = loongson_memmap->map[i].mem_type;
136 mem_size = loongson_memmap->map[i].mem_size;
137 mem_start = loongson_memmap->map[i].mem_start;
141 start_pfn = ((node_id << 44) + mem_start) >> PAGE_SHIFT;
142 node_psize = (mem_size << 20) >> PAGE_SHIFT;
143 end_pfn = start_pfn + node_psize;
144 num_physpages += node_psize;
145 pr_info("Node%d: mem_type:%d, mem_start:0x%llx, mem_size:0x%llx MB\n",
146 (u32)node_id, mem_type, mem_start, mem_size);
147 pr_info(" start_pfn:0x%llx, end_pfn:0x%llx, num_physpages:0x%lx\n",
148 start_pfn, end_pfn, num_physpages);
149 add_memory_region((node_id << 44) + mem_start,
150 (u64)mem_size << 20, BOOT_MEM_RAM);
151 memblock_add_node(PFN_PHYS(start_pfn),
152 PFN_PHYS(end_pfn - start_pfn), node);
154 case SYSTEM_RAM_HIGH:
155 start_pfn = ((node_id << 44) + mem_start) >> PAGE_SHIFT;
156 node_psize = (mem_size << 20) >> PAGE_SHIFT;
157 end_pfn = start_pfn + node_psize;
158 num_physpages += node_psize;
159 pr_info("Node%d: mem_type:%d, mem_start:0x%llx, mem_size:0x%llx MB\n",
160 (u32)node_id, mem_type, mem_start, mem_size);
161 pr_info(" start_pfn:0x%llx, end_pfn:0x%llx, num_physpages:0x%lx\n",
162 start_pfn, end_pfn, num_physpages);
163 add_memory_region((node_id << 44) + mem_start,
164 (u64)mem_size << 20, BOOT_MEM_RAM);
165 memblock_add_node(PFN_PHYS(start_pfn),
166 PFN_PHYS(end_pfn - start_pfn), node);
168 case SYSTEM_RAM_RESERVED:
169 pr_info("Node%d: mem_type:%d, mem_start:0x%llx, mem_size:0x%llx MB\n",
170 (u32)node_id, mem_type, mem_start, mem_size);
171 add_memory_region((node_id << 44) + mem_start,
172 (u64)mem_size << 20, BOOT_MEM_RESERVED);
173 memblock_reserve(((node_id << 44) + mem_start),
180 static void __init node_mem_init(unsigned int node)
182 unsigned long node_addrspace_offset;
183 unsigned long start_pfn, end_pfn;
185 node_addrspace_offset = nid_to_addroffset(node);
186 pr_info("Node%d's addrspace_offset is 0x%lx\n",
187 node, node_addrspace_offset);
189 get_pfn_range_for_nid(node, &start_pfn, &end_pfn);
190 pr_info("Node%d: start_pfn=0x%lx, end_pfn=0x%lx\n",
191 node, start_pfn, end_pfn);
193 __node_data[node] = prealloc__node_data + node;
195 NODE_DATA(node)->node_start_pfn = start_pfn;
196 NODE_DATA(node)->node_spanned_pages = end_pfn - start_pfn;
198 free_bootmem_with_active_regions(node, end_pfn);
201 /* kernel end address */
202 unsigned long kernel_end_pfn = PFN_UP(__pa_symbol(&_end));
204 /* used by finalize_initrd() */
205 max_low_pfn = end_pfn;
207 /* Reserve the kernel text/data/bss */
208 memblock_reserve(start_pfn << PAGE_SHIFT,
209 ((kernel_end_pfn - start_pfn) << PAGE_SHIFT));
211 /* Reserve 0xfe000000~0xffffffff for RS780E integrated GPU */
212 if (node_end_pfn(0) >= (0xffffffff >> PAGE_SHIFT))
213 memblock_reserve((node_addrspace_offset | 0xfe000000),
217 sparse_memory_present_with_active_regions(node);
220 static __init void prom_meminit(void)
222 unsigned int node, cpu, active_cpu = 0;
225 init_topology_matrix();
227 for (node = 0; node < loongson_sysconf.nr_nodes; node++) {
228 if (node_online(node)) {
231 cpumask_clear(&__node_data[(node)]->cpumask);
234 max_low_pfn = PHYS_PFN(memblock_end_of_DRAM());
236 for (cpu = 0; cpu < loongson_sysconf.nr_cpus; cpu++) {
237 node = cpu / loongson_sysconf.cores_per_node;
238 if (node >= num_online_nodes())
241 if (loongson_sysconf.reserved_cpus_mask & (1<<cpu))
244 cpumask_set_cpu(active_cpu, &__node_data[(node)]->cpumask);
245 pr_info("NUMA: set cpumask cpu %d on node %d\n", active_cpu, node);
251 void __init paging_init(void)
253 unsigned long zones_size[MAX_NR_ZONES] = {0, };
256 #ifdef CONFIG_ZONE_DMA32
257 zones_size[ZONE_DMA32] = MAX_DMA32_PFN;
259 zones_size[ZONE_NORMAL] = max_low_pfn;
260 free_area_init_nodes(zones_size);
263 void __init mem_init(void)
265 high_memory = (void *) __va(get_num_physpages() << PAGE_SHIFT);
267 setup_zero_pages(); /* This comes from node 0 */
268 mem_init_print_info(NULL);
271 /* All PCI device belongs to logical Node-0 */
272 int pcibus_to_node(struct pci_bus *bus)
276 EXPORT_SYMBOL(pcibus_to_node);
278 void __init prom_init_numa_memory(void)
283 EXPORT_SYMBOL(prom_init_numa_memory);