Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp900770pxk; Thu, 10 Sep 2020 01:19:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyRYMS2kYQEXAs6IjfnUFOs0V6EWtskqWbXKYVDjuIRqbB3WnIcpcmS+oEGZ6yD3hB4CB19 X-Received: by 2002:aa7:c387:: with SMTP id k7mr7852732edq.242.1599725992782; Thu, 10 Sep 2020 01:19:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599725992; cv=none; d=google.com; s=arc-20160816; b=wTJS1DG2hhsvolDI3wbfagP0y2k0Nc6mKYhU5aQdgI3TTSn6l5M2gN+jtTubfCSsDf W6L3Dj9rmZ+FsAufMixft5Te+MHnKjB5rSJpmDaSV7IYqCnObYjU9f7FUdlpWRVe6kP6 1D4EDgpqrtlPeVZG4tTTLfJ/XAn4s2AleQUQ9KVrfTSGSEEpg46wzNJNd42kdjHNNzG1 LM2ZIEJ+D5oG6Ng0mVLLJVsQEcNAQATerW73qQbSWi4V3EoB6pqBsKVrM4NQWnymOleQ JxDCIxUjdIP3UZIfgujWF0N6W+I+CCjgcYw+xsQ9FGVdr/gNnpbaLbzr2cvtPDjlxv6K JHAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=HxDqkyJEm7F+PhpjyX8QfZd9k5y4YbgtgWO8mXCoK44=; b=dFnGV/JjsZQ6Ou1b+Q6QJxjGLoynhtsSx+ay6CrZpiVtrZ55N4VtiD1PiqUg/RPFN3 fW/4BWHJC0RUohA9PXE7KrmKexQ/uge4Ci8LvfV47y+LRDHuHUs0RhriQHtHEmPHnMOC vw7LxP3ndRxwl4lUkItPfChacc8sw/WKI5M1qi5vfWM9glN5t54/AaV0PlMeQedsj/LI gLxuCSDab3oKtUjLpW6x2O1WReLrk5GR0e+6VaSUUXdYAlr7dUiOZCsOmML4gw22xWVU ZC3e15euBgOtp0j9Ro3Ueh0ItFskQl3dP8/U/bb9kkeVBPqb3rVo+eSIkuwp8b4aTaY2 h49w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=SDMQfIfm; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t5si3373614edt.527.2020.09.10.01.19.29; Thu, 10 Sep 2020 01:19:52 -0700 (PDT) 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=pass header.i=@sifive.com header.s=google header.b=SDMQfIfm; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729789AbgIJISi (ORCPT + 99 others); Thu, 10 Sep 2020 04:18:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729824AbgIJIND (ORCPT ); Thu, 10 Sep 2020 04:13:03 -0400 Received: from mail-pf1-x444.google.com (mail-pf1-x444.google.com [IPv6:2607:f8b0:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08826C061796 for ; Thu, 10 Sep 2020 01:12:57 -0700 (PDT) Received: by mail-pf1-x444.google.com with SMTP id x123so4182761pfc.7 for ; Thu, 10 Sep 2020 01:12:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=HxDqkyJEm7F+PhpjyX8QfZd9k5y4YbgtgWO8mXCoK44=; b=SDMQfIfmnO+BiMfaoVKX+1OmjRrxfCbnwFpMnM9nxhFAaHBvUwPiNUZ3uapoyTnOfM eTCt1dmg+R2zsHVH6jabU2FIbyUQm4uRqFgjHcBPacA8629DCumDX/w0FCdHvXzAXyAa l0+Qme/eUTnFOsloJefyNQyC3Hw01QIrjGGJ/qVoFnaSDIh4j2rhcvMoQ8Z/EXh97WCO T82WpTk2eSadWU6OhusKu1cChVGyPigb3/ISMG9WkcHrLxXQB2Mk1k1+p+x0AMwYiZ7p eMDGZKAfWNirC33jqzWAgvcpDJzWeHrFWWuBlcNVVIw0+CWl4Jc3PJTBr9PgUZQhzilb 12tg== 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:mime-version:content-transfer-encoding; bh=HxDqkyJEm7F+PhpjyX8QfZd9k5y4YbgtgWO8mXCoK44=; b=L9k2qoeTvwjjPAGiyHH+PpRmGFExWQq4dneBq5F2BaeyqgfEFofjfq2Pj2t3SryAw6 bitTCp1PKdERNc8bFGmM45U1XQD3l662YrUBcg9uSGWMURxbaJXNALtcf5ea1wJ0kWqQ 8pZfqAkTATnwkd2h6l0Ln4pUp9rdcbl6IOCBD3XHv1LvVXBfbfY8TxPBDjG+9kna3Kgp XLWmWukFzbB8uV2eXlHDqzG7kioFQgOzTqpCNKJJJ+cHX9CJY9XC128LVKuPH6VkAQdO vzGEBtN/iYU3+DyjBGbWVrxnACu3xMrlB6Szcodx1UE6RRzv7+sV9sXLngZ8bHxiVxwN hhpA== X-Gm-Message-State: AOAM531uE1HfciVJ4W90o0LOxk97CeEWc5+9+9Btadi+63eGy/JMrNgJ lWSHQZTkUG8RzxBbMEsfBsx5JExHUefB/dSu X-Received: by 2002:a63:e504:: with SMTP id r4mr3644529pgh.431.1599725575588; Thu, 10 Sep 2020 01:12:55 -0700 (PDT) Received: from hsinchu02.internal.sifive.com (114-34-229-221.HINET-IP.hinet.net. [114.34.229.221]) by smtp.gmail.com with ESMTPSA id e1sm2196056pfl.162.2020.09.10.01.12.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Sep 2020 01:12:55 -0700 (PDT) From: Greentime Hu To: greentime.hu@sifive.com, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, aou@eecs.berkeley.edu, palmer@dabbelt.com, paul.walmsley@sifive.com Cc: Nick Knight , Guo Ren , Vincent Chen Subject: [RFC PATCH v7 09/21] riscv: Add task switch support for vector Date: Thu, 10 Sep 2020 16:12:04 +0800 Message-Id: <0cade5900d72628cbf6a4a021858f000924d560b.1599719352.git.greentime.hu@sifive.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch adds task switch support for vector. It supports partial lazy save and restore mechanism. It also supports all lengths of vlen. [guoren@linux.alibaba.com: First available porting to support vector context switching] [nick.knight@sifive.com: Rewrite vector.S to support dynamic vlen, xlen and code refine] [vincent.chen@sifive.co: Fix the might_sleep issue in vstate_save, vstate_restore] Signed-off-by: Nick Knight Signed-off-by: Greentime Hu Signed-off-by: Guo Ren Signed-off-by: Vincent Chen --- arch/riscv/include/asm/switch_to.h | 65 +++++++++++++++++++++++ arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/process.c | 40 ++++++++++++++ arch/riscv/kernel/vector.S | 84 ++++++++++++++++++++++++++++++ 4 files changed, 190 insertions(+) create mode 100644 arch/riscv/kernel/vector.S diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h index b9234e7178d0..2afd0124701a 100644 --- a/arch/riscv/include/asm/switch_to.h +++ b/arch/riscv/include/asm/switch_to.h @@ -6,10 +6,12 @@ #ifndef _ASM_RISCV_SWITCH_TO_H #define _ASM_RISCV_SWITCH_TO_H +#include #include #include #include #include +#include #ifdef CONFIG_FPU extern void __fstate_save(struct task_struct *save_to); @@ -63,6 +65,67 @@ extern bool has_fpu; #define __switch_to_fpu(__prev, __next) do { } while (0) #endif +#ifdef CONFIG_VECTOR +extern bool has_vector; +extern unsigned long riscv_vsize; +extern void __vstate_save(struct __riscv_v_state *save_to, void *datap); +extern void __vstate_restore(struct __riscv_v_state *restore_from, void *datap); + +static inline void __vstate_clean(struct pt_regs *regs) +{ + regs->status = (regs->status & ~(SR_VS)) | SR_VS_CLEAN; +} + +static inline void vstate_off(struct task_struct *task, + struct pt_regs *regs) +{ + regs->status = (regs->status & ~SR_VS) | SR_VS_OFF; +} + +static inline void vstate_save(struct task_struct *task, + struct pt_regs *regs) +{ + if ((regs->status & SR_VS) == SR_VS_DIRTY) { + struct __riscv_v_state *vstate = &(task->thread.vstate); + __vstate_save(vstate, vstate->datap); + __vstate_clean(regs); + } +} + +static inline void vstate_restore(struct task_struct *task, + struct pt_regs *regs) +{ + if ((regs->status & SR_VS) != SR_VS_OFF) { + struct __riscv_v_state *vstate = &(task->thread.vstate); + + /* Allocate space for vector registers. */ + if (!vstate->datap) { + vstate->datap = kzalloc(riscv_vsize, GFP_ATOMIC); + vstate->size = riscv_vsize; + } + __vstate_restore(vstate, vstate->datap); + __vstate_clean(regs); + } +} + +static inline void __switch_to_vector(struct task_struct *prev, + struct task_struct *next) +{ + struct pt_regs *regs; + + regs = task_pt_regs(prev); + if (unlikely(regs->status & SR_SD)) + vstate_save(prev, regs); + vstate_restore(next, task_pt_regs(next)); +} + +#else +#define has_vector false +#define vstate_save(task, regs) do { } while (0) +#define vstate_restore(task, regs) do { } while (0) +#define __switch_to_vector(__prev, __next) do { } while (0) +#endif + extern struct task_struct *__switch_to(struct task_struct *, struct task_struct *); @@ -72,6 +135,8 @@ do { \ struct task_struct *__next = (next); \ if (has_fpu) \ __switch_to_fpu(__prev, __next); \ + if (has_vector) \ + __switch_to_vector(__prev, __next); \ ((last) = __switch_to(__prev, __next)); \ } while (0) diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index dc93710f0b2f..3a166c21ea49 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -33,6 +33,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_SMP) += smpboot.o obj-$(CONFIG_SMP) += smp.o obj-$(CONFIG_SMP) += cpu_ops.o diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index 2b97c493427c..fb485c9bceee 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -82,6 +82,16 @@ void start_thread(struct pt_regs *regs, unsigned long pc, */ fstate_restore(current, regs); } + + if (has_vector) { + regs->status |= SR_VS_INITIAL; + /* + * Restore the initial value to the vector register + * before starting the user program. + */ + vstate_restore(current, regs); + } + regs->epc = pc; regs->sp = sp; set_fs(USER_DS); @@ -98,15 +108,45 @@ void flush_thread(void) fstate_off(current, task_pt_regs(current)); memset(¤t->thread.fstate, 0, sizeof(current->thread.fstate)); #endif +#ifdef CONFIG_VECTOR + /* Reset vector state */ + vstate_off(current, task_pt_regs(current)); + memset(¤t->thread.vstate, 0, sizeof(current->thread.vstate)); +#endif } int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) { fstate_save(src, task_pt_regs(src)); + if (has_vector) + /* To make sure every dirty vector context is saved. */ + vstate_save(src, task_pt_regs(src)); *dst = *src; + if (has_vector) { + /* Copy vector context to the forked task from parent. */ + if ((task_pt_regs(src)->status & SR_VS) != SR_VS_OFF) { + unsigned long size = src->thread.vstate.size; + + dst->thread.vstate.datap = kzalloc(size, GFP_KERNEL); + /* Failed to allocate memory. */ + if (!dst->thread.vstate.datap) + return -ENOMEM; + /* Copy the src vector context to dst. */ + memcpy(dst->thread.vstate.datap, + src->thread.vstate.datap, size); + } + } + return 0; } +void arch_release_task_struct(struct task_struct *tsk) +{ + /* Free the vector context of datap. */ + if (has_vector) + kfree(tsk->thread.vstate.datap); +} + int copy_thread(unsigned long clone_flags, unsigned long usp, unsigned long arg, struct task_struct *p, unsigned long tls) { diff --git a/arch/riscv/kernel/vector.S b/arch/riscv/kernel/vector.S new file mode 100644 index 000000000000..4c880b1c32aa --- /dev/null +++ b/arch/riscv/kernel/vector.S @@ -0,0 +1,84 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2012 Regents of the University of California + * Copyright (C) 2017 SiFive + * Copyright (C) 2019 Alibaba Group Holding Limited + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, version 2. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include + +#include +#include +#include + +#define vstatep a0 +#define datap a1 +#define x_vstart t0 +#define x_vtype t1 +#define x_vl t2 +#define x_vcsr t3 +#define incr t4 +#define m_one t5 +#define status t6 + +ENTRY(__vstate_save) + li status, SR_VS + csrs sstatus, status + + csrr x_vstart, CSR_VSTART + csrr x_vtype, CSR_VTYPE + csrr x_vl, CSR_VL + csrr x_vcsr, CSR_VCSR + li m_one, -1 + vsetvli incr, m_one, e8, m8 + vse8.v v0, (datap) + add datap, datap, incr + vse8.v v8, (datap) + add datap, datap, incr + vse8.v v16, (datap) + add datap, datap, incr + vse8.v v24, (datap) + + REG_S x_vstart, RISCV_V_STATE_VSTART(vstatep) + REG_S x_vtype, RISCV_V_STATE_VTYPE(vstatep) + REG_S x_vl, RISCV_V_STATE_VL(vstatep) + REG_S x_vcsr, RISCV_V_STATE_VCSR(vstatep) + + csrc sstatus, status + ret +ENDPROC(__vstate_save) + +ENTRY(__vstate_restore) + li status, SR_VS + csrs sstatus, status + + li m_one, -1 + vsetvli incr, m_one, e8, m8 + vle8.v v0, (datap) + add datap, datap, incr + vle8.v v8, (datap) + add datap, datap, incr + vle8.v v16, (datap) + add datap, datap, incr + vle8.v v24, (datap) + + REG_L x_vstart, RISCV_V_STATE_VSTART(vstatep) + REG_L x_vtype, RISCV_V_STATE_VTYPE(vstatep) + REG_L x_vl, RISCV_V_STATE_VL(vstatep) + REG_L x_vcsr, RISCV_V_STATE_VCSR(vstatep) + vsetvl x0, x_vl, x_vtype + csrw CSR_VSTART, x_vstart + csrw CSR_VCSR, x_vcsr + + csrc sstatus, status + ret +ENDPROC(__vstate_restore) -- 2.28.0