Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp65445ybh; Tue, 17 Mar 2020 18:14:06 -0700 (PDT) X-Google-Smtp-Source: ADFU+vsXidpOeBHqHojQyEJ/2gdH+I/f8i174fixhMbKzSpu5run8OaH/rkCJh8V6MMRGriV1ut/ X-Received: by 2002:a9d:4001:: with SMTP id m1mr1765948ote.57.1584494045908; Tue, 17 Mar 2020 18:14:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584494045; cv=none; d=google.com; s=arc-20160816; b=QVbyVirSSD2nvctiuqV5ghqLg24dpS8F1ky1TLAQVDynTKf1V2qD5lJT1ZA03y454m cuJcsf/tBzEG/vhVc3Vx0lAk5K9M2f6xirQP+wMPXVp8T8bXE2OflocW5qByTQiNNwkS Qv/Yl/LA/1zHm8k0japM3Skent1pSql10iLDgqlYGpbkrSQslFDOR9D8UR4dwCGf4ATr 16bFiL5UCJkCc0KaKqy4sggwVZF93gRLMit+AY2F7vqElWUEafKyzilrS8RyhQDynF7j Ziq9aU2ZKATO8BvFIXGYJLLizUT/8OqZNXLTcvdDT8Hf8iHvCDl0k32HgqhTcues4XFp Ppyg== 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 :wdcironportexception:ironport-sdr:ironport-sdr:ironport-sdr :dkim-signature; bh=+OwMmriRxzv8p3oJtfnGjETT/R2J5XzN9lxx7jWHSWE=; b=qfhEDdY1R4igv22jEQKIeHfmcrsJ3IMzLfd7sIJE3s3dN98xShCTD/SqRjIj6Vb/jg mzzR47LBwKxgHb3dHBTM7WZqBsIcjfeDlvj95CwvQyG6ziIIBK2rEk3NPFtniUzrYqL9 FjjVBFhKIKUBG+nS/QK5/cIsbUhKss/ARuwQBxoLUByZWwzu14PvkQnQOSG3Urbrgy/h DpV+p1LSVfNa3kii1vMDxOevkNo8VWSppn+xrxvWhh4jBaUeOhOJwm1qYst1TXRTKHA0 P18PFe5smHKddMJwVoBRF5DRmZi6eKdc1jR0BoEj0AHQWDxkYJPo+WaqLElHxdgRKIqK 6z1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@wdc.com header.s=dkim.wdc.com header.b="rsYPuQ/Q"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id k24si3126999otf.213.2020.03.17.18.13.53; Tue, 17 Mar 2020 18:14:05 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@wdc.com header.s=dkim.wdc.com header.b="rsYPuQ/Q"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1727471AbgCRBMH (ORCPT + 99 others); Tue, 17 Mar 2020 21:12:07 -0400 Received: from esa5.hgst.iphmx.com ([216.71.153.144]:45924 "EHLO esa5.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727113AbgCRBMB (ORCPT ); Tue, 17 Mar 2020 21:12:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1584493921; x=1616029921; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=tPKIUvPrRM0YTzXmXxBH/vwESIM3PLnHx7sHtToL8Cc=; b=rsYPuQ/Qp8qQDy5AtHXhxKHsdp8rUvmjzuLlGK0w4Gi1qVGgLpgdp6+O b9fGMjbwz2Z9+nzD2PASf1EEStA8wgvC5Iy3fwPMJI47KBsqPtBPsw+iH KC0B4tNAO/ZRxOTIk+WbiaUdNPP8bI2BFEgFe2g5gEv2pjIYt84IifYXi HH7CvftH8ElXZ3+wK20d6pbRqcTSR3wI6nZqyhYWJRX/wsHaOUjeQ8omc AIdpAzNi6FEW59yru5ZCcNeEACK6jXKl5WobL/IX8D7YVJ9jPGbMbhWr1 Qj0BRjmcy0SV44KdNITKAZvbtdpWWjJFKdj4MXzFPWniLByzkRB2hILtE g==; IronPort-SDR: VgkleQER95BEM+t0UlRjfezGT9uAA/2Nay+cr6MPAsdXqLheEyT7AdhWykcTHWJyp7reFp1tIU vhvilm93znDOgiDobhcA9oGqUNvbvRnKShq9VuDifYvZ12MidWRgMtVuutRUYiSYCkFfWpZLQs R8RtTZQMPhtwCz1kntvq3GyuWMCCbgbXE18XPIXAZJILJa4brMkYT6sd14QGeD9ThkdXWfp1uW HaZi+AYXDaYioZYu39pB03zfFNXGfvXeRbhTELcUco3hci+53DVDfjxY1IUaQxzwefI9ZFMtKY 9ao= X-IronPort-AV: E=Sophos;i="5.70,565,1574092800"; d="scan'208";a="133241520" Received: from h199-255-45-14.hgst.com (HELO uls-op-cesaep01.wdc.com) ([199.255.45.14]) by ob1.hgst.iphmx.com with ESMTP; 18 Mar 2020 09:12:00 +0800 IronPort-SDR: ZbJdEhyWNbDuLGvFh2BdO0U3jxvyc6OOG3FUFtwe5JtcxE/ORi7U/kekBe2+6urf7rgtGw9CVL u7XUI89LSJpPo9c6GhFAQ0HqU4xRjj1VslcT45KtnOek0ABJQ6ZDQW/X6QmNGLtqzmMFIneMnf 8l2uK4swXh78bWv8F8RZlCa206LbcMmUdkVznhM9V1qB7cD2Np84u0ko2xlTNsZJjALkMBqaZj hcOn33FwGjKuRF9aX8+VqY1D+emnOdTmaPME004aR46KSu9vIu29eus2Z/t1psFIlRDfA4oWlJ bGNDV8YRjSPTZ60JGAqCITdx Received: from uls-op-cesaip01.wdc.com ([10.248.3.36]) by uls-op-cesaep01.wdc.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Mar 2020 18:03:50 -0700 IronPort-SDR: IDO1oItmItfkBs4ru2vcCxSddF4r2S6P2maOTamJKutS0wJO7Pe2HyHNAfsD3M9p45CvS77/xJ 8yfHbU7nrhjy8n/VxNi3r85WrxrN+nNCn2j1l/ppP4Ti68Clch3Wb0SIXtST2yAiwZStfR5P4b aErFmbvFerD8zo255+0XjiieEl2uJTaOlSwfcrMwdJDYzN/RnaKcdaOjfV7pQyHDD8aI4KsPI6 JCmc2TFbB67u1c+2G95imrBQKtl6vfTjH+oa3XOyt2KRp4IfSfNB3PIEcRiiEHdTy7PPOmBgzX qGM= WDCIronportException: Internal Received: from mccorma-lt.ad.shared (HELO yoda.hgst.com) ([10.86.54.125]) by uls-op-cesaip01.wdc.com with ESMTP; 17 Mar 2020 18:11:59 -0700 From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Anup Patel , Albert Ou , Gary Guo , Greentime Hu , linux-riscv@lists.infradead.org, Mao Han , Mike Rapoport , Nick Hu , Palmer Dabbelt , Paul Walmsley , Thomas Gleixner , Vincent Chen , Zong Li , Bin Meng Subject: [PATCH v11 07/11] RISC-V: Add cpu_ops and modify default booting method Date: Tue, 17 Mar 2020 18:11:40 -0700 Message-Id: <20200318011144.91532-8-atish.patra@wdc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200318011144.91532-1-atish.patra@wdc.com> References: <20200318011144.91532-1-atish.patra@wdc.com> 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 Currently, all non-booting harts start booting after the booting hart updates the per-hart stack pointer. This is done in a way that, it's difficult to implement any other booting method without breaking the backward compatibility. Define a cpu_ops method that allows to introduce other booting methods in future. Modify the current booting method to be compatible with cpu_ops. Signed-off-by: Atish Patra Reviewed-by: Anup Patel --- arch/riscv/include/asm/cpu_ops.h | 34 +++++++++++++++++++ arch/riscv/kernel/Makefile | 2 ++ arch/riscv/kernel/cpu_ops.c | 38 +++++++++++++++++++++ arch/riscv/kernel/cpu_ops_spinwait.c | 43 +++++++++++++++++++++++ arch/riscv/kernel/smpboot.c | 51 ++++++++++++++++------------ 5 files changed, 147 insertions(+), 21 deletions(-) create mode 100644 arch/riscv/include/asm/cpu_ops.h create mode 100644 arch/riscv/kernel/cpu_ops.c create mode 100644 arch/riscv/kernel/cpu_ops_spinwait.c diff --git a/arch/riscv/include/asm/cpu_ops.h b/arch/riscv/include/asm/cpu_ops.h new file mode 100644 index 000000000000..5ce81a28e1d9 --- /dev/null +++ b/arch/riscv/include/asm/cpu_ops.h @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2020 Western Digital Corporation or its affiliates. + * Based on arch/arm64/include/asm/cpu_ops.h + */ +#ifndef __ASM_CPU_OPS_H +#define __ASM_CPU_OPS_H + +#include +#include +#include + +/** + * struct cpu_operations - Callback operations for hotplugging CPUs. + * + * @name: Name of the boot protocol. + * @cpu_prepare: Early one-time preparation step for a cpu. If there + * is a mechanism for doing so, tests whether it is + * possible to boot the given HART. + * @cpu_start: Boots a cpu into the kernel. + */ +struct cpu_operations { + const char *name; + int (*cpu_prepare)(unsigned int cpu); + int (*cpu_start)(unsigned int cpu, + struct task_struct *tidle); +}; + +extern const struct cpu_operations *cpu_ops[NR_CPUS]; +void __init cpu_set_ops(int cpu); +void cpu_update_secondary_bootdata(unsigned int cpuid, + struct task_struct *tidle); + +#endif /* ifndef __ASM_CPU_OPS_H */ diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index f40205cb9a22..f81a6ff88005 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -32,6 +32,8 @@ obj-$(CONFIG_RISCV_M_MODE) += clint.o obj-$(CONFIG_FPU) += fpu.o obj-$(CONFIG_SMP) += smpboot.o obj-$(CONFIG_SMP) += smp.o +obj-$(CONFIG_SMP) += cpu_ops.o +obj-$(CONFIG_SMP) += cpu_ops_spinwait.o obj-$(CONFIG_MODULES) += module.o obj-$(CONFIG_MODULE_SECTIONS) += module-sections.o diff --git a/arch/riscv/kernel/cpu_ops.c b/arch/riscv/kernel/cpu_ops.c new file mode 100644 index 000000000000..62705908eee5 --- /dev/null +++ b/arch/riscv/kernel/cpu_ops.c @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2020 Western Digital Corporation or its affiliates. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +const struct cpu_operations *cpu_ops[NR_CPUS] __ro_after_init; + +void *__cpu_up_stack_pointer[NR_CPUS]; +void *__cpu_up_task_pointer[NR_CPUS]; + +extern const struct cpu_operations cpu_ops_spinwait; + +void cpu_update_secondary_bootdata(unsigned int cpuid, + struct task_struct *tidle) +{ + int hartid = cpuid_to_hartid_map(cpuid); + + /* Make sure tidle is updated */ + smp_mb(); + WRITE_ONCE(__cpu_up_stack_pointer[hartid], + task_stack_page(tidle) + THREAD_SIZE); + WRITE_ONCE(__cpu_up_task_pointer[hartid], tidle); +} + +void __init cpu_set_ops(int cpuid) +{ + cpu_ops[cpuid] = &cpu_ops_spinwait; +} diff --git a/arch/riscv/kernel/cpu_ops_spinwait.c b/arch/riscv/kernel/cpu_ops_spinwait.c new file mode 100644 index 000000000000..b2c957bb68c1 --- /dev/null +++ b/arch/riscv/kernel/cpu_ops_spinwait.c @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2020 Western Digital Corporation or its affiliates. + */ + +#include +#include +#include +#include +#include +#include + +const struct cpu_operations cpu_ops_spinwait; + +static int spinwait_cpu_prepare(unsigned int cpuid) +{ + if (!cpu_ops_spinwait.cpu_start) { + pr_err("cpu start method not defined for CPU [%d]\n", cpuid); + return -ENODEV; + } + return 0; +} + +static int spinwait_cpu_start(unsigned int cpuid, struct task_struct *tidle) +{ + /* + * In this protocol, all cpus boot on their own accord. _start + * selects the first cpu to boot the kernel and causes the remainder + * of the cpus to spin in a loop waiting for their stack pointer to be + * setup by that main cpu. Writing to bootdata + * (i.e __cpu_up_stack_pointer) signals to the spinning cpus that they + * can continue the boot process. + */ + cpu_update_secondary_bootdata(cpuid, tidle); + + return 0; +} + +const struct cpu_operations cpu_ops_spinwait = { + .name = "spinwait", + .cpu_prepare = spinwait_cpu_prepare, + .cpu_start = spinwait_cpu_start, +}; diff --git a/arch/riscv/kernel/smpboot.c b/arch/riscv/kernel/smpboot.c index 8bc01f0ca73b..e89396a2a1af 100644 --- a/arch/riscv/kernel/smpboot.c +++ b/arch/riscv/kernel/smpboot.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -34,8 +35,6 @@ #include "head.h" -void *__cpu_up_stack_pointer[NR_CPUS]; -void *__cpu_up_task_pointer[NR_CPUS]; static DECLARE_COMPLETION(cpu_running); void __init smp_prepare_boot_cpu(void) @@ -46,6 +45,7 @@ void __init smp_prepare_boot_cpu(void) void __init smp_prepare_cpus(unsigned int max_cpus) { int cpuid; + int ret; /* This covers non-smp usecase mandated by "nosmp" option */ if (max_cpus == 0) @@ -54,6 +54,11 @@ void __init smp_prepare_cpus(unsigned int max_cpus) for_each_possible_cpu(cpuid) { if (cpuid == smp_processor_id()) continue; + if (cpu_ops[cpuid]->cpu_prepare) { + ret = cpu_ops[cpuid]->cpu_prepare(cpuid); + if (ret) + continue; + } set_cpu_present(cpuid, true); } } @@ -65,6 +70,8 @@ void __init setup_smp(void) bool found_boot_cpu = false; int cpuid = 1; + cpu_set_ops(0); + for_each_of_cpu_node(dn) { hart = riscv_of_processor_hartid(dn); if (hart < 0) @@ -92,36 +99,38 @@ void __init setup_smp(void) cpuid, nr_cpu_ids); for (cpuid = 1; cpuid < nr_cpu_ids; cpuid++) { - if (cpuid_to_hartid_map(cpuid) != INVALID_HARTID) + if (cpuid_to_hartid_map(cpuid) != INVALID_HARTID) { + cpu_set_ops(cpuid); set_cpu_possible(cpuid, true); + } } } +int start_secondary_cpu(int cpu, struct task_struct *tidle) +{ + if (cpu_ops[cpu]->cpu_start) + return cpu_ops[cpu]->cpu_start(cpu, tidle); + + return -EOPNOTSUPP; +} + int __cpu_up(unsigned int cpu, struct task_struct *tidle) { int ret = 0; - int hartid = cpuid_to_hartid_map(cpu); tidle->thread_info.cpu = cpu; - /* - * On RISC-V systems, all harts boot on their own accord. Our _start - * selects the first hart to boot the kernel and causes the remainder - * of the harts to spin in a loop waiting for their stack pointer to be - * setup by that main hart. Writing __cpu_up_stack_pointer signals to - * the spinning harts that they can continue the boot process. - */ - smp_mb(); - WRITE_ONCE(__cpu_up_stack_pointer[hartid], - task_stack_page(tidle) + THREAD_SIZE); - WRITE_ONCE(__cpu_up_task_pointer[hartid], tidle); - - lockdep_assert_held(&cpu_running); - wait_for_completion_timeout(&cpu_running, + ret = start_secondary_cpu(cpu, tidle); + if (!ret) { + lockdep_assert_held(&cpu_running); + wait_for_completion_timeout(&cpu_running, msecs_to_jiffies(1000)); - if (!cpu_online(cpu)) { - pr_crit("CPU%u: failed to come online\n", cpu); - ret = -EIO; + if (!cpu_online(cpu)) { + pr_crit("CPU%u: failed to come online\n", cpu); + ret = -EIO; + } + } else { + pr_crit("CPU%u: failed to start\n", cpu); } return ret; -- 2.25.1