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