2 * Standard PCI Bridge Device
4 * Copyright (c) 2011 Red Hat Inc. Author: Michael S. Tsirkin <mst@redhat.com>
6 * http://www.pcisig.com/specifications/conventional/pci_to_pci_bridge_architecture/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "qemu/osdep.h"
23 #include "hw/pci/pci_bridge.h"
24 #include "hw/pci/pci_ids.h"
25 #include "hw/pci/msi.h"
26 #include "hw/pci/shpc.h"
27 #include "hw/pci/slotid_cap.h"
28 #include "exec/memory.h"
29 #include "hw/pci/pci_bus.h"
30 #include "hw/hotplug.h"
32 #define TYPE_PCI_BRIDGE_DEV "pci-bridge"
33 #define TYPE_PCI_BRIDGE_SEAT_DEV "pci-bridge-seat"
34 #define PCI_BRIDGE_DEV(obj) \
35 OBJECT_CHECK(PCIBridgeDev, (obj), TYPE_PCI_BRIDGE_DEV)
44 #define PCI_BRIDGE_DEV_F_MSI_REQ 0
45 #define PCI_BRIDGE_DEV_F_SHPC_REQ 1
48 typedef struct PCIBridgeDev PCIBridgeDev
;
50 static int pci_bridge_dev_initfn(PCIDevice
*dev
)
52 PCIBridge
*br
= PCI_BRIDGE(dev
);
53 PCIBridgeDev
*bridge_dev
= PCI_BRIDGE_DEV(dev
);
56 err
= pci_bridge_initfn(dev
, TYPE_PCI_BUS
);
60 if (bridge_dev
->flags
& (1 << PCI_BRIDGE_DEV_F_SHPC_REQ
)) {
61 dev
->config
[PCI_INTERRUPT_PIN
] = 0x1;
62 memory_region_init(&bridge_dev
->bar
, OBJECT(dev
), "shpc-bar",
64 err
= shpc_init(dev
, &br
->sec_bus
, &bridge_dev
->bar
, 0);
69 /* MSI is not applicable without SHPC */
70 bridge_dev
->flags
&= ~(1 << PCI_BRIDGE_DEV_F_MSI_REQ
);
72 err
= slotid_cap_init(dev
, 0, bridge_dev
->chassis_nr
, 0);
76 if ((bridge_dev
->flags
& (1 << PCI_BRIDGE_DEV_F_MSI_REQ
)) &&
78 err
= msi_init(dev
, 0, 1, true, true);
83 if (shpc_present(dev
)) {
84 /* TODO: spec recommends using 64 bit prefetcheable BAR.
85 * Check whether that works well. */
86 pci_register_bar(dev
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
|
87 PCI_BASE_ADDRESS_MEM_TYPE_64
, &bridge_dev
->bar
);
91 slotid_cap_cleanup(dev
);
93 if (shpc_present(dev
)) {
94 shpc_cleanup(dev
, &bridge_dev
->bar
);
97 pci_bridge_exitfn(dev
);
102 static void pci_bridge_dev_exitfn(PCIDevice
*dev
)
104 PCIBridgeDev
*bridge_dev
= PCI_BRIDGE_DEV(dev
);
105 if (msi_present(dev
)) {
108 slotid_cap_cleanup(dev
);
109 if (shpc_present(dev
)) {
110 shpc_cleanup(dev
, &bridge_dev
->bar
);
112 pci_bridge_exitfn(dev
);
115 static void pci_bridge_dev_instance_finalize(Object
*obj
)
117 /* this function is idempotent and handles (PCIDevice.shpc == NULL) */
118 shpc_free(PCI_DEVICE(obj
));
121 static void pci_bridge_dev_write_config(PCIDevice
*d
,
122 uint32_t address
, uint32_t val
, int len
)
124 pci_bridge_write_config(d
, address
, val
, len
);
125 if (msi_present(d
)) {
126 msi_write_config(d
, address
, val
, len
);
128 if (shpc_present(d
)) {
129 shpc_cap_write_config(d
, address
, val
, len
);
133 static void qdev_pci_bridge_dev_reset(DeviceState
*qdev
)
135 PCIDevice
*dev
= PCI_DEVICE(qdev
);
137 pci_bridge_reset(qdev
);
138 if (shpc_present(dev
)) {
143 static Property pci_bridge_dev_properties
[] = {
144 /* Note: 0 is not a legal chassis number. */
145 DEFINE_PROP_UINT8(PCI_BRIDGE_DEV_PROP_CHASSIS_NR
, PCIBridgeDev
, chassis_nr
,
147 DEFINE_PROP_BIT(PCI_BRIDGE_DEV_PROP_MSI
, PCIBridgeDev
, flags
,
148 PCI_BRIDGE_DEV_F_MSI_REQ
, true),
149 DEFINE_PROP_BIT(PCI_BRIDGE_DEV_PROP_SHPC
, PCIBridgeDev
, flags
,
150 PCI_BRIDGE_DEV_F_SHPC_REQ
, true),
151 DEFINE_PROP_END_OF_LIST(),
154 static bool pci_device_shpc_present(void *opaque
, int version_id
)
156 PCIDevice
*dev
= opaque
;
158 return shpc_present(dev
);
161 static const VMStateDescription pci_bridge_dev_vmstate
= {
162 .name
= "pci_bridge",
163 .fields
= (VMStateField
[]) {
164 VMSTATE_PCI_DEVICE(parent_obj
, PCIBridge
),
165 SHPC_VMSTATE(shpc
, PCIDevice
, pci_device_shpc_present
),
166 VMSTATE_END_OF_LIST()
170 static void pci_bridge_dev_hotplug_cb(HotplugHandler
*hotplug_dev
,
171 DeviceState
*dev
, Error
**errp
)
173 PCIDevice
*pci_hotplug_dev
= PCI_DEVICE(hotplug_dev
);
175 if (!shpc_present(pci_hotplug_dev
)) {
176 error_setg(errp
, "standard hotplug controller has been disabled for "
177 "this %s", TYPE_PCI_BRIDGE_DEV
);
180 shpc_device_hotplug_cb(hotplug_dev
, dev
, errp
);
183 static void pci_bridge_dev_hot_unplug_request_cb(HotplugHandler
*hotplug_dev
,
187 PCIDevice
*pci_hotplug_dev
= PCI_DEVICE(hotplug_dev
);
189 if (!shpc_present(pci_hotplug_dev
)) {
190 error_setg(errp
, "standard hotplug controller has been disabled for "
191 "this %s", TYPE_PCI_BRIDGE_DEV
);
194 shpc_device_hot_unplug_request_cb(hotplug_dev
, dev
, errp
);
197 static void pci_bridge_dev_class_init(ObjectClass
*klass
, void *data
)
199 DeviceClass
*dc
= DEVICE_CLASS(klass
);
200 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
201 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
203 k
->init
= pci_bridge_dev_initfn
;
204 k
->exit
= pci_bridge_dev_exitfn
;
205 k
->config_write
= pci_bridge_dev_write_config
;
206 k
->vendor_id
= PCI_VENDOR_ID_REDHAT
;
207 k
->device_id
= PCI_DEVICE_ID_REDHAT_BRIDGE
;
208 k
->class_id
= PCI_CLASS_BRIDGE_PCI
;
210 dc
->desc
= "Standard PCI Bridge";
211 dc
->reset
= qdev_pci_bridge_dev_reset
;
212 dc
->props
= pci_bridge_dev_properties
;
213 dc
->vmsd
= &pci_bridge_dev_vmstate
;
214 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
215 hc
->plug
= pci_bridge_dev_hotplug_cb
;
216 hc
->unplug_request
= pci_bridge_dev_hot_unplug_request_cb
;
219 static const TypeInfo pci_bridge_dev_info
= {
220 .name
= TYPE_PCI_BRIDGE_DEV
,
221 .parent
= TYPE_PCI_BRIDGE
,
222 .instance_size
= sizeof(PCIBridgeDev
),
223 .class_init
= pci_bridge_dev_class_init
,
224 .instance_finalize
= pci_bridge_dev_instance_finalize
,
225 .interfaces
= (InterfaceInfo
[]) {
226 { TYPE_HOTPLUG_HANDLER
},
232 * Multiseat bridge. Same as the standard pci bridge, only with a
233 * different pci id, so we can match it easily in the guest for
234 * automagic multiseat configuration. See docs/multiseat.txt for more.
236 static void pci_bridge_dev_seat_class_init(ObjectClass
*klass
, void *data
)
238 DeviceClass
*dc
= DEVICE_CLASS(klass
);
239 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
241 k
->device_id
= PCI_DEVICE_ID_REDHAT_BRIDGE_SEAT
;
242 dc
->desc
= "Standard PCI Bridge (multiseat)";
245 static const TypeInfo pci_bridge_dev_seat_info
= {
246 .name
= TYPE_PCI_BRIDGE_SEAT_DEV
,
247 .parent
= TYPE_PCI_BRIDGE_DEV
,
248 .instance_size
= sizeof(PCIBridgeDev
),
249 .class_init
= pci_bridge_dev_seat_class_init
,
252 static void pci_bridge_dev_register(void)
254 type_register_static(&pci_bridge_dev_info
);
255 type_register_static(&pci_bridge_dev_seat_info
);
258 type_init(pci_bridge_dev_register
);