9ae3a8
From b9968ba04f7d8528d07c49bf523474ac4cb63acd Mon Sep 17 00:00:00 2001
9ae3a8
From: Laszlo Ersek <lersek@redhat.com>
9ae3a8
Date: Wed, 29 Jan 2014 07:06:24 +0100
9ae3a8
Subject: [PATCH 12/22] pc_sysfw: do not make it a device anymore
9ae3a8
9ae3a8
Message-id: <1389463208-6278-13-git-send-email-lersek@redhat.com>
9ae3a8
Patchwork-id: 56625
9ae3a8
O-Subject: [RHEL-7.0 qemu-kvm PATCH 12/18] pc_sysfw: do not make it a device anymore
9ae3a8
Bugzilla: 1032346
9ae3a8
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
RH-Acked-by: Amos Kong <akong@redhat.com>
9ae3a8
RH-Acked-by: Andrew Jones <drjones@redhat.com>
9ae3a8
9ae3a8
From: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
9ae3a8
Move the code to hw/i386, the sole remaining property is available
9ae3a8
as !pci_enabled.
9ae3a8
9ae3a8
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
Signed-off-by: Markus Armbruster <armbru@redhat.com>
9ae3a8
Message-id: 1376069702-22330-4-git-send-email-aliguori@us.ibm.com
9ae3a8
9ae3a8
Rebased.
9ae3a8
9ae3a8
Signed-off-by: Markus Armbruster <armbru@redhat.com>
9ae3a8
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
9ae3a8
(cherry picked from commit 6dd2a5c98a6b1c9189d342bcc3493c9b5dd1217e)
9ae3a8
9ae3a8
Conflicts:
9ae3a8
	hw/block/Makefile.objs
9ae3a8
	hw/i386/pc_q35.c
9ae3a8
	include/hw/i386/pc.h
9ae3a8
9ae3a8
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
---
9ae3a8
 hw/block/Makefile.objs             |  1 -
9ae3a8
 hw/i386/Makefile.objs              |  1 +
9ae3a8
 include/hw/i386/pc.h               |  4 ++-
9ae3a8
 hw/i386/pc.c                       |  2 +-
9ae3a8
 hw/i386/pc_piix.c                  |  6 +----
9ae3a8
 hw/i386/pc_q35.c                   |  1 +
9ae3a8
 hw/{block => i386}/pc_sysfw.c      | 52 +++-----------------------------------
9ae3a8
 default-configs/i386-softmmu.mak   |  1 -
9ae3a8
 default-configs/x86_64-softmmu.mak |  1 -
9ae3a8
 9 files changed, 10 insertions(+), 59 deletions(-)
9ae3a8
 rename hw/{block => i386}/pc_sysfw.c (81%)
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 default-configs/i386-softmmu.mak   |    1 -
9ae3a8
 default-configs/x86_64-softmmu.mak |    1 -
9ae3a8
 hw/block/Makefile.objs             |    1 -
9ae3a8
 hw/block/pc_sysfw.c                |  231 ------------------------------------
9ae3a8
 hw/i386/Makefile.objs              |    1 +
9ae3a8
 hw/i386/pc.c                       |    2 +-
9ae3a8
 hw/i386/pc_piix.c                  |    6 +-
9ae3a8
 hw/i386/pc_q35.c                   |    1 +
9ae3a8
 hw/i386/pc_sysfw.c                 |  185 +++++++++++++++++++++++++++++
9ae3a8
 include/hw/i386/pc.h               |    4 +-
9ae3a8
 10 files changed, 192 insertions(+), 241 deletions(-)
9ae3a8
 delete mode 100644 hw/block/pc_sysfw.c
9ae3a8
 create mode 100644 hw/i386/pc_sysfw.c
9ae3a8
9ae3a8
diff --git a/default-configs/i386-softmmu.mak b/default-configs/i386-softmmu.mak
9ae3a8
index 03deca2..fb84f80 100644
9ae3a8
--- a/default-configs/i386-softmmu.mak
9ae3a8
+++ b/default-configs/i386-softmmu.mak
9ae3a8
@@ -34,7 +34,6 @@ CONFIG_PAM=y
9ae3a8
 CONFIG_PCI_PIIX=y
9ae3a8
 CONFIG_PCI_HOTPLUG=y
