blob: 677841aac5ef5f75fed7d12c60ea8c17f062fc5f [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>
10#include <command.h>
11#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Miao Yan4fcd7f22016-05-22 19:37:14 -070013#include <malloc.h>
Miao Yan92106272016-05-22 19:37:17 -070014#include <qfw.h>
Asherah Connor4ffa95d2021-03-19 18:21:40 +110015#include <dm.h>
16#include <misc.h>
Simon Glassf3425cb2021-12-01 09:02:40 -070017#include <tables_csum.h>
Miao Yan12970d22016-05-22 19:37:20 -070018#ifdef CONFIG_GENERATE_ACPI_TABLE
19#include <asm/tables.h>
20#endif
Miao Yan4fcd7f22016-05-22 19:37:14 -070021
Simon Glass16170882021-12-01 09:02:49 -070022#if defined(CONFIG_GENERATE_ACPI_TABLE) && !defined(CONFIG_SANDBOX)
Miao Yan12970d22016-05-22 19:37:20 -070023/*
24 * This function allocates memory for ACPI tables
25 *
26 * @entry : BIOS linker command entry which tells where to allocate memory
27 * (either high memory or low memory)
28 * @addr : The address that should be used for low memory allcation. If the
29 * memory allocation request is 'ZONE_HIGH' then this parameter will
30 * be ignored.
31 * @return: 0 on success, or negative value on failure
32 */
Asherah Connor4ffa95d2021-03-19 18:21:40 +110033static int bios_linker_allocate(struct udevice *dev,
34 struct bios_linker_entry *entry, ulong *addr)
Miao Yan12970d22016-05-22 19:37:20 -070035{
36 uint32_t size, align;
37 struct fw_file *file;
38 unsigned long aligned_addr;
39
40 align = le32_to_cpu(entry->alloc.align);
41 /* align must be power of 2 */
42 if (align & (align - 1)) {
43 printf("error: wrong alignment %u\n", align);
44 return -EINVAL;
45 }
46
Asherah Connor4ffa95d2021-03-19 18:21:40 +110047 file = qfw_find_file(dev, entry->alloc.file);
Miao Yan12970d22016-05-22 19:37:20 -070048 if (!file) {
49 printf("error: can't find file %s\n", entry->alloc.file);
50 return -ENOENT;
51 }
52
53 size = be32_to_cpu(file->cfg.size);
54
55 /*
56 * ZONE_HIGH means we need to allocate from high memory, since
57 * malloc space is already at the end of RAM, so we directly use it.
58 * If allocation zone is ZONE_FSEG, then we use the 'addr' passed
59 * in which is low memory
60 */
61 if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH) {
62 aligned_addr = (unsigned long)memalign(align, size);
63 if (!aligned_addr) {
64 printf("error: allocating resource\n");
65 return -ENOMEM;
66 }
67 } else if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG) {
68 aligned_addr = ALIGN(*addr, align);
69 } else {
70 printf("error: invalid allocation zone\n");
71 return -EINVAL;
72 }
73
74 debug("bios_linker_allocate: allocate file %s, size %u, zone %d, align %u, addr 0x%lx\n",
75 file->cfg.name, size, entry->alloc.zone, align, aligned_addr);
76
Asherah Connor4ffa95d2021-03-19 18:21:40 +110077 qfw_read_entry(dev, be16_to_cpu(file->cfg.select), size,
78 (void *)aligned_addr);
Miao Yan12970d22016-05-22 19:37:20 -070079 file->addr = aligned_addr;
80
81 /* adjust address for low memory allocation */
82 if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG)
83 *addr = (aligned_addr + size);
84
85 return 0;
86}
87
88/*
89 * This function patches ACPI tables previously loaded
90 * by bios_linker_allocate()
91 *
92 * @entry : BIOS linker command entry which tells how to patch
93 * ACPI tables
94 * @return: 0 on success, or negative value on failure
95 */
Asherah Connor4ffa95d2021-03-19 18:21:40 +110096static int bios_linker_add_pointer(struct udevice *dev,
97 struct bios_linker_entry *entry)
Miao Yan12970d22016-05-22 19:37:20 -070098{
99 struct fw_file *dest, *src;
100 uint32_t offset = le32_to_cpu(entry->pointer.offset);
101 uint64_t pointer = 0;
102
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100103 dest = qfw_find_file(dev, entry->pointer.dest_file);
Miao Yan12970d22016-05-22 19:37:20 -0700104 if (!dest || !dest->addr)
105 return -ENOENT;
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100106 src = qfw_find_file(dev, entry->pointer.src_file);
Miao Yan12970d22016-05-22 19:37:20 -0700107 if (!src || !src->addr)
108 return -ENOENT;
109
110 debug("bios_linker_add_pointer: dest->addr 0x%lx, src->addr 0x%lx, offset 0x%x size %u, 0x%llx\n",
111 dest->addr, src->addr, offset, entry->pointer.size, pointer);
112
113 memcpy(&pointer, (char *)dest->addr + offset, entry->pointer.size);
114 pointer = le64_to_cpu(pointer);
115 pointer += (unsigned long)src->addr;
116 pointer = cpu_to_le64(pointer);
117 memcpy((char *)dest->addr + offset, &pointer, entry->pointer.size);
118
119 return 0;
120}
121
122/*
123 * This function updates checksum fields of ACPI tables previously loaded
124 * by bios_linker_allocate()
125 *
126 * @entry : BIOS linker command entry which tells where to update ACPI table
127 * checksums
128 * @return: 0 on success, or negative value on failure
129 */
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100130static int bios_linker_add_checksum(struct udevice *dev,
131 struct bios_linker_entry *entry)
Miao Yan12970d22016-05-22 19:37:20 -0700132{
133 struct fw_file *file;
134 uint8_t *data, cksum = 0;
135 uint8_t *cksum_start;
136
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100137 file = qfw_find_file(dev, entry->cksum.file);
Miao Yan12970d22016-05-22 19:37:20 -0700138 if (!file || !file->addr)
139 return -ENOENT;
140
141 data = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.offset));
142 cksum_start = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.start));
143 cksum = table_compute_checksum(cksum_start,
144 le32_to_cpu(entry->cksum.length));
145 *data = cksum;
146
147 return 0;
148}
149
150/* This function loads and patches ACPI tables provided by QEMU */
Simon Glassca37a392017-01-16 07:03:35 -0700151ulong write_acpi_tables(ulong addr)
Miao Yan12970d22016-05-22 19:37:20 -0700152{
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100153 int i, ret;
Miao Yan12970d22016-05-22 19:37:20 -0700154 struct fw_file *file;
155 struct bios_linker_entry *table_loader;
156 struct bios_linker_entry *entry;
157 uint32_t size;
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100158 struct udevice *dev;
159
160 ret = qfw_get_dev(&dev);
161 if (ret) {
162 printf("error: no qfw\n");
163 return addr;
164 }
Miao Yan12970d22016-05-22 19:37:20 -0700165
166 /* make sure fw_list is loaded */
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100167 ret = qfw_read_firmware_list(dev);
Miao Yan12970d22016-05-22 19:37:20 -0700168 if (ret) {
169 printf("error: can't read firmware file list\n");
170 return addr;
171 }
172
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100173 file = qfw_find_file(dev, "etc/table-loader");
Miao Yan12970d22016-05-22 19:37:20 -0700174 if (!file) {
175 printf("error: can't find etc/table-loader\n");
176 return addr;
177 }
178
179 size = be32_to_cpu(file->cfg.size);
180 if ((size % sizeof(*entry)) != 0) {
181 printf("error: table-loader maybe corrupted\n");
182 return addr;
183 }
184
185 table_loader = malloc(size);
186 if (!table_loader) {
187 printf("error: no memory for table-loader\n");
188 return addr;
189 }
190
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100191 qfw_read_entry(dev, be16_to_cpu(file->cfg.select), size, table_loader);
Miao Yan12970d22016-05-22 19:37:20 -0700192
193 for (i = 0; i < (size / sizeof(*entry)); i++) {
194 entry = table_loader + i;
195 switch (le32_to_cpu(entry->command)) {
196 case BIOS_LINKER_LOADER_COMMAND_ALLOCATE:
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100197 ret = bios_linker_allocate(dev, entry, &addr);
Miao Yan12970d22016-05-22 19:37:20 -0700198 if (ret)
199 goto out;
200 break;
201 case BIOS_LINKER_LOADER_COMMAND_ADD_POINTER:
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100202 ret = bios_linker_add_pointer(dev, entry);
Miao Yan12970d22016-05-22 19:37:20 -0700203 if (ret)
204 goto out;
205 break;
206 case BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM:
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100207 ret = bios_linker_add_checksum(dev, entry);
Miao Yan12970d22016-05-22 19:37:20 -0700208 if (ret)
209 goto out;
210 break;
211 default:
212 break;
213 }
214 }
215
216out:
217 if (ret) {
218 struct fw_cfg_file_iter iter;
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100219 for (file = qfw_file_iter_init(dev, &iter);
220 !qfw_file_iter_end(&iter);
221 file = qfw_file_iter_next(&iter)) {
Miao Yan12970d22016-05-22 19:37:20 -0700222 if (file->addr) {
223 free((void *)file->addr);
224 file->addr = 0;
225 }
226 }
227 }
228
229 free(table_loader);
230 return addr;
231}
Bin Meng22069e22018-01-30 05:01:17 -0800232
233ulong acpi_get_rsdp_addr(void)
234{
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100235 int ret;
Bin Meng22069e22018-01-30 05:01:17 -0800236 struct fw_file *file;
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100237 struct udevice *dev;
Bin Meng22069e22018-01-30 05:01:17 -0800238
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100239 ret = qfw_get_dev(&dev);
240 if (ret) {
241 printf("error: no qfw\n");
242 return 0;
243 }
244
245 file = qfw_find_file(dev, "etc/acpi/rsdp");
Bin Meng22069e22018-01-30 05:01:17 -0800246 return file->addr;
247}
Miao Yan12970d22016-05-22 19:37:20 -0700248#endif
249
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100250static void qfw_read_entry_io(struct qfw_dev *qdev, u16 entry, u32 size,
251 void *address)
Miao Yan4fcd7f22016-05-22 19:37:14 -0700252{
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100253 struct dm_qfw_ops *ops = dm_qfw_get_ops(qdev->dev);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700254
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100255 debug("%s: entry 0x%x, size %u address %p\n", __func__, entry, size,
256 address);
257
258 ops->read_entry_io(qdev->dev, entry, size, address);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700259}
260
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100261static void qfw_read_entry_dma(struct qfw_dev *qdev, u16 entry, u32 size,
262 void *address)
Miao Yan4fcd7f22016-05-22 19:37:14 -0700263{
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100264 struct dm_qfw_ops *ops = dm_qfw_get_ops(qdev->dev);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700265
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100266 struct qfw_dma dma = {
267 .length = cpu_to_be32(size),
268 .address = cpu_to_be64((uintptr_t)address),
269 .control = cpu_to_be32(FW_CFG_DMA_READ),
270 };
Miao Yan4fcd7f22016-05-22 19:37:14 -0700271
272 /*
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100273 * writing FW_CFG_INVALID will cause read operation to resume at last
274 * offset, otherwise read will start at offset 0
Miao Yan4fcd7f22016-05-22 19:37:14 -0700275 */
276 if (entry != FW_CFG_INVALID)
277 dma.control |= cpu_to_be32(FW_CFG_DMA_SELECT | (entry << 16));
278
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100279 debug("%s: entry 0x%x, size %u address %p, control 0x%x\n", __func__,
Miao Yan8a153832016-05-22 19:37:15 -0700280 entry, size, address, be32_to_cpu(dma.control));
Miao Yan4fcd7f22016-05-22 19:37:14 -0700281
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100282 barrier();
Miao Yan4fcd7f22016-05-22 19:37:14 -0700283
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100284 ops->read_entry_dma(qdev->dev, &dma);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700285}
286
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100287void qfw_read_entry(struct udevice *dev, u16 entry, u32 size, void *address)
Miao Yan4fcd7f22016-05-22 19:37:14 -0700288{
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100289 struct qfw_dev *qdev = dev_get_uclass_priv(dev);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700290
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100291 if (qdev->dma_present)
292 qfw_read_entry_dma(qdev, entry, size, address);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700293 else
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100294 qfw_read_entry_io(qdev, entry, size, address);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700295}
296
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100297int qfw_register(struct udevice *dev)
Miao Yan4fcd7f22016-05-22 19:37:14 -0700298{
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100299 struct qfw_dev *qdev = dev_get_uclass_priv(dev);
300 u32 qemu, dma_enabled;
Miao Yan4fcd7f22016-05-22 19:37:14 -0700301
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100302 qdev->dev = dev;
303 INIT_LIST_HEAD(&qdev->fw_list);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700304
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100305 qfw_read_entry_io(qdev, FW_CFG_SIGNATURE, 4, &qemu);
306 if (be32_to_cpu(qemu) != QEMU_FW_CFG_SIGNATURE)
307 return -ENODEV;
Miao Yan4fcd7f22016-05-22 19:37:14 -0700308
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100309 qfw_read_entry_io(qdev, FW_CFG_ID, 1, &dma_enabled);
310 if (dma_enabled & FW_CFG_DMA_ENABLED)
311 qdev->dma_present = true;
Miao Yan4fcd7f22016-05-22 19:37:14 -0700312
313 return 0;
Miao Yan4fcd7f22016-05-22 19:37:14 -0700314}
315
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100316UCLASS_DRIVER(qfw) = {
317 .id = UCLASS_QFW,
318 .name = "qfw",
319 .per_device_auto = sizeof(struct qfw_dev),
320};