From patchwork Thu Mar 16 11:20:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andre Przywara X-Patchwork-Id: 9627925 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 E03A56048C for ; Thu, 16 Mar 2017 11:20:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D453B28628 for ; Thu, 16 Mar 2017 11:20:40 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C53A728643; Thu, 16 Mar 2017 11:20:40 +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=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 5141D28604 for ; Thu, 16 Mar 2017 11:20:40 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1coTR1-0007ym-OK; Thu, 16 Mar 2017 11:19:11 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1coTR0-0007ub-9u for xen-devel@lists.xenproject.org; Thu, 16 Mar 2017 11:19:10 +0000 Received: from [193.109.254.147] by server-10.bemta-6.messagelabs.com id 7A/23-13192-DA47AC85; Thu, 16 Mar 2017 11:19:09 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrKLMWRWlGSWpSXmKPExsVysyfVTXdtyak Ig7etnBbft0xmcmD0OPzhCksAYxRrZl5SfkUCa0b7pANsBY2qFScfXGduYLwu08XIxSEksJlR Ys3RDYxdjJxAznJGiddtriA2m4CuxI6br5lBbBGBUIk5Px+B2cwClRIXP+xnA7GFBdwkTh6/B WRzcLAIqEpMvFwEEuYVsJHo2f6dHcSWEJCTaDh/H6yVEyjecXIPG8Qqa4nPRz+zT2DkXsDIsI pRozi1qCy1SNfIWC+pKDM9oyQ3MTNH19DATC83tbg4MT01JzGpWC85P3cTI9C7DECwg/HP/MB DjJIcTEqivCqCJyKE+JLyUyozEosz4otKc1KLDzHKcHAoSfBOTgPKCRalpqdWpGXmAMMMJi3B waMkwjsTJM1bXJCYW5yZDpE6xagoJc7bBJIQAElklObBtcFC+xKjrJQwLyPQIUI8BalFuZklq PKvGMU5GJWEedNBpvBk5pXATX8FtJgJaHHizyMgi0sSEVJSDYxZF14YF2oF+82OL7GrZeIU2h T1dcall4Jv+u6xp7/Yvbbu9NbF/zaalZ0p5m7fdY0v+Mjpiq4jIgbxB5TCJ7xcyBxfsjqu9EG ZRsUU7oUzjj/PO5d976VVVfc1SfMn7Z8ffOA9dH3LGfM5dr8/H+I+zvhDy7moPCH/5tTVUYHc 4oeYXbQvJQkqsRRnJBpqMRcVJwIAU6wOFGgCAAA= X-Env-Sender: andre.przywara@arm.com X-Msg-Ref: server-15.tower-27.messagelabs.com!1489663148!39226947!1 X-Originating-IP: [217.140.101.70] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.2.3; banners=-,-,- X-VirusChecked: Checked Received: (qmail 49240 invoked from network); 16 Mar 2017 11:19:08 -0000 Received: from foss.arm.com (HELO foss.arm.com) (217.140.101.70) by server-15.tower-27.messagelabs.com with SMTP; 16 Mar 2017 11:19:08 -0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 4973E152D; Thu, 16 Mar 2017 04:19:08 -0700 (PDT) Received: from e104803-lin.lan (unknown [10.1.207.46]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 44A823F5C9; Thu, 16 Mar 2017 04:19:07 -0700 (PDT) From: Andre Przywara To: Stefano Stabellini , Julien Grall Date: Thu, 16 Mar 2017 11:20:18 +0000 Message-Id: <20170316112030.20419-16-andre.przywara@arm.com> X-Mailer: git-send-email 2.9.0 In-Reply-To: <20170316112030.20419-1-andre.przywara@arm.com> References: <20170316112030.20419-1-andre.przywara@arm.com> Cc: xen-devel@lists.xenproject.org, Shanker Donthineni , Vijay Kilari Subject: [Xen-devel] [PATCH v2 15/27] ARM: vITS: introduce translation table walks X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP The ITS stores the target (v)CPU and the (virtual) LPI number in tables. Introduce functions to walk those tables and translate an device ID - event ID pair into a pair of virtual LPI and vCPU. Since the final interrupt translation tables can be smaller than a page, we map them on demand (which is cheap on arm64). Also we take care of the locking on the way, since we can't easily protect those ITTs from being altered by the guest. To allow compiling without warnings, we declare two functions as non-static for the moment. Signed-off-by: Andre Przywara --- xen/arch/arm/vgic-v3-its.c | 135 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) diff --git a/xen/arch/arm/vgic-v3-its.c b/xen/arch/arm/vgic-v3-its.c index 5337638..267a573 100644 --- a/xen/arch/arm/vgic-v3-its.c +++ b/xen/arch/arm/vgic-v3-its.c @@ -62,6 +62,141 @@ struct vits_itte uint16_t collection; }; +#define UNMAPPED_COLLECTION ((uint16_t)~0) + +/* Must be called with the ITS lock held. */ +static struct vcpu *get_vcpu_from_collection(struct virt_its *its, int collid) +{ + uint16_t vcpu_id; + + if ( collid >= its->max_collections ) + return NULL; + + vcpu_id = its->coll_table[collid]; + if ( vcpu_id == UNMAPPED_COLLECTION || vcpu_id >= its->d->max_vcpus ) + return NULL; + + return its->d->vcpu[vcpu_id]; +} + +#define DEV_TABLE_ITT_ADDR(x) ((x) & GENMASK(51, 8)) +#define DEV_TABLE_ITT_SIZE(x) (BIT(((x) & GENMASK(7, 0)) + 1)) +#define DEV_TABLE_ENTRY(addr, bits) \ + (((addr) & GENMASK(51, 8)) | (((bits) - 1) & GENMASK(7, 0))) + +static paddr_t get_itte_address(struct virt_its *its, + uint32_t devid, uint32_t evid) +{ + paddr_t addr; + + if ( devid >= its->max_devices ) + return ~0; + + if ( evid >= DEV_TABLE_ITT_SIZE(its->dev_table[devid]) ) + return ~0; + + addr = DEV_TABLE_ITT_ADDR(its->dev_table[devid]); + + return addr + evid * sizeof(struct vits_itte); +} + +/* + * Looks up a given deviceID/eventID pair on an ITS and returns a pointer to + * the corresponding ITTE. This maps the respective guest page into Xen. + * Once finished with handling the ITTE, call put_devid_evid() to unmap + * the page again. + * Must be called with the ITS lock held. + */ +static struct vits_itte *get_devid_evid(struct virt_its *its, + uint32_t devid, uint32_t evid) +{ + paddr_t addr = get_itte_address(its, devid, evid); + + if ( addr == ~0 ) + return NULL; + + return map_guest_pages(its->d, addr, 1); +} + +/* Must be called with the ITS lock held. */ +static void put_devid_evid(struct virt_its *its, struct vits_itte *itte) +{ + unmap_guest_pages(itte, 1); +} + +/* + * Queries the collection and device tables to get the vCPU and virtual + * LPI number for a given guest event. This takes care of mapping the + * respective tables and validating the values, since we can't efficiently + * protect the ITTs with their less-than-page-size granularity. + * Takes and drops the its_lock. + */ +bool read_itte(struct virt_its *its, uint32_t devid, uint32_t evid, + struct vcpu **vcpu, uint32_t *vlpi) +{ + struct vits_itte *itte; + int collid; + uint32_t _vlpi; + struct vcpu *_vcpu; + + spin_lock(&its->its_lock); + itte = get_devid_evid(its, devid, evid); + if ( !itte ) + { + spin_unlock(&its->its_lock); + return false; + } + collid = itte->collection; + _vlpi = itte->vlpi; + put_devid_evid(its, itte); + + _vcpu = get_vcpu_from_collection(its, collid); + spin_unlock(&its->its_lock); + + if ( !_vcpu ) + return false; + + if ( collid >= its->max_collections ) + return false; + + *vcpu = _vcpu; + *vlpi = _vlpi; + + return true; +} + +#define SKIP_LPI_UPDATE 1 +bool write_itte(struct virt_its *its, uint32_t devid, uint32_t evid, + uint32_t collid, uint32_t vlpi, struct vcpu **vcpu) +{ + struct vits_itte *itte; + + if ( collid >= its->max_collections ) + return false; + + /* TODO: validate vlpi */ + + spin_lock(&its->its_lock); + itte = get_devid_evid(its, devid, evid); + if ( !itte ) + { + spin_unlock(&its->its_lock); + return false; + } + + itte->collection = collid; + if ( vlpi != SKIP_LPI_UPDATE ) + itte->vlpi = vlpi; + + if ( vcpu ) + *vcpu = get_vcpu_from_collection(its, collid); + + put_devid_evid(its, itte); + spin_unlock(&its->its_lock); + + return true; +} + /************************************** * Functions that handle ITS commands * **************************************/