0001
0002 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0003
0004 #include <linux/kernel.h>
0005 #include <linux/export.h>
0006 #include <linux/init.h>
0007 #include <linux/memblock.h>
0008 #include <linux/percpu.h>
0009 #include <linux/kexec.h>
0010 #include <linux/crash_dump.h>
0011 #include <linux/smp.h>
0012 #include <linux/topology.h>
0013 #include <linux/pfn.h>
0014 #include <asm/sections.h>
0015 #include <asm/processor.h>
0016 #include <asm/desc.h>
0017 #include <asm/setup.h>
0018 #include <asm/mpspec.h>
0019 #include <asm/apicdef.h>
0020 #include <asm/highmem.h>
0021 #include <asm/proto.h>
0022 #include <asm/cpumask.h>
0023 #include <asm/cpu.h>
0024 #include <asm/stackprotector.h>
0025
0026 DEFINE_PER_CPU_READ_MOSTLY(int, cpu_number);
0027 EXPORT_PER_CPU_SYMBOL(cpu_number);
0028
0029 #ifdef CONFIG_X86_64
0030 #define BOOT_PERCPU_OFFSET ((unsigned long)__per_cpu_load)
0031 #else
0032 #define BOOT_PERCPU_OFFSET 0
0033 #endif
0034
0035 DEFINE_PER_CPU_READ_MOSTLY(unsigned long, this_cpu_off) = BOOT_PERCPU_OFFSET;
0036 EXPORT_PER_CPU_SYMBOL(this_cpu_off);
0037
0038 unsigned long __per_cpu_offset[NR_CPUS] __ro_after_init = {
0039 [0 ... NR_CPUS-1] = BOOT_PERCPU_OFFSET,
0040 };
0041 EXPORT_SYMBOL(__per_cpu_offset);
0042
0043
0044
0045
0046
0047
0048
0049
0050 #ifdef CONFIG_X86_64
0051 #define PERCPU_FIRST_CHUNK_RESERVE PERCPU_MODULE_RESERVE
0052 #else
0053 #define PERCPU_FIRST_CHUNK_RESERVE 0
0054 #endif
0055
0056 #ifdef CONFIG_X86_32
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067 static bool __init pcpu_need_numa(void)
0068 {
0069 #ifdef CONFIG_NUMA
0070 pg_data_t *last = NULL;
0071 unsigned int cpu;
0072
0073 for_each_possible_cpu(cpu) {
0074 int node = early_cpu_to_node(cpu);
0075
0076 if (node_online(node) && NODE_DATA(node) &&
0077 last && last != NODE_DATA(node))
0078 return true;
0079
0080 last = NODE_DATA(node);
0081 }
0082 #endif
0083 return false;
0084 }
0085 #endif
0086
0087 static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
0088 {
0089 #ifdef CONFIG_NUMA
0090 if (early_cpu_to_node(from) == early_cpu_to_node(to))
0091 return LOCAL_DISTANCE;
0092 else
0093 return REMOTE_DISTANCE;
0094 #else
0095 return LOCAL_DISTANCE;
0096 #endif
0097 }
0098
0099 static int __init pcpu_cpu_to_node(int cpu)
0100 {
0101 return early_cpu_to_node(cpu);
0102 }
0103
0104 void __init pcpu_populate_pte(unsigned long addr)
0105 {
0106 populate_extra_pte(addr);
0107 }
0108
0109 static inline void setup_percpu_segment(int cpu)
0110 {
0111 #ifdef CONFIG_X86_32
0112 struct desc_struct d = GDT_ENTRY_INIT(0x8092, per_cpu_offset(cpu),
0113 0xFFFFF);
0114
0115 write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_PERCPU, &d, DESCTYPE_S);
0116 #endif
0117 }
0118
0119 void __init setup_per_cpu_areas(void)
0120 {
0121 unsigned int cpu;
0122 unsigned long delta;
0123 int rc;
0124
0125 pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%u nr_node_ids:%u\n",
0126 NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids);
0127
0128
0129
0130
0131
0132
0133
0134 #ifdef CONFIG_X86_32
0135 if (pcpu_chosen_fc == PCPU_FC_AUTO && pcpu_need_numa())
0136 pcpu_chosen_fc = PCPU_FC_PAGE;
0137 #endif
0138 rc = -EINVAL;
0139 if (pcpu_chosen_fc != PCPU_FC_PAGE) {
0140 const size_t dyn_size = PERCPU_MODULE_RESERVE +
0141 PERCPU_DYNAMIC_RESERVE - PERCPU_FIRST_CHUNK_RESERVE;
0142 size_t atom_size;
0143
0144
0145
0146
0147
0148
0149
0150
0151 #ifdef CONFIG_X86_64
0152 atom_size = PMD_SIZE;
0153 #else
0154 atom_size = PAGE_SIZE;
0155 #endif
0156 rc = pcpu_embed_first_chunk(PERCPU_FIRST_CHUNK_RESERVE,
0157 dyn_size, atom_size,
0158 pcpu_cpu_distance,
0159 pcpu_cpu_to_node);
0160 if (rc < 0)
0161 pr_warn("%s allocator failed (%d), falling back to page size\n",
0162 pcpu_fc_names[pcpu_chosen_fc], rc);
0163 }
0164 if (rc < 0)
0165 rc = pcpu_page_first_chunk(PERCPU_FIRST_CHUNK_RESERVE,
0166 pcpu_cpu_to_node);
0167 if (rc < 0)
0168 panic("cannot initialize percpu area (err=%d)", rc);
0169
0170
0171 delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
0172 for_each_possible_cpu(cpu) {
0173 per_cpu_offset(cpu) = delta + pcpu_unit_offsets[cpu];
0174 per_cpu(this_cpu_off, cpu) = per_cpu_offset(cpu);
0175 per_cpu(cpu_number, cpu) = cpu;
0176 setup_percpu_segment(cpu);
0177
0178
0179
0180
0181
0182
0183
0184 #ifdef CONFIG_X86_LOCAL_APIC
0185 per_cpu(x86_cpu_to_apicid, cpu) =
0186 early_per_cpu_map(x86_cpu_to_apicid, cpu);
0187 per_cpu(x86_bios_cpu_apicid, cpu) =
0188 early_per_cpu_map(x86_bios_cpu_apicid, cpu);
0189 per_cpu(x86_cpu_to_acpiid, cpu) =
0190 early_per_cpu_map(x86_cpu_to_acpiid, cpu);
0191 #endif
0192 #ifdef CONFIG_X86_32
0193 per_cpu(x86_cpu_to_logical_apicid, cpu) =
0194 early_per_cpu_map(x86_cpu_to_logical_apicid, cpu);
0195 #endif
0196 #ifdef CONFIG_NUMA
0197 per_cpu(x86_cpu_to_node_map, cpu) =
0198 early_per_cpu_map(x86_cpu_to_node_map, cpu);
0199
0200
0201
0202
0203
0204
0205
0206
0207 set_cpu_numa_node(cpu, early_cpu_to_node(cpu));
0208 #endif
0209
0210
0211
0212
0213 if (!cpu)
0214 switch_to_new_gdt(cpu);
0215 }
0216
0217
0218 #ifdef CONFIG_X86_LOCAL_APIC
0219 early_per_cpu_ptr(x86_cpu_to_apicid) = NULL;
0220 early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL;
0221 early_per_cpu_ptr(x86_cpu_to_acpiid) = NULL;
0222 #endif
0223 #ifdef CONFIG_X86_32
0224 early_per_cpu_ptr(x86_cpu_to_logical_apicid) = NULL;
0225 #endif
0226 #ifdef CONFIG_NUMA
0227 early_per_cpu_ptr(x86_cpu_to_node_map) = NULL;
0228 #endif
0229
0230
0231 setup_node_to_cpumask_map();
0232
0233
0234 setup_cpu_local_masks();
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246 sync_initial_page_table();
0247 }