From nobody Thu May 9 02:18:57 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1635798274; cv=none; d=zohomail.com; s=zohoarc; b=jkjAGCXRErnCtyp30UG9LA/0+yRS+lY2Am0F8QVNV/jujcpKpf7zogq8F3uIt8EGOSMwIjzp0JU6y9PQF8yaWnuPHWUtFalLbjLlLOXCDWefVpauL7VfrQ0B+EYbfLGyq+3S1yk5k9U2oSA69AH94HMbfsb7yKe6Lfx68OUbeAg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1635798274; h=Content-Type:Content-Transfer-Encoding:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=xEyRJSB1WfKc1hxrIIjzvBmb3RHiHblv6srUheYUS7Q=; b=LexAMjl5KTLN/JThRG3oQaWI4z+FPQrECywG+oRp4t4X20nnxcZYPTUArjTmjAeT4tNN36TkJT5bKcl7KlN+GZaPTRRq8AnfWm/X4Rwx6To5U8U1uTOstygKIv94NVfHSjhWXj4fT5Rsru9GQW3LtugZ+UXqVOKoRO+8Jg6cmO8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1635798274347616.7651864302554; Mon, 1 Nov 2021 13:24:34 -0700 (PDT) Received: from localhost ([::1]:57158 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mhdr7-00082W-7h for importer@patchew.org; Mon, 01 Nov 2021 16:24:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57728) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mhcN1-0005dl-SA for qemu-devel@nongnu.org; Mon, 01 Nov 2021 14:49:24 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:32398) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mhcMu-0000iO-Tx for qemu-devel@nongnu.org; Mon, 01 Nov 2021 14:49:22 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-53-V5p8ajv4ObOblv3gdFGCnw-1; Mon, 01 Nov 2021 14:49:12 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 385C018125C1; Mon, 1 Nov 2021 18:49:11 +0000 (UTC) Received: from laptop.redhat.com (unknown [10.39.192.74]) by smtp.corp.redhat.com (Postfix) with ESMTP id A804A60862; Mon, 1 Nov 2021 18:49:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1635792555; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=xEyRJSB1WfKc1hxrIIjzvBmb3RHiHblv6srUheYUS7Q=; b=KmfEBonptnEMMWCnxtkQOzgVDy8JirlawaHK78sr0PVtWhcAFwRTMz/m5c++utlJdDqC5s HNlIYiEq02itdBMgtNuBOgKDrb3HhW47mG4QJDN9TqibdtwtiaZ8rCdIZ41iXDR8jbhT4M tc4LmRcZi3mUfUf4tFFMULkJNP5dS+U= X-MC-Unique: V5p8ajv4ObOblv3gdFGCnw-1 From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, thuth@redhat.com, pbonzini@redhat.com, lvivier@redhat.com, qemu-arm@nongnu.org, qemu-devel@nongnu.org, jean-philippe@linaro.org Subject: [PATCH v5] tests: qtest: Add virtio-iommu test Date: Mon, 1 Nov 2021 19:48:58 +0100 Message-Id: <20211101184858.15223-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eric.auger@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eric.auger@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -8 X-Spam_score: -0.9 X-Spam_bar: / X-Spam_report: (-0.9 / 5.0 requ) DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, T_SPF_TEMPERROR=0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1635798276480100001 Content-Type: text/plain; charset="utf-8" Add the framework to test the virtio-iommu-pci device and tests exercising the attach/detach, map/unmap API. Signed-off-by: Eric Auger Tested-by: Jean-Philippe Brucker Reviewed-by: Jean-Philippe Brucker --- v4 -> v5: - remove printf and move a comment - Added Jean-Philippe's T-b and R-b --- tests/qtest/libqos/meson.build | 1 + tests/qtest/libqos/virtio-iommu.c | 126 ++++++++++++ tests/qtest/libqos/virtio-iommu.h | 40 ++++ tests/qtest/meson.build | 1 + tests/qtest/virtio-iommu-test.c | 326 ++++++++++++++++++++++++++++++ 5 files changed, 494 insertions(+) create mode 100644 tests/qtest/libqos/virtio-iommu.c create mode 100644 tests/qtest/libqos/virtio-iommu.h create mode 100644 tests/qtest/virtio-iommu-test.c diff --git a/tests/qtest/libqos/meson.build b/tests/qtest/libqos/meson.build index 1f5c8f1053..ba90bbe2b8 100644 --- a/tests/qtest/libqos/meson.build +++ b/tests/qtest/libqos/meson.build @@ -40,6 +40,7 @@ libqos_srcs =3D files('../libqtest.c', 'virtio-rng.c', 'virtio-scsi.c', 'virtio-serial.c', + 'virtio-iommu.c', =20 # qgraph machines: 'aarch64-xlnx-zcu102-machine.c', diff --git a/tests/qtest/libqos/virtio-iommu.c b/tests/qtest/libqos/virtio-= iommu.c new file mode 100644 index 0000000000..18cba4ca36 --- /dev/null +++ b/tests/qtest/libqos/virtio-iommu.c @@ -0,0 +1,126 @@ +/* + * libqos driver virtio-iommu-pci framework + * + * Copyright (c) 2021 Red Hat, Inc. + * + * Authors: + * Eric Auger + * + * This work is licensed under the terms of the GNU GPL, version 2 or (at = your + * option) any later version. See the COPYING file in the top-level direc= tory. + * + */ + +#include "qemu/osdep.h" +#include "libqtest.h" +#include "qemu/module.h" +#include "qgraph.h" +#include "virtio-iommu.h" +#include "hw/virtio/virtio-iommu.h" + +static QGuestAllocator *alloc; + +/* virtio-iommu-device */ +static void *qvirtio_iommu_get_driver(QVirtioIOMMU *v_iommu, + const char *interface) +{ + if (!g_strcmp0(interface, "virtio-iommu")) { + return v_iommu; + } + if (!g_strcmp0(interface, "virtio")) { + return v_iommu->vdev; + } + + fprintf(stderr, "%s not present in virtio-iommu-device\n", interface); + g_assert_not_reached(); +} + +static void virtio_iommu_cleanup(QVirtioIOMMU *interface) +{ + qvirtqueue_cleanup(interface->vdev->bus, interface->vq, alloc); +} + +static void virtio_iommu_setup(QVirtioIOMMU *interface) +{ + QVirtioDevice *vdev =3D interface->vdev; + uint64_t features; + + features =3D qvirtio_get_features(vdev); + features &=3D ~(QVIRTIO_F_BAD_FEATURE | + (1ull << VIRTIO_RING_F_INDIRECT_DESC) | + (1ull << VIRTIO_RING_F_EVENT_IDX) | + (1ull << VIRTIO_IOMMU_F_BYPASS)); + qvirtio_set_features(vdev, features); + interface->vq =3D qvirtqueue_setup(interface->vdev, alloc, 0); + qvirtio_set_driver_ok(interface->vdev); +} + +/* virtio-iommu-pci */ +static void *qvirtio_iommu_pci_get_driver(void *object, const char *interf= ace) +{ + QVirtioIOMMUPCI *v_iommu =3D object; + if (!g_strcmp0(interface, "pci-device")) { + return v_iommu->pci_vdev.pdev; + } + return qvirtio_iommu_get_driver(&v_iommu->iommu, interface); +} + +static void qvirtio_iommu_pci_destructor(QOSGraphObject *obj) +{ + QVirtioIOMMUPCI *iommu_pci =3D (QVirtioIOMMUPCI *) obj; + QVirtioIOMMU *interface =3D &iommu_pci->iommu; + QOSGraphObject *pci_vobj =3D &iommu_pci->pci_vdev.obj; + + virtio_iommu_cleanup(interface); + qvirtio_pci_destructor(pci_vobj); +} + +static void qvirtio_iommu_pci_start_hw(QOSGraphObject *obj) +{ + QVirtioIOMMUPCI *iommu_pci =3D (QVirtioIOMMUPCI *) obj; + QVirtioIOMMU *interface =3D &iommu_pci->iommu; + QOSGraphObject *pci_vobj =3D &iommu_pci->pci_vdev.obj; + + qvirtio_pci_start_hw(pci_vobj); + virtio_iommu_setup(interface); +} + + +static void *virtio_iommu_pci_create(void *pci_bus, QGuestAllocator *t_all= oc, + void *addr) +{ + QVirtioIOMMUPCI *virtio_rpci =3D g_new0(QVirtioIOMMUPCI, 1); + QVirtioIOMMU *interface =3D &virtio_rpci->iommu; + QOSGraphObject *obj =3D &virtio_rpci->pci_vdev.obj; + + virtio_pci_init(&virtio_rpci->pci_vdev, pci_bus, addr); + interface->vdev =3D &virtio_rpci->pci_vdev.vdev; + alloc =3D t_alloc; + + obj->get_driver =3D qvirtio_iommu_pci_get_driver; + obj->start_hw =3D qvirtio_iommu_pci_start_hw; + obj->destructor =3D qvirtio_iommu_pci_destructor; + + return obj; +} + +static void virtio_iommu_register_nodes(void) +{ + QPCIAddress addr =3D { + .devfn =3D QPCI_DEVFN(4, 0), + }; + + QOSGraphEdgeOptions opts =3D { + .extra_device_opts =3D "addr=3D04.0", + }; + + /* virtio-iommu-pci */ + add_qpci_address(&opts, &addr); + qos_node_create_driver("virtio-iommu-pci", virtio_iommu_pci_create); + qos_node_consumes("virtio-iommu-pci", "pci-bus", &opts); + qos_node_produces("virtio-iommu-pci", "pci-device"); + qos_node_produces("virtio-iommu-pci", "virtio"); + qos_node_produces("virtio-iommu-pci", "virtio-iommu"); +} + +libqos_init(virtio_iommu_register_nodes); diff --git a/tests/qtest/libqos/virtio-iommu.h b/tests/qtest/libqos/virtio-= iommu.h new file mode 100644 index 0000000000..d753761958 --- /dev/null +++ b/tests/qtest/libqos/virtio-iommu.h @@ -0,0 +1,40 @@ +/* + * libqos driver virtio-iommu-pci framework + * + * Copyright (c) 2021 Red Hat, Inc. + * + * Authors: + * Eric Auger + * + * This work is licensed under the terms of the GNU GPL, version 2 or (at = your + * option) any later version. See the COPYING file in the top-level direc= tory. + * + */ + +#ifndef TESTS_LIBQOS_VIRTIO_IOMMU_H +#define TESTS_LIBQOS_VIRTIO_IOMMU_H + +#include "qgraph.h" +#include "virtio.h" +#include "virtio-pci.h" + +typedef struct QVirtioIOMMU QVirtioIOMMU; +typedef struct QVirtioIOMMUPCI QVirtioIOMMUPCI; +typedef struct QVirtioIOMMUDevice QVirtioIOMMUDevice; + +struct QVirtioIOMMU { + QVirtioDevice *vdev; + QVirtQueue *vq; +}; + +struct QVirtioIOMMUPCI { + QVirtioPCIDevice pci_vdev; + QVirtioIOMMU iommu; +}; + +struct QVirtioIOMMUDevice { + QOSGraphObject obj; + QVirtioIOMMU iommu; +}; + +#endif diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build index c9d8458062..982ffb3e38 100644 --- a/tests/qtest/meson.build +++ b/tests/qtest/meson.build @@ -230,6 +230,7 @@ qos_test_ss.add( 'virtio-rng-test.c', 'virtio-scsi-test.c', 'virtio-serial-test.c', + 'virtio-iommu-test.c', 'vmxnet3-test.c', ) if have_virtfs diff --git a/tests/qtest/virtio-iommu-test.c b/tests/qtest/virtio-iommu-tes= t.c new file mode 100644 index 0000000000..42390eb2b7 --- /dev/null +++ b/tests/qtest/virtio-iommu-test.c @@ -0,0 +1,326 @@ +/* + * QTest testcase for VirtIO IOMMU + * + * Copyright (c) 2021 Red Hat, Inc. + * + * Authors: + * Eric Auger + * + * This work is licensed under the terms of the GNU GPL, version 2 or (at = your + * option) any later version. See the COPYING file in the top-level direc= tory. + * + */ + +#include "qemu/osdep.h" +#include "libqtest-single.h" +#include "qemu/module.h" +#include "libqos/qgraph.h" +#include "libqos/virtio-iommu.h" +#include "hw/virtio/virtio-iommu.h" + +#define PCI_SLOT_HP 0x06 +#define QVIRTIO_IOMMU_TIMEOUT_US (30 * 1000 * 1000) + +static QGuestAllocator *alloc; + +static void pci_config(void *obj, void *data, QGuestAllocator *t_alloc) +{ + QVirtioIOMMU *v_iommu =3D obj; + QVirtioDevice *dev =3D v_iommu->vdev; + uint64_t input_range_start =3D qvirtio_config_readq(dev, 8); + uint64_t input_range_end =3D qvirtio_config_readq(dev, 16); + uint32_t domain_range_start =3D qvirtio_config_readl(dev, 24); + uint32_t domain_range_end =3D qvirtio_config_readl(dev, 28); + + g_assert_cmpint(input_range_start, =3D=3D, 0); + g_assert_cmphex(input_range_end, =3D=3D, UINT64_MAX); + g_assert_cmpint(domain_range_start, =3D=3D, 0); + g_assert_cmpint(domain_range_end, =3D=3D, 32); +} + +static int read_tail_status(struct virtio_iommu_req_tail *buffer) +{ + int i; + + for (i =3D 0; i < 3; i++) { + g_assert_cmpint(buffer->reserved[i], =3D=3D, 0); + } + return buffer->status; +} + +/** + * send_attach_detach - Send an attach/detach command to the device + * @type: VIRTIO_IOMMU_T_ATTACH/VIRTIO_IOMMU_T_DETACH + * @domain: domain the endpoint is attached to + * @ep: endpoint + */ +static int send_attach_detach(QTestState *qts, QVirtioIOMMU *v_iommu, + uint8_t type, uint32_t domain, uint32_t ep) +{ + QVirtioDevice *dev =3D v_iommu->vdev; + QVirtQueue *vq =3D v_iommu->vq; + uint64_t ro_addr, wr_addr; + uint32_t free_head; + struct virtio_iommu_req_attach req =3D {}; /* same layout as detach */ + size_t ro_size =3D sizeof(req) - sizeof(struct virtio_iommu_req_tail); + size_t wr_size =3D sizeof(struct virtio_iommu_req_tail); + struct virtio_iommu_req_tail buffer; + int ret; + + req.head.type =3D type; + req.domain =3D cpu_to_le32(domain); + req.endpoint =3D cpu_to_le32(ep); + + ro_addr =3D guest_alloc(alloc, ro_size); + wr_addr =3D guest_alloc(alloc, wr_size); + + qtest_memwrite(qts, ro_addr, &req, ro_size); + free_head =3D qvirtqueue_add(qts, vq, ro_addr, ro_size, false, true); + qvirtqueue_add(qts, vq, wr_addr, wr_size, true, false); + qvirtqueue_kick(qts, dev, vq, free_head); + qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL, + QVIRTIO_IOMMU_TIMEOUT_US); + qtest_memread(qts, wr_addr, &buffer, wr_size); + ret =3D read_tail_status(&buffer); + guest_free(alloc, ro_addr); + guest_free(alloc, wr_addr); + return ret; +} + +/** + * send_map - Send a map command to the device + * @domain: domain the new mapping is attached to + * @virt_start: iova start + * @virt_end: iova end + * @phys_start: base physical address + * @flags: mapping flags + */ +static int send_map(QTestState *qts, QVirtioIOMMU *v_iommu, + uint32_t domain, uint64_t virt_start, uint64_t virt_en= d, + uint64_t phys_start, uint32_t flags) +{ + QVirtioDevice *dev =3D v_iommu->vdev; + QVirtQueue *vq =3D v_iommu->vq; + uint64_t ro_addr, wr_addr; + uint32_t free_head; + struct virtio_iommu_req_map req; + size_t ro_size =3D sizeof(req) - sizeof(struct virtio_iommu_req_tail); + size_t wr_size =3D sizeof(struct virtio_iommu_req_tail); + struct virtio_iommu_req_tail buffer; + int ret; + + req.head.type =3D VIRTIO_IOMMU_T_MAP; + req.domain =3D cpu_to_le32(domain); + req.virt_start =3D cpu_to_le64(virt_start); + req.virt_end =3D cpu_to_le64(virt_end); + req.phys_start =3D cpu_to_le64(phys_start); + req.flags =3D cpu_to_le32(flags); + + ro_addr =3D guest_alloc(alloc, ro_size); + wr_addr =3D guest_alloc(alloc, wr_size); + + qtest_memwrite(qts, ro_addr, &req, ro_size); + free_head =3D qvirtqueue_add(qts, vq, ro_addr, ro_size, false, true); + qvirtqueue_add(qts, vq, wr_addr, wr_size, true, false); + qvirtqueue_kick(qts, dev, vq, free_head); + qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL, + QVIRTIO_IOMMU_TIMEOUT_US); + qtest_memread(qts, wr_addr, &buffer, wr_size); + ret =3D read_tail_status(&buffer); + guest_free(alloc, ro_addr); + guest_free(alloc, wr_addr); + return ret; +} + +/** + * send_unmap - Send an unmap command to the device + * @domain: domain the new binding is attached to + * @virt_start: iova start + * @virt_end: iova end + */ +static int send_unmap(QTestState *qts, QVirtioIOMMU *v_iommu, + uint32_t domain, uint64_t virt_start, uint64_t virt_= end) +{ + QVirtioDevice *dev =3D v_iommu->vdev; + QVirtQueue *vq =3D v_iommu->vq; + uint64_t ro_addr, wr_addr; + uint32_t free_head; + struct virtio_iommu_req_unmap req; + size_t ro_size =3D sizeof(req) - sizeof(struct virtio_iommu_req_tail); + size_t wr_size =3D sizeof(struct virtio_iommu_req_tail); + struct virtio_iommu_req_tail buffer; + int ret; + + req.head.type =3D VIRTIO_IOMMU_T_UNMAP; + req.domain =3D cpu_to_le32(domain); + req.virt_start =3D cpu_to_le64(virt_start); + req.virt_end =3D cpu_to_le64(virt_end); + + ro_addr =3D guest_alloc(alloc, ro_size); + wr_addr =3D guest_alloc(alloc, wr_size); + + qtest_memwrite(qts, ro_addr, &req, ro_size); + free_head =3D qvirtqueue_add(qts, vq, ro_addr, ro_size, false, true); + qvirtqueue_add(qts, vq, wr_addr, wr_size, true, false); + qvirtqueue_kick(qts, dev, vq, free_head); + qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL, + QVIRTIO_IOMMU_TIMEOUT_US); + qtest_memread(qts, wr_addr, &buffer, wr_size); + ret =3D read_tail_status(&buffer); + guest_free(alloc, ro_addr); + guest_free(alloc, wr_addr); + return ret; +} + +static void test_attach_detach(void *obj, void *data, QGuestAllocator *t_a= lloc) +{ + QVirtioIOMMU *v_iommu =3D obj; + QTestState *qts =3D global_qtest; + int ret; + + alloc =3D t_alloc; + + /* type, domain, ep */ + + /* attach ep0 to domain 0 */ + ret =3D send_attach_detach(qts, v_iommu, VIRTIO_IOMMU_T_ATTACH, 0, 0); + g_assert_cmpint(ret, =3D=3D, 0); + + /* attach a non existing device */ + ret =3D send_attach_detach(qts, v_iommu, VIRTIO_IOMMU_T_ATTACH, 0, 444= ); + g_assert_cmpint(ret, =3D=3D, VIRTIO_IOMMU_S_NOENT); + + /* detach a non existing device (1) */ + ret =3D send_attach_detach(qts, v_iommu, VIRTIO_IOMMU_T_DETACH, 0, 1); + g_assert_cmpint(ret, =3D=3D, VIRTIO_IOMMU_S_NOENT); + + /* move ep0 from domain 0 to domain 1 */ + ret =3D send_attach_detach(qts, v_iommu, VIRTIO_IOMMU_T_ATTACH, 1, 0); + g_assert_cmpint(ret, =3D=3D, 0); + + /* detach ep0 from domain 0 */ + ret =3D send_attach_detach(qts, v_iommu, VIRTIO_IOMMU_T_DETACH, 0, 0); + g_assert_cmpint(ret, =3D=3D, VIRTIO_IOMMU_S_INVAL); + + /* detach ep0 from domain 1 */ + ret =3D send_attach_detach(qts, v_iommu, VIRTIO_IOMMU_T_DETACH, 1, 0); + g_assert_cmpint(ret, =3D=3D, 0); + + ret =3D send_attach_detach(qts, v_iommu, VIRTIO_IOMMU_T_ATTACH, 1, 0); + g_assert_cmpint(ret, =3D=3D, 0); + ret =3D send_map(qts, v_iommu, 1, 0x0, 0xFFF, 0xa1000, + VIRTIO_IOMMU_MAP_F_READ); + g_assert_cmpint(ret, =3D=3D, 0); + ret =3D send_map(qts, v_iommu, 1, 0x2000, 0x2FFF, 0xb1000, + VIRTIO_IOMMU_MAP_F_READ); + g_assert_cmpint(ret, =3D=3D, 0); + ret =3D send_attach_detach(qts, v_iommu, VIRTIO_IOMMU_T_DETACH, 1, 0); + g_assert_cmpint(ret, =3D=3D, 0); +} + +/* Test map/unmap scenari documented in the spec */ +static void test_map_unmap(void *obj, void *data, QGuestAllocator *t_alloc) +{ + QVirtioIOMMU *v_iommu =3D obj; + QTestState *qts =3D global_qtest; + int ret; + + alloc =3D t_alloc; + + /* attach ep0 to domain 1 */ + ret =3D send_attach_detach(qts, v_iommu, VIRTIO_IOMMU_T_ATTACH, 1, 0); + g_assert_cmpint(ret, =3D=3D, 0); + + ret =3D send_map(qts, v_iommu, 0, 0, 0xFFF, 0xa1000, VIRTIO_IOMMU_MAP_= F_READ); + g_assert_cmpint(ret, =3D=3D, VIRTIO_IOMMU_S_NOENT); + + /* domain, virt start, virt end, phys start, flags */ + ret =3D send_map(qts, v_iommu, 1, 0x0, 0xFFF, 0xa1000, VIRTIO_IOMMU_MA= P_F_READ); + g_assert_cmpint(ret, =3D=3D, 0); + + /* send a new mapping overlapping the previous one */ + ret =3D send_map(qts, v_iommu, 1, 0, 0xFFFF, 0xb1000, VIRTIO_IOMMU_MAP= _F_READ); + g_assert_cmpint(ret, =3D=3D, VIRTIO_IOMMU_S_INVAL); + + ret =3D send_unmap(qts, v_iommu, 4, 0x10, 0xFFF); + g_assert_cmpint(ret, =3D=3D, VIRTIO_IOMMU_S_NOENT); + + ret =3D send_unmap(qts, v_iommu, 1, 0x10, 0xFFF); + g_assert_cmpint(ret, =3D=3D, VIRTIO_IOMMU_S_RANGE); + + ret =3D send_unmap(qts, v_iommu, 1, 0, 0x1000); + g_assert_cmpint(ret, =3D=3D, 0); /* unmap everything */ + + /* Spec example sequence */ + + /* 1 */ + ret =3D send_unmap(qts, v_iommu, 1, 0, 4); + g_assert_cmpint(ret, =3D=3D, 0); /* doesn't unmap anything */ + + /* 2 */ + ret =3D send_map(qts, v_iommu, 1, 0, 9, 0xa1000, VIRTIO_IOMMU_MAP_F_RE= AD); + g_assert_cmpint(ret, =3D=3D, 0); + ret =3D send_unmap(qts, v_iommu, 1, 0, 9); + g_assert_cmpint(ret, =3D=3D, 0); /* unmaps [0,9] */ + + /* 3 */ + ret =3D send_map(qts, v_iommu, 1, 0, 4, 0xb1000, VIRTIO_IOMMU_MAP_F_RE= AD); + g_assert_cmpint(ret, =3D=3D, 0); + ret =3D send_map(qts, v_iommu, 1, 5, 9, 0xb2000, VIRTIO_IOMMU_MAP_F_RE= AD); + g_assert_cmpint(ret, =3D=3D, 0); + ret =3D send_unmap(qts, v_iommu, 1, 0, 9); + g_assert_cmpint(ret, =3D=3D, 0); /* unmaps [0,4] and [5,9] */ + + /* 4 */ + ret =3D send_map(qts, v_iommu, 1, 0, 9, 0xc1000, VIRTIO_IOMMU_MAP_F_RE= AD); + g_assert_cmpint(ret, =3D=3D, 0); + + ret =3D send_unmap(qts, v_iommu, 1, 0, 4); + g_assert_cmpint(ret, =3D=3D, VIRTIO_IOMMU_S_RANGE); /* doesn't unmap a= nything */ + + ret =3D send_unmap(qts, v_iommu, 1, 0, 10); + g_assert_cmpint(ret, =3D=3D, 0); + + /* 5 */ + ret =3D send_map(qts, v_iommu, 1, 0, 4, 0xd1000, VIRTIO_IOMMU_MAP_F_RE= AD); + g_assert_cmpint(ret, =3D=3D, 0); + ret =3D send_map(qts, v_iommu, 1, 5, 9, 0xd2000, VIRTIO_IOMMU_MAP_F_RE= AD); + g_assert_cmpint(ret, =3D=3D, 0); + ret =3D send_unmap(qts, v_iommu, 1, 0, 4); + g_assert_cmpint(ret, =3D=3D, 0); /* unmaps [0,4] */ + + ret =3D send_unmap(qts, v_iommu, 1, 5, 9); + g_assert_cmpint(ret, =3D=3D, 0); + + /* 6 */ + ret =3D send_map(qts, v_iommu, 1, 0, 4, 0xe2000, VIRTIO_IOMMU_MAP_F_RE= AD); + g_assert_cmpint(ret, =3D=3D, 0); + ret =3D send_unmap(qts, v_iommu, 1, 0, 9); + g_assert_cmpint(ret, =3D=3D, 0); /* unmaps [0,4] */ + + /* 7 */ + ret =3D send_map(qts, v_iommu, 1, 0, 4, 0xf2000, VIRTIO_IOMMU_MAP_F_RE= AD); + g_assert_cmpint(ret, =3D=3D, 0); + ret =3D send_map(qts, v_iommu, 1, 10, 14, 0xf3000, VIRTIO_IOMMU_MAP_F_= READ); + g_assert_cmpint(ret, =3D=3D, 0); + ret =3D send_unmap(qts, v_iommu, 1, 0, 14); + g_assert_cmpint(ret, =3D=3D, 0); /* unmaps [0,4] and [10,14] */ + + ret =3D send_map(qts, v_iommu, 1, 10, 14, 0xf3000, VIRTIO_IOMMU_MAP_F_= READ); + g_assert_cmpint(ret, =3D=3D, 0); + ret =3D send_map(qts, v_iommu, 1, 0, 4, 0xf2000, VIRTIO_IOMMU_MAP_F_RE= AD); + g_assert_cmpint(ret, =3D=3D, 0); + ret =3D send_unmap(qts, v_iommu, 1, 0, 4); + g_assert_cmpint(ret, =3D=3D, 0); /* only unmaps [0,4] */ + ret =3D send_map(qts, v_iommu, 1, 10, 14, 0xf3000, VIRTIO_IOMMU_MAP_F_= READ); + g_assert_cmpint(ret, =3D=3D, VIRTIO_IOMMU_S_INVAL); /* 10-14 still is = mapped */ +} + +static void register_virtio_iommu_test(void) +{ + qos_add_test("config", "virtio-iommu", pci_config, NULL); + qos_add_test("attach_detach", "virtio-iommu", test_attach_detach, NULL= ); + qos_add_test("map_unmap", "virtio-iommu", test_map_unmap, NULL); +} + +libqos_init(register_virtio_iommu_test); --=20 2.26.3