blob: 0a93feeb4b2eed11e497bc3c7de3ce4eca063dae [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Miao Yan4fcd7f22016-05-22 19:37:14 -07002/*
3 * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com>
Asherah Connor4ffa95d2021-03-19 18:21:40 +11004 * (C) Copyright 2021 Asherah Connor <ashe@kivikakk.ee>
Miao Yan4fcd7f22016-05-22 19:37:14 -07005 */
6
Asherah Connor4ffa95d2021-03-19 18:21:40 +11007#define LOG_CATEGORY UCLASS_QFW
8
Miao Yan4fcd7f22016-05-22 19:37:14 -07009#include <common.h>
Simon Glass77ca5ff2023-01-28 15:00:24 -070010#include <bootdev.h>
11#include <bootflow.h>
12#include <bootmeth.h>
Miao Yan4fcd7f22016-05-22 19:37:14 -070013#include <command.h>
14#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Miao Yan4fcd7f22016-05-22 19:37:14 -070016#include <malloc.h>
Miao Yan92106272016-05-22 19:37:17 -070017#include <qfw.h>
Asherah Connor4ffa95d2021-03-19 18:21:40 +110018#include <dm.h>
19#include <misc.h>
Simon Glassf3425cb2021-12-01 09:02:40 -070020#include <tables_csum.h>
Simon Glass3fed4672023-07-15 21:38:50 -060021#include <asm/acpi_table.h>
Miao Yan4fcd7f22016-05-22 19:37:14 -070022
Simon Glass16170882021-12-01 09:02:49 -070023#if defined(CONFIG_GENERATE_ACPI_TABLE) && !defined(CONFIG_SANDBOX)
Miao Yan12970d22016-05-22 19:37:20 -070024/*
25 * This function allocates memory for ACPI tables
26 *
27 * @entry : BIOS linker command entry which tells where to allocate memory
28 * (either high memory or low memory)
29 * @addr : The address that should be used for low memory allcation. If the
30 * memory allocation request is 'ZONE_HIGH' then this parameter will
31 * be ignored.
32 * @return: 0 on success, or negative value on failure
33 */
Asherah Connor4ffa95d2021-03-19 18:21:40 +110034static int bios_linker_allocate(struct udevice *dev,
35 struct bios_linker_entry *entry, ulong *addr)
Miao Yan12970d22016-05-22 19:37:20 -070036{
37 uint32_t size, align;
38 struct fw_file *file;
39 unsigned long aligned_addr;
40
41 align = le32_to_cpu(entry->alloc.align);
42 /* align must be power of 2 */
43 if (align & (align - 1)) {
44 printf("error: wrong alignment %u\n", align);
45 return -EINVAL;
46 }
47
Asherah Connor4ffa95d2021-03-19 18:21:40 +110048 file = qfw_find_file(dev, entry->alloc.file);
Miao Yan12970d22016-05-22 19:37:20 -070049 if (!file) {
50 printf("error: can't find file %s\n", entry->alloc.file);
51 return -ENOENT;
52 }
53
54 size = be32_to_cpu(file->cfg.size);
55
56 /*
57 * ZONE_HIGH means we need to allocate from high memory, since
58 * malloc space is already at the end of RAM, so we directly use it.
59 * If allocation zone is ZONE_FSEG, then we use the 'addr' passed
60 * in which is low memory
61 */
62 if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH) {
63 aligned_addr = (unsigned long)memalign(align, size);
64 if (!aligned_addr) {
65 printf("error: allocating resource\n");
66 return -ENOMEM;
67 }
68 } else if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG) {
69 aligned_addr = ALIGN(*addr, align);
70 } else {
71 printf("error: invalid allocation zone\n");
72 return -EINVAL;
73 }
74
75 debug("bios_linker_allocate: allocate file %s, size %u, zone %d, align %u, addr 0x%lx\n",
76 file->cfg.name, size, entry->alloc.zone, align, aligned_addr);
77
Asherah Connor4ffa95d2021-03-19 18:21:40 +110078 qfw_read_entry(dev, be16_to_cpu(file->cfg.select), size,
79 (void *)aligned_addr);
Miao Yan12970d22016-05-22 19:37:20 -070080 file->addr = aligned_addr;
81
82 /* adjust address for low memory allocation */
83 if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG)
84 *addr = (aligned_addr + size);
85
86 return 0;
87}
88
89/*
90 * This function patches ACPI tables previously loaded
91 * by bios_linker_allocate()
92 *
93 * @entry : BIOS linker command entry which tells how to patch
94 * ACPI tables
95 * @return: 0 on success, or negative value on failure
96 */
Asherah Connor4ffa95d2021-03-19 18:21:40 +110097static int bios_linker_add_pointer(struct udevice *dev,
98 struct bios_linker_entry *entry)
Miao Yan12970d22016-05-22 19:37:20 -070099{
100 struct fw_file *dest, *src;
101 uint32_t offset = le32_to_cpu(entry->pointer.offset);
102 uint64_t pointer = 0;
103
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100104 dest = qfw_find_file(dev, entry->pointer.dest_file);
Miao Yan12970d22016-05-22 19:37:20 -0700105 if (!dest || !dest->addr)
106 return -ENOENT;
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100107 src = qfw_find_file(dev, entry->pointer.src_file);
Miao Yan12970d22016-05-22 19:37:20 -0700108 if (!src || !src->addr)
109 return -ENOENT;
110
111 debug("bios_linker_add_pointer: dest->addr 0x%lx, src->addr 0x%lx, offset 0x%x size %u, 0x%llx\n",
112 dest->addr, src->addr, offset, entry->pointer.size, pointer);
113
114 memcpy(&pointer, (char *)dest->addr + offset, entry->pointer.size);
115 pointer = le64_to_cpu(pointer);
116 pointer += (unsigned long)src->addr;
117 pointer = cpu_to_le64(pointer);
118 memcpy((char *)dest->addr + offset, &pointer, entry->pointer.size);
119
120 return 0;
121}
122
123/*
124 * This function updates checksum fields of ACPI tables previously loaded
125 * by bios_linker_allocate()
126 *
127 * @entry : BIOS linker command entry which tells where to update ACPI table
128 * checksums
129 * @return: 0 on success, or negative value on failure
130 */
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100131static int bios_linker_add_checksum(struct udevice *dev,
132 struct bios_linker_entry *entry)
Miao Yan12970d22016-05-22 19:37:20 -0700133{
134 struct fw_file *file;
135 uint8_t *data, cksum = 0;
136 uint8_t *cksum_start;
137
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100138 file = qfw_find_file(dev, entry->cksum.file);
Miao Yan12970d22016-05-22 19:37:20 -0700139 if (!file || !file->addr)
140 return -ENOENT;
141
142 data = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.offset));
143 cksum_start = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.start));
144 cksum = table_compute_checksum(cksum_start,
145 le32_to_cpu(entry->cksum.length));
146 *data = cksum;
147
148 return 0;
149}
150
151/* This function loads and patches ACPI tables provided by QEMU */
Simon Glassca37a392017-01-16 07:03:35 -0700152ulong write_acpi_tables(ulong addr)
Miao Yan12970d22016-05-22 19:37:20 -0700153{
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100154 int i, ret;
Miao Yan12970d22016-05-22 19:37:20 -0700155 struct fw_file *file;
156 struct bios_linker_entry *table_loader;
157 struct bios_linker_entry *entry;
158 uint32_t size;
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100159 struct udevice *dev;
160
161 ret = qfw_get_dev(&dev);
162 if (ret) {
163 printf("error: no qfw\n");
164 return addr;
165 }
Miao Yan12970d22016-05-22 19:37:20 -0700166
167 /* make sure fw_list is loaded */
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100168 ret = qfw_read_firmware_list(dev);
Miao Yan12970d22016-05-22 19:37:20 -0700169 if (ret) {
170 printf("error: can't read firmware file list\n");
171 return addr;
172 }
173
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100174 file = qfw_find_file(dev, "etc/table-loader");
Miao Yan12970d22016-05-22 19:37:20 -0700175 if (!file) {
176 printf("error: can't find etc/table-loader\n");
177 return addr;
178 }
179
180 size = be32_to_cpu(file->cfg.size);
181 if ((size % sizeof(*entry)) != 0) {
182 printf("error: table-loader maybe corrupted\n");
183 return addr;
184 }
185
186 table_loader = malloc(size);
187 if (!table_loader) {
188 printf("error: no memory for table-loader\n");
189 return addr;
190 }
191
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100192 qfw_read_entry(dev, be16_to_cpu(file->cfg.select), size, table_loader);
Miao Yan12970d22016-05-22 19:37:20 -0700193
194 for (i = 0; i < (size / sizeof(*entry)); i++) {
195 entry = table_loader + i;
196 switch (le32_to_cpu(entry->command)) {
197 case BIOS_LINKER_LOADER_COMMAND_ALLOCATE:
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100198 ret = bios_linker_allocate(dev, entry, &addr);
Miao Yan12970d22016-05-22 19:37:20 -0700199 if (ret)
200 goto out;
201 break;
202 case BIOS_LINKER_LOADER_COMMAND_ADD_POINTER:
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100203 ret = bios_linker_add_pointer(dev, entry);
Miao Yan12970d22016-05-22 19:37:20 -0700204 if (ret)
205 goto out;
206 break;
207 case BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM:
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100208 ret = bios_linker_add_checksum(dev, entry);
Miao Yan12970d22016-05-22 19:37:20 -0700209 if (ret)
210 goto out;
211 break;
212 default:
213 break;
214 }
215 }
216
217out:
218 if (ret) {
219 struct fw_cfg_file_iter iter;
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100220 for (file = qfw_file_iter_init(dev, &iter);
221 !qfw_file_iter_end(&iter);
222 file = qfw_file_iter_next(&iter)) {
Miao Yan12970d22016-05-22 19:37:20 -0700223 if (file->addr) {
224 free((void *)file->addr);
225 file->addr = 0;
226 }
227 }
228 }
229
230 free(table_loader);
Simon Glass3fed4672023-07-15 21:38:50 -0600231
232 gd_set_acpi_start(acpi_get_rsdp_addr());
233
Miao Yan12970d22016-05-22 19:37:20 -0700234 return addr;
235}
Bin Meng22069e22018-01-30 05:01:17 -0800236
237ulong acpi_get_rsdp_addr(void)
238{
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100239 int ret;
Bin Meng22069e22018-01-30 05:01:17 -0800240 struct fw_file *file;
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100241 struct udevice *dev;
Bin Meng22069e22018-01-30 05:01:17 -0800242
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100243 ret = qfw_get_dev(&dev);
244 if (ret) {
245 printf("error: no qfw\n");
246 return 0;
247 }
248
249 file = qfw_find_file(dev, "etc/acpi/rsdp");
Bin Meng22069e22018-01-30 05:01:17 -0800250 return file->addr;
251}
Miao Yan12970d22016-05-22 19:37:20 -0700252#endif
253
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100254static void qfw_read_entry_io(struct qfw_dev *qdev, u16 entry, u32 size,
255 void *address)
Miao Yan4fcd7f22016-05-22 19:37:14 -0700256{
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100257 struct dm_qfw_ops *ops = dm_qfw_get_ops(qdev->dev);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700258
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100259 debug("%s: entry 0x%x, size %u address %p\n", __func__, entry, size,
260 address);
261
262 ops->read_entry_io(qdev->dev, entry, size, address);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700263}
264
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100265static void qfw_read_entry_dma(struct qfw_dev *qdev, u16 entry, u32 size,
266 void *address)
Miao Yan4fcd7f22016-05-22 19:37:14 -0700267{
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100268 struct dm_qfw_ops *ops = dm_qfw_get_ops(qdev->dev);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700269
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100270 struct qfw_dma dma = {
271 .length = cpu_to_be32(size),
272 .address = cpu_to_be64((uintptr_t)address),
273 .control = cpu_to_be32(FW_CFG_DMA_READ),
274 };
Miao Yan4fcd7f22016-05-22 19:37:14 -0700275
276 /*
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100277 * writing FW_CFG_INVALID will cause read operation to resume at last
278 * offset, otherwise read will start at offset 0
Miao Yan4fcd7f22016-05-22 19:37:14 -0700279 */
280 if (entry != FW_CFG_INVALID)
281 dma.control |= cpu_to_be32(FW_CFG_DMA_SELECT | (entry << 16));
282
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100283 debug("%s: entry 0x%x, size %u address %p, control 0x%x\n", __func__,
Miao Yan8a153832016-05-22 19:37:15 -0700284 entry, size, address, be32_to_cpu(dma.control));
Miao Yan4fcd7f22016-05-22 19:37:14 -0700285
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100286 barrier();
Miao Yan4fcd7f22016-05-22 19:37:14 -0700287
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100288 ops->read_entry_dma(qdev->dev, &dma);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700289}
290
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100291void qfw_read_entry(struct udevice *dev, u16 entry, u32 size, void *address)
Miao Yan4fcd7f22016-05-22 19:37:14 -0700292{
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100293 struct qfw_dev *qdev = dev_get_uclass_priv(dev);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700294
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100295 if (qdev->dma_present)
296 qfw_read_entry_dma(qdev, entry, size, address);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700297 else
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100298 qfw_read_entry_io(qdev, entry, size, address);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700299}
300
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100301int qfw_register(struct udevice *dev)
Miao Yan4fcd7f22016-05-22 19:37:14 -0700302{
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100303 struct qfw_dev *qdev = dev_get_uclass_priv(dev);
304 u32 qemu, dma_enabled;
Miao Yan4fcd7f22016-05-22 19:37:14 -0700305
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100306 qdev->dev = dev;
307 INIT_LIST_HEAD(&qdev->fw_list);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700308
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100309 qfw_read_entry_io(qdev, FW_CFG_SIGNATURE, 4, &qemu);
310 if (be32_to_cpu(qemu) != QEMU_FW_CFG_SIGNATURE)
311 return -ENODEV;
Miao Yan4fcd7f22016-05-22 19:37:14 -0700312
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100313 qfw_read_entry_io(qdev, FW_CFG_ID, 1, &dma_enabled);
314 if (dma_enabled & FW_CFG_DMA_ENABLED)
315 qdev->dma_present = true;
Miao Yan4fcd7f22016-05-22 19:37:14 -0700316
317 return 0;
Miao Yan4fcd7f22016-05-22 19:37:14 -0700318}
319
Simon Glass77ca5ff2023-01-28 15:00:24 -0700320static int qfw_post_bind(struct udevice *dev)
321{
322 int ret;
323
324 ret = bootdev_setup_for_dev(dev, "qfw_bootdev");
325 if (ret)
326 return log_msg_ret("dev", ret);
327
328 return 0;
329}
330
331static int qfw_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
332 struct bootflow *bflow)
333{
334 const struct udevice *media = dev_get_parent(dev);
335 int ret;
336
337 if (!CONFIG_IS_ENABLED(BOOTSTD))
338 return -ENOSYS;
339
340 log_debug("media=%s\n", media->name);
341 ret = bootmeth_check(bflow->method, iter);
342 if (ret)
343 return log_msg_ret("check", ret);
344
345 log_debug("iter->part=%d\n", iter->part);
346
347 /* We only support the whole device, not partitions */
348 if (iter->part)
349 return log_msg_ret("max", -ESHUTDOWN);
350
351 log_debug("reading bootflow with method: %s\n", bflow->method->name);
352 ret = bootmeth_read_bootflow(bflow->method, bflow);
353 if (ret)
354 return log_msg_ret("method", ret);
355
356 return 0;
357}
358
359static int qfw_bootdev_bind(struct udevice *dev)
360{
361 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
362
363 ucp->prio = BOOTDEVP_4_SCAN_FAST;
364
365 return 0;
366}
367
368static int qfw_bootdev_hunt(struct bootdev_hunter *info, bool show)
369{
370 int ret;
371
372 ret = uclass_probe_all(UCLASS_QFW);
373 if (ret && ret != -ENOENT)
374 return log_msg_ret("vir", ret);
375
376 return 0;
377}
378
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100379UCLASS_DRIVER(qfw) = {
380 .id = UCLASS_QFW,
381 .name = "qfw",
Simon Glass77ca5ff2023-01-28 15:00:24 -0700382 .post_bind = qfw_post_bind,
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100383 .per_device_auto = sizeof(struct qfw_dev),
384};
Simon Glass77ca5ff2023-01-28 15:00:24 -0700385
386struct bootdev_ops qfw_bootdev_ops = {
387 .get_bootflow = qfw_get_bootflow,
388};
389
390static const struct udevice_id qfw_bootdev_ids[] = {
391 { .compatible = "u-boot,bootdev-qfw" },
392 { }
393};
394
395U_BOOT_DRIVER(qfw_bootdev) = {
396 .name = "qfw_bootdev",
397 .id = UCLASS_BOOTDEV,
398 .ops = &qfw_bootdev_ops,
399 .bind = qfw_bootdev_bind,
400 .of_match = qfw_bootdev_ids,
401};
402
403BOOTDEV_HUNTER(qfw_bootdev_hunter) = {
404 .prio = BOOTDEVP_4_SCAN_FAST,
405 .uclass = UCLASS_QFW,
406 .hunt = qfw_bootdev_hunt,
407 .drv = DM_DRIVER_REF(qfw_bootdev),
408};