xref: /qemu/hw/pci-bridge/pcie_pci_bridge.c (revision 0b8fa32f551e863bb548a11394239239270dd3dc)
1a35fe226SAleksandr Bezzubikov /*
2a35fe226SAleksandr Bezzubikov  * QEMU Generic PCIE-PCI Bridge
3a35fe226SAleksandr Bezzubikov  *
4a35fe226SAleksandr Bezzubikov  * Copyright (c) 2017 Aleksandr Bezzubikov
5a35fe226SAleksandr Bezzubikov  *
6a35fe226SAleksandr Bezzubikov  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7a35fe226SAleksandr Bezzubikov  * See the COPYING file in the top-level directory.
8a35fe226SAleksandr Bezzubikov  */
9a35fe226SAleksandr Bezzubikov 
10a35fe226SAleksandr Bezzubikov #include "qemu/osdep.h"
11a35fe226SAleksandr Bezzubikov #include "qapi/error.h"
12*0b8fa32fSMarkus Armbruster #include "qemu/module.h"
13a35fe226SAleksandr Bezzubikov #include "hw/pci/pci.h"
14a35fe226SAleksandr Bezzubikov #include "hw/pci/pci_bus.h"
15a35fe226SAleksandr Bezzubikov #include "hw/pci/pci_bridge.h"
16a35fe226SAleksandr Bezzubikov #include "hw/pci/msi.h"
17a35fe226SAleksandr Bezzubikov #include "hw/pci/shpc.h"
18a35fe226SAleksandr Bezzubikov #include "hw/pci/slotid_cap.h"
19a35fe226SAleksandr Bezzubikov 
20a35fe226SAleksandr Bezzubikov typedef struct PCIEPCIBridge {
21a35fe226SAleksandr Bezzubikov     /*< private >*/
22a35fe226SAleksandr Bezzubikov     PCIBridge parent_obj;
23a35fe226SAleksandr Bezzubikov 
24a35fe226SAleksandr Bezzubikov     OnOffAuto msi;
25a35fe226SAleksandr Bezzubikov     MemoryRegion shpc_bar;
26a35fe226SAleksandr Bezzubikov     /*< public >*/
27a35fe226SAleksandr Bezzubikov } PCIEPCIBridge;
28a35fe226SAleksandr Bezzubikov 
29a35fe226SAleksandr Bezzubikov #define TYPE_PCIE_PCI_BRIDGE_DEV "pcie-pci-bridge"
30a35fe226SAleksandr Bezzubikov #define PCIE_PCI_BRIDGE_DEV(obj) \
31a35fe226SAleksandr Bezzubikov         OBJECT_CHECK(PCIEPCIBridge, (obj), TYPE_PCIE_PCI_BRIDGE_DEV)
32a35fe226SAleksandr Bezzubikov 
33a35fe226SAleksandr Bezzubikov static void pcie_pci_bridge_realize(PCIDevice *d, Error **errp)
34a35fe226SAleksandr Bezzubikov {
35a35fe226SAleksandr Bezzubikov     PCIBridge *br = PCI_BRIDGE(d);
36a35fe226SAleksandr Bezzubikov     PCIEPCIBridge *pcie_br = PCIE_PCI_BRIDGE_DEV(d);
37a35fe226SAleksandr Bezzubikov     int rc, pos;
38a35fe226SAleksandr Bezzubikov 
39a35fe226SAleksandr Bezzubikov     pci_bridge_initfn(d, TYPE_PCI_BUS);
40a35fe226SAleksandr Bezzubikov 
41a35fe226SAleksandr Bezzubikov     d->config[PCI_INTERRUPT_PIN] = 0x1;
42a35fe226SAleksandr Bezzubikov     memory_region_init(&pcie_br->shpc_bar, OBJECT(d), "shpc-bar",
43a35fe226SAleksandr Bezzubikov                        shpc_bar_size(d));
44a35fe226SAleksandr Bezzubikov     rc = shpc_init(d, &br->sec_bus, &pcie_br->shpc_bar, 0, errp);
45a35fe226SAleksandr Bezzubikov     if (rc) {
46a35fe226SAleksandr Bezzubikov         goto error;
47a35fe226SAleksandr Bezzubikov     }
48a35fe226SAleksandr Bezzubikov 
49a35fe226SAleksandr Bezzubikov     rc = pcie_cap_init(d, 0, PCI_EXP_TYPE_PCI_BRIDGE, 0, errp);
50a35fe226SAleksandr Bezzubikov     if (rc < 0) {
51a35fe226SAleksandr Bezzubikov         goto cap_error;
52a35fe226SAleksandr Bezzubikov     }
53a35fe226SAleksandr Bezzubikov 
54a35fe226SAleksandr Bezzubikov     pos = pci_add_capability(d, PCI_CAP_ID_PM, 0, PCI_PM_SIZEOF, errp);
55a35fe226SAleksandr Bezzubikov     if (pos < 0) {
56a35fe226SAleksandr Bezzubikov         goto pm_error;
57a35fe226SAleksandr Bezzubikov     }
58a35fe226SAleksandr Bezzubikov     d->exp.pm_cap = pos;
59a35fe226SAleksandr Bezzubikov     pci_set_word(d->config + pos + PCI_PM_PMC, 0x3);
60a35fe226SAleksandr Bezzubikov 
61a35fe226SAleksandr Bezzubikov     pcie_cap_arifwd_init(d);
62a35fe226SAleksandr Bezzubikov     pcie_cap_deverr_init(d);
63a35fe226SAleksandr Bezzubikov 
64a35fe226SAleksandr Bezzubikov     rc = pcie_aer_init(d, PCI_ERR_VER, 0x100, PCI_ERR_SIZEOF, errp);
65a35fe226SAleksandr Bezzubikov     if (rc < 0) {
66a35fe226SAleksandr Bezzubikov         goto aer_error;
67a35fe226SAleksandr Bezzubikov     }
68a35fe226SAleksandr Bezzubikov 
69d659d940SAleksandr Bezzubikov     Error *local_err = NULL;
70a35fe226SAleksandr Bezzubikov     if (pcie_br->msi != ON_OFF_AUTO_OFF) {
71d659d940SAleksandr Bezzubikov         rc = msi_init(d, 0, 1, true, true, &local_err);
72a35fe226SAleksandr Bezzubikov         if (rc < 0) {
73d659d940SAleksandr Bezzubikov             assert(rc == -ENOTSUP);
74d659d940SAleksandr Bezzubikov             if (pcie_br->msi != ON_OFF_AUTO_ON) {
75d659d940SAleksandr Bezzubikov                 error_free(local_err);
76d659d940SAleksandr Bezzubikov             } else {
77d659d940SAleksandr Bezzubikov                 /* failed to satisfy user's explicit request for MSI */
78d659d940SAleksandr Bezzubikov                 error_propagate(errp, local_err);
79a35fe226SAleksandr Bezzubikov                 goto msi_error;
80a35fe226SAleksandr Bezzubikov             }
81a35fe226SAleksandr Bezzubikov         }
82d659d940SAleksandr Bezzubikov     }
83a35fe226SAleksandr Bezzubikov     pci_register_bar(d, 0, PCI_BASE_ADDRESS_SPACE_MEMORY |
84a35fe226SAleksandr Bezzubikov                      PCI_BASE_ADDRESS_MEM_TYPE_64, &pcie_br->shpc_bar);
85a35fe226SAleksandr Bezzubikov     return;
86a35fe226SAleksandr Bezzubikov 
87a35fe226SAleksandr Bezzubikov msi_error:
88a35fe226SAleksandr Bezzubikov     pcie_aer_exit(d);
89a35fe226SAleksandr Bezzubikov aer_error:
90a35fe226SAleksandr Bezzubikov pm_error:
91a35fe226SAleksandr Bezzubikov     pcie_cap_exit(d);
92a35fe226SAleksandr Bezzubikov cap_error:
93d659d940SAleksandr Bezzubikov     shpc_cleanup(d, &pcie_br->shpc_bar);
94a35fe226SAleksandr Bezzubikov error:
95a35fe226SAleksandr Bezzubikov     pci_bridge_exitfn(d);
96a35fe226SAleksandr Bezzubikov }
97a35fe226SAleksandr Bezzubikov 
98a35fe226SAleksandr Bezzubikov static void pcie_pci_bridge_exit(PCIDevice *d)
99a35fe226SAleksandr Bezzubikov {
100a35fe226SAleksandr Bezzubikov     PCIEPCIBridge *bridge_dev = PCIE_PCI_BRIDGE_DEV(d);
101a35fe226SAleksandr Bezzubikov     pcie_cap_exit(d);
102a35fe226SAleksandr Bezzubikov     shpc_cleanup(d, &bridge_dev->shpc_bar);
103a35fe226SAleksandr Bezzubikov     pci_bridge_exitfn(d);
104a35fe226SAleksandr Bezzubikov }
105a35fe226SAleksandr Bezzubikov 
106a35fe226SAleksandr Bezzubikov static void pcie_pci_bridge_reset(DeviceState *qdev)
107a35fe226SAleksandr Bezzubikov {
108a35fe226SAleksandr Bezzubikov     PCIDevice *d = PCI_DEVICE(qdev);
109a35fe226SAleksandr Bezzubikov     pci_bridge_reset(qdev);
110d659d940SAleksandr Bezzubikov     if (msi_present(d)) {
111a35fe226SAleksandr Bezzubikov         msi_reset(d);
112d659d940SAleksandr Bezzubikov     }
113a35fe226SAleksandr Bezzubikov     shpc_reset(d);
114a35fe226SAleksandr Bezzubikov }
115a35fe226SAleksandr Bezzubikov 
116a35fe226SAleksandr Bezzubikov static void pcie_pci_bridge_write_config(PCIDevice *d,
117a35fe226SAleksandr Bezzubikov         uint32_t address, uint32_t val, int len)
118a35fe226SAleksandr Bezzubikov {
119a35fe226SAleksandr Bezzubikov     pci_bridge_write_config(d, address, val, len);
120d659d940SAleksandr Bezzubikov     if (msi_present(d)) {
121a35fe226SAleksandr Bezzubikov         msi_write_config(d, address, val, len);
122d659d940SAleksandr Bezzubikov     }
123a35fe226SAleksandr Bezzubikov     shpc_cap_write_config(d, address, val, len);
124a35fe226SAleksandr Bezzubikov }
125a35fe226SAleksandr Bezzubikov 
126a35fe226SAleksandr Bezzubikov static Property pcie_pci_bridge_dev_properties[] = {
127d659d940SAleksandr Bezzubikov         DEFINE_PROP_ON_OFF_AUTO("msi", PCIEPCIBridge, msi, ON_OFF_AUTO_AUTO),
128a35fe226SAleksandr Bezzubikov         DEFINE_PROP_END_OF_LIST(),
129a35fe226SAleksandr Bezzubikov };
130a35fe226SAleksandr Bezzubikov 
131a35fe226SAleksandr Bezzubikov static const VMStateDescription pcie_pci_bridge_dev_vmstate = {
132a35fe226SAleksandr Bezzubikov         .name = TYPE_PCIE_PCI_BRIDGE_DEV,
1339d6b9db1SPeter Xu         .priority = MIG_PRI_PCI_BUS,
134a35fe226SAleksandr Bezzubikov         .fields = (VMStateField[]) {
135a35fe226SAleksandr Bezzubikov             VMSTATE_PCI_DEVICE(parent_obj, PCIBridge),
136a35fe226SAleksandr Bezzubikov             SHPC_VMSTATE(shpc, PCIDevice, NULL),
137a35fe226SAleksandr Bezzubikov             VMSTATE_END_OF_LIST()
138a35fe226SAleksandr Bezzubikov         }
139a35fe226SAleksandr Bezzubikov };
140a35fe226SAleksandr Bezzubikov 
141a35fe226SAleksandr Bezzubikov static void pcie_pci_bridge_class_init(ObjectClass *klass, void *data)
142a35fe226SAleksandr Bezzubikov {
143a35fe226SAleksandr Bezzubikov     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
144a35fe226SAleksandr Bezzubikov     DeviceClass *dc = DEVICE_CLASS(klass);
145a35fe226SAleksandr Bezzubikov     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
146a35fe226SAleksandr Bezzubikov 
14791f4c995SDavid Gibson     k->is_bridge = true;
148a35fe226SAleksandr Bezzubikov     k->vendor_id = PCI_VENDOR_ID_REDHAT;
149a35fe226SAleksandr Bezzubikov     k->device_id = PCI_DEVICE_ID_REDHAT_PCIE_BRIDGE;
150a35fe226SAleksandr Bezzubikov     k->realize = pcie_pci_bridge_realize;
151a35fe226SAleksandr Bezzubikov     k->exit = pcie_pci_bridge_exit;
152a35fe226SAleksandr Bezzubikov     k->config_write = pcie_pci_bridge_write_config;
153a35fe226SAleksandr Bezzubikov     dc->vmsd = &pcie_pci_bridge_dev_vmstate;
154a35fe226SAleksandr Bezzubikov     dc->props = pcie_pci_bridge_dev_properties;
155a35fe226SAleksandr Bezzubikov     dc->reset = &pcie_pci_bridge_reset;
156a35fe226SAleksandr Bezzubikov     set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
15762b76563SDavid Hildenbrand     hc->plug = pci_bridge_dev_plug_cb;
1588f560cdcSDavid Hildenbrand     hc->unplug = pci_bridge_dev_unplug_cb;
15962b76563SDavid Hildenbrand     hc->unplug_request = pci_bridge_dev_unplug_request_cb;
160a35fe226SAleksandr Bezzubikov }
161a35fe226SAleksandr Bezzubikov 
162a35fe226SAleksandr Bezzubikov static const TypeInfo pcie_pci_bridge_info = {
163a35fe226SAleksandr Bezzubikov         .name = TYPE_PCIE_PCI_BRIDGE_DEV,
164a35fe226SAleksandr Bezzubikov         .parent = TYPE_PCI_BRIDGE,
165a35fe226SAleksandr Bezzubikov         .instance_size = sizeof(PCIEPCIBridge),
166a35fe226SAleksandr Bezzubikov         .class_init = pcie_pci_bridge_class_init,
167a35fe226SAleksandr Bezzubikov         .interfaces = (InterfaceInfo[]) {
168a35fe226SAleksandr Bezzubikov             { TYPE_HOTPLUG_HANDLER },
16971d78767SEduardo Habkost             { INTERFACE_PCIE_DEVICE },
170a35fe226SAleksandr Bezzubikov             { },
171a35fe226SAleksandr Bezzubikov         }
172a35fe226SAleksandr Bezzubikov };
173a35fe226SAleksandr Bezzubikov 
174a35fe226SAleksandr Bezzubikov static void pciepci_register(void)
175a35fe226SAleksandr Bezzubikov {
176a35fe226SAleksandr Bezzubikov     type_register_static(&pcie_pci_bridge_info);
177a35fe226SAleksandr Bezzubikov }
178a35fe226SAleksandr Bezzubikov 
179a35fe226SAleksandr Bezzubikov type_init(pciepci_register);
180