blob: 1d54b7542b8a850970bf7bc32dfdee54df569dad [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Miao Yanfcf5c042016-05-22 19:37:14 -07002/*
3 * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com>
Asherah Connor5b0b43e2021-03-19 18:21:40 +11004 * (C) Copyright 2021 Asherah Connor <ashe@kivikakk.ee>
Miao Yanfcf5c042016-05-22 19:37:14 -07005 */
6
Asherah Connor5b0b43e2021-03-19 18:21:40 +11007#define LOG_CATEGORY UCLASS_QFW
8
Miao Yanfcf5c042016-05-22 19:37:14 -07009#include <common.h>
10#include <command.h>
11#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Miao Yanfcf5c042016-05-22 19:37:14 -070013#include <malloc.h>
Miao Yan18686592016-05-22 19:37:17 -070014#include <qfw.h>
Asherah Connor5b0b43e2021-03-19 18:21:40 +110015#include <dm.h>
16#include <misc.h>
Simon Glass0679cca2021-12-01 09:02:40 -070017#include <tables_csum.h>
Miao Yanfcf5c042016-05-22 19:37:14 -070018
Simon Glass31c27eb2021-12-01 09:02:49 -070019#if defined(CONFIG_GENERATE_ACPI_TABLE) && !defined(CONFIG_SANDBOX)
Miao Yaneece4932016-05-22 19:37:20 -070020/*
21 * This function allocates memory for ACPI tables
22 *
23 * @entry : BIOS linker command entry which tells where to allocate memory
24 * (either high memory or low memory)
25 * @addr : The address that should be used for low memory allcation. If the
26 * memory allocation request is 'ZONE_HIGH' then this parameter will
27 * be ignored.
28 * @return: 0 on success, or negative value on failure
29 */
Asherah Connor5b0b43e2021-03-19 18:21:40 +110030static int bios_linker_allocate(struct udevice *dev,
31 struct bios_linker_entry *entry, ulong *addr)
Miao Yaneece4932016-05-22 19:37:20 -070032{
33 uint32_t size, align;
34 struct fw_file *file;
35 unsigned long aligned_addr;
36
37 align = le32_to_cpu(entry->alloc.align);
38 /* align must be power of 2 */
39 if (align & (align - 1)) {
40 printf("error: wrong alignment %u\n", align);
41 return -EINVAL;
42 }
43
Asherah Connor5b0b43e2021-03-19 18:21:40 +110044 file = qfw_find_file(dev, entry->alloc.file);
Miao Yaneece4932016-05-22 19:37:20 -070045 if (!file) {
46 printf("error: can't find file %s\n", entry->alloc.file);
47 return -ENOENT;
48 }
49
50 size = be32_to_cpu(file->cfg.size);
51
52 /*
53 * ZONE_HIGH means we need to allocate from high memory, since
54 * malloc space is already at the end of RAM, so we directly use it.
55 * If allocation zone is ZONE_FSEG, then we use the 'addr' passed
56 * in which is low memory
57 */
58 if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH) {
59 aligned_addr = (unsigned long)memalign(align, size);
60 if (!aligned_addr) {
61 printf("error: allocating resource\n");
62 return -ENOMEM;
63 }
64 } else if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG) {
65 aligned_addr = ALIGN(*addr, align);
66 } else {
67 printf("error: invalid allocation zone\n");
68 return -EINVAL;
69 }
70
71 debug("bios_linker_allocate: allocate file %s, size %u, zone %d, align %u, addr 0x%lx\n",
72 file->cfg.name, size, entry->alloc.zone, align, aligned_addr);
73
Asherah Connor5b0b43e2021-03-19 18:21:40 +110074 qfw_read_entry(dev, be16_to_cpu(file->cfg.select), size,
75 (void *)aligned_addr);
Miao Yaneece4932016-05-22 19:37:20 -070076 file->addr = aligned_addr;
77
78 /* adjust address for low memory allocation */
79 if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG)
80 *addr = (aligned_addr + size);
81
82 return 0;
83}
84
85/*
86 * This function patches ACPI tables previously loaded
87 * by bios_linker_allocate()
88 *
89 * @entry : BIOS linker command entry which tells how to patch
90 * ACPI tables
91 * @return: 0 on success, or negative value on failure
92 */
Asherah Connor5b0b43e2021-03-19 18:21:40 +110093static int bios_linker_add_pointer(struct udevice *dev,
94 struct bios_linker_entry *entry)
Miao Yaneece4932016-05-22 19:37:20 -070095{
96 struct fw_file *dest, *src;
97 uint32_t offset = le32_to_cpu(entry->pointer.offset);
98 uint64_t pointer = 0;
99
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100100 dest = qfw_find_file(dev, entry->pointer.dest_file);
Miao Yaneece4932016-05-22 19:37:20 -0700101 if (!dest || !dest->addr)
102 return -ENOENT;
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100103 src = qfw_find_file(dev, entry->pointer.src_file);
Miao Yaneece4932016-05-22 19:37:20 -0700104 if (!src || !src->addr)
105 return -ENOENT;
106
107 debug("bios_linker_add_pointer: dest->addr 0x%lx, src->addr 0x%lx, offset 0x%x size %u, 0x%llx\n",
108 dest->addr, src->addr, offset, entry->pointer.size, pointer);
109
110 memcpy(&pointer, (char *)dest->addr + offset, entry->pointer.size);
111 pointer = le64_to_cpu(pointer);
112 pointer += (unsigned long)src->addr;
113 pointer = cpu_to_le64(pointer);
114 memcpy((char *)dest->addr + offset, &pointer, entry->pointer.size);
115
116 return 0;
117}
118
119/*
120 * This function updates checksum fields of ACPI tables previously loaded
121 * by bios_linker_allocate()
122 *
123 * @entry : BIOS linker command entry which tells where to update ACPI table
124 * checksums
125 * @return: 0 on success, or negative value on failure
126 */
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100127static int bios_linker_add_checksum(struct udevice *dev,
128 struct bios_linker_entry *entry)
Miao Yaneece4932016-05-22 19:37:20 -0700129{
130 struct fw_file *file;
131 uint8_t *data, cksum = 0;
132 uint8_t *cksum_start;
133
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100134 file = qfw_find_file(dev, entry->cksum.file);
Miao Yaneece4932016-05-22 19:37:20 -0700135 if (!file || !file->addr)
136 return -ENOENT;
137
138 data = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.offset));
139 cksum_start = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.start));
140 cksum = table_compute_checksum(cksum_start,
141 le32_to_cpu(entry->cksum.length));
142 *data = cksum;
143
144 return 0;
145}
146
147/* This function loads and patches ACPI tables provided by QEMU */
Simon Glass42fd8c12017-01-16 07:03:35 -0700148ulong write_acpi_tables(ulong addr)
Miao Yaneece4932016-05-22 19:37:20 -0700149{
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100150 int i, ret;
Miao Yaneece4932016-05-22 19:37:20 -0700151 struct fw_file *file;
152 struct bios_linker_entry *table_loader;
153 struct bios_linker_entry *entry;
154 uint32_t size;
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100155 struct udevice *dev;
156
157 ret = qfw_get_dev(&dev);
158 if (ret) {
159 printf("error: no qfw\n");
160 return addr;
161 }
Miao Yaneece4932016-05-22 19:37:20 -0700162
163 /* make sure fw_list is loaded */
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100164 ret = qfw_read_firmware_list(dev);
Miao Yaneece4932016-05-22 19:37:20 -0700165 if (ret) {
166 printf("error: can't read firmware file list\n");
167 return addr;
168 }
169
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100170 file = qfw_find_file(dev, "etc/table-loader");
Miao Yaneece4932016-05-22 19:37:20 -0700171 if (!file) {
172 printf("error: can't find etc/table-loader\n");
173 return addr;
174 }
175
176 size = be32_to_cpu(file->cfg.size);
177 if ((size % sizeof(*entry)) != 0) {
178 printf("error: table-loader maybe corrupted\n");
179 return addr;
180 }
181
182 table_loader = malloc(size);
183 if (!table_loader) {
184 printf("error: no memory for table-loader\n");
185 return addr;
186 }
187
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100188 qfw_read_entry(dev, be16_to_cpu(file->cfg.select), size, table_loader);
Miao Yaneece4932016-05-22 19:37:20 -0700189
190 for (i = 0; i < (size / sizeof(*entry)); i++) {
191 entry = table_loader + i;
192 switch (le32_to_cpu(entry->command)) {
193 case BIOS_LINKER_LOADER_COMMAND_ALLOCATE:
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100194 ret = bios_linker_allocate(dev, entry, &addr);
Miao Yaneece4932016-05-22 19:37:20 -0700195 if (ret)
196 goto out;
197 break;
198 case BIOS_LINKER_LOADER_COMMAND_ADD_POINTER:
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100199 ret = bios_linker_add_pointer(dev, entry);
Miao Yaneece4932016-05-22 19:37:20 -0700200 if (ret)
201 goto out;
202 break;
203 case BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM:
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100204 ret = bios_linker_add_checksum(dev, entry);
Miao Yaneece4932016-05-22 19:37:20 -0700205 if (ret)
206 goto out;
207 break;
208 default:
209 break;
210 }
211 }
212
213out:
214 if (ret) {
215 struct fw_cfg_file_iter iter;
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100216 for (file = qfw_file_iter_init(dev, &iter);
217 !qfw_file_iter_end(&iter);
218 file = qfw_file_iter_next(&iter)) {
Miao Yaneece4932016-05-22 19:37:20 -0700219 if (file->addr) {
220 free((void *)file->addr);
221 file->addr = 0;
222 }
223 }
224 }
225
226 free(table_loader);
227 return addr;
228}
Bin Meng2d1c6612018-01-30 05:01:17 -0800229
230ulong acpi_get_rsdp_addr(void)
231{
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100232 int ret;
Bin Meng2d1c6612018-01-30 05:01:17 -0800233 struct fw_file *file;
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100234 struct udevice *dev;
Bin Meng2d1c6612018-01-30 05:01:17 -0800235
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100236 ret = qfw_get_dev(&dev);
237 if (ret) {
238 printf("error: no qfw\n");
239 return 0;
240 }
241
242 file = qfw_find_file(dev, "etc/acpi/rsdp");
Bin Meng2d1c6612018-01-30 05:01:17 -0800243 return file->addr;
244}
Miao Yaneece4932016-05-22 19:37:20 -0700245#endif
246
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100247static void qfw_read_entry_io(struct qfw_dev *qdev, u16 entry, u32 size,
248 void *address)
Miao Yanfcf5c042016-05-22 19:37:14 -0700249{
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100250 struct dm_qfw_ops *ops = dm_qfw_get_ops(qdev->dev);
Miao Yanfcf5c042016-05-22 19:37:14 -0700251
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100252 debug("%s: entry 0x%x, size %u address %p\n", __func__, entry, size,
253 address);
254
255 ops->read_entry_io(qdev->dev, entry, size, address);
Miao Yanfcf5c042016-05-22 19:37:14 -0700256}
257
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100258static void qfw_read_entry_dma(struct qfw_dev *qdev, u16 entry, u32 size,
259 void *address)
Miao Yanfcf5c042016-05-22 19:37:14 -0700260{
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100261 struct dm_qfw_ops *ops = dm_qfw_get_ops(qdev->dev);
Miao Yanfcf5c042016-05-22 19:37:14 -0700262
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100263 struct qfw_dma dma = {
264 .length = cpu_to_be32(size),
265 .address = cpu_to_be64((uintptr_t)address),
266 .control = cpu_to_be32(FW_CFG_DMA_READ),
267 };
Miao Yanfcf5c042016-05-22 19:37:14 -0700268
269 /*
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100270 * writing FW_CFG_INVALID will cause read operation to resume at last
271 * offset, otherwise read will start at offset 0
Miao Yanfcf5c042016-05-22 19:37:14 -0700272 */
273 if (entry != FW_CFG_INVALID)
274 dma.control |= cpu_to_be32(FW_CFG_DMA_SELECT | (entry << 16));
275
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100276 debug("%s: entry 0x%x, size %u address %p, control 0x%x\n", __func__,
Miao Yan2e82e742016-05-22 19:37:15 -0700277 entry, size, address, be32_to_cpu(dma.control));
Miao Yanfcf5c042016-05-22 19:37:14 -0700278
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100279 barrier();
280
281 ops->read_entry_dma(qdev->dev, &dma);
Miao Yanfcf5c042016-05-22 19:37:14 -0700282}
283
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100284void qfw_read_entry(struct udevice *dev, u16 entry, u32 size, void *address)
Miao Yanfcf5c042016-05-22 19:37:14 -0700285{
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100286 struct qfw_dev *qdev = dev_get_uclass_priv(dev);
Miao Yanfcf5c042016-05-22 19:37:14 -0700287
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100288 if (qdev->dma_present)
289 qfw_read_entry_dma(qdev, entry, size, address);
Miao Yanfcf5c042016-05-22 19:37:14 -0700290 else
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100291 qfw_read_entry_io(qdev, entry, size, address);
Miao Yanfcf5c042016-05-22 19:37:14 -0700292}
293
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100294int qfw_register(struct udevice *dev)
Miao Yanfcf5c042016-05-22 19:37:14 -0700295{
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100296 struct qfw_dev *qdev = dev_get_uclass_priv(dev);
297 u32 qemu, dma_enabled;
Miao Yanfcf5c042016-05-22 19:37:14 -0700298
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100299 qdev->dev = dev;
300 INIT_LIST_HEAD(&qdev->fw_list);
301
302 qfw_read_entry_io(qdev, FW_CFG_SIGNATURE, 4, &qemu);
303 if (be32_to_cpu(qemu) != QEMU_FW_CFG_SIGNATURE)
Miao Yanfcf5c042016-05-22 19:37:14 -0700304 return -ENODEV;
305
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100306 qfw_read_entry_io(qdev, FW_CFG_ID, 1, &dma_enabled);
307 if (dma_enabled & FW_CFG_DMA_ENABLED)
308 qdev->dma_present = true;
Miao Yanfcf5c042016-05-22 19:37:14 -0700309
310 return 0;
Miao Yanfcf5c042016-05-22 19:37:14 -0700311}
312
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100313UCLASS_DRIVER(qfw) = {
314 .id = UCLASS_QFW,
315 .name = "qfw",
316 .per_device_auto = sizeof(struct qfw_dev),
317};