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_encoding.h>
|
2019-08-18 15:44:44 +08:00
|
|
|
#include <sbi/sbi_error.h>
|
2020-05-10 07:47:30 +08:00
|
|
|
#include <sbi/sbi_hart.h>
|
2018-12-11 21:54:06 +08:00
|
|
|
#include <sbi/sbi_platform.h>
|
2020-03-27 14:38:34 +08:00
|
|
|
#include <sbi/sbi_scratch.h>
|
2018-12-11 21:54:06 +08:00
|
|
|
#include <sbi/sbi_timer.h>
|
|
|
|
|
2019-08-18 15:44:44 +08:00
|
|
|
static unsigned long time_delta_off;
|
2021-04-22 00:34:17 +08:00
|
|
|
static u64 (*get_time_val)(void);
|
|
|
|
static const struct sbi_timer_device *timer_dev = NULL;
|
2019-08-18 15:44:44 +08:00
|
|
|
|
2018-12-11 21:54:06 +08:00
|
|
|
#if __riscv_xlen == 32
|
2021-04-22 00:34:17 +08:00
|
|
|
static u64 get_ticks(void)
|
2018-12-11 21:54:06 +08:00
|
|
|
{
|
|
|
|
u32 lo, hi, tmp;
|
2019-04-11 08:41:52 +08:00
|
|
|
__asm__ __volatile__("1:\n"
|
|
|
|
"rdtimeh %0\n"
|
|
|
|
"rdtime %1\n"
|
|
|
|
"rdtimeh %2\n"
|
|
|
|
"bne %0, %2, 1b"
|
|
|
|
: "=&r"(hi), "=&r"(lo), "=&r"(tmp));
|
2018-12-11 21:54:06 +08:00
|
|
|
return ((u64)hi << 32) | lo;
|
|
|
|
}
|
|
|
|
#else
|
2021-04-22 00:34:17 +08:00
|
|
|
static u64 get_ticks(void)
|
2018-12-11 21:54:06 +08:00
|
|
|
{
|
|
|
|
unsigned long n;
|
|
|
|
|
2019-04-11 08:41:52 +08:00
|
|
|
__asm__ __volatile__("rdtime %0" : "=r"(n));
|
2018-12-11 21:54:06 +08:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-04-22 00:34:17 +08:00
|
|
|
static u64 get_platform_ticks(void)
|
|
|
|
{
|
|
|
|
return timer_dev->timer_value();
|
|
|
|
}
|
|
|
|
|
2020-03-27 14:38:34 +08:00
|
|
|
u64 sbi_timer_value(void)
|
2018-12-11 21:54:06 +08:00
|
|
|
{
|
2021-04-22 00:34:17 +08:00
|
|
|
if (get_time_val)
|
|
|
|
return get_time_val();
|
|
|
|
return 0;
|
2018-12-11 21:54:06 +08:00
|
|
|
}
|
|
|
|
|
2020-03-27 14:38:34 +08:00
|
|
|
u64 sbi_timer_virt_value(void)
|
2019-08-18 15:44:44 +08:00
|
|
|
{
|
2020-03-27 14:38:34 +08:00
|
|
|
u64 *time_delta = sbi_scratch_offset_ptr(sbi_scratch_thishart_ptr(),
|
|
|
|
time_delta_off);
|
2019-08-18 15:44:44 +08:00
|
|
|
|
2020-03-27 14:38:34 +08:00
|
|
|
return sbi_timer_value() + *time_delta;
|
2019-08-18 15:44:44 +08:00
|
|
|
}
|
|
|
|
|
2020-03-27 14:38:34 +08:00
|
|
|
u64 sbi_timer_get_delta(void)
|
2019-08-18 15:44:44 +08:00
|
|
|
{
|
2020-03-27 14:38:34 +08:00
|
|
|
u64 *time_delta = sbi_scratch_offset_ptr(sbi_scratch_thishart_ptr(),
|
|
|
|
time_delta_off);
|
2019-08-18 15:44:44 +08:00
|
|
|
|
|
|
|
return *time_delta;
|
|
|
|
}
|
|
|
|
|
2020-03-27 14:38:34 +08:00
|
|
|
void sbi_timer_set_delta(ulong delta)
|
2019-08-18 15:44:44 +08:00
|
|
|
{
|
2020-03-27 14:38:34 +08:00
|
|
|
u64 *time_delta = sbi_scratch_offset_ptr(sbi_scratch_thishart_ptr(),
|
|
|
|
time_delta_off);
|
2019-08-18 15:44:44 +08:00
|
|
|
|
|
|
|
*time_delta = (u64)delta;
|
|
|
|
}
|
|
|
|
|
2020-03-27 14:38:34 +08:00
|
|
|
void sbi_timer_set_delta_upper(ulong delta_upper)
|
2019-08-18 15:44:44 +08:00
|
|
|
{
|
2020-03-27 14:38:34 +08:00
|
|
|
u64 *time_delta = sbi_scratch_offset_ptr(sbi_scratch_thishart_ptr(),
|
|
|
|
time_delta_off);
|
2019-08-18 15:44:44 +08:00
|
|
|
|
|
|
|
*time_delta &= 0xffffffffULL;
|
|
|
|
*time_delta |= ((u64)delta_upper << 32);
|
|
|
|
}
|
|
|
|
|
2020-03-27 14:38:34 +08:00
|
|
|
void sbi_timer_event_start(u64 next_event)
|
2018-12-11 21:54:06 +08:00
|
|
|
{
|
2021-04-22 00:34:17 +08:00
|
|
|
if (timer_dev && timer_dev->timer_event_start)
|
|
|
|
timer_dev->timer_event_start(next_event);
|
2019-02-13 10:32:06 +08:00
|
|
|
csr_clear(CSR_MIP, MIP_STIP);
|
|
|
|
csr_set(CSR_MIE, MIP_MTIP);
|
2018-12-11 21:54:06 +08:00
|
|
|
}
|
|
|
|
|
2020-03-27 14:38:34 +08:00
|
|
|
void sbi_timer_process(void)
|
2018-12-11 21:54:06 +08:00
|
|
|
{
|
2019-02-13 10:32:06 +08:00
|
|
|
csr_clear(CSR_MIE, MIP_MTIP);
|
|
|
|
csr_set(CSR_MIP, MIP_STIP);
|
2018-12-11 21:54:06 +08:00
|
|
|
}
|
|
|
|
|
2021-04-22 00:34:17 +08:00
|
|
|
const struct sbi_timer_device *sbi_timer_get_device(void)
|
|
|
|
{
|
|
|
|
return timer_dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sbi_timer_set_device(const struct sbi_timer_device *dev)
|
|
|
|
{
|
|
|
|
if (!dev || timer_dev)
|
|
|
|
return;
|
|
|
|
|
|
|
|
timer_dev = dev;
|
|
|
|
if (!get_time_val && timer_dev->timer_value)
|
|
|
|
get_time_val = get_platform_ticks;
|
|
|
|
}
|
|
|
|
|
2019-01-22 17:17:03 +08:00
|
|
|
int sbi_timer_init(struct sbi_scratch *scratch, bool cold_boot)
|
2018-12-11 21:54:06 +08:00
|
|
|
{
|
2019-08-18 15:44:44 +08:00
|
|
|
u64 *time_delta;
|
2020-05-10 07:47:30 +08:00
|
|
|
const struct sbi_platform *plat = sbi_platform_ptr(scratch);
|
2019-08-18 15:44:44 +08:00
|
|
|
|
|
|
|
if (cold_boot) {
|
|
|
|
time_delta_off = sbi_scratch_alloc_offset(sizeof(*time_delta),
|
|
|
|
"TIME_DELTA");
|
|
|
|
if (!time_delta_off)
|
|
|
|
return SBI_ENOMEM;
|
2021-04-22 00:34:17 +08:00
|
|
|
|
|
|
|
if (sbi_hart_has_feature(scratch, SBI_HART_HAS_TIME))
|
|
|
|
get_time_val = get_ticks;
|
2019-08-18 15:44:44 +08:00
|
|
|
} else {
|
|
|
|
if (!time_delta_off)
|
|
|
|
return SBI_ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
time_delta = sbi_scratch_offset_ptr(scratch, time_delta_off);
|
|
|
|
*time_delta = 0;
|
|
|
|
|
2021-04-22 00:34:17 +08:00
|
|
|
return sbi_platform_timer_init(plat, cold_boot);
|
2018-12-11 21:54:06 +08:00
|
|
|
}
|
2020-01-03 11:48:42 +08:00
|
|
|
|
|
|
|
void sbi_timer_exit(struct sbi_scratch *scratch)
|
|
|
|
{
|
2021-04-22 00:34:17 +08:00
|
|
|
if (timer_dev && timer_dev->timer_event_stop)
|
|
|
|
timer_dev->timer_event_stop();
|
2020-01-03 11:48:42 +08:00
|
|
|
|
|
|
|
csr_clear(CSR_MIP, MIP_STIP);
|
|
|
|
csr_clear(CSR_MIE, MIP_MTIP);
|
|
|
|
|
|
|
|
sbi_platform_timer_exit(sbi_platform_ptr(scratch));
|
|
|
|
}
|