From patchwork Fri Apr 14 10:15:28 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 9681057 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 585F760386 for ; Fri, 14 Apr 2017 10:18:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4969A28663 for ; Fri, 14 Apr 2017 10:18:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3DC332866E; Fri, 14 Apr 2017 10:18:38 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id E1C2628664 for ; Fri, 14 Apr 2017 10:18:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=h4RY6E6JCmnhczhX6RzxdiHPi1jhEZ6OEDxUfSClr2o=; b=YVWUzz6uB26yqOrwQOj4FqnMpa JdQsWl3vW1srL2YewZ38/FZzeQt/lPsl1F3RbgzQLBzlI5q5sMX+iRg2e3tr/sX1SA5MozmOLwC27 bSYqd/+U/IGWWnfqJZJK+6D5FM7u5/ILk0PnlJ5klxW/cAa0aX61ZFafOnJfqbWED9lE2dkwoQF5o W1tuUZBmbsYHNFYsnkyCzNQMtfYSqRSLrj3iRWOlcGtrnkuGq7hgIiq0ajQktSR5Tp9r9J97JVf0I WBGDadiusf/oINLW6uOsuAAcJNq0FvNFRhKV/GDlGUN5mZQiSZ9v7pKa1B7wgx63KdTrU0oEGeY1x jvwAFXEg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1cyyJJ-0002em-2L; Fri, 14 Apr 2017 10:18:37 +0000 Received: from mx1.redhat.com ([209.132.183.28]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1cyyIJ-0001WT-Sk for linux-arm-kernel@lists.infradead.org; Fri, 14 Apr 2017 10:17:39 +0000 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6BBA67E9DB; Fri, 14 Apr 2017 10:17:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 6BBA67E9DB Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eric.auger@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 6BBA67E9DB Received: from localhost.localdomain.com (ovpn-117-27.ams2.redhat.com [10.36.117.27]) by smtp.corp.redhat.com (Postfix) with ESMTP id E4FDB18214; Fri, 14 Apr 2017 10:17:13 +0000 (UTC) From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, marc.zyngier@arm.com, christoffer.dall@linaro.org, andre.przywara@arm.com, vijayak@caviumnetworks.com, Vijaya.Kumar@cavium.com, peter.maydell@linaro.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org Subject: [PATCH v5 16/22] KVM: arm64: vgic-its: Add infrastructure for table lookup Date: Fri, 14 Apr 2017 12:15:28 +0200 Message-Id: <1492164934-988-17-git-send-email-eric.auger@redhat.com> In-Reply-To: <1492164934-988-1-git-send-email-eric.auger@redhat.com> References: <1492164934-988-1-git-send-email-eric.auger@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Fri, 14 Apr 2017 10:17:19 +0000 (UTC) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170414_031736_118144_57B06550 X-CRM114-Status: GOOD ( 17.84 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Prasun.Kapoor@cavium.com, drjones@redhat.com, quintela@redhat.com, dgilbert@redhat.com, pbonzini@redhat.com MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Add a generic lookup_table() helper whose role consists in scanning a contiguous table located in guest RAM and applying a callback on each entry. Entries can be handled as linked lists since the callback may return an offset to the next entry and also tell that an entry is the last one. Helper functions also are added to compute the device/event ID offset to the next DTE/ITE. compute_next_devid_offset, compute_next_eventid_offset and lookup_table will become static in subsequent patches Signed-off-by: Eric Auger Tested-by: Prakash, Brahmajyosyula --- v4 -> v5: - use kvm_read_guest v3 -> v4: - remove static to avoid compilation warning - correct size computation in looup_table() - defines now encode the number of bits used for devid and eventid offsets - use BIT() - 1 to encode the max offets --- virt/kvm/arm/vgic/vgic-its.c | 93 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/virt/kvm/arm/vgic/vgic-its.c b/virt/kvm/arm/vgic/vgic-its.c index 56c5123..c22b35d 100644 --- a/virt/kvm/arm/vgic/vgic-its.c +++ b/virt/kvm/arm/vgic/vgic-its.c @@ -195,6 +195,8 @@ static struct its_ite *find_ite(struct vgic_its *its, u32 device_id, #define VITS_TYPER_IDBITS 16 #define VITS_TYPER_DEVBITS 16 +#define VITS_DTE_MAX_DEVID_OFFSET (BIT(14) - 1) +#define VITS_ITE_MAX_EVENTID_OFFSET (BIT(16) - 1) /* * Finds and returns a collection in the ITS collection table. @@ -1674,6 +1676,97 @@ int vgic_its_attr_regs_access(struct kvm_device *dev, return ret; } +u32 compute_next_devid_offset(struct list_head *h, struct its_device *dev) +{ + struct list_head *e = &dev->dev_list; + struct its_device *next; + u32 next_offset; + + if (e->next == h) + return 0; + next = list_entry(e->next, struct its_device, dev_list); + next_offset = next->device_id - dev->device_id; + + return min_t(u32, next_offset, VITS_DTE_MAX_DEVID_OFFSET); +} + +u32 compute_next_eventid_offset(struct list_head *h, struct its_ite *ite) +{ + struct list_head *e = &ite->ite_list; + struct its_ite *next; + u32 next_offset; + + if (e->next == h) + return 0; + next = list_entry(e->next, struct its_ite, ite_list); + next_offset = next->event_id - ite->event_id; + + return min_t(u32, next_offset, VITS_ITE_MAX_EVENTID_OFFSET); +} + +/** + * entry_fn_t - Callback called on a table entry restore path + * @its: its handle + * @id: id of the entry + * @entry: pointer to the entry + * @opaque: pointer to an opaque data + * @next_offset: minimal ID offset to the next entry. 0 if this + * entry is the last one, 1 if the entry is invalid, >= 1 if an + * entry's next_offset field was truly decoded + * + * Return: < 0 on error, 0 otherwise + */ +typedef int (*entry_fn_t)(struct vgic_its *its, u32 id, void *entry, + void *opaque, u32 *next_offset); + +/** + * lookup_table - scan a contiguous table in guest RAM and applies a function + * to each entry + * + * @its: its handle + * @base: base gpa of the table + * @size: size of the table in bytes + * @esz: entry size in bytes + * @start_id: first entry's ID + * @fn: function to apply on each entry + * + * Return: < 0 on error, 1 if last element identified, 0 otherwise + */ +int lookup_table(struct vgic_its *its, gpa_t base, int size, int esz, + int start_id, entry_fn_t fn, void *opaque) +{ + void *entry = kzalloc(esz, GFP_KERNEL); + struct kvm *kvm = its->dev->kvm; + unsigned long len = size; + u32 id = start_id; + gpa_t gpa = base; + int ret; + + while (len > 0) { + u32 next_offset; + size_t byte_offset; + + ret = kvm_read_guest(kvm, gpa, entry, esz); + if (ret) + goto out; + + ret = fn(its, id, entry, opaque, &next_offset); + if (ret < 0 || (!ret && !next_offset)) + goto out; + + byte_offset = next_offset * esz; + id += next_offset; + gpa += byte_offset; + len -= byte_offset; + } + kfree(entry); + return 0; + +out: + kfree(entry); + return (ret < 0 ? ret : 1); +} + /** * vgic_its_save_device_tables - Save the device table and all ITT * into guest RAM