9ae3a8
 CONFIG_WDT_IB700=y
9ae3a8
-CONFIG_PC_SYSFW=y
9ae3a8
 CONFIG_XEN_I386=$(CONFIG_XEN)
9ae3a8
 CONFIG_ISA_DEBUG=y
9ae3a8
 CONFIG_ISA_TESTDEV=y
9ae3a8
diff --git a/default-configs/x86_64-softmmu.mak b/default-configs/x86_64-softmmu.mak
9ae3a8
index 7b8b05c..a6fdf84 100644
9ae3a8
--- a/default-configs/x86_64-softmmu.mak
9ae3a8
+++ b/default-configs/x86_64-softmmu.mak
9ae3a8
@@ -27,7 +27,6 @@ CONFIG_PAM=y
9ae3a8
 CONFIG_PCI_PIIX=y
9ae3a8
 CONFIG_PCI_HOTPLUG=y
9ae3a8
 CONFIG_WDT_IB700=y
9ae3a8
-CONFIG_PC_SYSFW=y
9ae3a8
 CONFIG_XEN_I386=$(CONFIG_XEN)
9ae3a8
 CONFIG_ISA_DEBUG=y
9ae3a8
 CONFIG_ISA_TESTDEV=y
9ae3a8
diff --git a/hw/block/Makefile.objs b/hw/block/Makefile.objs
9ae3a8
index e4329a0..94491bf 100644
9ae3a8
--- a/hw/block/Makefile.objs
9ae3a8
+++ b/hw/block/Makefile.objs
9ae3a8
@@ -7,7 +7,6 @@ common-obj-$(CONFIG_PFLASH_CFI02) += pflash_cfi02.o
9ae3a8
 common-obj-$(CONFIG_XEN_BACKEND) += xen_disk.o
9ae3a8
 common-obj-$(CONFIG_ECC) += ecc.o
9ae3a8
 common-obj-$(CONFIG_ONENAND) += onenand.o
9ae3a8
-common-obj-$(CONFIG_PC_SYSFW) += pc_sysfw.o
9ae3a8
 
9ae3a8
 obj-$(CONFIG_SH4) += tc58128.o
9ae3a8
 
