master
c 319 lines 8.69 KB
Raw
1 /*
2 * libqos fw_cfg support
3 *
4 * Copyright IBM, Corp. 2012-2013
5 * Copyright (C) 2013 Red Hat Inc.
6 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Markus Armbruster <armbru@redhat.com>
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
13 */
14
15 #include "qemu/osdep.h"
16 #include "fw_cfg.h"
17 #include "malloc-pc.h"
18 #include "libqos-malloc.h"
19 #include "../libqtest.h"
20 #include "hw/nvram/fw_cfg.h"
21
22 void qfw_cfg_select(QFWCFG *fw_cfg, uint16_t key)
23 {
24 fw_cfg->select(fw_cfg, key);
25 }
26
27 void qfw_cfg_read_data(QFWCFG *fw_cfg, void *data, size_t len)
28 {
29 fw_cfg->read(fw_cfg, data, len);
30 }
31
32 void qfw_cfg_get(QFWCFG *fw_cfg, uint16_t key, void *data, size_t len)
33 {
34 qfw_cfg_select(fw_cfg, key);
35 qfw_cfg_read_data(fw_cfg, data, len);
36 }
37
38 uint16_t qfw_cfg_get_u16(QFWCFG *fw_cfg, uint16_t key)
39 {
40 uint16_t value;
41 qfw_cfg_get(fw_cfg, key, &value, sizeof(value));
42 return le16_to_cpu(value);
43 }
44
45 uint32_t qfw_cfg_get_u32(QFWCFG *fw_cfg, uint16_t key)
46 {
47 uint32_t value;
48 qfw_cfg_get(fw_cfg, key, &value, sizeof(value));
49 return le32_to_cpu(value);
50 }
51
52 uint64_t qfw_cfg_get_u64(QFWCFG *fw_cfg, uint16_t key)
53 {
54 uint64_t value;
55 qfw_cfg_get(fw_cfg, key, &value, sizeof(value));
56 return le64_to_cpu(value);
57 }
58
59 static void mm_fw_cfg_select(QFWCFG *fw_cfg, uint16_t key)
60 {
61 qtest_writew(fw_cfg->qts, fw_cfg->base, key);
62 }
63
64 static void qfw_cfg_dma_transfer(QFWCFG *fw_cfg, QOSState *qs, void *address,
65 uint32_t length, uint32_t control)
66 {
67 FWCfgDmaAccess access;
68 uint32_t addr;
69 uint64_t guest_access_addr;
70 uint64_t gaddr;
71
72 /* create a data buffer in guest memory */
73 gaddr = guest_alloc(&qs->alloc, length);
74
75 if (control & FW_CFG_DMA_CTL_WRITE) {
76 qtest_bufwrite(fw_cfg->qts, gaddr, address, length);
77 }
78 access.address = cpu_to_be64(gaddr);
79 access.length = cpu_to_be32(length);
80 access.control = cpu_to_be32(control);
81
82 /* now create a separate buffer in guest memory for 'access' */
83 guest_access_addr = guest_alloc(&qs->alloc, sizeof(access));
84 qtest_bufwrite(fw_cfg->qts, guest_access_addr, &access, sizeof(access));
85
86 /* write lower 32 bits of address */
87 addr = cpu_to_be32((uint32_t)(uintptr_t)guest_access_addr);
88 qtest_outl(fw_cfg->qts, fw_cfg->base + 8, addr);
89
90 /* write upper 32 bits of address */
91 addr = cpu_to_be32((uint32_t)(uintptr_t)(guest_access_addr >> 32));
92 qtest_outl(fw_cfg->qts, fw_cfg->base + 4, addr);
93
94 g_assert(!(be32_to_cpu(access.control) & FW_CFG_DMA_CTL_ERROR));
95
96 if (control & FW_CFG_DMA_CTL_READ) {
97 qtest_bufread(fw_cfg->qts, gaddr, address, length);
98 }
99
100 guest_free(&qs->alloc, guest_access_addr);
101 guest_free(&qs->alloc, gaddr);
102 }
103
104 static void qfw_cfg_write_entry(QFWCFG *fw_cfg, QOSState *qs, uint16_t key,
105 void *buf, uint32_t len)
106 {
107 qfw_cfg_select(fw_cfg, key);
108 qfw_cfg_dma_transfer(fw_cfg, qs, buf, len, FW_CFG_DMA_CTL_WRITE);
109 }
110
111 static void qfw_cfg_read_entry(QFWCFG *fw_cfg, QOSState *qs, uint16_t key,
112 void *buf, uint32_t len)
113 {
114 qfw_cfg_select(fw_cfg, key);
115 qfw_cfg_dma_transfer(fw_cfg, qs, buf, len, FW_CFG_DMA_CTL_READ);
116 }
117
118 static bool find_pdir_entry(QFWCFG *fw_cfg, const char *filename,
119 uint16_t *sel, uint32_t *size)
120 {
121 g_autofree unsigned char *filesbuf = NULL;
122 uint32_t count;
123 size_t dsize;
124 FWCfgFile *pdir_entry;
125 uint32_t i;
126 bool found = false;
127
128 *size = 0;
129 *sel = 0;
130
131 qfw_cfg_get(fw_cfg, FW_CFG_FILE_DIR, &count, sizeof(count));
132 count = be32_to_cpu(count);
133 dsize = sizeof(uint32_t) + count * sizeof(struct fw_cfg_file);
134 filesbuf = g_malloc(dsize);
135 qfw_cfg_get(fw_cfg, FW_CFG_FILE_DIR, filesbuf, dsize);
136 pdir_entry = (FWCfgFile *)(filesbuf + sizeof(uint32_t));
137 for (i = 0; i < count; ++i, ++pdir_entry) {
138 if (!strcmp(pdir_entry->name, filename)) {
139 *size = be32_to_cpu(pdir_entry->size);
140 *sel = be16_to_cpu(pdir_entry->select);
141 found = true;
142 break;
143 }
144 }
145
146 return found;
147 }
148
149 /*
150 * The caller need check the return value. When the return value is
151 * nonzero, it means that some bytes have been transferred.
152 *
153 * If the fw_cfg file in question is smaller than the allocated & passed-in
154 * buffer, then the buffer has been populated only in part.
155 *
156 * If the fw_cfg file in question is larger than the passed-in
157 * buffer, then the return value explains how much room would have been
158 * necessary in total. And, while the caller's buffer has been fully
159 * populated, it has received only a starting slice of the fw_cfg file.
160 */
161 size_t qfw_cfg_get_file(QFWCFG *fw_cfg, const char *filename,
162 void *data, size_t buflen)
163 {
164 size_t filesize = 0;
165 uint32_t len;
166 uint16_t sel;
167
168 if (find_pdir_entry(fw_cfg, filename, &sel, &len)) {
169 filesize = len;
170 if (len > buflen) {
171 len = buflen;
172 }
173 qfw_cfg_get(fw_cfg, sel, data, len);
174 }
175
176 return filesize;
177 }
178
179 /*
180 * The caller need check the return value. When the return value is
181 * nonzero, it means that some bytes have been transferred.
182 *
183 * If the fw_cfg file in question is smaller than the allocated & passed-in
184 * buffer, then the first len bytes were read.
185 *
186 * If the fw_cfg file in question is larger than the passed-in
187 * buffer, then the return value explains how much was actually read.
188 *
189 * It is illegal to call this function if fw_cfg does not support DMA
190 * interface. The caller should ensure that DMA is supported before
191 * calling this function.
192 *
193 * Passed QOSState pointer qs must be initialized. qs->alloc must also be
194 * properly initialized.
195 */
196 size_t qfw_cfg_read_file(QFWCFG *fw_cfg, QOSState *qs, const char *filename,
197 void *data, size_t buflen)
198 {
199 uint32_t len = 0;
200 uint16_t sel;
201 uint32_t id;
202
203 g_assert(qs);
204 g_assert(filename);
205 g_assert(data);
206 g_assert(buflen);
207 /* check if DMA is supported since we use DMA for read */
208 id = qfw_cfg_get_u32(fw_cfg, FW_CFG_ID);
209 g_assert(id & FW_CFG_VERSION_DMA);
210
211 if (find_pdir_entry(fw_cfg, filename, &sel, &len)) {
212 if (len > buflen) {
213 len = buflen;
214 }
215 qfw_cfg_read_entry(fw_cfg, qs, sel, data, len);
216 }
217
218 return len;
219 }
220
221 /*
222 * The caller need check the return value. When the return value is
223 * nonzero, it means that some bytes have been transferred.
224 *
225 * If the fw_cfg file in question is smaller than the allocated & passed-in
226 * buffer, then the buffer has been partially written.
227 *
228 * If the fw_cfg file in question is larger than the passed-in
229 * buffer, then the return value explains how much was actually written.
230 *
231 * It is illegal to call this function if fw_cfg does not support DMA
232 * interface. The caller should ensure that DMA is supported before
233 * calling this function.
234 *
235 * Passed QOSState pointer qs must be initialized. qs->alloc must also be
236 * properly initialized.
237 */
238 size_t qfw_cfg_write_file(QFWCFG *fw_cfg, QOSState *qs, const char *filename,
239 void *data, size_t buflen)
240 {
241 uint32_t len = 0;
242 uint16_t sel;
243 uint32_t id;
244
245 g_assert(qs);
246 g_assert(filename);
247 g_assert(data);
248 g_assert(buflen);
249 /* write operation is only valid if DMA is supported */
250 id = qfw_cfg_get_u32(fw_cfg, FW_CFG_ID);
251 g_assert(id & FW_CFG_VERSION_DMA);
252
253 if (find_pdir_entry(fw_cfg, filename, &sel, &len)) {
254 if (len > buflen) {
255 len = buflen;
256 }
257 qfw_cfg_write_entry(fw_cfg, qs, sel, data, len);
258 }
259 return len;
260 }
261
262 static void mm_fw_cfg_read(QFWCFG *fw_cfg, void *data, size_t len)
263 {
264 uint8_t *ptr = data;
265 int i;
266
267 for (i = 0; i < len; i++) {
268 ptr[i] = qtest_readb(fw_cfg->qts, fw_cfg->base + 2);
269 }
270 }
271
272 QFWCFG *mm_fw_cfg_init(QTestState *qts, uint64_t base)
273 {
274 QFWCFG *fw_cfg = g_malloc0(sizeof(*fw_cfg));
275
276 fw_cfg->base = base;
277 fw_cfg->qts = qts;
278 fw_cfg->select = mm_fw_cfg_select;
279 fw_cfg->read = mm_fw_cfg_read;
280
281 return fw_cfg;
282 }
283
284 void mm_fw_cfg_uninit(QFWCFG *fw_cfg)
285 {
286 g_free(fw_cfg);
287 }
288
289 static void io_fw_cfg_select(QFWCFG *fw_cfg, uint16_t key)
290 {
291 qtest_outw(fw_cfg->qts, fw_cfg->base, key);
292 }
293
294 static void io_fw_cfg_read(QFWCFG *fw_cfg, void *data, size_t len)
295 {
296 uint8_t *ptr = data;
297 int i;
298
299 for (i = 0; i < len; i++) {
300 ptr[i] = qtest_inb(fw_cfg->qts, fw_cfg->base + 1);
301 }
302 }
303
304 QFWCFG *io_fw_cfg_init(QTestState *qts, uint16_t base)
305 {
306 QFWCFG *fw_cfg = g_malloc0(sizeof(*fw_cfg));
307
308 fw_cfg->base = base;
309 fw_cfg->qts = qts;
310 fw_cfg->select = io_fw_cfg_select;
311 fw_cfg->read = io_fw_cfg_read;
312
313 return fw_cfg;
314 }
315
316 void io_fw_cfg_uninit(QFWCFG *fw_cfg)
317 {
318 g_free(fw_cfg);
319 }