mm: remove include/linux/bootmem.h
[linux-2.6-block.git] / arch / mips / loongson64 / loongson-3 / numa.c
CommitLineData
c4617318
HC
1/*
2 * Copyright (C) 2010 Loongson Inc. & Lemote Inc. &
0bb383a2 3 * Institute of Computing Technology
c4617318
HC
4 * Author: Xiang Gao, gaoxiang@ict.ac.cn
5 * Huacai Chen, chenhc@lemote.com
6 * Xiaofu Meng, Shuangshuang Zhang
7 *
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.
12 */
13#include <linux/init.h>
14#include <linux/kernel.h>
15#include <linux/mm.h>
16#include <linux/mmzone.h>
26dd3e4f 17#include <linux/export.h>
c4617318
HC
18#include <linux/nodemask.h>
19#include <linux/swap.h>
20#include <linux/memblock.h>
c4617318
HC
21#include <linux/pfn.h>
22#include <linux/highmem.h>
23#include <asm/page.h>
24#include <asm/pgalloc.h>
25#include <asm/sections.h>
c4617318
HC
26#include <linux/irq.h>
27#include <asm/bootinfo.h>
28#include <asm/mc146818-time.h>
29#include <asm/time.h>
30#include <asm/wbflush.h>
31#include <boot_param.h>
32
33static struct node_data prealloc__node_data[MAX_NUMNODES];
34unsigned char __node_distances[MAX_NUMNODES][MAX_NUMNODES];
21255dad 35EXPORT_SYMBOL(__node_distances);
c4617318
HC
36struct node_data *__node_data[MAX_NUMNODES];
37EXPORT_SYMBOL(__node_data);
38
39static void enable_lpa(void)
40{
41 unsigned long value;
42
43 value = __read_32bit_c0_register($16, 3);
44 value |= 0x00000080;
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);
48
49 value = __read_32bit_c0_register($5, 1);
50 value |= 0x20000000;
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);
54}
55
56static void cpu_node_probe(void)
57{
58 int i;
59
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());
65 }
66
67 pr_info("NUMA: Discovered %d cpus on %d nodes\n",
68 loongson_sysconf.nr_cpus, num_online_nodes());
69}
70
71static int __init compute_node_distance(int row, int col)
72{
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;
77
78 if (col == row)
79 return 0;
80 else if (package_row == package_col)
81 return 40;
82 else
83 return 100;
84}
85
86static void __init init_topology_matrix(void)
87{
88 int row, col;
89
90 for (row = 0; row < MAX_NUMNODES; row++)
91 for (col = 0; col < MAX_NUMNODES; col++)
92 __node_distances[row][col] = -1;
93
94 for_each_online_node(row) {
95 for_each_online_node(col) {
96 __node_distances[row][col] =
97 compute_node_distance(row, col);
98 }
99 }
100}
101
102static unsigned long nid_to_addroffset(unsigned int nid)
103{
104 unsigned long result;
105 switch (nid) {
106 case 0:
107 default:
108 result = NODE0_ADDRSPACE_OFFSET;
109 break;
110 case 1:
111 result = NODE1_ADDRSPACE_OFFSET;
112 break;
113 case 2:
114 result = NODE2_ADDRSPACE_OFFSET;
115 break;
116 case 3:
117 result = NODE3_ADDRSPACE_OFFSET;
118 break;
119 }
120 return result;
121}
122
123static void __init szmem(unsigned int node)
124{
125 u32 i, mem_type;
126 static unsigned long num_physpages = 0;
127 u64 node_id, node_psize, start_pfn, end_pfn, mem_start, mem_size;
128
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;
132 if (node_id != node)
133 continue;
134
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;
138
139 switch (mem_type) {
140 case SYSTEM_RAM_LOW:
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);
153 break;
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);
167 break;
6045f241 168 case SYSTEM_RAM_RESERVED:
c4617318
HC
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),
174 mem_size << 20);
175 break;
176 }
177 }
178}
179
180static void __init node_mem_init(unsigned int node)
181{
c4617318 182 unsigned long node_addrspace_offset;
bcec54bf 183 unsigned long start_pfn, end_pfn;
c4617318
HC
184
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);
188
189 get_pfn_range_for_nid(node, &start_pfn, &end_pfn);
bcec54bf
MR
190 pr_info("Node%d: start_pfn=0x%lx, end_pfn=0x%lx\n",
191 node, start_pfn, end_pfn);
c4617318
HC
192
193 __node_data[node] = prealloc__node_data + node;
194
c4617318
HC
195 NODE_DATA(node)->node_start_pfn = start_pfn;
196 NODE_DATA(node)->node_spanned_pages = end_pfn - start_pfn;
197
c4617318 198 free_bootmem_with_active_regions(node, end_pfn);
bcec54bf
MR
199
200 if (node == 0) {
201 /* kernel end address */
202 unsigned long kernel_end_pfn = PFN_UP(__pa_symbol(&_end));
203
204 /* used by finalize_initrd() */
c4617318
HC
205 max_low_pfn = end_pfn;
206
bcec54bf
MR
207 /* Reserve the kernel text/data/bss */
208 memblock_reserve(start_pfn << PAGE_SHIFT,
209 ((kernel_end_pfn - start_pfn) << PAGE_SHIFT));
c4617318 210
3484de7b 211 /* Reserve 0xfe000000~0xffffffff for RS780E integrated GPU */
bcec54bf
MR
212 if (node_end_pfn(0) >= (0xffffffff >> PAGE_SHIFT))
213 memblock_reserve((node_addrspace_offset | 0xfe000000),
214 32 << 20);
c4617318
HC
215 }
216
217 sparse_memory_present_with_active_regions(node);
218}
219
220static __init void prom_meminit(void)
221{
ec0f8d3f 222 unsigned int node, cpu, active_cpu = 0;
c4617318
HC
223
224 cpu_node_probe();
225 init_topology_matrix();
226
227 for (node = 0; node < loongson_sysconf.nr_nodes; node++) {
228 if (node_online(node)) {
229 szmem(node);
230 node_mem_init(node);
8dd92891 231 cpumask_clear(&__node_data[(node)]->cpumask);
c4617318
HC
232 }
233 }
234 for (cpu = 0; cpu < loongson_sysconf.nr_cpus; cpu++) {
235 node = cpu / loongson_sysconf.cores_per_node;
236 if (node >= num_online_nodes())
237 node = 0;
ec0f8d3f
HC
238
239 if (loongson_sysconf.reserved_cpus_mask & (1<<cpu))
240 continue;
241
8dd92891 242 cpumask_set_cpu(active_cpu, &__node_data[(node)]->cpumask);
ec0f8d3f
HC
243 pr_info("NUMA: set cpumask cpu %d on node %d\n", active_cpu, node);
244
245 active_cpu++;
c4617318
HC
246 }
247}
248
249void __init paging_init(void)
250{
251 unsigned node;
252 unsigned long zones_size[MAX_NR_ZONES] = {0, };
253
254 pagetable_init();
255
256 for_each_online_node(node) {
257 unsigned long start_pfn, end_pfn;
258
259 get_pfn_range_for_nid(node, &start_pfn, &end_pfn);
260
261 if (end_pfn > max_low_pfn)
262 max_low_pfn = end_pfn;
263 }
264#ifdef CONFIG_ZONE_DMA32
265 zones_size[ZONE_DMA32] = MAX_DMA32_PFN;
266#endif
267 zones_size[ZONE_NORMAL] = max_low_pfn;
268 free_area_init_nodes(zones_size);
269}
270
271void __init mem_init(void)
272{
273 high_memory = (void *) __va(get_num_physpages() << PAGE_SHIFT);
c6ffc5ca 274 memblock_free_all();
c4617318
HC
275 setup_zero_pages(); /* This comes from node 0 */
276 mem_init_print_info(NULL);
277}
278
279/* All PCI device belongs to logical Node-0 */
280int pcibus_to_node(struct pci_bus *bus)
281{
282 return 0;
283}
284EXPORT_SYMBOL(pcibus_to_node);
285
286void __init prom_init_numa_memory(void)
287{
288 enable_lpa();
289 prom_meminit();
290}
291EXPORT_SYMBOL(prom_init_numa_memory);