blob: 36c2e81dfc3c5c0a435dbc8b77fec1449977ab57 [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 +090025DEFINE_PER_CPU(int, cpu_number);
26EXPORT_PER_CPU_SYMBOL(cpu_number);
Brian Gerstea927902009-01-19 00:38:58 +090027
Brian Gerst16884012009-01-27 12:56:48 +090028#ifdef CONFIG_X86_64
29#define BOOT_PERCPU_OFFSET ((unsigned long)__per_cpu_load)
30#else
31#define BOOT_PERCPU_OFFSET 0
32#endif
33
34DEFINE_PER_CPU(unsigned long, this_cpu_off) = BOOT_PERCPU_OFFSET;
35EXPORT_PER_CPU_SYMBOL(this_cpu_off);
36
Mike Travisc90aa892009-01-13 20:41:34 +090037#ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030038
Tejun Heo9939dda2009-01-13 20:41:35 +090039unsigned long __per_cpu_offset[NR_CPUS] __read_mostly = {
Brian Gerst16884012009-01-27 12:56:48 +090040 [0] = BOOT_PERCPU_OFFSET,
Tejun Heo9939dda2009-01-13 20:41:35 +090041};
Tejun Heo9939dda2009-01-13 20:41:35 +090042EXPORT_SYMBOL(__per_cpu_offset);
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030043
44/*
45 * Great future plan:
46 * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
47 * Always point %gs to its beginning
48 */
49void __init setup_per_cpu_areas(void)
50{
Brian Gerst74631a22009-01-27 12:56:47 +090051 ssize_t size;
Mike Travis3461b0a2008-05-12 21:21:13 +020052 char *ptr;
53 int cpu;
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030054
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030055 /* Copy section for each CPU (we discard the original) */
Brian Gerst74631a22009-01-27 12:56:47 +090056 size = roundup(PERCPU_ENOUGH_ROOM, PAGE_SIZE);
Mike Travisa1681962008-12-16 17:33:53 -080057
Cyrill Gorcunovab143982009-01-02 21:51:32 +030058 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 -080059 NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids);
60
Cyrill Gorcunovab143982009-01-02 21:51:32 +030061 pr_info("PERCPU: Allocating %zd bytes of per cpu data\n", size);
Mike Travisb447a462008-03-25 15:06:51 -070062
Mike Travis3461b0a2008-05-12 21:21:13 +020063 for_each_possible_cpu(cpu) {
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030064#ifndef CONFIG_NEED_MULTIPLE_NODES
Brian Gerst74631a22009-01-27 12:56:47 +090065 ptr = alloc_bootmem_pages(size);
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030066#else
Mike Travis3461b0a2008-05-12 21:21:13 +020067 int node = early_cpu_to_node(cpu);
Mike Travisb447a462008-03-25 15:06:51 -070068 if (!node_online(node) || !NODE_DATA(node)) {
Brian Gerst74631a22009-01-27 12:56:47 +090069 ptr = alloc_bootmem_pages(size);
Cyrill Gorcunovab143982009-01-02 21:51:32 +030070 pr_info("cpu %d has no node %d or node-local memory\n",
Mike Travis3461b0a2008-05-12 21:21:13 +020071 cpu, node);
Cyrill Gorcunovab143982009-01-02 21:51:32 +030072 pr_debug("per cpu data for cpu%d at %016lx\n",
73 cpu, __pa(ptr));
74 } else {
Brian Gerst74631a22009-01-27 12:56:47 +090075 ptr = alloc_bootmem_pages_node(NODE_DATA(node), size);
Cyrill Gorcunovab143982009-01-02 21:51:32 +030076 pr_debug("per cpu data for cpu%d on node%d at %016lx\n",
77 cpu, node, __pa(ptr));
Yinghai Lua677f582008-07-29 00:37:10 -070078 }
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030079#endif
Tejun Heo1a51e3a2009-01-13 20:41:35 +090080
Tejun Heo3e5d8f92009-01-13 20:41:35 +090081 memcpy(ptr, __per_cpu_load, __per_cpu_end - __per_cpu_start);
Tejun Heo9939dda2009-01-13 20:41:35 +090082 per_cpu_offset(cpu) = ptr - __per_cpu_start;
Brian Gerst26f80bd2009-01-19 00:38:58 +090083 per_cpu(this_cpu_off, cpu) = per_cpu_offset(cpu);
Brian Gerstea927902009-01-19 00:38:58 +090084 per_cpu(cpu_number, cpu) = cpu;
Brian Gerst0d77e7f2009-01-27 12:56:47 +090085 /*
86 * Copy data used in early init routines from the initial arrays to the
87 * per cpu data areas. These arrays then become expendable and the
88 * *_early_ptr's are zeroed indicating that the static arrays are gone.
89 */
Brian Gerstec70de82009-01-27 12:56:47 +090090#ifdef CONFIG_X86_LOCAL_APIC
Brian Gerst0d77e7f2009-01-27 12:56:47 +090091 per_cpu(x86_cpu_to_apicid, cpu) =
92 early_per_cpu_map(x86_cpu_to_apicid, cpu);
93 per_cpu(x86_bios_cpu_apicid, cpu) =
94 early_per_cpu_map(x86_bios_cpu_apicid, cpu);
Brian Gerstec70de82009-01-27 12:56:47 +090095#endif
Tejun Heo1a51e3a2009-01-13 20:41:35 +090096#ifdef CONFIG_X86_64
Brian Gerst26f80bd2009-01-19 00:38:58 +090097 per_cpu(irq_stack_ptr, cpu) =
Brian Gerst947e76c2009-01-19 12:21:28 +090098 per_cpu(irq_stack_union.irq_stack, cpu) + IRQ_STACK_SIZE - 64;
Brian Gerst6470aff2009-01-27 12:56:47 +090099#ifdef CONFIG_NUMA
100 per_cpu(x86_cpu_to_node_map, cpu) =
101 early_per_cpu_map(x86_cpu_to_node_map, cpu);
102#endif
Tejun Heo1a51e3a2009-01-13 20:41:35 +0900103 /*
Brian Gerst947e76c2009-01-19 12:21:28 +0900104 * Up to this point, CPU0 has been using .data.init
105 * area. Reload %gs offset for CPU0.
Tejun Heo1a51e3a2009-01-13 20:41:35 +0900106 */
107 if (cpu == 0)
Brian Gerst947e76c2009-01-19 12:21:28 +0900108 load_gs_base(cpu);
Tejun Heo1a51e3a2009-01-13 20:41:35 +0900109#endif
Mike Travisc90aa892009-01-13 20:41:34 +0900110
111 DBG("PERCPU: cpu %4d %p\n", cpu, ptr);
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300112 }
113
Brian Gerst0d77e7f2009-01-27 12:56:47 +0900114 /* indicate the early static arrays will soon be gone */
115 early_per_cpu_ptr(x86_cpu_to_apicid) = NULL;
116 early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL;
Brian Gerst6470aff2009-01-27 12:56:47 +0900117#if defined(CONFIG_X86_64) && defined(CONFIG_NUMA)
Brian Gerst0d77e7f2009-01-27 12:56:47 +0900118 early_per_cpu_ptr(x86_cpu_to_node_map) = NULL;
119#endif
Mike Travis9f0e8d02008-04-04 18:11:01 -0700120
Mike Travis9f248bd2008-05-12 21:21:12 +0200121 /* Setup node to cpumask map */
122 setup_node_to_cpumask_map();
Mike Travisc2d1cec2009-01-04 05:18:03 -0800123
124 /* Setup cpu initialized, callin, callout masks */
125 setup_cpu_local_masks();
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300126}
127
128#endif
Huang, Yingc45a7072008-06-02 14:26:25 +0800129