blob: 02f95765880a8e0be1e3cdfcfc7994c76b8f6a1d [file] [log] [blame]
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -05001/*
2 * QEMU MCH/ICH9 PCI Bridge Emulation
3 *
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>
9 *
Gongleief9f7b52014-08-11 16:10:25 +080010 * This is based on piix.c, but heavily modified.
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -050011 *
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:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 *
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.
29 */
Peter Maydellb6a0aa02016-01-26 18:17:03 +000030#include "qemu/osdep.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010031#include "hw/hw.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010032#include "hw/pci-host/q35.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010033#include "qapi/error.h"
Igor Mammedov39848902013-07-29 16:47:57 +020034#include "qapi/visitor.h"
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -050035
36/****************************************************************************
37 * Q35 host
38 */
39
Marcel Apfelbaum9fa99d22017-11-11 17:25:00 +020040#define Q35_PCI_HOST_HOLE64_SIZE_DEFAULT (1ULL << 35)
41
Hu Tao62d92e42013-07-01 18:18:23 +080042static void q35_host_realize(DeviceState *dev, Error **errp)
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -050043{
Hu Taoce888122013-07-01 18:18:22 +080044 PCIHostState *pci = PCI_HOST_BRIDGE(dev);
45 Q35PCIHost *s = Q35_HOST_DEVICE(dev);
Hu Tao62d92e42013-07-01 18:18:23 +080046 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -050047
Hu Tao62d92e42013-07-01 18:18:23 +080048 sysbus_add_io(sbd, MCH_HOST_BRIDGE_CONFIG_ADDR, &pci->conf_mem);
49 sysbus_init_ioports(sbd, MCH_HOST_BRIDGE_CONFIG_ADDR, 4);
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -050050
Hu Tao62d92e42013-07-01 18:18:23 +080051 sysbus_add_io(sbd, MCH_HOST_BRIDGE_CONFIG_DATA, &pci->data_mem);
52 sysbus_init_ioports(sbd, MCH_HOST_BRIDGE_CONFIG_DATA, 4);
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -050053
David Gibson1115ff62017-11-29 19:46:22 +110054 pci->bus = pci_root_bus_new(DEVICE(s), "pcie.0",
55 s->mch.pci_address_space,
56 s->mch.address_space_io,
57 0, TYPE_PCIE_BUS);
Marcel Apfelbaum621d9832016-06-27 18:38:34 +030058 PC_MACHINE(qdev_get_machine())->bus = pci->bus;
Hu Taoce888122013-07-01 18:18:22 +080059 qdev_set_parent_bus(DEVICE(&s->mch), BUS(pci->bus));
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -050060 qdev_init_nofail(DEVICE(&s->mch));
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -050061}
62
David Gibson568f0692013-06-06 18:48:49 +100063static const char *q35_host_root_bus_path(PCIHostState *host_bridge,
64 PCIBus *rootbus)
65{
Cole Robinson04c7d8b2013-11-05 18:46:27 -050066 Q35PCIHost *s = Q35_HOST_DEVICE(host_bridge);
67
68 /* For backwards compat with old device paths */
69 if (s->mch.short_root_bus) {
70 return "0000";
71 }
72 return "0000:00";
David Gibson568f0692013-06-06 18:48:49 +100073}
74
Igor Mammedov39848902013-07-29 16:47:57 +020075static void q35_host_get_pci_hole_start(Object *obj, Visitor *v,
Eric Blaked7bce992016-01-29 06:48:55 -070076 const char *name, void *opaque,
Igor Mammedov39848902013-07-29 16:47:57 +020077 Error **errp)
78{
79 Q35PCIHost *s = Q35_HOST_DEVICE(obj);
Markus Armbrustera0efbf12016-07-01 13:47:47 +020080 uint64_t val64;
81 uint32_t value;
Igor Mammedov39848902013-07-29 16:47:57 +020082
Markus Armbrustera0efbf12016-07-01 13:47:47 +020083 val64 = range_is_empty(&s->mch.pci_hole)
84 ? 0 : range_lob(&s->mch.pci_hole);
85 value = val64;
86 assert(value == val64);
Eric Blake51e72bc2016-01-29 06:48:54 -070087 visit_type_uint32(v, name, &value, errp);
Igor Mammedov39848902013-07-29 16:47:57 +020088}
89
90static void q35_host_get_pci_hole_end(Object *obj, Visitor *v,
Eric Blaked7bce992016-01-29 06:48:55 -070091 const char *name, void *opaque,
Igor Mammedov39848902013-07-29 16:47:57 +020092 Error **errp)
93{
94 Q35PCIHost *s = Q35_HOST_DEVICE(obj);
Markus Armbrustera0efbf12016-07-01 13:47:47 +020095 uint64_t val64;
96 uint32_t value;
Igor Mammedov39848902013-07-29 16:47:57 +020097
Markus Armbrustera0efbf12016-07-01 13:47:47 +020098 val64 = range_is_empty(&s->mch.pci_hole)
99 ? 0 : range_upb(&s->mch.pci_hole) + 1;
100 value = val64;
101 assert(value == val64);
Eric Blake51e72bc2016-01-29 06:48:54 -0700102 visit_type_uint32(v, name, &value, errp);
Igor Mammedov39848902013-07-29 16:47:57 +0200103}
104
Marcel Apfelbaum9fa99d22017-11-11 17:25:00 +0200105/*
106 * The 64bit PCI hole start is set by the Guest firmware
107 * as the address of the first 64bit PCI MEM resource.
108 * If no PCI device has resources on the 64bit area,
109 * the 64bit PCI hole will start after "over 4G RAM" and the
110 * reserved space for memory hotplug if any.
111 */
Igor Mammedov39848902013-07-29 16:47:57 +0200112static void q35_host_get_pci_hole64_start(Object *obj, Visitor *v,
Eric Blaked7bce992016-01-29 06:48:55 -0700113 const char *name, void *opaque,
Igor Mammedov39848902013-07-29 16:47:57 +0200114 Error **errp)
115{
Michael S. Tsirkin8b42d732013-09-02 12:57:36 +0300116 PCIHostState *h = PCI_HOST_BRIDGE(obj);
Marcel Apfelbaum9fa99d22017-11-11 17:25:00 +0200117 Q35PCIHost *s = Q35_HOST_DEVICE(obj);
Michael S. Tsirkin8b42d732013-09-02 12:57:36 +0300118 Range w64;
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200119 uint64_t value;
Igor Mammedov39848902013-07-29 16:47:57 +0200120
Michael S. Tsirkin8b42d732013-09-02 12:57:36 +0300121 pci_bus_get_w64_range(h->bus, &w64);
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200122 value = range_is_empty(&w64) ? 0 : range_lob(&w64);
Marcel Apfelbaum9fa99d22017-11-11 17:25:00 +0200123 if (!value && s->pci_hole64_fix) {
124 value = pc_pci_hole64_start();
125 }
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200126 visit_type_uint64(v, name, &value, errp);
Igor Mammedov39848902013-07-29 16:47:57 +0200127}
128
Marcel Apfelbaum9fa99d22017-11-11 17:25:00 +0200129/*
130 * The 64bit PCI hole end is set by the Guest firmware
131 * as the address of the last 64bit PCI MEM resource.
132 * Then it is expanded to the PCI_HOST_PROP_PCI_HOLE64_SIZE
133 * that can be configured by the user.
134 */
Igor Mammedov39848902013-07-29 16:47:57 +0200135static void q35_host_get_pci_hole64_end(Object *obj, Visitor *v,
Eric Blaked7bce992016-01-29 06:48:55 -0700136 const char *name, void *opaque,
Igor Mammedov39848902013-07-29 16:47:57 +0200137 Error **errp)
138{
Michael S. Tsirkin8b42d732013-09-02 12:57:36 +0300139 PCIHostState *h = PCI_HOST_BRIDGE(obj);
Marcel Apfelbaum9fa99d22017-11-11 17:25:00 +0200140 Q35PCIHost *s = Q35_HOST_DEVICE(obj);
141 uint64_t hole64_start = pc_pci_hole64_start();
Michael S. Tsirkin8b42d732013-09-02 12:57:36 +0300142 Range w64;
Marcel Apfelbaum9fa99d22017-11-11 17:25:00 +0200143 uint64_t value, hole64_end;
Igor Mammedov39848902013-07-29 16:47:57 +0200144
Michael S. Tsirkin8b42d732013-09-02 12:57:36 +0300145 pci_bus_get_w64_range(h->bus, &w64);
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200146 value = range_is_empty(&w64) ? 0 : range_upb(&w64) + 1;
Marcel Apfelbaum9fa99d22017-11-11 17:25:00 +0200147 hole64_end = ROUND_UP(hole64_start + s->mch.pci_hole64_size, 1ULL << 30);
148 if (s->pci_hole64_fix && value < hole64_end) {
149 value = hole64_end;
150 }
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200151 visit_type_uint64(v, name, &value, errp);
Igor Mammedov39848902013-07-29 16:47:57 +0200152}
153
Eric Blaked7bce992016-01-29 06:48:55 -0700154static void q35_host_get_mmcfg_size(Object *obj, Visitor *v, const char *name,
155 void *opaque, Error **errp)
Michael S. Tsirkincbcaf792013-09-10 10:16:02 +0300156{
157 PCIExpressHost *e = PCIE_HOST_BRIDGE(obj);
Michael S. Tsirkincbcaf792013-09-10 10:16:02 +0300158
Marc-André Lureaud015c4e2017-06-07 20:36:05 +0400159 visit_type_uint64(v, name, &e->size, errp);
Michael S. Tsirkincbcaf792013-09-10 10:16:02 +0300160}
161
Marcel Apfelbaum9fa99d22017-11-11 17:25:00 +0200162/*
163 * NOTE: setting defaults for the mch.* fields in this table
164 * doesn't work, because mch is a separate QOM object that is
165 * zeroed by the object_initialize(&s->mch, ...) call inside
166 * q35_host_initfn(). The default values for those
167 * properties need to be initialized manually by
168 * q35_host_initfn() after the object_initialize() call.
169 */
Laszlo Ersek2f295162017-06-08 18:10:13 +0200170static Property q35_host_props[] = {
Michael S. Tsirkin87f65242013-09-02 17:59:38 +0300171 DEFINE_PROP_UINT64(PCIE_HOST_MCFG_BASE, Q35PCIHost, parent_obj.base_addr,
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500172 MCH_HOST_BRIDGE_PCIEXBAR_DEFAULT),
Igor Mammedov39848902013-07-29 16:47:57 +0200173 DEFINE_PROP_SIZE(PCI_HOST_PROP_PCI_HOLE64_SIZE, Q35PCIHost,
Marcel Apfelbaum9fa99d22017-11-11 17:25:00 +0200174 mch.pci_hole64_size, Q35_PCI_HOST_HOLE64_SIZE_DEFAULT),
Cole Robinson04c7d8b2013-11-05 18:46:27 -0500175 DEFINE_PROP_UINT32("short_root_bus", Q35PCIHost, mch.short_root_bus, 0),
Efimov Vasily401f2f32016-06-22 15:24:49 +0300176 DEFINE_PROP_SIZE(PCI_HOST_BELOW_4G_MEM_SIZE, Q35PCIHost,
177 mch.below_4g_mem_size, 0),
178 DEFINE_PROP_SIZE(PCI_HOST_ABOVE_4G_MEM_SIZE, Q35PCIHost,
179 mch.above_4g_mem_size, 0),
Marcel Apfelbaum9fa99d22017-11-11 17:25:00 +0200180 DEFINE_PROP_BOOL("x-pci-hole64-fix", Q35PCIHost, pci_hole64_fix, true),
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500181 DEFINE_PROP_END_OF_LIST(),
182};
183
184static void q35_host_class_init(ObjectClass *klass, void *data)
185{
186 DeviceClass *dc = DEVICE_CLASS(klass);
David Gibson568f0692013-06-06 18:48:49 +1000187 PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(klass);
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500188
David Gibson568f0692013-06-06 18:48:49 +1000189 hc->root_bus_path = q35_host_root_bus_path;
Hu Tao62d92e42013-07-01 18:18:23 +0800190 dc->realize = q35_host_realize;
Laszlo Ersek2f295162017-06-08 18:10:13 +0200191 dc->props = q35_host_props;
Marcel Apfelbaumbf8d4922016-06-27 18:38:33 +0300192 /* Reason: needs to be wired up by pc_q35_init */
Eduardo Habkoste90f2a82017-05-03 17:35:44 -0300193 dc->user_creatable = false;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300194 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
Michael S. Tsirkin68c0e132013-05-30 11:35:23 +0300195 dc->fw_name = "pci";
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500196}
197
198static void q35_host_initfn(Object *obj)
199{
200 Q35PCIHost *s = Q35_HOST_DEVICE(obj);
Hu Tao62d92e42013-07-01 18:18:23 +0800201 PCIHostState *phb = PCI_HOST_BRIDGE(obj);
202
203 memory_region_init_io(&phb->conf_mem, obj, &pci_host_conf_le_ops, phb,
204 "pci-conf-idx", 4);
205 memory_region_init_io(&phb->data_mem, obj, &pci_host_data_le_ops, phb,
206 "pci-conf-data", 4);
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500207
Andreas Färber213f0c42013-08-23 19:37:12 +0200208 object_initialize(&s->mch, sizeof(s->mch), TYPE_MCH_PCI_DEVICE);
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500209 object_property_add_child(OBJECT(s), "mch", OBJECT(&s->mch), NULL);
Marc-André Lureau446de8b2017-06-07 20:36:11 +0400210 qdev_prop_set_int32(DEVICE(&s->mch), "addr", PCI_DEVFN(0, 0));
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500211 qdev_prop_set_bit(DEVICE(&s->mch), "multifunction", false);
Marcel Apfelbaum9fa99d22017-11-11 17:25:00 +0200212 /* mch's object_initialize resets the default value, set it again */
213 qdev_prop_set_uint64(DEVICE(s), PCI_HOST_PROP_PCI_HOLE64_SIZE,
214 Q35_PCI_HOST_HOLE64_SIZE_DEFAULT);
Marc-André Lureau1e507bb2017-06-07 20:36:06 +0400215 object_property_add(obj, PCI_HOST_PROP_PCI_HOLE_START, "uint32",
Igor Mammedov39848902013-07-29 16:47:57 +0200216 q35_host_get_pci_hole_start,
217 NULL, NULL, NULL, NULL);
218
Marc-André Lureau1e507bb2017-06-07 20:36:06 +0400219 object_property_add(obj, PCI_HOST_PROP_PCI_HOLE_END, "uint32",
Igor Mammedov39848902013-07-29 16:47:57 +0200220 q35_host_get_pci_hole_end,
221 NULL, NULL, NULL, NULL);
222
Marc-André Lureau1e507bb2017-06-07 20:36:06 +0400223 object_property_add(obj, PCI_HOST_PROP_PCI_HOLE64_START, "uint64",
Igor Mammedov39848902013-07-29 16:47:57 +0200224 q35_host_get_pci_hole64_start,
225 NULL, NULL, NULL, NULL);
226
Marc-André Lureau1e507bb2017-06-07 20:36:06 +0400227 object_property_add(obj, PCI_HOST_PROP_PCI_HOLE64_END, "uint64",
Igor Mammedov39848902013-07-29 16:47:57 +0200228 q35_host_get_pci_hole64_end,
229 NULL, NULL, NULL, NULL);
230
Marc-André Lureau1e507bb2017-06-07 20:36:06 +0400231 object_property_add(obj, PCIE_HOST_MCFG_SIZE, "uint64",
Michael S. Tsirkincbcaf792013-09-10 10:16:02 +0300232 q35_host_get_mmcfg_size,
233 NULL, NULL, NULL, NULL);
234
Efimov Vasily401f2f32016-06-22 15:24:49 +0300235 object_property_add_link(obj, MCH_HOST_PROP_RAM_MEM, TYPE_MEMORY_REGION,
236 (Object **) &s->mch.ram_memory,
237 qdev_prop_allow_set_link_before_realize, 0, NULL);
238
239 object_property_add_link(obj, MCH_HOST_PROP_PCI_MEM, TYPE_MEMORY_REGION,
240 (Object **) &s->mch.pci_address_space,
241 qdev_prop_allow_set_link_before_realize, 0, NULL);
242
243 object_property_add_link(obj, MCH_HOST_PROP_SYSTEM_MEM, TYPE_MEMORY_REGION,
244 (Object **) &s->mch.system_memory,
245 qdev_prop_allow_set_link_before_realize, 0, NULL);
246
247 object_property_add_link(obj, MCH_HOST_PROP_IO_MEM, TYPE_MEMORY_REGION,
248 (Object **) &s->mch.address_space_io,
249 qdev_prop_allow_set_link_before_realize, 0, NULL);
250
Igor Mammedov39848902013-07-29 16:47:57 +0200251 /* Leave enough space for the biggest MCFG BAR */
252 /* TODO: this matches current bios behaviour, but
253 * it's not a power of two, which means an MTRR
254 * can't cover it exactly.
255 */
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200256 range_set_bounds(&s->mch.pci_hole,
257 MCH_HOST_BRIDGE_PCIEXBAR_DEFAULT + MCH_HOST_BRIDGE_PCIEXBAR_MAX,
258 IO_APIC_DEFAULT_ADDRESS - 1);
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500259}
260
261static const TypeInfo q35_host_info = {
262 .name = TYPE_Q35_HOST_DEVICE,
263 .parent = TYPE_PCIE_HOST_BRIDGE,
264 .instance_size = sizeof(Q35PCIHost),
265 .instance_init = q35_host_initfn,
266 .class_init = q35_host_class_init,
267};
268
269/****************************************************************************
270 * MCH D0:F0
271 */
272
Gerd Hoffmannbafc90b2015-04-20 10:55:09 +0200273static uint64_t tseg_blackhole_read(void *ptr, hwaddr reg, unsigned size)
274{
275 return 0xffffffff;
276}
277
278static void tseg_blackhole_write(void *opaque, hwaddr addr, uint64_t val,
279 unsigned width)
280{
281 /* nothing */
282}
283
284static const MemoryRegionOps tseg_blackhole_ops = {
285 .read = tseg_blackhole_read,
286 .write = tseg_blackhole_write,
287 .endianness = DEVICE_NATIVE_ENDIAN,
288 .valid.min_access_size = 1,
289 .valid.max_access_size = 4,
290 .impl.min_access_size = 4,
291 .impl.max_access_size = 4,
292 .endianness = DEVICE_LITTLE_ENDIAN,
293};
294
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500295/* PCIe MMCFG */
296static void mch_update_pciexbar(MCHPCIState *mch)
297{
Hu Taoce888122013-07-01 18:18:22 +0800298 PCIDevice *pci_dev = PCI_DEVICE(mch);
299 BusState *bus = qdev_get_parent_bus(DEVICE(mch));
300 PCIExpressHost *pehb = PCIE_HOST_BRIDGE(bus->parent);
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500301
302 uint64_t pciexbar;
303 int enable;
304 uint64_t addr;
305 uint64_t addr_mask;
306 uint32_t length;
307
308 pciexbar = pci_get_quad(pci_dev->config + MCH_HOST_BRIDGE_PCIEXBAR);
309 enable = pciexbar & MCH_HOST_BRIDGE_PCIEXBAREN;
310 addr_mask = MCH_HOST_BRIDGE_PCIEXBAR_ADMSK;
311 switch (pciexbar & MCH_HOST_BRIDGE_PCIEXBAR_LENGTH_MASK) {
312 case MCH_HOST_BRIDGE_PCIEXBAR_LENGTH_256M:
313 length = 256 * 1024 * 1024;
314 break;
315 case MCH_HOST_BRIDGE_PCIEXBAR_LENGTH_128M:
316 length = 128 * 1024 * 1024;
317 addr_mask |= MCH_HOST_BRIDGE_PCIEXBAR_128ADMSK |
318 MCH_HOST_BRIDGE_PCIEXBAR_64ADMSK;
319 break;
320 case MCH_HOST_BRIDGE_PCIEXBAR_LENGTH_64M:
321 length = 64 * 1024 * 1024;
322 addr_mask |= MCH_HOST_BRIDGE_PCIEXBAR_64ADMSK;
323 break;
324 case MCH_HOST_BRIDGE_PCIEXBAR_LENGTH_RVD:
325 default:
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500326 abort();
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500327 }
328 addr = pciexbar & addr_mask;
Hu Taoce888122013-07-01 18:18:22 +0800329 pcie_host_mmcfg_update(pehb, enable, addr, length);
Michael S. Tsirkin636228a2013-09-01 13:26:03 +0300330 /* Leave enough space for the MCFG BAR */
331 /*
332 * TODO: this matches current bios behaviour, but it's not a power of two,
333 * which means an MTRR can't cover it exactly.
334 */
335 if (enable) {
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200336 range_set_bounds(&mch->pci_hole,
337 addr + length,
338 IO_APIC_DEFAULT_ADDRESS - 1);
Michael S. Tsirkin636228a2013-09-01 13:26:03 +0300339 } else {
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200340 range_set_bounds(&mch->pci_hole,
341 MCH_HOST_BRIDGE_PCIEXBAR_DEFAULT,
342 IO_APIC_DEFAULT_ADDRESS - 1);
Michael S. Tsirkin636228a2013-09-01 13:26:03 +0300343 }
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500344}
345
346/* PAM */
347static void mch_update_pam(MCHPCIState *mch)
348{
Hu Taoce888122013-07-01 18:18:22 +0800349 PCIDevice *pd = PCI_DEVICE(mch);
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500350 int i;
351
352 memory_region_transaction_begin();
353 for (i = 0; i < 13; i++) {
354 pam_update(&mch->pam_regions[i], i,
Marc-André Lureaufa141082017-06-22 13:04:16 +0200355 pd->config[MCH_HOST_BRIDGE_PAM0 + (DIV_ROUND_UP(i, 2))]);
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500356 }
357 memory_region_transaction_commit();
358}
359
360/* SMRAM */
361static void mch_update_smram(MCHPCIState *mch)
362{
Hu Taoce888122013-07-01 18:18:22 +0800363 PCIDevice *pd = PCI_DEVICE(mch);
Paolo Bonzini64130fa2015-03-31 17:13:01 +0200364 bool h_smrame = (pd->config[MCH_HOST_BRIDGE_ESMRAMC] & MCH_HOST_BRIDGE_ESMRAMC_H_SMRAME);
Gerd Hoffmannbafc90b2015-04-20 10:55:09 +0200365 uint32_t tseg_size;
Hu Taoce888122013-07-01 18:18:22 +0800366
Gerd Hoffmann68c77ac2015-04-14 14:03:22 +0200367 /* implement SMRAM.D_LCK */
368 if (pd->config[MCH_HOST_BRIDGE_SMRAM] & MCH_HOST_BRIDGE_SMRAM_D_LCK) {
369 pd->config[MCH_HOST_BRIDGE_SMRAM] &= ~MCH_HOST_BRIDGE_SMRAM_D_OPEN;
370 pd->wmask[MCH_HOST_BRIDGE_SMRAM] = MCH_HOST_BRIDGE_SMRAM_WMASK_LCK;
371 pd->wmask[MCH_HOST_BRIDGE_ESMRAMC] = MCH_HOST_BRIDGE_ESMRAMC_WMASK_LCK;
372 }
373
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500374 memory_region_transaction_begin();
Paolo Bonzini64130fa2015-03-31 17:13:01 +0200375
376 if (pd->config[MCH_HOST_BRIDGE_SMRAM] & SMRAM_D_OPEN) {
377 /* Hide (!) low SMRAM if H_SMRAME = 1 */
378 memory_region_set_enabled(&mch->smram_region, h_smrame);
379 /* Show high SMRAM if H_SMRAME = 1 */
380 memory_region_set_enabled(&mch->open_high_smram, h_smrame);
381 } else {
382 /* Hide high SMRAM and low SMRAM */
383 memory_region_set_enabled(&mch->smram_region, true);
384 memory_region_set_enabled(&mch->open_high_smram, false);
385 }
386
387 if (pd->config[MCH_HOST_BRIDGE_SMRAM] & SMRAM_G_SMRAME) {
388 memory_region_set_enabled(&mch->low_smram, !h_smrame);
389 memory_region_set_enabled(&mch->high_smram, h_smrame);
390 } else {
391 memory_region_set_enabled(&mch->low_smram, false);
392 memory_region_set_enabled(&mch->high_smram, false);
393 }
394
Gerd Hoffmannbafc90b2015-04-20 10:55:09 +0200395 if (pd->config[MCH_HOST_BRIDGE_ESMRAMC] & MCH_HOST_BRIDGE_ESMRAMC_T_EN) {
396 switch (pd->config[MCH_HOST_BRIDGE_ESMRAMC] &
397 MCH_HOST_BRIDGE_ESMRAMC_TSEG_SZ_MASK) {
398 case MCH_HOST_BRIDGE_ESMRAMC_TSEG_SZ_1MB:
399 tseg_size = 1024 * 1024;
400 break;
401 case MCH_HOST_BRIDGE_ESMRAMC_TSEG_SZ_2MB:
402 tseg_size = 1024 * 1024 * 2;
403 break;
404 case MCH_HOST_BRIDGE_ESMRAMC_TSEG_SZ_8MB:
405 tseg_size = 1024 * 1024 * 8;
406 break;
407 default:
Laszlo Ersek2f295162017-06-08 18:10:13 +0200408 tseg_size = 1024 * 1024 * (uint32_t)mch->ext_tseg_mbytes;
Gerd Hoffmannbafc90b2015-04-20 10:55:09 +0200409 break;
410 }
411 } else {
412 tseg_size = 0;
413 }
414 memory_region_del_subregion(mch->system_memory, &mch->tseg_blackhole);
415 memory_region_set_enabled(&mch->tseg_blackhole, tseg_size);
416 memory_region_set_size(&mch->tseg_blackhole, tseg_size);
417 memory_region_add_subregion_overlap(mch->system_memory,
418 mch->below_4g_mem_size - tseg_size,
419 &mch->tseg_blackhole, 1);
420
421 memory_region_set_enabled(&mch->tseg_window, tseg_size);
422 memory_region_set_size(&mch->tseg_window, tseg_size);
423 memory_region_set_address(&mch->tseg_window,
424 mch->below_4g_mem_size - tseg_size);
425 memory_region_set_alias_offset(&mch->tseg_window,
426 mch->below_4g_mem_size - tseg_size);
427
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500428 memory_region_transaction_commit();
429}
430
Laszlo Ersek2f295162017-06-08 18:10:13 +0200431static void mch_update_ext_tseg_mbytes(MCHPCIState *mch)
432{
433 PCIDevice *pd = PCI_DEVICE(mch);
434 uint8_t *reg = pd->config + MCH_HOST_BRIDGE_EXT_TSEG_MBYTES;
435
436 if (mch->ext_tseg_mbytes > 0 &&
437 pci_get_word(reg) == MCH_HOST_BRIDGE_EXT_TSEG_MBYTES_QUERY) {
438 pci_set_word(reg, mch->ext_tseg_mbytes);
439 }
440}
441
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500442static void mch_write_config(PCIDevice *d,
443 uint32_t address, uint32_t val, int len)
444{
445 MCHPCIState *mch = MCH_PCI_DEVICE(d);
446
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500447 pci_default_write_config(d, address, val, len);
448
449 if (ranges_overlap(address, len, MCH_HOST_BRIDGE_PAM0,
450 MCH_HOST_BRIDGE_PAM_SIZE)) {
451 mch_update_pam(mch);
452 }
453
454 if (ranges_overlap(address, len, MCH_HOST_BRIDGE_PCIEXBAR,
455 MCH_HOST_BRIDGE_PCIEXBAR_SIZE)) {
456 mch_update_pciexbar(mch);
457 }
458
BALATON Zoltan263cf432014-02-28 11:28:03 +0100459 if (ranges_overlap(address, len, MCH_HOST_BRIDGE_SMRAM,
460 MCH_HOST_BRIDGE_SMRAM_SIZE)) {
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500461 mch_update_smram(mch);
462 }
Laszlo Ersek2f295162017-06-08 18:10:13 +0200463
464 if (ranges_overlap(address, len, MCH_HOST_BRIDGE_EXT_TSEG_MBYTES,
465 MCH_HOST_BRIDGE_EXT_TSEG_MBYTES_SIZE)) {
466 mch_update_ext_tseg_mbytes(mch);
467 }
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500468}
469
470static void mch_update(MCHPCIState *mch)
471{
472 mch_update_pciexbar(mch);
473 mch_update_pam(mch);
474 mch_update_smram(mch);
Laszlo Ersek2f295162017-06-08 18:10:13 +0200475 mch_update_ext_tseg_mbytes(mch);
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500476}
477
478static int mch_post_load(void *opaque, int version_id)
479{
480 MCHPCIState *mch = opaque;
481 mch_update(mch);
482 return 0;
483}
484
485static const VMStateDescription vmstate_mch = {
486 .name = "mch",
487 .version_id = 1,
488 .minimum_version_id = 1,
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500489 .post_load = mch_post_load,
Juan Quintelad49805a2014-04-16 15:32:32 +0200490 .fields = (VMStateField[]) {
Hu Taoce888122013-07-01 18:18:22 +0800491 VMSTATE_PCI_DEVICE(parent_obj, MCHPCIState),
Paolo Bonzinif809c602015-03-31 14:12:25 +0200492 /* Used to be smm_enabled, which was basically always zero because
493 * SeaBIOS hardly uses SMM. SMRAM is now handled by CPU code.
494 */
495 VMSTATE_UNUSED(1),
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500496 VMSTATE_END_OF_LIST()
497 }
498};
499
500static void mch_reset(DeviceState *qdev)
501{
502 PCIDevice *d = PCI_DEVICE(qdev);
503 MCHPCIState *mch = MCH_PCI_DEVICE(d);
504
505 pci_set_quad(d->config + MCH_HOST_BRIDGE_PCIEXBAR,
506 MCH_HOST_BRIDGE_PCIEXBAR_DEFAULT);
507
BALATON Zoltan263cf432014-02-28 11:28:03 +0100508 d->config[MCH_HOST_BRIDGE_SMRAM] = MCH_HOST_BRIDGE_SMRAM_DEFAULT;
Gerd Hoffmann77447522015-04-15 16:43:24 +0200509 d->config[MCH_HOST_BRIDGE_ESMRAMC] = MCH_HOST_BRIDGE_ESMRAMC_DEFAULT;
Gerd Hoffmannb66a67d2015-04-15 16:48:12 +0200510 d->wmask[MCH_HOST_BRIDGE_SMRAM] = MCH_HOST_BRIDGE_SMRAM_WMASK;
511 d->wmask[MCH_HOST_BRIDGE_ESMRAMC] = MCH_HOST_BRIDGE_ESMRAMC_WMASK;
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500512
Laszlo Ersek2f295162017-06-08 18:10:13 +0200513 if (mch->ext_tseg_mbytes > 0) {
514 pci_set_word(d->config + MCH_HOST_BRIDGE_EXT_TSEG_MBYTES,
515 MCH_HOST_BRIDGE_EXT_TSEG_MBYTES_QUERY);
516 }
517
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500518 mch_update(mch);
519}
520
Markus Armbruster9af21db2015-01-19 15:52:30 +0100521static void mch_realize(PCIDevice *d, Error **errp)
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500522{
523 int i;
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500524 MCHPCIState *mch = MCH_PCI_DEVICE(d);
525
Laszlo Ersek2f295162017-06-08 18:10:13 +0200526 if (mch->ext_tseg_mbytes > MCH_HOST_BRIDGE_EXT_TSEG_MBYTES_MAX) {
527 error_setg(errp, "invalid extended-tseg-mbytes value: %" PRIu16,
528 mch->ext_tseg_mbytes);
529 return;
530 }
531
Michael S. Tsirkin83d08f22013-10-29 13:57:34 +0100532 /* setup pci memory mapping */
533 pc_pci_as_mapping_init(OBJECT(mch), mch->system_memory,
534 mch->pci_address_space);
Igor Mammedov39848902013-07-29 16:47:57 +0200535
Paolo Bonzinife6567d2015-03-31 14:10:22 +0200536 /* if *disabled* show SMRAM to all CPUs */
Paolo Bonzini40c5dce2013-06-06 21:25:08 -0400537 memory_region_init_alias(&mch->smram_region, OBJECT(mch), "smram-region",
Zihan Yangdda53ee2018-04-25 17:52:23 +0800538 mch->pci_address_space, MCH_HOST_BRIDGE_SMRAM_C_BASE,
539 MCH_HOST_BRIDGE_SMRAM_C_SIZE);
540 memory_region_add_subregion_overlap(mch->system_memory, MCH_HOST_BRIDGE_SMRAM_C_BASE,
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500541 &mch->smram_region, 1);
Paolo Bonzinife6567d2015-03-31 14:10:22 +0200542 memory_region_set_enabled(&mch->smram_region, true);
543
Paolo Bonzini64130fa2015-03-31 17:13:01 +0200544 memory_region_init_alias(&mch->open_high_smram, OBJECT(mch), "smram-open-high",
Zihan Yangdda53ee2018-04-25 17:52:23 +0800545 mch->ram_memory, MCH_HOST_BRIDGE_SMRAM_C_BASE,
546 MCH_HOST_BRIDGE_SMRAM_C_SIZE);
Paolo Bonzini64130fa2015-03-31 17:13:01 +0200547 memory_region_add_subregion_overlap(mch->system_memory, 0xfeda0000,
548 &mch->open_high_smram, 1);
549 memory_region_set_enabled(&mch->open_high_smram, false);
550
Paolo Bonzinife6567d2015-03-31 14:10:22 +0200551 /* smram, as seen by SMM CPUs */
552 memory_region_init(&mch->smram, OBJECT(mch), "smram", 1ull << 32);
553 memory_region_set_enabled(&mch->smram, true);
554 memory_region_init_alias(&mch->low_smram, OBJECT(mch), "smram-low",
Zihan Yangdda53ee2018-04-25 17:52:23 +0800555 mch->ram_memory, MCH_HOST_BRIDGE_SMRAM_C_BASE,
556 MCH_HOST_BRIDGE_SMRAM_C_SIZE);
Paolo Bonzinife6567d2015-03-31 14:10:22 +0200557 memory_region_set_enabled(&mch->low_smram, true);
Zihan Yangdda53ee2018-04-25 17:52:23 +0800558 memory_region_add_subregion(&mch->smram, MCH_HOST_BRIDGE_SMRAM_C_BASE,
559 &mch->low_smram);
Paolo Bonzini64130fa2015-03-31 17:13:01 +0200560 memory_region_init_alias(&mch->high_smram, OBJECT(mch), "smram-high",
Zihan Yangdda53ee2018-04-25 17:52:23 +0800561 mch->ram_memory, MCH_HOST_BRIDGE_SMRAM_C_BASE,
562 MCH_HOST_BRIDGE_SMRAM_C_SIZE);
Paolo Bonzini64130fa2015-03-31 17:13:01 +0200563 memory_region_set_enabled(&mch->high_smram, true);
564 memory_region_add_subregion(&mch->smram, 0xfeda0000, &mch->high_smram);
Gerd Hoffmannbafc90b2015-04-20 10:55:09 +0200565
566 memory_region_init_io(&mch->tseg_blackhole, OBJECT(mch),
567 &tseg_blackhole_ops, NULL,
568 "tseg-blackhole", 0);
569 memory_region_set_enabled(&mch->tseg_blackhole, false);
570 memory_region_add_subregion_overlap(mch->system_memory,
571 mch->below_4g_mem_size,
572 &mch->tseg_blackhole, 1);
573
574 memory_region_init_alias(&mch->tseg_window, OBJECT(mch), "tseg-window",
575 mch->ram_memory, mch->below_4g_mem_size, 0);
576 memory_region_set_enabled(&mch->tseg_window, false);
577 memory_region_add_subregion(&mch->smram, mch->below_4g_mem_size,
578 &mch->tseg_window);
Paolo Bonzinife6567d2015-03-31 14:10:22 +0200579 object_property_add_const_link(qdev_get_machine(), "smram",
580 OBJECT(&mch->smram), &error_abort);
581
Le Tanac40aa12014-08-16 13:55:41 +0800582 init_pam(DEVICE(mch), mch->ram_memory, mch->system_memory,
583 mch->pci_address_space, &mch->pam_regions[0],
584 PAM_BIOS_BASE, PAM_BIOS_SIZE);
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500585 for (i = 0; i < 12; ++i) {
Le Tanac40aa12014-08-16 13:55:41 +0800586 init_pam(DEVICE(mch), mch->ram_memory, mch->system_memory,
587 mch->pci_address_space, &mch->pam_regions[i+1],
588 PAM_EXPAN_BASE + i * PAM_EXPAN_SIZE, PAM_EXPAN_SIZE);
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500589 }
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500590}
591
Michael S. Tsirkin6f1426a2013-07-24 18:56:10 +0300592uint64_t mch_mcfg_base(void)
593{
594 bool ambiguous;
595 Object *o = object_resolve_path_type("", TYPE_MCH_PCI_DEVICE, &ambiguous);
596 if (!o) {
597 return 0;
598 }
599 return MCH_HOST_BRIDGE_PCIEXBAR_DEFAULT;
600}
601
Laszlo Ersek2f295162017-06-08 18:10:13 +0200602static Property mch_props[] = {
603 DEFINE_PROP_UINT16("extended-tseg-mbytes", MCHPCIState, ext_tseg_mbytes,
604 16),
605 DEFINE_PROP_END_OF_LIST(),
606};
607
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500608static void mch_class_init(ObjectClass *klass, void *data)
609{
610 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
611 DeviceClass *dc = DEVICE_CLASS(klass);
612
Markus Armbruster9af21db2015-01-19 15:52:30 +0100613 k->realize = mch_realize;
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500614 k->config_write = mch_write_config;
615 dc->reset = mch_reset;
Laszlo Ersek2f295162017-06-08 18:10:13 +0200616 dc->props = mch_props;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300617 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500618 dc->desc = "Host bridge";
619 dc->vmsd = &vmstate_mch;
620 k->vendor_id = PCI_VENDOR_ID_INTEL;
621 k->device_id = PCI_DEVICE_ID_INTEL_Q35_MCH;
Richard W.M. Jones451f7842013-09-02 14:43:36 +0100622 k->revision = MCH_HOST_BRIDGE_REVISION_DEFAULT;
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500623 k->class_id = PCI_CLASS_BRIDGE_HOST;
Markus Armbruster08c58f92013-11-28 17:26:58 +0100624 /*
625 * PCI-facing part of the host bridge, not usable without the
626 * host-facing part, which can't be device_add'ed, yet.
627 */
Eduardo Habkoste90f2a82017-05-03 17:35:44 -0300628 dc->user_creatable = false;
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500629}
630
631static const TypeInfo mch_info = {
632 .name = TYPE_MCH_PCI_DEVICE,
633 .parent = TYPE_PCI_DEVICE,
634 .instance_size = sizeof(MCHPCIState),
635 .class_init = mch_class_init,
Eduardo Habkostfd3b02c2017-09-27 16:56:34 -0300636 .interfaces = (InterfaceInfo[]) {
637 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
638 { },
639 },
Isaku Yamahatadf2d8b32012-11-14 15:54:06 -0500640};
641
642static void q35_register(void)
643{
644 type_register_static(&mch_info);
645 type_register_static(&q35_host_info);
646}
647
648type_init(q35_register);