blob: 76e305e064f96f9f7ece081665de18e7fb189e9a [file] [log] [blame]
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -03001#include <linux/kernel.h>
2#include <linux/module.h>
3#include <linux/init.h>
4#include <linux/bootmem.h>
5#include <linux/percpu.h>
Bernhard Walle1ecd2762008-06-20 15:38:22 +02006#include <linux/kexec.h>
Yinghai Lu17b4cce2008-06-21 21:02:20 -07007#include <linux/crash_dump.h>
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -03008#include <asm/smp.h>
9#include <asm/percpu.h>
10#include <asm/sections.h>
11#include <asm/processor.h>
12#include <asm/setup.h>
13#include <asm/topology.h>
Alexey Starikovskiy0fc09062008-04-04 23:40:48 +040014#include <asm/mpspec.h>
Alexey Starikovskiy76eb4132008-04-04 23:40:41 +040015#include <asm/apicdef.h>
Bernhard Walle1ecd2762008-06-20 15:38:22 +020016#include <asm/highmem.h>
Alexey Starikovskiy76eb4132008-04-04 23:40:41 +040017
James Bottomleyf8955eb2008-05-10 09:01:48 -050018#ifdef CONFIG_X86_LOCAL_APIC
Alexey Starikovskiy2fe60142008-04-04 23:41:44 +040019unsigned int num_processors;
20unsigned disabled_cpus __cpuinitdata;
21/* Processor that is doing the boot up */
22unsigned int boot_cpu_physical_apicid = -1U;
Yinghai Lue0da3362008-06-08 18:29:22 -070023unsigned int max_physical_apicid;
Alexey Starikovskiy2fe60142008-04-04 23:41:44 +040024EXPORT_SYMBOL(boot_cpu_physical_apicid);
25
Alexey Starikovskiy0fc09062008-04-04 23:40:48 +040026/* Bitmask of physically existing CPUs */
27physid_mask_t phys_cpu_present_map;
James Bottomleyf8955eb2008-05-10 09:01:48 -050028#endif
Alexey Starikovskiy0fc09062008-04-04 23:40:48 +040029
Mike Travis23ca4bb2008-05-12 21:21:12 +020030/* map cpu index to physical APIC ID */
31DEFINE_EARLY_PER_CPU(u16, x86_cpu_to_apicid, BAD_APICID);
32DEFINE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid, BAD_APICID);
33EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
34EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
35
36#if defined(CONFIG_NUMA) && defined(CONFIG_X86_64)
37#define X86_64_NUMA 1
38
Mike Travis7891a242008-05-12 21:21:12 +020039/* map cpu index to node index */
Mike Travis23ca4bb2008-05-12 21:21:12 +020040DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE);
41EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map);
Mike Travis9f248bd2008-05-12 21:21:12 +020042
43/* which logical CPUs are on which nodes */
44cpumask_t *node_to_cpumask_map;
45EXPORT_SYMBOL(node_to_cpumask_map);
46
47/* setup node_to_cpumask_map */
48static void __init setup_node_to_cpumask_map(void);
49
50#else
51static inline void setup_node_to_cpumask_map(void) { }
Mike Travis23ca4bb2008-05-12 21:21:12 +020052#endif
53
James Bottomleyf8955eb2008-05-10 09:01:48 -050054#if defined(CONFIG_HAVE_SETUP_PER_CPU_AREA) && defined(CONFIG_X86_SMP)
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030055/*
56 * Copy data used in early init routines from the initial arrays to the
57 * per cpu data areas. These arrays then become expendable and the
58 * *_early_ptr's are zeroed indicating that the static arrays are gone.
59 */
60static void __init setup_per_cpu_maps(void)
61{
62 int cpu;
63
64 for_each_possible_cpu(cpu) {
Mike Travis23ca4bb2008-05-12 21:21:12 +020065 per_cpu(x86_cpu_to_apicid, cpu) =
66 early_per_cpu_map(x86_cpu_to_apicid, cpu);
Mike Travisb447a462008-03-25 15:06:51 -070067 per_cpu(x86_bios_cpu_apicid, cpu) =
Mike Travis23ca4bb2008-05-12 21:21:12 +020068 early_per_cpu_map(x86_bios_cpu_apicid, cpu);
69#ifdef X86_64_NUMA
Mike Travisb447a462008-03-25 15:06:51 -070070 per_cpu(x86_cpu_to_node_map, cpu) =
Mike Travis23ca4bb2008-05-12 21:21:12 +020071 early_per_cpu_map(x86_cpu_to_node_map, cpu);
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030072#endif
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030073 }
74
75 /* indicate the early static arrays will soon be gone */
Mike Travis23ca4bb2008-05-12 21:21:12 +020076 early_per_cpu_ptr(x86_cpu_to_apicid) = NULL;
77 early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL;
78#ifdef X86_64_NUMA
79 early_per_cpu_ptr(x86_cpu_to_node_map) = NULL;
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030080#endif
81}
82
83#ifdef CONFIG_X86_32
84/*
85 * Great future not-so-futuristic plan: make i386 and x86_64 do it
86 * the same way
87 */
88unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
89EXPORT_SYMBOL(__per_cpu_offset);
Mike Travis3461b0a2008-05-12 21:21:13 +020090static inline void setup_cpu_pda_map(void) { }
91
92#elif !defined(CONFIG_SMP)
93static inline void setup_cpu_pda_map(void) { }
94
95#else /* CONFIG_SMP && CONFIG_X86_64 */
96
97/*
98 * Allocate cpu_pda pointer table and array via alloc_bootmem.
99 */
100static void __init setup_cpu_pda_map(void)
101{
102 char *pda;
103 struct x8664_pda **new_cpu_pda;
104 unsigned long size;
105 int cpu;
106
107 size = roundup(sizeof(struct x8664_pda), cache_line_size());
108
109 /* allocate cpu_pda array and pointer table */
110 {
111 unsigned long tsize = nr_cpu_ids * sizeof(void *);
112 unsigned long asize = size * (nr_cpu_ids - 1);
113
114 tsize = roundup(tsize, cache_line_size());
115 new_cpu_pda = alloc_bootmem(tsize + asize);
116 pda = (char *)new_cpu_pda + tsize;
117 }
118
119 /* initialize pointer table to static pda's */
120 for_each_possible_cpu(cpu) {
121 if (cpu == 0) {
122 /* leave boot cpu pda in place */
123 new_cpu_pda[0] = cpu_pda(0);
124 continue;
125 }
126 new_cpu_pda[cpu] = (struct x8664_pda *)pda;
127 new_cpu_pda[cpu]->in_bootmem = 1;
128 pda += size;
129 }
130
131 /* point to new pointer table */
132 _cpu_pda = new_cpu_pda;
133}
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300134#endif
135
136/*
137 * Great future plan:
138 * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
139 * Always point %gs to its beginning
140 */
141void __init setup_per_cpu_areas(void)
142{
Mike Travis3461b0a2008-05-12 21:21:13 +0200143 ssize_t size = PERCPU_ENOUGH_ROOM;
144 char *ptr;
145 int cpu;
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300146
Mike Travis3461b0a2008-05-12 21:21:13 +0200147 /* Setup cpu_pda map */
148 setup_cpu_pda_map();
149
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300150 /* Copy section for each CPU (we discard the original) */
151 size = PERCPU_ENOUGH_ROOM;
Randy Dunlap053713f2008-06-05 11:10:59 -0700152 printk(KERN_INFO "PERCPU: Allocating %zd bytes of per cpu data\n",
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300153 size);
Mike Travisb447a462008-03-25 15:06:51 -0700154
Mike Travis3461b0a2008-05-12 21:21:13 +0200155 for_each_possible_cpu(cpu) {
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300156#ifndef CONFIG_NEED_MULTIPLE_NODES
157 ptr = alloc_bootmem_pages(size);
158#else
Mike Travis3461b0a2008-05-12 21:21:13 +0200159 int node = early_cpu_to_node(cpu);
Mike Travisb447a462008-03-25 15:06:51 -0700160 if (!node_online(node) || !NODE_DATA(node)) {
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300161 ptr = alloc_bootmem_pages(size);
Mike Travisb447a462008-03-25 15:06:51 -0700162 printk(KERN_INFO
Mike Travis23ca4bb2008-05-12 21:21:12 +0200163 "cpu %d has no node %d or node-local memory\n",
Mike Travis3461b0a2008-05-12 21:21:13 +0200164 cpu, node);
Mike Travisb447a462008-03-25 15:06:51 -0700165 }
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300166 else
167 ptr = alloc_bootmem_pages_node(NODE_DATA(node), size);
168#endif
Mike Travis3461b0a2008-05-12 21:21:13 +0200169 per_cpu_offset(cpu) = ptr - __per_cpu_start;
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300170 memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
Mike Travis9f0e8d02008-04-04 18:11:01 -0700171
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300172 }
173
Mike Travis9f248bd2008-05-12 21:21:12 +0200174 printk(KERN_DEBUG "NR_CPUS: %d, nr_cpu_ids: %d, nr_node_ids %d\n",
175 NR_CPUS, nr_cpu_ids, nr_node_ids);
Mike Travis9f0e8d02008-04-04 18:11:01 -0700176
Mike Travisb447a462008-03-25 15:06:51 -0700177 /* Setup percpu data maps */
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300178 setup_per_cpu_maps();
Mike Travis9f0e8d02008-04-04 18:11:01 -0700179
Mike Travis9f248bd2008-05-12 21:21:12 +0200180 /* Setup node to cpumask map */
181 setup_node_to_cpumask_map();
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300182}
183
184#endif
Huang, Yingc45a7072008-06-02 14:26:25 +0800185
Mike Travis23ca4bb2008-05-12 21:21:12 +0200186#ifdef X86_64_NUMA
Mike Travis9f248bd2008-05-12 21:21:12 +0200187
188/*
189 * Allocate node_to_cpumask_map based on number of available nodes
190 * Requires node_possible_map to be valid.
191 *
192 * Note: node_to_cpumask() is not valid until after this is done.
193 */
194static void __init setup_node_to_cpumask_map(void)
195{
196 unsigned int node, num = 0;
197 cpumask_t *map;
198
199 /* setup nr_node_ids if not done yet */
200 if (nr_node_ids == MAX_NUMNODES) {
201 for_each_node_mask(node, node_possible_map)
202 num = node;
203 nr_node_ids = num + 1;
204 }
205
206 /* allocate the map */
207 map = alloc_bootmem_low(nr_node_ids * sizeof(cpumask_t));
208
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200209 pr_debug(KERN_DEBUG "Node to cpumask map at %p for %d nodes\n",
210 map, nr_node_ids);
Mike Travis9f248bd2008-05-12 21:21:12 +0200211
212 /* node_to_cpumask() will now work */
213 node_to_cpumask_map = map;
214}
215
Mike Travis23ca4bb2008-05-12 21:21:12 +0200216void __cpuinit numa_set_node(int cpu, int node)
217{
218 int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
219
Mike Travis3461b0a2008-05-12 21:21:13 +0200220 if (cpu_pda(cpu) && node != NUMA_NO_NODE)
Mike Travis7891a242008-05-12 21:21:12 +0200221 cpu_pda(cpu)->nodenumber = node;
222
Mike Travis23ca4bb2008-05-12 21:21:12 +0200223 if (cpu_to_node_map)
224 cpu_to_node_map[cpu] = node;
225
226 else if (per_cpu_offset(cpu))
227 per_cpu(x86_cpu_to_node_map, cpu) = node;
228
229 else
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200230 pr_debug("Setting node for non-present cpu %d\n", cpu);
Mike Travis23ca4bb2008-05-12 21:21:12 +0200231}
232
233void __cpuinit numa_clear_node(int cpu)
234{
235 numa_set_node(cpu, NUMA_NO_NODE);
236}
237
Mike Travis9f248bd2008-05-12 21:21:12 +0200238#ifndef CONFIG_DEBUG_PER_CPU_MAPS
239
Mike Travis23ca4bb2008-05-12 21:21:12 +0200240void __cpuinit numa_add_cpu(int cpu)
241{
242 cpu_set(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
243}
244
245void __cpuinit numa_remove_cpu(int cpu)
246{
247 cpu_clear(cpu, node_to_cpumask_map[cpu_to_node(cpu)]);
248}
Mike Travis23ca4bb2008-05-12 21:21:12 +0200249
Mike Travis9f248bd2008-05-12 21:21:12 +0200250#else /* CONFIG_DEBUG_PER_CPU_MAPS */
251
252/*
253 * --------- debug versions of the numa functions ---------
254 */
255static void __cpuinit numa_set_cpumask(int cpu, int enable)
256{
257 int node = cpu_to_node(cpu);
258 cpumask_t *mask;
259 char buf[64];
260
261 if (node_to_cpumask_map == NULL) {
262 printk(KERN_ERR "node_to_cpumask_map NULL\n");
263 dump_stack();
264 return;
265 }
266
267 mask = &node_to_cpumask_map[node];
268 if (enable)
269 cpu_set(cpu, *mask);
270 else
271 cpu_clear(cpu, *mask);
272
273 cpulist_scnprintf(buf, sizeof(buf), *mask);
274 printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
275 enable? "numa_add_cpu":"numa_remove_cpu", cpu, node, buf);
276 }
277
278void __cpuinit numa_add_cpu(int cpu)
279{
280 numa_set_cpumask(cpu, 1);
281}
282
283void __cpuinit numa_remove_cpu(int cpu)
284{
285 numa_set_cpumask(cpu, 0);
286}
Mike Travis23ca4bb2008-05-12 21:21:12 +0200287
288int cpu_to_node(int cpu)
289{
290 if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
291 printk(KERN_WARNING
292 "cpu_to_node(%d): usage too early!\n", cpu);
293 dump_stack();
294 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
295 }
296 return per_cpu(x86_cpu_to_node_map, cpu);
297}
298EXPORT_SYMBOL(cpu_to_node);
299
Mike Travis9f248bd2008-05-12 21:21:12 +0200300/*
301 * Same function as cpu_to_node() but used if called before the
302 * per_cpu areas are setup.
303 */
Mike Travis23ca4bb2008-05-12 21:21:12 +0200304int early_cpu_to_node(int cpu)
305{
306 if (early_per_cpu_ptr(x86_cpu_to_node_map))
307 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
308
309 if (!per_cpu_offset(cpu)) {
310 printk(KERN_WARNING
311 "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
Mike Travis9f248bd2008-05-12 21:21:12 +0200312 dump_stack();
Mike Travis23ca4bb2008-05-12 21:21:12 +0200313 return NUMA_NO_NODE;
314 }
315 return per_cpu(x86_cpu_to_node_map, cpu);
316}
Mike Travis9f248bd2008-05-12 21:21:12 +0200317
Mike Travis6a2f47c2008-06-27 10:10:13 -0700318
319/* empty cpumask */
320static const cpumask_t cpu_mask_none;
321
Mike Travis9f248bd2008-05-12 21:21:12 +0200322/*
323 * Returns a pointer to the bitmask of CPUs on Node 'node'.
324 */
Mike Travis11369f352008-07-08 14:35:21 -0700325const cpumask_t *_node_to_cpumask_ptr(int node)
Mike Travis9f248bd2008-05-12 21:21:12 +0200326{
327 if (node_to_cpumask_map == NULL) {
328 printk(KERN_WARNING
329 "_node_to_cpumask_ptr(%d): no node_to_cpumask_map!\n",
330 node);
331 dump_stack();
Mike Travis11369f352008-07-08 14:35:21 -0700332 return (const cpumask_t *)&cpu_online_map;
Mike Travis9f248bd2008-05-12 21:21:12 +0200333 }
Mike Travis6a2f47c2008-06-27 10:10:13 -0700334 if (node >= nr_node_ids) {
335 printk(KERN_WARNING
336 "_node_to_cpumask_ptr(%d): node > nr_node_ids(%d)\n",
337 node, nr_node_ids);
338 dump_stack();
Mike Travis11369f352008-07-08 14:35:21 -0700339 return &cpu_mask_none;
Mike Travis6a2f47c2008-06-27 10:10:13 -0700340 }
Mike Travis11369f352008-07-08 14:35:21 -0700341 return &node_to_cpumask_map[node];
Mike Travis9f248bd2008-05-12 21:21:12 +0200342}
343EXPORT_SYMBOL(_node_to_cpumask_ptr);
344
345/*
346 * Returns a bitmask of CPUs on Node 'node'.
Mike Travis6a2f47c2008-06-27 10:10:13 -0700347 *
348 * Side note: this function creates the returned cpumask on the stack
349 * so with a high NR_CPUS count, excessive stack space is used. The
350 * node_to_cpumask_ptr function should be used whenever possible.
Mike Travis9f248bd2008-05-12 21:21:12 +0200351 */
352cpumask_t node_to_cpumask(int node)
353{
354 if (node_to_cpumask_map == NULL) {
355 printk(KERN_WARNING
356 "node_to_cpumask(%d): no node_to_cpumask_map!\n", node);
357 dump_stack();
358 return cpu_online_map;
359 }
Mike Travis6a2f47c2008-06-27 10:10:13 -0700360 if (node >= nr_node_ids) {
361 printk(KERN_WARNING
362 "node_to_cpumask(%d): node > nr_node_ids(%d)\n",
363 node, nr_node_ids);
364 dump_stack();
365 return cpu_mask_none;
366 }
Mike Travis9f248bd2008-05-12 21:21:12 +0200367 return node_to_cpumask_map[node];
368}
369EXPORT_SYMBOL(node_to_cpumask);
370
371/*
372 * --------- end of debug versions of the numa functions ---------
373 */
374
375#endif /* CONFIG_DEBUG_PER_CPU_MAPS */
376
377#endif /* X86_64_NUMA */
Bernhard Walle1ecd2762008-06-20 15:38:22 +0200378