Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Bin Meng | 5e2400e | 2015-04-24 18:10:04 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com> |
Bin Meng | 5e2400e | 2015-04-24 18:10:04 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
Simon Glass | 8856d61 | 2023-07-15 21:39:09 -0600 | [diff] [blame] | 6 | #define LOG_CATEGORY LOGC_ACPI |
Heinrich Schuchardt | c193d9b | 2021-05-15 18:07:47 +0200 | [diff] [blame] | 7 | |
Simon Glass | d2cb7a2 | 2020-11-04 09:57:25 -0700 | [diff] [blame] | 8 | #include <bloblist.h> |
Simon Glass | b336a2b | 2020-07-16 21:22:31 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Alexander Graf | 4b6dddc | 2016-08-19 01:23:23 +0200 | [diff] [blame] | 11 | #include <smbios.h> |
Simon Glass | 776cc20 | 2020-04-08 16:57:36 -0600 | [diff] [blame] | 12 | #include <acpi/acpi_table.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
Simon Glass | 6388e35 | 2015-04-28 20:25:10 -0600 | [diff] [blame] | 14 | #include <asm/sfi.h> |
Bin Meng | 07545d8 | 2015-06-23 12:18:52 +0800 | [diff] [blame] | 15 | #include <asm/mpspec.h> |
Bin Meng | 5e2400e | 2015-04-24 18:10:04 +0800 | [diff] [blame] | 16 | #include <asm/tables.h> |
Bin Meng | 3cf2371 | 2016-02-28 23:54:50 -0800 | [diff] [blame] | 17 | #include <asm/coreboot_tables.h> |
Simon Glass | 1a2e02f | 2023-12-27 13:07:00 -0800 | [diff] [blame] | 18 | #include <linux/log2.h> |
Bin Meng | 5e2400e | 2015-04-24 18:10:04 +0800 | [diff] [blame] | 19 | |
Simon Glass | d2cb7a2 | 2020-11-04 09:57:25 -0700 | [diff] [blame] | 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Bin Meng | ef4d0a5 | 2016-02-27 22:58:01 -0800 | [diff] [blame] | 22 | /** |
| 23 | * Function prototype to write a specific configuration table |
| 24 | * |
| 25 | * @addr: start address to write the table |
| 26 | * @return: end address of the table |
| 27 | */ |
Simon Glass | 42fd8c1 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 28 | typedef ulong (*table_write)(ulong addr); |
Bin Meng | ef4d0a5 | 2016-02-27 22:58:01 -0800 | [diff] [blame] | 29 | |
Simon Glass | b336a2b | 2020-07-16 21:22:31 -0600 | [diff] [blame] | 30 | /** |
| 31 | * struct table_info - Information about each table to write |
| 32 | * |
| 33 | * @name: Name of table (for debugging) |
| 34 | * @write: Function to call to write this table |
Simon Glass | d2cb7a2 | 2020-11-04 09:57:25 -0700 | [diff] [blame] | 35 | * @tag: Bloblist tag if using CONFIG_BLOBLIST_TABLES |
| 36 | * @size: Maximum table size |
| 37 | * @align: Table alignment in bytes |
Simon Glass | b336a2b | 2020-07-16 21:22:31 -0600 | [diff] [blame] | 38 | */ |
| 39 | struct table_info { |
| 40 | const char *name; |
| 41 | table_write write; |
Simon Glass | d2cb7a2 | 2020-11-04 09:57:25 -0700 | [diff] [blame] | 42 | enum bloblist_tag_t tag; |
| 43 | int size; |
| 44 | int align; |
Simon Glass | b336a2b | 2020-07-16 21:22:31 -0600 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | static struct table_info table_list[] = { |
Bin Meng | ef4d0a5 | 2016-02-27 22:58:01 -0800 | [diff] [blame] | 48 | #ifdef CONFIG_GENERATE_PIRQ_TABLE |
Simon Glass | b336a2b | 2020-07-16 21:22:31 -0600 | [diff] [blame] | 49 | { "pirq", write_pirq_routing_table }, |
Bin Meng | ef4d0a5 | 2016-02-27 22:58:01 -0800 | [diff] [blame] | 50 | #endif |
| 51 | #ifdef CONFIG_GENERATE_SFI_TABLE |
Simon Glass | b336a2b | 2020-07-16 21:22:31 -0600 | [diff] [blame] | 52 | { "sfi", write_sfi_table, }, |
Bin Meng | ef4d0a5 | 2016-02-27 22:58:01 -0800 | [diff] [blame] | 53 | #endif |
| 54 | #ifdef CONFIG_GENERATE_MP_TABLE |
Simon Glass | b336a2b | 2020-07-16 21:22:31 -0600 | [diff] [blame] | 55 | { "mp", write_mp_table, }, |
Bin Meng | ef4d0a5 | 2016-02-27 22:58:01 -0800 | [diff] [blame] | 56 | #endif |
Simon Glass | 6a32489 | 2023-07-15 21:39:10 -0600 | [diff] [blame] | 57 | /* |
| 58 | * tables which can go in the bloblist must be last in this list, so |
| 59 | * that the calculation of gd->table_end works properly |
| 60 | */ |
Bin Meng | ef4d0a5 | 2016-02-27 22:58:01 -0800 | [diff] [blame] | 61 | #ifdef CONFIG_GENERATE_ACPI_TABLE |
Simon Glass | d2cb7a2 | 2020-11-04 09:57:25 -0700 | [diff] [blame] | 62 | { "acpi", write_acpi_tables, BLOBLISTT_ACPI_TABLES, 0x10000, 0x1000}, |
Bin Meng | ef4d0a5 | 2016-02-27 22:58:01 -0800 | [diff] [blame] | 63 | #endif |
Heinrich Schuchardt | 1c5aab8 | 2023-12-23 02:03:34 +0100 | [diff] [blame] | 64 | #if defined(CONFIG_GENERATE_SMBIOS_TABLE) && !defined(CONFIG_QFW_SMBIOS) |
Simon Glass | d2cb7a2 | 2020-11-04 09:57:25 -0700 | [diff] [blame] | 65 | { "smbios", write_smbios_table, BLOBLISTT_SMBIOS_TABLES, 0x1000, 0x100}, |
Bin Meng | ef4d0a5 | 2016-02-27 22:58:01 -0800 | [diff] [blame] | 66 | #endif |
| 67 | }; |
| 68 | |
Bin Meng | 7f5df8d | 2015-06-23 12:18:51 +0800 | [diff] [blame] | 69 | void table_fill_string(char *dest, const char *src, size_t n, char pad) |
| 70 | { |
| 71 | int start, len; |
| 72 | int i; |
| 73 | |
| 74 | strncpy(dest, src, n); |
| 75 | |
| 76 | /* Fill the remaining bytes with pad */ |
| 77 | len = strlen(src); |
| 78 | start = len < n ? len : n; |
| 79 | for (i = start; i < n; i++) |
| 80 | dest[i] = pad; |
| 81 | } |
| 82 | |
Simon Glass | 38e498c | 2020-11-04 09:57:18 -0700 | [diff] [blame] | 83 | int write_tables(void) |
Bin Meng | 5e2400e | 2015-04-24 18:10:04 +0800 | [diff] [blame] | 84 | { |
Bin Meng | ff94c21 | 2016-02-27 22:58:02 -0800 | [diff] [blame] | 85 | u32 high_table, table_size; |
Simon Glass | b336a2b | 2020-07-16 21:22:31 -0600 | [diff] [blame] | 86 | struct memory_area cfg_tables[ARRAY_SIZE(table_list) + 1]; |
Simon Glass | 6a32489 | 2023-07-15 21:39:10 -0600 | [diff] [blame] | 87 | bool use_high = false; |
Simon Glass | 8856d61 | 2023-07-15 21:39:09 -0600 | [diff] [blame] | 88 | u32 rom_addr; |
Bin Meng | ef4d0a5 | 2016-02-27 22:58:01 -0800 | [diff] [blame] | 89 | int i; |
Bin Meng | 5e2400e | 2015-04-24 18:10:04 +0800 | [diff] [blame] | 90 | |
Simon Glass | 6a32489 | 2023-07-15 21:39:10 -0600 | [diff] [blame] | 91 | gd->arch.table_start = ROM_TABLE_ADDR; |
| 92 | rom_addr = gd->arch.table_start; |
Simon Glass | d2cb7a2 | 2020-11-04 09:57:25 -0700 | [diff] [blame] | 93 | |
Simon Glass | 8856d61 | 2023-07-15 21:39:09 -0600 | [diff] [blame] | 94 | debug("Writing tables to %x:\n", rom_addr); |
Simon Glass | b336a2b | 2020-07-16 21:22:31 -0600 | [diff] [blame] | 95 | for (i = 0; i < ARRAY_SIZE(table_list); i++) { |
| 96 | const struct table_info *table = &table_list[i]; |
Simon Glass | d2cb7a2 | 2020-11-04 09:57:25 -0700 | [diff] [blame] | 97 | int size = table->size ? : CONFIG_ROM_TABLE_SIZE; |
Simon Glass | 8856d61 | 2023-07-15 21:39:09 -0600 | [diff] [blame] | 98 | u32 rom_table_end; |
Simon Glass | b336a2b | 2020-07-16 21:22:31 -0600 | [diff] [blame] | 99 | |
Heinrich Schuchardt | 52c62ac | 2024-01-02 00:11:44 +0100 | [diff] [blame] | 100 | rom_addr = ALIGN(rom_addr, 16); |
| 101 | |
Simon Glass | 5083488 | 2023-09-19 21:00:15 -0600 | [diff] [blame] | 102 | if (!strcmp("smbios", table->name)) |
| 103 | gd->arch.smbios_start = rom_addr; |
| 104 | |
Simon Glass | d2cb7a2 | 2020-11-04 09:57:25 -0700 | [diff] [blame] | 105 | if (IS_ENABLED(CONFIG_BLOBLIST_TABLES) && table->tag) { |
Simon Glass | 6a32489 | 2023-07-15 21:39:10 -0600 | [diff] [blame] | 106 | if (!gd->arch.table_end) |
| 107 | gd->arch.table_end = rom_addr; |
Simon Glass | 8856d61 | 2023-07-15 21:39:09 -0600 | [diff] [blame] | 108 | rom_addr = (ulong)bloblist_add(table->tag, size, |
Simon Glass | 1a2e02f | 2023-12-27 13:07:00 -0800 | [diff] [blame] | 109 | ilog2(table->align)); |
Simon Glass | 8856d61 | 2023-07-15 21:39:09 -0600 | [diff] [blame] | 110 | if (!rom_addr) |
Simon Glass | d2cb7a2 | 2020-11-04 09:57:25 -0700 | [diff] [blame] | 111 | return log_msg_ret("bloblist", -ENOBUFS); |
Simon Glass | 6a32489 | 2023-07-15 21:39:10 -0600 | [diff] [blame] | 112 | |
| 113 | /* the bloblist is always in high memory */ |
| 114 | use_high = true; |
| 115 | if (!gd->arch.table_start_high) |
| 116 | gd->arch.table_start_high = rom_addr; |
Simon Glass | d2cb7a2 | 2020-11-04 09:57:25 -0700 | [diff] [blame] | 117 | } |
Simon Glass | 8856d61 | 2023-07-15 21:39:09 -0600 | [diff] [blame] | 118 | rom_table_end = table->write(rom_addr); |
Heinrich Schuchardt | c193d9b | 2021-05-15 18:07:47 +0200 | [diff] [blame] | 119 | if (!rom_table_end) { |
| 120 | log_err("Can't create configuration table %d\n", i); |
| 121 | return -EINTR; |
| 122 | } |
Bin Meng | ff94c21 | 2016-02-27 22:58:02 -0800 | [diff] [blame] | 123 | |
Simon Glass | f36e4c7 | 2020-11-04 09:57:24 -0700 | [diff] [blame] | 124 | if (IS_ENABLED(CONFIG_SEABIOS)) { |
Simon Glass | 8856d61 | 2023-07-15 21:39:09 -0600 | [diff] [blame] | 125 | table_size = rom_table_end - rom_addr; |
Simon Glass | f36e4c7 | 2020-11-04 09:57:24 -0700 | [diff] [blame] | 126 | high_table = (u32)(ulong)high_table_malloc(table_size); |
| 127 | if (high_table) { |
Heinrich Schuchardt | c193d9b | 2021-05-15 18:07:47 +0200 | [diff] [blame] | 128 | if (!table->write(high_table)) { |
| 129 | log_err("Can't create configuration table %d\n", |
| 130 | i); |
| 131 | return -EINTR; |
| 132 | } |
Bin Meng | 3cf2371 | 2016-02-28 23:54:50 -0800 | [diff] [blame] | 133 | |
Simon Glass | f36e4c7 | 2020-11-04 09:57:24 -0700 | [diff] [blame] | 134 | cfg_tables[i].start = high_table; |
| 135 | cfg_tables[i].size = table_size; |
| 136 | } else { |
| 137 | printf("%d: no memory for configuration tables\n", |
| 138 | i); |
| 139 | return -ENOSPC; |
| 140 | } |
Bin Meng | ff94c21 | 2016-02-27 22:58:02 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Simon Glass | b336a2b | 2020-07-16 21:22:31 -0600 | [diff] [blame] | 143 | debug("- wrote '%s' to %x, end %x\n", table->name, |
Simon Glass | 8856d61 | 2023-07-15 21:39:09 -0600 | [diff] [blame] | 144 | rom_addr, rom_table_end); |
| 145 | if (rom_table_end - rom_addr > size) { |
Simon Glass | d2cb7a2 | 2020-11-04 09:57:25 -0700 | [diff] [blame] | 146 | log_err("Out of space for configuration tables: need %x, have %x\n", |
Simon Glass | 8856d61 | 2023-07-15 21:39:09 -0600 | [diff] [blame] | 147 | rom_table_end - rom_addr, size); |
Simon Glass | d2cb7a2 | 2020-11-04 09:57:25 -0700 | [diff] [blame] | 148 | return log_msg_ret("bloblist", -ENOSPC); |
| 149 | } |
Simon Glass | 8856d61 | 2023-07-15 21:39:09 -0600 | [diff] [blame] | 150 | rom_addr = rom_table_end; |
Bin Meng | ef4d0a5 | 2016-02-27 22:58:01 -0800 | [diff] [blame] | 151 | } |
Bin Meng | 3cf2371 | 2016-02-28 23:54:50 -0800 | [diff] [blame] | 152 | |
Simon Glass | 6a32489 | 2023-07-15 21:39:10 -0600 | [diff] [blame] | 153 | if (use_high) |
| 154 | gd->arch.table_end_high = rom_addr; |
| 155 | else |
| 156 | gd->arch.table_end = rom_addr; |
| 157 | |
Simon Glass | f36e4c7 | 2020-11-04 09:57:24 -0700 | [diff] [blame] | 158 | if (IS_ENABLED(CONFIG_SEABIOS)) { |
| 159 | /* make sure the last item is zero */ |
| 160 | cfg_tables[i].size = 0; |
| 161 | write_coreboot_table(CB_TABLE_ADDR, cfg_tables); |
| 162 | } |
| 163 | |
Simon Glass | d2cb7a2 | 2020-11-04 09:57:25 -0700 | [diff] [blame] | 164 | if (IS_ENABLED(CONFIG_BLOBLIST_TABLES)) { |
| 165 | void *ptr = (void *)CONFIG_ROM_TABLE_ADDR; |
| 166 | |
| 167 | /* Write an RSDP pointing to the tables */ |
| 168 | if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) { |
| 169 | struct acpi_ctx *ctx = gd_acpi_ctx(); |
| 170 | |
| 171 | acpi_write_rsdp(ptr, ctx->rsdt, ctx->xsdt); |
| 172 | ptr += ALIGN(sizeof(struct acpi_rsdp), 16); |
| 173 | } |
| 174 | if (IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE)) { |
| 175 | void *smbios; |
| 176 | |
| 177 | smbios = bloblist_find(BLOBLISTT_SMBIOS_TABLES, 0); |
| 178 | if (!smbios) |
| 179 | return log_msg_ret("smbios", -ENOENT); |
| 180 | memcpy(ptr, smbios, sizeof(struct smbios_entry)); |
| 181 | } |
| 182 | } |
| 183 | |
Simon Glass | b336a2b | 2020-07-16 21:22:31 -0600 | [diff] [blame] | 184 | debug("- done writing tables\n"); |
Simon Glass | 38e498c | 2020-11-04 09:57:18 -0700 | [diff] [blame] | 185 | |
| 186 | return 0; |
Bin Meng | 5e2400e | 2015-04-24 18:10:04 +0800 | [diff] [blame] | 187 | } |