From patchwork Tue May 9 10:44:49 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoffer Dall X-Patchwork-Id: 9717667 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 AA98F60364 for ; Tue, 9 May 2017 11:06:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9EA2820373 for ; Tue, 9 May 2017 11:06:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 939EB283F2; Tue, 9 May 2017 11:06:41 +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 3F86E20373 for ; Tue, 9 May 2017 11:06:41 +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=Act6wsic7I4WwaR3z20qqWHoF3tgCf+JTqXx/WYkbg4=; b=Xv04zzKvjngFs5ZWQzhtoQ9cUr bvCMGHUZQqwUdi9ZN22OjLdoyWwZC482OoAt/Bg4d9U9mG5oJr64aTb6IK4+AeFGHvUPe3MQm0tO5 aWCtZtjS5qCORsDz94j6Z2v9gS5vnbI6O6NnwBLBERDE3g4X8Xhxvo0gjAUAYOqIzYJDLpLUA4jPd Jdj6b6uT9Q/+GfEF/d5e5dujmiAWUJEsIh2VZgiyUax4SuarKINLl2hTRnm9cz2UorIZ7C9ers5Co 79bovmuUwPfBmA6OWue77H2ZefEGyWjTrUcmBdNO/C7wgDXu38MxyXgYNGaHZ2bhXF9PJbLVmqIZ0 jh5zCpAg==; 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 1d82yW-0004Ba-2A; Tue, 09 May 2017 11:06:40 +0000 Received: from merlin.infradead.org ([2001:4978:20e::2]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1d82xU-0002pf-TN for linux-arm-kernel@bombadil.infradead.org; Tue, 09 May 2017 11:05:36 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=wH8GdUR/ssueJqoeUeJbA5NIOlgqsOej8k9W9ifKMBU=; b=P8ZHXrxCucQbtI3irLtGux27Q R1a/h7CWHLC/v8cHM+7dPlzBeYJJr919+1lZitI/Wk/wqd0bReHcuqJvsC+VmE18OQV/fLtm6Uqvf 5Nf2dZzjY1PO56HCLay6I0ZHr37QadIQP7IimNS23qY4t6KbAL6leUEnMqGqD2RMyS5dhGFyuWXjY nSc+IDUUs+r5LKtZExdfBNr2eWTR6ThuVQwqpzeICVrM2hGQaH+2JcXJTvfJblGhXVLo8mMWDJA/6 h/VNXJIuTPxf05vK6GKdHcDQUlYRyCgTCtDD4r2H72RIFUL2r5ld/VbK6WBCW6VsvK5nDHZSMeABT qgNhMbEKA==; Received: from mail-qk0-x235.google.com ([2607:f8b0:400d:c09::235]) by merlin.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1d82eO-00071O-Vn for linux-arm-kernel@lists.infradead.org; Tue, 09 May 2017 10:45:54 +0000 Received: by mail-qk0-x235.google.com with SMTP id y201so42665098qka.0 for ; Tue, 09 May 2017 03:45:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=wH8GdUR/ssueJqoeUeJbA5NIOlgqsOej8k9W9ifKMBU=; b=MRwSG3N5bIb5+xhR7YLXcoMpiTmR4rVIjfErQ1br4hLX9vvzWAYB3rYxmX6tx9zAEj PGpf7Ui7nX423tT4Zs1DgnXMxM9Ficy97RqzvIDGgnX6K/kxF7wPdfE2jTqMnZ1fV/5A WOnCWz0pq2fonZia2nSuB85E/vBelg3imOzhM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=wH8GdUR/ssueJqoeUeJbA5NIOlgqsOej8k9W9ifKMBU=; b=a7typd3plYwCdIMeFEImAATHiikW7Mi6Z0dC05u4BdW14Vjt78VI7liHkY1cWrF4ts p3I5gaHzbAotQVuEmtYJ6KYsOHSkHoe80hGMcX/tNCKS1F16eULaw+kb/iF29zmrwJXE VKEBdqf2LDh7OPYl9mQEu94Mgfolp5/6eivioxD/Symc+HQlqM1ty+r5QzRMtaM7O5tf rlBIcv0htPIxHskOscEs9FHhvPXUIRbS/8sVA7gpBpcmRSzJwafYn2MSG1BTSsYNhsEB ybo+czrhdM7akLbz0f/psEO0WryMUI1nibJl52mojtD0d/CWkQmhziuPXcFjBbymA1SR KPzQ== X-Gm-Message-State: AN3rC/6iPmzbGTQ8EOp+NJSB3PRH1Bq19dKzUBuj6fb8q3KZZz2lXMaz 88lqZmeluM/vTtgh X-Received: by 10.80.172.122 with SMTP id w55mr25123032edc.120.1494326731673; Tue, 09 May 2017 03:45:31 -0700 (PDT) Received: from localhost.localdomain (xd93ddc2d.cust.hiper.dk. [217.61.220.45]) by smtp.gmail.com with ESMTPSA id o30sm2331510edc.42.2017.05.09.03.45.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 09 May 2017 03:45:31 -0700 (PDT) From: Christoffer Dall To: Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= Subject: [PULL 20/37] KVM: arm64: vgic-its: Add infrastructure for table lookup Date: Tue, 9 May 2017 12:44:49 +0200 Message-Id: <20170509104506.30929-21-cdall@linaro.org> X-Mailer: git-send-email 2.9.0 In-Reply-To: <20170509104506.30929-1-cdall@linaro.org> References: <20170509104506.30929-1-cdall@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170509_064553_114796_648270ED X-CRM114-Status: GOOD ( 19.05 ) 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: Marc Zyngier , Eric Auger , kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org 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 From: Eric Auger Add a generic scan_its_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 id offset to the next entry and also indicate whether the 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 scan_table will become static in subsequent patches Signed-off-by: Eric Auger Reviewed-by: Christoffer Dall Reviewed-by: Marc Zyngier --- virt/kvm/arm/vgic/vgic-its.c | 92 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/virt/kvm/arm/vgic/vgic-its.c b/virt/kvm/arm/vgic/vgic-its.c index 737ba3c..d5c0057 100644 --- a/virt/kvm/arm/vgic/vgic-its.c +++ b/virt/kvm/arm/vgic/vgic-its.c @@ -244,6 +244,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. @@ -1728,6 +1730,96 @@ 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 its_device *next; + u32 next_offset; + + if (list_is_last(&dev->dev_list, h)) + return 0; + next = list_next_entry(dev, 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 its_ite *next; + u32 next_offset; + + if (list_is_last(&ite->ite_list, h)) + return 0; + next = list_next_entry(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 + * + * Return: < 0 on error, 0 if last element was identified, id offset to next + * element otherwise + */ +typedef int (*entry_fn_t)(struct vgic_its *its, u32 id, void *entry, + void *opaque); + +/** + * scan_its_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: the ID of the first entry in the table + * (non zero for 2d level tables) + * @fn: function to apply on each entry + * + * Return: < 0 on error, 0 if last element was identified, 1 otherwise + * (the last element may not be found on second level tables) + */ +int scan_its_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; + int id = start_id; + gpa_t gpa = base; + int ret; + + while (len > 0) { + int next_offset; + size_t byte_offset; + + ret = kvm_read_guest(kvm, gpa, entry, esz); + if (ret) + goto out; + + next_offset = fn(its, id, entry, opaque); + if (next_offset <= 0) { + ret = next_offset; + goto out; + } + + byte_offset = next_offset * esz; + id += next_offset; + gpa += byte_offset; + len -= byte_offset; + } + ret = 1; + +out: + kfree(entry); + return ret; +} + /** * vgic_its_save_device_tables - Save the device table and all ITT * into guest RAM