2 * QEMU i440FX PCI Bridge Emulation
4 * Copyright (c) 2006 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
26 #include "qemu/range.h"
27 #include "hw/i386/pc.h"
28 #include "hw/pci/pci.h"
29 #include "hw/pci/pci_host.h"
30 #include "hw/pci-host/i440fx.h"
31 #include "hw/qdev-properties.h"
32 #include "hw/sysbus.h"
33 #include "qapi/error.h"
34 #include "migration/vmstate.h"
35 #include "qapi/visitor.h"
36 #include "qemu/error-report.h"
39 * I440FX chipset data sheet.
40 * https://wiki.qemu.org/File:29054901.pdf
43 #define I440FX_PCI_HOST_BRIDGE(obj) \
44 OBJECT_CHECK(I440FXState, (obj), TYPE_I440FX_PCI_HOST_BRIDGE)
46 typedef struct I440FXState
{
47 PCIHostState parent_obj
;
49 uint64_t pci_hole64_size
;
51 uint32_t short_root_bus
;
54 #define I440FX_PAM 0x59
55 #define I440FX_PAM_SIZE 7
56 #define I440FX_SMRAM 0x72
58 /* Keep it 2G to comply with older win32 guests */
59 #define I440FX_PCI_HOST_HOLE64_SIZE_DEFAULT (1ULL << 31)
61 /* Older coreboot versions (4.0 and older) read a config register that doesn't
62 * exist in real hardware, to get the RAM size from QEMU.
64 #define I440FX_COREBOOT_RAM_SIZE 0x57
66 static void i440fx_update_memory_mappings(PCII440FXState
*d
)
69 PCIDevice
*pd
= PCI_DEVICE(d
);
71 memory_region_transaction_begin();
72 for (i
= 0; i
< ARRAY_SIZE(d
->pam_regions
); i
++) {
73 pam_update(&d
->pam_regions
[i
], i
,
74 pd
->config
[I440FX_PAM
+ DIV_ROUND_UP(i
, 2)]);
76 memory_region_set_enabled(&d
->smram_region
,
77 !(pd
->config
[I440FX_SMRAM
] & SMRAM_D_OPEN
));
78 memory_region_set_enabled(&d
->smram
,
79 pd
->config
[I440FX_SMRAM
] & SMRAM_G_SMRAME
);
80 memory_region_transaction_commit();
84 static void i440fx_write_config(PCIDevice
*dev
,
85 uint32_t address
, uint32_t val
, int len
)
87 PCII440FXState
*d
= I440FX_PCI_DEVICE(dev
);
89 /* XXX: implement SMRAM.D_LOCK */
90 pci_default_write_config(dev
, address
, val
, len
);
91 if (ranges_overlap(address
, len
, I440FX_PAM
, I440FX_PAM_SIZE
) ||
92 range_covers_byte(address
, len
, I440FX_SMRAM
)) {
93 i440fx_update_memory_mappings(d
);
97 static int i440fx_post_load(void *opaque
, int version_id
)
99 PCII440FXState
*d
= opaque
;
101 i440fx_update_memory_mappings(d
);
105 static const VMStateDescription vmstate_i440fx
= {
108 .minimum_version_id
= 3,
109 .post_load
= i440fx_post_load
,
110 .fields
= (VMStateField
[]) {
111 VMSTATE_PCI_DEVICE(parent_obj
, PCII440FXState
),
112 /* Used to be smm_enabled, which was basically always zero because
113 * SeaBIOS hardly uses SMM. SMRAM is now handled by CPU code.
116 VMSTATE_END_OF_LIST()
120 static void i440fx_pcihost_get_pci_hole_start(Object
*obj
, Visitor
*v
,
121 const char *name
, void *opaque
,
124 I440FXState
*s
= I440FX_PCI_HOST_BRIDGE(obj
);
128 val64
= range_is_empty(&s
->pci_hole
) ? 0 : range_lob(&s
->pci_hole
);
130 assert(value
== val64
);
131 visit_type_uint32(v
, name
, &value
, errp
);
134 static void i440fx_pcihost_get_pci_hole_end(Object
*obj
, Visitor
*v
,
135 const char *name
, void *opaque
,
138 I440FXState
*s
= I440FX_PCI_HOST_BRIDGE(obj
);
142 val64
= range_is_empty(&s
->pci_hole
) ? 0 : range_upb(&s
->pci_hole
) + 1;
144 assert(value
== val64
);
145 visit_type_uint32(v
, name
, &value
, errp
);
149 * The 64bit PCI hole start is set by the Guest firmware
150 * as the address of the first 64bit PCI MEM resource.
151 * If no PCI device has resources on the 64bit area,
152 * the 64bit PCI hole will start after "over 4G RAM" and the
153 * reserved space for memory hotplug if any.
155 static uint64_t i440fx_pcihost_get_pci_hole64_start_value(Object
*obj
)
157 PCIHostState
*h
= PCI_HOST_BRIDGE(obj
);
158 I440FXState
*s
= I440FX_PCI_HOST_BRIDGE(obj
);
162 pci_bus_get_w64_range(h
->bus
, &w64
);
163 value
= range_is_empty(&w64
) ? 0 : range_lob(&w64
);
164 if (!value
&& s
->pci_hole64_fix
) {
165 value
= pc_pci_hole64_start();
170 static void i440fx_pcihost_get_pci_hole64_start(Object
*obj
, Visitor
*v
,
172 void *opaque
, Error
**errp
)
174 uint64_t hole64_start
= i440fx_pcihost_get_pci_hole64_start_value(obj
);
176 visit_type_uint64(v
, name
, &hole64_start
, errp
);
180 * The 64bit PCI hole end is set by the Guest firmware
181 * as the address of the last 64bit PCI MEM resource.
182 * Then it is expanded to the PCI_HOST_PROP_PCI_HOLE64_SIZE
183 * that can be configured by the user.
185 static void i440fx_pcihost_get_pci_hole64_end(Object
*obj
, Visitor
*v
,
186 const char *name
, void *opaque
,
189 PCIHostState
*h
= PCI_HOST_BRIDGE(obj
);
190 I440FXState
*s
= I440FX_PCI_HOST_BRIDGE(obj
);
191 uint64_t hole64_start
= i440fx_pcihost_get_pci_hole64_start_value(obj
);
193 uint64_t value
, hole64_end
;
195 pci_bus_get_w64_range(h
->bus
, &w64
);
196 value
= range_is_empty(&w64
) ? 0 : range_upb(&w64
) + 1;
197 hole64_end
= ROUND_UP(hole64_start
+ s
->pci_hole64_size
, 1ULL << 30);
198 if (s
->pci_hole64_fix
&& value
< hole64_end
) {
201 visit_type_uint64(v
, name
, &value
, errp
);
204 static void i440fx_pcihost_initfn(Object
*obj
)
206 PCIHostState
*s
= PCI_HOST_BRIDGE(obj
);
208 memory_region_init_io(&s
->conf_mem
, obj
, &pci_host_conf_le_ops
, s
,
210 memory_region_init_io(&s
->data_mem
, obj
, &pci_host_data_le_ops
, s
,
213 object_property_add(obj
, PCI_HOST_PROP_PCI_HOLE_START
, "uint32",
214 i440fx_pcihost_get_pci_hole_start
,
215 NULL
, NULL
, NULL
, NULL
);
217 object_property_add(obj
, PCI_HOST_PROP_PCI_HOLE_END
, "uint32",
218 i440fx_pcihost_get_pci_hole_end
,
219 NULL
, NULL
, NULL
, NULL
);
221 object_property_add(obj
, PCI_HOST_PROP_PCI_HOLE64_START
, "uint64",
222 i440fx_pcihost_get_pci_hole64_start
,
223 NULL
, NULL
, NULL
, NULL
);
225 object_property_add(obj
, PCI_HOST_PROP_PCI_HOLE64_END
, "uint64",
226 i440fx_pcihost_get_pci_hole64_end
,
227 NULL
, NULL
, NULL
, NULL
);
230 static void i440fx_pcihost_realize(DeviceState
*dev
, Error
**errp
)
232 PCIHostState
*s
= PCI_HOST_BRIDGE(dev
);
233 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
235 sysbus_add_io(sbd
, 0xcf8, &s
->conf_mem
);
236 sysbus_init_ioports(sbd
, 0xcf8, 4);
238 sysbus_add_io(sbd
, 0xcfc, &s
->data_mem
);
239 sysbus_init_ioports(sbd
, 0xcfc, 4);
241 /* register i440fx 0xcf8 port as coalesced pio */
242 memory_region_set_flush_coalesced(&s
->data_mem
);
243 memory_region_add_coalescing(&s
->conf_mem
, 0, 4);
246 static void i440fx_realize(PCIDevice
*dev
, Error
**errp
)
248 dev
->config
[I440FX_SMRAM
] = 0x02;
250 if (object_property_get_bool(qdev_get_machine(), "iommu", NULL
)) {
251 warn_report("i440fx doesn't support emulated iommu");
255 PCIBus
*i440fx_init(const char *host_type
, const char *pci_type
,
256 PCII440FXState
**pi440fx_state
,
257 MemoryRegion
*address_space_mem
,
258 MemoryRegion
*address_space_io
,
260 ram_addr_t below_4g_mem_size
,
261 ram_addr_t above_4g_mem_size
,
262 MemoryRegion
*pci_address_space
,
263 MemoryRegion
*ram_memory
)
273 dev
= qdev_create(NULL
, host_type
);
274 s
= PCI_HOST_BRIDGE(dev
);
275 b
= pci_root_bus_new(dev
, NULL
, pci_address_space
,
276 address_space_io
, 0, TYPE_PCI_BUS
);
278 object_property_add_child(qdev_get_machine(), "i440fx", OBJECT(dev
), NULL
);
279 qdev_init_nofail(dev
);
281 d
= pci_create_simple(b
, 0, pci_type
);
282 *pi440fx_state
= I440FX_PCI_DEVICE(d
);
284 f
->system_memory
= address_space_mem
;
285 f
->pci_address_space
= pci_address_space
;
286 f
->ram_memory
= ram_memory
;
288 i440fx
= I440FX_PCI_HOST_BRIDGE(dev
);
289 range_set_bounds(&i440fx
->pci_hole
, below_4g_mem_size
,
290 IO_APIC_DEFAULT_ADDRESS
- 1);
292 /* setup pci memory mapping */
293 pc_pci_as_mapping_init(OBJECT(f
), f
->system_memory
,
294 f
->pci_address_space
);
296 /* if *disabled* show SMRAM to all CPUs */
297 memory_region_init_alias(&f
->smram_region
, OBJECT(d
), "smram-region",
298 f
->pci_address_space
, 0xa0000, 0x20000);
299 memory_region_add_subregion_overlap(f
->system_memory
, 0xa0000,
300 &f
->smram_region
, 1);
301 memory_region_set_enabled(&f
->smram_region
, true);
303 /* smram, as seen by SMM CPUs */
304 memory_region_init(&f
->smram
, OBJECT(d
), "smram", 1ull << 32);
305 memory_region_set_enabled(&f
->smram
, true);
306 memory_region_init_alias(&f
->low_smram
, OBJECT(d
), "smram-low",
307 f
->ram_memory
, 0xa0000, 0x20000);
308 memory_region_set_enabled(&f
->low_smram
, true);
309 memory_region_add_subregion(&f
->smram
, 0xa0000, &f
->low_smram
);
310 object_property_add_const_link(qdev_get_machine(), "smram",
311 OBJECT(&f
->smram
), &error_abort
);
313 init_pam(dev
, f
->ram_memory
, f
->system_memory
, f
->pci_address_space
,
314 &f
->pam_regions
[0], PAM_BIOS_BASE
, PAM_BIOS_SIZE
);
315 for (i
= 0; i
< ARRAY_SIZE(f
->pam_regions
) - 1; ++i
) {
316 init_pam(dev
, f
->ram_memory
, f
->system_memory
, f
->pci_address_space
,
317 &f
->pam_regions
[i
+1], PAM_EXPAN_BASE
+ i
* PAM_EXPAN_SIZE
,
321 ram_size
= ram_size
/ 8 / 1024 / 1024;
322 if (ram_size
> 255) {
325 d
->config
[I440FX_COREBOOT_RAM_SIZE
] = ram_size
;
327 i440fx_update_memory_mappings(f
);
332 PCIBus
*find_i440fx(void)
334 PCIHostState
*s
= OBJECT_CHECK(PCIHostState
,
335 object_resolve_path("/machine/i440fx", NULL
),
336 TYPE_PCI_HOST_BRIDGE
);
337 return s
? s
->bus
: NULL
;
340 static void i440fx_class_init(ObjectClass
*klass
, void *data
)
342 DeviceClass
*dc
= DEVICE_CLASS(klass
);
343 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
345 k
->realize
= i440fx_realize
;
346 k
->config_write
= i440fx_write_config
;
347 k
->vendor_id
= PCI_VENDOR_ID_INTEL
;
348 k
->device_id
= PCI_DEVICE_ID_INTEL_82441
;
350 k
->class_id
= PCI_CLASS_BRIDGE_HOST
;
351 dc
->desc
= "Host bridge";
352 dc
->vmsd
= &vmstate_i440fx
;
354 * PCI-facing part of the host bridge, not usable without the
355 * host-facing part, which can't be device_add'ed, yet.
357 dc
->user_creatable
= false;
358 dc
->hotpluggable
= false;
361 static const TypeInfo i440fx_info
= {
362 .name
= TYPE_I440FX_PCI_DEVICE
,
363 .parent
= TYPE_PCI_DEVICE
,
364 .instance_size
= sizeof(PCII440FXState
),
365 .class_init
= i440fx_class_init
,
366 .interfaces
= (InterfaceInfo
[]) {
367 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
372 static const char *i440fx_pcihost_root_bus_path(PCIHostState
*host_bridge
,
375 I440FXState
*s
= I440FX_PCI_HOST_BRIDGE(host_bridge
);
377 /* For backwards compat with old device paths */
378 if (s
->short_root_bus
) {
384 static Property i440fx_props
[] = {
385 DEFINE_PROP_SIZE(PCI_HOST_PROP_PCI_HOLE64_SIZE
, I440FXState
,
386 pci_hole64_size
, I440FX_PCI_HOST_HOLE64_SIZE_DEFAULT
),
387 DEFINE_PROP_UINT32("short_root_bus", I440FXState
, short_root_bus
, 0),
388 DEFINE_PROP_BOOL("x-pci-hole64-fix", I440FXState
, pci_hole64_fix
, true),
389 DEFINE_PROP_END_OF_LIST(),
392 static void i440fx_pcihost_class_init(ObjectClass
*klass
, void *data
)
394 DeviceClass
*dc
= DEVICE_CLASS(klass
);
395 PCIHostBridgeClass
*hc
= PCI_HOST_BRIDGE_CLASS(klass
);
397 hc
->root_bus_path
= i440fx_pcihost_root_bus_path
;
398 dc
->realize
= i440fx_pcihost_realize
;
400 device_class_set_props(dc
, i440fx_props
);
401 /* Reason: needs to be wired up by pc_init1 */
402 dc
->user_creatable
= false;
405 static const TypeInfo i440fx_pcihost_info
= {
406 .name
= TYPE_I440FX_PCI_HOST_BRIDGE
,
407 .parent
= TYPE_PCI_HOST_BRIDGE
,
408 .instance_size
= sizeof(I440FXState
),
409 .instance_init
= i440fx_pcihost_initfn
,
410 .class_init
= i440fx_pcihost_class_init
,
413 static void i440fx_register_types(void)
415 type_register_static(&i440fx_info
);
416 type_register_static(&i440fx_pcihost_info
);
419 type_init(i440fx_register_types
)