From patchwork Tue Jan 28 02:27:33 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 11353495 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 80B781398 for ; Tue, 28 Jan 2020 02:28:42 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5005524686 for ; Tue, 28 Jan 2020 02:28:42 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="mMFJBkFZ"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=wdc.com header.i=@wdc.com header.b="lS+Y5DqR" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5005524686 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=wdc.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-riscv-bounces+patchwork-linux-riscv=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id: Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id:Date :Subject:To:From:Reply-To:Content-Type:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=1kS6VLkmiOPM6cC+EpqshJJj0lJWactZ6EyVR5osznw=; b=mMFJBkFZI6UvJx 92x12EiPr4KRDEDo0Wl4jcCIrxiszqMOX7yEGUeLixy1GxoOW/gdIU8LIcIG42fxHAbpDiReAaz3r gEhx8HNMAaKFRDGfdGC9Plx9ZMLI7YtOg/anSwMRdR71RKYFfwhG8cNLYzMPLFA0i1PGhB6WV1yCL de1SgJlVQ/b7NGcYDIykD4/m+VUXscM2krRafD969upm4nGGoS8LucZ2+ieJWxkw7rBLRrrqbxQB5 Z3eHbZUjvgY3LLa+ImVCMUw7E348Bgeb0luPI1ZuKQxqj+z0MfenRQtWryADSUbN2oJSjYqy3b5S8 RAl4/flTvZFr08Zo5DgQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1iwGcH-0006KG-Vc; Tue, 28 Jan 2020 02:28:38 +0000 Received: from esa2.hgst.iphmx.com ([68.232.143.124]) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1iwGbt-0005qJ-Gv for linux-riscv@lists.infradead.org; Tue, 28 Jan 2020 02:28:15 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1580178515; x=1611714515; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=LjWNcnZ0HW3Mw0WB2X6tMvLNLFihL+mOheFwNsYdZCM=; b=lS+Y5DqRijPVvQAYUxMppgKmjvEAmnrx3vejQtiHjdnGlgsw9bOYyoSf OUARDVLjVwIqAfNBWi4S7T6LAorPH5XWL7YAPfHuGu8tBniW+7QceqFq9 8oL9V3nsWtZuzCOnYflysrBXgA4nQytfIroislYn9ZR8cBmZe1IWTS8Ld oozDqygrrrziL7UdQxbdVKLUBA77GnvdeHbSwx+um+OZb6xYShjkw1kma hBCru85uEjdEjaSuZjJ9k+KQTi71jxFliXbG9dEVslDwQYg2C1j0a8pD2 Dpdq/Qgwc6WMEqxbxOBqRHjaey0wZ4APKkWIAR2rQjO+HAI22dPA1jPFB A==; IronPort-SDR: JhyNQu2Hmk2xpXKBf+4Xbb5dbC4CWEOaPa9vAIetaAdBISu/WLvVR3l05OK9+FmzvhMypc8eUm W1AV5vbaiFjRj5hYN+Y8SWqwwU9qOtAUta0/Ov3x2L1P5X9RjpYumZhgFXaNqWaMtR2R9PsrwU aG1TLdkScnm/6sWBccWKrxTLi8Cfn3glDCYC8aeAXDJkQHGIPerIoygg6wFEtBLWFBHqC6JcOW iVE2giOKOJ96OUTgECY2mrmOMbtFoVxRI5MYQez2+pzN/RtEUwhlnZH6VVt7Mf94IDYgnt2Afa Cmg= X-IronPort-AV: E=Sophos;i="5.70,372,1574092800"; d="scan'208";a="230237596" 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:30 +0800 IronPort-SDR: UI2C9vZq343uemvoNstHC3eByxCLbYb89qfepA603a5qbm7Tf99GIhZg3uO0E9K4xqw9fPAs5N ZBf04l3fL2ut/ZqpjZrHmpDbFWHbdagemdwpNrNiXPsKlltPI3lezgyWC0el5Me8MFwT5OIyQP k0QcRGC83plZmoUpbUriTcVgiWaHukSmuks+HU4UWD7tMGyo/HiedpN3f8PoICMW3i1IFE/XCY KmB6bYGnynZoIDipUmzm+Jp4CaMlEuWGvvzZ3ho4rKauSrno5Ynu59mu1BxYCSPV7OPoVun/yW 85UkVXxK1A6v1hn0hNhvXzSh 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 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200127_182813_636267_3F784B0D X-CRM114-Status: GOOD ( 22.62 ) X-Spam-Score: -2.5 (--) X-Spam-Report: SpamAssassin version 3.4.3 on bombadil.infradead.org summary: Content analysis details: (-2.5 points) pts rule name description ---- ---------------------- -------------------------------------------------- -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at https://www.dnswl.org/, medium trust [68.232.143.124 listed in list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Albert Ou , Thomas Gleixner , Kees Cook , abner.chang@hpe.com, Vincent Chen , nickhu@andestech.com, Anup Patel , Paul Walmsley , Heiko Carstens , Mike Rapoport , clin@suse.com, Atish Patra , Geert Uytterhoeven , "Eric W. Biederman" , Greg Kroah-Hartman , Palmer Dabbelt , Greentime Hu , linux-riscv@lists.infradead.org, Borislav Petkov , Palmer Dabbelt , Mao Han Sender: "linux-riscv" Errors-To: linux-riscv-bounces+patchwork-linux-riscv=patchwork.kernel.org@lists.infradead.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;