1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/init.h>
4 #include <linux/bootmem.h>
5 #include <linux/percpu.h>
6 #include <linux/kexec.h>
7 #include <linux/crash_dump.h>
9 #include <linux/topology.h>
10 #include <asm/sections.h>
11 #include <asm/processor.h>
12 #include <asm/setup.h>
13 #include <asm/mpspec.h>
14 #include <asm/apicdef.h>
15 #include <asm/highmem.h>
16 #include <asm/proto.h>
17 #include <asm/cpumask.h>
19 #ifdef CONFIG_DEBUG_PER_CPU_MAPS
20 # define DBG(x...) printk(KERN_DEBUG x)
26 * Could be inside CONFIG_HAVE_SETUP_PER_CPU_AREA with other stuff but
27 * voyager wants cpu_number too.
30 DEFINE_PER_CPU(int, cpu_number);
31 EXPORT_PER_CPU_SYMBOL(cpu_number);
34 #ifdef CONFIG_X86_LOCAL_APIC
35 unsigned int num_processors;
36 unsigned disabled_cpus __cpuinitdata;
37 /* Processor that is doing the boot up */
38 unsigned int boot_cpu_physical_apicid = -1U;
39 EXPORT_SYMBOL(boot_cpu_physical_apicid);
40 unsigned int max_physical_apicid;
42 /* Bitmask of physically existing CPUs */
43 physid_mask_t phys_cpu_present_map;
47 * Map cpu index to physical APIC ID
49 DEFINE_EARLY_PER_CPU(u16, x86_cpu_to_apicid, BAD_APICID);
50 DEFINE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid, BAD_APICID);
51 EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
52 EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
54 #if defined(CONFIG_NUMA) && defined(CONFIG_X86_64)
55 #define X86_64_NUMA 1 /* (used later) */
56 DEFINE_PER_CPU(int, node_number) = 0;
57 EXPORT_PER_CPU_SYMBOL(node_number);
60 * Map cpu index to node index
62 DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE);
63 EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map);
66 * Which logical CPUs are on which nodes
68 cpumask_t *node_to_cpumask_map;
69 EXPORT_SYMBOL(node_to_cpumask_map);
72 * Setup node_to_cpumask_map
74 static void __init setup_node_to_cpumask_map(void);
77 static inline void setup_node_to_cpumask_map(void) { }
82 /* correctly size the local cpu masks */
83 static void setup_cpu_local_masks(void)
85 alloc_bootmem_cpumask_var(&cpu_initialized_mask);
86 alloc_bootmem_cpumask_var(&cpu_callin_mask);
87 alloc_bootmem_cpumask_var(&cpu_callout_mask);
88 alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
91 #else /* CONFIG_X86_32 */
93 static inline void setup_cpu_local_masks(void)
97 #endif /* CONFIG_X86_32 */
99 #ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
102 unsigned long __per_cpu_offset[NR_CPUS] __read_mostly = {
103 [0] = (unsigned long)__per_cpu_load,
106 unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
108 EXPORT_SYMBOL(__per_cpu_offset);
112 * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
113 * Always point %gs to its beginning
115 void __init setup_per_cpu_areas(void)
117 ssize_t size, old_size;
120 unsigned long align = 1;
122 /* Copy section for each CPU (we discard the original) */
123 old_size = PERCPU_ENOUGH_ROOM;
124 align = max_t(unsigned long, PAGE_SIZE, align);
125 size = roundup(old_size, align);
127 pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%d nr_node_ids:%d\n",
128 NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids);
130 pr_info("PERCPU: Allocating %zd bytes of per cpu data\n", size);
132 for_each_possible_cpu(cpu) {
133 #ifndef CONFIG_NEED_MULTIPLE_NODES
134 ptr = __alloc_bootmem(size, align,
135 __pa(MAX_DMA_ADDRESS));
137 int node = early_cpu_to_node(cpu);
138 if (!node_online(node) || !NODE_DATA(node)) {
139 ptr = __alloc_bootmem(size, align,
140 __pa(MAX_DMA_ADDRESS));
141 pr_info("cpu %d has no node %d or node-local memory\n",
143 pr_debug("per cpu data for cpu%d at %016lx\n",
146 ptr = __alloc_bootmem_node(NODE_DATA(node), size, align,
147 __pa(MAX_DMA_ADDRESS));
148 pr_debug("per cpu data for cpu%d on node%d at %016lx\n",
149 cpu, node, __pa(ptr));
153 memcpy(ptr, __per_cpu_load, __per_cpu_end - __per_cpu_start);
154 per_cpu_offset(cpu) = ptr - __per_cpu_start;
155 per_cpu(this_cpu_off, cpu) = per_cpu_offset(cpu);
156 per_cpu(cpu_number, cpu) = cpu;
158 * Copy data used in early init routines from the initial arrays to the
159 * per cpu data areas. These arrays then become expendable and the
160 * *_early_ptr's are zeroed indicating that the static arrays are gone.
162 per_cpu(x86_cpu_to_apicid, cpu) =
163 early_per_cpu_map(x86_cpu_to_apicid, cpu);
164 per_cpu(x86_bios_cpu_apicid, cpu) =
165 early_per_cpu_map(x86_bios_cpu_apicid, cpu);
167 per_cpu(x86_cpu_to_node_map, cpu) =
168 early_per_cpu_map(x86_cpu_to_node_map, cpu);
171 per_cpu(irq_stack_ptr, cpu) =
172 per_cpu(irq_stack_union.irq_stack, cpu) + IRQ_STACK_SIZE - 64;
174 * Up to this point, CPU0 has been using .data.init
175 * area. Reload %gs offset for CPU0.
181 DBG("PERCPU: cpu %4d %p\n", cpu, ptr);
184 /* indicate the early static arrays will soon be gone */
185 early_per_cpu_ptr(x86_cpu_to_apicid) = NULL;
186 early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL;
188 early_per_cpu_ptr(x86_cpu_to_node_map) = NULL;
191 /* Setup node to cpumask map */
192 setup_node_to_cpumask_map();
194 /* Setup cpu initialized, callin, callout masks */
195 setup_cpu_local_masks();
203 * Allocate node_to_cpumask_map based on number of available nodes
204 * Requires node_possible_map to be valid.
206 * Note: node_to_cpumask() is not valid until after this is done.
207 * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.)
209 static void __init setup_node_to_cpumask_map(void)
211 unsigned int node, num = 0;
214 /* setup nr_node_ids if not done yet */
215 if (nr_node_ids == MAX_NUMNODES) {
216 for_each_node_mask(node, node_possible_map)
218 nr_node_ids = num + 1;
221 /* allocate the map */
222 map = alloc_bootmem_low(nr_node_ids * sizeof(cpumask_t));
223 DBG("node_to_cpumask_map at %p for %d nodes\n", map, nr_node_ids);
225 pr_debug("Node to cpumask map at %p for %d nodes\n",
228 /* node_to_cpumask() will now work */
229 node_to_cpumask_map = map;
232 void __cpuinit numa_set_node(int cpu, int node)
234 int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
236 /* early setting, no percpu area yet */
237 if (cpu_to_node_map) {
238 cpu_to_node_map[cpu] = node;
242 #ifdef CONFIG_DEBUG_PER_CPU_MAPS
243 if (cpu >= nr_cpu_ids || !per_cpu_offset(cpu)) {
244 printk(KERN_ERR "numa_set_node: invalid cpu# (%d)\n", cpu);
249 per_cpu(x86_cpu_to_node_map, cpu) = node;
251 if (node != NUMA_NO_NODE)
252 per_cpu(node_number, cpu) = node;
255 void __cpuinit numa_clear_node(int cpu)
257 numa_set_node(cpu, NUMA_NO_NODE);
260 #ifndef CONFIG_DEBUG_PER_CPU_MAPS
262 void __cpuinit numa_add_cpu(int cpu)
264 cpu_set(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
267 void __cpuinit numa_remove_cpu(int cpu)
269 cpu_clear(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
272 #else /* CONFIG_DEBUG_PER_CPU_MAPS */
275 * --------- debug versions of the numa functions ---------
277 static void __cpuinit numa_set_cpumask(int cpu, int enable)
279 int node = early_cpu_to_node(cpu);
283 if (node_to_cpumask_map == NULL) {
284 printk(KERN_ERR "node_to_cpumask_map NULL\n");
289 mask = &node_to_cpumask_map[node];
293 cpu_clear(cpu, *mask);
295 cpulist_scnprintf(buf, sizeof(buf), mask);
296 printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
297 enable ? "numa_add_cpu" : "numa_remove_cpu", cpu, node, buf);
300 void __cpuinit numa_add_cpu(int cpu)
302 numa_set_cpumask(cpu, 1);
305 void __cpuinit numa_remove_cpu(int cpu)
307 numa_set_cpumask(cpu, 0);
310 int cpu_to_node(int cpu)
312 if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
314 "cpu_to_node(%d): usage too early!\n", cpu);
316 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
318 return per_cpu(x86_cpu_to_node_map, cpu);
320 EXPORT_SYMBOL(cpu_to_node);
323 * Same function as cpu_to_node() but used if called before the
324 * per_cpu areas are setup.
326 int early_cpu_to_node(int cpu)
328 if (early_per_cpu_ptr(x86_cpu_to_node_map))
329 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
331 if (!per_cpu_offset(cpu)) {
333 "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
337 return per_cpu(x86_cpu_to_node_map, cpu);
342 static const cpumask_t cpu_mask_none;
345 * Returns a pointer to the bitmask of CPUs on Node 'node'.
347 const cpumask_t *cpumask_of_node(int node)
349 if (node_to_cpumask_map == NULL) {
351 "cpumask_of_node(%d): no node_to_cpumask_map!\n",
354 return (const cpumask_t *)&cpu_online_map;
356 if (node >= nr_node_ids) {
358 "cpumask_of_node(%d): node > nr_node_ids(%d)\n",
361 return &cpu_mask_none;
363 return &node_to_cpumask_map[node];
365 EXPORT_SYMBOL(cpumask_of_node);
368 * Returns a bitmask of CPUs on Node 'node'.
370 * Side note: this function creates the returned cpumask on the stack
371 * so with a high NR_CPUS count, excessive stack space is used. The
372 * node_to_cpumask_ptr function should be used whenever possible.
374 cpumask_t node_to_cpumask(int node)
376 if (node_to_cpumask_map == NULL) {
378 "node_to_cpumask(%d): no node_to_cpumask_map!\n", node);
380 return cpu_online_map;
382 if (node >= nr_node_ids) {
384 "node_to_cpumask(%d): node > nr_node_ids(%d)\n",
387 return cpu_mask_none;
389 return node_to_cpumask_map[node];
391 EXPORT_SYMBOL(node_to_cpumask);
394 * --------- end of debug versions of the numa functions ---------
397 #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
399 #endif /* X86_64_NUMA */