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.
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.
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/>.
16 #include "qemu/osdep.h"
17 #include "hw/acpi/cpu_hotplug.h"
18 #include "hw/i386/pc.h"
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"
27 void build_legacy_cpu_hotplug_aml(Aml
*ctx
, MachineState
*machine
,
28 uint16_t io_base
, uint16_t io_len
)
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
);
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
56 method
= aml_method(CPU_MAT_METHOD
, 1, AML_NOTSERIALIZED
);
58 aml_store(aml_derefof(aml_index(cpus_map
, cpu_id
)), cpu_on
));
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
);
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
73 method
= aml_method(CPU_STATUS_METHOD
, 1, AML_NOTSERIALIZED
);
75 aml_store(aml_derefof(aml_index(cpus_map
, cpu_id
)), cpu_on
));
76 if_ctx
= aml_if(cpu_on
);
78 aml_append(if_ctx
, aml_return(aml_int(0xF)));
80 aml_append(method
, if_ctx
);
81 else_ctx
= aml_else();
83 aml_append(else_ctx
, aml_return(zero
));
85 aml_append(method
, else_ctx
);
86 aml_append(sb_scope
, method
);
88 method
= aml_method(CPU_EJECT_METHOD
, 2, AML_NOTSERIALIZED
);
89 aml_append(method
, aml_sleep(200));
90 aml_append(sb_scope
, method
);
92 method
= aml_method(CPU_SCAN_METHOD
, 0, AML_NOTSERIALIZED
);
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 */
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
));
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
));
112 if_ctx
= aml_if(aml_and(idx
, aml_int(0x07), NULL
));
114 /* Shift down previously read bitmap byte */
115 aml_append(if_ctx
, aml_shiftright(byte
, one
, byte
));
117 aml_append(while_ctx
, if_ctx
);
119 else_ctx
= aml_else();
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
));
125 aml_append(while_ctx
, else_ctx
);
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
)));
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
));
135 aml_call2(AML_NOTIFY_METHOD
, idx
, bus_check_evt
));
137 aml_append(if_ctx
, if_ctx2
);
138 else_ctx2
= aml_else();
140 aml_append(else_ctx2
,
141 aml_call2(AML_NOTIFY_METHOD
, idx
, remove_evt
));
144 aml_append(if_ctx
, else_ctx2
);
145 aml_append(while_ctx
, if_ctx
);
147 aml_append(while_ctx
, aml_increment(idx
)); /* go to next cpu */
148 aml_append(method
, while_ctx
);
150 aml_append(sb_scope
, method
);
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
);
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")));
161 aml_name_decl("_UID", aml_string("CPU Hotplug resources"))
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();
167 aml_io(AML_DECODE16
, io_base
, io_base
, 1, io_len
)
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
);
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
;
182 assert(apic_id
< ACPI_CPU_HOTPLUG_ID_LIMIT
);
184 dev
= aml_processor(apic_id
, 0, 0, "CP%.02X", apic_id
);
186 method
= aml_method("_MAT", 0, AML_NOTSERIALIZED
);
188 aml_return(aml_call1(CPU_MAT_METHOD
, aml_int(apic_id
))));
189 aml_append(dev
, method
);
191 method
= aml_method("_STA", 0, AML_NOTSERIALIZED
);
193 aml_return(aml_call1(CPU_STATUS_METHOD
, aml_int(apic_id
))));
194 aml_append(dev
, method
);
196 method
= aml_method("_EJ0", 1, AML_NOTSERIALIZED
);
198 aml_return(aml_call2(CPU_EJECT_METHOD
, aml_int(apic_id
),
201 aml_append(dev
, method
);
203 aml_append(sb_scope
, dev
);
207 * Method(NTFY, 2) {If (LEqual(Arg0, 0x00)) {Notify(CP00, Arg1)} ...}
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
;
214 if_ctx
= aml_if(aml_equal(aml_arg(0), aml_int(apic_id
)));
216 aml_notify(aml_name("CP%.02X", apic_id
), aml_arg(1))
218 aml_append(method
, if_ctx
);
220 aml_append(sb_scope
, method
);
222 /* build "Name(CPON, Package() { One, One, ..., Zero, Zero, ... })"
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.
229 pkg
= pcms
->apic_id_limit
<= 255 ? aml_package(pcms
->apic_id_limit
) :
230 aml_varpackage(pcms
->apic_id_limit
);
232 for (i
= 0, apic_idx
= 0; i
< apic_ids
->len
; i
++) {
233 int apic_id
= apic_ids
->cpus
[i
].arch_id
;
235 for (; apic_idx
< apic_id
; apic_idx
++) {
236 aml_append(pkg
, aml_int(0));
238 aml_append(pkg
, aml_int(apic_ids
->cpus
[i
].cpu
? 1 : 0));
239 apic_idx
= apic_id
+ 1;
241 aml_append(sb_scope
, aml_name_decl(CPU_ON_BITMAP
, pkg
));
244 aml_append(ctx
, sb_scope
);
246 method
= aml_method("\\_GPE._E02", 0, AML_NOTSERIALIZED
);
247 aml_append(method
, aml_call0("\\_SB." CPU_SCAN_METHOD
));
248 aml_append(ctx
, method
);