Kevin O'Connor | 84ad59a | 2008-07-04 05:47:26 -0400 | [diff] [blame] | 1 | // CPU count detection |
| 2 | // |
| 3 | // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net> |
| 4 | // Copyright (C) 2006 Fabrice Bellard |
| 5 | // |
| 6 | // This file may be distributed under the terms of the GNU GPLv3 license. |
| 7 | |
| 8 | #include "util.h" // dprintf |
Kevin O'Connor | 9521e26 | 2008-07-04 13:04:29 -0400 | [diff] [blame] | 9 | #include "config.h" // CONFIG_* |
Kevin O'Connor | 31bfad6 | 2008-12-16 23:50:52 -0500 | [diff] [blame] | 10 | #include "cmos.h" // CMOS_BIOS_SMP_COUNT |
Kevin O'Connor | 84ad59a | 2008-07-04 05:47:26 -0400 | [diff] [blame] | 11 | |
| 12 | #define CPUID_APIC (1 << 9) |
| 13 | |
Kevin O'Connor | f5c1161 | 2008-12-14 10:11:45 -0500 | [diff] [blame] | 14 | #define APIC_ICR_LOW ((u8*)BUILD_APIC_ADDR + 0x300) |
| 15 | #define APIC_SVR ((u8*)BUILD_APIC_ADDR + 0x0F0) |
Kevin O'Connor | 84ad59a | 2008-07-04 05:47:26 -0400 | [diff] [blame] | 16 | |
| 17 | #define APIC_ENABLED 0x0100 |
| 18 | |
| 19 | static inline void writel(void *addr, u32 val) |
| 20 | { |
| 21 | *(volatile u32 *)addr = val; |
| 22 | } |
| 23 | |
| 24 | static inline void writew(void *addr, u16 val) |
| 25 | { |
| 26 | *(volatile u16 *)addr = val; |
| 27 | } |
| 28 | |
| 29 | static inline void writeb(void *addr, u8 val) |
| 30 | { |
| 31 | *(volatile u8 *)addr = val; |
| 32 | } |
| 33 | |
| 34 | static inline u32 readl(const void *addr) |
| 35 | { |
| 36 | return *(volatile const u32 *)addr; |
| 37 | } |
| 38 | |
| 39 | static inline u16 readw(const void *addr) |
| 40 | { |
| 41 | return *(volatile const u16 *)addr; |
| 42 | } |
| 43 | |
| 44 | static inline u8 readb(const void *addr) |
| 45 | { |
| 46 | return *(volatile const u8 *)addr; |
| 47 | } |
| 48 | |
Kevin O'Connor | 4a754b3 | 2008-12-28 21:37:27 -0500 | [diff] [blame^] | 49 | u32 smp_cpus VAR16; |
Kevin O'Connor | a06bfb6 | 2008-12-06 19:37:56 -0500 | [diff] [blame] | 50 | extern void smp_ap_boot_code(); |
Kevin O'Connor | 4a754b3 | 2008-12-28 21:37:27 -0500 | [diff] [blame^] | 51 | ASM16( |
Kevin O'Connor | a06bfb6 | 2008-12-06 19:37:56 -0500 | [diff] [blame] | 52 | " .global smp_ap_boot_code\n" |
| 53 | "smp_ap_boot_code:\n" |
Kevin O'Connor | 9649a96 | 2008-12-10 20:53:35 -0500 | [diff] [blame] | 54 | // Increment the cpu counter |
Kevin O'Connor | a06bfb6 | 2008-12-06 19:37:56 -0500 | [diff] [blame] | 55 | " movw $" __stringify(SEG_BIOS) ", %ax\n" |
Kevin O'Connor | 484270d | 2008-08-17 10:50:57 -0400 | [diff] [blame] | 56 | " movw %ax, %ds\n" |
Kevin O'Connor | a06bfb6 | 2008-12-06 19:37:56 -0500 | [diff] [blame] | 57 | " lock incl smp_cpus\n" |
Kevin O'Connor | 484270d | 2008-08-17 10:50:57 -0400 | [diff] [blame] | 58 | // Halt the processor. |
Kevin O'Connor | a06bfb6 | 2008-12-06 19:37:56 -0500 | [diff] [blame] | 59 | " jmp permanent_halt\n" |
Kevin O'Connor | 84ad59a | 2008-07-04 05:47:26 -0400 | [diff] [blame] | 60 | ); |
Kevin O'Connor | 84ad59a | 2008-07-04 05:47:26 -0400 | [diff] [blame] | 61 | |
| 62 | /* find the number of CPUs by launching a SIPI to them */ |
| 63 | int |
| 64 | smp_probe(void) |
| 65 | { |
| 66 | if (smp_cpus) |
| 67 | return smp_cpus; |
| 68 | |
Kevin O'Connor | 84ad59a | 2008-07-04 05:47:26 -0400 | [diff] [blame] | 69 | u32 eax, ebx, ecx, cpuid_features; |
| 70 | cpuid(1, &eax, &ebx, &ecx, &cpuid_features); |
Kevin O'Connor | a06bfb6 | 2008-12-06 19:37:56 -0500 | [diff] [blame] | 71 | if (! (cpuid_features & CPUID_APIC)) { |
| 72 | // No apic - only the main cpu is present. |
| 73 | smp_cpus = 1; |
| 74 | return 1; |
Kevin O'Connor | 84ad59a | 2008-07-04 05:47:26 -0400 | [diff] [blame] | 75 | } |
Kevin O'Connor | 84ad59a | 2008-07-04 05:47:26 -0400 | [diff] [blame] | 76 | |
Kevin O'Connor | a06bfb6 | 2008-12-06 19:37:56 -0500 | [diff] [blame] | 77 | // Init the counter. |
| 78 | writel(&smp_cpus, 1); |
| 79 | |
| 80 | // Setup jump trampoline to counter code. |
| 81 | u64 old = *(u64*)BUILD_AP_BOOT_ADDR; |
| 82 | // ljmpw $SEG_BIOS, $(smp_ap_boot_code - BUILD_BIOS_ADDR) |
| 83 | u64 new = (0xea | ((u64)SEG_BIOS<<24) |
| 84 | | (((u32)smp_ap_boot_code - BUILD_BIOS_ADDR) << 8)); |
| 85 | *(u64*)BUILD_AP_BOOT_ADDR = new; |
| 86 | |
| 87 | // enable local APIC |
Kevin O'Connor | f5c1161 | 2008-12-14 10:11:45 -0500 | [diff] [blame] | 88 | u32 val = readl(APIC_SVR); |
| 89 | writel(APIC_SVR, val | APIC_ENABLED); |
Kevin O'Connor | a06bfb6 | 2008-12-06 19:37:56 -0500 | [diff] [blame] | 90 | |
| 91 | // broadcast SIPI |
Kevin O'Connor | f5c1161 | 2008-12-14 10:11:45 -0500 | [diff] [blame] | 92 | writel(APIC_ICR_LOW, 0x000C4500); |
Kevin O'Connor | a06bfb6 | 2008-12-06 19:37:56 -0500 | [diff] [blame] | 93 | u32 sipi_vector = BUILD_AP_BOOT_ADDR >> 12; |
Kevin O'Connor | f5c1161 | 2008-12-14 10:11:45 -0500 | [diff] [blame] | 94 | writel(APIC_ICR_LOW, 0x000C4600 | sipi_vector); |
Kevin O'Connor | a06bfb6 | 2008-12-06 19:37:56 -0500 | [diff] [blame] | 95 | |
| 96 | // Wait for other CPUs to process the SIPI. |
Kevin O'Connor | 31bfad6 | 2008-12-16 23:50:52 -0500 | [diff] [blame] | 97 | if (CONFIG_COREBOOT) |
| 98 | mdelay(10); |
| 99 | else |
| 100 | while (inb_cmos(CMOS_BIOS_SMP_COUNT) + 1 != readl(&smp_cpus)) |
| 101 | ; |
Kevin O'Connor | a06bfb6 | 2008-12-06 19:37:56 -0500 | [diff] [blame] | 102 | |
| 103 | // Restore memory. |
Kevin O'Connor | a06bfb6 | 2008-12-06 19:37:56 -0500 | [diff] [blame] | 104 | *(u64*)BUILD_AP_BOOT_ADDR = old; |
| 105 | |
| 106 | u32 count = readl(&smp_cpus); |
| 107 | dprintf(1, "Found %d cpu(s)\n", count); |
| 108 | return count; |
Kevin O'Connor | 84ad59a | 2008-07-04 05:47:26 -0400 | [diff] [blame] | 109 | } |
Kevin O'Connor | acf1374 | 2008-11-29 11:19:19 -0500 | [diff] [blame] | 110 | |
| 111 | // Reset smp_cpus to zero (forces a recheck on reboots). |
| 112 | void |
| 113 | smp_probe_setup(void) |
| 114 | { |
| 115 | smp_cpus = 0; |
| 116 | } |