xref: /linux/arch/x86/kernel/setup_percpu.c (revision ec70de8b04bf37213982a5e8f303bc38679f3f8e)
1378b39a4SYinghai Lu #include <linux/kernel.h>
2378b39a4SYinghai Lu #include <linux/module.h>
3378b39a4SYinghai Lu #include <linux/init.h>
4378b39a4SYinghai Lu #include <linux/bootmem.h>
5378b39a4SYinghai Lu #include <linux/percpu.h>
6378b39a4SYinghai Lu #include <linux/kexec.h>
7378b39a4SYinghai Lu #include <linux/crash_dump.h>
88a87dd9aSJaswinder Singh Rajput #include <linux/smp.h>
98a87dd9aSJaswinder Singh Rajput #include <linux/topology.h>
10378b39a4SYinghai Lu #include <asm/sections.h>
11378b39a4SYinghai Lu #include <asm/processor.h>
12378b39a4SYinghai Lu #include <asm/setup.h>
13378b39a4SYinghai Lu #include <asm/mpspec.h>
14378b39a4SYinghai Lu #include <asm/apicdef.h>
15378b39a4SYinghai Lu #include <asm/highmem.h>
161a51e3a0STejun Heo #include <asm/proto.h>
1706879033SJaswinder Singh Rajput #include <asm/cpumask.h>
18378b39a4SYinghai Lu 
19c90aa894SMike Travis #ifdef CONFIG_DEBUG_PER_CPU_MAPS
20c90aa894SMike Travis # define DBG(x...) printk(KERN_DEBUG x)
21c90aa894SMike Travis #else
22c90aa894SMike Travis # define DBG(x...)
23c90aa894SMike Travis #endif
24c90aa894SMike Travis 
25ea927906SBrian Gerst /*
26ea927906SBrian Gerst  * Could be inside CONFIG_HAVE_SETUP_PER_CPU_AREA with other stuff but
27ea927906SBrian Gerst  * voyager wants cpu_number too.
28ea927906SBrian Gerst  */
29ea927906SBrian Gerst #ifdef CONFIG_SMP
30ea927906SBrian Gerst DEFINE_PER_CPU(int, cpu_number);
31ea927906SBrian Gerst EXPORT_PER_CPU_SYMBOL(cpu_number);
32ea927906SBrian Gerst #endif
33ea927906SBrian Gerst 
34c90aa894SMike Travis #ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
35378b39a4SYinghai Lu 
369939ddafSTejun Heo #ifdef CONFIG_X86_64
379939ddafSTejun Heo unsigned long __per_cpu_offset[NR_CPUS] __read_mostly = {
389939ddafSTejun Heo 	[0] = (unsigned long)__per_cpu_load,
399939ddafSTejun Heo };
409939ddafSTejun Heo #else
41378b39a4SYinghai Lu unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
421a51e3a0STejun Heo #endif
439939ddafSTejun Heo EXPORT_SYMBOL(__per_cpu_offset);
44378b39a4SYinghai Lu 
45378b39a4SYinghai Lu /*
46378b39a4SYinghai Lu  * Great future plan:
47378b39a4SYinghai Lu  * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
48378b39a4SYinghai Lu  * Always point %gs to its beginning
49378b39a4SYinghai Lu  */
50378b39a4SYinghai Lu void __init setup_per_cpu_areas(void)
51378b39a4SYinghai Lu {
5274631a24SBrian Gerst 	ssize_t size;
53378b39a4SYinghai Lu 	char *ptr;
54378b39a4SYinghai Lu 	int cpu;
55378b39a4SYinghai Lu 
56378b39a4SYinghai Lu 	/* Copy section for each CPU (we discard the original) */
5774631a24SBrian Gerst 	size = roundup(PERCPU_ENOUGH_ROOM, PAGE_SIZE);
58a1681965SMike Travis 
59ab14398aSCyrill Gorcunov 	pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%d nr_node_ids:%d\n",
60a1681965SMike Travis 		NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids);
61a1681965SMike Travis 
62ab14398aSCyrill Gorcunov 	pr_info("PERCPU: Allocating %zd bytes of per cpu data\n", size);
63378b39a4SYinghai Lu 
64378b39a4SYinghai Lu 	for_each_possible_cpu(cpu) {
65378b39a4SYinghai Lu #ifndef CONFIG_NEED_MULTIPLE_NODES
6674631a24SBrian Gerst 		ptr = alloc_bootmem_pages(size);
67378b39a4SYinghai Lu #else
68378b39a4SYinghai Lu 		int node = early_cpu_to_node(cpu);
69378b39a4SYinghai Lu 		if (!node_online(node) || !NODE_DATA(node)) {
7074631a24SBrian Gerst 			ptr = alloc_bootmem_pages(size);
71ab14398aSCyrill Gorcunov 			pr_info("cpu %d has no node %d or node-local memory\n",
72378b39a4SYinghai Lu 				cpu, node);
73ab14398aSCyrill Gorcunov 			pr_debug("per cpu data for cpu%d at %016lx\n",
74a677f58aSYinghai Lu 				 cpu, __pa(ptr));
75ab14398aSCyrill Gorcunov 		} else {
7674631a24SBrian Gerst 			ptr = alloc_bootmem_pages_node(NODE_DATA(node), size);
77ab14398aSCyrill Gorcunov 			pr_debug("per cpu data for cpu%d on node%d at %016lx\n",
78a677f58aSYinghai Lu 				cpu, node, __pa(ptr));
79a677f58aSYinghai Lu 		}
80378b39a4SYinghai Lu #endif
811a51e3a0STejun Heo 
823e5d8f97STejun Heo 		memcpy(ptr, __per_cpu_load, __per_cpu_end - __per_cpu_start);
839939ddafSTejun Heo 		per_cpu_offset(cpu) = ptr - __per_cpu_start;
8426f80bd6SBrian Gerst 		per_cpu(this_cpu_off, cpu) = per_cpu_offset(cpu);
85ea927906SBrian Gerst 		per_cpu(cpu_number, cpu) = cpu;
860d77e7f0SBrian Gerst 		/*
870d77e7f0SBrian Gerst 		 * Copy data used in early init routines from the initial arrays to the
880d77e7f0SBrian Gerst 		 * per cpu data areas.  These arrays then become expendable and the
890d77e7f0SBrian Gerst 		 * *_early_ptr's are zeroed indicating that the static arrays are gone.
900d77e7f0SBrian Gerst 		 */
91*ec70de8bSBrian Gerst #ifdef CONFIG_X86_LOCAL_APIC
920d77e7f0SBrian Gerst 		per_cpu(x86_cpu_to_apicid, cpu) =
930d77e7f0SBrian Gerst 				early_per_cpu_map(x86_cpu_to_apicid, cpu);
940d77e7f0SBrian Gerst 		per_cpu(x86_bios_cpu_apicid, cpu) =
950d77e7f0SBrian Gerst 				early_per_cpu_map(x86_bios_cpu_apicid, cpu);
96*ec70de8bSBrian Gerst #endif
971a51e3a0STejun Heo #ifdef CONFIG_X86_64
9826f80bd6SBrian Gerst 		per_cpu(irq_stack_ptr, cpu) =
99947e76cdSBrian Gerst 			per_cpu(irq_stack_union.irq_stack, cpu) + IRQ_STACK_SIZE - 64;
1006470aff6SBrian Gerst #ifdef CONFIG_NUMA
1016470aff6SBrian Gerst 		per_cpu(x86_cpu_to_node_map, cpu) =
1026470aff6SBrian Gerst 				early_per_cpu_map(x86_cpu_to_node_map, cpu);
1036470aff6SBrian Gerst #endif
1041a51e3a0STejun Heo 		/*
105947e76cdSBrian Gerst 		 * Up to this point, CPU0 has been using .data.init
106947e76cdSBrian Gerst 		 * area.  Reload %gs offset for CPU0.
1071a51e3a0STejun Heo 		 */
1081a51e3a0STejun Heo 		if (cpu == 0)
109947e76cdSBrian Gerst 			load_gs_base(cpu);
1101a51e3a0STejun Heo #endif
111c90aa894SMike Travis 
112c90aa894SMike Travis 		DBG("PERCPU: cpu %4d %p\n", cpu, ptr);
113378b39a4SYinghai Lu 	}
114378b39a4SYinghai Lu 
1150d77e7f0SBrian Gerst 	/* indicate the early static arrays will soon be gone */
1160d77e7f0SBrian Gerst 	early_per_cpu_ptr(x86_cpu_to_apicid) = NULL;
1170d77e7f0SBrian Gerst 	early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL;
1186470aff6SBrian Gerst #if defined(CONFIG_X86_64) && defined(CONFIG_NUMA)
1190d77e7f0SBrian Gerst 	early_per_cpu_ptr(x86_cpu_to_node_map) = NULL;
1200d77e7f0SBrian Gerst #endif
121378b39a4SYinghai Lu 
122378b39a4SYinghai Lu 	/* Setup node to cpumask map */
123378b39a4SYinghai Lu 	setup_node_to_cpumask_map();
124c2d1cec1SMike Travis 
125c2d1cec1SMike Travis 	/* Setup cpu initialized, callin, callout masks */
126c2d1cec1SMike Travis 	setup_cpu_local_masks();
127378b39a4SYinghai Lu }
128378b39a4SYinghai Lu 
129378b39a4SYinghai Lu #endif
130378b39a4SYinghai Lu 
131