blob: 5ea7a6e2e539c53b15801969b84ed908d80f1c31 [file] [log] [blame]
Miao Yan58a3ce22016-01-07 01:32:00 -08001/*
2 * (C) Copyright 2015 Miao Yan <yanmiaoebst@gmail.com>
3 *
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 */
235static int bios_linker_allocate(struct bios_linker_entry *entry,
236 unsigned long *addr)
237{
238 uint32_t size, align;
239 struct fw_file *file;
240 unsigned long aligned_addr;
241
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);
246 return -EINVAL;
247 }
248
249 file = qemu_fwcfg_find_file(entry->alloc.file);
250 if (!file) {
251 printf("error: can't find file %s\n", entry->alloc.file);
252 return -ENOENT;
253 }
254
255 size = be32_to_cpu(file->cfg.size);
256
257 /*
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
262 */
263 if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH) {
264 aligned_addr = (unsigned long)memalign(align, size);
265 if (!aligned_addr) {
266 printf("error: allocating resource\n");
267 return -ENOMEM;
268 }
269 } else if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG) {
270 aligned_addr = ALIGN(*addr, align);
271 } else {
272 printf("error: invalid allocation zone\n");
273 return -EINVAL;
274 }
275
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);
278
279 qemu_fwcfg_read_entry(be16_to_cpu(file->cfg.select),
280 size, (void *)aligned_addr);
281 file->addr = aligned_addr;
282
283 /* adjust address for low memory allocation */
284 if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG)
285 *addr = (aligned_addr + size);
286
287 return 0;
288}
289
290/*
291 * This function patches ACPI tables previously loaded
292 * by bios_linker_allocate()
293 *
294 * @entry : BIOS linker command entry which tells how to patch
295 * ACPI tables
296 * @return: 0 on success, or negative value on failure
297 */
298static int bios_linker_add_pointer(struct bios_linker_entry *entry)
299{
300 struct fw_file *dest, *src;
301 uint32_t offset = le32_to_cpu(entry->pointer.offset);
302 uint64_t pointer = 0;
303
304 dest = qemu_fwcfg_find_file(entry->pointer.dest_file);
305 if (!dest || !dest->addr)
306 return -ENOENT;
307 src = qemu_fwcfg_find_file(entry->pointer.src_file);
308 if (!src || !src->addr)
309 return -ENOENT;
310
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);
313
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);
319
320 return 0;
321}
322
323/*
324 * This function updates checksum fields of ACPI tables previously loaded
325 * by bios_linker_allocate()
326 *
327 * @entry : BIOS linker command entry which tells where to update ACPI table
328 * checksums
329 * @return: 0 on success, or negative value on failure
330 */
331static int bios_linker_add_checksum(struct bios_linker_entry *entry)
332{
333 struct fw_file *file;
334 uint8_t *data, cksum = 0;
335 uint8_t *cksum_start;
336
337 file = qemu_fwcfg_find_file(entry->cksum.file);
338 if (!file || !file->addr)
339 return -ENOENT;
340
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));
345 *data = cksum;
346
347 return 0;
348}
349
350unsigned install_e820_map(unsigned max_entries, struct e820entry *entries)
351{
352 entries[0].addr = 0;
353 entries[0].size = ISA_START_ADDRESS;
354 entries[0].type = E820_RAM;
355
356 entries[1].addr = ISA_START_ADDRESS;
357 entries[1].size = ISA_END_ADDRESS - ISA_START_ADDRESS;
358 entries[1].type = E820_RESERVED;
359
360 /*
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
364 */
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;
368
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;
373
374 entries[4].addr = gd->relocaddr;
375 entries[4].size = gd->ram_size - gd->relocaddr;
376 entries[4].type = E820_RESERVED;
377
378 entries[5].addr = CONFIG_PCIE_ECAM_BASE;
379 entries[5].size = CONFIG_PCIE_ECAM_SIZE;
380 entries[5].type = E820_RESERVED;
381
382 return 6;
383}
384
385/* This function loads and patches ACPI tables provided by QEMU */
386unsigned long write_acpi_tables(unsigned long addr)
387{
388 int i, ret = 0;
389 struct fw_file *file;
390 struct bios_linker_entry *table_loader;
391 struct bios_linker_entry *entry;
392 uint32_t size;
393 struct list_head *list;
394
395 /* make sure fw_list is loaded */
396 ret = qemu_fwcfg_read_firmware_list();
397 if (ret) {
398 printf("error: can't read firmware file list\n");
399 return addr;
400 }
401
402 file = qemu_fwcfg_find_file("etc/table-loader");
403 if (!file) {
404 printf("error: can't find etc/table-loader\n");
405 return addr;
406 }
407
408 size = be32_to_cpu(file->cfg.size);
409 if ((size % sizeof(*entry)) != 0) {
410 printf("error: table-loader maybe corrupted\n");
411 return addr;
412 }
413
414 table_loader = malloc(size);
415 if (!table_loader) {
416 printf("error: no memory for table-loader\n");
417 return addr;
418 }
419
420 qemu_fwcfg_read_entry(be16_to_cpu(file->cfg.select),
421 size, table_loader);
422
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);
428 if (ret)
429 goto out;
430 break;
431 case BIOS_LINKER_LOADER_COMMAND_ADD_POINTER:
432 ret = bios_linker_add_pointer(entry);
433 if (ret)
434 goto out;
435 break;
436 case BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM:
437 ret = bios_linker_add_checksum(entry);
438 if (ret)
439 goto out;
440 break;
441 default:
442 break;
443 }
444 }
445
446out:
447 if (ret) {
448 list_for_each(list, &fw_list) {
449 file = list_entry(list, struct fw_file, list);
450 if (file->addr)
451 free((void *)file->addr);
452 }
453 }
454
455 free(table_loader);
456 return addr;
457}
458#endif
459
Miao Yanc90a0582016-01-20 01:57:04 -0800460static int qemu_fwcfg_list_firmware(void)
461{
462 int ret;
463 struct list_head *entry;
464 struct fw_file *file;
465
466 /* make sure fw_list is loaded */
467 ret = qemu_fwcfg_read_firmware_list();
468 if (ret)
469 return ret;
470
471 list_for_each(entry, &fw_list) {
472 file = list_entry(entry, struct fw_file, list);
473 printf("%-56s\n", file->cfg.name);
474 }
Miao Yan58a3ce22016-01-07 01:32:00 -0800475
Miao Yan58a3ce22016-01-07 01:32:00 -0800476 return 0;
477}
478
479void qemu_fwcfg_init(void)
480{
481 fwcfg_present = qemu_fwcfg_present();
482 if (fwcfg_present)
483 fwcfg_dma_present = qemu_fwcfg_dma_present();
484}
485
486static int qemu_fwcfg_do_list(cmd_tbl_t *cmdtp, int flag,
487 int argc, char * const argv[])
488{
489 if (qemu_fwcfg_list_firmware() < 0)
490 return CMD_RET_FAILURE;
491
492 return 0;
493}
494
495static int qemu_fwcfg_do_cpus(cmd_tbl_t *cmdtp, int flag,
496 int argc, char * const argv[])
497{
498 int ret = qemu_fwcfg_online_cpus();
499 if (ret < 0) {
500 printf("QEMU fw_cfg interface not found\n");
501 return CMD_RET_FAILURE;
502 }
503
504 printf("%d cpu(s) online\n", qemu_fwcfg_online_cpus());
505
506 return 0;
507}
508
509static int qemu_fwcfg_do_load(cmd_tbl_t *cmdtp, int flag,
510 int argc, char * const argv[])
511{
512 char *env;
513 void *load_addr;
514 void *initrd_addr;
515
516 env = getenv("loadaddr");
517 load_addr = env ?
518 (void *)simple_strtoul(env, NULL, 16) :
519 (void *)CONFIG_LOADADDR;
520
521 env = getenv("ramdiskaddr");
522 initrd_addr = env ?
523 (void *)simple_strtoul(env, NULL, 16) :
524 (void *)CONFIG_RAMDISK_ADDR;
525
526 if (argc == 2) {
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);
531 }
532
533 return qemu_fwcfg_setup_kernel(load_addr, initrd_addr);
534}
535
536static 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, "", ""),
540};
541
542static int do_qemu_fw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
543{
544 int ret;
545 cmd_tbl_t *fwcfg_cmd;
546
547 if (!fwcfg_present) {
548 printf("QEMU fw_cfg interface not found\n");
549 return CMD_RET_USAGE;
550 }
551
552 fwcfg_cmd = find_cmd_tbl(argv[1], fwcfg_commands,
553 ARRAY_SIZE(fwcfg_commands));
554 argc -= 2;
555 argv += 2;
556 if (!fwcfg_cmd || argc > fwcfg_cmd->maxargs)
557 return CMD_RET_USAGE;
558
559 ret = fwcfg_cmd->cmd(fwcfg_cmd, flag, argc, argv);
560
561 return cmd_process_error(fwcfg_cmd, ret);
562}
563
564U_BOOT_CMD(
565 qfw, 4, 1, do_qemu_fw,
566 "QEMU firmware interface",
567 "<command>\n"
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"
571)