hw/pci-host/q35: Remove unused includes
[qemu/ar7.git] / hw / pci-host / i440fx.c
blobd980c9704906cd5dd705abb7e1ed225a417c03d5
1 /*
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
22 * THE SOFTWARE.
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;
48 Range pci_hole;
49 uint64_t pci_hole64_size;
50 bool pci_hole64_fix;
51 uint32_t short_root_bus;
52 } I440FXState;
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)
68 int i;
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);
102 return 0;
105 static const VMStateDescription vmstate_i440fx = {
106 .name = "I440FX",
107 .version_id = 3,
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.
115 VMSTATE_UNUSED(1),
116 VMSTATE_END_OF_LIST()
120 static void i440fx_pcihost_get_pci_hole_start(Object *obj, Visitor *v,
121 const char *name, void *opaque,
122 Error **errp)
124 I440FXState *s = I440FX_PCI_HOST_BRIDGE(obj);
125 uint64_t val64;
126 uint32_t value;
128 val64 = range_is_empty(&s->pci_hole) ? 0 : range_lob(&s->pci_hole);
129 value = val64;
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,
136 Error **errp)
138 I440FXState *s = I440FX_PCI_HOST_BRIDGE(obj);
139 uint64_t val64;
140 uint32_t value;
142 val64 = range_is_empty(&s->pci_hole) ? 0 : range_upb(&s->pci_hole) + 1;
143 value = val64;
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);
159 Range w64;
160 uint64_t value;
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();
167 return value;
170 static void i440fx_pcihost_get_pci_hole64_start(Object *obj, Visitor *v,
171 const char *name,
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,
187 Error **errp)
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);
192 Range w64;
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) {
199 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,
209 "pci-conf-idx", 4);
210 memory_region_init_io(&s->data_mem, obj, &pci_host_data_le_ops, s,
211 "pci-conf-data", 4);
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,
259 ram_addr_t ram_size,
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)
265 DeviceState *dev;
266 PCIBus *b;
267 PCIDevice *d;
268 PCIHostState *s;
269 PCII440FXState *f;
270 unsigned i;
271 I440FXState *i440fx;
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);
277 s->bus = b;
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);
283 f = *pi440fx_state;
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,
318 PAM_EXPAN_SIZE);
321 ram_size = ram_size / 8 / 1024 / 1024;
322 if (ram_size > 255) {
323 ram_size = 255;
325 d->config[I440FX_COREBOOT_RAM_SIZE] = ram_size;
327 i440fx_update_memory_mappings(f);
329 return b;
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;
349 k->revision = 0x02;
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 },
368 { },
372 static const char *i440fx_pcihost_root_bus_path(PCIHostState *host_bridge,
373 PCIBus *rootbus)
375 I440FXState *s = I440FX_PCI_HOST_BRIDGE(host_bridge);
377 /* For backwards compat with old device paths */
378 if (s->short_root_bus) {
379 return "0000";
381 return "0000:00";
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;
399 dc->fw_name = "pci";
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)