]>
Commit | Line | Data |
---|---|---|
3cce6243 BS |
1 | /* |
2 | * QEMU Firmware configuration device emulation | |
3 | * | |
4 | * Copyright (c) 2008 Gleb Natapov | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
0430891c | 24 | #include "qemu/osdep.h" |
83c9f4ca | 25 | #include "hw/hw.h" |
9c17d615 | 26 | #include "sysemu/sysemu.h" |
a4c0d1de | 27 | #include "sysemu/dma.h" |
cfc58cf3 | 28 | #include "hw/boards.h" |
0d09e41a PB |
29 | #include "hw/isa/isa.h" |
30 | #include "hw/nvram/fw_cfg.h" | |
83c9f4ca | 31 | #include "hw/sysbus.h" |
f6e35343 | 32 | #include "trace.h" |
1de7afc9 PB |
33 | #include "qemu/error-report.h" |
34 | #include "qemu/config-file.h" | |
f348b6d1 | 35 | #include "qemu/cutils.h" |
e12f3a13 | 36 | #include "qapi/error.h" |
3cce6243 | 37 | |
a5b3ebfd LE |
38 | #define FW_CFG_FILE_SLOTS_DFLT 0x20 |
39 | ||
a4c0d1de MM |
40 | /* FW_CFG_VERSION bits */ |
41 | #define FW_CFG_VERSION 0x01 | |
42 | #define FW_CFG_VERSION_DMA 0x02 | |
43 | ||
44 | /* FW_CFG_DMA_CONTROL bits */ | |
45 | #define FW_CFG_DMA_CTL_ERROR 0x01 | |
46 | #define FW_CFG_DMA_CTL_READ 0x02 | |
47 | #define FW_CFG_DMA_CTL_SKIP 0x04 | |
48 | #define FW_CFG_DMA_CTL_SELECT 0x08 | |
baf2d5bf | 49 | #define FW_CFG_DMA_CTL_WRITE 0x10 |
a4c0d1de | 50 | |
2cc06a88 KC |
51 | #define FW_CFG_DMA_SIGNATURE 0x51454d5520434647ULL /* "QEMU CFG" */ |
52 | ||
39736e18 | 53 | struct FWCfgEntry { |
ff06108b | 54 | uint32_t len; |
baf2d5bf | 55 | bool allow_write; |
3cce6243 BS |
56 | uint8_t *data; |
57 | void *callback_opaque; | |
d87072ce | 58 | FWCfgReadCallback read_callback; |
5712db6a LE |
59 | }; |
60 | ||
3d3b8303 WX |
61 | #define JPG_FILE 0 |
62 | #define BMP_FILE 1 | |
63 | ||
3d1bba20 | 64 | static char *read_splashfile(char *filename, gsize *file_sizep, |
d09acb9b | 65 | int *file_typep) |
3d3b8303 | 66 | { |
9477c87e PB |
67 | GError *err = NULL; |
68 | gboolean res; | |
69 | gchar *content; | |
9f8863eb MA |
70 | int file_type; |
71 | unsigned int filehead; | |
3d3b8303 WX |
72 | int bmp_bpp; |
73 | ||
d09acb9b | 74 | res = g_file_get_contents(filename, &content, file_sizep, &err); |
9477c87e PB |
75 | if (res == FALSE) { |
76 | error_report("failed to read splash file '%s'", filename); | |
77 | g_error_free(err); | |
78 | return NULL; | |
3d3b8303 | 79 | } |
9477c87e | 80 | |
3d3b8303 | 81 | /* check file size */ |
9477c87e PB |
82 | if (*file_sizep < 30) { |
83 | goto error; | |
3d3b8303 | 84 | } |
9477c87e | 85 | |
3d3b8303 | 86 | /* check magic ID */ |
9477c87e PB |
87 | filehead = ((content[0] & 0xff) + (content[1] << 8)) & 0xffff; |
88 | if (filehead == 0xd8ff) { | |
3d3b8303 | 89 | file_type = JPG_FILE; |
9477c87e PB |
90 | } else if (filehead == 0x4d42) { |
91 | file_type = BMP_FILE; | |
3d3b8303 | 92 | } else { |
9477c87e | 93 | goto error; |
3d3b8303 | 94 | } |
9477c87e | 95 | |
3d3b8303 WX |
96 | /* check BMP bpp */ |
97 | if (file_type == BMP_FILE) { | |
9477c87e | 98 | bmp_bpp = (content[28] + (content[29] << 8)) & 0xffff; |
3d3b8303 | 99 | if (bmp_bpp != 24) { |
9477c87e | 100 | goto error; |
3d3b8303 WX |
101 | } |
102 | } | |
9477c87e | 103 | |
3d3b8303 | 104 | /* return values */ |
3d3b8303 | 105 | *file_typep = file_type; |
9477c87e PB |
106 | |
107 | return content; | |
108 | ||
109 | error: | |
110 | error_report("splash file '%s' format not recognized; must be JPEG " | |
111 | "or 24 bit BMP", filename); | |
112 | g_free(content); | |
113 | return NULL; | |
3d3b8303 WX |
114 | } |
115 | ||
116 | static void fw_cfg_bootsplash(FWCfgState *s) | |
117 | { | |
118 | int boot_splash_time = -1; | |
119 | const char *boot_splash_filename = NULL; | |
120 | char *p; | |
9477c87e | 121 | char *filename, *file_data; |
3d1bba20 | 122 | gsize file_size; |
9f8863eb | 123 | int file_type; |
3d3b8303 WX |
124 | const char *temp; |
125 | ||
126 | /* get user configuration */ | |
127 | QemuOptsList *plist = qemu_find_opts("boot-opts"); | |
128 | QemuOpts *opts = QTAILQ_FIRST(&plist->head); | |
129 | if (opts != NULL) { | |
130 | temp = qemu_opt_get(opts, "splash"); | |
131 | if (temp != NULL) { | |
132 | boot_splash_filename = temp; | |
133 | } | |
134 | temp = qemu_opt_get(opts, "splash-time"); | |
135 | if (temp != NULL) { | |
136 | p = (char *)temp; | |
ec8193a0 | 137 | boot_splash_time = strtol(p, &p, 10); |
3d3b8303 WX |
138 | } |
139 | } | |
140 | ||
141 | /* insert splash time if user configurated */ | |
142 | if (boot_splash_time >= 0) { | |
143 | /* validate the input */ | |
144 | if (boot_splash_time > 0xffff) { | |
145 | error_report("splash time is big than 65535, force it to 65535."); | |
146 | boot_splash_time = 0xffff; | |
147 | } | |
148 | /* use little endian format */ | |
149 | qemu_extra_params_fw[0] = (uint8_t)(boot_splash_time & 0xff); | |
150 | qemu_extra_params_fw[1] = (uint8_t)((boot_splash_time >> 8) & 0xff); | |
151 | fw_cfg_add_file(s, "etc/boot-menu-wait", qemu_extra_params_fw, 2); | |
152 | } | |
153 | ||
154 | /* insert splash file if user configurated */ | |
155 | if (boot_splash_filename != NULL) { | |
156 | filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, boot_splash_filename); | |
157 | if (filename == NULL) { | |
158 | error_report("failed to find file '%s'.", boot_splash_filename); | |
159 | return; | |
160 | } | |
9477c87e PB |
161 | |
162 | /* loading file data */ | |
163 | file_data = read_splashfile(filename, &file_size, &file_type); | |
164 | if (file_data == NULL) { | |
7267c094 | 165 | g_free(filename); |
3d3b8303 WX |
166 | return; |
167 | } | |
ef1e1e07 | 168 | g_free(boot_splash_filedata); |
9477c87e | 169 | boot_splash_filedata = (uint8_t *)file_data; |
3d3b8303 | 170 | boot_splash_filedata_size = file_size; |
9477c87e | 171 | |
3d3b8303 WX |
172 | /* insert data */ |
173 | if (file_type == JPG_FILE) { | |
174 | fw_cfg_add_file(s, "bootsplash.jpg", | |
175 | boot_splash_filedata, boot_splash_filedata_size); | |
176 | } else { | |
177 | fw_cfg_add_file(s, "bootsplash.bmp", | |
178 | boot_splash_filedata, boot_splash_filedata_size); | |
179 | } | |
7267c094 | 180 | g_free(filename); |
3d3b8303 WX |
181 | } |
182 | } | |
183 | ||
ac05f349 AK |
184 | static void fw_cfg_reboot(FWCfgState *s) |
185 | { | |
186 | int reboot_timeout = -1; | |
187 | char *p; | |
188 | const char *temp; | |
189 | ||
190 | /* get user configuration */ | |
191 | QemuOptsList *plist = qemu_find_opts("boot-opts"); | |
192 | QemuOpts *opts = QTAILQ_FIRST(&plist->head); | |
193 | if (opts != NULL) { | |
194 | temp = qemu_opt_get(opts, "reboot-timeout"); | |
195 | if (temp != NULL) { | |
196 | p = (char *)temp; | |
ec8193a0 | 197 | reboot_timeout = strtol(p, &p, 10); |
ac05f349 AK |
198 | } |
199 | } | |
200 | /* validate the input */ | |
201 | if (reboot_timeout > 0xffff) { | |
202 | error_report("reboot timeout is larger than 65535, force it to 65535."); | |
203 | reboot_timeout = 0xffff; | |
204 | } | |
205 | fw_cfg_add_file(s, "etc/boot-fail-wait", g_memdup(&reboot_timeout, 4), 4); | |
206 | } | |
207 | ||
3cce6243 BS |
208 | static void fw_cfg_write(FWCfgState *s, uint8_t value) |
209 | { | |
023e3148 | 210 | /* nothing, write support removed in QEMU v2.4+ */ |
3cce6243 BS |
211 | } |
212 | ||
e12f3a13 LE |
213 | static inline uint16_t fw_cfg_file_slots(const FWCfgState *s) |
214 | { | |
215 | return s->file_slots; | |
216 | } | |
217 | ||
218 | /* Note: this function returns an exclusive limit. */ | |
219 | static inline uint32_t fw_cfg_max_entry(const FWCfgState *s) | |
220 | { | |
221 | return FW_CFG_FILE_FIRST + fw_cfg_file_slots(s); | |
222 | } | |
223 | ||
3cce6243 BS |
224 | static int fw_cfg_select(FWCfgState *s, uint16_t key) |
225 | { | |
3bef7e8a GS |
226 | int arch, ret; |
227 | FWCfgEntry *e; | |
3cce6243 BS |
228 | |
229 | s->cur_offset = 0; | |
e12f3a13 | 230 | if ((key & FW_CFG_ENTRY_MASK) >= fw_cfg_max_entry(s)) { |
3cce6243 BS |
231 | s->cur_entry = FW_CFG_INVALID; |
232 | ret = 0; | |
233 | } else { | |
234 | s->cur_entry = key; | |
235 | ret = 1; | |
3bef7e8a GS |
236 | /* entry successfully selected, now run callback if present */ |
237 | arch = !!(key & FW_CFG_ARCH_LOCAL); | |
238 | e = &s->entries[arch][key & FW_CFG_ENTRY_MASK]; | |
239 | if (e->read_callback) { | |
3f8752b4 | 240 | e->read_callback(e->callback_opaque); |
3bef7e8a | 241 | } |
3cce6243 BS |
242 | } |
243 | ||
f6e35343 | 244 | trace_fw_cfg_select(s, key, ret); |
3cce6243 BS |
245 | return ret; |
246 | } | |
247 | ||
38bf2093 GS |
248 | static uint64_t fw_cfg_data_read(void *opaque, hwaddr addr, unsigned size) |
249 | { | |
250 | FWCfgState *s = opaque; | |
251 | int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL); | |
252 | FWCfgEntry *e = (s->cur_entry == FW_CFG_INVALID) ? NULL : | |
253 | &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK]; | |
254 | uint64_t value = 0; | |
255 | ||
256 | assert(size > 0 && size <= sizeof(value)); | |
257 | if (s->cur_entry != FW_CFG_INVALID && e->data && s->cur_offset < e->len) { | |
258 | /* The least significant 'size' bytes of the return value are | |
259 | * expected to contain a string preserving portion of the item | |
260 | * data, padded with zeros on the right in case we run out early. | |
261 | * In technical terms, we're composing the host-endian representation | |
262 | * of the big endian interpretation of the fw_cfg string. | |
263 | */ | |
264 | do { | |
265 | value = (value << 8) | e->data[s->cur_offset++]; | |
266 | } while (--size && s->cur_offset < e->len); | |
267 | /* If size is still not zero, we *did* run out early, so continue | |
268 | * left-shifting, to add the appropriate number of padding zeros | |
269 | * on the right. | |
270 | */ | |
271 | value <<= 8 * size; | |
272 | } | |
273 | ||
274 | trace_fw_cfg_read(s, value); | |
275 | return value; | |
276 | } | |
277 | ||
a8170e5e | 278 | static void fw_cfg_data_mem_write(void *opaque, hwaddr addr, |
561e1827 | 279 | uint64_t value, unsigned size) |
3cce6243 | 280 | { |
cfaadf0e | 281 | FWCfgState *s = opaque; |
36b62ae6 | 282 | unsigned i = size; |
cfaadf0e | 283 | |
36b62ae6 LE |
284 | do { |
285 | fw_cfg_write(s, value >> (8 * --i)); | |
286 | } while (i); | |
cfaadf0e LE |
287 | } |
288 | ||
a4c0d1de MM |
289 | static void fw_cfg_dma_transfer(FWCfgState *s) |
290 | { | |
291 | dma_addr_t len; | |
292 | FWCfgDmaAccess dma; | |
293 | int arch; | |
294 | FWCfgEntry *e; | |
baf2d5bf | 295 | int read = 0, write = 0; |
a4c0d1de MM |
296 | dma_addr_t dma_addr; |
297 | ||
298 | /* Reset the address before the next access */ | |
299 | dma_addr = s->dma_addr; | |
300 | s->dma_addr = 0; | |
301 | ||
302 | if (dma_memory_read(s->dma_as, dma_addr, &dma, sizeof(dma))) { | |
303 | stl_be_dma(s->dma_as, dma_addr + offsetof(FWCfgDmaAccess, control), | |
304 | FW_CFG_DMA_CTL_ERROR); | |
305 | return; | |
306 | } | |
307 | ||
308 | dma.address = be64_to_cpu(dma.address); | |
309 | dma.length = be32_to_cpu(dma.length); | |
310 | dma.control = be32_to_cpu(dma.control); | |
311 | ||
312 | if (dma.control & FW_CFG_DMA_CTL_SELECT) { | |
313 | fw_cfg_select(s, dma.control >> 16); | |
314 | } | |
315 | ||
316 | arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL); | |
66f8fd9d GS |
317 | e = (s->cur_entry == FW_CFG_INVALID) ? NULL : |
318 | &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK]; | |
a4c0d1de MM |
319 | |
320 | if (dma.control & FW_CFG_DMA_CTL_READ) { | |
321 | read = 1; | |
baf2d5bf MT |
322 | write = 0; |
323 | } else if (dma.control & FW_CFG_DMA_CTL_WRITE) { | |
324 | read = 0; | |
325 | write = 1; | |
a4c0d1de MM |
326 | } else if (dma.control & FW_CFG_DMA_CTL_SKIP) { |
327 | read = 0; | |
baf2d5bf | 328 | write = 0; |
a4c0d1de MM |
329 | } else { |
330 | dma.length = 0; | |
331 | } | |
332 | ||
333 | dma.control = 0; | |
334 | ||
335 | while (dma.length > 0 && !(dma.control & FW_CFG_DMA_CTL_ERROR)) { | |
336 | if (s->cur_entry == FW_CFG_INVALID || !e->data || | |
337 | s->cur_offset >= e->len) { | |
338 | len = dma.length; | |
339 | ||
340 | /* If the access is not a read access, it will be a skip access, | |
341 | * tested before. | |
342 | */ | |
343 | if (read) { | |
344 | if (dma_memory_set(s->dma_as, dma.address, 0, len)) { | |
345 | dma.control |= FW_CFG_DMA_CTL_ERROR; | |
346 | } | |
347 | } | |
baf2d5bf MT |
348 | if (write) { |
349 | dma.control |= FW_CFG_DMA_CTL_ERROR; | |
350 | } | |
a4c0d1de MM |
351 | } else { |
352 | if (dma.length <= (e->len - s->cur_offset)) { | |
353 | len = dma.length; | |
354 | } else { | |
355 | len = (e->len - s->cur_offset); | |
356 | } | |
357 | ||
a4c0d1de MM |
358 | /* If the access is not a read access, it will be a skip access, |
359 | * tested before. | |
360 | */ | |
361 | if (read) { | |
362 | if (dma_memory_write(s->dma_as, dma.address, | |
363 | &e->data[s->cur_offset], len)) { | |
364 | dma.control |= FW_CFG_DMA_CTL_ERROR; | |
365 | } | |
366 | } | |
baf2d5bf MT |
367 | if (write) { |
368 | if (!e->allow_write || | |
369 | len != dma.length || | |
370 | dma_memory_read(s->dma_as, dma.address, | |
371 | &e->data[s->cur_offset], len)) { | |
372 | dma.control |= FW_CFG_DMA_CTL_ERROR; | |
373 | } | |
374 | } | |
a4c0d1de MM |
375 | |
376 | s->cur_offset += len; | |
377 | } | |
378 | ||
379 | dma.address += len; | |
380 | dma.length -= len; | |
381 | ||
382 | } | |
383 | ||
384 | stl_be_dma(s->dma_as, dma_addr + offsetof(FWCfgDmaAccess, control), | |
385 | dma.control); | |
386 | ||
387 | trace_fw_cfg_read(s, 0); | |
388 | } | |
389 | ||
2cc06a88 KC |
390 | static uint64_t fw_cfg_dma_mem_read(void *opaque, hwaddr addr, |
391 | unsigned size) | |
392 | { | |
393 | /* Return a signature value (and handle various read sizes) */ | |
394 | return extract64(FW_CFG_DMA_SIGNATURE, (8 - addr - size) * 8, size * 8); | |
395 | } | |
396 | ||
a4c0d1de MM |
397 | static void fw_cfg_dma_mem_write(void *opaque, hwaddr addr, |
398 | uint64_t value, unsigned size) | |
399 | { | |
400 | FWCfgState *s = opaque; | |
401 | ||
402 | if (size == 4) { | |
403 | if (addr == 0) { | |
404 | /* FWCfgDmaAccess high address */ | |
405 | s->dma_addr = value << 32; | |
406 | } else if (addr == 4) { | |
407 | /* FWCfgDmaAccess low address */ | |
408 | s->dma_addr |= value; | |
409 | fw_cfg_dma_transfer(s); | |
410 | } | |
411 | } else if (size == 8 && addr == 0) { | |
412 | s->dma_addr = value; | |
413 | fw_cfg_dma_transfer(s); | |
414 | } | |
415 | } | |
416 | ||
417 | static bool fw_cfg_dma_mem_valid(void *opaque, hwaddr addr, | |
418 | unsigned size, bool is_write) | |
419 | { | |
2cc06a88 KC |
420 | return !is_write || ((size == 4 && (addr == 0 || addr == 4)) || |
421 | (size == 8 && addr == 0)); | |
a4c0d1de MM |
422 | } |
423 | ||
cfaadf0e LE |
424 | static bool fw_cfg_data_mem_valid(void *opaque, hwaddr addr, |
425 | unsigned size, bool is_write) | |
426 | { | |
427 | return addr == 0; | |
3cce6243 BS |
428 | } |
429 | ||
a8170e5e | 430 | static void fw_cfg_ctl_mem_write(void *opaque, hwaddr addr, |
561e1827 | 431 | uint64_t value, unsigned size) |
3cce6243 BS |
432 | { |
433 | fw_cfg_select(opaque, (uint16_t)value); | |
434 | } | |
435 | ||
a8170e5e | 436 | static bool fw_cfg_ctl_mem_valid(void *opaque, hwaddr addr, |
561e1827 | 437 | unsigned size, bool is_write) |
3cce6243 | 438 | { |
561e1827 | 439 | return is_write && size == 2; |
3cce6243 BS |
440 | } |
441 | ||
a8170e5e | 442 | static void fw_cfg_comb_write(void *opaque, hwaddr addr, |
561e1827 | 443 | uint64_t value, unsigned size) |
3cce6243 | 444 | { |
561e1827 AK |
445 | switch (size) { |
446 | case 1: | |
447 | fw_cfg_write(opaque, (uint8_t)value); | |
448 | break; | |
449 | case 2: | |
450 | fw_cfg_select(opaque, (uint16_t)value); | |
451 | break; | |
452 | } | |
3cce6243 BS |
453 | } |
454 | ||
a8170e5e | 455 | static bool fw_cfg_comb_valid(void *opaque, hwaddr addr, |
561e1827 AK |
456 | unsigned size, bool is_write) |
457 | { | |
458 | return (size == 1) || (is_write && size == 2); | |
459 | } | |
3cce6243 | 460 | |
561e1827 AK |
461 | static const MemoryRegionOps fw_cfg_ctl_mem_ops = { |
462 | .write = fw_cfg_ctl_mem_write, | |
d789c845 | 463 | .endianness = DEVICE_BIG_ENDIAN, |
561e1827 | 464 | .valid.accepts = fw_cfg_ctl_mem_valid, |
3cce6243 BS |
465 | }; |
466 | ||
561e1827 | 467 | static const MemoryRegionOps fw_cfg_data_mem_ops = { |
38bf2093 | 468 | .read = fw_cfg_data_read, |
561e1827 | 469 | .write = fw_cfg_data_mem_write, |
d789c845 | 470 | .endianness = DEVICE_BIG_ENDIAN, |
561e1827 AK |
471 | .valid = { |
472 | .min_access_size = 1, | |
473 | .max_access_size = 1, | |
cfaadf0e | 474 | .accepts = fw_cfg_data_mem_valid, |
561e1827 | 475 | }, |
3cce6243 BS |
476 | }; |
477 | ||
561e1827 | 478 | static const MemoryRegionOps fw_cfg_comb_mem_ops = { |
6c8d56a2 | 479 | .read = fw_cfg_data_read, |
561e1827 | 480 | .write = fw_cfg_comb_write, |
6fdf98f2 | 481 | .endianness = DEVICE_LITTLE_ENDIAN, |
561e1827 | 482 | .valid.accepts = fw_cfg_comb_valid, |
3cce6243 BS |
483 | }; |
484 | ||
a4c0d1de | 485 | static const MemoryRegionOps fw_cfg_dma_mem_ops = { |
2cc06a88 | 486 | .read = fw_cfg_dma_mem_read, |
a4c0d1de MM |
487 | .write = fw_cfg_dma_mem_write, |
488 | .endianness = DEVICE_BIG_ENDIAN, | |
489 | .valid.accepts = fw_cfg_dma_mem_valid, | |
490 | .valid.max_access_size = 8, | |
491 | .impl.max_access_size = 8, | |
492 | }; | |
493 | ||
3a5c16fc | 494 | static void fw_cfg_reset(DeviceState *d) |
3cce6243 | 495 | { |
2ce92a11 | 496 | FWCfgState *s = FW_CFG(d); |
3cce6243 | 497 | |
3bef7e8a GS |
498 | /* we never register a read callback for FW_CFG_SIGNATURE */ |
499 | fw_cfg_select(s, FW_CFG_SIGNATURE); | |
3cce6243 BS |
500 | } |
501 | ||
ff06108b JQ |
502 | /* Save restore 32 bit int as uint16_t |
503 | This is a Big hack, but it is how the old state did it. | |
504 | Or we broke compatibility in the state, or we can't use struct tm | |
505 | */ | |
506 | ||
2c21ee76 JD |
507 | static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size, |
508 | VMStateField *field) | |
ff06108b JQ |
509 | { |
510 | uint32_t *v = pv; | |
511 | *v = qemu_get_be16(f); | |
512 | return 0; | |
513 | } | |
514 | ||
2c21ee76 JD |
515 | static int put_unused(QEMUFile *f, void *pv, size_t size, VMStateField *field, |
516 | QJSON *vmdesc) | |
ff06108b | 517 | { |
66c80e75 | 518 | fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n"); |
ff06108b | 519 | fprintf(stderr, "This functions shouldn't be called.\n"); |
2c21ee76 JD |
520 | |
521 | return 0; | |
ff06108b JQ |
522 | } |
523 | ||
d05ac8fa | 524 | static const VMStateInfo vmstate_hack_uint32_as_uint16 = { |
ff06108b JQ |
525 | .name = "int32_as_uint16", |
526 | .get = get_uint32_as_uint16, | |
527 | .put = put_unused, | |
528 | }; | |
529 | ||
530 | #define VMSTATE_UINT16_HACK(_f, _s, _t) \ | |
531 | VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t) | |
532 | ||
533 | ||
534 | static bool is_version_1(void *opaque, int version_id) | |
535 | { | |
536 | return version_id == 1; | |
537 | } | |
538 | ||
b2a575a1 | 539 | bool fw_cfg_dma_enabled(void *opaque) |
a4c0d1de MM |
540 | { |
541 | FWCfgState *s = opaque; | |
542 | ||
543 | return s->dma_enabled; | |
544 | } | |
545 | ||
546 | static const VMStateDescription vmstate_fw_cfg_dma = { | |
547 | .name = "fw_cfg/dma", | |
548 | .needed = fw_cfg_dma_enabled, | |
549 | .fields = (VMStateField[]) { | |
550 | VMSTATE_UINT64(dma_addr, FWCfgState), | |
551 | VMSTATE_END_OF_LIST() | |
552 | }, | |
553 | }; | |
554 | ||
7d2edd40 JQ |
555 | static const VMStateDescription vmstate_fw_cfg = { |
556 | .name = "fw_cfg", | |
ff06108b | 557 | .version_id = 2, |
7d2edd40 | 558 | .minimum_version_id = 1, |
d49805ae | 559 | .fields = (VMStateField[]) { |
7d2edd40 | 560 | VMSTATE_UINT16(cur_entry, FWCfgState), |
ff06108b JQ |
561 | VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1), |
562 | VMSTATE_UINT32_V(cur_offset, FWCfgState, 2), | |
7d2edd40 | 563 | VMSTATE_END_OF_LIST() |
a4c0d1de MM |
564 | }, |
565 | .subsections = (const VMStateDescription*[]) { | |
566 | &vmstate_fw_cfg_dma, | |
567 | NULL, | |
7d2edd40 JQ |
568 | } |
569 | }; | |
3cce6243 | 570 | |
d87072ce MT |
571 | static void fw_cfg_add_bytes_read_callback(FWCfgState *s, uint16_t key, |
572 | FWCfgReadCallback callback, | |
573 | void *callback_opaque, | |
baf2d5bf MT |
574 | void *data, size_t len, |
575 | bool read_only) | |
3cce6243 | 576 | { |
3cce6243 BS |
577 | int arch = !!(key & FW_CFG_ARCH_LOCAL); |
578 | ||
579 | key &= FW_CFG_ENTRY_MASK; | |
580 | ||
e12f3a13 | 581 | assert(key < fw_cfg_max_entry(s) && len < UINT32_MAX); |
0f9b2141 | 582 | assert(s->entries[arch][key].data == NULL); /* avoid key conflict */ |
3cce6243 BS |
583 | |
584 | s->entries[arch][key].data = data; | |
089da572 | 585 | s->entries[arch][key].len = (uint32_t)len; |
d87072ce MT |
586 | s->entries[arch][key].read_callback = callback; |
587 | s->entries[arch][key].callback_opaque = callback_opaque; | |
baf2d5bf | 588 | s->entries[arch][key].allow_write = !read_only; |
d87072ce MT |
589 | } |
590 | ||
bdbb5b17 GA |
591 | static void *fw_cfg_modify_bytes_read(FWCfgState *s, uint16_t key, |
592 | void *data, size_t len) | |
593 | { | |
594 | void *ptr; | |
595 | int arch = !!(key & FW_CFG_ARCH_LOCAL); | |
596 | ||
597 | key &= FW_CFG_ENTRY_MASK; | |
598 | ||
e12f3a13 | 599 | assert(key < fw_cfg_max_entry(s) && len < UINT32_MAX); |
bdbb5b17 GA |
600 | |
601 | /* return the old data to the function caller, avoid memory leak */ | |
602 | ptr = s->entries[arch][key].data; | |
603 | s->entries[arch][key].data = data; | |
604 | s->entries[arch][key].len = len; | |
605 | s->entries[arch][key].callback_opaque = NULL; | |
baf2d5bf | 606 | s->entries[arch][key].allow_write = false; |
bdbb5b17 GA |
607 | |
608 | return ptr; | |
609 | } | |
610 | ||
d87072ce MT |
611 | void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len) |
612 | { | |
baf2d5bf | 613 | fw_cfg_add_bytes_read_callback(s, key, NULL, NULL, data, len, true); |
3cce6243 BS |
614 | } |
615 | ||
44687f75 MA |
616 | void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value) |
617 | { | |
618 | size_t sz = strlen(value) + 1; | |
619 | ||
e7ae771f | 620 | fw_cfg_add_bytes(s, key, g_memdup(value, sz), sz); |
44687f75 MA |
621 | } |
622 | ||
4cad3867 | 623 | void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value) |
3cce6243 BS |
624 | { |
625 | uint16_t *copy; | |
626 | ||
7267c094 | 627 | copy = g_malloc(sizeof(value)); |
3cce6243 | 628 | *copy = cpu_to_le16(value); |
089da572 | 629 | fw_cfg_add_bytes(s, key, copy, sizeof(value)); |
3cce6243 BS |
630 | } |
631 | ||
1edd34b6 GS |
632 | void fw_cfg_modify_i16(FWCfgState *s, uint16_t key, uint16_t value) |
633 | { | |
634 | uint16_t *copy, *old; | |
635 | ||
636 | copy = g_malloc(sizeof(value)); | |
637 | *copy = cpu_to_le16(value); | |
638 | old = fw_cfg_modify_bytes_read(s, key, copy, sizeof(value)); | |
639 | g_free(old); | |
640 | } | |
641 | ||
4cad3867 | 642 | void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value) |
3cce6243 BS |
643 | { |
644 | uint32_t *copy; | |
645 | ||
7267c094 | 646 | copy = g_malloc(sizeof(value)); |
3cce6243 | 647 | *copy = cpu_to_le32(value); |
089da572 | 648 | fw_cfg_add_bytes(s, key, copy, sizeof(value)); |
3cce6243 BS |
649 | } |
650 | ||
4cad3867 | 651 | void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value) |
3cce6243 BS |
652 | { |
653 | uint64_t *copy; | |
654 | ||
7267c094 | 655 | copy = g_malloc(sizeof(value)); |
3cce6243 | 656 | *copy = cpu_to_le64(value); |
089da572 | 657 | fw_cfg_add_bytes(s, key, copy, sizeof(value)); |
3cce6243 BS |
658 | } |
659 | ||
bab47d9a GH |
660 | void fw_cfg_set_order_override(FWCfgState *s, int order) |
661 | { | |
662 | assert(s->fw_cfg_order_override == 0); | |
663 | s->fw_cfg_order_override = order; | |
664 | } | |
665 | ||
666 | void fw_cfg_reset_order_override(FWCfgState *s) | |
667 | { | |
668 | assert(s->fw_cfg_order_override != 0); | |
669 | s->fw_cfg_order_override = 0; | |
670 | } | |
671 | ||
672 | /* | |
673 | * This is the legacy order list. For legacy systems, files are in | |
674 | * the fw_cfg in the order defined below, by the "order" value. Note | |
675 | * that some entries (VGA ROMs, NIC option ROMS, etc.) go into a | |
676 | * specific area, but there may be more than one and they occur in the | |
677 | * order that the user specifies them on the command line. Those are | |
678 | * handled in a special manner, using the order override above. | |
679 | * | |
680 | * For non-legacy, the files are sorted by filename to avoid this kind | |
681 | * of complexity in the future. | |
682 | * | |
683 | * This is only for x86, other arches don't implement versioning so | |
684 | * they won't set legacy mode. | |
685 | */ | |
686 | static struct { | |
687 | const char *name; | |
688 | int order; | |
689 | } fw_cfg_order[] = { | |
690 | { "etc/boot-menu-wait", 10 }, | |
691 | { "bootsplash.jpg", 11 }, | |
692 | { "bootsplash.bmp", 12 }, | |
693 | { "etc/boot-fail-wait", 15 }, | |
694 | { "etc/smbios/smbios-tables", 20 }, | |
695 | { "etc/smbios/smbios-anchor", 30 }, | |
696 | { "etc/e820", 40 }, | |
697 | { "etc/reserved-memory-end", 50 }, | |
698 | { "genroms/kvmvapic.bin", 55 }, | |
699 | { "genroms/linuxboot.bin", 60 }, | |
700 | { }, /* VGA ROMs from pc_vga_init come here, 70. */ | |
701 | { }, /* NIC option ROMs from pc_nic_init come here, 80. */ | |
702 | { "etc/system-states", 90 }, | |
703 | { }, /* User ROMs come here, 100. */ | |
704 | { }, /* Device FW comes here, 110. */ | |
705 | { "etc/extra-pci-roots", 120 }, | |
706 | { "etc/acpi/tables", 130 }, | |
707 | { "etc/table-loader", 140 }, | |
708 | { "etc/tpm/log", 150 }, | |
709 | { "etc/acpi/rsdp", 160 }, | |
710 | { "bootorder", 170 }, | |
711 | ||
712 | #define FW_CFG_ORDER_OVERRIDE_LAST 200 | |
713 | }; | |
714 | ||
715 | static int get_fw_cfg_order(FWCfgState *s, const char *name) | |
716 | { | |
717 | int i; | |
718 | ||
a8d38f3b C |
719 | if (s->fw_cfg_order_override > 0) { |
720 | return s->fw_cfg_order_override; | |
721 | } | |
bab47d9a GH |
722 | |
723 | for (i = 0; i < ARRAY_SIZE(fw_cfg_order); i++) { | |
a8d38f3b C |
724 | if (fw_cfg_order[i].name == NULL) { |
725 | continue; | |
726 | } | |
727 | ||
728 | if (strcmp(name, fw_cfg_order[i].name) == 0) { | |
729 | return fw_cfg_order[i].order; | |
730 | } | |
bab47d9a | 731 | } |
a8d38f3b | 732 | |
bab47d9a | 733 | /* Stick unknown stuff at the end. */ |
3dc6f869 | 734 | warn_report("Unknown firmware file in legacy mode: %s", name); |
bab47d9a GH |
735 | return FW_CFG_ORDER_OVERRIDE_LAST; |
736 | } | |
737 | ||
d87072ce MT |
738 | void fw_cfg_add_file_callback(FWCfgState *s, const char *filename, |
739 | FWCfgReadCallback callback, void *callback_opaque, | |
baf2d5bf | 740 | void *data, size_t len, bool read_only) |
abe147e0 | 741 | { |
bab47d9a | 742 | int i, index, count; |
089da572 | 743 | size_t dsize; |
bab47d9a GH |
744 | MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine()); |
745 | int order = 0; | |
abe147e0 GH |
746 | |
747 | if (!s->files) { | |
e12f3a13 | 748 | dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * fw_cfg_file_slots(s); |
7267c094 | 749 | s->files = g_malloc0(dsize); |
089da572 | 750 | fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, s->files, dsize); |
abe147e0 GH |
751 | } |
752 | ||
bab47d9a | 753 | count = be32_to_cpu(s->files->count); |
e12f3a13 | 754 | assert(count < fw_cfg_file_slots(s)); |
bab47d9a GH |
755 | |
756 | /* Find the insertion point. */ | |
757 | if (mc->legacy_fw_cfg_order) { | |
758 | /* | |
759 | * Sort by order. For files with the same order, we keep them | |
760 | * in the sequence in which they were added. | |
761 | */ | |
762 | order = get_fw_cfg_order(s, filename); | |
763 | for (index = count; | |
764 | index > 0 && order < s->entry_order[index - 1]; | |
765 | index--); | |
766 | } else { | |
767 | /* Sort by file name. */ | |
768 | for (index = count; | |
769 | index > 0 && strcmp(filename, s->files->f[index - 1].name) < 0; | |
770 | index--); | |
771 | } | |
772 | ||
773 | /* | |
774 | * Move all the entries from the index point and after down one | |
775 | * to create a slot for the new entry. Because calculations are | |
776 | * being done with the index, make it so that "i" is the current | |
777 | * index and "i - 1" is the one being copied from, thus the | |
778 | * unusual start and end in the for statement. | |
779 | */ | |
780 | for (i = count + 1; i > index; i--) { | |
781 | s->files->f[i] = s->files->f[i - 1]; | |
782 | s->files->f[i].select = cpu_to_be16(FW_CFG_FILE_FIRST + i); | |
783 | s->entries[0][FW_CFG_FILE_FIRST + i] = | |
784 | s->entries[0][FW_CFG_FILE_FIRST + i - 1]; | |
785 | s->entry_order[i] = s->entry_order[i - 1]; | |
786 | } | |
787 | ||
788 | memset(&s->files->f[index], 0, sizeof(FWCfgFile)); | |
789 | memset(&s->entries[0][FW_CFG_FILE_FIRST + index], 0, sizeof(FWCfgEntry)); | |
abe147e0 | 790 | |
bab47d9a GH |
791 | pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name), filename); |
792 | for (i = 0; i <= count; i++) { | |
793 | if (i != index && | |
794 | strcmp(s->files->f[index].name, s->files->f[i].name) == 0) { | |
0eb973f9 GS |
795 | error_report("duplicate fw_cfg file name: %s", |
796 | s->files->f[index].name); | |
797 | exit(1); | |
de9352bc | 798 | } |
abe147e0 | 799 | } |
de9352bc | 800 | |
0eb973f9 | 801 | fw_cfg_add_bytes_read_callback(s, FW_CFG_FILE_FIRST + index, |
baf2d5bf MT |
802 | callback, callback_opaque, data, len, |
803 | read_only); | |
0eb973f9 | 804 | |
abe147e0 GH |
805 | s->files->f[index].size = cpu_to_be32(len); |
806 | s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index); | |
bab47d9a | 807 | s->entry_order[index] = order; |
f6e35343 | 808 | trace_fw_cfg_add_file(s, index, s->files->f[index].name, len); |
abe147e0 | 809 | |
bab47d9a | 810 | s->files->count = cpu_to_be32(count+1); |
abe147e0 GH |
811 | } |
812 | ||
d87072ce MT |
813 | void fw_cfg_add_file(FWCfgState *s, const char *filename, |
814 | void *data, size_t len) | |
815 | { | |
baf2d5bf | 816 | fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len, true); |
d87072ce MT |
817 | } |
818 | ||
bdbb5b17 GA |
819 | void *fw_cfg_modify_file(FWCfgState *s, const char *filename, |
820 | void *data, size_t len) | |
821 | { | |
822 | int i, index; | |
f3b37668 | 823 | void *ptr = NULL; |
bdbb5b17 GA |
824 | |
825 | assert(s->files); | |
826 | ||
827 | index = be32_to_cpu(s->files->count); | |
e12f3a13 | 828 | assert(index < fw_cfg_file_slots(s)); |
bdbb5b17 GA |
829 | |
830 | for (i = 0; i < index; i++) { | |
831 | if (strcmp(filename, s->files->f[i].name) == 0) { | |
f3b37668 GA |
832 | ptr = fw_cfg_modify_bytes_read(s, FW_CFG_FILE_FIRST + i, |
833 | data, len); | |
834 | s->files->f[i].size = cpu_to_be32(len); | |
835 | return ptr; | |
bdbb5b17 GA |
836 | } |
837 | } | |
838 | /* add new one */ | |
baf2d5bf | 839 | fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len, true); |
bdbb5b17 GA |
840 | return NULL; |
841 | } | |
842 | ||
843 | static void fw_cfg_machine_reset(void *opaque) | |
962630f2 | 844 | { |
bdbb5b17 | 845 | void *ptr; |
0e7a7592 | 846 | size_t len; |
bdbb5b17 | 847 | FWCfgState *s = opaque; |
30e32af7 | 848 | char *bootindex = get_boot_devices_list(&len, false); |
962630f2 | 849 | |
bdbb5b17 GA |
850 | ptr = fw_cfg_modify_file(s, "bootorder", (uint8_t *)bootindex, len); |
851 | g_free(ptr); | |
852 | } | |
853 | ||
854 | static void fw_cfg_machine_ready(struct Notifier *n, void *data) | |
855 | { | |
856 | FWCfgState *s = container_of(n, FWCfgState, machine_ready); | |
857 | qemu_register_reset(fw_cfg_machine_reset, s); | |
962630f2 GN |
858 | } |
859 | ||
3cce6243 | 860 | |
3a5c16fc | 861 | |
38f3adc3 | 862 | static void fw_cfg_common_realize(DeviceState *dev, Error **errp) |
5712db6a LE |
863 | { |
864 | FWCfgState *s = FW_CFG(dev); | |
cfc58cf3 | 865 | MachineState *machine = MACHINE(qdev_get_machine()); |
3c1aa733 | 866 | uint32_t version = FW_CFG_VERSION; |
3cce6243 | 867 | |
38f3adc3 MCA |
868 | if (!fw_cfg_find()) { |
869 | error_setg(errp, "at most one %s device is permitted", TYPE_FW_CFG); | |
870 | return; | |
871 | } | |
10a584b2 | 872 | |
089da572 | 873 | fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (char *)"QEMU", 4); |
9c5ce8db | 874 | fw_cfg_add_bytes(s, FW_CFG_UUID, &qemu_uuid, 16); |
cfc58cf3 | 875 | fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)!machine->enable_graphics); |
95387491 | 876 | fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu); |
3d3b8303 | 877 | fw_cfg_bootsplash(s); |
ac05f349 | 878 | fw_cfg_reboot(s); |
962630f2 | 879 | |
3c1aa733 MCA |
880 | if (s->dma_enabled) { |
881 | version |= FW_CFG_VERSION_DMA; | |
882 | } | |
883 | ||
884 | fw_cfg_add_i32(s, FW_CFG_ID, version); | |
885 | ||
962630f2 GN |
886 | s->machine_ready.notify = fw_cfg_machine_ready; |
887 | qemu_add_machine_init_done_notifier(&s->machine_ready); | |
3cce6243 | 888 | } |
3a5c16fc | 889 | |
a4c0d1de MM |
890 | FWCfgState *fw_cfg_init_io_dma(uint32_t iobase, uint32_t dma_iobase, |
891 | AddressSpace *dma_as) | |
3a5c16fc | 892 | { |
5712db6a | 893 | DeviceState *dev; |
91685323 MCA |
894 | SysBusDevice *sbd; |
895 | FWCfgIoState *ios; | |
a4c0d1de | 896 | FWCfgState *s; |
e6915b5f | 897 | bool dma_requested = dma_iobase && dma_as; |
3a5c16fc | 898 | |
5712db6a | 899 | dev = qdev_create(NULL, TYPE_FW_CFG_IO); |
e6915b5f LE |
900 | if (!dma_requested) { |
901 | qdev_prop_set_bit(dev, "dma_enabled", false); | |
902 | } | |
a4c0d1de | 903 | |
38f3adc3 MCA |
904 | object_property_add_child(OBJECT(qdev_get_machine()), TYPE_FW_CFG, |
905 | OBJECT(dev), NULL); | |
906 | qdev_init_nofail(dev); | |
91685323 MCA |
907 | |
908 | sbd = SYS_BUS_DEVICE(dev); | |
909 | ios = FW_CFG_IO(dev); | |
910 | sysbus_add_io(sbd, iobase, &ios->comb_iomem); | |
911 | ||
a4c0d1de MM |
912 | s = FW_CFG(dev); |
913 | ||
e6915b5f | 914 | if (s->dma_enabled) { |
a4c0d1de MM |
915 | /* 64 bits for the address field */ |
916 | s->dma_as = dma_as; | |
917 | s->dma_addr = 0; | |
91685323 | 918 | sysbus_add_io(sbd, dma_iobase, &s->dma_iomem); |
a4c0d1de MM |
919 | } |
920 | ||
a4c0d1de MM |
921 | return s; |
922 | } | |
923 | ||
924 | FWCfgState *fw_cfg_init_io(uint32_t iobase) | |
925 | { | |
926 | return fw_cfg_init_io_dma(iobase, 0, NULL); | |
56383955 HT |
927 | } |
928 | ||
a4c0d1de MM |
929 | FWCfgState *fw_cfg_init_mem_wide(hwaddr ctl_addr, |
930 | hwaddr data_addr, uint32_t data_width, | |
931 | hwaddr dma_addr, AddressSpace *dma_as) | |
56383955 | 932 | { |
5712db6a LE |
933 | DeviceState *dev; |
934 | SysBusDevice *sbd; | |
a4c0d1de | 935 | FWCfgState *s; |
e6915b5f | 936 | bool dma_requested = dma_addr && dma_as; |
56383955 | 937 | |
5712db6a | 938 | dev = qdev_create(NULL, TYPE_FW_CFG_MEM); |
6c87e3d5 | 939 | qdev_prop_set_uint32(dev, "data_width", data_width); |
e6915b5f LE |
940 | if (!dma_requested) { |
941 | qdev_prop_set_bit(dev, "dma_enabled", false); | |
942 | } | |
cfaadf0e | 943 | |
38f3adc3 MCA |
944 | object_property_add_child(OBJECT(qdev_get_machine()), TYPE_FW_CFG, |
945 | OBJECT(dev), NULL); | |
946 | qdev_init_nofail(dev); | |
5712db6a LE |
947 | |
948 | sbd = SYS_BUS_DEVICE(dev); | |
949 | sysbus_mmio_map(sbd, 0, ctl_addr); | |
950 | sysbus_mmio_map(sbd, 1, data_addr); | |
951 | ||
a4c0d1de MM |
952 | s = FW_CFG(dev); |
953 | ||
e6915b5f | 954 | if (s->dma_enabled) { |
a4c0d1de MM |
955 | s->dma_as = dma_as; |
956 | s->dma_addr = 0; | |
957 | sysbus_mmio_map(sbd, 2, dma_addr); | |
a4c0d1de MM |
958 | } |
959 | ||
a4c0d1de | 960 | return s; |
5712db6a LE |
961 | } |
962 | ||
6c87e3d5 LE |
963 | FWCfgState *fw_cfg_init_mem(hwaddr ctl_addr, hwaddr data_addr) |
964 | { | |
965 | return fw_cfg_init_mem_wide(ctl_addr, data_addr, | |
a4c0d1de MM |
966 | fw_cfg_data_mem_ops.valid.max_access_size, |
967 | 0, NULL); | |
6c87e3d5 LE |
968 | } |
969 | ||
5712db6a | 970 | |
600c60b7 MT |
971 | FWCfgState *fw_cfg_find(void) |
972 | { | |
6e99c075 MCA |
973 | /* Returns NULL unless there is exactly one fw_cfg device */ |
974 | return FW_CFG(object_resolve_path_type("", TYPE_FW_CFG, NULL)); | |
600c60b7 MT |
975 | } |
976 | ||
38f3adc3 | 977 | |
999e12bb AL |
978 | static void fw_cfg_class_init(ObjectClass *klass, void *data) |
979 | { | |
39bffca2 | 980 | DeviceClass *dc = DEVICE_CLASS(klass); |
999e12bb | 981 | |
39bffca2 AL |
982 | dc->reset = fw_cfg_reset; |
983 | dc->vmsd = &vmstate_fw_cfg; | |
999e12bb AL |
984 | } |
985 | ||
8c43a6f0 | 986 | static const TypeInfo fw_cfg_info = { |
600c60b7 | 987 | .name = TYPE_FW_CFG, |
39bffca2 | 988 | .parent = TYPE_SYS_BUS_DEVICE, |
e061fa3c | 989 | .abstract = true, |
39bffca2 AL |
990 | .instance_size = sizeof(FWCfgState), |
991 | .class_init = fw_cfg_class_init, | |
3a5c16fc BS |
992 | }; |
993 | ||
e12f3a13 LE |
994 | static void fw_cfg_file_slots_allocate(FWCfgState *s, Error **errp) |
995 | { | |
996 | uint16_t file_slots_max; | |
997 | ||
998 | if (fw_cfg_file_slots(s) < FW_CFG_FILE_SLOTS_MIN) { | |
999 | error_setg(errp, "\"file_slots\" must be at least 0x%x", | |
1000 | FW_CFG_FILE_SLOTS_MIN); | |
1001 | return; | |
1002 | } | |
1003 | ||
1004 | /* (UINT16_MAX & FW_CFG_ENTRY_MASK) is the highest inclusive selector value | |
1005 | * that we permit. The actual (exclusive) value coming from the | |
1006 | * configuration is (FW_CFG_FILE_FIRST + fw_cfg_file_slots(s)). */ | |
1007 | file_slots_max = (UINT16_MAX & FW_CFG_ENTRY_MASK) - FW_CFG_FILE_FIRST + 1; | |
1008 | if (fw_cfg_file_slots(s) > file_slots_max) { | |
1009 | error_setg(errp, "\"file_slots\" must not exceed 0x%" PRIx16, | |
1010 | file_slots_max); | |
1011 | return; | |
1012 | } | |
1013 | ||
1014 | s->entries[0] = g_new0(FWCfgEntry, fw_cfg_max_entry(s)); | |
1015 | s->entries[1] = g_new0(FWCfgEntry, fw_cfg_max_entry(s)); | |
1016 | s->entry_order = g_new0(int, fw_cfg_max_entry(s)); | |
1017 | } | |
5712db6a LE |
1018 | |
1019 | static Property fw_cfg_io_properties[] = { | |
a4c0d1de | 1020 | DEFINE_PROP_BOOL("dma_enabled", FWCfgIoState, parent_obj.dma_enabled, |
e6915b5f | 1021 | true), |
e12f3a13 | 1022 | DEFINE_PROP_UINT16("x-file-slots", FWCfgIoState, parent_obj.file_slots, |
a5b3ebfd | 1023 | FW_CFG_FILE_SLOTS_DFLT), |
5712db6a LE |
1024 | DEFINE_PROP_END_OF_LIST(), |
1025 | }; | |
1026 | ||
1027 | static void fw_cfg_io_realize(DeviceState *dev, Error **errp) | |
1028 | { | |
1029 | FWCfgIoState *s = FW_CFG_IO(dev); | |
e12f3a13 LE |
1030 | Error *local_err = NULL; |
1031 | ||
1032 | fw_cfg_file_slots_allocate(FW_CFG(s), &local_err); | |
1033 | if (local_err) { | |
1034 | error_propagate(errp, local_err); | |
1035 | return; | |
1036 | } | |
5712db6a | 1037 | |
ce9a2aa3 GS |
1038 | /* when using port i/o, the 8-bit data register ALWAYS overlaps |
1039 | * with half of the 16-bit control register. Hence, the total size | |
1040 | * of the i/o region used is FW_CFG_CTL_SIZE */ | |
5712db6a | 1041 | memory_region_init_io(&s->comb_iomem, OBJECT(s), &fw_cfg_comb_mem_ops, |
a4c0d1de | 1042 | FW_CFG(s), "fwcfg", FW_CFG_CTL_SIZE); |
a4c0d1de MM |
1043 | |
1044 | if (FW_CFG(s)->dma_enabled) { | |
1045 | memory_region_init_io(&FW_CFG(s)->dma_iomem, OBJECT(s), | |
1046 | &fw_cfg_dma_mem_ops, FW_CFG(s), "fwcfg.dma", | |
1047 | sizeof(dma_addr_t)); | |
a4c0d1de | 1048 | } |
38f3adc3 MCA |
1049 | |
1050 | fw_cfg_common_realize(dev, errp); | |
5712db6a LE |
1051 | } |
1052 | ||
1053 | static void fw_cfg_io_class_init(ObjectClass *klass, void *data) | |
1054 | { | |
1055 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1056 | ||
1057 | dc->realize = fw_cfg_io_realize; | |
1058 | dc->props = fw_cfg_io_properties; | |
1059 | } | |
1060 | ||
1061 | static const TypeInfo fw_cfg_io_info = { | |
1062 | .name = TYPE_FW_CFG_IO, | |
1063 | .parent = TYPE_FW_CFG, | |
1064 | .instance_size = sizeof(FWCfgIoState), | |
1065 | .class_init = fw_cfg_io_class_init, | |
1066 | }; | |
1067 | ||
1068 | ||
cfaadf0e LE |
1069 | static Property fw_cfg_mem_properties[] = { |
1070 | DEFINE_PROP_UINT32("data_width", FWCfgMemState, data_width, -1), | |
a4c0d1de | 1071 | DEFINE_PROP_BOOL("dma_enabled", FWCfgMemState, parent_obj.dma_enabled, |
e6915b5f | 1072 | true), |
e12f3a13 | 1073 | DEFINE_PROP_UINT16("x-file-slots", FWCfgMemState, parent_obj.file_slots, |
a5b3ebfd | 1074 | FW_CFG_FILE_SLOTS_DFLT), |
cfaadf0e LE |
1075 | DEFINE_PROP_END_OF_LIST(), |
1076 | }; | |
1077 | ||
5712db6a LE |
1078 | static void fw_cfg_mem_realize(DeviceState *dev, Error **errp) |
1079 | { | |
1080 | FWCfgMemState *s = FW_CFG_MEM(dev); | |
1081 | SysBusDevice *sbd = SYS_BUS_DEVICE(dev); | |
cfaadf0e | 1082 | const MemoryRegionOps *data_ops = &fw_cfg_data_mem_ops; |
e12f3a13 LE |
1083 | Error *local_err = NULL; |
1084 | ||
1085 | fw_cfg_file_slots_allocate(FW_CFG(s), &local_err); | |
1086 | if (local_err) { | |
1087 | error_propagate(errp, local_err); | |
1088 | return; | |
1089 | } | |
5712db6a LE |
1090 | |
1091 | memory_region_init_io(&s->ctl_iomem, OBJECT(s), &fw_cfg_ctl_mem_ops, | |
a4c0d1de | 1092 | FW_CFG(s), "fwcfg.ctl", FW_CFG_CTL_SIZE); |
5712db6a LE |
1093 | sysbus_init_mmio(sbd, &s->ctl_iomem); |
1094 | ||
cfaadf0e LE |
1095 | if (s->data_width > data_ops->valid.max_access_size) { |
1096 | /* memberwise copy because the "old_mmio" member is const */ | |
1097 | s->wide_data_ops.read = data_ops->read; | |
1098 | s->wide_data_ops.write = data_ops->write; | |
1099 | s->wide_data_ops.endianness = data_ops->endianness; | |
1100 | s->wide_data_ops.valid = data_ops->valid; | |
1101 | s->wide_data_ops.impl = data_ops->impl; | |
1102 | ||
1103 | s->wide_data_ops.valid.max_access_size = s->data_width; | |
1104 | s->wide_data_ops.impl.max_access_size = s->data_width; | |
1105 | data_ops = &s->wide_data_ops; | |
1106 | } | |
1107 | memory_region_init_io(&s->data_iomem, OBJECT(s), data_ops, FW_CFG(s), | |
1108 | "fwcfg.data", data_ops->valid.max_access_size); | |
5712db6a | 1109 | sysbus_init_mmio(sbd, &s->data_iomem); |
a4c0d1de MM |
1110 | |
1111 | if (FW_CFG(s)->dma_enabled) { | |
1112 | memory_region_init_io(&FW_CFG(s)->dma_iomem, OBJECT(s), | |
1113 | &fw_cfg_dma_mem_ops, FW_CFG(s), "fwcfg.dma", | |
1114 | sizeof(dma_addr_t)); | |
1115 | sysbus_init_mmio(sbd, &FW_CFG(s)->dma_iomem); | |
1116 | } | |
38f3adc3 MCA |
1117 | |
1118 | fw_cfg_common_realize(dev, errp); | |
5712db6a LE |
1119 | } |
1120 | ||
1121 | static void fw_cfg_mem_class_init(ObjectClass *klass, void *data) | |
1122 | { | |
1123 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1124 | ||
1125 | dc->realize = fw_cfg_mem_realize; | |
cfaadf0e | 1126 | dc->props = fw_cfg_mem_properties; |
5712db6a LE |
1127 | } |
1128 | ||
1129 | static const TypeInfo fw_cfg_mem_info = { | |
1130 | .name = TYPE_FW_CFG_MEM, | |
1131 | .parent = TYPE_FW_CFG, | |
1132 | .instance_size = sizeof(FWCfgMemState), | |
1133 | .class_init = fw_cfg_mem_class_init, | |
1134 | }; | |
1135 | ||
1136 | ||
83f7d43a | 1137 | static void fw_cfg_register_types(void) |
3a5c16fc | 1138 | { |
39bffca2 | 1139 | type_register_static(&fw_cfg_info); |
5712db6a LE |
1140 | type_register_static(&fw_cfg_io_info); |
1141 | type_register_static(&fw_cfg_mem_info); | |
3a5c16fc BS |
1142 | } |
1143 | ||
83f7d43a | 1144 | type_init(fw_cfg_register_types) |