x86: cleanup early per cpu variables/accesses v4
[sfrench/cifs-2.6.git] / arch / x86 / kernel / setup_64.c
1 /*
2  *  Copyright (C) 1995  Linus Torvalds
3  */
4
5 /*
6  * This file handles the architecture-dependent parts of initialization
7  */
8
9 #include <linux/errno.h>
10 #include <linux/sched.h>
11 #include <linux/kernel.h>
12 #include <linux/mm.h>
13 #include <linux/stddef.h>
14 #include <linux/unistd.h>
15 #include <linux/ptrace.h>
16 #include <linux/slab.h>
17 #include <linux/user.h>
18 #include <linux/screen_info.h>
19 #include <linux/ioport.h>
20 #include <linux/delay.h>
21 #include <linux/init.h>
22 #include <linux/initrd.h>
23 #include <linux/highmem.h>
24 #include <linux/bootmem.h>
25 #include <linux/module.h>
26 #include <asm/processor.h>
27 #include <linux/console.h>
28 #include <linux/seq_file.h>
29 #include <linux/crash_dump.h>
30 #include <linux/root_dev.h>
31 #include <linux/pci.h>
32 #include <asm/pci-direct.h>
33 #include <linux/efi.h>
34 #include <linux/acpi.h>
35 #include <linux/kallsyms.h>
36 #include <linux/edd.h>
37 #include <linux/iscsi_ibft.h>
38 #include <linux/mmzone.h>
39 #include <linux/kexec.h>
40 #include <linux/cpufreq.h>
41 #include <linux/dmi.h>
42 #include <linux/dma-mapping.h>
43 #include <linux/ctype.h>
44 #include <linux/sort.h>
45 #include <linux/uaccess.h>
46 #include <linux/init_ohci1394_dma.h>
47 #include <linux/kvm_para.h>
48
49 #include <asm/mtrr.h>
50 #include <asm/uaccess.h>
51 #include <asm/system.h>
52 #include <asm/vsyscall.h>
53 #include <asm/io.h>
54 #include <asm/smp.h>
55 #include <asm/msr.h>
56 #include <asm/desc.h>
57 #include <video/edid.h>
58 #include <asm/e820.h>
59 #include <asm/dma.h>
60 #include <asm/gart.h>
61 #include <asm/mpspec.h>
62 #include <asm/mmu_context.h>
63 #include <asm/proto.h>
64 #include <asm/setup.h>
65 #include <asm/numa.h>
66 #include <asm/sections.h>
67 #include <asm/dmi.h>
68 #include <asm/cacheflush.h>
69 #include <asm/mce.h>
70 #include <asm/ds.h>
71 #include <asm/topology.h>
72 #include <asm/trampoline.h>
73 #include <asm/pat.h>
74
75 #include <mach_apic.h>
76 #ifdef CONFIG_PARAVIRT
77 #include <asm/paravirt.h>
78 #else
79 #define ARCH_SETUP
80 #endif
81
82 /*
83  * Machine setup..
84  */
85
86 struct cpuinfo_x86 boot_cpu_data __read_mostly;
87 EXPORT_SYMBOL(boot_cpu_data);
88
89 __u32 cleared_cpu_caps[NCAPINTS] __cpuinitdata;
90
91 unsigned long mmu_cr4_features;
92
93 /* Boot loader ID as an integer, for the benefit of proc_dointvec */
94 int bootloader_type;
95
96 unsigned long saved_video_mode;
97
98 int force_mwait __cpuinitdata;
99
100 /*
101  * Early DMI memory
102  */
103 int dmi_alloc_index;
104 char dmi_alloc_data[DMI_MAX_DATA];
105
106 /*
107  * Setup options
108  */
109 struct screen_info screen_info;
110 EXPORT_SYMBOL(screen_info);
111 struct sys_desc_table_struct {
112         unsigned short length;
113         unsigned char table[0];
114 };
115
116 struct edid_info edid_info;
117 EXPORT_SYMBOL_GPL(edid_info);
118
119 extern int root_mountflags;
120
121 char __initdata command_line[COMMAND_LINE_SIZE];
122
123 static struct resource standard_io_resources[] = {
124         { .name = "dma1", .start = 0x00, .end = 0x1f,
125                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
126         { .name = "pic1", .start = 0x20, .end = 0x21,
127                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
128         { .name = "timer0", .start = 0x40, .end = 0x43,
129                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
130         { .name = "timer1", .start = 0x50, .end = 0x53,
131                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
132         { .name = "keyboard", .start = 0x60, .end = 0x60,
133                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
134         { .name = "keyboard", .start = 0x64, .end = 0x64,
135                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
136         { .name = "dma page reg", .start = 0x80, .end = 0x8f,
137                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
138         { .name = "pic2", .start = 0xa0, .end = 0xa1,
139                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
140         { .name = "dma2", .start = 0xc0, .end = 0xdf,
141                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
142         { .name = "fpu", .start = 0xf0, .end = 0xff,
143                 .flags = IORESOURCE_BUSY | IORESOURCE_IO }
144 };
145
146 #define IORESOURCE_RAM (IORESOURCE_BUSY | IORESOURCE_MEM)
147
148 static struct resource data_resource = {
149         .name = "Kernel data",
150         .start = 0,
151         .end = 0,
152         .flags = IORESOURCE_RAM,
153 };
154 static struct resource code_resource = {
155         .name = "Kernel code",
156         .start = 0,
157         .end = 0,
158         .flags = IORESOURCE_RAM,
159 };
160 static struct resource bss_resource = {
161         .name = "Kernel bss",
162         .start = 0,
163         .end = 0,
164         .flags = IORESOURCE_RAM,
165 };
166
167 static void __cpuinit early_identify_cpu(struct cpuinfo_x86 *c);
168
169 #ifdef CONFIG_PROC_VMCORE
170 /* elfcorehdr= specifies the location of elf core header
171  * stored by the crashed kernel. This option will be passed
172  * by kexec loader to the capture kernel.
173  */
174 static int __init setup_elfcorehdr(char *arg)
175 {
176         char *end;
177         if (!arg)
178                 return -EINVAL;
179         elfcorehdr_addr = memparse(arg, &end);
180         return end > arg ? 0 : -EINVAL;
181 }
182 early_param("elfcorehdr", setup_elfcorehdr);
183 #endif
184
185 #ifndef CONFIG_NUMA
186 static void __init
187 contig_initmem_init(unsigned long start_pfn, unsigned long end_pfn)
188 {
189         unsigned long bootmap_size, bootmap;
190
191         bootmap_size = bootmem_bootmap_pages(end_pfn)<<PAGE_SHIFT;
192         bootmap = find_e820_area(0, end_pfn<<PAGE_SHIFT, bootmap_size,
193                                  PAGE_SIZE);
194         if (bootmap == -1L)
195                 panic("Cannot find bootmem map of size %ld\n", bootmap_size);
196         bootmap_size = init_bootmem(bootmap >> PAGE_SHIFT, end_pfn);
197         e820_register_active_regions(0, start_pfn, end_pfn);
198         free_bootmem_with_active_regions(0, end_pfn);
199         early_res_to_bootmem(0, end_pfn<<PAGE_SHIFT);
200         reserve_bootmem(bootmap, bootmap_size, BOOTMEM_DEFAULT);
201 }
202 #endif
203
204 #if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
205 struct edd edd;
206 #ifdef CONFIG_EDD_MODULE
207 EXPORT_SYMBOL(edd);
208 #endif
209 /**
210  * copy_edd() - Copy the BIOS EDD information
211  *              from boot_params into a safe place.
212  *
213  */
214 static inline void copy_edd(void)
215 {
216      memcpy(edd.mbr_signature, boot_params.edd_mbr_sig_buffer,
217             sizeof(edd.mbr_signature));
218      memcpy(edd.edd_info, boot_params.eddbuf, sizeof(edd.edd_info));
219      edd.mbr_signature_nr = boot_params.edd_mbr_sig_buf_entries;
220      edd.edd_info_nr = boot_params.eddbuf_entries;
221 }
222 #else
223 static inline void copy_edd(void)
224 {
225 }
226 #endif
227
228 #ifdef CONFIG_KEXEC
229 static void __init reserve_crashkernel(void)
230 {
231         unsigned long long total_mem;
232         unsigned long long crash_size, crash_base;
233         int ret;
234
235         total_mem = ((unsigned long long)max_low_pfn - min_low_pfn) << PAGE_SHIFT;
236
237         ret = parse_crashkernel(boot_command_line, total_mem,
238                         &crash_size, &crash_base);
239         if (ret == 0 && crash_size) {
240                 if (crash_base <= 0) {
241                         printk(KERN_INFO "crashkernel reservation failed - "
242                                         "you have to specify a base address\n");
243                         return;
244                 }
245
246                 if (reserve_bootmem(crash_base, crash_size,
247                                         BOOTMEM_EXCLUSIVE) < 0) {
248                         printk(KERN_INFO "crashkernel reservation failed - "
249                                         "memory is in use\n");
250                         return;
251                 }
252
253                 printk(KERN_INFO "Reserving %ldMB of memory at %ldMB "
254                                 "for crashkernel (System RAM: %ldMB)\n",
255                                 (unsigned long)(crash_size >> 20),
256                                 (unsigned long)(crash_base >> 20),
257                                 (unsigned long)(total_mem >> 20));
258                 crashk_res.start = crash_base;
259                 crashk_res.end   = crash_base + crash_size - 1;
260                 insert_resource(&iomem_resource, &crashk_res);
261         }
262 }
263 #else
264 static inline void __init reserve_crashkernel(void)
265 {}
266 #endif
267
268 /* Overridden in paravirt.c if CONFIG_PARAVIRT */
269 void __attribute__((weak)) __init memory_setup(void)
270 {
271        machine_specific_memory_setup();
272 }
273
274 static void __init parse_setup_data(void)
275 {
276         struct setup_data *data;
277         unsigned long pa_data;
278
279         if (boot_params.hdr.version < 0x0209)
280                 return;
281         pa_data = boot_params.hdr.setup_data;
282         while (pa_data) {
283                 data = early_ioremap(pa_data, PAGE_SIZE);
284                 switch (data->type) {
285                 default:
286                         break;
287                 }
288 #ifndef CONFIG_DEBUG_BOOT_PARAMS
289                 free_early(pa_data, pa_data+sizeof(*data)+data->len);
290 #endif
291                 pa_data = data->next;
292                 early_iounmap(data, PAGE_SIZE);
293         }
294 }
295
296 #ifdef CONFIG_PCI_MMCONFIG
297 extern void __cpuinit fam10h_check_enable_mmcfg(void);
298 extern void __init check_enable_amd_mmconf_dmi(void);
299 #else
300 void __cpuinit fam10h_check_enable_mmcfg(void)
301 {
302 }
303 void __init check_enable_amd_mmconf_dmi(void)
304 {
305 }
306 #endif
307
308 /*
309  * setup_arch - architecture-specific boot-time initializations
310  *
311  * Note: On x86_64, fixmaps are ready for use even before this is called.
312  */
313 void __init setup_arch(char **cmdline_p)
314 {
315         unsigned i;
316
317         printk(KERN_INFO "Command line: %s\n", boot_command_line);
318
319         ROOT_DEV = old_decode_dev(boot_params.hdr.root_dev);
320         screen_info = boot_params.screen_info;
321         edid_info = boot_params.edid_info;
322         saved_video_mode = boot_params.hdr.vid_mode;
323         bootloader_type = boot_params.hdr.type_of_loader;
324
325 #ifdef CONFIG_BLK_DEV_RAM
326         rd_image_start = boot_params.hdr.ram_size & RAMDISK_IMAGE_START_MASK;
327         rd_prompt = ((boot_params.hdr.ram_size & RAMDISK_PROMPT_FLAG) != 0);
328         rd_doload = ((boot_params.hdr.ram_size & RAMDISK_LOAD_FLAG) != 0);
329 #endif
330 #ifdef CONFIG_EFI
331         if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature,
332                      "EL64", 4))
333                 efi_enabled = 1;
334 #endif
335
336         ARCH_SETUP
337
338         memory_setup();
339         copy_edd();
340
341         if (!boot_params.hdr.root_flags)
342                 root_mountflags &= ~MS_RDONLY;
343         init_mm.start_code = (unsigned long) &_text;
344         init_mm.end_code = (unsigned long) &_etext;
345         init_mm.end_data = (unsigned long) &_edata;
346         init_mm.brk = (unsigned long) &_end;
347
348         code_resource.start = virt_to_phys(&_text);
349         code_resource.end = virt_to_phys(&_etext)-1;
350         data_resource.start = virt_to_phys(&_etext);
351         data_resource.end = virt_to_phys(&_edata)-1;
352         bss_resource.start = virt_to_phys(&__bss_start);
353         bss_resource.end = virt_to_phys(&__bss_stop)-1;
354
355         early_identify_cpu(&boot_cpu_data);
356
357         strlcpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
358         *cmdline_p = command_line;
359
360         parse_setup_data();
361
362         parse_early_param();
363
364 #ifdef CONFIG_PROVIDE_OHCI1394_DMA_INIT
365         if (init_ohci1394_dma_early)
366                 init_ohci1394_dma_on_all_controllers();
367 #endif
368
369         finish_e820_parsing();
370
371         /* after parse_early_param, so could debug it */
372         insert_resource(&iomem_resource, &code_resource);
373         insert_resource(&iomem_resource, &data_resource);
374         insert_resource(&iomem_resource, &bss_resource);
375
376         early_gart_iommu_check();
377
378         e820_register_active_regions(0, 0, -1UL);
379         /*
380          * partially used pages are not usable - thus
381          * we are rounding upwards:
382          */
383         end_pfn = e820_end_of_ram();
384         /* update e820 for memory not covered by WB MTRRs */
385         mtrr_bp_init();
386         if (mtrr_trim_uncached_memory(end_pfn)) {
387                 e820_register_active_regions(0, 0, -1UL);
388                 end_pfn = e820_end_of_ram();
389         }
390
391         num_physpages = end_pfn;
392
393         check_efer();
394
395         max_pfn_mapped = init_memory_mapping(0, (max_pfn_mapped << PAGE_SHIFT));
396         if (efi_enabled)
397                 efi_init();
398
399         vsmp_init();
400
401         dmi_scan_machine();
402
403         io_delay_init();
404
405 #ifdef CONFIG_KVM_CLOCK
406         kvmclock_init();
407 #endif
408
409 #ifdef CONFIG_ACPI
410         /*
411          * Initialize the ACPI boot-time table parser (gets the RSDP and SDT).
412          * Call this early for SRAT node setup.
413          */
414         acpi_boot_table_init();
415 #endif
416
417         /* How many end-of-memory variables you have, grandma! */
418         max_low_pfn = end_pfn;
419         max_pfn = end_pfn;
420         high_memory = (void *)__va(end_pfn * PAGE_SIZE - 1) + 1;
421
422         /* Remove active ranges so rediscovery with NUMA-awareness happens */
423         remove_all_active_ranges();
424
425 #ifdef CONFIG_ACPI_NUMA
426         /*
427          * Parse SRAT to discover nodes.
428          */
429         acpi_numa_init();
430 #endif
431
432 #ifdef CONFIG_NUMA
433         numa_initmem_init(0, end_pfn);
434 #else
435         contig_initmem_init(0, end_pfn);
436 #endif
437
438         dma32_reserve_bootmem();
439
440 #ifdef CONFIG_ACPI_SLEEP
441         /*
442          * Reserve low memory region for sleep support.
443          */
444        acpi_reserve_bootmem();
445 #endif
446
447         if (efi_enabled)
448                 efi_reserve_bootmem();
449
450        /*
451         * Find and reserve possible boot-time SMP configuration:
452         */
453         find_smp_config();
454 #ifdef CONFIG_BLK_DEV_INITRD
455         if (boot_params.hdr.type_of_loader && boot_params.hdr.ramdisk_image) {
456                 unsigned long ramdisk_image = boot_params.hdr.ramdisk_image;
457                 unsigned long ramdisk_size  = boot_params.hdr.ramdisk_size;
458                 unsigned long ramdisk_end   = ramdisk_image + ramdisk_size;
459                 unsigned long end_of_mem    = end_pfn << PAGE_SHIFT;
460
461                 if (ramdisk_end <= end_of_mem) {
462                         /*
463                          * don't need to reserve again, already reserved early
464                          * in x86_64_start_kernel, and early_res_to_bootmem
465                          * convert that to reserved in bootmem
466                          */
467                         initrd_start = ramdisk_image + PAGE_OFFSET;
468                         initrd_end = initrd_start+ramdisk_size;
469                 } else {
470                         free_bootmem(ramdisk_image, ramdisk_size);
471                         printk(KERN_ERR "initrd extends beyond end of memory "
472                                "(0x%08lx > 0x%08lx)\ndisabling initrd\n",
473                                ramdisk_end, end_of_mem);
474                         initrd_start = 0;
475                 }
476         }
477 #endif
478         reserve_crashkernel();
479
480         reserve_ibft_region();
481
482         paging_init();
483         map_vsyscall();
484
485         early_quirks();
486
487 #ifdef CONFIG_ACPI
488         /*
489          * Read APIC and some other early information from ACPI tables.
490          */
491         acpi_boot_init();
492 #endif
493
494         init_cpu_to_node();
495
496         /*
497          * get boot-time SMP configuration:
498          */
499         if (smp_found_config)
500                 get_smp_config();
501         init_apic_mappings();
502         ioapic_init_mappings();
503
504         kvm_guest_init();
505
506         /*
507          * We trust e820 completely. No explicit ROM probing in memory.
508          */
509         e820_reserve_resources();
510         e820_mark_nosave_regions();
511
512         /* request I/O space for devices used on all i[345]86 PCs */
513         for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
514                 request_resource(&ioport_resource, &standard_io_resources[i]);
515
516         e820_setup_gap();
517
518 #ifdef CONFIG_VT
519 #if defined(CONFIG_VGA_CONSOLE)
520         if (!efi_enabled || (efi_mem_type(0xa0000) != EFI_CONVENTIONAL_MEMORY))
521                 conswitchp = &vga_con;
522 #elif defined(CONFIG_DUMMY_CONSOLE)
523         conswitchp = &dummy_con;
524 #endif
525 #endif
526
527         /* do this before identify_cpu for boot cpu */
528         check_enable_amd_mmconf_dmi();
529 }
530
531 static int __cpuinit get_model_name(struct cpuinfo_x86 *c)
532 {
533         unsigned int *v;
534
535         if (c->extended_cpuid_level < 0x80000004)
536                 return 0;
537
538         v = (unsigned int *) c->x86_model_id;
539         cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
540         cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
541         cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
542         c->x86_model_id[48] = 0;
543         return 1;
544 }
545
546
547 static void __cpuinit display_cacheinfo(struct cpuinfo_x86 *c)
548 {
549         unsigned int n, dummy, eax, ebx, ecx, edx;
550
551         n = c->extended_cpuid_level;
552
553         if (n >= 0x80000005) {
554                 cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
555                 printk(KERN_INFO "CPU: L1 I Cache: %dK (%d bytes/line), "
556                        "D cache %dK (%d bytes/line)\n",
557                        edx>>24, edx&0xFF, ecx>>24, ecx&0xFF);
558                 c->x86_cache_size = (ecx>>24) + (edx>>24);
559                 /* On K8 L1 TLB is inclusive, so don't count it */
560                 c->x86_tlbsize = 0;
561         }
562
563         if (n >= 0x80000006) {
564                 cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
565                 ecx = cpuid_ecx(0x80000006);
566                 c->x86_cache_size = ecx >> 16;
567                 c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
568
569                 printk(KERN_INFO "CPU: L2 Cache: %dK (%d bytes/line)\n",
570                 c->x86_cache_size, ecx & 0xFF);
571         }
572         if (n >= 0x80000008) {
573                 cpuid(0x80000008, &eax, &dummy, &dummy, &dummy);
574                 c->x86_virt_bits = (eax >> 8) & 0xff;
575                 c->x86_phys_bits = eax & 0xff;
576         }
577 }
578
579 #ifdef CONFIG_NUMA
580 static int __cpuinit nearby_node(int apicid)
581 {
582         int i, node;
583
584         for (i = apicid - 1; i >= 0; i--) {
585                 node = apicid_to_node[i];
586                 if (node != NUMA_NO_NODE && node_online(node))
587                         return node;
588         }
589         for (i = apicid + 1; i < MAX_LOCAL_APIC; i++) {
590                 node = apicid_to_node[i];
591                 if (node != NUMA_NO_NODE && node_online(node))
592                         return node;
593         }
594         return first_node(node_online_map); /* Shouldn't happen */
595 }
596 #endif
597
598 /*
599  * On a AMD dual core setup the lower bits of the APIC id distingush the cores.
600  * Assumes number of cores is a power of two.
601  */
602 static void __cpuinit amd_detect_cmp(struct cpuinfo_x86 *c)
603 {
604 #ifdef CONFIG_SMP
605         unsigned bits;
606 #ifdef CONFIG_NUMA
607         int cpu = smp_processor_id();
608         int node = 0;
609         unsigned apicid = hard_smp_processor_id();
610 #endif
611         bits = c->x86_coreid_bits;
612
613         /* Low order bits define the core id (index of core in socket) */
614         c->cpu_core_id = c->initial_apicid & ((1 << bits)-1);
615         /* Convert the initial APIC ID into the socket ID */
616         c->phys_proc_id = c->initial_apicid >> bits;
617
618 #ifdef CONFIG_NUMA
619         node = c->phys_proc_id;
620         if (apicid_to_node[apicid] != NUMA_NO_NODE)
621                 node = apicid_to_node[apicid];
622         if (!node_online(node)) {
623                 /* Two possibilities here:
624                    - The CPU is missing memory and no node was created.
625                    In that case try picking one from a nearby CPU
626                    - The APIC IDs differ from the HyperTransport node IDs
627                    which the K8 northbridge parsing fills in.
628                    Assume they are all increased by a constant offset,
629                    but in the same order as the HT nodeids.
630                    If that doesn't result in a usable node fall back to the
631                    path for the previous case.  */
632
633                 int ht_nodeid = c->initial_apicid;
634
635                 if (ht_nodeid >= 0 &&
636                     apicid_to_node[ht_nodeid] != NUMA_NO_NODE)
637                         node = apicid_to_node[ht_nodeid];
638                 /* Pick a nearby node */
639                 if (!node_online(node))
640                         node = nearby_node(apicid);
641         }
642         numa_set_node(cpu, node);
643
644         printk(KERN_INFO "CPU %d/%x -> Node %d\n", cpu, apicid, node);
645 #endif
646 #endif
647 }
648
649 static void __cpuinit early_init_amd_mc(struct cpuinfo_x86 *c)
650 {
651 #ifdef CONFIG_SMP
652         unsigned bits, ecx;
653
654         /* Multi core CPU? */
655         if (c->extended_cpuid_level < 0x80000008)
656                 return;
657
658         ecx = cpuid_ecx(0x80000008);
659
660         c->x86_max_cores = (ecx & 0xff) + 1;
661
662         /* CPU telling us the core id bits shift? */
663         bits = (ecx >> 12) & 0xF;
664
665         /* Otherwise recompute */
666         if (bits == 0) {
667                 while ((1 << bits) < c->x86_max_cores)
668                         bits++;
669         }
670
671         c->x86_coreid_bits = bits;
672
673 #endif
674 }
675
676 #define ENABLE_C1E_MASK         0x18000000
677 #define CPUID_PROCESSOR_SIGNATURE       1
678 #define CPUID_XFAM              0x0ff00000
679 #define CPUID_XFAM_K8           0x00000000
680 #define CPUID_XFAM_10H          0x00100000
681 #define CPUID_XFAM_11H          0x00200000
682 #define CPUID_XMOD              0x000f0000
683 #define CPUID_XMOD_REV_F        0x00040000
684
685 /* AMD systems with C1E don't have a working lAPIC timer. Check for that. */
686 static __cpuinit int amd_apic_timer_broken(void)
687 {
688         u32 lo, hi, eax = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
689
690         switch (eax & CPUID_XFAM) {
691         case CPUID_XFAM_K8:
692                 if ((eax & CPUID_XMOD) < CPUID_XMOD_REV_F)
693                         break;
694         case CPUID_XFAM_10H:
695         case CPUID_XFAM_11H:
696                 rdmsr(MSR_K8_ENABLE_C1E, lo, hi);
697                 if (lo & ENABLE_C1E_MASK)
698                         return 1;
699                 break;
700         default:
701                 /* err on the side of caution */
702                 return 1;
703         }
704         return 0;
705 }
706
707 static void __cpuinit early_init_amd(struct cpuinfo_x86 *c)
708 {
709         early_init_amd_mc(c);
710
711         /* c->x86_power is 8000_0007 edx. Bit 8 is constant TSC */
712         if (c->x86_power & (1<<8))
713                 set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
714 }
715
716 static void __cpuinit init_amd(struct cpuinfo_x86 *c)
717 {
718         unsigned level;
719
720 #ifdef CONFIG_SMP
721         unsigned long value;
722
723         /*
724          * Disable TLB flush filter by setting HWCR.FFDIS on K8
725          * bit 6 of msr C001_0015
726          *
727          * Errata 63 for SH-B3 steppings
728          * Errata 122 for all steppings (F+ have it disabled by default)
729          */
730         if (c->x86 == 15) {
731                 rdmsrl(MSR_K8_HWCR, value);
732                 value |= 1 << 6;
733                 wrmsrl(MSR_K8_HWCR, value);
734         }
735 #endif
736
737         /* Bit 31 in normal CPUID used for nonstandard 3DNow ID;
738            3DNow is IDd by bit 31 in extended CPUID (1*32+31) anyway */
739         clear_cpu_cap(c, 0*32+31);
740
741         /* On C+ stepping K8 rep microcode works well for copy/memset */
742         level = cpuid_eax(1);
743         if (c->x86 == 15 && ((level >= 0x0f48 && level < 0x0f50) ||
744                              level >= 0x0f58))
745                 set_cpu_cap(c, X86_FEATURE_REP_GOOD);
746         if (c->x86 == 0x10 || c->x86 == 0x11)
747                 set_cpu_cap(c, X86_FEATURE_REP_GOOD);
748
749         /* Enable workaround for FXSAVE leak */
750         if (c->x86 >= 6)
751                 set_cpu_cap(c, X86_FEATURE_FXSAVE_LEAK);
752
753         level = get_model_name(c);
754         if (!level) {
755                 switch (c->x86) {
756                 case 15:
757                         /* Should distinguish Models here, but this is only
758                            a fallback anyways. */
759                         strcpy(c->x86_model_id, "Hammer");
760                         break;
761                 }
762         }
763         display_cacheinfo(c);
764
765         /* Multi core CPU? */
766         if (c->extended_cpuid_level >= 0x80000008)
767                 amd_detect_cmp(c);
768
769         if (c->extended_cpuid_level >= 0x80000006 &&
770                 (cpuid_edx(0x80000006) & 0xf000))
771                 num_cache_leaves = 4;
772         else
773                 num_cache_leaves = 3;
774
775         if (c->x86 == 0xf || c->x86 == 0x10 || c->x86 == 0x11)
776                 set_cpu_cap(c, X86_FEATURE_K8);
777
778         /* MFENCE stops RDTSC speculation */
779         set_cpu_cap(c, X86_FEATURE_MFENCE_RDTSC);
780
781         if (c->x86 == 0x10)
782                 fam10h_check_enable_mmcfg();
783
784         if (amd_apic_timer_broken())
785                 disable_apic_timer = 1;
786
787         if (c == &boot_cpu_data && c->x86 >= 0xf && c->x86 <= 0x11) {
788                 unsigned long long tseg;
789
790                 /*
791                  * Split up direct mapping around the TSEG SMM area.
792                  * Don't do it for gbpages because there seems very little
793                  * benefit in doing so.
794                  */
795                 if (!rdmsrl_safe(MSR_K8_TSEG_ADDR, &tseg) &&
796                 (tseg >> PMD_SHIFT) < (max_pfn_mapped >> (PMD_SHIFT-PAGE_SHIFT)))
797                         set_memory_4k((unsigned long)__va(tseg), 1);
798         }
799 }
800
801 void __cpuinit detect_ht(struct cpuinfo_x86 *c)
802 {
803 #ifdef CONFIG_SMP
804         u32 eax, ebx, ecx, edx;
805         int index_msb, core_bits;
806
807         cpuid(1, &eax, &ebx, &ecx, &edx);
808
809
810         if (!cpu_has(c, X86_FEATURE_HT))
811                 return;
812         if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
813                 goto out;
814
815         smp_num_siblings = (ebx & 0xff0000) >> 16;
816
817         if (smp_num_siblings == 1) {
818                 printk(KERN_INFO  "CPU: Hyper-Threading is disabled\n");
819         } else if (smp_num_siblings > 1) {
820
821                 if (smp_num_siblings > NR_CPUS) {
822                         printk(KERN_WARNING "CPU: Unsupported number of "
823                                "siblings %d", smp_num_siblings);
824                         smp_num_siblings = 1;
825                         return;
826                 }
827
828                 index_msb = get_count_order(smp_num_siblings);
829                 c->phys_proc_id = phys_pkg_id(index_msb);
830
831                 smp_num_siblings = smp_num_siblings / c->x86_max_cores;
832
833                 index_msb = get_count_order(smp_num_siblings);
834
835                 core_bits = get_count_order(c->x86_max_cores);
836
837                 c->cpu_core_id = phys_pkg_id(index_msb) &
838                                                ((1 << core_bits) - 1);
839         }
840 out:
841         if ((c->x86_max_cores * smp_num_siblings) > 1) {
842                 printk(KERN_INFO  "CPU: Physical Processor ID: %d\n",
843                        c->phys_proc_id);
844                 printk(KERN_INFO  "CPU: Processor Core ID: %d\n",
845                        c->cpu_core_id);
846         }
847
848 #endif
849 }
850
851 /*
852  * find out the number of processor cores on the die
853  */
854 static int __cpuinit intel_num_cpu_cores(struct cpuinfo_x86 *c)
855 {
856         unsigned int eax, t;
857
858         if (c->cpuid_level < 4)
859                 return 1;
860
861         cpuid_count(4, 0, &eax, &t, &t, &t);
862
863         if (eax & 0x1f)
864                 return ((eax >> 26) + 1);
865         else
866                 return 1;
867 }
868
869 static void __cpuinit srat_detect_node(void)
870 {
871 #ifdef CONFIG_NUMA
872         unsigned node;
873         int cpu = smp_processor_id();
874         int apicid = hard_smp_processor_id();
875
876         /* Don't do the funky fallback heuristics the AMD version employs
877            for now. */
878         node = apicid_to_node[apicid];
879         if (node == NUMA_NO_NODE || !node_online(node))
880                 node = first_node(node_online_map);
881         numa_set_node(cpu, node);
882
883         printk(KERN_INFO "CPU %d/%x -> Node %d\n", cpu, apicid, node);
884 #endif
885 }
886
887 static void __cpuinit early_init_intel(struct cpuinfo_x86 *c)
888 {
889         if ((c->x86 == 0xf && c->x86_model >= 0x03) ||
890             (c->x86 == 0x6 && c->x86_model >= 0x0e))
891                 set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
892 }
893
894 static void __cpuinit init_intel(struct cpuinfo_x86 *c)
895 {
896         /* Cache sizes */
897         unsigned n;
898
899         init_intel_cacheinfo(c);
900         if (c->cpuid_level > 9) {
901                 unsigned eax = cpuid_eax(10);
902                 /* Check for version and the number of counters */
903                 if ((eax & 0xff) && (((eax>>8) & 0xff) > 1))
904                         set_cpu_cap(c, X86_FEATURE_ARCH_PERFMON);
905         }
906
907         if (cpu_has_ds) {
908                 unsigned int l1, l2;
909                 rdmsr(MSR_IA32_MISC_ENABLE, l1, l2);
910                 if (!(l1 & (1<<11)))
911                         set_cpu_cap(c, X86_FEATURE_BTS);
912                 if (!(l1 & (1<<12)))
913                         set_cpu_cap(c, X86_FEATURE_PEBS);
914         }
915
916
917         if (cpu_has_bts)
918                 ds_init_intel(c);
919
920         n = c->extended_cpuid_level;
921         if (n >= 0x80000008) {
922                 unsigned eax = cpuid_eax(0x80000008);
923                 c->x86_virt_bits = (eax >> 8) & 0xff;
924                 c->x86_phys_bits = eax & 0xff;
925                 /* CPUID workaround for Intel 0F34 CPU */
926                 if (c->x86_vendor == X86_VENDOR_INTEL &&
927                     c->x86 == 0xF && c->x86_model == 0x3 &&
928                     c->x86_mask == 0x4)
929                         c->x86_phys_bits = 36;
930         }
931
932         if (c->x86 == 15)
933                 c->x86_cache_alignment = c->x86_clflush_size * 2;
934         if (c->x86 == 6)
935                 set_cpu_cap(c, X86_FEATURE_REP_GOOD);
936         set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC);
937         c->x86_max_cores = intel_num_cpu_cores(c);
938
939         srat_detect_node();
940 }
941
942 static void __cpuinit early_init_centaur(struct cpuinfo_x86 *c)
943 {
944         if (c->x86 == 0x6 && c->x86_model >= 0xf)
945                 set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
946 }
947
948 static void __cpuinit init_centaur(struct cpuinfo_x86 *c)
949 {
950         /* Cache sizes */
951         unsigned n;
952
953         n = c->extended_cpuid_level;
954         if (n >= 0x80000008) {
955                 unsigned eax = cpuid_eax(0x80000008);
956                 c->x86_virt_bits = (eax >> 8) & 0xff;
957                 c->x86_phys_bits = eax & 0xff;
958         }
959
960         if (c->x86 == 0x6 && c->x86_model >= 0xf) {
961                 c->x86_cache_alignment = c->x86_clflush_size * 2;
962                 set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
963                 set_cpu_cap(c, X86_FEATURE_REP_GOOD);
964         }
965         set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC);
966 }
967
968 static void __cpuinit get_cpu_vendor(struct cpuinfo_x86 *c)
969 {
970         char *v = c->x86_vendor_id;
971
972         if (!strcmp(v, "AuthenticAMD"))
973                 c->x86_vendor = X86_VENDOR_AMD;
974         else if (!strcmp(v, "GenuineIntel"))
975                 c->x86_vendor = X86_VENDOR_INTEL;
976         else if (!strcmp(v, "CentaurHauls"))
977                 c->x86_vendor = X86_VENDOR_CENTAUR;
978         else
979                 c->x86_vendor = X86_VENDOR_UNKNOWN;
980 }
981
982 /* Do some early cpuid on the boot CPU to get some parameter that are
983    needed before check_bugs. Everything advanced is in identify_cpu
984    below. */
985 static void __cpuinit early_identify_cpu(struct cpuinfo_x86 *c)
986 {
987         u32 tfms, xlvl;
988
989         c->loops_per_jiffy = loops_per_jiffy;
990         c->x86_cache_size = -1;
991         c->x86_vendor = X86_VENDOR_UNKNOWN;
992         c->x86_model = c->x86_mask = 0; /* So far unknown... */
993         c->x86_vendor_id[0] = '\0'; /* Unset */
994         c->x86_model_id[0] = '\0';  /* Unset */
995         c->x86_clflush_size = 64;
996         c->x86_cache_alignment = c->x86_clflush_size;
997         c->x86_max_cores = 1;
998         c->x86_coreid_bits = 0;
999         c->extended_cpuid_level = 0;
1000         memset(&c->x86_capability, 0, sizeof c->x86_capability);
1001
1002         /* Get vendor name */
1003         cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
1004               (unsigned int *)&c->x86_vendor_id[0],
1005               (unsigned int *)&c->x86_vendor_id[8],
1006               (unsigned int *)&c->x86_vendor_id[4]);
1007
1008         get_cpu_vendor(c);
1009
1010         /* Initialize the standard set of capabilities */
1011         /* Note that the vendor-specific code below might override */
1012
1013         /* Intel-defined flags: level 0x00000001 */
1014         if (c->cpuid_level >= 0x00000001) {
1015                 __u32 misc;
1016                 cpuid(0x00000001, &tfms, &misc, &c->x86_capability[4],
1017                       &c->x86_capability[0]);
1018                 c->x86 = (tfms >> 8) & 0xf;
1019                 c->x86_model = (tfms >> 4) & 0xf;
1020                 c->x86_mask = tfms & 0xf;
1021                 if (c->x86 == 0xf)
1022                         c->x86 += (tfms >> 20) & 0xff;
1023                 if (c->x86 >= 0x6)
1024                         c->x86_model += ((tfms >> 16) & 0xF) << 4;
1025                 if (test_cpu_cap(c, X86_FEATURE_CLFLSH))
1026                         c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
1027         } else {
1028                 /* Have CPUID level 0 only - unheard of */
1029                 c->x86 = 4;
1030         }
1031
1032         c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xff;
1033 #ifdef CONFIG_SMP
1034         c->phys_proc_id = c->initial_apicid;
1035 #endif
1036         /* AMD-defined flags: level 0x80000001 */
1037         xlvl = cpuid_eax(0x80000000);
1038         c->extended_cpuid_level = xlvl;
1039         if ((xlvl & 0xffff0000) == 0x80000000) {
1040                 if (xlvl >= 0x80000001) {
1041                         c->x86_capability[1] = cpuid_edx(0x80000001);
1042                         c->x86_capability[6] = cpuid_ecx(0x80000001);
1043                 }
1044                 if (xlvl >= 0x80000004)
1045                         get_model_name(c); /* Default name */
1046         }
1047
1048         /* Transmeta-defined flags: level 0x80860001 */
1049         xlvl = cpuid_eax(0x80860000);
1050         if ((xlvl & 0xffff0000) == 0x80860000) {
1051                 /* Don't set x86_cpuid_level here for now to not confuse. */
1052                 if (xlvl >= 0x80860001)
1053                         c->x86_capability[2] = cpuid_edx(0x80860001);
1054         }
1055
1056         c->extended_cpuid_level = cpuid_eax(0x80000000);
1057         if (c->extended_cpuid_level >= 0x80000007)
1058                 c->x86_power = cpuid_edx(0x80000007);
1059
1060         switch (c->x86_vendor) {
1061         case X86_VENDOR_AMD:
1062                 early_init_amd(c);
1063                 break;
1064         case X86_VENDOR_INTEL:
1065                 early_init_intel(c);
1066                 break;
1067         case X86_VENDOR_CENTAUR:
1068                 early_init_centaur(c);
1069                 break;
1070         }
1071
1072         validate_pat_support(c);
1073 }
1074
1075 /*
1076  * This does the hard work of actually picking apart the CPU stuff...
1077  */
1078 void __cpuinit identify_cpu(struct cpuinfo_x86 *c)
1079 {
1080         int i;
1081
1082         early_identify_cpu(c);
1083
1084         init_scattered_cpuid_features(c);
1085
1086         c->apicid = phys_pkg_id(0);
1087
1088         /*
1089          * Vendor-specific initialization.  In this section we
1090          * canonicalize the feature flags, meaning if there are
1091          * features a certain CPU supports which CPUID doesn't
1092          * tell us, CPUID claiming incorrect flags, or other bugs,
1093          * we handle them here.
1094          *
1095          * At the end of this section, c->x86_capability better
1096          * indicate the features this CPU genuinely supports!
1097          */
1098         switch (c->x86_vendor) {
1099         case X86_VENDOR_AMD:
1100                 init_amd(c);
1101                 break;
1102
1103         case X86_VENDOR_INTEL:
1104                 init_intel(c);
1105                 break;
1106
1107         case X86_VENDOR_CENTAUR:
1108                 init_centaur(c);
1109                 break;
1110
1111         case X86_VENDOR_UNKNOWN:
1112         default:
1113                 display_cacheinfo(c);
1114                 break;
1115         }
1116
1117         detect_ht(c);
1118
1119         /*
1120          * On SMP, boot_cpu_data holds the common feature set between
1121          * all CPUs; so make sure that we indicate which features are
1122          * common between the CPUs.  The first time this routine gets
1123          * executed, c == &boot_cpu_data.
1124          */
1125         if (c != &boot_cpu_data) {
1126                 /* AND the already accumulated flags with these */
1127                 for (i = 0; i < NCAPINTS; i++)
1128                         boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
1129         }
1130
1131         /* Clear all flags overriden by options */
1132         for (i = 0; i < NCAPINTS; i++)
1133                 c->x86_capability[i] &= ~cleared_cpu_caps[i];
1134
1135 #ifdef CONFIG_X86_MCE
1136         mcheck_init(c);
1137 #endif
1138         select_idle_routine(c);
1139
1140 #ifdef CONFIG_NUMA
1141         numa_add_cpu(smp_processor_id());
1142 #endif
1143
1144 }
1145
1146 void __cpuinit identify_boot_cpu(void)
1147 {
1148         identify_cpu(&boot_cpu_data);
1149 }
1150
1151 void __cpuinit identify_secondary_cpu(struct cpuinfo_x86 *c)
1152 {
1153         BUG_ON(c == &boot_cpu_data);
1154         identify_cpu(c);
1155         mtrr_ap_init();
1156 }
1157
1158 static __init int setup_noclflush(char *arg)
1159 {
1160         setup_clear_cpu_cap(X86_FEATURE_CLFLSH);
1161         return 1;
1162 }
1163 __setup("noclflush", setup_noclflush);
1164
1165 void __cpuinit print_cpu_info(struct cpuinfo_x86 *c)
1166 {
1167         if (c->x86_model_id[0])
1168                 printk(KERN_CONT "%s", c->x86_model_id);
1169
1170         if (c->x86_mask || c->cpuid_level >= 0)
1171                 printk(KERN_CONT " stepping %02x\n", c->x86_mask);
1172         else
1173                 printk(KERN_CONT "\n");
1174 }
1175
1176 static __init int setup_disablecpuid(char *arg)
1177 {
1178         int bit;
1179         if (get_option(&arg, &bit) && bit < NCAPINTS*32)
1180                 setup_clear_cpu_cap(bit);
1181         else
1182                 return 0;
1183         return 1;
1184 }
1185 __setup("clearcpuid=", setup_disablecpuid);