Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp4182637ybl; Mon, 27 Jan 2020 18:32:07 -0800 (PST) X-Google-Smtp-Source: APXvYqwn5qR9s5+jajdW5dqtyxXa+jYoJYIh3U95f1Pp3/9YRkzmV4nqoO0UPNEBS+GY9SdQhGZn X-Received: by 2002:aca:cc87:: with SMTP id c129mr1437104oig.13.1580178726964; Mon, 27 Jan 2020 18:32:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580178726; cv=none; d=google.com; s=arc-20160816; b=hR+TXdHgGcLAybs1tBfzhEzefF2r86lcmjohbAqsj7+g4nh6WRaV2Ah27Ui17+S6XU rbQfKy6+1la40fUBqJ+/Wj1OKq3xNzdv6Ak/DRJ5eHSFEWFg+vu+5xjuhchjwPT+HEXR Uv4Bik2dCBPNK3Q7GJnhPg5CYY2sbiA9x+0PL/Xsf2rx4nHSjzTna971HW8M3yqLB6OR wIZjN9DpVHReJmX3pm+h1SZFLUQYasRMofzNmZ5VN0AHwI2S9Nz3ldiqJLcXiC5XOgLN A6GP/eyDm61eCMsnRqAW52BVfPrC6bR5Vk+0xfNWnfLynELU3ou7Ju7gQfVTK5fP6HUp Pzog== 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=1kS6VLkmiOPM6cC+EpqshJJj0lJWactZ6EyVR5osznw=; b=IJGQADPDlLFj9UeJI/mV4psmfRusIuRIlDkPr2PRKtdwlRSfPx0ELwCcPpC1DW1ao9 PL3cjOBJ1/ADoamM4utS6qMava0KQx0OQzEoSzKhMBkYUP6Vr5tUcxXUntHA+dUch/7O khbdbsNKnuiiM6rBLH3E8lTYCpZ4c18oR4sSCe04ijk6LeXEVmhHsEPaeDGUM3yowN0u TivT46djhxpQT29TY0Fl/manvEh+FTOfF41Qc8g1Ly4lnbTuQQ/V+j1EXYgpP8Nt5jHM WU1q6sx21FwFK+qfzse4eAfsPCVmrizw4ymX4sVgY58LNefCHo6eLdqfAqrC3TUDyt+s b67A== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@wdc.com header.s=dkim.wdc.com header.b=FUrdncPy; 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 l204si4425641oif.261.2020.01.27.18.31.51; Mon, 27 Jan 2020 18:32:06 -0800 (PST) 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=FUrdncPy; 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 S1728431AbgA1C23 (ORCPT + 99 others); Mon, 27 Jan 2020 21:28:29 -0500 Received: from esa3.hgst.iphmx.com ([216.71.153.141]:43171 "EHLO esa3.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726099AbgA1C2L (ORCPT ); Mon, 27 Jan 2020 21:28:11 -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=1580178491; x=1611714491; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=LjWNcnZ0HW3Mw0WB2X6tMvLNLFihL+mOheFwNsYdZCM=; b=FUrdncPyv3XDleJrmhXZvXV+zKGwmetTSYKAzRv5oRpb18tBaNu4E6LA wDCM/wxagRuhkF+/M/STKYbh3UjO87hzf9EIVTEZ28Otgj+sCJyeIqpGc 8kcIN9sDz3noMnYBVaSmEzEk4JN4Rqprq2+nItggrIXmaooMcyq4XmDnZ c7yQtZwmVM+vbmZnm+Q551Pqw9b7eL94h7SZ+hNmN330Zq0jN3xdcQrbt erl51ZDg/MCOO3GEiMYj45IPIGDBImZ8/BuunxGGgaOk6K3L/HhyOa0y8 zoYI8yKWk59IMt8prKx8F2QHfzqyjdCmC/JehM8gTnEdC33qB8AgwGUBT g==; IronPort-SDR: TjT3H6wXHbSQJNRnuwKQmKNNIJRC6OiDmvfijQvk4xBfSSz3OCKfRp6LfgKKKFQtXTEMJQmfGl NCOpKCFQ3YNBrCXZofWHnnmUJ4BpDwHwKY+fOdUVNm9CuQ6zcT7JknIqzhqdlxyAlCxTWmr3Xj gEtueo+gYV/1ph9adgCg3QKZberDLmKtQnaIcumDLaU7J/HLxIsmZJfAlTxwW7pzmu7VMfkPG/ pFYWZ2S6+VSA2cHDe29lJ0yq3tEkTF/KUnhG8ML1zeiVfwi6rKyteozWYh/BS+v6+KG6rfY0JI qlg= X-IronPort-AV: E=Sophos;i="5.70,372,1574092800"; d="scan'208";a="132899408" 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; 28 Jan 2020 10:28:10 +0800 IronPort-SDR: zaolefvoPNNAZ+4/et5haH1OgxvbSq6VQl/TTI1EKUlc+JnU6LDVqZkCIWxIqOn0e9GDVKuxdU o7ZLXOv1G1ZDoeINT0Mj9/3LZjqXfW+Dw+yIrs4Wi8slBM5UMVfnEZPEiCh+Y+B1vdfXotgE8q kcbaIC4XMYnHG+Nd2HpZCLdLEpRnnqZVnluOw1IucSbgRXDJ4bBhcf+0XhuN/F2P5LPOEncgL+ 578+kTj/rPcatzbNHrF6wEdogJv87yCoXI6NtEfIP/mtmLzJONA1vGDEG5sCqjbPtaZhayi9iL IEt+b9CIKkCBgUDiIvURs68G 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; 27 Jan 2020 18:21:25 -0800 IronPort-SDR: 2//zZrb9DrjXTVRNAgkL9F6hLc8fQP6LMnTUzfweOyBTaAB2DG0yvjJ78FF2sUCK7PfozWTwDT wy2H+H5I9/4tN7Z3GmexMFR8Cj5i9WUd1bPoUjKVLcsPZxH0UBO6msExy76/KQvFS8Dpp6v/6o +wl6WWQOt8xFnqTpuP0xC6xii8TRAcOa0me/W4stSaGXcQR+ztnGtAOrY7LbHb29VIaH8LDUfj O1VKXAReUbisIE1RUsHKT7ZUde1IRcKnGIj0A2HczSRUJW/p37XOkF/IOkhLfj9mnuSeUhC7yJ prY= WDCIronportException: Internal Received: from jedi-01.sdcorp.global.sandisk.com (HELO jedi-01.int.fusionio.com) ([10.11.143.218]) by uls-op-cesaip01.wdc.com with ESMTP; 27 Jan 2020 18:28:10 -0800 From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Albert Ou , Anup Patel , Borislav Petkov , "Eric W. Biederman" , Geert Uytterhoeven , Greentime Hu , Greg Kroah-Hartman , Heiko Carstens , Kees Cook , linux-riscv@lists.infradead.org, Mao Han , Mike Rapoport , Palmer Dabbelt , Paul Walmsley , Thomas Gleixner , Vincent Chen , abner.chang@hpe.com, clin@suse.com, nickhu@andestech.com, Palmer Dabbelt Subject: [PATCH v7 06/10] RISC-V: Add cpu_ops and modify default booting method Date: Mon, 27 Jan 2020 18:27:33 -0800 Message-Id: <20200128022737.15371-7-atish.patra@wdc.com> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20200128022737.15371-1-atish.patra@wdc.com> References: <20200128022737.15371-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 --- arch/riscv/include/asm/cpu_ops.h | 31 ++++++++++++++++ arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/cpu_ops.c | 61 ++++++++++++++++++++++++++++++++ arch/riscv/kernel/setup.c | 4 ++- arch/riscv/kernel/smpboot.c | 52 ++++++++++++++++----------- 5 files changed, 127 insertions(+), 22 deletions(-) create mode 100644 arch/riscv/include/asm/cpu_ops.h create mode 100644 arch/riscv/kernel/cpu_ops.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..27e9dfee5460 --- /dev/null +++ b/arch/riscv/include/asm/cpu_ops.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2019 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 + +/** + * 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]; +int __init cpu_set_ops(int cpu); + +#endif /* ifndef __ASM_CPU_OPS_H */ diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index f40205cb9a22..d77def5b4e87 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -32,6 +32,7 @@ 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_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..099dbb6ff9f0 --- /dev/null +++ b/arch/riscv/kernel/cpu_ops.c @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2019 Western Digital Corporation or its affiliates. + * + */ + +#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]; + +const struct cpu_operations cpu_spinwait_ops; + +static int spinwait_cpu_prepare(unsigned int cpuid) +{ + if (!cpu_spinwait_ops.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) +{ + int hartid = cpuid_to_hartid_map(cpuid); + + /* + * 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 __cpu_up_stack_pointer signals to + * the spinning cpus 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); + + return 0; +} + +const struct cpu_operations cpu_spinwait_ops = { + .name = "spinwait", + .cpu_prepare = spinwait_cpu_prepare, + .cpu_start = spinwait_cpu_start, +}; + +int __init cpu_set_ops(int cpuid) +{ + cpu_ops[cpuid] = &cpu_spinwait_ops; + return 0; +} diff --git a/arch/riscv/kernel/setup.c b/arch/riscv/kernel/setup.c index de3e65dae83a..8208d1109ddb 100644 --- a/arch/riscv/kernel/setup.c +++ b/arch/riscv/kernel/setup.c @@ -16,12 +16,13 @@ #include #include #include +#include #include +#include #include #include #include -#include #include #include #include @@ -79,6 +80,7 @@ void __init setup_arch(char **cmdline_p) sbi_init(); #ifdef CONFIG_SMP + cpu_set_ops(0); setup_smp(); #endif diff --git a/arch/riscv/kernel/smpboot.c b/arch/riscv/kernel/smpboot.c index 8bc01f0ca73b..f2cf541bc895 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); } } @@ -92,36 +97,41 @@ 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) { + if (cpu_set_ops(cpuid)) { + cpuid_to_hartid_map(cpuid) = INVALID_HARTID; + continue; + } 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.24.0