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