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>
17 #ifdef CONFIG_X86_LOCAL_APIC
18 unsigned int num_processors;
19 unsigned disabled_cpus __cpuinitdata;
20 /* Processor that is doing the boot up */
21 unsigned int boot_cpu_physical_apicid = -1U;
22 EXPORT_SYMBOL(boot_cpu_physical_apicid);
23 unsigned int max_physical_apicid;
25 /* Bitmask of physically existing CPUs */
26 physid_mask_t phys_cpu_present_map;
29 /* map cpu index to physical APIC ID */
30 DEFINE_EARLY_PER_CPU(u16, x86_cpu_to_apicid, BAD_APICID);
31 DEFINE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid, BAD_APICID);
32 EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
33 EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
35 #if defined(CONFIG_NUMA) && defined(CONFIG_X86_64)
38 /* map cpu index to node index */
39 DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE);
40 EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map);
42 /* which logical CPUs are on which nodes */
43 cpumask_t *node_to_cpumask_map;
44 EXPORT_SYMBOL(node_to_cpumask_map);
46 /* setup node_to_cpumask_map */
47 static void __init setup_node_to_cpumask_map(void);
50 static inline void setup_node_to_cpumask_map(void) { }
53 #if defined(CONFIG_HAVE_SETUP_PER_CPU_AREA) && defined(CONFIG_X86_SMP)
55 * Copy data used in early init routines from the initial arrays to the
56 * per cpu data areas. These arrays then become expendable and the
57 * *_early_ptr's are zeroed indicating that the static arrays are gone.
59 static void __init setup_per_cpu_maps(void)
63 for_each_possible_cpu(cpu) {
64 per_cpu(x86_cpu_to_apicid, cpu) =
65 early_per_cpu_map(x86_cpu_to_apicid, cpu);
66 per_cpu(x86_bios_cpu_apicid, cpu) =
67 early_per_cpu_map(x86_bios_cpu_apicid, cpu);
69 per_cpu(x86_cpu_to_node_map, cpu) =
70 early_per_cpu_map(x86_cpu_to_node_map, cpu);
74 /* indicate the early static arrays will soon be gone */
75 early_per_cpu_ptr(x86_cpu_to_apicid) = NULL;
76 early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL;
78 early_per_cpu_ptr(x86_cpu_to_node_map) = NULL;
84 * Great future not-so-futuristic plan: make i386 and x86_64 do it
87 unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
88 EXPORT_SYMBOL(__per_cpu_offset);
89 static inline void setup_cpu_pda_map(void) { }
91 #elif !defined(CONFIG_SMP)
92 static inline void setup_cpu_pda_map(void) { }
94 #else /* CONFIG_SMP && CONFIG_X86_64 */
97 * Allocate cpu_pda pointer table and array via alloc_bootmem.
99 static void __init setup_cpu_pda_map(void)
102 struct x8664_pda **new_cpu_pda;
106 size = roundup(sizeof(struct x8664_pda), cache_line_size());
108 /* allocate cpu_pda array and pointer table */
110 unsigned long tsize = nr_cpu_ids * sizeof(void *);
111 unsigned long asize = size * (nr_cpu_ids - 1);
113 tsize = roundup(tsize, cache_line_size());
114 new_cpu_pda = alloc_bootmem(tsize + asize);
115 pda = (char *)new_cpu_pda + tsize;
118 /* initialize pointer table to static pda's */
119 for_each_possible_cpu(cpu) {
121 /* leave boot cpu pda in place */
122 new_cpu_pda[0] = cpu_pda(0);
125 new_cpu_pda[cpu] = (struct x8664_pda *)pda;
126 new_cpu_pda[cpu]->in_bootmem = 1;
130 /* point to new pointer table */
131 _cpu_pda = new_cpu_pda;
134 #endif /* CONFIG_SMP && CONFIG_X86_64 */
138 /* correctly size the local cpu masks */
139 static void setup_cpu_local_masks(void)
141 alloc_bootmem_cpumask_var(&cpu_initialized_mask);
142 alloc_bootmem_cpumask_var(&cpu_callin_mask);
143 alloc_bootmem_cpumask_var(&cpu_callout_mask);
144 alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
147 #else /* CONFIG_X86_32 */
149 static inline void setup_cpu_local_masks(void)
153 #endif /* CONFIG_X86_32 */
157 * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
158 * Always point %gs to its beginning
160 void __init setup_per_cpu_areas(void)
162 ssize_t size, old_size;
165 unsigned long align = 1;
167 /* Setup cpu_pda map */
170 /* Copy section for each CPU (we discard the original) */
171 old_size = PERCPU_ENOUGH_ROOM;
172 align = max_t(unsigned long, PAGE_SIZE, align);
173 size = roundup(old_size, align);
175 pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%d nr_node_ids:%d\n",
176 NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids);
178 pr_info("PERCPU: Allocating %zd bytes of per cpu data\n", size);
180 for_each_possible_cpu(cpu) {
181 #ifndef CONFIG_NEED_MULTIPLE_NODES
182 ptr = __alloc_bootmem(size, align,
183 __pa(MAX_DMA_ADDRESS));
185 int node = early_cpu_to_node(cpu);
186 if (!node_online(node) || !NODE_DATA(node)) {
187 ptr = __alloc_bootmem(size, align,
188 __pa(MAX_DMA_ADDRESS));
189 pr_info("cpu %d has no node %d or node-local memory\n",
191 pr_debug("per cpu data for cpu%d at %016lx\n",
194 ptr = __alloc_bootmem_node(NODE_DATA(node), size, align,
195 __pa(MAX_DMA_ADDRESS));
196 pr_debug("per cpu data for cpu%d on node%d at %016lx\n",
197 cpu, node, __pa(ptr));
200 per_cpu_offset(cpu) = ptr - __per_cpu_start;
201 memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
204 /* Setup percpu data maps */
205 setup_per_cpu_maps();
207 /* Setup node to cpumask map */
208 setup_node_to_cpumask_map();
210 /* Setup cpu initialized, callin, callout masks */
211 setup_cpu_local_masks();
219 * Allocate node_to_cpumask_map based on number of available nodes
220 * Requires node_possible_map to be valid.
222 * Note: node_to_cpumask() is not valid until after this is done.
224 static void __init setup_node_to_cpumask_map(void)
226 unsigned int node, num = 0;
229 /* setup nr_node_ids if not done yet */
230 if (nr_node_ids == MAX_NUMNODES) {
231 for_each_node_mask(node, node_possible_map)
233 nr_node_ids = num + 1;
236 /* allocate the map */
237 map = alloc_bootmem_low(nr_node_ids * sizeof(cpumask_t));
239 pr_debug("Node to cpumask map at %p for %d nodes\n",
242 /* node_to_cpumask() will now work */
243 node_to_cpumask_map = map;
246 void __cpuinit numa_set_node(int cpu, int node)
248 int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
250 if (cpu_pda(cpu) && node != NUMA_NO_NODE)
251 cpu_pda(cpu)->nodenumber = node;
254 cpu_to_node_map[cpu] = node;
256 else if (per_cpu_offset(cpu))
257 per_cpu(x86_cpu_to_node_map, cpu) = node;
260 pr_debug("Setting node for non-present cpu %d\n", cpu);
263 void __cpuinit numa_clear_node(int cpu)
265 numa_set_node(cpu, NUMA_NO_NODE);
268 #ifndef CONFIG_DEBUG_PER_CPU_MAPS
270 void __cpuinit numa_add_cpu(int cpu)
272 cpu_set(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
275 void __cpuinit numa_remove_cpu(int cpu)
277 cpu_clear(cpu, node_to_cpumask_map[cpu_to_node(cpu)]);
280 #else /* CONFIG_DEBUG_PER_CPU_MAPS */
283 * --------- debug versions of the numa functions ---------
285 static void __cpuinit numa_set_cpumask(int cpu, int enable)
287 int node = cpu_to_node(cpu);
291 if (node_to_cpumask_map == NULL) {
292 printk(KERN_ERR "node_to_cpumask_map NULL\n");
297 mask = &node_to_cpumask_map[node];
301 cpu_clear(cpu, *mask);
303 cpulist_scnprintf(buf, sizeof(buf), mask);
304 printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
305 enable ? "numa_add_cpu" : "numa_remove_cpu", cpu, node, buf);
308 void __cpuinit numa_add_cpu(int cpu)
310 numa_set_cpumask(cpu, 1);
313 void __cpuinit numa_remove_cpu(int cpu)
315 numa_set_cpumask(cpu, 0);
318 int cpu_to_node(int cpu)
320 if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
322 "cpu_to_node(%d): usage too early!\n", cpu);
324 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
326 return per_cpu(x86_cpu_to_node_map, cpu);
328 EXPORT_SYMBOL(cpu_to_node);
331 * Same function as cpu_to_node() but used if called before the
332 * per_cpu areas are setup.
334 int early_cpu_to_node(int cpu)
336 if (early_per_cpu_ptr(x86_cpu_to_node_map))
337 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
339 if (!per_cpu_offset(cpu)) {
341 "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
345 return per_cpu(x86_cpu_to_node_map, cpu);
350 static const cpumask_t cpu_mask_none;
353 * Returns a pointer to the bitmask of CPUs on Node 'node'.
355 const cpumask_t *cpumask_of_node(int node)
357 if (node_to_cpumask_map == NULL) {
359 "cpumask_of_node(%d): no node_to_cpumask_map!\n",
362 return (const cpumask_t *)&cpu_online_map;
364 if (node >= nr_node_ids) {
366 "cpumask_of_node(%d): node > nr_node_ids(%d)\n",
369 return &cpu_mask_none;
371 return &node_to_cpumask_map[node];
373 EXPORT_SYMBOL(cpumask_of_node);
376 * Returns a bitmask of CPUs on Node 'node'.
378 * Side note: this function creates the returned cpumask on the stack
379 * so with a high NR_CPUS count, excessive stack space is used. The
380 * node_to_cpumask_ptr function should be used whenever possible.
382 cpumask_t node_to_cpumask(int node)
384 if (node_to_cpumask_map == NULL) {
386 "node_to_cpumask(%d): no node_to_cpumask_map!\n", node);
388 return cpu_online_map;
390 if (node >= nr_node_ids) {
392 "node_to_cpumask(%d): node > nr_node_ids(%d)\n",
395 return cpu_mask_none;
397 return node_to_cpumask_map[node];
399 EXPORT_SYMBOL(node_to_cpumask);
402 * --------- end of debug versions of the numa functions ---------
405 #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
407 #endif /* X86_64_NUMA */