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 "hw/i386/pc.h"
27 #include "hw/pci/pci.h"
28 #include "hw/pci/pci_host.h"
29 #include "hw/pci-host/i440fx.h"
30 #include "hw/qdev-properties.h"
31 #include "hw/sysbus.h"
32 #include "qapi/error.h"
33 #include "migration/vmstate.h"
34 #include "qapi/visitor.h"
35 #include "qemu/error-report.h"
38 * I440FX chipset data sheet.
39 * https://wiki.qemu.org/File:29054901.pdf
42 #define I440FX_PCI_HOST_BRIDGE(obj) \
43 OBJECT_CHECK(I440FXState, (obj), TYPE_I440FX_PCI_HOST_BRIDGE)
45 typedef struct I440FXState
{
46 PCIHostState parent_obj
;
48 uint64_t pci_hole64_size
;
50 uint32_t short_root_bus
;
53 #define I440FX_PAM 0x59
54 #define I440FX_PAM_SIZE 7
55 #define I440FX_SMRAM 0x72
57 /* Keep it 2G to comply with older win32 guests */
58 #define I440FX_PCI_HOST_HOLE64_SIZE_DEFAULT (1ULL << 31)
60 /* Older coreboot versions (4.0 and older) read a config register that doesn't
61 * exist in real hardware, to get the RAM size from QEMU.
63 #define I440FX_COREBOOT_RAM_SIZE 0x57
65 static void i440fx_update_memory_mappings(PCII440FXState
*d
)
68 PCIDevice
*pd
= PCI_DEVICE(d
);
70 memory_region_transaction_begin();
71 for (i
= 0; i
< ARRAY_SIZE(d
->pam_regions
); i
++) {
72 pam_update(&d
->pam_regions
[i
], i
,
73 pd
->config
[I440FX_PAM
+ DIV_ROUND_UP(i
, 2)]);
75 memory_region_set_enabled(&d
->smram_region
,
76 !(pd
->config
[I440FX_SMRAM
] & SMRAM_D_OPEN
));
77 memory_region_set_enabled(&d
->smram
,
78 pd
->config
[I440FX_SMRAM
] & SMRAM_G_SMRAME
);
79 memory_region_transaction_commit();
83 static void i440fx_write_config(PCIDevice
*dev
,
84 uint32_t address
, uint32_t val
, int len
)
86 PCII440FXState
*d
= I440FX_PCI_DEVICE(dev
);
88 /* XXX: implement SMRAM.D_LOCK */
89 pci_default_write_config(dev
, address
, val
, len
);
90 if (ranges_overlap(address
, len
, I440FX_PAM
, I440FX_PAM_SIZE
) ||
91 range_covers_byte(address
, len
, I440FX_SMRAM
)) {
92 i440fx_update_memory_mappings(d
);
96 static int i440fx_post_load(void *opaque
, int version_id
)
98 PCII440FXState
*d
= opaque
;
100 i440fx_update_memory_mappings(d
);
104 static const VMStateDescription vmstate_i440fx
= {
107 .minimum_version_id
= 3,
108 .post_load
= i440fx_post_load
,
109 .fields
= (VMStateField
[]) {
110 VMSTATE_PCI_DEVICE(parent_obj
, PCII440FXState
),
111 /* Used to be smm_enabled, which was basically always zero because
112 * SeaBIOS hardly uses SMM. SMRAM is now handled by CPU code.
115 VMSTATE_END_OF_LIST()
119 static void i440fx_pcihost_get_pci_hole_start(Object
*obj
, Visitor
*v
,
120 const char *name
, void *opaque
,
123 I440FXState
*s
= I440FX_PCI_HOST_BRIDGE(obj
);
127 val64
= range_is_empty(&s
->pci_hole
) ? 0 : range_lob(&s
->pci_hole
);
129 assert(value
== val64
);
130 visit_type_uint32(v
, name
, &value
, errp
);
133 static void i440fx_pcihost_get_pci_hole_end(Object
*obj
, Visitor
*v
,
134 const char *name
, void *opaque
,
137 I440FXState
*s
= I440FX_PCI_HOST_BRIDGE(obj
);
141 val64
= range_is_empty(&s
->pci_hole
) ? 0 : range_upb(&s
->pci_hole
) + 1;
143 assert(value
== val64
);
144 visit_type_uint32(v
, name
, &value
, errp
);
148 * The 64bit PCI hole start is set by the Guest firmware
149 * as the address of the first 64bit PCI MEM resource.
150 * If no PCI device has resources on the 64bit area,
151 * the 64bit PCI hole will start after "over 4G RAM" and the
152 * reserved space for memory hotplug if any.
154 static uint64_t i440fx_pcihost_get_pci_hole64_start_value(Object
*obj
)
156 PCIHostState
*h
= PCI_HOST_BRIDGE(obj
);
157 I440FXState
*s
= I440FX_PCI_HOST_BRIDGE(obj
);
161 pci_bus_get_w64_range(h
->bus
, &w64
);
162 value
= range_is_empty(&w64
) ? 0 : range_lob(&w64
);
163 if (!value
&& s
->pci_hole64_fix
) {
164 value
= pc_pci_hole64_start();
169 static void i440fx_pcihost_get_pci_hole64_start(Object
*obj
, Visitor
*v
,
171 void *opaque
, Error
**errp
)
173 uint64_t hole64_start
= i440fx_pcihost_get_pci_hole64_start_value(obj
);
175 visit_type_uint64(v
, name
, &hole64_start
, errp
);
179 * The 64bit PCI hole end is set by the Guest firmware
180 * as the address of the last 64bit PCI MEM resource.
181 * Then it is expanded to the PCI_HOST_PROP_PCI_HOLE64_SIZE
182 * that can be configured by the user.
184 static void i440fx_pcihost_get_pci_hole64_end(Object
*obj
, Visitor
*v
,
185 const char *name
, void *opaque
,
188 PCIHostState
*h
= PCI_HOST_BRIDGE(obj
);
189 I440FXState
*s
= I440FX_PCI_HOST_BRIDGE(obj
);
190 uint64_t hole64_start
= i440fx_pcihost_get_pci_hole64_start_value(obj
);
192 uint64_t value
, hole64_end
;
194 pci_bus_get_w64_range(h
->bus
, &w64
);
195 value
= range_is_empty(&w64
) ? 0 : range_upb(&w64
) + 1;
196 hole64_end
= ROUND_UP(hole64_start
+ s
->pci_hole64_size
, 1ULL << 30);
197 if (s
->pci_hole64_fix
&& value
< hole64_end
) {
200 visit_type_uint64(v
, name
, &value
, errp
);
203 static void i440fx_pcihost_initfn(Object
*obj
)
205 PCIHostState
*s
= PCI_HOST_BRIDGE(obj
);
207 memory_region_init_io(&s
->conf_mem
, obj
, &pci_host_conf_le_ops
, s
,
209 memory_region_init_io(&s
->data_mem
, obj
, &pci_host_data_le_ops
, s
,
212 object_property_add(obj
, PCI_HOST_PROP_PCI_HOLE_START
, "uint32",
213 i440fx_pcihost_get_pci_hole_start
,
214 NULL
, NULL
, NULL
, NULL
);
216 object_property_add(obj
, PCI_HOST_PROP_PCI_HOLE_END
, "uint32",
217 i440fx_pcihost_get_pci_hole_end
,
218 NULL
, NULL
, NULL
, NULL
);
220 object_property_add(obj
, PCI_HOST_PROP_PCI_HOLE64_START
, "uint64",
221 i440fx_pcihost_get_pci_hole64_start
,
222 NULL
, NULL
, NULL
, NULL
);
224 object_property_add(obj
, PCI_HOST_PROP_PCI_HOLE64_END
, "uint64",
225 i440fx_pcihost_get_pci_hole64_end
,
226 NULL
, NULL
, NULL
, NULL
);
229 static void i440fx_pcihost_realize(DeviceState
*dev
, Error
**errp
)
231 PCIHostState
*s
= PCI_HOST_BRIDGE(dev
);
232 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
234 sysbus_add_io(sbd
, 0xcf8, &s
->conf_mem
);
235 sysbus_init_ioports(sbd
, 0xcf8, 4);
237 sysbus_add_io(sbd
, 0xcfc, &s
->data_mem
);
238 sysbus_init_ioports(sbd
, 0xcfc, 4);
240 /* register i440fx 0xcf8 port as coalesced pio */
241 memory_region_set_flush_coalesced(&s
->data_mem
);
242 memory_region_add_coalescing(&s
->conf_mem
, 0, 4);
245 static void i440fx_realize(PCIDevice
*dev
, Error
**errp
)
247 dev
->config
[I440FX_SMRAM
] = 0x02;
249 if (object_property_get_bool(qdev_get_machine(), "iommu", NULL
)) {
250 warn_report("i440fx doesn't support emulated iommu");
254 PCIBus
*i440fx_init(const char *host_type
, const char *pci_type
,
255 PCII440FXState
**pi440fx_state
,
256 MemoryRegion
*address_space_mem
,
257 MemoryRegion
*address_space_io
,
259 ram_addr_t below_4g_mem_size
,
260 ram_addr_t above_4g_mem_size
,
261 MemoryRegion
*pci_address_space
,
262 MemoryRegion
*ram_memory
)
272 dev
= qdev_create(NULL
, host_type
);
273 s
= PCI_HOST_BRIDGE(dev
);
274 b
= pci_root_bus_new(dev
, NULL
, pci_address_space
,
275 address_space_io
, 0, TYPE_PCI_BUS
);
277 object_property_add_child(qdev_get_machine(), "i440fx", OBJECT(dev
), NULL
);
278 qdev_init_nofail(dev
);
280 d
= pci_create_simple(b
, 0, pci_type
);
281 *pi440fx_state
= I440FX_PCI_DEVICE(d
);
283 f
->system_memory
= address_space_mem
;
284 f
->pci_address_space
= pci_address_space
;
285 f
->ram_memory
= ram_memory
;
287 i440fx
= I440FX_PCI_HOST_BRIDGE(dev
);
288 range_set_bounds(&i440fx
->pci_hole
, below_4g_mem_size
,
289 IO_APIC_DEFAULT_ADDRESS
- 1);
291 /* setup pci memory mapping */
292 pc_pci_as_mapping_init(OBJECT(f
), f
->system_memory
,
293 f
->pci_address_space
);
295 /* if *disabled* show SMRAM to all CPUs */
296 memory_region_init_alias(&f
->smram_region
, OBJECT(d
), "smram-region",
297 f
->pci_address_space
, 0xa0000, 0x20000);
298 memory_region_add_subregion_overlap(f
->system_memory
, 0xa0000,
299 &f
->smram_region
, 1);
300 memory_region_set_enabled(&f
->smram_region
, true);
302 /* smram, as seen by SMM CPUs */
303 memory_region_init(&f
->smram
, OBJECT(d
), "smram", 1ull << 32);
304 memory_region_set_enabled(&f
->smram
, true);
305 memory_region_init_alias(&f
->low_smram
, OBJECT(d
), "smram-low",
306 f
->ram_memory
, 0xa0000, 0x20000);
307 memory_region_set_enabled(&f
->low_smram
, true);
308 memory_region_add_subregion(&f
->smram
, 0xa0000, &f
->low_smram
);
309 object_property_add_const_link(qdev_get_machine(), "smram",
310 OBJECT(&f
->smram
), &error_abort
);
312 init_pam(dev
, f
->ram_memory
, f
->system_memory
, f
->pci_address_space
,
313 &f
->pam_regions
[0], PAM_BIOS_BASE
, PAM_BIOS_SIZE
);
314 for (i
= 0; i
< ARRAY_SIZE(f
->pam_regions
) - 1; ++i
) {
315 init_pam(dev
, f
->ram_memory
, f
->system_memory
, f
->pci_address_space
,
316 &f
->pam_regions
[i
+1], PAM_EXPAN_BASE
+ i
* PAM_EXPAN_SIZE
,
320 ram_size
= ram_size
/ 8 / 1024 / 1024;
321 if (ram_size
> 255) {
324 d
->config
[I440FX_COREBOOT_RAM_SIZE
] = ram_size
;
326 i440fx_update_memory_mappings(f
);
331 PCIBus
*find_i440fx(void)
333 PCIHostState
*s
= OBJECT_CHECK(PCIHostState
,
334 object_resolve_path("/machine/i440fx", NULL
),
335 TYPE_PCI_HOST_BRIDGE
);
336 return s
? s
->bus
: NULL
;
339 static void i440fx_class_init(ObjectClass
*klass
, void *data
)
341 DeviceClass
*dc
= DEVICE_CLASS(klass
);
342 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
344 k
->realize
= i440fx_realize
;
345 k
->config_write
= i440fx_write_config
;
346 k
->vendor_id
= PCI_VENDOR_ID_INTEL
;
347 k
->device_id
= PCI_DEVICE_ID_INTEL_82441
;
349 k
->class_id
= PCI_CLASS_BRIDGE_HOST
;
350 dc
->desc
= "Host bridge";
351 dc
->vmsd
= &vmstate_i440fx
;
353 * PCI-facing part of the host bridge, not usable without the
354 * host-facing part, which can't be device_add'ed, yet.
356 dc
->user_creatable
= false;
357 dc
->hotpluggable
= false;
360 static const TypeInfo i440fx_info
= {
361 .name
= TYPE_I440FX_PCI_DEVICE
,
362 .parent
= TYPE_PCI_DEVICE
,
363 .instance_size
= sizeof(PCII440FXState
),
364 .class_init
= i440fx_class_init
,
365 .interfaces
= (InterfaceInfo
[]) {
366 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
371 static const char *i440fx_pcihost_root_bus_path(PCIHostState
*host_bridge
,
374 I440FXState
*s
= I440FX_PCI_HOST_BRIDGE(host_bridge
);
376 /* For backwards compat with old device paths */
377 if (s
->short_root_bus
) {
383 static Property i440fx_props
[] = {
384 DEFINE_PROP_SIZE(PCI_HOST_PROP_PCI_HOLE64_SIZE
, I440FXState
,
385 pci_hole64_size
, I440FX_PCI_HOST_HOLE64_SIZE_DEFAULT
),
386 DEFINE_PROP_UINT32("short_root_bus", I440FXState
, short_root_bus
, 0),
387 DEFINE_PROP_BOOL("x-pci-hole64-fix", I440FXState
, pci_hole64_fix
, true),
388 DEFINE_PROP_END_OF_LIST(),
391 static void i440fx_pcihost_class_init(ObjectClass
*klass
, void *data
)
393 DeviceClass
*dc
= DEVICE_CLASS(klass
);
394 PCIHostBridgeClass
*hc
= PCI_HOST_BRIDGE_CLASS(klass
);
396 hc
->root_bus_path
= i440fx_pcihost_root_bus_path
;
397 dc
->realize
= i440fx_pcihost_realize
;
399 dc
->props
= i440fx_props
;
400 /* Reason: needs to be wired up by pc_init1 */
401 dc
->user_creatable
= false;
404 static const TypeInfo i440fx_pcihost_info
= {
405 .name
= TYPE_I440FX_PCI_HOST_BRIDGE
,
406 .parent
= TYPE_PCI_HOST_BRIDGE
,
407 .instance_size
= sizeof(I440FXState
),
408 .instance_init
= i440fx_pcihost_initfn
,
409 .class_init
= i440fx_pcihost_class_init
,
412 static void i440fx_register_types(void)
414 type_register_static(&i440fx_info
);
415 type_register_static(&i440fx_pcihost_info
);
418 type_init(i440fx_register_types
)