George Shen | c43f30a | 2018-01-04 13:53:55 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010-2018, The Linux Foundation. All rights reserved. |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/export.h> |
| 15 | #include <linux/kernel.h> |
Prakash Kamliya | c9b7da0 | 2016-12-20 17:45:41 +0530 | [diff] [blame] | 16 | #include <linux/hrtimer.h> |
Ram Chandrasekar | cd728d5 | 2017-03-21 11:36:35 -0600 | [diff] [blame] | 17 | #include <linux/devfreq_cooling.h> |
George Shen | c43f30a | 2018-01-04 13:53:55 -0800 | [diff] [blame] | 18 | #include <linux/pm_opp.h> |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 19 | |
| 20 | #include "kgsl.h" |
| 21 | #include "kgsl_pwrscale.h" |
| 22 | #include "kgsl_device.h" |
| 23 | #include "kgsl_trace.h" |
| 24 | |
| 25 | /* |
| 26 | * "SLEEP" is generic counting both NAP & SLUMBER |
| 27 | * PERIODS generally won't exceed 9 for the relavent 150msec |
| 28 | * window, but can be significantly smaller and still POPP |
| 29 | * pushable in cases where SLUMBER is involved. Hence the |
| 30 | * additional reliance on PERCENT to make sure a reasonable |
| 31 | * amount of down-time actually exists. |
| 32 | */ |
| 33 | #define MIN_SLEEP_PERIODS 3 |
| 34 | #define MIN_SLEEP_PERCENT 5 |
| 35 | |
| 36 | static struct kgsl_popp popp_param[POPP_MAX] = { |
| 37 | {0, 0}, |
| 38 | {-5, 20}, |
| 39 | {-5, 0}, |
| 40 | {0, 0}, |
| 41 | }; |
| 42 | |
Prakash Kamliya | c9b7da0 | 2016-12-20 17:45:41 +0530 | [diff] [blame] | 43 | /** |
| 44 | * struct kgsl_midframe_info - midframe power stats sampling info |
| 45 | * @timer - midframe sampling timer |
| 46 | * @timer_check_ws - Updates powerstats on midframe expiry |
| 47 | * @device - pointer to kgsl_device |
| 48 | */ |
| 49 | static struct kgsl_midframe_info { |
| 50 | struct hrtimer timer; |
| 51 | struct work_struct timer_check_ws; |
| 52 | struct kgsl_device *device; |
| 53 | } *kgsl_midframe = NULL; |
| 54 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 55 | static void do_devfreq_suspend(struct work_struct *work); |
| 56 | static void do_devfreq_resume(struct work_struct *work); |
| 57 | static void do_devfreq_notify(struct work_struct *work); |
| 58 | |
| 59 | /* |
| 60 | * These variables are used to keep the latest data |
| 61 | * returned by kgsl_devfreq_get_dev_status |
| 62 | */ |
| 63 | static struct xstats last_xstats; |
| 64 | static struct devfreq_dev_status last_status = { .private_data = &last_xstats }; |
| 65 | |
| 66 | /* |
| 67 | * kgsl_pwrscale_sleep - notify governor that device is going off |
| 68 | * @device: The device |
| 69 | * |
| 70 | * Called shortly after all pending work is completed. |
| 71 | */ |
| 72 | void kgsl_pwrscale_sleep(struct kgsl_device *device) |
| 73 | { |
| 74 | struct kgsl_pwrscale *psc = &device->pwrscale; |
| 75 | |
| 76 | if (!device->pwrscale.enabled) |
| 77 | return; |
| 78 | device->pwrscale.on_time = 0; |
| 79 | |
| 80 | psc->popp_level = 0; |
| 81 | clear_bit(POPP_PUSH, &device->pwrscale.popp_state); |
| 82 | |
| 83 | /* to call devfreq_suspend_device() from a kernel thread */ |
| 84 | queue_work(device->pwrscale.devfreq_wq, |
| 85 | &device->pwrscale.devfreq_suspend_ws); |
| 86 | } |
| 87 | EXPORT_SYMBOL(kgsl_pwrscale_sleep); |
| 88 | |
| 89 | /* |
| 90 | * kgsl_pwrscale_wake - notify governor that device is going on |
| 91 | * @device: The device |
| 92 | * |
| 93 | * Called when the device is returning to an active state. |
| 94 | */ |
| 95 | void kgsl_pwrscale_wake(struct kgsl_device *device) |
| 96 | { |
| 97 | struct kgsl_power_stats stats; |
| 98 | struct kgsl_pwrscale *psc = &device->pwrscale; |
| 99 | |
| 100 | if (!device->pwrscale.enabled) |
| 101 | return; |
| 102 | /* clear old stats before waking */ |
| 103 | memset(&psc->accum_stats, 0, sizeof(psc->accum_stats)); |
| 104 | memset(&last_xstats, 0, sizeof(last_xstats)); |
| 105 | |
| 106 | /* and any hw activity from waking up*/ |
| 107 | device->ftbl->power_stats(device, &stats); |
| 108 | |
| 109 | psc->time = ktime_get(); |
| 110 | |
| 111 | psc->next_governor_call = ktime_add_us(psc->time, |
| 112 | KGSL_GOVERNOR_CALL_INTERVAL); |
| 113 | |
| 114 | /* to call devfreq_resume_device() from a kernel thread */ |
| 115 | queue_work(psc->devfreq_wq, &psc->devfreq_resume_ws); |
| 116 | } |
| 117 | EXPORT_SYMBOL(kgsl_pwrscale_wake); |
| 118 | |
| 119 | /* |
| 120 | * kgsl_pwrscale_busy - update pwrscale state for new work |
| 121 | * @device: The device |
| 122 | * |
| 123 | * Called when new work is submitted to the device. |
| 124 | * This function must be called with the device mutex locked. |
| 125 | */ |
| 126 | void kgsl_pwrscale_busy(struct kgsl_device *device) |
| 127 | { |
| 128 | if (!device->pwrscale.enabled) |
| 129 | return; |
| 130 | if (device->pwrscale.on_time == 0) |
| 131 | device->pwrscale.on_time = ktime_to_us(ktime_get()); |
| 132 | } |
| 133 | EXPORT_SYMBOL(kgsl_pwrscale_busy); |
| 134 | |
| 135 | /** |
| 136 | * kgsl_pwrscale_update_stats() - update device busy statistics |
| 137 | * @device: The device |
| 138 | * |
| 139 | * Read hardware busy counters and accumulate the results. |
| 140 | */ |
| 141 | void kgsl_pwrscale_update_stats(struct kgsl_device *device) |
| 142 | { |
| 143 | struct kgsl_pwrctrl *pwrctrl = &device->pwrctrl; |
| 144 | struct kgsl_pwrscale *psc = &device->pwrscale; |
| 145 | |
| 146 | if (WARN_ON(!mutex_is_locked(&device->mutex))) |
| 147 | return; |
| 148 | |
| 149 | if (!psc->enabled) |
| 150 | return; |
| 151 | |
| 152 | if (device->state == KGSL_STATE_ACTIVE) { |
| 153 | struct kgsl_power_stats stats; |
| 154 | |
| 155 | device->ftbl->power_stats(device, &stats); |
| 156 | if (psc->popp_level) { |
| 157 | u64 x = stats.busy_time; |
| 158 | u64 y = stats.ram_time; |
| 159 | |
| 160 | do_div(x, 100); |
| 161 | do_div(y, 100); |
| 162 | x *= popp_param[psc->popp_level].gpu_x; |
| 163 | y *= popp_param[psc->popp_level].ddr_y; |
| 164 | trace_kgsl_popp_mod(device, x, y); |
| 165 | stats.busy_time += x; |
| 166 | stats.ram_time += y; |
| 167 | } |
| 168 | device->pwrscale.accum_stats.busy_time += stats.busy_time; |
| 169 | device->pwrscale.accum_stats.ram_time += stats.ram_time; |
| 170 | device->pwrscale.accum_stats.ram_wait += stats.ram_wait; |
| 171 | pwrctrl->clock_times[pwrctrl->active_pwrlevel] += |
| 172 | stats.busy_time; |
| 173 | } |
| 174 | } |
| 175 | EXPORT_SYMBOL(kgsl_pwrscale_update_stats); |
| 176 | |
| 177 | /** |
| 178 | * kgsl_pwrscale_update() - update device busy statistics |
| 179 | * @device: The device |
| 180 | * |
| 181 | * If enough time has passed schedule the next call to devfreq |
| 182 | * get_dev_status. |
| 183 | */ |
| 184 | void kgsl_pwrscale_update(struct kgsl_device *device) |
| 185 | { |
| 186 | ktime_t t; |
| 187 | |
| 188 | if (WARN_ON(!mutex_is_locked(&device->mutex))) |
| 189 | return; |
| 190 | |
| 191 | if (!device->pwrscale.enabled) |
| 192 | return; |
| 193 | |
| 194 | t = ktime_get(); |
| 195 | if (ktime_compare(t, device->pwrscale.next_governor_call) < 0) |
| 196 | return; |
| 197 | |
| 198 | device->pwrscale.next_governor_call = ktime_add_us(t, |
| 199 | KGSL_GOVERNOR_CALL_INTERVAL); |
| 200 | |
| 201 | /* to call srcu_notifier_call_chain() from a kernel thread */ |
| 202 | if (device->state != KGSL_STATE_SLUMBER) |
| 203 | queue_work(device->pwrscale.devfreq_wq, |
| 204 | &device->pwrscale.devfreq_notify_ws); |
Prakash Kamliya | c9b7da0 | 2016-12-20 17:45:41 +0530 | [diff] [blame] | 205 | |
| 206 | kgsl_pwrscale_midframe_timer_restart(device); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 207 | } |
| 208 | EXPORT_SYMBOL(kgsl_pwrscale_update); |
| 209 | |
Prakash Kamliya | c9b7da0 | 2016-12-20 17:45:41 +0530 | [diff] [blame] | 210 | void kgsl_pwrscale_midframe_timer_restart(struct kgsl_device *device) |
| 211 | { |
| 212 | if (kgsl_midframe) { |
| 213 | WARN_ON(!mutex_is_locked(&device->mutex)); |
| 214 | |
| 215 | /* If the timer is already running, stop it */ |
| 216 | if (hrtimer_active(&kgsl_midframe->timer)) |
| 217 | hrtimer_cancel( |
| 218 | &kgsl_midframe->timer); |
| 219 | |
| 220 | hrtimer_start(&kgsl_midframe->timer, |
| 221 | ns_to_ktime(KGSL_GOVERNOR_CALL_INTERVAL |
| 222 | * NSEC_PER_USEC), HRTIMER_MODE_REL); |
| 223 | } |
| 224 | } |
| 225 | EXPORT_SYMBOL(kgsl_pwrscale_midframe_timer_restart); |
| 226 | |
| 227 | void kgsl_pwrscale_midframe_timer_cancel(struct kgsl_device *device) |
| 228 | { |
| 229 | if (kgsl_midframe) { |
| 230 | WARN_ON(!mutex_is_locked(&device->mutex)); |
| 231 | hrtimer_cancel(&kgsl_midframe->timer); |
| 232 | } |
| 233 | } |
| 234 | EXPORT_SYMBOL(kgsl_pwrscale_midframe_timer_cancel); |
| 235 | |
| 236 | static void kgsl_pwrscale_midframe_timer_check(struct work_struct *work) |
| 237 | { |
| 238 | struct kgsl_device *device = kgsl_midframe->device; |
| 239 | |
| 240 | mutex_lock(&device->mutex); |
| 241 | if (device->state == KGSL_STATE_ACTIVE) |
| 242 | kgsl_pwrscale_update(device); |
| 243 | mutex_unlock(&device->mutex); |
| 244 | } |
| 245 | |
| 246 | static enum hrtimer_restart kgsl_pwrscale_midframe_timer(struct hrtimer *timer) |
| 247 | { |
| 248 | struct kgsl_device *device = kgsl_midframe->device; |
| 249 | |
| 250 | queue_work(device->pwrscale.devfreq_wq, |
| 251 | &kgsl_midframe->timer_check_ws); |
| 252 | |
| 253 | return HRTIMER_NORESTART; |
| 254 | } |
| 255 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 256 | /* |
| 257 | * kgsl_pwrscale_disable - temporarily disable the governor |
| 258 | * @device: The device |
| 259 | * @turbo: Indicates if pwrlevel should be forced to turbo |
| 260 | * |
| 261 | * Temporarily disable the governor, to prevent interference |
| 262 | * with profiling tools that expect a fixed clock frequency. |
| 263 | * This function must be called with the device mutex locked. |
| 264 | */ |
| 265 | void kgsl_pwrscale_disable(struct kgsl_device *device, bool turbo) |
| 266 | { |
| 267 | if (WARN_ON(!mutex_is_locked(&device->mutex))) |
| 268 | return; |
| 269 | |
| 270 | if (device->pwrscale.devfreqptr) |
| 271 | queue_work(device->pwrscale.devfreq_wq, |
| 272 | &device->pwrscale.devfreq_suspend_ws); |
| 273 | device->pwrscale.enabled = false; |
| 274 | if (turbo) |
| 275 | kgsl_pwrctrl_pwrlevel_change(device, KGSL_PWRLEVEL_TURBO); |
| 276 | } |
| 277 | EXPORT_SYMBOL(kgsl_pwrscale_disable); |
| 278 | |
| 279 | /* |
| 280 | * kgsl_pwrscale_enable - re-enable the governor |
| 281 | * @device: The device |
| 282 | * |
| 283 | * Reenable the governor after a kgsl_pwrscale_disable() call. |
| 284 | * This function must be called with the device mutex locked. |
| 285 | */ |
| 286 | void kgsl_pwrscale_enable(struct kgsl_device *device) |
| 287 | { |
| 288 | if (WARN_ON(!mutex_is_locked(&device->mutex))) |
| 289 | return; |
| 290 | |
| 291 | if (device->pwrscale.devfreqptr) { |
| 292 | queue_work(device->pwrscale.devfreq_wq, |
| 293 | &device->pwrscale.devfreq_resume_ws); |
| 294 | device->pwrscale.enabled = true; |
| 295 | } else { |
| 296 | /* |
| 297 | * Don't enable it if devfreq is not set and let the device |
| 298 | * run at default level; |
| 299 | */ |
| 300 | kgsl_pwrctrl_pwrlevel_change(device, |
| 301 | device->pwrctrl.default_pwrlevel); |
| 302 | device->pwrscale.enabled = false; |
| 303 | } |
| 304 | } |
| 305 | EXPORT_SYMBOL(kgsl_pwrscale_enable); |
| 306 | |
| 307 | static int _thermal_adjust(struct kgsl_pwrctrl *pwr, int level) |
| 308 | { |
| 309 | if (level < pwr->active_pwrlevel) |
| 310 | return pwr->active_pwrlevel; |
| 311 | |
| 312 | /* |
| 313 | * A lower frequency has been recommended! Stop thermal |
| 314 | * cycling (but keep the upper thermal limit) and switch to |
| 315 | * the lower frequency. |
| 316 | */ |
| 317 | pwr->thermal_cycle = CYCLE_ENABLE; |
| 318 | del_timer_sync(&pwr->thermal_timer); |
| 319 | return level; |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * Use various metrics including level stability, NAP intervals, and |
| 324 | * overall GPU freq / DDR freq combination to decide if POPP should |
| 325 | * be activated. |
| 326 | */ |
| 327 | static bool popp_stable(struct kgsl_device *device) |
| 328 | { |
| 329 | s64 t; |
| 330 | s64 nap_time = 0; |
| 331 | s64 go_time = 0; |
| 332 | int i, index; |
| 333 | int nap = 0; |
| 334 | s64 percent_nap = 0; |
| 335 | struct kgsl_pwr_event *e; |
| 336 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
| 337 | struct kgsl_pwrscale *psc = &device->pwrscale; |
| 338 | |
| 339 | if (!test_bit(POPP_ON, &psc->popp_state)) |
| 340 | return false; |
| 341 | |
| 342 | /* If already pushed or running naturally at min don't push further */ |
| 343 | if (test_bit(POPP_PUSH, &psc->popp_state)) |
| 344 | return false; |
| 345 | if (!psc->popp_level && |
| 346 | (pwr->active_pwrlevel == pwr->min_pwrlevel)) |
| 347 | return false; |
| 348 | if (psc->history[KGSL_PWREVENT_STATE].events == NULL) |
| 349 | return false; |
| 350 | |
| 351 | t = ktime_to_ms(ktime_get()); |
| 352 | /* Check for recent NAP statistics: NAPping regularly and well? */ |
| 353 | if (pwr->active_pwrlevel == 0) { |
| 354 | index = psc->history[KGSL_PWREVENT_STATE].index; |
| 355 | i = index > 0 ? (index - 1) : |
| 356 | (psc->history[KGSL_PWREVENT_STATE].size - 1); |
| 357 | while (i != index) { |
| 358 | e = &psc->history[KGSL_PWREVENT_STATE].events[i]; |
| 359 | if (e->data == KGSL_STATE_NAP || |
| 360 | e->data == KGSL_STATE_SLUMBER) { |
| 361 | if (ktime_to_ms(e->start) + STABLE_TIME > t) { |
| 362 | nap++; |
| 363 | nap_time += e->duration; |
| 364 | } |
| 365 | } else if (e->data == KGSL_STATE_ACTIVE) { |
| 366 | if (ktime_to_ms(e->start) + STABLE_TIME > t) |
| 367 | go_time += e->duration; |
| 368 | } |
| 369 | if (i == 0) |
| 370 | i = psc->history[KGSL_PWREVENT_STATE].size - 1; |
| 371 | else |
| 372 | i--; |
| 373 | } |
| 374 | if (nap_time && go_time) { |
| 375 | percent_nap = 100 * nap_time; |
Maria Yu | a04533e | 2017-11-10 19:25:22 +0800 | [diff] [blame] | 376 | div64_s64(percent_nap, nap_time + go_time); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 377 | } |
| 378 | trace_kgsl_popp_nap(device, (int)nap_time / 1000, nap, |
| 379 | percent_nap); |
| 380 | /* If running high at turbo, don't push */ |
| 381 | if (nap < MIN_SLEEP_PERIODS || percent_nap < MIN_SLEEP_PERCENT) |
| 382 | return false; |
| 383 | } |
| 384 | |
| 385 | /* Finally check that there hasn't been a recent change */ |
| 386 | if ((device->pwrscale.freq_change_time + STABLE_TIME) < t) { |
| 387 | device->pwrscale.freq_change_time = t; |
| 388 | return true; |
| 389 | } |
| 390 | return false; |
| 391 | } |
| 392 | |
| 393 | bool kgsl_popp_check(struct kgsl_device *device) |
| 394 | { |
| 395 | int i; |
| 396 | struct kgsl_pwrscale *psc = &device->pwrscale; |
| 397 | struct kgsl_pwr_event *e; |
| 398 | |
| 399 | if (!test_bit(POPP_ON, &psc->popp_state)) |
| 400 | return false; |
| 401 | if (!test_bit(POPP_PUSH, &psc->popp_state)) |
| 402 | return false; |
| 403 | if (psc->history[KGSL_PWREVENT_STATE].events == NULL) { |
| 404 | clear_bit(POPP_PUSH, &psc->popp_state); |
| 405 | return false; |
| 406 | } |
| 407 | |
| 408 | e = &psc->history[KGSL_PWREVENT_STATE]. |
| 409 | events[psc->history[KGSL_PWREVENT_STATE].index]; |
| 410 | if (e->data == KGSL_STATE_SLUMBER) |
| 411 | e->duration = ktime_us_delta(ktime_get(), e->start); |
| 412 | |
| 413 | /* If there's been a long SLUMBER in recent history, clear the _PUSH */ |
| 414 | for (i = 0; i < psc->history[KGSL_PWREVENT_STATE].size; i++) { |
| 415 | e = &psc->history[KGSL_PWREVENT_STATE].events[i]; |
| 416 | if ((e->data == KGSL_STATE_SLUMBER) && |
| 417 | (e->duration > POPP_RESET_TIME)) { |
| 418 | clear_bit(POPP_PUSH, &psc->popp_state); |
| 419 | return false; |
| 420 | } |
| 421 | } |
| 422 | return true; |
| 423 | } |
| 424 | |
| 425 | /* |
| 426 | * The GPU has been running at the current frequency for a while. Attempt |
| 427 | * to lower the frequency for boarderline cases. |
| 428 | */ |
| 429 | static void popp_trans1(struct kgsl_device *device) |
| 430 | { |
| 431 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
| 432 | struct kgsl_pwrlevel *pl = &pwr->pwrlevels[pwr->active_pwrlevel]; |
| 433 | struct kgsl_pwrscale *psc = &device->pwrscale; |
| 434 | int old_level = psc->popp_level; |
| 435 | |
| 436 | switch (old_level) { |
| 437 | case 0: |
| 438 | psc->popp_level = 2; |
| 439 | /* If the current level has a high default bus don't push it */ |
| 440 | if (pl->bus_freq == pl->bus_max) |
| 441 | pwr->bus_mod = 1; |
| 442 | kgsl_pwrctrl_pwrlevel_change(device, pwr->active_pwrlevel + 1); |
| 443 | break; |
| 444 | case 1: |
| 445 | case 2: |
| 446 | psc->popp_level++; |
| 447 | break; |
| 448 | case 3: |
| 449 | set_bit(POPP_PUSH, &psc->popp_state); |
| 450 | psc->popp_level = 0; |
| 451 | break; |
| 452 | case POPP_MAX: |
| 453 | default: |
| 454 | psc->popp_level = 0; |
| 455 | break; |
| 456 | } |
| 457 | |
| 458 | trace_kgsl_popp_level(device, old_level, psc->popp_level); |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | * The GPU DCVS algorithm recommends a level change. Apply any |
| 463 | * POPP restrictions and update the level accordingly |
| 464 | */ |
| 465 | static int popp_trans2(struct kgsl_device *device, int level) |
| 466 | { |
| 467 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
| 468 | struct kgsl_pwrscale *psc = &device->pwrscale; |
| 469 | int old_level = psc->popp_level; |
| 470 | |
| 471 | if (!test_bit(POPP_ON, &psc->popp_state)) |
| 472 | return level; |
| 473 | |
| 474 | clear_bit(POPP_PUSH, &psc->popp_state); |
| 475 | /* If the governor recommends going down, do it! */ |
| 476 | if (pwr->active_pwrlevel < level) { |
| 477 | psc->popp_level = 0; |
| 478 | trace_kgsl_popp_level(device, old_level, psc->popp_level); |
| 479 | return level; |
| 480 | } |
| 481 | |
| 482 | switch (psc->popp_level) { |
| 483 | case 0: |
| 484 | /* If the feature isn't engaged, go up immediately */ |
| 485 | break; |
| 486 | case 1: |
| 487 | /* Turn off mitigation, and go up a level */ |
| 488 | psc->popp_level = 0; |
| 489 | break; |
| 490 | case 2: |
| 491 | case 3: |
| 492 | /* Try a more aggressive mitigation */ |
| 493 | psc->popp_level--; |
| 494 | level++; |
| 495 | /* Update the stable timestamp */ |
| 496 | device->pwrscale.freq_change_time = ktime_to_ms(ktime_get()); |
| 497 | break; |
| 498 | case POPP_MAX: |
| 499 | default: |
| 500 | psc->popp_level = 0; |
| 501 | break; |
| 502 | } |
| 503 | |
| 504 | trace_kgsl_popp_level(device, old_level, psc->popp_level); |
| 505 | |
| 506 | return level; |
| 507 | } |
| 508 | |
| 509 | #ifdef DEVFREQ_FLAG_WAKEUP_MAXFREQ |
| 510 | static inline bool _check_maxfreq(u32 flags) |
| 511 | { |
| 512 | return (flags & DEVFREQ_FLAG_WAKEUP_MAXFREQ); |
| 513 | } |
| 514 | #else |
| 515 | static inline bool _check_maxfreq(u32 flags) |
| 516 | { |
| 517 | return false; |
| 518 | } |
| 519 | #endif |
| 520 | |
| 521 | /* |
| 522 | * kgsl_devfreq_target - devfreq_dev_profile.target callback |
| 523 | * @dev: see devfreq.h |
| 524 | * @freq: see devfreq.h |
| 525 | * @flags: see devfreq.h |
| 526 | * |
| 527 | * This function expects the device mutex to be unlocked. |
| 528 | */ |
| 529 | int kgsl_devfreq_target(struct device *dev, unsigned long *freq, u32 flags) |
| 530 | { |
| 531 | struct kgsl_device *device = dev_get_drvdata(dev); |
| 532 | struct kgsl_pwrctrl *pwr; |
| 533 | struct kgsl_pwrlevel *pwr_level; |
Venkateswara Rao Tadikonda | 997a8de | 2017-06-27 13:05:10 +0530 | [diff] [blame] | 534 | int level; |
| 535 | unsigned int i; |
Ram Chandrasekar | cd728d5 | 2017-03-21 11:36:35 -0600 | [diff] [blame] | 536 | unsigned long cur_freq, rec_freq; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 537 | |
| 538 | if (device == NULL) |
| 539 | return -ENODEV; |
| 540 | if (freq == NULL) |
| 541 | return -EINVAL; |
| 542 | if (!device->pwrscale.enabled) |
| 543 | return 0; |
| 544 | |
| 545 | pwr = &device->pwrctrl; |
| 546 | if (_check_maxfreq(flags)) { |
| 547 | /* |
| 548 | * The GPU is about to get suspended, |
| 549 | * but it needs to be at the max power level when waking up |
| 550 | */ |
| 551 | pwr->wakeup_maxpwrlevel = 1; |
| 552 | return 0; |
| 553 | } |
| 554 | |
Ram Chandrasekar | cd728d5 | 2017-03-21 11:36:35 -0600 | [diff] [blame] | 555 | rec_freq = *freq; |
Ram Chandrasekar | cd728d5 | 2017-03-21 11:36:35 -0600 | [diff] [blame] | 556 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 557 | mutex_lock(&device->mutex); |
| 558 | cur_freq = kgsl_pwrctrl_active_freq(pwr); |
| 559 | level = pwr->active_pwrlevel; |
| 560 | pwr_level = &pwr->pwrlevels[level]; |
| 561 | |
| 562 | /* If the governor recommends a new frequency, update it here */ |
Ram Chandrasekar | cd728d5 | 2017-03-21 11:36:35 -0600 | [diff] [blame] | 563 | if (rec_freq != cur_freq) { |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 564 | level = pwr->max_pwrlevel; |
Venkateswara Rao Tadikonda | 997a8de | 2017-06-27 13:05:10 +0530 | [diff] [blame] | 565 | /* |
| 566 | * Array index of pwrlevels[] should be within the permitted |
| 567 | * power levels, i.e., from max_pwrlevel to min_pwrlevel. |
| 568 | */ |
| 569 | for (i = pwr->min_pwrlevel; (i >= pwr->max_pwrlevel |
| 570 | && i <= pwr->min_pwrlevel); i--) |
Ram Chandrasekar | cd728d5 | 2017-03-21 11:36:35 -0600 | [diff] [blame] | 571 | if (rec_freq <= pwr->pwrlevels[i].gpu_freq) { |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 572 | if (pwr->thermal_cycle == CYCLE_ACTIVE) |
| 573 | level = _thermal_adjust(pwr, i); |
| 574 | else |
| 575 | level = popp_trans2(device, i); |
| 576 | break; |
| 577 | } |
| 578 | if (level != pwr->active_pwrlevel) |
| 579 | kgsl_pwrctrl_pwrlevel_change(device, level); |
| 580 | } else if (popp_stable(device)) { |
| 581 | popp_trans1(device); |
| 582 | } |
| 583 | |
| 584 | *freq = kgsl_pwrctrl_active_freq(pwr); |
| 585 | |
| 586 | mutex_unlock(&device->mutex); |
| 587 | return 0; |
| 588 | } |
| 589 | EXPORT_SYMBOL(kgsl_devfreq_target); |
| 590 | |
| 591 | /* |
| 592 | * kgsl_devfreq_get_dev_status - devfreq_dev_profile.get_dev_status callback |
| 593 | * @dev: see devfreq.h |
| 594 | * @freq: see devfreq.h |
| 595 | * @flags: see devfreq.h |
| 596 | * |
| 597 | * This function expects the device mutex to be unlocked. |
| 598 | */ |
| 599 | int kgsl_devfreq_get_dev_status(struct device *dev, |
| 600 | struct devfreq_dev_status *stat) |
| 601 | { |
| 602 | struct kgsl_device *device = dev_get_drvdata(dev); |
| 603 | struct kgsl_pwrctrl *pwrctrl; |
| 604 | struct kgsl_pwrscale *pwrscale; |
Abhilash Kumar | d5908d0 | 2017-07-17 01:05:23 +0530 | [diff] [blame] | 605 | ktime_t tmp1, tmp2; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 606 | |
| 607 | if (device == NULL) |
| 608 | return -ENODEV; |
| 609 | if (stat == NULL) |
| 610 | return -EINVAL; |
| 611 | |
| 612 | pwrscale = &device->pwrscale; |
| 613 | pwrctrl = &device->pwrctrl; |
| 614 | |
| 615 | mutex_lock(&device->mutex); |
Abhilash Kumar | d5908d0 | 2017-07-17 01:05:23 +0530 | [diff] [blame] | 616 | |
| 617 | tmp1 = ktime_get(); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 618 | /* |
| 619 | * If the GPU clock is on grab the latest power counter |
| 620 | * values. Otherwise the most recent ACTIVE values will |
| 621 | * already be stored in accum_stats. |
| 622 | */ |
| 623 | kgsl_pwrscale_update_stats(device); |
| 624 | |
Abhilash Kumar | d5908d0 | 2017-07-17 01:05:23 +0530 | [diff] [blame] | 625 | tmp2 = ktime_get(); |
| 626 | stat->total_time = ktime_us_delta(tmp2, pwrscale->time); |
| 627 | pwrscale->time = tmp1; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 628 | |
| 629 | stat->busy_time = pwrscale->accum_stats.busy_time; |
| 630 | |
| 631 | stat->current_frequency = kgsl_pwrctrl_active_freq(&device->pwrctrl); |
| 632 | |
| 633 | stat->private_data = &device->active_context_count; |
| 634 | |
| 635 | /* |
| 636 | * keep the latest devfreq_dev_status values |
| 637 | * and vbif counters data |
| 638 | * to be (re)used by kgsl_busmon_get_dev_status() |
| 639 | */ |
| 640 | if (pwrctrl->bus_control) { |
| 641 | struct xstats *last_b = |
| 642 | (struct xstats *)last_status.private_data; |
| 643 | |
| 644 | last_status.total_time = stat->total_time; |
| 645 | last_status.busy_time = stat->busy_time; |
| 646 | last_status.current_frequency = stat->current_frequency; |
| 647 | |
| 648 | last_b->ram_time = device->pwrscale.accum_stats.ram_time; |
| 649 | last_b->ram_wait = device->pwrscale.accum_stats.ram_wait; |
| 650 | last_b->mod = device->pwrctrl.bus_mod; |
| 651 | } |
| 652 | |
| 653 | kgsl_pwrctrl_busy_time(device, stat->total_time, stat->busy_time); |
| 654 | trace_kgsl_pwrstats(device, stat->total_time, |
| 655 | &pwrscale->accum_stats, device->active_context_count); |
| 656 | memset(&pwrscale->accum_stats, 0, sizeof(pwrscale->accum_stats)); |
| 657 | |
| 658 | mutex_unlock(&device->mutex); |
| 659 | |
| 660 | return 0; |
| 661 | } |
| 662 | EXPORT_SYMBOL(kgsl_devfreq_get_dev_status); |
| 663 | |
| 664 | /* |
| 665 | * kgsl_devfreq_get_cur_freq - devfreq_dev_profile.get_cur_freq callback |
| 666 | * @dev: see devfreq.h |
| 667 | * @freq: see devfreq.h |
| 668 | * @flags: see devfreq.h |
| 669 | * |
| 670 | * This function expects the device mutex to be unlocked. |
| 671 | */ |
| 672 | int kgsl_devfreq_get_cur_freq(struct device *dev, unsigned long *freq) |
| 673 | { |
| 674 | struct kgsl_device *device = dev_get_drvdata(dev); |
| 675 | |
| 676 | if (device == NULL) |
| 677 | return -ENODEV; |
| 678 | if (freq == NULL) |
| 679 | return -EINVAL; |
| 680 | |
| 681 | mutex_lock(&device->mutex); |
| 682 | *freq = kgsl_pwrctrl_active_freq(&device->pwrctrl); |
| 683 | mutex_unlock(&device->mutex); |
| 684 | |
| 685 | return 0; |
| 686 | } |
| 687 | EXPORT_SYMBOL(kgsl_devfreq_get_cur_freq); |
| 688 | |
| 689 | /* |
| 690 | * kgsl_devfreq_add_notifier - add a fine grained notifier. |
| 691 | * @dev: The device |
| 692 | * @nb: Notifier block that will receive updates. |
| 693 | * |
| 694 | * Add a notifier to receive ADRENO_DEVFREQ_NOTIFY_* events |
| 695 | * from the device. |
| 696 | */ |
| 697 | int kgsl_devfreq_add_notifier(struct device *dev, |
| 698 | struct notifier_block *nb) |
| 699 | { |
| 700 | struct kgsl_device *device = dev_get_drvdata(dev); |
| 701 | |
| 702 | if (device == NULL) |
| 703 | return -ENODEV; |
| 704 | |
| 705 | if (nb == NULL) |
| 706 | return -EINVAL; |
| 707 | |
| 708 | return srcu_notifier_chain_register(&device->pwrscale.nh, nb); |
| 709 | } |
| 710 | EXPORT_SYMBOL(kgsl_devfreq_add_notifier); |
| 711 | |
| 712 | /* |
| 713 | * kgsl_devfreq_del_notifier - remove a fine grained notifier. |
| 714 | * @dev: The device |
| 715 | * @nb: The notifier block. |
| 716 | * |
| 717 | * Remove a notifier registered with kgsl_devfreq_add_notifier(). |
| 718 | */ |
| 719 | int kgsl_devfreq_del_notifier(struct device *dev, struct notifier_block *nb) |
| 720 | { |
| 721 | struct kgsl_device *device = dev_get_drvdata(dev); |
| 722 | |
| 723 | if (device == NULL) |
| 724 | return -ENODEV; |
| 725 | |
| 726 | if (nb == NULL) |
| 727 | return -EINVAL; |
| 728 | |
| 729 | return srcu_notifier_chain_unregister(&device->pwrscale.nh, nb); |
| 730 | } |
| 731 | EXPORT_SYMBOL(kgsl_devfreq_del_notifier); |
| 732 | |
| 733 | |
| 734 | /* |
| 735 | * kgsl_busmon_get_dev_status - devfreq_dev_profile.get_dev_status callback |
| 736 | * @dev: see devfreq.h |
| 737 | * @freq: see devfreq.h |
| 738 | * @flags: see devfreq.h |
| 739 | * |
| 740 | * This function expects the device mutex to be unlocked. |
| 741 | */ |
| 742 | int kgsl_busmon_get_dev_status(struct device *dev, |
| 743 | struct devfreq_dev_status *stat) |
| 744 | { |
| 745 | struct xstats *b; |
| 746 | |
| 747 | stat->total_time = last_status.total_time; |
| 748 | stat->busy_time = last_status.busy_time; |
| 749 | stat->current_frequency = last_status.current_frequency; |
| 750 | if (stat->private_data) { |
| 751 | struct xstats *last_b = |
| 752 | (struct xstats *)last_status.private_data; |
| 753 | b = (struct xstats *)stat->private_data; |
| 754 | b->ram_time = last_b->ram_time; |
| 755 | b->ram_wait = last_b->ram_wait; |
| 756 | b->mod = last_b->mod; |
| 757 | } |
| 758 | return 0; |
| 759 | } |
| 760 | |
| 761 | #ifdef DEVFREQ_FLAG_FAST_HINT |
| 762 | static inline bool _check_fast_hint(u32 flags) |
| 763 | { |
| 764 | return (flags & DEVFREQ_FLAG_FAST_HINT); |
| 765 | } |
| 766 | #else |
| 767 | static inline bool _check_fast_hint(u32 flags) |
| 768 | { |
| 769 | return false; |
| 770 | } |
| 771 | #endif |
| 772 | |
| 773 | #ifdef DEVFREQ_FLAG_SLOW_HINT |
| 774 | static inline bool _check_slow_hint(u32 flags) |
| 775 | { |
| 776 | return (flags & DEVFREQ_FLAG_SLOW_HINT); |
| 777 | } |
| 778 | #else |
| 779 | static inline bool _check_slow_hint(u32 flags) |
| 780 | { |
| 781 | return false; |
| 782 | } |
| 783 | #endif |
| 784 | |
| 785 | /* |
| 786 | * kgsl_busmon_target - devfreq_dev_profile.target callback |
| 787 | * @dev: see devfreq.h |
| 788 | * @freq: see devfreq.h |
| 789 | * @flags: see devfreq.h |
| 790 | * |
| 791 | * This function expects the device mutex to be unlocked. |
| 792 | */ |
| 793 | int kgsl_busmon_target(struct device *dev, unsigned long *freq, u32 flags) |
| 794 | { |
| 795 | struct kgsl_device *device = dev_get_drvdata(dev); |
| 796 | struct kgsl_pwrctrl *pwr; |
| 797 | struct kgsl_pwrlevel *pwr_level; |
| 798 | int level, b; |
| 799 | u32 bus_flag; |
| 800 | unsigned long ab_mbytes; |
| 801 | |
| 802 | if (device == NULL) |
| 803 | return -ENODEV; |
| 804 | if (freq == NULL) |
| 805 | return -EINVAL; |
| 806 | if (!device->pwrscale.enabled) |
| 807 | return 0; |
| 808 | |
| 809 | pwr = &device->pwrctrl; |
| 810 | |
| 811 | if (!pwr->bus_control) |
| 812 | return 0; |
| 813 | |
| 814 | mutex_lock(&device->mutex); |
| 815 | level = pwr->active_pwrlevel; |
| 816 | pwr_level = &pwr->pwrlevels[level]; |
| 817 | bus_flag = device->pwrscale.bus_profile.flag; |
| 818 | device->pwrscale.bus_profile.flag = 0; |
| 819 | ab_mbytes = device->pwrscale.bus_profile.ab_mbytes; |
| 820 | |
| 821 | /* |
| 822 | * Bus devfreq governor has calculated its recomendations |
| 823 | * when gpu was running with *freq frequency. |
| 824 | * If the gpu frequency is different now it's better to |
| 825 | * ignore the call |
| 826 | */ |
| 827 | if (pwr_level->gpu_freq != *freq) { |
| 828 | mutex_unlock(&device->mutex); |
| 829 | return 0; |
| 830 | } |
| 831 | |
| 832 | b = pwr->bus_mod; |
Oleg Perelet | d31e9b3 | 2017-11-15 09:41:11 -0800 | [diff] [blame] | 833 | if (_check_fast_hint(bus_flag)) |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 834 | pwr->bus_mod++; |
Oleg Perelet | d31e9b3 | 2017-11-15 09:41:11 -0800 | [diff] [blame] | 835 | else if (_check_slow_hint(bus_flag)) |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 836 | pwr->bus_mod--; |
| 837 | |
Oleg Perelet | d31e9b3 | 2017-11-15 09:41:11 -0800 | [diff] [blame] | 838 | /* trim calculated change to fit range */ |
| 839 | if (pwr_level->bus_freq + pwr->bus_mod < pwr_level->bus_min) |
| 840 | pwr->bus_mod = -(pwr_level->bus_freq - pwr_level->bus_min); |
| 841 | else if (pwr_level->bus_freq + pwr->bus_mod > pwr_level->bus_max) |
| 842 | pwr->bus_mod = pwr_level->bus_max - pwr_level->bus_freq; |
| 843 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 844 | /* Update bus vote if AB or IB is modified */ |
| 845 | if ((pwr->bus_mod != b) || (pwr->bus_ab_mbytes != ab_mbytes)) { |
| 846 | pwr->bus_percent_ab = device->pwrscale.bus_profile.percent_ab; |
| 847 | pwr->bus_ab_mbytes = ab_mbytes; |
| 848 | kgsl_pwrctrl_buslevel_update(device, true); |
| 849 | } |
| 850 | |
| 851 | mutex_unlock(&device->mutex); |
| 852 | return 0; |
| 853 | } |
| 854 | |
| 855 | int kgsl_busmon_get_cur_freq(struct device *dev, unsigned long *freq) |
| 856 | { |
| 857 | return 0; |
| 858 | } |
| 859 | |
George Shen | c43f30a | 2018-01-04 13:53:55 -0800 | [diff] [blame] | 860 | /* |
| 861 | * opp_notify - Callback function registered to receive OPP events. |
| 862 | * @nb: The notifier block |
| 863 | * @type: The event type. Two OPP events are expected in this function: |
| 864 | * - OPP_EVENT_ENABLE: an GPU OPP is enabled. The in_opp parameter |
| 865 | * contains the OPP that is enabled |
| 866 | * - OPP_EVENT_DISALBE: an GPU OPP is disabled. The in_opp parameter |
| 867 | * contains the OPP that is disabled. |
| 868 | * @in_opp: the GPU OPP whose status is changed and triggered the event |
| 869 | * |
| 870 | * GPU OPP event callback function. The function subscribe GPU OPP status |
| 871 | * change and update thermal power level accordingly. |
| 872 | */ |
| 873 | |
| 874 | static int opp_notify(struct notifier_block *nb, |
| 875 | unsigned long type, void *in_opp) |
| 876 | { |
| 877 | int result = -EINVAL, level, min_level, max_level; |
| 878 | struct kgsl_pwrctrl *pwr = container_of(nb, struct kgsl_pwrctrl, nb); |
| 879 | struct kgsl_device *device = container_of(pwr, |
| 880 | struct kgsl_device, pwrctrl); |
| 881 | struct device *dev = &device->pdev->dev; |
| 882 | struct dev_pm_opp *opp; |
| 883 | unsigned long min_freq = 0, max_freq = pwr->pwrlevels[0].gpu_freq; |
| 884 | |
| 885 | if (type != OPP_EVENT_ENABLE && type != OPP_EVENT_DISABLE) |
| 886 | return result; |
| 887 | |
| 888 | rcu_read_lock(); |
| 889 | opp = dev_pm_opp_find_freq_floor(dev, &max_freq); |
| 890 | if (IS_ERR(opp)) { |
| 891 | rcu_read_unlock(); |
| 892 | return PTR_ERR(opp); |
| 893 | } |
| 894 | |
| 895 | max_freq = dev_pm_opp_get_freq(opp); |
| 896 | if (!max_freq) { |
| 897 | rcu_read_unlock(); |
| 898 | return result; |
| 899 | } |
| 900 | |
| 901 | opp = dev_pm_opp_find_freq_ceil(dev, &min_freq); |
| 902 | if (IS_ERR(opp)) |
| 903 | min_freq = pwr->pwrlevels[pwr->min_pwrlevel].gpu_freq; |
| 904 | |
| 905 | rcu_read_unlock(); |
| 906 | |
| 907 | mutex_lock(&device->mutex); |
| 908 | |
| 909 | max_level = pwr->thermal_pwrlevel; |
| 910 | min_level = pwr->thermal_pwrlevel_floor; |
| 911 | |
| 912 | /* Thermal limit cannot be lower than lowest non-zero operating freq */ |
Deepak Kumar | d91870c | 2018-02-01 16:48:56 +0530 | [diff] [blame] | 913 | for (level = 0; level < (pwr->num_pwrlevels - 1); level++) { |
George Shen | c43f30a | 2018-01-04 13:53:55 -0800 | [diff] [blame] | 914 | if (pwr->pwrlevels[level].gpu_freq == max_freq) |
| 915 | max_level = level; |
| 916 | if (pwr->pwrlevels[level].gpu_freq == min_freq) |
| 917 | min_level = level; |
Deepak Kumar | d91870c | 2018-02-01 16:48:56 +0530 | [diff] [blame] | 918 | } |
George Shen | c43f30a | 2018-01-04 13:53:55 -0800 | [diff] [blame] | 919 | |
| 920 | pwr->thermal_pwrlevel = max_level; |
| 921 | pwr->thermal_pwrlevel_floor = min_level; |
| 922 | |
| 923 | /* Update the current level using the new limit */ |
| 924 | kgsl_pwrctrl_pwrlevel_change(device, pwr->active_pwrlevel); |
| 925 | mutex_unlock(&device->mutex); |
| 926 | |
| 927 | return 0; |
| 928 | } |
| 929 | |
| 930 | /* |
| 931 | * kgsl_opp_add_notifier - add a fine grained notifier. |
| 932 | * @dev: The device |
| 933 | * @nb: Notifier block that will receive updates. |
| 934 | * |
| 935 | * Add a notifier to receive GPU OPP_EVENT_* events |
| 936 | * from the OPP framework. |
| 937 | */ |
| 938 | static int kgsl_opp_add_notifier(struct device *dev, |
| 939 | struct notifier_block *nb) |
| 940 | { |
| 941 | struct srcu_notifier_head *nh; |
| 942 | int ret = 0; |
| 943 | |
| 944 | rcu_read_lock(); |
| 945 | nh = dev_pm_opp_get_notifier(dev); |
| 946 | if (IS_ERR(nh)) |
| 947 | ret = PTR_ERR(nh); |
| 948 | rcu_read_unlock(); |
| 949 | if (!ret) |
| 950 | ret = srcu_notifier_chain_register(nh, nb); |
| 951 | |
| 952 | return ret; |
| 953 | } |
| 954 | |
| 955 | /* |
| 956 | * kgsl_opp_remove_notifier - remove registered opp event notifier. |
| 957 | * @dev: The device |
| 958 | * @nb: Notifier block that will receive updates. |
| 959 | * |
| 960 | * Remove gpu notifier that receives GPU OPP_EVENT_* events |
| 961 | * from the OPP framework. |
| 962 | */ |
| 963 | static int kgsl_opp_remove_notifier(struct device *dev, |
| 964 | struct notifier_block *nb) |
| 965 | { |
| 966 | struct srcu_notifier_head *nh; |
| 967 | int ret = 0; |
| 968 | |
| 969 | rcu_read_lock(); |
| 970 | nh = dev_pm_opp_get_notifier(dev); |
| 971 | if (IS_ERR(nh)) |
| 972 | ret = PTR_ERR(nh); |
| 973 | rcu_read_unlock(); |
| 974 | if (!ret) |
| 975 | ret = srcu_notifier_chain_unregister(nh, nb); |
| 976 | |
| 977 | return ret; |
| 978 | } |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 979 | |
| 980 | /* |
| 981 | * kgsl_pwrscale_init - Initialize pwrscale. |
| 982 | * @dev: The device |
| 983 | * @governor: The initial governor to use. |
| 984 | * |
| 985 | * Initialize devfreq and any non-constant profile data. |
| 986 | */ |
| 987 | int kgsl_pwrscale_init(struct device *dev, const char *governor) |
| 988 | { |
| 989 | struct kgsl_device *device; |
| 990 | struct kgsl_pwrscale *pwrscale; |
| 991 | struct kgsl_pwrctrl *pwr; |
| 992 | struct devfreq *devfreq; |
| 993 | struct devfreq *bus_devfreq; |
| 994 | struct msm_adreno_extended_profile *gpu_profile; |
| 995 | struct devfreq_dev_profile *profile; |
| 996 | struct devfreq_msm_adreno_tz_data *data; |
| 997 | int i, out = 0; |
| 998 | int ret; |
| 999 | |
| 1000 | device = dev_get_drvdata(dev); |
| 1001 | if (device == NULL) |
| 1002 | return -ENODEV; |
| 1003 | |
| 1004 | pwrscale = &device->pwrscale; |
| 1005 | pwr = &device->pwrctrl; |
| 1006 | gpu_profile = &pwrscale->gpu_profile; |
| 1007 | profile = &pwrscale->gpu_profile.profile; |
| 1008 | |
George Shen | c43f30a | 2018-01-04 13:53:55 -0800 | [diff] [blame] | 1009 | pwr->nb.notifier_call = opp_notify; |
| 1010 | |
| 1011 | kgsl_opp_add_notifier(dev, &pwr->nb); |
| 1012 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1013 | srcu_init_notifier_head(&pwrscale->nh); |
| 1014 | |
| 1015 | profile->initial_freq = |
| 1016 | pwr->pwrlevels[pwr->default_pwrlevel].gpu_freq; |
| 1017 | /* Let's start with 10 ms and tune in later */ |
| 1018 | profile->polling_ms = 10; |
| 1019 | |
| 1020 | /* do not include the 'off' level or duplicate freq. levels */ |
| 1021 | for (i = 0; i < (pwr->num_pwrlevels - 1); i++) |
| 1022 | pwrscale->freq_table[out++] = pwr->pwrlevels[i].gpu_freq; |
| 1023 | |
| 1024 | /* |
| 1025 | * Max_state is the number of valid power levels. |
| 1026 | * The valid power levels range from 0 - (max_state - 1) |
| 1027 | */ |
| 1028 | profile->max_state = pwr->num_pwrlevels - 1; |
| 1029 | /* link storage array to the devfreq profile pointer */ |
| 1030 | profile->freq_table = pwrscale->freq_table; |
| 1031 | |
| 1032 | /* if there is only 1 freq, no point in running a governor */ |
| 1033 | if (profile->max_state == 1) |
| 1034 | governor = "performance"; |
| 1035 | |
| 1036 | /* initialize msm-adreno-tz governor specific data here */ |
| 1037 | data = gpu_profile->private_data; |
| 1038 | |
| 1039 | data->disable_busy_time_burst = of_property_read_bool( |
| 1040 | device->pdev->dev.of_node, "qcom,disable-busy-time-burst"); |
| 1041 | |
| 1042 | data->ctxt_aware_enable = |
| 1043 | of_property_read_bool(device->pdev->dev.of_node, |
| 1044 | "qcom,enable-ca-jump"); |
| 1045 | |
| 1046 | if (data->ctxt_aware_enable) { |
| 1047 | if (of_property_read_u32(device->pdev->dev.of_node, |
| 1048 | "qcom,ca-target-pwrlevel", |
| 1049 | &data->bin.ctxt_aware_target_pwrlevel)) |
| 1050 | data->bin.ctxt_aware_target_pwrlevel = 1; |
| 1051 | |
Lynus Vaz | d43d8b1 | 2017-04-17 18:43:04 +0530 | [diff] [blame] | 1052 | if ((data->bin.ctxt_aware_target_pwrlevel > |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1053 | pwr->num_pwrlevels)) |
| 1054 | data->bin.ctxt_aware_target_pwrlevel = 1; |
| 1055 | |
| 1056 | if (of_property_read_u32(device->pdev->dev.of_node, |
| 1057 | "qcom,ca-busy-penalty", |
| 1058 | &data->bin.ctxt_aware_busy_penalty)) |
| 1059 | data->bin.ctxt_aware_busy_penalty = 12000; |
| 1060 | } |
| 1061 | |
Prakash Kamliya | c9b7da0 | 2016-12-20 17:45:41 +0530 | [diff] [blame] | 1062 | if (of_property_read_bool(device->pdev->dev.of_node, |
| 1063 | "qcom,enable-midframe-timer")) { |
| 1064 | kgsl_midframe = kzalloc( |
| 1065 | sizeof(struct kgsl_midframe_info), GFP_KERNEL); |
Rajesh Kemisetti | c361550 | 2017-02-02 20:32:29 +0530 | [diff] [blame] | 1066 | if (kgsl_midframe) { |
| 1067 | hrtimer_init(&kgsl_midframe->timer, |
| 1068 | CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 1069 | kgsl_midframe->timer.function = |
| 1070 | kgsl_pwrscale_midframe_timer; |
| 1071 | kgsl_midframe->device = device; |
| 1072 | } else |
| 1073 | KGSL_PWR_ERR(device, |
| 1074 | "Failed to enable-midframe-timer feature\n"); |
Prakash Kamliya | c9b7da0 | 2016-12-20 17:45:41 +0530 | [diff] [blame] | 1075 | } |
| 1076 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1077 | /* |
| 1078 | * If there is a separate GX power rail, allow |
| 1079 | * independent modification to its voltage through |
| 1080 | * the bus bandwidth vote. |
| 1081 | */ |
| 1082 | if (pwr->bus_control) { |
| 1083 | out = 0; |
| 1084 | while (pwr->bus_ib[out] && out <= pwr->pwrlevels[0].bus_max) { |
| 1085 | pwr->bus_ib[out] = |
| 1086 | pwr->bus_ib[out] >> 20; |
| 1087 | out++; |
| 1088 | } |
| 1089 | data->bus.num = out; |
| 1090 | data->bus.ib = &pwr->bus_ib[0]; |
| 1091 | data->bus.index = &pwr->bus_index[0]; |
| 1092 | data->bus.width = pwr->bus_width; |
| 1093 | } else |
| 1094 | data->bus.num = 0; |
| 1095 | |
| 1096 | devfreq = devfreq_add_device(dev, &pwrscale->gpu_profile.profile, |
| 1097 | governor, pwrscale->gpu_profile.private_data); |
| 1098 | if (IS_ERR(devfreq)) { |
| 1099 | device->pwrscale.enabled = false; |
| 1100 | return PTR_ERR(devfreq); |
| 1101 | } |
| 1102 | |
| 1103 | pwrscale->devfreqptr = devfreq; |
Ram Chandrasekar | cd728d5 | 2017-03-21 11:36:35 -0600 | [diff] [blame] | 1104 | pwrscale->cooling_dev = of_devfreq_cooling_register( |
| 1105 | device->pdev->dev.of_node, devfreq); |
| 1106 | if (IS_ERR(pwrscale->cooling_dev)) |
| 1107 | pwrscale->cooling_dev = NULL; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1108 | |
| 1109 | pwrscale->gpu_profile.bus_devfreq = NULL; |
| 1110 | if (data->bus.num) { |
| 1111 | pwrscale->bus_profile.profile.max_state |
| 1112 | = pwr->num_pwrlevels - 1; |
| 1113 | pwrscale->bus_profile.profile.freq_table |
| 1114 | = pwrscale->freq_table; |
| 1115 | |
| 1116 | bus_devfreq = devfreq_add_device(device->busmondev, |
| 1117 | &pwrscale->bus_profile.profile, "gpubw_mon", NULL); |
| 1118 | if (!IS_ERR(bus_devfreq)) |
| 1119 | pwrscale->gpu_profile.bus_devfreq = bus_devfreq; |
| 1120 | } |
| 1121 | |
| 1122 | ret = sysfs_create_link(&device->dev->kobj, |
| 1123 | &devfreq->dev.kobj, "devfreq"); |
| 1124 | |
| 1125 | pwrscale->devfreq_wq = create_freezable_workqueue("kgsl_devfreq_wq"); |
| 1126 | INIT_WORK(&pwrscale->devfreq_suspend_ws, do_devfreq_suspend); |
| 1127 | INIT_WORK(&pwrscale->devfreq_resume_ws, do_devfreq_resume); |
| 1128 | INIT_WORK(&pwrscale->devfreq_notify_ws, do_devfreq_notify); |
Prakash Kamliya | c9b7da0 | 2016-12-20 17:45:41 +0530 | [diff] [blame] | 1129 | if (kgsl_midframe) |
| 1130 | INIT_WORK(&kgsl_midframe->timer_check_ws, |
| 1131 | kgsl_pwrscale_midframe_timer_check); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1132 | |
| 1133 | pwrscale->next_governor_call = ktime_add_us(ktime_get(), |
| 1134 | KGSL_GOVERNOR_CALL_INTERVAL); |
| 1135 | |
| 1136 | /* history tracking */ |
| 1137 | for (i = 0; i < KGSL_PWREVENT_MAX; i++) { |
| 1138 | pwrscale->history[i].events = kzalloc( |
| 1139 | pwrscale->history[i].size * |
| 1140 | sizeof(struct kgsl_pwr_event), GFP_KERNEL); |
| 1141 | pwrscale->history[i].type = i; |
| 1142 | } |
| 1143 | |
| 1144 | /* Add links to the devfreq sysfs nodes */ |
| 1145 | kgsl_gpu_sysfs_add_link(device->gpu_sysfs_kobj, |
| 1146 | &pwrscale->devfreqptr->dev.kobj, "governor", |
| 1147 | "gpu_governor"); |
| 1148 | kgsl_gpu_sysfs_add_link(device->gpu_sysfs_kobj, |
| 1149 | &pwrscale->devfreqptr->dev.kobj, |
| 1150 | "available_governors", "gpu_available_governor"); |
| 1151 | |
| 1152 | return 0; |
| 1153 | } |
| 1154 | EXPORT_SYMBOL(kgsl_pwrscale_init); |
| 1155 | |
| 1156 | /* |
| 1157 | * kgsl_pwrscale_close - clean up pwrscale |
| 1158 | * @device: the device |
| 1159 | * |
| 1160 | * This function should be called with the device mutex locked. |
| 1161 | */ |
| 1162 | void kgsl_pwrscale_close(struct kgsl_device *device) |
| 1163 | { |
| 1164 | int i; |
| 1165 | struct kgsl_pwrscale *pwrscale; |
George Shen | c43f30a | 2018-01-04 13:53:55 -0800 | [diff] [blame] | 1166 | struct kgsl_pwrctrl *pwr; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1167 | |
George Shen | c43f30a | 2018-01-04 13:53:55 -0800 | [diff] [blame] | 1168 | pwr = &device->pwrctrl; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1169 | pwrscale = &device->pwrscale; |
| 1170 | if (!pwrscale->devfreqptr) |
| 1171 | return; |
Ram Chandrasekar | cd728d5 | 2017-03-21 11:36:35 -0600 | [diff] [blame] | 1172 | if (pwrscale->cooling_dev) |
| 1173 | devfreq_cooling_unregister(pwrscale->cooling_dev); |
Prakash Kamliya | c9b7da0 | 2016-12-20 17:45:41 +0530 | [diff] [blame] | 1174 | |
| 1175 | kgsl_pwrscale_midframe_timer_cancel(device); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1176 | flush_workqueue(pwrscale->devfreq_wq); |
| 1177 | destroy_workqueue(pwrscale->devfreq_wq); |
| 1178 | devfreq_remove_device(device->pwrscale.devfreqptr); |
Prakash Kamliya | c9b7da0 | 2016-12-20 17:45:41 +0530 | [diff] [blame] | 1179 | kfree(kgsl_midframe); |
| 1180 | kgsl_midframe = NULL; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1181 | device->pwrscale.devfreqptr = NULL; |
| 1182 | srcu_cleanup_notifier_head(&device->pwrscale.nh); |
George Shen | c43f30a | 2018-01-04 13:53:55 -0800 | [diff] [blame] | 1183 | kgsl_opp_remove_notifier(&device->pdev->dev, &pwr->nb); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1184 | for (i = 0; i < KGSL_PWREVENT_MAX; i++) |
| 1185 | kfree(pwrscale->history[i].events); |
| 1186 | } |
| 1187 | EXPORT_SYMBOL(kgsl_pwrscale_close); |
| 1188 | |
| 1189 | static void do_devfreq_suspend(struct work_struct *work) |
| 1190 | { |
| 1191 | struct kgsl_pwrscale *pwrscale = container_of(work, |
| 1192 | struct kgsl_pwrscale, devfreq_suspend_ws); |
| 1193 | struct devfreq *devfreq = pwrscale->devfreqptr; |
| 1194 | |
| 1195 | devfreq_suspend_device(devfreq); |
| 1196 | } |
| 1197 | |
| 1198 | static void do_devfreq_resume(struct work_struct *work) |
| 1199 | { |
| 1200 | struct kgsl_pwrscale *pwrscale = container_of(work, |
| 1201 | struct kgsl_pwrscale, devfreq_resume_ws); |
| 1202 | struct devfreq *devfreq = pwrscale->devfreqptr; |
| 1203 | |
| 1204 | devfreq_resume_device(devfreq); |
| 1205 | } |
| 1206 | |
| 1207 | static void do_devfreq_notify(struct work_struct *work) |
| 1208 | { |
| 1209 | struct kgsl_pwrscale *pwrscale = container_of(work, |
| 1210 | struct kgsl_pwrscale, devfreq_notify_ws); |
| 1211 | struct devfreq *devfreq = pwrscale->devfreqptr; |
| 1212 | |
| 1213 | srcu_notifier_call_chain(&pwrscale->nh, |
| 1214 | ADRENO_DEVFREQ_NOTIFY_RETIRE, |
| 1215 | devfreq); |
| 1216 | } |