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
, u32
*addr
)
237 uint32_t size
, align
;
238 struct fw_file
*file
;
239 unsigned long aligned_addr
;
241 align
= le32_to_cpu(entry
->alloc
.align
);
242 /* align must be power of 2 */
243 if (align
& (align
- 1)) {
244 printf("error: wrong alignment %u\n", align
);
248 file
= qemu_fwcfg_find_file(entry
->alloc
.file
);
250 printf("error: can't find file %s\n", entry
->alloc
.file
);
254 size
= be32_to_cpu(file
->cfg
.size
);
257 * ZONE_HIGH means we need to allocate from high memory, since
258 * malloc space is already at the end of RAM, so we directly use it.
259 * If allocation zone is ZONE_FSEG, then we use the 'addr' passed
260 * in which is low memory
262 if (entry
->alloc
.zone
== BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH
) {
263 aligned_addr
= (unsigned long)memalign(align
, size
);
265 printf("error: allocating resource\n");
268 } else if (entry
->alloc
.zone
== BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG
) {
269 aligned_addr
= ALIGN(*addr
, align
);
271 printf("error: invalid allocation zone\n");
275 debug("bios_linker_allocate: allocate file %s, size %u, zone %d, align %u, addr 0x%lx\n",
276 file
->cfg
.name
, size
, entry
->alloc
.zone
, align
, aligned_addr
);
278 qemu_fwcfg_read_entry(be16_to_cpu(file
->cfg
.select
),
279 size
, (void *)aligned_addr
);
280 file
->addr
= aligned_addr
;
282 /* adjust address for low memory allocation */
283 if (entry
->alloc
.zone
== BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG
)
284 *addr
= (aligned_addr
+ size
);
290 * This function patches ACPI tables previously loaded
291 * by bios_linker_allocate()
293 * @entry : BIOS linker command entry which tells how to patch
295 * @return: 0 on success, or negative value on failure
297 static int bios_linker_add_pointer(struct bios_linker_entry
*entry
)
299 struct fw_file
*dest
, *src
;
300 uint32_t offset
= le32_to_cpu(entry
->pointer
.offset
);
301 uint64_t pointer
= 0;
303 dest
= qemu_fwcfg_find_file(entry
->pointer
.dest_file
);
304 if (!dest
|| !dest
->addr
)
306 src
= qemu_fwcfg_find_file(entry
->pointer
.src_file
);
307 if (!src
|| !src
->addr
)
310 debug("bios_linker_add_pointer: dest->addr 0x%lx, src->addr 0x%lx, offset 0x%x size %u, 0x%llx\n",
311 dest
->addr
, src
->addr
, offset
, entry
->pointer
.size
, pointer
);
313 memcpy(&pointer
, (char *)dest
->addr
+ offset
, entry
->pointer
.size
);
314 pointer
= le64_to_cpu(pointer
);
315 pointer
+= (unsigned long)src
->addr
;
316 pointer
= cpu_to_le64(pointer
);
317 memcpy((char *)dest
->addr
+ offset
, &pointer
, entry
->pointer
.size
);
323 * This function updates checksum fields of ACPI tables previously loaded
324 * by bios_linker_allocate()
326 * @entry : BIOS linker command entry which tells where to update ACPI table
328 * @return: 0 on success, or negative value on failure
330 static int bios_linker_add_checksum(struct bios_linker_entry
*entry
)
332 struct fw_file
*file
;
333 uint8_t *data
, cksum
= 0;
334 uint8_t *cksum_start
;
336 file
= qemu_fwcfg_find_file(entry
->cksum
.file
);
337 if (!file
|| !file
->addr
)
340 data
= (uint8_t *)(file
->addr
+ le32_to_cpu(entry
->cksum
.offset
));
341 cksum_start
= (uint8_t *)(file
->addr
+ le32_to_cpu(entry
->cksum
.start
));
342 cksum
= table_compute_checksum(cksum_start
,
343 le32_to_cpu(entry
->cksum
.length
));
349 unsigned install_e820_map(unsigned max_entries
, struct e820entry
*entries
)
352 entries
[0].size
= ISA_START_ADDRESS
;
353 entries
[0].type
= E820_RAM
;
355 entries
[1].addr
= ISA_START_ADDRESS
;
356 entries
[1].size
= ISA_END_ADDRESS
- ISA_START_ADDRESS
;
357 entries
[1].type
= E820_RESERVED
;
360 * since we use memalign(malloc) to allocate high memory for
361 * storing ACPI tables, we need to reserve them in e820 tables,
362 * otherwise kernel will reclaim them and data will be corrupted
364 entries
[2].addr
= ISA_END_ADDRESS
;
365 entries
[2].size
= gd
->relocaddr
- TOTAL_MALLOC_LEN
- ISA_END_ADDRESS
;
366 entries
[2].type
= E820_RAM
;
368 /* for simplicity, reserve entire malloc space */
369 entries
[3].addr
= gd
->relocaddr
- TOTAL_MALLOC_LEN
;
370 entries
[3].size
= TOTAL_MALLOC_LEN
;
371 entries
[3].type
= E820_RESERVED
;
373 entries
[4].addr
= gd
->relocaddr
;
374 entries
[4].size
= gd
->ram_size
- gd
->relocaddr
;
375 entries
[4].type
= E820_RESERVED
;
377 entries
[5].addr
= CONFIG_PCIE_ECAM_BASE
;
378 entries
[5].size
= CONFIG_PCIE_ECAM_SIZE
;
379 entries
[5].type
= E820_RESERVED
;
384 /* This function loads and patches ACPI tables provided by QEMU */
385 u32
write_acpi_tables(u32 addr
)
388 struct fw_file
*file
;
389 struct bios_linker_entry
*table_loader
;
390 struct bios_linker_entry
*entry
;
392 struct list_head
*list
;
394 /* make sure fw_list is loaded */
395 ret
= qemu_fwcfg_read_firmware_list();
397 printf("error: can't read firmware file list\n");
401 file
= qemu_fwcfg_find_file("etc/table-loader");
403 printf("error: can't find etc/table-loader\n");
407 size
= be32_to_cpu(file
->cfg
.size
);
408 if ((size
% sizeof(*entry
)) != 0) {
409 printf("error: table-loader maybe corrupted\n");
413 table_loader
= malloc(size
);
415 printf("error: no memory for table-loader\n");
419 qemu_fwcfg_read_entry(be16_to_cpu(file
->cfg
.select
),
422 for (i
= 0; i
< (size
/ sizeof(*entry
)); i
++) {
423 entry
= table_loader
+ i
;
424 switch (le32_to_cpu(entry
->command
)) {
425 case BIOS_LINKER_LOADER_COMMAND_ALLOCATE
:
426 ret
= bios_linker_allocate(entry
, &addr
);
430 case BIOS_LINKER_LOADER_COMMAND_ADD_POINTER
:
431 ret
= bios_linker_add_pointer(entry
);
435 case BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM
:
436 ret
= bios_linker_add_checksum(entry
);
447 list_for_each(list
, &fw_list
) {
448 file
= list_entry(list
, struct fw_file
, list
);
450 free((void *)file
->addr
);
459 static int qemu_fwcfg_list_firmware(void)
462 struct list_head
*entry
;
463 struct fw_file
*file
;
465 /* make sure fw_list is loaded */
466 ret
= qemu_fwcfg_read_firmware_list();
470 list_for_each(entry
, &fw_list
) {
471 file
= list_entry(entry
, struct fw_file
, list
);
472 printf("%-56s\n", file
->cfg
.name
);
478 void qemu_fwcfg_init(void)
480 fwcfg_present
= qemu_fwcfg_present();
482 fwcfg_dma_present
= qemu_fwcfg_dma_present();
485 static int qemu_fwcfg_do_list(cmd_tbl_t
*cmdtp
, int flag
,
486 int argc
, char * const argv
[])
488 if (qemu_fwcfg_list_firmware() < 0)
489 return CMD_RET_FAILURE
;
494 static int qemu_fwcfg_do_cpus(cmd_tbl_t
*cmdtp
, int flag
,
495 int argc
, char * const argv
[])
497 int ret
= qemu_fwcfg_online_cpus();
499 printf("QEMU fw_cfg interface not found\n");
500 return CMD_RET_FAILURE
;
503 printf("%d cpu(s) online\n", qemu_fwcfg_online_cpus());
508 static int qemu_fwcfg_do_load(cmd_tbl_t
*cmdtp
, int flag
,
509 int argc
, char * const argv
[])
515 env
= getenv("loadaddr");
517 (void *)simple_strtoul(env
, NULL
, 16) :
518 (void *)CONFIG_LOADADDR
;
520 env
= getenv("ramdiskaddr");
522 (void *)simple_strtoul(env
, NULL
, 16) :
523 (void *)CONFIG_RAMDISK_ADDR
;
526 load_addr
= (void *)simple_strtoul(argv
[0], NULL
, 16);
527 initrd_addr
= (void *)simple_strtoul(argv
[1], NULL
, 16);
528 } else if (argc
== 1) {
529 load_addr
= (void *)simple_strtoul(argv
[0], NULL
, 16);
532 return qemu_fwcfg_setup_kernel(load_addr
, initrd_addr
);
535 static cmd_tbl_t fwcfg_commands
[] = {
536 U_BOOT_CMD_MKENT(list
, 0, 1, qemu_fwcfg_do_list
, "", ""),
537 U_BOOT_CMD_MKENT(cpus
, 0, 1, qemu_fwcfg_do_cpus
, "", ""),
538 U_BOOT_CMD_MKENT(load
, 2, 1, qemu_fwcfg_do_load
, "", ""),
541 static int do_qemu_fw(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
544 cmd_tbl_t
*fwcfg_cmd
;
546 if (!fwcfg_present
) {
547 printf("QEMU fw_cfg interface not found\n");
548 return CMD_RET_USAGE
;
551 fwcfg_cmd
= find_cmd_tbl(argv
[1], fwcfg_commands
,
552 ARRAY_SIZE(fwcfg_commands
));
555 if (!fwcfg_cmd
|| argc
> fwcfg_cmd
->maxargs
)
556 return CMD_RET_USAGE
;
558 ret
= fwcfg_cmd
->cmd(fwcfg_cmd
, flag
, argc
, argv
);
560 return cmd_process_error(fwcfg_cmd
, ret
);
564 qfw
, 4, 1, do_qemu_fw
,
565 "QEMU firmware interface",
567 " - list : print firmware(s) currently loaded\n"
568 " - cpus : print online cpu number\n"
569 " - load <kernel addr> <initrd addr> : load kernel and initrd (if any), and setup for zboot\n"