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