Revert "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  * @callback_power_on: Callback when the GPU needs to be turned on. See
211  *                     &struct kbase_pm_callback_conf
212  * @callback_power_off: Callback when the GPU may be turned off. See
213  *                     &struct kbase_pm_callback_conf
214  * @callback_power_suspend: Callback when a suspend occurs and the GPU needs to
215  *                          be turned off. See &struct kbase_pm_callback_conf
216  * @callback_power_resume: Callback when a resume occurs and the GPU needs to
217  *                          be turned on. See &struct kbase_pm_callback_conf
218  * @callback_power_runtime_on: Callback when the GPU needs to be turned on. See
219  *                             &struct kbase_pm_callback_conf
220  * @callback_power_runtime_off: Callback when the GPU may be turned off. See
221  *                              &struct kbase_pm_callback_conf
222  * @callback_power_runtime_idle: Optional callback when the GPU may be idle. See
223  *                              &struct kbase_pm_callback_conf
224  *
225  * Note:
226  * During an IRQ, @ca_current_policy or @pm_current_policy can be NULL when the
227  * policy is being changed with kbase_pm_ca_set_policy() or
228  * kbase_pm_set_policy(). The change is protected under
229  * kbase_device.pm.power_change_lock. Direct access to this
230  * from IRQ context must therefore check for NULL. If NULL, then
231  * kbase_pm_ca_set_policy() or kbase_pm_set_policy() will re-issue the policy
232  * functions that would have been done under IRQ.
233  */
234 struct kbase_pm_backend_data {
235         const struct kbase_pm_ca_policy *ca_current_policy;
236         const struct kbase_pm_policy *pm_current_policy;
237         union kbase_pm_ca_policy_data ca_policy_data;
238         union kbase_pm_policy_data pm_policy_data;
239         bool ca_in_transition;
240         bool reset_done;
241         wait_queue_head_t reset_done_wait;
242         wait_queue_head_t l2_powered_wait;
243         int l2_powered;
244         int gpu_cycle_counter_requests;
245         spinlock_t gpu_cycle_counter_requests_lock;
246
247         u64 desired_shader_state;
248         u64 powering_on_shader_state;
249         u64 desired_tiler_state;
250         u64 powering_on_tiler_state;
251         u64 powering_on_l2_state;
252
253         bool gpu_in_desired_state;
254         wait_queue_head_t gpu_in_desired_state_wait;
255
256         bool gpu_powered;
257
258         bool instr_enabled;
259
260         bool cg1_disabled;
261
262 #ifdef CONFIG_MALI_DEBUG
263         bool driver_ready_for_irqs;
264 #endif /* CONFIG_MALI_DEBUG */
265
266         spinlock_t gpu_powered_lock;
267
268
269         struct kbasep_pm_metrics_data metrics;
270
271         int gpu_poweroff_pending;
272         int shader_poweroff_pending_time;
273
274         struct hrtimer gpu_poweroff_timer;
275         struct workqueue_struct *gpu_poweroff_wq;
276         struct work_struct gpu_poweroff_work;
277
278         u64 shader_poweroff_pending;
279         u64 tiler_poweroff_pending;
280
281         bool poweroff_timer_needed;
282         bool poweroff_timer_running;
283
284         int (*callback_power_on)(struct kbase_device *kbdev);
285         void (*callback_power_off)(struct kbase_device *kbdev);
286         void (*callback_power_suspend)(struct kbase_device *kbdev);
287         void (*callback_power_resume)(struct kbase_device *kbdev);
288         int (*callback_power_runtime_on)(struct kbase_device *kbdev);
289         void (*callback_power_runtime_off)(struct kbase_device *kbdev);
290         int (*callback_power_runtime_idle)(struct kbase_device *kbdev);
291 };
292
293
294 /* List of policy IDs */
295 enum kbase_pm_policy_id {
296         KBASE_PM_POLICY_ID_DEMAND = 1,
297         KBASE_PM_POLICY_ID_ALWAYS_ON,
298         KBASE_PM_POLICY_ID_COARSE_DEMAND,
299 #if !MALI_CUSTOMER_RELEASE
300         KBASE_PM_POLICY_ID_DEMAND_ALWAYS_POWERED,
301         KBASE_PM_POLICY_ID_FAST_START
302 #endif
303 };
304
305 typedef u32 kbase_pm_policy_flags;
306
307 /**
308  * struct kbase_pm_policy - Power policy structure.
309  *
310  * Each power policy exposes a (static) instance of this structure which
311  * contains function pointers to the policy's methods.
312  *
313  * @name:               The name of this policy
314  * @init:               Function called when the policy is selected
315  * @term:               Function called when the policy is unselected
316  * @get_core_mask:      Function called to get the current shader core mask
317  * @get_core_active:    Function called to get the current overall GPU power
318  *                      state
319  * @flags:              Field indicating flags for this policy
320  * @id:                 Field indicating an ID for this policy. This is not
321  *                      necessarily the same as its index in the list returned
322  *                      by kbase_pm_list_policies().
323  *                      It is used purely for debugging.
324  */
325 struct kbase_pm_policy {
326         char *name;
327
328         /**
329          * Function called when the policy is selected
330          *
331          * This should initialize the kbdev->pm.pm_policy_data structure. It
332          * should not attempt to make any changes to hardware state.
333          *
334          * It is undefined what state the cores are in when the function is
335          * called.
336          *
337          * @kbdev: The kbase device structure for the device (must be a
338          *         valid pointer)
339          */
340         void (*init)(struct kbase_device *kbdev);
341
342         /**
343          * Function called when the policy is unselected.
344          *
345          * @kbdev: The kbase device structure for the device (must be a
346          *         valid pointer)
347          */
348         void (*term)(struct kbase_device *kbdev);
349
350         /**
351          * Function called to get the current shader core mask
352          *
353          * The returned mask should meet or exceed (kbdev->shader_needed_bitmap
354          * | kbdev->shader_inuse_bitmap).
355          *
356          * @kbdev: The kbase device structure for the device (must be a
357          *         valid pointer)
358          *
359          * Return: The mask of shader cores to be powered
360          */
361         u64 (*get_core_mask)(struct kbase_device *kbdev);
362
363         /**
364          * Function called to get the current overall GPU power state
365          *
366          * This function should consider the state of kbdev->pm.active_count. If
367          * this count is greater than 0 then there is at least one active
368          * context on the device and the GPU should be powered. If it is equal
369          * to 0 then there are no active contexts and the GPU could be powered
370          * off if desired.
371          *
372          * @kbdev: The kbase device structure for the device (must be a
373          *         valid pointer)
374          *
375          * Return: true if the GPU should be powered, false otherwise
376          */
377         bool (*get_core_active)(struct kbase_device *kbdev);
378
379         kbase_pm_policy_flags flags;
380         enum kbase_pm_policy_id id;
381 };
382
383
384 enum kbase_pm_ca_policy_id {
385         KBASE_PM_CA_POLICY_ID_FIXED = 1,
386         KBASE_PM_CA_POLICY_ID_RANDOM
387 };
388
389 typedef u32 kbase_pm_ca_policy_flags;
390
391 /**
392  * struct kbase_pm_ca_policy - Core availability policy structure.
393  *
394  * Each core availability policy exposes a (static) instance of this structure
395  * which contains function pointers to the policy's methods.
396  *
397  * @name:               The name of this policy
398  * @init:               Function called when the policy is selected
399  * @term:               Function called when the policy is unselected
400  * @get_core_mask:      Function called to get the current shader core
401  *                      availability mask
402  * @update_core_status: Function called to update the current core status
403  * @flags:              Field indicating flags for this policy
404  * @id:                 Field indicating an ID for this policy. This is not
405  *                      necessarily the same as its index in the list returned
406  *                      by kbase_pm_list_policies().
407  *                      It is used purely for debugging.
408  */
409 struct kbase_pm_ca_policy {
410         char *name;
411
412         /**
413          * Function called when the policy is selected
414          *
415          * This should initialize the kbdev->pm.ca_policy_data structure. It
416          * should not attempt to make any changes to hardware state.
417          *
418          * It is undefined what state the cores are in when the function is
419          * called.
420          *
421          * @kbdev The kbase device structure for the device (must be a
422          *        valid pointer)
423          */
424         void (*init)(struct kbase_device *kbdev);
425
426         /**
427          * Function called when the policy is unselected.
428          *
429          * @kbdev The kbase device structure for the device (must be a
430          *        valid pointer)
431          */
432         void (*term)(struct kbase_device *kbdev);
433
434         /**
435          * Function called to get the current shader core availability mask
436          *
437          * When a change in core availability is occurring, the policy must set
438          * kbdev->pm.ca_in_transition to true. This is to indicate that
439          * reporting changes in power state cannot be optimized out, even if
440          * kbdev->pm.desired_shader_state remains unchanged. This must be done
441          * by any functions internal to the Core Availability Policy that change
442          * the return value of kbase_pm_ca_policy::get_core_mask.
443          *
444          * @kbdev The kbase device structure for the device (must be a
445          *              valid pointer)
446          *
447          * Return: The current core availability mask
448          */
449         u64 (*get_core_mask)(struct kbase_device *kbdev);
450
451         /**
452          * Function called to update the current core status
453          *
454          * If none of the cores in core group 0 are ready or transitioning, then
455          * the policy must ensure that the next call to get_core_mask does not
456          * return 0 for all cores in core group 0. It is an error to disable
457          * core group 0 through the core availability policy.
458          *
459          * When a change in core availability has finished, the policy must set
460          * kbdev->pm.ca_in_transition to false. This is to indicate that
461          * changes in power state can once again be optimized out when
462          * kbdev->pm.desired_shader_state is unchanged.
463          *
464          * @kbdev:               The kbase device structure for the device
465          *                       (must be a valid pointer)
466          * @cores_ready:         The mask of cores currently powered and
467          *                       ready to run jobs
468          * @cores_transitioning: The mask of cores currently transitioning
469          *                       power state
470          */
471         void (*update_core_status)(struct kbase_device *kbdev, u64 cores_ready,
472                                                 u64 cores_transitioning);
473
474         kbase_pm_ca_policy_flags flags;
475
476         /**
477          * Field indicating an ID for this policy. This is not necessarily the
478          * same as its index in the list returned by kbase_pm_list_policies().
479          * It is used purely for debugging.
480          */
481         enum kbase_pm_ca_policy_id id;
482 };
483
484 #endif /* _KBASE_PM_HWACCESS_DEFS_H_ */