2018-12-11 21:54:06 +08:00
|
|
|
/*
|
2019-01-24 14:11:10 +08:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019 Western Digital Corporation or its affiliates.
|
2018-12-11 21:54:06 +08:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anup Patel <anup.patel@wdc.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sbi/riscv_asm.h>
|
|
|
|
#include <sbi/riscv_atomic.h>
|
2020-03-02 19:20:01 +08:00
|
|
|
#include <sbi/riscv_locks.h>
|
2018-12-11 21:54:06 +08:00
|
|
|
#include <sbi/sbi_console.h>
|
|
|
|
#include <sbi/sbi_ecall.h>
|
|
|
|
#include <sbi/sbi_hart.h>
|
2020-03-04 17:00:49 +08:00
|
|
|
#include <sbi/sbi_hartmask.h>
|
2020-02-12 09:32:37 +08:00
|
|
|
#include <sbi/sbi_hsm.h>
|
2018-12-11 21:54:06 +08:00
|
|
|
#include <sbi/sbi_ipi.h>
|
|
|
|
#include <sbi/sbi_platform.h>
|
|
|
|
#include <sbi/sbi_system.h>
|
2020-05-10 07:47:31 +08:00
|
|
|
#include <sbi/sbi_string.h>
|
2018-12-11 21:54:06 +08:00
|
|
|
#include <sbi/sbi_timer.h>
|
2020-01-15 16:01:12 +08:00
|
|
|
#include <sbi/sbi_tlb.h>
|
2019-01-25 13:49:22 +08:00
|
|
|
#include <sbi/sbi_version.h>
|
2018-12-11 21:54:06 +08:00
|
|
|
|
2019-04-11 08:41:52 +08:00
|
|
|
#define BANNER \
|
|
|
|
" ____ _____ ____ _____\n" \
|
|
|
|
" / __ \\ / ____| _ \\_ _|\n" \
|
|
|
|
" | | | |_ __ ___ _ __ | (___ | |_) || |\n" \
|
2019-01-18 12:18:25 +08:00
|
|
|
" | | | | '_ \\ / _ \\ '_ \\ \\___ \\| _ < | |\n" \
|
2019-04-11 08:41:52 +08:00
|
|
|
" | |__| | |_) | __/ | | |____) | |_) || |_\n" \
|
|
|
|
" \\____/| .__/ \\___|_| |_|_____/|____/_____|\n" \
|
|
|
|
" | |\n" \
|
2019-01-18 12:18:25 +08:00
|
|
|
" |_|\n\n"
|
2018-12-21 13:58:23 +08:00
|
|
|
|
2019-03-27 22:39:08 +08:00
|
|
|
static void sbi_boot_prints(struct sbi_scratch *scratch, u32 hartid)
|
|
|
|
{
|
2020-05-10 14:06:14 +08:00
|
|
|
int xlen;
|
|
|
|
char str[128];
|
2019-03-27 22:39:08 +08:00
|
|
|
const struct sbi_platform *plat = sbi_platform_ptr(scratch);
|
|
|
|
|
2019-08-17 22:11:09 +08:00
|
|
|
#ifdef OPENSBI_VERSION_GIT
|
2019-11-12 08:40:34 +08:00
|
|
|
sbi_printf("\nOpenSBI %s\n", OPENSBI_VERSION_GIT);
|
2019-08-17 22:11:09 +08:00
|
|
|
#else
|
2019-11-12 08:40:34 +08:00
|
|
|
sbi_printf("\nOpenSBI v%d.%d\n", OPENSBI_VERSION_MAJOR,
|
|
|
|
OPENSBI_VERSION_MINOR);
|
2019-08-17 22:11:09 +08:00
|
|
|
#endif
|
2019-03-27 22:39:08 +08:00
|
|
|
|
|
|
|
sbi_printf(BANNER);
|
|
|
|
|
2019-11-26 18:36:29 +08:00
|
|
|
/* Determine MISA XLEN and MISA string */
|
|
|
|
xlen = misa_xlen();
|
|
|
|
if (xlen < 1) {
|
|
|
|
sbi_printf("Error %d getting MISA XLEN\n", xlen);
|
|
|
|
sbi_hart_hang();
|
|
|
|
}
|
|
|
|
|
2019-03-27 22:39:08 +08:00
|
|
|
/* Platform details */
|
2020-05-10 14:16:55 +08:00
|
|
|
sbi_printf("Platform Name : %s\n", sbi_platform_name(plat));
|
|
|
|
sbi_printf("Platform HART Count : %u\n",
|
2020-04-26 18:22:26 +08:00
|
|
|
sbi_platform_hart_count(plat));
|
2020-05-10 14:06:14 +08:00
|
|
|
sbi_platform_get_features_str(plat, str, sizeof(str));
|
2020-05-10 14:16:55 +08:00
|
|
|
sbi_printf("Platform Features : %s\n", str);
|
2020-05-10 07:47:31 +08:00
|
|
|
|
2020-05-05 14:16:25 +08:00
|
|
|
/* Boot HART details */
|
2020-05-10 14:16:55 +08:00
|
|
|
sbi_printf("Boot HART ID : %u\n", hartid);
|
2020-05-10 14:06:14 +08:00
|
|
|
misa_string(xlen, str, sizeof(str));
|
2020-05-10 14:16:55 +08:00
|
|
|
sbi_printf("Boot HART ISA : %s\n", str);
|
2020-05-10 14:06:14 +08:00
|
|
|
sbi_hart_get_features_str(hartid, str, sizeof(str));
|
2020-05-10 14:16:55 +08:00
|
|
|
sbi_printf("BOOT HART Features : %s\n", str);
|
2020-05-10 07:47:32 +08:00
|
|
|
|
2019-03-27 22:39:08 +08:00
|
|
|
/* Firmware details */
|
2020-05-10 14:16:55 +08:00
|
|
|
sbi_printf("Firmware Base : 0x%lx\n", scratch->fw_start);
|
|
|
|
sbi_printf("Firmware Size : %d KB\n",
|
2019-03-27 22:39:08 +08:00
|
|
|
(u32)(scratch->fw_size / 1024));
|
2020-05-10 14:06:14 +08:00
|
|
|
|
2019-03-27 22:39:08 +08:00
|
|
|
/* Generic details */
|
2020-05-10 14:16:55 +08:00
|
|
|
sbi_printf("Runtime SBI Version : %d.%d\n",
|
2019-03-27 22:39:08 +08:00
|
|
|
sbi_ecall_version_major(), sbi_ecall_version_minor());
|
|
|
|
sbi_printf("\n");
|
|
|
|
|
2020-02-18 14:23:52 +08:00
|
|
|
sbi_hart_delegation_dump(scratch);
|
2019-03-27 22:39:08 +08:00
|
|
|
sbi_hart_pmp_dump(scratch);
|
|
|
|
}
|
|
|
|
|
2020-03-02 19:20:01 +08:00
|
|
|
static spinlock_t coldboot_lock = SPIN_LOCK_INITIALIZER;
|
|
|
|
static unsigned long coldboot_done = 0;
|
2020-03-04 20:00:21 +08:00
|
|
|
static struct sbi_hartmask coldboot_wait_hmask = { 0 };
|
2020-03-02 19:20:01 +08:00
|
|
|
|
|
|
|
static void wait_for_coldboot(struct sbi_scratch *scratch, u32 hartid)
|
|
|
|
{
|
2020-03-21 06:10:36 +08:00
|
|
|
unsigned long saved_mie, cmip;
|
2020-03-02 19:20:01 +08:00
|
|
|
const struct sbi_platform *plat = sbi_platform_ptr(scratch);
|
|
|
|
|
|
|
|
/* Save MIE CSR */
|
|
|
|
saved_mie = csr_read(CSR_MIE);
|
|
|
|
|
|
|
|
/* Set MSIE bit to receive IPI */
|
|
|
|
csr_set(CSR_MIE, MIP_MSIP);
|
|
|
|
|
|
|
|
/* Acquire coldboot lock */
|
|
|
|
spin_lock(&coldboot_lock);
|
|
|
|
|
|
|
|
/* Mark current HART as waiting */
|
2020-03-04 20:00:21 +08:00
|
|
|
sbi_hartmask_set_hart(hartid, &coldboot_wait_hmask);
|
2020-03-02 19:20:01 +08:00
|
|
|
|
|
|
|
/* Wait for coldboot to finish using WFI */
|
|
|
|
while (!coldboot_done) {
|
|
|
|
spin_unlock(&coldboot_lock);
|
2020-03-21 06:10:36 +08:00
|
|
|
do {
|
|
|
|
wfi();
|
|
|
|
cmip = csr_read(CSR_MIP);
|
|
|
|
} while (!(cmip & MIP_MSIP));
|
2020-03-02 19:20:01 +08:00
|
|
|
spin_lock(&coldboot_lock);
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Unmark current HART as waiting */
|
2020-03-04 20:00:21 +08:00
|
|
|
sbi_hartmask_clear_hart(hartid, &coldboot_wait_hmask);
|
2020-03-02 19:20:01 +08:00
|
|
|
|
|
|
|
/* Release coldboot lock */
|
|
|
|
spin_unlock(&coldboot_lock);
|
|
|
|
|
|
|
|
/* Restore MIE CSR */
|
|
|
|
csr_write(CSR_MIE, saved_mie);
|
|
|
|
|
|
|
|
/* Clear current HART IPI */
|
|
|
|
sbi_platform_ipi_clear(plat, hartid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wake_coldboot_harts(struct sbi_scratch *scratch, u32 hartid)
|
|
|
|
{
|
|
|
|
const struct sbi_platform *plat = sbi_platform_ptr(scratch);
|
|
|
|
|
|
|
|
/* Acquire coldboot lock */
|
|
|
|
spin_lock(&coldboot_lock);
|
|
|
|
|
|
|
|
/* Mark coldboot done */
|
|
|
|
coldboot_done = 1;
|
|
|
|
|
|
|
|
/* Send an IPI to all HARTs waiting for coldboot */
|
2020-03-26 21:05:38 +08:00
|
|
|
for (int i = 0; i <= sbi_scratch_last_hartid(); i++) {
|
2020-03-04 20:00:21 +08:00
|
|
|
if ((i != hartid) &&
|
|
|
|
sbi_hartmask_test_hart(i, &coldboot_wait_hmask))
|
2020-03-02 19:20:01 +08:00
|
|
|
sbi_platform_ipi_send(plat, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release coldboot lock */
|
|
|
|
spin_unlock(&coldboot_lock);
|
|
|
|
}
|
|
|
|
|
2020-01-03 17:43:33 +08:00
|
|
|
static unsigned long init_count_offset;
|
|
|
|
|
2019-01-14 16:28:29 +08:00
|
|
|
static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
|
2018-12-11 21:54:06 +08:00
|
|
|
{
|
|
|
|
int rc;
|
2020-01-03 17:43:33 +08:00
|
|
|
unsigned long *init_count;
|
2019-03-06 15:29:34 +08:00
|
|
|
const struct sbi_platform *plat = sbi_platform_ptr(scratch);
|
2018-12-11 21:54:06 +08:00
|
|
|
|
2020-03-14 12:27:45 +08:00
|
|
|
/* Note: This has to be first thing in coldboot init sequence */
|
|
|
|
rc = sbi_scratch_init(scratch);
|
|
|
|
if (rc)
|
|
|
|
sbi_hart_hang();
|
|
|
|
|
2020-01-03 17:43:33 +08:00
|
|
|
init_count_offset = sbi_scratch_alloc_offset(__SIZEOF_POINTER__,
|
|
|
|
"INIT_COUNT");
|
|
|
|
if (!init_count_offset)
|
|
|
|
sbi_hart_hang();
|
|
|
|
|
2020-02-12 09:32:37 +08:00
|
|
|
rc = sbi_hsm_init(scratch, hartid, TRUE);
|
|
|
|
if (rc)
|
|
|
|
sbi_hart_hang();
|
|
|
|
|
2020-03-27 14:13:37 +08:00
|
|
|
rc = sbi_platform_early_init(plat, TRUE);
|
2018-12-11 21:54:06 +08:00
|
|
|
if (rc)
|
|
|
|
sbi_hart_hang();
|
|
|
|
|
2019-05-21 19:25:04 +08:00
|
|
|
rc = sbi_hart_init(scratch, hartid, TRUE);
|
2018-12-11 21:54:06 +08:00
|
|
|
if (rc)
|
|
|
|
sbi_hart_hang();
|
|
|
|
|
|
|
|
rc = sbi_console_init(scratch);
|
|
|
|
if (rc)
|
|
|
|
sbi_hart_hang();
|
|
|
|
|
2019-01-22 17:03:41 +08:00
|
|
|
rc = sbi_platform_irqchip_init(plat, TRUE);
|
2018-12-11 21:54:06 +08:00
|
|
|
if (rc)
|
|
|
|
sbi_hart_hang();
|
|
|
|
|
2019-01-22 16:50:59 +08:00
|
|
|
rc = sbi_ipi_init(scratch, TRUE);
|
2018-12-11 21:54:06 +08:00
|
|
|
if (rc)
|
|
|
|
sbi_hart_hang();
|
|
|
|
|
2020-01-15 16:01:12 +08:00
|
|
|
rc = sbi_tlb_init(scratch, TRUE);
|
|
|
|
if (rc)
|
|
|
|
sbi_hart_hang();
|
|
|
|
|
2019-01-22 17:17:03 +08:00
|
|
|
rc = sbi_timer_init(scratch, TRUE);
|
2018-12-11 21:54:06 +08:00
|
|
|
if (rc)
|
|
|
|
sbi_hart_hang();
|
|
|
|
|
2020-01-16 14:08:49 +08:00
|
|
|
rc = sbi_ecall_init();
|
|
|
|
if (rc)
|
|
|
|
sbi_hart_hang();
|
|
|
|
|
2020-03-27 14:13:37 +08:00
|
|
|
rc = sbi_platform_final_init(plat, TRUE);
|
2018-12-11 21:54:06 +08:00
|
|
|
if (rc)
|
|
|
|
sbi_hart_hang();
|
|
|
|
|
2019-03-27 22:58:49 +08:00
|
|
|
if (!(scratch->options & SBI_SCRATCH_NO_BOOT_PRINTS))
|
|
|
|
sbi_boot_prints(scratch, hartid);
|
2018-12-11 21:54:06 +08:00
|
|
|
|
2020-03-02 19:20:01 +08:00
|
|
|
wake_coldboot_harts(scratch, hartid);
|
2020-01-03 11:04:51 +08:00
|
|
|
|
2020-01-03 17:43:33 +08:00
|
|
|
init_count = sbi_scratch_offset_ptr(scratch, init_count_offset);
|
|
|
|
(*init_count)++;
|
|
|
|
|
2020-02-12 09:32:37 +08:00
|
|
|
sbi_hsm_prepare_next_jump(scratch, hartid);
|
2019-04-11 08:41:52 +08:00
|
|
|
sbi_hart_switch_mode(hartid, scratch->next_arg1, scratch->next_addr,
|
2019-04-05 16:38:57 +08:00
|
|
|
scratch->next_mode, FALSE);
|
2018-12-11 21:54:06 +08:00
|
|
|
}
|
|
|
|
|
2019-01-14 16:28:29 +08:00
|
|
|
static void __noreturn init_warmboot(struct sbi_scratch *scratch, u32 hartid)
|
2018-12-11 21:54:06 +08:00
|
|
|
{
|
|
|
|
int rc;
|
2020-01-03 17:43:33 +08:00
|
|
|
unsigned long *init_count;
|
2019-03-06 15:29:34 +08:00
|
|
|
const struct sbi_platform *plat = sbi_platform_ptr(scratch);
|
2018-12-11 21:54:06 +08:00
|
|
|
|
2020-03-02 19:20:01 +08:00
|
|
|
wait_for_coldboot(scratch, hartid);
|
2018-12-22 03:29:28 +08:00
|
|
|
|
2020-01-03 17:43:33 +08:00
|
|
|
if (!init_count_offset)
|
|
|
|
sbi_hart_hang();
|
|
|
|
|
2020-02-12 09:32:37 +08:00
|
|
|
rc = sbi_hsm_init(scratch, hartid, FALSE);
|
|
|
|
if (rc)
|
|
|
|
sbi_hart_hang();
|
|
|
|
|
2020-03-27 14:13:37 +08:00
|
|
|
rc = sbi_platform_early_init(plat, FALSE);
|
2018-12-11 21:54:06 +08:00
|
|
|
if (rc)
|
|
|
|
sbi_hart_hang();
|
|
|
|
|
2019-05-21 19:25:04 +08:00
|
|
|
rc = sbi_hart_init(scratch, hartid, FALSE);
|
2018-12-11 21:54:06 +08:00
|
|
|
if (rc)
|
|
|
|
sbi_hart_hang();
|
|
|
|
|
2019-01-22 17:03:41 +08:00
|
|
|
rc = sbi_platform_irqchip_init(plat, FALSE);
|
2018-12-11 21:54:06 +08:00
|
|
|
if (rc)
|
|
|
|
sbi_hart_hang();
|
|
|
|
|
2019-01-22 16:50:59 +08:00
|
|
|
rc = sbi_ipi_init(scratch, FALSE);
|
2018-12-11 21:54:06 +08:00
|
|
|
if (rc)
|
|
|
|
sbi_hart_hang();
|
|
|
|
|
2020-01-15 16:01:12 +08:00
|
|
|
rc = sbi_tlb_init(scratch, FALSE);
|
|
|
|
if (rc)
|
|
|
|
sbi_hart_hang();
|
|
|
|
|
2019-01-22 17:17:03 +08:00
|
|
|
rc = sbi_timer_init(scratch, FALSE);
|
2018-12-11 21:54:06 +08:00
|
|
|
if (rc)
|
|
|
|
sbi_hart_hang();
|
|
|
|
|
2020-03-27 14:13:37 +08:00
|
|
|
rc = sbi_platform_final_init(plat, FALSE);
|
2018-12-11 21:54:06 +08:00
|
|
|
if (rc)
|
|
|
|
sbi_hart_hang();
|
|
|
|
|
2020-01-03 17:43:33 +08:00
|
|
|
init_count = sbi_scratch_offset_ptr(scratch, init_count_offset);
|
|
|
|
(*init_count)++;
|
|
|
|
|
2020-02-12 09:32:37 +08:00
|
|
|
sbi_hsm_prepare_next_jump(scratch, hartid);
|
2020-01-03 11:04:51 +08:00
|
|
|
sbi_hart_switch_mode(hartid, scratch->next_arg1,
|
|
|
|
scratch->next_addr,
|
|
|
|
scratch->next_mode, FALSE);
|
2018-12-11 21:54:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static atomic_t coldboot_lottery = ATOMIC_INITIALIZER(0);
|
|
|
|
|
2019-01-14 16:28:29 +08:00
|
|
|
/**
|
|
|
|
* Initialize OpenSBI library for current HART and jump to next
|
|
|
|
* booting stage.
|
|
|
|
*
|
|
|
|
* The function expects following:
|
|
|
|
* 1. The 'mscratch' CSR is pointing to sbi_scratch of current HART
|
|
|
|
* 2. Stack pointer (SP) is setup for current HART
|
2019-01-14 17:30:15 +08:00
|
|
|
* 3. Interrupts are disabled in MSTATUS CSR
|
2019-01-22 13:36:31 +08:00
|
|
|
* 4. All interrupts are disabled in MIE CSR
|
2019-01-14 16:28:29 +08:00
|
|
|
*
|
|
|
|
* @param scratch pointer to sbi_scratch of current HART
|
|
|
|
*/
|
|
|
|
void __noreturn sbi_init(struct sbi_scratch *scratch)
|
2018-12-11 21:54:06 +08:00
|
|
|
{
|
2019-04-11 08:41:52 +08:00
|
|
|
bool coldboot = FALSE;
|
2020-03-12 12:51:25 +08:00
|
|
|
u32 hartid = current_hartid();
|
2019-03-06 15:29:34 +08:00
|
|
|
const struct sbi_platform *plat = sbi_platform_ptr(scratch);
|
2018-12-11 21:54:06 +08:00
|
|
|
|
2020-03-04 17:00:49 +08:00
|
|
|
if ((SBI_HARTMASK_MAX_BITS <= hartid) ||
|
2020-03-15 14:09:25 +08:00
|
|
|
sbi_platform_hart_invalid(plat, hartid))
|
2018-12-22 03:29:28 +08:00
|
|
|
sbi_hart_hang();
|
2019-04-11 08:41:52 +08:00
|
|
|
|
2020-03-26 20:40:02 +08:00
|
|
|
if (atomic_xchg(&coldboot_lottery, 1) == 0)
|
2018-12-11 21:54:06 +08:00
|
|
|
coldboot = TRUE;
|
|
|
|
|
|
|
|
if (coldboot)
|
|
|
|
init_coldboot(scratch, hartid);
|
|
|
|
else
|
|
|
|
init_warmboot(scratch, hartid);
|
|
|
|
}
|
2020-01-03 11:19:23 +08:00
|
|
|
|
2020-01-03 17:43:33 +08:00
|
|
|
unsigned long sbi_init_count(u32 hartid)
|
|
|
|
{
|
|
|
|
struct sbi_scratch *scratch;
|
|
|
|
unsigned long *init_count;
|
|
|
|
|
2020-03-14 22:13:26 +08:00
|
|
|
if (!init_count_offset)
|
2020-01-03 17:43:33 +08:00
|
|
|
return 0;
|
|
|
|
|
2020-03-14 16:50:22 +08:00
|
|
|
scratch = sbi_hartid_to_scratch(hartid);
|
2020-03-14 21:25:53 +08:00
|
|
|
if (!scratch)
|
|
|
|
return 0;
|
|
|
|
|
2020-01-03 17:43:33 +08:00
|
|
|
init_count = sbi_scratch_offset_ptr(scratch, init_count_offset);
|
|
|
|
|
|
|
|
return *init_count;
|
|
|
|
}
|
|
|
|
|
2020-01-03 11:19:23 +08:00
|
|
|
/**
|
|
|
|
* Exit OpenSBI library for current HART and stop HART
|
|
|
|
*
|
|
|
|
* The function expects following:
|
|
|
|
* 1. The 'mscratch' CSR is pointing to sbi_scratch of current HART
|
|
|
|
* 2. Stack pointer (SP) is setup for current HART
|
|
|
|
*
|
|
|
|
* @param scratch pointer to sbi_scratch of current HART
|
|
|
|
*/
|
|
|
|
void __noreturn sbi_exit(struct sbi_scratch *scratch)
|
|
|
|
{
|
2020-03-12 12:51:25 +08:00
|
|
|
u32 hartid = current_hartid();
|
2020-01-03 11:19:23 +08:00
|
|
|
const struct sbi_platform *plat = sbi_platform_ptr(scratch);
|
|
|
|
|
2020-03-15 14:09:25 +08:00
|
|
|
if (sbi_platform_hart_invalid(plat, hartid))
|
2020-01-03 11:19:23 +08:00
|
|
|
sbi_hart_hang();
|
|
|
|
|
2020-01-03 11:40:41 +08:00
|
|
|
sbi_platform_early_exit(plat);
|
|
|
|
|
2020-01-03 11:48:42 +08:00
|
|
|
sbi_timer_exit(scratch);
|
|
|
|
|
2020-01-03 12:09:10 +08:00
|
|
|
sbi_ipi_exit(scratch);
|
|
|
|
|
2020-01-03 12:21:58 +08:00
|
|
|
sbi_platform_irqchip_exit(plat);
|
|
|
|
|
2020-01-03 11:40:41 +08:00
|
|
|
sbi_platform_final_exit(plat);
|
|
|
|
|
2020-02-12 09:32:37 +08:00
|
|
|
sbi_hsm_exit(scratch);
|
2020-01-03 11:19:23 +08:00
|
|
|
}
|