blob: 4a17c96f4f6cfdfaf4d35a817dec6f272b207deb [file] [log] [blame]
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -03001#include <linux/kernel.h>
2#include <linux/module.h>
3#include <linux/init.h>
4#include <linux/bootmem.h>
5#include <linux/percpu.h>
Bernhard Walle1ecd2762008-06-20 15:38:22 +02006#include <linux/kexec.h>
Yinghai Lu17b4cce2008-06-21 21:02:20 -07007#include <linux/crash_dump.h>
Jaswinder Singh Rajput8a87dd92009-01-04 17:04:26 +05308#include <linux/smp.h>
9#include <linux/topology.h>
Tejun Heo5f5d8402009-02-24 11:57:21 +090010#include <linux/pfn.h>
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030011#include <asm/sections.h>
12#include <asm/processor.h>
13#include <asm/setup.h>
Alexey Starikovskiy0fc09062008-04-04 23:40:48 +040014#include <asm/mpspec.h>
Alexey Starikovskiy76eb4132008-04-04 23:40:41 +040015#include <asm/apicdef.h>
Bernhard Walle1ecd2762008-06-20 15:38:22 +020016#include <asm/highmem.h>
Tejun Heo1a51e3a2009-01-13 20:41:35 +090017#include <asm/proto.h>
Jaswinder Singh Rajput06879032009-01-10 12:17:37 +053018#include <asm/cpumask.h>
Brian Gerst34019be2009-01-27 12:56:48 +090019#include <asm/cpu.h>
Tejun Heo60a53172009-02-09 22:17:40 +090020#include <asm/stackprotector.h>
Alexey Starikovskiy76eb4132008-04-04 23:40:41 +040021
Mike Travisc90aa892009-01-13 20:41:34 +090022#ifdef CONFIG_DEBUG_PER_CPU_MAPS
23# define DBG(x...) printk(KERN_DEBUG x)
24#else
25# define DBG(x...)
26#endif
27
Brian Gerstea927902009-01-19 00:38:58 +090028DEFINE_PER_CPU(int, cpu_number);
29EXPORT_PER_CPU_SYMBOL(cpu_number);
Brian Gerstea927902009-01-19 00:38:58 +090030
Brian Gerst16884012009-01-27 12:56:48 +090031#ifdef CONFIG_X86_64
32#define BOOT_PERCPU_OFFSET ((unsigned long)__per_cpu_load)
33#else
34#define BOOT_PERCPU_OFFSET 0
35#endif
36
37DEFINE_PER_CPU(unsigned long, this_cpu_off) = BOOT_PERCPU_OFFSET;
38EXPORT_PER_CPU_SYMBOL(this_cpu_off);
39
Tejun Heo9939dda2009-01-13 20:41:35 +090040unsigned long __per_cpu_offset[NR_CPUS] __read_mostly = {
Brian Gerst34019be2009-01-27 12:56:48 +090041 [0 ... NR_CPUS-1] = BOOT_PERCPU_OFFSET,
Tejun Heo9939dda2009-01-13 20:41:35 +090042};
Tejun Heo9939dda2009-01-13 20:41:35 +090043EXPORT_SYMBOL(__per_cpu_offset);
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030044
Tejun Heo5f5d8402009-02-24 11:57:21 +090045/**
46 * pcpu_alloc_bootmem - NUMA friendly alloc_bootmem wrapper for percpu
47 * @cpu: cpu to allocate for
48 * @size: size allocation in bytes
49 * @align: alignment
50 *
51 * Allocate @size bytes aligned at @align for cpu @cpu. This wrapper
52 * does the right thing for NUMA regardless of the current
53 * configuration.
54 *
55 * RETURNS:
56 * Pointer to the allocated area on success, NULL on failure.
57 */
58static void * __init pcpu_alloc_bootmem(unsigned int cpu, unsigned long size,
59 unsigned long align)
60{
61 const unsigned long goal = __pa(MAX_DMA_ADDRESS);
62#ifdef CONFIG_NEED_MULTIPLE_NODES
63 int node = early_cpu_to_node(cpu);
64 void *ptr;
65
66 if (!node_online(node) || !NODE_DATA(node)) {
67 ptr = __alloc_bootmem_nopanic(size, align, goal);
68 pr_info("cpu %d has no node %d or node-local memory\n",
69 cpu, node);
70 pr_debug("per cpu data for cpu%d %lu bytes at %016lx\n",
71 cpu, size, __pa(ptr));
72 } else {
73 ptr = __alloc_bootmem_node_nopanic(NODE_DATA(node),
74 size, align, goal);
75 pr_debug("per cpu data for cpu%d %lu bytes on node%d at "
76 "%016lx\n", cpu, size, node, __pa(ptr));
77 }
78 return ptr;
79#else
80 return __alloc_bootmem_nopanic(size, align, goal);
81#endif
82}
83
84/*
85 * 4k page allocator
86 *
87 * This is the basic allocator. Static percpu area is allocated
88 * page-by-page and most of initialization is done by the generic
89 * setup function.
90 */
Tejun Heo8d408b42009-02-24 11:57:21 +090091static struct page **pcpu4k_pages __initdata;
92static int pcpu4k_nr_static_pages __initdata;
93
94static struct page * __init pcpu4k_get_page(unsigned int cpu, int pageno)
95{
96 if (pageno < pcpu4k_nr_static_pages)
97 return pcpu4k_pages[cpu * pcpu4k_nr_static_pages + pageno];
98 return NULL;
99}
100
Tejun Heo458a3e62009-02-24 11:57:21 +0900101static void __init pcpu4k_populate_pte(unsigned long addr)
102{
103 populate_extra_pte(addr);
104}
105
Tejun Heo5f5d8402009-02-24 11:57:21 +0900106static ssize_t __init setup_pcpu_4k(size_t static_size)
107{
108 size_t pages_size;
109 unsigned int cpu;
110 int i, j;
111 ssize_t ret;
112
113 pcpu4k_nr_static_pages = PFN_UP(static_size);
114
115 /* unaligned allocations can't be freed, round up to page size */
116 pages_size = PFN_ALIGN(pcpu4k_nr_static_pages * num_possible_cpus()
117 * sizeof(pcpu4k_pages[0]));
118 pcpu4k_pages = alloc_bootmem(pages_size);
119
120 /* allocate and copy */
121 j = 0;
122 for_each_possible_cpu(cpu)
123 for (i = 0; i < pcpu4k_nr_static_pages; i++) {
124 void *ptr;
125
126 ptr = pcpu_alloc_bootmem(cpu, PAGE_SIZE, PAGE_SIZE);
127 if (!ptr)
128 goto enomem;
129
130 memcpy(ptr, __per_cpu_load + i * PAGE_SIZE, PAGE_SIZE);
131 pcpu4k_pages[j++] = virt_to_page(ptr);
132 }
133
134 /* we're ready, commit */
135 pr_info("PERCPU: Allocated %d 4k pages, static data %zu bytes\n",
136 pcpu4k_nr_static_pages, static_size);
137
138 ret = pcpu_setup_first_chunk(pcpu4k_get_page, static_size, 0, 0, NULL,
139 pcpu4k_populate_pte);
140 goto out_free_ar;
141
142enomem:
143 while (--j >= 0)
144 free_bootmem(__pa(page_address(pcpu4k_pages[j])), PAGE_SIZE);
145 ret = -ENOMEM;
146out_free_ar:
147 free_bootmem(__pa(pcpu4k_pages), pages_size);
148 return ret;
149}
150
Brian Gerstb2d2f432009-01-27 12:56:48 +0900151static inline void setup_percpu_segment(int cpu)
152{
153#ifdef CONFIG_X86_32
154 struct desc_struct gdt;
155
156 pack_descriptor(&gdt, per_cpu_offset(cpu), 0xFFFFF,
157 0x2 | DESCTYPE_S, 0x8);
158 gdt.s = 1;
159 write_gdt_entry(get_cpu_gdt_table(cpu),
160 GDT_ENTRY_PERCPU, &gdt, DESCTYPE_S);
161#endif
162}
163
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300164/*
165 * Great future plan:
166 * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
167 * Always point %gs to its beginning
168 */
169void __init setup_per_cpu_areas(void)
170{
Tejun Heo5f5d8402009-02-24 11:57:21 +0900171 size_t static_size = __per_cpu_end - __per_cpu_start;
172 unsigned int cpu;
Tejun Heo11124412009-02-20 16:29:09 +0900173 unsigned long delta;
174 size_t pcpu_unit_size;
Tejun Heo5f5d8402009-02-24 11:57:21 +0900175 ssize_t ret;
Mike Travisa1681962008-12-16 17:33:53 -0800176
Cyrill Gorcunovab143982009-01-02 21:51:32 +0300177 pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%d nr_node_ids:%d\n",
Mike Travisa1681962008-12-16 17:33:53 -0800178 NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids);
179
Tejun Heo5f5d8402009-02-24 11:57:21 +0900180 /* allocate percpu area */
181 ret = setup_pcpu_4k(static_size);
182 if (ret < 0)
183 panic("cannot allocate static percpu area (%zu bytes, err=%zd)",
184 static_size, ret);
Mike Travisb447a462008-03-25 15:06:51 -0700185
Tejun Heo5f5d8402009-02-24 11:57:21 +0900186 pcpu_unit_size = ret;
Tejun Heo1a51e3a2009-01-13 20:41:35 +0900187
Tejun Heo5f5d8402009-02-24 11:57:21 +0900188 /* alrighty, percpu areas up and running */
Tejun Heo11124412009-02-20 16:29:09 +0900189 delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
190 for_each_possible_cpu(cpu) {
191 per_cpu_offset(cpu) = delta + cpu * pcpu_unit_size;
Brian Gerst26f80bd2009-01-19 00:38:58 +0900192 per_cpu(this_cpu_off, cpu) = per_cpu_offset(cpu);
Brian Gerstea927902009-01-19 00:38:58 +0900193 per_cpu(cpu_number, cpu) = cpu;
Brian Gerstb2d2f432009-01-27 12:56:48 +0900194 setup_percpu_segment(cpu);
Tejun Heo60a53172009-02-09 22:17:40 +0900195 setup_stack_canary_segment(cpu);
Brian Gerst0d77e7f2009-01-27 12:56:47 +0900196 /*
Tejun Heocf3997f2009-01-27 14:25:05 +0900197 * Copy data used in early init routines from the
198 * initial arrays to the per cpu data areas. These
199 * arrays then become expendable and the *_early_ptr's
200 * are zeroed indicating that the static arrays are
201 * gone.
Brian Gerst0d77e7f2009-01-27 12:56:47 +0900202 */
Brian Gerstec70de82009-01-27 12:56:47 +0900203#ifdef CONFIG_X86_LOCAL_APIC
Brian Gerst0d77e7f2009-01-27 12:56:47 +0900204 per_cpu(x86_cpu_to_apicid, cpu) =
Tejun Heocf3997f2009-01-27 14:25:05 +0900205 early_per_cpu_map(x86_cpu_to_apicid, cpu);
Brian Gerst0d77e7f2009-01-27 12:56:47 +0900206 per_cpu(x86_bios_cpu_apicid, cpu) =
Tejun Heocf3997f2009-01-27 14:25:05 +0900207 early_per_cpu_map(x86_bios_cpu_apicid, cpu);
Brian Gerstec70de82009-01-27 12:56:47 +0900208#endif
Tejun Heo1a51e3a2009-01-13 20:41:35 +0900209#ifdef CONFIG_X86_64
Brian Gerst26f80bd2009-01-19 00:38:58 +0900210 per_cpu(irq_stack_ptr, cpu) =
Tejun Heocf3997f2009-01-27 14:25:05 +0900211 per_cpu(irq_stack_union.irq_stack, cpu) +
212 IRQ_STACK_SIZE - 64;
Brian Gerst6470aff2009-01-27 12:56:47 +0900213#ifdef CONFIG_NUMA
214 per_cpu(x86_cpu_to_node_map, cpu) =
Tejun Heocf3997f2009-01-27 14:25:05 +0900215 early_per_cpu_map(x86_cpu_to_node_map, cpu);
Brian Gerst6470aff2009-01-27 12:56:47 +0900216#endif
Brian Gerst2697fbd2009-01-27 12:56:48 +0900217#endif
Tejun Heo1a51e3a2009-01-13 20:41:35 +0900218 /*
Brian Gerst34019be2009-01-27 12:56:48 +0900219 * Up to this point, the boot CPU has been using .data.init
Brian Gerst2697fbd2009-01-27 12:56:48 +0900220 * area. Reload any changed state for the boot CPU.
Tejun Heo1a51e3a2009-01-13 20:41:35 +0900221 */
Brian Gerst34019be2009-01-27 12:56:48 +0900222 if (cpu == boot_cpu_id)
Brian Gerst552be872009-01-30 17:47:53 +0900223 switch_to_new_gdt(cpu);
Mike Travisc90aa892009-01-13 20:41:34 +0900224
225 DBG("PERCPU: cpu %4d %p\n", cpu, ptr);
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300226 }
227
Brian Gerst0d77e7f2009-01-27 12:56:47 +0900228 /* indicate the early static arrays will soon be gone */
James Bottomley22f25132009-01-27 14:21:37 +0900229#ifdef CONFIG_X86_LOCAL_APIC
Brian Gerst0d77e7f2009-01-27 12:56:47 +0900230 early_per_cpu_ptr(x86_cpu_to_apicid) = NULL;
231 early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL;
James Bottomley22f25132009-01-27 14:21:37 +0900232#endif
Brian Gerst6470aff2009-01-27 12:56:47 +0900233#if defined(CONFIG_X86_64) && defined(CONFIG_NUMA)
Brian Gerst0d77e7f2009-01-27 12:56:47 +0900234 early_per_cpu_ptr(x86_cpu_to_node_map) = NULL;
235#endif
Mike Travis9f0e8d02008-04-04 18:11:01 -0700236
Mike Travis9f248bd2008-05-12 21:21:12 +0200237 /* Setup node to cpumask map */
238 setup_node_to_cpumask_map();
Mike Travisc2d1cec2009-01-04 05:18:03 -0800239
240 /* Setup cpu initialized, callin, callout masks */
241 setup_cpu_local_masks();
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300242}