Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp5969224rwb; Wed, 21 Sep 2022 15:28:01 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7qIVW0Qj+72Zj3qhzTvoF2kFzL22S9Z/7ZlDpkzKU8ZWDDEXNJj3s0WT6W2r9STmh/RfJA X-Received: by 2002:a17:906:4fca:b0:782:2484:6d72 with SMTP id i10-20020a1709064fca00b0078224846d72mr407574ejw.150.1663799281470; Wed, 21 Sep 2022 15:28:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663799281; cv=none; d=google.com; s=arc-20160816; b=TPn2j+ITEt8GpMDeqp9RwaXaaIwFPHZn6oNvp9OBHudixBQpoqNlYU2u/DRFobU3gF +5X2qYTQptkcAnHQUhQpCz9dHM3PNRlWG0m5u2YYzZyBXxAILdYWF7CmDs9GKcRXeXG2 be3aFqCFtCYy604fSvikRJwUFkFMc+aP2IpSYbzL+V4w/v0VXjCgI0LC2gmylecSKJR9 J4R66auRL/kuO07kZPo5Cw+CKz8kJ3bVY7/l+F4M6QixbGVXihdI4bU+jdTp3IHKOt// 7NYWq+ojQr/NTDkQswnqJbimLkoLst85VC/eK+2IJJ2GdKetWdPRNttk+7iI8uev1Hi8 8DHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:from :dkim-signature; bh=9QztRJhDFYW9WZfYeW38b+1O0y8hGYDxe0zbOFpNx/M=; b=IFaTiRl6nkVw/hg9ncge9lEHd/dQJ6ileqSwPNFYBLj2ekQau2r8RPSyonpeNETH6y 2b2m7h+YwJ+UUS/16URUTMZVvKMqsAxaRs3g/sF0JogLoSIvR4D6LHZf/ZUktqfWDHoo nbG20llUK6QHX/iP3vAQImb310lqcuyCWSJorvhjKvqtTqLkYvTKIT15HrAIDJ1iKjrx ZOjZOiGTWeBWVJlVNDPzhWjUs2sfa01SIUPamSrBwbt2R2zyBlGYov/EG7IYDq4QFzq9 +IUn4NSYxfsF5XKHHTSYNEzyUKYoreefp0wGpgqI7dIvcJW6Yfiz0X4eyVqtT5RNQi0E 4a+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=4FXEAyWn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hv11-20020a17090760cb00b007309350f2f9si4001751ejc.555.2022.09.21.15.27.35; Wed, 21 Sep 2022 15:28:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=fail header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=4FXEAyWn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229907AbiIUVxK (ORCPT + 99 others); Wed, 21 Sep 2022 17:53:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230220AbiIUVww (ORCPT ); Wed, 21 Sep 2022 17:52:52 -0400 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97328A6C0A for ; Wed, 21 Sep 2022 14:52:50 -0700 (PDT) Received: by mail-pl1-x636.google.com with SMTP id iw17so7034160plb.0 for ; Wed, 21 Sep 2022 14:52:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=9QztRJhDFYW9WZfYeW38b+1O0y8hGYDxe0zbOFpNx/M=; b=4FXEAyWnl8rEPNR7wbfnj5FmGXdFG3ED7A5Trq3qXhO0TVRAkAzXiPi9m5aZu4TXdp 5gc4oJKyuy3QlWo8Zd3hGGQHg+9Z3RO3jY9PV5ln8dUqo9ty/PLDiMeFquJ8YxaLG8GP VAcPaec5zu3wKgRpoab3awGaBYP0XOZZmopsp0pGY/nhLfCGDz+A8GlBJ9+ZdvUoxqNr C+gdgHobTAA5WrJkfWII9pqdRzQ53hwmIbOPGQxSk8FK52ETaEDBp6llve5Su/T6KTWQ fPffBNJisYYv56YkayiFxA+AD2MUBbQbT+ttFlAwI9DLuIe6coi9VqGQ9XbGR30mhK3p LlSg== 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; bh=9QztRJhDFYW9WZfYeW38b+1O0y8hGYDxe0zbOFpNx/M=; b=DdTBIzAPYy78Bn0hpAVAH5CZIrgRVA4lLqBsbOWokwDl+0fJMzuifsw6V+B1d2mZcw QVmgmF7yqLeUQdCtkpnYZwhRmMQNK4h1omm8tmhj8AyYX0I/ojA3p4Ov0x8BrxmOYkr0 A95vUmh3+S3Sl3rPE4M9hC9+TSz+cJtX+Nsw/kx1xEYdKLU0z6o6CotiPzoUFJhkLB2V gHh9bX9AV50rV3fhQc/eCDAje0g9SEJ19FNnDrn7KGL4aQQuz81nlhzvlDeUt3X6YA5C b5uFZ3UMB0JTuuhoQ6bZP0VdDnxNK05906JK6ppKKvb0O3ym1mGT3XJEXlZznALbIzN+ s3Gw== X-Gm-Message-State: ACrzQf2+T9P34gMKWijyJ3EJ2ryX6/VHTqXM+NafZoB8wsLrywqn8tIh PK0niQzDLqS0zNfhQuMdMmKzsg== X-Received: by 2002:a17:903:44e:b0:178:8492:d0ff with SMTP id iw14-20020a170903044e00b001788492d0ffmr126643plb.146.1663797170122; Wed, 21 Sep 2022 14:52:50 -0700 (PDT) Received: from stillson.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id k7-20020aa79727000000b005484d133127sm2634536pfg.129.2022.09.21.14.52.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Sep 2022 14:52:49 -0700 (PDT) From: Chris Stillson Cc: Greentime Hu , Vincent Chen , Paul Walmsley , Palmer Dabbelt , Albert Ou , Eric Biederman , Kees Cook , Anup Patel , Atish Patra , Oleg Nesterov , Heinrich Schuchardt , Guo Ren , Chris Stillson , Conor Dooley , Paolo Bonzini , Alexandre Ghiti , Qinglin Pan , Arnd Bergmann , Heiko Stuebner , Jisheng Zhang , Dao Lu , "Peter Zijlstra (Intel)" , Sunil V L , Han-Kuan Chen , Li Zhengyu , Changbin Du , Alexander Graf , Ard Biesheuvel , Tsukasa OI , Yury Norov , Frederic Weisbecker , Mark Rutland , Nicolas Saenz Julienne , Vitaly Wool , Myrtle Shah , Nick Knight , Catalin Marinas , Will Deacon , Mark Brown , WANG Xuerui , Alexey Dobriyan , Huacai Chen , Christian Brauner , Eugene Syromiatnikov , Colin Cross , Peter Collingbourne , Andrew Morton , Suren Baghdasaryan , Barret Rhoden , Davidlohr Bueso , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org Subject: [PATCH v12 12/17] riscv: Add support for kernel mode vector Date: Wed, 21 Sep 2022 14:43:54 -0700 Message-Id: <20220921214439.1491510-12-stillson@rivosinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220921214439.1491510-1-stillson@rivosinc.com> References: <20220921214439.1491510-1-stillson@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Greentime Hu Add kernel_rvv_begin() and kernel_rvv_end() function declarations and corresponding definitions in kernel_mode_vector.c These are needed to wrap uses of vector in kernel mode. Co-developed-by: Vincent Chen Signed-off-by: Vincent Chen Signed-off-by: Greentime Hu --- arch/riscv/include/asm/vector.h | 3 + arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/kernel_mode_vector.c | 132 +++++++++++++++++++++++++ arch/riscv/kernel/vector.S | 9 ++ 4 files changed, 145 insertions(+) create mode 100644 arch/riscv/kernel/kernel_mode_vector.c diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index 16304b0c6a6f..a59841cc81fb 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -10,5 +10,8 @@ void rvv_enable(void); void rvv_disable(void); +void kernel_rvv_begin(void); +void kernel_rvv_end(void); +void vector_flush_cpu_state(void); #endif /* ! __ASM_RISCV_VECTOR_H */ diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index 35752fb6d145..8c238415f800 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -56,6 +56,7 @@ obj-$(CONFIG_MMU) += vdso.o vdso/ obj-$(CONFIG_RISCV_M_MODE) += traps_misaligned.o obj-$(CONFIG_FPU) += fpu.o obj-$(CONFIG_VECTOR) += vector.o +obj-$(CONFIG_VECTOR) += kernel_mode_vector.o obj-$(CONFIG_SMP) += smpboot.o obj-$(CONFIG_SMP) += smp.o obj-$(CONFIG_SMP) += cpu_ops.o diff --git a/arch/riscv/kernel/kernel_mode_vector.c b/arch/riscv/kernel/kernel_mode_vector.c new file mode 100644 index 000000000000..0277168af0c5 --- /dev/null +++ b/arch/riscv/kernel/kernel_mode_vector.c @@ -0,0 +1,132 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2012 ARM Ltd. + * Author: Catalin Marinas + * Copyright (C) 2017 Linaro Ltd. + * Copyright (C) 2021 SiFive + */ +#include +#include +#include +#include +#include + +#include +#include + +DECLARE_PER_CPU(bool, vector_context_busy); +DEFINE_PER_CPU(bool, vector_context_busy); + +/* + * may_use_vector - whether it is allowable at this time to issue vector + * instructions or access the vector register file + * + * Callers must not assume that the result remains true beyond the next + * preempt_enable() or return from softirq context. + */ +static __must_check inline bool may_use_vector(void) +{ + /* + * vector_context_busy is only set while preemption is disabled, + * and is clear whenever preemption is enabled. Since + * this_cpu_read() is atomic w.r.t. preemption, vector_context_busy + * cannot change under our feet -- if it's set we cannot be + * migrated, and if it's clear we cannot be migrated to a CPU + * where it is set. + */ + return !in_irq() && !irqs_disabled() && !in_nmi() && + !this_cpu_read(vector_context_busy); +} + +/* + * Claim ownership of the CPU vector context for use by the calling context. + * + * The caller may freely manipulate the vector context metadata until + * put_cpu_vector_context() is called. + */ +static void get_cpu_vector_context(void) +{ + bool busy; + + preempt_disable(); + busy = __this_cpu_xchg(vector_context_busy, true); + + WARN_ON(busy); +} + +/* + * Release the CPU vector context. + * + * Must be called from a context in which get_cpu_vector_context() was + * previously called, with no call to put_cpu_vector_context() in the + * meantime. + */ +static void put_cpu_vector_context(void) +{ + bool busy = __this_cpu_xchg(vector_context_busy, false); + + WARN_ON(!busy); + preempt_enable(); +} + +/* + * kernel_rvv_begin(): obtain the CPU vector registers for use by the calling + * context + * + * Must not be called unless may_use_vector() returns true. + * Task context in the vector registers is saved back to memory as necessary. + * + * A matching call to kernel_rvv_end() must be made before returning from the + * calling context. + * + * The caller may freely use the vector registers until kernel_rvv_end() is + * called. + */ +void kernel_rvv_begin(void) +{ + if (WARN_ON(!has_vector())) + return; + + WARN_ON(!may_use_vector()); + + /* Acquire kernel mode vector */ + get_cpu_vector_context(); + + /* Save vector state, if any */ + vstate_save(current, task_pt_regs(current)); + + /* Enable vector */ + rvv_enable(); + + /* Invalidate vector regs */ + vector_flush_cpu_state(); +} +EXPORT_SYMBOL_GPL(kernel_rvv_begin); + +/* + * kernel_rvv_end(): give the CPU vector registers back to the current task + * + * Must be called from a context in which kernel_rvv_begin() was previously + * called, with no call to kernel_rvv_end() in the meantime. + * + * The caller must not use the vector registers after this function is called, + * unless kernel_rvv_begin() is called again in the meantime. + */ +void kernel_rvv_end(void) +{ + if (WARN_ON(!has_vector())) + return; + + /* Invalidate vector regs */ + vector_flush_cpu_state(); + + /* Restore vector state, if any */ + vstate_restore(current, task_pt_regs(current)); + + /* disable vector */ + rvv_disable(); + + /* release kernel mode vector */ + put_cpu_vector_context(); +} +EXPORT_SYMBOL_GPL(kernel_rvv_end); diff --git a/arch/riscv/kernel/vector.S b/arch/riscv/kernel/vector.S index 9f7dc70c4443..9c2de823c0d9 100644 --- a/arch/riscv/kernel/vector.S +++ b/arch/riscv/kernel/vector.S @@ -91,3 +91,12 @@ ENTRY(rvv_disable) csrc CSR_STATUS, status ret ENDPROC(rvv_disable) + +ENTRY(vector_flush_cpu_state) + vsetvli t0, x0, e8, m8, ta, ma + vmv.v.i v0, 0 + vmv.v.i v8, 0 + vmv.v.i v16, 0 + vmv.v.i v24, 0 + ret +ENDPROC(vector_flush_cpu_state) -- 2.25.1