blob: e3b6b4cd741a1c5f9c9bd17b43776cadf1ffdb4e [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>
Heinrich Schuchardt2c1fd4a2023-11-09 09:23:02 -080010#include <acpi/acpi_table.h>
Simon Glass77ca5ff2023-01-28 15:00:24 -070011#include <bootdev.h>
12#include <bootflow.h>
13#include <bootmeth.h>
Miao Yan4fcd7f22016-05-22 19:37:14 -070014#include <command.h>
15#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Miao Yan4fcd7f22016-05-22 19:37:14 -070017#include <malloc.h>
Miao Yan92106272016-05-22 19:37:17 -070018#include <qfw.h>
Asherah Connor4ffa95d2021-03-19 18:21:40 +110019#include <dm.h>
20#include <misc.h>
Simon Glassf3425cb2021-12-01 09:02:40 -070021#include <tables_csum.h>
Simon Glass3fed4672023-07-15 21:38:50 -060022#include <asm/acpi_table.h>
Miao Yan4fcd7f22016-05-22 19:37:14 -070023
Simon Glass16170882021-12-01 09:02:49 -070024#if defined(CONFIG_GENERATE_ACPI_TABLE) && !defined(CONFIG_SANDBOX)
Miao Yan12970d22016-05-22 19:37:20 -070025/*
26 * This function allocates memory for ACPI tables
27 *
28 * @entry : BIOS linker command entry which tells where to allocate memory
29 * (either high memory or low memory)
30 * @addr : The address that should be used for low memory allcation. If the
31 * memory allocation request is 'ZONE_HIGH' then this parameter will
32 * be ignored.
33 * @return: 0 on success, or negative value on failure
34 */
Asherah Connor4ffa95d2021-03-19 18:21:40 +110035static int bios_linker_allocate(struct udevice *dev,
36 struct bios_linker_entry *entry, ulong *addr)
Miao Yan12970d22016-05-22 19:37:20 -070037{
38 uint32_t size, align;
39 struct fw_file *file;
40 unsigned long aligned_addr;
41
42 align = le32_to_cpu(entry->alloc.align);
43 /* align must be power of 2 */
44 if (align & (align - 1)) {
45 printf("error: wrong alignment %u\n", align);
46 return -EINVAL;
47 }
48
Asherah Connor4ffa95d2021-03-19 18:21:40 +110049 file = qfw_find_file(dev, entry->alloc.file);
Miao Yan12970d22016-05-22 19:37:20 -070050 if (!file) {
51 printf("error: can't find file %s\n", entry->alloc.file);
52 return -ENOENT;
53 }
54
55 size = be32_to_cpu(file->cfg.size);
56
57 /*
58 * ZONE_HIGH means we need to allocate from high memory, since
59 * malloc space is already at the end of RAM, so we directly use it.
60 * If allocation zone is ZONE_FSEG, then we use the 'addr' passed
61 * in which is low memory
62 */
63 if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH) {
64 aligned_addr = (unsigned long)memalign(align, size);
65 if (!aligned_addr) {
66 printf("error: allocating resource\n");
67 return -ENOMEM;
68 }
Simon Glass66ca25c2023-07-15 21:39:10 -060069 if (aligned_addr < gd->arch.table_start_high)
70 gd->arch.table_start_high = aligned_addr;
71 if (aligned_addr + size > gd->arch.table_end_high)
72 gd->arch.table_end_high = aligned_addr + size;
73
Miao Yan12970d22016-05-22 19:37:20 -070074 } else if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG) {
75 aligned_addr = ALIGN(*addr, align);
76 } else {
77 printf("error: invalid allocation zone\n");
78 return -EINVAL;
79 }
80
81 debug("bios_linker_allocate: allocate file %s, size %u, zone %d, align %u, addr 0x%lx\n",
82 file->cfg.name, size, entry->alloc.zone, align, aligned_addr);
83
Asherah Connor4ffa95d2021-03-19 18:21:40 +110084 qfw_read_entry(dev, be16_to_cpu(file->cfg.select), size,
85 (void *)aligned_addr);
Miao Yan12970d22016-05-22 19:37:20 -070086 file->addr = aligned_addr;
87
88 /* adjust address for low memory allocation */
89 if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG)
90 *addr = (aligned_addr + size);
91
92 return 0;
93}
94
95/*
96 * This function patches ACPI tables previously loaded
97 * by bios_linker_allocate()
98 *
99 * @entry : BIOS linker command entry which tells how to patch
100 * ACPI tables
101 * @return: 0 on success, or negative value on failure
102 */
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100103static int bios_linker_add_pointer(struct udevice *dev,
104 struct bios_linker_entry *entry)
Miao Yan12970d22016-05-22 19:37:20 -0700105{
106 struct fw_file *dest, *src;
107 uint32_t offset = le32_to_cpu(entry->pointer.offset);
108 uint64_t pointer = 0;
109
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100110 dest = qfw_find_file(dev, entry->pointer.dest_file);
Miao Yan12970d22016-05-22 19:37:20 -0700111 if (!dest || !dest->addr)
112 return -ENOENT;
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100113 src = qfw_find_file(dev, entry->pointer.src_file);
Miao Yan12970d22016-05-22 19:37:20 -0700114 if (!src || !src->addr)
115 return -ENOENT;
116
117 debug("bios_linker_add_pointer: dest->addr 0x%lx, src->addr 0x%lx, offset 0x%x size %u, 0x%llx\n",
118 dest->addr, src->addr, offset, entry->pointer.size, pointer);
119
120 memcpy(&pointer, (char *)dest->addr + offset, entry->pointer.size);
121 pointer = le64_to_cpu(pointer);
122 pointer += (unsigned long)src->addr;
123 pointer = cpu_to_le64(pointer);
124 memcpy((char *)dest->addr + offset, &pointer, entry->pointer.size);
125
126 return 0;
127}
128
129/*
130 * This function updates checksum fields of ACPI tables previously loaded
131 * by bios_linker_allocate()
132 *
133 * @entry : BIOS linker command entry which tells where to update ACPI table
134 * checksums
135 * @return: 0 on success, or negative value on failure
136 */
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100137static int bios_linker_add_checksum(struct udevice *dev,
138 struct bios_linker_entry *entry)
Miao Yan12970d22016-05-22 19:37:20 -0700139{
140 struct fw_file *file;
141 uint8_t *data, cksum = 0;
142 uint8_t *cksum_start;
143
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100144 file = qfw_find_file(dev, entry->cksum.file);
Miao Yan12970d22016-05-22 19:37:20 -0700145 if (!file || !file->addr)
146 return -ENOENT;
147
148 data = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.offset));
149 cksum_start = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.start));
150 cksum = table_compute_checksum(cksum_start,
151 le32_to_cpu(entry->cksum.length));
152 *data = cksum;
153
154 return 0;
155}
156
157/* This function loads and patches ACPI tables provided by QEMU */
Simon Glassca37a392017-01-16 07:03:35 -0700158ulong write_acpi_tables(ulong addr)
Miao Yan12970d22016-05-22 19:37:20 -0700159{
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100160 int i, ret;
Miao Yan12970d22016-05-22 19:37:20 -0700161 struct fw_file *file;
162 struct bios_linker_entry *table_loader;
163 struct bios_linker_entry *entry;
164 uint32_t size;
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100165 struct udevice *dev;
166
167 ret = qfw_get_dev(&dev);
168 if (ret) {
169 printf("error: no qfw\n");
170 return addr;
171 }
Miao Yan12970d22016-05-22 19:37:20 -0700172
173 /* make sure fw_list is loaded */
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100174 ret = qfw_read_firmware_list(dev);
Miao Yan12970d22016-05-22 19:37:20 -0700175 if (ret) {
176 printf("error: can't read firmware file list\n");
177 return addr;
178 }
179
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100180 file = qfw_find_file(dev, "etc/table-loader");
Miao Yan12970d22016-05-22 19:37:20 -0700181 if (!file) {
182 printf("error: can't find etc/table-loader\n");
183 return addr;
184 }
185
186 size = be32_to_cpu(file->cfg.size);
187 if ((size % sizeof(*entry)) != 0) {
188 printf("error: table-loader maybe corrupted\n");
189 return addr;
190 }
191
192 table_loader = malloc(size);
193 if (!table_loader) {
194 printf("error: no memory for table-loader\n");
195 return addr;
196 }
197
Simon Glass66ca25c2023-07-15 21:39:10 -0600198 /* QFW always puts tables at high addresses */
199 gd->arch.table_start_high = (ulong)table_loader;
200 gd->arch.table_end_high = (ulong)table_loader;
201
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100202 qfw_read_entry(dev, be16_to_cpu(file->cfg.select), size, table_loader);
Miao Yan12970d22016-05-22 19:37:20 -0700203
204 for (i = 0; i < (size / sizeof(*entry)); i++) {
205 entry = table_loader + i;
206 switch (le32_to_cpu(entry->command)) {
207 case BIOS_LINKER_LOADER_COMMAND_ALLOCATE:
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100208 ret = bios_linker_allocate(dev, entry, &addr);
Miao Yan12970d22016-05-22 19:37:20 -0700209 if (ret)
210 goto out;
211 break;
212 case BIOS_LINKER_LOADER_COMMAND_ADD_POINTER:
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100213 ret = bios_linker_add_pointer(dev, entry);
Miao Yan12970d22016-05-22 19:37:20 -0700214 if (ret)
215 goto out;
216 break;
217 case BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM:
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100218 ret = bios_linker_add_checksum(dev, entry);
Miao Yan12970d22016-05-22 19:37:20 -0700219 if (ret)
220 goto out;
221 break;
222 default:
223 break;
224 }
225 }
226
227out:
228 if (ret) {
229 struct fw_cfg_file_iter iter;
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100230 for (file = qfw_file_iter_init(dev, &iter);
231 !qfw_file_iter_end(&iter);
232 file = qfw_file_iter_next(&iter)) {
Miao Yan12970d22016-05-22 19:37:20 -0700233 if (file->addr) {
234 free((void *)file->addr);
235 file->addr = 0;
236 }
237 }
238 }
239
240 free(table_loader);
Simon Glass3fed4672023-07-15 21:38:50 -0600241
242 gd_set_acpi_start(acpi_get_rsdp_addr());
243
Miao Yan12970d22016-05-22 19:37:20 -0700244 return addr;
245}
Bin Meng22069e22018-01-30 05:01:17 -0800246
247ulong acpi_get_rsdp_addr(void)
248{
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100249 int ret;
Bin Meng22069e22018-01-30 05:01:17 -0800250 struct fw_file *file;
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100251 struct udevice *dev;
Bin Meng22069e22018-01-30 05:01:17 -0800252
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100253 ret = qfw_get_dev(&dev);
254 if (ret) {
255 printf("error: no qfw\n");
256 return 0;
257 }
258
259 file = qfw_find_file(dev, "etc/acpi/rsdp");
Bin Meng22069e22018-01-30 05:01:17 -0800260 return file->addr;
261}
Miao Yan12970d22016-05-22 19:37:20 -0700262#endif
263
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100264static void qfw_read_entry_io(struct qfw_dev *qdev, u16 entry, u32 size,
265 void *address)
Miao Yan4fcd7f22016-05-22 19:37:14 -0700266{
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100267 struct dm_qfw_ops *ops = dm_qfw_get_ops(qdev->dev);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700268
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100269 debug("%s: entry 0x%x, size %u address %p\n", __func__, entry, size,
270 address);
271
272 ops->read_entry_io(qdev->dev, entry, size, address);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700273}
274
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100275static void qfw_read_entry_dma(struct qfw_dev *qdev, u16 entry, u32 size,
276 void *address)
Miao Yan4fcd7f22016-05-22 19:37:14 -0700277{
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100278 struct dm_qfw_ops *ops = dm_qfw_get_ops(qdev->dev);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700279
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100280 struct qfw_dma dma = {
281 .length = cpu_to_be32(size),
282 .address = cpu_to_be64((uintptr_t)address),
283 .control = cpu_to_be32(FW_CFG_DMA_READ),
284 };
Miao Yan4fcd7f22016-05-22 19:37:14 -0700285
286 /*
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100287 * writing FW_CFG_INVALID will cause read operation to resume at last
288 * offset, otherwise read will start at offset 0
Miao Yan4fcd7f22016-05-22 19:37:14 -0700289 */
290 if (entry != FW_CFG_INVALID)
291 dma.control |= cpu_to_be32(FW_CFG_DMA_SELECT | (entry << 16));
292
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100293 debug("%s: entry 0x%x, size %u address %p, control 0x%x\n", __func__,
Miao Yan8a153832016-05-22 19:37:15 -0700294 entry, size, address, be32_to_cpu(dma.control));
Miao Yan4fcd7f22016-05-22 19:37:14 -0700295
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100296 barrier();
Miao Yan4fcd7f22016-05-22 19:37:14 -0700297
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100298 ops->read_entry_dma(qdev->dev, &dma);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700299}
300
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100301void qfw_read_entry(struct udevice *dev, u16 entry, u32 size, void *address)
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);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700304
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100305 if (qdev->dma_present)
306 qfw_read_entry_dma(qdev, entry, size, address);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700307 else
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100308 qfw_read_entry_io(qdev, entry, size, address);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700309}
310
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100311int qfw_register(struct udevice *dev)
Miao Yan4fcd7f22016-05-22 19:37:14 -0700312{
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100313 struct qfw_dev *qdev = dev_get_uclass_priv(dev);
314 u32 qemu, dma_enabled;
Miao Yan4fcd7f22016-05-22 19:37:14 -0700315
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100316 qdev->dev = dev;
317 INIT_LIST_HEAD(&qdev->fw_list);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700318
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100319 qfw_read_entry_io(qdev, FW_CFG_SIGNATURE, 4, &qemu);
320 if (be32_to_cpu(qemu) != QEMU_FW_CFG_SIGNATURE)
321 return -ENODEV;
Miao Yan4fcd7f22016-05-22 19:37:14 -0700322
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100323 qfw_read_entry_io(qdev, FW_CFG_ID, 1, &dma_enabled);
324 if (dma_enabled & FW_CFG_DMA_ENABLED)
325 qdev->dma_present = true;
Miao Yan4fcd7f22016-05-22 19:37:14 -0700326
327 return 0;
Miao Yan4fcd7f22016-05-22 19:37:14 -0700328}
329
Simon Glass77ca5ff2023-01-28 15:00:24 -0700330static int qfw_post_bind(struct udevice *dev)
331{
332 int ret;
333
334 ret = bootdev_setup_for_dev(dev, "qfw_bootdev");
335 if (ret)
336 return log_msg_ret("dev", ret);
337
338 return 0;
339}
340
341static int qfw_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
342 struct bootflow *bflow)
343{
344 const struct udevice *media = dev_get_parent(dev);
345 int ret;
346
347 if (!CONFIG_IS_ENABLED(BOOTSTD))
348 return -ENOSYS;
349
350 log_debug("media=%s\n", media->name);
351 ret = bootmeth_check(bflow->method, iter);
352 if (ret)
353 return log_msg_ret("check", ret);
354
355 log_debug("iter->part=%d\n", iter->part);
356
357 /* We only support the whole device, not partitions */
358 if (iter->part)
359 return log_msg_ret("max", -ESHUTDOWN);
360
361 log_debug("reading bootflow with method: %s\n", bflow->method->name);
362 ret = bootmeth_read_bootflow(bflow->method, bflow);
363 if (ret)
364 return log_msg_ret("method", ret);
365
366 return 0;
367}
368
369static int qfw_bootdev_bind(struct udevice *dev)
370{
371 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
372
373 ucp->prio = BOOTDEVP_4_SCAN_FAST;
374
375 return 0;
376}
377
378static int qfw_bootdev_hunt(struct bootdev_hunter *info, bool show)
379{
380 int ret;
381
382 ret = uclass_probe_all(UCLASS_QFW);
383 if (ret && ret != -ENOENT)
384 return log_msg_ret("vir", ret);
385
386 return 0;
387}
388
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100389UCLASS_DRIVER(qfw) = {
390 .id = UCLASS_QFW,
391 .name = "qfw",
Simon Glass77ca5ff2023-01-28 15:00:24 -0700392 .post_bind = qfw_post_bind,
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100393 .per_device_auto = sizeof(struct qfw_dev),
394};
Simon Glass77ca5ff2023-01-28 15:00:24 -0700395
396struct bootdev_ops qfw_bootdev_ops = {
397 .get_bootflow = qfw_get_bootflow,
398};
399
400static const struct udevice_id qfw_bootdev_ids[] = {
401 { .compatible = "u-boot,bootdev-qfw" },
402 { }
403};
404
405U_BOOT_DRIVER(qfw_bootdev) = {
406 .name = "qfw_bootdev",
407 .id = UCLASS_BOOTDEV,
408 .ops = &qfw_bootdev_ops,
409 .bind = qfw_bootdev_bind,
410 .of_match = qfw_bootdev_ids,
411};
412
413BOOTDEV_HUNTER(qfw_bootdev_hunter) = {
414 .prio = BOOTDEVP_4_SCAN_FAST,
415 .uclass = UCLASS_QFW,
416 .hunt = qfw_bootdev_hunt,
417 .drv = DM_DRIVER_REF(qfw_bootdev),
418};