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/misc/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 void spapr_phb_vfio_class_init(ObjectClass
*klass
, void *data
)
81 DeviceClass
*dc
= DEVICE_CLASS(klass
);
82 sPAPRPHBClass
*spc
= SPAPR_PCI_HOST_BRIDGE_CLASS(klass
);
84 dc
->props
= spapr_phb_vfio_properties
;
85 dc
->reset
= spapr_phb_vfio_reset
;
86 spc
->finish_realize
= spapr_phb_vfio_finish_realize
;
89 static const TypeInfo spapr_phb_vfio_info
= {
90 .name
= TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE
,
91 .parent
= TYPE_SPAPR_PCI_HOST_BRIDGE
,
92 .instance_size
= sizeof(sPAPRPHBVFIOState
),
93 .class_init
= spapr_phb_vfio_class_init
,
94 .class_size
= sizeof(sPAPRPHBClass
),
97 static void spapr_pci_vfio_register_types(void)
99 type_register_static(&spapr_phb_vfio_info
);
102 type_init(spapr_pci_vfio_register_types
)