2 * Generic PCI Express Root Port emulation
4 * Copyright (C) 2017 Red Hat Inc
7 * Marcel Apfelbaum <marcel@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "hw/pci/msix.h"
16 #include "hw/pci/pcie_port.h"
18 #define TYPE_GEN_PCIE_ROOT_PORT "pcie-root-port"
19 #define GEN_PCIE_ROOT_PORT(obj) \
20 OBJECT_CHECK(GenPCIERootPort, (obj), TYPE_GEN_PCIE_ROOT_PORT)
22 #define GEN_PCIE_ROOT_PORT_AER_OFFSET 0x100
23 #define GEN_PCIE_ROOT_PORT_MSIX_NR_VECTOR 1
25 typedef struct GenPCIERootPort
{
32 /* additional resources to reserve */
33 PCIResReserve res_reserve
;
36 static uint8_t gen_rp_aer_vector(const PCIDevice
*d
)
41 static int gen_rp_interrupts_init(PCIDevice
*d
, Error
**errp
)
45 rc
= msix_init_exclusive_bar(d
, GEN_PCIE_ROOT_PORT_MSIX_NR_VECTOR
, 0, errp
);
48 assert(rc
== -ENOTSUP
);
50 msix_vector_use(d
, 0);
56 static void gen_rp_interrupts_uninit(PCIDevice
*d
)
58 msix_uninit_exclusive_bar(d
);
61 static bool gen_rp_test_migrate_msix(void *opaque
, int version_id
)
63 GenPCIERootPort
*rp
= opaque
;
65 return rp
->migrate_msix
;
68 static void gen_rp_realize(DeviceState
*dev
, Error
**errp
)
70 PCIDevice
*d
= PCI_DEVICE(dev
);
71 GenPCIERootPort
*grp
= GEN_PCIE_ROOT_PORT(d
);
72 PCIERootPortClass
*rpc
= PCIE_ROOT_PORT_GET_CLASS(d
);
73 Error
*local_err
= NULL
;
75 rpc
->parent_realize(dev
, &local_err
);
77 error_propagate(errp
, local_err
);
81 int rc
= pci_bridge_qemu_reserve_cap_init(d
, 0,
82 grp
->res_reserve
, errp
);
85 rpc
->parent_class
.exit(d
);
89 if (!grp
->res_reserve
.io
) {
90 pci_word_test_and_clear_mask(d
->wmask
+ PCI_COMMAND
,
92 d
->wmask
[PCI_IO_BASE
] = 0;
93 d
->wmask
[PCI_IO_LIMIT
] = 0;
97 static const VMStateDescription vmstate_rp_dev
= {
98 .name
= "pcie-root-port",
99 .priority
= MIG_PRI_PCI_BUS
,
101 .minimum_version_id
= 1,
102 .post_load
= pcie_cap_slot_post_load
,
103 .fields
= (VMStateField
[]) {
104 VMSTATE_PCI_DEVICE(parent_obj
.parent_obj
.parent_obj
, PCIESlot
),
105 VMSTATE_STRUCT(parent_obj
.parent_obj
.parent_obj
.exp
.aer_log
,
106 PCIESlot
, 0, vmstate_pcie_aer_log
, PCIEAERLog
),
107 VMSTATE_MSIX_TEST(parent_obj
.parent_obj
.parent_obj
.parent_obj
,
109 gen_rp_test_migrate_msix
),
110 VMSTATE_END_OF_LIST()
114 static Property gen_rp_props
[] = {
115 DEFINE_PROP_BOOL("x-migrate-msix", GenPCIERootPort
,
117 DEFINE_PROP_UINT32("bus-reserve", GenPCIERootPort
,
118 res_reserve
.bus
, -1),
119 DEFINE_PROP_SIZE("io-reserve", GenPCIERootPort
,
121 DEFINE_PROP_SIZE("mem-reserve", GenPCIERootPort
,
122 res_reserve
.mem_non_pref
, -1),
123 DEFINE_PROP_SIZE("pref32-reserve", GenPCIERootPort
,
124 res_reserve
.mem_pref_32
, -1),
125 DEFINE_PROP_SIZE("pref64-reserve", GenPCIERootPort
,
126 res_reserve
.mem_pref_64
, -1),
127 DEFINE_PROP_PCIE_LINK_SPEED("x-speed", PCIESlot
,
128 speed
, PCIE_LINK_SPEED_16
),
129 DEFINE_PROP_PCIE_LINK_WIDTH("x-width", PCIESlot
,
130 width
, PCIE_LINK_WIDTH_32
),
131 DEFINE_PROP_END_OF_LIST()
134 static void gen_rp_dev_class_init(ObjectClass
*klass
, void *data
)
136 DeviceClass
*dc
= DEVICE_CLASS(klass
);
137 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
138 PCIERootPortClass
*rpc
= PCIE_ROOT_PORT_CLASS(klass
);
140 k
->vendor_id
= PCI_VENDOR_ID_REDHAT
;
141 k
->device_id
= PCI_DEVICE_ID_REDHAT_PCIE_RP
;
142 dc
->desc
= "PCI Express Root Port";
143 dc
->vmsd
= &vmstate_rp_dev
;
144 dc
->props
= gen_rp_props
;
146 device_class_set_parent_realize(dc
, gen_rp_realize
, &rpc
->parent_realize
);
148 rpc
->aer_vector
= gen_rp_aer_vector
;
149 rpc
->interrupts_init
= gen_rp_interrupts_init
;
150 rpc
->interrupts_uninit
= gen_rp_interrupts_uninit
;
151 rpc
->aer_offset
= GEN_PCIE_ROOT_PORT_AER_OFFSET
;
154 static const TypeInfo gen_rp_dev_info
= {
155 .name
= TYPE_GEN_PCIE_ROOT_PORT
,
156 .parent
= TYPE_PCIE_ROOT_PORT
,
157 .instance_size
= sizeof(GenPCIERootPort
),
158 .class_init
= gen_rp_dev_class_init
,
161 static void gen_rp_register_types(void)
163 type_register_static(&gen_rp_dev_info
);
165 type_init(gen_rp_register_types
)