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