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