2 * QEMU sPAPR PCI host for VFIO
4 * Copyright (c) 2011-2014 Alexey Kardashevskiy, IBM Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License,
9 * or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "qapi/error.h"
22 #include "hw/ppc/spapr.h"
23 #include "hw/pci-host/spapr.h"
24 #include "hw/pci/msix.h"
25 #include "linux/vfio.h"
26 #include "hw/vfio/vfio.h"
27 #include "qemu/error-report.h"
29 #define TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE "spapr-pci-vfio-host-bridge"
31 #define SPAPR_PCI_VFIO_HOST_BRIDGE(obj) \
32 OBJECT_CHECK(sPAPRPHBVFIOState, (obj), TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE)
34 typedef struct sPAPRPHBVFIOState sPAPRPHBVFIOState
;
36 struct sPAPRPHBVFIOState
{
42 static Property spapr_phb_vfio_properties
[] = {
43 DEFINE_PROP_INT32("iommu", sPAPRPHBVFIOState
, iommugroupid
, -1),
44 DEFINE_PROP_END_OF_LIST(),
47 static void spapr_phb_vfio_instance_init(Object
*obj
)
49 error_report("spapr-pci-vfio-host-bridge is deprecated");
52 bool spapr_phb_eeh_available(sPAPRPHBState
*sphb
)
54 return vfio_eeh_as_ok(&sphb
->iommu_as
);
57 static void spapr_phb_vfio_eeh_reenable(sPAPRPHBState
*sphb
)
59 vfio_eeh_as_op(&sphb
->iommu_as
, VFIO_EEH_PE_ENABLE
);
62 void spapr_phb_vfio_reset(DeviceState
*qdev
)
65 * The PE might be in frozen state. To reenable the EEH
66 * functionality on it will clean the frozen state, which
67 * ensures that the contained PCI devices will work properly
70 spapr_phb_vfio_eeh_reenable(SPAPR_PCI_HOST_BRIDGE(qdev
));
73 int spapr_phb_vfio_eeh_set_option(sPAPRPHBState
*sphb
,
74 unsigned int addr
, int option
)
80 case RTAS_EEH_DISABLE
:
81 op
= VFIO_EEH_PE_DISABLE
;
83 case RTAS_EEH_ENABLE
: {
88 * The EEH functionality is enabled on basis of PCI device,
89 * instead of PE. We need check the validity of the PCI
92 phb
= PCI_HOST_BRIDGE(sphb
);
93 pdev
= pci_find_device(phb
->bus
,
94 (addr
>> 16) & 0xFF, (addr
>> 8) & 0xFF);
95 if (!pdev
|| !object_dynamic_cast(OBJECT(pdev
), "vfio-pci")) {
96 return RTAS_OUT_PARAM_ERROR
;
99 op
= VFIO_EEH_PE_ENABLE
;
102 case RTAS_EEH_THAW_IO
:
103 op
= VFIO_EEH_PE_UNFREEZE_IO
;
105 case RTAS_EEH_THAW_DMA
:
106 op
= VFIO_EEH_PE_UNFREEZE_DMA
;
109 return RTAS_OUT_PARAM_ERROR
;
112 ret
= vfio_eeh_as_op(&sphb
->iommu_as
, op
);
114 return RTAS_OUT_HW_ERROR
;
117 return RTAS_OUT_SUCCESS
;
120 int spapr_phb_vfio_eeh_get_state(sPAPRPHBState
*sphb
, int *state
)
124 ret
= vfio_eeh_as_op(&sphb
->iommu_as
, VFIO_EEH_PE_GET_STATE
);
126 return RTAS_OUT_PARAM_ERROR
;
130 return RTAS_OUT_SUCCESS
;
133 static void spapr_phb_vfio_eeh_clear_dev_msix(PCIBus
*bus
,
137 /* Check if the device is VFIO PCI device */
138 if (!object_dynamic_cast(OBJECT(pdev
), "vfio-pci")) {
143 * The MSIx table will be cleaned out by reset. We need
144 * disable it so that it can be reenabled properly. Also,
145 * the cached MSIx table should be cleared as it's not
146 * reflecting the contents in hardware.
148 if (msix_enabled(pdev
)) {
151 flags
= pci_host_config_read_common(pdev
,
152 pdev
->msix_cap
+ PCI_MSIX_FLAGS
,
153 pci_config_size(pdev
), 2);
154 flags
&= ~PCI_MSIX_FLAGS_ENABLE
;
155 pci_host_config_write_common(pdev
,
156 pdev
->msix_cap
+ PCI_MSIX_FLAGS
,
157 pci_config_size(pdev
), flags
, 2);
163 static void spapr_phb_vfio_eeh_clear_bus_msix(PCIBus
*bus
, void *opaque
)
165 pci_for_each_device(bus
, pci_bus_num(bus
),
166 spapr_phb_vfio_eeh_clear_dev_msix
, NULL
);
169 static void spapr_phb_vfio_eeh_pre_reset(sPAPRPHBState
*sphb
)
171 PCIHostState
*phb
= PCI_HOST_BRIDGE(sphb
);
173 pci_for_each_bus(phb
->bus
, spapr_phb_vfio_eeh_clear_bus_msix
, NULL
);
176 int spapr_phb_vfio_eeh_reset(sPAPRPHBState
*sphb
, int option
)
182 case RTAS_SLOT_RESET_DEACTIVATE
:
183 op
= VFIO_EEH_PE_RESET_DEACTIVATE
;
185 case RTAS_SLOT_RESET_HOT
:
186 spapr_phb_vfio_eeh_pre_reset(sphb
);
187 op
= VFIO_EEH_PE_RESET_HOT
;
189 case RTAS_SLOT_RESET_FUNDAMENTAL
:
190 spapr_phb_vfio_eeh_pre_reset(sphb
);
191 op
= VFIO_EEH_PE_RESET_FUNDAMENTAL
;
194 return RTAS_OUT_PARAM_ERROR
;
197 ret
= vfio_eeh_as_op(&sphb
->iommu_as
, op
);
199 return RTAS_OUT_HW_ERROR
;
202 return RTAS_OUT_SUCCESS
;
205 int spapr_phb_vfio_eeh_configure(sPAPRPHBState
*sphb
)
209 ret
= vfio_eeh_as_op(&sphb
->iommu_as
, VFIO_EEH_PE_CONFIGURE
);
211 return RTAS_OUT_PARAM_ERROR
;
214 return RTAS_OUT_SUCCESS
;
217 static void spapr_phb_vfio_class_init(ObjectClass
*klass
, void *data
)
219 DeviceClass
*dc
= DEVICE_CLASS(klass
);
221 dc
->props
= spapr_phb_vfio_properties
;
224 static const TypeInfo spapr_phb_vfio_info
= {
225 .name
= TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE
,
226 .parent
= TYPE_SPAPR_PCI_HOST_BRIDGE
,
227 .instance_size
= sizeof(sPAPRPHBVFIOState
),
228 .instance_init
= spapr_phb_vfio_instance_init
,
229 .class_init
= spapr_phb_vfio_class_init
,
232 static void spapr_pci_vfio_register_types(void)
234 type_register_static(&spapr_phb_vfio_info
);
237 type_init(spapr_pci_vfio_register_types
)