blob: cc80cb48989d94761454f0de7b879c1dfd1934ac [file] [log] [blame]
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +03001/*
2 * Standard PCI Bridge Device
3 *
4 * Copyright (c) 2011 Red Hat Inc. Author: Michael S. Tsirkin <mst@redhat.com>
5 *
6 * http://www.pcisig.com/specifications/conventional/pci_to_pci_bridge_architecture/
7 *
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.
12 *
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.
17 *
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/>.
20 */
21
Peter Maydell97d54082016-01-26 18:17:15 +000022#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010023#include "qapi/error.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020024#include "qemu/module.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010025#include "hw/pci/pci_bridge.h"
26#include "hw/pci/pci_ids.h"
27#include "hw/pci/msi.h"
28#include "hw/pci/shpc.h"
29#include "hw/pci/slotid_cap.h"
Markus Armbrustera27bd6c2019-08-12 07:23:51 +020030#include "hw/qdev-properties.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010031#include "exec/memory.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010032#include "hw/pci/pci_bus.h"
Igor Mammedov5d268702014-02-05 16:36:50 +010033#include "hw/hotplug.h"
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +030034
Gerd Hoffmanneb6c6a62015-06-18 12:17:29 +020035#define TYPE_PCI_BRIDGE_DEV "pci-bridge"
36#define TYPE_PCI_BRIDGE_SEAT_DEV "pci-bridge-seat"
Andreas Färber57524e12013-07-12 19:16:46 +020037#define PCI_BRIDGE_DEV(obj) \
38 OBJECT_CHECK(PCIBridgeDev, (obj), TYPE_PCI_BRIDGE_DEV)
39
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +030040struct PCIBridgeDev {
Andreas Färber57524e12013-07-12 19:16:46 +020041 /*< private >*/
42 PCIBridge parent_obj;
43 /*< public >*/
44
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +030045 MemoryRegion bar;
46 uint8_t chassis_nr;
Cao jin69b205b2016-06-20 14:13:38 +080047#define PCI_BRIDGE_DEV_F_SHPC_REQ 0
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +030048 uint32_t flags;
Cao jin69b205b2016-06-20 14:13:38 +080049
50 OnOffAuto msi;
Jing Liu6755e612018-08-21 11:18:07 +080051
52 /* additional resources to reserve */
53 PCIResReserve res_reserve;
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +030054};
55typedef struct PCIBridgeDev PCIBridgeDev;
56
Mao Zhongyi344475e2017-06-27 14:16:53 +080057static void pci_bridge_dev_realize(PCIDevice *dev, Error **errp)
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +030058{
Andreas Färberf055e962013-07-11 17:13:43 +020059 PCIBridge *br = PCI_BRIDGE(dev);
Andreas Färber57524e12013-07-12 19:16:46 +020060 PCIBridgeDev *bridge_dev = PCI_BRIDGE_DEV(dev);
Alex Williamsonf90c2bc2012-07-03 22:39:27 -060061 int err;
Cao jin1108b2f2016-06-20 14:13:39 +080062 Error *local_err = NULL;
Alex Williamsonf90c2bc2012-07-03 22:39:27 -060063
Cao jin9cfaa002016-01-15 10:23:32 +080064 pci_bridge_initfn(dev, TYPE_PCI_BUS);
65
Laszlo Ersek4e5c9bf2015-06-19 04:40:13 +020066 if (bridge_dev->flags & (1 << PCI_BRIDGE_DEV_F_SHPC_REQ)) {
67 dev->config[PCI_INTERRUPT_PIN] = 0x1;
68 memory_region_init(&bridge_dev->bar, OBJECT(dev), "shpc-bar",
69 shpc_bar_size(dev));
Mao Zhongyi344475e2017-06-27 14:16:53 +080070 err = shpc_init(dev, &br->sec_bus, &bridge_dev->bar, 0, errp);
Laszlo Ersek4e5c9bf2015-06-19 04:40:13 +020071 if (err) {
72 goto shpc_error;
73 }
74 } else {
75 /* MSI is not applicable without SHPC */
Cao jin69b205b2016-06-20 14:13:38 +080076 bridge_dev->msi = ON_OFF_AUTO_OFF;
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +030077 }
Cao jin52ea63d2016-06-10 17:54:23 +080078
Mao Zhongyi344475e2017-06-27 14:16:53 +080079 err = slotid_cap_init(dev, 0, bridge_dev->chassis_nr, 0, errp);
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +030080 if (err) {
81 goto slotid_error;
82 }
Cao jin52ea63d2016-06-10 17:54:23 +080083
Cao jin1108b2f2016-06-20 14:13:39 +080084 if (bridge_dev->msi != ON_OFF_AUTO_OFF) {
85 /* it means SHPC exists, because MSI is needed by SHPC */
86
87 err = msi_init(dev, 0, 1, true, true, &local_err);
88 /* Any error other than -ENOTSUP(board's MSI support is broken)
89 * is a programming error */
90 assert(!err || err == -ENOTSUP);
91 if (err && bridge_dev->msi == ON_OFF_AUTO_ON) {
92 /* Can't satisfy user's explicit msi=on request, fail */
93 error_append_hint(&local_err, "You have to use msi=auto (default) "
94 "or msi=off with this machine type.\n");
Mao Zhongyi344475e2017-06-27 14:16:53 +080095 error_propagate(errp, local_err);
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +030096 goto msi_error;
97 }
Cao jin1108b2f2016-06-20 14:13:39 +080098 assert(!local_err || bridge_dev->msi == ON_OFF_AUTO_AUTO);
99 /* With msi=auto, we fall back to MSI off silently */
100 error_free(local_err);
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +0300101 }
Cao jin52ea63d2016-06-10 17:54:23 +0800102
Jing Liu6755e612018-08-21 11:18:07 +0800103 err = pci_bridge_qemu_reserve_cap_init(dev, 0,
104 bridge_dev->res_reserve, errp);
105 if (err) {
106 goto cap_error;
107 }
108
Laszlo Ersek4e5c9bf2015-06-19 04:40:13 +0200109 if (shpc_present(dev)) {
110 /* TODO: spec recommends using 64 bit prefetcheable BAR.
111 * Check whether that works well. */
112 pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY |
113 PCI_BASE_ADDRESS_MEM_TYPE_64, &bridge_dev->bar);
114 }
Mao Zhongyi344475e2017-06-27 14:16:53 +0800115 return;
Cao jin52ea63d2016-06-10 17:54:23 +0800116
Jing Liu6755e612018-08-21 11:18:07 +0800117cap_error:
118 msi_uninit(dev);
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +0300119msi_error:
120 slotid_cap_cleanup(dev);
121slotid_error:
Laszlo Ersek4e5c9bf2015-06-19 04:40:13 +0200122 if (shpc_present(dev)) {
123 shpc_cleanup(dev, &bridge_dev->bar);
124 }
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +0300125shpc_error:
Alex Williamsonf90c2bc2012-07-03 22:39:27 -0600126 pci_bridge_exitfn(dev);
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +0300127}
128
Alex Williamsonf90c2bc2012-07-03 22:39:27 -0600129static void pci_bridge_dev_exitfn(PCIDevice *dev)
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +0300130{
Andreas Färber57524e12013-07-12 19:16:46 +0200131 PCIBridgeDev *bridge_dev = PCI_BRIDGE_DEV(dev);
Jing Liu6755e612018-08-21 11:18:07 +0800132
133 pci_del_capability(dev, PCI_CAP_ID_VNDR, sizeof(PCIBridgeQemuCap));
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +0300134 if (msi_present(dev)) {
135 msi_uninit(dev);
136 }
137 slotid_cap_cleanup(dev);
Laszlo Ersek4e5c9bf2015-06-19 04:40:13 +0200138 if (shpc_present(dev)) {
139 shpc_cleanup(dev, &bridge_dev->bar);
140 }
Alex Williamsonf90c2bc2012-07-03 22:39:27 -0600141 pci_bridge_exitfn(dev);
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +0300142}
143
Paolo Bonzini5cd5e702015-02-12 21:39:20 +0100144static void pci_bridge_dev_instance_finalize(Object *obj)
145{
Laszlo Ersek4e5c9bf2015-06-19 04:40:13 +0200146 /* this function is idempotent and handles (PCIDevice.shpc == NULL) */
Paolo Bonzini5cd5e702015-02-12 21:39:20 +0100147 shpc_free(PCI_DEVICE(obj));
148}
149
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +0300150static void pci_bridge_dev_write_config(PCIDevice *d,
151 uint32_t address, uint32_t val, int len)
152{
153 pci_bridge_write_config(d, address, val, len);
154 if (msi_present(d)) {
155 msi_write_config(d, address, val, len);
156 }
Laszlo Ersek4e5c9bf2015-06-19 04:40:13 +0200157 if (shpc_present(d)) {
158 shpc_cap_write_config(d, address, val, len);
159 }
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +0300160}
161
162static void qdev_pci_bridge_dev_reset(DeviceState *qdev)
163{
Andreas Färber57524e12013-07-12 19:16:46 +0200164 PCIDevice *dev = PCI_DEVICE(qdev);
Jan Kiszkacbd2d432012-05-15 20:09:56 -0300165
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +0300166 pci_bridge_reset(qdev);
Laszlo Ersek4e5c9bf2015-06-19 04:40:13 +0200167 if (shpc_present(dev)) {
168 shpc_reset(dev);
169 }
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +0300170}
171
172static Property pci_bridge_dev_properties[] = {
173 /* Note: 0 is not a legal chassis number. */
Laszlo Ersek3cf0ecb2015-06-19 04:40:10 +0200174 DEFINE_PROP_UINT8(PCI_BRIDGE_DEV_PROP_CHASSIS_NR, PCIBridgeDev, chassis_nr,
175 0),
Cao jin69b205b2016-06-20 14:13:38 +0800176 DEFINE_PROP_ON_OFF_AUTO(PCI_BRIDGE_DEV_PROP_MSI, PCIBridgeDev, msi,
177 ON_OFF_AUTO_AUTO),
Laszlo Ersek4e5c9bf2015-06-19 04:40:13 +0200178 DEFINE_PROP_BIT(PCI_BRIDGE_DEV_PROP_SHPC, PCIBridgeDev, flags,
Marcel Apfelbaum2fa35662017-05-11 13:25:29 +0300179 PCI_BRIDGE_DEV_F_SHPC_REQ, true),
Jing Liu6755e612018-08-21 11:18:07 +0800180 DEFINE_PROP_UINT32("bus-reserve", PCIBridgeDev,
181 res_reserve.bus, -1),
182 DEFINE_PROP_SIZE("io-reserve", PCIBridgeDev,
183 res_reserve.io, -1),
184 DEFINE_PROP_SIZE("mem-reserve", PCIBridgeDev,
185 res_reserve.mem_non_pref, -1),
186 DEFINE_PROP_SIZE("pref32-reserve", PCIBridgeDev,
187 res_reserve.mem_pref_32, -1),
188 DEFINE_PROP_SIZE("pref64-reserve", PCIBridgeDev,
189 res_reserve.mem_pref_64, -1),
190
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +0300191 DEFINE_PROP_END_OF_LIST(),
192};
193
Laszlo Ersek4e5c9bf2015-06-19 04:40:13 +0200194static bool pci_device_shpc_present(void *opaque, int version_id)
195{
196 PCIDevice *dev = opaque;
197
198 return shpc_present(dev);
199}
200
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +0300201static const VMStateDescription pci_bridge_dev_vmstate = {
202 .name = "pci_bridge",
Peter Xu9d6b9db2018-02-06 15:39:33 +0800203 .priority = MIG_PRI_PCI_BUS,
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +0300204 .fields = (VMStateField[]) {
Andreas Färber57524e12013-07-12 19:16:46 +0200205 VMSTATE_PCI_DEVICE(parent_obj, PCIBridge),
Laszlo Ersek4e5c9bf2015-06-19 04:40:13 +0200206 SHPC_VMSTATE(shpc, PCIDevice, pci_device_shpc_present),
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +0300207 VMSTATE_END_OF_LIST()
208 }
209};
210
David Hildenbrand62b76562018-12-12 10:16:21 +0100211void pci_bridge_dev_plug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
212 Error **errp)
Laszlo Ersek4e5c9bf2015-06-19 04:40:13 +0200213{
214 PCIDevice *pci_hotplug_dev = PCI_DEVICE(hotplug_dev);
215
216 if (!shpc_present(pci_hotplug_dev)) {
217 error_setg(errp, "standard hotplug controller has been disabled for "
David Hildenbrand62b76562018-12-12 10:16:21 +0100218 "this %s", object_get_typename(OBJECT(hotplug_dev)));
Laszlo Ersek4e5c9bf2015-06-19 04:40:13 +0200219 return;
220 }
David Hildenbrand851fedf2018-12-12 10:16:14 +0100221 shpc_device_plug_cb(hotplug_dev, dev, errp);
Laszlo Ersek4e5c9bf2015-06-19 04:40:13 +0200222}
223
David Hildenbrand8f560cd2018-12-12 10:16:22 +0100224void pci_bridge_dev_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
225 Error **errp)
226{
227 PCIDevice *pci_hotplug_dev = PCI_DEVICE(hotplug_dev);
228
229 g_assert(shpc_present(pci_hotplug_dev));
230 shpc_device_unplug_cb(hotplug_dev, dev, errp);
231}
232
David Hildenbrand62b76562018-12-12 10:16:21 +0100233void pci_bridge_dev_unplug_request_cb(HotplugHandler *hotplug_dev,
234 DeviceState *dev, Error **errp)
Laszlo Ersek4e5c9bf2015-06-19 04:40:13 +0200235{
236 PCIDevice *pci_hotplug_dev = PCI_DEVICE(hotplug_dev);
237
238 if (!shpc_present(pci_hotplug_dev)) {
239 error_setg(errp, "standard hotplug controller has been disabled for "
David Hildenbrand62b76562018-12-12 10:16:21 +0100240 "this %s", object_get_typename(OBJECT(hotplug_dev)));
Laszlo Ersek4e5c9bf2015-06-19 04:40:13 +0200241 return;
242 }
David Hildenbrand851fedf2018-12-12 10:16:14 +0100243 shpc_device_unplug_request_cb(hotplug_dev, dev, errp);
Laszlo Ersek4e5c9bf2015-06-19 04:40:13 +0200244}
245
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +0300246static void pci_bridge_dev_class_init(ObjectClass *klass, void *data)
247{
248 DeviceClass *dc = DEVICE_CLASS(klass);
249 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
Igor Mammedov5d268702014-02-05 16:36:50 +0100250 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
251
Mao Zhongyi344475e2017-06-27 14:16:53 +0800252 k->realize = pci_bridge_dev_realize;
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +0300253 k->exit = pci_bridge_dev_exitfn;
254 k->config_write = pci_bridge_dev_write_config;
Paolo Bonzini5c03a252012-12-13 10:19:38 +0100255 k->vendor_id = PCI_VENDOR_ID_REDHAT;
256 k->device_id = PCI_DEVICE_ID_REDHAT_BRIDGE;
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +0300257 k->class_id = PCI_CLASS_BRIDGE_PCI;
David Gibson91f4c992019-05-13 16:19:38 +1000258 k->is_bridge = true;
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +0300259 dc->desc = "Standard PCI Bridge";
260 dc->reset = qdev_pci_bridge_dev_reset;
261 dc->props = pci_bridge_dev_properties;
262 dc->vmsd = &pci_bridge_dev_vmstate;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300263 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
David Hildenbrand851fedf2018-12-12 10:16:14 +0100264 hc->plug = pci_bridge_dev_plug_cb;
David Hildenbrand8f560cd2018-12-12 10:16:22 +0100265 hc->unplug = pci_bridge_dev_unplug_cb;
David Hildenbrand851fedf2018-12-12 10:16:14 +0100266 hc->unplug_request = pci_bridge_dev_unplug_request_cb;
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +0300267}
268
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100269static const TypeInfo pci_bridge_dev_info = {
Paolo Bonzini5cd5e702015-02-12 21:39:20 +0100270 .name = TYPE_PCI_BRIDGE_DEV,
271 .parent = TYPE_PCI_BRIDGE,
272 .instance_size = sizeof(PCIBridgeDev),
273 .class_init = pci_bridge_dev_class_init,
274 .instance_finalize = pci_bridge_dev_instance_finalize,
Igor Mammedov5d268702014-02-05 16:36:50 +0100275 .interfaces = (InterfaceInfo[]) {
276 { TYPE_HOTPLUG_HANDLER },
Eduardo Habkostfd3b02c2017-09-27 16:56:34 -0300277 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
Igor Mammedov5d268702014-02-05 16:36:50 +0100278 { }
279 }
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +0300280};
281
Gerd Hoffmanneb6c6a62015-06-18 12:17:29 +0200282/*
283 * Multiseat bridge. Same as the standard pci bridge, only with a
284 * different pci id, so we can match it easily in the guest for
285 * automagic multiseat configuration. See docs/multiseat.txt for more.
286 */
287static void pci_bridge_dev_seat_class_init(ObjectClass *klass, void *data)
288{
289 DeviceClass *dc = DEVICE_CLASS(klass);
290 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
291
292 k->device_id = PCI_DEVICE_ID_REDHAT_BRIDGE_SEAT;
293 dc->desc = "Standard PCI Bridge (multiseat)";
294}
295
296static const TypeInfo pci_bridge_dev_seat_info = {
297 .name = TYPE_PCI_BRIDGE_SEAT_DEV,
298 .parent = TYPE_PCI_BRIDGE_DEV,
299 .instance_size = sizeof(PCIBridgeDev),
300 .class_init = pci_bridge_dev_seat_class_init,
301};
302
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +0300303static void pci_bridge_dev_register(void)
304{
305 type_register_static(&pci_bridge_dev_info);
Gerd Hoffmanneb6c6a62015-06-18 12:17:29 +0200306 type_register_static(&pci_bridge_dev_seat_info);
Michael S. Tsirkin4eb812f2011-07-03 16:00:09 +0300307}
308
309type_init(pci_bridge_dev_register);