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 */ 24 25 #include "qemu/osdep.h" 26 #include "qemu/datadir.h" 27 #include "sysemu/sysemu.h" 28 #include "sysemu/dma.h" 29 #include "sysemu/reset.h" 30 #include "hw/boards.h" 31 #include "hw/nvram/fw_cfg.h" 32 #include "hw/qdev-properties.h" 33 #include "hw/sysbus.h" 34 #include "migration/qemu-file-types.h" 35 #include "migration/vmstate.h" 36 #include "trace.h" 37 #include "qemu/error-report.h" 38 #include "qemu/option.h" 39 #include "qemu/config-file.h" 40 #include "qemu/cutils.h" 41 #include "qapi/error.h" 42 #include "hw/acpi/aml-build.h" 43 #include "hw/pci/pci_bus.h" 44 45 #define FW_CFG_FILE_SLOTS_DFLT 0x20 46 47 /* FW_CFG_VERSION bits */ 48 #define FW_CFG_VERSION 0x01 49 #define FW_CFG_VERSION_DMA 0x02 50 51 /* FW_CFG_DMA_CONTROL bits */ 52 #define FW_CFG_DMA_CTL_ERROR 0x01 53 #define FW_CFG_DMA_CTL_READ 0x02 54 #define FW_CFG_DMA_CTL_SKIP 0x04 55 #define FW_CFG_DMA_CTL_SELECT 0x08 56 #define FW_CFG_DMA_CTL_WRITE 0x10 57 58 #define FW_CFG_DMA_SIGNATURE 0x51454d5520434647ULL /* "QEMU CFG" */ 59 60 struct FWCfgEntry { 61 uint32_t len; 62 bool allow_write; 63 uint8_t *data; 64 void *callback_opaque; 65 FWCfgCallback select_cb; 66 FWCfgWriteCallback write_cb; 67 }; 68 69 /** 70 * key_name: 71 * 72 * @key: The uint16 selector key. 73 * 74 * Returns: The stringified name if the selector refers to a well-known 75 * numerically defined item, or NULL on key lookup failure. 76 */ 77 static const char *key_name(uint16_t key) 78 { 79 static const char *fw_cfg_wellknown_keys[FW_CFG_FILE_FIRST] = { 80 [FW_CFG_SIGNATURE] = "signature", 81 [FW_CFG_ID] = "id", 82 [FW_CFG_UUID] = "uuid", 83 [FW_CFG_RAM_SIZE] = "ram_size", 84 [FW_CFG_NOGRAPHIC] = "nographic", 85 [FW_CFG_NB_CPUS] = "nb_cpus", 86 [FW_CFG_MACHINE_ID] = "machine_id", 87 [FW_CFG_KERNEL_ADDR] = "kernel_addr", 88 [FW_CFG_KERNEL_SIZE] = "kernel_size", 89 [FW_CFG_KERNEL_CMDLINE] = "kernel_cmdline", 90 [FW_CFG_INITRD_ADDR] = "initrd_addr", 91 [FW_CFG_INITRD_SIZE] = "initdr_size", 92 [FW_CFG_BOOT_DEVICE] = "boot_device", 93 [FW_CFG_NUMA] = "numa", 94 [FW_CFG_BOOT_MENU] = "boot_menu", 95 [FW_CFG_MAX_CPUS] = "max_cpus", 96 [FW_CFG_KERNEL_ENTRY] = "kernel_entry", 97 [FW_CFG_KERNEL_DATA] = "kernel_data", 98 [FW_CFG_INITRD_DATA] = "initrd_data", 99 [FW_CFG_CMDLINE_ADDR] = "cmdline_addr", 100 [FW_CFG_CMDLINE_SIZE] = "cmdline_size", 101 [FW_CFG_CMDLINE_DATA] = "cmdline_data", 102 [FW_CFG_SETUP_ADDR] = "setup_addr", 103 [FW_CFG_SETUP_SIZE] = "setup_size", 104 [FW_CFG_SETUP_DATA] = "setup_data", 105 [FW_CFG_FILE_DIR] = "file_dir", 106 }; 107 108 if (key & FW_CFG_ARCH_LOCAL) { 109 return fw_cfg_arch_key_name(key); 110 } 111 if (key < FW_CFG_FILE_FIRST) { 112 return fw_cfg_wellknown_keys[key]; 113 } 114 115 return NULL; 116 } 117 118 static inline const char *trace_key_name(uint16_t key) 119 { 120 const char *name = key_name(key); 121 122 return name ? name : "unknown"; 123 } 124 125 #define JPG_FILE 0 126 #define BMP_FILE 1 127 128 static char *read_splashfile(char *filename, gsize *file_sizep, 129 int *file_typep) 130 { 131 GError *err = NULL; 132 gchar *content; 133 int file_type; 134 unsigned int filehead; 135 int bmp_bpp; 136 137 if (!g_file_get_contents(filename, &content, file_sizep, &err)) { 138 error_report("failed to read splash file '%s': %s", 139 filename, err->message); 140 g_error_free(err); 141 return NULL; 142 } 143 144 /* check file size */ 145 if (*file_sizep < 30) { 146 goto error; 147 } 148 149 /* check magic ID */ 150 filehead = lduw_le_p(content); 151 if (filehead == 0xd8ff) { 152 file_type = JPG_FILE; 153 } else if (filehead == 0x4d42) { 154 file_type = BMP_FILE; 155 } else { 156 goto error; 157 } 158 159 /* check BMP bpp */ 160 if (file_type == BMP_FILE) { 161 bmp_bpp = lduw_le_p(&content[28]); 162 if (bmp_bpp != 24) { 163 goto error; 164 } 165 } 166 167 /* return values */ 168 *file_typep = file_type; 169 170 return content; 171 172 error: 173 error_report("splash file '%s' format not recognized; must be JPEG " 174 "or 24 bit BMP", filename); 175 g_free(content); 176 return NULL; 177 } 178 179 static void fw_cfg_bootsplash(FWCfgState *s) 180 { 181 char *filename, *file_data; 182 gsize file_size; 183 int file_type; 184 185 /* insert splash time if user configurated */ 186 if (current_machine->boot_config.has_splash_time) { 187 int64_t bst_val = current_machine->boot_config.splash_time; 188 uint16_t bst_le16; 189 190 /* validate the input */ 191 if (bst_val < 0 || bst_val > 0xffff) { 192 error_report("splash-time is invalid," 193 "it should be a value between 0 and 65535"); 194 exit(1); 195 } 196 /* use little endian format */ 197 bst_le16 = cpu_to_le16(bst_val); 198 fw_cfg_add_file(s, "etc/boot-menu-wait", 199 g_memdup(&bst_le16, sizeof bst_le16), sizeof bst_le16); 200 } 201 202 /* insert splash file if user configurated */ 203 if (current_machine->boot_config.has_splash) { 204 const char *boot_splash_filename = current_machine->boot_config.splash; 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 } 210 211 /* loading file data */ 212 file_data = read_splashfile(filename, &file_size, &file_type); 213 if (file_data == NULL) { 214 g_free(filename); 215 return; 216 } 217 g_free(boot_splash_filedata); 218 boot_splash_filedata = (uint8_t *)file_data; 219 220 /* insert data */ 221 if (file_type == JPG_FILE) { 222 fw_cfg_add_file(s, "bootsplash.jpg", 223 boot_splash_filedata, file_size); 224 } else { 225 fw_cfg_add_file(s, "bootsplash.bmp", 226 boot_splash_filedata, file_size); 227 } 228 g_free(filename); 229 } 230 } 231 232 static void fw_cfg_reboot(FWCfgState *s) 233 { 234 uint64_t rt_val = -1; 235 uint32_t rt_le32; 236 237 if (current_machine->boot_config.has_reboot_timeout) { 238 rt_val = current_machine->boot_config.reboot_timeout; 239 240 /* validate the input */ 241 if (rt_val > 0xffff && rt_val != (uint64_t)-1) { 242 error_report("reboot timeout is invalid," 243 "it should be a value between -1 and 65535"); 244 exit(1); 245 } 246 } 247 248 rt_le32 = cpu_to_le32(rt_val); 249 fw_cfg_add_file(s, "etc/boot-fail-wait", g_memdup(&rt_le32, 4), 4); 250 } 251 252 static void fw_cfg_write(FWCfgState *s, uint8_t value) 253 { 254 /* nothing, write support removed in QEMU v2.4+ */ 255 } 256 257 static inline uint16_t fw_cfg_file_slots(const FWCfgState *s) 258 { 259 return s->file_slots; 260 } 261 262 /* Note: this function returns an exclusive limit. */ 263 static inline uint32_t fw_cfg_max_entry(const FWCfgState *s) 264 { 265 return FW_CFG_FILE_FIRST + fw_cfg_file_slots(s); 266 } 267 268 static int fw_cfg_select(FWCfgState *s, uint16_t key) 269 { 270 int arch, ret; 271 FWCfgEntry *e; 272 273 s->cur_offset = 0; 274 if ((key & FW_CFG_ENTRY_MASK) >= fw_cfg_max_entry(s)) { 275 s->cur_entry = FW_CFG_INVALID; 276 ret = 0; 277 } else { 278 s->cur_entry = key; 279 ret = 1; 280 /* entry successfully selected, now run callback if present */ 281 arch = !!(key & FW_CFG_ARCH_LOCAL); 282 e = &s->entries[arch][key & FW_CFG_ENTRY_MASK]; 283 if (e->select_cb) { 284 e->select_cb(e->callback_opaque); 285 } 286 } 287 288 trace_fw_cfg_select(s, key, trace_key_name(key), ret); 289 return ret; 290 } 291 292 static uint64_t fw_cfg_data_read(void *opaque, hwaddr addr, unsigned size) 293 { 294 FWCfgState *s = opaque; 295 int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL); 296 FWCfgEntry *e = (s->cur_entry == FW_CFG_INVALID) ? NULL : 297 &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK]; 298 uint64_t value = 0; 299 300 assert(size > 0 && size <= sizeof(value)); 301 if (s->cur_entry != FW_CFG_INVALID && e->data && s->cur_offset < e->len) { 302 /* The least significant 'size' bytes of the return value are 303 * expected to contain a string preserving portion of the item 304 * data, padded with zeros on the right in case we run out early. 305 * In technical terms, we're composing the host-endian representation 306 * of the big endian interpretation of the fw_cfg string. 307 */ 308 do { 309 value = (value << 8) | e->data[s->cur_offset++]; 310 } while (--size && s->cur_offset < e->len); 311 /* If size is still not zero, we *did* run out early, so continue 312 * left-shifting, to add the appropriate number of padding zeros 313 * on the right. 314 */ 315 value <<= 8 * size; 316 } 317 318 trace_fw_cfg_read(s, value); 319 return value; 320 } 321 322 static void fw_cfg_data_mem_write(void *opaque, hwaddr addr, 323 uint64_t value, unsigned size) 324 { 325 FWCfgState *s = opaque; 326 unsigned i = size; 327 328 do { 329 fw_cfg_write(s, value >> (8 * --i)); 330 } while (i); 331 } 332 333 static void fw_cfg_dma_transfer(FWCfgState *s) 334 { 335 dma_addr_t len; 336 FWCfgDmaAccess dma; 337 int arch; 338 FWCfgEntry *e; 339 int read = 0, write = 0; 340 dma_addr_t dma_addr; 341 342 /* Reset the address before the next access */ 343 dma_addr = s->dma_addr; 344 s->dma_addr = 0; 345 346 if (dma_memory_read(s->dma_as, dma_addr, 347 &dma, sizeof(dma), MEMTXATTRS_UNSPECIFIED)) { 348 stl_be_dma(s->dma_as, dma_addr + offsetof(FWCfgDmaAccess, control), 349 FW_CFG_DMA_CTL_ERROR, MEMTXATTRS_UNSPECIFIED); 350 return; 351 } 352 353 dma.address = be64_to_cpu(dma.address); 354 dma.length = be32_to_cpu(dma.length); 355 dma.control = be32_to_cpu(dma.control); 356 357 if (dma.control & FW_CFG_DMA_CTL_SELECT) { 358 fw_cfg_select(s, dma.control >> 16); 359 } 360 361 arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL); 362 e = (s->cur_entry == FW_CFG_INVALID) ? NULL : 363 &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK]; 364 365 if (dma.control & FW_CFG_DMA_CTL_READ) { 366 read = 1; 367 write = 0; 368 } else if (dma.control & FW_CFG_DMA_CTL_WRITE) { 369 read = 0; 370 write = 1; 371 } else if (dma.control & FW_CFG_DMA_CTL_SKIP) { 372 read = 0; 373 write = 0; 374 } else { 375 dma.length = 0; 376 } 377 378 dma.control = 0; 379 380 while (dma.length > 0 && !(dma.control & FW_CFG_DMA_CTL_ERROR)) { 381 if (s->cur_entry == FW_CFG_INVALID || !e->data || 382 s->cur_offset >= e->len) { 383 len = dma.length; 384 385 /* If the access is not a read access, it will be a skip access, 386 * tested before. 387 */ 388 if (read) { 389 if (dma_memory_set(s->dma_as, dma.address, 0, len, 390 MEMTXATTRS_UNSPECIFIED)) { 391 dma.control |= FW_CFG_DMA_CTL_ERROR; 392 } 393 } 394 if (write) { 395 dma.control |= FW_CFG_DMA_CTL_ERROR; 396 } 397 } else { 398 if (dma.length <= (e->len - s->cur_offset)) { 399 len = dma.length; 400 } else { 401 len = (e->len - s->cur_offset); 402 } 403 404 /* If the access is not a read access, it will be a skip access, 405 * tested before. 406 */ 407 if (read) { 408 if (dma_memory_write(s->dma_as, dma.address, 409 &e->data[s->cur_offset], len, 410 MEMTXATTRS_UNSPECIFIED)) { 411 dma.control |= FW_CFG_DMA_CTL_ERROR; 412 } 413 } 414 if (write) { 415 if (!e->allow_write || 416 len != dma.length || 417 dma_memory_read(s->dma_as, dma.address, 418 &e->data[s->cur_offset], len, 419 MEMTXATTRS_UNSPECIFIED)) { 420 dma.control |= FW_CFG_DMA_CTL_ERROR; 421 } else if (e->write_cb) { 422 e->write_cb(e->callback_opaque, s->cur_offset, len); 423 } 424 } 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, MEMTXATTRS_UNSPECIFIED); 436 437 trace_fw_cfg_read(s, 0); 438 } 439 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 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 MemTxAttrs attrs) 470 { 471 return !is_write || ((size == 4 && (addr == 0 || addr == 4)) || 472 (size == 8 && addr == 0)); 473 } 474 475 static bool fw_cfg_data_mem_valid(void *opaque, hwaddr addr, 476 unsigned size, bool is_write, 477 MemTxAttrs attrs) 478 { 479 return addr == 0; 480 } 481 482 static uint64_t fw_cfg_ctl_mem_read(void *opaque, hwaddr addr, unsigned size) 483 { 484 return 0; 485 } 486 487 static void fw_cfg_ctl_mem_write(void *opaque, hwaddr addr, 488 uint64_t value, unsigned size) 489 { 490 fw_cfg_select(opaque, (uint16_t)value); 491 } 492 493 static bool fw_cfg_ctl_mem_valid(void *opaque, hwaddr addr, 494 unsigned size, bool is_write, 495 MemTxAttrs attrs) 496 { 497 return is_write && size == 2; 498 } 499 500 static void fw_cfg_comb_write(void *opaque, hwaddr addr, 501 uint64_t value, unsigned size) 502 { 503 switch (size) { 504 case 1: 505 fw_cfg_write(opaque, (uint8_t)value); 506 break; 507 case 2: 508 fw_cfg_select(opaque, (uint16_t)value); 509 break; 510 } 511 } 512 513 static bool fw_cfg_comb_valid(void *opaque, hwaddr addr, 514 unsigned size, bool is_write, 515 MemTxAttrs attrs) 516 { 517 return (size == 1) || (is_write && size == 2); 518 } 519 520 static const MemoryRegionOps fw_cfg_ctl_mem_ops = { 521 .read = fw_cfg_ctl_mem_read, 522 .write = fw_cfg_ctl_mem_write, 523 .endianness = DEVICE_BIG_ENDIAN, 524 .valid.accepts = fw_cfg_ctl_mem_valid, 525 }; 526 527 static const MemoryRegionOps fw_cfg_data_mem_ops = { 528 .read = fw_cfg_data_read, 529 .write = fw_cfg_data_mem_write, 530 .endianness = DEVICE_BIG_ENDIAN, 531 .valid = { 532 .min_access_size = 1, 533 .max_access_size = 1, 534 .accepts = fw_cfg_data_mem_valid, 535 }, 536 }; 537 538 static const MemoryRegionOps fw_cfg_comb_mem_ops = { 539 .read = fw_cfg_data_read, 540 .write = fw_cfg_comb_write, 541 .endianness = DEVICE_LITTLE_ENDIAN, 542 .valid.accepts = fw_cfg_comb_valid, 543 }; 544 545 static const MemoryRegionOps fw_cfg_dma_mem_ops = { 546 .read = fw_cfg_dma_mem_read, 547 .write = fw_cfg_dma_mem_write, 548 .endianness = DEVICE_BIG_ENDIAN, 549 .valid.accepts = fw_cfg_dma_mem_valid, 550 .valid.max_access_size = 8, 551 .impl.max_access_size = 8, 552 }; 553 554 static void fw_cfg_reset(DeviceState *d) 555 { 556 FWCfgState *s = FW_CFG(d); 557 558 /* we never register a read callback for FW_CFG_SIGNATURE */ 559 fw_cfg_select(s, FW_CFG_SIGNATURE); 560 } 561 562 /* Save restore 32 bit int as uint16_t 563 This is a Big hack, but it is how the old state did it. 564 Or we broke compatibility in the state, or we can't use struct tm 565 */ 566 567 static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size, 568 const VMStateField *field) 569 { 570 uint32_t *v = pv; 571 *v = qemu_get_be16(f); 572 return 0; 573 } 574 575 static int put_unused(QEMUFile *f, void *pv, size_t size, 576 const VMStateField *field, JSONWriter *vmdesc) 577 { 578 fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n"); 579 fprintf(stderr, "This functions shouldn't be called.\n"); 580 581 return 0; 582 } 583 584 static const VMStateInfo vmstate_hack_uint32_as_uint16 = { 585 .name = "int32_as_uint16", 586 .get = get_uint32_as_uint16, 587 .put = put_unused, 588 }; 589 590 #define VMSTATE_UINT16_HACK(_f, _s, _t) \ 591 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t) 592 593 594 static bool is_version_1(void *opaque, int version_id) 595 { 596 return version_id == 1; 597 } 598 599 bool fw_cfg_dma_enabled(void *opaque) 600 { 601 FWCfgState *s = opaque; 602 603 return s->dma_enabled; 604 } 605 606 static bool fw_cfg_acpi_mr_restore(void *opaque) 607 { 608 FWCfgState *s = opaque; 609 bool mr_aligned; 610 611 mr_aligned = QEMU_IS_ALIGNED(s->table_mr_size, qemu_real_host_page_size()) && 612 QEMU_IS_ALIGNED(s->linker_mr_size, qemu_real_host_page_size()) && 613 QEMU_IS_ALIGNED(s->rsdp_mr_size, qemu_real_host_page_size()); 614 return s->acpi_mr_restore && !mr_aligned; 615 } 616 617 static void fw_cfg_update_mr(FWCfgState *s, uint16_t key, size_t size) 618 { 619 MemoryRegion *mr; 620 ram_addr_t offset; 621 int arch = !!(key & FW_CFG_ARCH_LOCAL); 622 void *ptr; 623 624 key &= FW_CFG_ENTRY_MASK; 625 assert(key < fw_cfg_max_entry(s)); 626 627 ptr = s->entries[arch][key].data; 628 mr = memory_region_from_host(ptr, &offset); 629 630 memory_region_ram_resize(mr, size, &error_abort); 631 } 632 633 static int fw_cfg_acpi_mr_restore_post_load(void *opaque, int version_id) 634 { 635 FWCfgState *s = opaque; 636 int i, index; 637 638 assert(s->files); 639 640 index = be32_to_cpu(s->files->count); 641 642 for (i = 0; i < index; i++) { 643 if (!strcmp(s->files->f[i].name, ACPI_BUILD_TABLE_FILE)) { 644 fw_cfg_update_mr(s, FW_CFG_FILE_FIRST + i, s->table_mr_size); 645 } else if (!strcmp(s->files->f[i].name, ACPI_BUILD_LOADER_FILE)) { 646 fw_cfg_update_mr(s, FW_CFG_FILE_FIRST + i, s->linker_mr_size); 647 } else if (!strcmp(s->files->f[i].name, ACPI_BUILD_RSDP_FILE)) { 648 fw_cfg_update_mr(s, FW_CFG_FILE_FIRST + i, s->rsdp_mr_size); 649 } 650 } 651 652 return 0; 653 } 654 655 static const VMStateDescription vmstate_fw_cfg_dma = { 656 .name = "fw_cfg/dma", 657 .needed = fw_cfg_dma_enabled, 658 .fields = (VMStateField[]) { 659 VMSTATE_UINT64(dma_addr, FWCfgState), 660 VMSTATE_END_OF_LIST() 661 }, 662 }; 663 664 static const VMStateDescription vmstate_fw_cfg_acpi_mr = { 665 .name = "fw_cfg/acpi_mr", 666 .version_id = 1, 667 .minimum_version_id = 1, 668 .needed = fw_cfg_acpi_mr_restore, 669 .post_load = fw_cfg_acpi_mr_restore_post_load, 670 .fields = (VMStateField[]) { 671 VMSTATE_UINT64(table_mr_size, FWCfgState), 672 VMSTATE_UINT64(linker_mr_size, FWCfgState), 673 VMSTATE_UINT64(rsdp_mr_size, FWCfgState), 674 VMSTATE_END_OF_LIST() 675 }, 676 }; 677 678 static const VMStateDescription vmstate_fw_cfg = { 679 .name = "fw_cfg", 680 .version_id = 2, 681 .minimum_version_id = 1, 682 .fields = (VMStateField[]) { 683 VMSTATE_UINT16(cur_entry, FWCfgState), 684 VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1), 685 VMSTATE_UINT32_V(cur_offset, FWCfgState, 2), 686 VMSTATE_END_OF_LIST() 687 }, 688 .subsections = (const VMStateDescription*[]) { 689 &vmstate_fw_cfg_dma, 690 &vmstate_fw_cfg_acpi_mr, 691 NULL, 692 } 693 }; 694 695 void fw_cfg_add_bytes_callback(FWCfgState *s, uint16_t key, 696 FWCfgCallback select_cb, 697 FWCfgWriteCallback write_cb, 698 void *callback_opaque, 699 void *data, size_t len, 700 bool read_only) 701 { 702 int arch = !!(key & FW_CFG_ARCH_LOCAL); 703 704 key &= FW_CFG_ENTRY_MASK; 705 706 assert(key < fw_cfg_max_entry(s) && len < UINT32_MAX); 707 assert(s->entries[arch][key].data == NULL); /* avoid key conflict */ 708 709 s->entries[arch][key].data = data; 710 s->entries[arch][key].len = (uint32_t)len; 711 s->entries[arch][key].select_cb = select_cb; 712 s->entries[arch][key].write_cb = write_cb; 713 s->entries[arch][key].callback_opaque = callback_opaque; 714 s->entries[arch][key].allow_write = !read_only; 715 } 716 717 static void *fw_cfg_modify_bytes_read(FWCfgState *s, uint16_t key, 718 void *data, size_t len) 719 { 720 void *ptr; 721 int arch = !!(key & FW_CFG_ARCH_LOCAL); 722 723 key &= FW_CFG_ENTRY_MASK; 724 725 assert(key < fw_cfg_max_entry(s) && len < UINT32_MAX); 726 727 /* return the old data to the function caller, avoid memory leak */ 728 ptr = s->entries[arch][key].data; 729 s->entries[arch][key].data = data; 730 s->entries[arch][key].len = len; 731 s->entries[arch][key].callback_opaque = NULL; 732 s->entries[arch][key].allow_write = false; 733 734 return ptr; 735 } 736 737 void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len) 738 { 739 trace_fw_cfg_add_bytes(key, trace_key_name(key), len); 740 fw_cfg_add_bytes_callback(s, key, NULL, NULL, NULL, data, len, true); 741 } 742 743 void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value) 744 { 745 size_t sz = strlen(value) + 1; 746 747 trace_fw_cfg_add_string(key, trace_key_name(key), value); 748 fw_cfg_add_bytes(s, key, g_memdup(value, sz), sz); 749 } 750 751 void fw_cfg_modify_string(FWCfgState *s, uint16_t key, const char *value) 752 { 753 size_t sz = strlen(value) + 1; 754 char *old; 755 756 old = fw_cfg_modify_bytes_read(s, key, g_memdup(value, sz), sz); 757 g_free(old); 758 } 759 760 void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value) 761 { 762 uint16_t *copy; 763 764 copy = g_malloc(sizeof(value)); 765 *copy = cpu_to_le16(value); 766 trace_fw_cfg_add_i16(key, trace_key_name(key), value); 767 fw_cfg_add_bytes(s, key, copy, sizeof(value)); 768 } 769 770 void fw_cfg_modify_i16(FWCfgState *s, uint16_t key, uint16_t value) 771 { 772 uint16_t *copy, *old; 773 774 copy = g_malloc(sizeof(value)); 775 *copy = cpu_to_le16(value); 776 old = fw_cfg_modify_bytes_read(s, key, copy, sizeof(value)); 777 g_free(old); 778 } 779 780 void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value) 781 { 782 uint32_t *copy; 783 784 copy = g_malloc(sizeof(value)); 785 *copy = cpu_to_le32(value); 786 trace_fw_cfg_add_i32(key, trace_key_name(key), value); 787 fw_cfg_add_bytes(s, key, copy, sizeof(value)); 788 } 789 790 void fw_cfg_modify_i32(FWCfgState *s, uint16_t key, uint32_t value) 791 { 792 uint32_t *copy, *old; 793 794 copy = g_malloc(sizeof(value)); 795 *copy = cpu_to_le32(value); 796 old = fw_cfg_modify_bytes_read(s, key, copy, sizeof(value)); 797 g_free(old); 798 } 799 800 void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value) 801 { 802 uint64_t *copy; 803 804 copy = g_malloc(sizeof(value)); 805 *copy = cpu_to_le64(value); 806 trace_fw_cfg_add_i64(key, trace_key_name(key), value); 807 fw_cfg_add_bytes(s, key, copy, sizeof(value)); 808 } 809 810 void fw_cfg_modify_i64(FWCfgState *s, uint16_t key, uint64_t value) 811 { 812 uint64_t *copy, *old; 813 814 copy = g_malloc(sizeof(value)); 815 *copy = cpu_to_le64(value); 816 old = fw_cfg_modify_bytes_read(s, key, copy, sizeof(value)); 817 g_free(old); 818 } 819 820 void fw_cfg_set_order_override(FWCfgState *s, int order) 821 { 822 assert(s->fw_cfg_order_override == 0); 823 s->fw_cfg_order_override = order; 824 } 825 826 void fw_cfg_reset_order_override(FWCfgState *s) 827 { 828 assert(s->fw_cfg_order_override != 0); 829 s->fw_cfg_order_override = 0; 830 } 831 832 /* 833 * This is the legacy order list. For legacy systems, files are in 834 * the fw_cfg in the order defined below, by the "order" value. Note 835 * that some entries (VGA ROMs, NIC option ROMS, etc.) go into a 836 * specific area, but there may be more than one and they occur in the 837 * order that the user specifies them on the command line. Those are 838 * handled in a special manner, using the order override above. 839 * 840 * For non-legacy, the files are sorted by filename to avoid this kind 841 * of complexity in the future. 842 * 843 * This is only for x86, other arches don't implement versioning so 844 * they won't set legacy mode. 845 */ 846 static struct { 847 const char *name; 848 int order; 849 } fw_cfg_order[] = { 850 { "etc/boot-menu-wait", 10 }, 851 { "bootsplash.jpg", 11 }, 852 { "bootsplash.bmp", 12 }, 853 { "etc/boot-fail-wait", 15 }, 854 { "etc/smbios/smbios-tables", 20 }, 855 { "etc/smbios/smbios-anchor", 30 }, 856 { "etc/e820", 40 }, 857 { "etc/reserved-memory-end", 50 }, 858 { "genroms/kvmvapic.bin", 55 }, 859 { "genroms/linuxboot.bin", 60 }, 860 { }, /* VGA ROMs from pc_vga_init come here, 70. */ 861 { }, /* NIC option ROMs from pc_nic_init come here, 80. */ 862 { "etc/system-states", 90 }, 863 { }, /* User ROMs come here, 100. */ 864 { }, /* Device FW comes here, 110. */ 865 { "etc/extra-pci-roots", 120 }, 866 { "etc/acpi/tables", 130 }, 867 { "etc/table-loader", 140 }, 868 { "etc/tpm/log", 150 }, 869 { "etc/acpi/rsdp", 160 }, 870 { "bootorder", 170 }, 871 { "etc/msr_feature_control", 180 }, 872 873 #define FW_CFG_ORDER_OVERRIDE_LAST 200 874 }; 875 876 /* 877 * Any sub-page size update to these table MRs will be lost during migration, 878 * as we use aligned size in ram_load_precopy() -> qemu_ram_resize() path. 879 * In order to avoid the inconsistency in sizes save them seperately and 880 * migrate over in vmstate post_load(). 881 */ 882 static void fw_cfg_acpi_mr_save(FWCfgState *s, const char *filename, size_t len) 883 { 884 if (!strcmp(filename, ACPI_BUILD_TABLE_FILE)) { 885 s->table_mr_size = len; 886 } else if (!strcmp(filename, ACPI_BUILD_LOADER_FILE)) { 887 s->linker_mr_size = len; 888 } else if (!strcmp(filename, ACPI_BUILD_RSDP_FILE)) { 889 s->rsdp_mr_size = len; 890 } 891 } 892 893 static int get_fw_cfg_order(FWCfgState *s, const char *name) 894 { 895 int i; 896 897 if (s->fw_cfg_order_override > 0) { 898 return s->fw_cfg_order_override; 899 } 900 901 for (i = 0; i < ARRAY_SIZE(fw_cfg_order); i++) { 902 if (fw_cfg_order[i].name == NULL) { 903 continue; 904 } 905 906 if (strcmp(name, fw_cfg_order[i].name) == 0) { 907 return fw_cfg_order[i].order; 908 } 909 } 910 911 /* Stick unknown stuff at the end. */ 912 warn_report("Unknown firmware file in legacy mode: %s", name); 913 return FW_CFG_ORDER_OVERRIDE_LAST; 914 } 915 916 void fw_cfg_add_file_callback(FWCfgState *s, const char *filename, 917 FWCfgCallback select_cb, 918 FWCfgWriteCallback write_cb, 919 void *callback_opaque, 920 void *data, size_t len, bool read_only) 921 { 922 int i, index, count; 923 size_t dsize; 924 MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine()); 925 int order = 0; 926 927 if (!s->files) { 928 dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * fw_cfg_file_slots(s); 929 s->files = g_malloc0(dsize); 930 fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, s->files, dsize); 931 } 932 933 count = be32_to_cpu(s->files->count); 934 assert(count < fw_cfg_file_slots(s)); 935 936 /* Find the insertion point. */ 937 if (mc->legacy_fw_cfg_order) { 938 /* 939 * Sort by order. For files with the same order, we keep them 940 * in the sequence in which they were added. 941 */ 942 order = get_fw_cfg_order(s, filename); 943 for (index = count; 944 index > 0 && order < s->entry_order[index - 1]; 945 index--); 946 } else { 947 /* Sort by file name. */ 948 for (index = count; 949 index > 0 && strcmp(filename, s->files->f[index - 1].name) < 0; 950 index--); 951 } 952 953 /* 954 * Move all the entries from the index point and after down one 955 * to create a slot for the new entry. Because calculations are 956 * being done with the index, make it so that "i" is the current 957 * index and "i - 1" is the one being copied from, thus the 958 * unusual start and end in the for statement. 959 */ 960 for (i = count; i > index; i--) { 961 s->files->f[i] = s->files->f[i - 1]; 962 s->files->f[i].select = cpu_to_be16(FW_CFG_FILE_FIRST + i); 963 s->entries[0][FW_CFG_FILE_FIRST + i] = 964 s->entries[0][FW_CFG_FILE_FIRST + i - 1]; 965 s->entry_order[i] = s->entry_order[i - 1]; 966 } 967 968 memset(&s->files->f[index], 0, sizeof(FWCfgFile)); 969 memset(&s->entries[0][FW_CFG_FILE_FIRST + index], 0, sizeof(FWCfgEntry)); 970 971 pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name), filename); 972 for (i = 0; i <= count; i++) { 973 if (i != index && 974 strcmp(s->files->f[index].name, s->files->f[i].name) == 0) { 975 error_report("duplicate fw_cfg file name: %s", 976 s->files->f[index].name); 977 exit(1); 978 } 979 } 980 981 fw_cfg_add_bytes_callback(s, FW_CFG_FILE_FIRST + index, 982 select_cb, write_cb, 983 callback_opaque, data, len, 984 read_only); 985 986 s->files->f[index].size = cpu_to_be32(len); 987 s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index); 988 s->entry_order[index] = order; 989 trace_fw_cfg_add_file(s, index, s->files->f[index].name, len); 990 991 s->files->count = cpu_to_be32(count+1); 992 fw_cfg_acpi_mr_save(s, filename, len); 993 } 994 995 void fw_cfg_add_file(FWCfgState *s, const char *filename, 996 void *data, size_t len) 997 { 998 fw_cfg_add_file_callback(s, filename, NULL, NULL, NULL, data, len, true); 999 } 1000 1001 void *fw_cfg_modify_file(FWCfgState *s, const char *filename, 1002 void *data, size_t len) 1003 { 1004 int i, index; 1005 void *ptr = NULL; 1006 1007 assert(s->files); 1008 1009 index = be32_to_cpu(s->files->count); 1010 1011 for (i = 0; i < index; i++) { 1012 if (strcmp(filename, s->files->f[i].name) == 0) { 1013 ptr = fw_cfg_modify_bytes_read(s, FW_CFG_FILE_FIRST + i, 1014 data, len); 1015 s->files->f[i].size = cpu_to_be32(len); 1016 fw_cfg_acpi_mr_save(s, filename, len); 1017 return ptr; 1018 } 1019 } 1020 1021 assert(index < fw_cfg_file_slots(s)); 1022 1023 /* add new one */ 1024 fw_cfg_add_file_callback(s, filename, NULL, NULL, NULL, data, len, true); 1025 return NULL; 1026 } 1027 1028 bool fw_cfg_add_from_generator(FWCfgState *s, const char *filename, 1029 const char *gen_id, Error **errp) 1030 { 1031 FWCfgDataGeneratorClass *klass; 1032 GByteArray *array; 1033 Object *obj; 1034 gsize size; 1035 1036 obj = object_resolve_path_component(object_get_objects_root(), gen_id); 1037 if (!obj) { 1038 error_setg(errp, "Cannot find object ID '%s'", gen_id); 1039 return false; 1040 } 1041 if (!object_dynamic_cast(obj, TYPE_FW_CFG_DATA_GENERATOR_INTERFACE)) { 1042 error_setg(errp, "Object ID '%s' is not a '%s' subclass", 1043 gen_id, TYPE_FW_CFG_DATA_GENERATOR_INTERFACE); 1044 return false; 1045 } 1046 klass = FW_CFG_DATA_GENERATOR_GET_CLASS(obj); 1047 array = klass->get_data(obj, errp); 1048 if (!array) { 1049 return false; 1050 } 1051 size = array->len; 1052 fw_cfg_add_file(s, filename, g_byte_array_free(array, FALSE), size); 1053 1054 return true; 1055 } 1056 1057 void fw_cfg_add_extra_pci_roots(PCIBus *bus, FWCfgState *s) 1058 { 1059 int extra_hosts = 0; 1060 1061 if (!bus) { 1062 return; 1063 } 1064 1065 QLIST_FOREACH(bus, &bus->child, sibling) { 1066 /* look for expander root buses */ 1067 if (pci_bus_is_root(bus)) { 1068 extra_hosts++; 1069 } 1070 } 1071 1072 if (extra_hosts && s) { 1073 uint64_t *val = g_malloc(sizeof(*val)); 1074 *val = cpu_to_le64(extra_hosts); 1075 fw_cfg_add_file(s, "etc/extra-pci-roots", val, sizeof(*val)); 1076 } 1077 } 1078 1079 static void fw_cfg_machine_reset(void *opaque) 1080 { 1081 MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine()); 1082 FWCfgState *s = opaque; 1083 void *ptr; 1084 size_t len; 1085 char *buf; 1086 1087 buf = get_boot_devices_list(&len); 1088 ptr = fw_cfg_modify_file(s, "bootorder", (uint8_t *)buf, len); 1089 g_free(ptr); 1090 1091 if (!mc->legacy_fw_cfg_order) { 1092 buf = get_boot_devices_lchs_list(&len); 1093 ptr = fw_cfg_modify_file(s, "bios-geometry", (uint8_t *)buf, len); 1094 g_free(ptr); 1095 } 1096 } 1097 1098 static void fw_cfg_machine_ready(struct Notifier *n, void *data) 1099 { 1100 FWCfgState *s = container_of(n, FWCfgState, machine_ready); 1101 qemu_register_reset(fw_cfg_machine_reset, s); 1102 } 1103 1104 static Property fw_cfg_properties[] = { 1105 DEFINE_PROP_BOOL("acpi-mr-restore", FWCfgState, acpi_mr_restore, true), 1106 DEFINE_PROP_END_OF_LIST(), 1107 }; 1108 1109 static void fw_cfg_common_realize(DeviceState *dev, Error **errp) 1110 { 1111 FWCfgState *s = FW_CFG(dev); 1112 MachineState *machine = MACHINE(qdev_get_machine()); 1113 uint32_t version = FW_CFG_VERSION; 1114 1115 if (!fw_cfg_find()) { 1116 error_setg(errp, "at most one %s device is permitted", TYPE_FW_CFG); 1117 return; 1118 } 1119 1120 fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (char *)"QEMU", 4); 1121 fw_cfg_add_bytes(s, FW_CFG_UUID, &qemu_uuid, 16); 1122 fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)!machine->enable_graphics); 1123 fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)(machine->boot_config.has_menu && machine->boot_config.menu)); 1124 fw_cfg_bootsplash(s); 1125 fw_cfg_reboot(s); 1126 1127 if (s->dma_enabled) { 1128 version |= FW_CFG_VERSION_DMA; 1129 } 1130 1131 fw_cfg_add_i32(s, FW_CFG_ID, version); 1132 1133 s->machine_ready.notify = fw_cfg_machine_ready; 1134 qemu_add_machine_init_done_notifier(&s->machine_ready); 1135 } 1136 1137 FWCfgState *fw_cfg_init_io_dma(uint32_t iobase, uint32_t dma_iobase, 1138 AddressSpace *dma_as) 1139 { 1140 DeviceState *dev; 1141 SysBusDevice *sbd; 1142 FWCfgIoState *ios; 1143 FWCfgState *s; 1144 bool dma_requested = dma_iobase && dma_as; 1145 1146 dev = qdev_new(TYPE_FW_CFG_IO); 1147 if (!dma_requested) { 1148 qdev_prop_set_bit(dev, "dma_enabled", false); 1149 } 1150 1151 object_property_add_child(OBJECT(qdev_get_machine()), TYPE_FW_CFG, 1152 OBJECT(dev)); 1153 1154 sbd = SYS_BUS_DEVICE(dev); 1155 sysbus_realize_and_unref(sbd, &error_fatal); 1156 ios = FW_CFG_IO(dev); 1157 sysbus_add_io(sbd, iobase, &ios->comb_iomem); 1158 1159 s = FW_CFG(dev); 1160 1161 if (s->dma_enabled) { 1162 /* 64 bits for the address field */ 1163 s->dma_as = dma_as; 1164 s->dma_addr = 0; 1165 sysbus_add_io(sbd, dma_iobase, &s->dma_iomem); 1166 } 1167 1168 return s; 1169 } 1170 1171 FWCfgState *fw_cfg_init_io(uint32_t iobase) 1172 { 1173 return fw_cfg_init_io_dma(iobase, 0, NULL); 1174 } 1175 1176 FWCfgState *fw_cfg_init_mem_wide(hwaddr ctl_addr, 1177 hwaddr data_addr, uint32_t data_width, 1178 hwaddr dma_addr, AddressSpace *dma_as) 1179 { 1180 DeviceState *dev; 1181 SysBusDevice *sbd; 1182 FWCfgState *s; 1183 bool dma_requested = dma_addr && dma_as; 1184 1185 dev = qdev_new(TYPE_FW_CFG_MEM); 1186 qdev_prop_set_uint32(dev, "data_width", data_width); 1187 if (!dma_requested) { 1188 qdev_prop_set_bit(dev, "dma_enabled", false); 1189 } 1190 1191 object_property_add_child(OBJECT(qdev_get_machine()), TYPE_FW_CFG, 1192 OBJECT(dev)); 1193 1194 sbd = SYS_BUS_DEVICE(dev); 1195 sysbus_realize_and_unref(sbd, &error_fatal); 1196 sysbus_mmio_map(sbd, 0, ctl_addr); 1197 sysbus_mmio_map(sbd, 1, data_addr); 1198 1199 s = FW_CFG(dev); 1200 1201 if (s->dma_enabled) { 1202 s->dma_as = dma_as; 1203 s->dma_addr = 0; 1204 sysbus_mmio_map(sbd, 2, dma_addr); 1205 } 1206 1207 return s; 1208 } 1209 1210 FWCfgState *fw_cfg_init_mem(hwaddr ctl_addr, hwaddr data_addr) 1211 { 1212 return fw_cfg_init_mem_wide(ctl_addr, data_addr, 1213 fw_cfg_data_mem_ops.valid.max_access_size, 1214 0, NULL); 1215 } 1216 1217 1218 FWCfgState *fw_cfg_find(void) 1219 { 1220 /* Returns NULL unless there is exactly one fw_cfg device */ 1221 return FW_CFG(object_resolve_path_type("", TYPE_FW_CFG, NULL)); 1222 } 1223 1224 1225 static void fw_cfg_class_init(ObjectClass *klass, void *data) 1226 { 1227 DeviceClass *dc = DEVICE_CLASS(klass); 1228 1229 dc->reset = fw_cfg_reset; 1230 dc->vmsd = &vmstate_fw_cfg; 1231 1232 device_class_set_props(dc, fw_cfg_properties); 1233 } 1234 1235 static const TypeInfo fw_cfg_info = { 1236 .name = TYPE_FW_CFG, 1237 .parent = TYPE_SYS_BUS_DEVICE, 1238 .abstract = true, 1239 .instance_size = sizeof(FWCfgState), 1240 .class_init = fw_cfg_class_init, 1241 }; 1242 1243 static void fw_cfg_file_slots_allocate(FWCfgState *s, Error **errp) 1244 { 1245 uint16_t file_slots_max; 1246 1247 if (fw_cfg_file_slots(s) < FW_CFG_FILE_SLOTS_MIN) { 1248 error_setg(errp, "\"file_slots\" must be at least 0x%x", 1249 FW_CFG_FILE_SLOTS_MIN); 1250 return; 1251 } 1252 1253 /* (UINT16_MAX & FW_CFG_ENTRY_MASK) is the highest inclusive selector value 1254 * that we permit. The actual (exclusive) value coming from the 1255 * configuration is (FW_CFG_FILE_FIRST + fw_cfg_file_slots(s)). */ 1256 file_slots_max = (UINT16_MAX & FW_CFG_ENTRY_MASK) - FW_CFG_FILE_FIRST + 1; 1257 if (fw_cfg_file_slots(s) > file_slots_max) { 1258 error_setg(errp, "\"file_slots\" must not exceed 0x%" PRIx16, 1259 file_slots_max); 1260 return; 1261 } 1262 1263 s->entries[0] = g_new0(FWCfgEntry, fw_cfg_max_entry(s)); 1264 s->entries[1] = g_new0(FWCfgEntry, fw_cfg_max_entry(s)); 1265 s->entry_order = g_new0(int, fw_cfg_max_entry(s)); 1266 } 1267 1268 static Property fw_cfg_io_properties[] = { 1269 DEFINE_PROP_BOOL("dma_enabled", FWCfgIoState, parent_obj.dma_enabled, 1270 true), 1271 DEFINE_PROP_UINT16("x-file-slots", FWCfgIoState, parent_obj.file_slots, 1272 FW_CFG_FILE_SLOTS_DFLT), 1273 DEFINE_PROP_END_OF_LIST(), 1274 }; 1275 1276 static void fw_cfg_io_realize(DeviceState *dev, Error **errp) 1277 { 1278 ERRP_GUARD(); 1279 FWCfgIoState *s = FW_CFG_IO(dev); 1280 1281 fw_cfg_file_slots_allocate(FW_CFG(s), errp); 1282 if (*errp) { 1283 return; 1284 } 1285 1286 /* when using port i/o, the 8-bit data register ALWAYS overlaps 1287 * with half of the 16-bit control register. Hence, the total size 1288 * of the i/o region used is FW_CFG_CTL_SIZE */ 1289 memory_region_init_io(&s->comb_iomem, OBJECT(s), &fw_cfg_comb_mem_ops, 1290 FW_CFG(s), "fwcfg", FW_CFG_CTL_SIZE); 1291 1292 if (FW_CFG(s)->dma_enabled) { 1293 memory_region_init_io(&FW_CFG(s)->dma_iomem, OBJECT(s), 1294 &fw_cfg_dma_mem_ops, FW_CFG(s), "fwcfg.dma", 1295 sizeof(dma_addr_t)); 1296 } 1297 1298 fw_cfg_common_realize(dev, errp); 1299 } 1300 1301 static void fw_cfg_io_class_init(ObjectClass *klass, void *data) 1302 { 1303 DeviceClass *dc = DEVICE_CLASS(klass); 1304 1305 dc->realize = fw_cfg_io_realize; 1306 device_class_set_props(dc, fw_cfg_io_properties); 1307 } 1308 1309 static const TypeInfo fw_cfg_io_info = { 1310 .name = TYPE_FW_CFG_IO, 1311 .parent = TYPE_FW_CFG, 1312 .instance_size = sizeof(FWCfgIoState), 1313 .class_init = fw_cfg_io_class_init, 1314 }; 1315 1316 1317 static Property fw_cfg_mem_properties[] = { 1318 DEFINE_PROP_UINT32("data_width", FWCfgMemState, data_width, -1), 1319 DEFINE_PROP_BOOL("dma_enabled", FWCfgMemState, parent_obj.dma_enabled, 1320 true), 1321 DEFINE_PROP_UINT16("x-file-slots", FWCfgMemState, parent_obj.file_slots, 1322 FW_CFG_FILE_SLOTS_DFLT), 1323 DEFINE_PROP_END_OF_LIST(), 1324 }; 1325 1326 static void fw_cfg_mem_realize(DeviceState *dev, Error **errp) 1327 { 1328 ERRP_GUARD(); 1329 FWCfgMemState *s = FW_CFG_MEM(dev); 1330 SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 1331 const MemoryRegionOps *data_ops = &fw_cfg_data_mem_ops; 1332 1333 fw_cfg_file_slots_allocate(FW_CFG(s), errp); 1334 if (*errp) { 1335 return; 1336 } 1337 1338 memory_region_init_io(&s->ctl_iomem, OBJECT(s), &fw_cfg_ctl_mem_ops, 1339 FW_CFG(s), "fwcfg.ctl", FW_CFG_CTL_SIZE); 1340 sysbus_init_mmio(sbd, &s->ctl_iomem); 1341 1342 if (s->data_width > data_ops->valid.max_access_size) { 1343 s->wide_data_ops = *data_ops; 1344 1345 s->wide_data_ops.valid.max_access_size = s->data_width; 1346 s->wide_data_ops.impl.max_access_size = s->data_width; 1347 data_ops = &s->wide_data_ops; 1348 } 1349 memory_region_init_io(&s->data_iomem, OBJECT(s), data_ops, FW_CFG(s), 1350 "fwcfg.data", data_ops->valid.max_access_size); 1351 sysbus_init_mmio(sbd, &s->data_iomem); 1352 1353 if (FW_CFG(s)->dma_enabled) { 1354 memory_region_init_io(&FW_CFG(s)->dma_iomem, OBJECT(s), 1355 &fw_cfg_dma_mem_ops, FW_CFG(s), "fwcfg.dma", 1356 sizeof(dma_addr_t)); 1357 sysbus_init_mmio(sbd, &FW_CFG(s)->dma_iomem); 1358 } 1359 1360 fw_cfg_common_realize(dev, errp); 1361 } 1362 1363 static void fw_cfg_mem_class_init(ObjectClass *klass, void *data) 1364 { 1365 DeviceClass *dc = DEVICE_CLASS(klass); 1366 1367 dc->realize = fw_cfg_mem_realize; 1368 device_class_set_props(dc, fw_cfg_mem_properties); 1369 } 1370 1371 static const TypeInfo fw_cfg_mem_info = { 1372 .name = TYPE_FW_CFG_MEM, 1373 .parent = TYPE_FW_CFG, 1374 .instance_size = sizeof(FWCfgMemState), 1375 .class_init = fw_cfg_mem_class_init, 1376 }; 1377 1378 static void fw_cfg_register_types(void) 1379 { 1380 type_register_static(&fw_cfg_info); 1381 type_register_static(&fw_cfg_io_info); 1382 type_register_static(&fw_cfg_mem_info); 1383 } 1384 1385 type_init(fw_cfg_register_types) 1386