From nobody Sun May 12 23:37:35 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1574447553; cv=none; d=zohomail.com; s=zohoarc; b=RaRjOqPFM/k+OuNpn4x1eXND3hNXD6vg4w+tCbAeHyhqZq4baJcMuhrSYmlXWkGl6ZrC90v+tFdcc/j+e6WV00+kSlFN2yFdLeMTmdp1+FTkiyj4v4nxKbG77pFhTljol4y/DJs1u/oxdgScRoeZ2coiu4S2tEEfYnhEK9Kp9cU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574447553; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=peTkTFYmLxL5F9uNJgV5RSas0ai1TQy9nbSwswRnA9s=; b=JlcHiRyC1iMxvUPyhlyqTxTRdawrVwhaN9n4d/jEabQ+Dicad25JDfgZkyrQTbd7zjqYxHitHVGepVFb3E1ZdgLni3EWyeqQ/JMT/5U476oSOVvLdzPwBc8LxcM+aMrRcUsz5m1wMxS/deRebqZ2WT3miH+Rq4yhKEV03ynOzDA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574447553747556.4166112173118; Fri, 22 Nov 2019 10:32:33 -0800 (PST) Received: from localhost ([::1]:54032 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDjM-0000zu-4p for importer@patchew.org; Fri, 22 Nov 2019 13:32:32 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39593) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDhC-0007jt-3c for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:30:20 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iYDh9-000833-K3 for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:30:17 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:55643 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iYDh9-00082A-FR for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:30:15 -0500 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-209-J4XbWGD0MBSzGyFdslFsUA-1; Fri, 22 Nov 2019 13:30:13 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A55DF8024DC; Fri, 22 Nov 2019 18:30:11 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-37.ams2.redhat.com [10.36.116.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id 26DA46E712; Fri, 22 Nov 2019 18:30:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574447414; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=peTkTFYmLxL5F9uNJgV5RSas0ai1TQy9nbSwswRnA9s=; b=MqJ5fyVkjDrsuTZZM6IZYDM/g6gaq7TXSXFQvfMRBABk/PDTYzaM4H5Rd/snatPJSZ7J7f YH0RAAqiB1cED404RB6Is6zVkMTXELbPA6WZ5WkTSOV/lDKPtgW56b/Dyrh8Ny8M9cQo+H QOPGQDGDqroBy9G10JsETxu+mnoKfSM= From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org, mst@redhat.com, armbru@redhat.com, jean-philippe.brucker@arm.com, bharatb.linux@gmail.com, yang.zhong@intel.com, dgilbert@redhat.com, quintela@redhat.com Subject: [PATCH for-5.0 v11 01/20] migration: Support QLIST migration Date: Fri, 22 Nov 2019 19:29:24 +0100 Message-Id: <20191122182943.4656-2-eric.auger@redhat.com> In-Reply-To: <20191122182943.4656-1-eric.auger@redhat.com> References: <20191122182943.4656-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: J4XbWGD0MBSzGyFdslFsUA-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, peterx@redhat.com, tnowicki@marvell.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Support QLIST migration using the same principle as QTAILQ: 94869d5c52 ("migration: migrate QTAILQ"). The VMSTATE_QLIST_V macro has the same proto as VMSTATE_QTAILQ_V. The change mainly resides in QLIST RAW macros: QLIST_RAW_INSERT_HEAD and QLIST_RAW_REVERSE. Tests also are provided. Signed-off-by: Eric Auger --- v5 - v6: - by doing more advanced testing with virtio-iommu migration I noticed this was broken. "prev" field was not set properly. I improved the tests to manipulate both the next and prev fields. - Removed Peter and Juan's R-b --- include/migration/vmstate.h | 21 +++++ include/qemu/queue.h | 39 +++++++++ migration/trace-events | 5 ++ migration/vmstate-types.c | 70 +++++++++++++++ tests/test-vmstate.c | 170 ++++++++++++++++++++++++++++++++++++ 5 files changed, 305 insertions(+) diff --git a/include/migration/vmstate.h b/include/migration/vmstate.h index ac4f46a67d..08683d93c6 100644 --- a/include/migration/vmstate.h +++ b/include/migration/vmstate.h @@ -227,6 +227,7 @@ extern const VMStateInfo vmstate_info_tmp; extern const VMStateInfo vmstate_info_bitmap; extern const VMStateInfo vmstate_info_qtailq; extern const VMStateInfo vmstate_info_gtree; +extern const VMStateInfo vmstate_info_qlist; =20 #define type_check_2darray(t1,t2,n,m) ((t1(*)[n][m])0 - (t2*)0) /* @@ -796,6 +797,26 @@ extern const VMStateInfo vmstate_info_gtree; .offset =3D offsetof(_state, _field), = \ } =20 +/* + * For migrating a QLIST + * Target QLIST needs be properly initialized. + * _type: type of QLIST element + * _next: name of QLIST_ENTRY entry field in QLIST element + * _vmsd: VMSD for QLIST element + * size: size of QLIST element + * start: offset of QLIST_ENTRY in QTAILQ element + */ +#define VMSTATE_QLIST_V(_field, _state, _version, _vmsd, _type, _next) \ +{ \ + .name =3D (stringify(_field)), = \ + .version_id =3D (_version), = \ + .vmsd =3D &(_vmsd), = \ + .size =3D sizeof(_type), = \ + .info =3D &vmstate_info_qlist, = \ + .offset =3D offsetof(_state, _field), = \ + .start =3D offsetof(_type, _next), = \ +} + /* _f : field name _f_n : num of elements field_name _n : num of elements diff --git a/include/qemu/queue.h b/include/qemu/queue.h index 4764d93ea3..4d4554a7ce 100644 --- a/include/qemu/queue.h +++ b/include/qemu/queue.h @@ -501,4 +501,43 @@ union { = \ QTAILQ_RAW_TQH_CIRC(head)->tql_prev =3D QTAILQ_RAW_TQE_CIRC(elm, e= ntry); \ } while (/*CONSTCOND*/0) =20 +#define QLIST_RAW_FIRST(head) = \ + field_at_offset(head, 0, void *) + +#define QLIST_RAW_NEXT(elm, entry) = \ + field_at_offset(elm, entry, void *) + +#define QLIST_RAW_PREVIOUS(elm, entry) = \ + field_at_offset(elm, entry + sizeof(void *), void *) + +#define QLIST_RAW_FOREACH(elm, head, entry) = \ + for ((elm) =3D *QLIST_RAW_FIRST(head); = \ + (elm); = \ + (elm) =3D *QLIST_RAW_NEXT(elm, entry)) + +#define QLIST_RAW_INSERT_HEAD(head, elm, entry) do { = \ + void *first =3D *QLIST_RAW_FIRST(head); = \ + *QLIST_RAW_FIRST(head) =3D elm; = \ + *QLIST_RAW_PREVIOUS(elm, entry) =3D QLIST_RAW_FIRST(head); = \ + if (first) { = \ + *QLIST_RAW_NEXT(elm, entry) =3D first; = \ + *QLIST_RAW_PREVIOUS(first, entry) =3D QLIST_RAW_NEXT(elm, entr= y); \ + } else { = \ + *QLIST_RAW_NEXT(elm, entry) =3D NULL; = \ + } = \ +} while (0) + +#define QLIST_RAW_REVERSE(head, elm, entry) do { = \ + void *iter =3D *QLIST_RAW_FIRST(head), *prev =3D NULL, *next; = \ + while (iter) { = \ + next =3D *QLIST_RAW_NEXT(iter, entry); = \ + *QLIST_RAW_PREVIOUS(iter, entry) =3D QLIST_RAW_NEXT(next, entr= y); \ + *QLIST_RAW_NEXT(iter, entry) =3D prev; = \ + prev =3D iter; = \ + iter =3D next; = \ + } = \ + *QLIST_RAW_FIRST(head) =3D prev; = \ + *QLIST_RAW_PREVIOUS(prev, entry) =3D QLIST_RAW_FIRST(head); = \ +} while (0) + #endif /* QEMU_SYS_QUEUE_H */ diff --git a/migration/trace-events b/migration/trace-events index 6dee7b5389..e0a33cffca 100644 --- a/migration/trace-events +++ b/migration/trace-events @@ -76,6 +76,11 @@ get_gtree_end(const char *field_name, const char *key_vm= sd_name, const char *val put_gtree(const char *field_name, const char *key_vmsd_name, const char *v= al_vmsd_name, uint32_t nnodes) "%s(%s/%s) nnodes=3D%d" put_gtree_end(const char *field_name, const char *key_vmsd_name, const cha= r *val_vmsd_name, int ret) "%s(%s/%s) %d" =20 +get_qlist(const char *field_name, const char *vmsd_name, int version_id) "= %s(%s v%d)" +get_qlist_end(const char *field_name, const char *vmsd_name) "%s(%s)" +put_qlist(const char *field_name, const char *vmsd_name, int version_id) "= %s(%s v%d)" +put_qlist_end(const char *field_name, const char *vmsd_name) "%s(%s)" + # qemu-file.c qemu_file_fclose(void) "" =20 diff --git a/migration/vmstate-types.c b/migration/vmstate-types.c index 7236cf92bc..1eee36773a 100644 --- a/migration/vmstate-types.c +++ b/migration/vmstate-types.c @@ -843,3 +843,73 @@ const VMStateInfo vmstate_info_gtree =3D { .get =3D get_gtree, .put =3D put_gtree, }; + +static int put_qlist(QEMUFile *f, void *pv, size_t unused_size, + const VMStateField *field, QJSON *vmdesc) +{ + const VMStateDescription *vmsd =3D field->vmsd; + /* offset of the QTAILQ entry in a QTAILQ element*/ + size_t entry_offset =3D field->start; + void *elm; + int ret; + + trace_put_qlist(field->name, vmsd->name, vmsd->version_id); + QLIST_RAW_FOREACH(elm, pv, entry_offset) { + qemu_put_byte(f, true); + ret =3D vmstate_save_state(f, vmsd, elm, vmdesc); + if (ret) { + error_report("%s: failed to save %s (%d)", field->name, + vmsd->name, ret); + return ret; + } + } + qemu_put_byte(f, false); + trace_put_qlist_end(field->name, vmsd->name); + + return 0; +} + +static int get_qlist(QEMUFile *f, void *pv, size_t unused_size, + const VMStateField *field) +{ + int ret =3D 0; + const VMStateDescription *vmsd =3D field->vmsd; + /* size of a QLIST element */ + size_t size =3D field->size; + /* offset of the QLIST entry in a QLIST element */ + size_t entry_offset =3D field->start; + int version_id =3D field->version_id; + void *elm; + + trace_get_qlist(field->name, vmsd->name, vmsd->version_id); + if (version_id > vmsd->version_id) { + error_report("%s %s", vmsd->name, "too new"); + return -EINVAL; + } + if (version_id < vmsd->minimum_version_id) { + error_report("%s %s", vmsd->name, "too old"); + return -EINVAL; + } + + while (qemu_get_byte(f)) { + elm =3D g_malloc(size); + ret =3D vmstate_load_state(f, vmsd, elm, version_id); + if (ret) { + error_report("%s: failed to load %s (%d)", field->name, + vmsd->name, ret); + g_free(elm); + return ret; + } + QLIST_RAW_INSERT_HEAD(pv, elm, entry_offset); + } + QLIST_RAW_REVERSE(pv, elm, entry_offset); + trace_get_qlist_end(field->name, vmsd->name); + + return ret; +} + +const VMStateInfo vmstate_info_qlist =3D { + .name =3D "qlist", + .get =3D get_qlist, + .put =3D put_qlist, +}; diff --git a/tests/test-vmstate.c b/tests/test-vmstate.c index 1e5be1d4ff..9660f932b9 100644 --- a/tests/test-vmstate.c +++ b/tests/test-vmstate.c @@ -926,6 +926,28 @@ static const VMStateDescription vmstate_domain =3D { } }; =20 +/* test QLIST Migration */ + +typedef struct TestQListElement { + uint32_t id; + QLIST_ENTRY(TestQListElement) next; +} TestQListElement; + +typedef struct TestQListContainer { + uint32_t id; + QLIST_HEAD(, TestQListElement) list; +} TestQListContainer; + +static const VMStateDescription vmstate_qlist_element =3D { + .name =3D "test/queue list", + .version_id =3D 1, + .minimum_version_id =3D 1, + .fields =3D (VMStateField[]) { + VMSTATE_UINT32(id, TestQListElement), + VMSTATE_END_OF_LIST() + } +}; + static const VMStateDescription vmstate_iommu =3D { .name =3D "iommu", .version_id =3D 1, @@ -939,6 +961,18 @@ static const VMStateDescription vmstate_iommu =3D { } }; =20 +static const VMStateDescription vmstate_container =3D { + .name =3D "test/container/qlist", + .version_id =3D 1, + .minimum_version_id =3D 1, + .fields =3D (VMStateField[]) { + VMSTATE_UINT32(id, TestQListContainer), + VMSTATE_QLIST_V(list, TestQListContainer, 1, vmstate_qlist_element, + TestQListElement, next), + VMSTATE_END_OF_LIST() + } +}; + uint8_t first_domain_dump[] =3D { /* id */ 0x00, 0x0, 0x0, 0x6, @@ -1229,6 +1263,140 @@ static void test_gtree_load_iommu(void) qemu_fclose(fload); } =20 +static uint8_t qlist_dump[] =3D { + 0x00, 0x00, 0x00, 0x01, /* container id */ + 0x1, /* start of a */ + 0x00, 0x00, 0x00, 0x0a, + 0x1, /* start of b */ + 0x00, 0x00, 0x0b, 0x00, + 0x1, /* start of c */ + 0x00, 0x0c, 0x00, 0x00, + 0x1, /* start of d */ + 0x0d, 0x00, 0x00, 0x00, + 0x0, /* end of list */ + QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */ +}; + +static TestQListContainer *alloc_container(void) +{ + TestQListElement *a =3D g_malloc(sizeof(TestQListElement)); + TestQListElement *b =3D g_malloc(sizeof(TestQListElement)); + TestQListElement *c =3D g_malloc(sizeof(TestQListElement)); + TestQListElement *d =3D g_malloc(sizeof(TestQListElement)); + TestQListContainer *container =3D g_malloc(sizeof(TestQListContainer)); + + a->id =3D 0x0a; + b->id =3D 0x0b00; + c->id =3D 0xc0000; + d->id =3D 0xd000000; + container->id =3D 1; + + QLIST_INIT(&container->list); + QLIST_INSERT_HEAD(&container->list, d, next); + QLIST_INSERT_HEAD(&container->list, c, next); + QLIST_INSERT_HEAD(&container->list, b, next); + QLIST_INSERT_HEAD(&container->list, a, next); + return container; +} + +static void free_container(TestQListContainer *container) +{ + TestQListElement *iter, *tmp; + + QLIST_FOREACH_SAFE(iter, &container->list, next, tmp) { + QLIST_REMOVE(iter, next); + g_free(iter); + } + g_free(container); +} + +static void compare_containers(TestQListContainer *c1, TestQListContainer = *c2) +{ + TestQListElement *first_item_c1, *first_item_c2; + + while (!QLIST_EMPTY(&c1->list)) { + first_item_c1 =3D QLIST_FIRST(&c1->list); + first_item_c2 =3D QLIST_FIRST(&c2->list); + assert(first_item_c2); + assert(first_item_c1->id =3D=3D first_item_c2->id); + QLIST_REMOVE(first_item_c1, next); + QLIST_REMOVE(first_item_c2, next); + g_free(first_item_c1); + g_free(first_item_c2); + } + assert(QLIST_EMPTY(&c2->list)); +} + +/* + * Check the prev & next fields are correct by doing list + * manipulations on the container. We will do that for both + * the source and the destination containers + */ +static void manipulate_container(TestQListContainer *c) +{ + TestQListElement *prev, *iter =3D QLIST_FIRST(&c->list); + TestQListElement *elem; + + elem =3D g_malloc(sizeof(TestQListElement)); + elem->id =3D 0x12; + QLIST_INSERT_AFTER(iter, elem, next); + + elem =3D g_malloc(sizeof(TestQListElement)); + elem->id =3D 0x13; + QLIST_INSERT_HEAD(&c->list, elem, next); + + while (iter) { + prev =3D iter; + iter =3D QLIST_NEXT(iter, next); + } + + elem =3D g_malloc(sizeof(TestQListElement)); + elem->id =3D 0x14; + QLIST_INSERT_BEFORE(prev, elem, next); + + elem =3D g_malloc(sizeof(TestQListElement)); + elem->id =3D 0x15; + QLIST_INSERT_AFTER(prev, elem, next); + + QLIST_REMOVE(prev, next); + g_free(prev); +} + +static void test_save_qlist(void) +{ + TestQListContainer *container =3D alloc_container(); + + save_vmstate(&vmstate_container, container); + compare_vmstate(qlist_dump, sizeof(qlist_dump)); + free_container(container); +} + +static void test_load_qlist(void) +{ + QEMUFile *fsave, *fload; + TestQListContainer *orig_container =3D alloc_container(); + TestQListContainer *dest_container =3D g_malloc0(sizeof(TestQListConta= iner)); + char eof; + + QLIST_INIT(&dest_container->list); + + fsave =3D open_test_file(true); + qemu_put_buffer(fsave, qlist_dump, sizeof(qlist_dump)); + g_assert(!qemu_file_get_error(fsave)); + qemu_fclose(fsave); + + fload =3D open_test_file(false); + vmstate_load_state(fload, &vmstate_container, dest_container, 1); + eof =3D qemu_get_byte(fload); + g_assert(!qemu_file_get_error(fload)); + g_assert_cmpint(eof, =3D=3D, QEMU_VM_EOF); + manipulate_container(orig_container); + manipulate_container(dest_container); + compare_containers(orig_container, dest_container); + free_container(orig_container); + free_container(dest_container); +} + typedef struct TmpTestStruct { TestStruct *parent; int64_t diff; @@ -1353,6 +1521,8 @@ int main(int argc, char **argv) g_test_add_func("/vmstate/gtree/load/loaddomain", test_gtree_load_doma= in); g_test_add_func("/vmstate/gtree/save/saveiommu", test_gtree_save_iommu= ); g_test_add_func("/vmstate/gtree/load/loadiommu", test_gtree_load_iommu= ); + g_test_add_func("/vmstate/qlist/save/saveqlist", test_save_qlist); + g_test_add_func("/vmstate/qlist/load/loadqlist", test_load_qlist); g_test_add_func("/vmstate/tmp_struct", test_tmp_struct); g_test_run(); =20 --=20 2.20.1 From nobody Sun May 12 23:37:35 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1574448475; cv=none; d=zohomail.com; s=zohoarc; b=fax2j6EsN3fVxkhmQDwqXQzsbQwINuMy2WU+/3Jhy8plwszrZgQIMlw1Kkk2R0sAtRLXqbrVBUajRa5D4D6rI1kfpwvTtDIPpbec90XUH9CwXdsyi/wjmJQS4qn+dZVxWlK225ULFJdlFBJtOXrOOKuymbGV0Ij6HYN6HBAZLyE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574448475; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=q+AWoigxA/f1SVYKE2NPjNu5i9smsXgQfU76sXFUZF0=; b=d0zQRi/BqFWcxpX6fvTbjPEZiEHYXf8UZYF9WpXDT3UIW8604WsRZcC+xlgGhzDLuTPhO32Qxk+aZfH4KMB+AO/N5YuOJkliNJqSFMaFNtmdWEINj641D1HCUhokavCSOJnP8v74aVRy/rE6niUTocGRbLT1wVPzBDyy+Pv/UDA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574448475529692.1697629498592; Fri, 22 Nov 2019 10:47:55 -0800 (PST) Received: from localhost ([::1]:54258 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDyC-0000zA-AP for importer@patchew.org; Fri, 22 Nov 2019 13:47:52 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39656) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDhP-000864-Fl for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:30:34 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iYDhM-0008Lb-IY for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:30:31 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:21474 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iYDhM-0008L8-Db for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:30:28 -0500 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-71-paYufjMGPzOiWtMdwnW8yQ-1; Fri, 22 Nov 2019 13:30:26 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 15EDA5F9; Fri, 22 Nov 2019 18:30:23 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-37.ams2.redhat.com [10.36.116.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id F011B6E712; Fri, 22 Nov 2019 18:30:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574447428; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=q+AWoigxA/f1SVYKE2NPjNu5i9smsXgQfU76sXFUZF0=; b=BJ9NVA8CkV3W7mDTvtjSaisRfkQIKjpYKeHDmSC76quBQtPV3xEBJbChu5e9v+GnuuBxP5 QgoyncyCfgzX74SE/g/e56gSXZ6QHPhjp7TqGChuNVppmScxdflJO1yEGUTZbUOdm5aPOI GORiU2zAc0AlLUKllDi2V7sK0EYZ7y8= From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org, mst@redhat.com, armbru@redhat.com, jean-philippe.brucker@arm.com, bharatb.linux@gmail.com, yang.zhong@intel.com, dgilbert@redhat.com, quintela@redhat.com Subject: [PATCH for-5.0 v11 02/20] virtio-iommu: Add skeleton Date: Fri, 22 Nov 2019 19:29:25 +0100 Message-Id: <20191122182943.4656-3-eric.auger@redhat.com> In-Reply-To: <20191122182943.4656-1-eric.auger@redhat.com> References: <20191122182943.4656-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: paYufjMGPzOiWtMdwnW8yQ-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, peterx@redhat.com, tnowicki@marvell.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This patchs adds the skeleton for the virtio-iommu device. Signed-off-by: Eric Auger --- v9 -> v10: - mutex initialized here - initialize tail - included hw/qdev-properties.h - removed g_memdup - removed s->config.domain_range.start =3D 0; v9 -> v10: - expose VIRTIO_IOMMU_F_MMIO feature - s/domain_bits/domain_range struct - change error codes - enforce unmigratable - Kconfig v7 -> v8: - expose VIRTIO_IOMMU_F_BYPASS and VIRTIO_F_VERSION_1 features - set_config dummy implementation + tracing - add trace in get_features - set the features on realize() and store the acked ones - remove inclusion of linux/virtio_iommu.h v6 -> v7: - removed qapi-event.h include - add primary_bus and associated property v4 -> v5: - use the new v0.5 terminology (domain, endpoint) - add the event virtqueue v3 -> v4: - use page_size_mask instead of page_sizes - added set_features() - added some traces (reset, set_status, set_features) - empty virtio_iommu_set_config() as the driver MUST NOT write to device configuration fields - add get_config trace v2 -> v3: - rebase on 2.10-rc0, ie. use IOMMUMemoryRegion and remove iommu_ops. - advertise VIRTIO_IOMMU_F_MAP_UNMAP feature - page_sizes set to TARGET_PAGE_SIZE Conflicts: hw/virtio/trace-events --- hw/virtio/Kconfig | 5 + hw/virtio/Makefile.objs | 1 + hw/virtio/trace-events | 8 + hw/virtio/virtio-iommu.c | 274 +++++++++++++++++++++++++++++++ include/hw/virtio/virtio-iommu.h | 62 +++++++ 5 files changed, 350 insertions(+) create mode 100644 hw/virtio/virtio-iommu.c create mode 100644 include/hw/virtio/virtio-iommu.h diff --git a/hw/virtio/Kconfig b/hw/virtio/Kconfig index 3724ff8bac..a30107b439 100644 --- a/hw/virtio/Kconfig +++ b/hw/virtio/Kconfig @@ -6,6 +6,11 @@ config VIRTIO_RNG default y depends on VIRTIO =20 +config VIRTIO_IOMMU + bool + default y + depends on VIRTIO + config VIRTIO_PCI bool default y if PCI_DEVICES diff --git a/hw/virtio/Makefile.objs b/hw/virtio/Makefile.objs index e2f70fbb89..f68ac14a90 100644 --- a/hw/virtio/Makefile.objs +++ b/hw/virtio/Makefile.objs @@ -16,6 +16,7 @@ obj-$(call land,$(CONFIG_VIRTIO_CRYPTO),$(CONFIG_VIRTIO_P= CI)) +=3D virtio-crypto-p obj-$(CONFIG_VIRTIO_PMEM) +=3D virtio-pmem.o common-obj-$(call land,$(CONFIG_VIRTIO_PMEM),$(CONFIG_VIRTIO_PCI)) +=3D vi= rtio-pmem-pci.o obj-$(call land,$(CONFIG_VHOST_USER_FS),$(CONFIG_VIRTIO_PCI)) +=3D vhost-u= ser-fs-pci.o +obj-$(CONFIG_VIRTIO_IOMMU) +=3D virtio-iommu.o obj-$(CONFIG_VHOST_VSOCK) +=3D vhost-vsock.o =20 ifeq ($(CONFIG_VIRTIO_PCI),y) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index e28ba48da6..f7dac39213 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -53,3 +53,11 @@ virtio_mmio_write_offset(uint64_t offset, uint64_t value= ) "virtio_mmio_write off virtio_mmio_guest_page(uint64_t size, int shift) "guest page size 0x%" PRI= x64 " shift %d" virtio_mmio_queue_write(uint64_t value, int max_size) "mmio_queue write 0x= %" PRIx64 " max %d" virtio_mmio_setting_irq(int level) "virtio_mmio setting IRQ %d" + +# hw/virtio/virtio-iommu.c +virtio_iommu_device_reset(void) "reset!" +virtio_iommu_get_features(uint64_t features) "device supports features=3D0= x%"PRIx64 +virtio_iommu_set_features(uint64_t features) "features accepted by the dri= ver =3D0x%"PRIx64 +virtio_iommu_device_status(uint8_t status) "driver status =3D %d" +virtio_iommu_get_config(uint64_t page_size_mask, uint64_t start, uint64_t = end, uint32_t domain_range, uint32_t probe_size) "page_size_mask=3D0x%"PRIx= 64" start=3D0x%"PRIx64" end=3D0x%"PRIx64" domain_range=3D%d probe_size=3D0x= %x" +virtio_iommu_set_config(uint64_t page_size_mask, uint64_t start, uint64_t = end, uint32_t domain_range, uint32_t probe_size) "page_size_mask=3D0x%"PRIx= 64" start=3D0x%"PRIx64" end=3D0x%"PRIx64" domain_bits=3D%d probe_size=3D0x%= x" diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c new file mode 100644 index 0000000000..7b25db3713 --- /dev/null +++ b/hw/virtio/virtio-iommu.c @@ -0,0 +1,274 @@ +/* + * virtio-iommu device + * + * Copyright (c) 2017 Red Hat, Inc. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2 or later, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License f= or + * more details. + * + * You should have received a copy of the GNU General Public License along= with + * this program. If not, see . + * + */ + +#include "qemu/osdep.h" +#include "qemu/iov.h" +#include "qemu-common.h" +#include "hw/qdev-properties.h" +#include "hw/virtio/virtio.h" +#include "sysemu/kvm.h" +#include "trace.h" + +#include "standard-headers/linux/virtio_ids.h" + +#include "hw/virtio/virtio-bus.h" +#include "hw/virtio/virtio-access.h" +#include "hw/virtio/virtio-iommu.h" + +/* Max size */ +#define VIOMMU_DEFAULT_QUEUE_SIZE 256 + +static int virtio_iommu_handle_attach(VirtIOIOMMU *s, + struct iovec *iov, + unsigned int iov_cnt) +{ + return VIRTIO_IOMMU_S_UNSUPP; +} +static int virtio_iommu_handle_detach(VirtIOIOMMU *s, + struct iovec *iov, + unsigned int iov_cnt) +{ + return VIRTIO_IOMMU_S_UNSUPP; +} +static int virtio_iommu_handle_map(VirtIOIOMMU *s, + struct iovec *iov, + unsigned int iov_cnt) +{ + return VIRTIO_IOMMU_S_UNSUPP; +} +static int virtio_iommu_handle_unmap(VirtIOIOMMU *s, + struct iovec *iov, + unsigned int iov_cnt) +{ + return VIRTIO_IOMMU_S_UNSUPP; +} + +static void virtio_iommu_handle_command(VirtIODevice *vdev, VirtQueue *vq) +{ + VirtIOIOMMU *s =3D VIRTIO_IOMMU(vdev); + struct virtio_iommu_req_head head; + struct virtio_iommu_req_tail tail =3D {}; + VirtQueueElement *elem; + unsigned int iov_cnt; + struct iovec *iov; + size_t sz; + + for (;;) { + elem =3D virtqueue_pop(vq, sizeof(VirtQueueElement)); + if (!elem) { + return; + } + + if (iov_size(elem->in_sg, elem->in_num) < sizeof(tail) || + iov_size(elem->out_sg, elem->out_num) < sizeof(head)) { + virtio_error(vdev, "virtio-iommu bad head/tail size"); + virtqueue_detach_element(vq, elem, 0); + g_free(elem); + break; + } + + iov_cnt =3D elem->out_num; + iov =3D elem->out_sg; + sz =3D iov_to_buf(iov, iov_cnt, 0, &head, sizeof(head)); + if (unlikely(sz !=3D sizeof(head))) { + tail.status =3D VIRTIO_IOMMU_S_DEVERR; + goto out; + } + qemu_mutex_lock(&s->mutex); + switch (head.type) { + case VIRTIO_IOMMU_T_ATTACH: + tail.status =3D virtio_iommu_handle_attach(s, iov, iov_cnt); + break; + case VIRTIO_IOMMU_T_DETACH: + tail.status =3D virtio_iommu_handle_detach(s, iov, iov_cnt); + break; + case VIRTIO_IOMMU_T_MAP: + tail.status =3D virtio_iommu_handle_map(s, iov, iov_cnt); + break; + case VIRTIO_IOMMU_T_UNMAP: + tail.status =3D virtio_iommu_handle_unmap(s, iov, iov_cnt); + break; + default: + tail.status =3D VIRTIO_IOMMU_S_UNSUPP; + } + qemu_mutex_unlock(&s->mutex); + +out: + sz =3D iov_from_buf(elem->in_sg, elem->in_num, 0, + &tail, sizeof(tail)); + assert(sz =3D=3D sizeof(tail)); + + virtqueue_push(vq, elem, sizeof(tail)); + virtio_notify(vdev, vq); + g_free(elem); + } +} + +static void virtio_iommu_get_config(VirtIODevice *vdev, uint8_t *config_da= ta) +{ + VirtIOIOMMU *dev =3D VIRTIO_IOMMU(vdev); + struct virtio_iommu_config *config =3D &dev->config; + + trace_virtio_iommu_get_config(config->page_size_mask, + config->input_range.start, + config->input_range.end, + config->domain_range.end, + config->probe_size); + memcpy(config_data, &dev->config, sizeof(struct virtio_iommu_config)); +} + +static void virtio_iommu_set_config(VirtIODevice *vdev, + const uint8_t *config_data) +{ + struct virtio_iommu_config config; + + memcpy(&config, config_data, sizeof(struct virtio_iommu_config)); + trace_virtio_iommu_set_config(config.page_size_mask, + config.input_range.start, + config.input_range.end, + config.domain_range.end, + config.probe_size); +} + +static uint64_t virtio_iommu_get_features(VirtIODevice *vdev, uint64_t f, + Error **errp) +{ + VirtIOIOMMU *dev =3D VIRTIO_IOMMU(vdev); + + f |=3D dev->features; + trace_virtio_iommu_get_features(f); + return f; +} + +static void virtio_iommu_set_features(VirtIODevice *vdev, uint64_t val) +{ + VirtIOIOMMU *dev =3D VIRTIO_IOMMU(vdev); + + dev->acked_features =3D val; + trace_virtio_iommu_set_features(dev->acked_features); +} + +/* + * Migration is not yet supported: most of the state consists + * of balanced binary trees which are not yet ready for getting + * migrated + */ +static const VMStateDescription vmstate_virtio_iommu_device =3D { + .name =3D "virtio-iommu-device", + .unmigratable =3D 1, +}; + +static void virtio_iommu_device_realize(DeviceState *dev, Error **errp) +{ + VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); + VirtIOIOMMU *s =3D VIRTIO_IOMMU(dev); + + virtio_init(vdev, "virtio-iommu", VIRTIO_ID_IOMMU, + sizeof(struct virtio_iommu_config)); + + s->req_vq =3D virtio_add_queue(vdev, VIOMMU_DEFAULT_QUEUE_SIZE, + virtio_iommu_handle_command); + s->event_vq =3D virtio_add_queue(vdev, VIOMMU_DEFAULT_QUEUE_SIZE, NULL= ); + + s->config.page_size_mask =3D TARGET_PAGE_MASK; + s->config.input_range.end =3D -1UL; + s->config.domain_range.end =3D 32; + + virtio_add_feature(&s->features, VIRTIO_RING_F_EVENT_IDX); + virtio_add_feature(&s->features, VIRTIO_RING_F_INDIRECT_DESC); + virtio_add_feature(&s->features, VIRTIO_F_VERSION_1); + virtio_add_feature(&s->features, VIRTIO_IOMMU_F_INPUT_RANGE); + virtio_add_feature(&s->features, VIRTIO_IOMMU_F_DOMAIN_RANGE); + virtio_add_feature(&s->features, VIRTIO_IOMMU_F_MAP_UNMAP); + virtio_add_feature(&s->features, VIRTIO_IOMMU_F_BYPASS); + virtio_add_feature(&s->features, VIRTIO_IOMMU_F_MMIO); + + qemu_mutex_init(&s->mutex); +} + +static void virtio_iommu_device_unrealize(DeviceState *dev, Error **errp) +{ + VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); + + virtio_cleanup(vdev); +} + +static void virtio_iommu_device_reset(VirtIODevice *vdev) +{ + trace_virtio_iommu_device_reset(); +} + +static void virtio_iommu_set_status(VirtIODevice *vdev, uint8_t status) +{ + trace_virtio_iommu_device_status(status); +} + +static void virtio_iommu_instance_init(Object *obj) +{ +} + +static const VMStateDescription vmstate_virtio_iommu =3D { + .name =3D "virtio-iommu", + .minimum_version_id =3D 1, + .version_id =3D 1, + .fields =3D (VMStateField[]) { + VMSTATE_VIRTIO_DEVICE, + VMSTATE_END_OF_LIST() + }, +}; + +static Property virtio_iommu_properties[] =3D { + DEFINE_PROP_LINK("primary-bus", VirtIOIOMMU, primary_bus, "PCI", PCIBu= s *), + DEFINE_PROP_END_OF_LIST(), +}; + +static void virtio_iommu_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + VirtioDeviceClass *vdc =3D VIRTIO_DEVICE_CLASS(klass); + + dc->props =3D virtio_iommu_properties; + dc->vmsd =3D &vmstate_virtio_iommu; + + set_bit(DEVICE_CATEGORY_MISC, dc->categories); + vdc->realize =3D virtio_iommu_device_realize; + vdc->unrealize =3D virtio_iommu_device_unrealize; + vdc->reset =3D virtio_iommu_device_reset; + vdc->get_config =3D virtio_iommu_get_config; + vdc->set_config =3D virtio_iommu_set_config; + vdc->get_features =3D virtio_iommu_get_features; + vdc->set_features =3D virtio_iommu_set_features; + vdc->set_status =3D virtio_iommu_set_status; + vdc->vmsd =3D &vmstate_virtio_iommu_device; +} + +static const TypeInfo virtio_iommu_info =3D { + .name =3D TYPE_VIRTIO_IOMMU, + .parent =3D TYPE_VIRTIO_DEVICE, + .instance_size =3D sizeof(VirtIOIOMMU), + .instance_init =3D virtio_iommu_instance_init, + .class_init =3D virtio_iommu_class_init, +}; + +static void virtio_register_types(void) +{ + type_register_static(&virtio_iommu_info); +} + +type_init(virtio_register_types) diff --git a/include/hw/virtio/virtio-iommu.h b/include/hw/virtio/virtio-io= mmu.h new file mode 100644 index 0000000000..4d47b6abeb --- /dev/null +++ b/include/hw/virtio/virtio-iommu.h @@ -0,0 +1,62 @@ +/* + * virtio-iommu device + * + * Copyright (c) 2017 Red Hat, Inc. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2 or later, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License f= or + * more details. + * + * You should have received a copy of the GNU General Public License along= with + * this program. If not, see . + * + */ + +#ifndef QEMU_VIRTIO_IOMMU_H +#define QEMU_VIRTIO_IOMMU_H + +#include "standard-headers/linux/virtio_iommu.h" +#include "hw/virtio/virtio.h" +#include "hw/pci/pci.h" + +#define TYPE_VIRTIO_IOMMU "virtio-iommu-device" +#define VIRTIO_IOMMU(obj) \ + OBJECT_CHECK(VirtIOIOMMU, (obj), TYPE_VIRTIO_IOMMU) + +#define IOMMU_PCI_BUS_MAX 256 +#define IOMMU_PCI_DEVFN_MAX 256 + +typedef struct IOMMUDevice { + void *viommu; + PCIBus *bus; + int devfn; + IOMMUMemoryRegion iommu_mr; + AddressSpace as; +} IOMMUDevice; + +typedef struct IOMMUPciBus { + PCIBus *bus; + IOMMUDevice *pbdev[0]; /* Parent array is sparse, so dynamically allo= c */ +} IOMMUPciBus; + +typedef struct VirtIOIOMMU { + VirtIODevice parent_obj; + VirtQueue *req_vq; + VirtQueue *event_vq; + struct virtio_iommu_config config; + uint64_t features; + uint64_t acked_features; + GHashTable *as_by_busptr; + IOMMUPciBus *as_by_bus_num[IOMMU_PCI_BUS_MAX]; + PCIBus *primary_bus; + GTree *domains; + QemuMutex mutex; + GTree *endpoints; +} VirtIOIOMMU; + +#endif --=20 2.20.1 From nobody Sun May 12 23:37:35 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1574447752; cv=none; d=zohomail.com; s=zohoarc; b=k4x47Ow4j2Ko07QXt0JU8dEn6Yz/WifXOS6JfRRLsvJZToqCvzVIuXOS58/tJ11PI92T3uuDgN+xtq9nsY1YQtGQgAsDHdJrwWTSI4mcYTRxE3m8/Hk/TTZGfLf6COaRcasSrhbdDC3GiWB341znfHsVmqRQupUlfkTBHwCSJIE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574447752; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=8hhhgWsyNc3pla/vGmQZILIxsZVrcOAjmqOxlbdFndM=; b=HgjqnAdno/gDwE7nbl+juONGAZOS+Jxb2QTeqA05AC6h5sUNbnWZEwFuVY/QvramlWEVrtQyzpvQ89XF295AQYnIWDQeURQJ06WdcpijGicRvLlu4XRHz59Rw91Tc5lb0XIxsd1JSibWy1c4+ZjsYuE2gkcWjybyALeDfK8dzf8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574447752091261.884167465818; Fri, 22 Nov 2019 10:35:52 -0800 (PST) Received: from localhost ([::1]:54114 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDmX-0004r3-S9 for importer@patchew.org; Fri, 22 Nov 2019 13:35:50 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39662) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDhP-000868-Nf for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:30:34 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iYDhN-0008NY-Sp for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:30:31 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:49315 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iYDhN-0008Mz-OC for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:30:29 -0500 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-38-xA-7bsoSNDedgquKPaJDHQ-1; Fri, 22 Nov 2019 13:30:28 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CB12B2F34; Fri, 22 Nov 2019 18:30:26 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-37.ams2.redhat.com [10.36.116.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6B8346E71C; Fri, 22 Nov 2019 18:30:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574447429; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8hhhgWsyNc3pla/vGmQZILIxsZVrcOAjmqOxlbdFndM=; b=T30Ea0E7HqtkIRisBXCLVmac7edfsyDVylsQg9/Jh3aTQ9Gsxs43oQQOJO65/uk1NwIZRS xPudb1wbYbPXpFOqUJ08G+HfhPSqIngVAz9thf6LlVSakAziHNjkLFfy8gs4jCDPK1dv8d CDUrlNznaQXNEuFS5Gg7/6b7aoora0E= From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org, mst@redhat.com, armbru@redhat.com, jean-philippe.brucker@arm.com, bharatb.linux@gmail.com, yang.zhong@intel.com, dgilbert@redhat.com, quintela@redhat.com Subject: [PATCH for-5.0 v11 03/20] virtio-iommu: Decode the command payload Date: Fri, 22 Nov 2019 19:29:26 +0100 Message-Id: <20191122182943.4656-4-eric.auger@redhat.com> In-Reply-To: <20191122182943.4656-1-eric.auger@redhat.com> References: <20191122182943.4656-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: xA-7bsoSNDedgquKPaJDHQ-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 205.139.110.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, peterx@redhat.com, tnowicki@marvell.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This patch adds the command payload decoding and introduces the functions that will do the actual command handling. Those functions are not yet implemented. Signed-off-by: Eric Auger Reviewed-by: Jean-Philippe Brucker Reviewed-by: Peter Xu --- v10 -> v11: - use a macro for handle command functions v9 -> v10: - make virtio_iommu_handle_* more compact and remove get_payload_size v7 -> v8: - handle new domain parameter in detach - remove reserved checks v5 -> v6: - change map/unmap semantics (remove size) v4 -> v5: - adopt new v0.5 terminology v3 -> v4: - no flags field anymore in struct virtio_iommu_req_unmap - test reserved on attach/detach, change trace proto - rebase on v2.10.0. --- hw/virtio/trace-events | 4 +++ hw/virtio/virtio-iommu.c | 76 +++++++++++++++++++++++++++++++++------- 2 files changed, 68 insertions(+), 12 deletions(-) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index f7dac39213..c7276116e7 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -61,3 +61,7 @@ virtio_iommu_set_features(uint64_t features) "features ac= cepted by the driver =3D0 virtio_iommu_device_status(uint8_t status) "driver status =3D %d" virtio_iommu_get_config(uint64_t page_size_mask, uint64_t start, uint64_t = end, uint32_t domain_range, uint32_t probe_size) "page_size_mask=3D0x%"PRIx= 64" start=3D0x%"PRIx64" end=3D0x%"PRIx64" domain_range=3D%d probe_size=3D0x= %x" virtio_iommu_set_config(uint64_t page_size_mask, uint64_t start, uint64_t = end, uint32_t domain_range, uint32_t probe_size) "page_size_mask=3D0x%"PRIx= 64" start=3D0x%"PRIx64" end=3D0x%"PRIx64" domain_bits=3D%d probe_size=3D0x%= x" +virtio_iommu_attach(uint32_t domain_id, uint32_t ep_id) "domain=3D%d endpo= int=3D%d" +virtio_iommu_detach(uint32_t domain_id, uint32_t ep_id) "domain=3D%d endpo= int=3D%d" +virtio_iommu_map(uint32_t domain_id, uint64_t virt_start, uint64_t virt_en= d, uint64_t phys_start, uint32_t flags) "domain=3D%d virt_start=3D0x%"PRIx6= 4" virt_end=3D0x%"PRIx64 " phys_start=3D0x%"PRIx64" flags=3D%d" +virtio_iommu_unmap(uint32_t domain_id, uint64_t virt_start, uint64_t virt_= end) "domain=3D%d virt_start=3D0x%"PRIx64" virt_end=3D0x%"PRIx64 diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index 7b25db3713..afd6397ac9 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -34,31 +34,83 @@ /* Max size */ #define VIOMMU_DEFAULT_QUEUE_SIZE 256 =20 -static int virtio_iommu_handle_attach(VirtIOIOMMU *s, - struct iovec *iov, - unsigned int iov_cnt) +static int virtio_iommu_attach(VirtIOIOMMU *s, + struct virtio_iommu_req_attach *req) { + uint32_t domain_id =3D le32_to_cpu(req->domain); + uint32_t ep_id =3D le32_to_cpu(req->endpoint); + + trace_virtio_iommu_attach(domain_id, ep_id); + return VIRTIO_IOMMU_S_UNSUPP; } -static int virtio_iommu_handle_detach(VirtIOIOMMU *s, - struct iovec *iov, - unsigned int iov_cnt) + +static int virtio_iommu_detach(VirtIOIOMMU *s, + struct virtio_iommu_req_detach *req) { + uint32_t domain_id =3D le32_to_cpu(req->domain); + uint32_t ep_id =3D le32_to_cpu(req->endpoint); + + trace_virtio_iommu_detach(domain_id, ep_id); + return VIRTIO_IOMMU_S_UNSUPP; } -static int virtio_iommu_handle_map(VirtIOIOMMU *s, - struct iovec *iov, - unsigned int iov_cnt) + +static int virtio_iommu_map(VirtIOIOMMU *s, + struct virtio_iommu_req_map *req) { + uint32_t domain_id =3D le32_to_cpu(req->domain); + uint64_t phys_start =3D le64_to_cpu(req->phys_start); + uint64_t virt_start =3D le64_to_cpu(req->virt_start); + uint64_t virt_end =3D le64_to_cpu(req->virt_end); + uint32_t flags =3D le32_to_cpu(req->flags); + + trace_virtio_iommu_map(domain_id, virt_start, virt_end, phys_start, fl= ags); + return VIRTIO_IOMMU_S_UNSUPP; } -static int virtio_iommu_handle_unmap(VirtIOIOMMU *s, - struct iovec *iov, - unsigned int iov_cnt) + +static int virtio_iommu_unmap(VirtIOIOMMU *s, + struct virtio_iommu_req_unmap *req) { + uint32_t domain_id =3D le32_to_cpu(req->domain); + uint64_t virt_start =3D le64_to_cpu(req->virt_start); + uint64_t virt_end =3D le64_to_cpu(req->virt_end); + + trace_virtio_iommu_unmap(domain_id, virt_start, virt_end); + return VIRTIO_IOMMU_S_UNSUPP; } =20 +static int virtio_iommu_iov_to_req(struct iovec *iov, + unsigned int iov_cnt, + void *req, size_t req_sz) +{ + size_t sz, payload_sz =3D req_sz - sizeof(struct virtio_iommu_req_tail= ); + + sz =3D iov_to_buf(iov, iov_cnt, 0, req, payload_sz); + if (unlikely(sz !=3D payload_sz)) { + return VIRTIO_IOMMU_S_INVAL; + } + return 0; +} + +#define virtio_iommu_handle_req(__req) \ +static int virtio_iommu_handle_ ## __req(VirtIOIOMMU *s, \ + struct iovec *iov, \ + unsigned int iov_cnt) \ +{ \ + struct virtio_iommu_req_ ## __req req; \ + int ret =3D virtio_iommu_iov_to_req(iov, iov_cnt, &req, sizeof(req)); \ + \ + return ret ? ret : virtio_iommu_ ## __req(s, &req); \ +} + +virtio_iommu_handle_req(attach) +virtio_iommu_handle_req(detach) +virtio_iommu_handle_req(map) +virtio_iommu_handle_req(unmap) + static void virtio_iommu_handle_command(VirtIODevice *vdev, VirtQueue *vq) { VirtIOIOMMU *s =3D VIRTIO_IOMMU(vdev); --=20 2.20.1 From nobody Sun May 12 23:37:35 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1574447762; cv=none; d=zohomail.com; s=zohoarc; b=Z0HxO89UtOj8Kzs4x3j1vxB1Y7caNWlJv3VCBhF/x0AjJTQu598Y0NIQC9qVWifNT+4P98+igT3TkwK+lcNOCPh3hxE6dGcYET7e+MVZngDJu+9IKhrpIsDlFIPY4yPxYQkAWCq9yxvdFNmhyw8SvRnWA4J8LAHR1g1Mg4NhcOI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574447762; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=IYq9Kbn8tUyBJ5YbMtHCok3UyrIzqucJ1IOR+X4VnBk=; b=J+kcC1clEv+cMH5YHhF+zLc1iMJLP2wFRB4NDXgFxi7PNE2huSVh1s1O5FOsh1Z7Tf94e80ljuktp6Hx6BuQw4To6OSLpH1lj0JmMyQZWRA/HPq97GCEniHbZ+IUBjf7LjPEl0/4WMRTUnIUSr5Kut500hvvDrE0MixCigg3ApE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574447762197480.62342102289847; Fri, 22 Nov 2019 10:36:02 -0800 (PST) Received: from localhost ([::1]:54116 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDmi-00051z-Lc for importer@patchew.org; Fri, 22 Nov 2019 13:36:00 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39696) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDhU-00087W-AY for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:30:40 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iYDhS-0008Rw-AG for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:30:36 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:25320 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iYDhS-0008RN-5O for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:30:34 -0500 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-19-tMeRrcjlNSme2BkKYasAVw-1; Fri, 22 Nov 2019 13:30:32 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 83A70108C303; Fri, 22 Nov 2019 18:30:30 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-37.ams2.redhat.com [10.36.116.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id 273536E712; Fri, 22 Nov 2019 18:30:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574447433; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IYq9Kbn8tUyBJ5YbMtHCok3UyrIzqucJ1IOR+X4VnBk=; b=J+Xp0n4uIdbR+ujSr+Ekf1F8leYR/7nufOkxvC4O1yZt1fh8BT82Stnzham19x1nmY1Q22 iUFgCa/iUffmFqVijtr1pgSbIHFE8E1RLUFGXfbkne4xVrhSlnny//LHBNbM6m0aw6VXH5 j+3xASownj16pfVJLBWwE7EVITT5GI0= From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org, mst@redhat.com, armbru@redhat.com, jean-philippe.brucker@arm.com, bharatb.linux@gmail.com, yang.zhong@intel.com, dgilbert@redhat.com, quintela@redhat.com Subject: [PATCH for-5.0 v11 04/20] virtio-iommu: Add the iommu regions Date: Fri, 22 Nov 2019 19:29:27 +0100 Message-Id: <20191122182943.4656-5-eric.auger@redhat.com> In-Reply-To: <20191122182943.4656-1-eric.auger@redhat.com> References: <20191122182943.4656-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: tMeRrcjlNSme2BkKYasAVw-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, peterx@redhat.com, tnowicki@marvell.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This patch initializes the iommu memory regions so that PCIe end point transactions get translated. The translation function is not yet implemented though. Signed-off-by: Eric Auger Reviewed-by: Jean-Philippe Brucker Reviewed-by: Peter Xu --- v10 -> v11: - use g_hash_table_new_full for allocating as_by_busptr v9 -> v10: - remove pc/virt machine headers - virtio_iommu_find_add_as: mr_index introduced in that patch and name properly freed v6 -> v7: - use primary_bus - rebase on new translate proto featuring iommu_idx v5 -> v6: - include qapi/error.h - fix g_hash_table_lookup key in virtio_iommu_find_add_as v4 -> v5: - use PCI bus handle as a key - use get_primary_pci_bus() callback v3 -> v4: - add trace_virtio_iommu_init_iommu_mr v2 -> v3: - use IOMMUMemoryRegion - iommu mr name built with BDF - rename smmu_get_sid into virtio_iommu_get_sid and use PCI_BUILD_BDF --- hw/virtio/trace-events | 2 + hw/virtio/virtio-iommu.c | 92 ++++++++++++++++++++++++++++++++ include/hw/virtio/virtio-iommu.h | 2 + 3 files changed, 96 insertions(+) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index c7276116e7..b32169d56c 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -65,3 +65,5 @@ virtio_iommu_attach(uint32_t domain_id, uint32_t ep_id) "= domain=3D%d endpoint=3D%d" virtio_iommu_detach(uint32_t domain_id, uint32_t ep_id) "domain=3D%d endpo= int=3D%d" virtio_iommu_map(uint32_t domain_id, uint64_t virt_start, uint64_t virt_en= d, uint64_t phys_start, uint32_t flags) "domain=3D%d virt_start=3D0x%"PRIx6= 4" virt_end=3D0x%"PRIx64 " phys_start=3D0x%"PRIx64" flags=3D%d" virtio_iommu_unmap(uint32_t domain_id, uint64_t virt_start, uint64_t virt_= end) "domain=3D%d virt_start=3D0x%"PRIx64" virt_end=3D0x%"PRIx64 +virtio_iommu_translate(const char *name, uint32_t rid, uint64_t iova, int = flag) "mr=3D%s rid=3D%d addr=3D0x%"PRIx64" flag=3D%d" +virtio_iommu_init_iommu_mr(char *iommu_mr) "init %s" diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index afd6397ac9..2d7b1752b7 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -23,6 +23,8 @@ #include "hw/qdev-properties.h" #include "hw/virtio/virtio.h" #include "sysemu/kvm.h" +#include "qapi/error.h" +#include "qemu/error-report.h" #include "trace.h" =20 #include "standard-headers/linux/virtio_ids.h" @@ -34,6 +36,50 @@ /* Max size */ #define VIOMMU_DEFAULT_QUEUE_SIZE 256 =20 +static inline uint16_t virtio_iommu_get_sid(IOMMUDevice *dev) +{ + return PCI_BUILD_BDF(pci_bus_num(dev->bus), dev->devfn); +} + +static AddressSpace *virtio_iommu_find_add_as(PCIBus *bus, void *opaque, + int devfn) +{ + VirtIOIOMMU *s =3D opaque; + IOMMUPciBus *sbus =3D g_hash_table_lookup(s->as_by_busptr, bus); + static uint32_t mr_index; + IOMMUDevice *sdev; + + if (!sbus) { + sbus =3D g_malloc0(sizeof(IOMMUPciBus) + + sizeof(IOMMUDevice *) * IOMMU_PCI_DEVFN_MAX); + sbus->bus =3D bus; + g_hash_table_insert(s->as_by_busptr, bus, sbus); + } + + sdev =3D sbus->pbdev[devfn]; + if (!sdev) { + char *name =3D g_strdup_printf("%s-%d-%d", + TYPE_VIRTIO_IOMMU_MEMORY_REGION, + mr_index++, devfn); + sdev =3D sbus->pbdev[devfn] =3D g_malloc0(sizeof(IOMMUDevice)); + + sdev->viommu =3D s; + sdev->bus =3D bus; + sdev->devfn =3D devfn; + + trace_virtio_iommu_init_iommu_mr(name); + + memory_region_init_iommu(&sdev->iommu_mr, sizeof(sdev->iommu_mr), + TYPE_VIRTIO_IOMMU_MEMORY_REGION, + OBJECT(s), name, + UINT64_MAX); + address_space_init(&sdev->as, + MEMORY_REGION(&sdev->iommu_mr), TYPE_VIRTIO_IOM= MU); + g_free(name); + } + return &sdev->as; +} + static int virtio_iommu_attach(VirtIOIOMMU *s, struct virtio_iommu_req_attach *req) { @@ -172,6 +218,27 @@ out: } } =20 +static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemoryRegion *mr, hwaddr = addr, + IOMMUAccessFlags flag, + int iommu_idx) +{ + IOMMUDevice *sdev =3D container_of(mr, IOMMUDevice, iommu_mr); + uint32_t sid; + + IOMMUTLBEntry entry =3D { + .target_as =3D &address_space_memory, + .iova =3D addr, + .translated_addr =3D addr, + .addr_mask =3D ~(hwaddr)0, + .perm =3D IOMMU_NONE, + }; + + sid =3D virtio_iommu_get_sid(sdev); + + trace_virtio_iommu_translate(mr->parent_obj.name, sid, addr, flag); + return entry; +} + static void virtio_iommu_get_config(VirtIODevice *vdev, uint8_t *config_da= ta) { VirtIOIOMMU *dev =3D VIRTIO_IOMMU(vdev); @@ -252,6 +319,15 @@ static void virtio_iommu_device_realize(DeviceState *d= ev, Error **errp) virtio_add_feature(&s->features, VIRTIO_IOMMU_F_MMIO); =20 qemu_mutex_init(&s->mutex); + + memset(s->as_by_bus_num, 0, sizeof(s->as_by_bus_num)); + s->as_by_busptr =3D g_hash_table_new_full(NULL, NULL, NULL, g_free); + + if (s->primary_bus) { + pci_setup_iommu(s->primary_bus, virtio_iommu_find_add_as, s); + } else { + error_setg(errp, "VIRTIO-IOMMU is not attached to any PCI bus!"); + } } =20 static void virtio_iommu_device_unrealize(DeviceState *dev, Error **errp) @@ -310,6 +386,14 @@ static void virtio_iommu_class_init(ObjectClass *klass= , void *data) vdc->vmsd =3D &vmstate_virtio_iommu_device; } =20 +static void virtio_iommu_memory_region_class_init(ObjectClass *klass, + void *data) +{ + IOMMUMemoryRegionClass *imrc =3D IOMMU_MEMORY_REGION_CLASS(klass); + + imrc->translate =3D virtio_iommu_translate; +} + static const TypeInfo virtio_iommu_info =3D { .name =3D TYPE_VIRTIO_IOMMU, .parent =3D TYPE_VIRTIO_DEVICE, @@ -318,9 +402,17 @@ static const TypeInfo virtio_iommu_info =3D { .class_init =3D virtio_iommu_class_init, }; =20 +static const TypeInfo virtio_iommu_memory_region_info =3D { + .parent =3D TYPE_IOMMU_MEMORY_REGION, + .name =3D TYPE_VIRTIO_IOMMU_MEMORY_REGION, + .class_init =3D virtio_iommu_memory_region_class_init, +}; + + static void virtio_register_types(void) { type_register_static(&virtio_iommu_info); + type_register_static(&virtio_iommu_memory_region_info); } =20 type_init(virtio_register_types) diff --git a/include/hw/virtio/virtio-iommu.h b/include/hw/virtio/virtio-io= mmu.h index 4d47b6abeb..f55f48d304 100644 --- a/include/hw/virtio/virtio-iommu.h +++ b/include/hw/virtio/virtio-iommu.h @@ -28,6 +28,8 @@ #define VIRTIO_IOMMU(obj) \ OBJECT_CHECK(VirtIOIOMMU, (obj), TYPE_VIRTIO_IOMMU) =20 +#define TYPE_VIRTIO_IOMMU_MEMORY_REGION "virtio-iommu-memory-region" + #define IOMMU_PCI_BUS_MAX 256 #define IOMMU_PCI_DEVFN_MAX 256 =20 --=20 2.20.1 From nobody Sun May 12 23:37:35 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1574447942; cv=none; d=zohomail.com; s=zohoarc; b=QIcMBhiuvEjYJXQfeJ001g6/3TYxPXVWj7gSdB9GOz79gCTI5jwyTYiagkn51swR6cUUvSSvPtz0CnDxrze9/9sepcih7JmxUxhMBh+pVmxWpT6Nr7EoqDk/0GY+y5GRO7n14z6XAxyaAxEs2cOkE1Z3km++NDmPkzYx7EhQgjI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574447942; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=WpyPPjF7SQVxRQMHFwtWy7FSdjEZV1XI5I2O4de8PUE=; b=f8WiQt6CiUGMHD8cSB1P8yy4/N6AkJBCpdF0m9NUvgPVDbp7Do2O+jx2yR+pgFUo1FDQ5PpRO3ZlxCWz3MbSeayVzEc7aitZ95CEm9sxMLeD0UKcLyFnFRZtfGk4AnfZwDNx9wx3DY311IKJmVUfYRxpUZvdGCWXK6QiZsM45Lw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574447942322566.2336509839731; Fri, 22 Nov 2019 10:39:02 -0800 (PST) Received: from localhost ([::1]:54156 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDpc-0000R8-Tl for importer@patchew.org; Fri, 22 Nov 2019 13:39:00 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39738) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDhi-0008Ci-2s for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:30:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iYDhg-00009P-HJ for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:30:49 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:60273 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iYDhg-00009G-DV for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:30:48 -0500 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-301-98oLz2D9POWh_i9Xz6gRTQ-1; Fri, 22 Nov 2019 13:30:46 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id DB45E109D0AC; Fri, 22 Nov 2019 18:30:44 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-37.ams2.redhat.com [10.36.116.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id DACE46E712; Fri, 22 Nov 2019 18:30:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574447448; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WpyPPjF7SQVxRQMHFwtWy7FSdjEZV1XI5I2O4de8PUE=; b=CjN2VUq3ooDp4NU269bAILb7SW1hjTLLnRVT1qPNyZoCW/PI5eCPjfrgAR/B1lCDBCtrv1 sBQaa22OhhK8IDJFlG8mVNq0jmNxStfNK7izB6akjwZIPHJI85wcmEXpPjpKQqyEOQa4UO aA42fx6SB5Jm+NLJgnyUDxqqJfEVvN4= From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org, mst@redhat.com, armbru@redhat.com, jean-philippe.brucker@arm.com, bharatb.linux@gmail.com, yang.zhong@intel.com, dgilbert@redhat.com, quintela@redhat.com Subject: [PATCH for-5.0 v11 05/20] virtio-iommu: Endpoint and domains structs and helpers Date: Fri, 22 Nov 2019 19:29:28 +0100 Message-Id: <20191122182943.4656-6-eric.auger@redhat.com> In-Reply-To: <20191122182943.4656-1-eric.auger@redhat.com> References: <20191122182943.4656-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: 98oLz2D9POWh_i9Xz6gRTQ-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, peterx@redhat.com, tnowicki@marvell.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This patch introduce domain and endpoint internal datatypes. Both are stored in RB trees. The domain owns a list of endpoints attached to it. Helpers to get/put end points and domains are introduced. get() helpers will become static in subsequent patches. Signed-off-by: Eric Auger --- v10 -> v11: - fixed interval_cmp (<=3D -> < and >=3D -> >) - removed unused viommu field from endpoint - removed Bharat's R-b v9 -> v10: - added Bharat's R-b v6 -> v7: - on virtio_iommu_find_add_as the bus number computation may not be finalized yet so we cannot register the EPs at that time. Hence, let's remove the get_endpoint and also do not use the bus number for building the memory region name string (only used for debug though). v4 -> v5: - initialize as->endpoint_list v3 -> v4: - new separate patch --- hw/virtio/trace-events | 4 ++ hw/virtio/virtio-iommu.c | 117 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index b32169d56c..a373bdebb3 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -67,3 +67,7 @@ virtio_iommu_map(uint32_t domain_id, uint64_t virt_start,= uint64_t virt_end, uin virtio_iommu_unmap(uint32_t domain_id, uint64_t virt_start, uint64_t virt_= end) "domain=3D%d virt_start=3D0x%"PRIx64" virt_end=3D0x%"PRIx64 virtio_iommu_translate(const char *name, uint32_t rid, uint64_t iova, int = flag) "mr=3D%s rid=3D%d addr=3D0x%"PRIx64" flag=3D%d" virtio_iommu_init_iommu_mr(char *iommu_mr) "init %s" +virtio_iommu_get_endpoint(uint32_t ep_id) "Alloc endpoint=3D%d" +virtio_iommu_put_endpoint(uint32_t ep_id) "Free endpoint=3D%d" +virtio_iommu_get_domain(uint32_t domain_id) "Alloc domain=3D%d" +virtio_iommu_put_domain(uint32_t domain_id) "Free domain=3D%d" diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index 2d7b1752b7..235bde2203 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -32,15 +32,116 @@ #include "hw/virtio/virtio-bus.h" #include "hw/virtio/virtio-access.h" #include "hw/virtio/virtio-iommu.h" +#include "hw/pci/pci_bus.h" +#include "hw/pci/pci.h" =20 /* Max size */ #define VIOMMU_DEFAULT_QUEUE_SIZE 256 =20 +typedef struct viommu_domain { + uint32_t id; + GTree *mappings; + QLIST_HEAD(, viommu_endpoint) endpoint_list; +} viommu_domain; + +typedef struct viommu_endpoint { + uint32_t id; + viommu_domain *domain; + QLIST_ENTRY(viommu_endpoint) next; +} viommu_endpoint; + +typedef struct viommu_interval { + uint64_t low; + uint64_t high; +} viommu_interval; + static inline uint16_t virtio_iommu_get_sid(IOMMUDevice *dev) { return PCI_BUILD_BDF(pci_bus_num(dev->bus), dev->devfn); } =20 +static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_d= ata) +{ + viommu_interval *inta =3D (viommu_interval *)a; + viommu_interval *intb =3D (viommu_interval *)b; + + if (inta->high < intb->low) { + return -1; + } else if (intb->high < inta->low) { + return 1; + } else { + return 0; + } +} + +static void virtio_iommu_detach_endpoint_from_domain(viommu_endpoint *ep) +{ + QLIST_REMOVE(ep, next); + ep->domain =3D NULL; +} + +viommu_endpoint *virtio_iommu_get_endpoint(VirtIOIOMMU *s, uint32_t ep_id); +viommu_endpoint *virtio_iommu_get_endpoint(VirtIOIOMMU *s, uint32_t ep_id) +{ + viommu_endpoint *ep; + + ep =3D g_tree_lookup(s->endpoints, GUINT_TO_POINTER(ep_id)); + if (ep) { + return ep; + } + ep =3D g_malloc0(sizeof(*ep)); + ep->id =3D ep_id; + trace_virtio_iommu_get_endpoint(ep_id); + g_tree_insert(s->endpoints, GUINT_TO_POINTER(ep_id), ep); + return ep; +} + +static void virtio_iommu_put_endpoint(gpointer data) +{ + viommu_endpoint *ep =3D (viommu_endpoint *)data; + + if (ep->domain) { + virtio_iommu_detach_endpoint_from_domain(ep); + g_tree_unref(ep->domain->mappings); + } + + trace_virtio_iommu_put_endpoint(ep->id); + g_free(ep); +} + +viommu_domain *virtio_iommu_get_domain(VirtIOIOMMU *s, uint32_t domain_id); +viommu_domain *virtio_iommu_get_domain(VirtIOIOMMU *s, uint32_t domain_id) +{ + viommu_domain *domain; + + domain =3D g_tree_lookup(s->domains, GUINT_TO_POINTER(domain_id)); + if (domain) { + return domain; + } + domain =3D g_malloc0(sizeof(*domain)); + domain->id =3D domain_id; + domain->mappings =3D g_tree_new_full((GCompareDataFunc)interval_cmp, + NULL, (GDestroyNotify)g_free, + (GDestroyNotify)g_free); + g_tree_insert(s->domains, GUINT_TO_POINTER(domain_id), domain); + QLIST_INIT(&domain->endpoint_list); + trace_virtio_iommu_get_domain(domain_id); + return domain; +} + +static void virtio_iommu_put_domain(gpointer data) +{ + viommu_domain *domain =3D (viommu_domain *)data; + viommu_endpoint *iter, *tmp; + + QLIST_FOREACH_SAFE(iter, &domain->endpoint_list, next, tmp) { + virtio_iommu_detach_endpoint_from_domain(iter); + } + g_tree_destroy(domain->mappings); + trace_virtio_iommu_put_domain(domain->id); + g_free(domain); +} + static AddressSpace *virtio_iommu_find_add_as(PCIBus *bus, void *opaque, int devfn) { @@ -293,6 +394,13 @@ static const VMStateDescription vmstate_virtio_iommu_d= evice =3D { .unmigratable =3D 1, }; =20 +static gint int_cmp(gconstpointer a, gconstpointer b, gpointer user_data) +{ + uint ua =3D GPOINTER_TO_UINT(a); + uint ub =3D GPOINTER_TO_UINT(b); + return (ua > ub) - (ua < ub); +} + static void virtio_iommu_device_realize(DeviceState *dev, Error **errp) { VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); @@ -328,11 +436,20 @@ static void virtio_iommu_device_realize(DeviceState *= dev, Error **errp) } else { error_setg(errp, "VIRTIO-IOMMU is not attached to any PCI bus!"); } + + s->domains =3D g_tree_new_full((GCompareDataFunc)int_cmp, + NULL, NULL, virtio_iommu_put_domain); + s->endpoints =3D g_tree_new_full((GCompareDataFunc)int_cmp, + NULL, NULL, virtio_iommu_put_endpoint); } =20 static void virtio_iommu_device_unrealize(DeviceState *dev, Error **errp) { VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); + VirtIOIOMMU *s =3D VIRTIO_IOMMU(dev); + + g_tree_destroy(s->domains); + g_tree_destroy(s->endpoints); =20 virtio_cleanup(vdev); } --=20 2.20.1 From nobody Sun May 12 23:37:35 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1574447765; cv=none; d=zohomail.com; s=zohoarc; b=hlqMgCWW/7Z1Ejf+H7Wz2yJeT1n4NYQ4V21Bnn/R1N9p/0nsrU/Jj3Xi98RLDKXzwoqyQAME++VgcyzQHTeuUrww1YvmkhMgBtEbDUEQyQAYK9857qqLGYq7Ye/OECIhsYpJPW/KgNVLSjplDWTH4/1+AhvWREO7XP9hQVnF2TI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574447765; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=R0ACkwGHuTAl+zNilTLyl19bEIp0WiTEJD5IwjDPAak=; b=RNm9Ntp939JGKt1HG5gVh7MbQBfCD9WqR8oz5R9gmWJT7DZyWMkulBvXaGnmCUYIiZ3EA4a0qGP/iXPupfYeMGqM3oY9GfznIV2OPspTmAHbIQe7xLFwGmVSQelmk93LiCME1Z7DO+ayWEsC6WaKWfwjMoamTyWSEtQw9q/TkYA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574447765236541.0061452298592; Fri, 22 Nov 2019 10:36:05 -0800 (PST) Received: from localhost ([::1]:54118 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDmk-00056J-Kt for importer@patchew.org; Fri, 22 Nov 2019 13:36:02 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39766) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDhl-0008H7-Bu for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:30:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iYDhk-0000AU-5e for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:30:53 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:37035 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iYDhk-0000AG-1j for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:30:52 -0500 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-37-Tg2joNn_NZKiNIsD2oYoXA-1; Fri, 22 Nov 2019 13:30:50 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9E55718A43CE; Fri, 22 Nov 2019 18:30:48 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-37.ams2.redhat.com [10.36.116.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3EC859F47; Fri, 22 Nov 2019 18:30:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574447451; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=R0ACkwGHuTAl+zNilTLyl19bEIp0WiTEJD5IwjDPAak=; b=D61oZeyYQm9gMOn11DMZ5yzgxvJRXJEO5Ed1v5DowR3+8VdfPUR7/O9t2cUiAXErtP2AaZ ddcjNQpYKnFi+OKutLb7MVN/1Z5Ks0r8MBFQvDCUHbPE/asjRNbbk6YoV6I7Jc6/Lvkwrk ZCt09YemaIdtwny3SyDgjEmN2+SwEtI= From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org, mst@redhat.com, armbru@redhat.com, jean-philippe.brucker@arm.com, bharatb.linux@gmail.com, yang.zhong@intel.com, dgilbert@redhat.com, quintela@redhat.com Subject: [PATCH for-5.0 v11 06/20] virtio-iommu: Implement attach/detach command Date: Fri, 22 Nov 2019 19:29:29 +0100 Message-Id: <20191122182943.4656-7-eric.auger@redhat.com> In-Reply-To: <20191122182943.4656-1-eric.auger@redhat.com> References: <20191122182943.4656-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: Tg2joNn_NZKiNIsD2oYoXA-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, peterx@redhat.com, tnowicki@marvell.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This patch implements the endpoint attach/detach to/from a domain. Signed-off-by: Eric Auger --- --- hw/virtio/virtio-iommu.c | 43 ++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index 235bde2203..138d5b2a9c 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -77,11 +77,12 @@ static gint interval_cmp(gconstpointer a, gconstpointer= b, gpointer user_data) static void virtio_iommu_detach_endpoint_from_domain(viommu_endpoint *ep) { QLIST_REMOVE(ep, next); + g_tree_unref(ep->domain->mappings); ep->domain =3D NULL; } =20 -viommu_endpoint *virtio_iommu_get_endpoint(VirtIOIOMMU *s, uint32_t ep_id); -viommu_endpoint *virtio_iommu_get_endpoint(VirtIOIOMMU *s, uint32_t ep_id) +static viommu_endpoint *virtio_iommu_get_endpoint(VirtIOIOMMU *s, + uint32_t ep_id) { viommu_endpoint *ep; =20 @@ -102,15 +103,14 @@ static void virtio_iommu_put_endpoint(gpointer data) =20 if (ep->domain) { virtio_iommu_detach_endpoint_from_domain(ep); - g_tree_unref(ep->domain->mappings); } =20 trace_virtio_iommu_put_endpoint(ep->id); g_free(ep); } =20 -viommu_domain *virtio_iommu_get_domain(VirtIOIOMMU *s, uint32_t domain_id); -viommu_domain *virtio_iommu_get_domain(VirtIOIOMMU *s, uint32_t domain_id) +static viommu_domain *virtio_iommu_get_domain(VirtIOIOMMU *s, + uint32_t domain_id) { viommu_domain *domain; =20 @@ -137,7 +137,6 @@ static void virtio_iommu_put_domain(gpointer data) QLIST_FOREACH_SAFE(iter, &domain->endpoint_list, next, tmp) { virtio_iommu_detach_endpoint_from_domain(iter); } - g_tree_destroy(domain->mappings); trace_virtio_iommu_put_domain(domain->id); g_free(domain); } @@ -186,10 +185,27 @@ static int virtio_iommu_attach(VirtIOIOMMU *s, { uint32_t domain_id =3D le32_to_cpu(req->domain); uint32_t ep_id =3D le32_to_cpu(req->endpoint); + viommu_domain *domain; + viommu_endpoint *ep; =20 trace_virtio_iommu_attach(domain_id, ep_id); =20 - return VIRTIO_IOMMU_S_UNSUPP; + ep =3D virtio_iommu_get_endpoint(s, ep_id); + if (ep->domain) { + /* + * the device is already attached to a domain, + * detach it first + */ + virtio_iommu_detach_endpoint_from_domain(ep); + } + + domain =3D virtio_iommu_get_domain(s, domain_id); + QLIST_INSERT_HEAD(&domain->endpoint_list, ep, next); + + ep->domain =3D domain; + g_tree_ref(domain->mappings); + + return VIRTIO_IOMMU_S_OK; } =20 static int virtio_iommu_detach(VirtIOIOMMU *s, @@ -197,10 +213,21 @@ static int virtio_iommu_detach(VirtIOIOMMU *s, { uint32_t domain_id =3D le32_to_cpu(req->domain); uint32_t ep_id =3D le32_to_cpu(req->endpoint); + viommu_endpoint *ep; =20 trace_virtio_iommu_detach(domain_id, ep_id); =20 - return VIRTIO_IOMMU_S_UNSUPP; + ep =3D g_tree_lookup(s->endpoints, GUINT_TO_POINTER(ep_id)); + if (!ep) { + return VIRTIO_IOMMU_S_NOENT; + } + + if (!ep->domain) { + return VIRTIO_IOMMU_S_INVAL; + } + + virtio_iommu_detach_endpoint_from_domain(ep); + return VIRTIO_IOMMU_S_OK; } =20 static int virtio_iommu_map(VirtIOIOMMU *s, --=20 2.20.1 From nobody Sun May 12 23:37:35 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1574447947; cv=none; d=zohomail.com; s=zohoarc; b=e5UaPPGLyGV+L4yPYk6CLL7znGQd3c1xPU7t0U/7K/+kCdvqdFzhS97zJWIyQyB5pmgirFKQ8ptirDHceWO8fgnFUypIlEdC2Os+OZJwh8TavCqVdkG2Z0uARt8PwnIihadVTBkZ074NhfZsnvFTbo7AisNVylmjnF5+TISQBto= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574447947; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=MIfDgq7O2vkNImHIG2oBLQwfXHcYioYFwv5jnarjvgs=; b=mqKjm9ZMM/sVRb3wCuIeZn6oPIercBA24U5iNZ2a2PENw8nmh0LLasSQejQET0aVjQNqYdmIHCVu4DPgcNszgtaD7AZ84m4GSykX9zNoTii1WRoP28MpeqBOsm8HOh7p+puoLb1f3CmT7k3GA6vcUZAxji2+FYdMyL+B0A1YC/Q= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574447947780265.8758171621172; Fri, 22 Nov 2019 10:39:07 -0800 (PST) Received: from localhost ([::1]:54158 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDpg-0000Za-C6 for importer@patchew.org; Fri, 22 Nov 2019 13:39:04 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39788) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDhr-0008OZ-JB for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iYDhq-0000BM-B1 for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:30:59 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:52434 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iYDhq-0000BC-77 for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:30:58 -0500 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-388-sVkn4u9eOs26evF7cQGD6w-1; Fri, 22 Nov 2019 13:30:56 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id F2E548AB90C; Fri, 22 Nov 2019 18:30:54 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-37.ams2.redhat.com [10.36.116.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id F3A2A61074; Fri, 22 Nov 2019 18:30:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574447457; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MIfDgq7O2vkNImHIG2oBLQwfXHcYioYFwv5jnarjvgs=; b=TKyKBclMxxYgwV4kDXU08Hh9BruIH/DCfR6xE4EUh3dSgwdOA3h1blkU+iTvngSSTnytEl Oj67N5mE0S9+56AUCkywWBFwQ76WeTmYQhOs4WzKe3tyKRoRT7HtNJUMJspA1i78OfHWzE wVVUa3EKDnuH0u9/VgkmP5TsWsUZ39k= From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org, mst@redhat.com, armbru@redhat.com, jean-philippe.brucker@arm.com, bharatb.linux@gmail.com, yang.zhong@intel.com, dgilbert@redhat.com, quintela@redhat.com Subject: [PATCH for-5.0 v11 07/20] virtio-iommu: Implement map/unmap Date: Fri, 22 Nov 2019 19:29:30 +0100 Message-Id: <20191122182943.4656-8-eric.auger@redhat.com> In-Reply-To: <20191122182943.4656-1-eric.auger@redhat.com> References: <20191122182943.4656-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: sVkn4u9eOs26evF7cQGD6w-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 205.139.110.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, peterx@redhat.com, tnowicki@marvell.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This patch implements virtio_iommu_map/unmap. Signed-off-by: Eric Auger --- v10 -> v11: - revisit the implementation of unmap according to Peter's suggestion - removed virt_addr and size from viommu_mapping struct - use g_tree_lookup_extended() - return VIRTIO_IOMMU_S_RANGE in case a mapping were to be split on unmap (instead of INVAL) v5 -> v6: - use new v0.6 fields - replace error_report by qemu_log_mask v3 -> v4: - implement unmap semantics as specified in v0.4 --- hw/virtio/trace-events | 1 + hw/virtio/virtio-iommu.c | 65 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index a373bdebb3..f25359cee2 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -65,6 +65,7 @@ virtio_iommu_attach(uint32_t domain_id, uint32_t ep_id) "= domain=3D%d endpoint=3D%d" virtio_iommu_detach(uint32_t domain_id, uint32_t ep_id) "domain=3D%d endpo= int=3D%d" virtio_iommu_map(uint32_t domain_id, uint64_t virt_start, uint64_t virt_en= d, uint64_t phys_start, uint32_t flags) "domain=3D%d virt_start=3D0x%"PRIx6= 4" virt_end=3D0x%"PRIx64 " phys_start=3D0x%"PRIx64" flags=3D%d" virtio_iommu_unmap(uint32_t domain_id, uint64_t virt_start, uint64_t virt_= end) "domain=3D%d virt_start=3D0x%"PRIx64" virt_end=3D0x%"PRIx64 +virtio_iommu_unmap_done(uint32_t domain_id, uint64_t virt_start, uint64_t = virt_end) "domain=3D%d virt_start=3D0x%"PRIx64" virt_end=3D0x%"PRIx64 virtio_iommu_translate(const char *name, uint32_t rid, uint64_t iova, int = flag) "mr=3D%s rid=3D%d addr=3D0x%"PRIx64" flag=3D%d" virtio_iommu_init_iommu_mr(char *iommu_mr) "init %s" virtio_iommu_get_endpoint(uint32_t ep_id) "Alloc endpoint=3D%d" diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index 138d5b2a9c..f0a56833a2 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -18,6 +18,7 @@ */ =20 #include "qemu/osdep.h" +#include "qemu/log.h" #include "qemu/iov.h" #include "qemu-common.h" #include "hw/qdev-properties.h" @@ -55,6 +56,11 @@ typedef struct viommu_interval { uint64_t high; } viommu_interval; =20 +typedef struct viommu_mapping { + uint64_t phys_addr; + uint32_t flags; +} viommu_mapping; + static inline uint16_t virtio_iommu_get_sid(IOMMUDevice *dev) { return PCI_BUILD_BDF(pci_bus_num(dev->bus), dev->devfn); @@ -238,10 +244,35 @@ static int virtio_iommu_map(VirtIOIOMMU *s, uint64_t virt_start =3D le64_to_cpu(req->virt_start); uint64_t virt_end =3D le64_to_cpu(req->virt_end); uint32_t flags =3D le32_to_cpu(req->flags); + viommu_domain *domain; + viommu_interval *interval; + viommu_mapping *mapping; + + interval =3D g_malloc0(sizeof(*interval)); + + interval->low =3D virt_start; + interval->high =3D virt_end; + + domain =3D g_tree_lookup(s->domains, GUINT_TO_POINTER(domain_id)); + if (!domain) { + return VIRTIO_IOMMU_S_NOENT; + } + + mapping =3D g_tree_lookup(domain->mappings, (gpointer)interval); + if (mapping) { + g_free(interval); + return VIRTIO_IOMMU_S_INVAL; + } =20 trace_virtio_iommu_map(domain_id, virt_start, virt_end, phys_start, fl= ags); =20 - return VIRTIO_IOMMU_S_UNSUPP; + mapping =3D g_malloc0(sizeof(*mapping)); + mapping->phys_addr =3D phys_start; + mapping->flags =3D flags; + + g_tree_insert(domain->mappings, interval, mapping); + + return VIRTIO_IOMMU_S_OK; } =20 static int virtio_iommu_unmap(VirtIOIOMMU *s, @@ -250,10 +281,40 @@ static int virtio_iommu_unmap(VirtIOIOMMU *s, uint32_t domain_id =3D le32_to_cpu(req->domain); uint64_t virt_start =3D le64_to_cpu(req->virt_start); uint64_t virt_end =3D le64_to_cpu(req->virt_end); + viommu_mapping *iter_val; + viommu_interval interval, *iter_key; + viommu_domain *domain; + int ret =3D VIRTIO_IOMMU_S_OK; =20 trace_virtio_iommu_unmap(domain_id, virt_start, virt_end); =20 - return VIRTIO_IOMMU_S_UNSUPP; + domain =3D g_tree_lookup(s->domains, GUINT_TO_POINTER(domain_id)); + if (!domain) { + qemu_log_mask(LOG_GUEST_ERROR, "%s: no domain\n", __func__); + return VIRTIO_IOMMU_S_NOENT; + } + interval.low =3D virt_start; + interval.high =3D virt_end; + + while (g_tree_lookup_extended(domain->mappings, &interval, + (void **)&iter_key, (void**)&iter_val)) { + uint64_t current_low =3D iter_key->low; + uint64_t current_high =3D iter_key->high; + + if (interval.low <=3D current_low && interval.high >=3D current_hi= gh) { + g_tree_remove(domain->mappings, iter_key); + trace_virtio_iommu_unmap_done(domain_id, current_low, current_= high); + } else { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: domain=3D %d Unmap [0x%"PRIx64",0x%"PRIx64"] forbidde= n as " + "it would split existing mapping [0x%"PRIx64", 0x%"PRIx64"= ]\n", + __func__, domain_id, interval.low, interval.high, + current_low, current_high); + ret =3D VIRTIO_IOMMU_S_RANGE; + break; + } + } + return ret; } =20 static int virtio_iommu_iov_to_req(struct iovec *iov, --=20 2.20.1 From nobody Sun May 12 23:37:35 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1574448214; cv=none; d=zohomail.com; s=zohoarc; b=jZ7y0utkMRAH0NCWIj4bU2nfHcndsTOWSdf7moCOcAw9SSx/Wik/x8VAsyRgFxr2xofkq9bYhbMbhp/todv7ENRKLxtvPy9iOy+ld2zPMKLQSduqm6l/OAyxmhHb/gdr7uL5PZGpyr8lq+HegDKiQ3Zuv4JIvM41K9inTd/QBIQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574448214; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=vfMeaOXX+t023+1hBgHOnvRfnhFC18bmrnLW83129pU=; b=NZA1tdlII43yAsyhdTvzyhHxQoE04730wGjgqp0+HCBQNUHmChfaiTd3oGBW0wll8djqVB/sUp7jcklBNQaDLN1YBum9Ahx3ZV2/motqjpiTGGs/CLKHLlewpYhLLwHFVopEMehV4eudNB7PKTMBTG5mFFE6Kge35fzWFK5ghKg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574448214368427.7469990933798; Fri, 22 Nov 2019 10:43:34 -0800 (PST) Received: from localhost ([::1]:54202 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDtz-0004ly-W8 for importer@patchew.org; Fri, 22 Nov 2019 13:43:32 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39851) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDi0-0000AR-QM for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:09 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iYDhz-0000G4-GR for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:08 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:33390 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iYDhz-0000Fv-Ci for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:07 -0500 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-286-XL5rKNVFOauGOs0KvYm3Ig-1; Fri, 22 Nov 2019 13:31:05 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E43D6800054; Fri, 22 Nov 2019 18:31:03 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-37.ams2.redhat.com [10.36.116.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4D92D6E712; Fri, 22 Nov 2019 18:30:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574447466; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vfMeaOXX+t023+1hBgHOnvRfnhFC18bmrnLW83129pU=; b=gGPmrIkfzcPW17qzXpGpuLfEJjCC+pCR8cPTeu7LUvRrJ9v7O2DdCzcukdhfoic5asj3K9 mtGcoVqVmP0G1f+sdOq4E/uiQ8/qVDwJnyC6bH+NDtlOaiCXJEKsynuHIE8BgDQeBWGVP+ 060Btvx1uBevj3E3C83qXqjlRXAzpFk= From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org, mst@redhat.com, armbru@redhat.com, jean-philippe.brucker@arm.com, bharatb.linux@gmail.com, yang.zhong@intel.com, dgilbert@redhat.com, quintela@redhat.com Subject: [PATCH for-5.0 v11 08/20] virtio-iommu: Implement translate Date: Fri, 22 Nov 2019 19:29:31 +0100 Message-Id: <20191122182943.4656-9-eric.auger@redhat.com> In-Reply-To: <20191122182943.4656-1-eric.auger@redhat.com> References: <20191122182943.4656-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: XL5rKNVFOauGOs0KvYm3Ig-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 205.139.110.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, peterx@redhat.com, tnowicki@marvell.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This patch implements the translate callback Signed-off-by: Eric Auger Reviewed-by: Jean-Philippe Brucker --- v10 -> v11: - take into account the new value struct and use g_tree_lookup_extended - switched to error_report_once v6 -> v7: - implemented bypass-mode v5 -> v6: - replace error_report by qemu_log_mask v4 -> v5: - check the device domain is not NULL - s/printf/error_report - set flags to IOMMU_NONE in case of all translation faults --- hw/virtio/trace-events | 1 + hw/virtio/virtio-iommu.c | 63 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index f25359cee2..de7cbb3c8f 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -72,3 +72,4 @@ virtio_iommu_get_endpoint(uint32_t ep_id) "Alloc endpoint= =3D%d" virtio_iommu_put_endpoint(uint32_t ep_id) "Free endpoint=3D%d" virtio_iommu_get_domain(uint32_t domain_id) "Alloc domain=3D%d" virtio_iommu_put_domain(uint32_t domain_id) "Free domain=3D%d" +virtio_iommu_translate_out(uint64_t virt_addr, uint64_t phys_addr, uint32_= t sid) "0x%"PRIx64" -> 0x%"PRIx64 " for sid=3D%d" diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index f0a56833a2..a83666557b 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -412,19 +412,80 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemo= ryRegion *mr, hwaddr addr, int iommu_idx) { IOMMUDevice *sdev =3D container_of(mr, IOMMUDevice, iommu_mr); + viommu_interval interval, *mapping_key; + viommu_mapping *mapping_value; + VirtIOIOMMU *s =3D sdev->viommu; + viommu_endpoint *ep; + bool bypass_allowed; uint32_t sid; + bool found; + + interval.low =3D addr; + interval.high =3D addr + 1; =20 IOMMUTLBEntry entry =3D { .target_as =3D &address_space_memory, .iova =3D addr, .translated_addr =3D addr, - .addr_mask =3D ~(hwaddr)0, + .addr_mask =3D (1 << ctz32(s->config.page_size_mask)) - 1, .perm =3D IOMMU_NONE, }; =20 + bypass_allowed =3D virtio_has_feature(s->acked_features, + VIRTIO_IOMMU_F_BYPASS); + sid =3D virtio_iommu_get_sid(sdev); =20 trace_virtio_iommu_translate(mr->parent_obj.name, sid, addr, flag); + qemu_mutex_lock(&s->mutex); + + ep =3D g_tree_lookup(s->endpoints, GUINT_TO_POINTER(sid)); + if (!ep) { + if (!bypass_allowed) { + error_report_once("%s sid=3D%d is not known!!", __func__, sid); + } else { + entry.perm =3D flag; + } + goto unlock; + } + + if (!ep->domain) { + if (!bypass_allowed) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s %02x:%02x.%01x not attached to any domain\n", + __func__, PCI_BUS_NUM(sid), + PCI_SLOT(sid), PCI_FUNC(sid)); + } else { + entry.perm =3D flag; + } + goto unlock; + } + + found =3D g_tree_lookup_extended(ep->domain->mappings, (gpointer)(&int= erval), + (void **)&mapping_key, + (void **)&mapping_value); + if (!found) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s no mapping for 0x%"PRIx64" for sid=3D%d\n", + __func__, addr, sid); + goto unlock; + } + + if (((flag & IOMMU_RO) && + !(mapping_value->flags & VIRTIO_IOMMU_MAP_F_READ)) || + ((flag & IOMMU_WO) && + !(mapping_value->flags & VIRTIO_IOMMU_MAP_F_WRITE))) { + qemu_log_mask(LOG_GUEST_ERROR, + "Permission error on 0x%"PRIx64"(%d): allowed=3D%d\n= ", + addr, flag, mapping_value->flags); + goto unlock; + } + entry.translated_addr =3D addr - mapping_key->low + mapping_value->phy= s_addr; + entry.perm =3D flag; + trace_virtio_iommu_translate_out(addr, entry.translated_addr, sid); + +unlock: + qemu_mutex_unlock(&s->mutex); return entry; } =20 --=20 2.20.1 From nobody Sun May 12 23:37:35 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1574448640; cv=none; d=zohomail.com; s=zohoarc; b=BNYX1jHyoqpKQhMMrgxVQxQRulYjkONX1DnICqUI4TGAwHak+Df8h9+PkP0jPryj1+s1cdu8VY+ZrJuE6D+jWD2U6jO/lJdTgHsNQ91OGUn8TIFwYQlW8e+a/Z5wYaQKzHZSlTb2/+zDYXGtKmn0CISpt6BD9JhR5E6ySsA5VuE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574448640; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=hP1XRUr8hBzDtWLyDgeKj1K15HOETKuvmw9ZYMPh4qw=; b=kDkrHgHU5Z6gVB43Unv23QZDgzCXS2bOxCaq6iYhjqJp+BmWh8bHHQRvhDKqGEoaNSDP++n86ZfnjvMdB5/A5rvYbFawfMshIIQSHvNktSUBP1+TLCWKIU1i3TzzU3/iAogTCrKqcPcV8B5uzIsoNR1y/ORt2um4CydAilu05hY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574448640078244.50111327887078; Fri, 22 Nov 2019 10:50:40 -0800 (PST) Received: from localhost ([::1]:54296 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYE0r-0003fs-O8 for importer@patchew.org; Fri, 22 Nov 2019 13:50:37 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39885) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDi4-0000Fm-Jl for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:13 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iYDi3-0000Hc-4T for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:12 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:23718 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iYDi3-0000HH-01 for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:11 -0500 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-2-Z1jxOUdkPhWfzFz-RbzO7A-1; Fri, 22 Nov 2019 13:31:09 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9B137477; Fri, 22 Nov 2019 18:31:07 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-37.ams2.redhat.com [10.36.116.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3CABC6E712; Fri, 22 Nov 2019 18:31:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574447470; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hP1XRUr8hBzDtWLyDgeKj1K15HOETKuvmw9ZYMPh4qw=; b=Ecmc/wFNh4U9DY5OXzMEjuyajO6JbbsPeIrQPIP4SifIo8qZO3+u9anvpaGdknR+VHbVxY n1Igj+frg46j9Uc8RiB0ecK7G180dLi6dwpF1ZN+OYSHEhZ3hcXjr3IARzOeb1xqYq/HGo kH/IgYB+ig8udJKi3CMbnvwWhQ9w14E= From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org, mst@redhat.com, armbru@redhat.com, jean-philippe.brucker@arm.com, bharatb.linux@gmail.com, yang.zhong@intel.com, dgilbert@redhat.com, quintela@redhat.com Subject: [PATCH for-5.0 v11 09/20] virtio-iommu: Implement fault reporting Date: Fri, 22 Nov 2019 19:29:32 +0100 Message-Id: <20191122182943.4656-10-eric.auger@redhat.com> In-Reply-To: <20191122182943.4656-1-eric.auger@redhat.com> References: <20191122182943.4656-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: Z1jxOUdkPhWfzFz-RbzO7A-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 205.139.110.61 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, peterx@redhat.com, tnowicki@marvell.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" The event queue allows to report asynchronous errors. The translate function now injects faults when relevant. Signed-off-by: Eric Auger --- v10 -> v11: - change a virtio_error into an error_report_once (no buffer available for output faults) --- hw/virtio/trace-events | 1 + hw/virtio/virtio-iommu.c | 69 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index de7cbb3c8f..a572eb71aa 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -73,3 +73,4 @@ virtio_iommu_put_endpoint(uint32_t ep_id) "Free endpoint= =3D%d" virtio_iommu_get_domain(uint32_t domain_id) "Alloc domain=3D%d" virtio_iommu_put_domain(uint32_t domain_id) "Free domain=3D%d" virtio_iommu_translate_out(uint64_t virt_addr, uint64_t phys_addr, uint32_= t sid) "0x%"PRIx64" -> 0x%"PRIx64 " for sid=3D%d" +virtio_iommu_report_fault(uint8_t reason, uint32_t flags, uint32_t endpoin= t, uint64_t addr) "FAULT reason=3D%d flags=3D%d endpoint=3D%d address =3D0x= %"PRIx64 diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index a83666557b..723616a5db 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -407,6 +407,51 @@ out: } } =20 +static void virtio_iommu_report_fault(VirtIOIOMMU *viommu, uint8_t reason, + uint32_t flags, uint32_t endpoint, + uint64_t address) +{ + VirtIODevice *vdev =3D &viommu->parent_obj; + VirtQueue *vq =3D viommu->event_vq; + struct virtio_iommu_fault fault; + VirtQueueElement *elem; + size_t sz; + + memset(&fault, 0, sizeof(fault)); + fault.reason =3D reason; + fault.flags =3D flags; + fault.endpoint =3D endpoint; + fault.address =3D address; + + for (;;) { + elem =3D virtqueue_pop(vq, sizeof(VirtQueueElement)); + + if (!elem) { + error_report_once( + "no buffer available in event queue to report event"); + return; + } + + if (iov_size(elem->in_sg, elem->in_num) < sizeof(fault)) { + virtio_error(vdev, "error buffer of wrong size"); + virtqueue_detach_element(vq, elem, 0); + g_free(elem); + continue; + } + break; + } + /* we have a buffer to fill in */ + sz =3D iov_from_buf(elem->in_sg, elem->in_num, 0, + &fault, sizeof(fault)); + assert(sz =3D=3D sizeof(fault)); + + trace_virtio_iommu_report_fault(reason, flags, endpoint, address); + virtqueue_push(vq, elem, sz); + virtio_notify(vdev, vq); + g_free(elem); + +} + static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemoryRegion *mr, hwaddr = addr, IOMMUAccessFlags flag, int iommu_idx) @@ -415,9 +460,10 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemor= yRegion *mr, hwaddr addr, viommu_interval interval, *mapping_key; viommu_mapping *mapping_value; VirtIOIOMMU *s =3D sdev->viommu; + bool read_fault, write_fault; viommu_endpoint *ep; + uint32_t sid, flags; bool bypass_allowed; - uint32_t sid; bool found; =20 interval.low =3D addr; @@ -443,6 +489,8 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemory= Region *mr, hwaddr addr, if (!ep) { if (!bypass_allowed) { error_report_once("%s sid=3D%d is not known!!", __func__, sid); + virtio_iommu_report_fault(s, VIRTIO_IOMMU_FAULT_R_UNKNOWN, + 0, sid, 0); } else { entry.perm =3D flag; } @@ -455,6 +503,8 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemory= Region *mr, hwaddr addr, "%s %02x:%02x.%01x not attached to any domain\n", __func__, PCI_BUS_NUM(sid), PCI_SLOT(sid), PCI_FUNC(sid)); + virtio_iommu_report_fault(s, VIRTIO_IOMMU_FAULT_R_DOMAIN, + 0, sid, 0); } else { entry.perm =3D flag; } @@ -468,16 +518,25 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemo= ryRegion *mr, hwaddr addr, qemu_log_mask(LOG_GUEST_ERROR, "%s no mapping for 0x%"PRIx64" for sid=3D%d\n", __func__, addr, sid); + virtio_iommu_report_fault(s, VIRTIO_IOMMU_FAULT_R_MAPPING, + 0, sid, addr); goto unlock; } =20 - if (((flag & IOMMU_RO) && - !(mapping_value->flags & VIRTIO_IOMMU_MAP_F_READ)) || - ((flag & IOMMU_WO) && - !(mapping_value->flags & VIRTIO_IOMMU_MAP_F_WRITE))) { + read_fault =3D (flag & IOMMU_RO) && + !(mapping_value->flags & VIRTIO_IOMMU_MAP_F_READ); + write_fault =3D (flag & IOMMU_WO) && + !(mapping_value->flags & VIRTIO_IOMMU_MAP_F_WRITE); + + flags =3D read_fault ? VIRTIO_IOMMU_FAULT_F_READ : 0; + flags |=3D write_fault ? VIRTIO_IOMMU_FAULT_F_WRITE : 0; + if (flags) { qemu_log_mask(LOG_GUEST_ERROR, "Permission error on 0x%"PRIx64"(%d): allowed=3D%d\n= ", addr, flag, mapping_value->flags); + flags |=3D VIRTIO_IOMMU_FAULT_F_ADDRESS; + virtio_iommu_report_fault(s, VIRTIO_IOMMU_FAULT_R_MAPPING, + flags, sid, addr); goto unlock; } entry.translated_addr =3D addr - mapping_key->low + mapping_value->phy= s_addr; --=20 2.20.1 From nobody Sun May 12 23:37:35 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1574448729; cv=none; d=zohomail.com; s=zohoarc; b=PZPVP9wvK4ugiyBWaw02JhSngw0t2B5dI8rqNTrRrQhSxQJNY5vUr2Mklbec1CILHNAfgfeCUkaeUd549sVzyhM0JE9/engsf3GiW4CqvHQZ7/zP8lOpqY+qgDkiMofQBckaSI1YFEwNhkAZ0ldNZ0zfGTTQaHtAL701ba7pRoI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574448729; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=TPw8HfXS6c8+/kaQMqsXBz05eJr0Po1JCxc9Y7B3JfM=; b=OaXs2inPoIes6Mhwwoz/FEZ5Q1KuZK4AhEiIaVQDT7K91XT+K8ShJTimIz00SEBbk5u5klmEtKGPDetXE7BN0upf5OFl5i4xyHUpkyHJYqPunqz6iUSANTIqleswbefK9nlwdbREz/fQKJFeAiqi1/XnHhHj9AuixWwDqZHmw/U= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574448729539149.2727219260055; Fri, 22 Nov 2019 10:52:09 -0800 (PST) Received: from localhost ([::1]:54318 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYE2K-00060j-Eb for importer@patchew.org; Fri, 22 Nov 2019 13:52:08 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39932) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDiF-0000UP-Q8 for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iYDiD-0000K6-9A for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:23 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:51299 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iYDiD-0000Jv-4V for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:21 -0500 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-255-OrGnNxdAMZOzpYtLVAZBRA-1; Fri, 22 Nov 2019 13:31:19 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9E3BE800054; Fri, 22 Nov 2019 18:31:14 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-37.ams2.redhat.com [10.36.116.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id F28A26E712; Fri, 22 Nov 2019 18:31:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574447480; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TPw8HfXS6c8+/kaQMqsXBz05eJr0Po1JCxc9Y7B3JfM=; b=bnZmZ0Ful9ISM5iF2NXZHan957LaRlQ8wiPTrQGjTh108w20RC3GsIkNZQ4CaBSvJYo284 xz0vI1QtQsJ9MWo1RBXHv+/T/6Hmg1HseWZAeL0rT07fpILS1AlKDy9tU2psYVJg87pa5s L/jyvWfIsTvgNFMWAI9NBBnCu+qX5E8= From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org, mst@redhat.com, armbru@redhat.com, jean-philippe.brucker@arm.com, bharatb.linux@gmail.com, yang.zhong@intel.com, dgilbert@redhat.com, quintela@redhat.com Subject: [PATCH for-5.0 v11 10/20] virtio-iommu-pci: Add virtio iommu pci support Date: Fri, 22 Nov 2019 19:29:33 +0100 Message-Id: <20191122182943.4656-11-eric.auger@redhat.com> In-Reply-To: <20191122182943.4656-1-eric.auger@redhat.com> References: <20191122182943.4656-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: OrGnNxdAMZOzpYtLVAZBRA-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, peterx@redhat.com, tnowicki@marvell.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This patch adds virtio-iommu-pci, which is the pci proxy for the virtio-iommu device. Signed-off-by: Eric Auger Reviewed-by: Jean-Philippe Brucker --- v10 -> v11: - add the reserved_regions array property v9 -> v10: - include "hw/qdev-properties.h" header v8 -> v9: - add the msi-bypass property - create virtio-iommu-pci.c --- hw/virtio/Makefile.objs | 1 + hw/virtio/virtio-iommu-pci.c | 91 ++++++++++++++++++++++++++++++++ include/hw/pci/pci.h | 1 + include/hw/virtio/virtio-iommu.h | 1 + qdev-monitor.c | 1 + 5 files changed, 95 insertions(+) create mode 100644 hw/virtio/virtio-iommu-pci.c diff --git a/hw/virtio/Makefile.objs b/hw/virtio/Makefile.objs index f68ac14a90..33e6bc591a 100644 --- a/hw/virtio/Makefile.objs +++ b/hw/virtio/Makefile.objs @@ -29,6 +29,7 @@ obj-$(CONFIG_VIRTIO_INPUT_HOST) +=3D virtio-input-host-pc= i.o obj-$(CONFIG_VIRTIO_INPUT) +=3D virtio-input-pci.o obj-$(CONFIG_VIRTIO_RNG) +=3D virtio-rng-pci.o obj-$(CONFIG_VIRTIO_BALLOON) +=3D virtio-balloon-pci.o +obj-$(CONFIG_VIRTIO_IOMMU) +=3D virtio-iommu-pci.o obj-$(CONFIG_VIRTIO_9P) +=3D virtio-9p-pci.o obj-$(CONFIG_VIRTIO_SCSI) +=3D virtio-scsi-pci.o obj-$(CONFIG_VIRTIO_BLK) +=3D virtio-blk-pci.o diff --git a/hw/virtio/virtio-iommu-pci.c b/hw/virtio/virtio-iommu-pci.c new file mode 100644 index 0000000000..280230b31e --- /dev/null +++ b/hw/virtio/virtio-iommu-pci.c @@ -0,0 +1,91 @@ +/* + * Virtio IOMMU PCI Bindings + * + * Copyright (c) 2019 Red Hat, Inc. + * Written by Eric Auger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 or + * (at your option) any later version. + */ + +#include "qemu/osdep.h" + +#include "virtio-pci.h" +#include "hw/virtio/virtio-iommu.h" +#include "hw/qdev-properties.h" + +typedef struct VirtIOIOMMUPCI VirtIOIOMMUPCI; + +/* + * virtio-iommu-pci: This extends VirtioPCIProxy. + * + */ +#define VIRTIO_IOMMU_PCI(obj) \ + OBJECT_CHECK(VirtIOIOMMUPCI, (obj), TYPE_VIRTIO_IOMMU_PCI) + +struct VirtIOIOMMUPCI { + VirtIOPCIProxy parent_obj; + VirtIOIOMMU vdev; +}; + +static Property virtio_iommu_pci_properties[] =3D { + DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), + DEFINE_PROP_ARRAY("reserved-regions", VirtIOIOMMUPCI, + vdev.nb_reserved_regions, vdev.reserved_regions, + qdev_prop_interval, Interval), + DEFINE_PROP_END_OF_LIST(), +}; + +static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **err= p) +{ + VirtIOIOMMUPCI *dev =3D VIRTIO_IOMMU_PCI(vpci_dev); + DeviceState *vdev =3D DEVICE(&dev->vdev); + + qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); + object_property_set_link(OBJECT(dev), + OBJECT(pci_get_bus(&vpci_dev->pci_dev)), + "primary-bus", errp); + object_property_set_bool(OBJECT(vdev), true, "realized", errp); +} + +static void virtio_iommu_pci_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + VirtioPCIClass *k =3D VIRTIO_PCI_CLASS(klass); + PCIDeviceClass *pcidev_k =3D PCI_DEVICE_CLASS(klass); + k->realize =3D virtio_iommu_pci_realize; + set_bit(DEVICE_CATEGORY_MISC, dc->categories); + dc->props =3D virtio_iommu_pci_properties; + pcidev_k->vendor_id =3D PCI_VENDOR_ID_REDHAT_QUMRANET; + pcidev_k->device_id =3D PCI_DEVICE_ID_VIRTIO_IOMMU; + pcidev_k->revision =3D VIRTIO_PCI_ABI_VERSION; + pcidev_k->class_id =3D PCI_CLASS_OTHERS; +} + +static void virtio_iommu_pci_instance_init(Object *obj) +{ + VirtIOIOMMUPCI *dev =3D VIRTIO_IOMMU_PCI(obj); + + virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), + TYPE_VIRTIO_IOMMU); +} + +static const VirtioPCIDeviceTypeInfo virtio_iommu_pci_info =3D { + .base_name =3D TYPE_VIRTIO_IOMMU_PCI, + .generic_name =3D "virtio-iommu-pci", + .transitional_name =3D "virtio-iommu-pci-transitional", + .non_transitional_name =3D "virtio-iommu-pci-non-transitional", + .instance_size =3D sizeof(VirtIOIOMMUPCI), + .instance_init =3D virtio_iommu_pci_instance_init, + .class_init =3D virtio_iommu_pci_class_init, +}; + +static void virtio_iommu_pci_register(void) +{ + virtio_pci_types_register(&virtio_iommu_pci_info); +} + +type_init(virtio_iommu_pci_register) + + diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h index db75c6dfd0..d7715c826a 100644 --- a/include/hw/pci/pci.h +++ b/include/hw/pci/pci.h @@ -86,6 +86,7 @@ extern bool pci_available; #define PCI_DEVICE_ID_VIRTIO_9P 0x1009 #define PCI_DEVICE_ID_VIRTIO_VSOCK 0x1012 #define PCI_DEVICE_ID_VIRTIO_PMEM 0x1013 +#define PCI_DEVICE_ID_VIRTIO_IOMMU 0x1014 =20 #define PCI_VENDOR_ID_REDHAT 0x1b36 #define PCI_DEVICE_ID_REDHAT_BRIDGE 0x0001 diff --git a/include/hw/virtio/virtio-iommu.h b/include/hw/virtio/virtio-io= mmu.h index f55f48d304..1ab6993d29 100644 --- a/include/hw/virtio/virtio-iommu.h +++ b/include/hw/virtio/virtio-iommu.h @@ -25,6 +25,7 @@ #include "hw/pci/pci.h" =20 #define TYPE_VIRTIO_IOMMU "virtio-iommu-device" +#define TYPE_VIRTIO_IOMMU_PCI "virtio-iommu-device-base" #define VIRTIO_IOMMU(obj) \ OBJECT_CHECK(VirtIOIOMMU, (obj), TYPE_VIRTIO_IOMMU) =20 diff --git a/qdev-monitor.c b/qdev-monitor.c index e6b112eb0a..e61ca62061 100644 --- a/qdev-monitor.c +++ b/qdev-monitor.c @@ -66,6 +66,7 @@ static const QDevAlias qdev_alias_table[] =3D { { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_S390X }, { "virtio-input-host-pci", "virtio-input-host", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, + { "virtio-iommu-pci", "virtio-iommu", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X= }, { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_S390X }, { "virtio-keyboard-pci", "virtio-keyboard", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, --=20 2.20.1 From nobody Sun May 12 23:37:35 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1574448275; cv=none; d=zohomail.com; s=zohoarc; b=WvhYN8sATnBPt+aHPCm+bzZ0y2JPsyDLAS6Hl/qnq5xJpvTWAcUV2h0ziBK8iNJl32terICk8sG6nVAHBe9yXHqJJoAkXVmj/+UHbWXaz0axUdZtMIe26SJBXJ4mc5xwjkrRpRbFPmT/kPSszJHFTV9ko44c1opFx0DltuCuyok= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574448275; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=iuvJrdkj+jiCFJbEGxVZxnxKHKNSAoCsIkt5jQKyK5o=; b=XQhpLt4t0W2y1Pzp1zGCnQgBqrnNJwbxM74szf/TJFn1HTUF7+TNXyHCToPccVCR6ShSW889fi4jW2eIN1vpGmYwznPSaxWtfjX+4MGY8wjc4E1FK6e2NExpuXSYX592ZF/QgALPcqTQXdIbbDid4U5fU0ztxNLIGVpYOxTUbRM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574448275784294.0910948481486; Fri, 22 Nov 2019 10:44:35 -0800 (PST) Received: from localhost ([::1]:54218 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDuu-0006c7-TH for importer@patchew.org; Fri, 22 Nov 2019 13:44:28 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39960) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDiN-0000gJ-MI for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:33 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iYDiM-0000MK-4S for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:31 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:24188 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iYDiL-0000MF-W2 for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:30 -0500 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-333-fj9fONsDO5ePGKTNViJrCw-1; Fri, 22 Nov 2019 13:31:27 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6E08F8018A1; Fri, 22 Nov 2019 18:31:25 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-37.ams2.redhat.com [10.36.116.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id F3CFE6E712; Fri, 22 Nov 2019 18:31:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574447489; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iuvJrdkj+jiCFJbEGxVZxnxKHKNSAoCsIkt5jQKyK5o=; b=jWJtKaRFKzWYErKHu+NKIS8j4zrVCw79cB5rnpYbIldoEb8qtn8U3aoj4sK81Cx8pQx/tg UIKIM0Tz6DRpB78TSSzjbJEJ+E0N4Ca01b5dMaHozSEcrDkBZyqE99XOpjZWIPEQqyUZ1R 7N67OKwR/gzqeuUQ+iSzRE/Nt/sA8/0= From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org, mst@redhat.com, armbru@redhat.com, jean-philippe.brucker@arm.com, bharatb.linux@gmail.com, yang.zhong@intel.com, dgilbert@redhat.com, quintela@redhat.com Subject: [PATCH for-5.0 v11 11/20] hw/arm/virt: Add the virtio-iommu device tree mappings Date: Fri, 22 Nov 2019 19:29:34 +0100 Message-Id: <20191122182943.4656-12-eric.auger@redhat.com> In-Reply-To: <20191122182943.4656-1-eric.auger@redhat.com> References: <20191122182943.4656-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: fj9fONsDO5ePGKTNViJrCw-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, peterx@redhat.com, tnowicki@marvell.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Adds the "virtio,pci-iommu" node in the host bridge node and the RID mapping, excluding the IOMMU RID. Signed-off-by: Eric Auger Reviewed-by: Jean-Philippe Brucker --- v10 -> v11: - remove msi_bypass v8 -> v9: - disable msi-bypass property - addition of the subnode is handled is the hotplug handler and IOMMU RID is notimposed anymore v6 -> v7: - align to the smmu instantiation code v4 -> v5: - VirtMachineClass no_iommu added in this patch - Use object_resolve_path_type --- hw/arm/virt.c | 53 +++++++++++++++++++++++++++++++----- hw/virtio/virtio-iommu-pci.c | 3 -- include/hw/arm/virt.h | 2 ++ 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/hw/arm/virt.c b/hw/arm/virt.c index d4bedc2607..cb6a95e7c8 100644 --- a/hw/arm/virt.c +++ b/hw/arm/virt.c @@ -32,6 +32,7 @@ #include "qemu-common.h" #include "qemu/units.h" #include "qemu/option.h" +#include "monitor/qdev.h" #include "qapi/error.h" #include "hw/sysbus.h" #include "hw/boards.h" @@ -54,6 +55,7 @@ #include "qemu/error-report.h" #include "qemu/module.h" #include "hw/pci-host/gpex.h" +#include "hw/virtio/virtio-pci.h" #include "hw/arm/sysbus-fdt.h" #include "hw/platform-bus.h" #include "hw/qdev-properties.h" @@ -71,6 +73,7 @@ #include "hw/mem/pc-dimm.h" #include "hw/mem/nvdimm.h" #include "hw/acpi/generic_event_device.h" +#include "hw/virtio/virtio-iommu.h" =20 #define DEFINE_VIRT_MACHINE_LATEST(major, minor, latest) \ static void virt_##major##_##minor##_class_init(ObjectClass *oc, \ @@ -1181,6 +1184,30 @@ static void create_smmu(const VirtMachineState *vms,= qemu_irq *pic, g_free(node); } =20 +static void create_virtio_iommu(VirtMachineState *vms, Error **errp) +{ + const char compat[] =3D "virtio,pci-iommu"; + uint16_t bdf =3D vms->virtio_iommu_bdf; + char *node; + + vms->iommu_phandle =3D qemu_fdt_alloc_phandle(vms->fdt); + + node =3D g_strdup_printf("%s/virtio_iommu@%d", vms->pciehb_nodename, b= df); + qemu_fdt_add_subnode(vms->fdt, node); + qemu_fdt_setprop(vms->fdt, node, "compatible", compat, sizeof(compat)); + qemu_fdt_setprop_sized_cells(vms->fdt, node, "reg", + 1, bdf << 8, 1, 0, 1, 0, + 1, 0, 1, 0); + + qemu_fdt_setprop_cell(vms->fdt, node, "#iommu-cells", 1); + qemu_fdt_setprop_cell(vms->fdt, node, "phandle", vms->iommu_phandle); + g_free(node); + + qemu_fdt_setprop_cells(vms->fdt, vms->pciehb_nodename, "iommu-map", + 0x0, vms->iommu_phandle, 0x0, bdf, + bdf + 1, vms->iommu_phandle, bdf + 1, 0xffff - = bdf); +} + static void create_pcie(VirtMachineState *vms, qemu_irq *pic) { hwaddr base_mmio =3D vms->memmap[VIRT_PCIE_MMIO].base; @@ -1258,7 +1285,7 @@ static void create_pcie(VirtMachineState *vms, qemu_i= rq *pic) } } =20 - nodename =3D g_strdup_printf("/pcie@%" PRIx64, base); + nodename =3D vms->pciehb_nodename =3D g_strdup_printf("/pcie@%" PRIx64= , base); qemu_fdt_add_subnode(vms->fdt, nodename); qemu_fdt_setprop_string(vms->fdt, nodename, "compatible", "pci-host-ecam-generic"); @@ -1301,13 +1328,17 @@ static void create_pcie(VirtMachineState *vms, qemu= _irq *pic) if (vms->iommu) { vms->iommu_phandle =3D qemu_fdt_alloc_phandle(vms->fdt); =20 - create_smmu(vms, pic, pci->bus); + switch (vms->iommu) { + case VIRT_IOMMU_SMMUV3: + create_smmu(vms, pic, pci->bus); + qemu_fdt_setprop_cells(vms->fdt, nodename, "iommu-map", + 0x0, vms->iommu_phandle, 0x0, 0x10000); + break; + default: + g_assert_not_reached(); + } =20 - qemu_fdt_setprop_cells(vms->fdt, nodename, "iommu-map", - 0x0, vms->iommu_phandle, 0x0, 0x10000); } - - g_free(nodename); } =20 static void create_platform_bus(VirtMachineState *vms, qemu_irq *pic) @@ -1972,6 +2003,13 @@ static void virt_machine_device_plug_cb(HotplugHandl= er *hotplug_dev, if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) { virt_memory_plug(hotplug_dev, dev, errp); } + if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) { + PCIDevice *pdev =3D PCI_DEVICE(dev); + + vms->iommu =3D VIRT_IOMMU_VIRTIO; + vms->virtio_iommu_bdf =3D pci_get_bdf(pdev); + create_virtio_iommu(vms, errp); + } } =20 static void virt_machine_device_unplug_request_cb(HotplugHandler *hotplug_= dev, @@ -1985,7 +2023,8 @@ static HotplugHandler *virt_machine_get_hotplug_handl= er(MachineState *machine, DeviceState *dev) { if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE) || - (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM))) { + (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) || + (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI))) { return HOTPLUG_HANDLER(machine); } =20 diff --git a/hw/virtio/virtio-iommu-pci.c b/hw/virtio/virtio-iommu-pci.c index 280230b31e..4cfae1f9df 100644 --- a/hw/virtio/virtio-iommu-pci.c +++ b/hw/virtio/virtio-iommu-pci.c @@ -31,9 +31,6 @@ struct VirtIOIOMMUPCI { =20 static Property virtio_iommu_pci_properties[] =3D { DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), - DEFINE_PROP_ARRAY("reserved-regions", VirtIOIOMMUPCI, - vdev.nb_reserved_regions, vdev.reserved_regions, - qdev_prop_interval, Interval), DEFINE_PROP_END_OF_LIST(), }; =20 diff --git a/include/hw/arm/virt.h b/include/hw/arm/virt.h index 0b41083e9d..32fb1142ef 100644 --- a/include/hw/arm/virt.h +++ b/include/hw/arm/virt.h @@ -124,8 +124,10 @@ typedef struct { bool virt; int32_t gic_version; VirtIOMMUType iommu; + uint16_t virtio_iommu_bdf; struct arm_boot_info bootinfo; MemMapEntry *memmap; + char *pciehb_nodename; const int *irqmap; int smp_cpus; void *fdt; --=20 2.20.1 From nobody Sun May 12 23:37:35 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1574448357; cv=none; d=zohomail.com; s=zohoarc; b=XHxxPWslGoBsasdB6SCpNDlFdhgHeT26onT9ytSW/gUUAl2bGlC4bbKD4vvYI4a0oLptS8OuYd7j0ah7tvSLDuz/EhEE9ys+fwm4oBM/C/865WCfzmbit8IlueQQZs27fiABAwUC9LoOWrfL+vSXaaRSck0qzcjDDXD1T7VxQLw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574448357; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=JrkR9KYpXJDWqiPpiZFZOE335FpagScjc50Vgro92e4=; b=ERtTU6dJD7FxoZ7SQYrSukAp4yIIZvRMYjGEbMGxcb/WZuan3z4zDZvQSCAi8U/Y86QXjBnI9K7lrneoD21xmjIPyC/cX3zNdGgtM8nqN0dubW+zK8ND2aWbUEdy11g8RhpMX2l90kdNY9vgYjvySFg/AGo1IJXS0La7JEAUygY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574448357085778.7279954155636; Fri, 22 Nov 2019 10:45:57 -0800 (PST) Received: from localhost ([::1]:54248 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDwH-000850-KA for importer@patchew.org; Fri, 22 Nov 2019 13:45:53 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39981) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDiQ-0000jb-3Z for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:35 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iYDiO-0000PT-I2 for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:33 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:38882 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iYDiO-0000PL-DN for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:32 -0500 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-315-zAd0-MfUMdie55aYUXHa8Q-1; Fri, 22 Nov 2019 13:31:31 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 39A591005512; Fri, 22 Nov 2019 18:31:29 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-37.ams2.redhat.com [10.36.116.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id C51926E712; Fri, 22 Nov 2019 18:31:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574447492; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JrkR9KYpXJDWqiPpiZFZOE335FpagScjc50Vgro92e4=; b=XL8I0v30sbPCta8qm3hVw9vooOlFW5pzcyIuDp9nydFnenqckXVdvvMVc0UnCmSRv/8/ai P70IsDus43/YBhZX+jHjbxMJFrNPBPiIuucoNjTk8CtGmetFLPWCsH3S4Wb9pT7dG+ZNfX OezCqejmrhWinj79CYPw7dvtdH6dRYk= From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org, mst@redhat.com, armbru@redhat.com, jean-philippe.brucker@arm.com, bharatb.linux@gmail.com, yang.zhong@intel.com, dgilbert@redhat.com, quintela@redhat.com Subject: [PATCH for-5.0 v11 12/20] qapi: Introduce DEFINE_PROP_INTERVAL Date: Fri, 22 Nov 2019 19:29:35 +0100 Message-Id: <20191122182943.4656-13-eric.auger@redhat.com> In-Reply-To: <20191122182943.4656-1-eric.auger@redhat.com> References: <20191122182943.4656-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: zAd0-MfUMdie55aYUXHa8Q-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, peterx@redhat.com, tnowicki@marvell.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Introduce a new property defining a labelled interval: ,,label. This will be used to encode reserved IOVA regions. The label is left undefined to ease reuse accross use cases. For instance, in virtio-iommu use case, reserved IOVA regions will be passed by the machine code to the virtio-iommu-pci device (an array of those). The label will match the virtio_iommu_probe_resv_mem subtype value: - VIRTIO_IOMMU_RESV_MEM_T_RESERVED (0) - VIRTIO_IOMMU_RESV_MEM_T_MSI (1) This is used to inform the virtio-iommu-pci device it should bypass the MSI region: 0xfee00000, 0xfeefffff, 1. Signed-off-by: Eric Auger --- hw/core/qdev-properties.c | 90 ++++++++++++++++++++++++++++++++++++ include/exec/memory.h | 6 +++ include/hw/qdev-properties.h | 3 ++ include/qemu/typedefs.h | 1 + 4 files changed, 100 insertions(+) diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c index ac28890e5a..8d70f34e37 100644 --- a/hw/core/qdev-properties.c +++ b/hw/core/qdev-properties.c @@ -13,6 +13,7 @@ #include "qapi/visitor.h" #include "chardev/char.h" #include "qemu/uuid.h" +#include "qemu/cutils.h" =20 void qdev_prop_set_after_realize(DeviceState *dev, const char *name, Error **errp) @@ -585,6 +586,95 @@ const PropertyInfo qdev_prop_macaddr =3D { .set =3D set_mac, }; =20 +/* --- Labelled Interval --- */ + +/* + * accepted syntax versions: + * ,, + * where low/high addresses are uint64_t in hexa (feat. 0x prefix) + * and type is an unsigned integer + */ +static void get_interval(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + DeviceState *dev =3D DEVICE(obj); + Property *prop =3D opaque; + Interval *interval =3D qdev_get_prop_ptr(dev, prop); + char buffer[64]; + char *p =3D buffer; + + snprintf(buffer, sizeof(buffer), "0x%"PRIx64",0x%"PRIx64",%d", + interval->low, interval->high, interval->type); + + visit_type_str(v, name, &p, errp); +} + +static void set_interval(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + DeviceState *dev =3D DEVICE(obj); + Property *prop =3D opaque; + Interval *interval =3D qdev_get_prop_ptr(dev, prop); + Error *local_err =3D NULL; + unsigned int type; + gchar **fields; + uint64_t addr; + char *str; + int ret; + + if (dev->realized) { + qdev_prop_set_after_realize(dev, name, errp); + return; + } + + visit_type_str(v, name, &str, &local_err); + if (local_err) { + error_propagate(errp, local_err); + return; + } + + fields =3D g_strsplit(str, ",", 3); + + ret =3D qemu_strtou64(fields[0], NULL, 16, &addr); + if (!ret) { + interval->low =3D addr; + } else { + error_setg(errp, "Failed to decode interval low addr"); + error_append_hint(errp, + "should be an address in hexa with 0x prefix\n"); + goto out; + } + + ret =3D qemu_strtou64(fields[1], NULL, 16, &addr); + if (!ret) { + interval->high =3D addr; + } else { + error_setg(errp, "Failed to decode interval high addr"); + error_append_hint(errp, + "should be an address in hexa with 0x prefix\n"); + goto out; + } + + ret =3D qemu_strtoui(fields[2], NULL, 10, &type); + if (!ret) { + interval->type =3D type; + } else { + error_setg(errp, "Failed to decode interval type"); + error_append_hint(errp, "should be an unsigned int in decimal\n"); + } +out: + g_free(str); + g_strfreev(fields); + return; +} + +const PropertyInfo qdev_prop_interval =3D { + .name =3D "labelled_interval", + .description =3D "Labelled interval, example: 0xFEE00000,0xFEEFFFFF,0", + .get =3D get_interval, + .set =3D set_interval, +}; + /* --- on/off/auto --- */ =20 const PropertyInfo qdev_prop_on_off_auto =3D { diff --git a/include/exec/memory.h b/include/exec/memory.h index e499dc215b..e238d1c352 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -57,6 +57,12 @@ struct MemoryRegionMmio { CPUWriteMemoryFunc *write[3]; }; =20 +struct Interval { + hwaddr low; + hwaddr high; + unsigned int type; +}; + typedef struct IOMMUTLBEntry IOMMUTLBEntry; =20 /* See address_space_translate: bit 0 is read, bit 1 is write. */ diff --git a/include/hw/qdev-properties.h b/include/hw/qdev-properties.h index c6a8cb5516..2ba7c8711b 100644 --- a/include/hw/qdev-properties.h +++ b/include/hw/qdev-properties.h @@ -20,6 +20,7 @@ extern const PropertyInfo qdev_prop_chr; extern const PropertyInfo qdev_prop_tpm; extern const PropertyInfo qdev_prop_ptr; extern const PropertyInfo qdev_prop_macaddr; +extern const PropertyInfo qdev_prop_interval; extern const PropertyInfo qdev_prop_on_off_auto; extern const PropertyInfo qdev_prop_losttickpolicy; extern const PropertyInfo qdev_prop_blockdev_on_error; @@ -202,6 +203,8 @@ extern const PropertyInfo qdev_prop_pcie_link_width; DEFINE_PROP(_n, _s, _f, qdev_prop_drive_iothread, BlockBackend *) #define DEFINE_PROP_MACADDR(_n, _s, _f) \ DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, MACAddr) +#define DEFINE_PROP_INTERVAL(_n, _s, _f) \ + DEFINE_PROP(_n, _s, _f, qdev_prop_interval, Interval) #define DEFINE_PROP_ON_OFF_AUTO(_n, _s, _f, _d) \ DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_on_off_auto, OnOffAuto) #define DEFINE_PROP_LOSTTICKPOLICY(_n, _s, _f, _d) \ diff --git a/include/qemu/typedefs.h b/include/qemu/typedefs.h index 375770a80f..a827c9a3fe 100644 --- a/include/qemu/typedefs.h +++ b/include/qemu/typedefs.h @@ -58,6 +58,7 @@ typedef struct ISABus ISABus; typedef struct ISADevice ISADevice; typedef struct IsaDma IsaDma; typedef struct MACAddr MACAddr; +typedef struct Interval Interval; typedef struct MachineClass MachineClass; typedef struct MachineState MachineState; typedef struct MemoryListener MemoryListener; --=20 2.20.1 From nobody Sun May 12 23:37:35 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1574448580; cv=none; d=zohomail.com; s=zohoarc; b=n/ED5tYOQ/Sp8+1I5/uOEe4lrR+DfrgluheZHWaQ/FlfA3+j0L/JRmcMvIQG73nSDx6X2zzx2DQ0GwEwv/g4K8SbWOglx6T1XOPKQxm/uu7aLUw5QBicbsX8XY2dYXc7SVjLT1VAiivrgWlWGtDhyPPsHV5wUPSxIJ9nBKyKGaU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574448580; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=tigei1AT8gTpq4W3JA9zJ/vbvpERCvoUOccYM5U+v/U=; b=Tf1SdT2aeKL4CRiiL9txVrdqPUPv8NrLAA+/+kDPx/xyPJ5FsI2MTch04yXmzJlbNwX6C8v2i9TYGmjjLa2xysHkjT8N33M795KHwBgCsIkRJue7B1woWOXrXPkxTfarWRiCh4X+mezQDuzwrWAG6Tn6C32cfgqzCZvZj6zpez8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574448580133827.8542262963686; Fri, 22 Nov 2019 10:49:40 -0800 (PST) Received: from localhost ([::1]:54270 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDzt-0002TK-U2 for importer@patchew.org; Fri, 22 Nov 2019 13:49:37 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40011) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDiW-0000sh-DN for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:41 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iYDiU-0000S8-UR for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:40 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:42624 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iYDiU-0000Ro-QN for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:38 -0500 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-356-ttrazPEAP1K2IIP8UOTcJg-1; Fri, 22 Nov 2019 13:31:37 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A51478018A1; Fri, 22 Nov 2019 18:31:35 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-37.ams2.redhat.com [10.36.116.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8D4446E712; Fri, 22 Nov 2019 18:31:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574447498; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tigei1AT8gTpq4W3JA9zJ/vbvpERCvoUOccYM5U+v/U=; b=UmszZgCX2Sos7s+1yjgp3pRLCUA7uH4vJJ8OzeLdmSQvdWPdp3ngtV61R9F5YhPE6qlEvK LQjAnaUpjECAHS7sXZg7urd6dr5YKmAzOLqAGAxnm7f/ITJcTaJJSnHXMFdlq7BPIqoTzD T6qaWzzHWDX3JFFGNDkAQvjOBxyilCY= From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org, mst@redhat.com, armbru@redhat.com, jean-philippe.brucker@arm.com, bharatb.linux@gmail.com, yang.zhong@intel.com, dgilbert@redhat.com, quintela@redhat.com Subject: [PATCH for-5.0 v11 13/20] virtio-iommu: Implement probe request Date: Fri, 22 Nov 2019 19:29:36 +0100 Message-Id: <20191122182943.4656-14-eric.auger@redhat.com> In-Reply-To: <20191122182943.4656-1-eric.auger@redhat.com> References: <20191122182943.4656-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: ttrazPEAP1K2IIP8UOTcJg-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 205.139.110.61 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, peterx@redhat.com, tnowicki@marvell.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This patch implements the PROBE request. At the moment, no reserved regions are returned as none are registered per device. Only a NONE property is returned. Signed-off-by: Eric Auger Reviewed-by: Jean-Philippe Brucker Reviewed-by: Peter Xu --- v9 -> v10 - fully rewrite the code in preparation of reserved_regions array property introduction v8 -> v9: - fix filling of properties (changes induced by v0.7 -> v0.8 spec evolution) - return VIRTIO_IOMMU_S_INVAL in case of error v7 -> v8: - adapt to removal of value filed in virtio_iommu_probe_property v6 -> v7: - adapt to the change in virtio_iommu_probe_resv_mem fields - use get_endpoint() instead of directly checking the EP was registered. v4 -> v5: - initialize bufstate.error to false - add cpu_to_le64(size) --- hw/virtio/trace-events | 1 + hw/virtio/virtio-iommu.c | 89 +++++++++++++++++++++++++++++++- include/hw/virtio/virtio-iommu.h | 2 + 3 files changed, 90 insertions(+), 2 deletions(-) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index a572eb71aa..b7bc8ac6d1 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -74,3 +74,4 @@ virtio_iommu_get_domain(uint32_t domain_id) "Alloc domain= =3D%d" virtio_iommu_put_domain(uint32_t domain_id) "Free domain=3D%d" virtio_iommu_translate_out(uint64_t virt_addr, uint64_t phys_addr, uint32_= t sid) "0x%"PRIx64" -> 0x%"PRIx64 " for sid=3D%d" virtio_iommu_report_fault(uint8_t reason, uint32_t flags, uint32_t endpoin= t, uint64_t addr) "FAULT reason=3D%d flags=3D%d endpoint=3D%d address =3D0x= %"PRIx64 +virtio_iommu_fill_resv_property(uint32_t devid, uint8_t subtype, uint64_t = start, uint64_t end) "dev=3D %d, type=3D%d start=3D0x%"PRIx64" end=3D0x%"PR= Ix64 diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index 723616a5db..1ce2218935 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -38,6 +38,7 @@ =20 /* Max size */ #define VIOMMU_DEFAULT_QUEUE_SIZE 256 +#define VIOMMU_PROBE_SIZE 512 =20 typedef struct viommu_domain { uint32_t id; @@ -317,6 +318,61 @@ static int virtio_iommu_unmap(VirtIOIOMMU *s, return ret; } =20 +static ssize_t virtio_iommu_fill_resv_mem_prop(VirtIOIOMMU *s, uint32_t ep, + uint8_t *buf, size_t free) +{ + struct virtio_iommu_probe_resv_mem prop =3D {}; + size_t size =3D sizeof(prop), length =3D size - sizeof(prop.head), tot= al; + int i; + + total =3D size * s->nb_reserved_regions; + + if (total > free) { + return -ENOSPC; + } + + for (i =3D 0; i < s->nb_reserved_regions; i++) { + prop.head.type =3D VIRTIO_IOMMU_PROBE_T_RESV_MEM; + prop.head.length =3D cpu_to_le64(length); + prop.subtype =3D cpu_to_le64(s->reserved_regions[i].type); + prop.start =3D cpu_to_le64(s->reserved_regions[i].low); + prop.end =3D cpu_to_le64(s->reserved_regions[i].high); + + memcpy(buf, &prop, size); + + trace_virtio_iommu_fill_resv_property(ep, prop.subtype, + prop.start, prop.end); + buf +=3D size; + } + return total; +} + +/** + * virtio_iommu_probe - Fill the probe request buffer with + * the properties the device is able to return and add a NONE + * property at the end. + */ +static int virtio_iommu_probe(VirtIOIOMMU *s, + struct virtio_iommu_req_probe *req, + uint8_t *buf) +{ + uint32_t ep_id =3D le32_to_cpu(req->endpoint); + struct virtio_iommu_probe_property last =3D {}; + size_t free =3D VIOMMU_PROBE_SIZE - sizeof(last); + ssize_t count; + + count =3D virtio_iommu_fill_resv_mem_prop(s, ep_id, buf, free); + if (count < 0) { + return VIRTIO_IOMMU_S_INVAL; + } + buf +=3D count; + free -=3D count; + + memcpy(buf, &last, sizeof(last)); + + return VIRTIO_IOMMU_S_OK; +} + static int virtio_iommu_iov_to_req(struct iovec *iov, unsigned int iov_cnt, void *req, size_t req_sz) @@ -346,6 +402,17 @@ virtio_iommu_handle_req(detach) virtio_iommu_handle_req(map) virtio_iommu_handle_req(unmap) =20 +static int virtio_iommu_handle_probe(VirtIOIOMMU *s, + struct iovec *iov, + unsigned int iov_cnt, + uint8_t *buf) +{ + struct virtio_iommu_req_probe req; + int ret =3D virtio_iommu_iov_to_req(iov, iov_cnt, &req, sizeof(req)); + + return ret ? ret : virtio_iommu_probe(s, &req, buf); +} + static void virtio_iommu_handle_command(VirtIODevice *vdev, VirtQueue *vq) { VirtIOIOMMU *s =3D VIRTIO_IOMMU(vdev); @@ -391,17 +458,33 @@ static void virtio_iommu_handle_command(VirtIODevice = *vdev, VirtQueue *vq) case VIRTIO_IOMMU_T_UNMAP: tail.status =3D virtio_iommu_handle_unmap(s, iov, iov_cnt); break; + case VIRTIO_IOMMU_T_PROBE: + { + struct virtio_iommu_req_tail *ptail; + uint8_t *buf =3D g_malloc0(s->config.probe_size + sizeof(tail)= ); + + ptail =3D (struct virtio_iommu_req_tail *) + (buf + s->config.probe_size); + ptail->status =3D virtio_iommu_handle_probe(s, iov, iov_cnt, b= uf); + + sz =3D iov_from_buf(elem->in_sg, elem->in_num, 0, + buf, s->config.probe_size + sizeof(tail)); + g_free(buf); + assert(sz =3D=3D s->config.probe_size + sizeof(tail)); + goto push; + } default: tail.status =3D VIRTIO_IOMMU_S_UNSUPP; } - qemu_mutex_unlock(&s->mutex); =20 out: sz =3D iov_from_buf(elem->in_sg, elem->in_num, 0, &tail, sizeof(tail)); assert(sz =3D=3D sizeof(tail)); =20 - virtqueue_push(vq, elem, sizeof(tail)); +push: + qemu_mutex_unlock(&s->mutex); + virtqueue_push(vq, elem, sz); virtio_notify(vdev, vq); g_free(elem); } @@ -624,6 +707,7 @@ static void virtio_iommu_device_realize(DeviceState *de= v, Error **errp) s->config.page_size_mask =3D TARGET_PAGE_MASK; s->config.input_range.end =3D -1UL; s->config.domain_range.end =3D 32; + s->config.probe_size =3D VIOMMU_PROBE_SIZE; =20 virtio_add_feature(&s->features, VIRTIO_RING_F_EVENT_IDX); virtio_add_feature(&s->features, VIRTIO_RING_F_INDIRECT_DESC); @@ -633,6 +717,7 @@ static void virtio_iommu_device_realize(DeviceState *de= v, Error **errp) virtio_add_feature(&s->features, VIRTIO_IOMMU_F_MAP_UNMAP); virtio_add_feature(&s->features, VIRTIO_IOMMU_F_BYPASS); virtio_add_feature(&s->features, VIRTIO_IOMMU_F_MMIO); + virtio_add_feature(&s->features, VIRTIO_IOMMU_F_PROBE); =20 qemu_mutex_init(&s->mutex); =20 diff --git a/include/hw/virtio/virtio-iommu.h b/include/hw/virtio/virtio-io= mmu.h index 1ab6993d29..4176785368 100644 --- a/include/hw/virtio/virtio-iommu.h +++ b/include/hw/virtio/virtio-iommu.h @@ -57,6 +57,8 @@ typedef struct VirtIOIOMMU { GHashTable *as_by_busptr; IOMMUPciBus *as_by_bus_num[IOMMU_PCI_BUS_MAX]; PCIBus *primary_bus; + Interval *reserved_regions; + uint32_t nb_reserved_regions; GTree *domains; QemuMutex mutex; GTree *endpoints; --=20 2.20.1 From nobody Sun May 12 23:37:35 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1574448678; cv=none; d=zohomail.com; s=zohoarc; b=gtvDgQsHC1E/cJUgs76T7XlGvrCmnZ2A1DpZWoRLbqta8sTq1ty4G5UqplvLC2xd3y/fKGsJ0Jx8R6MouDhpe2hvQopwSLZffOoBqMsSsI2KnoUv+9VR0d4lhg3F7CHmsRbA+eGiTOsCjp/p5/dpUNxsCvJtNdJk5VRM6bum3Ro= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574448678; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=B76/q/QIgTbwaStE7V12zNd/AKuPjF8S7ms1RzIMaeM=; b=A3rGkAfAhYOgtFFcEzBb0ipiumU3/2sghRTE1/oiA9x819QQg9NTFX/ndRYVth1557dtR4e/Ma73HfoNC2Akqh4bcxVydXeUU9hFhv7ev1K0rZlI/0Q+wUGrrEZSTWPD/TNqYlBvAcVOnBQyEIt4I9rQXR2pQLekhjUAHo6XX1w= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574448678682407.9687397229327; Fri, 22 Nov 2019 10:51:18 -0800 (PST) Received: from localhost ([::1]:54298 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYE1V-0004gW-AF for importer@patchew.org; Fri, 22 Nov 2019 13:51:17 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40050) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDif-00014q-My for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:50 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iYDie-0000Vf-IX for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:49 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:52146 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iYDie-0000VP-EK for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:48 -0500 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-37-wxLWuDKoNRWCfV2kziUo7w-1; Fri, 22 Nov 2019 13:31:47 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id F30C5107ACE3; Fri, 22 Nov 2019 18:31:44 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-37.ams2.redhat.com [10.36.116.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id 047636E71C; Fri, 22 Nov 2019 18:31:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574447508; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=B76/q/QIgTbwaStE7V12zNd/AKuPjF8S7ms1RzIMaeM=; b=MCZQe0VPv76ufCszZYkGiRAFJmxdF+daNnLLeCifnPssblKWNhEitmFUsR8NFtAmF32CYG 2fHNOlTosCcbb8IuOjnnxUKDfJ6lglt2skbU8sZ8/WagxkO9dJe3w6Ws2wgbhTverQEqOO S4z2OvC3+erF4BXIqOja7bbO9OAwrFQ= From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org, mst@redhat.com, armbru@redhat.com, jean-philippe.brucker@arm.com, bharatb.linux@gmail.com, yang.zhong@intel.com, dgilbert@redhat.com, quintela@redhat.com Subject: [PATCH for-5.0 v11 14/20] virtio-iommu: Handle reserved regions in the translation process Date: Fri, 22 Nov 2019 19:29:37 +0100 Message-Id: <20191122182943.4656-15-eric.auger@redhat.com> In-Reply-To: <20191122182943.4656-1-eric.auger@redhat.com> References: <20191122182943.4656-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: wxLWuDKoNRWCfV2kziUo7w-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, peterx@redhat.com, tnowicki@marvell.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" When translating an address we need to check if it belongs to a reserved virtual address range. If it does, there are 2 cases: - it belongs to a RESERVED region: the guest should neither use this address in a MAP not instruct the end-point to DMA on them. We report an error - It belongs to an MSI region: we bypass the translation. Signed-off-by: Eric Auger --- v10 -> v11: - directly use the reserved_regions properties array v9 -> v10: - in case of MSI region, we immediatly return --- hw/virtio/virtio-iommu.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index 1ce2218935..c5b202fab7 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -548,6 +548,7 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemory= Region *mr, hwaddr addr, uint32_t sid, flags; bool bypass_allowed; bool found; + int i; =20 interval.low =3D addr; interval.high =3D addr + 1; @@ -580,6 +581,22 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemor= yRegion *mr, hwaddr addr, goto unlock; } =20 + for (i =3D 0; i < s->nb_reserved_regions; i++) { + if (interval.low >=3D s->reserved_regions[i].low && + interval.low <=3D s->reserved_regions[i].high) { + switch (s->reserved_regions[i].type) { + case VIRTIO_IOMMU_RESV_MEM_T_MSI: + entry.perm =3D flag; + goto unlock; + case VIRTIO_IOMMU_RESV_MEM_T_RESERVED: + default: + virtio_iommu_report_fault(s, VIRTIO_IOMMU_FAULT_R_MAPPING, + 0, sid, addr); + goto unlock; + } + } + } + if (!ep->domain) { if (!bypass_allowed) { qemu_log_mask(LOG_GUEST_ERROR, --=20 2.20.1 From nobody Sun May 12 23:37:35 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1574448824; cv=none; d=zohomail.com; s=zohoarc; b=SH9/UgK2T2rARxqGU3p3OHxnNO2yV6B1WY637djc64OMII+pk/6CN+r1hu/GbXmgJLaJNQNmauLNhzKZxSde38rWqU5zzrXQ0f3eoXoK/4HKUYmiy5jhif2L6EtsnTEIHIYCNd0n4I9+XahkN50IENhqCh2rAmZ/lDi7220AmvM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574448824; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=FhRwI/WfLYzUQvZHwKeIbcL5ij+ifra/E2ARLgqQG1Y=; b=mKdUK8Hr8++1jR9wXslnt+qn+fz7IcfvsrQVYC1RKviFfJS2wzsTEaTsNOEu/ggnUCil6gT7duxKT/Ti5Ap1gmVEXpDoRbxWgLl1O/cNamuI+qrPfE4IlIrzxA9mS4apKjpz50UvLjQsqzmO1QaPIAgxwKUUfAWwnfKel6D6VgY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 157444882453917.137219555029674; Fri, 22 Nov 2019 10:53:44 -0800 (PST) Received: from localhost ([::1]:54356 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYE3r-0008PJ-Dh for importer@patchew.org; Fri, 22 Nov 2019 13:53:43 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40078) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDii-00019l-T7 for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:53 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iYDih-0000Yk-SD for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:52 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:39155 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iYDih-0000Xo-OA for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:51 -0500 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-438-fnO2wUozNTiU70nlouFa9Q-1; Fri, 22 Nov 2019 13:31:50 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id AF3E58024C0; Fri, 22 Nov 2019 18:31:48 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-37.ams2.redhat.com [10.36.116.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4E55A6E71C; Fri, 22 Nov 2019 18:31:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574447511; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FhRwI/WfLYzUQvZHwKeIbcL5ij+ifra/E2ARLgqQG1Y=; b=R4EI2U/fi7CDekkE4i1U4Cerw8IxyPMWyUvHjR+3z6KgdSVRAeLW7XW5ObAPksWI58h79i +GvWCGDfJy4vHV0YFX8nta0+Hn9eH/2BdHIRL4/l2bfWcum+OF5TE7c7VL7BWPbR32dG4Q FcX0o/KJAGeuRv+Apbrz0qIQdWM7WQ4= From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org, mst@redhat.com, armbru@redhat.com, jean-philippe.brucker@arm.com, bharatb.linux@gmail.com, yang.zhong@intel.com, dgilbert@redhat.com, quintela@redhat.com Subject: [PATCH for-5.0 v11 15/20] virtio-iommu-pci: Add array of Interval properties Date: Fri, 22 Nov 2019 19:29:38 +0100 Message-Id: <20191122182943.4656-16-eric.auger@redhat.com> In-Reply-To: <20191122182943.4656-1-eric.auger@redhat.com> References: <20191122182943.4656-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: fnO2wUozNTiU70nlouFa9Q-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 205.139.110.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, peterx@redhat.com, tnowicki@marvell.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" The machine may need to pass reserved regions to the virtio-iommu-pci device (such as the MSI window on x86). So let's add an array of Interval properties. Signed-off-by: Eric Auger Reviewed-by: Jean-Philippe Brucker --- hw/virtio/virtio-iommu-pci.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/hw/virtio/virtio-iommu-pci.c b/hw/virtio/virtio-iommu-pci.c index 4cfae1f9df..280230b31e 100644 --- a/hw/virtio/virtio-iommu-pci.c +++ b/hw/virtio/virtio-iommu-pci.c @@ -31,6 +31,9 @@ struct VirtIOIOMMUPCI { =20 static Property virtio_iommu_pci_properties[] =3D { DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), + DEFINE_PROP_ARRAY("reserved-regions", VirtIOIOMMUPCI, + vdev.nb_reserved_regions, vdev.reserved_regions, + qdev_prop_interval, Interval), DEFINE_PROP_END_OF_LIST(), }; =20 --=20 2.20.1 From nobody Sun May 12 23:37:35 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1574448030; cv=none; d=zohomail.com; s=zohoarc; b=QdofrIy8RjXJ/tAxq1amrMlQgW9ebX6NRpGSNbHuDFZEiGdNaQHZVcyvZ+gN5klZKPDUbExRiHEwxetH7A1Q2l0L1mfv0qFpBEMZ4sqSPpmiIyA0e6j59i0wikfBUKKHd8NwdJGVupv12IxvEeEellaREY3ymAGybn5UB31vw7A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574448030; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=s9rjhk/7jHqlU0LM08VfBejpiGVoEQ1gFejwY9kPP/8=; b=iUFL+avhc6MmXrIiZFtrdMUf+RDmEUdCKFkY+CEddIdazJDVr5XbdUuovsGpuQWDtOSDq+EvCYtdbnyG0U84ZMYm9SXnkJ16cnf/Jkx544TrUrLPsdhwP+MsAArvXI/rEKlHcKhnred+Jv0UKw2qE1vunNRvJaSDc4Uu12QOJxE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574448030809312.47548441737536; Fri, 22 Nov 2019 10:40:30 -0800 (PST) Received: from localhost ([::1]:54162 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDr1-0001xl-BK for importer@patchew.org; Fri, 22 Nov 2019 13:40:27 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40128) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDip-0001KN-Ud for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:32:01 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iYDio-0000cL-FN for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:59 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:56098 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iYDio-0000bv-9f for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:31:58 -0500 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-10-V4LzwQZ-MhezUPBWDkZPYg-1; Fri, 22 Nov 2019 13:31:56 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id F3D64107ACC5; Fri, 22 Nov 2019 18:31:54 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-37.ams2.redhat.com [10.36.116.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0E4B06E712; Fri, 22 Nov 2019 18:31:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574447517; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=s9rjhk/7jHqlU0LM08VfBejpiGVoEQ1gFejwY9kPP/8=; b=ieQw3ETEbG9lTvrdUr76XGhsbzd+k02DXftMjMIpX0MlRZgtiXwf1SKUOUpB4tOmb6eqEl sgXC9XcudCu++BvZKTlS1UdvtaNs5vhlN8beHMEdM48hlm55JOIVw+7asD9JjKMUm3296g F+O0k/0wd8kQf+Hl+FXZn6iVfaxPu8Q= From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org, mst@redhat.com, armbru@redhat.com, jean-philippe.brucker@arm.com, bharatb.linux@gmail.com, yang.zhong@intel.com, dgilbert@redhat.com, quintela@redhat.com Subject: [PATCH for-5.0 v11 16/20] hw/arm/virt-acpi-build: Introduce fill_iort_idmap helper Date: Fri, 22 Nov 2019 19:29:39 +0100 Message-Id: <20191122182943.4656-17-eric.auger@redhat.com> In-Reply-To: <20191122182943.4656-1-eric.auger@redhat.com> References: <20191122182943.4656-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: V4LzwQZ-MhezUPBWDkZPYg-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 205.139.110.61 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, peterx@redhat.com, tnowicki@marvell.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" To avoid code duplication, let's introduce an helper that fills one IORT ID mappings array index. Signed-off-by: Eric Auger Reviewed-by: Jean-Philippe Brucker --- v8: new --- hw/arm/virt-acpi-build.c | 43 ++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/hw/arm/virt-acpi-build.c b/hw/arm/virt-acpi-build.c index 4cd50175e0..825f3a79c0 100644 --- a/hw/arm/virt-acpi-build.c +++ b/hw/arm/virt-acpi-build.c @@ -368,6 +368,17 @@ static void acpi_dsdt_add_power_button(Aml *scope) aml_append(scope, dev); } =20 +static inline void +fill_iort_idmap(AcpiIortIdMapping *idmap, int i, + uint32_t input_base, uint32_t id_count, + uint32_t output_base, uint32_t output_reference) +{ + idmap[i].input_base =3D cpu_to_le32(input_base); + idmap[i].id_count =3D cpu_to_le32(id_count); + idmap[i].output_base =3D cpu_to_le32(output_base); + idmap[i].output_reference =3D cpu_to_le32(output_reference); +} + static void build_iort(GArray *table_data, BIOSLinker *linker, VirtMachineState *vms) { @@ -426,13 +437,12 @@ build_iort(GArray *table_data, BIOSLinker *linker, Vi= rtMachineState *vms) smmu->gerr_gsiv =3D cpu_to_le32(irq + 2); smmu->sync_gsiv =3D cpu_to_le32(irq + 3); =20 - /* Identity RID mapping covering the whole input RID range */ - idmap =3D &smmu->id_mapping_array[0]; - idmap->input_base =3D 0; - idmap->id_count =3D cpu_to_le32(0xFFFF); - idmap->output_base =3D 0; - /* output IORT node is the ITS group node (the first node) */ - idmap->output_reference =3D cpu_to_le32(iort_node_offset); + /* + * Identity RID mapping covering the whole input RID range. + * The output IORT node is the ITS group node (the first node). + */ + fill_iort_idmap(smmu->id_mapping_array, 0, 0, 0xffff, 0, + iort_node_offset); } =20 /* Root Complex Node */ @@ -450,18 +460,17 @@ build_iort(GArray *table_data, BIOSLinker *linker, Vi= rtMachineState *vms) rc->memory_properties.memory_flags =3D 0x3; /* CCA =3D CPM =3D DCAS = =3D 1 */ rc->pci_segment_number =3D 0; /* MCFG pci_segment */ =20 - /* Identity RID mapping covering the whole input RID range */ - idmap =3D &rc->id_mapping_array[0]; - idmap->input_base =3D 0; - idmap->id_count =3D cpu_to_le32(0xFFFF); - idmap->output_base =3D 0; - if (vms->iommu =3D=3D VIRT_IOMMU_SMMUV3) { - /* output IORT node is the smmuv3 node */ - idmap->output_reference =3D cpu_to_le32(smmu_offset); + /* Identity RID mapping and output IORT node is the iommu node */ + fill_iort_idmap(rc->id_mapping_array, 0, 0, 0xFFFF, 0, + smmu_offset); } else { - /* output IORT node is the ITS group node (the first node) */ - idmap->output_reference =3D cpu_to_le32(iort_node_offset); + /* + * Identity RID mapping and the output IORT node is the ITS group + * node (the first node). + */ + fill_iort_idmap(rc->id_mapping_array, 0, 0, 0xFFFF, 0, + iort_node_offset); } =20 /* --=20 2.20.1 From nobody Sun May 12 23:37:35 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1574448914; cv=none; d=zohomail.com; s=zohoarc; b=FPhb15MP1ks556uw8DP79UaeNkRjDC1qvHvuAesQbYVSNT8dvRd7AtdJZmEVFCtp1HHYs4kRwYkAn0CRLjfRa4f65z83Cnu951QjD0lrUH3SXOum7jfipSc/iGrELJwHw7/XHoN1krsS4PUpnf7Un+8LjUmSHJyEtmfUlcDYezc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574448914; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=4xFawQT2V6zPzd9LUbAi8pc+Qp3elmFlQpDHpoRueTI=; b=Sano60JR3O9fE7wr1UfaaB6hnt0wOqTDsVHNre7a620Sr8bJHWye8Jxs9FQ5CGlmX2QsJdPy98S+CcI0m5CV+VazKhtMygBP7GrIHcs9AQH3xv6TC59z645p7k1fcdwhMNpX974bbc9RhZQ3Tl8a2i7GVQ6khBKzKy4uvdeq4x4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574448914115814.519783191818; Fri, 22 Nov 2019 10:55:14 -0800 (PST) Received: from localhost ([::1]:54378 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYE5H-0001W3-5N for importer@patchew.org; Fri, 22 Nov 2019 13:55:11 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40190) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDiz-0001a4-1j for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:32:10 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iYDix-0000g9-Ij for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:32:08 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:30879 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iYDix-0000fw-78 for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:32:07 -0500 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-124-JC4ZDa6mOye60nSHeAuYKA-1; Fri, 22 Nov 2019 13:32:05 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1726C107ACC5; Fri, 22 Nov 2019 18:32:04 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-37.ams2.redhat.com [10.36.116.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5559D6E712; Fri, 22 Nov 2019 18:31:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574447526; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4xFawQT2V6zPzd9LUbAi8pc+Qp3elmFlQpDHpoRueTI=; b=KufSrxz0cSNTt9y5/nXcVjOccy4uvIhZIcaUV8SpSUncMImctZi15k95DX1ZrmHwLbDpCQ sR2iYGWHa6a6lbXdrXKF+ooLxwRgBKEdvFZqREfug+w6OBNGFbxz6cDW5B3DiWaiXCvL00 /Ayy9nSMfD2KFhS20x1/pkpIUf+85yY= From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org, mst@redhat.com, armbru@redhat.com, jean-philippe.brucker@arm.com, bharatb.linux@gmail.com, yang.zhong@intel.com, dgilbert@redhat.com, quintela@redhat.com Subject: [PATCH for-5.0 v11 17/20] hw/arm/virt-acpi-build: Add virtio-iommu node in IORT table Date: Fri, 22 Nov 2019 19:29:40 +0100 Message-Id: <20191122182943.4656-18-eric.auger@redhat.com> In-Reply-To: <20191122182943.4656-1-eric.auger@redhat.com> References: <20191122182943.4656-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: JC4ZDa6mOye60nSHeAuYKA-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 205.139.110.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, peterx@redhat.com, tnowicki@marvell.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This patch builds the virtio-iommu node in the ACPI IORT table. The RID space of the root complex, which spans 0x0-0x10000 maps to streamid space 0x0-0x10000 in the virtio-iommu which in turn maps to deviceid space 0x0-0x10000 in the ITS group. The iommu RID is excluded as described in virtio-iommu specification. Signed-off-by: Eric Auger Reviewed-by: Jean-Philippe Brucker --- v8 -> v9: - iommu RID is not fixed anymore v7 -> v8: - exclude the iommu RID (0x8) in the root complex ID mapping --- hw/arm/virt-acpi-build.c | 50 ++++++++++++++++++++++++++++++------- include/hw/acpi/acpi-defs.h | 21 +++++++++++++++- 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/hw/arm/virt-acpi-build.c b/hw/arm/virt-acpi-build.c index 825f3a79c0..1e22cbbbfd 100644 --- a/hw/arm/virt-acpi-build.c +++ b/hw/arm/virt-acpi-build.c @@ -386,14 +386,14 @@ build_iort(GArray *table_data, BIOSLinker *linker, Vi= rtMachineState *vms) AcpiIortIdMapping *idmap; AcpiIortItsGroup *its; AcpiIortTable *iort; - AcpiIortSmmu3 *smmu; - size_t node_size, iort_node_offset, iort_length, smmu_offset =3D 0; + size_t node_size, iort_node_offset, iort_length, iommu_offset =3D 0; AcpiIortRC *rc; + int nb_rc_idmappings =3D 1; =20 iort =3D acpi_data_push(table_data, sizeof(*iort)); =20 - if (vms->iommu =3D=3D VIRT_IOMMU_SMMUV3) { - nb_nodes =3D 3; /* RC, ITS, SMMUv3 */ + if (vms->iommu) { + nb_nodes =3D 3; /* RC, ITS, IOMMU */ } else { nb_nodes =3D 2; /* RC, ITS */ } @@ -419,9 +419,9 @@ build_iort(GArray *table_data, BIOSLinker *linker, Virt= MachineState *vms) =20 if (vms->iommu =3D=3D VIRT_IOMMU_SMMUV3) { int irq =3D vms->irqmap[VIRT_SMMU] + ARM_SPI_BASE; + AcpiIortSmmu3 *smmu; =20 - /* SMMUv3 node */ - smmu_offset =3D iort_node_offset + node_size; + iommu_offset =3D iort_node_offset + node_size; node_size =3D sizeof(*smmu) + sizeof(*idmap); iort_length +=3D node_size; smmu =3D acpi_data_push(table_data, node_size); @@ -443,16 +443,38 @@ build_iort(GArray *table_data, BIOSLinker *linker, Vi= rtMachineState *vms) */ fill_iort_idmap(smmu->id_mapping_array, 0, 0, 0xffff, 0, iort_node_offset); + } else if (vms->iommu =3D=3D VIRT_IOMMU_VIRTIO) { + AcpiIortPVIommuPCI *iommu; + + nb_rc_idmappings =3D 2; + iommu_offset =3D iort_node_offset + node_size; + node_size =3D sizeof(*iommu) + 2 * sizeof(*idmap); + iort_length +=3D node_size; + iommu =3D acpi_data_push(table_data, node_size); + + iommu->type =3D ACPI_IORT_NODE_PARAVIRT; + iommu->length =3D cpu_to_le16(node_size); + iommu->mapping_count =3D cpu_to_le32(2); + iommu->mapping_offset =3D cpu_to_le32(sizeof(*iommu)); + iommu->devid =3D cpu_to_le32(vms->virtio_iommu_bdf); + iommu->model =3D cpu_to_le32(ACPI_IORT_NODE_PV_VIRTIO_IOMMU_PCI); + + /* + * Identity RID mapping covering the whole input RID range + * output IORT node is the ITS group node (the first node) + */ + fill_iort_idmap(iommu->id_mapping_array, 0, 0, 0xffff, 0, + iort_node_offset); } =20 /* Root Complex Node */ - node_size =3D sizeof(*rc) + sizeof(*idmap); + node_size =3D sizeof(*rc) + nb_rc_idmappings * sizeof(*idmap); iort_length +=3D node_size; rc =3D acpi_data_push(table_data, node_size); =20 rc->type =3D ACPI_IORT_NODE_PCI_ROOT_COMPLEX; rc->length =3D cpu_to_le16(node_size); - rc->mapping_count =3D cpu_to_le32(1); + rc->mapping_count =3D cpu_to_le32(nb_rc_idmappings); rc->mapping_offset =3D cpu_to_le32(sizeof(*rc)); =20 /* fully coherent device */ @@ -463,7 +485,17 @@ build_iort(GArray *table_data, BIOSLinker *linker, Vir= tMachineState *vms) if (vms->iommu =3D=3D VIRT_IOMMU_SMMUV3) { /* Identity RID mapping and output IORT node is the iommu node */ fill_iort_idmap(rc->id_mapping_array, 0, 0, 0xFFFF, 0, - smmu_offset); + iommu_offset); + } else if (vms->iommu =3D=3D VIRT_IOMMU_VIRTIO) { + /* + * Identity mapping with the IOMMU RID (0x8) excluded. The output + * IORT node is the iommu node. + */ + fill_iort_idmap(rc->id_mapping_array, 0, 0, vms->virtio_iommu_bdf,= 0, + iommu_offset); + fill_iort_idmap(rc->id_mapping_array, 1, vms->virtio_iommu_bdf + 1, + 0xFFFF - vms->virtio_iommu_bdf, + vms->virtio_iommu_bdf + 1, iommu_offset); } else { /* * Identity RID mapping and the output IORT node is the ITS group diff --git a/include/hw/acpi/acpi-defs.h b/include/hw/acpi/acpi-defs.h index 57a3f58b0c..ba06f41fc0 100644 --- a/include/hw/acpi/acpi-defs.h +++ b/include/hw/acpi/acpi-defs.h @@ -581,7 +581,8 @@ enum { ACPI_IORT_NODE_NAMED_COMPONENT =3D 0x01, ACPI_IORT_NODE_PCI_ROOT_COMPLEX =3D 0x02, ACPI_IORT_NODE_SMMU =3D 0x03, - ACPI_IORT_NODE_SMMU_V3 =3D 0x04 + ACPI_IORT_NODE_SMMU_V3 =3D 0x04, + ACPI_IORT_NODE_PARAVIRT =3D 0x80 }; =20 struct AcpiIortIdMapping { @@ -610,6 +611,24 @@ typedef struct AcpiIortItsGroup AcpiIortItsGroup; =20 #define ACPI_IORT_SMMU_V3_COHACC_OVERRIDE 1 =20 +struct AcpiIortPVIommuPCI { + ACPI_IORT_NODE_HEADER_DEF + uint32_t devid; + uint8_t reserved2[12]; + uint32_t model; + uint32_t flags; + uint8_t reserved3[16]; + AcpiIortIdMapping id_mapping_array[0]; +} QEMU_PACKED; +typedef struct AcpiIortPVIommuPCI AcpiIortPVIommuPCI; + +enum { + ACPI_IORT_NODE_PV_VIRTIO_IOMMU =3D 0x0, + ACPI_IORT_NODE_PV_VIRTIO_IOMMU_PCI =3D 0x1, +}; + +#define ACPI_IORT_NODE_PV_CACHE_COHERENT (1 << 0) + struct AcpiIortSmmu3 { ACPI_IORT_NODE_HEADER_DEF uint64_t base_address; --=20 2.20.1 From nobody Sun May 12 23:37:35 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1574448762; cv=none; d=zohomail.com; s=zohoarc; b=A7do8fvXu2BGa83vajnDtthHiTpCWfs3HE7fk2NrJMAtzUyXSsvzfPePFyK3RWQ4yktv7g766saN1cwyfmlUx/2VotcauIAZD8PQ+Gdq+exhrOjFMVQ0Kq7C7m39TQ1xwBkHpnuJYgcpiLjTw66REcQqGijz4Ik5VjOOD+CnZl8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574448762; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=mfZYIw3UEsPwHAATMfS141k0p6DtF7ITPQOxirCtQ/o=; b=C89ChzsUUZvYp1ofrhxDp4GD6ra4zOf5ZFZc7lrydfYjYy3VDdrUwYyvaX0MTxqWjwm9q/WWRNYlciES3yR4nU2KGhN8yeMyUbr0cZ4UOh5UmcXdvMR3rjhM+DBHtdKhzptodC4XorPHpFhFSHlgBKGiAG+lfyjadeY0cefDzR8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574448761997871.2981753648747; Fri, 22 Nov 2019 10:52:41 -0800 (PST) Received: from localhost ([::1]:54326 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYE2q-0006js-RV for importer@patchew.org; Fri, 22 Nov 2019 13:52:40 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40221) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDj2-0001eZ-IV for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:32:14 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iYDj1-0000hy-6T for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:32:12 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:46222 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iYDj1-0000h8-1u for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:32:11 -0500 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-213-gO6uWk4uOd2yIJgV7sIkWw-1; Fri, 22 Nov 2019 13:32:09 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BBE18DB60; Fri, 22 Nov 2019 18:32:07 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-37.ams2.redhat.com [10.36.116.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6225361074; Fri, 22 Nov 2019 18:32:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574447530; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mfZYIw3UEsPwHAATMfS141k0p6DtF7ITPQOxirCtQ/o=; b=N60b60Em4NjMiqzXOTPHqGA36wwda+WJ+zH4byA4Wi4bcTY+ybcLb5GaYo9e1tUOqw+XMD 98vChgp9hCF4kBaLJuCdc9yHqUDemS6RxU7PPauhrtXwt4MEd+IN+b61rzhxftf2nyrZ2+ ls4SkvKev9CNpmEc/q2J1MmwQPWoJ0w= From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org, mst@redhat.com, armbru@redhat.com, jean-philippe.brucker@arm.com, bharatb.linux@gmail.com, yang.zhong@intel.com, dgilbert@redhat.com, quintela@redhat.com Subject: [PATCH for-5.0 v11 18/20] virtio-iommu: Support migration Date: Fri, 22 Nov 2019 19:29:41 +0100 Message-Id: <20191122182943.4656-19-eric.auger@redhat.com> In-Reply-To: <20191122182943.4656-1-eric.auger@redhat.com> References: <20191122182943.4656-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: gO6uWk4uOd2yIJgV7sIkWw-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, peterx@redhat.com, tnowicki@marvell.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Add Migration support. We rely on recently added gtree and qlist migration. Besides, we have to fixup end point <-> domain link. Indeed each domain has a list of endpoints attached to it. And each endpoint has a pointer to its domain. Raw gtree and qlist migration cannot handle this as it re-allocates all the nodes while reconstructing the trees/lists. So in post_load we re-construct the relationship between endpoints and domains. Signed-off-by: Eric Auger Acked-by: Dr. David Alan Gilbert Reviewed-by: Jean-Philippe Brucker --- hw/virtio/virtio-iommu.c | 127 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 117 insertions(+), 10 deletions(-) diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index c5b202fab7..4e92fc0c95 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -692,16 +692,6 @@ static void virtio_iommu_set_features(VirtIODevice *vd= ev, uint64_t val) trace_virtio_iommu_set_features(dev->acked_features); } =20 -/* - * Migration is not yet supported: most of the state consists - * of balanced binary trees which are not yet ready for getting - * migrated - */ -static const VMStateDescription vmstate_virtio_iommu_device =3D { - .name =3D "virtio-iommu-device", - .unmigratable =3D 1, -}; - static gint int_cmp(gconstpointer a, gconstpointer b, gpointer user_data) { uint ua =3D GPOINTER_TO_UINT(a); @@ -778,6 +768,123 @@ static void virtio_iommu_instance_init(Object *obj) { } =20 +#define VMSTATE_INTERVAL \ +{ \ + .name =3D "interval", \ + .version_id =3D 1, \ + .minimum_version_id =3D 1, \ + .fields =3D (VMStateField[]) { \ + VMSTATE_UINT64(low, viommu_interval), \ + VMSTATE_UINT64(high, viommu_interval), \ + VMSTATE_END_OF_LIST() \ + } \ +} + +#define VMSTATE_MAPPING \ +{ \ + .name =3D "mapping", \ + .version_id =3D 1, \ + .minimum_version_id =3D 1, \ + .fields =3D (VMStateField[]) { \ + VMSTATE_UINT64(phys_addr, viommu_mapping), \ + VMSTATE_UINT32(flags, viommu_mapping), \ + VMSTATE_END_OF_LIST() \ + }, \ +} + +static const VMStateDescription vmstate_interval_mapping[2] =3D { + VMSTATE_MAPPING, /* value */ + VMSTATE_INTERVAL /* key */ +}; + +static int domain_preload(void *opaque) +{ + viommu_domain *domain =3D opaque; + + domain->mappings =3D g_tree_new_full((GCompareDataFunc)interval_cmp, + NULL, g_free, g_free); + return 0; +} + +static const VMStateDescription vmstate_endpoint =3D { + .name =3D "endpoint", + .version_id =3D 1, + .minimum_version_id =3D 1, + .fields =3D (VMStateField[]) { + VMSTATE_UINT32(id, viommu_endpoint), + VMSTATE_END_OF_LIST() + } +}; + +static const VMStateDescription vmstate_domain =3D { + .name =3D "domain", + .version_id =3D 1, + .minimum_version_id =3D 1, + .pre_load =3D domain_preload, + .fields =3D (VMStateField[]) { + VMSTATE_UINT32(id, viommu_domain), + VMSTATE_GTREE_V(mappings, viommu_domain, 1, + vmstate_interval_mapping, + viommu_interval, viommu_mapping), + VMSTATE_QLIST_V(endpoint_list, viommu_domain, 1, + vmstate_endpoint, viommu_endpoint, next), + VMSTATE_END_OF_LIST() + } +}; + +static gboolean reconstruct_ep_domain_link(gpointer key, gpointer value, + gpointer data) +{ + viommu_domain *d =3D (viommu_domain *)value; + viommu_endpoint *iter, *tmp; + viommu_endpoint *ep =3D (viommu_endpoint *)data; + + QLIST_FOREACH_SAFE(iter, &d->endpoint_list, next, tmp) { + if (iter->id =3D=3D ep->id) { + /* remove the ep */ + QLIST_REMOVE(iter, next); + g_free(iter); + /* replace it with the good one */ + QLIST_INSERT_HEAD(&d->endpoint_list, ep, next); + /* update the domain */ + ep->domain =3D d; + return true; /* stop the search */ + } + } + return false; /* continue the traversal */ +} + +static gboolean fix_endpoint(gpointer key, gpointer value, gpointer data) +{ + VirtIOIOMMU *s =3D (VirtIOIOMMU *)data; + + g_tree_foreach(s->domains, reconstruct_ep_domain_link, value); + return false; +} + +static int iommu_post_load(void *opaque, int version_id) +{ + VirtIOIOMMU *s =3D opaque; + + g_tree_foreach(s->endpoints, fix_endpoint, s); + return 0; +} + +static const VMStateDescription vmstate_virtio_iommu_device =3D { + .name =3D "virtio-iommu-device", + .minimum_version_id =3D 1, + .version_id =3D 1, + .post_load =3D iommu_post_load, + .fields =3D (VMStateField[]) { + VMSTATE_GTREE_DIRECT_KEY_V(domains, VirtIOIOMMU, 1, + &vmstate_domain, viommu_domain), + VMSTATE_GTREE_DIRECT_KEY_V(endpoints, VirtIOIOMMU, 1, + &vmstate_endpoint, viommu_endpoint), + VMSTATE_END_OF_LIST() + }, +}; + + static const VMStateDescription vmstate_virtio_iommu =3D { .name =3D "virtio-iommu", .minimum_version_id =3D 1, --=20 2.20.1 From nobody Sun May 12 23:37:35 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1574449006; cv=none; d=zohomail.com; s=zohoarc; b=FDZSXw8TlkH1r7JHDH8HZC8RDog7GbTSpIErpq+kwy/ByjfHxr7oXrZ+gqPV5T9IbbJ6CprxPAyZ2FkXUGNDEVx+ch3uhlsI5kfFA9Mz+O7raFltnX8idmvzbxfO5TUvK65tr2zvxrdtmHvukPoq8cq5ZBlC3h6tGpRbHUAuN0Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574449006; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=vS8Pmmvbc+nSyR3aOCA1ym+0sVsQeVo9J9FaMVzpxsw=; b=BfhT+lz27+S5hZJob/LQfQVz9wZM9EiN51PG+qGbRyskibUHuac+ARy2xsIrZ6M5FDRk9Ziy/45ndB+PRqmI4HqA080SIUNKWPOO6KHA29Gjipk3Pi01CNH5uODv6b09tvl5MrOCyyK7dHCFdgkjgRZGOAI5D7m30HmsTYO+6VE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574449006714777.1711664547864; Fri, 22 Nov 2019 10:56:46 -0800 (PST) Received: from localhost ([::1]:54392 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYE6m-0002Wk-K1 for importer@patchew.org; Fri, 22 Nov 2019 13:56:44 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40273) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDjB-0001ls-Gh for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:32:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iYDj9-0000mA-OB for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:32:21 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:26137 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iYDj8-0000jS-8D for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:32:18 -0500 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-158-_A0TeD7KMUiBna88vLYPlQ-1; Fri, 22 Nov 2019 13:32:16 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8C45E107ACC4; Fri, 22 Nov 2019 18:32:14 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-37.ams2.redhat.com [10.36.116.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id 225F76E712; Fri, 22 Nov 2019 18:32:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574447537; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vS8Pmmvbc+nSyR3aOCA1ym+0sVsQeVo9J9FaMVzpxsw=; b=DwfLmtgpFDM5BX3IGV2rZ6U1gA+jj+hwGnfjpZwJyZA8XOtuVowAHbf0V2gh05x3XomkQs rqfhFcXMnbXmlxYUNeNS/D4cGFW255dLbIcvInUVhX+YMa5GEFtamzzhCKUfeBuhC3QJhQ WTz1SfYt1B4HRdZcsIDvpJ4iwzK5AaA= From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org, mst@redhat.com, armbru@redhat.com, jean-philippe.brucker@arm.com, bharatb.linux@gmail.com, yang.zhong@intel.com, dgilbert@redhat.com, quintela@redhat.com Subject: [PATCH for-5.0 v11 19/20] pc: Add support for virtio-iommu-pci Date: Fri, 22 Nov 2019 19:29:42 +0100 Message-Id: <20191122182943.4656-20-eric.auger@redhat.com> In-Reply-To: <20191122182943.4656-1-eric.auger@redhat.com> References: <20191122182943.4656-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: _A0TeD7KMUiBna88vLYPlQ-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 205.139.110.61 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, peterx@redhat.com, tnowicki@marvell.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" The virtio-iommu-pci is instantiated through the -device QEMU option. However if instantiated it also requires an IORT ACPI table to describe the ID mappings between the root complex and the iommu. This patch adds the generation of the IORT table if the virtio-iommu-pci device is instantiated. We also declare the [0xfee00000 - 0xfeefffff] MSI reserved region so that it gets bypassed by the IOMMU. Signed-off-by: Eric Auger Reviewed-by: Jean-Philippe Brucker --- hw/i386/acpi-build.c | 72 ++++++++++++++++++++++++++++++++++++++++++++ hw/i386/pc.c | 15 ++++++++- include/hw/i386/pc.h | 2 ++ 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c index 12ff55fcfb..f09cabdcae 100644 --- a/hw/i386/acpi-build.c +++ b/hw/i386/acpi-build.c @@ -2744,6 +2744,72 @@ static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg) return true; } =20 +static inline void +fill_iort_idmap(AcpiIortIdMapping *idmap, int i, + uint32_t input_base, uint32_t id_count, + uint32_t output_base, uint32_t output_reference) +{ + idmap[i].input_base =3D cpu_to_le32(input_base); + idmap[i].id_count =3D cpu_to_le32(id_count); + idmap[i].output_base =3D cpu_to_le32(output_base); + idmap[i].output_reference =3D cpu_to_le32(output_reference); +} + +static void +build_iort(GArray *table_data, BIOSLinker *linker, PCMachineState *pcms) +{ + size_t iommu_node_size, rc_node_size, iommu_node_offset; + int iort_start =3D table_data->len; + AcpiIortPVIommuPCI *iommu; + AcpiIortIdMapping *idmap; + AcpiIortTable *iort; + size_t iort_length; + AcpiIortRC *rc; + + iort =3D acpi_data_push(table_data, sizeof(*iort)); + iort_length =3D sizeof(*iort); + iort->node_count =3D cpu_to_le32(2); + + /* virtio-iommu node */ + + iommu_node_offset =3D sizeof(*iort); + iort->node_offset =3D cpu_to_le32(iommu_node_offset); + iommu_node_size =3D sizeof(*iommu); + iort_length +=3D iommu_node_offset; + iommu =3D acpi_data_push(table_data, iommu_node_size); + iommu->type =3D ACPI_IORT_NODE_PARAVIRT; + iommu->length =3D cpu_to_le16(iommu_node_size); + iommu->mapping_count =3D 0; + iommu->devid =3D cpu_to_le32(pcms->virtio_iommu_bdf); + iommu->model =3D cpu_to_le32(ACPI_IORT_NODE_PV_VIRTIO_IOMMU_PCI); + + /* Root Complex Node */ + rc_node_size =3D sizeof(*rc) + 2 * sizeof(*idmap); + iort_length +=3D rc_node_size; + rc =3D acpi_data_push(table_data, rc_node_size); + + rc->type =3D ACPI_IORT_NODE_PCI_ROOT_COMPLEX; + rc->length =3D cpu_to_le16(rc_node_size); + rc->mapping_count =3D cpu_to_le32(2); + rc->mapping_offset =3D cpu_to_le32(sizeof(*rc)); + + /* fully coherent device */ + rc->memory_properties.cache_coherency =3D cpu_to_le32(1); + rc->memory_properties.memory_flags =3D 0x3; /* CCA =3D CPM =3D DCAS = =3D 1 */ + rc->pci_segment_number =3D 0; /* MCFG pci_segment */ + fill_iort_idmap(rc->id_mapping_array, 0, 0, pcms->virtio_iommu_bdf, 0, + iommu_node_offset); + fill_iort_idmap(rc->id_mapping_array, 1, pcms->virtio_iommu_bdf + 1, + 0xFFFF - pcms->virtio_iommu_bdf, + pcms->virtio_iommu_bdf + 1, iommu_node_offset); + + iort =3D (AcpiIortTable *)(table_data->data + iort_start); + iort->length =3D cpu_to_le32(iort_length); + + build_header(linker, table_data, (void *)(table_data->data + iort_star= t), + "IORT", table_data->len - iort_start, 0, NULL, NULL); +} + static void acpi_build(AcpiBuildTables *tables, MachineState *machine) { @@ -2835,6 +2901,12 @@ void acpi_build(AcpiBuildTables *tables, MachineStat= e *machine) build_slit(tables_blob, tables->linker, machine); } } + + if (pcms->virtio_iommu) { + acpi_add_table(table_offsets, tables_blob); + build_iort(tables_blob, tables->linker, pcms); + } + if (acpi_get_mcfg(&mcfg)) { acpi_add_table(table_offsets, tables_blob); build_mcfg(tables_blob, tables->linker, &mcfg); diff --git a/hw/i386/pc.c b/hw/i386/pc.c index ac08e63604..af984ee041 100644 --- a/hw/i386/pc.c +++ b/hw/i386/pc.c @@ -84,6 +84,7 @@ #include "hw/net/ne2000-isa.h" #include "standard-headers/asm-x86/bootparam.h" #include "hw/virtio/virtio-pmem-pci.h" +#include "hw/virtio/virtio-iommu.h" #include "hw/mem/memory-device.h" #include "sysemu/replay.h" #include "qapi/qmp/qerror.h" @@ -1940,6 +1941,11 @@ static void pc_machine_device_pre_plug_cb(HotplugHan= dler *hotplug_dev, pc_cpu_pre_plug(hotplug_dev, dev, errp); } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) { pc_virtio_pmem_pci_pre_plug(hotplug_dev, dev, errp); + } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) { + /* we declare a VIRTIO_IOMMU_RESV_MEM_T_MSI region */ + qdev_prop_set_uint32(dev, "len-reserved-regions", 1); + qdev_prop_set_string(dev, "reserved-regions[0]", + "0xfee00000, 0xfeefffff, 1"); } } =20 @@ -1952,6 +1958,12 @@ static void pc_machine_device_plug_cb(HotplugHandler= *hotplug_dev, pc_cpu_plug(hotplug_dev, dev, errp); } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) { pc_virtio_pmem_pci_plug(hotplug_dev, dev, errp); + } else if (object_dynamic_cast(OBJECT(dev), "virtio-iommu-pci")) { + PCMachineState *pcms =3D PC_MACHINE(hotplug_dev); + PCIDevice *pdev =3D PCI_DEVICE(dev); + + pcms->virtio_iommu =3D true; + pcms->virtio_iommu_bdf =3D pci_get_bdf(pdev); } } =20 @@ -1990,7 +2002,8 @@ static HotplugHandler *pc_get_hotplug_handler(Machine= State *machine, { if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) || object_dynamic_cast(OBJECT(dev), TYPE_CPU) || - object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) { + object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI) || + object_dynamic_cast(OBJECT(dev), "virtio-iommu-pci")) { return HOTPLUG_HANDLER(machine); } =20 diff --git a/include/hw/i386/pc.h b/include/hw/i386/pc.h index 1f86eba3f9..221b4c6ef9 100644 --- a/include/hw/i386/pc.h +++ b/include/hw/i386/pc.h @@ -49,6 +49,8 @@ struct PCMachineState { bool smbus_enabled; bool sata_enabled; bool pit_enabled; + bool virtio_iommu; + uint16_t virtio_iommu_bdf; =20 /* NUMA information: */ uint64_t numa_nodes; --=20 2.20.1 From nobody Sun May 12 23:37:35 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1574448531; cv=none; d=zohomail.com; s=zohoarc; b=BUdDVnjj5nURedueOm3oLvFK/TzUqgj7csKaR5067IsLMXx1VO7lHLI2s1Mx5QSPa+n9MHwPi8tXfp7HcoIAhD1Hym7++g4vEH/IVNX3iDFzrwxq5L1/vuMpO8U9mg8gCmW/yB43mWNXyFWRiSAlp0J4GLSQTPNX1VkuAxQolIY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574448531; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=FH18n8gL+YCFEKuqvhyl1UKyiT9gf7yTQQ3G6LrFWao=; b=CLOqQLClX3GWKXFShyDLAoLNInTmQV3r51Aw0FWo0ugveQ/Hog/1PZ+vZHj7IlhW44TzeyPi3eORFMETgeSOnhj2h/vs/PAhETzv98Tz4PHOyswWozt96TQXWzWyIhraw0slvVIxLGi+lGADtxVL3rlvQ9VnpGounstv5WKkmKM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574448531574462.8356030769021; Fri, 22 Nov 2019 10:48:51 -0800 (PST) Received: from localhost ([::1]:54264 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDz8-0001ak-1p for importer@patchew.org; Fri, 22 Nov 2019 13:48:50 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40309) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYDjM-0001us-6S for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:32:34 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iYDjJ-0000oF-La for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:32:32 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:34867 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iYDjH-0000o1-LB for qemu-devel@nongnu.org; Fri, 22 Nov 2019 13:32:29 -0500 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-191-zGnxj3RaNsWfCxjK8mi69A-1; Fri, 22 Nov 2019 13:32:26 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 055B31005502; Fri, 22 Nov 2019 18:32:24 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-37.ams2.redhat.com [10.36.116.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id DE40D6E712; Fri, 22 Nov 2019 18:32:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574447547; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FH18n8gL+YCFEKuqvhyl1UKyiT9gf7yTQQ3G6LrFWao=; b=Tgw4hJroTk/2sLcXadlsTvmXeJFrhFCEynh3nien5ahBr+lKX3kW8yvOvt53U7LiN11/2O qFS3gle891V91cuI/xWDsxgvfKPP1OPKO09m/pOA2pZg7liIcWzxwySkXIHkfHLTaIYXi8 9sLgCsgmoLWxa0emHxAvZThbkerq+fE= From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org, mst@redhat.com, armbru@redhat.com, jean-philippe.brucker@arm.com, bharatb.linux@gmail.com, yang.zhong@intel.com, dgilbert@redhat.com, quintela@redhat.com Subject: [PATCH for-5.0 v11 20/20] tests: Add virtio-iommu test Date: Fri, 22 Nov 2019 19:29:43 +0100 Message-Id: <20191122182943.4656-21-eric.auger@redhat.com> In-Reply-To: <20191122182943.4656-1-eric.auger@redhat.com> References: <20191122182943.4656-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: zGnxj3RaNsWfCxjK8mi69A-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, peterx@redhat.com, tnowicki@marvell.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This adds the framework to test the virtio-iommu-pci device and tests exercising the attach/detach, map/unmap API. To run the tests: make tests/qos-test QTEST_QEMU_BINARY=3Dx86_64-softmmu/qemu-system-x86_64 tests/qos-test V=3D1 Signed-off-by: Eric Auger --- tests/Makefile.include | 2 + tests/libqos/virtio-iommu.c | 177 ++++++++++++++++++++++++ tests/libqos/virtio-iommu.h | 45 +++++++ tests/virtio-iommu-test.c | 261 ++++++++++++++++++++++++++++++++++++ 4 files changed, 485 insertions(+) create mode 100644 tests/libqos/virtio-iommu.c create mode 100644 tests/libqos/virtio-iommu.h create mode 100644 tests/virtio-iommu-test.c diff --git a/tests/Makefile.include b/tests/Makefile.include index 8566f5f119..76a303c4fb 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -734,6 +734,7 @@ qos-test-obj-y +=3D tests/libqos/virtio-net.o qos-test-obj-y +=3D tests/libqos/virtio-pci.o qos-test-obj-y +=3D tests/libqos/virtio-pci-modern.o qos-test-obj-y +=3D tests/libqos/virtio-rng.o +qos-test-obj-y +=3D tests/libqos/virtio-iommu.o qos-test-obj-y +=3D tests/libqos/virtio-scsi.o qos-test-obj-y +=3D tests/libqos/virtio-serial.o =20 @@ -773,6 +774,7 @@ qos-test-obj-$(CONFIG_VIRTFS) +=3D tests/virtio-9p-test= .o qos-test-obj-y +=3D tests/virtio-blk-test.o qos-test-obj-y +=3D tests/virtio-net-test.o qos-test-obj-y +=3D tests/virtio-rng-test.o +qos-test-obj-y +=3D tests/virtio-iommu-test.o qos-test-obj-y +=3D tests/virtio-scsi-test.o qos-test-obj-y +=3D tests/virtio-serial-test.o qos-test-obj-y +=3D tests/vmxnet3-test.o diff --git a/tests/libqos/virtio-iommu.c b/tests/libqos/virtio-iommu.c new file mode 100644 index 0000000000..b4e9ea44fb --- /dev/null +++ b/tests/libqos/virtio-iommu.c @@ -0,0 +1,177 @@ +/* + * libqos driver framework + * + * Copyright (c) 2018 Emanuele Giuseppe Esposito + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see + */ + +#include "qemu/osdep.h" +#include "libqtest.h" +#include "qemu/module.h" +#include "libqos/qgraph.h" +#include "libqos/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 *qvirtio_iommu_device_get_driver(void *object, + const char *interface) +{ + QVirtioIOMMUDevice *v_iommu =3D object; + return qvirtio_iommu_get_driver(&v_iommu->iommu, interface); +} + +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); +} + +static void qvirtio_iommu_device_destructor(QOSGraphObject *obj) +{ + QVirtioIOMMUDevice *v_iommu =3D (QVirtioIOMMUDevice *) obj; + QVirtioIOMMU *iommu =3D &v_iommu->iommu; + + virtio_iommu_cleanup(iommu); +} + +static void qvirtio_iommu_device_start_hw(QOSGraphObject *obj) +{ + QVirtioIOMMUDevice *v_iommu =3D (QVirtioIOMMUDevice *) obj; + QVirtioIOMMU *iommu =3D &v_iommu->iommu; + + virtio_iommu_setup(iommu); +} + +static void *virtio_iommu_device_create(void *virtio_dev, + QGuestAllocator *t_alloc, + void *addr) +{ + QVirtioIOMMUDevice *virtio_rdevice =3D g_new0(QVirtioIOMMUDevice, 1); + QVirtioIOMMU *interface =3D &virtio_rdevice->iommu; + + interface->vdev =3D virtio_dev; + alloc =3D t_alloc; + + virtio_rdevice->obj.get_driver =3D qvirtio_iommu_device_get_driver; + virtio_rdevice->obj.destructor =3D qvirtio_iommu_device_destructor; + virtio_rdevice->obj.start_hw =3D qvirtio_iommu_device_start_hw; + + return &virtio_rdevice->obj; +} + +/* 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-device */ + qos_node_create_driver("virtio-iommu-device", virtio_iommu_device_crea= te); + qos_node_consumes("virtio-iommu-device", "virtio-bus", NULL); + qos_node_produces("virtio-iommu-device", "virtio"); + qos_node_produces("virtio-iommu-device", "virtio-iommu"); + + /* 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/libqos/virtio-iommu.h b/tests/libqos/virtio-iommu.h new file mode 100644 index 0000000000..6970b45a01 --- /dev/null +++ b/tests/libqos/virtio-iommu.h @@ -0,0 +1,45 @@ +/* + * libqos driver framework + * + * Copyright (c) 2018 Emanuele Giuseppe Esposito + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see + */ + +#ifndef TESTS_LIBQOS_VIRTIO_IOMMU_H +#define TESTS_LIBQOS_VIRTIO_IOMMU_H + +#include "libqos/qgraph.h" +#include "libqos/virtio.h" +#include "libqos/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/virtio-iommu-test.c b/tests/virtio-iommu-test.c new file mode 100644 index 0000000000..1d93d686bc --- /dev/null +++ b/tests/virtio-iommu-test.c @@ -0,0 +1,261 @@ +/* + * QTest testcase for VirtIO IOMMU + * + * Copyright (c) 2014 SUSE LINUX Products GmbH + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + */ + +#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 iommu_hotplug(void *obj, void *data, QGuestAllocator *alloc) +{ + QVirtioPCIDevice *dev =3D obj; + QTestState *qts =3D dev->pdev->bus->qts; + + qtest_qmp_device_add(qts, "virtio-iommu-pci", "iommu1", + "{'addr': %s}", stringify(PCI_SLOT_HP)); + +} + +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); + uint32_t probe_size =3D qvirtio_config_readl(dev, 32); + + g_assert_cmpint(input_range_start, =3D=3D, 0); + g_assert_cmphex(input_range_end, =3D=3D, 0xFFFFFFFFFFFFFFFF); + g_assert_cmpint(domain_range_start, =3D=3D, 0); + g_assert_cmpint(domain_range_end, =3D=3D, 32); + g_assert_cmphex(probe_size, =3D=3D, 0x200); +} + +/** + * send_attach_detach - Send an attach/detach command to the device + * @type: VIRTIO_IOMMU_T_ATTACH/VIRTIO_IOMMU_T_DETACH + * @domain: domain the end point is attached to + * @ep: end-point + */ +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; /* 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); + char buffer[64]; + int ret; + + req.head.type =3D type; + req.domain =3D domain; + req.endpoint =3D 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 ((struct virtio_iommu_req_tail *)buffer)->status; + 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 binding 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); + char buffer[64]; + int ret; + + req.head.type =3D VIRTIO_IOMMU_T_MAP; + req.domain =3D domain; + req.virt_start =3D virt_start; + req.virt_end =3D virt_end; + req.phys_start =3D phys_start; + req.flags =3D 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); + memread(wr_addr, buffer, wr_size); + ret =3D ((struct virtio_iommu_req_tail *)buffer)->status; + 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); + char buffer[64]; + int ret; + + req.head.type =3D VIRTIO_IOMMU_T_UNMAP; + req.domain =3D domain; + req.virt_start =3D virt_start; + req.virt_end =3D 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); + memread(wr_addr, buffer, wr_size); + ret =3D ((struct virtio_iommu_req_tail *)buffer)->status; + guest_free(alloc, ro_addr); + guest_free(alloc, wr_addr); + return ret; +} + +/* Test unmap scenari documented in the spec v0.12 */ +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 */ + ret =3D send_attach_detach(qts, v_iommu, VIRTIO_IOMMU_T_ATTACH, 0, 0); + g_assert_cmpint(ret, =3D=3D, 0); + ret =3D send_attach_detach(qts, v_iommu, VIRTIO_IOMMU_T_ATTACH, 1, 2); + g_assert_cmpint(ret, =3D=3D, 0); + ret =3D send_attach_detach(qts, v_iommu, VIRTIO_IOMMU_T_ATTACH, 1, 2); + g_assert_cmpint(ret, =3D=3D, 0); + ret =3D send_attach_detach(qts, v_iommu, VIRTIO_IOMMU_T_ATTACH, 0, 2); + g_assert_cmpint(ret, =3D=3D, 0); + + /* domain, virt start, virt end, phys start, flags */ + ret =3D send_map(qts, v_iommu, 0, 0, 0xFFF, 0xa1000, VIRTIO_IOMMU_MAP_= F_READ); + g_assert_cmpint(ret, =3D=3D, 0); + + 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, 0, 0x10, 0xFFF); + g_assert_cmpint(ret, =3D=3D, VIRTIO_IOMMU_S_RANGE); + + /* 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 */ + send_map(qts, v_iommu, 1, 0, 9, 0xa1000, VIRTIO_IOMMU_MAP_F_READ); + ret =3D send_unmap(qts, v_iommu, 1, 0, 9); + g_assert_cmpint(ret, =3D=3D, 0); /* unmaps [0,9] */ + + /* 3 */ + send_map(qts, v_iommu, 1, 0, 4, 0xb1000, VIRTIO_IOMMU_MAP_F_READ); + send_map(qts, v_iommu, 1, 5, 9, 0xb2000, VIRTIO_IOMMU_MAP_F_READ); + ret =3D send_unmap(qts, v_iommu, 1, 0, 9); + g_assert_cmpint(ret, =3D=3D, 0); /* unmaps [0,4] and [5,9] */ + + /* 4 */ + send_map(qts, v_iommu, 1, 0, 9, 0xc1000, VIRTIO_IOMMU_MAP_F_READ); + 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 */ + send_map(qts, v_iommu, 1, 0, 4, 0xd1000, VIRTIO_IOMMU_MAP_F_READ); + send_map(qts, v_iommu, 1, 5, 9, 0xd2000, VIRTIO_IOMMU_MAP_F_READ); + 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 */ + send_map(qts, v_iommu, 1, 0, 4, 0xe2000, VIRTIO_IOMMU_MAP_F_READ); + ret =3D send_unmap(qts, v_iommu, 1, 0, 9); + g_assert_cmpint(ret, =3D=3D, 0); /* unmaps [0,4] */ + + /* 7 */ + send_map(qts, v_iommu, 1, 0, 4, 0xf2000, VIRTIO_IOMMU_MAP_F_READ); + send_map(qts, v_iommu, 1, 10, 14, 0xf3000, VIRTIO_IOMMU_MAP_F_READ); + ret =3D send_unmap(qts, v_iommu, 1, 0, 14); + g_assert_cmpint(ret, =3D=3D, 0); /* unmaps [0,4] and [10,14] */ + + send_unmap(qts, v_iommu, 1, 0, 100); + send_map(qts, v_iommu, 1, 10, 14, 0xf3000, VIRTIO_IOMMU_MAP_F_READ); + send_map(qts, v_iommu, 1, 0, 4, 0xf2000, VIRTIO_IOMMU_MAP_F_READ); + ret =3D send_unmap(qts, v_iommu, 1, 0, 4); + g_assert_cmpint(ret, =3D=3D, 0); /* unmaps [0,4] and [10,14] */ +} + +static void register_virtio_iommu_test(void) +{ + qos_add_test("hotplug", "virtio-iommu-pci", iommu_hotplug, NULL); + qos_add_test("config", "virtio-iommu", pci_config, NULL); + qos_add_test("attach_detach", "virtio-iommu", test_attach_detach, NULL= ); +} + +libqos_init(register_virtio_iommu_test); --=20 2.20.1