tcg/i386: Always use TZCNT when available
[qemu/ar7.git] / hw / i386 / pc_sysfw.c
blobf915ad0a360886f5e23fc2e92ae759f365051c13
1 /*
2 * QEMU PC System Firmware
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 * Copyright (c) 2011-2012 Intel Corporation
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
26 #include "qemu/osdep.h"
27 #include "qapi/error.h"
28 #include "sysemu/block-backend.h"
29 #include "qemu/error-report.h"
30 #include "hw/sysbus.h"
31 #include "hw/hw.h"
32 #include "hw/i386/pc.h"
33 #include "hw/boards.h"
34 #include "hw/loader.h"
35 #include "sysemu/sysemu.h"
36 #include "hw/block/flash.h"
37 #include "sysemu/kvm.h"
39 #define BIOS_FILENAME "bios.bin"
41 typedef struct PcSysFwDevice {
42 SysBusDevice busdev;
43 uint8_t isapc_ram_fw;
44 } PcSysFwDevice;
46 static void pc_isa_bios_init(MemoryRegion *rom_memory,
47 MemoryRegion *flash_mem,
48 int ram_size)
50 int isa_bios_size;
51 MemoryRegion *isa_bios;
52 uint64_t flash_size;
53 void *flash_ptr, *isa_bios_ptr;
55 flash_size = memory_region_size(flash_mem);
57 /* map the last 128KB of the BIOS in ISA space */
58 isa_bios_size = MIN(flash_size, 128 * 1024);
59 isa_bios = g_malloc(sizeof(*isa_bios));
60 memory_region_init_ram(isa_bios, NULL, "isa-bios", isa_bios_size,
61 &error_fatal);
62 vmstate_register_ram_global(isa_bios);
63 memory_region_add_subregion_overlap(rom_memory,
64 0x100000 - isa_bios_size,
65 isa_bios,
66 1);
68 /* copy ISA rom image from top of flash memory */
69 flash_ptr = memory_region_get_ram_ptr(flash_mem);
70 isa_bios_ptr = memory_region_get_ram_ptr(isa_bios);
71 memcpy(isa_bios_ptr,
72 ((uint8_t*)flash_ptr) + (flash_size - isa_bios_size),
73 isa_bios_size);
75 memory_region_set_readonly(isa_bios, true);
78 #define FLASH_MAP_UNIT_MAX 2
80 /* We don't have a theoretically justifiable exact lower bound on the base
81 * address of any flash mapping. In practice, the IO-APIC MMIO range is
82 * [0xFEE00000..0xFEE01000[ -- see IO_APIC_DEFAULT_ADDRESS --, leaving free
83 * only 18MB-4KB below 4G. For now, restrict the cumulative mapping to 8MB in
84 * size.
86 #define FLASH_MAP_BASE_MIN ((hwaddr)(0x100000000ULL - 8*1024*1024))
88 /* This function maps flash drives from 4G downward, in order of their unit
89 * numbers. The mapping starts at unit#0, with unit number increments of 1, and
90 * stops before the first missing flash drive, or before
91 * unit#FLASH_MAP_UNIT_MAX, whichever is reached first.
93 * Addressing within one flash drive is of course not reversed.
95 * An error message is printed and the process exits if:
96 * - the size of the backing file for a flash drive is non-positive, or not a
97 * multiple of the required sector size, or
98 * - the current mapping's base address would fall below FLASH_MAP_BASE_MIN.
100 * The drive with unit#0 (if available) is mapped at the highest address, and
101 * it is passed to pc_isa_bios_init(). Merging several drives for isa-bios is
102 * not supported.
104 static void pc_system_flash_init(MemoryRegion *rom_memory)
106 int unit;
107 DriveInfo *pflash_drv;
108 BlockBackend *blk;
109 int64_t size;
110 char *fatal_errmsg = NULL;
111 hwaddr phys_addr = 0x100000000ULL;
112 int sector_bits, sector_size;
113 pflash_t *system_flash;
114 MemoryRegion *flash_mem;
115 char name[64];
117 sector_bits = 12;
118 sector_size = 1 << sector_bits;
120 for (unit = 0;
121 (unit < FLASH_MAP_UNIT_MAX &&
122 (pflash_drv = drive_get(IF_PFLASH, 0, unit)) != NULL);
123 ++unit) {
124 blk = blk_by_legacy_dinfo(pflash_drv);
125 size = blk_getlength(blk);
126 if (size < 0) {
127 fatal_errmsg = g_strdup_printf("failed to get backing file size");
128 } else if (size == 0) {
129 fatal_errmsg = g_strdup_printf("PC system firmware (pflash) "
130 "cannot have zero size");
131 } else if ((size % sector_size) != 0) {
132 fatal_errmsg = g_strdup_printf("PC system firmware (pflash) "
133 "must be a multiple of 0x%x", sector_size);
134 } else if (phys_addr < size || phys_addr - size < FLASH_MAP_BASE_MIN) {
135 fatal_errmsg = g_strdup_printf("oversized backing file, pflash "
136 "segments cannot be mapped under "
137 TARGET_FMT_plx, FLASH_MAP_BASE_MIN);
139 if (fatal_errmsg != NULL) {
140 Location loc;
142 /* push a new, "none" location on the location stack; overwrite its
143 * contents with the location saved in the option; print the error
144 * (includes location); pop the top
146 loc_push_none(&loc);
147 if (pflash_drv->opts != NULL) {
148 qemu_opts_loc_restore(pflash_drv->opts);
150 error_report("%s", fatal_errmsg);
151 loc_pop(&loc);
152 g_free(fatal_errmsg);
153 exit(1);
156 phys_addr -= size;
158 /* pflash_cfi01_register() creates a deep copy of the name */
159 snprintf(name, sizeof name, "system.flash%d", unit);
160 system_flash = pflash_cfi01_register(phys_addr, NULL /* qdev */, name,
161 size, blk, sector_size,
162 size >> sector_bits,
163 1 /* width */,
164 0x0000 /* id0 */,
165 0x0000 /* id1 */,
166 0x0000 /* id2 */,
167 0x0000 /* id3 */,
168 0 /* be */);
169 if (unit == 0) {
170 flash_mem = pflash_cfi01_get_memory(system_flash);
171 pc_isa_bios_init(rom_memory, flash_mem, size);
176 static void old_pc_system_rom_init(MemoryRegion *rom_memory, bool isapc_ram_fw)
178 char *filename;
179 MemoryRegion *bios, *isa_bios;
180 int bios_size, isa_bios_size;
181 int ret;
183 /* BIOS load */
184 if (bios_name == NULL) {
185 bios_name = BIOS_FILENAME;
187 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
188 if (filename) {
189 bios_size = get_image_size(filename);
190 } else {
191 bios_size = -1;
193 if (bios_size <= 0 ||
194 (bios_size % 65536) != 0) {
195 goto bios_error;
197 bios = g_malloc(sizeof(*bios));
198 memory_region_init_ram(bios, NULL, "pc.bios", bios_size, &error_fatal);
199 vmstate_register_ram_global(bios);
200 if (!isapc_ram_fw) {
201 memory_region_set_readonly(bios, true);
203 ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1);
204 if (ret != 0) {
205 bios_error:
206 fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
207 exit(1);
209 g_free(filename);
211 /* map the last 128KB of the BIOS in ISA space */
212 isa_bios_size = bios_size;
213 if (isa_bios_size > (128 * 1024)) {
214 isa_bios_size = 128 * 1024;
216 isa_bios = g_malloc(sizeof(*isa_bios));
217 memory_region_init_alias(isa_bios, NULL, "isa-bios", bios,
218 bios_size - isa_bios_size, isa_bios_size);
219 memory_region_add_subregion_overlap(rom_memory,
220 0x100000 - isa_bios_size,
221 isa_bios,
223 if (!isapc_ram_fw) {
224 memory_region_set_readonly(isa_bios, true);
227 /* map all the bios at the top of memory */
228 memory_region_add_subregion(rom_memory,
229 (uint32_t)(-bios_size),
230 bios);
233 void pc_system_firmware_init(MemoryRegion *rom_memory, bool isapc_ram_fw)
235 DriveInfo *pflash_drv;
237 pflash_drv = drive_get(IF_PFLASH, 0, 0);
239 if (isapc_ram_fw || pflash_drv == NULL) {
240 /* When a pflash drive is not found, use rom-mode */
241 old_pc_system_rom_init(rom_memory, isapc_ram_fw);
242 return;
245 if (kvm_enabled() && !kvm_readonly_mem_enabled()) {
246 /* Older KVM cannot execute from device memory. So, flash memory
247 * cannot be used unless the readonly memory kvm capability is present. */
248 fprintf(stderr, "qemu: pflash with kvm requires KVM readonly memory support\n");
249 exit(1);
252 pc_system_flash_init(rom_memory);