From mboxrd@z Thu Jan 1 00:00:00 1970 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 24E0CECAAD8 for ; Wed, 21 Sep 2022 21:52:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B755E94000B; Wed, 21 Sep 2022 17:52:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AFE6A940008; Wed, 21 Sep 2022 17:52:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9044A94000B; Wed, 21 Sep 2022 17:52:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 7A4D8940008 for ; Wed, 21 Sep 2022 17:52:51 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 50A83404C6 for ; Wed, 21 Sep 2022 21:52:51 +0000 (UTC) X-FDA: 79937442942.20.A0AE84D Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) by imf22.hostedemail.com (Postfix) with ESMTP id 0BAE4C000D for ; Wed, 21 Sep 2022 21:52:50 +0000 (UTC) Received: by mail-pl1-f180.google.com with SMTP id w20so6960706ply.12 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=2FY8B+rOIY2mKCvzyaAxLpTqLg4WE00hhpO/oeln7gw5cX/8IstvNpXRDkwwpCy6GW d+n/T29M5rD+rTdCxo7fli6Y+9WMbdcKkRUk8YDUs2dGvsMK9hu7KNHz27Tp0RiNmPxz e0hR6/w8KbZTlnpLOyYuD06AJ68QFmKixYlIIdMocaLrXmgr/GVM2GUTQ+BpWPwiyySu tgdhDm04gGWcXLMeDGiJ0Dck5QzQMnoFbe+esVmMb/zjklZQu89v8ntREtqOTl3t4yvl 1545dIxdu9xJz8KeJHI2G3/atwHKcGVxBz3nT9GDbETyTCeKTLDXUR+aJsOaopOlrDF4 cQtw== X-Gm-Message-State: ACrzQf3TXgESjPux/iEwIvCwacPbSS02BMVhJwvWu4BurtB+avnW8z1D HMkMoOp7IDpjLZD9FDoo5rWcQQ== X-Google-Smtp-Source: AMsMyM7FtJQyPVFcN5hAmKNQxQ5f0hiJdHy7x3MFQ7yEuNRelMTlBhku+aHb3OLsUtpA4xwmLlO6Vw== 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 To: 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 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663797171; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=9QztRJhDFYW9WZfYeW38b+1O0y8hGYDxe0zbOFpNx/M=; b=UfA0qxHt+OYADJeryribuVhjXMvE8fMifT6CquYpGc98DfdqgebIPdmRkIRNGh/kaF2hrx rj8P1YicPWu5PPELiRaJjBdr+n1ooS+WQOffzhX4+c08JabhJB19Bj0n5WFODHA1eFv4DT y1qyoJIa1Yti5TtZDXG5x2c77Empess= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=4FXEAyWn; spf=pass (imf22.hostedemail.com: domain of stillson@rivosinc.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=stillson@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663797171; a=rsa-sha256; cv=none; b=RpWtXcOGA4O0BpBnoaOgl308r6DnphnowbckCWQF4udEHwW+NV05fps+BTE4+NCjEAlvHJ 0L7hdtbzHLsWkyHg6g5WfPDSSXeh/EIOc1eXk9ZQqzcA7MJ8WLTOOrMTu7Ve4gw51zWM0t 83XW+j+JC4YEK/YFq6PqYEbRVmV51to= Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=4FXEAyWn; spf=pass (imf22.hostedemail.com: domain of stillson@rivosinc.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=stillson@rivosinc.com; dmarc=none X-Stat-Signature: ykc7uzt7yh9xg7bfjhc79fajeys9qfs3 X-Rspamd-Queue-Id: 0BAE4C000D X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1663797170-432136 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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