blob: ea00be88a8d5151926ed2058a818968a067dba4a [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>
Miao Yaneece4932016-05-22 19:37:20 -070017#ifdef CONFIG_GENERATE_ACPI_TABLE
18#include <asm/tables.h>
19#endif
Miao Yanfcf5c042016-05-22 19:37:14 -070020
Miao Yaneece4932016-05-22 19:37:20 -070021#ifdef CONFIG_GENERATE_ACPI_TABLE
22/*
23 * This function allocates memory for ACPI tables
24 *
25 * @entry : BIOS linker command entry which tells where to allocate memory
26 * (either high memory or low memory)
27 * @addr : The address that should be used for low memory allcation. If the
28 * memory allocation request is 'ZONE_HIGH' then this parameter will
29 * be ignored.
30 * @return: 0 on success, or negative value on failure
31 */
Asherah Connor5b0b43e2021-03-19 18:21:40 +110032static int bios_linker_allocate(struct udevice *dev,
33 struct bios_linker_entry *entry, ulong *addr)
Miao Yaneece4932016-05-22 19:37:20 -070034{
35 uint32_t size, align;
36 struct fw_file *file;
37 unsigned long aligned_addr;
38
39 align = le32_to_cpu(entry->alloc.align);
40 /* align must be power of 2 */
41 if (align & (align - 1)) {
42 printf("error: wrong alignment %u\n", align);
43 return -EINVAL;
44 }
45
Asherah Connor5b0b43e2021-03-19 18:21:40 +110046 file = qfw_find_file(dev, entry->alloc.file);
Miao Yaneece4932016-05-22 19:37:20 -070047 if (!file) {
48 printf("error: can't find file %s\n", entry->alloc.file);
49 return -ENOENT;
50 }
51
52 size = be32_to_cpu(file->cfg.size);
53
54 /*
55 * ZONE_HIGH means we need to allocate from high memory, since
56 * malloc space is already at the end of RAM, so we directly use it.
57 * If allocation zone is ZONE_FSEG, then we use the 'addr' passed
58 * in which is low memory
59 */
60 if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH) {
61 aligned_addr = (unsigned long)memalign(align, size);
62 if (!aligned_addr) {
63 printf("error: allocating resource\n");
64 return -ENOMEM;
65 }
66 } else if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG) {
67 aligned_addr = ALIGN(*addr, align);
68 } else {
69 printf("error: invalid allocation zone\n");
70 return -EINVAL;
71 }
72
73 debug("bios_linker_allocate: allocate file %s, size %u, zone %d, align %u, addr 0x%lx\n",
74 file->cfg.name, size, entry->alloc.zone, align, aligned_addr);
75
Asherah Connor5b0b43e2021-03-19 18:21:40 +110076 qfw_read_entry(dev, be16_to_cpu(file->cfg.select), size,
77 (void *)aligned_addr);
Miao Yaneece4932016-05-22 19:37:20 -070078 file->addr = aligned_addr;
79
80 /* adjust address for low memory allocation */
81 if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG)
82 *addr = (aligned_addr + size);
83
84 return 0;
85}
86
87/*
88 * This function patches ACPI tables previously loaded
89 * by bios_linker_allocate()
90 *
91 * @entry : BIOS linker command entry which tells how to patch
92 * ACPI tables
93 * @return: 0 on success, or negative value on failure
94 */
Asherah Connor5b0b43e2021-03-19 18:21:40 +110095static int bios_linker_add_pointer(struct udevice *dev,
96 struct bios_linker_entry *entry)
Miao Yaneece4932016-05-22 19:37:20 -070097{
98 struct fw_file *dest, *src;
99 uint32_t offset = le32_to_cpu(entry->pointer.offset);
100 uint64_t pointer = 0;
101
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100102 dest = qfw_find_file(dev, entry->pointer.dest_file);
Miao Yaneece4932016-05-22 19:37:20 -0700103 if (!dest || !dest->addr)
104 return -ENOENT;
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100105 src = qfw_find_file(dev, entry->pointer.src_file);
Miao Yaneece4932016-05-22 19:37:20 -0700106 if (!src || !src->addr)
107 return -ENOENT;
108
109 debug("bios_linker_add_pointer: dest->addr 0x%lx, src->addr 0x%lx, offset 0x%x size %u, 0x%llx\n",
110 dest->addr, src->addr, offset, entry->pointer.size, pointer);
111
112 memcpy(&pointer, (char *)dest->addr + offset, entry->pointer.size);
113 pointer = le64_to_cpu(pointer);
114 pointer += (unsigned long)src->addr;
115 pointer = cpu_to_le64(pointer);
116 memcpy((char *)dest->addr + offset, &pointer, entry->pointer.size);
117
118 return 0;
119}
120
121/*
122 * This function updates checksum fields of ACPI tables previously loaded
123 * by bios_linker_allocate()
124 *
125 * @entry : BIOS linker command entry which tells where to update ACPI table
126 * checksums
127 * @return: 0 on success, or negative value on failure
128 */
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100129static int bios_linker_add_checksum(struct udevice *dev,
130 struct bios_linker_entry *entry)
Miao Yaneece4932016-05-22 19:37:20 -0700131{
132 struct fw_file *file;
133 uint8_t *data, cksum = 0;
134 uint8_t *cksum_start;
135
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100136 file = qfw_find_file(dev, entry->cksum.file);
Miao Yaneece4932016-05-22 19:37:20 -0700137 if (!file || !file->addr)
138 return -ENOENT;
139
140 data = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.offset));
141 cksum_start = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.start));
142 cksum = table_compute_checksum(cksum_start,
143 le32_to_cpu(entry->cksum.length));
144 *data = cksum;
145
146 return 0;
147}
148
149/* This function loads and patches ACPI tables provided by QEMU */
Simon Glass42fd8c12017-01-16 07:03:35 -0700150ulong write_acpi_tables(ulong addr)
Miao Yaneece4932016-05-22 19:37:20 -0700151{
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100152 int i, ret;
Miao Yaneece4932016-05-22 19:37:20 -0700153 struct fw_file *file;
154 struct bios_linker_entry *table_loader;
155 struct bios_linker_entry *entry;
156 uint32_t size;
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100157 struct udevice *dev;
158
159 ret = qfw_get_dev(&dev);
160 if (ret) {
161 printf("error: no qfw\n");
162 return addr;
163 }
Miao Yaneece4932016-05-22 19:37:20 -0700164
165 /* make sure fw_list is loaded */
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100166 ret = qfw_read_firmware_list(dev);
Miao Yaneece4932016-05-22 19:37:20 -0700167 if (ret) {
168 printf("error: can't read firmware file list\n");
169 return addr;
170 }
171
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100172 file = qfw_find_file(dev, "etc/table-loader");
Miao Yaneece4932016-05-22 19:37:20 -0700173 if (!file) {
174 printf("error: can't find etc/table-loader\n");
175 return addr;
176 }
177
178 size = be32_to_cpu(file->cfg.size);
179 if ((size % sizeof(*entry)) != 0) {
180 printf("error: table-loader maybe corrupted\n");
181 return addr;
182 }
183
184 table_loader = malloc(size);
185 if (!table_loader) {
186 printf("error: no memory for table-loader\n");
187 return addr;
188 }
189
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100190 qfw_read_entry(dev, be16_to_cpu(file->cfg.select), size, table_loader);
Miao Yaneece4932016-05-22 19:37:20 -0700191
192 for (i = 0; i < (size / sizeof(*entry)); i++) {
193 entry = table_loader + i;
194 switch (le32_to_cpu(entry->command)) {
195 case BIOS_LINKER_LOADER_COMMAND_ALLOCATE:
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100196 ret = bios_linker_allocate(dev, entry, &addr);
Miao Yaneece4932016-05-22 19:37:20 -0700197 if (ret)
198 goto out;
199 break;
200 case BIOS_LINKER_LOADER_COMMAND_ADD_POINTER:
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100201 ret = bios_linker_add_pointer(dev, entry);
Miao Yaneece4932016-05-22 19:37:20 -0700202 if (ret)
203 goto out;
204 break;
205 case BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM:
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100206 ret = bios_linker_add_checksum(dev, entry);
Miao Yaneece4932016-05-22 19:37:20 -0700207 if (ret)
208 goto out;
209 break;
210 default:
211 break;
212 }
213 }
214
215out:
216 if (ret) {
217 struct fw_cfg_file_iter iter;
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100218 for (file = qfw_file_iter_init(dev, &iter);
219 !qfw_file_iter_end(&iter);
220 file = qfw_file_iter_next(&iter)) {
Miao Yaneece4932016-05-22 19:37:20 -0700221 if (file->addr) {
222 free((void *)file->addr);
223 file->addr = 0;
224 }
225 }
226 }
227
228 free(table_loader);
229 return addr;
230}
Bin Meng2d1c6612018-01-30 05:01:17 -0800231
232ulong acpi_get_rsdp_addr(void)
233{
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100234 int ret;
Bin Meng2d1c6612018-01-30 05:01:17 -0800235 struct fw_file *file;
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100236 struct udevice *dev;
Bin Meng2d1c6612018-01-30 05:01:17 -0800237
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100238 ret = qfw_get_dev(&dev);
239 if (ret) {
240 printf("error: no qfw\n");
241 return 0;
242 }
243
244 file = qfw_find_file(dev, "etc/acpi/rsdp");
Bin Meng2d1c6612018-01-30 05:01:17 -0800245 return file->addr;
246}
Miao Yaneece4932016-05-22 19:37:20 -0700247#endif
248
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100249static void qfw_read_entry_io(struct qfw_dev *qdev, u16 entry, u32 size,
250 void *address)
Miao Yanfcf5c042016-05-22 19:37:14 -0700251{
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100252 struct dm_qfw_ops *ops = dm_qfw_get_ops(qdev->dev);
Miao Yanfcf5c042016-05-22 19:37:14 -0700253
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100254 debug("%s: entry 0x%x, size %u address %p\n", __func__, entry, size,
255 address);
256
257 ops->read_entry_io(qdev->dev, entry, size, address);
Miao Yanfcf5c042016-05-22 19:37:14 -0700258}
259
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100260static void qfw_read_entry_dma(struct qfw_dev *qdev, u16 entry, u32 size,
261 void *address)
Miao Yanfcf5c042016-05-22 19:37:14 -0700262{
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100263 struct dm_qfw_ops *ops = dm_qfw_get_ops(qdev->dev);
Miao Yanfcf5c042016-05-22 19:37:14 -0700264
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100265 struct qfw_dma dma = {
266 .length = cpu_to_be32(size),
267 .address = cpu_to_be64((uintptr_t)address),
268 .control = cpu_to_be32(FW_CFG_DMA_READ),
269 };
Miao Yanfcf5c042016-05-22 19:37:14 -0700270
271 /*
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100272 * writing FW_CFG_INVALID will cause read operation to resume at last
273 * offset, otherwise read will start at offset 0
Miao Yanfcf5c042016-05-22 19:37:14 -0700274 */
275 if (entry != FW_CFG_INVALID)
276 dma.control |= cpu_to_be32(FW_CFG_DMA_SELECT | (entry << 16));
277
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100278 debug("%s: entry 0x%x, size %u address %p, control 0x%x\n", __func__,
Miao Yan2e82e742016-05-22 19:37:15 -0700279 entry, size, address, be32_to_cpu(dma.control));
Miao Yanfcf5c042016-05-22 19:37:14 -0700280
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100281 barrier();
282
283 ops->read_entry_dma(qdev->dev, &dma);
Miao Yanfcf5c042016-05-22 19:37:14 -0700284}
285
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100286void qfw_read_entry(struct udevice *dev, u16 entry, u32 size, void *address)
Miao Yanfcf5c042016-05-22 19:37:14 -0700287{
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100288 struct qfw_dev *qdev = dev_get_uclass_priv(dev);
Miao Yanfcf5c042016-05-22 19:37:14 -0700289
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100290 if (qdev->dma_present)
291 qfw_read_entry_dma(qdev, entry, size, address);
Miao Yanfcf5c042016-05-22 19:37:14 -0700292 else
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100293 qfw_read_entry_io(qdev, entry, size, address);
Miao Yanfcf5c042016-05-22 19:37:14 -0700294}
295
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100296int qfw_register(struct udevice *dev)
Miao Yanfcf5c042016-05-22 19:37:14 -0700297{
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100298 struct qfw_dev *qdev = dev_get_uclass_priv(dev);
299 u32 qemu, dma_enabled;
Miao Yanfcf5c042016-05-22 19:37:14 -0700300
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100301 qdev->dev = dev;
302 INIT_LIST_HEAD(&qdev->fw_list);
303
304 qfw_read_entry_io(qdev, FW_CFG_SIGNATURE, 4, &qemu);
305 if (be32_to_cpu(qemu) != QEMU_FW_CFG_SIGNATURE)
Miao Yanfcf5c042016-05-22 19:37:14 -0700306 return -ENODEV;
307
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100308 qfw_read_entry_io(qdev, FW_CFG_ID, 1, &dma_enabled);
309 if (dma_enabled & FW_CFG_DMA_ENABLED)
310 qdev->dma_present = true;
Miao Yanfcf5c042016-05-22 19:37:14 -0700311
312 return 0;
Miao Yanfcf5c042016-05-22 19:37:14 -0700313}
314
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100315UCLASS_DRIVER(qfw) = {
316 .id = UCLASS_QFW,
317 .name = "qfw",
318 .per_device_auto = sizeof(struct qfw_dev),
319};