Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Based on acpi.c from coreboot |
| 3 | * |
| 4 | * Copyright (C) 2015, Saket Sinha <saket.sinha89@gmail.com> |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 5 | * Copyright (C) 2016, Bin Meng <bmeng.cn@gmail.com> |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 6 | * |
| 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <cpu.h> |
| 12 | #include <dm.h> |
| 13 | #include <dm/uclass-internal.h> |
Bin Meng | d9050c6 | 2016-06-17 02:13:16 -0700 | [diff] [blame] | 14 | #include <asm/acpi/global_nvs.h> |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 15 | #include <asm/acpi_table.h> |
Bin Meng | e259eb2 | 2016-05-11 07:45:03 -0700 | [diff] [blame] | 16 | #include <asm/io.h> |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 17 | #include <asm/lapic.h> |
| 18 | #include <asm/tables.h> |
Bin Meng | d9050c6 | 2016-06-17 02:13:16 -0700 | [diff] [blame] | 19 | #include <asm/arch/global_nvs.h> |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 20 | |
| 21 | /* |
Bin Meng | b063d5f | 2016-05-07 07:46:24 -0700 | [diff] [blame] | 22 | * IASL compiles the dsdt entries and writes the hex values |
| 23 | * to a C array AmlCode[] (see dsdt.c). |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 24 | */ |
| 25 | extern const unsigned char AmlCode[]; |
| 26 | |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 27 | static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt, |
| 28 | struct acpi_xsdt *xsdt) |
| 29 | { |
| 30 | memset(rsdp, 0, sizeof(struct acpi_rsdp)); |
| 31 | |
| 32 | memcpy(rsdp->signature, RSDP_SIG, 8); |
| 33 | memcpy(rsdp->oem_id, OEM_ID, 6); |
| 34 | |
| 35 | rsdp->length = sizeof(struct acpi_rsdp); |
| 36 | rsdp->rsdt_address = (u32)rsdt; |
| 37 | |
| 38 | /* |
| 39 | * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2 |
| 40 | * |
| 41 | * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2. |
| 42 | * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR |
| 43 | * revision 0) |
| 44 | */ |
| 45 | if (xsdt == NULL) { |
| 46 | rsdp->revision = ACPI_RSDP_REV_ACPI_1_0; |
| 47 | } else { |
| 48 | rsdp->xsdt_address = (u64)(u32)xsdt; |
| 49 | rsdp->revision = ACPI_RSDP_REV_ACPI_2_0; |
| 50 | } |
| 51 | |
| 52 | /* Calculate checksums */ |
| 53 | rsdp->checksum = table_compute_checksum((void *)rsdp, 20); |
| 54 | rsdp->ext_checksum = table_compute_checksum((void *)rsdp, |
| 55 | sizeof(struct acpi_rsdp)); |
| 56 | } |
| 57 | |
| 58 | void acpi_fill_header(struct acpi_table_header *header, char *signature) |
| 59 | { |
| 60 | memcpy(header->signature, signature, 4); |
| 61 | memcpy(header->oem_id, OEM_ID, 6); |
| 62 | memcpy(header->oem_table_id, OEM_TABLE_ID, 8); |
| 63 | memcpy(header->aslc_id, ASLC_ID, 4); |
| 64 | } |
| 65 | |
| 66 | static void acpi_write_rsdt(struct acpi_rsdt *rsdt) |
| 67 | { |
| 68 | struct acpi_table_header *header = &(rsdt->header); |
| 69 | |
| 70 | /* Fill out header fields */ |
| 71 | acpi_fill_header(header, "RSDT"); |
| 72 | header->length = sizeof(struct acpi_rsdt); |
Bin Meng | f662fe4 | 2016-05-07 07:46:28 -0700 | [diff] [blame] | 73 | header->revision = 1; |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 74 | |
| 75 | /* Entries are filled in later, we come with an empty set */ |
| 76 | |
| 77 | /* Fix checksum */ |
| 78 | header->checksum = table_compute_checksum((void *)rsdt, |
| 79 | sizeof(struct acpi_rsdt)); |
| 80 | } |
| 81 | |
| 82 | static void acpi_write_xsdt(struct acpi_xsdt *xsdt) |
| 83 | { |
| 84 | struct acpi_table_header *header = &(xsdt->header); |
| 85 | |
| 86 | /* Fill out header fields */ |
| 87 | acpi_fill_header(header, "XSDT"); |
| 88 | header->length = sizeof(struct acpi_xsdt); |
Bin Meng | f662fe4 | 2016-05-07 07:46:28 -0700 | [diff] [blame] | 89 | header->revision = 1; |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 90 | |
| 91 | /* Entries are filled in later, we come with an empty set */ |
| 92 | |
| 93 | /* Fix checksum */ |
| 94 | header->checksum = table_compute_checksum((void *)xsdt, |
| 95 | sizeof(struct acpi_xsdt)); |
| 96 | } |
| 97 | |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 98 | /** |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 99 | * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length |
| 100 | * and checksum. |
| 101 | */ |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 102 | static void acpi_add_table(struct acpi_rsdp *rsdp, void *table) |
| 103 | { |
| 104 | int i, entries_num; |
| 105 | struct acpi_rsdt *rsdt; |
| 106 | struct acpi_xsdt *xsdt = NULL; |
| 107 | |
| 108 | /* The RSDT is mandatory while the XSDT is not */ |
| 109 | rsdt = (struct acpi_rsdt *)rsdp->rsdt_address; |
| 110 | |
| 111 | if (rsdp->xsdt_address) |
| 112 | xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address); |
| 113 | |
| 114 | /* This should always be MAX_ACPI_TABLES */ |
| 115 | entries_num = ARRAY_SIZE(rsdt->entry); |
| 116 | |
| 117 | for (i = 0; i < entries_num; i++) { |
| 118 | if (rsdt->entry[i] == 0) |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | if (i >= entries_num) { |
Bin Meng | d2d2218 | 2016-05-07 07:46:12 -0700 | [diff] [blame] | 123 | debug("ACPI: Error: too many tables\n"); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 124 | return; |
| 125 | } |
| 126 | |
| 127 | /* Add table to the RSDT */ |
| 128 | rsdt->entry[i] = (u32)table; |
| 129 | |
| 130 | /* Fix RSDT length or the kernel will assume invalid entries */ |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 131 | rsdt->header.length = sizeof(struct acpi_table_header) + |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 132 | (sizeof(u32) * (i + 1)); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 133 | |
| 134 | /* Re-calculate checksum */ |
| 135 | rsdt->header.checksum = 0; |
| 136 | rsdt->header.checksum = table_compute_checksum((u8 *)rsdt, |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 137 | rsdt->header.length); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 138 | |
| 139 | /* |
| 140 | * And now the same thing for the XSDT. We use the same index as for |
| 141 | * now we want the XSDT and RSDT to always be in sync in U-Boot |
| 142 | */ |
| 143 | if (xsdt) { |
| 144 | /* Add table to the XSDT */ |
| 145 | xsdt->entry[i] = (u64)(u32)table; |
| 146 | |
| 147 | /* Fix XSDT length */ |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 148 | xsdt->header.length = sizeof(struct acpi_table_header) + |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 149 | (sizeof(u64) * (i + 1)); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 150 | |
| 151 | /* Re-calculate checksum */ |
| 152 | xsdt->header.checksum = 0; |
| 153 | xsdt->header.checksum = table_compute_checksum((u8 *)xsdt, |
| 154 | xsdt->header.length); |
| 155 | } |
| 156 | } |
| 157 | |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 158 | static void acpi_create_facs(struct acpi_facs *facs) |
| 159 | { |
| 160 | memset((void *)facs, 0, sizeof(struct acpi_facs)); |
| 161 | |
| 162 | memcpy(facs->signature, "FACS", 4); |
| 163 | facs->length = sizeof(struct acpi_facs); |
| 164 | facs->hardware_signature = 0; |
| 165 | facs->firmware_waking_vector = 0; |
| 166 | facs->global_lock = 0; |
| 167 | facs->flags = 0; |
| 168 | facs->x_firmware_waking_vector_l = 0; |
| 169 | facs->x_firmware_waking_vector_h = 0; |
| 170 | facs->version = 1; |
| 171 | } |
| 172 | |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 173 | static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic, |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 174 | u8 cpu, u8 apic) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 175 | { |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 176 | lapic->type = ACPI_APIC_LAPIC; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 177 | lapic->length = sizeof(struct acpi_madt_lapic); |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 178 | lapic->flags = LOCAL_APIC_FLAG_ENABLED; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 179 | lapic->processor_id = cpu; |
| 180 | lapic->apic_id = apic; |
| 181 | |
| 182 | return lapic->length; |
| 183 | } |
| 184 | |
Bin Meng | 3c5234e | 2016-05-07 07:46:30 -0700 | [diff] [blame] | 185 | int acpi_create_madt_lapics(u32 current) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 186 | { |
| 187 | struct udevice *dev; |
George McCollister | 5a49f87 | 2016-06-07 13:40:18 -0500 | [diff] [blame] | 188 | int total_length = 0; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 189 | |
| 190 | for (uclass_find_first_device(UCLASS_CPU, &dev); |
| 191 | dev; |
| 192 | uclass_find_next_device(&dev)) { |
| 193 | struct cpu_platdata *plat = dev_get_parent_platdata(dev); |
George McCollister | 5a49f87 | 2016-06-07 13:40:18 -0500 | [diff] [blame] | 194 | int length = acpi_create_madt_lapic( |
| 195 | (struct acpi_madt_lapic *)current, |
| 196 | plat->cpu_id, plat->cpu_id); |
Bin Meng | 3c5234e | 2016-05-07 07:46:30 -0700 | [diff] [blame] | 197 | current += length; |
George McCollister | 5a49f87 | 2016-06-07 13:40:18 -0500 | [diff] [blame] | 198 | total_length += length; |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 199 | } |
| 200 | |
George McCollister | 5a49f87 | 2016-06-07 13:40:18 -0500 | [diff] [blame] | 201 | return total_length; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 202 | } |
| 203 | |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 204 | int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id, |
| 205 | u32 addr, u32 gsi_base) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 206 | { |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 207 | ioapic->type = ACPI_APIC_IOAPIC; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 208 | ioapic->length = sizeof(struct acpi_madt_ioapic); |
| 209 | ioapic->reserved = 0x00; |
| 210 | ioapic->gsi_base = gsi_base; |
| 211 | ioapic->ioapic_id = id; |
| 212 | ioapic->ioapic_addr = addr; |
| 213 | |
| 214 | return ioapic->length; |
| 215 | } |
| 216 | |
| 217 | int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride, |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 218 | u8 bus, u8 source, u32 gsirq, u16 flags) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 219 | { |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 220 | irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 221 | irqoverride->length = sizeof(struct acpi_madt_irqoverride); |
| 222 | irqoverride->bus = bus; |
| 223 | irqoverride->source = source; |
| 224 | irqoverride->gsirq = gsirq; |
| 225 | irqoverride->flags = flags; |
| 226 | |
| 227 | return irqoverride->length; |
| 228 | } |
| 229 | |
| 230 | int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi, |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 231 | u8 cpu, u16 flags, u8 lint) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 232 | { |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 233 | lapic_nmi->type = ACPI_APIC_LAPIC_NMI; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 234 | lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi); |
| 235 | lapic_nmi->flags = flags; |
| 236 | lapic_nmi->processor_id = cpu; |
| 237 | lapic_nmi->lint = lint; |
| 238 | |
| 239 | return lapic_nmi->length; |
| 240 | } |
| 241 | |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 242 | static void acpi_create_madt(struct acpi_madt *madt) |
| 243 | { |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 244 | struct acpi_table_header *header = &(madt->header); |
Bin Meng | a1ec7db | 2016-05-07 07:46:26 -0700 | [diff] [blame] | 245 | u32 current = (u32)madt + sizeof(struct acpi_madt); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 246 | |
| 247 | memset((void *)madt, 0, sizeof(struct acpi_madt)); |
| 248 | |
| 249 | /* Fill out header fields */ |
Bin Meng | b063d5f | 2016-05-07 07:46:24 -0700 | [diff] [blame] | 250 | acpi_fill_header(header, "APIC"); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 251 | header->length = sizeof(struct acpi_madt); |
Bin Meng | f662fe4 | 2016-05-07 07:46:28 -0700 | [diff] [blame] | 252 | header->revision = 4; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 253 | |
| 254 | madt->lapic_addr = LAPIC_DEFAULT_BASE; |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 255 | madt->flags = ACPI_MADT_PCAT_COMPAT; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 256 | |
| 257 | current = acpi_fill_madt(current); |
| 258 | |
| 259 | /* (Re)calculate length and checksum */ |
Bin Meng | a1ec7db | 2016-05-07 07:46:26 -0700 | [diff] [blame] | 260 | header->length = current - (u32)madt; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 261 | |
| 262 | header->checksum = table_compute_checksum((void *)madt, header->length); |
| 263 | } |
| 264 | |
| 265 | static int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig, |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 266 | u32 base, u16 seg_nr, u8 start, u8 end) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 267 | { |
| 268 | memset(mmconfig, 0, sizeof(*mmconfig)); |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 269 | mmconfig->base_address_l = base; |
| 270 | mmconfig->base_address_h = 0; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 271 | mmconfig->pci_segment_group_number = seg_nr; |
| 272 | mmconfig->start_bus_number = start; |
| 273 | mmconfig->end_bus_number = end; |
| 274 | |
| 275 | return sizeof(struct acpi_mcfg_mmconfig); |
| 276 | } |
| 277 | |
Bin Meng | a1ec7db | 2016-05-07 07:46:26 -0700 | [diff] [blame] | 278 | static u32 acpi_fill_mcfg(u32 current) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 279 | { |
| 280 | current += acpi_create_mcfg_mmconfig |
| 281 | ((struct acpi_mcfg_mmconfig *)current, |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 282 | CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 283 | |
| 284 | return current; |
| 285 | } |
| 286 | |
| 287 | /* MCFG is defined in the PCI Firmware Specification 3.0 */ |
| 288 | static void acpi_create_mcfg(struct acpi_mcfg *mcfg) |
| 289 | { |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 290 | struct acpi_table_header *header = &(mcfg->header); |
Bin Meng | a1ec7db | 2016-05-07 07:46:26 -0700 | [diff] [blame] | 291 | u32 current = (u32)mcfg + sizeof(struct acpi_mcfg); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 292 | |
| 293 | memset((void *)mcfg, 0, sizeof(struct acpi_mcfg)); |
| 294 | |
| 295 | /* Fill out header fields */ |
Bin Meng | b063d5f | 2016-05-07 07:46:24 -0700 | [diff] [blame] | 296 | acpi_fill_header(header, "MCFG"); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 297 | header->length = sizeof(struct acpi_mcfg); |
Bin Meng | f662fe4 | 2016-05-07 07:46:28 -0700 | [diff] [blame] | 298 | header->revision = 1; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 299 | |
| 300 | current = acpi_fill_mcfg(current); |
| 301 | |
| 302 | /* (Re)calculate length and checksum */ |
Bin Meng | a1ec7db | 2016-05-07 07:46:26 -0700 | [diff] [blame] | 303 | header->length = current - (u32)mcfg; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 304 | header->checksum = table_compute_checksum((void *)mcfg, header->length); |
| 305 | } |
| 306 | |
Bin Meng | daa5b83 | 2017-04-21 07:24:43 -0700 | [diff] [blame^] | 307 | void enter_acpi_mode(int pm1_cnt) |
Bin Meng | e259eb2 | 2016-05-11 07:45:03 -0700 | [diff] [blame] | 308 | { |
Bin Meng | 313ddd6 | 2017-04-21 07:24:42 -0700 | [diff] [blame] | 309 | u16 val = inw(pm1_cnt); |
| 310 | |
Bin Meng | e259eb2 | 2016-05-11 07:45:03 -0700 | [diff] [blame] | 311 | /* |
| 312 | * PM1_CNT register bit0 selects the power management event to be |
| 313 | * either an SCI or SMI interrupt. When this bit is set, then power |
| 314 | * management events will generate an SCI interrupt. When this bit |
| 315 | * is reset power management events will generate an SMI interrupt. |
| 316 | * |
| 317 | * Per ACPI spec, it is the responsibility of the hardware to set |
| 318 | * or reset this bit. OSPM always preserves this bit position. |
| 319 | * |
| 320 | * U-Boot does not support SMI. And we don't have plan to support |
| 321 | * anything running in SMM within U-Boot. To create a legacy-free |
| 322 | * system, and expose ourselves to OSPM as working under ACPI mode |
| 323 | * already, turn this bit on. |
| 324 | */ |
Bin Meng | 313ddd6 | 2017-04-21 07:24:42 -0700 | [diff] [blame] | 325 | outw(val | PM1_CNT_SCI_EN, pm1_cnt); |
Bin Meng | e259eb2 | 2016-05-11 07:45:03 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Miao Yan | 3b68c52 | 2016-01-20 01:57:06 -0800 | [diff] [blame] | 328 | /* |
| 329 | * QEMU's version of write_acpi_tables is defined in |
Tom Rini | bcb3c8d | 2016-05-06 10:40:22 -0400 | [diff] [blame] | 330 | * arch/x86/cpu/qemu/acpi_table.c |
Miao Yan | 3b68c52 | 2016-01-20 01:57:06 -0800 | [diff] [blame] | 331 | */ |
Simon Glass | ca37a39 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 332 | ulong write_acpi_tables(ulong start) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 333 | { |
Bin Meng | 5c54910 | 2016-02-27 22:58:00 -0800 | [diff] [blame] | 334 | u32 current; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 335 | struct acpi_rsdp *rsdp; |
| 336 | struct acpi_rsdt *rsdt; |
| 337 | struct acpi_xsdt *xsdt; |
| 338 | struct acpi_facs *facs; |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 339 | struct acpi_table_header *dsdt; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 340 | struct acpi_fadt *fadt; |
| 341 | struct acpi_mcfg *mcfg; |
| 342 | struct acpi_madt *madt; |
Bin Meng | d9050c6 | 2016-06-17 02:13:16 -0700 | [diff] [blame] | 343 | int i; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 344 | |
| 345 | current = start; |
| 346 | |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 347 | /* Align ACPI tables to 16 byte */ |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 348 | current = ALIGN(current, 16); |
| 349 | |
Simon Glass | ca37a39 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 350 | debug("ACPI: Writing ACPI tables at %lx\n", start); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 351 | |
| 352 | /* We need at least an RSDP and an RSDT Table */ |
| 353 | rsdp = (struct acpi_rsdp *)current; |
| 354 | current += sizeof(struct acpi_rsdp); |
| 355 | current = ALIGN(current, 16); |
| 356 | rsdt = (struct acpi_rsdt *)current; |
| 357 | current += sizeof(struct acpi_rsdt); |
| 358 | current = ALIGN(current, 16); |
| 359 | xsdt = (struct acpi_xsdt *)current; |
| 360 | current += sizeof(struct acpi_xsdt); |
Bin Meng | 3d938d1 | 2016-05-07 07:46:27 -0700 | [diff] [blame] | 361 | /* |
| 362 | * Per ACPI spec, the FACS table address must be aligned to a 64 byte |
| 363 | * boundary (Windows checks this, but Linux does not). |
| 364 | */ |
| 365 | current = ALIGN(current, 64); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 366 | |
| 367 | /* clear all table memory */ |
| 368 | memset((void *)start, 0, current - start); |
| 369 | |
| 370 | acpi_write_rsdp(rsdp, rsdt, xsdt); |
| 371 | acpi_write_rsdt(rsdt); |
| 372 | acpi_write_xsdt(xsdt); |
| 373 | |
| 374 | debug("ACPI: * FACS\n"); |
| 375 | facs = (struct acpi_facs *)current; |
| 376 | current += sizeof(struct acpi_facs); |
| 377 | current = ALIGN(current, 16); |
| 378 | |
| 379 | acpi_create_facs(facs); |
| 380 | |
| 381 | debug("ACPI: * DSDT\n"); |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 382 | dsdt = (struct acpi_table_header *)current; |
| 383 | memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header)); |
Bin Meng | d90434b | 2016-05-11 07:45:05 -0700 | [diff] [blame] | 384 | current += sizeof(struct acpi_table_header); |
| 385 | memcpy((char *)current, |
| 386 | (char *)&AmlCode + sizeof(struct acpi_table_header), |
| 387 | dsdt->length - sizeof(struct acpi_table_header)); |
| 388 | current += dsdt->length - sizeof(struct acpi_table_header); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 389 | current = ALIGN(current, 16); |
| 390 | |
Bin Meng | d9050c6 | 2016-06-17 02:13:16 -0700 | [diff] [blame] | 391 | /* Pack GNVS into the ACPI table area */ |
| 392 | for (i = 0; i < dsdt->length; i++) { |
| 393 | u32 *gnvs = (u32 *)((u32)dsdt + i); |
| 394 | if (*gnvs == ACPI_GNVS_ADDR) { |
| 395 | debug("Fix up global NVS in DSDT to 0x%08x\n", current); |
| 396 | *gnvs = current; |
| 397 | break; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /* Update DSDT checksum since we patched the GNVS address */ |
| 402 | dsdt->checksum = 0; |
| 403 | dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length); |
| 404 | |
| 405 | /* Fill in platform-specific global NVS variables */ |
| 406 | acpi_create_gnvs((struct acpi_global_nvs *)current); |
| 407 | current += sizeof(struct acpi_global_nvs); |
| 408 | current = ALIGN(current, 16); |
| 409 | |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 410 | debug("ACPI: * FADT\n"); |
| 411 | fadt = (struct acpi_fadt *)current; |
| 412 | current += sizeof(struct acpi_fadt); |
| 413 | current = ALIGN(current, 16); |
| 414 | acpi_create_fadt(fadt, facs, dsdt); |
| 415 | acpi_add_table(rsdp, fadt); |
| 416 | |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 417 | debug("ACPI: * MADT\n"); |
| 418 | madt = (struct acpi_madt *)current; |
| 419 | acpi_create_madt(madt); |
Bin Meng | d90434b | 2016-05-11 07:45:05 -0700 | [diff] [blame] | 420 | current += madt->header.length; |
| 421 | acpi_add_table(rsdp, madt); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 422 | current = ALIGN(current, 16); |
| 423 | |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 424 | debug("ACPI: * MCFG\n"); |
| 425 | mcfg = (struct acpi_mcfg *)current; |
| 426 | acpi_create_mcfg(mcfg); |
Bin Meng | d90434b | 2016-05-11 07:45:05 -0700 | [diff] [blame] | 427 | current += mcfg->header.length; |
| 428 | acpi_add_table(rsdp, mcfg); |
| 429 | current = ALIGN(current, 16); |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 430 | |
Bin Meng | d2d2218 | 2016-05-07 07:46:12 -0700 | [diff] [blame] | 431 | debug("current = %x\n", current); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 432 | |
Bin Meng | d2d2218 | 2016-05-07 07:46:12 -0700 | [diff] [blame] | 433 | debug("ACPI: done\n"); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 434 | |
Bin Meng | e259eb2 | 2016-05-11 07:45:03 -0700 | [diff] [blame] | 435 | /* |
| 436 | * Other than waiting for OSPM to request us to switch to ACPI mode, |
| 437 | * do it by ourselves, since SMI will not be triggered. |
| 438 | */ |
| 439 | enter_acpi_mode(fadt->pm1a_cnt_blk); |
| 440 | |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 441 | return current; |
| 442 | } |
Bin Meng | 34bc74a | 2017-04-21 07:24:36 -0700 | [diff] [blame] | 443 | |
| 444 | static struct acpi_rsdp *acpi_valid_rsdp(struct acpi_rsdp *rsdp) |
| 445 | { |
| 446 | if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0) |
| 447 | return NULL; |
| 448 | |
| 449 | debug("Looking on %p for valid checksum\n", rsdp); |
| 450 | |
| 451 | if (table_compute_checksum((void *)rsdp, 20) != 0) |
| 452 | return NULL; |
| 453 | debug("acpi rsdp checksum 1 passed\n"); |
| 454 | |
| 455 | if ((rsdp->revision > 1) && |
| 456 | (table_compute_checksum((void *)rsdp, rsdp->length) != 0)) |
| 457 | return NULL; |
| 458 | debug("acpi rsdp checksum 2 passed\n"); |
| 459 | |
| 460 | return rsdp; |
| 461 | } |
| 462 | |
| 463 | void *acpi_find_wakeup_vector(void) |
| 464 | { |
| 465 | char *p, *end; |
| 466 | struct acpi_rsdp *rsdp = NULL; |
| 467 | struct acpi_rsdt *rsdt; |
| 468 | struct acpi_fadt *fadt = NULL; |
| 469 | struct acpi_facs *facs; |
| 470 | void *wake_vec; |
| 471 | int i; |
| 472 | |
| 473 | debug("Trying to find the wakeup vector...\n"); |
| 474 | |
| 475 | /* Find RSDP */ |
| 476 | for (p = (char *)ROM_TABLE_ADDR; p < (char *)ROM_TABLE_END; p += 16) { |
| 477 | rsdp = acpi_valid_rsdp((struct acpi_rsdp *)p); |
| 478 | if (rsdp) |
| 479 | break; |
| 480 | } |
| 481 | |
| 482 | if (rsdp == NULL) |
| 483 | return NULL; |
| 484 | |
| 485 | debug("RSDP found at %p\n", rsdp); |
| 486 | rsdt = (struct acpi_rsdt *)rsdp->rsdt_address; |
| 487 | |
| 488 | end = (char *)rsdt + rsdt->header.length; |
| 489 | debug("RSDT found at %p ends at %p\n", rsdt, end); |
| 490 | |
| 491 | for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) { |
| 492 | fadt = (struct acpi_fadt *)rsdt->entry[i]; |
| 493 | if (strncmp((char *)fadt, "FACP", 4) == 0) |
| 494 | break; |
| 495 | fadt = NULL; |
| 496 | } |
| 497 | |
| 498 | if (fadt == NULL) |
| 499 | return NULL; |
| 500 | |
| 501 | debug("FADT found at %p\n", fadt); |
| 502 | facs = (struct acpi_facs *)fadt->firmware_ctrl; |
| 503 | |
| 504 | if (facs == NULL) { |
| 505 | debug("No FACS found, wake up from S3 not possible.\n"); |
| 506 | return NULL; |
| 507 | } |
| 508 | |
| 509 | debug("FACS found at %p\n", facs); |
| 510 | wake_vec = (void *)facs->firmware_waking_vector; |
| 511 | debug("OS waking vector is %p\n", wake_vec); |
| 512 | |
| 513 | return wake_vec; |
| 514 | } |