xref: /OK3568_Linux_fs/kernel/drivers/gpu/arm/bifrost/mali_kbase_ccswe.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /*
3  *
4  * (C) COPYRIGHT 2020-2022 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_CCSWE_H_
23 #define _KBASE_CCSWE_H_
24 
25 #include <linux/spinlock.h>
26 
27 /**
28  * struct kbase_ccswe - Cycle count software estimator.
29  *
30  * @access:         Spinlock protecting this structure access.
31  * @timestamp_ns:   Timestamp(ns) when the last frequency change
32  *                  occurred.
33  * @cycles_elapsed: Number of cycles elapsed before the last frequency
34  *                  change
35  * @gpu_freq:       Current GPU frequency(Hz) value.
36  * @prev_gpu_freq:  Previous GPU frequency(Hz) before the last frequency
37  *                  change.
38  */
39 struct kbase_ccswe {
40 	spinlock_t access;
41 	u64 timestamp_ns;
42 	u64 cycles_elapsed;
43 	u32 gpu_freq;
44 	u32 prev_gpu_freq;
45 };
46 
47 /**
48  * kbase_ccswe_init() - initialize the cycle count estimator.
49  *
50  * @self: Cycles count software estimator instance.
51  */
52 void kbase_ccswe_init(struct kbase_ccswe *self);
53 
54 /**
55  * kbase_ccswe_cycle_at() - Estimate cycle count at given timestamp.
56  *
57  * @self: Cycles count software estimator instance.
58  * @timestamp_ns: The timestamp(ns) for cycle count estimation.
59  *
60  * The timestamp must be bigger than the timestamp of the penultimate
61  * frequency change. If only one frequency change occurred, the
62  * timestamp must be bigger than the timestamp of the frequency change.
63  * This is to allow the following code to be executed w/o synchronization.
64  * If lines below executed atomically, it is safe to assume that only
65  * one frequency change may happen in between.
66  *
67  *     u64 ts = ktime_get_raw_ns();
68  *     u64 cycle = kbase_ccswe_cycle_at(&ccswe, ts)
69  *
70  * Return: estimated value of cycle count at a given time.
71  */
72 u64 kbase_ccswe_cycle_at(struct kbase_ccswe *self, u64 timestamp_ns);
73 
74 /**
75  * kbase_ccswe_freq_change() - update GPU frequency.
76  *
77  * @self:         Cycles count software estimator instance.
78  * @timestamp_ns: Timestamp(ns) when frequency change occurred.
79  * @gpu_freq:     New GPU frequency value.
80  *
81  * The timestamp must be bigger than the timestamp of the previous
82  * frequency change. The function is to be called at the frequency
83  * change moment (not later).
84  */
85 void kbase_ccswe_freq_change(
86 	struct kbase_ccswe *self, u64 timestamp_ns, u32 gpu_freq);
87 
88 /**
89  * kbase_ccswe_reset() - reset estimator state
90  *
91  * @self:    Cycles count software estimator instance.
92  */
93 void kbase_ccswe_reset(struct kbase_ccswe *self);
94 
95 #endif /* _KBASE_CCSWE_H_ */
96