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