/src/qemu/hw/pci-host/i440fx.c
Line | Count | Source |
1 | | /* |
2 | | * QEMU i440FX PCI Bridge Emulation |
3 | | * |
4 | | * Copyright (c) 2006 Fabrice Bellard |
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/units.h" |
27 | | #include "qemu/range.h" |
28 | | #include "hw/i386/pc.h" |
29 | | #include "hw/pci/pci.h" |
30 | | #include "hw/pci/pci_host.h" |
31 | | #include "hw/pci-host/i440fx.h" |
32 | | #include "hw/core/qdev-properties.h" |
33 | | #include "hw/core/sysbus.h" |
34 | | #include "qapi/error.h" |
35 | | #include "qapi/qapi-builtin-type-infos.h" |
36 | | #include "migration/vmstate.h" |
37 | | #include "qapi/visitor.h" |
38 | | #include "qemu/error-report.h" |
39 | | #include "qom/object.h" |
40 | | |
41 | | /* |
42 | | * I440FX chipset data sheet. |
43 | | * https://wiki.qemu.org/File:29054901.pdf |
44 | | */ |
45 | | |
46 | | OBJECT_DECLARE_SIMPLE_TYPE(I440FXState, I440FX_PCI_HOST_BRIDGE) |
47 | | |
48 | | struct I440FXState { |
49 | | PCIHostState parent_obj; |
50 | | |
51 | | MemoryRegion *system_memory; |
52 | | MemoryRegion *io_memory; |
53 | | MemoryRegion *pci_address_space; |
54 | | MemoryRegion *ram_memory; |
55 | | Range pci_hole; |
56 | | uint64_t below_4g_mem_size; |
57 | | uint64_t above_4g_mem_size; |
58 | | uint64_t pci_hole64_size; |
59 | | bool pci_hole64_fix; |
60 | | |
61 | | char *pci_type; |
62 | | }; |
63 | | |
64 | 104 | #define I440FX_PAM 0x59 |
65 | 0 | #define I440FX_PAM_SIZE 7 |
66 | 24 | #define I440FX_SMRAM 0x72 |
67 | | |
68 | | /* Keep it 2G to comply with older win32 guests */ |
69 | | #define I440FX_PCI_HOST_HOLE64_SIZE_DEFAULT (1ULL << 31) |
70 | | |
71 | | /* Older coreboot versions (4.0 and older) read a config register that doesn't |
72 | | * exist in real hardware, to get the RAM size from QEMU. |
73 | | */ |
74 | 8 | #define I440FX_COREBOOT_RAM_SIZE 0x57 |
75 | | |
76 | | static void i440fx_realize(PCIDevice *dev, Error **errp) |
77 | 8 | { |
78 | 8 | dev->config[I440FX_SMRAM] = 0x02; |
79 | | |
80 | 8 | if (object_property_get_bool(qdev_get_machine(), "iommu", NULL)) { |
81 | 0 | warn_report("i440fx doesn't support emulated iommu"); |
82 | 0 | } |
83 | 8 | } |
84 | | |
85 | | static void i440fx_update_memory_mappings(PCII440FXState *d) |
86 | 8 | { |
87 | 8 | int i; |
88 | 8 | PCIDevice *pd = PCI_DEVICE(d); |
89 | | |
90 | 8 | memory_region_transaction_begin(); |
91 | 112 | for (i = 0; i < ARRAY_SIZE(d->pam_regions); i++) { |
92 | 104 | pam_update(&d->pam_regions[i], i, |
93 | 104 | pd->config[I440FX_PAM + DIV_ROUND_UP(i, 2)]); |
94 | 104 | } |
95 | 8 | memory_region_set_enabled(&d->smram_region, |
96 | 8 | !(pd->config[I440FX_SMRAM] & SMRAM_D_OPEN)); |
97 | 8 | memory_region_set_enabled(&d->smram, |
98 | 8 | pd->config[I440FX_SMRAM] & SMRAM_G_SMRAME); |
99 | 8 | memory_region_transaction_commit(); |
100 | 8 | } |
101 | | |
102 | | |
103 | | static void i440fx_write_config(PCIDevice *dev, |
104 | | uint32_t address, uint32_t val, int len) |
105 | 0 | { |
106 | 0 | PCII440FXState *d = I440FX_PCI_DEVICE(dev); |
107 | | |
108 | | /* XXX: implement SMRAM.D_LOCK */ |
109 | 0 | pci_default_write_config(dev, address, val, len); |
110 | 0 | if (ranges_overlap(address, len, I440FX_PAM, I440FX_PAM_SIZE) || |
111 | 0 | range_covers_byte(address, len, I440FX_SMRAM)) { |
112 | 0 | i440fx_update_memory_mappings(d); |
113 | 0 | } |
114 | 0 | } |
115 | | |
116 | | static int i440fx_post_load(void *opaque, int version_id) |
117 | 0 | { |
118 | 0 | PCII440FXState *d = opaque; |
119 | |
|
120 | 0 | i440fx_update_memory_mappings(d); |
121 | 0 | return 0; |
122 | 0 | } |
123 | | |
124 | | static const VMStateDescription vmstate_i440fx = { |
125 | | .name = "I440FX", |
126 | | .version_id = 3, |
127 | | .minimum_version_id = 3, |
128 | | .post_load = i440fx_post_load, |
129 | | .fields = (const VMStateField[]) { |
130 | | VMSTATE_PCI_DEVICE(parent_obj, PCII440FXState), |
131 | | /* Used to be smm_enabled, which was basically always zero because |
132 | | * SeaBIOS hardly uses SMM. SMRAM is now handled by CPU code. |
133 | | */ |
134 | | VMSTATE_UNUSED(1), |
135 | | VMSTATE_END_OF_LIST() |
136 | | } |
137 | | }; |
138 | | |
139 | | static void i440fx_pcihost_get_pci_hole_start(Object *obj, Visitor *v, |
140 | | const char *name, void *opaque, |
141 | | Error **errp) |
142 | 8 | { |
143 | 8 | I440FXState *s = I440FX_PCI_HOST_BRIDGE(obj); |
144 | 8 | uint64_t val64; |
145 | 8 | uint32_t value; |
146 | | |
147 | 8 | val64 = range_is_empty(&s->pci_hole) ? 0 : range_lob(&s->pci_hole); |
148 | 8 | value = val64; |
149 | 8 | assert(value == val64); |
150 | 8 | visit_type_uint32(v, name, &value, errp); |
151 | 8 | } |
152 | | |
153 | | static void i440fx_pcihost_get_pci_hole_end(Object *obj, Visitor *v, |
154 | | const char *name, void *opaque, |
155 | | Error **errp) |
156 | 8 | { |
157 | 8 | I440FXState *s = I440FX_PCI_HOST_BRIDGE(obj); |
158 | 8 | uint64_t val64; |
159 | 8 | uint32_t value; |
160 | | |
161 | 8 | val64 = range_is_empty(&s->pci_hole) ? 0 : range_upb(&s->pci_hole) + 1; |
162 | 8 | value = val64; |
163 | 8 | assert(value == val64); |
164 | 8 | visit_type_uint32(v, name, &value, errp); |
165 | 8 | } |
166 | | |
167 | | /* |
168 | | * The 64bit PCI hole start is set by the Guest firmware |
169 | | * as the address of the first 64bit PCI MEM resource. |
170 | | * If no PCI device has resources on the 64bit area, |
171 | | * the 64bit PCI hole will start after "over 4G RAM" and the |
172 | | * reserved space for memory hotplug if any. |
173 | | */ |
174 | | static uint64_t i440fx_pcihost_get_pci_hole64_start_value(Object *obj) |
175 | 16 | { |
176 | 16 | PCIHostState *h = PCI_HOST_BRIDGE(obj); |
177 | 16 | I440FXState *s = I440FX_PCI_HOST_BRIDGE(obj); |
178 | 16 | Range w64; |
179 | 16 | uint64_t value; |
180 | | |
181 | 16 | pci_bus_get_w64_range(h->bus, &w64); |
182 | 16 | value = range_is_empty(&w64) ? 0 : range_lob(&w64); |
183 | 16 | if (!value && s->pci_hole64_fix) { |
184 | 16 | value = pc_pci_hole64_start(); |
185 | 16 | } |
186 | 16 | return value; |
187 | 16 | } |
188 | | |
189 | | static void i440fx_pcihost_get_pci_hole64_start(Object *obj, Visitor *v, |
190 | | const char *name, |
191 | | void *opaque, Error **errp) |
192 | 8 | { |
193 | 8 | PCIHostState *h = PCI_HOST_BRIDGE(obj); |
194 | 8 | uint64_t hole64_start; |
195 | | |
196 | 8 | if (!h->bus) { |
197 | 0 | error_setg(errp, "PCI host bridge not realized"); |
198 | 0 | return; |
199 | 0 | } |
200 | 8 | hole64_start = i440fx_pcihost_get_pci_hole64_start_value(obj); |
201 | 8 | visit_type_uint64(v, name, &hole64_start, errp); |
202 | 8 | } |
203 | | |
204 | | /* |
205 | | * The 64bit PCI hole end is set by the Guest firmware |
206 | | * as the address of the last 64bit PCI MEM resource. |
207 | | * Then it is expanded to the PCI_HOST_PROP_PCI_HOLE64_SIZE |
208 | | * that can be configured by the user. |
209 | | */ |
210 | | static void i440fx_pcihost_get_pci_hole64_end(Object *obj, Visitor *v, |
211 | | const char *name, void *opaque, |
212 | | Error **errp) |
213 | 8 | { |
214 | 8 | PCIHostState *h = PCI_HOST_BRIDGE(obj); |
215 | 8 | I440FXState *s = I440FX_PCI_HOST_BRIDGE(obj); |
216 | 8 | uint64_t hole64_start; |
217 | 8 | Range w64; |
218 | 8 | uint64_t value, hole64_end; |
219 | | |
220 | 8 | if (!h->bus) { |
221 | 0 | error_setg(errp, "PCI host bridge not realized"); |
222 | 0 | return; |
223 | 0 | } |
224 | 8 | hole64_start = i440fx_pcihost_get_pci_hole64_start_value(obj); |
225 | 8 | pci_bus_get_w64_range(h->bus, &w64); |
226 | 8 | value = range_is_empty(&w64) ? 0 : range_upb(&w64) + 1; |
227 | 8 | hole64_end = ROUND_UP(hole64_start + s->pci_hole64_size, 1ULL << 30); |
228 | 8 | if (s->pci_hole64_fix && value < hole64_end) { |
229 | 8 | value = hole64_end; |
230 | 8 | } |
231 | 8 | visit_type_uint64(v, name, &value, errp); |
232 | 8 | } |
233 | | |
234 | | static void i440fx_pcihost_initfn(Object *obj) |
235 | 8 | { |
236 | 8 | I440FXState *s = I440FX_PCI_HOST_BRIDGE(obj); |
237 | 8 | PCIHostState *phb = PCI_HOST_BRIDGE(obj); |
238 | | |
239 | 8 | memory_region_init_io(&phb->conf_mem, obj, &pci_host_conf_le_ops, phb, |
240 | 8 | "pci-conf-idx", 4); |
241 | 8 | memory_region_init_io(&phb->data_mem, obj, &pci_host_data_le_ops, phb, |
242 | 8 | "pci-conf-data", 4); |
243 | | |
244 | 8 | object_property_add_link(obj, PCI_HOST_PROP_RAM_MEM, TYPE_MEMORY_REGION, |
245 | 8 | (Object **) &s->ram_memory, |
246 | 8 | qdev_prop_allow_set_link_before_realize, 0); |
247 | | |
248 | 8 | object_property_add_link(obj, PCI_HOST_PROP_PCI_MEM, TYPE_MEMORY_REGION, |
249 | 8 | (Object **) &s->pci_address_space, |
250 | 8 | qdev_prop_allow_set_link_before_realize, 0); |
251 | | |
252 | 8 | object_property_add_link(obj, PCI_HOST_PROP_SYSTEM_MEM, TYPE_MEMORY_REGION, |
253 | 8 | (Object **) &s->system_memory, |
254 | 8 | qdev_prop_allow_set_link_before_realize, 0); |
255 | | |
256 | 8 | object_property_add_link(obj, PCI_HOST_PROP_IO_MEM, TYPE_MEMORY_REGION, |
257 | 8 | (Object **) &s->io_memory, |
258 | 8 | qdev_prop_allow_set_link_before_realize, 0); |
259 | 8 | } |
260 | | |
261 | | static void i440fx_pcihost_realize(DeviceState *dev, Error **errp) |
262 | 8 | { |
263 | 8 | ERRP_GUARD(); |
264 | 8 | I440FXState *s = I440FX_PCI_HOST_BRIDGE(dev); |
265 | 8 | PCIHostState *phb = PCI_HOST_BRIDGE(dev); |
266 | 8 | SysBusDevice *sbd = SYS_BUS_DEVICE(dev); |
267 | 8 | PCIBus *b; |
268 | 8 | PCIDevice *d; |
269 | 8 | PCII440FXState *f; |
270 | 8 | unsigned i; |
271 | | |
272 | 8 | memory_region_add_subregion(s->io_memory, 0xcf8, &phb->conf_mem); |
273 | 8 | sysbus_init_ioports(sbd, 0xcf8, 4); |
274 | | |
275 | 8 | memory_region_add_subregion(s->io_memory, 0xcfc, &phb->data_mem); |
276 | 8 | sysbus_init_ioports(sbd, 0xcfc, 4); |
277 | | |
278 | | /* register i440fx 0xcf8 port as coalesced pio */ |
279 | 8 | memory_region_set_flush_coalesced(&phb->data_mem); |
280 | 8 | memory_region_add_coalescing(&phb->conf_mem, 0, 4); |
281 | | |
282 | 8 | b = pci_root_bus_new(dev, NULL, s->pci_address_space, |
283 | 8 | s->io_memory, 0, TYPE_PCI_BUS); |
284 | 8 | phb->bus = b; |
285 | | |
286 | 8 | d = pci_create_simple(b, 0, s->pci_type); |
287 | 8 | f = I440FX_PCI_DEVICE(d); |
288 | | |
289 | 8 | range_set_bounds(&s->pci_hole, s->below_4g_mem_size, |
290 | 8 | IO_APIC_DEFAULT_ADDRESS - 1); |
291 | | |
292 | | /* setup pci memory mapping */ |
293 | 8 | pc_pci_as_mapping_init(s->system_memory, s->pci_address_space); |
294 | | |
295 | | /* if *disabled* show SMRAM to all CPUs */ |
296 | 8 | memory_region_init_alias(&f->smram_region, OBJECT(d), "smram-region", |
297 | 8 | s->pci_address_space, SMRAM_C_BASE, SMRAM_C_SIZE); |
298 | 8 | memory_region_add_subregion_overlap(s->system_memory, SMRAM_C_BASE, |
299 | 8 | &f->smram_region, 1); |
300 | 8 | memory_region_set_enabled(&f->smram_region, true); |
301 | | |
302 | | /* smram, as seen by SMM CPUs */ |
303 | 8 | memory_region_init(&f->smram, OBJECT(d), "smram", 4 * GiB); |
304 | 8 | memory_region_set_enabled(&f->smram, true); |
305 | 8 | memory_region_init_alias(&f->low_smram, OBJECT(d), "smram-low", |
306 | 8 | s->ram_memory, SMRAM_C_BASE, SMRAM_C_SIZE); |
307 | 8 | memory_region_set_enabled(&f->low_smram, true); |
308 | 8 | memory_region_add_subregion(&f->smram, SMRAM_C_BASE, &f->low_smram); |
309 | 8 | object_property_add_const_link(qdev_get_machine(), "smram", |
310 | 8 | OBJECT(&f->smram)); |
311 | | |
312 | 8 | init_pam(&f->pam_regions[0], OBJECT(d), s->ram_memory, s->system_memory, |
313 | 8 | s->pci_address_space, PAM_BIOS_BASE, PAM_BIOS_SIZE); |
314 | 104 | for (i = 0; i < ARRAY_SIZE(f->pam_regions) - 1; ++i) { |
315 | 96 | init_pam(&f->pam_regions[i + 1], OBJECT(d), s->ram_memory, |
316 | 96 | s->system_memory, s->pci_address_space, |
317 | 96 | PAM_EXPAN_BASE + i * PAM_EXPAN_SIZE, PAM_EXPAN_SIZE); |
318 | 96 | } |
319 | | |
320 | 8 | ram_addr_t ram_size = s->below_4g_mem_size + s->above_4g_mem_size; |
321 | 8 | ram_size = ram_size / 8 / 1024 / 1024; |
322 | 8 | if (ram_size > 255) { |
323 | 0 | ram_size = 255; |
324 | 0 | } |
325 | 8 | d->config[I440FX_COREBOOT_RAM_SIZE] = ram_size; |
326 | | |
327 | 8 | i440fx_update_memory_mappings(f); |
328 | 8 | } |
329 | | |
330 | | static void i440fx_class_init(ObjectClass *klass, const void *data) |
331 | 63 | { |
332 | 63 | DeviceClass *dc = DEVICE_CLASS(klass); |
333 | 63 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
334 | | |
335 | 63 | k->realize = i440fx_realize; |
336 | 63 | k->config_write = i440fx_write_config; |
337 | 63 | k->vendor_id = PCI_VENDOR_ID_INTEL; |
338 | 63 | k->device_id = PCI_DEVICE_ID_INTEL_82441; |
339 | 63 | k->revision = 0x02; |
340 | 63 | k->class_id = PCI_CLASS_BRIDGE_HOST; |
341 | 63 | dc->desc = "Host bridge"; |
342 | 63 | dc->vmsd = &vmstate_i440fx; |
343 | | /* |
344 | | * PCI-facing part of the host bridge, not usable without the |
345 | | * host-facing part, which can't be device_add'ed, yet. |
346 | | */ |
347 | 63 | dc->user_creatable = false; |
348 | 63 | dc->hotpluggable = false; |
349 | 63 | } |
350 | | |
351 | | static const TypeInfo i440fx_info = { |
352 | | .name = TYPE_I440FX_PCI_DEVICE, |
353 | | .parent = TYPE_PCI_DEVICE, |
354 | | .instance_size = sizeof(PCII440FXState), |
355 | | .class_init = i440fx_class_init, |
356 | | .secure = true, |
357 | | .interfaces = (const InterfaceInfo[]) { |
358 | | { INTERFACE_CONVENTIONAL_PCI_DEVICE }, |
359 | | { }, |
360 | | }, |
361 | | }; |
362 | | |
363 | | static const char *i440fx_pcihost_root_bus_path(PCIHostState *host_bridge, |
364 | | PCIBus *rootbus) |
365 | 42 | { |
366 | 42 | return "0000:00"; |
367 | 42 | } |
368 | | |
369 | | static const Property i440fx_props[] = { |
370 | | DEFINE_PROP_SIZE(PCI_HOST_PROP_PCI_HOLE64_SIZE, I440FXState, |
371 | | pci_hole64_size, I440FX_PCI_HOST_HOLE64_SIZE_DEFAULT), |
372 | | DEFINE_PROP_SIZE(PCI_HOST_BELOW_4G_MEM_SIZE, I440FXState, |
373 | | below_4g_mem_size, 0), |
374 | | DEFINE_PROP_SIZE(PCI_HOST_ABOVE_4G_MEM_SIZE, I440FXState, |
375 | | above_4g_mem_size, 0), |
376 | | DEFINE_PROP_BOOL("x-pci-hole64-fix", I440FXState, pci_hole64_fix, true), |
377 | | DEFINE_PROP_STRING(I440FX_HOST_PROP_PCI_TYPE, I440FXState, pci_type), |
378 | | }; |
379 | | |
380 | | static void i440fx_pcihost_class_init(ObjectClass *klass, const void *data) |
381 | 63 | { |
382 | 63 | DeviceClass *dc = DEVICE_CLASS(klass); |
383 | 63 | PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(klass); |
384 | | |
385 | 63 | hc->root_bus_path = i440fx_pcihost_root_bus_path; |
386 | 63 | dc->realize = i440fx_pcihost_realize; |
387 | 63 | dc->fw_name = "pci"; |
388 | 63 | device_class_set_props(dc, i440fx_props); |
389 | | /* Reason: needs to be wired up by pc_init1 */ |
390 | 63 | dc->user_creatable = false; |
391 | | |
392 | 63 | object_class_property_add(klass, PCI_HOST_PROP_PCI_HOLE_START, "uint32", |
393 | 63 | &uint32_type_info, |
394 | 63 | i440fx_pcihost_get_pci_hole_start, |
395 | 63 | NULL, NULL, NULL); |
396 | | |
397 | 63 | object_class_property_add(klass, PCI_HOST_PROP_PCI_HOLE_END, "uint32", |
398 | 63 | &uint32_type_info, |
399 | 63 | i440fx_pcihost_get_pci_hole_end, |
400 | 63 | NULL, NULL, NULL); |
401 | | |
402 | 63 | object_class_property_add(klass, PCI_HOST_PROP_PCI_HOLE64_START, "uint64", |
403 | 63 | &uint64_type_info, |
404 | 63 | i440fx_pcihost_get_pci_hole64_start, |
405 | 63 | NULL, NULL, NULL); |
406 | | |
407 | 63 | object_class_property_add(klass, PCI_HOST_PROP_PCI_HOLE64_END, "uint64", |
408 | 63 | &uint64_type_info, |
409 | 63 | i440fx_pcihost_get_pci_hole64_end, |
410 | 63 | NULL, NULL, NULL); |
411 | 63 | } |
412 | | |
413 | | static const TypeInfo i440fx_pcihost_info = { |
414 | | .name = TYPE_I440FX_PCI_HOST_BRIDGE, |
415 | | .parent = TYPE_PCI_HOST_BRIDGE, |
416 | | .instance_size = sizeof(I440FXState), |
417 | | .instance_init = i440fx_pcihost_initfn, |
418 | | .class_init = i440fx_pcihost_class_init, |
419 | | .secure = true, |
420 | | }; |
421 | | |
422 | | static void i440fx_register_types(void) |
423 | 63 | { |
424 | 63 | type_register_static(&i440fx_info); |
425 | 63 | type_register_static(&i440fx_pcihost_info); |
426 | 63 | } |
427 | | |
428 | | type_init(i440fx_register_types) |