blob: a574bd1ec186438c3667fa753ed3766f0f57e5b5 [file] [log] [blame]
Miao Yan4fcd7f22016-05-22 19:37:14 -07001/*
2 * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <command.h>
9#include <errno.h>
10#include <malloc.h>
11#include <qemu_fw_cfg.h>
12#include <asm/io.h>
13#include <linux/list.h>
14
15static bool fwcfg_present;
16static bool fwcfg_dma_present;
17
18static LIST_HEAD(fw_list);
19
20/* Read configuration item using fw_cfg PIO interface */
21static void qemu_fwcfg_read_entry_pio(uint16_t entry,
22 uint32_t size, void *address)
23{
24 uint32_t i = 0;
25 uint8_t *data = address;
26
27 /*
28 * writting FW_CFG_INVALID will cause read operation to resume at
29 * last offset, otherwise read will start at offset 0
30 */
31 if (entry != FW_CFG_INVALID)
32 outw(entry, FW_CONTROL_PORT);
33 while (size--)
34 data[i++] = inb(FW_DATA_PORT);
35}
36
37/* Read configuration item using fw_cfg DMA interface */
38static void qemu_fwcfg_read_entry_dma(uint16_t entry,
39 uint32_t size, void *address)
40{
41 struct fw_cfg_dma_access dma;
42
43 dma.length = cpu_to_be32(size);
44 dma.address = cpu_to_be64((uintptr_t)address);
45 dma.control = cpu_to_be32(FW_CFG_DMA_READ);
46
47 /*
48 * writting FW_CFG_INVALID will cause read operation to resume at
49 * last offset, otherwise read will start at offset 0
50 */
51 if (entry != FW_CFG_INVALID)
52 dma.control |= cpu_to_be32(FW_CFG_DMA_SELECT | (entry << 16));
53
54 barrier();
55
56 debug("qemu_fwcfg_dma_read_entry: addr %p, length %u control 0x%x\n",
57 address, size, be32_to_cpu(dma.control));
58
59 outl(cpu_to_be32((uint32_t)&dma), FW_DMA_PORT_HIGH);
60
61 while (be32_to_cpu(dma.control) & ~FW_CFG_DMA_ERROR)
62 __asm__ __volatile__ ("pause");
63}
64
65bool qemu_fwcfg_present(void)
66{
67 return fwcfg_present;
68}
69
70bool qemu_fwcfg_dma_present(void)
71{
72 return fwcfg_dma_present;
73}
74
75void qemu_fwcfg_read_entry(uint16_t entry, uint32_t length, void *address)
76{
77 if (fwcfg_dma_present)
78 qemu_fwcfg_read_entry_dma(entry, length, address);
79 else
80 qemu_fwcfg_read_entry_pio(entry, length, address);
81}
82
83int qemu_fwcfg_online_cpus(void)
84{
85 uint16_t nb_cpus;
86
87 if (!fwcfg_present)
88 return -ENODEV;
89
90 qemu_fwcfg_read_entry(FW_CFG_NB_CPUS, 2, &nb_cpus);
91
92 return le16_to_cpu(nb_cpus);
93}
94
95int qemu_fwcfg_read_firmware_list(void)
96{
97 int i;
98 uint32_t count;
99 struct fw_file *file;
100 struct list_head *entry;
101
102 /* don't read it twice */
103 if (!list_empty(&fw_list))
104 return 0;
105
106 qemu_fwcfg_read_entry(FW_CFG_FILE_DIR, 4, &count);
107 if (!count)
108 return 0;
109
110 count = be32_to_cpu(count);
111 for (i = 0; i < count; i++) {
112 file = malloc(sizeof(*file));
113 if (!file) {
114 printf("error: allocating resource\n");
115 goto err;
116 }
117 qemu_fwcfg_read_entry(FW_CFG_INVALID,
118 sizeof(struct fw_cfg_file), &file->cfg);
119 file->addr = 0;
120 list_add_tail(&file->list, &fw_list);
121 }
122
123 return 0;
124
125err:
126 list_for_each(entry, &fw_list) {
127 file = list_entry(entry, struct fw_file, list);
128 free(file);
129 }
130
131 return -ENOMEM;
132}
133
134struct fw_file *qemu_fwcfg_find_file(const char *name)
135{
136 struct list_head *entry;
137 struct fw_file *file;
138
139 list_for_each(entry, &fw_list) {
140 file = list_entry(entry, struct fw_file, list);
141 if (!strcmp(file->cfg.name, name))
142 return file;
143 }
144
145 return NULL;
146}
147
148struct fw_file *qemu_fwcfg_file_iter_init(struct fw_cfg_file_iter *iter)
149{
150 iter->entry = fw_list.next;
151 return list_entry((struct list_head *)iter->entry,
152 struct fw_file, list);
153}
154
155struct fw_file *qemu_fwcfg_file_iter_next(struct fw_cfg_file_iter *iter)
156{
157 iter->entry = ((struct list_head *)iter->entry)->next;
158 return list_entry((struct list_head *)iter->entry,
159 struct fw_file, list);
160}
161
162bool qemu_fwcfg_file_iter_end(struct fw_cfg_file_iter *iter)
163{
164 return iter->entry == &fw_list;
165}
166
167void qemu_fwcfg_init(void)
168{
169 uint32_t qemu;
170 uint32_t dma_enabled;
171
172 fwcfg_present = false;
173 fwcfg_dma_present = false;
174
175 qemu_fwcfg_read_entry_pio(FW_CFG_SIGNATURE, 4, &qemu);
176 if (be32_to_cpu(qemu) == QEMU_FW_CFG_SIGNATURE)
177 fwcfg_present = true;
178
179 if (fwcfg_present) {
180 qemu_fwcfg_read_entry_pio(FW_CFG_ID, 1, &dma_enabled);
181 if (dma_enabled & FW_CFG_DMA_ENABLED)
182 fwcfg_dma_present = true;
183 }
184}