From patchwork Wed Mar 21 16:32:01 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andre Przywara X-Patchwork-Id: 132217 Delivered-To: patch@linaro.org Received: by 10.46.84.29 with SMTP id i29csp2356053ljb; Wed, 21 Mar 2018 09:34:59 -0700 (PDT) X-Google-Smtp-Source: AG47ELvYkYg6XyivG1TuMJaWHUF3hDx6TgxUM0nrmi7TcsumESvr2kCZSkf/nGbvhCp6Feyw2nr8 X-Received: by 2002:a24:c543:: with SMTP id f64-v6mr2534989itg.143.1521650099403; Wed, 21 Mar 2018 09:34:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521650099; cv=none; d=google.com; s=arc-20160816; b=bCtghDedAhqAbx61CqrPaKBGK98MiXTepay7hJ1Eqk/SoX92baxBD3ikJNCg2ADVSB j8i08N2zhrfhhTJcRuJ/BPkgDsWHyHMY+RIaQlQcL1GTkBvL2B4b1nIiERAeI6V8nXFn ixw2KL4OstfJ6ns/lwx6tayXX17Ukw0AJGN6hA6S4GUD65H7bhQNn78Ogn4F/WNvOJMu Rpg9G6OlzUEu0SUOXG/57722XyMBpX0HTQMtfor22dZ90JdQUMmMVlmpiSh2yz9MRA1G FqGR6/edyntxHxWk2kF/e4Rvo9T00DCFysou/t2qlA0V3N2oSZc5CJs11rD79rTRbMCP d2fA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc :list-subscribe:list-help:list-post:list-unsubscribe:list-id :precedence:subject:references:in-reply-to:message-id:date:to:from :dkim-signature:arc-authentication-results; bh=X5rKTZjTfoURBCXSxJVdQB2+yXWh5TYdNZ30eUO40Po=; b=K52jBaj4KuP7PDQWgRjLtSiynmX1DUJT2+/H1KA3fd1VXQs1BpmNWm/D9/gqVwXAdz VJp3r3862MRrL8pD0DlwGpkA9Giq6NHxWQe5aolNEMvfT1MSfaaHooOREYw5fpAoVmtQ PCOXktAhheFikWGXr0Kz0H0AsHbq0RLOJtEMRQdWebLZ/RUWR23GMOrLqzugzBjimwMO K3ECEcYmsNAvxAWZNvnbJULFwciM4IMXgSKYgzr2Wm4xVOVvN7F9zL/aTeXLELJRzoir I6VXMQruzlniKa1+2ahH/fQkRo6bOhfrxch1BYADafW/7j/C6yrGjztoZceR4UGZ2Tt9 5qPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=ZOmtEEA5; spf=pass (google.com: best guess record for domain of xen-devel-bounces@lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org. [192.237.175.120]) by mx.google.com with ESMTPS id j15-v6si3544716itf.73.2018.03.21.09.34.59 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 21 Mar 2018 09:34:59 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of xen-devel-bounces@lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=ZOmtEEA5; spf=pass (google.com: best guess record for domain of xen-devel-bounces@lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1eygfZ-00022z-B4; Wed, 21 Mar 2018 16:32:57 +0000 Received: from us1-rack-dfw2.inumbo.com ([104.130.134.6]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1eygfX-000210-Ii for xen-devel@lists.xenproject.org; Wed, 21 Mar 2018 16:32:55 +0000 X-Inumbo-ID: 78590b12-2d25-11e8-9728-bc764e045a96 Received: from mail-wm0-x242.google.com (unknown [2a00:1450:400c:c09::242]) by us1-rack-dfw2.inumbo.com (Halon) with ESMTPS id 78590b12-2d25-11e8-9728-bc764e045a96; Wed, 21 Mar 2018 17:32:39 +0100 (CET) Received: by mail-wm0-x242.google.com with SMTP id x82so10945638wmg.1 for ; Wed, 21 Mar 2018 09:32:54 -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=UqXDQ4uTDoYArXVqQiB+NMLnW7mFy43MueGilTz7szM=; b=ZOmtEEA5ZmzZEmrfpGCFDv6ldI+EZsmy0I3I92lqQ/dYpLbWb/IFPTumkSpSvUhdYQ I6snMGN7E+0bGBR81vILHH7Cv/8hsOx4NtawHXNZNdepW7b8M7JM3xC4pC//UYTc1Qjy b9KtGy19Hb33UOwLfaxI/fcxMY+9YCHU+v4QE= 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=UqXDQ4uTDoYArXVqQiB+NMLnW7mFy43MueGilTz7szM=; b=FCM0khKDP2D6s4o6HozmrREniq6Yx+jy9Pq/uKbC2abcwnyBRUk0KrRcC7TEyUOQTw UG4NsuwgmLdkweOYldh2djntJiD1qsD3C08ONaSFdXqochLYtpPmpIuTRB1IC20esIIJ NatJ/EgBWxLd9MC85Ro1ikHm577Vjm3PsrQHme0Z2Fh/TGmZdIiN6u7mP4glRv/G+MFZ vTX9/H7R3xFsnree3P0qh8bqf0gbz/87sw8RCDICAXHZCVswQNhRugsKkZ6XEBjW8/iy NN8g5w37GVVPMQbN3BTUlwlh5JI5a/tX2hZ55viQ8FnlSHpvZSPNNTzvsLvPq67TYeTF oR8A== X-Gm-Message-State: AElRT7FillzBn5cwFVNmEs8/xZuKAHmv1/QhZp7ZZhJFsmGsMkMBWPZj wWRa4kJVbybxyyuNJUU0PUqgbQ== X-Received: by 10.28.212.141 with SMTP id l135mr2877376wmg.122.1521649973244; Wed, 21 Mar 2018 09:32:53 -0700 (PDT) Received: from e104803-lin.lan (mail.andrep.de. [217.160.17.100]) by smtp.gmail.com with ESMTPSA id n64sm4423724wmd.11.2018.03.21.09.32.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 21 Mar 2018 09:32:52 -0700 (PDT) From: Andre Przywara To: Julien Grall , Stefano Stabellini Date: Wed, 21 Mar 2018 16:32:01 +0000 Message-Id: <20180321163235.12529-6-andre.przywara@linaro.org> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20180321163235.12529-1-andre.przywara@linaro.org> References: <20180321163235.12529-1-andre.przywara@linaro.org> Subject: [Xen-devel] [PATCH v3 05/39] ARM: timer: Handle level triggered IRQs correctly X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: xen-devel@lists.xenproject.org, Andre Przywara MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" The ARM Generic Timer uses a level-sensitive interrupt semantic. We easily catch when the line goes high, as this triggers the hardware IRQ. However we also have to keep track of when the line lowers, as the emulation depends on it: Upon entering the guest, the new VGIC will *clear* the virtual interrupt line, so it needs to re-sample the actual state after returning from the guest. So we have to sync the state of the interrupt condition at certain points to catch when the line goes low and we can remove the vtimer vIRQ from the vGIC (and the LR). The VGIC in Xen so far only implemented edge triggered vIRQs, really, so we need to add new functionality to re-sample the interrupt state. Do this only when the new VGIC is in use. Signed-off-by: Andre Przywara Acked-by: Julien Grall Acked-by: Stefano Stabellini --- Changelog v2 ... v3: - move vtimer_sync() from time.c into vtimer.c - rename function to vtimer_update_irqs() - refactor functionality into new static function, to ... - handle physical timer as well - extending comments Changelog v1 ... v2: - restrict to new VGIC - add TODO: comment xen/arch/arm/traps.c | 11 ++++++++++ xen/arch/arm/vtimer.c | 49 ++++++++++++++++++++++++++++++++++++++++++++ xen/include/asm-arm/vtimer.h | 1 + 3 files changed, 61 insertions(+) diff --git a/xen/arch/arm/traps.c b/xen/arch/arm/traps.c index 7411bff7a7..2638446693 100644 --- a/xen/arch/arm/traps.c +++ b/xen/arch/arm/traps.c @@ -2024,6 +2024,17 @@ static void enter_hypervisor_head(struct cpu_user_regs *regs) if ( current->arch.hcr_el2 & HCR_VA ) current->arch.hcr_el2 = READ_SYSREG(HCR_EL2); +#ifdef CONFIG_NEW_VGIC + /* + * We need to update the state of our emulated devices using level + * triggered interrupts before syncing back the VGIC state. + * + * TODO: Investigate whether this is necessary to do on every + * trap and how it can be optimised. + */ + vtimer_update_irqs(current); +#endif + vgic_sync_from_lrs(current); } } diff --git a/xen/arch/arm/vtimer.c b/xen/arch/arm/vtimer.c index 8164f6c7f1..c99dd237d1 100644 --- a/xen/arch/arm/vtimer.c +++ b/xen/arch/arm/vtimer.c @@ -334,6 +334,55 @@ bool vtimer_emulate(struct cpu_user_regs *regs, union hsr hsr) } } +static void vtimer_update_irq(struct vcpu *v, struct vtimer *vtimer, + uint32_t vtimer_ctl) +{ + bool level; + + /* Filter for the three bits that determine the status of the timer */ + vtimer_ctl &= (CNTx_CTL_ENABLE | CNTx_CTL_PENDING | CNTx_CTL_MASK); + + /* The level is high if the timer is pending and enabled, but not masked. */ + level = (vtimer_ctl == (CNTx_CTL_ENABLE | CNTx_CTL_PENDING)); + + /* + * This is mostly here to *lower* the virtual interrupt line if the timer + * is no longer pending. + * We would have injected an IRQ already via SOFTIRQ when the timer expired. + * Doing it here again is basically a NOP if the line was already high. + */ + vgic_inject_irq(v->domain, v, vtimer->irq, level); +} + +/** + * vtimer_update_irqs() - update the virtual timers' IRQ lines after a guest run + * @vcpu: The VCPU to sync the timer state + * + * After returning from a guest, update the state of the timers' virtual + * interrupt lines, to model the level triggered interrupts correctly. + * If the guest has handled a timer interrupt, the virtual interrupt line + * needs to be lowered explicitly. vgic_inject_irq() takes care of that. + */ +void vtimer_update_irqs(struct vcpu *v) +{ + /* + * For the virtual timer we read the current state from the hardware. + * Technically we should keep the CNTx_CTL_MASK bit here, to catch if + * the timer interrupt is masked. However Xen *always* masks the timer + * upon entering the hypervisor, leaving it up to the guest to un-mask it. + * So we would always read a "low" level, despite the condition being + * actually "high". Ignoring the mask bit solves this (for now). + * + * TODO: The proper fix for this is to make vtimer vIRQ hardware mapped, + * but this requires reworking the arch timer to implement this. + */ + vtimer_update_irq(v, &v->arch.virt_timer, + READ_SYSREG32(CNTV_CTL_EL0) & ~CNTx_CTL_MASK); + + /* For the physical timer we rely on our emulated state. */ + vtimer_update_irq(v, &v->arch.phys_timer, v->arch.phys_timer.ctl); +} + /* * Local variables: * mode: C diff --git a/xen/include/asm-arm/vtimer.h b/xen/include/asm-arm/vtimer.h index 5aaddc6f63..91d88b377f 100644 --- a/xen/include/asm-arm/vtimer.h +++ b/xen/include/asm-arm/vtimer.h @@ -27,6 +27,7 @@ extern bool vtimer_emulate(struct cpu_user_regs *regs, union hsr hsr); extern int virt_timer_save(struct vcpu *v); extern int virt_timer_restore(struct vcpu *v); extern void vcpu_timer_destroy(struct vcpu *v); +void vtimer_update_irqs(struct vcpu *v); #endif