GNU Linux-libre 6.1.90-gnu
[releases.git] / arch / powerpc / include / asm / cputime.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Definitions for measuring cputime on powerpc machines.
4  *
5  * Copyright (C) 2006 Paul Mackerras, IBM Corp.
6  *
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.
10  */
11
12 #ifndef __POWERPC_CPUTIME_H
13 #define __POWERPC_CPUTIME_H
14
15 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
16
17 #include <linux/types.h>
18 #include <linux/time.h>
19 #include <asm/div64.h>
20 #include <asm/time.h>
21 #include <asm/param.h>
22 #include <asm/firmware.h>
23
24 typedef u64 __nocast cputime_t;
25 typedef u64 __nocast cputime64_t;
26
27 #define cmpxchg_cputime(ptr, old, new) cmpxchg(ptr, old, new)
28
29 #ifdef __KERNEL__
30 /*
31  * Convert cputime <-> microseconds
32  */
33 extern u64 __cputime_usec_factor;
34
35 static inline unsigned long cputime_to_usecs(const cputime_t ct)
36 {
37         return mulhdu((__force u64) ct, __cputime_usec_factor);
38 }
39
40 #define cputime_to_nsecs(cputime) tb_to_ns((__force u64)cputime)
41
42 /*
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
46  */
47 #ifdef CONFIG_PPC64
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) { }
51
52 #else
53 #define get_accounting(tsk)     (&task_thread_info(tsk)->accounting)
54 #define raw_get_accounting(tsk) get_accounting(tsk)
55 /*
56  * Called from the context switch with interrupts disabled, to charge all
57  * accumulated times to the current process, and to prepare accounting on
58  * the next process.
59  */
60 static inline void arch_vtime_task_switch(struct task_struct *prev)
61 {
62         struct cpu_accounting_data *acct = get_accounting(current);
63         struct cpu_accounting_data *acct0 = get_accounting(prev);
64
65         acct->starttime = acct0->starttime;
66 }
67 #endif
68
69 /*
70  * account_cpu_user_entry/exit runs "unreconciled", so can't trace,
71  * can't use get_paca()
72  */
73 static notrace inline void account_cpu_user_entry(void)
74 {
75         unsigned long tb = mftb();
76         struct cpu_accounting_data *acct = raw_get_accounting(current);
77
78         acct->utime += (tb - acct->starttime_user);
79         acct->starttime = tb;
80 }
81
82 static notrace inline void account_cpu_user_exit(void)
83 {
84         unsigned long tb = mftb();
85         struct cpu_accounting_data *acct = raw_get_accounting(current);
86
87         acct->stime += (tb - acct->starttime);
88         acct->starttime_user = tb;
89 }
90
91 static notrace inline void account_stolen_time(void)
92 {
93 #ifdef CONFIG_PPC_SPLPAR
94         if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
95                 struct lppaca *lp = local_paca->lppaca_ptr;
96
97                 if (unlikely(local_paca->dtl_ridx != be64_to_cpu(lp->dtl_idx)))
98                         pseries_accumulate_stolen_time();
99         }
100 #endif
101 }
102
103 #endif /* __KERNEL__ */
104 #else /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
105 static inline void account_cpu_user_entry(void)
106 {
107 }
108 static inline void account_cpu_user_exit(void)
109 {
110 }
111 static notrace inline void account_stolen_time(void)
112 {
113 }
114 #endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
115 #endif /* __POWERPC_CPUTIME_H */