]>
Commit | Line | Data |
---|---|---|
cbc5b5f3 JJ |
1 | /* |
2 | * QEMU PC System Firmware | |
3 | * | |
4 | * Copyright (c) 2003-2004 Fabrice Bellard | |
5 | * Copyright (c) 2011-2012 Intel Corporation | |
6 | * | |
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: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
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. | |
24 | */ | |
25 | ||
b6a0aa05 | 26 | #include "qemu/osdep.h" |
a8d25326 | 27 | #include "qemu-common.h" |
da34e65c | 28 | #include "qapi/error.h" |
fa1d36df | 29 | #include "sysemu/block-backend.h" |
b4a42f81 | 30 | #include "qemu/error-report.h" |
922a01a0 | 31 | #include "qemu/option.h" |
d471bf3e | 32 | #include "qemu/units.h" |
83c9f4ca | 33 | #include "hw/sysbus.h" |
549e984e | 34 | #include "hw/i386/x86.h" |
0d09e41a | 35 | #include "hw/i386/pc.h" |
83c9f4ca | 36 | #include "hw/loader.h" |
a27bd6c7 | 37 | #include "hw/qdev-properties.h" |
9c17d615 | 38 | #include "sysemu/sysemu.h" |
0d09e41a | 39 | #include "hw/block/flash.h" |
9c17d615 | 40 | #include "sysemu/kvm.h" |
cbc5b5f3 | 41 | |
ebc29e1b MA |
42 | /* |
43 | * We don't have a theoretically justifiable exact lower bound on the base | |
44 | * address of any flash mapping. In practice, the IO-APIC MMIO range is | |
45 | * [0xFEE00000..0xFEE01000] -- see IO_APIC_DEFAULT_ADDRESS --, leaving free | |
46 | * only 18MB-4KB below 4G. For now, restrict the cumulative mapping to 8MB in | |
47 | * size. | |
48 | */ | |
49 | #define FLASH_SIZE_LIMIT (8 * MiB) | |
50 | ||
51 | #define FLASH_SECTOR_SIZE 4096 | |
52 | ||
bd183c79 JJ |
53 | static void pc_isa_bios_init(MemoryRegion *rom_memory, |
54 | MemoryRegion *flash_mem, | |
55 | int ram_size) | |
56 | { | |
57 | int isa_bios_size; | |
58 | MemoryRegion *isa_bios; | |
59 | uint64_t flash_size; | |
60 | void *flash_ptr, *isa_bios_ptr; | |
61 | ||
62 | flash_size = memory_region_size(flash_mem); | |
63 | ||
64 | /* map the last 128KB of the BIOS in ISA space */ | |
d471bf3e | 65 | isa_bios_size = MIN(flash_size, 128 * KiB); |
bd183c79 | 66 | isa_bios = g_malloc(sizeof(*isa_bios)); |
98a99ce0 | 67 | memory_region_init_ram(isa_bios, NULL, "isa-bios", isa_bios_size, |
f8ed85ac | 68 | &error_fatal); |
bd183c79 JJ |
69 | memory_region_add_subregion_overlap(rom_memory, |
70 | 0x100000 - isa_bios_size, | |
71 | isa_bios, | |
72 | 1); | |
73 | ||
74 | /* copy ISA rom image from top of flash memory */ | |
75 | flash_ptr = memory_region_get_ram_ptr(flash_mem); | |
76 | isa_bios_ptr = memory_region_get_ram_ptr(isa_bios); | |
77 | memcpy(isa_bios_ptr, | |
78 | ((uint8_t*)flash_ptr) + (flash_size - isa_bios_size), | |
79 | isa_bios_size); | |
80 | ||
81 | memory_region_set_readonly(isa_bios, true); | |
82 | } | |
83 | ||
ebc29e1b MA |
84 | static PFlashCFI01 *pc_pflash_create(PCMachineState *pcms, |
85 | const char *name, | |
86 | const char *alias_prop_name) | |
87 | { | |
df707969 | 88 | DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01); |
637a5acb | 89 | |
ebc29e1b MA |
90 | qdev_prop_set_uint64(dev, "sector-length", FLASH_SECTOR_SIZE); |
91 | qdev_prop_set_uint8(dev, "width", 1); | |
92 | qdev_prop_set_string(dev, "name", name); | |
d2623129 | 93 | object_property_add_child(OBJECT(pcms), name, OBJECT(dev)); |
ebc29e1b | 94 | object_property_add_alias(OBJECT(pcms), alias_prop_name, |
d2623129 | 95 | OBJECT(dev), "drive"); |
0b33521e AB |
96 | /* |
97 | * The returned reference is tied to the child property and | |
98 | * will be removed with object_unparent. | |
99 | */ | |
100 | object_unref(OBJECT(dev)); | |
ebc29e1b MA |
101 | return PFLASH_CFI01(dev); |
102 | } | |
637a5acb | 103 | |
ebc29e1b MA |
104 | void pc_system_flash_create(PCMachineState *pcms) |
105 | { | |
106 | PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms); | |
107 | ||
108 | if (pcmc->pci_enabled) { | |
109 | pcms->flash[0] = pc_pflash_create(pcms, "system.flash0", | |
110 | "pflash0"); | |
111 | pcms->flash[1] = pc_pflash_create(pcms, "system.flash1", | |
112 | "pflash1"); | |
113 | } | |
114 | } | |
115 | ||
116 | static void pc_system_flash_cleanup_unused(PCMachineState *pcms) | |
117 | { | |
118 | char *prop_name; | |
119 | int i; | |
120 | Object *dev_obj; | |
121 | ||
122 | assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled); | |
123 | ||
124 | for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) { | |
125 | dev_obj = OBJECT(pcms->flash[i]); | |
126 | if (!object_property_get_bool(dev_obj, "realized", &error_abort)) { | |
127 | prop_name = g_strdup_printf("pflash%d", i); | |
df4fe0b2 | 128 | object_property_del(OBJECT(pcms), prop_name); |
ebc29e1b MA |
129 | g_free(prop_name); |
130 | object_unparent(dev_obj); | |
131 | pcms->flash[i] = NULL; | |
132 | } | |
133 | } | |
134 | } | |
135 | ||
136 | /* | |
137 | * Map the pcms->flash[] from 4GiB downward, and realize. | |
138 | * Map them in descending order, i.e. pcms->flash[0] at the top, | |
139 | * without gaps. | |
140 | * Stop at the first pcms->flash[0] lacking a block backend. | |
141 | * Set each flash's size from its block backend. Fatal error if the | |
142 | * size isn't a non-zero multiple of 4KiB, or the total size exceeds | |
143 | * FLASH_SIZE_LIMIT. | |
637a5acb | 144 | * |
ebc29e1b MA |
145 | * If pcms->flash[0] has a block backend, its memory is passed to |
146 | * pc_isa_bios_init(). Merging several flash devices for isa-bios is | |
637a5acb LE |
147 | * not supported. |
148 | */ | |
ebc29e1b MA |
149 | static void pc_system_flash_map(PCMachineState *pcms, |
150 | MemoryRegion *rom_memory) | |
bd183c79 | 151 | { |
ebc29e1b MA |
152 | hwaddr total_size = 0; |
153 | int i; | |
4be74634 | 154 | BlockBackend *blk; |
bd183c79 | 155 | int64_t size; |
16434065 | 156 | PFlashCFI01 *system_flash; |
bd183c79 | 157 | MemoryRegion *flash_mem; |
952e0668 BS |
158 | void *flash_ptr; |
159 | int ret, flash_size; | |
bd183c79 | 160 | |
ebc29e1b MA |
161 | assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled); |
162 | ||
163 | for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) { | |
164 | system_flash = pcms->flash[i]; | |
165 | blk = pflash_cfi01_get_blk(system_flash); | |
166 | if (!blk) { | |
167 | break; | |
168 | } | |
4be74634 | 169 | size = blk_getlength(blk); |
637a5acb | 170 | if (size < 0) { |
ebc29e1b MA |
171 | error_report("can't get size of block device %s: %s", |
172 | blk_name(blk), strerror(-size)); | |
173 | exit(1); | |
637a5acb | 174 | } |
4cdd0a77 | 175 | if (size == 0 || !QEMU_IS_ALIGNED(size, FLASH_SECTOR_SIZE)) { |
ebc29e1b MA |
176 | error_report("system firmware block device %s has invalid size " |
177 | "%" PRId64, | |
178 | blk_name(blk), size); | |
179 | info_report("its size must be a non-zero multiple of 0x%x", | |
180 | FLASH_SECTOR_SIZE); | |
181 | exit(1); | |
182 | } | |
183 | if ((hwaddr)size != size | |
184 | || total_size > HWADDR_MAX - size | |
185 | || total_size + size > FLASH_SIZE_LIMIT) { | |
186 | error_report("combined size of system firmware exceeds " | |
187 | "%" PRIu64 " bytes", | |
188 | FLASH_SIZE_LIMIT); | |
637a5acb LE |
189 | exit(1); |
190 | } | |
191 | ||
ebc29e1b MA |
192 | total_size += size; |
193 | qdev_prop_set_uint32(DEVICE(system_flash), "num-blocks", | |
194 | size / FLASH_SECTOR_SIZE); | |
3c6ef471 | 195 | sysbus_realize_and_unref(SYS_BUS_DEVICE(system_flash), &error_fatal); |
ebc29e1b MA |
196 | sysbus_mmio_map(SYS_BUS_DEVICE(system_flash), 0, |
197 | 0x100000000ULL - total_size); | |
198 | ||
199 | if (i == 0) { | |
637a5acb LE |
200 | flash_mem = pflash_cfi01_get_memory(system_flash); |
201 | pc_isa_bios_init(rom_memory, flash_mem, size); | |
952e0668 BS |
202 | |
203 | /* Encrypt the pflash boot ROM */ | |
204 | if (kvm_memcrypt_enabled()) { | |
205 | flash_ptr = memory_region_get_ram_ptr(flash_mem); | |
206 | flash_size = memory_region_size(flash_mem); | |
207 | ret = kvm_memcrypt_encrypt_data(flash_ptr, flash_size); | |
208 | if (ret) { | |
209 | error_report("failed to encrypt pflash rom"); | |
210 | exit(1); | |
211 | } | |
212 | } | |
637a5acb | 213 | } |
bd183c79 | 214 | } |
bd183c79 JJ |
215 | } |
216 | ||
5e640a9e PMD |
217 | void pc_system_firmware_init(PCMachineState *pcms, |
218 | MemoryRegion *rom_memory) | |
cbc5b5f3 | 219 | { |
5e640a9e | 220 | PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms); |
ebc29e1b | 221 | int i; |
ebc29e1b | 222 | BlockBackend *pflash_blk[ARRAY_SIZE(pcms->flash)]; |
1d38574f | 223 | |
ebc29e1b | 224 | if (!pcmc->pci_enabled) { |
81ef68e4 | 225 | x86_bios_rom_init(rom_memory, true); |
dafb82e0 | 226 | return; |
bd183c79 JJ |
227 | } |
228 | ||
ebc29e1b MA |
229 | /* Map legacy -drive if=pflash to machine properties */ |
230 | for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) { | |
2d731dbd MA |
231 | pflash_cfi01_legacy_drive(pcms->flash[i], |
232 | drive_get(IF_PFLASH, 0, i)); | |
c8d8ef00 | 233 | pflash_blk[i] = pflash_cfi01_get_blk(pcms->flash[i]); |
ebc29e1b MA |
234 | } |
235 | ||
236 | /* Reject gaps */ | |
237 | for (i = 1; i < ARRAY_SIZE(pcms->flash); i++) { | |
238 | if (pflash_blk[i] && !pflash_blk[i - 1]) { | |
239 | error_report("pflash%d requires pflash%d", i, i - 1); | |
240 | exit(1); | |
241 | } | |
242 | } | |
243 | ||
244 | if (!pflash_blk[0]) { | |
245 | /* Machine property pflash0 not set, use ROM mode */ | |
81ef68e4 | 246 | x86_bios_rom_init(rom_memory, false); |
ebc29e1b MA |
247 | } else { |
248 | if (kvm_enabled() && !kvm_readonly_mem_enabled()) { | |
249 | /* | |
250 | * Older KVM cannot execute from device memory. So, flash | |
251 | * memory cannot be used unless the readonly memory kvm | |
252 | * capability is present. | |
253 | */ | |
254 | error_report("pflash with kvm requires KVM readonly memory support"); | |
255 | exit(1); | |
256 | } | |
257 | ||
258 | pc_system_flash_map(pcms, rom_memory); | |
bd183c79 | 259 | } |
a904410a | 260 | |
ebc29e1b | 261 | pc_system_flash_cleanup_unused(pcms); |
cbc5b5f3 | 262 | } |