Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp856880pxb; Wed, 3 Feb 2021 21:38:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJzqviRKACBPHO1qvg0Qpro2t/Cln4FGHVhz5gosv7FHWt5X2VwwGGp0IOZvcxTIvt2DVcKs X-Received: by 2002:a17:906:e48:: with SMTP id q8mr6363027eji.478.1612417085721; Wed, 03 Feb 2021 21:38:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612417085; cv=none; d=google.com; s=arc-20160816; b=qWMQ4HtKbzEIsXD4p4IMLzTDgk22KJ+qpz1g6n3dVUl2ZeBXaBP95PtJFNTGdSA+pQ AlWjPL72/eECzie1J992GwwfL2yZSNYBYqLXOuPvj8yyrs4b0hTKkTFuKUntO1g3Hqhe aiBcMUDlVKH418yYcjQGzE8wqSH62TOoIF+QJ+dZZrg3nSMmHYGJdD8OEZH6vHHY5qsn 2DxTKiPrDqbfx3eBeeoDSvrkPtGkwp+9vEBYu/1RhBb0w7RKra4CWf4Vf1fVogQLMP9f 9PknMpXBhGHk6l5WzLm1gweI8uoHRp4m4MC36NIw1LGZ9/DSp7DLao51J+zXd8K8lbO5 fxsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :wdcironportexception:ironport-sdr:ironport-sdr:ironport-sdr :dkim-signature; bh=sV7ieOltsE2W6CF4ZpWRU965lrWlDvy6Q2CEh3pd6MQ=; b=0tXryNSF9TdjPXex9mx8A7o3P+RZiZR+b/+coLEOcezYeCJsjXVT1FA//fF+I3fWgQ WH6kAJBQCDKN5Y2wOPv3C7KLm1v+CRij8HHBYswSO6faSZsfCJC486Nk6cDbqhuEaY7y avUhGpHq3dimlyulHvcSkG/5LtJ+KlktgSh9LYh0pq6epNx75GS7dduIWYMlIup7SlPq 6sEm5+kSv7mBHfC6K1vEhqolGAv73okuuqovphf6J1K9LcEr1xGz2P7KF71ktVF7qDv9 FBnpEBKSPAvTU9Z1RmZUj5wCHyxDCG9W5X4OT2WfcHAE62yrgf4p8aRfJ8YZI7HcPbOT xj+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@wdc.com header.s=dkim.wdc.com header.b=MlDTis74; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=wdc.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id sa7si2723272ejb.28.2021.02.03.21.37.41; Wed, 03 Feb 2021 21:38:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=fail header.i=@wdc.com header.s=dkim.wdc.com header.b=MlDTis74; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=wdc.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231872AbhBDFfL (ORCPT + 99 others); Thu, 4 Feb 2021 00:35:11 -0500 Received: from esa5.hgst.iphmx.com ([216.71.153.144]:38918 "EHLO esa5.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231910AbhBDFfG (ORCPT ); Thu, 4 Feb 2021 00:35:06 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1612416905; x=1643952905; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zkz7usoeD7H22CZL96Ux56lGImBj0Y0tKyEFaWX6wQE=; b=MlDTis74QSYxE6KDJZntJiGN44er/N0dqjZwQorxMv4IS2r4KVoHAS8+ RZJ6qxGTO8RJHXZPFxXGhe/PSOG5hW6oBzdWXNao6Uoq1EFsWX26B5btl Awpgl96kKTF/9YhGkwzon00RK+b3yY15SUl6KgZv15mkisYR/VP1QXIkk t7I6RHThuj9i+aju0xgAt2zNuvnc6dcdkUjxn1u8Hw7SXGJZ0su4pKcl9 szZqj7lR9l1Eh+Icvoc8KAlAcT4TUg5mnC5ZOG0RSNzXhW+3v6tWEbUhX 2ODNOt4/RcL/IJikaIeXppxQ9h8BGgEI1RGEFeV4YGS9NmEBogTZHzKpi g==; IronPort-SDR: 5I+XYCo47HOs1VQ59oabpIpD0wmIY5JACiUu2cd6AkUxGjAH9gsXtpD+wsAVLJdOMQCyb+NogW aRAWtcPpsInRUcfdwVsqu3fkbEQCzeZD9Ql9wdzojLSJj9OX1Y3PIH6OAamGSxiu/8sFjLxwiG XUSJbJtVoSwVdddUIb0Z54atpafmLy63BQ+EjkTvNUbOSvOrHr+a9kgDMp93D/G/iTOuF5TEDv VKOYVK/FDubcv9iszboTCWDXqNSniScvQKxsUSyfWPv/VuYonbYIFsoPdVr7xUcKPE4WWSwUXe UI0= X-IronPort-AV: E=Sophos;i="5.79,400,1602518400"; d="scan'208";a="159086446" Received: from uls-op-cesaip01.wdc.com (HELO uls-op-cesaep01.wdc.com) ([199.255.45.14]) by ob1.hgst.iphmx.com with ESMTP; 04 Feb 2021 13:32:54 +0800 IronPort-SDR: y/AZzVlfMO7OO2HxqJpogOm2jFHLF/HuzCCb9uCARAVTGQc6jkg+rz/wqQ/se3PiUqHR1/owZn rfOFRj1YbgCjun0uqkX10gVJ3l+0ItbcQzsAhsRmV8HT+5ZmcXNz6QES2qtI5hFqYoMjrYqXTB ZSO/ynnDVXgU0Mv1oDg9ReJQVOQfyoZ6kDdO8zOP5lKeMRkjMf+pj0vXSRbkFZaGV8eoBkiRsr ohYoNXOiFxTC9jWCf3FgoJVPtpXgNSPaQYbFslB/SusopwOxkvAONl1OdW6hv2vOs07bcO1KmF jEhyye6/0UX9AONKW9tIVufM Received: from uls-op-cesaip02.wdc.com ([10.248.3.37]) by uls-op-cesaep01.wdc.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2021 21:16:54 -0800 IronPort-SDR: Oymd+BRq45I98/7mHFwaiDoPJ21T5pwZQXWJE5sq0waNsatNrFzyeyC2kQF3HolfPtZsN3amJf Qtvd/K/x4MYehN016BMKakZ3O2smkSv0KiZ5CuQw8deXQG1E2mk5oalyodKFk9KMklxAdWr+XM WIsgHqocevKHM9jEd2HSdZEjxc4d6QfIy60/+AW0BYbzmJy9yqZxRHX6+OSFrugi/Aajz4QN9i RLIxZjH4W6b7H0PKBG8vTIky/YGOCUTQweNFkBkj7a5c+naFedLTUmxPcIItIB81h7lVSPbn4Y 1nc= WDCIronportException: Internal Received: from cnf008142.ad.shared (HELO jedi-01.hgst.com) ([10.86.63.165]) by uls-op-cesaip02.wdc.com with ESMTP; 03 Feb 2021 21:32:55 -0800 From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Albert Ou , Anup Patel , Kefeng Wang , kvm-riscv@lists.infradead.org, kvm@vger.kernel.org, linux-riscv@lists.infradead.org, Palmer Dabbelt , Paul Walmsley Subject: [PATCH v2 4/6] RISC-V: Add v0.1 replacement SBI extensions defined in v02 Date: Wed, 3 Feb 2021 21:32:37 -0800 Message-Id: <20210204053239.1609558-5-atish.patra@wdc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210204053239.1609558-1-atish.patra@wdc.com> References: <20210204053239.1609558-1-atish.patra@wdc.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The SBI v0.2 contains some of the improved versions of required v0.1 extensions such as remote fence, timer and IPI. This patch implements those extensions. Signed-off-by: Atish Patra --- arch/riscv/kvm/Makefile | 2 +- arch/riscv/kvm/vcpu_sbi.c | 6 ++ arch/riscv/kvm/vcpu_sbi_replace.c | 136 ++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/kvm/vcpu_sbi_replace.c diff --git a/arch/riscv/kvm/Makefile b/arch/riscv/kvm/Makefile index 0f17dffe8d46..302501295397 100644 --- a/arch/riscv/kvm/Makefile +++ b/arch/riscv/kvm/Makefile @@ -11,5 +11,5 @@ kvm-objs := $(common-objs-y) kvm-objs += main.o vm.o vmid.o tlb.o mmu.o kvm-objs += vcpu.o vcpu_exit.o vcpu_switch.o vcpu_timer.o -kvm-objs += vcpu_sbi.o vcpu_sbi_base.o vcpu_sbi_legacy.o +kvm-objs += vcpu_sbi.o vcpu_sbi_base.o vcpu_sbi_legacy.o vcpu_sbi_replace.o obj-$(CONFIG_KVM) += kvm.o diff --git a/arch/riscv/kvm/vcpu_sbi.c b/arch/riscv/kvm/vcpu_sbi.c index c85c3b1dd2eb..e21ce1e69e03 100644 --- a/arch/riscv/kvm/vcpu_sbi.c +++ b/arch/riscv/kvm/vcpu_sbi.c @@ -32,10 +32,16 @@ static int kvm_linux_err_map_sbi(int err) extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_legacy; extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_base; +extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_time; +extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_ipi; +extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_rfence; static const struct kvm_vcpu_sbi_extension *sbi_ext[] = { &vcpu_sbi_ext_legacy, &vcpu_sbi_ext_base, + &vcpu_sbi_ext_time, + &vcpu_sbi_ext_ipi, + &vcpu_sbi_ext_rfence, }; void kvm_riscv_vcpu_sbi_forward(struct kvm_vcpu *vcpu, struct kvm_run *run) diff --git a/arch/riscv/kvm/vcpu_sbi_replace.c b/arch/riscv/kvm/vcpu_sbi_replace.c new file mode 100644 index 000000000000..dffb1930cada --- /dev/null +++ b/arch/riscv/kvm/vcpu_sbi_replace.c @@ -0,0 +1,136 @@ +// SPDX-License-Identifier: GPL-2.0 +/** + * Copyright (c) 2020 Western Digital Corporation or its affiliates. + * + * Authors: + * Atish Patra + */ + +#include +#include +#include +#include +#include +#include +#include + +static int kvm_sbi_ext_time_handler(struct kvm_vcpu *vcpu, struct kvm_run *run, + unsigned long *out_val, + struct kvm_cpu_trap *utrap, bool *exit) +{ + int ret = 0; + struct kvm_cpu_context *cp = &vcpu->arch.guest_context; + u64 next_cycle; + + if (!cp || (cp->a6 != SBI_EXT_TIME_SET_TIMER)) + return -EINVAL; + +#if __riscv_xlen == 32 + next_cycle = ((u64)cp->a1 << 32) | (u64)cp->a0; +#else + next_cycle = (u64)cp->a0; +#endif + kvm_riscv_vcpu_timer_next_event(vcpu, next_cycle); + + return ret; +} + +const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_time = { + .extid_start = SBI_EXT_TIME, + .extid_end = SBI_EXT_TIME, + .handler = kvm_sbi_ext_time_handler, +}; + +static int kvm_sbi_ext_ipi_handler(struct kvm_vcpu *vcpu, struct kvm_run *run, + unsigned long *out_val, + struct kvm_cpu_trap *utrap, bool *exit) +{ + int i, ret = 0; + struct kvm_vcpu *tmp; + struct kvm_cpu_context *cp = &vcpu->arch.guest_context; + unsigned long hmask = cp->a0; + unsigned long hbase = cp->a1; + + if (!cp || (cp->a6 != SBI_EXT_IPI_SEND_IPI)) + return -EINVAL; + + kvm_for_each_vcpu(i, tmp, vcpu->kvm) { + if (hbase != -1UL) { + if (tmp->vcpu_id < hbase) + continue; + if (!(hmask & (1UL << (tmp->vcpu_id - hbase)))) + continue; + } + ret = kvm_riscv_vcpu_set_interrupt(tmp, IRQ_VS_SOFT); + if (ret < 0) + break; + } + + return ret; +} + +const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_ipi = { + .extid_start = SBI_EXT_IPI, + .extid_end = SBI_EXT_IPI, + .handler = kvm_sbi_ext_ipi_handler, +}; + +static int kvm_sbi_ext_rfence_handler(struct kvm_vcpu *vcpu, struct kvm_run *run, + unsigned long *out_val, + struct kvm_cpu_trap *utrap, bool *exit) +{ + int i, ret = 0; + struct cpumask cm, hm; + struct kvm_vcpu *tmp; + struct kvm_cpu_context *cp = &vcpu->arch.guest_context; + unsigned long hmask = cp->a0; + unsigned long hbase = cp->a1; + unsigned long funcid = cp->a6; + + if (!cp) + return -EINVAL; + + cpumask_clear(&cm); + cpumask_clear(&hm); + kvm_for_each_vcpu(i, tmp, vcpu->kvm) { + if (hbase != -1UL) { + if (tmp->vcpu_id < hbase) + continue; + if (!(hmask & (1UL << (tmp->vcpu_id - hbase)))) + continue; + } + if (tmp->cpu < 0) + continue; + cpumask_set_cpu(tmp->cpu, &cm); + } + + riscv_cpuid_to_hartid_mask(&cm, &hm); + + switch (funcid) { + case SBI_EXT_RFENCE_REMOTE_FENCE_I: + ret = sbi_remote_fence_i(cpumask_bits(&hm)); + break; + case SBI_EXT_RFENCE_REMOTE_SFENCE_VMA: + ret = sbi_remote_hfence_vvma(cpumask_bits(&hm), cp->a2, cp->a3); + break; + case SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID: + ret = sbi_remote_hfence_vvma_asid(cpumask_bits(&hm), cp->a2, + cp->a3, cp->a4); + break; + case SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA: + case SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA_VMID: + case SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA: + case SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA_ASID: + /* TODO: implement for nested hypervisor case */ + default: + ret = -EOPNOTSUPP; + } + + return ret; +} + +const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_rfence = { + .extid_start = SBI_EXT_RFENCE, + .extid_end = SBI_EXT_RFENCE, + .handler = kvm_sbi_ext_rfence_handler, +}; -- 2.25.1