9ae3a8
diff --git a/hw/block/pc_sysfw.c b/hw/block/pc_sysfw.c
9ae3a8
deleted file mode 100644
9ae3a8
index f9c0633..0000000
9ae3a8
--- a/hw/block/pc_sysfw.c
9ae3a8
+++ /dev/null
9ae3a8
@@ -1,231 +0,0 @@
9ae3a8
-/*
9ae3a8
- * QEMU PC System Firmware
9ae3a8
- *
9ae3a8
- * Copyright (c) 2003-2004 Fabrice Bellard
9ae3a8
- * Copyright (c) 2011-2012 Intel Corporation
9ae3a8
- *
9ae3a8
- * Permission is hereby granted, free of charge, to any person obtaining a copy
9ae3a8
- * of this software and associated documentation files (the "Software"), to deal
9ae3a8
- * in the Software without restriction, including without limitation the rights
9ae3a8
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9ae3a8
- * copies of the Software, and to permit persons to whom the Software is
9ae3a8
- * furnished to do so, subject to the following conditions:
9ae3a8
- *
9ae3a8
- * The above copyright notice and this permission notice shall be included in
9ae3a8
- * all copies or substantial portions of the Software.
9ae3a8
- *
9ae3a8
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
9ae3a8
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9ae3a8
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
9ae3a8
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9ae3a8
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9ae3a8
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
9ae3a8
- * THE SOFTWARE.
9ae3a8
- */
9ae3a8
-
9ae3a8
-#include "sysemu/blockdev.h"
9ae3a8
-#include "qemu/error-report.h"
9ae3a8
-#include "hw/sysbus.h"
9ae3a8
-#include "hw/hw.h"
9ae3a8
-#include "hw/i386/pc.h"
9ae3a8
-#include "hw/boards.h"
9ae3a8
-#include "hw/loader.h"
9ae3a8
-#include "sysemu/sysemu.h"
9ae3a8
-#include "hw/block/flash.h"
9ae3a8
-#include "sysemu/kvm.h"
9ae3a8
-
9ae3a8
-#define BIOS_FILENAME "bios.bin"
9ae3a8
-
9ae3a8
-typedef struct PcSysFwDevice {
9ae3a8
-    SysBusDevice busdev;
9ae3a8
-    uint8_t isapc_ram_fw;
9ae3a8
-} PcSysFwDevice;
9ae3a8
-
9ae3a8
-static void pc_isa_bios_init(MemoryRegion *rom_memory,
9ae3a8
-                             MemoryRegion *flash_mem,
9ae3a8
-                             int ram_size)
9ae3a8
-{
9ae3a8
-    int isa_bios_size;
9ae3a8
-    MemoryRegion *isa_bios;
9ae3a8
-    uint64_t flash_size;
9ae3a8
-    void *flash_ptr, *isa_bios_ptr;
9ae3a8
-
9ae3a8
-    flash_size = memory_region_size(flash_mem);
9ae3a8
-
9ae3a8
-    /* map the last 128KB of the BIOS in ISA space */
9ae3a8
-    isa_bios_size = MIN(flash_size, 128 * 1024);
9ae3a8
-    isa_bios = g_malloc(sizeof(*isa_bios));
9ae3a8
-    memory_region_init_ram(isa_bios, "isa-bios", isa_bios_size);
9ae3a8
-    vmstate_register_ram_global(isa_bios);
9ae3a8
-    memory_region_add_subregion_overlap(rom_memory,
9ae3a8
-                                        0x100000 - isa_bios_size,
9ae3a8
-                                        isa_bios,
9ae3a8
-                                        1);
9ae3a8
-
9ae3a8
-    /* copy ISA rom image from top of flash memory */
9ae3a8
-    flash_ptr = memory_region_get_ram_ptr(flash_mem);
9ae3a8
-    isa_bios_ptr = memory_region_get_ram_ptr(isa_bios);
9ae3a8
-    memcpy(isa_bios_ptr,
9ae3a8
-           ((uint8_t*)flash_ptr) + (flash_size - isa_bios_size),
9ae3a8
-           isa_bios_size);
9ae3a8
-
9ae3a8
-    memory_region_set_readonly(isa_bios, true);
9ae3a8
-}
9ae3a8
-
9ae3a8
-static void pc_system_flash_init(MemoryRegion *rom_memory,
9ae3a8
-                                 DriveInfo *pflash_drv)
9ae3a8
-{
9ae3a8
-    BlockDriverState *bdrv;
9ae3a8
-    int64_t size;
9ae3a8
-    hwaddr phys_addr;
9ae3a8
-    int sector_bits, sector_size;
9ae3a8
-    pflash_t *system_flash;
9ae3a8
-    MemoryRegion *flash_mem;
9ae3a8
-
9ae3a8
-    bdrv = pflash_drv->bdrv;
9ae3a8
-    size = bdrv_getlength(pflash_drv->bdrv);
9ae3a8
-    sector_bits = 12;
9ae3a8
-    sector_size = 1 << sector_bits;
9ae3a8
-
9ae3a8
-    if ((size % sector_size) != 0) {
9ae3a8
-        fprintf(stderr,
9ae3a8
-                "qemu: PC system firmware (pflash) must be a multiple of 0x%x\n",
9ae3a8
-                sector_size);
9ae3a8
-        exit(1);
9ae3a8
-    }
9ae3a8
-
9ae3a8
-    phys_addr = 0x100000000ULL - size;
9ae3a8
-    system_flash = pflash_cfi01_register(phys_addr, NULL, "system.flash", size,
9ae3a8
-                                         bdrv, sector_size, size >> sector_bits,
9ae3a8
-                                         1, 0x0000, 0x0000, 0x0000, 0x0000, 0);
9ae3a8
-    flash_mem = pflash_cfi01_get_memory(system_flash);
9ae3a8
-
9ae3a8
-    pc_isa_bios_init(rom_memory, flash_mem, size);
9ae3a8
-}
9ae3a8
-
9ae3a8
-static void old_pc_system_rom_init(MemoryRegion *rom_memory, bool isapc_ram_fw)
9ae3a8
-{
9ae3a8
-    char *filename;
9ae3a8
-    MemoryRegion *bios, *isa_bios;
9ae3a8
-    int bios_size, isa_bios_size;
9ae3a8
-    int ret;
9ae3a8
-
9ae3a8
-    /* BIOS load */
9ae3a8
-    if (bios_name == NULL) {
9ae3a8
-        bios_name = BIOS_FILENAME;
9ae3a8
-    }
9ae3a8
-    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
9ae3a8
-    if (filename) {
9ae3a8
-        bios_size = get_image_size(filename);
9ae3a8
-    } else {
9ae3a8
-        bios_size = -1;
9ae3a8
-    }
9ae3a8
-    if (bios_size <= 0 ||
9ae3a8
-        (bios_size % 65536) != 0) {
9ae3a8
-        goto bios_error;
9ae3a8
-    }
9ae3a8
-    bios = g_malloc(sizeof(*bios));
9ae3a8
-    memory_region_init_ram(bios, "pc.bios", bios_size);
9ae3a8
-    vmstate_register_ram_global(bios);
9ae3a8
-    if (!isapc_ram_fw) {
9ae3a8
-        memory_region_set_readonly(bios, true);
9ae3a8
-    }
9ae3a8
-    ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1);
9ae3a8
-    if (ret != 0) {
9ae3a8
-    bios_error:
9ae3a8
-        fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
9ae3a8
-        exit(1);
9ae3a8
-    }
9ae3a8
-    if (filename) {
9ae3a8
-        g_free(filename);
9ae3a8
-    }
9ae3a8
-
9ae3a8
-    /* map the last 128KB of the BIOS in ISA space */
9ae3a8
-    isa_bios_size = bios_size;
9ae3a8
-    if (isa_bios_size > (128 * 1024)) {
9ae3a8
-        isa_bios_size = 128 * 1024;
9ae3a8
-    }
9ae3a8
-    isa_bios = g_malloc(sizeof(*isa_bios));
9ae3a8
-    memory_region_init_alias(isa_bios, "isa-bios", bios,
9ae3a8
-                             bios_size - isa_bios_size, isa_bios_size);
9ae3a8
-    memory_region_add_subregion_overlap(rom_memory,
9ae3a8
-                                        0x100000 - isa_bios_size,
9ae3a8
-                                        isa_bios,
9ae3a8
-                                        1);
9ae3a8
-    if (!isapc_ram_fw) {
9ae3a8
-        memory_region_set_readonly(isa_bios, true);
9ae3a8
-    }
9ae3a8
-
9ae3a8
-    /* map all the bios at the top of memory */
9ae3a8
-    memory_region_add_subregion(rom_memory,
9ae3a8
-                                (uint32_t)(-bios_size),
9ae3a8
-                                bios);
9ae3a8
-}
9ae3a8
-
9ae3a8
-void pc_system_firmware_init(MemoryRegion *rom_memory)
9ae3a8
-{
9ae3a8
-    DriveInfo *pflash_drv;
9ae3a8
-    PcSysFwDevice *sysfw_dev;
9ae3a8
-
9ae3a8
-    /*
9ae3a8
-     * TODO This device exists only so that users can switch between
9ae3a8
-     * use of flash and ROM for the BIOS.  The ability to switch was
9ae3a8
-     * created because flash doesn't work with KVM.  Once it does, we
9ae3a8
-     * should drop this device.
9ae3a8
-     */
9ae3a8
-    sysfw_dev = (PcSysFwDevice*) qdev_create(NULL, "pc-sysfw");
9ae3a8
-
9ae3a8
-    qdev_init_nofail(DEVICE(sysfw_dev));
9ae3a8
-
9ae3a8
-    pflash_drv = drive_get(IF_PFLASH, 0, 0);
9ae3a8
-
9ae3a8
-    if (sysfw_dev->isapc_ram_fw || pflash_drv == NULL) {
9ae3a8
-        /* When a pflash drive is not found, use rom-mode */
9ae3a8
-        old_pc_system_rom_init(rom_memory, sysfw_dev->isapc_ram_fw);
9ae3a8
-        return;
9ae3a8
-    }
9ae3a8
-
9ae3a8
-    if (kvm_enabled() && !kvm_readonly_mem_enabled()) {
9ae3a8
-        /* Older KVM cannot execute from device memory. So, flash memory
9ae3a8
-         * cannot be used unless the readonly memory kvm capability is present. */
9ae3a8
-        fprintf(stderr, "qemu: pflash with kvm requires KVM readonly memory support\n");
9ae3a8
-        exit(1);
9ae3a8
-    }
9ae3a8
-
9ae3a8
-    pc_system_flash_init(rom_memory, pflash_drv);
9ae3a8
-}
9ae3a8
-
9ae3a8
-static Property pcsysfw_properties[] = {
9ae3a8
-    DEFINE_PROP_UINT8("isapc_ram_fw", PcSysFwDevice, isapc_ram_fw, 0),
9ae3a8
-    DEFINE_PROP_END_OF_LIST(),
9ae3a8
-};
9ae3a8
-
9ae3a8
-static int pcsysfw_init(DeviceState *dev)
9ae3a8
-{
9ae3a8
-    return 0;
9ae3a8
-}
9ae3a8
-
9ae3a8
-static void pcsysfw_class_init (ObjectClass *klass, void *data)
9ae3a8
-{
9ae3a8
-    DeviceClass *dc = DEVICE_CLASS (klass);
9ae3a8
-
9ae3a8
-    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
9ae3a8
-    dc->desc = "PC System Firmware";
9ae3a8
-    dc->init = pcsysfw_init;
9ae3a8
-    dc->props = pcsysfw_properties;
9ae3a8
-}
9ae3a8
-
9ae3a8
-static const TypeInfo pcsysfw_info = {
9ae3a8
-    .name          = "pc-sysfw",
9ae3a8
-    .parent        = TYPE_SYS_BUS_DEVICE,
9ae3a8
-    .instance_size = sizeof (PcSysFwDevice),
9ae3a8
-    .class_init    = pcsysfw_class_init,
9ae3a8
-};
9ae3a8
-
9ae3a8
-static void pcsysfw_register (void)
9ae3a8
-{
9ae3a8
-    type_register_static (&pcsysfw_info);
9ae3a8
-}
9ae3a8
-
9ae3a8
-type_init (pcsysfw_register);
9ae3a8
-
9ae3a8
diff --git a/hw/i386/Makefile.objs b/hw/i386/Makefile.objs
9ae3a8
index 6a17cf8..09ac433 100644
9ae3a8
--- a/hw/i386/Makefile.objs
9ae3a8
+++ b/hw/i386/Makefile.objs
9ae3a8
@@ -1,6 +1,7 @@
9ae3a8
 obj-$(CONFIG_KVM) += kvm/
