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 20 #include "hw/ppc/spapr.h" 21 #include "hw/pci-host/spapr.h" 22 #include "linux/vfio.h" 23 #include "hw/misc/vfio.h" 24 25 static Property spapr_phb_vfio_properties[] = { 26 DEFINE_PROP_INT32("iommu", sPAPRPHBVFIOState, iommugroupid, -1), 27 DEFINE_PROP_END_OF_LIST(), 28 }; 29 30 static void spapr_phb_vfio_finish_realize(sPAPRPHBState *sphb, Error **errp) 31 { 32 sPAPRPHBVFIOState *svphb = SPAPR_PCI_VFIO_HOST_BRIDGE(sphb); 33 struct vfio_iommu_spapr_tce_info info = { .argsz = sizeof(info) }; 34 int ret; 35 sPAPRTCETable *tcet; 36 uint32_t liobn = svphb->phb.dma_liobn; 37 38 if (svphb->iommugroupid == -1) { 39 error_setg(errp, "Wrong IOMMU group ID %d", svphb->iommugroupid); 40 return; 41 } 42 43 ret = vfio_container_ioctl(&svphb->phb.iommu_as, svphb->iommugroupid, 44 VFIO_CHECK_EXTENSION, 45 (void *) VFIO_SPAPR_TCE_IOMMU); 46 if (ret != 1) { 47 error_setg_errno(errp, -ret, 48 "spapr-vfio: SPAPR extension is not supported"); 49 return; 50 } 51 52 ret = vfio_container_ioctl(&svphb->phb.iommu_as, svphb->iommugroupid, 53 VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info); 54 if (ret) { 55 error_setg_errno(errp, -ret, 56 "spapr-vfio: get info from container failed"); 57 return; 58 } 59 60 tcet = spapr_tce_new_table(DEVICE(sphb), liobn, info.dma32_window_start, 61 SPAPR_TCE_PAGE_SHIFT, 62 info.dma32_window_size >> SPAPR_TCE_PAGE_SHIFT, 63 true); 64 if (!tcet) { 65 error_setg(errp, "spapr-vfio: failed to create VFIO TCE table"); 66 return; 67 } 68 69 /* Register default 32bit DMA window */ 70 memory_region_add_subregion(&sphb->iommu_root, tcet->bus_offset, 71 spapr_tce_get_iommu(tcet)); 72 } 73 74 static void spapr_phb_vfio_reset(DeviceState *qdev) 75 { 76 /* Do nothing */ 77 } 78 79 static void spapr_phb_vfio_class_init(ObjectClass *klass, void *data) 80 { 81 DeviceClass *dc = DEVICE_CLASS(klass); 82 sPAPRPHBClass *spc = SPAPR_PCI_HOST_BRIDGE_CLASS(klass); 83 84 dc->props = spapr_phb_vfio_properties; 85 dc->reset = spapr_phb_vfio_reset; 86 spc->finish_realize = spapr_phb_vfio_finish_realize; 87 } 88 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), 95 }; 96 97 static void spapr_pci_vfio_register_types(void) 98 { 99 type_register_static(&spapr_phb_vfio_info); 100 } 101 102 type_init(spapr_pci_vfio_register_types) 103