2 * (C) Copyright 2015 Miao Yan <yanmiaoebst@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/fw_cfg.h>
13 #include <asm/tables.h>
15 #include <linux/list.h>
18 static bool fwcfg_present;
19 static bool fwcfg_dma_present;
21 static LIST_HEAD(fw_list);
23 /* Read configuration item using fw_cfg PIO interface */
24 static void qemu_fwcfg_read_entry_pio(uint16_t entry,
25 uint32_t size, void *address)
28 uint8_t *data = address;
31 * writting FW_CFG_INVALID will cause read operation to resume at
32 * last offset, otherwise read will start at offset 0
34 if (entry != FW_CFG_INVALID)
35 outw(entry, FW_CONTROL_PORT);
37 data[i++] = inb(FW_DATA_PORT);
40 /* Read configuration item using fw_cfg DMA interface */
41 static void qemu_fwcfg_read_entry_dma(uint16_t entry,
42 uint32_t size, void *address)
44 struct fw_cfg_dma_access dma;
46 dma.length = cpu_to_be32(size);
47 dma.address = cpu_to_be64((uintptr_t)address);
48 dma.control = cpu_to_be32(FW_CFG_DMA_READ);
51 * writting FW_CFG_INVALID will cause read operation to resume at
52 * last offset, otherwise read will start at offset 0
54 if (entry != FW_CFG_INVALID)
55 dma.control |= cpu_to_be32(FW_CFG_DMA_SELECT | (entry << 16));
59 debug("qemu_fwcfg_dma_read_entry: addr %p, length %u control 0x%x\n",
60 address, size, be32_to_cpu(dma.control));
62 outl(cpu_to_be32((uint32_t)&dma), FW_DMA_PORT_HIGH);
64 while (be32_to_cpu(dma.control) & ~FW_CFG_DMA_ERROR)
65 __asm__ __volatile__ ("pause");
68 static bool qemu_fwcfg_present(void)
72 qemu_fwcfg_read_entry_pio(FW_CFG_SIGNATURE, 4, &qemu);
73 return be32_to_cpu(qemu) == QEMU_FW_CFG_SIGNATURE;
76 static bool qemu_fwcfg_dma_present(void)
80 qemu_fwcfg_read_entry_pio(FW_CFG_ID, 1, &dma_enabled);
81 if (dma_enabled & FW_CFG_DMA_ENABLED)
87 static void qemu_fwcfg_read_entry(uint16_t entry,
88 uint32_t length, void *address)
90 if (fwcfg_dma_present)
91 qemu_fwcfg_read_entry_dma(entry, length, address);
93 qemu_fwcfg_read_entry_pio(entry, length, address);
96 int qemu_fwcfg_online_cpus(void)
103 qemu_fwcfg_read_entry(FW_CFG_NB_CPUS, 2, &nb_cpus);
105 return le16_to_cpu(nb_cpus);
109 * This function prepares kernel for zboot. It loads kernel data
110 * to 'load_addr', initrd to 'initrd_addr' and kernel command
111 * line using qemu fw_cfg interface.
113 static int qemu_fwcfg_setup_kernel(void *load_addr, void *initrd_addr)
116 uint32_t setup_size, kernel_size, cmdline_size, initrd_size;
118 qemu_fwcfg_read_entry(FW_CFG_SETUP_SIZE, 4, &setup_size);
119 qemu_fwcfg_read_entry(FW_CFG_KERNEL_SIZE, 4, &kernel_size);
121 if (setup_size == 0 || kernel_size == 0) {
122 printf("warning: no kernel available\n");
126 data_addr = load_addr;
127 qemu_fwcfg_read_entry(FW_CFG_SETUP_DATA,
128 le32_to_cpu(setup_size), data_addr);
129 data_addr += le32_to_cpu(setup_size);
131 qemu_fwcfg_read_entry(FW_CFG_KERNEL_DATA,
132 le32_to_cpu(kernel_size), data_addr);
133 data_addr += le32_to_cpu(kernel_size);
135 data_addr = initrd_addr;
136 qemu_fwcfg_read_entry(FW_CFG_INITRD_SIZE, 4, &initrd_size);
137 if (initrd_size == 0) {
138 printf("warning: no initrd available\n");
140 qemu_fwcfg_read_entry(FW_CFG_INITRD_DATA,
141 le32_to_cpu(initrd_size), data_addr);
142 data_addr += le32_to_cpu(initrd_size);
145 qemu_fwcfg_read_entry(FW_CFG_CMDLINE_SIZE, 4, &cmdline_size);
147 qemu_fwcfg_read_entry(FW_CFG_CMDLINE_DATA,
148 le32_to_cpu(cmdline_size), data_addr);
150 * if kernel cmdline only contains '\0', (e.g. no -append
151 * when invoking qemu), do not update bootargs
153 if (*data_addr != '\0') {
154 if (setenv("bootargs", data_addr) < 0)
155 printf("warning: unable to change bootargs\n");
159 printf("loading kernel to address %p size %x", load_addr,
160 le32_to_cpu(kernel_size));
162 printf(" initrd %p size %x\n",
164 le32_to_cpu(initrd_size));
171 static int qemu_fwcfg_read_firmware_list(void)
175 struct fw_file *file;
176 struct list_head *entry;
178 /* don't read it twice */
179 if (!list_empty(&fw_list))
182 qemu_fwcfg_read_entry(FW_CFG_FILE_DIR, 4, &count);
186 count = be32_to_cpu(count);
187 for (i = 0; i < count; i++) {
188 file = malloc(sizeof(*file));
190 printf("error: allocating resource\n");
193 qemu_fwcfg_read_entry(FW_CFG_INVALID,
194 sizeof(struct fw_cfg_file), &file->cfg);
196 list_add_tail(&file->list, &fw_list);
202 list_for_each(entry, &fw_list) {
203 file = list_entry(entry, struct fw_file, list);
210 #ifdef CONFIG_QEMU_ACPI_TABLE
211 static struct fw_file *qemu_fwcfg_find_file(const char *name)
213 struct list_head *entry;
214 struct fw_file *file;
216 list_for_each(entry, &fw_list) {
217 file = list_entry(entry, struct fw_file, list);
218 if (!strcmp(file->cfg.name, name))
226 * This function allocates memory for ACPI tables
228 * @entry : BIOS linker command entry which tells where to allocate memory
229 * (either high memory or low memory)
230 * @addr : The address that should be used for low memory allcation. If the
231 * memory allocation request is 'ZONE_HIGH' then this parameter will
233 * @return: 0 on success, or negative value on failure
235 static int bios_linker_allocate(struct bios_linker_entry *entry,
238 uint32_t size, align;
239 struct fw_file *file;
240 unsigned long aligned_addr;
242 align = le32_to_cpu(entry->alloc.align);
243 /* align must be power of 2 */
244 if (align & (align - 1)) {
245 printf("error: wrong alignment %u\n", align);
249 file = qemu_fwcfg_find_file(entry->alloc.file);
251 printf("error: can't find file %s\n", entry->alloc.file);
255 size = be32_to_cpu(file->cfg.size);
258 * ZONE_HIGH means we need to allocate from high memory, since
259 * malloc space is already at the end of RAM, so we directly use it.
260 * If allocation zone is ZONE_FSEG, then we use the 'addr' passed
261 * in which is low memory
263 if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH) {
264 aligned_addr = (unsigned long)memalign(align, size);
266 printf("error: allocating resource\n");
269 } else if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG) {
270 aligned_addr = ALIGN(*addr, align);
272 printf("error: invalid allocation zone\n");
276 debug("bios_linker_allocate: allocate file %s, size %u, zone %d, align %u, addr 0x%lx\n",
277 file->cfg.name, size, entry->alloc.zone, align, aligned_addr);
279 qemu_fwcfg_read_entry(be16_to_cpu(file->cfg.select),
280 size, (void *)aligned_addr);
281 file->addr = aligned_addr;
283 /* adjust address for low memory allocation */
284 if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG)
285 *addr = (aligned_addr + size);
291 * This function patches ACPI tables previously loaded
292 * by bios_linker_allocate()
294 * @entry : BIOS linker command entry which tells how to patch
296 * @return: 0 on success, or negative value on failure
298 static int bios_linker_add_pointer(struct bios_linker_entry *entry)
300 struct fw_file *dest, *src;
301 uint32_t offset = le32_to_cpu(entry->pointer.offset);
302 uint64_t pointer = 0;
304 dest = qemu_fwcfg_find_file(entry->pointer.dest_file);
305 if (!dest || !dest->addr)
307 src = qemu_fwcfg_find_file(entry->pointer.src_file);
308 if (!src || !src->addr)
311 debug("bios_linker_add_pointer: dest->addr 0x%lx, src->addr 0x%lx, offset 0x%x size %u, 0x%llx\n",
312 dest->addr, src->addr, offset, entry->pointer.size, pointer);
314 memcpy(&pointer, (char *)dest->addr + offset, entry->pointer.size);
315 pointer = le64_to_cpu(pointer);
316 pointer += (unsigned long)src->addr;
317 pointer = cpu_to_le64(pointer);
318 memcpy((char *)dest->addr + offset, &pointer, entry->pointer.size);
324 * This function updates checksum fields of ACPI tables previously loaded
325 * by bios_linker_allocate()
327 * @entry : BIOS linker command entry which tells where to update ACPI table
329 * @return: 0 on success, or negative value on failure
331 static int bios_linker_add_checksum(struct bios_linker_entry *entry)
333 struct fw_file *file;
334 uint8_t *data, cksum = 0;
335 uint8_t *cksum_start;
337 file = qemu_fwcfg_find_file(entry->cksum.file);
338 if (!file || !file->addr)
341 data = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.offset));
342 cksum_start = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.start));
343 cksum = table_compute_checksum(cksum_start,
344 le32_to_cpu(entry->cksum.length));
350 unsigned install_e820_map(unsigned max_entries, struct e820entry *entries)
353 entries[0].size = ISA_START_ADDRESS;
354 entries[0].type = E820_RAM;
356 entries[1].addr = ISA_START_ADDRESS;
357 entries[1].size = ISA_END_ADDRESS - ISA_START_ADDRESS;
358 entries[1].type = E820_RESERVED;
361 * since we use memalign(malloc) to allocate high memory for
362 * storing ACPI tables, we need to reserve them in e820 tables,
363 * otherwise kernel will reclaim them and data will be corrupted
365 entries[2].addr = ISA_END_ADDRESS;
366 entries[2].size = gd->relocaddr - TOTAL_MALLOC_LEN - ISA_END_ADDRESS;
367 entries[2].type = E820_RAM;
369 /* for simplicity, reserve entire malloc space */
370 entries[3].addr = gd->relocaddr - TOTAL_MALLOC_LEN;
371 entries[3].size = TOTAL_MALLOC_LEN;
372 entries[3].type = E820_RESERVED;
374 entries[4].addr = gd->relocaddr;
375 entries[4].size = gd->ram_size - gd->relocaddr;
376 entries[4].type = E820_RESERVED;
378 entries[5].addr = CONFIG_PCIE_ECAM_BASE;
379 entries[5].size = CONFIG_PCIE_ECAM_SIZE;
380 entries[5].type = E820_RESERVED;
385 /* This function loads and patches ACPI tables provided by QEMU */
386 unsigned long write_acpi_tables(unsigned long addr)
389 struct fw_file *file;
390 struct bios_linker_entry *table_loader;
391 struct bios_linker_entry *entry;
393 struct list_head *list;
395 /* make sure fw_list is loaded */
396 ret = qemu_fwcfg_read_firmware_list();
398 printf("error: can't read firmware file list\n");
402 file = qemu_fwcfg_find_file("etc/table-loader");
404 printf("error: can't find etc/table-loader\n");
408 size = be32_to_cpu(file->cfg.size);
409 if ((size % sizeof(*entry)) != 0) {
410 printf("error: table-loader maybe corrupted\n");
414 table_loader = malloc(size);
416 printf("error: no memory for table-loader\n");
420 qemu_fwcfg_read_entry(be16_to_cpu(file->cfg.select),
423 for (i = 0; i < (size / sizeof(*entry)); i++) {
424 entry = table_loader + i;
425 switch (le32_to_cpu(entry->command)) {
426 case BIOS_LINKER_LOADER_COMMAND_ALLOCATE:
427 ret = bios_linker_allocate(entry, &addr);
431 case BIOS_LINKER_LOADER_COMMAND_ADD_POINTER:
432 ret = bios_linker_add_pointer(entry);
436 case BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM:
437 ret = bios_linker_add_checksum(entry);
448 list_for_each(list, &fw_list) {
449 file = list_entry(list, struct fw_file, list);
451 free((void *)file->addr);
460 static int qemu_fwcfg_list_firmware(void)
463 struct list_head *entry;
464 struct fw_file *file;
466 /* make sure fw_list is loaded */
467 ret = qemu_fwcfg_read_firmware_list();
471 list_for_each(entry, &fw_list) {
472 file = list_entry(entry, struct fw_file, list);
473 printf("%-56s\n", file->cfg.name);
479 void qemu_fwcfg_init(void)
481 fwcfg_present = qemu_fwcfg_present();
483 fwcfg_dma_present = qemu_fwcfg_dma_present();
486 static int qemu_fwcfg_do_list(cmd_tbl_t *cmdtp, int flag,
487 int argc, char * const argv[])
489 if (qemu_fwcfg_list_firmware() < 0)
490 return CMD_RET_FAILURE;
495 static int qemu_fwcfg_do_cpus(cmd_tbl_t *cmdtp, int flag,
496 int argc, char * const argv[])
498 int ret = qemu_fwcfg_online_cpus();
500 printf("QEMU fw_cfg interface not found\n");
501 return CMD_RET_FAILURE;
504 printf("%d cpu(s) online\n", qemu_fwcfg_online_cpus());
509 static int qemu_fwcfg_do_load(cmd_tbl_t *cmdtp, int flag,
510 int argc, char * const argv[])
516 env = getenv("loadaddr");
518 (void *)simple_strtoul(env, NULL, 16) :
519 (void *)CONFIG_LOADADDR;
521 env = getenv("ramdiskaddr");
523 (void *)simple_strtoul(env, NULL, 16) :
524 (void *)CONFIG_RAMDISK_ADDR;
527 load_addr = (void *)simple_strtoul(argv[0], NULL, 16);
528 initrd_addr = (void *)simple_strtoul(argv[1], NULL, 16);
529 } else if (argc == 1) {
530 load_addr = (void *)simple_strtoul(argv[0], NULL, 16);
533 return qemu_fwcfg_setup_kernel(load_addr, initrd_addr);
536 static cmd_tbl_t fwcfg_commands[] = {
537 U_BOOT_CMD_MKENT(list, 0, 1, qemu_fwcfg_do_list, "", ""),
538 U_BOOT_CMD_MKENT(cpus, 0, 1, qemu_fwcfg_do_cpus, "", ""),
539 U_BOOT_CMD_MKENT(load, 2, 1, qemu_fwcfg_do_load, "", ""),
542 static int do_qemu_fw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
545 cmd_tbl_t *fwcfg_cmd;
547 if (!fwcfg_present) {
548 printf("QEMU fw_cfg interface not found\n");
549 return CMD_RET_USAGE;
552 fwcfg_cmd = find_cmd_tbl(argv[1], fwcfg_commands,
553 ARRAY_SIZE(fwcfg_commands));
556 if (!fwcfg_cmd || argc > fwcfg_cmd->maxargs)
557 return CMD_RET_USAGE;
559 ret = fwcfg_cmd->cmd(fwcfg_cmd, flag, argc, argv);
561 return cmd_process_error(fwcfg_cmd, ret);
565 qfw, 4, 1, do_qemu_fw,
566 "QEMU firmware interface",
568 " - list : print firmware(s) currently loaded\n"
569 " - cpus : print online cpu number\n"
570 " - load <kernel addr> <initrd addr> : load kernel and initrd (if any), and setup for zboot\n"