18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Definitions for measuring cputime on powerpc machines.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2006 Paul Mackerras, IBM Corp.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * If we have CONFIG_VIRT_CPU_ACCOUNTING_NATIVE, we measure cpu time in
88c2ecf20Sopenharmony_ci * the same units as the timebase.  Otherwise we measure cpu time
98c2ecf20Sopenharmony_ci * in jiffies using the generic definitions.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#ifndef __POWERPC_CPUTIME_H
138c2ecf20Sopenharmony_ci#define __POWERPC_CPUTIME_H
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <linux/types.h>
188c2ecf20Sopenharmony_ci#include <linux/time.h>
198c2ecf20Sopenharmony_ci#include <asm/div64.h>
208c2ecf20Sopenharmony_ci#include <asm/time.h>
218c2ecf20Sopenharmony_ci#include <asm/param.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_citypedef u64 __nocast cputime_t;
248c2ecf20Sopenharmony_citypedef u64 __nocast cputime64_t;
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define cmpxchg_cputime(ptr, old, new) cmpxchg(ptr, old, new)
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#ifdef __KERNEL__
298c2ecf20Sopenharmony_ci/*
308c2ecf20Sopenharmony_ci * Convert cputime <-> microseconds
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_ciextern u64 __cputime_usec_factor;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic inline unsigned long cputime_to_usecs(const cputime_t ct)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	return mulhdu((__force u64) ct, __cputime_usec_factor);
378c2ecf20Sopenharmony_ci}
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define cputime_to_nsecs(cputime) tb_to_ns((__force u64)cputime)
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/*
428c2ecf20Sopenharmony_ci * PPC64 uses PACA which is task independent for storing accounting data while
438c2ecf20Sopenharmony_ci * PPC32 uses struct thread_info, therefore at task switch the accounting data
448c2ecf20Sopenharmony_ci * has to be populated in the new task
458c2ecf20Sopenharmony_ci */
468c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC64
478c2ecf20Sopenharmony_ci#define get_accounting(tsk)	(&get_paca()->accounting)
488c2ecf20Sopenharmony_ci#define raw_get_accounting(tsk)	(&local_paca->accounting)
498c2ecf20Sopenharmony_cistatic inline void arch_vtime_task_switch(struct task_struct *tsk) { }
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#else
528c2ecf20Sopenharmony_ci#define get_accounting(tsk)	(&task_thread_info(tsk)->accounting)
538c2ecf20Sopenharmony_ci#define raw_get_accounting(tsk)	get_accounting(tsk)
548c2ecf20Sopenharmony_ci/*
558c2ecf20Sopenharmony_ci * Called from the context switch with interrupts disabled, to charge all
568c2ecf20Sopenharmony_ci * accumulated times to the current process, and to prepare accounting on
578c2ecf20Sopenharmony_ci * the next process.
588c2ecf20Sopenharmony_ci */
598c2ecf20Sopenharmony_cistatic inline void arch_vtime_task_switch(struct task_struct *prev)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	struct cpu_accounting_data *acct = get_accounting(current);
628c2ecf20Sopenharmony_ci	struct cpu_accounting_data *acct0 = get_accounting(prev);
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	acct->starttime = acct0->starttime;
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci#endif
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/*
698c2ecf20Sopenharmony_ci * account_cpu_user_entry/exit runs "unreconciled", so can't trace,
708c2ecf20Sopenharmony_ci * can't use get_paca()
718c2ecf20Sopenharmony_ci */
728c2ecf20Sopenharmony_cistatic notrace inline void account_cpu_user_entry(void)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	unsigned long tb = mftb();
758c2ecf20Sopenharmony_ci	struct cpu_accounting_data *acct = raw_get_accounting(current);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	acct->utime += (tb - acct->starttime_user);
788c2ecf20Sopenharmony_ci	acct->starttime = tb;
798c2ecf20Sopenharmony_ci}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic notrace inline void account_cpu_user_exit(void)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	unsigned long tb = mftb();
848c2ecf20Sopenharmony_ci	struct cpu_accounting_data *acct = raw_get_accounting(current);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	acct->stime += (tb - acct->starttime);
878c2ecf20Sopenharmony_ci	acct->starttime_user = tb;
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci#endif /* __KERNEL__ */
928c2ecf20Sopenharmony_ci#else /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
938c2ecf20Sopenharmony_cistatic inline void account_cpu_user_entry(void)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_cistatic inline void account_cpu_user_exit(void)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci#endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
1008c2ecf20Sopenharmony_ci#endif /* __POWERPC_CPUTIME_H */
101