linux/tools/testing/selftests/kvm/include/loongarch/arch_timer.h
David Matlack 26f8453288 KVM: selftests: Use u64 instead of uint64_t
Use u64 instead of uint64_t to make the KVM selftests code more concise
and more similar to the kernel (since selftests are primarily developed
by kernel developers).

This commit was generated with the following command:

  git ls-files tools/testing/selftests/kvm | xargs sed -i 's/uint64_t/u64/g'

Then by manually adjusting whitespace to make checkpatch.pl happy.

Include <linux/types.h> in include/kvm_util_types.h, iinclude/test_util.h,
and include/x86/pmu.h to pick up the tools-defined u64.  Arguably, all
headers (especially kvm_util_types.h) should have already been including
stdint.h to get uint64_t from the libc headers, but the missing dependency
only rears its head once KVM uses u64 instead of uint64_t.

No functional change intended.

Signed-off-by: David Matlack <dmatlack@google.com>
[sean: rename pread_uint64() => pread_u64, expand on types.h include]
Link: https://patch.msgid.link/20260420212004.3938325-5-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
2026-04-20 14:54:16 -07:00

86 lines
1.7 KiB
C

/* SPDX-License-Identifier: GPL-2.0 */
/*
* LoongArch Constant Timer specific interface
*/
#ifndef SELFTEST_KVM_ARCH_TIMER_H
#define SELFTEST_KVM_ARCH_TIMER_H
#include "processor.h"
/* LoongArch timer frequency is constant 100MHZ */
#define TIMER_FREQ (100UL << 20)
#define msec_to_cycles(msec) (TIMER_FREQ * (unsigned long)(msec) / 1000)
#define usec_to_cycles(usec) (TIMER_FREQ * (unsigned long)(usec) / 1000000)
#define cycles_to_usec(cycles) ((unsigned long)(cycles) * 1000000 / TIMER_FREQ)
static inline unsigned long timer_get_cycles(void)
{
unsigned long val = 0;
__asm__ __volatile__(
"rdtime.d %0, $zero\n\t"
: "=r"(val)
:
);
return val;
}
static inline unsigned long timer_get_cfg(void)
{
return csr_read(LOONGARCH_CSR_TCFG);
}
static inline unsigned long timer_get_val(void)
{
return csr_read(LOONGARCH_CSR_TVAL);
}
static inline void disable_timer(void)
{
csr_write(0, LOONGARCH_CSR_TCFG);
}
static inline void timer_irq_enable(void)
{
unsigned long val;
val = csr_read(LOONGARCH_CSR_ECFG);
val |= ECFGF_TIMER;
csr_write(val, LOONGARCH_CSR_ECFG);
}
static inline void timer_irq_disable(void)
{
unsigned long val;
val = csr_read(LOONGARCH_CSR_ECFG);
val &= ~ECFGF_TIMER;
csr_write(val, LOONGARCH_CSR_ECFG);
}
static inline void timer_set_next_cmp_ms(unsigned int msec, bool period)
{
unsigned long val;
val = msec_to_cycles(msec) & CSR_TCFG_VAL;
val |= CSR_TCFG_EN;
if (period)
val |= CSR_TCFG_PERIOD;
csr_write(val, LOONGARCH_CSR_TCFG);
}
static inline void __delay(u64 cycles)
{
u64 start = timer_get_cycles();
while ((timer_get_cycles() - start) < cycles)
cpu_relax();
}
static inline void udelay(unsigned long usec)
{
__delay(usec_to_cycles(usec));
}
#endif /* SELFTEST_KVM_ARCH_TIMER_H */