]> git.proxmox.com Git - mirror_qemu.git/blob - hw/acpi/cpu_hotplug_acpi_table.c
fc79c5482f9776d4fc98b951cc2ca50c1b60fb98
[mirror_qemu.git] / hw / acpi / cpu_hotplug_acpi_table.c
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11
12 * You should have received a copy of the GNU General Public License along
13 * with this program; if not, see <http://www.gnu.org/licenses/>.
14 */
15
16 #include "qemu/osdep.h"
17 #include "hw/acpi/cpu_hotplug.h"
18 #include "hw/i386/pc.h"
19
20 #define CPU_EJECT_METHOD "CPEJ"
21 #define CPU_MAT_METHOD "CPMA"
22 #define CPU_ON_BITMAP "CPON"
23 #define CPU_STATUS_METHOD "CPST"
24 #define CPU_STATUS_MAP "PRS"
25 #define CPU_SCAN_METHOD "PRSC"
26
27 void build_legacy_cpu_hotplug_aml(Aml *ctx, MachineState *machine,
28 uint16_t io_base, uint16_t io_len)
29 {
30 Aml *dev;
31 Aml *crs;
32 Aml *pkg;
33 Aml *field;
34 Aml *method;
35 Aml *if_ctx;
36 Aml *else_ctx;
37 int i, apic_idx;
38 Aml *sb_scope = aml_scope("_SB");
39 uint8_t madt_tmpl[8] = {0x00, 0x08, 0x00, 0x00, 0x00, 0, 0, 0};
40 Aml *cpu_id = aml_arg(0);
41 Aml *cpu_on = aml_local(0);
42 Aml *madt = aml_local(1);
43 Aml *cpus_map = aml_name(CPU_ON_BITMAP);
44 Aml *zero = aml_int(0);
45 Aml *one = aml_int(1);
46 MachineClass *mc = MACHINE_GET_CLASS(machine);
47 CPUArchIdList *apic_ids = mc->possible_cpu_arch_ids(machine);
48 PCMachineState *pcms = PC_MACHINE(machine);
49
50 /*
51 * _MAT method - creates an madt apic buffer
52 * cpu_id = Arg0 = Processor ID = Local APIC ID
53 * cpu_on = Local0 = CPON flag for this cpu
54 * madt = Local1 = Buffer (in madt apic form) to return
55 */
56 method = aml_method(CPU_MAT_METHOD, 1, AML_NOTSERIALIZED);
57 aml_append(method,
58 aml_store(aml_derefof(aml_index(cpus_map, cpu_id)), cpu_on));
59 aml_append(method,
60 aml_store(aml_buffer(sizeof(madt_tmpl), madt_tmpl), madt));
61 /* Update the processor id, lapic id, and enable/disable status */
62 aml_append(method, aml_store(cpu_id, aml_index(madt, aml_int(2))));
63 aml_append(method, aml_store(cpu_id, aml_index(madt, aml_int(3))));
64 aml_append(method, aml_store(cpu_on, aml_index(madt, aml_int(4))));
65 aml_append(method, aml_return(madt));
66 aml_append(sb_scope, method);
67
68 /*
69 * _STA method - return ON status of cpu
70 * cpu_id = Arg0 = Processor ID = Local APIC ID
71 * cpu_on = Local0 = CPON flag for this cpu
72 */
73 method = aml_method(CPU_STATUS_METHOD, 1, AML_NOTSERIALIZED);
74 aml_append(method,
75 aml_store(aml_derefof(aml_index(cpus_map, cpu_id)), cpu_on));
76 if_ctx = aml_if(cpu_on);
77 {
78 aml_append(if_ctx, aml_return(aml_int(0xF)));
79 }
80 aml_append(method, if_ctx);
81 else_ctx = aml_else();
82 {
83 aml_append(else_ctx, aml_return(zero));
84 }
85 aml_append(method, else_ctx);
86 aml_append(sb_scope, method);
87
88 method = aml_method(CPU_EJECT_METHOD, 2, AML_NOTSERIALIZED);
89 aml_append(method, aml_sleep(200));
90 aml_append(sb_scope, method);
91
92 method = aml_method(CPU_SCAN_METHOD, 0, AML_NOTSERIALIZED);
93 {
94 Aml *while_ctx, *if_ctx2, *else_ctx2;
95 Aml *bus_check_evt = aml_int(1);
96 Aml *remove_evt = aml_int(3);
97 Aml *status_map = aml_local(5); /* Local5 = active cpu bitmap */
98 Aml *byte = aml_local(2); /* Local2 = last read byte from bitmap */
99 Aml *idx = aml_local(0); /* Processor ID / APIC ID iterator */
100 Aml *is_cpu_on = aml_local(1); /* Local1 = CPON flag for cpu */
101 Aml *status = aml_local(3); /* Local3 = active state for cpu */
102
103 aml_append(method, aml_store(aml_name(CPU_STATUS_MAP), status_map));
104 aml_append(method, aml_store(zero, byte));
105 aml_append(method, aml_store(zero, idx));
106
107 /* While (idx < SizeOf(CPON)) */
108 while_ctx = aml_while(aml_lless(idx, aml_sizeof(cpus_map)));
109 aml_append(while_ctx,
110 aml_store(aml_derefof(aml_index(cpus_map, idx)), is_cpu_on));
111
112 if_ctx = aml_if(aml_and(idx, aml_int(0x07), NULL));
113 {
114 /* Shift down previously read bitmap byte */
115 aml_append(if_ctx, aml_shiftright(byte, one, byte));
116 }
117 aml_append(while_ctx, if_ctx);
118
119 else_ctx = aml_else();
120 {
121 /* Read next byte from cpu bitmap */
122 aml_append(else_ctx, aml_store(aml_derefof(aml_index(status_map,
123 aml_shiftright(idx, aml_int(3), NULL))), byte));
124 }
125 aml_append(while_ctx, else_ctx);
126
127 aml_append(while_ctx, aml_store(aml_and(byte, one, NULL), status));
128 if_ctx = aml_if(aml_lnot(aml_equal(is_cpu_on, status)));
129 {
130 /* State change - update CPON with new state */
131 aml_append(if_ctx, aml_store(status, aml_index(cpus_map, idx)));
132 if_ctx2 = aml_if(aml_equal(status, one));
133 {
134 aml_append(if_ctx2,
135 aml_call2(AML_NOTIFY_METHOD, idx, bus_check_evt));
136 }
137 aml_append(if_ctx, if_ctx2);
138 else_ctx2 = aml_else();
139 {
140 aml_append(else_ctx2,
141 aml_call2(AML_NOTIFY_METHOD, idx, remove_evt));
142 }
143 }
144 aml_append(if_ctx, else_ctx2);
145 aml_append(while_ctx, if_ctx);
146
147 aml_append(while_ctx, aml_increment(idx)); /* go to next cpu */
148 aml_append(method, while_ctx);
149 }
150 aml_append(sb_scope, method);
151
152 /* The current AML generator can cover the APIC ID range [0..255],
153 * inclusive, for VCPU hotplug. */
154 QEMU_BUILD_BUG_ON(ACPI_CPU_HOTPLUG_ID_LIMIT > 256);
155 g_assert(pcms->apic_id_limit <= ACPI_CPU_HOTPLUG_ID_LIMIT);
156
157 /* create PCI0.PRES device and its _CRS to reserve CPU hotplug MMIO */
158 dev = aml_device("PCI0." stringify(CPU_HOTPLUG_RESOURCE_DEVICE));
159 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A06")));
160 aml_append(dev,
161 aml_name_decl("_UID", aml_string("CPU Hotplug resources"))
162 );
163 /* device present, functioning, decoding, not shown in UI */
164 aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
165 crs = aml_resource_template();
166 aml_append(crs,
167 aml_io(AML_DECODE16, io_base, io_base, 1, io_len)
168 );
169 aml_append(dev, aml_name_decl("_CRS", crs));
170 aml_append(sb_scope, dev);
171 /* declare CPU hotplug MMIO region and PRS field to access it */
172 aml_append(sb_scope, aml_operation_region(
173 "PRST", AML_SYSTEM_IO, aml_int(io_base), io_len));
174 field = aml_field("PRST", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
175 aml_append(field, aml_named_field("PRS", 256));
176 aml_append(sb_scope, field);
177
178 /* build Processor object for each processor */
179 for (i = 0; i < apic_ids->len; i++) {
180 int apic_id = apic_ids->cpus[i].arch_id;
181
182 assert(apic_id < ACPI_CPU_HOTPLUG_ID_LIMIT);
183
184 dev = aml_processor(apic_id, 0, 0, "CP%.02X", apic_id);
185
186 method = aml_method("_MAT", 0, AML_NOTSERIALIZED);
187 aml_append(method,
188 aml_return(aml_call1(CPU_MAT_METHOD, aml_int(apic_id))));
189 aml_append(dev, method);
190
191 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
192 aml_append(method,
193 aml_return(aml_call1(CPU_STATUS_METHOD, aml_int(apic_id))));
194 aml_append(dev, method);
195
196 method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
197 aml_append(method,
198 aml_return(aml_call2(CPU_EJECT_METHOD, aml_int(apic_id),
199 aml_arg(0)))
200 );
201 aml_append(dev, method);
202
203 aml_append(sb_scope, dev);
204 }
205
206 /* build this code:
207 * Method(NTFY, 2) {If (LEqual(Arg0, 0x00)) {Notify(CP00, Arg1)} ...}
208 */
209 /* Arg0 = Processor ID = APIC ID */
210 method = aml_method(AML_NOTIFY_METHOD, 2, AML_NOTSERIALIZED);
211 for (i = 0; i < apic_ids->len; i++) {
212 int apic_id = apic_ids->cpus[i].arch_id;
213
214 if_ctx = aml_if(aml_equal(aml_arg(0), aml_int(apic_id)));
215 aml_append(if_ctx,
216 aml_notify(aml_name("CP%.02X", apic_id), aml_arg(1))
217 );
218 aml_append(method, if_ctx);
219 }
220 aml_append(sb_scope, method);
221
222 /* build "Name(CPON, Package() { One, One, ..., Zero, Zero, ... })"
223 *
224 * Note: The ability to create variable-sized packages was first
225 * introduced in ACPI 2.0. ACPI 1.0 only allowed fixed-size packages
226 * ith up to 255 elements. Windows guests up to win2k8 fail when
227 * VarPackageOp is used.
228 */
229 pkg = pcms->apic_id_limit <= 255 ? aml_package(pcms->apic_id_limit) :
230 aml_varpackage(pcms->apic_id_limit);
231
232 for (i = 0, apic_idx = 0; i < apic_ids->len; i++) {
233 int apic_id = apic_ids->cpus[i].arch_id;
234
235 for (; apic_idx < apic_id; apic_idx++) {
236 aml_append(pkg, aml_int(0));
237 }
238 aml_append(pkg, aml_int(apic_ids->cpus[i].cpu ? 1 : 0));
239 apic_idx = apic_id + 1;
240 }
241 aml_append(sb_scope, aml_name_decl(CPU_ON_BITMAP, pkg));
242 g_free(apic_ids);
243
244 aml_append(ctx, sb_scope);
245
246 method = aml_method("\\_GPE._E02", 0, AML_NOTSERIALIZED);
247 aml_append(method, aml_call0("\\_SB." CPU_SCAN_METHOD));
248 aml_append(ctx, method);
249 }