blob: 8c0a54d50d0e9b90eb7b7bf4d7e0ed2e9f97ee9b [file] [log] [blame]
Alex Chiang47817252009-12-20 12:19:34 -07001/*
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
Yinghai Luecf56362015-02-05 13:44:48 +08007 *
8 * I/O APIC hotplug support
9 * Yinghai Lu <yinghai@kernel.org>
10 * Jiang Liu <jiang.liu@intel.com>
Alex Chiang47817252009-12-20 12:19:34 -070011 */
Paul Gortmaker214f2c92011-10-26 16:22:14 -040012#include <linux/export.h>
Lv Zheng8b484632013-12-03 08:49:16 +080013#include <linux/acpi.h>
Alex Chiang78f16992009-12-20 12:19:09 -070014#include <acpi/processor.h>
15
Alex Chiang78f16992009-12-20 12:19:09 -070016#define _COMPONENT ACPI_PROCESSOR_COMPONENT
Alex Chiang4d5d4cd2010-02-22 12:11:14 -070017ACPI_MODULE_NAME("processor_core");
Alex Chiang78f16992009-12-20 12:19:09 -070018
Yinghai Luecf56362015-02-05 13:44:48 +080019static struct acpi_table_madt *get_madt_table(void)
20{
21 static struct acpi_table_madt *madt;
22 static int read_madt;
23
24 if (!read_madt) {
25 if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_MADT, 0,
26 (struct acpi_table_header **)&madt)))
27 madt = NULL;
28 read_madt++;
29 }
30
31 return madt;
32}
33
Alex Chiang78ed8bd2010-02-22 12:11:24 -070034static int map_lapic_id(struct acpi_subtable_header *entry,
Dou Liyang09c3f2b2017-03-03 16:02:24 +080035 u32 acpi_id, phys_cpuid_t *apic_id)
Alex Chiang78ed8bd2010-02-22 12:11:24 -070036{
37 struct acpi_madt_local_apic *lapic =
Fabian Frederickef86c3f2014-09-14 15:12:43 +020038 container_of(entry, struct acpi_madt_local_apic, header);
Alex Chiang11130732010-02-22 12:11:44 -070039
Dou Liyang09c3f2b2017-03-03 16:02:24 +080040 if (!(lapic->lapic_flags & ACPI_MADT_ENABLED))
Hanjun Guo038d7b52014-01-17 12:37:02 +080041 return -ENODEV;
Alex Chiang11130732010-02-22 12:11:44 -070042
43 if (lapic->processor_id != acpi_id)
Hanjun Guo038d7b52014-01-17 12:37:02 +080044 return -EINVAL;
Alex Chiang11130732010-02-22 12:11:44 -070045
46 *apic_id = lapic->id;
Hanjun Guo038d7b52014-01-17 12:37:02 +080047 return 0;
Alex Chiang78ed8bd2010-02-22 12:11:24 -070048}
49
50static int map_x2apic_id(struct acpi_subtable_header *entry,
Dou Liyang09c3f2b2017-03-03 16:02:24 +080051 int device_declaration, u32 acpi_id, phys_cpuid_t *apic_id)
Alex Chiang78ed8bd2010-02-22 12:11:24 -070052{
53 struct acpi_madt_local_x2apic *apic =
Fabian Frederickef86c3f2014-09-14 15:12:43 +020054 container_of(entry, struct acpi_madt_local_x2apic, header);
Alex Chiang78ed8bd2010-02-22 12:11:24 -070055
Dou Liyang09c3f2b2017-03-03 16:02:24 +080056 if (!(apic->lapic_flags & ACPI_MADT_ENABLED))
Hanjun Guo038d7b52014-01-17 12:37:02 +080057 return -ENODEV;
Alex Chiang78ed8bd2010-02-22 12:11:24 -070058
Alex Chiangd6742092010-02-22 12:11:50 -070059 if (device_declaration && (apic->uid == acpi_id)) {
60 *apic_id = apic->local_apic_id;
Hanjun Guo038d7b52014-01-17 12:37:02 +080061 return 0;
Alex Chiang78ed8bd2010-02-22 12:11:24 -070062 }
63
Hanjun Guo038d7b52014-01-17 12:37:02 +080064 return -EINVAL;
Alex Chiang78ed8bd2010-02-22 12:11:24 -070065}
66
67static int map_lsapic_id(struct acpi_subtable_header *entry,
Dou Liyang09c3f2b2017-03-03 16:02:24 +080068 int device_declaration, u32 acpi_id, phys_cpuid_t *apic_id)
Alex Chiang78ed8bd2010-02-22 12:11:24 -070069{
70 struct acpi_madt_local_sapic *lsapic =
Fabian Frederickef86c3f2014-09-14 15:12:43 +020071 container_of(entry, struct acpi_madt_local_sapic, header);
Alex Chiang78ed8bd2010-02-22 12:11:24 -070072
Dou Liyang09c3f2b2017-03-03 16:02:24 +080073 if (!(lsapic->lapic_flags & ACPI_MADT_ENABLED))
Hanjun Guo038d7b52014-01-17 12:37:02 +080074 return -ENODEV;
Alex Chiang78ed8bd2010-02-22 12:11:24 -070075
Alex Chiang78ed8bd2010-02-22 12:11:24 -070076 if (device_declaration) {
Alex Chiangeae701c2010-02-22 12:11:55 -070077 if ((entry->length < 16) || (lsapic->uid != acpi_id))
Hanjun Guo038d7b52014-01-17 12:37:02 +080078 return -EINVAL;
Alex Chiangeae701c2010-02-22 12:11:55 -070079 } else if (lsapic->processor_id != acpi_id)
Hanjun Guo038d7b52014-01-17 12:37:02 +080080 return -EINVAL;
Alex Chiang78ed8bd2010-02-22 12:11:24 -070081
Alex Chiangeae701c2010-02-22 12:11:55 -070082 *apic_id = (lsapic->id << 8) | lsapic->eid;
Hanjun Guo038d7b52014-01-17 12:37:02 +080083 return 0;
Alex Chiang78ed8bd2010-02-22 12:11:24 -070084}
85
Hanjun Guo020295b2015-03-24 14:02:47 +000086/*
87 * Retrieve the ARM CPU physical identifier (MPIDR)
88 */
89static int map_gicc_mpidr(struct acpi_subtable_header *entry,
Dou Liyang09c3f2b2017-03-03 16:02:24 +080090 int device_declaration, u32 acpi_id, phys_cpuid_t *mpidr)
Hanjun Guo020295b2015-03-24 14:02:47 +000091{
92 struct acpi_madt_generic_interrupt *gicc =
93 container_of(entry, struct acpi_madt_generic_interrupt, header);
94
Dou Liyang09c3f2b2017-03-03 16:02:24 +080095 if (!(gicc->flags & ACPI_MADT_ENABLED))
Hanjun Guo020295b2015-03-24 14:02:47 +000096 return -ENODEV;
97
98 /* device_declaration means Device object in DSDT, in the
99 * GIC interrupt model, logical processors are required to
100 * have a Processor Device object in the DSDT, so we should
101 * check device_declaration here
102 */
103 if (device_declaration && (gicc->uid == acpi_id)) {
104 *mpidr = gicc->arm_mpidr;
105 return 0;
106 }
107
108 return -EINVAL;
109}
110
David Daneyfb7c2ba2016-05-24 15:35:43 -0700111static phys_cpuid_t map_madt_entry(struct acpi_table_madt *madt,
Dou Liyang09c3f2b2017-03-03 16:02:24 +0800112 int type, u32 acpi_id)
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700113{
114 unsigned long madt_end, entry;
Catalin Marinas828aef32015-03-24 14:02:46 +0000115 phys_cpuid_t phys_id = PHYS_CPUID_INVALID; /* CPU hardware ID */
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700116
117 if (!madt)
Hanjun Guoaf8f3f52015-01-04 18:55:02 +0800118 return phys_id;
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700119
120 entry = (unsigned long)madt;
121 madt_end = entry + madt->header.length;
122
123 /* Parse all entries looking for a match. */
124
125 entry += sizeof(struct acpi_table_madt);
126 while (entry + sizeof(struct acpi_subtable_header) < madt_end) {
127 struct acpi_subtable_header *header =
128 (struct acpi_subtable_header *)entry;
129 if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
Dou Liyang09c3f2b2017-03-03 16:02:24 +0800130 if (!map_lapic_id(header, acpi_id, &phys_id))
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700131 break;
132 } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
Dou Liyang09c3f2b2017-03-03 16:02:24 +0800133 if (!map_x2apic_id(header, type, acpi_id, &phys_id))
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700134 break;
135 } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
Dou Liyang09c3f2b2017-03-03 16:02:24 +0800136 if (!map_lsapic_id(header, type, acpi_id, &phys_id))
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700137 break;
Hanjun Guo020295b2015-03-24 14:02:47 +0000138 } else if (header->type == ACPI_MADT_TYPE_GENERIC_INTERRUPT) {
Dou Liyang09c3f2b2017-03-03 16:02:24 +0800139 if (!map_gicc_mpidr(header, type, acpi_id, &phys_id))
Hanjun Guo020295b2015-03-24 14:02:47 +0000140 break;
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700141 }
142 entry += header->length;
143 }
Hanjun Guoaf8f3f52015-01-04 18:55:02 +0800144 return phys_id;
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700145}
146
David Daneyfb7c2ba2016-05-24 15:35:43 -0700147phys_cpuid_t __init acpi_map_madt_entry(u32 acpi_id)
148{
149 struct acpi_table_madt *madt = NULL;
David Daneyfb7c2ba2016-05-24 15:35:43 -0700150 phys_cpuid_t rv;
151
Lv Zheng6b11d1d2016-12-14 15:04:39 +0800152 acpi_get_table(ACPI_SIG_MADT, 0,
153 (struct acpi_table_header **)&madt);
David Daneyfb7c2ba2016-05-24 15:35:43 -0700154 if (!madt)
155 return PHYS_CPUID_INVALID;
156
Dou Liyang09c3f2b2017-03-03 16:02:24 +0800157 rv = map_madt_entry(madt, 1, acpi_id);
David Daneyfb7c2ba2016-05-24 15:35:43 -0700158
Lv Zheng6b11d1d2016-12-14 15:04:39 +0800159 acpi_put_table((struct acpi_table_header *)madt);
David Daneyfb7c2ba2016-05-24 15:35:43 -0700160
161 return rv;
162}
163
Dou Liyang09c3f2b2017-03-03 16:02:24 +0800164static phys_cpuid_t map_mat_entry(acpi_handle handle, int type, u32 acpi_id)
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700165{
166 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
167 union acpi_object *obj;
168 struct acpi_subtable_header *header;
Catalin Marinas828aef32015-03-24 14:02:46 +0000169 phys_cpuid_t phys_id = PHYS_CPUID_INVALID;
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700170
171 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
172 goto exit;
173
174 if (!buffer.length || !buffer.pointer)
175 goto exit;
176
177 obj = buffer.pointer;
178 if (obj->type != ACPI_TYPE_BUFFER ||
179 obj->buffer.length < sizeof(struct acpi_subtable_header)) {
180 goto exit;
181 }
182
183 header = (struct acpi_subtable_header *)obj->buffer.pointer;
Jiang Liu13ca62b2014-10-27 13:21:36 +0800184 if (header->type == ACPI_MADT_TYPE_LOCAL_APIC)
Dou Liyang09c3f2b2017-03-03 16:02:24 +0800185 map_lapic_id(header, acpi_id, &phys_id);
Jiang Liu13ca62b2014-10-27 13:21:36 +0800186 else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC)
Dou Liyang09c3f2b2017-03-03 16:02:24 +0800187 map_lsapic_id(header, type, acpi_id, &phys_id);
Jiang Liu13ca62b2014-10-27 13:21:36 +0800188 else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC)
Dou Liyang09c3f2b2017-03-03 16:02:24 +0800189 map_x2apic_id(header, type, acpi_id, &phys_id);
Hanjun Guo020295b2015-03-24 14:02:47 +0000190 else if (header->type == ACPI_MADT_TYPE_GENERIC_INTERRUPT)
Dou Liyang09c3f2b2017-03-03 16:02:24 +0800191 map_gicc_mpidr(header, type, acpi_id, &phys_id);
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700192
193exit:
Syam Sidhardhan5273a252013-02-24 23:12:53 +0000194 kfree(buffer.pointer);
Hanjun Guoaf8f3f52015-01-04 18:55:02 +0800195 return phys_id;
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700196}
197
Dou Liyang09c3f2b2017-03-03 16:02:24 +0800198phys_cpuid_t acpi_get_phys_id(acpi_handle handle, int type, u32 acpi_id)
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700199{
Catalin Marinas828aef32015-03-24 14:02:46 +0000200 phys_cpuid_t phys_id;
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700201
Dou Liyang09c3f2b2017-03-03 16:02:24 +0800202 phys_id = map_mat_entry(handle, type, acpi_id);
Hanjun Guoddcc18f2015-05-13 16:19:30 +0800203 if (invalid_phys_cpuid(phys_id))
Dou Liyang09c3f2b2017-03-03 16:02:24 +0800204 phys_id = map_madt_entry(get_madt_table(), type, acpi_id);
Jiang Liuca9f62a2013-09-02 11:57:34 +0800205
Hanjun Guoaf8f3f52015-01-04 18:55:02 +0800206 return phys_id;
Jiang Liuca9f62a2013-09-02 11:57:34 +0800207}
Jan Beulich166deb02018-06-25 04:17:35 -0600208EXPORT_SYMBOL_GPL(acpi_get_phys_id);
Jiang Liuca9f62a2013-09-02 11:57:34 +0800209
Catalin Marinas828aef32015-03-24 14:02:46 +0000210int acpi_map_cpuid(phys_cpuid_t phys_id, u32 acpi_id)
Jiang Liuca9f62a2013-09-02 11:57:34 +0800211{
212#ifdef CONFIG_SMP
213 int i;
214#endif
215
Hanjun Guoddcc18f2015-05-13 16:19:30 +0800216 if (invalid_phys_cpuid(phys_id)) {
Lin Mingd6401132011-12-13 09:36:03 +0800217 /*
218 * On UP processor, there is no _MAT or MADT table.
Catalin Marinas828aef32015-03-24 14:02:46 +0000219 * So above phys_id is always set to PHYS_CPUID_INVALID.
Lin Mingd6401132011-12-13 09:36:03 +0800220 *
221 * BIOS may define multiple CPU handles even for UP processor.
222 * For example,
223 *
224 * Scope (_PR)
Jiang Liu13ca62b2014-10-27 13:21:36 +0800225 * {
Lin Mingd6401132011-12-13 09:36:03 +0800226 * Processor (CPU0, 0x00, 0x00000410, 0x06) {}
227 * Processor (CPU1, 0x01, 0x00000410, 0x06) {}
228 * Processor (CPU2, 0x02, 0x00000410, 0x06) {}
229 * Processor (CPU3, 0x03, 0x00000410, 0x06) {}
230 * }
231 *
Hanjun Guoaf8f3f52015-01-04 18:55:02 +0800232 * Ignores phys_id and always returns 0 for the processor
Thomas Renningerc4686c72012-07-12 12:24:33 +0200233 * handle with acpi id 0 if nr_cpu_ids is 1.
234 * This should be the case if SMP tables are not found.
Hanjun Guod3da7cb2015-05-11 12:17:18 +0800235 * Return -EINVAL for other CPU's handle.
Lin Mingd6401132011-12-13 09:36:03 +0800236 */
Thomas Renningerc4686c72012-07-12 12:24:33 +0200237 if (nr_cpu_ids <= 1 && acpi_id == 0)
Lin Mingd6401132011-12-13 09:36:03 +0800238 return acpi_id;
239 else
Hanjun Guod3da7cb2015-05-11 12:17:18 +0800240 return -EINVAL;
Lin Mingd6401132011-12-13 09:36:03 +0800241 }
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700242
Lin Ming932df742011-05-16 09:11:00 +0800243#ifdef CONFIG_SMP
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700244 for_each_possible_cpu(i) {
Hanjun Guoaf8f3f52015-01-04 18:55:02 +0800245 if (cpu_physical_id(i) == phys_id)
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700246 return i;
247 }
Lin Ming932df742011-05-16 09:11:00 +0800248#else
249 /* In UP kernel, only processor 0 is valid */
Hanjun Guoaf8f3f52015-01-04 18:55:02 +0800250 if (phys_id == 0)
251 return phys_id;
Lin Ming932df742011-05-16 09:11:00 +0800252#endif
Hanjun Guod3da7cb2015-05-11 12:17:18 +0800253 return -ENODEV;
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700254}
Jiang Liuca9f62a2013-09-02 11:57:34 +0800255
256int acpi_get_cpuid(acpi_handle handle, int type, u32 acpi_id)
257{
Catalin Marinas828aef32015-03-24 14:02:46 +0000258 phys_cpuid_t phys_id;
Jiang Liuca9f62a2013-09-02 11:57:34 +0800259
Hanjun Guoaf8f3f52015-01-04 18:55:02 +0800260 phys_id = acpi_get_phys_id(handle, type, acpi_id);
Jiang Liuca9f62a2013-09-02 11:57:34 +0800261
Hanjun Guoaf8f3f52015-01-04 18:55:02 +0800262 return acpi_map_cpuid(phys_id, acpi_id);
Jiang Liuca9f62a2013-09-02 11:57:34 +0800263}
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700264EXPORT_SYMBOL_GPL(acpi_get_cpuid);
Yinghai Luecf56362015-02-05 13:44:48 +0800265
266#ifdef CONFIG_ACPI_HOTPLUG_IOAPIC
267static int get_ioapic_id(struct acpi_subtable_header *entry, u32 gsi_base,
268 u64 *phys_addr, int *ioapic_id)
269{
270 struct acpi_madt_io_apic *ioapic = (struct acpi_madt_io_apic *)entry;
271
272 if (ioapic->global_irq_base != gsi_base)
273 return 0;
274
275 *phys_addr = ioapic->address;
276 *ioapic_id = ioapic->id;
277 return 1;
278}
279
280static int parse_madt_ioapic_entry(u32 gsi_base, u64 *phys_addr)
281{
282 struct acpi_subtable_header *hdr;
283 unsigned long madt_end, entry;
284 struct acpi_table_madt *madt;
285 int apic_id = -1;
286
287 madt = get_madt_table();
288 if (!madt)
289 return apic_id;
290
291 entry = (unsigned long)madt;
292 madt_end = entry + madt->header.length;
293
294 /* Parse all entries looking for a match. */
295 entry += sizeof(struct acpi_table_madt);
296 while (entry + sizeof(struct acpi_subtable_header) < madt_end) {
297 hdr = (struct acpi_subtable_header *)entry;
298 if (hdr->type == ACPI_MADT_TYPE_IO_APIC &&
299 get_ioapic_id(hdr, gsi_base, phys_addr, &apic_id))
300 break;
301 else
302 entry += hdr->length;
303 }
304
305 return apic_id;
306}
307
308static int parse_mat_ioapic_entry(acpi_handle handle, u32 gsi_base,
309 u64 *phys_addr)
310{
311 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
312 struct acpi_subtable_header *header;
313 union acpi_object *obj;
314 int apic_id = -1;
315
316 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
317 goto exit;
318
319 if (!buffer.length || !buffer.pointer)
320 goto exit;
321
322 obj = buffer.pointer;
323 if (obj->type != ACPI_TYPE_BUFFER ||
324 obj->buffer.length < sizeof(struct acpi_subtable_header))
325 goto exit;
326
327 header = (struct acpi_subtable_header *)obj->buffer.pointer;
328 if (header->type == ACPI_MADT_TYPE_IO_APIC)
329 get_ioapic_id(header, gsi_base, phys_addr, &apic_id);
330
331exit:
332 kfree(buffer.pointer);
333 return apic_id;
334}
335
336/**
337 * acpi_get_ioapic_id - Get IOAPIC ID and physical address matching @gsi_base
338 * @handle: ACPI object for IOAPIC device
339 * @gsi_base: GSI base to match with
340 * @phys_addr: Pointer to store physical address of matching IOAPIC record
341 *
342 * Walk resources returned by ACPI_MAT method, then ACPI MADT table, to search
343 * for an ACPI IOAPIC record matching @gsi_base.
344 * Return IOAPIC id and store physical address in @phys_addr if found a match,
345 * otherwise return <0.
346 */
347int acpi_get_ioapic_id(acpi_handle handle, u32 gsi_base, u64 *phys_addr)
348{
349 int apic_id;
350
351 apic_id = parse_mat_ioapic_entry(handle, gsi_base, phys_addr);
352 if (apic_id == -1)
353 apic_id = parse_madt_ioapic_entry(gsi_base, phys_addr);
354
355 return apic_id;
356}
357#endif /* CONFIG_ACPI_HOTPLUG_IOAPIC */