xref: /openbmc/qemu/hw/pci-bridge/pci_bridge_dev.c (revision eb6c6a604890201e321a6ace32973d10dc033245)
1 /*
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 
22 #include "hw/pci/pci_bridge.h"
23 #include "hw/pci/pci_ids.h"
24 #include "hw/pci/msi.h"
25 #include "hw/pci/shpc.h"
26 #include "hw/pci/slotid_cap.h"
27 #include "exec/memory.h"
28 #include "hw/pci/pci_bus.h"
29 #include "hw/hotplug.h"
30 
31 #define TYPE_PCI_BRIDGE_DEV      "pci-bridge"
32 #define TYPE_PCI_BRIDGE_SEAT_DEV "pci-bridge-seat"
33 #define PCI_BRIDGE_DEV(obj) \
34     OBJECT_CHECK(PCIBridgeDev, (obj), TYPE_PCI_BRIDGE_DEV)
35 
36 struct PCIBridgeDev {
37     /*< private >*/
38     PCIBridge parent_obj;
39     /*< public >*/
40 
41     MemoryRegion bar;
42     uint8_t chassis_nr;
43 #define PCI_BRIDGE_DEV_F_MSI_REQ 0
44     uint32_t flags;
45 };
46 typedef struct PCIBridgeDev PCIBridgeDev;
47 
48 static int pci_bridge_dev_initfn(PCIDevice *dev)
49 {
50     PCIBridge *br = PCI_BRIDGE(dev);
51     PCIBridgeDev *bridge_dev = PCI_BRIDGE_DEV(dev);
52     int err;
53 
54     err = pci_bridge_initfn(dev, TYPE_PCI_BUS);
55     if (err) {
56         goto bridge_error;
57     }
58     dev->config[PCI_INTERRUPT_PIN] = 0x1;
59     memory_region_init(&bridge_dev->bar, OBJECT(dev), "shpc-bar", shpc_bar_size(dev));
60     err = shpc_init(dev, &br->sec_bus, &bridge_dev->bar, 0);
61     if (err) {
62         goto shpc_error;
63     }
64     err = slotid_cap_init(dev, 0, bridge_dev->chassis_nr, 0);
65     if (err) {
66         goto slotid_error;
67     }
68     if ((bridge_dev->flags & (1 << PCI_BRIDGE_DEV_F_MSI_REQ)) &&
69         msi_supported) {
70         err = msi_init(dev, 0, 1, true, true);
71         if (err < 0) {
72             goto msi_error;
73         }
74     }
75     /* TODO: spec recommends using 64 bit prefetcheable BAR.
76      * Check whether that works well. */
77     pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY |
78 		     PCI_BASE_ADDRESS_MEM_TYPE_64, &bridge_dev->bar);
79     return 0;
80 msi_error:
81     slotid_cap_cleanup(dev);
82 slotid_error:
83     shpc_cleanup(dev, &bridge_dev->bar);
84 shpc_error:
85     pci_bridge_exitfn(dev);
86 bridge_error:
87     return err;
88 }
89 
90 static void pci_bridge_dev_exitfn(PCIDevice *dev)
91 {
92     PCIBridgeDev *bridge_dev = PCI_BRIDGE_DEV(dev);
93     if (msi_present(dev)) {
94         msi_uninit(dev);
95     }
96     slotid_cap_cleanup(dev);
97     shpc_cleanup(dev, &bridge_dev->bar);
98     pci_bridge_exitfn(dev);
99 }
100 
101 static void pci_bridge_dev_instance_finalize(Object *obj)
102 {
103     shpc_free(PCI_DEVICE(obj));
104 }
105 
106 static void pci_bridge_dev_write_config(PCIDevice *d,
107                                         uint32_t address, uint32_t val, int len)
108 {
109     pci_bridge_write_config(d, address, val, len);
110     if (msi_present(d)) {
111         msi_write_config(d, address, val, len);
112     }
113     shpc_cap_write_config(d, address, val, len);
114 }
115 
116 static void qdev_pci_bridge_dev_reset(DeviceState *qdev)
117 {
118     PCIDevice *dev = PCI_DEVICE(qdev);
119 
120     pci_bridge_reset(qdev);
121     shpc_reset(dev);
122 }
123 
124 static Property pci_bridge_dev_properties[] = {
125                     /* Note: 0 is not a legal chassis number. */
126     DEFINE_PROP_UINT8("chassis_nr", PCIBridgeDev, chassis_nr, 0),
127     DEFINE_PROP_BIT("msi", PCIBridgeDev, flags, PCI_BRIDGE_DEV_F_MSI_REQ, true),
128     DEFINE_PROP_END_OF_LIST(),
129 };
130 
131 static const VMStateDescription pci_bridge_dev_vmstate = {
132     .name = "pci_bridge",
133     .fields = (VMStateField[]) {
134         VMSTATE_PCI_DEVICE(parent_obj, PCIBridge),
135         SHPC_VMSTATE(shpc, PCIDevice),
136         VMSTATE_END_OF_LIST()
137     }
138 };
139 
140 static void pci_bridge_dev_class_init(ObjectClass *klass, void *data)
141 {
142     DeviceClass *dc = DEVICE_CLASS(klass);
143     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
144     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
145 
146     k->init = pci_bridge_dev_initfn;
147     k->exit = pci_bridge_dev_exitfn;
148     k->config_write = pci_bridge_dev_write_config;
149     k->vendor_id = PCI_VENDOR_ID_REDHAT;
150     k->device_id = PCI_DEVICE_ID_REDHAT_BRIDGE;
151     k->class_id = PCI_CLASS_BRIDGE_PCI;
152     k->is_bridge = 1,
153     dc->desc = "Standard PCI Bridge";
154     dc->reset = qdev_pci_bridge_dev_reset;
155     dc->props = pci_bridge_dev_properties;
156     dc->vmsd = &pci_bridge_dev_vmstate;
157     set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
158     hc->plug = shpc_device_hotplug_cb;
159     hc->unplug_request = shpc_device_hot_unplug_request_cb;
160 }
161 
162 static const TypeInfo pci_bridge_dev_info = {
163     .name              = TYPE_PCI_BRIDGE_DEV,
164     .parent            = TYPE_PCI_BRIDGE,
165     .instance_size     = sizeof(PCIBridgeDev),
166     .class_init        = pci_bridge_dev_class_init,
167     .instance_finalize = pci_bridge_dev_instance_finalize,
168     .interfaces = (InterfaceInfo[]) {
169         { TYPE_HOTPLUG_HANDLER },
170         { }
171     }
172 };
173 
174 /*
175  * Multiseat bridge.  Same as the standard pci bridge, only with a
176  * different pci id, so we can match it easily in the guest for
177  * automagic multiseat configuration.  See docs/multiseat.txt for more.
178  */
179 static void pci_bridge_dev_seat_class_init(ObjectClass *klass, void *data)
180 {
181     DeviceClass *dc = DEVICE_CLASS(klass);
182     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
183 
184     k->device_id = PCI_DEVICE_ID_REDHAT_BRIDGE_SEAT;
185     dc->desc = "Standard PCI Bridge (multiseat)";
186 }
187 
188 static const TypeInfo pci_bridge_dev_seat_info = {
189     .name              = TYPE_PCI_BRIDGE_SEAT_DEV,
190     .parent            = TYPE_PCI_BRIDGE_DEV,
191     .instance_size     = sizeof(PCIBridgeDev),
192     .class_init        = pci_bridge_dev_seat_class_init,
193 };
194 
195 static void pci_bridge_dev_register(void)
196 {
197     type_register_static(&pci_bridge_dev_info);
198     type_register_static(&pci_bridge_dev_seat_info);
199 }
200 
201 type_init(pci_bridge_dev_register);
202