2 * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0+
13 #ifdef CONFIG_GENERATE_ACPI_TABLE
14 #include <asm/tables.h>
16 #include <linux/list.h>
18 static bool fwcfg_present;
19 static bool fwcfg_dma_present;
20 static struct fw_cfg_arch_ops *fwcfg_arch_ops;
22 static LIST_HEAD(fw_list);
24 #ifdef CONFIG_GENERATE_ACPI_TABLE
26 * This function allocates memory for ACPI tables
28 * @entry : BIOS linker command entry which tells where to allocate memory
29 * (either high memory or low memory)
30 * @addr : The address that should be used for low memory allcation. If the
31 * memory allocation request is 'ZONE_HIGH' then this parameter will
33 * @return: 0 on success, or negative value on failure
35 static int bios_linker_allocate(struct bios_linker_entry *entry, ulong *addr)
39 unsigned long aligned_addr;
41 align = le32_to_cpu(entry->alloc.align);
42 /* align must be power of 2 */
43 if (align & (align - 1)) {
44 printf("error: wrong alignment %u\n", align);
48 file = qemu_fwcfg_find_file(entry->alloc.file);
50 printf("error: can't find file %s\n", entry->alloc.file);
54 size = be32_to_cpu(file->cfg.size);
57 * ZONE_HIGH means we need to allocate from high memory, since
58 * malloc space is already at the end of RAM, so we directly use it.
59 * If allocation zone is ZONE_FSEG, then we use the 'addr' passed
60 * in which is low memory
62 if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH) {
63 aligned_addr = (unsigned long)memalign(align, size);
65 printf("error: allocating resource\n");
68 } else if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG) {
69 aligned_addr = ALIGN(*addr, align);
71 printf("error: invalid allocation zone\n");
75 debug("bios_linker_allocate: allocate file %s, size %u, zone %d, align %u, addr 0x%lx\n",
76 file->cfg.name, size, entry->alloc.zone, align, aligned_addr);
78 qemu_fwcfg_read_entry(be16_to_cpu(file->cfg.select),
79 size, (void *)aligned_addr);
80 file->addr = aligned_addr;
82 /* adjust address for low memory allocation */
83 if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG)
84 *addr = (aligned_addr + size);
90 * This function patches ACPI tables previously loaded
91 * by bios_linker_allocate()
93 * @entry : BIOS linker command entry which tells how to patch
95 * @return: 0 on success, or negative value on failure
97 static int bios_linker_add_pointer(struct bios_linker_entry *entry)
99 struct fw_file *dest, *src;
100 uint32_t offset = le32_to_cpu(entry->pointer.offset);
101 uint64_t pointer = 0;
103 dest = qemu_fwcfg_find_file(entry->pointer.dest_file);
104 if (!dest || !dest->addr)
106 src = qemu_fwcfg_find_file(entry->pointer.src_file);
107 if (!src || !src->addr)
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);
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);
123 * This function updates checksum fields of ACPI tables previously loaded
124 * by bios_linker_allocate()
126 * @entry : BIOS linker command entry which tells where to update ACPI table
128 * @return: 0 on success, or negative value on failure
130 static int bios_linker_add_checksum(struct bios_linker_entry *entry)
132 struct fw_file *file;
133 uint8_t *data, cksum = 0;
134 uint8_t *cksum_start;
136 file = qemu_fwcfg_find_file(entry->cksum.file);
137 if (!file || !file->addr)
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));
149 /* This function loads and patches ACPI tables provided by QEMU */
150 ulong write_acpi_tables(ulong addr)
153 struct fw_file *file;
154 struct bios_linker_entry *table_loader;
155 struct bios_linker_entry *entry;
158 /* make sure fw_list is loaded */
159 ret = qemu_fwcfg_read_firmware_list();
161 printf("error: can't read firmware file list\n");
165 file = qemu_fwcfg_find_file("etc/table-loader");
167 printf("error: can't find etc/table-loader\n");
171 size = be32_to_cpu(file->cfg.size);
172 if ((size % sizeof(*entry)) != 0) {
173 printf("error: table-loader maybe corrupted\n");
177 table_loader = malloc(size);
179 printf("error: no memory for table-loader\n");
183 qemu_fwcfg_read_entry(be16_to_cpu(file->cfg.select),
186 for (i = 0; i < (size / sizeof(*entry)); i++) {
187 entry = table_loader + i;
188 switch (le32_to_cpu(entry->command)) {
189 case BIOS_LINKER_LOADER_COMMAND_ALLOCATE:
190 ret = bios_linker_allocate(entry, &addr);
194 case BIOS_LINKER_LOADER_COMMAND_ADD_POINTER:
195 ret = bios_linker_add_pointer(entry);
199 case BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM:
200 ret = bios_linker_add_checksum(entry);
211 struct fw_cfg_file_iter iter;
212 for (file = qemu_fwcfg_file_iter_init(&iter);
213 !qemu_fwcfg_file_iter_end(&iter);
214 file = qemu_fwcfg_file_iter_next(&iter)) {
216 free((void *)file->addr);
226 ulong acpi_get_rsdp_addr(void)
228 struct fw_file *file;
230 file = qemu_fwcfg_find_file("etc/acpi/rsdp");
235 /* Read configuration item using fw_cfg PIO interface */
236 static void qemu_fwcfg_read_entry_pio(uint16_t entry,
237 uint32_t size, void *address)
239 debug("qemu_fwcfg_read_entry_pio: entry 0x%x, size %u address %p\n",
240 entry, size, address);
242 return fwcfg_arch_ops->arch_read_pio(entry, size, address);
245 /* Read configuration item using fw_cfg DMA interface */
246 static void qemu_fwcfg_read_entry_dma(uint16_t entry,
247 uint32_t size, void *address)
249 struct fw_cfg_dma_access dma;
251 dma.length = cpu_to_be32(size);
252 dma.address = cpu_to_be64((uintptr_t)address);
253 dma.control = cpu_to_be32(FW_CFG_DMA_READ);
256 * writting FW_CFG_INVALID will cause read operation to resume at
257 * last offset, otherwise read will start at offset 0
259 if (entry != FW_CFG_INVALID)
260 dma.control |= cpu_to_be32(FW_CFG_DMA_SELECT | (entry << 16));
264 debug("qemu_fwcfg_read_entry_dma: entry 0x%x, size %u address %p, control 0x%x\n",
265 entry, size, address, be32_to_cpu(dma.control));
267 fwcfg_arch_ops->arch_read_dma(&dma);
270 bool qemu_fwcfg_present(void)
272 return fwcfg_present;
275 bool qemu_fwcfg_dma_present(void)
277 return fwcfg_dma_present;
280 void qemu_fwcfg_read_entry(uint16_t entry, uint32_t length, void *address)
282 if (fwcfg_dma_present)
283 qemu_fwcfg_read_entry_dma(entry, length, address);
285 qemu_fwcfg_read_entry_pio(entry, length, address);
288 int qemu_fwcfg_online_cpus(void)
295 qemu_fwcfg_read_entry(FW_CFG_NB_CPUS, 2, &nb_cpus);
297 return le16_to_cpu(nb_cpus);
300 int qemu_fwcfg_read_firmware_list(void)
304 struct fw_file *file;
305 struct list_head *entry;
307 /* don't read it twice */
308 if (!list_empty(&fw_list))
311 qemu_fwcfg_read_entry(FW_CFG_FILE_DIR, 4, &count);
315 count = be32_to_cpu(count);
316 for (i = 0; i < count; i++) {
317 file = malloc(sizeof(*file));
319 printf("error: allocating resource\n");
322 qemu_fwcfg_read_entry(FW_CFG_INVALID,
323 sizeof(struct fw_cfg_file), &file->cfg);
325 list_add_tail(&file->list, &fw_list);
331 list_for_each(entry, &fw_list) {
332 file = list_entry(entry, struct fw_file, list);
339 struct fw_file *qemu_fwcfg_find_file(const char *name)
341 struct list_head *entry;
342 struct fw_file *file;
344 list_for_each(entry, &fw_list) {
345 file = list_entry(entry, struct fw_file, list);
346 if (!strcmp(file->cfg.name, name))
353 struct fw_file *qemu_fwcfg_file_iter_init(struct fw_cfg_file_iter *iter)
355 iter->entry = fw_list.next;
356 return list_entry((struct list_head *)iter->entry,
357 struct fw_file, list);
360 struct fw_file *qemu_fwcfg_file_iter_next(struct fw_cfg_file_iter *iter)
362 iter->entry = ((struct list_head *)iter->entry)->next;
363 return list_entry((struct list_head *)iter->entry,
364 struct fw_file, list);
367 bool qemu_fwcfg_file_iter_end(struct fw_cfg_file_iter *iter)
369 return iter->entry == &fw_list;
372 void qemu_fwcfg_init(struct fw_cfg_arch_ops *ops)
375 uint32_t dma_enabled;
377 fwcfg_present = false;
378 fwcfg_dma_present = false;
379 fwcfg_arch_ops = NULL;
381 if (!ops || !ops->arch_read_pio || !ops->arch_read_dma)
383 fwcfg_arch_ops = ops;
385 qemu_fwcfg_read_entry_pio(FW_CFG_SIGNATURE, 4, &qemu);
386 if (be32_to_cpu(qemu) == QEMU_FW_CFG_SIGNATURE)
387 fwcfg_present = true;
390 qemu_fwcfg_read_entry_pio(FW_CFG_ID, 1, &dma_enabled);
391 if (dma_enabled & FW_CFG_DMA_ENABLED)
392 fwcfg_dma_present = true;