xref: /linux/arch/x86/kernel/setup_percpu.c (revision 34019be1cd2941128b5de6d7c0fbdb51f967d268)
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>
18*34019be1SBrian Gerst #include <asm/cpu.h>
19378b39a4SYinghai Lu 
20c90aa894SMike Travis #ifdef CONFIG_DEBUG_PER_CPU_MAPS
21c90aa894SMike Travis # define DBG(x...) printk(KERN_DEBUG x)
22c90aa894SMike Travis #else
23c90aa894SMike Travis # define DBG(x...)
24c90aa894SMike Travis #endif
25c90aa894SMike Travis 
26ea927906SBrian Gerst DEFINE_PER_CPU(int, cpu_number);
27ea927906SBrian Gerst EXPORT_PER_CPU_SYMBOL(cpu_number);
28ea927906SBrian Gerst 
291688401aSBrian Gerst #ifdef CONFIG_X86_64
301688401aSBrian Gerst #define BOOT_PERCPU_OFFSET ((unsigned long)__per_cpu_load)
311688401aSBrian Gerst #else
321688401aSBrian Gerst #define BOOT_PERCPU_OFFSET 0
331688401aSBrian Gerst #endif
341688401aSBrian Gerst 
351688401aSBrian Gerst DEFINE_PER_CPU(unsigned long, this_cpu_off) = BOOT_PERCPU_OFFSET;
361688401aSBrian Gerst EXPORT_PER_CPU_SYMBOL(this_cpu_off);
371688401aSBrian Gerst 
38c90aa894SMike Travis #ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
39378b39a4SYinghai Lu 
409939ddafSTejun Heo unsigned long __per_cpu_offset[NR_CPUS] __read_mostly = {
41*34019be1SBrian Gerst 	[0 ... NR_CPUS-1] = BOOT_PERCPU_OFFSET,
429939ddafSTejun Heo };
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 		 */
91ec70de8bSBrian 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);
96ec70de8bSBrian 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 		/*
105*34019be1SBrian Gerst 		 * Up to this point, the boot CPU has been using .data.init
106*34019be1SBrian Gerst 		 * area.  Reload %gs offset for the boot CPU.
1071a51e3a0STejun Heo 		 */
108*34019be1SBrian Gerst 		if (cpu == boot_cpu_id)
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