1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com>
12 #ifdef CONFIG_GENERATE_ACPI_TABLE
13 #include <asm/tables.h>
15 #include <linux/list.h>
17 static bool fwcfg_present;
18 static bool fwcfg_dma_present;
19 static struct fw_cfg_arch_ops *fwcfg_arch_ops;
21 static LIST_HEAD(fw_list);
23 #ifdef CONFIG_GENERATE_ACPI_TABLE
25 * This function allocates memory for ACPI tables
27 * @entry : BIOS linker command entry which tells where to allocate memory
28 * (either high memory or low memory)
29 * @addr : The address that should be used for low memory allcation. If the
30 * memory allocation request is 'ZONE_HIGH' then this parameter will
32 * @return: 0 on success, or negative value on failure
34 static int bios_linker_allocate(struct bios_linker_entry *entry, ulong *addr)
38 unsigned long aligned_addr;
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);
47 file = qemu_fwcfg_find_file(entry->alloc.file);
49 printf("error: can't find file %s\n", entry->alloc.file);
53 size = be32_to_cpu(file->cfg.size);
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
61 if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH) {
62 aligned_addr = (unsigned long)memalign(align, size);
64 printf("error: allocating resource\n");
67 } else if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG) {
68 aligned_addr = ALIGN(*addr, align);
70 printf("error: invalid allocation zone\n");
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);
77 qemu_fwcfg_read_entry(be16_to_cpu(file->cfg.select),
78 size, (void *)aligned_addr);
79 file->addr = aligned_addr;
81 /* adjust address for low memory allocation */
82 if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG)
83 *addr = (aligned_addr + size);
89 * This function patches ACPI tables previously loaded
90 * by bios_linker_allocate()
92 * @entry : BIOS linker command entry which tells how to patch
94 * @return: 0 on success, or negative value on failure
96 static int bios_linker_add_pointer(struct bios_linker_entry *entry)
98 struct fw_file *dest, *src;
99 uint32_t offset = le32_to_cpu(entry->pointer.offset);
100 uint64_t pointer = 0;
102 dest = qemu_fwcfg_find_file(entry->pointer.dest_file);
103 if (!dest || !dest->addr)
105 src = qemu_fwcfg_find_file(entry->pointer.src_file);
106 if (!src || !src->addr)
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);
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);
122 * This function updates checksum fields of ACPI tables previously loaded
123 * by bios_linker_allocate()
125 * @entry : BIOS linker command entry which tells where to update ACPI table
127 * @return: 0 on success, or negative value on failure
129 static int bios_linker_add_checksum(struct bios_linker_entry *entry)
131 struct fw_file *file;
132 uint8_t *data, cksum = 0;
133 uint8_t *cksum_start;
135 file = qemu_fwcfg_find_file(entry->cksum.file);
136 if (!file || !file->addr)
139 data = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.offset));
140 cksum_start = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.start));
141 cksum = table_compute_checksum(cksum_start,
142 le32_to_cpu(entry->cksum.length));
148 /* This function loads and patches ACPI tables provided by QEMU */
149 ulong write_acpi_tables(ulong addr)
152 struct fw_file *file;
153 struct bios_linker_entry *table_loader;
154 struct bios_linker_entry *entry;
157 /* make sure fw_list is loaded */
158 ret = qemu_fwcfg_read_firmware_list();
160 printf("error: can't read firmware file list\n");
164 file = qemu_fwcfg_find_file("etc/table-loader");
166 printf("error: can't find etc/table-loader\n");
170 size = be32_to_cpu(file->cfg.size);
171 if ((size % sizeof(*entry)) != 0) {
172 printf("error: table-loader maybe corrupted\n");
176 table_loader = malloc(size);
178 printf("error: no memory for table-loader\n");
182 qemu_fwcfg_read_entry(be16_to_cpu(file->cfg.select),
185 for (i = 0; i < (size / sizeof(*entry)); i++) {
186 entry = table_loader + i;
187 switch (le32_to_cpu(entry->command)) {
188 case BIOS_LINKER_LOADER_COMMAND_ALLOCATE:
189 ret = bios_linker_allocate(entry, &addr);
193 case BIOS_LINKER_LOADER_COMMAND_ADD_POINTER:
194 ret = bios_linker_add_pointer(entry);
198 case BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM:
199 ret = bios_linker_add_checksum(entry);
210 struct fw_cfg_file_iter iter;
211 for (file = qemu_fwcfg_file_iter_init(&iter);
212 !qemu_fwcfg_file_iter_end(&iter);
213 file = qemu_fwcfg_file_iter_next(&iter)) {
215 free((void *)file->addr);
225 ulong acpi_get_rsdp_addr(void)
227 struct fw_file *file;
229 file = qemu_fwcfg_find_file("etc/acpi/rsdp");
234 /* Read configuration item using fw_cfg PIO interface */
235 static void qemu_fwcfg_read_entry_pio(uint16_t entry,
236 uint32_t size, void *address)
238 debug("qemu_fwcfg_read_entry_pio: entry 0x%x, size %u address %p\n",
239 entry, size, address);
241 return fwcfg_arch_ops->arch_read_pio(entry, size, address);
244 /* Read configuration item using fw_cfg DMA interface */
245 static void qemu_fwcfg_read_entry_dma(uint16_t entry,
246 uint32_t size, void *address)
248 struct fw_cfg_dma_access dma;
250 dma.length = cpu_to_be32(size);
251 dma.address = cpu_to_be64((uintptr_t)address);
252 dma.control = cpu_to_be32(FW_CFG_DMA_READ);
255 * writting FW_CFG_INVALID will cause read operation to resume at
256 * last offset, otherwise read will start at offset 0
258 if (entry != FW_CFG_INVALID)
259 dma.control |= cpu_to_be32(FW_CFG_DMA_SELECT | (entry << 16));
263 debug("qemu_fwcfg_read_entry_dma: entry 0x%x, size %u address %p, control 0x%x\n",
264 entry, size, address, be32_to_cpu(dma.control));
266 fwcfg_arch_ops->arch_read_dma(&dma);
269 bool qemu_fwcfg_present(void)
271 return fwcfg_present;
274 bool qemu_fwcfg_dma_present(void)
276 return fwcfg_dma_present;
279 void qemu_fwcfg_read_entry(uint16_t entry, uint32_t length, void *address)
281 if (fwcfg_dma_present)
282 qemu_fwcfg_read_entry_dma(entry, length, address);
284 qemu_fwcfg_read_entry_pio(entry, length, address);
287 int qemu_fwcfg_online_cpus(void)
294 qemu_fwcfg_read_entry(FW_CFG_NB_CPUS, 2, &nb_cpus);
296 return le16_to_cpu(nb_cpus);
299 int qemu_fwcfg_read_firmware_list(void)
303 struct fw_file *file;
304 struct list_head *entry;
306 /* don't read it twice */
307 if (!list_empty(&fw_list))
310 qemu_fwcfg_read_entry(FW_CFG_FILE_DIR, 4, &count);
314 count = be32_to_cpu(count);
315 for (i = 0; i < count; i++) {
316 file = malloc(sizeof(*file));
318 printf("error: allocating resource\n");
321 qemu_fwcfg_read_entry(FW_CFG_INVALID,
322 sizeof(struct fw_cfg_file), &file->cfg);
324 list_add_tail(&file->list, &fw_list);
330 list_for_each(entry, &fw_list) {
331 file = list_entry(entry, struct fw_file, list);
338 struct fw_file *qemu_fwcfg_find_file(const char *name)
340 struct list_head *entry;
341 struct fw_file *file;
343 list_for_each(entry, &fw_list) {
344 file = list_entry(entry, struct fw_file, list);
345 if (!strcmp(file->cfg.name, name))
352 struct fw_file *qemu_fwcfg_file_iter_init(struct fw_cfg_file_iter *iter)
354 iter->entry = fw_list.next;
355 return list_entry((struct list_head *)iter->entry,
356 struct fw_file, list);
359 struct fw_file *qemu_fwcfg_file_iter_next(struct fw_cfg_file_iter *iter)
361 iter->entry = ((struct list_head *)iter->entry)->next;
362 return list_entry((struct list_head *)iter->entry,
363 struct fw_file, list);
366 bool qemu_fwcfg_file_iter_end(struct fw_cfg_file_iter *iter)
368 return iter->entry == &fw_list;
371 void qemu_fwcfg_init(struct fw_cfg_arch_ops *ops)
374 uint32_t dma_enabled;
376 fwcfg_present = false;
377 fwcfg_dma_present = false;
378 fwcfg_arch_ops = NULL;
380 if (!ops || !ops->arch_read_pio || !ops->arch_read_dma)
382 fwcfg_arch_ops = ops;
384 qemu_fwcfg_read_entry_pio(FW_CFG_SIGNATURE, 4, &qemu);
385 if (be32_to_cpu(qemu) == QEMU_FW_CFG_SIGNATURE)
386 fwcfg_present = true;
389 qemu_fwcfg_read_entry_pio(FW_CFG_ID, 1, &dma_enabled);
390 if (dma_enabled & FW_CFG_DMA_ENABLED)
391 fwcfg_dma_present = true;