xref: /OK3568_Linux_fs/kernel/drivers/gpu/arm/bifrost/ipa/backend/mali_kbase_ipa_counter_common_jm.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /*
3  *
4  * (C) COPYRIGHT 2017-2018, 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_IPA_COUNTER_COMMON_JM_H_
23 #define _KBASE_IPA_COUNTER_COMMON_JM_H_
24 
25 #include "mali_kbase.h"
26 #include "hwcnt/mali_kbase_hwcnt_virtualizer.h"
27 #include "hwcnt/mali_kbase_hwcnt_types.h"
28 
29 /* Maximum number of IPA groups for an IPA model. */
30 #define KBASE_IPA_MAX_GROUP_DEF_NUM  16
31 
32 /* Number of bytes per hardware counter in a vinstr_buffer. */
33 #define KBASE_IPA_NR_BYTES_PER_CNT (sizeof(u64))
34 
35 /* Number of hardware counters per block in a vinstr_buffer. */
36 #define KBASE_IPA_NR_CNT_PER_BLOCK   64
37 
38 /* Number of bytes per block in a vinstr_buffer. */
39 #define KBASE_IPA_NR_BYTES_PER_BLOCK \
40 	(KBASE_IPA_NR_CNT_PER_BLOCK * KBASE_IPA_NR_BYTES_PER_CNT)
41 
42 struct kbase_ipa_model_vinstr_data;
43 
44 typedef u32
45 kbase_ipa_get_active_cycles_callback(struct kbase_ipa_model_vinstr_data *);
46 
47 /**
48  * struct kbase_ipa_model_vinstr_data - IPA context per device
49  * @kbdev:               pointer to kbase device
50  * @group_values:        values of coefficients for IPA groups
51  * @groups_def:          Array of IPA groups.
52  * @groups_def_num:      Number of elements in the array of IPA groups.
53  * @get_active_cycles:   Callback to return number of active cycles during
54  *                       counter sample period
55  * @hvirt_cli:           hardware counter virtualizer client handle
56  * @dump_buf:            buffer to dump hardware counters onto
57  * @reference_voltage:   voltage, in mV, of the operating point used when
58  *                       deriving the power model coefficients. Range approx
59  *                       0.1V - 5V (~= 8V): 2^7 <= reference_voltage <= 2^13
60  * @scaling_factor:      User-specified power scaling factor. This is an
61  *                       integer, which is multiplied by the power coefficient
62  *                       just before OPP scaling.
63  *                       Range approx 0-32: 0 < scaling_factor < 2^5
64  * @min_sample_cycles:   If the value of the GPU_ACTIVE counter (the number of
65  *                       cycles the GPU was working) is less than
66  *                       min_sample_cycles, the counter model will return an
67  *                       error, causing the IPA framework to approximate using
68  *                       the cached simple model results instead. This may be
69  *                       more accurate than extrapolating  using a very small
70  *                       counter dump.
71  */
72 struct kbase_ipa_model_vinstr_data {
73 	struct kbase_device *kbdev;
74 	s32 group_values[KBASE_IPA_MAX_GROUP_DEF_NUM];
75 	const struct kbase_ipa_group *groups_def;
76 	size_t groups_def_num;
77 	kbase_ipa_get_active_cycles_callback *get_active_cycles;
78 	struct kbase_hwcnt_virtualizer_client *hvirt_cli;
79 	struct kbase_hwcnt_dump_buffer dump_buf;
80 	s32 reference_voltage;
81 	s32 scaling_factor;
82 	s32 min_sample_cycles;
83 };
84 
85 /**
86  * struct kbase_ipa_group - represents a single IPA group
87  * @name:               name of the IPA group
88  * @default_value:      default value of coefficient for IPA group.
89  *                      Coefficients are interpreted as fractions where the
90  *                      denominator is 1000000.
91  * @op:                 which operation to be performed on the counter values
92  * @counter_block_offset:  block offset in bytes of the counter used to calculate energy for IPA group
93  */
94 struct kbase_ipa_group {
95 	const char *name;
96 	s32 default_value;
97 	s64 (*op)(
98 		struct kbase_ipa_model_vinstr_data *model_data,
99 		s32 coeff,
100 		u32 counter_block_offset);
101 	u32 counter_block_offset;
102 };
103 
104 /**
105  * kbase_ipa_sum_all_shader_cores() - sum a counter over all cores
106  * @model_data:		pointer to model data
107  * @coeff:		model coefficient. Unity is ~2^20, so range approx
108  *			+/- 4.0: -2^22 < coeff < 2^22
109  * @counter:		offset in bytes of the counter used to calculate energy
110  *			for IPA group
111  *
112  * Calculate energy estimation based on hardware counter `counter'
113  * across all shader cores.
114  *
115  * Return: Sum of counter values. Range: -2^54 < ret < 2^54
116  */
117 s64 kbase_ipa_sum_all_shader_cores(
118 	struct kbase_ipa_model_vinstr_data *model_data,
119 	s32 coeff, u32 counter);
120 
121 /**
122  * kbase_ipa_sum_all_memsys_blocks() - sum a counter over all mem system blocks
123  * @model_data:		pointer to model data
124  * @coeff:		model coefficient. Unity is ~2^20, so range approx
125  *			+/- 4.0: -2^22 < coeff < 2^22
126  * @counter:		offset in bytes of the counter used to calculate energy
127  *			for IPA group
128  *
129  * Calculate energy estimation based on hardware counter `counter' across all
130  * memory system blocks.
131  *
132  * Return: Sum of counter values. Range: -2^51 < ret < 2^51
133  */
134 s64 kbase_ipa_sum_all_memsys_blocks(
135 	struct kbase_ipa_model_vinstr_data *model_data,
136 	s32 coeff, u32 counter);
137 
138 /**
139  * kbase_ipa_single_counter() - sum a single counter
140  * @model_data:		pointer to model data
141  * @coeff:		model coefficient. Unity is ~2^20, so range approx
142  *			+/- 4.0: -2^22 < coeff < 2^22
143  * @counter:		offset in bytes of the counter used to calculate energy
144  *			for IPA group
145  *
146  * Calculate energy estimation based on hardware counter `counter'.
147  *
148  * Return: Counter value. Range: -2^49 < ret < 2^49
149  */
150 s64 kbase_ipa_single_counter(
151 	struct kbase_ipa_model_vinstr_data *model_data,
152 	s32 coeff, u32 counter);
153 
154 /**
155  * kbase_ipa_attach_vinstr() - attach a vinstr_buffer to an IPA model.
156  * @model_data:		pointer to model data
157  *
158  * Attach a vinstr_buffer to an IPA model. The vinstr_buffer
159  * allows access to the hardware counters used to calculate
160  * energy consumption.
161  *
162  * Return: 0 on success, or an error code.
163  */
164 int kbase_ipa_attach_vinstr(struct kbase_ipa_model_vinstr_data *model_data);
165 
166 /**
167  * kbase_ipa_detach_vinstr() - detach a vinstr_buffer from an IPA model.
168  * @model_data:		pointer to model data
169  *
170  * Detach a vinstr_buffer from an IPA model.
171  */
172 void kbase_ipa_detach_vinstr(struct kbase_ipa_model_vinstr_data *model_data);
173 
174 /**
175  * kbase_ipa_vinstr_dynamic_coeff() - calculate dynamic power based on HW counters
176  * @model:		pointer to instantiated model
177  * @coeffp:		pointer to location where calculated power, in
178  *			pW/(Hz V^2), is stored.
179  *
180  * This is a GPU-agnostic implementation of the get_dynamic_coeff()
181  * function of an IPA model. It relies on the model being populated
182  * with GPU-specific attributes at initialization time.
183  *
184  * Return: 0 on success, or an error code.
185  */
186 int kbase_ipa_vinstr_dynamic_coeff(struct kbase_ipa_model *model, u32 *coeffp);
187 
188 /**
189  * kbase_ipa_vinstr_reset_data() - Reset the counters data used for dynamic
190  *                                 power estimation
191  * @model:		pointer to instantiated model
192  *
193  * Currently it is not implemented for JM GPUs.
194  * When implemented it is expected to retrieve the accumulated value of HW
195  * counters from the Vinstr component, without doing any processing, which is
196  * effectively a reset as the next call to kbase_ipa_counter_dynamic_coeff()
197  * will see the increment in counter values from this point onwards.
198  */
199 void kbase_ipa_vinstr_reset_data(struct kbase_ipa_model *model);
200 
201 /**
202  * kbase_ipa_vinstr_common_model_init() - initialize ipa power model
203  * @model:		ipa power model to initialize
204  * @ipa_groups_def:	array of ipa groups which sets coefficients for
205  *			the corresponding counters used in the ipa model
206  * @ipa_group_size:     number of elements in the array @ipa_groups_def
207  * @get_active_cycles:  callback to return the number of cycles the GPU was
208  *			active during the counter sample period.
209  * @reference_voltage:  voltage, in mV, of the operating point used when
210  *                      deriving the power model coefficients.
211  *
212  * This initialization function performs initialization steps common
213  * for ipa models based on counter values. In each call, the model
214  * passes its specific coefficient values per ipa counter group via
215  * @ipa_groups_def array.
216  *
217  * Return: 0 on success, error code otherwise
218  */
219 int kbase_ipa_vinstr_common_model_init(struct kbase_ipa_model *model,
220 				       const struct kbase_ipa_group *ipa_groups_def,
221 				       size_t ipa_group_size,
222 				       kbase_ipa_get_active_cycles_callback *get_active_cycles,
223 				       s32 reference_voltage);
224 
225 /**
226  * kbase_ipa_vinstr_common_model_term() - terminate ipa power model
227  * @model: ipa power model to terminate
228  *
229  * This function performs all necessary steps to terminate ipa power model
230  * including clean up of resources allocated to hold model data.
231  */
232 void kbase_ipa_vinstr_common_model_term(struct kbase_ipa_model *model);
233 
234 #endif /* _KBASE_IPA_COUNTER_COMMON_JM_H_ */
235