From patchwork Sun Jan 8 19:51:37 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: jordan.l.justen@intel.com X-Patchwork-Id: 134926 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 7258EB6F68 for ; Mon, 9 Jan 2012 06:53:24 +1100 (EST) Received: from localhost ([::1]:56538 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Rjynu-0005pW-Vz for incoming@patchwork.ozlabs.org; Sun, 08 Jan 2012 14:53:18 -0500 Received: from eggs.gnu.org ([140.186.70.92]:35569) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Rjynf-0005cn-5C for qemu-devel@nongnu.org; Sun, 08 Jan 2012 14:53:04 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Rjynd-0004oU-Hw for qemu-devel@nongnu.org; Sun, 08 Jan 2012 14:53:03 -0500 Received: from mga02.intel.com ([134.134.136.20]:43876) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Rjynd-0004mK-70 for qemu-devel@nongnu.org; Sun, 08 Jan 2012 14:53:01 -0500 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga101.jf.intel.com with ESMTP; 08 Jan 2012 11:53:00 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="4.67,352,1309762800"; d="scan'208";a="94193991" Received: from unknown (HELO jljusten-laptop.amr.corp.intel.com) ([10.255.13.217]) by orsmga001.jf.intel.com with ESMTP; 08 Jan 2012 11:53:00 -0800 From: Jordan Justen To: qemu-devel@nongnu.org Date: Sun, 8 Jan 2012 11:51:37 -0800 Message-Id: <1326052300-1172-6-git-send-email-jordan.l.justen@intel.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1326052300-1172-1-git-send-email-jordan.l.justen@intel.com> References: <1326052300-1172-1-git-send-email-jordan.l.justen@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 134.134.136.20 Cc: Jordan Justen Subject: [Qemu-devel] [PATCH v10 5/8] hw/pc_sysfw: Support system flash memory with pflash X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Flash can be enabled by calling pc_system_firmware_init with the system_flash_enabled parameter being non-zero. If system_flash_enabled is zero, then the older qemu rom creation method will be used. If flash is enabled and a pflash image is found, then it is used for the system firmware image. If flash is enabled and a pflash image is not initially found, then a read-only pflash device is created using the -bios filename. KVM cannot execute from a pflash region currently. Therefore, when KVM is enabled, a (read-only) ram memory region is created and filled with the contents of the pflash drive. Signed-off-by: Jordan Justen --- default-configs/i386-softmmu.mak | 1 + default-configs/x86_64-softmmu.mak | 1 + hw/pc.c | 2 +- hw/pc.h | 3 +- hw/pc_sysfw.c | 172 +++++++++++++++++++++++++++++++++++- 5 files changed, 173 insertions(+), 6 deletions(-) diff --git a/default-configs/i386-softmmu.mak b/default-configs/i386-softmmu.mak index e67ebb3..cd407a9 100644 --- a/default-configs/i386-softmmu.mak +++ b/default-configs/i386-softmmu.mak @@ -22,3 +22,4 @@ CONFIG_SOUND=y CONFIG_HPET=y CONFIG_APPLESMC=y CONFIG_I8259=y +CONFIG_PFLASH_CFI01=y diff --git a/default-configs/x86_64-softmmu.mak b/default-configs/x86_64-softmmu.mak index b75757e..47734ea 100644 --- a/default-configs/x86_64-softmmu.mak +++ b/default-configs/x86_64-softmmu.mak @@ -22,3 +22,4 @@ CONFIG_SOUND=y CONFIG_HPET=y CONFIG_APPLESMC=y CONFIG_I8259=y +CONFIG_PFLASH_CFI01=y diff --git a/hw/pc.c b/hw/pc.c index 47e946a..7a7af90 100644 --- a/hw/pc.c +++ b/hw/pc.c @@ -999,7 +999,7 @@ void pc_memory_init(MemoryRegion *system_memory, /* Initialize PC system firmware */ - pc_system_firmware_init(rom_memory); + pc_system_firmware_init(rom_memory, 0); option_rom_mr = g_malloc(sizeof(*option_rom_mr)); memory_region_init_ram(option_rom_mr, "pc.rom", PC_ROM_SIZE); diff --git a/hw/pc.h b/hw/pc.h index 227f495..ca5e76b 100644 --- a/hw/pc.h +++ b/hw/pc.h @@ -247,7 +247,8 @@ static inline bool isa_ne2000_init(ISABus *bus, int base, int irq, NICInfo *nd) } /* pc_sysfw.c */ -void pc_system_firmware_init(MemoryRegion *rom_memory); +void pc_system_firmware_init(MemoryRegion *rom_memory, + int system_flash_enabled); /* e820 types */ #define E820_RAM 1 diff --git a/hw/pc_sysfw.c b/hw/pc_sysfw.c index 3121c95..57ac81c 100644 --- a/hw/pc_sysfw.c +++ b/hw/pc_sysfw.c @@ -23,13 +23,151 @@ * THE SOFTWARE. */ +#include "hw.h" #include "pc.h" +#include "hw/boards.h" #include "loader.h" #include "sysemu.h" +#include "flash.h" +#include "kvm.h" #define BIOS_FILENAME "bios.bin" -static void pc_system_rom_init(MemoryRegion *rom_memory) +static void pc_isa_bios_init(MemoryRegion *rom_memory, + MemoryRegion *flash_mem, + int ram_size) +{ + int isa_bios_size; + MemoryRegion *isa_bios; + uint64_t flash_size; + void *flash_ptr, *isa_bios_ptr; + + flash_size = memory_region_size(flash_mem); + + /* map the last 128KB of the BIOS in ISA space */ + isa_bios_size = flash_size; + if (isa_bios_size > (128 * 1024)) { + isa_bios_size = 128 * 1024; + } + isa_bios = g_malloc(sizeof(*isa_bios)); + memory_region_init_ram(isa_bios, "isa-bios", isa_bios_size); + vmstate_register_ram_global(isa_bios); + memory_region_add_subregion_overlap(rom_memory, + 0x100000 - isa_bios_size, + isa_bios, + 1); + + /* copy ISA rom image from top of flash memory */ + flash_ptr = memory_region_get_ram_ptr(flash_mem); + isa_bios_ptr = memory_region_get_ram_ptr(isa_bios); + memcpy(isa_bios_ptr, + ((uint8_t*)flash_ptr) + (flash_size - isa_bios_size), + isa_bios_size); + + memory_region_set_readonly(isa_bios, true); +} + +static void pc_fw_add_pflash_drv(void) +{ + QemuOpts *opts; + QEMUMachine *machine; + char *filename; + + if (bios_name == NULL) { + bios_name = BIOS_FILENAME; + } + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); + + opts = drive_add(IF_PFLASH, -1, filename, "readonly=on"); + if (opts == NULL) { + return; + } + + machine = find_default_machine(); + if (machine == NULL) { + return; + } + + drive_init(opts, machine->use_scsi); +} + +static void pc_system_flash_init(MemoryRegion *rom_memory, + DriveInfo *pflash_drv) +{ + BlockDriverState *bdrv; + int64_t size; + target_phys_addr_t phys_addr; + int sector_bits, sector_size; + pflash_t *system_flash; + MemoryRegion *flash_mem; + + bdrv = pflash_drv->bdrv; + size = bdrv_getlength(pflash_drv->bdrv); + sector_bits = 12; + sector_size = 1 << sector_bits; + + if ((size % sector_size) != 0) { + fprintf(stderr, + "qemu: PC system firmware (pflash) must be a multiple of 0x%x\n", + sector_size); + exit(1); + } + + phys_addr = 0x100000000ULL - size; + system_flash = pflash_cfi01_register(phys_addr, NULL, "system.flash", size, + bdrv, sector_size, size >> sector_bits, + 1, 0x0000, 0x0000, 0x0000, 0x0000, 0); + flash_mem = pflash_cfi01_get_memory(system_flash); + + pc_isa_bios_init(rom_memory, flash_mem, size); +} + +static void pc_system_rom_init(MemoryRegion *rom_memory, + DriveInfo *pflash_drv) +{ + BlockDriverState *bdrv; + int64_t size; + target_phys_addr_t phys_addr; + int sector_bits, sector_size; + MemoryRegion *sys_rom; + void *buffer; + int ret; + + bdrv = pflash_drv->bdrv; + size = bdrv_getlength(pflash_drv->bdrv); + sector_bits = 9; + sector_size = 1 << sector_bits; + + if ((size % sector_size) != 0) { + fprintf(stderr, + "qemu: PC system rom (pflash) must be a multiple of 0x%x\n", + sector_size); + exit(1); + } + + phys_addr = 0x100000000ULL - size; + sys_rom = g_malloc(sizeof(*sys_rom)); + memory_region_init_ram(sys_rom, "system.rom", size); + vmstate_register_ram_global(sys_rom); + buffer = memory_region_get_ram_ptr(sys_rom); + memory_region_add_subregion(rom_memory, phys_addr, sys_rom); + + /* read the rom content */ + ret = bdrv_read(bdrv, 0, buffer, size >> sector_bits); + if (ret < 0) { + memory_region_destroy(sys_rom); + g_free(sys_rom); + fprintf(stderr, + "qemu: Failed to read rom image from pflash drive\n"); + exit(1); + } + + memory_region_set_readonly(sys_rom, true); + + pc_isa_bios_init(rom_memory, sys_rom, size); +} + +static void old_pc_system_rom_init(MemoryRegion *rom_memory) { char *filename; MemoryRegion *bios, *isa_bios; @@ -84,9 +222,35 @@ static void pc_system_rom_init(MemoryRegion *rom_memory) bios); } -void pc_system_firmware_init(MemoryRegion *rom_memory) +void pc_system_firmware_init(MemoryRegion *rom_memory, + int system_flash_enabled) { - pc_system_rom_init(rom_memory); -} + int flash_present; + DriveInfo *pflash_drv; + + if (!system_flash_enabled) { + old_pc_system_rom_init(rom_memory); + return; + } + + pflash_drv = drive_get(IF_PFLASH, 0, 0); + flash_present = (pflash_drv != NULL); + if (!flash_present) { + pc_fw_add_pflash_drv(); + pflash_drv = drive_get(IF_PFLASH, 0, 0); + flash_present = (pflash_drv != NULL); + } + + if (!flash_present) { + fprintf(stderr, "qemu: PC system firmware (pflash) not available\n"); + exit(1); + } + + if (!kvm_enabled()) { + pc_system_flash_init(rom_memory, pflash_drv); + } else { + pc_system_rom_init(rom_memory, pflash_drv); + } +}