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> 10378b39a4SYinghai Lu #include <asm/sections.h> 11378b39a4SYinghai Lu #include <asm/processor.h> 12378b39a4SYinghai Lu #include <asm/setup.h> 13378b39a4SYinghai Lu #include <asm/mpspec.h> 14378b39a4SYinghai Lu #include <asm/apicdef.h> 15378b39a4SYinghai Lu #include <asm/highmem.h> 161a51e3a0STejun Heo #include <asm/proto.h> 1706879033SJaswinder Singh Rajput #include <asm/cpumask.h> 1834019be1SBrian Gerst #include <asm/cpu.h> 19378b39a4SYinghai Lu 20c90aa894SMike Travis #ifdef CONFIG_DEBUG_PER_CPU_MAPS 21c90aa894SMike Travis # define DBG(x...) printk(KERN_DEBUG x) 22c90aa894SMike Travis #else 23c90aa894SMike Travis # define DBG(x...) 24c90aa894SMike Travis #endif 25c90aa894SMike Travis 26ea927906SBrian Gerst DEFINE_PER_CPU(int, cpu_number); 27ea927906SBrian Gerst EXPORT_PER_CPU_SYMBOL(cpu_number); 28ea927906SBrian Gerst 291688401aSBrian Gerst #ifdef CONFIG_X86_64 301688401aSBrian Gerst #define BOOT_PERCPU_OFFSET ((unsigned long)__per_cpu_load) 311688401aSBrian Gerst #else 321688401aSBrian Gerst #define BOOT_PERCPU_OFFSET 0 331688401aSBrian Gerst #endif 341688401aSBrian Gerst 351688401aSBrian Gerst DEFINE_PER_CPU(unsigned long, this_cpu_off) = BOOT_PERCPU_OFFSET; 361688401aSBrian Gerst EXPORT_PER_CPU_SYMBOL(this_cpu_off); 371688401aSBrian Gerst 389939ddafSTejun Heo unsigned long __per_cpu_offset[NR_CPUS] __read_mostly = { 3934019be1SBrian Gerst [0 ... NR_CPUS-1] = BOOT_PERCPU_OFFSET, 409939ddafSTejun Heo }; 419939ddafSTejun Heo EXPORT_SYMBOL(__per_cpu_offset); 42378b39a4SYinghai Lu 43b2d2f431SBrian Gerst static inline void setup_percpu_segment(int cpu) 44b2d2f431SBrian Gerst { 45b2d2f431SBrian Gerst #ifdef CONFIG_X86_32 46b2d2f431SBrian Gerst struct desc_struct gdt; 47b2d2f431SBrian Gerst 48b2d2f431SBrian Gerst pack_descriptor(&gdt, per_cpu_offset(cpu), 0xFFFFF, 49b2d2f431SBrian Gerst 0x2 | DESCTYPE_S, 0x8); 50b2d2f431SBrian Gerst gdt.s = 1; 51b2d2f431SBrian Gerst write_gdt_entry(get_cpu_gdt_table(cpu), 52b2d2f431SBrian Gerst GDT_ENTRY_PERCPU, &gdt, DESCTYPE_S); 53b2d2f431SBrian Gerst #endif 54b2d2f431SBrian Gerst } 55b2d2f431SBrian Gerst 56378b39a4SYinghai Lu /* 57378b39a4SYinghai Lu * Great future plan: 58378b39a4SYinghai Lu * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data. 59378b39a4SYinghai Lu * Always point %gs to its beginning 60378b39a4SYinghai Lu */ 61378b39a4SYinghai Lu void __init setup_per_cpu_areas(void) 62378b39a4SYinghai Lu { 6374631a24SBrian Gerst ssize_t size; 64378b39a4SYinghai Lu char *ptr; 65378b39a4SYinghai Lu int cpu; 66378b39a4SYinghai Lu 67378b39a4SYinghai Lu /* Copy section for each CPU (we discard the original) */ 6874631a24SBrian Gerst size = roundup(PERCPU_ENOUGH_ROOM, PAGE_SIZE); 69a1681965SMike Travis 70ab14398aSCyrill Gorcunov pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%d nr_node_ids:%d\n", 71a1681965SMike Travis NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids); 72a1681965SMike Travis 73ab14398aSCyrill Gorcunov pr_info("PERCPU: Allocating %zd bytes of per cpu data\n", size); 74378b39a4SYinghai Lu 75378b39a4SYinghai Lu for_each_possible_cpu(cpu) { 76378b39a4SYinghai Lu #ifndef CONFIG_NEED_MULTIPLE_NODES 7774631a24SBrian Gerst ptr = alloc_bootmem_pages(size); 78378b39a4SYinghai Lu #else 79378b39a4SYinghai Lu int node = early_cpu_to_node(cpu); 80378b39a4SYinghai Lu if (!node_online(node) || !NODE_DATA(node)) { 8174631a24SBrian Gerst ptr = alloc_bootmem_pages(size); 82ab14398aSCyrill Gorcunov pr_info("cpu %d has no node %d or node-local memory\n", 83378b39a4SYinghai Lu cpu, node); 84ab14398aSCyrill Gorcunov pr_debug("per cpu data for cpu%d at %016lx\n", 85a677f58aSYinghai Lu cpu, __pa(ptr)); 86ab14398aSCyrill Gorcunov } else { 8774631a24SBrian Gerst ptr = alloc_bootmem_pages_node(NODE_DATA(node), size); 88ab14398aSCyrill Gorcunov pr_debug("per cpu data for cpu%d on node%d at %016lx\n", 89a677f58aSYinghai Lu cpu, node, __pa(ptr)); 90a677f58aSYinghai Lu } 91378b39a4SYinghai Lu #endif 921a51e3a0STejun Heo 933e5d8f97STejun Heo memcpy(ptr, __per_cpu_load, __per_cpu_end - __per_cpu_start); 949939ddafSTejun Heo per_cpu_offset(cpu) = ptr - __per_cpu_start; 9526f80bd6SBrian Gerst per_cpu(this_cpu_off, cpu) = per_cpu_offset(cpu); 96ea927906SBrian Gerst per_cpu(cpu_number, cpu) = cpu; 97b2d2f431SBrian Gerst setup_percpu_segment(cpu); 980d77e7f0SBrian Gerst /* 990d77e7f0SBrian Gerst * Copy data used in early init routines from the initial arrays to the 1000d77e7f0SBrian Gerst * per cpu data areas. These arrays then become expendable and the 1010d77e7f0SBrian Gerst * *_early_ptr's are zeroed indicating that the static arrays are gone. 1020d77e7f0SBrian Gerst */ 103ec70de8bSBrian Gerst #ifdef CONFIG_X86_LOCAL_APIC 1040d77e7f0SBrian Gerst per_cpu(x86_cpu_to_apicid, cpu) = 1050d77e7f0SBrian Gerst early_per_cpu_map(x86_cpu_to_apicid, cpu); 1060d77e7f0SBrian Gerst per_cpu(x86_bios_cpu_apicid, cpu) = 1070d77e7f0SBrian Gerst early_per_cpu_map(x86_bios_cpu_apicid, cpu); 108ec70de8bSBrian Gerst #endif 1091a51e3a0STejun Heo #ifdef CONFIG_X86_64 11026f80bd6SBrian Gerst per_cpu(irq_stack_ptr, cpu) = 111947e76cdSBrian Gerst per_cpu(irq_stack_union.irq_stack, cpu) + IRQ_STACK_SIZE - 64; 1126470aff6SBrian Gerst #ifdef CONFIG_NUMA 1136470aff6SBrian Gerst per_cpu(x86_cpu_to_node_map, cpu) = 1146470aff6SBrian Gerst early_per_cpu_map(x86_cpu_to_node_map, cpu); 1156470aff6SBrian Gerst #endif 116*2697fbd5SBrian Gerst #endif 1171a51e3a0STejun Heo /* 11834019be1SBrian Gerst * Up to this point, the boot CPU has been using .data.init 119*2697fbd5SBrian Gerst * area. Reload any changed state for the boot CPU. 1201a51e3a0STejun Heo */ 12134019be1SBrian Gerst if (cpu == boot_cpu_id) 122*2697fbd5SBrian Gerst switch_to_new_gdt(); 123c90aa894SMike Travis 124c90aa894SMike Travis DBG("PERCPU: cpu %4d %p\n", cpu, ptr); 125378b39a4SYinghai Lu } 126378b39a4SYinghai Lu 1270d77e7f0SBrian Gerst /* indicate the early static arrays will soon be gone */ 1280d77e7f0SBrian Gerst early_per_cpu_ptr(x86_cpu_to_apicid) = NULL; 1290d77e7f0SBrian Gerst early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL; 1306470aff6SBrian Gerst #if defined(CONFIG_X86_64) && defined(CONFIG_NUMA) 1310d77e7f0SBrian Gerst early_per_cpu_ptr(x86_cpu_to_node_map) = NULL; 1320d77e7f0SBrian Gerst #endif 133378b39a4SYinghai Lu 134378b39a4SYinghai Lu /* Setup node to cpumask map */ 135378b39a4SYinghai Lu setup_node_to_cpumask_map(); 136c2d1cec1SMike Travis 137c2d1cec1SMike Travis /* Setup cpu initialized, callin, callout masks */ 138c2d1cec1SMike Travis setup_cpu_local_masks(); 139378b39a4SYinghai Lu } 140