|
| 1 | +/*- |
| 2 | + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD |
| 3 | + * |
| 4 | + * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org> |
| 5 | + * Copyright (c) 2018 Intel Corporation |
| 6 | + * All rights reserved. |
| 7 | + * |
| 8 | + * Redistribution and use in source and binary forms, with or without |
| 9 | + * modification, are permitted provided that the following conditions |
| 10 | + * are met: |
| 11 | + * 1. Redistributions of source code must retain the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer. |
| 13 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | + * notice, this list of conditions and the following disclaimer in the |
| 15 | + * documentation and/or other materials provided with the distribution. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | + * SUCH DAMAGE. |
| 28 | + */ |
| 29 | +#include <types.h> |
| 30 | +#include <rtl.h> |
| 31 | +#include <vboot.h> |
| 32 | +#include "acpi.h" |
| 33 | +#include <pgtable.h> |
| 34 | +#include <ioapic.h> |
| 35 | +#include <logmsg.h> |
| 36 | +#include <host_pm.h> |
| 37 | +#include <acrn_common.h> |
| 38 | + |
| 39 | +#ifndef CONFIG_CONSTANT_ACPI |
| 40 | +/* Per ACPI spec: |
| 41 | + * There are two fundamental types of ACPI tables: |
| 42 | + * |
| 43 | + * Tables that contain AML code produced from the ACPI Source Language (ASL). |
| 44 | + * These include the DSDT, any SSDTs, and sometimes OEM-specific tables (OEMx). |
| 45 | + * |
| 46 | + * Tables that contain simple data and no AML byte code. These types of tables |
| 47 | + * are known as ACPI Data Tables. They include tables such as the FADT, MADT, |
| 48 | + * ECDT, SRAT, etc. -essentially any table other than a DSDT or SSDT. |
| 49 | + * |
| 50 | + * The second type of table, the ACPI Data Table, could be parsed here. |
| 51 | + * |
| 52 | + * When ACRN go FuSa, the platform ACPI data should be fixed. The MACRO of |
| 53 | + * CONFIG_CONSTANT_ACPI will be defined, then this code is not needed. |
| 54 | + */ |
| 55 | + |
| 56 | +#define ACPI_SIG_FACS 0x53434146U /* "FACS" */ |
| 57 | +#define ACPI_SIG_FADT "FACP" /* Fixed ACPI Description Table */ |
| 58 | + |
| 59 | +/* FACP field offsets */ |
| 60 | +#define OFFSET_FACS_ADDR 36U |
| 61 | +#define OFFSET_FACS_X_ADDR 132U |
| 62 | +#define OFFSET_PM1A_EVT 148U |
| 63 | +#define OFFSET_PM1A_CNT 172U |
| 64 | + |
| 65 | +/* FACS field offsets */ |
| 66 | +#define OFFSET_FACS_SIGNATURE 0U |
| 67 | +#define OFFSET_FACS_LENGTH 4U |
| 68 | +#define OFFSET_WAKE_VECTOR_32 12U |
| 69 | +#define OFFSET_WAKE_VECTOR_64 24U |
| 70 | + |
| 71 | +/* get a dword value from given table and its offset */ |
| 72 | +static inline uint32_t get_acpi_dt_dword(const uint8_t *dt_addr, uint32_t dt_offset) |
| 73 | +{ |
| 74 | + return *(uint32_t *)(dt_addr + dt_offset); |
| 75 | +} |
| 76 | + |
| 77 | +/* get a qword value from given table and its offset */ |
| 78 | +static inline uint64_t get_acpi_dt_qword(const uint8_t *dt_addr, uint32_t dt_offset) |
| 79 | +{ |
| 80 | + return *(uint64_t *)(dt_addr + dt_offset); |
| 81 | +} |
| 82 | + |
| 83 | +struct packed_gas { |
| 84 | + uint8_t space_id; |
| 85 | + uint8_t bit_width; |
| 86 | + uint8_t bit_offset; |
| 87 | + uint8_t access_size; |
| 88 | + uint64_t address; |
| 89 | +} __attribute__((packed)); |
| 90 | + |
| 91 | +/* get a GAS struct from given table and its offset. |
| 92 | + * ACPI table stores packed gas, but it is not guaranteed that |
| 93 | + * struct acpi_generic_address is packed, so do not use memcpy in function. |
| 94 | + * @pre dt_addr != NULL && gas != NULL |
| 95 | + */ |
| 96 | +static inline void get_acpi_dt_gas(const uint8_t *dt_addr, uint32_t dt_offset, struct acpi_generic_address *gas) |
| 97 | +{ |
| 98 | + struct packed_gas *dt_gas = (struct packed_gas *)(dt_addr + dt_offset); |
| 99 | + |
| 100 | + gas->space_id = dt_gas->space_id; |
| 101 | + gas->bit_width = dt_gas->bit_width; |
| 102 | + gas->bit_offset = dt_gas->bit_offset; |
| 103 | + gas->access_size = dt_gas->access_size; |
| 104 | + gas->address = dt_gas->address; |
| 105 | +} |
| 106 | + |
| 107 | +/* @pre facp_addr != NULL */ |
| 108 | +static void *get_facs_table(const uint8_t *facp_addr) |
| 109 | +{ |
| 110 | + uint8_t *facs_addr, *facs_x_addr; |
| 111 | + uint32_t signature, length; |
| 112 | + |
| 113 | + facs_addr = (uint8_t *)(uint64_t)get_acpi_dt_dword(facp_addr, OFFSET_FACS_ADDR); |
| 114 | + |
| 115 | + facs_x_addr = (uint8_t *)get_acpi_dt_qword(facp_addr, OFFSET_FACS_X_ADDR); |
| 116 | + |
| 117 | + if (facs_x_addr != NULL) { |
| 118 | + facs_addr = facs_x_addr; |
| 119 | + } |
| 120 | + |
| 121 | + if (facs_addr != NULL) { |
| 122 | + signature = get_acpi_dt_dword(facs_addr, OFFSET_FACS_SIGNATURE); |
| 123 | + |
| 124 | + if (signature != ACPI_SIG_FACS) { |
| 125 | + facs_addr = NULL; |
| 126 | + } else { |
| 127 | + length = get_acpi_dt_dword(facs_addr, OFFSET_FACS_LENGTH); |
| 128 | + |
| 129 | + if (length < 64U) { |
| 130 | + facs_addr = NULL; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + return (void *)facs_addr; |
| 135 | +} |
| 136 | + |
| 137 | +/* put all ACPI fix up code here */ |
| 138 | +void acpi_fixup(void) |
| 139 | +{ |
| 140 | + uint8_t *facp_addr, *facs_addr; |
| 141 | + struct acpi_generic_address pm1a_cnt, pm1a_evt; |
| 142 | + struct pm_s_state_data *sx_data = get_host_sstate_data(); |
| 143 | + |
| 144 | + facp_addr = (uint8_t *)get_acpi_tbl(ACPI_SIG_FADT); |
| 145 | + |
| 146 | + if (facp_addr != NULL) { |
| 147 | + get_acpi_dt_gas(facp_addr, OFFSET_PM1A_EVT, &pm1a_evt); |
| 148 | + get_acpi_dt_gas(facp_addr, OFFSET_PM1A_CNT, &pm1a_cnt); |
| 149 | + (void)memcpy_s((void *)&sx_data->pm1a_evt, sizeof(struct acpi_generic_address), |
| 150 | + (const void *)&pm1a_evt, sizeof(struct acpi_generic_address)); |
| 151 | + (void)memcpy_s((void *)&sx_data->pm1a_cnt, sizeof(struct acpi_generic_address), |
| 152 | + (const void *)&pm1a_cnt, sizeof(struct acpi_generic_address)); |
| 153 | + |
| 154 | + facs_addr = (uint8_t *)get_facs_table(facp_addr); |
| 155 | + if (facs_addr != NULL) { |
| 156 | + sx_data->wake_vector_32 = (uint32_t *)(facs_addr + OFFSET_WAKE_VECTOR_32); |
| 157 | + sx_data->wake_vector_64 = (uint64_t *)(facs_addr + OFFSET_WAKE_VECTOR_64); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
| 161 | +#endif |
0 commit comments