Glauber de Oliveira Costa | 4fe29a8 | 2008-03-19 14:25:23 -0300 | [diff] [blame] | 1 | #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 Walle | 1ecd276 | 2008-06-20 15:38:22 +0200 | [diff] [blame] | 6 | #include <linux/kexec.h> |
Yinghai Lu | 17b4cce | 2008-06-21 21:02:20 -0700 | [diff] [blame] | 7 | #include <linux/crash_dump.h> |
Jaswinder Singh Rajput | 8a87dd9 | 2009-01-04 17:04:26 +0530 | [diff] [blame] | 8 | #include <linux/smp.h> |
| 9 | #include <linux/topology.h> |
Tejun Heo | 5f5d840 | 2009-02-24 11:57:21 +0900 | [diff] [blame^] | 10 | #include <linux/pfn.h> |
Glauber de Oliveira Costa | 4fe29a8 | 2008-03-19 14:25:23 -0300 | [diff] [blame] | 11 | #include <asm/sections.h> |
| 12 | #include <asm/processor.h> |
| 13 | #include <asm/setup.h> |
Alexey Starikovskiy | 0fc0906 | 2008-04-04 23:40:48 +0400 | [diff] [blame] | 14 | #include <asm/mpspec.h> |
Alexey Starikovskiy | 76eb413 | 2008-04-04 23:40:41 +0400 | [diff] [blame] | 15 | #include <asm/apicdef.h> |
Bernhard Walle | 1ecd276 | 2008-06-20 15:38:22 +0200 | [diff] [blame] | 16 | #include <asm/highmem.h> |
Tejun Heo | 1a51e3a | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 17 | #include <asm/proto.h> |
Jaswinder Singh Rajput | 0687903 | 2009-01-10 12:17:37 +0530 | [diff] [blame] | 18 | #include <asm/cpumask.h> |
Brian Gerst | 34019be | 2009-01-27 12:56:48 +0900 | [diff] [blame] | 19 | #include <asm/cpu.h> |
Tejun Heo | 60a5317 | 2009-02-09 22:17:40 +0900 | [diff] [blame] | 20 | #include <asm/stackprotector.h> |
Alexey Starikovskiy | 76eb413 | 2008-04-04 23:40:41 +0400 | [diff] [blame] | 21 | |
Mike Travis | c90aa89 | 2009-01-13 20:41:34 +0900 | [diff] [blame] | 22 | #ifdef CONFIG_DEBUG_PER_CPU_MAPS |
| 23 | # define DBG(x...) printk(KERN_DEBUG x) |
| 24 | #else |
| 25 | # define DBG(x...) |
| 26 | #endif |
| 27 | |
Brian Gerst | ea92790 | 2009-01-19 00:38:58 +0900 | [diff] [blame] | 28 | DEFINE_PER_CPU(int, cpu_number); |
| 29 | EXPORT_PER_CPU_SYMBOL(cpu_number); |
Brian Gerst | ea92790 | 2009-01-19 00:38:58 +0900 | [diff] [blame] | 30 | |
Brian Gerst | 1688401 | 2009-01-27 12:56:48 +0900 | [diff] [blame] | 31 | #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 | |
| 37 | DEFINE_PER_CPU(unsigned long, this_cpu_off) = BOOT_PERCPU_OFFSET; |
| 38 | EXPORT_PER_CPU_SYMBOL(this_cpu_off); |
| 39 | |
Tejun Heo | 9939dda | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 40 | unsigned long __per_cpu_offset[NR_CPUS] __read_mostly = { |
Brian Gerst | 34019be | 2009-01-27 12:56:48 +0900 | [diff] [blame] | 41 | [0 ... NR_CPUS-1] = BOOT_PERCPU_OFFSET, |
Tejun Heo | 9939dda | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 42 | }; |
Tejun Heo | 9939dda | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 43 | EXPORT_SYMBOL(__per_cpu_offset); |
Glauber de Oliveira Costa | 4fe29a8 | 2008-03-19 14:25:23 -0300 | [diff] [blame] | 44 | |
Tejun Heo | 5f5d840 | 2009-02-24 11:57:21 +0900 | [diff] [blame^] | 45 | /** |
| 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 | */ |
| 58 | static 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 Heo | 8d408b4 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 91 | static struct page **pcpu4k_pages __initdata; |
| 92 | static int pcpu4k_nr_static_pages __initdata; |
| 93 | |
| 94 | static 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 Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 101 | static void __init pcpu4k_populate_pte(unsigned long addr) |
| 102 | { |
| 103 | populate_extra_pte(addr); |
| 104 | } |
| 105 | |
Tejun Heo | 5f5d840 | 2009-02-24 11:57:21 +0900 | [diff] [blame^] | 106 | static 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 | |
| 142 | enomem: |
| 143 | while (--j >= 0) |
| 144 | free_bootmem(__pa(page_address(pcpu4k_pages[j])), PAGE_SIZE); |
| 145 | ret = -ENOMEM; |
| 146 | out_free_ar: |
| 147 | free_bootmem(__pa(pcpu4k_pages), pages_size); |
| 148 | return ret; |
| 149 | } |
| 150 | |
Brian Gerst | b2d2f43 | 2009-01-27 12:56:48 +0900 | [diff] [blame] | 151 | static 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 Costa | 4fe29a8 | 2008-03-19 14:25:23 -0300 | [diff] [blame] | 164 | /* |
| 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 | */ |
| 169 | void __init setup_per_cpu_areas(void) |
| 170 | { |
Tejun Heo | 5f5d840 | 2009-02-24 11:57:21 +0900 | [diff] [blame^] | 171 | size_t static_size = __per_cpu_end - __per_cpu_start; |
| 172 | unsigned int cpu; |
Tejun Heo | 1112441 | 2009-02-20 16:29:09 +0900 | [diff] [blame] | 173 | unsigned long delta; |
| 174 | size_t pcpu_unit_size; |
Tejun Heo | 5f5d840 | 2009-02-24 11:57:21 +0900 | [diff] [blame^] | 175 | ssize_t ret; |
Mike Travis | a168196 | 2008-12-16 17:33:53 -0800 | [diff] [blame] | 176 | |
Cyrill Gorcunov | ab14398 | 2009-01-02 21:51:32 +0300 | [diff] [blame] | 177 | pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%d nr_node_ids:%d\n", |
Mike Travis | a168196 | 2008-12-16 17:33:53 -0800 | [diff] [blame] | 178 | NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids); |
| 179 | |
Tejun Heo | 5f5d840 | 2009-02-24 11:57:21 +0900 | [diff] [blame^] | 180 | /* 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 Travis | b447a46 | 2008-03-25 15:06:51 -0700 | [diff] [blame] | 185 | |
Tejun Heo | 5f5d840 | 2009-02-24 11:57:21 +0900 | [diff] [blame^] | 186 | pcpu_unit_size = ret; |
Tejun Heo | 1a51e3a | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 187 | |
Tejun Heo | 5f5d840 | 2009-02-24 11:57:21 +0900 | [diff] [blame^] | 188 | /* alrighty, percpu areas up and running */ |
Tejun Heo | 1112441 | 2009-02-20 16:29:09 +0900 | [diff] [blame] | 189 | 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 Gerst | 26f80bd | 2009-01-19 00:38:58 +0900 | [diff] [blame] | 192 | per_cpu(this_cpu_off, cpu) = per_cpu_offset(cpu); |
Brian Gerst | ea92790 | 2009-01-19 00:38:58 +0900 | [diff] [blame] | 193 | per_cpu(cpu_number, cpu) = cpu; |
Brian Gerst | b2d2f43 | 2009-01-27 12:56:48 +0900 | [diff] [blame] | 194 | setup_percpu_segment(cpu); |
Tejun Heo | 60a5317 | 2009-02-09 22:17:40 +0900 | [diff] [blame] | 195 | setup_stack_canary_segment(cpu); |
Brian Gerst | 0d77e7f | 2009-01-27 12:56:47 +0900 | [diff] [blame] | 196 | /* |
Tejun Heo | cf3997f | 2009-01-27 14:25:05 +0900 | [diff] [blame] | 197 | * 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 Gerst | 0d77e7f | 2009-01-27 12:56:47 +0900 | [diff] [blame] | 202 | */ |
Brian Gerst | ec70de8 | 2009-01-27 12:56:47 +0900 | [diff] [blame] | 203 | #ifdef CONFIG_X86_LOCAL_APIC |
Brian Gerst | 0d77e7f | 2009-01-27 12:56:47 +0900 | [diff] [blame] | 204 | per_cpu(x86_cpu_to_apicid, cpu) = |
Tejun Heo | cf3997f | 2009-01-27 14:25:05 +0900 | [diff] [blame] | 205 | early_per_cpu_map(x86_cpu_to_apicid, cpu); |
Brian Gerst | 0d77e7f | 2009-01-27 12:56:47 +0900 | [diff] [blame] | 206 | per_cpu(x86_bios_cpu_apicid, cpu) = |
Tejun Heo | cf3997f | 2009-01-27 14:25:05 +0900 | [diff] [blame] | 207 | early_per_cpu_map(x86_bios_cpu_apicid, cpu); |
Brian Gerst | ec70de8 | 2009-01-27 12:56:47 +0900 | [diff] [blame] | 208 | #endif |
Tejun Heo | 1a51e3a | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 209 | #ifdef CONFIG_X86_64 |
Brian Gerst | 26f80bd | 2009-01-19 00:38:58 +0900 | [diff] [blame] | 210 | per_cpu(irq_stack_ptr, cpu) = |
Tejun Heo | cf3997f | 2009-01-27 14:25:05 +0900 | [diff] [blame] | 211 | per_cpu(irq_stack_union.irq_stack, cpu) + |
| 212 | IRQ_STACK_SIZE - 64; |
Brian Gerst | 6470aff | 2009-01-27 12:56:47 +0900 | [diff] [blame] | 213 | #ifdef CONFIG_NUMA |
| 214 | per_cpu(x86_cpu_to_node_map, cpu) = |
Tejun Heo | cf3997f | 2009-01-27 14:25:05 +0900 | [diff] [blame] | 215 | early_per_cpu_map(x86_cpu_to_node_map, cpu); |
Brian Gerst | 6470aff | 2009-01-27 12:56:47 +0900 | [diff] [blame] | 216 | #endif |
Brian Gerst | 2697fbd | 2009-01-27 12:56:48 +0900 | [diff] [blame] | 217 | #endif |
Tejun Heo | 1a51e3a | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 218 | /* |
Brian Gerst | 34019be | 2009-01-27 12:56:48 +0900 | [diff] [blame] | 219 | * Up to this point, the boot CPU has been using .data.init |
Brian Gerst | 2697fbd | 2009-01-27 12:56:48 +0900 | [diff] [blame] | 220 | * area. Reload any changed state for the boot CPU. |
Tejun Heo | 1a51e3a | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 221 | */ |
Brian Gerst | 34019be | 2009-01-27 12:56:48 +0900 | [diff] [blame] | 222 | if (cpu == boot_cpu_id) |
Brian Gerst | 552be87 | 2009-01-30 17:47:53 +0900 | [diff] [blame] | 223 | switch_to_new_gdt(cpu); |
Mike Travis | c90aa89 | 2009-01-13 20:41:34 +0900 | [diff] [blame] | 224 | |
| 225 | DBG("PERCPU: cpu %4d %p\n", cpu, ptr); |
Glauber de Oliveira Costa | 4fe29a8 | 2008-03-19 14:25:23 -0300 | [diff] [blame] | 226 | } |
| 227 | |
Brian Gerst | 0d77e7f | 2009-01-27 12:56:47 +0900 | [diff] [blame] | 228 | /* indicate the early static arrays will soon be gone */ |
James Bottomley | 22f2513 | 2009-01-27 14:21:37 +0900 | [diff] [blame] | 229 | #ifdef CONFIG_X86_LOCAL_APIC |
Brian Gerst | 0d77e7f | 2009-01-27 12:56:47 +0900 | [diff] [blame] | 230 | early_per_cpu_ptr(x86_cpu_to_apicid) = NULL; |
| 231 | early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL; |
James Bottomley | 22f2513 | 2009-01-27 14:21:37 +0900 | [diff] [blame] | 232 | #endif |
Brian Gerst | 6470aff | 2009-01-27 12:56:47 +0900 | [diff] [blame] | 233 | #if defined(CONFIG_X86_64) && defined(CONFIG_NUMA) |
Brian Gerst | 0d77e7f | 2009-01-27 12:56:47 +0900 | [diff] [blame] | 234 | early_per_cpu_ptr(x86_cpu_to_node_map) = NULL; |
| 235 | #endif |
Mike Travis | 9f0e8d0 | 2008-04-04 18:11:01 -0700 | [diff] [blame] | 236 | |
Mike Travis | 9f248bd | 2008-05-12 21:21:12 +0200 | [diff] [blame] | 237 | /* Setup node to cpumask map */ |
| 238 | setup_node_to_cpumask_map(); |
Mike Travis | c2d1cec | 2009-01-04 05:18:03 -0800 | [diff] [blame] | 239 | |
| 240 | /* Setup cpu initialized, callin, callout masks */ |
| 241 | setup_cpu_local_masks(); |
Glauber de Oliveira Costa | 4fe29a8 | 2008-03-19 14:25:23 -0300 | [diff] [blame] | 242 | } |