xref: /OK3568_Linux_fs/kernel/drivers/gpu/arm/bifrost/mali_kbase_hwaccess_time.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /*
3  *
4  * (C) COPYRIGHT 2014, 2018-2021, 2023 ARM Limited. All rights reserved.
5  *
6  * This program is free software and is provided to you under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation, and any use by you of this program is subject to the terms
9  * of such GNU license.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you can access it online at
18  * http://www.gnu.org/licenses/gpl-2.0.html.
19  *
20  */
21 
22 #ifndef _KBASE_BACKEND_TIME_H_
23 #define _KBASE_BACKEND_TIME_H_
24 
25 #if MALI_USE_CSF
26 /**
27  * struct kbase_backend_time - System timestamp attributes.
28  *
29  * @multiplier:		Numerator of the converter's fraction.
30  * @divisor:		Denominator of the converter's fraction.
31  * @offset:		Converter's offset term.
32  *
33  * According to Generic timer spec, system timer:
34  * - Increments at a fixed frequency
35  * - Starts operating from zero
36  *
37  * Hence CPU time is a linear function of System Time.
38  *
39  * CPU_ts = alpha * SYS_ts + beta
40  *
41  * Where
42  * - alpha = 10^9/SYS_ts_freq
43  * - beta is calculated by two timer samples taken at the same time:
44  *   beta = CPU_ts_s - SYS_ts_s * alpha
45  *
46  * Since alpha is a rational number, we minimizing possible
47  * rounding error by simplifying the ratio. Thus alpha is stored
48  * as a simple `multiplier / divisor` ratio.
49  *
50  */
51 struct kbase_backend_time {
52 	u64 multiplier;
53 	u64 divisor;
54 	s64 offset;
55 };
56 
57 /**
58  * kbase_backend_time_convert_gpu_to_cpu() - Convert GPU timestamp to CPU timestamp.
59  *
60  * @kbdev:	Kbase device pointer
61  * @gpu_ts:	System timestamp value to converter.
62  *
63  * Return: The CPU timestamp.
64  */
65 u64 __maybe_unused kbase_backend_time_convert_gpu_to_cpu(struct kbase_device *kbdev, u64 gpu_ts);
66 #endif
67 
68 /**
69  * kbase_backend_get_gpu_time() - Get current GPU time
70  * @kbdev:              Device pointer
71  * @cycle_counter:      Pointer to u64 to store cycle counter in.
72  * @system_time:        Pointer to u64 to store system time in
73  * @ts:                 Pointer to struct timespec to store current monotonic
74  *			time in
75  */
76 void kbase_backend_get_gpu_time(struct kbase_device *kbdev, u64 *cycle_counter,
77 				u64 *system_time, struct timespec64 *ts);
78 
79 /**
80  * kbase_backend_get_gpu_time_norequest() - Get current GPU time without
81  *                                          request/release cycle counter
82  * @kbdev:		Device pointer
83  * @cycle_counter:	Pointer to u64 to store cycle counter in
84  * @system_time:	Pointer to u64 to store system time in
85  * @ts:			Pointer to struct timespec to store current monotonic
86  *			time in
87  */
88 void kbase_backend_get_gpu_time_norequest(struct kbase_device *kbdev,
89 					  u64 *cycle_counter,
90 					  u64 *system_time,
91 					  struct timespec64 *ts);
92 /**
93  * kbase_get_timeout_ms - Choose a timeout value to get a timeout scaled
94  *                        GPU frequency, using a choice from
95  *                        kbase_timeout_selector.
96  *
97  * @kbdev:	KBase device pointer.
98  * @selector:	Value from kbase_scaled_timeout_selector enum.
99  *
100  * Return:	Timeout in milliseconds, as an unsigned integer.
101  */
102 unsigned int kbase_get_timeout_ms(struct kbase_device *kbdev,
103 				  enum kbase_timeout_selector selector);
104 
105 /**
106  * kbase_backend_get_cycle_cnt - Reads the GPU cycle counter
107  *
108  * @kbdev: Instance of a GPU platform device that implements a CSF interface.
109  *
110  * Return: Snapshot of the GPU cycle count register.
111  */
112 u64 kbase_backend_get_cycle_cnt(struct kbase_device *kbdev);
113 
114 /**
115  * kbase_backend_time_init() - Initialize system timestamp converter.
116  *
117  * @kbdev:	Kbase device pointer
118  *
119  * This function should only be called after GPU is powered-up and
120  * L2 cached power-up has been initiated.
121  *
122  * Return: Zero on success, error code otherwise.
123  */
124 int kbase_backend_time_init(struct kbase_device *kbdev);
125 
126 #endif /* _KBASE_BACKEND_TIME_H_ */
127