1 // SPDX-License-Identifier: GPL-2.0+
7 #define LOG_CATEGORY UCLASS_QFW
9 #include <acpi/acpi_table.h>
14 #include <tables_csum.h>
16 #include <linux/sizes.h>
17 #include <asm/byteorder.h>
18 #include <asm/global_data.h>
20 DECLARE_GLOBAL_DATA_PTR;
23 * This function allocates memory for ACPI tables
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
30 * @return: 0 on success, or negative value on failure
32 static int bios_linker_allocate(struct udevice *dev,
33 struct bios_linker_entry *entry, ulong *addr)
37 unsigned long aligned_addr;
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);
46 file = qfw_find_file(dev, entry->alloc.file);
48 printf("error: can't find file %s\n", entry->alloc.file);
52 size = be32_to_cpu(file->cfg.size);
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
60 if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH) {
61 aligned_addr = (unsigned long)memalign(align, size);
63 printf("error: allocating resource\n");
66 if (aligned_addr < gd->arch.table_start_high)
67 gd->arch.table_start_high = aligned_addr;
68 if (aligned_addr + size > gd->arch.table_end_high)
69 gd->arch.table_end_high = aligned_addr + size;
71 } else if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG) {
72 aligned_addr = ALIGN(*addr, align);
74 printf("error: invalid allocation zone\n");
78 debug("bios_linker_allocate: allocate file %s, size %u, zone %d, align %u, addr 0x%lx\n",
79 file->cfg.name, size, entry->alloc.zone, align, aligned_addr);
81 qfw_read_entry(dev, be16_to_cpu(file->cfg.select), size,
82 (void *)aligned_addr);
83 file->addr = aligned_addr;
85 /* adjust address for low memory allocation */
86 if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG)
87 *addr = (aligned_addr + size);
93 * This function patches ACPI tables previously loaded
94 * by bios_linker_allocate()
96 * @entry : BIOS linker command entry which tells how to patch
98 * @return: 0 on success, or negative value on failure
100 static int bios_linker_add_pointer(struct udevice *dev,
101 struct bios_linker_entry *entry)
103 struct fw_file *dest, *src;
104 uint32_t offset = le32_to_cpu(entry->pointer.offset);
105 uint64_t pointer = 0;
107 dest = qfw_find_file(dev, entry->pointer.dest_file);
108 if (!dest || !dest->addr)
110 src = qfw_find_file(dev, entry->pointer.src_file);
111 if (!src || !src->addr)
114 debug("bios_linker_add_pointer: dest->addr 0x%lx, src->addr 0x%lx, offset 0x%x size %u, 0x%llx\n",
115 dest->addr, src->addr, offset, entry->pointer.size, pointer);
117 memcpy(&pointer, (char *)dest->addr + offset, entry->pointer.size);
118 pointer = le64_to_cpu(pointer);
119 pointer += (unsigned long)src->addr;
120 pointer = cpu_to_le64(pointer);
121 memcpy((char *)dest->addr + offset, &pointer, entry->pointer.size);
127 * This function updates checksum fields of ACPI tables previously loaded
128 * by bios_linker_allocate()
130 * @entry : BIOS linker command entry which tells where to update ACPI table
132 * @return: 0 on success, or negative value on failure
134 static int bios_linker_add_checksum(struct udevice *dev,
135 struct bios_linker_entry *entry)
137 struct fw_file *file;
138 uint8_t *data, cksum = 0;
139 uint8_t *cksum_start;
141 file = qfw_find_file(dev, entry->cksum.file);
142 if (!file || !file->addr)
145 data = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.offset));
146 cksum_start = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.start));
147 cksum = table_compute_checksum(cksum_start,
148 le32_to_cpu(entry->cksum.length));
154 /* This function loads and patches ACPI tables provided by QEMU */
155 ulong write_acpi_tables(ulong addr)
158 struct fw_file *file;
159 struct bios_linker_entry *table_loader;
160 struct bios_linker_entry *entry;
164 ret = qfw_get_dev(&dev);
166 printf("error: no qfw\n");
170 /* make sure fw_list is loaded */
171 ret = qfw_read_firmware_list(dev);
173 printf("error: can't read firmware file list\n");
177 file = qfw_find_file(dev, "etc/table-loader");
179 printf("error: can't find etc/table-loader\n");
183 size = be32_to_cpu(file->cfg.size);
184 if ((size % sizeof(*entry)) != 0) {
185 printf("error: table-loader maybe corrupted\n");
189 table_loader = malloc(size);
191 printf("error: no memory for table-loader\n");
195 /* QFW always puts tables at high addresses */
196 gd->arch.table_start_high = (ulong)table_loader;
197 gd->arch.table_end_high = (ulong)table_loader;
199 qfw_read_entry(dev, be16_to_cpu(file->cfg.select), size, table_loader);
201 for (i = 0; i < (size / sizeof(*entry)); i++) {
202 entry = table_loader + i;
203 switch (le32_to_cpu(entry->command)) {
204 case BIOS_LINKER_LOADER_COMMAND_ALLOCATE:
205 ret = bios_linker_allocate(dev, entry, &addr);
209 case BIOS_LINKER_LOADER_COMMAND_ADD_POINTER:
210 ret = bios_linker_add_pointer(dev, entry);
214 case BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM:
215 ret = bios_linker_add_checksum(dev, entry);
226 struct fw_cfg_file_iter iter;
227 for (file = qfw_file_iter_init(dev, &iter);
228 !qfw_file_iter_end(&iter);
229 file = qfw_file_iter_next(&iter)) {
231 free((void *)file->addr);
239 gd_set_acpi_start(acpi_get_rsdp_addr());
244 ulong acpi_get_rsdp_addr(void)
247 struct fw_file *file;
250 ret = qfw_get_dev(&dev);
252 printf("error: no qfw\n");
256 file = qfw_find_file(dev, "etc/acpi/rsdp");
261 static int evt_write_acpi_tables(void)
266 /* Reserve 64K for ACPI tables, aligned to a 4K boundary */
267 ptr = memalign(SZ_4K, SZ_64K);
270 addr = map_to_sysmem(ptr);
272 /* Generate ACPI tables */
273 end = write_acpi_tables(addr);
274 gd->arch.table_start = addr;
275 gd->arch.table_end = addr;
280 EVENT_SPY_SIMPLE(EVT_LAST_STAGE_INIT, evt_write_acpi_tables);