blob: d367996693e6013e825fb77d65e5b701db44d677 [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>
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030010#include <asm/sections.h>
11#include <asm/processor.h>
12#include <asm/setup.h>
Alexey Starikovskiy0fc09062008-04-04 23:40:48 +040013#include <asm/mpspec.h>
Alexey Starikovskiy76eb4132008-04-04 23:40:41 +040014#include <asm/apicdef.h>
Bernhard Walle1ecd2762008-06-20 15:38:22 +020015#include <asm/highmem.h>
Tejun Heo1a51e3a2009-01-13 20:41:35 +090016#include <asm/proto.h>
Jaswinder Singh Rajput06879032009-01-10 12:17:37 +053017#include <asm/cpumask.h>
Alexey Starikovskiy76eb4132008-04-04 23:40:41 +040018
Mike Travisc90aa892009-01-13 20:41:34 +090019#ifdef CONFIG_DEBUG_PER_CPU_MAPS
20# define DBG(x...) printk(KERN_DEBUG x)
21#else
22# define DBG(x...)
23#endif
24
Brian Gerstea927902009-01-19 00:38:58 +090025/*
26 * Could be inside CONFIG_HAVE_SETUP_PER_CPU_AREA with other stuff but
27 * voyager wants cpu_number too.
28 */
29#ifdef CONFIG_SMP
30DEFINE_PER_CPU(int, cpu_number);
31EXPORT_PER_CPU_SYMBOL(cpu_number);
32#endif
33
Mike Travisc90aa892009-01-13 20:41:34 +090034#ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030035
Tejun Heo9939dda2009-01-13 20:41:35 +090036#ifdef CONFIG_X86_64
37unsigned long __per_cpu_offset[NR_CPUS] __read_mostly = {
38 [0] = (unsigned long)__per_cpu_load,
39};
40#else
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030041unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
Tejun Heo1a51e3a2009-01-13 20:41:35 +090042#endif
Tejun Heo9939dda2009-01-13 20:41:35 +090043EXPORT_SYMBOL(__per_cpu_offset);
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030044
45/*
46 * Great future plan:
47 * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
48 * Always point %gs to its beginning
49 */
50void __init setup_per_cpu_areas(void)
51{
Brian Gerst74631a22009-01-27 12:56:47 +090052 ssize_t size;
Mike Travis3461b0a2008-05-12 21:21:13 +020053 char *ptr;
54 int cpu;
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030055
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030056 /* Copy section for each CPU (we discard the original) */
Brian Gerst74631a22009-01-27 12:56:47 +090057 size = roundup(PERCPU_ENOUGH_ROOM, PAGE_SIZE);
Mike Travisa1681962008-12-16 17:33:53 -080058
Cyrill Gorcunovab143982009-01-02 21:51:32 +030059 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 -080060 NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids);
61
Cyrill Gorcunovab143982009-01-02 21:51:32 +030062 pr_info("PERCPU: Allocating %zd bytes of per cpu data\n", size);
Mike Travisb447a462008-03-25 15:06:51 -070063
Mike Travis3461b0a2008-05-12 21:21:13 +020064 for_each_possible_cpu(cpu) {
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030065#ifndef CONFIG_NEED_MULTIPLE_NODES
Brian Gerst74631a22009-01-27 12:56:47 +090066 ptr = alloc_bootmem_pages(size);
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030067#else
Mike Travis3461b0a2008-05-12 21:21:13 +020068 int node = early_cpu_to_node(cpu);
Mike Travisb447a462008-03-25 15:06:51 -070069 if (!node_online(node) || !NODE_DATA(node)) {
Brian Gerst74631a22009-01-27 12:56:47 +090070 ptr = alloc_bootmem_pages(size);
Cyrill Gorcunovab143982009-01-02 21:51:32 +030071 pr_info("cpu %d has no node %d or node-local memory\n",
Mike Travis3461b0a2008-05-12 21:21:13 +020072 cpu, node);
Cyrill Gorcunovab143982009-01-02 21:51:32 +030073 pr_debug("per cpu data for cpu%d at %016lx\n",
74 cpu, __pa(ptr));
75 } else {
Brian Gerst74631a22009-01-27 12:56:47 +090076 ptr = alloc_bootmem_pages_node(NODE_DATA(node), size);
Cyrill Gorcunovab143982009-01-02 21:51:32 +030077 pr_debug("per cpu data for cpu%d on node%d at %016lx\n",
78 cpu, node, __pa(ptr));
Yinghai Lua677f582008-07-29 00:37:10 -070079 }
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030080#endif
Tejun Heo1a51e3a2009-01-13 20:41:35 +090081
Tejun Heo3e5d8f92009-01-13 20:41:35 +090082 memcpy(ptr, __per_cpu_load, __per_cpu_end - __per_cpu_start);
Tejun Heo9939dda2009-01-13 20:41:35 +090083 per_cpu_offset(cpu) = ptr - __per_cpu_start;
Brian Gerst26f80bd2009-01-19 00:38:58 +090084 per_cpu(this_cpu_off, cpu) = per_cpu_offset(cpu);
Brian Gerstea927902009-01-19 00:38:58 +090085 per_cpu(cpu_number, cpu) = cpu;
Brian Gerst0d77e7f2009-01-27 12:56:47 +090086 /*
87 * Copy data used in early init routines from the initial arrays to the
88 * per cpu data areas. These arrays then become expendable and the
89 * *_early_ptr's are zeroed indicating that the static arrays are gone.
90 */
Brian Gerstec70de82009-01-27 12:56:47 +090091#ifdef CONFIG_X86_LOCAL_APIC
Brian Gerst0d77e7f2009-01-27 12:56:47 +090092 per_cpu(x86_cpu_to_apicid, cpu) =
93 early_per_cpu_map(x86_cpu_to_apicid, cpu);
94 per_cpu(x86_bios_cpu_apicid, cpu) =
95 early_per_cpu_map(x86_bios_cpu_apicid, cpu);
Brian Gerstec70de82009-01-27 12:56:47 +090096#endif
Tejun Heo1a51e3a2009-01-13 20:41:35 +090097#ifdef CONFIG_X86_64
Brian Gerst26f80bd2009-01-19 00:38:58 +090098 per_cpu(irq_stack_ptr, cpu) =
Brian Gerst947e76c2009-01-19 12:21:28 +090099 per_cpu(irq_stack_union.irq_stack, cpu) + IRQ_STACK_SIZE - 64;
Brian Gerst6470aff2009-01-27 12:56:47 +0900100#ifdef CONFIG_NUMA
101 per_cpu(x86_cpu_to_node_map, cpu) =
102 early_per_cpu_map(x86_cpu_to_node_map, cpu);
103#endif
Tejun Heo1a51e3a2009-01-13 20:41:35 +0900104 /*
Brian Gerst947e76c2009-01-19 12:21:28 +0900105 * Up to this point, CPU0 has been using .data.init
106 * area. Reload %gs offset for CPU0.
Tejun Heo1a51e3a2009-01-13 20:41:35 +0900107 */
108 if (cpu == 0)
Brian Gerst947e76c2009-01-19 12:21:28 +0900109 load_gs_base(cpu);
Tejun Heo1a51e3a2009-01-13 20:41:35 +0900110#endif
Mike Travisc90aa892009-01-13 20:41:34 +0900111
112 DBG("PERCPU: cpu %4d %p\n", cpu, ptr);
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300113 }
114
Brian Gerst0d77e7f2009-01-27 12:56:47 +0900115 /* indicate the early static arrays will soon be gone */
116 early_per_cpu_ptr(x86_cpu_to_apicid) = NULL;
117 early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL;
Brian Gerst6470aff2009-01-27 12:56:47 +0900118#if defined(CONFIG_X86_64) && defined(CONFIG_NUMA)
Brian Gerst0d77e7f2009-01-27 12:56:47 +0900119 early_per_cpu_ptr(x86_cpu_to_node_map) = NULL;
120#endif
Mike Travis9f0e8d02008-04-04 18:11:01 -0700121
Mike Travis9f248bd2008-05-12 21:21:12 +0200122 /* Setup node to cpumask map */
123 setup_node_to_cpumask_map();
Mike Travisc2d1cec2009-01-04 05:18:03 -0800124
125 /* Setup cpu initialized, callin, callout masks */
126 setup_cpu_local_masks();
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300127}
128
129#endif
Huang, Yingc45a7072008-06-02 14:26:25 +0800130