1 /* Support for generating ACPI tables and passing them to Guests
3 * ARM virt ACPI generation
5 * Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
6 * Copyright (C) 2006 Fabrice Bellard
7 * Copyright (C) 2013 Red Hat Inc
9 * Author: Michael S. Tsirkin <mst@redhat.com>
11 * Copyright (c) 2015 HUAWEI TECHNOLOGIES CO.,LTD.
13 * Author: Shannon Zhao <zhaoshenglong@huawei.com>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, see <http://www.gnu.org/licenses/>.
29 #include "qemu-common.h"
30 #include "hw/arm/virt-acpi-build.h"
31 #include "qemu/bitmap.h"
34 #include "target-arm/cpu.h"
35 #include "hw/acpi/acpi-defs.h"
36 #include "hw/acpi/acpi.h"
37 #include "hw/nvram/fw_cfg.h"
38 #include "hw/acpi/bios-linker-loader.h"
39 #include "hw/loader.h"
41 #include "hw/acpi/aml-build.h"
42 #include "hw/pci/pcie_host.h"
43 #include "hw/pci/pci.h"
45 #define ARM_SPI_BASE 32
46 #define ACPI_POWER_BUTTON_DEVICE "PWRB"
48 typedef struct VirtAcpiCpuInfo
{
49 DECLARE_BITMAP(found_cpus
, VIRT_ACPI_CPU_ID_LIMIT
);
52 static void virt_acpi_get_cpu_info(VirtAcpiCpuInfo
*cpuinfo
)
56 memset(cpuinfo
->found_cpus
, 0, sizeof cpuinfo
->found_cpus
);
58 set_bit(cpu
->cpu_index
, cpuinfo
->found_cpus
);
62 static void acpi_dsdt_add_cpus(Aml
*scope
, int smp_cpus
)
66 for (i
= 0; i
< smp_cpus
; i
++) {
67 Aml
*dev
= aml_device("C%03x", i
);
68 aml_append(dev
, aml_name_decl("_HID", aml_string("ACPI0007")));
69 aml_append(dev
, aml_name_decl("_UID", aml_int(i
)));
70 aml_append(scope
, dev
);
74 static void acpi_dsdt_add_uart(Aml
*scope
, const MemMapEntry
*uart_memmap
,
77 Aml
*dev
= aml_device("COM0");
78 aml_append(dev
, aml_name_decl("_HID", aml_string("ARMH0011")));
79 aml_append(dev
, aml_name_decl("_UID", aml_int(0)));
81 Aml
*crs
= aml_resource_template();
82 aml_append(crs
, aml_memory32_fixed(uart_memmap
->base
,
83 uart_memmap
->size
, AML_READ_WRITE
));
85 aml_interrupt(AML_CONSUMER
, AML_LEVEL
, AML_ACTIVE_HIGH
,
86 AML_EXCLUSIVE
, &uart_irq
, 1));
87 aml_append(dev
, aml_name_decl("_CRS", crs
));
89 /* The _ADR entry is used to link this device to the UART described
90 * in the SPCR table, i.e. SPCR.base_address.address == _ADR.
92 aml_append(dev
, aml_name_decl("_ADR", aml_int(uart_memmap
->base
)));
94 aml_append(scope
, dev
);
97 static void acpi_dsdt_add_rtc(Aml
*scope
, const MemMapEntry
*rtc_memmap
,
100 Aml
*dev
= aml_device("RTC0");
101 aml_append(dev
, aml_name_decl("_HID", aml_string("LNRO0013")));
102 aml_append(dev
, aml_name_decl("_UID", aml_int(0)));
104 Aml
*crs
= aml_resource_template();
105 aml_append(crs
, aml_memory32_fixed(rtc_memmap
->base
,
106 rtc_memmap
->size
, AML_READ_WRITE
));
108 aml_interrupt(AML_CONSUMER
, AML_LEVEL
, AML_ACTIVE_HIGH
,
109 AML_EXCLUSIVE
, &rtc_irq
, 1));
110 aml_append(dev
, aml_name_decl("_CRS", crs
));
111 aml_append(scope
, dev
);
114 static void acpi_dsdt_add_flash(Aml
*scope
, const MemMapEntry
*flash_memmap
)
117 hwaddr base
= flash_memmap
->base
;
118 hwaddr size
= flash_memmap
->size
/ 2;
120 dev
= aml_device("FLS0");
121 aml_append(dev
, aml_name_decl("_HID", aml_string("LNRO0015")));
122 aml_append(dev
, aml_name_decl("_UID", aml_int(0)));
124 crs
= aml_resource_template();
125 aml_append(crs
, aml_memory32_fixed(base
, size
, AML_READ_WRITE
));
126 aml_append(dev
, aml_name_decl("_CRS", crs
));
127 aml_append(scope
, dev
);
129 dev
= aml_device("FLS1");
130 aml_append(dev
, aml_name_decl("_HID", aml_string("LNRO0015")));
131 aml_append(dev
, aml_name_decl("_UID", aml_int(1)));
132 crs
= aml_resource_template();
133 aml_append(crs
, aml_memory32_fixed(base
+ size
, size
, AML_READ_WRITE
));
134 aml_append(dev
, aml_name_decl("_CRS", crs
));
135 aml_append(scope
, dev
);
138 static void acpi_dsdt_add_virtio(Aml
*scope
,
139 const MemMapEntry
*virtio_mmio_memmap
,
140 uint32_t mmio_irq
, int num
)
142 hwaddr base
= virtio_mmio_memmap
->base
;
143 hwaddr size
= virtio_mmio_memmap
->size
;
146 for (i
= 0; i
< num
; i
++) {
147 uint32_t irq
= mmio_irq
+ i
;
148 Aml
*dev
= aml_device("VR%02u", i
);
149 aml_append(dev
, aml_name_decl("_HID", aml_string("LNRO0005")));
150 aml_append(dev
, aml_name_decl("_UID", aml_int(i
)));
152 Aml
*crs
= aml_resource_template();
153 aml_append(crs
, aml_memory32_fixed(base
, size
, AML_READ_WRITE
));
155 aml_interrupt(AML_CONSUMER
, AML_LEVEL
, AML_ACTIVE_HIGH
,
156 AML_EXCLUSIVE
, &irq
, 1));
157 aml_append(dev
, aml_name_decl("_CRS", crs
));
158 aml_append(scope
, dev
);
163 static void acpi_dsdt_add_pci(Aml
*scope
, const MemMapEntry
*memmap
,
164 uint32_t irq
, bool use_highmem
)
166 Aml
*method
, *crs
, *ifctx
, *UUID
, *ifctx1
, *elsectx
, *buf
;
168 hwaddr base_mmio
= memmap
[VIRT_PCIE_MMIO
].base
;
169 hwaddr size_mmio
= memmap
[VIRT_PCIE_MMIO
].size
;
170 hwaddr base_pio
= memmap
[VIRT_PCIE_PIO
].base
;
171 hwaddr size_pio
= memmap
[VIRT_PCIE_PIO
].size
;
172 hwaddr base_ecam
= memmap
[VIRT_PCIE_ECAM
].base
;
173 hwaddr size_ecam
= memmap
[VIRT_PCIE_ECAM
].size
;
174 int nr_pcie_buses
= size_ecam
/ PCIE_MMCFG_SIZE_MIN
;
176 Aml
*dev
= aml_device("%s", "PCI0");
177 aml_append(dev
, aml_name_decl("_HID", aml_string("PNP0A08")));
178 aml_append(dev
, aml_name_decl("_CID", aml_string("PNP0A03")));
179 aml_append(dev
, aml_name_decl("_SEG", aml_int(0)));
180 aml_append(dev
, aml_name_decl("_BBN", aml_int(0)));
181 aml_append(dev
, aml_name_decl("_ADR", aml_int(0)));
182 aml_append(dev
, aml_name_decl("_UID", aml_string("PCI0")));
183 aml_append(dev
, aml_name_decl("_STR", aml_unicode("PCIe 0 Device")));
184 aml_append(dev
, aml_name_decl("_CCA", aml_int(1)));
186 /* Declare the PCI Routing Table. */
187 Aml
*rt_pkg
= aml_package(nr_pcie_buses
* PCI_NUM_PINS
);
188 for (bus_no
= 0; bus_no
< nr_pcie_buses
; bus_no
++) {
189 for (i
= 0; i
< PCI_NUM_PINS
; i
++) {
190 int gsi
= (i
+ bus_no
) % PCI_NUM_PINS
;
191 Aml
*pkg
= aml_package(4);
192 aml_append(pkg
, aml_int((bus_no
<< 16) | 0xFFFF));
193 aml_append(pkg
, aml_int(i
));
194 aml_append(pkg
, aml_name("GSI%d", gsi
));
195 aml_append(pkg
, aml_int(0));
196 aml_append(rt_pkg
, pkg
);
199 aml_append(dev
, aml_name_decl("_PRT", rt_pkg
));
201 /* Create GSI link device */
202 for (i
= 0; i
< PCI_NUM_PINS
; i
++) {
203 uint32_t irqs
= irq
+ i
;
204 Aml
*dev_gsi
= aml_device("GSI%d", i
);
205 aml_append(dev_gsi
, aml_name_decl("_HID", aml_string("PNP0C0F")));
206 aml_append(dev_gsi
, aml_name_decl("_UID", aml_int(0)));
207 crs
= aml_resource_template();
209 aml_interrupt(AML_CONSUMER
, AML_LEVEL
, AML_ACTIVE_HIGH
,
210 AML_EXCLUSIVE
, &irqs
, 1));
211 aml_append(dev_gsi
, aml_name_decl("_PRS", crs
));
212 crs
= aml_resource_template();
214 aml_interrupt(AML_CONSUMER
, AML_LEVEL
, AML_ACTIVE_HIGH
,
215 AML_EXCLUSIVE
, &irqs
, 1));
216 aml_append(dev_gsi
, aml_name_decl("_CRS", crs
));
217 method
= aml_method("_SRS", 1, AML_NOTSERIALIZED
);
218 aml_append(dev_gsi
, method
);
219 aml_append(dev
, dev_gsi
);
222 method
= aml_method("_CBA", 0, AML_NOTSERIALIZED
);
223 aml_append(method
, aml_return(aml_int(base_ecam
)));
224 aml_append(dev
, method
);
226 method
= aml_method("_CRS", 0, AML_NOTSERIALIZED
);
227 Aml
*rbuf
= aml_resource_template();
229 aml_word_bus_number(AML_MIN_FIXED
, AML_MAX_FIXED
, AML_POS_DECODE
,
230 0x0000, 0x0000, nr_pcie_buses
- 1, 0x0000,
233 aml_dword_memory(AML_POS_DECODE
, AML_MIN_FIXED
, AML_MAX_FIXED
,
234 AML_NON_CACHEABLE
, AML_READ_WRITE
, 0x0000, base_mmio
,
235 base_mmio
+ size_mmio
- 1, 0x0000, size_mmio
));
237 aml_dword_io(AML_MIN_FIXED
, AML_MAX_FIXED
, AML_POS_DECODE
,
238 AML_ENTIRE_RANGE
, 0x0000, 0x0000, size_pio
- 1, base_pio
,
242 hwaddr base_mmio_high
= memmap
[VIRT_PCIE_MMIO_HIGH
].base
;
243 hwaddr size_mmio_high
= memmap
[VIRT_PCIE_MMIO_HIGH
].size
;
246 aml_qword_memory(AML_POS_DECODE
, AML_MIN_FIXED
, AML_MAX_FIXED
,
247 AML_NON_CACHEABLE
, AML_READ_WRITE
, 0x0000,
248 base_mmio_high
, base_mmio_high
, 0x0000,
252 aml_append(method
, aml_name_decl("RBUF", rbuf
));
253 aml_append(method
, aml_return(rbuf
));
254 aml_append(dev
, method
);
256 /* Declare an _OSC (OS Control Handoff) method */
257 aml_append(dev
, aml_name_decl("SUPP", aml_int(0)));
258 aml_append(dev
, aml_name_decl("CTRL", aml_int(0)));
259 method
= aml_method("_OSC", 4, AML_NOTSERIALIZED
);
261 aml_create_dword_field(aml_arg(3), aml_int(0), "CDW1"));
263 /* PCI Firmware Specification 3.0
264 * 4.5.1. _OSC Interface for PCI Host Bridge Devices
265 * The _OSC interface for a PCI/PCI-X/PCI Express hierarchy is
266 * identified by the Universal Unique IDentifier (UUID)
267 * 33DB4D5B-1FF7-401C-9657-7441C03DD766
269 UUID
= aml_touuid("33DB4D5B-1FF7-401C-9657-7441C03DD766");
270 ifctx
= aml_if(aml_equal(aml_arg(0), UUID
));
272 aml_create_dword_field(aml_arg(3), aml_int(4), "CDW2"));
274 aml_create_dword_field(aml_arg(3), aml_int(8), "CDW3"));
275 aml_append(ifctx
, aml_store(aml_name("CDW2"), aml_name("SUPP")));
276 aml_append(ifctx
, aml_store(aml_name("CDW3"), aml_name("CTRL")));
277 aml_append(ifctx
, aml_store(aml_and(aml_name("CTRL"), aml_int(0x1D)),
280 ifctx1
= aml_if(aml_lnot(aml_equal(aml_arg(1), aml_int(0x1))));
281 aml_append(ifctx1
, aml_store(aml_or(aml_name("CDW1"), aml_int(0x08)),
283 aml_append(ifctx
, ifctx1
);
285 ifctx1
= aml_if(aml_lnot(aml_equal(aml_name("CDW3"), aml_name("CTRL"))));
286 aml_append(ifctx1
, aml_store(aml_or(aml_name("CDW1"), aml_int(0x10)),
288 aml_append(ifctx
, ifctx1
);
290 aml_append(ifctx
, aml_store(aml_name("CTRL"), aml_name("CDW3")));
291 aml_append(ifctx
, aml_return(aml_arg(3)));
292 aml_append(method
, ifctx
);
294 elsectx
= aml_else();
295 aml_append(elsectx
, aml_store(aml_or(aml_name("CDW1"), aml_int(4)),
297 aml_append(elsectx
, aml_return(aml_arg(3)));
298 aml_append(method
, elsectx
);
299 aml_append(dev
, method
);
301 method
= aml_method("_DSM", 4, AML_NOTSERIALIZED
);
303 /* PCI Firmware Specification 3.0
304 * 4.6.1. _DSM for PCI Express Slot Information
305 * The UUID in _DSM in this context is
306 * {E5C937D0-3553-4D7A-9117-EA4D19C3434D}
308 UUID
= aml_touuid("E5C937D0-3553-4D7A-9117-EA4D19C3434D");
309 ifctx
= aml_if(aml_equal(aml_arg(0), UUID
));
310 ifctx1
= aml_if(aml_equal(aml_arg(2), aml_int(0)));
311 uint8_t byte_list
[1] = {1};
312 buf
= aml_buffer(1, byte_list
);
313 aml_append(ifctx1
, aml_return(buf
));
314 aml_append(ifctx
, ifctx1
);
315 aml_append(method
, ifctx
);
318 buf
= aml_buffer(1, byte_list
);
319 aml_append(method
, aml_return(buf
));
320 aml_append(dev
, method
);
322 Aml
*dev_rp0
= aml_device("%s", "RP0");
323 aml_append(dev_rp0
, aml_name_decl("_ADR", aml_int(0)));
324 aml_append(dev
, dev_rp0
);
325 aml_append(scope
, dev
);
328 static void acpi_dsdt_add_gpio(Aml
*scope
, const MemMapEntry
*gpio_memmap
,
331 Aml
*dev
= aml_device("GPO0");
332 aml_append(dev
, aml_name_decl("_HID", aml_string("ARMH0061")));
333 aml_append(dev
, aml_name_decl("_ADR", aml_int(0)));
334 aml_append(dev
, aml_name_decl("_UID", aml_int(0)));
336 Aml
*crs
= aml_resource_template();
337 aml_append(crs
, aml_memory32_fixed(gpio_memmap
->base
, gpio_memmap
->size
,
339 aml_append(crs
, aml_interrupt(AML_CONSUMER
, AML_LEVEL
, AML_ACTIVE_HIGH
,
340 AML_EXCLUSIVE
, &gpio_irq
, 1));
341 aml_append(dev
, aml_name_decl("_CRS", crs
));
343 Aml
*aei
= aml_resource_template();
344 /* Pin 3 for power button */
345 const uint32_t pin_list
[1] = {3};
346 aml_append(aei
, aml_gpio_int(AML_CONSUMER
, AML_EDGE
, AML_ACTIVE_HIGH
,
347 AML_EXCLUSIVE
, AML_PULL_UP
, 0, pin_list
, 1,
349 aml_append(dev
, aml_name_decl("_AEI", aei
));
351 /* _E03 is handle for power button */
352 Aml
*method
= aml_method("_E03", 0, AML_NOTSERIALIZED
);
353 aml_append(method
, aml_notify(aml_name(ACPI_POWER_BUTTON_DEVICE
),
355 aml_append(dev
, method
);
356 aml_append(scope
, dev
);
359 static void acpi_dsdt_add_power_button(Aml
*scope
)
361 Aml
*dev
= aml_device(ACPI_POWER_BUTTON_DEVICE
);
362 aml_append(dev
, aml_name_decl("_HID", aml_string("PNP0C0C")));
363 aml_append(dev
, aml_name_decl("_ADR", aml_int(0)));
364 aml_append(dev
, aml_name_decl("_UID", aml_int(0)));
365 aml_append(scope
, dev
);
370 build_rsdp(GArray
*rsdp_table
, GArray
*linker
, unsigned rsdt
)
372 AcpiRsdpDescriptor
*rsdp
= acpi_data_push(rsdp_table
, sizeof *rsdp
);
374 bios_linker_loader_alloc(linker
, ACPI_BUILD_RSDP_FILE
, 16,
375 true /* fseg memory */);
377 memcpy(&rsdp
->signature
, "RSD PTR ", sizeof(rsdp
->signature
));
378 memcpy(rsdp
->oem_id
, ACPI_BUILD_APPNAME6
, sizeof(rsdp
->oem_id
));
379 rsdp
->length
= cpu_to_le32(sizeof(*rsdp
));
380 rsdp
->revision
= 0x02;
383 rsdp
->rsdt_physical_address
= cpu_to_le32(rsdt
);
384 /* Address to be filled by Guest linker */
385 bios_linker_loader_add_pointer(linker
, ACPI_BUILD_RSDP_FILE
,
386 ACPI_BUILD_TABLE_FILE
,
387 rsdp_table
, &rsdp
->rsdt_physical_address
,
388 sizeof rsdp
->rsdt_physical_address
);
390 /* Checksum to be filled by Guest linker */
391 bios_linker_loader_add_checksum(linker
, ACPI_BUILD_RSDP_FILE
,
392 rsdp
, rsdp
, sizeof *rsdp
, &rsdp
->checksum
);
398 build_spcr(GArray
*table_data
, GArray
*linker
, VirtGuestInfo
*guest_info
)
400 AcpiSerialPortConsoleRedirection
*spcr
;
401 const MemMapEntry
*uart_memmap
= &guest_info
->memmap
[VIRT_UART
];
402 int irq
= guest_info
->irqmap
[VIRT_UART
] + ARM_SPI_BASE
;
404 spcr
= acpi_data_push(table_data
, sizeof(*spcr
));
406 spcr
->interface_type
= 0x3; /* ARM PL011 UART */
408 spcr
->base_address
.space_id
= AML_SYSTEM_MEMORY
;
409 spcr
->base_address
.bit_width
= 8;
410 spcr
->base_address
.bit_offset
= 0;
411 spcr
->base_address
.access_width
= 1;
412 spcr
->base_address
.address
= cpu_to_le64(uart_memmap
->base
);
414 spcr
->interrupt_types
= (1 << 3); /* Bit[3] ARMH GIC interrupt */
415 spcr
->gsi
= cpu_to_le32(irq
); /* Global System Interrupt */
417 spcr
->baud
= 3; /* Baud Rate: 3 = 9600 */
418 spcr
->parity
= 0; /* No Parity */
419 spcr
->stopbits
= 1; /* 1 Stop bit */
420 spcr
->flowctrl
= (1 << 1); /* Bit[1] = RTS/CTS hardware flow control */
421 spcr
->term_type
= 0; /* Terminal Type: 0 = VT100 */
423 spcr
->pci_device_id
= 0xffff; /* PCI Device ID: not a PCI device */
424 spcr
->pci_vendor_id
= 0xffff; /* PCI Vendor ID: not a PCI device */
426 build_header(linker
, table_data
, (void *)spcr
, "SPCR", sizeof(*spcr
), 2,
431 build_mcfg(GArray
*table_data
, GArray
*linker
, VirtGuestInfo
*guest_info
)
434 const MemMapEntry
*memmap
= guest_info
->memmap
;
435 int len
= sizeof(*mcfg
) + sizeof(mcfg
->allocation
[0]);
437 mcfg
= acpi_data_push(table_data
, len
);
438 mcfg
->allocation
[0].address
= cpu_to_le64(memmap
[VIRT_PCIE_ECAM
].base
);
440 /* Only a single allocation so no need to play with segments */
441 mcfg
->allocation
[0].pci_segment
= cpu_to_le16(0);
442 mcfg
->allocation
[0].start_bus_number
= 0;
443 mcfg
->allocation
[0].end_bus_number
= (memmap
[VIRT_PCIE_ECAM
].size
444 / PCIE_MMCFG_SIZE_MIN
) - 1;
446 build_header(linker
, table_data
, (void *)mcfg
, "MCFG", len
, 1, NULL
);
451 build_gtdt(GArray
*table_data
, GArray
*linker
)
453 int gtdt_start
= table_data
->len
;
454 AcpiGenericTimerTable
*gtdt
;
456 gtdt
= acpi_data_push(table_data
, sizeof *gtdt
);
457 /* The interrupt values are the same with the device tree when adding 16 */
458 gtdt
->secure_el1_interrupt
= ARCH_TIMER_S_EL1_IRQ
+ 16;
459 gtdt
->secure_el1_flags
= ACPI_EDGE_SENSITIVE
;
461 gtdt
->non_secure_el1_interrupt
= ARCH_TIMER_NS_EL1_IRQ
+ 16;
462 gtdt
->non_secure_el1_flags
= ACPI_EDGE_SENSITIVE
;
464 gtdt
->virtual_timer_interrupt
= ARCH_TIMER_VIRT_IRQ
+ 16;
465 gtdt
->virtual_timer_flags
= ACPI_EDGE_SENSITIVE
;
467 gtdt
->non_secure_el2_interrupt
= ARCH_TIMER_NS_EL2_IRQ
+ 16;
468 gtdt
->non_secure_el2_flags
= ACPI_EDGE_SENSITIVE
;
470 build_header(linker
, table_data
,
471 (void *)(table_data
->data
+ gtdt_start
), "GTDT",
472 table_data
->len
- gtdt_start
, 2, NULL
);
477 build_madt(GArray
*table_data
, GArray
*linker
, VirtGuestInfo
*guest_info
,
478 VirtAcpiCpuInfo
*cpuinfo
)
480 int madt_start
= table_data
->len
;
481 const MemMapEntry
*memmap
= guest_info
->memmap
;
482 const int *irqmap
= guest_info
->irqmap
;
483 AcpiMultipleApicTable
*madt
;
484 AcpiMadtGenericDistributor
*gicd
;
485 AcpiMadtGenericMsiFrame
*gic_msi
;
488 madt
= acpi_data_push(table_data
, sizeof *madt
);
490 gicd
= acpi_data_push(table_data
, sizeof *gicd
);
491 gicd
->type
= ACPI_APIC_GENERIC_DISTRIBUTOR
;
492 gicd
->length
= sizeof(*gicd
);
493 gicd
->base_address
= memmap
[VIRT_GIC_DIST
].base
;
495 for (i
= 0; i
< guest_info
->smp_cpus
; i
++) {
496 AcpiMadtGenericInterrupt
*gicc
= acpi_data_push(table_data
,
498 ARMCPU
*armcpu
= ARM_CPU(qemu_get_cpu(i
));
500 gicc
->type
= ACPI_APIC_GENERIC_INTERRUPT
;
501 gicc
->length
= sizeof(*gicc
);
502 if (guest_info
->gic_version
== 2) {
503 gicc
->base_address
= memmap
[VIRT_GIC_CPU
].base
;
505 gicc
->cpu_interface_number
= i
;
506 gicc
->arm_mpidr
= armcpu
->mp_affinity
;
508 if (test_bit(i
, cpuinfo
->found_cpus
)) {
509 gicc
->flags
= cpu_to_le32(ACPI_GICC_ENABLED
);
513 if (guest_info
->gic_version
== 3) {
514 AcpiMadtGenericRedistributor
*gicr
= acpi_data_push(table_data
,
517 gicr
->type
= ACPI_APIC_GENERIC_REDISTRIBUTOR
;
518 gicr
->length
= sizeof(*gicr
);
519 gicr
->base_address
= cpu_to_le64(memmap
[VIRT_GIC_REDIST
].base
);
520 gicr
->range_length
= cpu_to_le32(memmap
[VIRT_GIC_REDIST
].size
);
522 gic_msi
= acpi_data_push(table_data
, sizeof *gic_msi
);
523 gic_msi
->type
= ACPI_APIC_GENERIC_MSI_FRAME
;
524 gic_msi
->length
= sizeof(*gic_msi
);
525 gic_msi
->gic_msi_frame_id
= 0;
526 gic_msi
->base_address
= cpu_to_le64(memmap
[VIRT_GIC_V2M
].base
);
527 gic_msi
->flags
= cpu_to_le32(1);
528 gic_msi
->spi_count
= cpu_to_le16(NUM_GICV2M_SPIS
);
529 gic_msi
->spi_base
= cpu_to_le16(irqmap
[VIRT_GIC_V2M
] + ARM_SPI_BASE
);
532 build_header(linker
, table_data
,
533 (void *)(table_data
->data
+ madt_start
), "APIC",
534 table_data
->len
- madt_start
, 3, NULL
);
539 build_fadt(GArray
*table_data
, GArray
*linker
, unsigned dsdt
)
541 AcpiFadtDescriptorRev5_1
*fadt
= acpi_data_push(table_data
, sizeof(*fadt
));
543 /* Hardware Reduced = 1 and use PSCI 0.2+ and with HVC */
544 fadt
->flags
= cpu_to_le32(1 << ACPI_FADT_F_HW_REDUCED_ACPI
);
545 fadt
->arm_boot_flags
= cpu_to_le16((1 << ACPI_FADT_ARM_USE_PSCI_G_0_2
) |
546 (1 << ACPI_FADT_ARM_PSCI_USE_HVC
));
548 /* ACPI v5.1 (fadt->revision.fadt->minor_revision) */
549 fadt
->minor_revision
= 0x1;
551 fadt
->dsdt
= cpu_to_le32(dsdt
);
552 /* DSDT address to be filled by Guest linker */
553 bios_linker_loader_add_pointer(linker
, ACPI_BUILD_TABLE_FILE
,
554 ACPI_BUILD_TABLE_FILE
,
555 table_data
, &fadt
->dsdt
,
558 build_header(linker
, table_data
,
559 (void *)fadt
, "FACP", sizeof(*fadt
), 5, NULL
);
564 build_dsdt(GArray
*table_data
, GArray
*linker
, VirtGuestInfo
*guest_info
)
567 const MemMapEntry
*memmap
= guest_info
->memmap
;
568 const int *irqmap
= guest_info
->irqmap
;
570 dsdt
= init_aml_allocator();
571 /* Reserve space for header */
572 acpi_data_push(dsdt
->buf
, sizeof(AcpiTableHeader
));
574 scope
= aml_scope("\\_SB");
575 acpi_dsdt_add_cpus(scope
, guest_info
->smp_cpus
);
576 acpi_dsdt_add_uart(scope
, &memmap
[VIRT_UART
],
577 (irqmap
[VIRT_UART
] + ARM_SPI_BASE
));
578 acpi_dsdt_add_rtc(scope
, &memmap
[VIRT_RTC
],
579 (irqmap
[VIRT_RTC
] + ARM_SPI_BASE
));
580 acpi_dsdt_add_flash(scope
, &memmap
[VIRT_FLASH
]);
581 acpi_dsdt_add_virtio(scope
, &memmap
[VIRT_MMIO
],
582 (irqmap
[VIRT_MMIO
] + ARM_SPI_BASE
), NUM_VIRTIO_TRANSPORTS
);
583 acpi_dsdt_add_pci(scope
, memmap
, (irqmap
[VIRT_PCIE
] + ARM_SPI_BASE
),
584 guest_info
->use_highmem
);
585 acpi_dsdt_add_gpio(scope
, &memmap
[VIRT_GPIO
],
586 (irqmap
[VIRT_GPIO
] + ARM_SPI_BASE
));
587 acpi_dsdt_add_power_button(scope
);
589 aml_append(dsdt
, scope
);
591 /* copy AML table into ACPI tables blob and patch header there */
592 g_array_append_vals(table_data
, dsdt
->buf
->data
, dsdt
->buf
->len
);
593 build_header(linker
, table_data
,
594 (void *)(table_data
->data
+ table_data
->len
- dsdt
->buf
->len
),
595 "DSDT", dsdt
->buf
->len
, 2, NULL
);
596 free_aml_allocator();
600 struct AcpiBuildState
{
601 /* Copy of table in RAM (for patching). */
602 MemoryRegion
*table_mr
;
603 MemoryRegion
*rsdp_mr
;
604 MemoryRegion
*linker_mr
;
605 /* Is table patched? */
607 VirtGuestInfo
*guest_info
;
611 void virt_acpi_build(VirtGuestInfo
*guest_info
, AcpiBuildTables
*tables
)
613 GArray
*table_offsets
;
615 VirtAcpiCpuInfo cpuinfo
;
616 GArray
*tables_blob
= tables
->table_data
;
618 virt_acpi_get_cpu_info(&cpuinfo
);
620 table_offsets
= g_array_new(false, true /* clear */,
623 bios_linker_loader_alloc(tables
->linker
, ACPI_BUILD_TABLE_FILE
,
624 64, false /* high memory */);
627 * The ACPI v5.1 tables for Hardware-reduced ACPI platform are:
637 /* DSDT is pointed to by FADT */
638 dsdt
= tables_blob
->len
;
639 build_dsdt(tables_blob
, tables
->linker
, guest_info
);
641 /* FADT MADT GTDT MCFG SPCR pointed to by RSDT */
642 acpi_add_table(table_offsets
, tables_blob
);
643 build_fadt(tables_blob
, tables
->linker
, dsdt
);
645 acpi_add_table(table_offsets
, tables_blob
);
646 build_madt(tables_blob
, tables
->linker
, guest_info
, &cpuinfo
);
648 acpi_add_table(table_offsets
, tables_blob
);
649 build_gtdt(tables_blob
, tables
->linker
);
651 acpi_add_table(table_offsets
, tables_blob
);
652 build_mcfg(tables_blob
, tables
->linker
, guest_info
);
654 acpi_add_table(table_offsets
, tables_blob
);
655 build_spcr(tables_blob
, tables
->linker
, guest_info
);
657 /* RSDT is pointed to by RSDP */
658 rsdt
= tables_blob
->len
;
659 build_rsdt(tables_blob
, tables
->linker
, table_offsets
);
661 /* RSDP is in FSEG memory, so allocate it separately */
662 build_rsdp(tables
->rsdp
, tables
->linker
, rsdt
);
664 /* Cleanup memory that's no longer used. */
665 g_array_free(table_offsets
, true);
668 static void acpi_ram_update(MemoryRegion
*mr
, GArray
*data
)
670 uint32_t size
= acpi_data_len(data
);
672 /* Make sure RAM size is correct - in case it got changed
673 * e.g. by migration */
674 memory_region_ram_resize(mr
, size
, &error_abort
);
676 memcpy(memory_region_get_ram_ptr(mr
), data
->data
, size
);
677 memory_region_set_dirty(mr
, 0, size
);
680 static void virt_acpi_build_update(void *build_opaque
)
682 AcpiBuildState
*build_state
= build_opaque
;
683 AcpiBuildTables tables
;
685 /* No state to update or already patched? Nothing to do. */
686 if (!build_state
|| build_state
->patched
) {
689 build_state
->patched
= true;
691 acpi_build_tables_init(&tables
);
693 virt_acpi_build(build_state
->guest_info
, &tables
);
695 acpi_ram_update(build_state
->table_mr
, tables
.table_data
);
696 acpi_ram_update(build_state
->rsdp_mr
, tables
.rsdp
);
697 acpi_ram_update(build_state
->linker_mr
, tables
.linker
);
700 acpi_build_tables_cleanup(&tables
, true);
703 static void virt_acpi_build_reset(void *build_opaque
)
705 AcpiBuildState
*build_state
= build_opaque
;
706 build_state
->patched
= false;
709 static MemoryRegion
*acpi_add_rom_blob(AcpiBuildState
*build_state
,
710 GArray
*blob
, const char *name
,
713 return rom_add_blob(name
, blob
->data
, acpi_data_len(blob
), max_size
, -1,
714 name
, virt_acpi_build_update
, build_state
);
717 static const VMStateDescription vmstate_virt_acpi_build
= {
718 .name
= "virt_acpi_build",
720 .minimum_version_id
= 1,
721 .fields
= (VMStateField
[]) {
722 VMSTATE_BOOL(patched
, AcpiBuildState
),
723 VMSTATE_END_OF_LIST()
727 void virt_acpi_setup(VirtGuestInfo
*guest_info
)
729 AcpiBuildTables tables
;
730 AcpiBuildState
*build_state
;
732 if (!guest_info
->fw_cfg
) {
733 trace_virt_acpi_setup();
738 trace_virt_acpi_setup();
742 build_state
= g_malloc0(sizeof *build_state
);
743 build_state
->guest_info
= guest_info
;
745 acpi_build_tables_init(&tables
);
746 virt_acpi_build(build_state
->guest_info
, &tables
);
748 /* Now expose it all to Guest */
749 build_state
->table_mr
= acpi_add_rom_blob(build_state
, tables
.table_data
,
750 ACPI_BUILD_TABLE_FILE
,
751 ACPI_BUILD_TABLE_MAX_SIZE
);
752 assert(build_state
->table_mr
!= NULL
);
754 build_state
->linker_mr
=
755 acpi_add_rom_blob(build_state
, tables
.linker
, "etc/table-loader", 0);
757 fw_cfg_add_file(guest_info
->fw_cfg
, ACPI_BUILD_TPMLOG_FILE
,
758 tables
.tcpalog
->data
, acpi_data_len(tables
.tcpalog
));
760 build_state
->rsdp_mr
= acpi_add_rom_blob(build_state
, tables
.rsdp
,
761 ACPI_BUILD_RSDP_FILE
, 0);
763 qemu_register_reset(virt_acpi_build_reset
, build_state
);
764 virt_acpi_build_reset(build_state
);
765 vmstate_register(NULL
, 0, &vmstate_virt_acpi_build
, build_state
);
767 /* Cleanup tables but don't free the memory: we track it
770 acpi_build_tables_cleanup(&tables
, false);