162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Definitions for measuring cputime on powerpc machines.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2006 Paul Mackerras, IBM Corp.
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * If we have CONFIG_VIRT_CPU_ACCOUNTING_NATIVE, we measure cpu time in
862306a36Sopenharmony_ci * the same units as the timebase.  Otherwise we measure cpu time
962306a36Sopenharmony_ci * in jiffies using the generic definitions.
1062306a36Sopenharmony_ci */
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#ifndef __POWERPC_CPUTIME_H
1362306a36Sopenharmony_ci#define __POWERPC_CPUTIME_H
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include <linux/types.h>
1862306a36Sopenharmony_ci#include <linux/time.h>
1962306a36Sopenharmony_ci#include <asm/div64.h>
2062306a36Sopenharmony_ci#include <asm/time.h>
2162306a36Sopenharmony_ci#include <asm/param.h>
2262306a36Sopenharmony_ci#include <asm/firmware.h>
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#ifdef __KERNEL__
2562306a36Sopenharmony_ci#define cputime_to_nsecs(cputime) tb_to_ns(cputime)
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci/*
2862306a36Sopenharmony_ci * PPC64 uses PACA which is task independent for storing accounting data while
2962306a36Sopenharmony_ci * PPC32 uses struct thread_info, therefore at task switch the accounting data
3062306a36Sopenharmony_ci * has to be populated in the new task
3162306a36Sopenharmony_ci */
3262306a36Sopenharmony_ci#ifdef CONFIG_PPC64
3362306a36Sopenharmony_ci#define get_accounting(tsk)	(&get_paca()->accounting)
3462306a36Sopenharmony_ci#define raw_get_accounting(tsk)	(&local_paca->accounting)
3562306a36Sopenharmony_cistatic inline void arch_vtime_task_switch(struct task_struct *tsk) { }
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci#else
3862306a36Sopenharmony_ci#define get_accounting(tsk)	(&task_thread_info(tsk)->accounting)
3962306a36Sopenharmony_ci#define raw_get_accounting(tsk)	get_accounting(tsk)
4062306a36Sopenharmony_ci/*
4162306a36Sopenharmony_ci * Called from the context switch with interrupts disabled, to charge all
4262306a36Sopenharmony_ci * accumulated times to the current process, and to prepare accounting on
4362306a36Sopenharmony_ci * the next process.
4462306a36Sopenharmony_ci */
4562306a36Sopenharmony_cistatic inline void arch_vtime_task_switch(struct task_struct *prev)
4662306a36Sopenharmony_ci{
4762306a36Sopenharmony_ci	struct cpu_accounting_data *acct = get_accounting(current);
4862306a36Sopenharmony_ci	struct cpu_accounting_data *acct0 = get_accounting(prev);
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci	acct->starttime = acct0->starttime;
5162306a36Sopenharmony_ci}
5262306a36Sopenharmony_ci#endif
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci/*
5562306a36Sopenharmony_ci * account_cpu_user_entry/exit runs "unreconciled", so can't trace,
5662306a36Sopenharmony_ci * can't use get_paca()
5762306a36Sopenharmony_ci */
5862306a36Sopenharmony_cistatic notrace inline void account_cpu_user_entry(void)
5962306a36Sopenharmony_ci{
6062306a36Sopenharmony_ci	unsigned long tb = mftb();
6162306a36Sopenharmony_ci	struct cpu_accounting_data *acct = raw_get_accounting(current);
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	acct->utime += (tb - acct->starttime_user);
6462306a36Sopenharmony_ci	acct->starttime = tb;
6562306a36Sopenharmony_ci}
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_cistatic notrace inline void account_cpu_user_exit(void)
6862306a36Sopenharmony_ci{
6962306a36Sopenharmony_ci	unsigned long tb = mftb();
7062306a36Sopenharmony_ci	struct cpu_accounting_data *acct = raw_get_accounting(current);
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	acct->stime += (tb - acct->starttime);
7362306a36Sopenharmony_ci	acct->starttime_user = tb;
7462306a36Sopenharmony_ci}
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_cistatic notrace inline void account_stolen_time(void)
7762306a36Sopenharmony_ci{
7862306a36Sopenharmony_ci#ifdef CONFIG_PPC_SPLPAR
7962306a36Sopenharmony_ci	if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
8062306a36Sopenharmony_ci		struct lppaca *lp = local_paca->lppaca_ptr;
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci		if (unlikely(local_paca->dtl_ridx != be64_to_cpu(lp->dtl_idx)))
8362306a36Sopenharmony_ci			pseries_accumulate_stolen_time();
8462306a36Sopenharmony_ci	}
8562306a36Sopenharmony_ci#endif
8662306a36Sopenharmony_ci}
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci#endif /* __KERNEL__ */
8962306a36Sopenharmony_ci#else /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
9062306a36Sopenharmony_cistatic inline void account_cpu_user_entry(void)
9162306a36Sopenharmony_ci{
9262306a36Sopenharmony_ci}
9362306a36Sopenharmony_cistatic inline void account_cpu_user_exit(void)
9462306a36Sopenharmony_ci{
9562306a36Sopenharmony_ci}
9662306a36Sopenharmony_cistatic notrace inline void account_stolen_time(void)
9762306a36Sopenharmony_ci{
9862306a36Sopenharmony_ci}
9962306a36Sopenharmony_ci#endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
10062306a36Sopenharmony_ci#endif /* __POWERPC_CPUTIME_H */
101