1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Definitions for measuring cputime on powerpc machines.
5 * Copyright (C) 2006 Paul Mackerras, IBM Corp.
7 * If we have CONFIG_VIRT_CPU_ACCOUNTING_NATIVE, we measure cpu time in
8 * the same units as the timebase. Otherwise we measure cpu time
9 * in jiffies using the generic definitions.
12 #ifndef __POWERPC_CPUTIME_H
13 #define __POWERPC_CPUTIME_H
15 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
17 #include <linux/types.h>
18 #include <linux/time.h>
19 #include <asm/div64.h>
21 #include <asm/param.h>
22 #include <asm/firmware.h>
24 typedef u64 __nocast cputime_t;
25 typedef u64 __nocast cputime64_t;
27 #define cmpxchg_cputime(ptr, old, new) cmpxchg(ptr, old, new)
31 * Convert cputime <-> microseconds
33 extern u64 __cputime_usec_factor;
35 static inline unsigned long cputime_to_usecs(const cputime_t ct)
37 return mulhdu((__force u64) ct, __cputime_usec_factor);
40 #define cputime_to_nsecs(cputime) tb_to_ns((__force u64)cputime)
43 * PPC64 uses PACA which is task independent for storing accounting data while
44 * PPC32 uses struct thread_info, therefore at task switch the accounting data
45 * has to be populated in the new task
48 #define get_accounting(tsk) (&get_paca()->accounting)
49 #define raw_get_accounting(tsk) (&local_paca->accounting)
50 static inline void arch_vtime_task_switch(struct task_struct *tsk) { }
53 #define get_accounting(tsk) (&task_thread_info(tsk)->accounting)
54 #define raw_get_accounting(tsk) get_accounting(tsk)
56 * Called from the context switch with interrupts disabled, to charge all
57 * accumulated times to the current process, and to prepare accounting on
60 static inline void arch_vtime_task_switch(struct task_struct *prev)
62 struct cpu_accounting_data *acct = get_accounting(current);
63 struct cpu_accounting_data *acct0 = get_accounting(prev);
65 acct->starttime = acct0->starttime;
70 * account_cpu_user_entry/exit runs "unreconciled", so can't trace,
71 * can't use get_paca()
73 static notrace inline void account_cpu_user_entry(void)
75 unsigned long tb = mftb();
76 struct cpu_accounting_data *acct = raw_get_accounting(current);
78 acct->utime += (tb - acct->starttime_user);
82 static notrace inline void account_cpu_user_exit(void)
84 unsigned long tb = mftb();
85 struct cpu_accounting_data *acct = raw_get_accounting(current);
87 acct->stime += (tb - acct->starttime);
88 acct->starttime_user = tb;
91 static notrace inline void account_stolen_time(void)
93 #ifdef CONFIG_PPC_SPLPAR
94 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
95 struct lppaca *lp = local_paca->lppaca_ptr;
97 if (unlikely(local_paca->dtl_ridx != be64_to_cpu(lp->dtl_idx)))
98 pseries_accumulate_stolen_time();
103 #endif /* __KERNEL__ */
104 #else /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
105 static inline void account_cpu_user_entry(void)
108 static inline void account_cpu_user_exit(void)
111 static notrace inline void account_stolen_time(void)
114 #endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
115 #endif /* __POWERPC_CPUTIME_H */