xref: /OK3568_Linux_fs/kernel/drivers/gpu/arm/bifrost/mali_kbase_ccswe.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * (C) COPYRIGHT 2020-2022 ARM Limited. All rights reserved.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * This program is free software and is provided to you under the terms of the
7*4882a593Smuzhiyun  * GNU General Public License version 2 as published by the Free Software
8*4882a593Smuzhiyun  * Foundation, and any use by you of this program is subject to the terms
9*4882a593Smuzhiyun  * of such GNU license.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * This program is distributed in the hope that it will be useful,
12*4882a593Smuzhiyun  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*4882a593Smuzhiyun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*4882a593Smuzhiyun  * GNU General Public License for more details.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * You should have received a copy of the GNU General Public License
17*4882a593Smuzhiyun  * along with this program; if not, you can access it online at
18*4882a593Smuzhiyun  * http://www.gnu.org/licenses/gpl-2.0.html.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  */
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #ifndef _KBASE_CCSWE_H_
23*4882a593Smuzhiyun #define _KBASE_CCSWE_H_
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #include <linux/spinlock.h>
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun /**
28*4882a593Smuzhiyun  * struct kbase_ccswe - Cycle count software estimator.
29*4882a593Smuzhiyun  *
30*4882a593Smuzhiyun  * @access:         Spinlock protecting this structure access.
31*4882a593Smuzhiyun  * @timestamp_ns:   Timestamp(ns) when the last frequency change
32*4882a593Smuzhiyun  *                  occurred.
33*4882a593Smuzhiyun  * @cycles_elapsed: Number of cycles elapsed before the last frequency
34*4882a593Smuzhiyun  *                  change
35*4882a593Smuzhiyun  * @gpu_freq:       Current GPU frequency(Hz) value.
36*4882a593Smuzhiyun  * @prev_gpu_freq:  Previous GPU frequency(Hz) before the last frequency
37*4882a593Smuzhiyun  *                  change.
38*4882a593Smuzhiyun  */
39*4882a593Smuzhiyun struct kbase_ccswe {
40*4882a593Smuzhiyun 	spinlock_t access;
41*4882a593Smuzhiyun 	u64 timestamp_ns;
42*4882a593Smuzhiyun 	u64 cycles_elapsed;
43*4882a593Smuzhiyun 	u32 gpu_freq;
44*4882a593Smuzhiyun 	u32 prev_gpu_freq;
45*4882a593Smuzhiyun };
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun /**
48*4882a593Smuzhiyun  * kbase_ccswe_init() - initialize the cycle count estimator.
49*4882a593Smuzhiyun  *
50*4882a593Smuzhiyun  * @self: Cycles count software estimator instance.
51*4882a593Smuzhiyun  */
52*4882a593Smuzhiyun void kbase_ccswe_init(struct kbase_ccswe *self);
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun /**
55*4882a593Smuzhiyun  * kbase_ccswe_cycle_at() - Estimate cycle count at given timestamp.
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  * @self: Cycles count software estimator instance.
58*4882a593Smuzhiyun  * @timestamp_ns: The timestamp(ns) for cycle count estimation.
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  * The timestamp must be bigger than the timestamp of the penultimate
61*4882a593Smuzhiyun  * frequency change. If only one frequency change occurred, the
62*4882a593Smuzhiyun  * timestamp must be bigger than the timestamp of the frequency change.
63*4882a593Smuzhiyun  * This is to allow the following code to be executed w/o synchronization.
64*4882a593Smuzhiyun  * If lines below executed atomically, it is safe to assume that only
65*4882a593Smuzhiyun  * one frequency change may happen in between.
66*4882a593Smuzhiyun  *
67*4882a593Smuzhiyun  *     u64 ts = ktime_get_raw_ns();
68*4882a593Smuzhiyun  *     u64 cycle = kbase_ccswe_cycle_at(&ccswe, ts)
69*4882a593Smuzhiyun  *
70*4882a593Smuzhiyun  * Return: estimated value of cycle count at a given time.
71*4882a593Smuzhiyun  */
72*4882a593Smuzhiyun u64 kbase_ccswe_cycle_at(struct kbase_ccswe *self, u64 timestamp_ns);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun /**
75*4882a593Smuzhiyun  * kbase_ccswe_freq_change() - update GPU frequency.
76*4882a593Smuzhiyun  *
77*4882a593Smuzhiyun  * @self:         Cycles count software estimator instance.
78*4882a593Smuzhiyun  * @timestamp_ns: Timestamp(ns) when frequency change occurred.
79*4882a593Smuzhiyun  * @gpu_freq:     New GPU frequency value.
80*4882a593Smuzhiyun  *
81*4882a593Smuzhiyun  * The timestamp must be bigger than the timestamp of the previous
82*4882a593Smuzhiyun  * frequency change. The function is to be called at the frequency
83*4882a593Smuzhiyun  * change moment (not later).
84*4882a593Smuzhiyun  */
85*4882a593Smuzhiyun void kbase_ccswe_freq_change(
86*4882a593Smuzhiyun 	struct kbase_ccswe *self, u64 timestamp_ns, u32 gpu_freq);
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun /**
89*4882a593Smuzhiyun  * kbase_ccswe_reset() - reset estimator state
90*4882a593Smuzhiyun  *
91*4882a593Smuzhiyun  * @self:    Cycles count software estimator instance.
92*4882a593Smuzhiyun  */
93*4882a593Smuzhiyun void kbase_ccswe_reset(struct kbase_ccswe *self);
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun #endif /* _KBASE_CCSWE_H_ */
96