1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include "qemu/osdep.h"
21#include "qapi/error.h"
22#include "qemu-common.h"
23#include "cpu.h"
24#include "hw/ppc/spapr.h"
25#include "hw/pci-host/spapr.h"
26#include "hw/pci/msix.h"
27#include "linux/vfio.h"
28#include "hw/vfio/vfio.h"
29#include "qemu/error-report.h"
30
31#define TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE "spapr-pci-vfio-host-bridge"
32
33#define SPAPR_PCI_VFIO_HOST_BRIDGE(obj) \
34 OBJECT_CHECK(sPAPRPHBVFIOState, (obj), TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE)
35
36typedef struct sPAPRPHBVFIOState sPAPRPHBVFIOState;
37
38struct sPAPRPHBVFIOState {
39 sPAPRPHBState phb;
40
41 int32_t iommugroupid;
42};
43
44static Property spapr_phb_vfio_properties[] = {
45 DEFINE_PROP_INT32("iommu", sPAPRPHBVFIOState, iommugroupid, -1),
46 DEFINE_PROP_END_OF_LIST(),
47};
48
49static void spapr_phb_vfio_instance_init(Object *obj)
50{
51 error_report("spapr-pci-vfio-host-bridge is deprecated");
52}
53
54bool spapr_phb_eeh_available(sPAPRPHBState *sphb)
55{
56 return vfio_eeh_as_ok(&sphb->iommu_as);
57}
58
59static void spapr_phb_vfio_eeh_reenable(sPAPRPHBState *sphb)
60{
61 vfio_eeh_as_op(&sphb->iommu_as, VFIO_EEH_PE_ENABLE);
62}
63
64void spapr_phb_vfio_reset(DeviceState *qdev)
65{
66
67
68
69
70
71
72 spapr_phb_vfio_eeh_reenable(SPAPR_PCI_HOST_BRIDGE(qdev));
73}
74
75int spapr_phb_vfio_eeh_set_option(sPAPRPHBState *sphb,
76 unsigned int addr, int option)
77{
78 uint32_t op;
79 int ret;
80
81 switch (option) {
82 case RTAS_EEH_DISABLE:
83 op = VFIO_EEH_PE_DISABLE;
84 break;
85 case RTAS_EEH_ENABLE: {
86 PCIHostState *phb;
87 PCIDevice *pdev;
88
89
90
91
92
93
94 phb = PCI_HOST_BRIDGE(sphb);
95 pdev = pci_find_device(phb->bus,
96 (addr >> 16) & 0xFF, (addr >> 8) & 0xFF);
97 if (!pdev || !object_dynamic_cast(OBJECT(pdev), "vfio-pci")) {
98 return RTAS_OUT_PARAM_ERROR;
99 }
100
101 op = VFIO_EEH_PE_ENABLE;
102 break;
103 }
104 case RTAS_EEH_THAW_IO:
105 op = VFIO_EEH_PE_UNFREEZE_IO;
106 break;
107 case RTAS_EEH_THAW_DMA:
108 op = VFIO_EEH_PE_UNFREEZE_DMA;
109 break;
110 default:
111 return RTAS_OUT_PARAM_ERROR;
112 }
113
114 ret = vfio_eeh_as_op(&sphb->iommu_as, op);
115 if (ret < 0) {
116 return RTAS_OUT_HW_ERROR;
117 }
118
119 return RTAS_OUT_SUCCESS;
120}
121
122int spapr_phb_vfio_eeh_get_state(sPAPRPHBState *sphb, int *state)
123{
124 int ret;
125
126 ret = vfio_eeh_as_op(&sphb->iommu_as, VFIO_EEH_PE_GET_STATE);
127 if (ret < 0) {
128 return RTAS_OUT_PARAM_ERROR;
129 }
130
131 *state = ret;
132 return RTAS_OUT_SUCCESS;
133}
134
135static void spapr_phb_vfio_eeh_clear_dev_msix(PCIBus *bus,
136 PCIDevice *pdev,
137 void *opaque)
138{
139
140 if (!object_dynamic_cast(OBJECT(pdev), "vfio-pci")) {
141 return;
142 }
143
144
145
146
147
148
149
150 if (msix_enabled(pdev)) {
151 uint16_t flags;
152
153 flags = pci_host_config_read_common(pdev,
154 pdev->msix_cap + PCI_MSIX_FLAGS,
155 pci_config_size(pdev), 2);
156 flags &= ~PCI_MSIX_FLAGS_ENABLE;
157 pci_host_config_write_common(pdev,
158 pdev->msix_cap + PCI_MSIX_FLAGS,
159 pci_config_size(pdev), flags, 2);
160 }
161
162 msix_reset(pdev);
163}
164
165static void spapr_phb_vfio_eeh_clear_bus_msix(PCIBus *bus, void *opaque)
166{
167 pci_for_each_device(bus, pci_bus_num(bus),
168 spapr_phb_vfio_eeh_clear_dev_msix, NULL);
169}
170
171static void spapr_phb_vfio_eeh_pre_reset(sPAPRPHBState *sphb)
172{
173 PCIHostState *phb = PCI_HOST_BRIDGE(sphb);
174
175 pci_for_each_bus(phb->bus, spapr_phb_vfio_eeh_clear_bus_msix, NULL);
176}
177
178int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option)
179{
180 uint32_t op;
181 int ret;
182
183 switch (option) {
184 case RTAS_SLOT_RESET_DEACTIVATE:
185 op = VFIO_EEH_PE_RESET_DEACTIVATE;
186 break;
187 case RTAS_SLOT_RESET_HOT:
188 spapr_phb_vfio_eeh_pre_reset(sphb);
189 op = VFIO_EEH_PE_RESET_HOT;
190 break;
191 case RTAS_SLOT_RESET_FUNDAMENTAL:
192 spapr_phb_vfio_eeh_pre_reset(sphb);
193 op = VFIO_EEH_PE_RESET_FUNDAMENTAL;
194 break;
195 default:
196 return RTAS_OUT_PARAM_ERROR;
197 }
198
199 ret = vfio_eeh_as_op(&sphb->iommu_as, op);
200 if (ret < 0) {
201 return RTAS_OUT_HW_ERROR;
202 }
203
204 return RTAS_OUT_SUCCESS;
205}
206
207int spapr_phb_vfio_eeh_configure(sPAPRPHBState *sphb)
208{
209 int ret;
210
211 ret = vfio_eeh_as_op(&sphb->iommu_as, VFIO_EEH_PE_CONFIGURE);
212 if (ret < 0) {
213 return RTAS_OUT_PARAM_ERROR;
214 }
215
216 return RTAS_OUT_SUCCESS;
217}
218
219static void spapr_phb_vfio_class_init(ObjectClass *klass, void *data)
220{
221 DeviceClass *dc = DEVICE_CLASS(klass);
222
223 dc->props = spapr_phb_vfio_properties;
224}
225
226static const TypeInfo spapr_phb_vfio_info = {
227 .name = TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE,
228 .parent = TYPE_SPAPR_PCI_HOST_BRIDGE,
229 .instance_size = sizeof(sPAPRPHBVFIOState),
230 .instance_init = spapr_phb_vfio_instance_init,
231 .class_init = spapr_phb_vfio_class_init,
232};
233
234static void spapr_pci_vfio_register_types(void)
235{
236 type_register_static(&spapr_phb_vfio_info);
237}
238
239type_init(spapr_pci_vfio_register_types)
240