9ae3a8
 obj-y += multiboot.o smbios.o
9ae3a8
 obj-y += pc.o pc_piix.o pc_q35.o
9ae3a8
+obj-y += pc_sysfw.o
9ae3a8
 obj-$(CONFIG_XEN) += xen_domainbuild.o xen_machine_pv.o
9ae3a8
 
9ae3a8
 obj-y += kvmvapic.o
9ae3a8
diff --git a/hw/i386/pc.c b/hw/i386/pc.c
9ae3a8
index 320f668..a0f0b24 100644
9ae3a8
--- a/hw/i386/pc.c
9ae3a8
+++ b/hw/i386/pc.c
9ae3a8
@@ -1162,7 +1162,7 @@ FWCfgState *pc_memory_init(MemoryRegion *system_memory,
9ae3a8
 
9ae3a8
 
9ae3a8
     /* Initialize PC system firmware */
9ae3a8
-    pc_system_firmware_init(rom_memory);
9ae3a8
+    pc_system_firmware_init(rom_memory, guest_info->isapc_ram_fw);
9ae3a8
 
9ae3a8
     option_rom_mr = g_malloc(sizeof(*option_rom_mr));
9ae3a8
     memory_region_init_ram(option_rom_mr, "pc.rom", PC_ROM_SIZE);
9ae3a8
diff --git a/hw/i386/pc_piix.c b/hw/i386/pc_piix.c
9ae3a8
index 6f273db..641f3d9 100644
9ae3a8
--- a/hw/i386/pc_piix.c
9ae3a8
+++ b/hw/i386/pc_piix.c
9ae3a8
@@ -141,6 +141,7 @@ static void pc_init1(QEMUMachineInitArgs *args,
9ae3a8
     guest_info->has_acpi_build = has_acpi_build;
9ae3a8
 
9ae3a8
     guest_info->has_pci_info = has_pci_info;
9ae3a8
+    guest_info->isapc_ram_fw = !pci_enabled;
9ae3a8
 
9ae3a8
     /* allocate ram and load rom/bios */
9ae3a8
     if (!xen_enabled()) {
9ae3a8
@@ -705,11 +706,6 @@ static QEMUMachine isapc_machine = {
9ae3a8
     .init = pc_init_isa,
9ae3a8
     .max_cpus = 1,
9ae3a8
     .compat_props = (GlobalProperty[]) {
9ae3a8
-        {
9ae3a8
-            .driver   = "pc-sysfw",
9ae3a8
-            .property = "isapc_ram_fw",
9ae3a8
-            .value    = stringify(1),
9ae3a8
-        },
9ae3a8
         { /* end of list */ }
9ae3a8
     },
9ae3a8
     DEFAULT_MACHINE_OPTIONS,
9ae3a8
diff --git a/hw/i386/pc_q35.c b/hw/i386/pc_q35.c
9ae3a8
index 7e46799..1ac46be 100644
9ae3a8
--- a/hw/i386/pc_q35.c
9ae3a8
+++ b/hw/i386/pc_q35.c
9ae3a8
@@ -114,6 +114,7 @@ static void pc_q35_init(QEMUMachineInitArgs *args)
9ae3a8
     guest_info = pc_guest_info_init(below_4g_mem_size, above_4g_mem_size);
9ae3a8
     guest_info->has_pci_info = has_pci_info;
9ae3a8
     guest_info->has_acpi_build = has_acpi_build;
9ae3a8
+    guest_info->isapc_ram_fw = false;
9ae3a8
 
9ae3a8
     /* allocate ram and load rom/bios */
9ae3a8
     if (!xen_enabled()) {
9ae3a8
diff --git a/hw/i386/pc_sysfw.c b/hw/i386/pc_sysfw.c
9ae3a8
new file mode 100644
9ae3a8
index 0000000..eb56dee
9ae3a8
--- /dev/null
9ae3a8
+++ b/hw/i386/pc_sysfw.c
9ae3a8
@@ -0,0 +1,185 @@
9ae3a8
+/*
9ae3a8
+ * QEMU PC System Firmware
9ae3a8
+ *
9ae3a8
+ * Copyright (c) 2003-2004 Fabrice Bellard
9ae3a8
+ * Copyright (c) 2011-2012 Intel Corporation
9ae3a8
+ *
9ae3a8
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
9ae3a8
+ * of this software and associated documentation files (the "Software"), to deal
9ae3a8
+ * in the Software without restriction, including without limitation the rights
9ae3a8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9ae3a8
+ * copies of the Software, and to permit persons to whom the Software is
9ae3a8
+ * furnished to do so, subject to the following conditions:
9ae3a8
+ *
9ae3a8
+ * The above copyright notice and this permission notice shall be included in
9ae3a8
+ * all copies or substantial portions of the Software.
9ae3a8
+ *
9ae3a8
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
9ae3a8
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9ae3a8
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
9ae3a8
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9ae3a8
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9ae3a8
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
9ae3a8
+ * THE SOFTWARE.
9ae3a8
+ */
9ae3a8
+
9ae3a8
+#include "sysemu/blockdev.h"
9ae3a8
+#include "qemu/error-report.h"
9ae3a8
+#include "hw/sysbus.h"
9ae3a8
+#include "hw/hw.h"
9ae3a8
+#include "hw/i386/pc.h"
9ae3a8
+#include "hw/boards.h"
9ae3a8
+#include "hw/loader.h"
9ae3a8
+#include "sysemu/sysemu.h"
9ae3a8
+#include "hw/block/flash.h"
9ae3a8
+#include "sysemu/kvm.h"
9ae3a8
+
9ae3a8
+#define BIOS_FILENAME "bios.bin"
9ae3a8
+
9ae3a8
+typedef struct PcSysFwDevice {
9ae3a8
+    SysBusDevice busdev;
9ae3a8
+    uint8_t isapc_ram_fw;
9ae3a8
+} PcSysFwDevice;
9ae3a8
+
9ae3a8
+static void pc_isa_bios_init(MemoryRegion *rom_memory,
9ae3a8
+                             MemoryRegion *flash_mem,
9ae3a8
+                             int ram_size)
9ae3a8
+{
9ae3a8
+    int isa_bios_size;
9ae3a8
+    MemoryRegion *isa_bios;
9ae3a8
+    uint64_t flash_size;
9ae3a8
+    void *flash_ptr, *isa_bios_ptr;
9ae3a8
+
9ae3a8
+    flash_size = memory_region_size(flash_mem);
9ae3a8
+
9ae3a8
+    /* map the last 128KB of the BIOS in ISA space */
9ae3a8
+    isa_bios_size = MIN(flash_size, 128 * 1024);
9ae3a8
+    isa_bios = g_malloc(sizeof(*isa_bios));
9ae3a8
+    memory_region_init_ram(isa_bios, "isa-bios", isa_bios_size);
9ae3a8
+    vmstate_register_ram_global(isa_bios);
9ae3a8
+    memory_region_add_subregion_overlap(rom_memory,
9ae3a8
+                                        0x100000 - isa_bios_size,
9ae3a8
+                                        isa_bios,
9ae3a8
+                                        1);
9ae3a8
+
9ae3a8
+    /* copy ISA rom image from top of flash memory */
9ae3a8
+    flash_ptr = memory_region_get_ram_ptr(flash_mem);
9ae3a8
+    isa_bios_ptr = memory_region_get_ram_ptr(isa_bios);
9ae3a8
+    memcpy(isa_bios_ptr,
9ae3a8
+           ((uint8_t*)flash_ptr) + (flash_size - isa_bios_size),
9ae3a8
+           isa_bios_size);
9ae3a8
+
9ae3a8
+    memory_region_set_readonly(isa_bios, true);
9ae3a8
+}
9ae3a8
+
9ae3a8
+static void pc_system_flash_init(MemoryRegion *rom_memory,
9ae3a8
+                                 DriveInfo *pflash_drv)
9ae3a8
+{
9ae3a8
+    BlockDriverState *bdrv;
9ae3a8
+    int64_t size;
9ae3a8
+    hwaddr phys_addr;
9ae3a8
+    int sector_bits, sector_size;
9ae3a8
+    pflash_t *system_flash;
9ae3a8
+    MemoryRegion *flash_mem;
9ae3a8
+
9ae3a8
+    bdrv = pflash_drv->bdrv;
9ae3a8
+    size = bdrv_getlength(pflash_drv->bdrv);
9ae3a8
+    sector_bits = 12;
9ae3a8
+    sector_size = 1 << sector_bits;
9ae3a8
+
9ae3a8
+    if ((size % sector_size) != 0) {
9ae3a8
+        fprintf(stderr,
9ae3a8
+                "qemu: PC system firmware (pflash) must be a multiple of 0x%x\n",
9ae3a8
+                sector_size);
9ae3a8
+        exit(1);
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    phys_addr = 0x100000000ULL - size;
9ae3a8
+    system_flash = pflash_cfi01_register(phys_addr, NULL, "system.flash", size,
9ae3a8
+                                         bdrv, sector_size, size >> sector_bits,
9ae3a8
+                                         1, 0x0000, 0x0000, 0x0000, 0x0000, 0);
9ae3a8
+    flash_mem = pflash_cfi01_get_memory(system_flash);
9ae3a8
+
9ae3a8
+    pc_isa_bios_init(rom_memory, flash_mem, size);
9ae3a8
+}
9ae3a8
+
9ae3a8
+static void old_pc_system_rom_init(MemoryRegion *rom_memory, bool isapc_ram_fw)
9ae3a8
+{
9ae3a8
+    char *filename;
9ae3a8
+    MemoryRegion *bios, *isa_bios;
9ae3a8
+    int bios_size, isa_bios_size;
9ae3a8
+    int ret;
9ae3a8
+
9ae3a8
+    /* BIOS load */
9ae3a8
+    if (bios_name == NULL) {
9ae3a8
+        bios_name = BIOS_FILENAME;
9ae3a8
+    }
9ae3a8
+    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
9ae3a8
+    if (filename) {
9ae3a8
+        bios_size = get_image_size(filename);
9ae3a8
+    } else {
9ae3a8
+        bios_size = -1;
9ae3a8
+    }
9ae3a8
+    if (bios_size <= 0 ||
9ae3a8
+        (bios_size % 65536) != 0) {
9ae3a8
+        goto bios_error;
9ae3a8
+    }
9ae3a8
+    bios = g_malloc(sizeof(*bios));
9ae3a8
+    memory_region_init_ram(bios, "pc.bios", bios_size);
9ae3a8
+    vmstate_register_ram_global(bios);
9ae3a8
+    if (!isapc_ram_fw) {
9ae3a8
+        memory_region_set_readonly(bios, true);
9ae3a8
+    }
9ae3a8
+    ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1);
9ae3a8
+    if (ret != 0) {
9ae3a8
+    bios_error:
9ae3a8
+        fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
9ae3a8
+        exit(1);
9ae3a8
+    }
9ae3a8
+    if (filename) {
9ae3a8
+        g_free(filename);
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    /* map the last 128KB of the BIOS in ISA space */
9ae3a8
+    isa_bios_size = bios_size;
9ae3a8
+    if (isa_bios_size > (128 * 1024)) {
9ae3a8
+        isa_bios_size = 128 * 1024;
9ae3a8
+    }
9ae3a8
+    isa_bios = g_malloc(sizeof(*isa_bios));
9ae3a8
+    memory_region_init_alias(isa_bios, "isa-bios", bios,
9ae3a8
+                             bios_size - isa_bios_size, isa_bios_size);
9ae3a8
+    memory_region_add_subregion_overlap(rom_memory,
9ae3a8
+                                        0x100000 - isa_bios_size,
9ae3a8
+                                        isa_bios,
9ae3a8
+                                        1);
9ae3a8
+    if (!isapc_ram_fw) {
9ae3a8
+        memory_region_set_readonly(isa_bios, true);
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    /* map all the bios at the top of memory */
9ae3a8
+    memory_region_add_subregion(rom_memory,
9ae3a8
+                                (uint32_t)(-bios_size),
9ae3a8
+                                bios);
9ae3a8
+}
9ae3a8
+
9ae3a8
+void pc_system_firmware_init(MemoryRegion *rom_memory, bool isapc_ram_fw)
9ae3a8
+{
9ae3a8
+    DriveInfo *pflash_drv;
9ae3a8
+
9ae3a8
+    pflash_drv = drive_get(IF_PFLASH, 0, 0);
9ae3a8
+
9ae3a8
+    if (isapc_ram_fw || pflash_drv == NULL) {
9ae3a8
+        /* When a pflash drive is not found, use rom-mode */
9ae3a8
+        old_pc_system_rom_init(rom_memory, isapc_ram_fw);
9ae3a8
+        return;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    if (kvm_enabled() && !kvm_readonly_mem_enabled()) {
9ae3a8
+        /* Older KVM cannot execute from device memory. So, flash memory
9ae3a8
+         * cannot be used unless the readonly memory kvm capability is present. */
9ae3a8
+        fprintf(stderr, "qemu: pflash with kvm requires KVM readonly memory support\n");
9ae3a8
+        exit(1);
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    pc_system_flash_init(rom_memory, pflash_drv);
9ae3a8
+}
9ae3a8
diff --git a/include/hw/i386/pc.h b/include/hw/i386/pc.h
9ae3a8
index 5c69abe..413358f 100644
9ae3a8
--- a/include/hw/i386/pc.h
9ae3a8
+++ b/include/hw/i386/pc.h
9ae3a8
@@ -34,6 +34,7 @@ typedef struct PcPciInfo {
9ae3a8
 
9ae3a8
 struct PcGuestInfo {
9ae3a8
     bool has_pci_info;
9ae3a8
+    bool isapc_ram_fw;
9ae3a8
     hwaddr ram_size;
9ae3a8
     unsigned apic_id_limit;
9ae3a8
     bool apic_xrupt_override;
9ae3a8
@@ -228,7 +229,8 @@ static inline bool isa_ne2000_init(ISABus *bus, int base, int irq, NICInfo *nd)
9ae3a8
 }
9ae3a8
 
9ae3a8
 /* pc_sysfw.c */
9ae3a8
-void pc_system_firmware_init(MemoryRegion *rom_memory);
9ae3a8
+void pc_system_firmware_init(MemoryRegion *rom_memory,
9ae3a8
+                             bool isapc_ram_fw);
9ae3a8
 
9ae3a8
 /* pvpanic.c */
9ae3a8
 void pvpanic_init(ISABus *bus);
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8