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