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 <linux/vfio.h>
23 #include "hw/ppc/spapr.h"
24 #include "hw/pci-host/spapr.h"
25 #include "hw/pci/msix.h"
26 #include "hw/vfio/vfio.h"
27 #include "qemu/error-report.h"
28 #include "sysemu/qtest.h"
30 bool spapr_phb_eeh_available(SpaprPhbState
*sphb
)
32 return vfio_eeh_as_ok(&sphb
->iommu_as
);
35 static void spapr_phb_vfio_eeh_reenable(SpaprPhbState
*sphb
)
37 vfio_eeh_as_op(&sphb
->iommu_as
, VFIO_EEH_PE_ENABLE
);
40 void spapr_phb_vfio_reset(DeviceState
*qdev
)
43 * The PE might be in frozen state. To reenable the EEH
44 * functionality on it will clean the frozen state, which
45 * ensures that the contained PCI devices will work properly
48 spapr_phb_vfio_eeh_reenable(SPAPR_PCI_HOST_BRIDGE(qdev
));
51 int spapr_phb_vfio_eeh_set_option(SpaprPhbState
*sphb
,
52 unsigned int addr
, int option
)
58 case RTAS_EEH_DISABLE
:
59 op
= VFIO_EEH_PE_DISABLE
;
61 case RTAS_EEH_ENABLE
: {
66 * The EEH functionality is enabled on basis of PCI device,
67 * instead of PE. We need check the validity of the PCI
70 phb
= PCI_HOST_BRIDGE(sphb
);
71 pdev
= pci_find_device(phb
->bus
,
72 (addr
>> 16) & 0xFF, (addr
>> 8) & 0xFF);
73 if (!pdev
|| !object_dynamic_cast(OBJECT(pdev
), "vfio-pci")) {
74 return RTAS_OUT_PARAM_ERROR
;
77 op
= VFIO_EEH_PE_ENABLE
;
80 case RTAS_EEH_THAW_IO
:
81 op
= VFIO_EEH_PE_UNFREEZE_IO
;
83 case RTAS_EEH_THAW_DMA
:
84 op
= VFIO_EEH_PE_UNFREEZE_DMA
;
87 return RTAS_OUT_PARAM_ERROR
;
90 ret
= vfio_eeh_as_op(&sphb
->iommu_as
, op
);
92 return RTAS_OUT_HW_ERROR
;
95 return RTAS_OUT_SUCCESS
;
98 int spapr_phb_vfio_eeh_get_state(SpaprPhbState
*sphb
, int *state
)
102 ret
= vfio_eeh_as_op(&sphb
->iommu_as
, VFIO_EEH_PE_GET_STATE
);
104 return RTAS_OUT_PARAM_ERROR
;
108 return RTAS_OUT_SUCCESS
;
111 static void spapr_phb_vfio_eeh_clear_dev_msix(PCIBus
*bus
,
115 /* Check if the device is VFIO PCI device */
116 if (!object_dynamic_cast(OBJECT(pdev
), "vfio-pci")) {
121 * The MSIx table will be cleaned out by reset. We need
122 * disable it so that it can be reenabled properly. Also,
123 * the cached MSIx table should be cleared as it's not
124 * reflecting the contents in hardware.
126 if (msix_enabled(pdev
)) {
129 flags
= pci_host_config_read_common(pdev
,
130 pdev
->msix_cap
+ PCI_MSIX_FLAGS
,
131 pci_config_size(pdev
), 2);
132 flags
&= ~PCI_MSIX_FLAGS_ENABLE
;
133 pci_host_config_write_common(pdev
,
134 pdev
->msix_cap
+ PCI_MSIX_FLAGS
,
135 pci_config_size(pdev
), flags
, 2);
141 static void spapr_phb_vfio_eeh_clear_bus_msix(PCIBus
*bus
, void *opaque
)
143 pci_for_each_device(bus
, pci_bus_num(bus
),
144 spapr_phb_vfio_eeh_clear_dev_msix
, NULL
);
147 static void spapr_phb_vfio_eeh_pre_reset(SpaprPhbState
*sphb
)
149 PCIHostState
*phb
= PCI_HOST_BRIDGE(sphb
);
151 pci_for_each_bus(phb
->bus
, spapr_phb_vfio_eeh_clear_bus_msix
, NULL
);
154 int spapr_phb_vfio_eeh_reset(SpaprPhbState
*sphb
, int option
)
160 case RTAS_SLOT_RESET_DEACTIVATE
:
161 op
= VFIO_EEH_PE_RESET_DEACTIVATE
;
163 case RTAS_SLOT_RESET_HOT
:
164 spapr_phb_vfio_eeh_pre_reset(sphb
);
165 op
= VFIO_EEH_PE_RESET_HOT
;
167 case RTAS_SLOT_RESET_FUNDAMENTAL
:
168 spapr_phb_vfio_eeh_pre_reset(sphb
);
169 op
= VFIO_EEH_PE_RESET_FUNDAMENTAL
;
172 return RTAS_OUT_PARAM_ERROR
;
175 ret
= vfio_eeh_as_op(&sphb
->iommu_as
, op
);
177 return RTAS_OUT_HW_ERROR
;
180 return RTAS_OUT_SUCCESS
;
183 int spapr_phb_vfio_eeh_configure(SpaprPhbState
*sphb
)
187 ret
= vfio_eeh_as_op(&sphb
->iommu_as
, VFIO_EEH_PE_CONFIGURE
);
189 return RTAS_OUT_PARAM_ERROR
;
192 return RTAS_OUT_SUCCESS
;