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 "hw/ppc/spapr.h"
21 #include "hw/pci-host/spapr.h"
22 #include "linux/vfio.h"
23 #include "hw/vfio/vfio.h"
25 static Property spapr_phb_vfio_properties
[] = {
26 DEFINE_PROP_INT32("iommu", sPAPRPHBVFIOState
, iommugroupid
, -1),
27 DEFINE_PROP_END_OF_LIST(),
30 static void spapr_phb_vfio_finish_realize(sPAPRPHBState
*sphb
, Error
**errp
)
32 sPAPRPHBVFIOState
*svphb
= SPAPR_PCI_VFIO_HOST_BRIDGE(sphb
);
33 struct vfio_iommu_spapr_tce_info info
= { .argsz
= sizeof(info
) };
36 uint32_t liobn
= svphb
->phb
.dma_liobn
;
38 if (svphb
->iommugroupid
== -1) {
39 error_setg(errp
, "Wrong IOMMU group ID %d", svphb
->iommugroupid
);
43 ret
= vfio_container_ioctl(&svphb
->phb
.iommu_as
, svphb
->iommugroupid
,
45 (void *) VFIO_SPAPR_TCE_IOMMU
);
47 error_setg_errno(errp
, -ret
,
48 "spapr-vfio: SPAPR extension is not supported");
52 ret
= vfio_container_ioctl(&svphb
->phb
.iommu_as
, svphb
->iommugroupid
,
53 VFIO_IOMMU_SPAPR_TCE_GET_INFO
, &info
);
55 error_setg_errno(errp
, -ret
,
56 "spapr-vfio: get info from container failed");
60 tcet
= spapr_tce_new_table(DEVICE(sphb
), liobn
, info
.dma32_window_start
,
62 info
.dma32_window_size
>> SPAPR_TCE_PAGE_SHIFT
,
65 error_setg(errp
, "spapr-vfio: failed to create VFIO TCE table");
69 /* Register default 32bit DMA window */
70 memory_region_add_subregion(&sphb
->iommu_root
, tcet
->bus_offset
,
71 spapr_tce_get_iommu(tcet
));
74 static void spapr_phb_vfio_reset(DeviceState
*qdev
)
79 static int spapr_phb_vfio_eeh_set_option(sPAPRPHBState
*sphb
,
80 unsigned int addr
, int option
)
82 sPAPRPHBVFIOState
*svphb
= SPAPR_PCI_VFIO_HOST_BRIDGE(sphb
);
83 struct vfio_eeh_pe_op op
= { .argsz
= sizeof(op
) };
87 case RTAS_EEH_DISABLE
:
88 op
.op
= VFIO_EEH_PE_DISABLE
;
90 case RTAS_EEH_ENABLE
: {
95 * The EEH functionality is enabled on basis of PCI device,
96 * instead of PE. We need check the validity of the PCI
99 phb
= PCI_HOST_BRIDGE(sphb
);
100 pdev
= pci_find_device(phb
->bus
,
101 (addr
>> 16) & 0xFF, (addr
>> 8) & 0xFF);
103 return RTAS_OUT_PARAM_ERROR
;
106 op
.op
= VFIO_EEH_PE_ENABLE
;
109 case RTAS_EEH_THAW_IO
:
110 op
.op
= VFIO_EEH_PE_UNFREEZE_IO
;
112 case RTAS_EEH_THAW_DMA
:
113 op
.op
= VFIO_EEH_PE_UNFREEZE_DMA
;
116 return RTAS_OUT_PARAM_ERROR
;
119 ret
= vfio_container_ioctl(&svphb
->phb
.iommu_as
, svphb
->iommugroupid
,
120 VFIO_EEH_PE_OP
, &op
);
122 return RTAS_OUT_HW_ERROR
;
125 return RTAS_OUT_SUCCESS
;
128 static int spapr_phb_vfio_eeh_get_state(sPAPRPHBState
*sphb
, int *state
)
130 sPAPRPHBVFIOState
*svphb
= SPAPR_PCI_VFIO_HOST_BRIDGE(sphb
);
131 struct vfio_eeh_pe_op op
= { .argsz
= sizeof(op
) };
134 op
.op
= VFIO_EEH_PE_GET_STATE
;
135 ret
= vfio_container_ioctl(&svphb
->phb
.iommu_as
, svphb
->iommugroupid
,
136 VFIO_EEH_PE_OP
, &op
);
138 return RTAS_OUT_PARAM_ERROR
;
142 return RTAS_OUT_SUCCESS
;
145 static int spapr_phb_vfio_eeh_reset(sPAPRPHBState
*sphb
, int option
)
147 sPAPRPHBVFIOState
*svphb
= SPAPR_PCI_VFIO_HOST_BRIDGE(sphb
);
148 struct vfio_eeh_pe_op op
= { .argsz
= sizeof(op
) };
152 case RTAS_SLOT_RESET_DEACTIVATE
:
153 op
.op
= VFIO_EEH_PE_RESET_DEACTIVATE
;
155 case RTAS_SLOT_RESET_HOT
:
156 op
.op
= VFIO_EEH_PE_RESET_HOT
;
158 case RTAS_SLOT_RESET_FUNDAMENTAL
:
159 op
.op
= VFIO_EEH_PE_RESET_FUNDAMENTAL
;
162 return RTAS_OUT_PARAM_ERROR
;
165 ret
= vfio_container_ioctl(&svphb
->phb
.iommu_as
, svphb
->iommugroupid
,
166 VFIO_EEH_PE_OP
, &op
);
168 return RTAS_OUT_HW_ERROR
;
171 return RTAS_OUT_SUCCESS
;
174 static int spapr_phb_vfio_eeh_configure(sPAPRPHBState
*sphb
)
176 sPAPRPHBVFIOState
*svphb
= SPAPR_PCI_VFIO_HOST_BRIDGE(sphb
);
177 struct vfio_eeh_pe_op op
= { .argsz
= sizeof(op
) };
180 op
.op
= VFIO_EEH_PE_CONFIGURE
;
181 ret
= vfio_container_ioctl(&svphb
->phb
.iommu_as
, svphb
->iommugroupid
,
182 VFIO_EEH_PE_OP
, &op
);
184 return RTAS_OUT_PARAM_ERROR
;
187 return RTAS_OUT_SUCCESS
;
190 static void spapr_phb_vfio_class_init(ObjectClass
*klass
, void *data
)
192 DeviceClass
*dc
= DEVICE_CLASS(klass
);
193 sPAPRPHBClass
*spc
= SPAPR_PCI_HOST_BRIDGE_CLASS(klass
);
195 dc
->props
= spapr_phb_vfio_properties
;
196 dc
->reset
= spapr_phb_vfio_reset
;
197 spc
->finish_realize
= spapr_phb_vfio_finish_realize
;
198 spc
->eeh_set_option
= spapr_phb_vfio_eeh_set_option
;
199 spc
->eeh_get_state
= spapr_phb_vfio_eeh_get_state
;
200 spc
->eeh_reset
= spapr_phb_vfio_eeh_reset
;
201 spc
->eeh_configure
= spapr_phb_vfio_eeh_configure
;
204 static const TypeInfo spapr_phb_vfio_info
= {
205 .name
= TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE
,
206 .parent
= TYPE_SPAPR_PCI_HOST_BRIDGE
,
207 .instance_size
= sizeof(sPAPRPHBVFIOState
),
208 .class_init
= spapr_phb_vfio_class_init
,
209 .class_size
= sizeof(sPAPRPHBClass
),
212 static void spapr_pci_vfio_register_types(void)
214 type_register_static(&spapr_phb_vfio_info
);
217 type_init(spapr_pci_vfio_register_types
)