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);
227 /* Read configuration item using fw_cfg PIO interface */
228 static void qemu_fwcfg_read_entry_pio(uint16_t entry,
229 uint32_t size, void *address)
231 debug("qemu_fwcfg_read_entry_pio: entry 0x%x, size %u address %p\n",
232 entry, size, address);
234 return fwcfg_arch_ops->arch_read_pio(entry, size, address);
237 /* Read configuration item using fw_cfg DMA interface */
238 static void qemu_fwcfg_read_entry_dma(uint16_t entry,
239 uint32_t size, void *address)
241 struct fw_cfg_dma_access dma;
243 dma.length = cpu_to_be32(size);
244 dma.address = cpu_to_be64((uintptr_t)address);
245 dma.control = cpu_to_be32(FW_CFG_DMA_READ);
248 * writting FW_CFG_INVALID will cause read operation to resume at
249 * last offset, otherwise read will start at offset 0
251 if (entry != FW_CFG_INVALID)
252 dma.control |= cpu_to_be32(FW_CFG_DMA_SELECT | (entry << 16));
256 debug("qemu_fwcfg_read_entry_dma: entry 0x%x, size %u address %p, control 0x%x\n",
257 entry, size, address, be32_to_cpu(dma.control));
259 fwcfg_arch_ops->arch_read_dma(&dma);
262 bool qemu_fwcfg_present(void)
264 return fwcfg_present;
267 bool qemu_fwcfg_dma_present(void)
269 return fwcfg_dma_present;
272 void qemu_fwcfg_read_entry(uint16_t entry, uint32_t length, void *address)
274 if (fwcfg_dma_present)
275 qemu_fwcfg_read_entry_dma(entry, length, address);
277 qemu_fwcfg_read_entry_pio(entry, length, address);
280 int qemu_fwcfg_online_cpus(void)
287 qemu_fwcfg_read_entry(FW_CFG_NB_CPUS, 2, &nb_cpus);
289 return le16_to_cpu(nb_cpus);
292 int qemu_fwcfg_read_firmware_list(void)
296 struct fw_file *file;
297 struct list_head *entry;
299 /* don't read it twice */
300 if (!list_empty(&fw_list))
303 qemu_fwcfg_read_entry(FW_CFG_FILE_DIR, 4, &count);
307 count = be32_to_cpu(count);
308 for (i = 0; i < count; i++) {
309 file = malloc(sizeof(*file));
311 printf("error: allocating resource\n");
314 qemu_fwcfg_read_entry(FW_CFG_INVALID,
315 sizeof(struct fw_cfg_file), &file->cfg);
317 list_add_tail(&file->list, &fw_list);
323 list_for_each(entry, &fw_list) {
324 file = list_entry(entry, struct fw_file, list);
331 struct fw_file *qemu_fwcfg_find_file(const char *name)
333 struct list_head *entry;
334 struct fw_file *file;
336 list_for_each(entry, &fw_list) {
337 file = list_entry(entry, struct fw_file, list);
338 if (!strcmp(file->cfg.name, name))
345 struct fw_file *qemu_fwcfg_file_iter_init(struct fw_cfg_file_iter *iter)
347 iter->entry = fw_list.next;
348 return list_entry((struct list_head *)iter->entry,
349 struct fw_file, list);
352 struct fw_file *qemu_fwcfg_file_iter_next(struct fw_cfg_file_iter *iter)
354 iter->entry = ((struct list_head *)iter->entry)->next;
355 return list_entry((struct list_head *)iter->entry,
356 struct fw_file, list);
359 bool qemu_fwcfg_file_iter_end(struct fw_cfg_file_iter *iter)
361 return iter->entry == &fw_list;
364 void qemu_fwcfg_init(struct fw_cfg_arch_ops *ops)
367 uint32_t dma_enabled;
369 fwcfg_present = false;
370 fwcfg_dma_present = false;
371 fwcfg_arch_ops = NULL;
373 if (!ops || !ops->arch_read_pio || !ops->arch_read_dma)
375 fwcfg_arch_ops = ops;
377 qemu_fwcfg_read_entry_pio(FW_CFG_SIGNATURE, 4, &qemu);
378 if (be32_to_cpu(qemu) == QEMU_FW_CFG_SIGNATURE)
379 fwcfg_present = true;
382 qemu_fwcfg_read_entry_pio(FW_CFG_ID, 1, &dma_enabled);
383 if (dma_enabled & FW_CFG_DMA_ENABLED)
384 fwcfg_dma_present = true;