blob: ef58f46c844287e4f64ff44a16ff63b1dd884537 [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
Alex Chiang47817252009-12-20 12:19:34 -07007 */
Paul Gortmaker214f2c92011-10-26 16:22:14 -04008#include <linux/export.h>
Lv Zheng8b484632013-12-03 08:49:16 +08009#include <linux/acpi.h>
Alex Chiang78f16992009-12-20 12:19:09 -070010#include <acpi/processor.h>
11
Alex Chiang78f16992009-12-20 12:19:09 -070012#define _COMPONENT ACPI_PROCESSOR_COMPONENT
Alex Chiang4d5d4cd2010-02-22 12:11:14 -070013ACPI_MODULE_NAME("processor_core");
Alex Chiang78f16992009-12-20 12:19:09 -070014
Alex Chiang78ed8bd2010-02-22 12:11:24 -070015static 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 Frederickef86c3f2014-09-14 15:12:43 +020019 container_of(entry, struct acpi_madt_local_apic, header);
Alex Chiang11130732010-02-22 12:11:44 -070020
21 if (!(lapic->lapic_flags & ACPI_MADT_ENABLED))
Hanjun Guo038d7b52014-01-17 12:37:02 +080022 return -ENODEV;
Alex Chiang11130732010-02-22 12:11:44 -070023
24 if (lapic->processor_id != acpi_id)
Hanjun Guo038d7b52014-01-17 12:37:02 +080025 return -EINVAL;
Alex Chiang11130732010-02-22 12:11:44 -070026
27 *apic_id = lapic->id;
Hanjun Guo038d7b52014-01-17 12:37:02 +080028 return 0;
Alex Chiang78ed8bd2010-02-22 12:11:24 -070029}
30
31static 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 Frederickef86c3f2014-09-14 15:12:43 +020035 container_of(entry, struct acpi_madt_local_x2apic, header);
Alex Chiang78ed8bd2010-02-22 12:11:24 -070036
Alex Chiang78ed8bd2010-02-22 12:11:24 -070037 if (!(apic->lapic_flags & ACPI_MADT_ENABLED))
Hanjun Guo038d7b52014-01-17 12:37:02 +080038 return -ENODEV;
Alex Chiang78ed8bd2010-02-22 12:11:24 -070039
Alex Chiangd6742092010-02-22 12:11:50 -070040 if (device_declaration && (apic->uid == acpi_id)) {
41 *apic_id = apic->local_apic_id;
Hanjun Guo038d7b52014-01-17 12:37:02 +080042 return 0;
Alex Chiang78ed8bd2010-02-22 12:11:24 -070043 }
44
Hanjun Guo038d7b52014-01-17 12:37:02 +080045 return -EINVAL;
Alex Chiang78ed8bd2010-02-22 12:11:24 -070046}
47
48static 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 Frederickef86c3f2014-09-14 15:12:43 +020052 container_of(entry, struct acpi_madt_local_sapic, header);
Alex Chiang78ed8bd2010-02-22 12:11:24 -070053
Alex Chiang78ed8bd2010-02-22 12:11:24 -070054 if (!(lsapic->lapic_flags & ACPI_MADT_ENABLED))
Hanjun Guo038d7b52014-01-17 12:37:02 +080055 return -ENODEV;
Alex Chiang78ed8bd2010-02-22 12:11:24 -070056
Alex Chiang78ed8bd2010-02-22 12:11:24 -070057 if (device_declaration) {
Alex Chiangeae701c2010-02-22 12:11:55 -070058 if ((entry->length < 16) || (lsapic->uid != acpi_id))
Hanjun Guo038d7b52014-01-17 12:37:02 +080059 return -EINVAL;
Alex Chiangeae701c2010-02-22 12:11:55 -070060 } else if (lsapic->processor_id != acpi_id)
Hanjun Guo038d7b52014-01-17 12:37:02 +080061 return -EINVAL;
Alex Chiang78ed8bd2010-02-22 12:11:24 -070062
Alex Chiangeae701c2010-02-22 12:11:55 -070063 *apic_id = (lsapic->id << 8) | lsapic->eid;
Hanjun Guo038d7b52014-01-17 12:37:02 +080064 return 0;
Alex Chiang78ed8bd2010-02-22 12:11:24 -070065}
66
67static int map_madt_entry(int type, u32 acpi_id)
68{
69 unsigned long madt_end, entry;
Alex Chiang149fe9c2010-02-22 12:12:00 -070070 static struct acpi_table_madt *madt;
71 static int read_madt;
Alex Chiang78ed8bd2010-02-22 12:11:24 -070072 int apic_id = -1;
73
Alex Chiang149fe9c2010-02-22 12:12:00 -070074 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 Chiang78ed8bd2010-02-22 12:11:24 -070081 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 Guo038d7b52014-01-17 12:37:02 +080094 if (!map_lapic_id(header, acpi_id, &apic_id))
Alex Chiang78ed8bd2010-02-22 12:11:24 -070095 break;
96 } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
Hanjun Guo038d7b52014-01-17 12:37:02 +080097 if (!map_x2apic_id(header, type, acpi_id, &apic_id))
Alex Chiang78ed8bd2010-02-22 12:11:24 -070098 break;
99 } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
Hanjun Guo038d7b52014-01-17 12:37:02 +0800100 if (!map_lsapic_id(header, type, acpi_id, &apic_id))
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700101 break;
102 }
103 entry += header->length;
104 }
105 return apic_id;
106}
107
108static 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 Guo515afdc2014-07-29 11:27:50 +0800132 } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
133 map_x2apic_id(header, type, acpi_id, &apic_id);
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700134 }
135
136exit:
Syam Sidhardhan5273a252013-02-24 23:12:53 +0000137 kfree(buffer.pointer);
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700138 return apic_id;
139}
140
Jiang Liuca9f62a2013-09-02 11:57:34 +0800141int acpi_get_apicid(acpi_handle handle, int type, u32 acpi_id)
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700142{
Jiang Liuca9f62a2013-09-02 11:57:34 +0800143 int apic_id;
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700144
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 Liuca9f62a2013-09-02 11:57:34 +0800148
149 return apic_id;
150}
151
152int acpi_map_cpuid(int apic_id, u32 acpi_id)
153{
154#ifdef CONFIG_SMP
155 int i;
156#endif
157
Lin Mingd6401132011-12-13 09:36:03 +0800158 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 Renningerc4686c72012-07-12 12:24:33 +0200174 * 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 Mingd6401132011-12-13 09:36:03 +0800177 * Return -1 for other CPU's handle.
178 */
Thomas Renningerc4686c72012-07-12 12:24:33 +0200179 if (nr_cpu_ids <= 1 && acpi_id == 0)
Lin Mingd6401132011-12-13 09:36:03 +0800180 return acpi_id;
181 else
182 return apic_id;
183 }
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700184
Lin Ming932df742011-05-16 09:11:00 +0800185#ifdef CONFIG_SMP
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700186 for_each_possible_cpu(i) {
187 if (cpu_physical_id(i) == apic_id)
188 return i;
189 }
Lin Ming932df742011-05-16 09:11:00 +0800190#else
191 /* In UP kernel, only processor 0 is valid */
192 if (apic_id == 0)
193 return apic_id;
194#endif
Alex Chiang78ed8bd2010-02-22 12:11:24 -0700195 return -1;
196}
Jiang Liuca9f62a2013-09-02 11:57:34 +0800197
198int 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 Chiang78ed8bd2010-02-22 12:11:24 -0700206EXPORT_SYMBOL_GPL(acpi_get_cpuid);