From patchwork Wed Jan 9 12:41:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pierre Morel X-Patchwork-Id: 10754069 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D74BE13B4 for ; Wed, 9 Jan 2019 12:57:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C51DF28E75 for ; Wed, 9 Jan 2019 12:57:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B779828EB8; Wed, 9 Jan 2019 12:57:57 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 388FB28E75 for ; Wed, 9 Jan 2019 12:57:57 +0000 (UTC) Received: from localhost ([127.0.0.1]:58657 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ghDQh-000604-T2 for patchwork-qemu-devel@patchwork.kernel.org; Wed, 09 Jan 2019 07:57:55 -0500 Received: from eggs.gnu.org ([209.51.188.92]:41822) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ghDKm-0000Y2-6C for qemu-devel@nongnu.org; Wed, 09 Jan 2019 07:51:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ghDAs-0004Cf-6j for qemu-devel@nongnu.org; Wed, 09 Jan 2019 07:41:36 -0500 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:41952) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ghDAq-0004B7-E4 for qemu-devel@nongnu.org; Wed, 09 Jan 2019 07:41:34 -0500 Received: from pps.filterd (m0098393.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id x09CXowL045772 for ; Wed, 9 Jan 2019 07:41:30 -0500 Received: from e06smtp04.uk.ibm.com (e06smtp04.uk.ibm.com [195.75.94.100]) by mx0a-001b2d01.pphosted.com with ESMTP id 2pwepfq73v-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 09 Jan 2019 07:41:30 -0500 Received: from localhost by e06smtp04.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 9 Jan 2019 12:41:28 -0000 Received: from b06cxnps3074.portsmouth.uk.ibm.com (9.149.109.194) by e06smtp04.uk.ibm.com (192.168.101.134) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Wed, 9 Jan 2019 12:41:25 -0000 Received: from d06av22.portsmouth.uk.ibm.com (d06av22.portsmouth.uk.ibm.com [9.149.105.58]) by b06cxnps3074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x09CfO1n52363304 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Wed, 9 Jan 2019 12:41:24 GMT Received: from d06av22.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2FE344C058; Wed, 9 Jan 2019 12:41:24 +0000 (GMT) Received: from d06av22.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id DC2054C040; Wed, 9 Jan 2019 12:41:23 +0000 (GMT) Received: from morel-ThinkPad-W530.boeblingen.de.ibm.com (unknown [9.152.224.140]) by d06av22.portsmouth.uk.ibm.com (Postfix) with ESMTP; Wed, 9 Jan 2019 12:41:23 +0000 (GMT) From: Pierre Morel To: walling@linux.ibm.com Date: Wed, 9 Jan 2019 13:41:19 +0100 X-Mailer: git-send-email 2.7.4 In-Reply-To: <1547037680-21458-1-git-send-email-pmorel@linux.ibm.com> References: <1547037680-21458-1-git-send-email-pmorel@linux.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 19010912-0016-0000-0000-000002428067 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19010912-0017-0000-0000-0000329C999D Message-Id: <1547037680-21458-3-git-send-email-pmorel@linux.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2019-01-09_07:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=3 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=907 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1901090106 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.156.1 Subject: [Qemu-devel] [PATCH v1 2/3] vfio/pci: Get real IOMMU information from container X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: thuth@redhat.com, david@redhat.com, cohuck@redhat.com, qemu-devel@nongnu.org, pasic@linux.ibm.com, borntraeger@de.ibm.com, qemu-s390x@nongnu.org, rth@twiddle.net Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP When the vfio_iommu_type1 supports the VFIO_IOMMU_INFO_CAPABILITIES and the capability ID VFIO_IOMMU_INFO_CAP_DMA we can use an ioctl to retrieve this information from the real IOMMU device. Let's use this information to add the host window associated with the container. Signed-off-by: Pierre Morel --- hw/vfio/common.c | 73 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 56 insertions(+), 17 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 7c185e5a..fd7e991 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1036,6 +1036,61 @@ static void vfio_put_address_space(VFIOAddressSpace *space) } } +static void vfio_iommu_type1_get_info(int fd, VFIOContainer *container) +{ + struct vfio_iommu_type1_info *info; + size_t argsz = sizeof(*info); + struct vfio_info_cap_header *hdr; + struct vfio_iommu_cap_dma *dma_info; + int ret = 0; + + info = g_malloc0(argsz); + +retry: + info->argsz = argsz; + info->flags = VFIO_IOMMU_INFO_CAPABILITIES; + + ret = ioctl(fd, VFIO_IOMMU_GET_INFO, info); + /* Ignore errors */ + if (ret) { + goto out; + } + if (!(info->flags & VFIO_IOMMU_INFO_PGSIZES)) { + info->iova_pgsizes = 4096; + } + + if (info->argsz > argsz) { + argsz = info->argsz; + info = g_realloc(info, argsz); + + goto retry; + } + if (info->argsz != argsz) { + goto out; + } + /* Now we have the capabilities */ + hdr = (struct vfio_info_cap_header *)((unsigned char *)info + + sizeof(struct vfio_iommu_type1_info) + + info->cap_offset); + do { + dma_info = (struct vfio_iommu_cap_dma *) (hdr); + if (hdr->id == VFIO_IOMMU_INFO_CAP_DMA) { + vfio_host_win_add(container, 0, + dma_info->dma_end - dma_info->dma_start, + info->iova_pgsizes); + container->pgsizes = info->iova_pgsizes; + return; + } + hdr = (struct vfio_info_cap_header *)((unsigned char *) dma_info + + hdr->next); + } while (hdr->next); +out: + /* Assume 4k IOVA page size */ + vfio_host_win_add(container, 0, (hwaddr)-1, 4096); + container->pgsizes = 4096; + return; +} + static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, Error **errp) { @@ -1104,7 +1159,6 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) || ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1v2_IOMMU)) { bool v2 = !!ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1v2_IOMMU); - struct vfio_iommu_type1_info info; ret = ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &fd); if (ret) { @@ -1121,22 +1175,7 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, goto free_container_exit; } - /* - * FIXME: This assumes that a Type1 IOMMU can map any 64-bit - * IOVA whatsoever. That's not actually true, but the current - * kernel interface doesn't tell us what it can map, and the - * existing Type1 IOMMUs generally support any IOVA we're - * going to actually try in practice. - */ - info.argsz = sizeof(info); - ret = ioctl(fd, VFIO_IOMMU_GET_INFO, &info); - /* Ignore errors */ - if (ret || !(info.flags & VFIO_IOMMU_INFO_PGSIZES)) { - /* Assume 4k IOVA page size */ - info.iova_pgsizes = 4096; - } - vfio_host_win_add(container, 0, (hwaddr)-1, info.iova_pgsizes); - container->pgsizes = info.iova_pgsizes; + vfio_iommu_type1_get_info(fd, container); } else if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_IOMMU) || ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_v2_IOMMU)) { struct vfio_iommu_spapr_tce_info info;