blob: bcd34afe06bc8a9b3aa396689db60bdc43d7da84 [file] [log] [blame]
Miao Yanf60df202016-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 Yan25757222016-01-20 01:57:04 -080013#include <linux/list.h>
Miao Yanf60df202016-01-07 01:32:00 -080014
15static bool fwcfg_present;
16static bool fwcfg_dma_present;
17
Miao Yan25757222016-01-20 01:57:04 -080018static LIST_HEAD(fw_list);
19
Miao Yanf60df202016-01-07 01:32:00 -080020/* Read configuration item using fw_cfg PIO interface */
21static void qemu_fwcfg_read_entry_pio(uint16_t entry,
22 uint32_t size, void *address)
23{
24 uint32_t i = 0;
25 uint8_t *data = address;
26
27 /*
28 * writting FW_CFG_INVALID will cause read operation to resume at
29 * last offset, otherwise read will start at offset 0
30 */
31 if (entry != FW_CFG_INVALID)
32 outw(entry, FW_CONTROL_PORT);
33 while (size--)
34 data[i++] = inb(FW_DATA_PORT);
35}
36
37/* Read configuration item using fw_cfg DMA interface */
38static void qemu_fwcfg_read_entry_dma(uint16_t entry,
39 uint32_t size, void *address)
40{
41 struct fw_cfg_dma_access dma;
42
43 dma.length = cpu_to_be32(size);
44 dma.address = cpu_to_be64((uintptr_t)address);
45 dma.control = cpu_to_be32(FW_CFG_DMA_READ);
46
47 /*
48 * writting FW_CFG_INVALID will cause read operation to resume at
49 * last offset, otherwise read will start at offset 0
50 */
51 if (entry != FW_CFG_INVALID)
52 dma.control |= cpu_to_be32(FW_CFG_DMA_SELECT | (entry << 16));
53
54 barrier();
55
56 debug("qemu_fwcfg_dma_read_entry: addr %p, length %u control 0x%x\n",
57 address, size, be32_to_cpu(dma.control));
58
59 outl(cpu_to_be32((uint32_t)&dma), FW_DMA_PORT_HIGH);
60
61 while (be32_to_cpu(dma.control) & ~FW_CFG_DMA_ERROR)
62 __asm__ __volatile__ ("pause");
63}
64
65static bool qemu_fwcfg_present(void)
66{
67 uint32_t qemu;
68
69 qemu_fwcfg_read_entry_pio(FW_CFG_SIGNATURE, 4, &qemu);
70 return be32_to_cpu(qemu) == QEMU_FW_CFG_SIGNATURE;
71}
72
73static bool qemu_fwcfg_dma_present(void)
74{
75 uint8_t dma_enabled;
76
77 qemu_fwcfg_read_entry_pio(FW_CFG_ID, 1, &dma_enabled);
78 if (dma_enabled & FW_CFG_DMA_ENABLED)
79 return true;
80
81 return false;
82}
83
84static void qemu_fwcfg_read_entry(uint16_t entry,
85 uint32_t length, void *address)
86{
87 if (fwcfg_dma_present)
88 qemu_fwcfg_read_entry_dma(entry, length, address);
89 else
90 qemu_fwcfg_read_entry_pio(entry, length, address);
91}
92
93int qemu_fwcfg_online_cpus(void)
94{
95 uint16_t nb_cpus;
96
97 if (!fwcfg_present)
98 return -ENODEV;
99
100 qemu_fwcfg_read_entry(FW_CFG_NB_CPUS, 2, &nb_cpus);
101
102 return le16_to_cpu(nb_cpus);
103}
104
105/*
106 * This function prepares kernel for zboot. It loads kernel data
107 * to 'load_addr', initrd to 'initrd_addr' and kernel command
108 * line using qemu fw_cfg interface.
109 */
110static int qemu_fwcfg_setup_kernel(void *load_addr, void *initrd_addr)
111{
112 char *data_addr;
113 uint32_t setup_size, kernel_size, cmdline_size, initrd_size;
114
115 qemu_fwcfg_read_entry(FW_CFG_SETUP_SIZE, 4, &setup_size);
116 qemu_fwcfg_read_entry(FW_CFG_KERNEL_SIZE, 4, &kernel_size);
117
118 if (setup_size == 0 || kernel_size == 0) {
119 printf("warning: no kernel available\n");
120 return -1;
121 }
122
123 data_addr = load_addr;
124 qemu_fwcfg_read_entry(FW_CFG_SETUP_DATA,
125 le32_to_cpu(setup_size), data_addr);
126 data_addr += le32_to_cpu(setup_size);
127
128 qemu_fwcfg_read_entry(FW_CFG_KERNEL_DATA,
129 le32_to_cpu(kernel_size), data_addr);
130 data_addr += le32_to_cpu(kernel_size);
131
132 data_addr = initrd_addr;
133 qemu_fwcfg_read_entry(FW_CFG_INITRD_SIZE, 4, &initrd_size);
134 if (initrd_size == 0) {
135 printf("warning: no initrd available\n");
136 } else {
137 qemu_fwcfg_read_entry(FW_CFG_INITRD_DATA,
138 le32_to_cpu(initrd_size), data_addr);
139 data_addr += le32_to_cpu(initrd_size);
140 }
141
142 qemu_fwcfg_read_entry(FW_CFG_CMDLINE_SIZE, 4, &cmdline_size);
143 if (cmdline_size) {
144 qemu_fwcfg_read_entry(FW_CFG_CMDLINE_DATA,
145 le32_to_cpu(cmdline_size), data_addr);
146 /*
147 * if kernel cmdline only contains '\0', (e.g. no -append
148 * when invoking qemu), do not update bootargs
149 */
150 if (*data_addr != '\0') {
151 if (setenv("bootargs", data_addr) < 0)
152 printf("warning: unable to change bootargs\n");
153 }
154 }
155
156 printf("loading kernel to address %p size %x", load_addr,
157 le32_to_cpu(kernel_size));
158 if (initrd_size)
159 printf(" initrd %p size %x\n",
160 initrd_addr,
161 le32_to_cpu(initrd_size));
162 else
163 printf("\n");
164
165 return 0;
166}
167
Miao Yan25757222016-01-20 01:57:04 -0800168static int qemu_fwcfg_read_firmware_list(void)
Miao Yanf60df202016-01-07 01:32:00 -0800169{
170 int i;
171 uint32_t count;
Miao Yan25757222016-01-20 01:57:04 -0800172 struct fw_file *file;
173 struct list_head *entry;
174
175 /* don't read it twice */
176 if (!list_empty(&fw_list))
177 return 0;
Miao Yanf60df202016-01-07 01:32:00 -0800178
179 qemu_fwcfg_read_entry(FW_CFG_FILE_DIR, 4, &count);
180 if (!count)
181 return 0;
182
183 count = be32_to_cpu(count);
Miao Yan25757222016-01-20 01:57:04 -0800184 for (i = 0; i < count; i++) {
185 file = malloc(sizeof(*file));
186 if (!file) {
187 printf("error: allocating resource\n");
188 goto err;
189 }
190 qemu_fwcfg_read_entry(FW_CFG_INVALID,
191 sizeof(struct fw_cfg_file), &file->cfg);
192 file->addr = 0;
193 list_add_tail(&file->list, &fw_list);
194 }
Miao Yanf60df202016-01-07 01:32:00 -0800195
Miao Yan25757222016-01-20 01:57:04 -0800196 return 0;
Miao Yanf60df202016-01-07 01:32:00 -0800197
Miao Yan25757222016-01-20 01:57:04 -0800198err:
199 list_for_each(entry, &fw_list) {
200 file = list_entry(entry, struct fw_file, list);
201 free(file);
202 }
203
204 return -ENOMEM;
205}
206
207static int qemu_fwcfg_list_firmware(void)
208{
209 int ret;
210 struct list_head *entry;
211 struct fw_file *file;
212
213 /* make sure fw_list is loaded */
214 ret = qemu_fwcfg_read_firmware_list();
215 if (ret)
216 return ret;
217
218 list_for_each(entry, &fw_list) {
219 file = list_entry(entry, struct fw_file, list);
220 printf("%-56s\n", file->cfg.name);
221 }
222
Miao Yanf60df202016-01-07 01:32:00 -0800223 return 0;
224}
225
226void qemu_fwcfg_init(void)
227{
228 fwcfg_present = qemu_fwcfg_present();
229 if (fwcfg_present)
230 fwcfg_dma_present = qemu_fwcfg_dma_present();
231}
232
233static int qemu_fwcfg_do_list(cmd_tbl_t *cmdtp, int flag,
234 int argc, char * const argv[])
235{
236 if (qemu_fwcfg_list_firmware() < 0)
237 return CMD_RET_FAILURE;
238
239 return 0;
240}
241
242static int qemu_fwcfg_do_cpus(cmd_tbl_t *cmdtp, int flag,
243 int argc, char * const argv[])
244{
245 int ret = qemu_fwcfg_online_cpus();
246 if (ret < 0) {
247 printf("QEMU fw_cfg interface not found\n");
248 return CMD_RET_FAILURE;
249 }
250
251 printf("%d cpu(s) online\n", qemu_fwcfg_online_cpus());
252
253 return 0;
254}
255
256static int qemu_fwcfg_do_load(cmd_tbl_t *cmdtp, int flag,
257 int argc, char * const argv[])
258{
259 char *env;
260 void *load_addr;
261 void *initrd_addr;
262
263 env = getenv("loadaddr");
264 load_addr = env ?
265 (void *)simple_strtoul(env, NULL, 16) :
266 (void *)CONFIG_LOADADDR;
267
268 env = getenv("ramdiskaddr");
269 initrd_addr = env ?
270 (void *)simple_strtoul(env, NULL, 16) :
271 (void *)CONFIG_RAMDISK_ADDR;
272
273 if (argc == 2) {
274 load_addr = (void *)simple_strtoul(argv[0], NULL, 16);
275 initrd_addr = (void *)simple_strtoul(argv[1], NULL, 16);
276 } else if (argc == 1) {
277 load_addr = (void *)simple_strtoul(argv[0], NULL, 16);
278 }
279
280 return qemu_fwcfg_setup_kernel(load_addr, initrd_addr);
281}
282
283static cmd_tbl_t fwcfg_commands[] = {
284 U_BOOT_CMD_MKENT(list, 0, 1, qemu_fwcfg_do_list, "", ""),
285 U_BOOT_CMD_MKENT(cpus, 0, 1, qemu_fwcfg_do_cpus, "", ""),
286 U_BOOT_CMD_MKENT(load, 2, 1, qemu_fwcfg_do_load, "", ""),
287};
288
289static int do_qemu_fw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
290{
291 int ret;
292 cmd_tbl_t *fwcfg_cmd;
293
294 if (!fwcfg_present) {
295 printf("QEMU fw_cfg interface not found\n");
296 return CMD_RET_USAGE;
297 }
298
299 fwcfg_cmd = find_cmd_tbl(argv[1], fwcfg_commands,
300 ARRAY_SIZE(fwcfg_commands));
301 argc -= 2;
302 argv += 2;
303 if (!fwcfg_cmd || argc > fwcfg_cmd->maxargs)
304 return CMD_RET_USAGE;
305
306 ret = fwcfg_cmd->cmd(fwcfg_cmd, flag, argc, argv);
307
308 return cmd_process_error(fwcfg_cmd, ret);
309}
310
311U_BOOT_CMD(
312 qfw, 4, 1, do_qemu_fw,
313 "QEMU firmware interface",
314 "<command>\n"
315 " - list : print firmware(s) currently loaded\n"
316 " - cpus : print online cpu number\n"
317 " - load <kernel addr> <initrd addr> : load kernel and initrd (if any), and setup for zboot\n"
318)