]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ppc/spapr_pci_vfio.c
include/qemu/osdep.h: Don't include qapi/error.h
[mirror_qemu.git] / hw / ppc / spapr_pci_vfio.c
CommitLineData
9fc34ada
AK
1/*
2 * QEMU sPAPR PCI host for VFIO
3 *
4 * Copyright (c) 2011-2014 Alexey Kardashevskiy, IBM Corporation.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
0d75590d 20#include "qemu/osdep.h"
da34e65c 21#include "qapi/error.h"
9fc34ada
AK
22#include "hw/ppc/spapr.h"
23#include "hw/pci-host/spapr.h"
6319b1da 24#include "hw/pci/msix.h"
9fc34ada 25#include "linux/vfio.h"
cf7087db 26#include "hw/vfio/vfio.h"
72700d7e 27#include "qemu/error-report.h"
9fc34ada 28
72700d7e 29#define TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE "spapr-pci-vfio-host-bridge"
9fc34ada 30
72700d7e
DG
31#define SPAPR_PCI_VFIO_HOST_BRIDGE(obj) \
32 OBJECT_CHECK(sPAPRPHBVFIOState, (obj), TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE)
9fc34ada 33
72700d7e 34typedef struct sPAPRPHBVFIOState sPAPRPHBVFIOState;
9fc34ada 35
72700d7e
DG
36struct sPAPRPHBVFIOState {
37 sPAPRPHBState phb;
9fc34ada 38
72700d7e
DG
39 int32_t iommugroupid;
40};
9fc34ada 41
72700d7e
DG
42static Property spapr_phb_vfio_properties[] = {
43 DEFINE_PROP_INT32("iommu", sPAPRPHBVFIOState, iommugroupid, -1),
44 DEFINE_PROP_END_OF_LIST(),
45};
9fc34ada 46
72700d7e
DG
47static void spapr_phb_vfio_instance_init(Object *obj)
48{
49 error_report("spapr-pci-vfio-host-bridge is deprecated");
9fc34ada
AK
50}
51
c1fa017c
DG
52bool spapr_phb_eeh_available(sPAPRPHBState *sphb)
53{
54 return vfio_eeh_as_ok(&sphb->iommu_as);
55}
56
76a9e9f6 57static void spapr_phb_vfio_eeh_reenable(sPAPRPHBState *sphb)
aef87d1b 58{
76a9e9f6 59 vfio_eeh_as_op(&sphb->iommu_as, VFIO_EEH_PE_ENABLE);
aef87d1b
GS
60}
61
fbb4e983 62void spapr_phb_vfio_reset(DeviceState *qdev)
9fc34ada 63{
aef87d1b
GS
64 /*
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
68 * after reboot.
69 */
76a9e9f6 70 spapr_phb_vfio_eeh_reenable(SPAPR_PCI_HOST_BRIDGE(qdev));
9fc34ada
AK
71}
72
fbb4e983
DG
73int spapr_phb_vfio_eeh_set_option(sPAPRPHBState *sphb,
74 unsigned int addr, int option)
2aad88f4 75{
76a9e9f6 76 uint32_t op;
2aad88f4
GS
77 int ret;
78
79 switch (option) {
80 case RTAS_EEH_DISABLE:
76a9e9f6 81 op = VFIO_EEH_PE_DISABLE;
2aad88f4
GS
82 break;
83 case RTAS_EEH_ENABLE: {
84 PCIHostState *phb;
85 PCIDevice *pdev;
86
87 /*
88 * The EEH functionality is enabled on basis of PCI device,
89 * instead of PE. We need check the validity of the PCI
90 * device address.
91 */
92 phb = PCI_HOST_BRIDGE(sphb);
93 pdev = pci_find_device(phb->bus,
94 (addr >> 16) & 0xFF, (addr >> 8) & 0xFF);
d76548a9 95 if (!pdev || !object_dynamic_cast(OBJECT(pdev), "vfio-pci")) {
2aad88f4
GS
96 return RTAS_OUT_PARAM_ERROR;
97 }
98
76a9e9f6 99 op = VFIO_EEH_PE_ENABLE;
2aad88f4
GS
100 break;
101 }
102 case RTAS_EEH_THAW_IO:
76a9e9f6 103 op = VFIO_EEH_PE_UNFREEZE_IO;
2aad88f4
GS
104 break;
105 case RTAS_EEH_THAW_DMA:
76a9e9f6 106 op = VFIO_EEH_PE_UNFREEZE_DMA;
2aad88f4
GS
107 break;
108 default:
109 return RTAS_OUT_PARAM_ERROR;
110 }
111
76a9e9f6 112 ret = vfio_eeh_as_op(&sphb->iommu_as, op);
2aad88f4
GS
113 if (ret < 0) {
114 return RTAS_OUT_HW_ERROR;
115 }
116
117 return RTAS_OUT_SUCCESS;
118}
119
fbb4e983 120int spapr_phb_vfio_eeh_get_state(sPAPRPHBState *sphb, int *state)
2aad88f4 121{
2aad88f4
GS
122 int ret;
123
76a9e9f6 124 ret = vfio_eeh_as_op(&sphb->iommu_as, VFIO_EEH_PE_GET_STATE);
2aad88f4
GS
125 if (ret < 0) {
126 return RTAS_OUT_PARAM_ERROR;
127 }
128
129 *state = ret;
130 return RTAS_OUT_SUCCESS;
131}
132
6319b1da
GS
133static void spapr_phb_vfio_eeh_clear_dev_msix(PCIBus *bus,
134 PCIDevice *pdev,
135 void *opaque)
136{
137 /* Check if the device is VFIO PCI device */
138 if (!object_dynamic_cast(OBJECT(pdev), "vfio-pci")) {
139 return;
140 }
141
142 /*
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.
147 */
148 if (msix_enabled(pdev)) {
149 uint16_t flags;
150
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);
158 }
159
160 msix_reset(pdev);
161}
162
163static void spapr_phb_vfio_eeh_clear_bus_msix(PCIBus *bus, void *opaque)
164{
165 pci_for_each_device(bus, pci_bus_num(bus),
166 spapr_phb_vfio_eeh_clear_dev_msix, NULL);
167}
168
169static void spapr_phb_vfio_eeh_pre_reset(sPAPRPHBState *sphb)
170{
171 PCIHostState *phb = PCI_HOST_BRIDGE(sphb);
172
173 pci_for_each_bus(phb->bus, spapr_phb_vfio_eeh_clear_bus_msix, NULL);
174}
175
fbb4e983 176int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option)
2aad88f4 177{
76a9e9f6 178 uint32_t op;
2aad88f4
GS
179 int ret;
180
181 switch (option) {
182 case RTAS_SLOT_RESET_DEACTIVATE:
76a9e9f6 183 op = VFIO_EEH_PE_RESET_DEACTIVATE;
2aad88f4
GS
184 break;
185 case RTAS_SLOT_RESET_HOT:
6319b1da 186 spapr_phb_vfio_eeh_pre_reset(sphb);
76a9e9f6 187 op = VFIO_EEH_PE_RESET_HOT;
2aad88f4
GS
188 break;
189 case RTAS_SLOT_RESET_FUNDAMENTAL:
6319b1da 190 spapr_phb_vfio_eeh_pre_reset(sphb);
76a9e9f6 191 op = VFIO_EEH_PE_RESET_FUNDAMENTAL;
2aad88f4
GS
192 break;
193 default:
194 return RTAS_OUT_PARAM_ERROR;
195 }
196
76a9e9f6 197 ret = vfio_eeh_as_op(&sphb->iommu_as, op);
2aad88f4
GS
198 if (ret < 0) {
199 return RTAS_OUT_HW_ERROR;
200 }
201
202 return RTAS_OUT_SUCCESS;
203}
204
fbb4e983 205int spapr_phb_vfio_eeh_configure(sPAPRPHBState *sphb)
2aad88f4 206{
2aad88f4
GS
207 int ret;
208
76a9e9f6 209 ret = vfio_eeh_as_op(&sphb->iommu_as, VFIO_EEH_PE_CONFIGURE);
2aad88f4
GS
210 if (ret < 0) {
211 return RTAS_OUT_PARAM_ERROR;
212 }
213
214 return RTAS_OUT_SUCCESS;
215}
216
9fc34ada
AK
217static void spapr_phb_vfio_class_init(ObjectClass *klass, void *data)
218{
219 DeviceClass *dc = DEVICE_CLASS(klass);
9fc34ada
AK
220
221 dc->props = spapr_phb_vfio_properties;
9fc34ada
AK
222}
223
224static 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),
72700d7e 228 .instance_init = spapr_phb_vfio_instance_init,
9fc34ada 229 .class_init = spapr_phb_vfio_class_init,
9fc34ada
AK
230};
231
232static void spapr_pci_vfio_register_types(void)
233{
234 type_register_static(&spapr_phb_vfio_info);
235}
236
237type_init(spapr_pci_vfio_register_types)