Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com> |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 4 | * (C) Copyright 2021 Asherah Connor <ashe@kivikakk.ee> |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_QFW |
| 8 | |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <command.h> |
| 11 | #include <errno.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Miao Yan | 1868659 | 2016-05-22 19:37:17 -0700 | [diff] [blame] | 14 | #include <qfw.h> |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 15 | #include <dm.h> |
| 16 | #include <misc.h> |
Simon Glass | 0679cca | 2021-12-01 09:02:40 -0700 | [diff] [blame] | 17 | #include <tables_csum.h> |
Miao Yan | eece493 | 2016-05-22 19:37:20 -0700 | [diff] [blame] | 18 | #ifdef CONFIG_GENERATE_ACPI_TABLE |
| 19 | #include <asm/tables.h> |
| 20 | #endif |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 21 | |
Simon Glass | 31c27eb | 2021-12-01 09:02:49 -0700 | [diff] [blame] | 22 | #if defined(CONFIG_GENERATE_ACPI_TABLE) && !defined(CONFIG_SANDBOX) |
Miao Yan | eece493 | 2016-05-22 19:37:20 -0700 | [diff] [blame] | 23 | /* |
| 24 | * This function allocates memory for ACPI tables |
| 25 | * |
| 26 | * @entry : BIOS linker command entry which tells where to allocate memory |
| 27 | * (either high memory or low memory) |
| 28 | * @addr : The address that should be used for low memory allcation. If the |
| 29 | * memory allocation request is 'ZONE_HIGH' then this parameter will |
| 30 | * be ignored. |
| 31 | * @return: 0 on success, or negative value on failure |
| 32 | */ |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 33 | static int bios_linker_allocate(struct udevice *dev, |
| 34 | struct bios_linker_entry *entry, ulong *addr) |
Miao Yan | eece493 | 2016-05-22 19:37:20 -0700 | [diff] [blame] | 35 | { |
| 36 | uint32_t size, align; |
| 37 | struct fw_file *file; |
| 38 | unsigned long aligned_addr; |
| 39 | |
| 40 | align = le32_to_cpu(entry->alloc.align); |
| 41 | /* align must be power of 2 */ |
| 42 | if (align & (align - 1)) { |
| 43 | printf("error: wrong alignment %u\n", align); |
| 44 | return -EINVAL; |
| 45 | } |
| 46 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 47 | file = qfw_find_file(dev, entry->alloc.file); |
Miao Yan | eece493 | 2016-05-22 19:37:20 -0700 | [diff] [blame] | 48 | if (!file) { |
| 49 | printf("error: can't find file %s\n", entry->alloc.file); |
| 50 | return -ENOENT; |
| 51 | } |
| 52 | |
| 53 | size = be32_to_cpu(file->cfg.size); |
| 54 | |
| 55 | /* |
| 56 | * ZONE_HIGH means we need to allocate from high memory, since |
| 57 | * malloc space is already at the end of RAM, so we directly use it. |
| 58 | * If allocation zone is ZONE_FSEG, then we use the 'addr' passed |
| 59 | * in which is low memory |
| 60 | */ |
| 61 | if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH) { |
| 62 | aligned_addr = (unsigned long)memalign(align, size); |
| 63 | if (!aligned_addr) { |
| 64 | printf("error: allocating resource\n"); |
| 65 | return -ENOMEM; |
| 66 | } |
| 67 | } else if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG) { |
| 68 | aligned_addr = ALIGN(*addr, align); |
| 69 | } else { |
| 70 | printf("error: invalid allocation zone\n"); |
| 71 | return -EINVAL; |
| 72 | } |
| 73 | |
| 74 | debug("bios_linker_allocate: allocate file %s, size %u, zone %d, align %u, addr 0x%lx\n", |
| 75 | file->cfg.name, size, entry->alloc.zone, align, aligned_addr); |
| 76 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 77 | qfw_read_entry(dev, be16_to_cpu(file->cfg.select), size, |
| 78 | (void *)aligned_addr); |
Miao Yan | eece493 | 2016-05-22 19:37:20 -0700 | [diff] [blame] | 79 | file->addr = aligned_addr; |
| 80 | |
| 81 | /* adjust address for low memory allocation */ |
| 82 | if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG) |
| 83 | *addr = (aligned_addr + size); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * This function patches ACPI tables previously loaded |
| 90 | * by bios_linker_allocate() |
| 91 | * |
| 92 | * @entry : BIOS linker command entry which tells how to patch |
| 93 | * ACPI tables |
| 94 | * @return: 0 on success, or negative value on failure |
| 95 | */ |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 96 | static int bios_linker_add_pointer(struct udevice *dev, |
| 97 | struct bios_linker_entry *entry) |
Miao Yan | eece493 | 2016-05-22 19:37:20 -0700 | [diff] [blame] | 98 | { |
| 99 | struct fw_file *dest, *src; |
| 100 | uint32_t offset = le32_to_cpu(entry->pointer.offset); |
| 101 | uint64_t pointer = 0; |
| 102 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 103 | dest = qfw_find_file(dev, entry->pointer.dest_file); |
Miao Yan | eece493 | 2016-05-22 19:37:20 -0700 | [diff] [blame] | 104 | if (!dest || !dest->addr) |
| 105 | return -ENOENT; |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 106 | src = qfw_find_file(dev, entry->pointer.src_file); |
Miao Yan | eece493 | 2016-05-22 19:37:20 -0700 | [diff] [blame] | 107 | if (!src || !src->addr) |
| 108 | return -ENOENT; |
| 109 | |
| 110 | debug("bios_linker_add_pointer: dest->addr 0x%lx, src->addr 0x%lx, offset 0x%x size %u, 0x%llx\n", |
| 111 | dest->addr, src->addr, offset, entry->pointer.size, pointer); |
| 112 | |
| 113 | memcpy(&pointer, (char *)dest->addr + offset, entry->pointer.size); |
| 114 | pointer = le64_to_cpu(pointer); |
| 115 | pointer += (unsigned long)src->addr; |
| 116 | pointer = cpu_to_le64(pointer); |
| 117 | memcpy((char *)dest->addr + offset, &pointer, entry->pointer.size); |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * This function updates checksum fields of ACPI tables previously loaded |
| 124 | * by bios_linker_allocate() |
| 125 | * |
| 126 | * @entry : BIOS linker command entry which tells where to update ACPI table |
| 127 | * checksums |
| 128 | * @return: 0 on success, or negative value on failure |
| 129 | */ |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 130 | static int bios_linker_add_checksum(struct udevice *dev, |
| 131 | struct bios_linker_entry *entry) |
Miao Yan | eece493 | 2016-05-22 19:37:20 -0700 | [diff] [blame] | 132 | { |
| 133 | struct fw_file *file; |
| 134 | uint8_t *data, cksum = 0; |
| 135 | uint8_t *cksum_start; |
| 136 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 137 | file = qfw_find_file(dev, entry->cksum.file); |
Miao Yan | eece493 | 2016-05-22 19:37:20 -0700 | [diff] [blame] | 138 | if (!file || !file->addr) |
| 139 | return -ENOENT; |
| 140 | |
| 141 | data = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.offset)); |
| 142 | cksum_start = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.start)); |
| 143 | cksum = table_compute_checksum(cksum_start, |
| 144 | le32_to_cpu(entry->cksum.length)); |
| 145 | *data = cksum; |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | /* This function loads and patches ACPI tables provided by QEMU */ |
Simon Glass | 42fd8c1 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 151 | ulong write_acpi_tables(ulong addr) |
Miao Yan | eece493 | 2016-05-22 19:37:20 -0700 | [diff] [blame] | 152 | { |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 153 | int i, ret; |
Miao Yan | eece493 | 2016-05-22 19:37:20 -0700 | [diff] [blame] | 154 | struct fw_file *file; |
| 155 | struct bios_linker_entry *table_loader; |
| 156 | struct bios_linker_entry *entry; |
| 157 | uint32_t size; |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 158 | struct udevice *dev; |
| 159 | |
| 160 | ret = qfw_get_dev(&dev); |
| 161 | if (ret) { |
| 162 | printf("error: no qfw\n"); |
| 163 | return addr; |
| 164 | } |
Miao Yan | eece493 | 2016-05-22 19:37:20 -0700 | [diff] [blame] | 165 | |
| 166 | /* make sure fw_list is loaded */ |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 167 | ret = qfw_read_firmware_list(dev); |
Miao Yan | eece493 | 2016-05-22 19:37:20 -0700 | [diff] [blame] | 168 | if (ret) { |
| 169 | printf("error: can't read firmware file list\n"); |
| 170 | return addr; |
| 171 | } |
| 172 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 173 | file = qfw_find_file(dev, "etc/table-loader"); |
Miao Yan | eece493 | 2016-05-22 19:37:20 -0700 | [diff] [blame] | 174 | if (!file) { |
| 175 | printf("error: can't find etc/table-loader\n"); |
| 176 | return addr; |
| 177 | } |
| 178 | |
| 179 | size = be32_to_cpu(file->cfg.size); |
| 180 | if ((size % sizeof(*entry)) != 0) { |
| 181 | printf("error: table-loader maybe corrupted\n"); |
| 182 | return addr; |
| 183 | } |
| 184 | |
| 185 | table_loader = malloc(size); |
| 186 | if (!table_loader) { |
| 187 | printf("error: no memory for table-loader\n"); |
| 188 | return addr; |
| 189 | } |
| 190 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 191 | qfw_read_entry(dev, be16_to_cpu(file->cfg.select), size, table_loader); |
Miao Yan | eece493 | 2016-05-22 19:37:20 -0700 | [diff] [blame] | 192 | |
| 193 | for (i = 0; i < (size / sizeof(*entry)); i++) { |
| 194 | entry = table_loader + i; |
| 195 | switch (le32_to_cpu(entry->command)) { |
| 196 | case BIOS_LINKER_LOADER_COMMAND_ALLOCATE: |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 197 | ret = bios_linker_allocate(dev, entry, &addr); |
Miao Yan | eece493 | 2016-05-22 19:37:20 -0700 | [diff] [blame] | 198 | if (ret) |
| 199 | goto out; |
| 200 | break; |
| 201 | case BIOS_LINKER_LOADER_COMMAND_ADD_POINTER: |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 202 | ret = bios_linker_add_pointer(dev, entry); |
Miao Yan | eece493 | 2016-05-22 19:37:20 -0700 | [diff] [blame] | 203 | if (ret) |
| 204 | goto out; |
| 205 | break; |
| 206 | case BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM: |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 207 | ret = bios_linker_add_checksum(dev, entry); |
Miao Yan | eece493 | 2016-05-22 19:37:20 -0700 | [diff] [blame] | 208 | if (ret) |
| 209 | goto out; |
| 210 | break; |
| 211 | default: |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | out: |
| 217 | if (ret) { |
| 218 | struct fw_cfg_file_iter iter; |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 219 | for (file = qfw_file_iter_init(dev, &iter); |
| 220 | !qfw_file_iter_end(&iter); |
| 221 | file = qfw_file_iter_next(&iter)) { |
Miao Yan | eece493 | 2016-05-22 19:37:20 -0700 | [diff] [blame] | 222 | if (file->addr) { |
| 223 | free((void *)file->addr); |
| 224 | file->addr = 0; |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | free(table_loader); |
| 230 | return addr; |
| 231 | } |
Bin Meng | 2d1c661 | 2018-01-30 05:01:17 -0800 | [diff] [blame] | 232 | |
| 233 | ulong acpi_get_rsdp_addr(void) |
| 234 | { |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 235 | int ret; |
Bin Meng | 2d1c661 | 2018-01-30 05:01:17 -0800 | [diff] [blame] | 236 | struct fw_file *file; |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 237 | struct udevice *dev; |
Bin Meng | 2d1c661 | 2018-01-30 05:01:17 -0800 | [diff] [blame] | 238 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 239 | ret = qfw_get_dev(&dev); |
| 240 | if (ret) { |
| 241 | printf("error: no qfw\n"); |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | file = qfw_find_file(dev, "etc/acpi/rsdp"); |
Bin Meng | 2d1c661 | 2018-01-30 05:01:17 -0800 | [diff] [blame] | 246 | return file->addr; |
| 247 | } |
Miao Yan | eece493 | 2016-05-22 19:37:20 -0700 | [diff] [blame] | 248 | #endif |
| 249 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 250 | static void qfw_read_entry_io(struct qfw_dev *qdev, u16 entry, u32 size, |
| 251 | void *address) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 252 | { |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 253 | struct dm_qfw_ops *ops = dm_qfw_get_ops(qdev->dev); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 254 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 255 | debug("%s: entry 0x%x, size %u address %p\n", __func__, entry, size, |
| 256 | address); |
| 257 | |
| 258 | ops->read_entry_io(qdev->dev, entry, size, address); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 261 | static void qfw_read_entry_dma(struct qfw_dev *qdev, u16 entry, u32 size, |
| 262 | void *address) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 263 | { |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 264 | struct dm_qfw_ops *ops = dm_qfw_get_ops(qdev->dev); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 265 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 266 | struct qfw_dma dma = { |
| 267 | .length = cpu_to_be32(size), |
| 268 | .address = cpu_to_be64((uintptr_t)address), |
| 269 | .control = cpu_to_be32(FW_CFG_DMA_READ), |
| 270 | }; |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 271 | |
| 272 | /* |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 273 | * writing FW_CFG_INVALID will cause read operation to resume at last |
| 274 | * offset, otherwise read will start at offset 0 |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 275 | */ |
| 276 | if (entry != FW_CFG_INVALID) |
| 277 | dma.control |= cpu_to_be32(FW_CFG_DMA_SELECT | (entry << 16)); |
| 278 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 279 | debug("%s: entry 0x%x, size %u address %p, control 0x%x\n", __func__, |
Miao Yan | 2e82e74 | 2016-05-22 19:37:15 -0700 | [diff] [blame] | 280 | entry, size, address, be32_to_cpu(dma.control)); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 281 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 282 | barrier(); |
| 283 | |
| 284 | ops->read_entry_dma(qdev->dev, &dma); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 287 | void qfw_read_entry(struct udevice *dev, u16 entry, u32 size, void *address) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 288 | { |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 289 | struct qfw_dev *qdev = dev_get_uclass_priv(dev); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 290 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 291 | if (qdev->dma_present) |
| 292 | qfw_read_entry_dma(qdev, entry, size, address); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 293 | else |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 294 | qfw_read_entry_io(qdev, entry, size, address); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 297 | int qfw_register(struct udevice *dev) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 298 | { |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 299 | struct qfw_dev *qdev = dev_get_uclass_priv(dev); |
| 300 | u32 qemu, dma_enabled; |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 301 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 302 | qdev->dev = dev; |
| 303 | INIT_LIST_HEAD(&qdev->fw_list); |
| 304 | |
| 305 | qfw_read_entry_io(qdev, FW_CFG_SIGNATURE, 4, &qemu); |
| 306 | if (be32_to_cpu(qemu) != QEMU_FW_CFG_SIGNATURE) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 307 | return -ENODEV; |
| 308 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 309 | qfw_read_entry_io(qdev, FW_CFG_ID, 1, &dma_enabled); |
| 310 | if (dma_enabled & FW_CFG_DMA_ENABLED) |
| 311 | qdev->dma_present = true; |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 312 | |
| 313 | return 0; |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 316 | UCLASS_DRIVER(qfw) = { |
| 317 | .id = UCLASS_QFW, |
| 318 | .name = "qfw", |
| 319 | .per_device_auto = sizeof(struct qfw_dev), |
| 320 | }; |