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