From patchwork Wed Feb 1 12:52:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 13124369 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4F7CBC636CD for ; Wed, 1 Feb 2023 14:03:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=VC///6NtvLE5RN9TcBvgMuTcPZjxb8WBnF5vBeK2Jm8=; b=aLLkckXw4d6GMC IYAPhsXPVwYXD64SMHmRG4egFHBvy/hMcnPFSrwTt626V5A7ZaWW7Gr0tx+LUd2IRXV1xQcQaUauI F7ILxvUXxsOKZMnk8YzIfW7QUEJytfVmQpj2Rx3NDg2TC0mg4zQBpj4xX3hQGGAjmczWZfJflfkgF aKJdiKygHbCNgHGKZUQagfoBGMH9Yr0PlvNs4+4auvsxKDSXENYr8283n7tMkih2HErvh0ucoX2Ut af5BrK0QAjy9ULg6+7wcdcOYZtAPMr3CWNX5dkG6m8+7C4JFC87ERO3w6y7nps6pV1MmXR64IRiy6 wuChhT8MSCIW2x+FLsUw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pNDgl-00CBIE-PP; Wed, 01 Feb 2023 14:02:15 +0000 Received: from mail-wr1-x42f.google.com ([2a00:1450:4864:20::42f]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pNCi2-00BnI4-Aj for linux-arm-kernel@lists.infradead.org; Wed, 01 Feb 2023 12:59:33 +0000 Received: by mail-wr1-x42f.google.com with SMTP id o18so7753471wrj.3 for ; Wed, 01 Feb 2023 04:59:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mpqAxnbuSEle+b4ZozqvuRbtg84sOU2wLsHHXy60Xf4=; b=H8N6t8azgmWBH886NvtNIa/88xftg/d47CN1lNWpwQY8g41X2dJULDBUt8RWJTJeSI WThReSjvqRrR6PVFUKbeH+UFULZSwESdu2a/Lm9dDMa5lpETn4ZZ/+Opu68LiZmqq9sN 7P/93+ZxvEA2KO5Usrr4b/D33b6zAhssqHN/jocvhOTGBVp9C2dmRhMkotIsbkaCIu35 VlkRPDQbg9E1wdVnf/L6ySFyOuxkCqSpAFTCvU8t7w+YTqswvLQFx7+ljAE9t0xAPWKe 1n4xTnAPGdVNWDKH/odBfQ6RBeJ1YG99BQDMs9ceNFwJeIzOmly9KiYRBCORoZE+neG/ rcxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mpqAxnbuSEle+b4ZozqvuRbtg84sOU2wLsHHXy60Xf4=; b=0q6nsnP0CLnrOYLw7yBo/sK0ODUj8kbimALLkfdZj6p+ZSXunMpE2XLoW0FmRMSgpZ L8TezRnwKL/+ghr+vfiWYjhNeguaqUBnq/S3Jo1Va79UN1HoFccDhEJdURDU7jB2h6uJ 5LB6kdi1oT23zKpq+HUZzgONrMkrjXO2HZdSa6OqSEvAp4j5ofCvwdWznHl8PtW7PnPc 1aLGVg6mLtUOust+gyxfqEaciQwf58d7gMpAb0+cj2SecHn0JQM0/GY7gABaioA/IRAk 5IN0aUTFjB1ip7NsS7Y7HVq/2DQjicNVWjtN3LJsgCGUjeN4urkrt5imXvY5QeeF0cI8 K8jg== X-Gm-Message-State: AO0yUKW8YBQma8ki2BmsWTpPxgIRlBwhWsBVaFyxo5KEhNP+6I5rYs5g hYEIOCuTPBKJOiRz0kY0j3pv5T37D7sf7dpo X-Google-Smtp-Source: AK7set8PyrVNoe2MKjwL7WkMsO801xwUwupVDKmMI+GE07B0RcxtODTmVsZbmKSEFKICiI5L55zPfQ== X-Received: by 2002:adf:e181:0:b0:2bd:c4ea:3b59 with SMTP id az1-20020adfe181000000b002bdc4ea3b59mr2690988wrb.0.1675256365691; Wed, 01 Feb 2023 04:59:25 -0800 (PST) Received: from localhost.localdomain (054592b0.skybroadband.com. [5.69.146.176]) by smtp.gmail.com with ESMTPSA id m15-20020a056000024f00b002bfae16ee2fsm17972811wrz.111.2023.02.01.04.59.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 04:59:25 -0800 (PST) From: Jean-Philippe Brucker To: maz@kernel.org, catalin.marinas@arm.com, will@kernel.org, joro@8bytes.org Cc: robin.murphy@arm.com, james.morse@arm.com, suzuki.poulose@arm.com, oliver.upton@linux.dev, yuzenghui@huawei.com, smostafa@google.com, dbrazdil@google.com, ryan.roberts@arm.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, iommu@lists.linux.dev, Jean-Philippe Brucker Subject: [RFC PATCH 08/45] KVM: arm64: pkvm: Add pkvm_udelay() Date: Wed, 1 Feb 2023 12:52:52 +0000 Message-Id: <20230201125328.2186498-9-jean-philippe@linaro.org> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230201125328.2186498-1-jean-philippe@linaro.org> References: <20230201125328.2186498-1-jean-philippe@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230201_045930_407184_AB99925B X-CRM114-Status: GOOD ( 16.63 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add a simple delay loop for drivers. This could use more work. It should be possible to insert a wfe and save power, but I haven't studied whether it is safe to do so with the host in control of the event stream. The SMMU driver will use wfe anyway for frequent waits (provided the implementation can send command queue events). Signed-off-by: Jean-Philippe Brucker --- arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 3 ++ arch/arm64/kvm/hyp/nvhe/setup.c | 4 +++ arch/arm64/kvm/hyp/nvhe/timer-sr.c | 43 ++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/arch/arm64/kvm/hyp/include/nvhe/pkvm.h b/arch/arm64/kvm/hyp/include/nvhe/pkvm.h index 6160d1a34fa2..746dc1c05a8e 100644 --- a/arch/arm64/kvm/hyp/include/nvhe/pkvm.h +++ b/arch/arm64/kvm/hyp/include/nvhe/pkvm.h @@ -109,4 +109,7 @@ bool kvm_handle_pvm_hvc64(struct kvm_vcpu *vcpu, u64 *exit_code); struct pkvm_hyp_vcpu *pkvm_mpidr_to_hyp_vcpu(struct pkvm_hyp_vm *vm, u64 mpidr); +int pkvm_timer_init(void); +void pkvm_udelay(unsigned long usecs); + #endif /* __ARM64_KVM_NVHE_PKVM_H__ */ diff --git a/arch/arm64/kvm/hyp/nvhe/setup.c b/arch/arm64/kvm/hyp/nvhe/setup.c index 8a357637ce81..629e74c46b35 100644 --- a/arch/arm64/kvm/hyp/nvhe/setup.c +++ b/arch/arm64/kvm/hyp/nvhe/setup.c @@ -300,6 +300,10 @@ void __noreturn __pkvm_init_finalise(void) }; pkvm_pgtable.mm_ops = &pkvm_pgtable_mm_ops; + ret = pkvm_timer_init(); + if (ret) + goto out; + ret = fix_host_ownership(); if (ret) goto out; diff --git a/arch/arm64/kvm/hyp/nvhe/timer-sr.c b/arch/arm64/kvm/hyp/nvhe/timer-sr.c index 9072e71693ba..202df9003a0d 100644 --- a/arch/arm64/kvm/hyp/nvhe/timer-sr.c +++ b/arch/arm64/kvm/hyp/nvhe/timer-sr.c @@ -10,6 +10,10 @@ #include +#include + +static u32 timer_freq; + void __kvm_timer_set_cntvoff(u64 cntvoff) { write_sysreg(cntvoff, cntvoff_el2); @@ -46,3 +50,42 @@ void __timer_enable_traps(struct kvm_vcpu *vcpu) val |= CNTHCTL_EL1PCTEN; write_sysreg(val, cnthctl_el2); } + +static u64 pkvm_ticks_get(void) +{ + return __arch_counter_get_cntvct(); +} + +#define SEC_TO_US 1000000 + +int pkvm_timer_init(void) +{ + timer_freq = read_sysreg(cntfrq_el0); + /* + * TODO: The highest privileged level is supposed to initialize this + * register. But on some systems (which?), this information is only + * contained in the device-tree, so we'll need to find it out some other + * way. + */ + if (!timer_freq || timer_freq < SEC_TO_US) + return -ENODEV; + return 0; +} + + +#define pkvm_time_us_to_ticks(us) ((u64)(us) * timer_freq / SEC_TO_US) + +void pkvm_udelay(unsigned long usecs) +{ + u64 ticks = pkvm_time_us_to_ticks(usecs); + u64 start = pkvm_ticks_get(); + + while (true) { + u64 cur = pkvm_ticks_get(); + + if ((cur - start) >= ticks || cur < start) + break; + /* TODO wfe */ + cpu_relax(); + } +}