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