hw/i386: remove smram_update
[qemu.git] / hw / pci-host / q35.c
blob24829e0d525a85f14b29fe2d698424d388665b82
1 /*
2 * QEMU MCH/ICH9 PCI Bridge Emulation
4 * Copyright (c) 2006 Fabrice Bellard
5 * Copyright (c) 2009, 2010, 2011
6 * Isaku Yamahata <yamahata at valinux co jp>
7 * VA Linux Systems Japan K.K.
8 * Copyright (C) 2012 Jason Baron <jbaron@redhat.com>
10 * This is based on piix.c, but heavily modified.
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to deal
14 * in the Software without restriction, including without limitation the rights
15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 * copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 * THE SOFTWARE.
30 #include "hw/hw.h"
31 #include "hw/pci-host/q35.h"
32 #include "qapi/visitor.h"
34 /****************************************************************************
35 * Q35 host
38 static void q35_host_realize(DeviceState *dev, Error **errp)
40 PCIHostState *pci = PCI_HOST_BRIDGE(dev);
41 Q35PCIHost *s = Q35_HOST_DEVICE(dev);
42 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
44 sysbus_add_io(sbd, MCH_HOST_BRIDGE_CONFIG_ADDR, &pci->conf_mem);
45 sysbus_init_ioports(sbd, MCH_HOST_BRIDGE_CONFIG_ADDR, 4);
47 sysbus_add_io(sbd, MCH_HOST_BRIDGE_CONFIG_DATA, &pci->data_mem);
48 sysbus_init_ioports(sbd, MCH_HOST_BRIDGE_CONFIG_DATA, 4);
50 pci->bus = pci_bus_new(DEVICE(s), "pcie.0",
51 s->mch.pci_address_space, s->mch.address_space_io,
52 0, TYPE_PCIE_BUS);
53 qdev_set_parent_bus(DEVICE(&s->mch), BUS(pci->bus));
54 qdev_init_nofail(DEVICE(&s->mch));
57 static const char *q35_host_root_bus_path(PCIHostState *host_bridge,
58 PCIBus *rootbus)
60 Q35PCIHost *s = Q35_HOST_DEVICE(host_bridge);
62 /* For backwards compat with old device paths */
63 if (s->mch.short_root_bus) {
64 return "0000";
66 return "0000:00";
69 static void q35_host_get_pci_hole_start(Object *obj, Visitor *v,
70 void *opaque, const char *name,
71 Error **errp)
73 Q35PCIHost *s = Q35_HOST_DEVICE(obj);
74 uint32_t value = s->mch.pci_info.w32.begin;
76 visit_type_uint32(v, &value, name, errp);
79 static void q35_host_get_pci_hole_end(Object *obj, Visitor *v,
80 void *opaque, const char *name,
81 Error **errp)
83 Q35PCIHost *s = Q35_HOST_DEVICE(obj);
84 uint32_t value = s->mch.pci_info.w32.end;
86 visit_type_uint32(v, &value, name, errp);
89 static void q35_host_get_pci_hole64_start(Object *obj, Visitor *v,
90 void *opaque, const char *name,
91 Error **errp)
93 PCIHostState *h = PCI_HOST_BRIDGE(obj);
94 Range w64;
96 pci_bus_get_w64_range(h->bus, &w64);
98 visit_type_uint64(v, &w64.begin, name, errp);
101 static void q35_host_get_pci_hole64_end(Object *obj, Visitor *v,
102 void *opaque, const char *name,
103 Error **errp)
105 PCIHostState *h = PCI_HOST_BRIDGE(obj);
106 Range w64;
108 pci_bus_get_w64_range(h->bus, &w64);
110 visit_type_uint64(v, &w64.end, name, errp);
113 static void q35_host_get_mmcfg_size(Object *obj, Visitor *v,
114 void *opaque, const char *name,
115 Error **errp)
117 PCIExpressHost *e = PCIE_HOST_BRIDGE(obj);
118 uint32_t value = e->size;
120 visit_type_uint32(v, &value, name, errp);
123 static Property mch_props[] = {
124 DEFINE_PROP_UINT64(PCIE_HOST_MCFG_BASE, Q35PCIHost, parent_obj.base_addr,
125 MCH_HOST_BRIDGE_PCIEXBAR_DEFAULT),
126 DEFINE_PROP_SIZE(PCI_HOST_PROP_PCI_HOLE64_SIZE, Q35PCIHost,
127 mch.pci_hole64_size, DEFAULT_PCI_HOLE64_SIZE),
128 DEFINE_PROP_UINT32("short_root_bus", Q35PCIHost, mch.short_root_bus, 0),
129 DEFINE_PROP_END_OF_LIST(),
132 static void q35_host_class_init(ObjectClass *klass, void *data)
134 DeviceClass *dc = DEVICE_CLASS(klass);
135 PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(klass);
137 hc->root_bus_path = q35_host_root_bus_path;
138 dc->realize = q35_host_realize;
139 dc->props = mch_props;
140 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
141 dc->fw_name = "pci";
144 static void q35_host_initfn(Object *obj)
146 Q35PCIHost *s = Q35_HOST_DEVICE(obj);
147 PCIHostState *phb = PCI_HOST_BRIDGE(obj);
149 memory_region_init_io(&phb->conf_mem, obj, &pci_host_conf_le_ops, phb,
150 "pci-conf-idx", 4);
151 memory_region_init_io(&phb->data_mem, obj, &pci_host_data_le_ops, phb,
152 "pci-conf-data", 4);
154 object_initialize(&s->mch, sizeof(s->mch), TYPE_MCH_PCI_DEVICE);
155 object_property_add_child(OBJECT(s), "mch", OBJECT(&s->mch), NULL);
156 qdev_prop_set_uint32(DEVICE(&s->mch), "addr", PCI_DEVFN(0, 0));
157 qdev_prop_set_bit(DEVICE(&s->mch), "multifunction", false);
159 object_property_add(obj, PCI_HOST_PROP_PCI_HOLE_START, "int",
160 q35_host_get_pci_hole_start,
161 NULL, NULL, NULL, NULL);
163 object_property_add(obj, PCI_HOST_PROP_PCI_HOLE_END, "int",
164 q35_host_get_pci_hole_end,
165 NULL, NULL, NULL, NULL);
167 object_property_add(obj, PCI_HOST_PROP_PCI_HOLE64_START, "int",
168 q35_host_get_pci_hole64_start,
169 NULL, NULL, NULL, NULL);
171 object_property_add(obj, PCI_HOST_PROP_PCI_HOLE64_END, "int",
172 q35_host_get_pci_hole64_end,
173 NULL, NULL, NULL, NULL);
175 object_property_add(obj, PCIE_HOST_MCFG_SIZE, "int",
176 q35_host_get_mmcfg_size,
177 NULL, NULL, NULL, NULL);
179 /* Leave enough space for the biggest MCFG BAR */
180 /* TODO: this matches current bios behaviour, but
181 * it's not a power of two, which means an MTRR
182 * can't cover it exactly.
184 s->mch.pci_info.w32.begin = MCH_HOST_BRIDGE_PCIEXBAR_DEFAULT +
185 MCH_HOST_BRIDGE_PCIEXBAR_MAX;
186 s->mch.pci_info.w32.end = IO_APIC_DEFAULT_ADDRESS;
189 static const TypeInfo q35_host_info = {
190 .name = TYPE_Q35_HOST_DEVICE,
191 .parent = TYPE_PCIE_HOST_BRIDGE,
192 .instance_size = sizeof(Q35PCIHost),
193 .instance_init = q35_host_initfn,
194 .class_init = q35_host_class_init,
197 /****************************************************************************
198 * MCH D0:F0
201 /* PCIe MMCFG */
202 static void mch_update_pciexbar(MCHPCIState *mch)
204 PCIDevice *pci_dev = PCI_DEVICE(mch);
205 BusState *bus = qdev_get_parent_bus(DEVICE(mch));
206 PCIExpressHost *pehb = PCIE_HOST_BRIDGE(bus->parent);
208 uint64_t pciexbar;
209 int enable;
210 uint64_t addr;
211 uint64_t addr_mask;
212 uint32_t length;
214 pciexbar = pci_get_quad(pci_dev->config + MCH_HOST_BRIDGE_PCIEXBAR);
215 enable = pciexbar & MCH_HOST_BRIDGE_PCIEXBAREN;
216 addr_mask = MCH_HOST_BRIDGE_PCIEXBAR_ADMSK;
217 switch (pciexbar & MCH_HOST_BRIDGE_PCIEXBAR_LENGTH_MASK) {
218 case MCH_HOST_BRIDGE_PCIEXBAR_LENGTH_256M:
219 length = 256 * 1024 * 1024;
220 break;
221 case MCH_HOST_BRIDGE_PCIEXBAR_LENGTH_128M:
222 length = 128 * 1024 * 1024;
223 addr_mask |= MCH_HOST_BRIDGE_PCIEXBAR_128ADMSK |
224 MCH_HOST_BRIDGE_PCIEXBAR_64ADMSK;
225 break;
226 case MCH_HOST_BRIDGE_PCIEXBAR_LENGTH_64M:
227 length = 64 * 1024 * 1024;
228 addr_mask |= MCH_HOST_BRIDGE_PCIEXBAR_64ADMSK;
229 break;
230 case MCH_HOST_BRIDGE_PCIEXBAR_LENGTH_RVD:
231 default:
232 enable = 0;
233 length = 0;
234 abort();
235 break;
237 addr = pciexbar & addr_mask;
238 pcie_host_mmcfg_update(pehb, enable, addr, length);
239 /* Leave enough space for the MCFG BAR */
241 * TODO: this matches current bios behaviour, but it's not a power of two,
242 * which means an MTRR can't cover it exactly.
244 if (enable) {
245 mch->pci_info.w32.begin = addr + length;
246 } else {
247 mch->pci_info.w32.begin = MCH_HOST_BRIDGE_PCIEXBAR_DEFAULT;
251 /* PAM */
252 static void mch_update_pam(MCHPCIState *mch)
254 PCIDevice *pd = PCI_DEVICE(mch);
255 int i;
257 memory_region_transaction_begin();
258 for (i = 0; i < 13; i++) {
259 pam_update(&mch->pam_regions[i], i,
260 pd->config[MCH_HOST_BRIDGE_PAM0 + ((i + 1) / 2)]);
262 memory_region_transaction_commit();
265 /* SMRAM */
266 static void mch_update_smram(MCHPCIState *mch)
268 PCIDevice *pd = PCI_DEVICE(mch);
270 memory_region_transaction_begin();
271 memory_region_set_enabled(&mch->smram_region,
272 !(pd->config[MCH_HOST_BRIDGE_SMRAM] & SMRAM_D_OPEN));
273 memory_region_set_enabled(&mch->smram,
274 pd->config[MCH_HOST_BRIDGE_SMRAM] & SMRAM_G_SMRAME);
275 memory_region_transaction_commit();
278 static void mch_write_config(PCIDevice *d,
279 uint32_t address, uint32_t val, int len)
281 MCHPCIState *mch = MCH_PCI_DEVICE(d);
283 /* XXX: implement SMRAM.D_LOCK */
284 pci_default_write_config(d, address, val, len);
286 if (ranges_overlap(address, len, MCH_HOST_BRIDGE_PAM0,
287 MCH_HOST_BRIDGE_PAM_SIZE)) {
288 mch_update_pam(mch);
291 if (ranges_overlap(address, len, MCH_HOST_BRIDGE_PCIEXBAR,
292 MCH_HOST_BRIDGE_PCIEXBAR_SIZE)) {
293 mch_update_pciexbar(mch);
296 if (ranges_overlap(address, len, MCH_HOST_BRIDGE_SMRAM,
297 MCH_HOST_BRIDGE_SMRAM_SIZE)) {
298 mch_update_smram(mch);
302 static void mch_update(MCHPCIState *mch)
304 mch_update_pciexbar(mch);
305 mch_update_pam(mch);
306 mch_update_smram(mch);
309 static int mch_post_load(void *opaque, int version_id)
311 MCHPCIState *mch = opaque;
312 mch_update(mch);
313 return 0;
316 static const VMStateDescription vmstate_mch = {
317 .name = "mch",
318 .version_id = 1,
319 .minimum_version_id = 1,
320 .post_load = mch_post_load,
321 .fields = (VMStateField[]) {
322 VMSTATE_PCI_DEVICE(parent_obj, MCHPCIState),
323 /* Used to be smm_enabled, which was basically always zero because
324 * SeaBIOS hardly uses SMM. SMRAM is now handled by CPU code.
326 VMSTATE_UNUSED(1),
327 VMSTATE_END_OF_LIST()
331 static void mch_reset(DeviceState *qdev)
333 PCIDevice *d = PCI_DEVICE(qdev);
334 MCHPCIState *mch = MCH_PCI_DEVICE(d);
336 pci_set_quad(d->config + MCH_HOST_BRIDGE_PCIEXBAR,
337 MCH_HOST_BRIDGE_PCIEXBAR_DEFAULT);
339 d->config[MCH_HOST_BRIDGE_SMRAM] = MCH_HOST_BRIDGE_SMRAM_DEFAULT;
341 mch_update(mch);
344 static AddressSpace *q35_host_dma_iommu(PCIBus *bus, void *opaque, int devfn)
346 IntelIOMMUState *s = opaque;
347 VTDAddressSpace **pvtd_as;
348 int bus_num = pci_bus_num(bus);
350 assert(0 <= bus_num && bus_num <= VTD_PCI_BUS_MAX);
351 assert(0 <= devfn && devfn <= VTD_PCI_DEVFN_MAX);
353 pvtd_as = s->address_spaces[bus_num];
354 if (!pvtd_as) {
355 /* No corresponding free() */
356 pvtd_as = g_malloc0(sizeof(VTDAddressSpace *) * VTD_PCI_DEVFN_MAX);
357 s->address_spaces[bus_num] = pvtd_as;
359 if (!pvtd_as[devfn]) {
360 pvtd_as[devfn] = g_malloc0(sizeof(VTDAddressSpace));
362 pvtd_as[devfn]->bus_num = (uint8_t)bus_num;
363 pvtd_as[devfn]->devfn = (uint8_t)devfn;
364 pvtd_as[devfn]->iommu_state = s;
365 pvtd_as[devfn]->context_cache_entry.context_cache_gen = 0;
366 memory_region_init_iommu(&pvtd_as[devfn]->iommu, OBJECT(s),
367 &s->iommu_ops, "intel_iommu", UINT64_MAX);
368 address_space_init(&pvtd_as[devfn]->as,
369 &pvtd_as[devfn]->iommu, "intel_iommu");
371 return &pvtd_as[devfn]->as;
374 static void mch_init_dmar(MCHPCIState *mch)
376 PCIBus *pci_bus = PCI_BUS(qdev_get_parent_bus(DEVICE(mch)));
378 mch->iommu = INTEL_IOMMU_DEVICE(qdev_create(NULL, TYPE_INTEL_IOMMU_DEVICE));
379 object_property_add_child(OBJECT(mch), "intel-iommu",
380 OBJECT(mch->iommu), NULL);
381 qdev_init_nofail(DEVICE(mch->iommu));
382 sysbus_mmio_map(SYS_BUS_DEVICE(mch->iommu), 0, Q35_HOST_BRIDGE_IOMMU_ADDR);
384 pci_setup_iommu(pci_bus, q35_host_dma_iommu, mch->iommu);
387 static void mch_realize(PCIDevice *d, Error **errp)
389 int i;
390 MCHPCIState *mch = MCH_PCI_DEVICE(d);
392 /* setup pci memory mapping */
393 pc_pci_as_mapping_init(OBJECT(mch), mch->system_memory,
394 mch->pci_address_space);
396 /* if *disabled* show SMRAM to all CPUs */
397 memory_region_init_alias(&mch->smram_region, OBJECT(mch), "smram-region",
398 mch->pci_address_space, 0xa0000, 0x20000);
399 memory_region_add_subregion_overlap(mch->system_memory, 0xa0000,
400 &mch->smram_region, 1);
401 memory_region_set_enabled(&mch->smram_region, true);
403 /* smram, as seen by SMM CPUs */
404 memory_region_init(&mch->smram, OBJECT(mch), "smram", 1ull << 32);
405 memory_region_set_enabled(&mch->smram, true);
406 memory_region_init_alias(&mch->low_smram, OBJECT(mch), "smram-low",
407 mch->ram_memory, 0xa0000, 0x20000);
408 memory_region_set_enabled(&mch->low_smram, true);
409 memory_region_add_subregion(&mch->smram, 0xa0000, &mch->low_smram);
410 object_property_add_const_link(qdev_get_machine(), "smram",
411 OBJECT(&mch->smram), &error_abort);
413 init_pam(DEVICE(mch), mch->ram_memory, mch->system_memory,
414 mch->pci_address_space, &mch->pam_regions[0],
415 PAM_BIOS_BASE, PAM_BIOS_SIZE);
416 for (i = 0; i < 12; ++i) {
417 init_pam(DEVICE(mch), mch->ram_memory, mch->system_memory,
418 mch->pci_address_space, &mch->pam_regions[i+1],
419 PAM_EXPAN_BASE + i * PAM_EXPAN_SIZE, PAM_EXPAN_SIZE);
421 /* Intel IOMMU (VT-d) */
422 if (machine_iommu(current_machine)) {
423 mch_init_dmar(mch);
427 uint64_t mch_mcfg_base(void)
429 bool ambiguous;
430 Object *o = object_resolve_path_type("", TYPE_MCH_PCI_DEVICE, &ambiguous);
431 if (!o) {
432 return 0;
434 return MCH_HOST_BRIDGE_PCIEXBAR_DEFAULT;
437 static void mch_class_init(ObjectClass *klass, void *data)
439 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
440 DeviceClass *dc = DEVICE_CLASS(klass);
442 k->realize = mch_realize;
443 k->config_write = mch_write_config;
444 dc->reset = mch_reset;
445 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
446 dc->desc = "Host bridge";
447 dc->vmsd = &vmstate_mch;
448 k->vendor_id = PCI_VENDOR_ID_INTEL;
449 k->device_id = PCI_DEVICE_ID_INTEL_Q35_MCH;
450 k->revision = MCH_HOST_BRIDGE_REVISION_DEFAULT;
451 k->class_id = PCI_CLASS_BRIDGE_HOST;
453 * PCI-facing part of the host bridge, not usable without the
454 * host-facing part, which can't be device_add'ed, yet.
456 dc->cannot_instantiate_with_device_add_yet = true;
459 static const TypeInfo mch_info = {
460 .name = TYPE_MCH_PCI_DEVICE,
461 .parent = TYPE_PCI_DEVICE,
462 .instance_size = sizeof(MCHPCIState),
463 .class_init = mch_class_init,
466 static void q35_register(void)
468 type_register_static(&mch_info);
469 type_register_static(&q35_host_info);
472 type_init(q35_register);