blob: 2e2794ebd7fcd8a12c37e90515dcac2857d59351 [file] [log] [blame]
Miao Yan58a3ce22016-01-07 01:32:00 -08001/*
Bin Meng529b7612016-04-13 01:00:51 -07002 * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com>
Miao Yan58a3ce22016-01-07 01:32:00 -08003 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <command.h>
9#include <errno.h>
10#include <malloc.h>
11#include <asm/io.h>
12#include <asm/fw_cfg.h>
Miao Yan3b68c522016-01-20 01:57:06 -080013#include <asm/tables.h>
14#include <asm/e820.h>
Miao Yanc90a0582016-01-20 01:57:04 -080015#include <linux/list.h>
Miao Yan3b68c522016-01-20 01:57:06 -080016#include <memalign.h>
Miao Yan58a3ce22016-01-07 01:32:00 -080017
18static bool fwcfg_present;
19static bool fwcfg_dma_present;
20
Miao Yanc90a0582016-01-20 01:57:04 -080021static LIST_HEAD(fw_list);
22
Miao Yan58a3ce22016-01-07 01:32:00 -080023/* Read configuration item using fw_cfg PIO interface */
24static void qemu_fwcfg_read_entry_pio(uint16_t entry,
25 uint32_t size, void *address)
26{
27 uint32_t i = 0;
28 uint8_t *data = address;
29
30 /*
31 * writting FW_CFG_INVALID will cause read operation to resume at
32 * last offset, otherwise read will start at offset 0
33 */
34 if (entry != FW_CFG_INVALID)
35 outw(entry, FW_CONTROL_PORT);
36 while (size--)
37 data[i++] = inb(FW_DATA_PORT);
38}
39
40/* Read configuration item using fw_cfg DMA interface */
41static void qemu_fwcfg_read_entry_dma(uint16_t entry,
42 uint32_t size, void *address)
43{
44 struct fw_cfg_dma_access dma;
45
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);
49
50 /*
51 * writting FW_CFG_INVALID will cause read operation to resume at
52 * last offset, otherwise read will start at offset 0
53 */
54 if (entry != FW_CFG_INVALID)
55 dma.control |= cpu_to_be32(FW_CFG_DMA_SELECT | (entry << 16));
56
57 barrier();
58
59 debug("qemu_fwcfg_dma_read_entry: addr %p, length %u control 0x%x\n",
60 address, size, be32_to_cpu(dma.control));
61
62 outl(cpu_to_be32((uint32_t)&dma), FW_DMA_PORT_HIGH);
63
64 while (be32_to_cpu(dma.control) & ~FW_CFG_DMA_ERROR)
65 __asm__ __volatile__ ("pause");
66}
67
68static bool qemu_fwcfg_present(void)
69{
70 uint32_t qemu;
71
72 qemu_fwcfg_read_entry_pio(FW_CFG_SIGNATURE, 4, &qemu);
73 return be32_to_cpu(qemu) == QEMU_FW_CFG_SIGNATURE;
74}
75
76static bool qemu_fwcfg_dma_present(void)
77{
78 uint8_t dma_enabled;
79
80 qemu_fwcfg_read_entry_pio(FW_CFG_ID, 1, &dma_enabled);
81 if (dma_enabled & FW_CFG_DMA_ENABLED)
82 return true;
83
84 return false;
85}
86
87static void qemu_fwcfg_read_entry(uint16_t entry,
88 uint32_t length, void *address)
89{
90 if (fwcfg_dma_present)
91 qemu_fwcfg_read_entry_dma(entry, length, address);
92 else
93 qemu_fwcfg_read_entry_pio(entry, length, address);
94}
95
96int qemu_fwcfg_online_cpus(void)
97{
98 uint16_t nb_cpus;
99
100 if (!fwcfg_present)
101 return -ENODEV;
102
103 qemu_fwcfg_read_entry(FW_CFG_NB_CPUS, 2, &nb_cpus);
104
105 return le16_to_cpu(nb_cpus);
106}
107
108/*
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.
112 */
113static int qemu_fwcfg_setup_kernel(void *load_addr, void *initrd_addr)
114{
115 char *data_addr;
116 uint32_t setup_size, kernel_size, cmdline_size, initrd_size;
117
118 qemu_fwcfg_read_entry(FW_CFG_SETUP_SIZE, 4, &setup_size);
119 qemu_fwcfg_read_entry(FW_CFG_KERNEL_SIZE, 4, &kernel_size);
120
121 if (setup_size == 0 || kernel_size == 0) {
122 printf("warning: no kernel available\n");
123 return -1;
124 }
125
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);
130
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);
134
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");
139 } else {
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);
143 }
144
145 qemu_fwcfg_read_entry(FW_CFG_CMDLINE_SIZE, 4, &cmdline_size);
146 if (cmdline_size) {
147 qemu_fwcfg_read_entry(FW_CFG_CMDLINE_DATA,
148 le32_to_cpu(cmdline_size), data_addr);
149 /*
150 * if kernel cmdline only contains '\0', (e.g. no -append
151 * when invoking qemu), do not update bootargs
152 */
153 if (*data_addr != '\0') {
154 if (setenv("bootargs", data_addr) < 0)
155 printf("warning: unable to change bootargs\n");
156 }
157 }
158
159 printf("loading kernel to address %p size %x", load_addr,
160 le32_to_cpu(kernel_size));
161 if (initrd_size)
162 printf(" initrd %p size %x\n",
163 initrd_addr,
164 le32_to_cpu(initrd_size));
165 else
166 printf("\n");
167
168 return 0;
169}
170
Miao Yanc90a0582016-01-20 01:57:04 -0800171static int qemu_fwcfg_read_firmware_list(void)
Miao Yan58a3ce22016-01-07 01:32:00 -0800172{
173 int i;
174 uint32_t count;
Miao Yanc90a0582016-01-20 01:57:04 -0800175 struct fw_file *file;
176 struct list_head *entry;
177
178 /* don't read it twice */
179 if (!list_empty(&fw_list))
180 return 0;
Miao Yan58a3ce22016-01-07 01:32:00 -0800181
182 qemu_fwcfg_read_entry(FW_CFG_FILE_DIR, 4, &count);
183 if (!count)
184 return 0;
185
186 count = be32_to_cpu(count);
Miao Yanc90a0582016-01-20 01:57:04 -0800187 for (i = 0; i < count; i++) {
188 file = malloc(sizeof(*file));
189 if (!file) {
190 printf("error: allocating resource\n");
191 goto err;
192 }
193 qemu_fwcfg_read_entry(FW_CFG_INVALID,
194 sizeof(struct fw_cfg_file), &file->cfg);
195 file->addr = 0;
196 list_add_tail(&file->list, &fw_list);
197 }
198
199 return 0;
Miao Yan58a3ce22016-01-07 01:32:00 -0800200
Miao Yanc90a0582016-01-20 01:57:04 -0800201err:
202 list_for_each(entry, &fw_list) {
203 file = list_entry(entry, struct fw_file, list);
204 free(file);
205 }
206
207 return -ENOMEM;
208}
209
Miao Yan3b68c522016-01-20 01:57:06 -0800210#ifdef CONFIG_QEMU_ACPI_TABLE
211static struct fw_file *qemu_fwcfg_find_file(const char *name)
212{
213 struct list_head *entry;
214 struct fw_file *file;
215
216 list_for_each(entry, &fw_list) {
217 file = list_entry(entry, struct fw_file, list);
218 if (!strcmp(file->cfg.name, name))
219 return file;
220 }
221
222 return NULL;
223}
224
225/*
226 * This function allocates memory for ACPI tables
227 *
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
232 * be ignored.
233 * @return: 0 on success, or negative value on failure
234 */
Bin Meng5c549102016-02-27 22:58:00 -0800235static int bios_linker_allocate(struct bios_linker_entry *entry, u32 *addr)
Miao Yan3b68c522016-01-20 01:57:06 -0800236{
237 uint32_t size, align;
238 struct fw_file *file;
239 unsigned long aligned_addr;
240
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);
245 return -EINVAL;
246 }
247
248 file = qemu_fwcfg_find_file(entry->alloc.file);
249 if (!file) {
250 printf("error: can't find file %s\n", entry->alloc.file);
251 return -ENOENT;
252 }
253
254 size = be32_to_cpu(file->cfg.size);
255
256 /*
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
261 */
262 if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH) {
263 aligned_addr = (unsigned long)memalign(align, size);
264 if (!aligned_addr) {
265 printf("error: allocating resource\n");
266 return -ENOMEM;
267 }
268 } else if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG) {
269 aligned_addr = ALIGN(*addr, align);
270 } else {
271 printf("error: invalid allocation zone\n");
272 return -EINVAL;
273 }
274
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);
277
278 qemu_fwcfg_read_entry(be16_to_cpu(file->cfg.select),
279 size, (void *)aligned_addr);
280 file->addr = aligned_addr;
281
282 /* adjust address for low memory allocation */
283 if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG)
284 *addr = (aligned_addr + size);
285
286 return 0;
287}
288
289/*
290 * This function patches ACPI tables previously loaded
291 * by bios_linker_allocate()
292 *
293 * @entry : BIOS linker command entry which tells how to patch
294 * ACPI tables
295 * @return: 0 on success, or negative value on failure
296 */
297static int bios_linker_add_pointer(struct bios_linker_entry *entry)
298{
299 struct fw_file *dest, *src;
300 uint32_t offset = le32_to_cpu(entry->pointer.offset);
301 uint64_t pointer = 0;
302
303 dest = qemu_fwcfg_find_file(entry->pointer.dest_file);
304 if (!dest || !dest->addr)
305 return -ENOENT;
306 src = qemu_fwcfg_find_file(entry->pointer.src_file);
307 if (!src || !src->addr)
308 return -ENOENT;
309
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);
312
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);
318
319 return 0;
320}
321
322/*
323 * This function updates checksum fields of ACPI tables previously loaded
324 * by bios_linker_allocate()
325 *
326 * @entry : BIOS linker command entry which tells where to update ACPI table
327 * checksums
328 * @return: 0 on success, or negative value on failure
329 */
330static int bios_linker_add_checksum(struct bios_linker_entry *entry)
331{
332 struct fw_file *file;
333 uint8_t *data, cksum = 0;
334 uint8_t *cksum_start;
335
336 file = qemu_fwcfg_find_file(entry->cksum.file);
337 if (!file || !file->addr)
338 return -ENOENT;
339
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));
344 *data = cksum;
345
346 return 0;
347}
348
349unsigned install_e820_map(unsigned max_entries, struct e820entry *entries)
350{
351 entries[0].addr = 0;
352 entries[0].size = ISA_START_ADDRESS;
353 entries[0].type = E820_RAM;
354
355 entries[1].addr = ISA_START_ADDRESS;
356 entries[1].size = ISA_END_ADDRESS - ISA_START_ADDRESS;
357 entries[1].type = E820_RESERVED;
358
359 /*
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
363 */
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;
367
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;
372
373 entries[4].addr = gd->relocaddr;
374 entries[4].size = gd->ram_size - gd->relocaddr;
375 entries[4].type = E820_RESERVED;
376
377 entries[5].addr = CONFIG_PCIE_ECAM_BASE;
378 entries[5].size = CONFIG_PCIE_ECAM_SIZE;
379 entries[5].type = E820_RESERVED;
380
381 return 6;
382}
383
384/* This function loads and patches ACPI tables provided by QEMU */
Bin Meng5c549102016-02-27 22:58:00 -0800385u32 write_acpi_tables(u32 addr)
Miao Yan3b68c522016-01-20 01:57:06 -0800386{
387 int i, ret = 0;
388 struct fw_file *file;
389 struct bios_linker_entry *table_loader;
390 struct bios_linker_entry *entry;
391 uint32_t size;
392 struct list_head *list;
393
394 /* make sure fw_list is loaded */
395 ret = qemu_fwcfg_read_firmware_list();
396 if (ret) {
397 printf("error: can't read firmware file list\n");
398 return addr;
399 }
400
401 file = qemu_fwcfg_find_file("etc/table-loader");
402 if (!file) {
403 printf("error: can't find etc/table-loader\n");
404 return addr;
405 }
406
407 size = be32_to_cpu(file->cfg.size);
408 if ((size % sizeof(*entry)) != 0) {
409 printf("error: table-loader maybe corrupted\n");
410 return addr;
411 }
412
413 table_loader = malloc(size);
414 if (!table_loader) {
415 printf("error: no memory for table-loader\n");
416 return addr;
417 }
418
419 qemu_fwcfg_read_entry(be16_to_cpu(file->cfg.select),
420 size, table_loader);
421
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);
427 if (ret)
428 goto out;
429 break;
430 case BIOS_LINKER_LOADER_COMMAND_ADD_POINTER:
431 ret = bios_linker_add_pointer(entry);
432 if (ret)
433 goto out;
434 break;
435 case BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM:
436 ret = bios_linker_add_checksum(entry);
437 if (ret)
438 goto out;
439 break;
440 default:
441 break;
442 }
443 }
444
445out:
446 if (ret) {
447 list_for_each(list, &fw_list) {
448 file = list_entry(list, struct fw_file, list);
449 if (file->addr)
450 free((void *)file->addr);
451 }
452 }
453
454 free(table_loader);
455 return addr;
456}
457#endif
458
Miao Yanc90a0582016-01-20 01:57:04 -0800459static int qemu_fwcfg_list_firmware(void)
460{
461 int ret;
462 struct list_head *entry;
463 struct fw_file *file;
464
465 /* make sure fw_list is loaded */
466 ret = qemu_fwcfg_read_firmware_list();
467 if (ret)
468 return ret;
469
470 list_for_each(entry, &fw_list) {
471 file = list_entry(entry, struct fw_file, list);
472 printf("%-56s\n", file->cfg.name);
473 }
Miao Yan58a3ce22016-01-07 01:32:00 -0800474
Miao Yan58a3ce22016-01-07 01:32:00 -0800475 return 0;
476}
477
478void qemu_fwcfg_init(void)
479{
480 fwcfg_present = qemu_fwcfg_present();
481 if (fwcfg_present)
482 fwcfg_dma_present = qemu_fwcfg_dma_present();
483}
484
485static int qemu_fwcfg_do_list(cmd_tbl_t *cmdtp, int flag,
486 int argc, char * const argv[])
487{
488 if (qemu_fwcfg_list_firmware() < 0)
489 return CMD_RET_FAILURE;
490
491 return 0;
492}
493
494static int qemu_fwcfg_do_cpus(cmd_tbl_t *cmdtp, int flag,
495 int argc, char * const argv[])
496{
497 int ret = qemu_fwcfg_online_cpus();
498 if (ret < 0) {
499 printf("QEMU fw_cfg interface not found\n");
500 return CMD_RET_FAILURE;
501 }
502
503 printf("%d cpu(s) online\n", qemu_fwcfg_online_cpus());
504
505 return 0;
506}
507
508static int qemu_fwcfg_do_load(cmd_tbl_t *cmdtp, int flag,
509 int argc, char * const argv[])
510{
511 char *env;
512 void *load_addr;
513 void *initrd_addr;
514
515 env = getenv("loadaddr");
516 load_addr = env ?
517 (void *)simple_strtoul(env, NULL, 16) :
518 (void *)CONFIG_LOADADDR;
519
520 env = getenv("ramdiskaddr");
521 initrd_addr = env ?
522 (void *)simple_strtoul(env, NULL, 16) :
523 (void *)CONFIG_RAMDISK_ADDR;
524
525 if (argc == 2) {
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);
530 }
531
532 return qemu_fwcfg_setup_kernel(load_addr, initrd_addr);
533}
534
535static 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, "", ""),
539};
540
541static int do_qemu_fw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
542{
543 int ret;
544 cmd_tbl_t *fwcfg_cmd;
545
546 if (!fwcfg_present) {
547 printf("QEMU fw_cfg interface not found\n");
548 return CMD_RET_USAGE;
549 }
550
551 fwcfg_cmd = find_cmd_tbl(argv[1], fwcfg_commands,
552 ARRAY_SIZE(fwcfg_commands));
553 argc -= 2;
554 argv += 2;
555 if (!fwcfg_cmd || argc > fwcfg_cmd->maxargs)
556 return CMD_RET_USAGE;
557
558 ret = fwcfg_cmd->cmd(fwcfg_cmd, flag, argc, argv);
559
560 return cmd_process_error(fwcfg_cmd, ret);
561}
562
563U_BOOT_CMD(
564 qfw, 4, 1, do_qemu_fw,
565 "QEMU firmware interface",
566 "<command>\n"
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"
570)