gpu: update midgard r21p0 kernel driver
[GitHub/LineageOS/G12/android_hardware_amlogic_kernel-modules_mali-driver.git] / t83x / kernel / drivers / gpu / arm / midgard / backend / gpu / mali_kbase_pm_defs.h
1 /*
2 *
3 * (C) COPYRIGHT 2014-2017 ARM Limited. All rights reserved.
4 *
5 * This program is free software and is provided to you under the terms of the
6 * GNU General Public License version 2 as published by the Free Software
7 * Foundation, and any use by you of this program is subject to the terms
8 * of such GNU licence.
9 *
10 * A copy of the licence is included with the program, and can also be obtained
11 * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
12 * Boston, MA 02110-1301, USA.
13 *
14 */
15
16
17
18 /*
19 * Backend-specific Power Manager definitions
20 */
21
22 #ifndef _KBASE_PM_HWACCESS_DEFS_H_
23 #define _KBASE_PM_HWACCESS_DEFS_H_
24
25 #include "mali_kbase_pm_ca_fixed.h"
26 #include "mali_kbase_pm_ca_devfreq.h"
27 #if !MALI_CUSTOMER_RELEASE
28 #include "mali_kbase_pm_ca_random.h"
29 #include "mali_kbase_pm_ca_demand.h"
30 #endif
31
32 #include "mali_kbase_pm_always_on.h"
33 #include "mali_kbase_pm_coarse_demand.h"
34 #include "mali_kbase_pm_demand.h"
35 #if !MALI_CUSTOMER_RELEASE
36 #include "mali_kbase_pm_demand_always_powered.h"
37 #include "mali_kbase_pm_fast_start.h"
38 #endif
39
40 /* Forward definition - see mali_kbase.h */
41 struct kbase_device;
42 struct kbase_jd_atom;
43
44 /**
45 * enum kbase_pm_core_type - The types of core in a GPU.
46 *
47 * These enumerated values are used in calls to
48 * - kbase_pm_get_present_cores()
49 * - kbase_pm_get_active_cores()
50 * - kbase_pm_get_trans_cores()
51 * - kbase_pm_get_ready_cores().
52 *
53 * They specify which type of core should be acted on. These values are set in
54 * a manner that allows core_type_to_reg() function to be simpler and more
55 * efficient.
56 *
57 * @KBASE_PM_CORE_L2: The L2 cache
58 * @KBASE_PM_CORE_SHADER: Shader cores
59 * @KBASE_PM_CORE_TILER: Tiler cores
60 * @KBASE_PM_CORE_STACK: Core stacks
61 */
62 enum kbase_pm_core_type {
63 KBASE_PM_CORE_L2 = L2_PRESENT_LO,
64 KBASE_PM_CORE_SHADER = SHADER_PRESENT_LO,
65 KBASE_PM_CORE_TILER = TILER_PRESENT_LO,
66 KBASE_PM_CORE_STACK = STACK_PRESENT_LO
67 };
68
69 /**
70 * struct kbasep_pm_metrics_data - Metrics data collected for use by the power
71 * management framework.
72 *
73 * @time_period_start: time at which busy/idle measurements started
74 * @time_busy: number of ns the GPU was busy executing jobs since the
75 * @time_period_start timestamp.
76 * @time_idle: number of ns since time_period_start the GPU was not executing
77 * jobs since the @time_period_start timestamp.
78 * @prev_busy: busy time in ns of previous time period.
79 * Updated when metrics are reset.
80 * @prev_idle: idle time in ns of previous time period
81 * Updated when metrics are reset.
82 * @gpu_active: true when the GPU is executing jobs. false when
83 * not. Updated when the job scheduler informs us a job in submitted
84 * or removed from a GPU slot.
85 * @busy_cl: number of ns the GPU was busy executing CL jobs. Note that
86 * if two CL jobs were active for 400ns, this value would be updated
87 * with 800.
88 * @busy_gl: number of ns the GPU was busy executing GL jobs. Note that
89 * if two GL jobs were active for 400ns, this value would be updated
90 * with 800.
91 * @active_cl_ctx: number of CL jobs active on the GPU. Array is per-device.
92 * @active_gl_ctx: number of GL jobs active on the GPU. Array is per-slot. As
93 * GL jobs never run on slot 2 this slot is not recorded.
94 * @lock: spinlock protecting the kbasep_pm_metrics_data structure
95 * @timer: timer to regularly make DVFS decisions based on the power
96 * management metrics.
97 * @timer_active: boolean indicating @timer is running
98 * @platform_data: pointer to data controlled by platform specific code
99 * @kbdev: pointer to kbase device for which metrics are collected
100 *
101 */
102 struct kbasep_pm_metrics_data {
103 ktime_t time_period_start;
104 u32 time_busy;
105 u32 time_idle;
106 u32 prev_busy;
107 u32 prev_idle;
108 bool gpu_active;
109 u32 busy_cl[2];
110 u32 busy_gl;
111 u32 active_cl_ctx[2];
112 u32 active_gl_ctx[2]; /* GL jobs can only run on 2 of the 3 job slots */
113 spinlock_t lock;
114
115 #ifdef CONFIG_MALI_MIDGARD_DVFS
116 struct hrtimer timer;
117 bool timer_active;
118 #endif
119
120 void *platform_data;
121 struct kbase_device *kbdev;
122 };
123
124 union kbase_pm_policy_data {
125 struct kbasep_pm_policy_always_on always_on;
126 struct kbasep_pm_policy_coarse_demand coarse_demand;
127 struct kbasep_pm_policy_demand demand;
128 #if !MALI_CUSTOMER_RELEASE
129 struct kbasep_pm_policy_demand_always_powered demand_always_powered;
130 struct kbasep_pm_policy_fast_start fast_start;
131 #endif
132 };
133
134 union kbase_pm_ca_policy_data {
135 struct kbasep_pm_ca_policy_fixed fixed;
136 struct kbasep_pm_ca_policy_devfreq devfreq;
137 #if !MALI_CUSTOMER_RELEASE
138 struct kbasep_pm_ca_policy_random random;
139 struct kbasep_pm_ca_policy_demand demand;
140 #endif
141 };
142
143 /**
144 * struct kbase_pm_backend_data - Data stored per device for power management.
145 *
146 * This structure contains data for the power management framework. There is one
147 * instance of this structure per device in the system.
148 *
149 * @ca_current_policy: The policy that is currently actively controlling core
150 * availability.
151 * @pm_current_policy: The policy that is currently actively controlling the
152 * power state.
153 * @ca_policy_data: Private data for current CA policy
154 * @pm_policy_data: Private data for current PM policy
155 * @ca_in_transition: Flag indicating when core availability policy is
156 * transitioning cores. The core availability policy must
157 * set this when a change in core availability is occurring.
158 * power_change_lock must be held when accessing this.
159 * @reset_done: Flag when a reset is complete
160 * @reset_done_wait: Wait queue to wait for changes to @reset_done
161 * @l2_powered_wait: Wait queue for whether the l2 cache has been powered as
162 * requested
163 * @l2_powered: State indicating whether all the l2 caches are powered.
164 * Non-zero indicates they're *all* powered
165 * Zero indicates that some (or all) are not powered
166 * @gpu_cycle_counter_requests: The reference count of active gpu cycle counter
167 * users
168 * @gpu_cycle_counter_requests_lock: Lock to protect @gpu_cycle_counter_requests
169 * @desired_shader_state: A bit mask identifying the shader cores that the
170 * power policy would like to be on. The current state
171 * of the cores may be different, but there should be
172 * transitions in progress that will eventually achieve
173 * this state (assuming that the policy doesn't change
174 * its mind in the mean time).
175 * @powering_on_shader_state: A bit mask indicating which shader cores are
176 * currently in a power-on transition
177 * @desired_tiler_state: A bit mask identifying the tiler cores that the power
178 * policy would like to be on. See @desired_shader_state
179 * @powering_on_tiler_state: A bit mask indicating which tiler core are
180 * currently in a power-on transition
181 * @powering_on_l2_state: A bit mask indicating which l2-caches are currently
182 * in a power-on transition
183 * @powering_on_stack_state: A bit mask indicating which core stacks are
184 * currently in a power-on transition
185 * @gpu_in_desired_state: This flag is set if the GPU is powered as requested
186 * by the desired_xxx_state variables
187 * @gpu_in_desired_state_wait: Wait queue set when @gpu_in_desired_state != 0
188 * @gpu_powered: Set to true when the GPU is powered and register
189 * accesses are possible, false otherwise
190 * @instr_enabled: Set to true when instrumentation is enabled,
191 * false otherwise
192 * @cg1_disabled: Set if the policy wants to keep the second core group
193 * powered off
194 * @driver_ready_for_irqs: Debug state indicating whether sufficient
195 * initialization of the driver has occurred to handle
196 * IRQs
197 * @gpu_powered_lock: Spinlock that must be held when writing @gpu_powered or
198 * accessing @driver_ready_for_irqs
199 * @metrics: Structure to hold metrics for the GPU
200 * @gpu_poweroff_pending: number of poweroff timer ticks until the GPU is
201 * powered off
202 * @shader_poweroff_pending_time: number of poweroff timer ticks until shaders
203 * and/or timers are powered off
204 * @gpu_poweroff_timer: Timer for powering off GPU
205 * @gpu_poweroff_wq: Workqueue to power off GPU on when timer fires
206 * @gpu_poweroff_work: Workitem used on @gpu_poweroff_wq
207 * @shader_poweroff_pending: Bit mask of shaders to be powered off on next
208 * timer callback
209 * @tiler_poweroff_pending: Bit mask of tilers to be powered off on next timer
210 * callback
211 * @poweroff_timer_needed: true if the poweroff timer is currently required,
212 * false otherwise
213 * @poweroff_timer_running: true if the poweroff timer is currently running,
214 * false otherwise
215 * power_change_lock should be held when accessing,
216 * unless there is no way the timer can be running (eg
217 * hrtimer_cancel() was called immediately before)
218 * @poweroff_wait_in_progress: true if a wait for GPU power off is in progress.
219 * hwaccess_lock must be held when accessing
220 * @poweron_required: true if a GPU power on is required. Should only be set
221 * when poweroff_wait_in_progress is true, and therefore the
222 * GPU can not immediately be powered on. pm.lock must be
223 * held when accessing
224 * @poweroff_is_suspend: true if the GPU is being powered off due to a suspend
225 * request. pm.lock must be held when accessing
226 * @gpu_poweroff_wait_wq: workqueue for waiting for GPU to power off
227 * @gpu_poweroff_wait_work: work item for use with @gpu_poweroff_wait_wq
228 * @poweroff_wait: waitqueue for waiting for @gpu_poweroff_wait_work to complete
229 * @callback_power_on: Callback when the GPU needs to be turned on. See
230 * &struct kbase_pm_callback_conf
231 * @callback_power_off: Callback when the GPU may be turned off. See
232 * &struct kbase_pm_callback_conf
233 * @callback_power_suspend: Callback when a suspend occurs and the GPU needs to
234 * be turned off. See &struct kbase_pm_callback_conf
235 * @callback_power_resume: Callback when a resume occurs and the GPU needs to
236 * be turned on. See &struct kbase_pm_callback_conf
237 * @callback_power_runtime_on: Callback when the GPU needs to be turned on. See
238 * &struct kbase_pm_callback_conf
239 * @callback_power_runtime_off: Callback when the GPU may be turned off. See
240 * &struct kbase_pm_callback_conf
241 * @callback_power_runtime_idle: Optional callback when the GPU may be idle. See
242 * &struct kbase_pm_callback_conf
243 *
244 * Note:
245 * During an IRQ, @ca_current_policy or @pm_current_policy can be NULL when the
246 * policy is being changed with kbase_pm_ca_set_policy() or
247 * kbase_pm_set_policy(). The change is protected under
248 * kbase_device.pm.power_change_lock. Direct access to this
249 * from IRQ context must therefore check for NULL. If NULL, then
250 * kbase_pm_ca_set_policy() or kbase_pm_set_policy() will re-issue the policy
251 * functions that would have been done under IRQ.
252 */
253 struct kbase_pm_backend_data {
254 const struct kbase_pm_ca_policy *ca_current_policy;
255 const struct kbase_pm_policy *pm_current_policy;
256 union kbase_pm_ca_policy_data ca_policy_data;
257 union kbase_pm_policy_data pm_policy_data;
258 bool ca_in_transition;
259 bool reset_done;
260 wait_queue_head_t reset_done_wait;
261 wait_queue_head_t l2_powered_wait;
262 int l2_powered;
263 int gpu_cycle_counter_requests;
264 spinlock_t gpu_cycle_counter_requests_lock;
265
266 u64 desired_shader_state;
267 u64 powering_on_shader_state;
268 u64 desired_tiler_state;
269 u64 powering_on_tiler_state;
270 u64 powering_on_l2_state;
271 #ifdef CONFIG_MALI_CORESTACK
272 u64 powering_on_stack_state;
273 #endif /* CONFIG_MALI_CORESTACK */
274
275 bool gpu_in_desired_state;
276 wait_queue_head_t gpu_in_desired_state_wait;
277
278 bool gpu_powered;
279
280 bool instr_enabled;
281
282 bool cg1_disabled;
283
284 #ifdef CONFIG_MALI_DEBUG
285 bool driver_ready_for_irqs;
286 #endif /* CONFIG_MALI_DEBUG */
287
288 spinlock_t gpu_powered_lock;
289
290
291 struct kbasep_pm_metrics_data metrics;
292
293 int gpu_poweroff_pending;
294 int shader_poweroff_pending_time;
295
296 struct hrtimer gpu_poweroff_timer;
297 struct workqueue_struct *gpu_poweroff_wq;
298 struct work_struct gpu_poweroff_work;
299
300 u64 shader_poweroff_pending;
301 u64 tiler_poweroff_pending;
302
303 bool poweroff_timer_needed;
304 bool poweroff_timer_running;
305
306 bool poweroff_wait_in_progress;
307 bool poweron_required;
308 bool poweroff_is_suspend;
309
310 struct workqueue_struct *gpu_poweroff_wait_wq;
311 struct work_struct gpu_poweroff_wait_work;
312
313 wait_queue_head_t poweroff_wait;
314
315 int (*callback_power_on)(struct kbase_device *kbdev);
316 void (*callback_power_off)(struct kbase_device *kbdev);
317 void (*callback_power_suspend)(struct kbase_device *kbdev);
318 void (*callback_power_resume)(struct kbase_device *kbdev);
319 int (*callback_power_runtime_on)(struct kbase_device *kbdev);
320 void (*callback_power_runtime_off)(struct kbase_device *kbdev);
321 int (*callback_power_runtime_idle)(struct kbase_device *kbdev);
322 };
323
324
325 /* List of policy IDs */
326 enum kbase_pm_policy_id {
327 KBASE_PM_POLICY_ID_DEMAND = 1,
328 KBASE_PM_POLICY_ID_ALWAYS_ON,
329 KBASE_PM_POLICY_ID_COARSE_DEMAND,
330 #if !MALI_CUSTOMER_RELEASE
331 KBASE_PM_POLICY_ID_DEMAND_ALWAYS_POWERED,
332 KBASE_PM_POLICY_ID_FAST_START
333 #endif
334 };
335
336 typedef u32 kbase_pm_policy_flags;
337
338 /**
339 * struct kbase_pm_policy - Power policy structure.
340 *
341 * Each power policy exposes a (static) instance of this structure which
342 * contains function pointers to the policy's methods.
343 *
344 * @name: The name of this policy
345 * @init: Function called when the policy is selected
346 * @term: Function called when the policy is unselected
347 * @get_core_mask: Function called to get the current shader core mask
348 * @get_core_active: Function called to get the current overall GPU power
349 * state
350 * @flags: Field indicating flags for this policy
351 * @id: Field indicating an ID for this policy. This is not
352 * necessarily the same as its index in the list returned
353 * by kbase_pm_list_policies().
354 * It is used purely for debugging.
355 */
356 struct kbase_pm_policy {
357 char *name;
358
359 /**
360 * Function called when the policy is selected
361 *
362 * This should initialize the kbdev->pm.pm_policy_data structure. It
363 * should not attempt to make any changes to hardware state.
364 *
365 * It is undefined what state the cores are in when the function is
366 * called.
367 *
368 * @kbdev: The kbase device structure for the device (must be a
369 * valid pointer)
370 */
371 void (*init)(struct kbase_device *kbdev);
372
373 /**
374 * Function called when the policy is unselected.
375 *
376 * @kbdev: The kbase device structure for the device (must be a
377 * valid pointer)
378 */
379 void (*term)(struct kbase_device *kbdev);
380
381 /**
382 * Function called to get the current shader core mask
383 *
384 * The returned mask should meet or exceed (kbdev->shader_needed_bitmap
385 * | kbdev->shader_inuse_bitmap).
386 *
387 * @kbdev: The kbase device structure for the device (must be a
388 * valid pointer)
389 *
390 * Return: The mask of shader cores to be powered
391 */
392 u64 (*get_core_mask)(struct kbase_device *kbdev);
393
394 /**
395 * Function called to get the current overall GPU power state
396 *
397 * This function should consider the state of kbdev->pm.active_count. If
398 * this count is greater than 0 then there is at least one active
399 * context on the device and the GPU should be powered. If it is equal
400 * to 0 then there are no active contexts and the GPU could be powered
401 * off if desired.
402 *
403 * @kbdev: The kbase device structure for the device (must be a
404 * valid pointer)
405 *
406 * Return: true if the GPU should be powered, false otherwise
407 */
408 bool (*get_core_active)(struct kbase_device *kbdev);
409
410 kbase_pm_policy_flags flags;
411 enum kbase_pm_policy_id id;
412 };
413
414
415 enum kbase_pm_ca_policy_id {
416 KBASE_PM_CA_POLICY_ID_FIXED = 1,
417 KBASE_PM_CA_POLICY_ID_DEVFREQ,
418 KBASE_PM_CA_POLICY_ID_RANDOM
419 };
420
421 typedef u32 kbase_pm_ca_policy_flags;
422
423 /**
424 * Maximum length of a CA policy names
425 */
426 #define KBASE_PM_CA_MAX_POLICY_NAME_LEN 15
427
428 /**
429 * struct kbase_pm_ca_policy - Core availability policy structure.
430 *
431 * Each core availability policy exposes a (static) instance of this structure
432 * which contains function pointers to the policy's methods.
433 *
434 * @name: The name of this policy
435 * @init: Function called when the policy is selected
436 * @term: Function called when the policy is unselected
437 * @get_core_mask: Function called to get the current shader core
438 * availability mask
439 * @update_core_status: Function called to update the current core status
440 * @flags: Field indicating flags for this policy
441 * @id: Field indicating an ID for this policy. This is not
442 * necessarily the same as its index in the list returned
443 * by kbase_pm_list_policies().
444 * It is used purely for debugging.
445 */
446 struct kbase_pm_ca_policy {
447 char name[KBASE_PM_CA_MAX_POLICY_NAME_LEN + 1];
448
449 /**
450 * Function called when the policy is selected
451 *
452 * This should initialize the kbdev->pm.ca_policy_data structure. It
453 * should not attempt to make any changes to hardware state.
454 *
455 * It is undefined what state the cores are in when the function is
456 * called.
457 *
458 * @kbdev The kbase device structure for the device (must be a
459 * valid pointer)
460 */
461 void (*init)(struct kbase_device *kbdev);
462
463 /**
464 * Function called when the policy is unselected.
465 *
466 * @kbdev The kbase device structure for the device (must be a
467 * valid pointer)
468 */
469 void (*term)(struct kbase_device *kbdev);
470
471 /**
472 * Function called to get the current shader core availability mask
473 *
474 * When a change in core availability is occurring, the policy must set
475 * kbdev->pm.ca_in_transition to true. This is to indicate that
476 * reporting changes in power state cannot be optimized out, even if
477 * kbdev->pm.desired_shader_state remains unchanged. This must be done
478 * by any functions internal to the Core Availability Policy that change
479 * the return value of kbase_pm_ca_policy::get_core_mask.
480 *
481 * @kbdev The kbase device structure for the device (must be a
482 * valid pointer)
483 *
484 * Return: The current core availability mask
485 */
486 u64 (*get_core_mask)(struct kbase_device *kbdev);
487
488 /**
489 * Function called to update the current core status
490 *
491 * If none of the cores in core group 0 are ready or transitioning, then
492 * the policy must ensure that the next call to get_core_mask does not
493 * return 0 for all cores in core group 0. It is an error to disable
494 * core group 0 through the core availability policy.
495 *
496 * When a change in core availability has finished, the policy must set
497 * kbdev->pm.ca_in_transition to false. This is to indicate that
498 * changes in power state can once again be optimized out when
499 * kbdev->pm.desired_shader_state is unchanged.
500 *
501 * @kbdev: The kbase device structure for the device
502 * (must be a valid pointer)
503 * @cores_ready: The mask of cores currently powered and
504 * ready to run jobs
505 * @cores_transitioning: The mask of cores currently transitioning
506 * power state
507 */
508 void (*update_core_status)(struct kbase_device *kbdev, u64 cores_ready,
509 u64 cores_transitioning);
510
511 kbase_pm_ca_policy_flags flags;
512
513 /**
514 * Field indicating an ID for this policy. This is not necessarily the
515 * same as its index in the list returned by kbase_pm_list_policies().
516 * It is used purely for debugging.
517 */
518 enum kbase_pm_ca_policy_id id;
519 };
520
521 #endif /* _KBASE_PM_HWACCESS_DEFS_H_ */