Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 James Cleverdon, IBM. |
| 3 | * Subject to the GNU Public License, v.2 |
| 4 | * |
| 5 | * Generic APIC sub-arch probe layer. |
| 6 | * |
| 7 | * Hacked for x86-64 by James Cleverdon from i386 architecture code by |
| 8 | * Martin Bligh, Andi Kleen, James Bottomley, John Stultz, and |
| 9 | * James Cleverdon. |
| 10 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/threads.h> |
| 12 | #include <linux/cpumask.h> |
| 13 | #include <linux/string.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/ctype.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/module.h> |
| 18 | |
| 19 | #include <asm/smp.h> |
| 20 | #include <asm/ipi.h> |
| 21 | |
Len Brown | 76f5858 | 2005-08-24 12:10:49 -0400 | [diff] [blame] | 22 | #if defined(CONFIG_ACPI) |
Jason Davis | 90660ec | 2005-04-16 15:24:53 -0700 | [diff] [blame] | 23 | #include <acpi/acpi_bus.h> |
| 24 | #endif |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | /* which logical CPU number maps to which CPU (physical APIC ID) */ |
Ravikiran G Thirumalai | 6c231b7 | 2005-09-06 15:17:45 -0700 | [diff] [blame] | 27 | u8 x86_cpu_to_apicid[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = BAD_APICID }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | EXPORT_SYMBOL(x86_cpu_to_apicid); |
| 29 | u8 x86_cpu_to_log_apicid[NR_CPUS] = { [0 ... NR_CPUS-1] = BAD_APICID }; |
| 30 | |
| 31 | extern struct genapic apic_cluster; |
| 32 | extern struct genapic apic_flat; |
Andi Kleen | f8d3119 | 2005-07-28 21:15:42 -0700 | [diff] [blame] | 33 | extern struct genapic apic_physflat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | struct genapic *genapic = &apic_flat; |
Siddha, Suresh B | 9899f82 | 2006-12-07 02:14:10 +0100 | [diff] [blame] | 36 | struct genapic *genapic_force; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * Check the APIC IDs in bios_cpu_apicid and choose the APIC mode. |
| 40 | */ |
| 41 | void __init clustered_apic_check(void) |
| 42 | { |
| 43 | long i; |
| 44 | u8 clusters, max_cluster; |
| 45 | u8 id; |
| 46 | u8 cluster_cnt[NUM_APIC_CLUSTERS]; |
Andi Kleen | 7055646 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 47 | int max_apic = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Siddha, Suresh B | 9899f82 | 2006-12-07 02:14:10 +0100 | [diff] [blame] | 49 | /* genapic selection can be forced because of certain quirks. |
| 50 | */ |
| 51 | if (genapic_force) { |
| 52 | genapic = genapic_force; |
| 53 | goto print; |
| 54 | } |
| 55 | |
Len Brown | 76f5858 | 2005-08-24 12:10:49 -0400 | [diff] [blame] | 56 | #if defined(CONFIG_ACPI) |
Jason Davis | 90660ec | 2005-04-16 15:24:53 -0700 | [diff] [blame] | 57 | /* |
| 58 | * Some x86_64 machines use physical APIC mode regardless of how many |
| 59 | * procs/clusters are present (x86_64 ES7000 is an example). |
| 60 | */ |
Alexey Starikovskiy | cee324b | 2007-02-02 19:48:22 +0300 | [diff] [blame^] | 61 | if (acpi_gbl_FADT.header.revision > FADT2_REVISION_ID) |
| 62 | if (acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL) { |
Jason Davis | 90660ec | 2005-04-16 15:24:53 -0700 | [diff] [blame] | 63 | genapic = &apic_cluster; |
| 64 | goto print; |
| 65 | } |
| 66 | #endif |
| 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | memset(cluster_cnt, 0, sizeof(cluster_cnt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | for (i = 0; i < NR_CPUS; i++) { |
| 70 | id = bios_cpu_apicid[i]; |
Andi Kleen | f8d3119 | 2005-07-28 21:15:42 -0700 | [diff] [blame] | 71 | if (id == BAD_APICID) |
| 72 | continue; |
Andi Kleen | 7055646 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 73 | if (id > max_apic) |
| 74 | max_apic = id; |
Andi Kleen | f8d3119 | 2005-07-28 21:15:42 -0700 | [diff] [blame] | 75 | cluster_cnt[APIC_CLUSTERID(id)]++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Andi Kleen | f8d3119 | 2005-07-28 21:15:42 -0700 | [diff] [blame] | 78 | /* Don't use clustered mode on AMD platforms. */ |
| 79 | if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) { |
| 80 | genapic = &apic_physflat; |
Andi Kleen | 016102d | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 81 | #ifndef CONFIG_HOTPLUG_CPU |
Andi Kleen | f8d3119 | 2005-07-28 21:15:42 -0700 | [diff] [blame] | 82 | /* In the CPU hotplug case we cannot use broadcast mode |
| 83 | because that opens a race when a CPU is removed. |
| 84 | Stay at physflat mode in this case. |
| 85 | It is bad to do this unconditionally though. Once |
| 86 | we have ACPI platform support for CPU hotplug |
| 87 | we should detect hotplug capablity from ACPI tables and |
| 88 | only do this when really needed. -AK */ |
Andi Kleen | 7055646 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 89 | if (max_apic <= 8) |
Andi Kleen | f8d3119 | 2005-07-28 21:15:42 -0700 | [diff] [blame] | 90 | genapic = &apic_flat; |
| 91 | #endif |
| 92 | goto print; |
| 93 | } |
| 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | clusters = 0; |
| 96 | max_cluster = 0; |
Andi Kleen | f8d3119 | 2005-07-28 21:15:42 -0700 | [diff] [blame] | 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | for (i = 0; i < NUM_APIC_CLUSTERS; i++) { |
| 99 | if (cluster_cnt[i] > 0) { |
| 100 | ++clusters; |
| 101 | if (cluster_cnt[i] > max_cluster) |
| 102 | max_cluster = cluster_cnt[i]; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * If we have clusters <= 1 and CPUs <= 8 in cluster 0, then flat mode, |
| 108 | * else if max_cluster <= 4 and cluster_cnt[15] == 0, clustered logical |
| 109 | * else physical mode. |
| 110 | * (We don't use lowest priority delivery + HW APIC IRQ steering, so |
| 111 | * can ignore the clustered logical case and go straight to physical.) |
| 112 | */ |
Andi Kleen | 5bf97e0 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 113 | if (clusters <= 1 && max_cluster <= 8 && cluster_cnt[0] == max_cluster) { |
| 114 | #ifdef CONFIG_HOTPLUG_CPU |
| 115 | /* Don't use APIC shortcuts in CPU hotplug to avoid races */ |
| 116 | genapic = &apic_physflat; |
| 117 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | genapic = &apic_flat; |
Andi Kleen | 5bf97e0 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 119 | #endif |
| 120 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | genapic = &apic_cluster; |
| 122 | |
| 123 | print: |
| 124 | printk(KERN_INFO "Setting APIC routing to %s\n", genapic->name); |
| 125 | } |
| 126 | |
| 127 | /* Same for both flat and clustered. */ |
| 128 | |
| 129 | void send_IPI_self(int vector) |
| 130 | { |
| 131 | __send_IPI_shortcut(APIC_DEST_SELF, vector, APIC_DEST_PHYSICAL); |
| 132 | } |