xref: /linux/arch/x86/kernel/setup_percpu.c (revision 5f5d8405d1c50f5cf7e1dbfe9c9b44e2f015c8fd)
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>
10*5f5d8405STejun Heo #include <linux/pfn.h>
11378b39a4SYinghai Lu #include <asm/sections.h>
12378b39a4SYinghai Lu #include <asm/processor.h>
13378b39a4SYinghai Lu #include <asm/setup.h>
14378b39a4SYinghai Lu #include <asm/mpspec.h>
15378b39a4SYinghai Lu #include <asm/apicdef.h>
16378b39a4SYinghai Lu #include <asm/highmem.h>
171a51e3a0STejun Heo #include <asm/proto.h>
1806879033SJaswinder Singh Rajput #include <asm/cpumask.h>
1934019be1SBrian Gerst #include <asm/cpu.h>
2060a5317fSTejun Heo #include <asm/stackprotector.h>
21378b39a4SYinghai Lu 
22c90aa894SMike Travis #ifdef CONFIG_DEBUG_PER_CPU_MAPS
23c90aa894SMike Travis # define DBG(x...) printk(KERN_DEBUG x)
24c90aa894SMike Travis #else
25c90aa894SMike Travis # define DBG(x...)
26c90aa894SMike Travis #endif
27c90aa894SMike Travis 
28ea927906SBrian Gerst DEFINE_PER_CPU(int, cpu_number);
29ea927906SBrian Gerst EXPORT_PER_CPU_SYMBOL(cpu_number);
30ea927906SBrian Gerst 
311688401aSBrian Gerst #ifdef CONFIG_X86_64
321688401aSBrian Gerst #define BOOT_PERCPU_OFFSET ((unsigned long)__per_cpu_load)
331688401aSBrian Gerst #else
341688401aSBrian Gerst #define BOOT_PERCPU_OFFSET 0
351688401aSBrian Gerst #endif
361688401aSBrian Gerst 
371688401aSBrian Gerst DEFINE_PER_CPU(unsigned long, this_cpu_off) = BOOT_PERCPU_OFFSET;
381688401aSBrian Gerst EXPORT_PER_CPU_SYMBOL(this_cpu_off);
391688401aSBrian Gerst 
409939ddafSTejun Heo unsigned long __per_cpu_offset[NR_CPUS] __read_mostly = {
4134019be1SBrian Gerst 	[0 ... NR_CPUS-1] = BOOT_PERCPU_OFFSET,
429939ddafSTejun Heo };
439939ddafSTejun Heo EXPORT_SYMBOL(__per_cpu_offset);
44378b39a4SYinghai Lu 
45*5f5d8405STejun Heo /**
46*5f5d8405STejun Heo  * pcpu_alloc_bootmem - NUMA friendly alloc_bootmem wrapper for percpu
47*5f5d8405STejun Heo  * @cpu: cpu to allocate for
48*5f5d8405STejun Heo  * @size: size allocation in bytes
49*5f5d8405STejun Heo  * @align: alignment
50*5f5d8405STejun Heo  *
51*5f5d8405STejun Heo  * Allocate @size bytes aligned at @align for cpu @cpu.  This wrapper
52*5f5d8405STejun Heo  * does the right thing for NUMA regardless of the current
53*5f5d8405STejun Heo  * configuration.
54*5f5d8405STejun Heo  *
55*5f5d8405STejun Heo  * RETURNS:
56*5f5d8405STejun Heo  * Pointer to the allocated area on success, NULL on failure.
57*5f5d8405STejun Heo  */
58*5f5d8405STejun Heo static void * __init pcpu_alloc_bootmem(unsigned int cpu, unsigned long size,
59*5f5d8405STejun Heo 					unsigned long align)
60*5f5d8405STejun Heo {
61*5f5d8405STejun Heo 	const unsigned long goal = __pa(MAX_DMA_ADDRESS);
62*5f5d8405STejun Heo #ifdef CONFIG_NEED_MULTIPLE_NODES
63*5f5d8405STejun Heo 	int node = early_cpu_to_node(cpu);
64*5f5d8405STejun Heo 	void *ptr;
65*5f5d8405STejun Heo 
66*5f5d8405STejun Heo 	if (!node_online(node) || !NODE_DATA(node)) {
67*5f5d8405STejun Heo 		ptr = __alloc_bootmem_nopanic(size, align, goal);
68*5f5d8405STejun Heo 		pr_info("cpu %d has no node %d or node-local memory\n",
69*5f5d8405STejun Heo 			cpu, node);
70*5f5d8405STejun Heo 		pr_debug("per cpu data for cpu%d %lu bytes at %016lx\n",
71*5f5d8405STejun Heo 			 cpu, size, __pa(ptr));
72*5f5d8405STejun Heo 	} else {
73*5f5d8405STejun Heo 		ptr = __alloc_bootmem_node_nopanic(NODE_DATA(node),
74*5f5d8405STejun Heo 						   size, align, goal);
75*5f5d8405STejun Heo 		pr_debug("per cpu data for cpu%d %lu bytes on node%d at "
76*5f5d8405STejun Heo 			 "%016lx\n", cpu, size, node, __pa(ptr));
77*5f5d8405STejun Heo 	}
78*5f5d8405STejun Heo 	return ptr;
79*5f5d8405STejun Heo #else
80*5f5d8405STejun Heo 	return __alloc_bootmem_nopanic(size, align, goal);
81*5f5d8405STejun Heo #endif
82*5f5d8405STejun Heo }
83*5f5d8405STejun Heo 
84*5f5d8405STejun Heo /*
85*5f5d8405STejun Heo  * 4k page allocator
86*5f5d8405STejun Heo  *
87*5f5d8405STejun Heo  * This is the basic allocator.  Static percpu area is allocated
88*5f5d8405STejun Heo  * page-by-page and most of initialization is done by the generic
89*5f5d8405STejun Heo  * setup function.
90*5f5d8405STejun Heo  */
918d408b4bSTejun Heo static struct page **pcpu4k_pages __initdata;
928d408b4bSTejun Heo static int pcpu4k_nr_static_pages __initdata;
938d408b4bSTejun Heo 
948d408b4bSTejun Heo static struct page * __init pcpu4k_get_page(unsigned int cpu, int pageno)
958d408b4bSTejun Heo {
968d408b4bSTejun Heo 	if (pageno < pcpu4k_nr_static_pages)
978d408b4bSTejun Heo 		return pcpu4k_pages[cpu * pcpu4k_nr_static_pages + pageno];
988d408b4bSTejun Heo 	return NULL;
998d408b4bSTejun Heo }
1008d408b4bSTejun Heo 
101458a3e64STejun Heo static void __init pcpu4k_populate_pte(unsigned long addr)
102458a3e64STejun Heo {
103458a3e64STejun Heo 	populate_extra_pte(addr);
104458a3e64STejun Heo }
105458a3e64STejun Heo 
106*5f5d8405STejun Heo static ssize_t __init setup_pcpu_4k(size_t static_size)
107*5f5d8405STejun Heo {
108*5f5d8405STejun Heo 	size_t pages_size;
109*5f5d8405STejun Heo 	unsigned int cpu;
110*5f5d8405STejun Heo 	int i, j;
111*5f5d8405STejun Heo 	ssize_t ret;
112*5f5d8405STejun Heo 
113*5f5d8405STejun Heo 	pcpu4k_nr_static_pages = PFN_UP(static_size);
114*5f5d8405STejun Heo 
115*5f5d8405STejun Heo 	/* unaligned allocations can't be freed, round up to page size */
116*5f5d8405STejun Heo 	pages_size = PFN_ALIGN(pcpu4k_nr_static_pages * num_possible_cpus()
117*5f5d8405STejun Heo 			       * sizeof(pcpu4k_pages[0]));
118*5f5d8405STejun Heo 	pcpu4k_pages = alloc_bootmem(pages_size);
119*5f5d8405STejun Heo 
120*5f5d8405STejun Heo 	/* allocate and copy */
121*5f5d8405STejun Heo 	j = 0;
122*5f5d8405STejun Heo 	for_each_possible_cpu(cpu)
123*5f5d8405STejun Heo 		for (i = 0; i < pcpu4k_nr_static_pages; i++) {
124*5f5d8405STejun Heo 			void *ptr;
125*5f5d8405STejun Heo 
126*5f5d8405STejun Heo 			ptr = pcpu_alloc_bootmem(cpu, PAGE_SIZE, PAGE_SIZE);
127*5f5d8405STejun Heo 			if (!ptr)
128*5f5d8405STejun Heo 				goto enomem;
129*5f5d8405STejun Heo 
130*5f5d8405STejun Heo 			memcpy(ptr, __per_cpu_load + i * PAGE_SIZE, PAGE_SIZE);
131*5f5d8405STejun Heo 			pcpu4k_pages[j++] = virt_to_page(ptr);
132*5f5d8405STejun Heo 		}
133*5f5d8405STejun Heo 
134*5f5d8405STejun Heo 	/* we're ready, commit */
135*5f5d8405STejun Heo 	pr_info("PERCPU: Allocated %d 4k pages, static data %zu bytes\n",
136*5f5d8405STejun Heo 		pcpu4k_nr_static_pages, static_size);
137*5f5d8405STejun Heo 
138*5f5d8405STejun Heo 	ret = pcpu_setup_first_chunk(pcpu4k_get_page, static_size, 0, 0, NULL,
139*5f5d8405STejun Heo 				     pcpu4k_populate_pte);
140*5f5d8405STejun Heo 	goto out_free_ar;
141*5f5d8405STejun Heo 
142*5f5d8405STejun Heo enomem:
143*5f5d8405STejun Heo 	while (--j >= 0)
144*5f5d8405STejun Heo 		free_bootmem(__pa(page_address(pcpu4k_pages[j])), PAGE_SIZE);
145*5f5d8405STejun Heo 	ret = -ENOMEM;
146*5f5d8405STejun Heo out_free_ar:
147*5f5d8405STejun Heo 	free_bootmem(__pa(pcpu4k_pages), pages_size);
148*5f5d8405STejun Heo 	return ret;
149*5f5d8405STejun Heo }
150*5f5d8405STejun Heo 
151b2d2f431SBrian Gerst static inline void setup_percpu_segment(int cpu)
152b2d2f431SBrian Gerst {
153b2d2f431SBrian Gerst #ifdef CONFIG_X86_32
154b2d2f431SBrian Gerst 	struct desc_struct gdt;
155b2d2f431SBrian Gerst 
156b2d2f431SBrian Gerst 	pack_descriptor(&gdt, per_cpu_offset(cpu), 0xFFFFF,
157b2d2f431SBrian Gerst 			0x2 | DESCTYPE_S, 0x8);
158b2d2f431SBrian Gerst 	gdt.s = 1;
159b2d2f431SBrian Gerst 	write_gdt_entry(get_cpu_gdt_table(cpu),
160b2d2f431SBrian Gerst 			GDT_ENTRY_PERCPU, &gdt, DESCTYPE_S);
161b2d2f431SBrian Gerst #endif
162b2d2f431SBrian Gerst }
163b2d2f431SBrian Gerst 
164378b39a4SYinghai Lu /*
165378b39a4SYinghai Lu  * Great future plan:
166378b39a4SYinghai Lu  * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
167378b39a4SYinghai Lu  * Always point %gs to its beginning
168378b39a4SYinghai Lu  */
169378b39a4SYinghai Lu void __init setup_per_cpu_areas(void)
170378b39a4SYinghai Lu {
171*5f5d8405STejun Heo 	size_t static_size = __per_cpu_end - __per_cpu_start;
172*5f5d8405STejun Heo 	unsigned int cpu;
17311124411STejun Heo 	unsigned long delta;
17411124411STejun Heo 	size_t pcpu_unit_size;
175*5f5d8405STejun Heo 	ssize_t ret;
176a1681965SMike Travis 
177ab14398aSCyrill Gorcunov 	pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%d nr_node_ids:%d\n",
178a1681965SMike Travis 		NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids);
179a1681965SMike Travis 
180*5f5d8405STejun Heo 	/* allocate percpu area */
181*5f5d8405STejun Heo 	ret = setup_pcpu_4k(static_size);
182*5f5d8405STejun Heo 	if (ret < 0)
183*5f5d8405STejun Heo 		panic("cannot allocate static percpu area (%zu bytes, err=%zd)",
184*5f5d8405STejun Heo 		      static_size, ret);
185378b39a4SYinghai Lu 
186*5f5d8405STejun Heo 	pcpu_unit_size = ret;
18711124411STejun Heo 
188*5f5d8405STejun Heo 	/* alrighty, percpu areas up and running */
18911124411STejun Heo 	delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
19011124411STejun Heo 	for_each_possible_cpu(cpu) {
19111124411STejun Heo 		per_cpu_offset(cpu) = delta + cpu * pcpu_unit_size;
19226f80bd6SBrian Gerst 		per_cpu(this_cpu_off, cpu) = per_cpu_offset(cpu);
193ea927906SBrian Gerst 		per_cpu(cpu_number, cpu) = cpu;
194b2d2f431SBrian Gerst 		setup_percpu_segment(cpu);
19560a5317fSTejun Heo 		setup_stack_canary_segment(cpu);
1960d77e7f0SBrian Gerst 		/*
197cf3997f5STejun Heo 		 * Copy data used in early init routines from the
198cf3997f5STejun Heo 		 * initial arrays to the per cpu data areas.  These
199cf3997f5STejun Heo 		 * arrays then become expendable and the *_early_ptr's
200cf3997f5STejun Heo 		 * are zeroed indicating that the static arrays are
201cf3997f5STejun Heo 		 * gone.
2020d77e7f0SBrian Gerst 		 */
203ec70de8bSBrian Gerst #ifdef CONFIG_X86_LOCAL_APIC
2040d77e7f0SBrian Gerst 		per_cpu(x86_cpu_to_apicid, cpu) =
2050d77e7f0SBrian Gerst 			early_per_cpu_map(x86_cpu_to_apicid, cpu);
2060d77e7f0SBrian Gerst 		per_cpu(x86_bios_cpu_apicid, cpu) =
2070d77e7f0SBrian Gerst 			early_per_cpu_map(x86_bios_cpu_apicid, cpu);
208ec70de8bSBrian Gerst #endif
2091a51e3a0STejun Heo #ifdef CONFIG_X86_64
21026f80bd6SBrian Gerst 		per_cpu(irq_stack_ptr, cpu) =
211cf3997f5STejun Heo 			per_cpu(irq_stack_union.irq_stack, cpu) +
212cf3997f5STejun Heo 			IRQ_STACK_SIZE - 64;
2136470aff6SBrian Gerst #ifdef CONFIG_NUMA
2146470aff6SBrian Gerst 		per_cpu(x86_cpu_to_node_map, cpu) =
2156470aff6SBrian Gerst 			early_per_cpu_map(x86_cpu_to_node_map, cpu);
2166470aff6SBrian Gerst #endif
2172697fbd5SBrian Gerst #endif
2181a51e3a0STejun Heo 		/*
21934019be1SBrian Gerst 		 * Up to this point, the boot CPU has been using .data.init
2202697fbd5SBrian Gerst 		 * area.  Reload any changed state for the boot CPU.
2211a51e3a0STejun Heo 		 */
22234019be1SBrian Gerst 		if (cpu == boot_cpu_id)
223552be871SBrian Gerst 			switch_to_new_gdt(cpu);
224c90aa894SMike Travis 
225c90aa894SMike Travis 		DBG("PERCPU: cpu %4d %p\n", cpu, ptr);
226378b39a4SYinghai Lu 	}
227378b39a4SYinghai Lu 
2280d77e7f0SBrian Gerst 	/* indicate the early static arrays will soon be gone */
22922f25138SJames Bottomley #ifdef CONFIG_X86_LOCAL_APIC
2300d77e7f0SBrian Gerst 	early_per_cpu_ptr(x86_cpu_to_apicid) = NULL;
2310d77e7f0SBrian Gerst 	early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL;
23222f25138SJames Bottomley #endif
2336470aff6SBrian Gerst #if defined(CONFIG_X86_64) && defined(CONFIG_NUMA)
2340d77e7f0SBrian Gerst 	early_per_cpu_ptr(x86_cpu_to_node_map) = NULL;
2350d77e7f0SBrian Gerst #endif
236378b39a4SYinghai Lu 
237378b39a4SYinghai Lu 	/* Setup node to cpumask map */
238378b39a4SYinghai Lu 	setup_node_to_cpumask_map();
239c2d1cec1SMike Travis 
240c2d1cec1SMike Travis 	/* Setup cpu initialized, callin, callout masks */
241c2d1cec1SMike Travis 	setup_cpu_local_masks();
242378b39a4SYinghai Lu }
243