2 * Base class for PCI Express Root Ports
4 * Copyright (C) 2017 Red Hat Inc
7 * Marcel Apfelbaum <marcel@redhat.com>
9 * Most of the code was migrated from hw/pci-bridge/ioh3420.
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
15 #include "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include "hw/pci/pcie_port.h"
19 static void rp_aer_vector_update(PCIDevice
*d
)
21 PCIERootPortClass
*rpc
= PCIE_ROOT_PORT_GET_CLASS(d
);
23 if (rpc
->aer_vector
) {
24 pcie_aer_root_set_vector(d
, rpc
->aer_vector(d
));
28 static void rp_write_config(PCIDevice
*d
, uint32_t address
,
29 uint32_t val
, int len
)
32 pci_get_long(d
->config
+ d
->exp
.aer_cap
+ PCI_ERR_ROOT_COMMAND
);
34 pci_bridge_write_config(d
, address
, val
, len
);
35 rp_aer_vector_update(d
);
36 pcie_cap_slot_write_config(d
, address
, val
, len
);
37 pcie_aer_write_config(d
, address
, val
, len
);
38 pcie_aer_root_write_config(d
, address
, val
, len
, root_cmd
);
41 static void rp_reset(DeviceState
*qdev
)
43 PCIDevice
*d
= PCI_DEVICE(qdev
);
45 rp_aer_vector_update(d
);
46 pcie_cap_root_reset(d
);
47 pcie_cap_deverr_reset(d
);
48 pcie_cap_slot_reset(d
);
49 pcie_cap_arifwd_reset(d
);
50 pcie_aer_root_reset(d
);
51 pci_bridge_reset(qdev
);
52 pci_bridge_disable_base_limit(d
);
55 static void rp_realize(PCIDevice
*d
, Error
**errp
)
57 PCIEPort
*p
= PCIE_PORT(d
);
58 PCIESlot
*s
= PCIE_SLOT(d
);
59 PCIDeviceClass
*dc
= PCI_DEVICE_GET_CLASS(d
);
60 PCIERootPortClass
*rpc
= PCIE_ROOT_PORT_GET_CLASS(d
);
63 pci_config_set_interrupt_pin(d
->config
, 1);
64 pci_bridge_initfn(d
, TYPE_PCIE_BUS
);
65 pcie_port_init_reg(d
);
67 rc
= pci_bridge_ssvid_init(d
, rpc
->ssvid_offset
, dc
->vendor_id
,
70 error_append_hint(errp
, "Can't init SSV ID, error %d\n", rc
);
74 if (rpc
->interrupts_init
) {
75 rc
= rpc
->interrupts_init(d
, errp
);
81 rc
= pcie_cap_init(d
, rpc
->exp_offset
, PCI_EXP_TYPE_ROOT_PORT
,
84 error_append_hint(errp
, "Can't add Root Port capability, "
89 pcie_cap_arifwd_init(d
);
90 pcie_cap_deverr_init(d
);
91 pcie_cap_slot_init(d
, s
->slot
);
92 pcie_cap_root_init(d
);
94 pcie_chassis_create(s
->chassis
);
95 rc
= pcie_chassis_add_slot(s
);
97 error_setg(errp
, "Can't add chassis slot, error %d", rc
);
101 rc
= pcie_aer_init(d
, PCI_ERR_VER
, rpc
->aer_offset
,
102 PCI_ERR_SIZEOF
, errp
);
106 pcie_aer_root_init(d
);
107 rp_aer_vector_update(d
);
112 pcie_chassis_del_slot(s
);
116 if (rpc
->interrupts_uninit
) {
117 rpc
->interrupts_uninit(d
);
120 pci_bridge_exitfn(d
);
123 static void rp_exit(PCIDevice
*d
)
125 PCIERootPortClass
*rpc
= PCIE_ROOT_PORT_GET_CLASS(d
);
126 PCIESlot
*s
= PCIE_SLOT(d
);
129 pcie_chassis_del_slot(s
);
131 if (rpc
->interrupts_uninit
) {
132 rpc
->interrupts_uninit(d
);
134 pci_bridge_exitfn(d
);
137 static Property rp_props
[] = {
138 DEFINE_PROP_BIT(COMPAT_PROP_PCP
, PCIDevice
, cap_present
,
139 QEMU_PCIE_SLTCAP_PCP_BITNR
, true),
140 DEFINE_PROP_END_OF_LIST()
143 static void rp_class_init(ObjectClass
*klass
, void *data
)
145 DeviceClass
*dc
= DEVICE_CLASS(klass
);
146 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
150 k
->config_write
= rp_write_config
;
151 k
->realize
= rp_realize
;
153 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
154 dc
->reset
= rp_reset
;
155 dc
->props
= rp_props
;
158 static const TypeInfo rp_info
= {
159 .name
= TYPE_PCIE_ROOT_PORT
,
160 .parent
= TYPE_PCIE_SLOT
,
161 .class_init
= rp_class_init
,
163 .class_size
= sizeof(PCIERootPortClass
),
164 .interfaces
= (InterfaceInfo
[]) {
165 { INTERFACE_PCIE_DEVICE
},
170 static void rp_register_types(void)
172 type_register_static(&rp_info
);
175 type_init(rp_register_types
)