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