blob: 1cd53dfcd3092c289b6e478b8da64d6767d08cb7 [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
Mike Travis9f0e8d02008-04-04 18:11:01 -070083#ifdef CONFIG_HAVE_CPUMASK_OF_CPU_MAP
Mike Travis6524d932008-07-24 18:21:30 -070084/*
85 * Replace static cpumask_of_cpu_map in the initdata section,
86 * with one that's allocated sized by the possible number of cpus.
87 *
88 * (requires nr_cpu_ids to be initialized)
89 */
Mike Travis9f0e8d02008-04-04 18:11:01 -070090static void __init setup_cpumask_of_cpu(void)
91{
92 int i;
93
94 /* alloc_bootmem zeroes memory */
95 cpumask_of_cpu_map = alloc_bootmem_low(sizeof(cpumask_t) * nr_cpu_ids);
96 for (i = 0; i < nr_cpu_ids; i++)
97 cpu_set(i, cpumask_of_cpu_map[i]);
98}
99#else
100static inline void setup_cpumask_of_cpu(void) { }
101#endif
102
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300103#ifdef CONFIG_X86_32
104/*
105 * Great future not-so-futuristic plan: make i386 and x86_64 do it
106 * the same way
107 */
108unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
109EXPORT_SYMBOL(__per_cpu_offset);
Mike Travis3461b0a2008-05-12 21:21:13 +0200110static inline void setup_cpu_pda_map(void) { }
111
112#elif !defined(CONFIG_SMP)
113static inline void setup_cpu_pda_map(void) { }
114
115#else /* CONFIG_SMP && CONFIG_X86_64 */
116
117/*
118 * Allocate cpu_pda pointer table and array via alloc_bootmem.
119 */
120static void __init setup_cpu_pda_map(void)
121{
122 char *pda;
123 struct x8664_pda **new_cpu_pda;
124 unsigned long size;
125 int cpu;
126
127 size = roundup(sizeof(struct x8664_pda), cache_line_size());
128
129 /* allocate cpu_pda array and pointer table */
130 {
131 unsigned long tsize = nr_cpu_ids * sizeof(void *);
132 unsigned long asize = size * (nr_cpu_ids - 1);
133
134 tsize = roundup(tsize, cache_line_size());
135 new_cpu_pda = alloc_bootmem(tsize + asize);
136 pda = (char *)new_cpu_pda + tsize;
137 }
138
139 /* initialize pointer table to static pda's */
140 for_each_possible_cpu(cpu) {
141 if (cpu == 0) {
142 /* leave boot cpu pda in place */
143 new_cpu_pda[0] = cpu_pda(0);
144 continue;
145 }
146 new_cpu_pda[cpu] = (struct x8664_pda *)pda;
147 new_cpu_pda[cpu]->in_bootmem = 1;
148 pda += size;
149 }
150
151 /* point to new pointer table */
152 _cpu_pda = new_cpu_pda;
153}
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300154#endif
155
156/*
157 * Great future plan:
158 * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
159 * Always point %gs to its beginning
160 */
161void __init setup_per_cpu_areas(void)
162{
Mike Travis3461b0a2008-05-12 21:21:13 +0200163 ssize_t size = PERCPU_ENOUGH_ROOM;
164 char *ptr;
165 int cpu;
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300166
Mike Travis3461b0a2008-05-12 21:21:13 +0200167 /* Setup cpu_pda map */
168 setup_cpu_pda_map();
169
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300170 /* Copy section for each CPU (we discard the original) */
171 size = PERCPU_ENOUGH_ROOM;
Randy Dunlap053713f2008-06-05 11:10:59 -0700172 printk(KERN_INFO "PERCPU: Allocating %zd bytes of per cpu data\n",
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300173 size);
Mike Travisb447a462008-03-25 15:06:51 -0700174
Mike Travis3461b0a2008-05-12 21:21:13 +0200175 for_each_possible_cpu(cpu) {
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300176#ifndef CONFIG_NEED_MULTIPLE_NODES
177 ptr = alloc_bootmem_pages(size);
178#else
Mike Travis3461b0a2008-05-12 21:21:13 +0200179 int node = early_cpu_to_node(cpu);
Mike Travisb447a462008-03-25 15:06:51 -0700180 if (!node_online(node) || !NODE_DATA(node)) {
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300181 ptr = alloc_bootmem_pages(size);
Mike Travisb447a462008-03-25 15:06:51 -0700182 printk(KERN_INFO
Mike Travis23ca4bb2008-05-12 21:21:12 +0200183 "cpu %d has no node %d or node-local memory\n",
Mike Travis3461b0a2008-05-12 21:21:13 +0200184 cpu, node);
Mike Travisb447a462008-03-25 15:06:51 -0700185 }
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300186 else
187 ptr = alloc_bootmem_pages_node(NODE_DATA(node), size);
188#endif
Mike Travis3461b0a2008-05-12 21:21:13 +0200189 per_cpu_offset(cpu) = ptr - __per_cpu_start;
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300190 memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
Mike Travis9f0e8d02008-04-04 18:11:01 -0700191
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300192 }
193
Mike Travis9f248bd2008-05-12 21:21:12 +0200194 printk(KERN_DEBUG "NR_CPUS: %d, nr_cpu_ids: %d, nr_node_ids %d\n",
195 NR_CPUS, nr_cpu_ids, nr_node_ids);
Mike Travis9f0e8d02008-04-04 18:11:01 -0700196
Mike Travisb447a462008-03-25 15:06:51 -0700197 /* Setup percpu data maps */
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300198 setup_per_cpu_maps();
Mike Travis9f0e8d02008-04-04 18:11:01 -0700199
Mike Travis9f248bd2008-05-12 21:21:12 +0200200 /* Setup node to cpumask map */
201 setup_node_to_cpumask_map();
202
Mike Travis9f0e8d02008-04-04 18:11:01 -0700203 /* Setup cpumask_of_cpu map */
204 setup_cpumask_of_cpu();
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300205}
206
207#endif
Huang, Yingc45a7072008-06-02 14:26:25 +0800208
Mike Travis23ca4bb2008-05-12 21:21:12 +0200209#ifdef X86_64_NUMA
Mike Travis9f248bd2008-05-12 21:21:12 +0200210
211/*
212 * Allocate node_to_cpumask_map based on number of available nodes
213 * Requires node_possible_map to be valid.
214 *
215 * Note: node_to_cpumask() is not valid until after this is done.
216 */
217static void __init setup_node_to_cpumask_map(void)
218{
219 unsigned int node, num = 0;
220 cpumask_t *map;
221
222 /* setup nr_node_ids if not done yet */
223 if (nr_node_ids == MAX_NUMNODES) {
224 for_each_node_mask(node, node_possible_map)
225 num = node;
226 nr_node_ids = num + 1;
227 }
228
229 /* allocate the map */
230 map = alloc_bootmem_low(nr_node_ids * sizeof(cpumask_t));
231
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200232 pr_debug(KERN_DEBUG "Node to cpumask map at %p for %d nodes\n",
233 map, nr_node_ids);
Mike Travis9f248bd2008-05-12 21:21:12 +0200234
235 /* node_to_cpumask() will now work */
236 node_to_cpumask_map = map;
237}
238
Mike Travis23ca4bb2008-05-12 21:21:12 +0200239void __cpuinit numa_set_node(int cpu, int node)
240{
241 int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
242
Mike Travis3461b0a2008-05-12 21:21:13 +0200243 if (cpu_pda(cpu) && node != NUMA_NO_NODE)
Mike Travis7891a242008-05-12 21:21:12 +0200244 cpu_pda(cpu)->nodenumber = node;
245
Mike Travis23ca4bb2008-05-12 21:21:12 +0200246 if (cpu_to_node_map)
247 cpu_to_node_map[cpu] = node;
248
249 else if (per_cpu_offset(cpu))
250 per_cpu(x86_cpu_to_node_map, cpu) = node;
251
252 else
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200253 pr_debug("Setting node for non-present cpu %d\n", cpu);
Mike Travis23ca4bb2008-05-12 21:21:12 +0200254}
255
256void __cpuinit numa_clear_node(int cpu)
257{
258 numa_set_node(cpu, NUMA_NO_NODE);
259}
260
Mike Travis9f248bd2008-05-12 21:21:12 +0200261#ifndef CONFIG_DEBUG_PER_CPU_MAPS
262
Mike Travis23ca4bb2008-05-12 21:21:12 +0200263void __cpuinit numa_add_cpu(int cpu)
264{
265 cpu_set(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
266}
267
268void __cpuinit numa_remove_cpu(int cpu)
269{
270 cpu_clear(cpu, node_to_cpumask_map[cpu_to_node(cpu)]);
271}
Mike Travis23ca4bb2008-05-12 21:21:12 +0200272
Mike Travis9f248bd2008-05-12 21:21:12 +0200273#else /* CONFIG_DEBUG_PER_CPU_MAPS */
274
275/*
276 * --------- debug versions of the numa functions ---------
277 */
278static void __cpuinit numa_set_cpumask(int cpu, int enable)
279{
280 int node = cpu_to_node(cpu);
281 cpumask_t *mask;
282 char buf[64];
283
284 if (node_to_cpumask_map == NULL) {
285 printk(KERN_ERR "node_to_cpumask_map NULL\n");
286 dump_stack();
287 return;
288 }
289
290 mask = &node_to_cpumask_map[node];
291 if (enable)
292 cpu_set(cpu, *mask);
293 else
294 cpu_clear(cpu, *mask);
295
296 cpulist_scnprintf(buf, sizeof(buf), *mask);
297 printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
298 enable? "numa_add_cpu":"numa_remove_cpu", cpu, node, buf);
299 }
300
301void __cpuinit numa_add_cpu(int cpu)
302{
303 numa_set_cpumask(cpu, 1);
304}
305
306void __cpuinit numa_remove_cpu(int cpu)
307{
308 numa_set_cpumask(cpu, 0);
309}
Mike Travis23ca4bb2008-05-12 21:21:12 +0200310
311int cpu_to_node(int cpu)
312{
313 if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
314 printk(KERN_WARNING
315 "cpu_to_node(%d): usage too early!\n", cpu);
316 dump_stack();
317 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
318 }
319 return per_cpu(x86_cpu_to_node_map, cpu);
320}
321EXPORT_SYMBOL(cpu_to_node);
322
Mike Travis9f248bd2008-05-12 21:21:12 +0200323/*
324 * Same function as cpu_to_node() but used if called before the
325 * per_cpu areas are setup.
326 */
Mike Travis23ca4bb2008-05-12 21:21:12 +0200327int early_cpu_to_node(int cpu)
328{
329 if (early_per_cpu_ptr(x86_cpu_to_node_map))
330 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
331
332 if (!per_cpu_offset(cpu)) {
333 printk(KERN_WARNING
334 "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
Mike Travis9f248bd2008-05-12 21:21:12 +0200335 dump_stack();
Mike Travis23ca4bb2008-05-12 21:21:12 +0200336 return NUMA_NO_NODE;
337 }
338 return per_cpu(x86_cpu_to_node_map, cpu);
339}
Mike Travis9f248bd2008-05-12 21:21:12 +0200340
Mike Travis6a2f47c2008-06-27 10:10:13 -0700341
342/* empty cpumask */
343static const cpumask_t cpu_mask_none;
344
Mike Travis9f248bd2008-05-12 21:21:12 +0200345/*
346 * Returns a pointer to the bitmask of CPUs on Node 'node'.
347 */
Mike Travis11369f352008-07-08 14:35:21 -0700348const cpumask_t *_node_to_cpumask_ptr(int node)
Mike Travis9f248bd2008-05-12 21:21:12 +0200349{
350 if (node_to_cpumask_map == NULL) {
351 printk(KERN_WARNING
352 "_node_to_cpumask_ptr(%d): no node_to_cpumask_map!\n",
353 node);
354 dump_stack();
Mike Travis11369f352008-07-08 14:35:21 -0700355 return (const cpumask_t *)&cpu_online_map;
Mike Travis9f248bd2008-05-12 21:21:12 +0200356 }
Mike Travis6a2f47c2008-06-27 10:10:13 -0700357 if (node >= nr_node_ids) {
358 printk(KERN_WARNING
359 "_node_to_cpumask_ptr(%d): node > nr_node_ids(%d)\n",
360 node, nr_node_ids);
361 dump_stack();
Mike Travis11369f352008-07-08 14:35:21 -0700362 return &cpu_mask_none;
Mike Travis6a2f47c2008-06-27 10:10:13 -0700363 }
Mike Travis11369f352008-07-08 14:35:21 -0700364 return &node_to_cpumask_map[node];
Mike Travis9f248bd2008-05-12 21:21:12 +0200365}
366EXPORT_SYMBOL(_node_to_cpumask_ptr);
367
368/*
369 * Returns a bitmask of CPUs on Node 'node'.
Mike Travis6a2f47c2008-06-27 10:10:13 -0700370 *
371 * Side note: this function creates the returned cpumask on the stack
372 * so with a high NR_CPUS count, excessive stack space is used. The
373 * node_to_cpumask_ptr function should be used whenever possible.
Mike Travis9f248bd2008-05-12 21:21:12 +0200374 */
375cpumask_t node_to_cpumask(int node)
376{
377 if (node_to_cpumask_map == NULL) {
378 printk(KERN_WARNING
379 "node_to_cpumask(%d): no node_to_cpumask_map!\n", node);
380 dump_stack();
381 return cpu_online_map;
382 }
Mike Travis6a2f47c2008-06-27 10:10:13 -0700383 if (node >= nr_node_ids) {
384 printk(KERN_WARNING
385 "node_to_cpumask(%d): node > nr_node_ids(%d)\n",
386 node, nr_node_ids);
387 dump_stack();
388 return cpu_mask_none;
389 }
Mike Travis9f248bd2008-05-12 21:21:12 +0200390 return node_to_cpumask_map[node];
391}
392EXPORT_SYMBOL(node_to_cpumask);
393
394/*
395 * --------- end of debug versions of the numa functions ---------
396 */
397
398#endif /* CONFIG_DEBUG_PER_CPU_MAPS */
399
400#endif /* X86_64_NUMA */
Bernhard Walle1ecd2762008-06-20 15:38:22 +0200401