David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Definitions for measuring cputime on powerpc machines. |
| 4 | * |
| 5 | * Copyright (C) 2006 Paul Mackerras, IBM Corp. |
| 6 | * |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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 | |
| 23 | typedef u64 __nocast cputime_t; |
| 24 | typedef u64 __nocast cputime64_t; |
| 25 | |
| 26 | #define cmpxchg_cputime(ptr, old, new) cmpxchg(ptr, old, new) |
| 27 | |
| 28 | #ifdef __KERNEL__ |
| 29 | /* |
| 30 | * Convert cputime <-> microseconds |
| 31 | */ |
| 32 | extern u64 __cputime_usec_factor; |
| 33 | |
| 34 | static inline unsigned long cputime_to_usecs(const cputime_t ct) |
| 35 | { |
| 36 | return mulhdu((__force u64) ct, __cputime_usec_factor); |
| 37 | } |
| 38 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 39 | #define cputime_to_nsecs(cputime) tb_to_ns((__force u64)cputime) |
| 40 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 41 | /* |
| 42 | * PPC64 uses PACA which is task independent for storing accounting data while |
| 43 | * PPC32 uses struct thread_info, therefore at task switch the accounting data |
| 44 | * has to be populated in the new task |
| 45 | */ |
| 46 | #ifdef CONFIG_PPC64 |
| 47 | #define get_accounting(tsk) (&get_paca()->accounting) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 48 | #define raw_get_accounting(tsk) (&local_paca->accounting) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 49 | static inline void arch_vtime_task_switch(struct task_struct *tsk) { } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 50 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 51 | #else |
| 52 | #define get_accounting(tsk) (&task_thread_info(tsk)->accounting) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 53 | #define raw_get_accounting(tsk) get_accounting(tsk) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 54 | /* |
| 55 | * Called from the context switch with interrupts disabled, to charge all |
| 56 | * accumulated times to the current process, and to prepare accounting on |
| 57 | * the next process. |
| 58 | */ |
| 59 | static inline void arch_vtime_task_switch(struct task_struct *prev) |
| 60 | { |
| 61 | struct cpu_accounting_data *acct = get_accounting(current); |
| 62 | struct cpu_accounting_data *acct0 = get_accounting(prev); |
| 63 | |
| 64 | acct->starttime = acct0->starttime; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 65 | } |
| 66 | #endif |
| 67 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 68 | /* |
| 69 | * account_cpu_user_entry/exit runs "unreconciled", so can't trace, |
| 70 | * can't use get_paca() |
| 71 | */ |
| 72 | static notrace inline void account_cpu_user_entry(void) |
| 73 | { |
| 74 | unsigned long tb = mftb(); |
| 75 | struct cpu_accounting_data *acct = raw_get_accounting(current); |
| 76 | |
| 77 | acct->utime += (tb - acct->starttime_user); |
| 78 | acct->starttime = tb; |
| 79 | } |
| 80 | |
| 81 | static notrace inline void account_cpu_user_exit(void) |
| 82 | { |
| 83 | unsigned long tb = mftb(); |
| 84 | struct cpu_accounting_data *acct = raw_get_accounting(current); |
| 85 | |
| 86 | acct->stime += (tb - acct->starttime); |
| 87 | acct->starttime_user = tb; |
| 88 | } |
| 89 | |
| 90 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 91 | #endif /* __KERNEL__ */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 92 | #else /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */ |
| 93 | static inline void account_cpu_user_entry(void) |
| 94 | { |
| 95 | } |
| 96 | static inline void account_cpu_user_exit(void) |
| 97 | { |
| 98 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 99 | #endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */ |
| 100 | #endif /* __POWERPC_CPUTIME_H */ |