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#ifdef __KERNEL__
25#define cputime_to_nsecs(cputime) tb_to_ns(cputime)
26
27/*
28 * PPC64 uses PACA which is task independent for storing accounting data while
29 * PPC32 uses struct thread_info, therefore at task switch the accounting data
30 * has to be populated in the new task
31 */
32#ifdef CONFIG_PPC64
33#define get_accounting(tsk)	(&get_paca()->accounting)
34#define raw_get_accounting(tsk)	(&local_paca->accounting)
35static inline void arch_vtime_task_switch(struct task_struct *tsk) { }
36
37#else
38#define get_accounting(tsk)	(&task_thread_info(tsk)->accounting)
39#define raw_get_accounting(tsk)	get_accounting(tsk)
40/*
41 * Called from the context switch with interrupts disabled, to charge all
42 * accumulated times to the current process, and to prepare accounting on
43 * the next process.
44 */
45static inline void arch_vtime_task_switch(struct task_struct *prev)
46{
47	struct cpu_accounting_data *acct = get_accounting(current);
48	struct cpu_accounting_data *acct0 = get_accounting(prev);
49
50	acct->starttime = acct0->starttime;
51}
52#endif
53
54/*
55 * account_cpu_user_entry/exit runs "unreconciled", so can't trace,
56 * can't use get_paca()
57 */
58static notrace inline void account_cpu_user_entry(void)
59{
60	unsigned long tb = mftb();
61	struct cpu_accounting_data *acct = raw_get_accounting(current);
62
63	acct->utime += (tb - acct->starttime_user);
64	acct->starttime = tb;
65}
66
67static notrace inline void account_cpu_user_exit(void)
68{
69	unsigned long tb = mftb();
70	struct cpu_accounting_data *acct = raw_get_accounting(current);
71
72	acct->stime += (tb - acct->starttime);
73	acct->starttime_user = tb;
74}
75
76static notrace inline void account_stolen_time(void)
77{
78#ifdef CONFIG_PPC_SPLPAR
79	if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
80		struct lppaca *lp = local_paca->lppaca_ptr;
81
82		if (unlikely(local_paca->dtl_ridx != be64_to_cpu(lp->dtl_idx)))
83			pseries_accumulate_stolen_time();
84	}
85#endif
86}
87
88#endif /* __KERNEL__ */
89#else /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
90static inline void account_cpu_user_entry(void)
91{
92}
93static inline void account_cpu_user_exit(void)
94{
95}
96static notrace inline void account_stolen_time(void)
97{
98}
99#endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
100#endif /* __POWERPC_CPUTIME_H */
101