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