Alex Chiang | 4781725 | 2009-12-20 12:19:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 Intel Corporation |
| 3 | * Copyright (C) 2009 Hewlett-Packard Development Company, L.P. |
| 4 | * |
| 5 | * Alex Chiang <achiang@hp.com> |
| 6 | * - Unified x86/ia64 implementations |
Alex Chiang | 4781725 | 2009-12-20 12:19:34 -0700 | [diff] [blame] | 7 | */ |
Paul Gortmaker | 214f2c9 | 2011-10-26 16:22:14 -0400 | [diff] [blame] | 8 | #include <linux/export.h> |
Lv Zheng | 8b48463 | 2013-12-03 08:49:16 +0800 | [diff] [blame] | 9 | #include <linux/acpi.h> |
Alex Chiang | 78f1699 | 2009-12-20 12:19:09 -0700 | [diff] [blame] | 10 | #include <acpi/processor.h> |
| 11 | |
Alex Chiang | 78f1699 | 2009-12-20 12:19:09 -0700 | [diff] [blame] | 12 | #define _COMPONENT ACPI_PROCESSOR_COMPONENT |
Alex Chiang | 4d5d4cd | 2010-02-22 12:11:14 -0700 | [diff] [blame] | 13 | ACPI_MODULE_NAME("processor_core"); |
Alex Chiang | 78f1699 | 2009-12-20 12:19:09 -0700 | [diff] [blame] | 14 | |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 15 | static int map_lapic_id(struct acpi_subtable_header *entry, |
| 16 | u32 acpi_id, int *apic_id) |
| 17 | { |
| 18 | struct acpi_madt_local_apic *lapic = |
Fabian Frederick | ef86c3f | 2014-09-14 15:12:43 +0200 | [diff] [blame] | 19 | container_of(entry, struct acpi_madt_local_apic, header); |
Alex Chiang | 1113073 | 2010-02-22 12:11:44 -0700 | [diff] [blame] | 20 | |
| 21 | if (!(lapic->lapic_flags & ACPI_MADT_ENABLED)) |
Hanjun Guo | 038d7b5 | 2014-01-17 12:37:02 +0800 | [diff] [blame] | 22 | return -ENODEV; |
Alex Chiang | 1113073 | 2010-02-22 12:11:44 -0700 | [diff] [blame] | 23 | |
| 24 | if (lapic->processor_id != acpi_id) |
Hanjun Guo | 038d7b5 | 2014-01-17 12:37:02 +0800 | [diff] [blame] | 25 | return -EINVAL; |
Alex Chiang | 1113073 | 2010-02-22 12:11:44 -0700 | [diff] [blame] | 26 | |
| 27 | *apic_id = lapic->id; |
Hanjun Guo | 038d7b5 | 2014-01-17 12:37:02 +0800 | [diff] [blame] | 28 | return 0; |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | static int map_x2apic_id(struct acpi_subtable_header *entry, |
| 32 | int device_declaration, u32 acpi_id, int *apic_id) |
| 33 | { |
| 34 | struct acpi_madt_local_x2apic *apic = |
Fabian Frederick | ef86c3f | 2014-09-14 15:12:43 +0200 | [diff] [blame] | 35 | container_of(entry, struct acpi_madt_local_x2apic, header); |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 36 | |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 37 | if (!(apic->lapic_flags & ACPI_MADT_ENABLED)) |
Hanjun Guo | 038d7b5 | 2014-01-17 12:37:02 +0800 | [diff] [blame] | 38 | return -ENODEV; |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 39 | |
Alex Chiang | d674209 | 2010-02-22 12:11:50 -0700 | [diff] [blame] | 40 | if (device_declaration && (apic->uid == acpi_id)) { |
| 41 | *apic_id = apic->local_apic_id; |
Hanjun Guo | 038d7b5 | 2014-01-17 12:37:02 +0800 | [diff] [blame] | 42 | return 0; |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Hanjun Guo | 038d7b5 | 2014-01-17 12:37:02 +0800 | [diff] [blame] | 45 | return -EINVAL; |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static int map_lsapic_id(struct acpi_subtable_header *entry, |
| 49 | int device_declaration, u32 acpi_id, int *apic_id) |
| 50 | { |
| 51 | struct acpi_madt_local_sapic *lsapic = |
Fabian Frederick | ef86c3f | 2014-09-14 15:12:43 +0200 | [diff] [blame] | 52 | container_of(entry, struct acpi_madt_local_sapic, header); |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 53 | |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 54 | if (!(lsapic->lapic_flags & ACPI_MADT_ENABLED)) |
Hanjun Guo | 038d7b5 | 2014-01-17 12:37:02 +0800 | [diff] [blame] | 55 | return -ENODEV; |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 56 | |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 57 | if (device_declaration) { |
Alex Chiang | eae701c | 2010-02-22 12:11:55 -0700 | [diff] [blame] | 58 | if ((entry->length < 16) || (lsapic->uid != acpi_id)) |
Hanjun Guo | 038d7b5 | 2014-01-17 12:37:02 +0800 | [diff] [blame] | 59 | return -EINVAL; |
Alex Chiang | eae701c | 2010-02-22 12:11:55 -0700 | [diff] [blame] | 60 | } else if (lsapic->processor_id != acpi_id) |
Hanjun Guo | 038d7b5 | 2014-01-17 12:37:02 +0800 | [diff] [blame] | 61 | return -EINVAL; |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 62 | |
Alex Chiang | eae701c | 2010-02-22 12:11:55 -0700 | [diff] [blame] | 63 | *apic_id = (lsapic->id << 8) | lsapic->eid; |
Hanjun Guo | 038d7b5 | 2014-01-17 12:37:02 +0800 | [diff] [blame] | 64 | return 0; |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static int map_madt_entry(int type, u32 acpi_id) |
| 68 | { |
| 69 | unsigned long madt_end, entry; |
Alex Chiang | 149fe9c | 2010-02-22 12:12:00 -0700 | [diff] [blame] | 70 | static struct acpi_table_madt *madt; |
| 71 | static int read_madt; |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 72 | int apic_id = -1; |
| 73 | |
Alex Chiang | 149fe9c | 2010-02-22 12:12:00 -0700 | [diff] [blame] | 74 | if (!read_madt) { |
| 75 | if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_MADT, 0, |
| 76 | (struct acpi_table_header **)&madt))) |
| 77 | madt = NULL; |
| 78 | read_madt++; |
| 79 | } |
| 80 | |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 81 | if (!madt) |
| 82 | return apic_id; |
| 83 | |
| 84 | entry = (unsigned long)madt; |
| 85 | madt_end = entry + madt->header.length; |
| 86 | |
| 87 | /* Parse all entries looking for a match. */ |
| 88 | |
| 89 | entry += sizeof(struct acpi_table_madt); |
| 90 | while (entry + sizeof(struct acpi_subtable_header) < madt_end) { |
| 91 | struct acpi_subtable_header *header = |
| 92 | (struct acpi_subtable_header *)entry; |
| 93 | if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) { |
Hanjun Guo | 038d7b5 | 2014-01-17 12:37:02 +0800 | [diff] [blame] | 94 | if (!map_lapic_id(header, acpi_id, &apic_id)) |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 95 | break; |
| 96 | } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) { |
Hanjun Guo | 038d7b5 | 2014-01-17 12:37:02 +0800 | [diff] [blame] | 97 | if (!map_x2apic_id(header, type, acpi_id, &apic_id)) |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 98 | break; |
| 99 | } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) { |
Hanjun Guo | 038d7b5 | 2014-01-17 12:37:02 +0800 | [diff] [blame] | 100 | if (!map_lsapic_id(header, type, acpi_id, &apic_id)) |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 101 | break; |
| 102 | } |
| 103 | entry += header->length; |
| 104 | } |
| 105 | return apic_id; |
| 106 | } |
| 107 | |
| 108 | static int map_mat_entry(acpi_handle handle, int type, u32 acpi_id) |
| 109 | { |
| 110 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 111 | union acpi_object *obj; |
| 112 | struct acpi_subtable_header *header; |
| 113 | int apic_id = -1; |
| 114 | |
| 115 | if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer))) |
| 116 | goto exit; |
| 117 | |
| 118 | if (!buffer.length || !buffer.pointer) |
| 119 | goto exit; |
| 120 | |
| 121 | obj = buffer.pointer; |
| 122 | if (obj->type != ACPI_TYPE_BUFFER || |
| 123 | obj->buffer.length < sizeof(struct acpi_subtable_header)) { |
| 124 | goto exit; |
| 125 | } |
| 126 | |
| 127 | header = (struct acpi_subtable_header *)obj->buffer.pointer; |
| 128 | if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) { |
| 129 | map_lapic_id(header, acpi_id, &apic_id); |
| 130 | } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) { |
| 131 | map_lsapic_id(header, type, acpi_id, &apic_id); |
Hanjun Guo | 515afdc | 2014-07-29 11:27:50 +0800 | [diff] [blame] | 132 | } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) { |
| 133 | map_x2apic_id(header, type, acpi_id, &apic_id); |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | exit: |
Syam Sidhardhan | 5273a25 | 2013-02-24 23:12:53 +0000 | [diff] [blame] | 137 | kfree(buffer.pointer); |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 138 | return apic_id; |
| 139 | } |
| 140 | |
Jiang Liu | ca9f62a | 2013-09-02 11:57:34 +0800 | [diff] [blame] | 141 | int acpi_get_apicid(acpi_handle handle, int type, u32 acpi_id) |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 142 | { |
Jiang Liu | ca9f62a | 2013-09-02 11:57:34 +0800 | [diff] [blame] | 143 | int apic_id; |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 144 | |
| 145 | apic_id = map_mat_entry(handle, type, acpi_id); |
| 146 | if (apic_id == -1) |
| 147 | apic_id = map_madt_entry(type, acpi_id); |
Jiang Liu | ca9f62a | 2013-09-02 11:57:34 +0800 | [diff] [blame] | 148 | |
| 149 | return apic_id; |
| 150 | } |
| 151 | |
| 152 | int acpi_map_cpuid(int apic_id, u32 acpi_id) |
| 153 | { |
| 154 | #ifdef CONFIG_SMP |
| 155 | int i; |
| 156 | #endif |
| 157 | |
Lin Ming | d640113 | 2011-12-13 09:36:03 +0800 | [diff] [blame] | 158 | if (apic_id == -1) { |
| 159 | /* |
| 160 | * On UP processor, there is no _MAT or MADT table. |
| 161 | * So above apic_id is always set to -1. |
| 162 | * |
| 163 | * BIOS may define multiple CPU handles even for UP processor. |
| 164 | * For example, |
| 165 | * |
| 166 | * Scope (_PR) |
| 167 | * { |
| 168 | * Processor (CPU0, 0x00, 0x00000410, 0x06) {} |
| 169 | * Processor (CPU1, 0x01, 0x00000410, 0x06) {} |
| 170 | * Processor (CPU2, 0x02, 0x00000410, 0x06) {} |
| 171 | * Processor (CPU3, 0x03, 0x00000410, 0x06) {} |
| 172 | * } |
| 173 | * |
Thomas Renninger | c4686c7 | 2012-07-12 12:24:33 +0200 | [diff] [blame] | 174 | * Ignores apic_id and always returns 0 for the processor |
| 175 | * handle with acpi id 0 if nr_cpu_ids is 1. |
| 176 | * This should be the case if SMP tables are not found. |
Lin Ming | d640113 | 2011-12-13 09:36:03 +0800 | [diff] [blame] | 177 | * Return -1 for other CPU's handle. |
| 178 | */ |
Thomas Renninger | c4686c7 | 2012-07-12 12:24:33 +0200 | [diff] [blame] | 179 | if (nr_cpu_ids <= 1 && acpi_id == 0) |
Lin Ming | d640113 | 2011-12-13 09:36:03 +0800 | [diff] [blame] | 180 | return acpi_id; |
| 181 | else |
| 182 | return apic_id; |
| 183 | } |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 184 | |
Lin Ming | 932df74 | 2011-05-16 09:11:00 +0800 | [diff] [blame] | 185 | #ifdef CONFIG_SMP |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 186 | for_each_possible_cpu(i) { |
| 187 | if (cpu_physical_id(i) == apic_id) |
| 188 | return i; |
| 189 | } |
Lin Ming | 932df74 | 2011-05-16 09:11:00 +0800 | [diff] [blame] | 190 | #else |
| 191 | /* In UP kernel, only processor 0 is valid */ |
| 192 | if (apic_id == 0) |
| 193 | return apic_id; |
| 194 | #endif |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 195 | return -1; |
| 196 | } |
Jiang Liu | ca9f62a | 2013-09-02 11:57:34 +0800 | [diff] [blame] | 197 | |
| 198 | int acpi_get_cpuid(acpi_handle handle, int type, u32 acpi_id) |
| 199 | { |
| 200 | int apic_id; |
| 201 | |
| 202 | apic_id = acpi_get_apicid(handle, type, acpi_id); |
| 203 | |
| 204 | return acpi_map_cpuid(apic_id, acpi_id); |
| 205 | } |
Alex Chiang | 78ed8bd | 2010-02-22 12:11:24 -0700 | [diff] [blame] | 206 | EXPORT_SYMBOL_GPL(acpi_get_cpuid); |