setup_percpu.c 3.4 KB
Newer Older
1 2 3 4 5
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/bootmem.h>
#include <linux/percpu.h>
6
#include <linux/kexec.h>
7
#include <linux/crash_dump.h>
8 9
#include <linux/smp.h>
#include <linux/topology.h>
10 11 12
#include <asm/sections.h>
#include <asm/processor.h>
#include <asm/setup.h>
13
#include <asm/mpspec.h>
14
#include <asm/apicdef.h>
15
#include <asm/highmem.h>
16
#include <asm/proto.h>
17
#include <asm/cpumask.h>
18

19 20 21 22 23 24
#ifdef CONFIG_DEBUG_PER_CPU_MAPS
# define DBG(x...) printk(KERN_DEBUG x)
#else
# define DBG(x...)
#endif

25 26 27
DEFINE_PER_CPU(int, cpu_number);
EXPORT_PER_CPU_SYMBOL(cpu_number);

B
Brian Gerst 已提交
28 29 30 31 32 33 34 35 36
#ifdef CONFIG_X86_64
#define BOOT_PERCPU_OFFSET ((unsigned long)__per_cpu_load)
#else
#define BOOT_PERCPU_OFFSET 0
#endif

DEFINE_PER_CPU(unsigned long, this_cpu_off) = BOOT_PERCPU_OFFSET;
EXPORT_PER_CPU_SYMBOL(this_cpu_off);

37
#ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
38

39
unsigned long __per_cpu_offset[NR_CPUS] __read_mostly = {
B
Brian Gerst 已提交
40
	[0] = BOOT_PERCPU_OFFSET,
41 42
};
EXPORT_SYMBOL(__per_cpu_offset);
43 44 45 46 47 48 49 50

/*
 * Great future plan:
 * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
 * Always point %gs to its beginning
 */
void __init setup_per_cpu_areas(void)
{
51
	ssize_t size;
52 53
	char *ptr;
	int cpu;
54 55

	/* Copy section for each CPU (we discard the original) */
56
	size = roundup(PERCPU_ENOUGH_ROOM, PAGE_SIZE);
57

58
	pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%d nr_node_ids:%d\n",
59 60
		NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids);

61
	pr_info("PERCPU: Allocating %zd bytes of per cpu data\n", size);
62

63
	for_each_possible_cpu(cpu) {
64
#ifndef CONFIG_NEED_MULTIPLE_NODES
65
		ptr = alloc_bootmem_pages(size);
66
#else
67
		int node = early_cpu_to_node(cpu);
68
		if (!node_online(node) || !NODE_DATA(node)) {
69
			ptr = alloc_bootmem_pages(size);
70
			pr_info("cpu %d has no node %d or node-local memory\n",
71
				cpu, node);
72 73 74
			pr_debug("per cpu data for cpu%d at %016lx\n",
				 cpu, __pa(ptr));
		} else {
75
			ptr = alloc_bootmem_pages_node(NODE_DATA(node), size);
76 77
			pr_debug("per cpu data for cpu%d on node%d at %016lx\n",
				cpu, node, __pa(ptr));
Y
Yinghai Lu 已提交
78
		}
79
#endif
80

81
		memcpy(ptr, __per_cpu_load, __per_cpu_end - __per_cpu_start);
82
		per_cpu_offset(cpu) = ptr - __per_cpu_start;
83
		per_cpu(this_cpu_off, cpu) = per_cpu_offset(cpu);
84
		per_cpu(cpu_number, cpu) = cpu;
85 86 87 88 89
		/*
		 * Copy data used in early init routines from the initial arrays to the
		 * per cpu data areas.  These arrays then become expendable and the
		 * *_early_ptr's are zeroed indicating that the static arrays are gone.
		 */
B
Brian Gerst 已提交
90
#ifdef CONFIG_X86_LOCAL_APIC
91 92 93 94
		per_cpu(x86_cpu_to_apicid, cpu) =
				early_per_cpu_map(x86_cpu_to_apicid, cpu);
		per_cpu(x86_bios_cpu_apicid, cpu) =
				early_per_cpu_map(x86_bios_cpu_apicid, cpu);
B
Brian Gerst 已提交
95
#endif
96
#ifdef CONFIG_X86_64
97
		per_cpu(irq_stack_ptr, cpu) =
98
			per_cpu(irq_stack_union.irq_stack, cpu) + IRQ_STACK_SIZE - 64;
B
Brian Gerst 已提交
99 100 101 102
#ifdef CONFIG_NUMA
		per_cpu(x86_cpu_to_node_map, cpu) =
				early_per_cpu_map(x86_cpu_to_node_map, cpu);
#endif
103
		/*
104 105
		 * Up to this point, CPU0 has been using .data.init
		 * area.  Reload %gs offset for CPU0.
106 107
		 */
		if (cpu == 0)
108
			load_gs_base(cpu);
109
#endif
110 111

		DBG("PERCPU: cpu %4d %p\n", cpu, ptr);
112 113
	}

114 115 116
	/* indicate the early static arrays will soon be gone */
	early_per_cpu_ptr(x86_cpu_to_apicid) = NULL;
	early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL;
B
Brian Gerst 已提交
117
#if defined(CONFIG_X86_64) && defined(CONFIG_NUMA)
118 119
	early_per_cpu_ptr(x86_cpu_to_node_map) = NULL;
#endif
120

121 122
	/* Setup node to cpumask map */
	setup_node_to_cpumask_map();
123 124 125

	/* Setup cpu initialized, callin, callout masks */
	setup_cpu_local_masks();
126 127 128
}

#endif
129