Simon Glass | 94ba15a | 2021-12-01 09:02:50 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Write base ACPI tables |
| 4 | * |
| 5 | * Copyright 2021 Google LLC |
| 6 | */ |
| 7 | |
Simon Glass | eacb6d0 | 2021-12-01 09:02:52 -0700 | [diff] [blame] | 8 | #define LOG_CATEGORY LOGC_ACPI |
| 9 | |
Simon Glass | 94ba15a | 2021-12-01 09:02:50 -0700 | [diff] [blame] | 10 | #include <acpi/acpi_table.h> |
| 11 | #include <dm/acpi.h> |
| 12 | #include <mapmem.h> |
| 13 | #include <tables_csum.h> |
Heinrich Schuchardt | c7b31a9 | 2023-11-13 00:53:56 +0100 | [diff] [blame] | 14 | #include <linux/sizes.h> |
Tom Rini | 467382c | 2023-12-14 13:16:58 -0500 | [diff] [blame] | 15 | #include <linux/errno.h> |
| 16 | #include <linux/string.h> |
Simon Glass | 94ba15a | 2021-12-01 09:02:50 -0700 | [diff] [blame] | 17 | |
| 18 | void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt, |
| 19 | struct acpi_xsdt *xsdt) |
| 20 | { |
| 21 | memset(rsdp, 0, sizeof(struct acpi_rsdp)); |
| 22 | |
| 23 | memcpy(rsdp->signature, RSDP_SIG, 8); |
| 24 | memcpy(rsdp->oem_id, OEM_ID, 6); |
| 25 | |
Heinrich Schuchardt | c7b31a9 | 2023-11-13 00:53:56 +0100 | [diff] [blame] | 26 | if (rsdt) |
Simon Glass | a8efebe | 2023-12-31 08:25:54 -0700 | [diff] [blame] | 27 | rsdp->rsdt_address = nomap_to_sysmem(rsdt); |
Simon Glass | 94ba15a | 2021-12-01 09:02:50 -0700 | [diff] [blame] | 28 | |
Heinrich Schuchardt | c7b31a9 | 2023-11-13 00:53:56 +0100 | [diff] [blame] | 29 | if (xsdt) |
Simon Glass | a8efebe | 2023-12-31 08:25:54 -0700 | [diff] [blame] | 30 | rsdp->xsdt_address = nomap_to_sysmem(xsdt); |
Heinrich Schuchardt | c7b31a9 | 2023-11-13 00:53:56 +0100 | [diff] [blame] | 31 | |
| 32 | rsdp->length = sizeof(struct acpi_rsdp); |
Simon Glass | 94ba15a | 2021-12-01 09:02:50 -0700 | [diff] [blame] | 33 | rsdp->revision = ACPI_RSDP_REV_ACPI_2_0; |
| 34 | |
| 35 | /* Calculate checksums */ |
| 36 | rsdp->checksum = table_compute_checksum(rsdp, 20); |
| 37 | rsdp->ext_checksum = table_compute_checksum(rsdp, |
| 38 | sizeof(struct acpi_rsdp)); |
| 39 | } |
| 40 | |
| 41 | static void acpi_write_rsdt(struct acpi_rsdt *rsdt) |
| 42 | { |
| 43 | struct acpi_table_header *header = &rsdt->header; |
| 44 | |
| 45 | /* Fill out header fields */ |
| 46 | acpi_fill_header(header, "RSDT"); |
| 47 | header->length = sizeof(struct acpi_rsdt); |
| 48 | header->revision = 1; |
| 49 | |
| 50 | /* Entries are filled in later, we come with an empty set */ |
| 51 | |
| 52 | /* Fix checksum */ |
| 53 | header->checksum = table_compute_checksum(rsdt, |
| 54 | sizeof(struct acpi_rsdt)); |
| 55 | } |
| 56 | |
| 57 | static void acpi_write_xsdt(struct acpi_xsdt *xsdt) |
| 58 | { |
| 59 | struct acpi_table_header *header = &xsdt->header; |
| 60 | |
| 61 | /* Fill out header fields */ |
| 62 | acpi_fill_header(header, "XSDT"); |
| 63 | header->length = sizeof(struct acpi_xsdt); |
| 64 | header->revision = 1; |
| 65 | |
| 66 | /* Entries are filled in later, we come with an empty set */ |
| 67 | |
| 68 | /* Fix checksum */ |
| 69 | header->checksum = table_compute_checksum(xsdt, |
| 70 | sizeof(struct acpi_xsdt)); |
| 71 | } |
| 72 | |
| 73 | static int acpi_write_base(struct acpi_ctx *ctx, |
| 74 | const struct acpi_writer *entry) |
| 75 | { |
Heinrich Schuchardt | c7b31a9 | 2023-11-13 00:53:56 +0100 | [diff] [blame] | 76 | /* We need at least an RSDP and an XSDT Table */ |
Simon Glass | 94ba15a | 2021-12-01 09:02:50 -0700 | [diff] [blame] | 77 | ctx->rsdp = ctx->current; |
| 78 | acpi_inc_align(ctx, sizeof(struct acpi_rsdp)); |
Heinrich Schuchardt | c7b31a9 | 2023-11-13 00:53:56 +0100 | [diff] [blame] | 79 | if (map_to_sysmem(ctx->current) < SZ_4G - SZ_64K) { |
| 80 | ctx->rsdt = ctx->current; |
| 81 | acpi_inc_align(ctx, sizeof(struct acpi_rsdt)); |
| 82 | } else { |
| 83 | ctx->rsdt = 0; |
| 84 | } |
Simon Glass | 94ba15a | 2021-12-01 09:02:50 -0700 | [diff] [blame] | 85 | ctx->xsdt = ctx->current; |
| 86 | acpi_inc_align(ctx, sizeof(struct acpi_xsdt)); |
| 87 | |
| 88 | /* clear all table memory */ |
| 89 | memset(ctx->base, '\0', ctx->current - ctx->base); |
| 90 | |
| 91 | acpi_write_rsdp(ctx->rsdp, ctx->rsdt, ctx->xsdt); |
Heinrich Schuchardt | c7b31a9 | 2023-11-13 00:53:56 +0100 | [diff] [blame] | 92 | if (ctx->rsdt) |
| 93 | acpi_write_rsdt(ctx->rsdt); |
Simon Glass | 94ba15a | 2021-12-01 09:02:50 -0700 | [diff] [blame] | 94 | acpi_write_xsdt(ctx->xsdt); |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | /* |
| 99 | * Per ACPI spec, the FACS table address must be aligned to a 64-byte boundary |
| 100 | * (Windows checks this, but Linux does not). |
| 101 | * |
| 102 | * Use the '0' prefix to put this one first |
| 103 | */ |
| 104 | ACPI_WRITER(0base, NULL, acpi_write_base, ACPIWF_ALIGN64); |