]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/video/tegra/host/pod_scaling.c
video: tegra: host: pod: change vic freq tuning
[sojka/nv-tegra/linux-3.10.git] / drivers / video / tegra / host / pod_scaling.c
1 /*
2  * drivers/video/tegra/host/gr3d/pod_scaling.c
3  *
4  * Tegra Graphics Host 3D clock scaling
5  *
6  * Copyright (c) 2012-2015, NVIDIA CORPORATION.  All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms and conditions of the GNU General Public License,
10  * version 2, as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 /*
22  * Power-on-demand clock scaling for nvhost devices
23  *
24  * devfreq calls nvhost_pod_estimate_freq() for estimating the new
25  * frequency for the device. The clocking is done using two properties:
26  *
27  *  (1) Usually the governor receives actively throughput hints that indicate
28  *      whether scaling up or down is required.
29  *  (2) The load of the device is estimated using the busy times from the
30  *      device profile. This information indicates if the device frequency
31  *      should be altered.
32  *
33  */
34
35 #include <linux/devfreq.h>
36 #include <linux/debugfs.h>
37 #include <linux/types.h>
38 #include <linux/clk.h>
39 #include <linux/export.h>
40 #include <linux/slab.h>
41 #include <linux/clk/tegra.h>
42 #include <linux/tegra-soc.h>
43
44 #include <linux/notifier.h>
45 #include <linux/tegra-throughput.h>
46
47 #include <linux/notifier.h>
48 #include <linux/tegra-throughput.h>
49
50 #define CREATE_TRACE_POINTS
51 #include <trace/events/nvhost_podgov.h>
52
53 #include <governor.h>
54
55 #include "nvhost_acm.h"
56 #include "scale3d.h"
57 #include "dev.h"
58
59 #define GET_TARGET_FREQ_DONTSCALE       1
60
61 /* time frame for load and hint tracking - when events come in at a larger
62  * interval, this probably indicates the current estimates are stale
63  */
64 #define GR3D_TIMEFRAME 1000000 /* 1 sec */
65
66 /* the number of frames to use in the running average of load estimates and
67  * throughput hints. Choosing 6 frames targets a window of about 100 msec.
68  * Large flucutuations in frame times require a window that's large enough to
69  * prevent spiky scaling behavior, which in turn exacerbates frame rate
70  * instability.
71  */
72
73 static void podgov_enable(struct devfreq *df, int enable);
74 static void podgov_set_user_ctl(struct devfreq *df, int enable);
75
76 static struct devfreq_governor nvhost_podgov;
77
78 /*******************************************************************************
79  * podgov_info_rec - gr3d scaling governor specific parameters
80  ******************************************************************************/
81
82 struct podgov_info_rec {
83
84         int                     enable;
85         int                     init;
86
87         ktime_t                 last_throughput_hint;
88         ktime_t                 last_scale;
89
90         struct delayed_work     idle_timer;
91
92         unsigned int            p_slowdown_delay;
93         unsigned int            p_block_window;
94         unsigned int            p_use_throughput_hint;
95         unsigned int            p_hint_lo_limit;
96         unsigned int            p_hint_hi_limit;
97         unsigned int            p_scaleup_limit;
98         unsigned int            p_scaledown_limit;
99         unsigned int            p_smooth;
100         int                     p_damp;
101         int                     p_load_max;
102         int                     p_load_target;
103         int                     p_bias;
104         unsigned int            p_user;
105         unsigned int            p_freq_request;
106
107         long                    idle;
108
109         int                     adjustment_type;
110         unsigned long           adjustment_frequency;
111
112         int                     last_event_type;
113
114         struct devfreq          *power_manager;
115         struct dentry           *debugdir;
116
117         unsigned long           *freqlist;
118         int                     freq_count;
119
120         unsigned int            idle_avg;
121         int                     freq_avg;
122         unsigned int            hint_avg;
123         int                     block;
124         struct kobj_attribute   enable_3d_scaling_attr;
125         struct kobj_attribute   user_attr;
126         struct kobj_attribute   freq_request_attr;
127
128         struct notifier_block   throughput_hint_notifier;
129 };
130
131 /*******************************************************************************
132  * Adjustment type is used to tell the source that requested frequency re-
133  * estimation. Type ADJUSTMENT_LOCAL indicates that the re-estimation was
134  * initiated by the governor itself. This happens when one of the worker
135  * threads want to adjust the frequency.
136  *
137  * ADJUSTMENT_DEVICE_REQ (default value) indicates that the adjustment was
138  * initiated by a device event.
139  ******************************************************************************/
140
141 enum podgov_adjustment_type {
142         ADJUSTMENT_LOCAL = 0,
143         ADJUSTMENT_DEVICE_REQ = 1
144 };
145
146
147 static void stop_podgov_workers(struct podgov_info_rec *podgov)
148 {
149         /* idle_timer can rearm itself */
150         do {
151                 cancel_delayed_work_sync(&podgov->idle_timer);
152         } while (delayed_work_pending(&podgov->idle_timer));
153 }
154
155 /*******************************************************************************
156  * scaling_limit(df, freq)
157  *
158  * Limit the given frequency
159  ******************************************************************************/
160
161 static void scaling_limit(struct devfreq *df, unsigned long *freq)
162 {
163         if (*freq < df->min_freq)
164                 *freq = df->min_freq;
165         else if (*freq > df->max_freq)
166                 *freq = df->max_freq;
167 }
168
169 /*******************************************************************************
170  * nvhost_pod_suspend(dev)
171  *
172  * Prepare the device for suspend
173  ******************************************************************************/
174
175 static void nvhost_pod_suspend(struct devfreq *df)
176 {
177         struct podgov_info_rec *podgov;
178
179         mutex_lock(&df->lock);
180
181         podgov = df->data;
182         if (!(df->governor == &nvhost_podgov &&
183               podgov && podgov->enable)) {
184                 mutex_unlock(&df->lock);
185                 return;
186         }
187         mutex_unlock(&df->lock);
188
189         stop_podgov_workers(podgov);
190 }
191
192 /*******************************************************************************
193  * podgov_enable(dev, enable)
194  *
195  * This function enables (enable=1) or disables (enable=0) the automatic scaling
196  * of the device. If the device is disabled, the device's clock is set to its
197  * maximum.
198  ******************************************************************************/
199
200 static void podgov_enable(struct devfreq *df, int enable)
201 {
202         struct device *dev = df->dev.parent;
203         struct podgov_info_rec *podgov;
204
205         /* make sure the device is alive before doing any scaling */
206         pm_runtime_get_noresume(dev);
207
208         mutex_lock(&df->lock);
209
210         podgov = df->data;
211
212         trace_podgov_enabled(df->dev.parent, enable);
213
214         /* bad configuration. quit. */
215         if (df->min_freq == df->max_freq)
216                 goto exit_unlock;
217
218         /* store the enable information */
219         podgov->enable = enable;
220
221         /* skip local adjustment if we are enabling or the device is
222          * suspended */
223         if (enable || !pm_runtime_active(dev))
224                 goto exit_unlock;
225
226         /* full speed */
227         podgov->adjustment_frequency = df->max_freq;
228         podgov->adjustment_type = ADJUSTMENT_LOCAL;
229         update_devfreq(df);
230
231         mutex_unlock(&df->lock);
232
233         pm_runtime_put(dev);
234
235         stop_podgov_workers(podgov);
236
237         return;
238
239 exit_unlock:
240         mutex_unlock(&df->lock);
241         pm_runtime_put(dev);
242 }
243
244 /*****************************************************************************
245  * podgov_set_user_ctl(dev, user)
246  *
247  * This function enables or disables user control of the gpu. If user control
248  * is enabled, setting the freq_request controls the gpu frequency, and other
249  * gpu scaling mechanisms are disabled.
250  ******************************************************************************/
251
252 static void podgov_set_user_ctl(struct devfreq *df, int user)
253 {
254         struct device *dev = df->dev.parent;
255         struct podgov_info_rec *podgov;
256         int old_user;
257
258         /* make sure the device is alive before doing any scaling */
259         pm_runtime_get_noresume(dev);
260
261         mutex_lock(&df->lock);
262         podgov = df->data;
263
264         trace_podgov_set_user_ctl(df->dev.parent, user);
265
266         /* store the new user value */
267         old_user = podgov->p_user;
268         podgov->p_user = user;
269
270         /* skip scaling, if scaling (or the whole device) is turned off
271          * - or the scaling already was in user mode */
272         if (!pm_runtime_active(dev) || !podgov->enable ||
273             !(user && !old_user))
274                 goto exit_unlock;
275
276         /* write request */
277         podgov->adjustment_frequency = podgov->p_freq_request;
278         podgov->adjustment_type = ADJUSTMENT_LOCAL;
279         update_devfreq(df);
280
281         mutex_unlock(&df->lock);
282         pm_runtime_put(dev);
283
284         stop_podgov_workers(podgov);
285
286         return;
287
288 exit_unlock:
289         mutex_unlock(&df->lock);
290         pm_runtime_put(dev);
291 }
292
293 /*****************************************************************************
294  * podgov_set_freq_request(dev, user)
295  *
296  * Set the current freq request. If scaling is enabled, and podgov user space
297  * control is enabled, this will set the gpu frequency.
298  ******************************************************************************/
299
300 static void podgov_set_freq_request(struct devfreq *df, int freq_request)
301 {
302         struct device *dev = df->dev.parent;
303         struct podgov_info_rec *podgov;
304
305         /* make sure the device is alive before doing any scaling */
306         pm_runtime_get_noresume(dev);
307
308         mutex_lock(&df->lock);
309
310         podgov = df->data;
311
312         trace_podgov_set_freq_request(df->dev.parent, freq_request);
313
314         podgov->p_freq_request = freq_request;
315
316         /* update the request only if podgov is enabled, device is turned on
317          * and the scaling is in user mode */
318         if (podgov->enable && podgov->p_user &&
319             pm_runtime_active(dev)) {
320                 podgov->adjustment_frequency = freq_request;
321                 podgov->adjustment_type = ADJUSTMENT_LOCAL;
322                 update_devfreq(df);
323         }
324
325         mutex_unlock(&df->lock);
326         pm_runtime_put(dev);
327 }
328
329
330 /*******************************************************************************
331  * freq = scaling_state_check(df, time)
332  *
333  * This handler is called to adjust the frequency of the device. The function
334  * returns the desired frequency for the clock. If there is no need to tune the
335  * clock immediately, 0 is returned.
336  ******************************************************************************/
337
338 static unsigned long scaling_state_check(struct devfreq *df, ktime_t time)
339 {
340         struct podgov_info_rec *podgov = df->data;
341         unsigned long dt;
342         long max_boost, load, damp, freq, boost, res;
343
344         dt = (unsigned long) ktime_us_delta(time, podgov->last_scale);
345         if (dt < podgov->p_block_window || df->previous_freq == 0)
346                 return 0;
347
348         /* convert to mhz to avoid overflow */
349         freq = df->previous_freq / 1000000;
350         max_boost = (df->max_freq/3) / 1000000;
351
352         /* calculate and trace load */
353         load = 1000 - podgov->idle_avg;
354         trace_podgov_busy(df->dev.parent, load);
355         damp = podgov->p_damp;
356
357         if ((1000 - podgov->idle) > podgov->p_load_max) {
358                 /* if too busy, scale up max/3, do not damp */
359                 boost = max_boost;
360                 damp = 10;
361
362         } else {
363                 /* boost = bias * freq * (load - target)/target */
364                 boost = (load - podgov->p_load_target);
365                 boost *= (podgov->p_bias * freq);
366                 boost /= (100 * podgov->p_load_target);
367
368                 /* clamp to max boost */
369                 boost = (boost < max_boost) ? boost : max_boost;
370         }
371
372         /* calculate new request */
373         res = freq + boost;
374
375         /* Maintain average request */
376         podgov->freq_avg = (podgov->freq_avg * podgov->p_smooth) + res;
377         podgov->freq_avg /= (podgov->p_smooth+1);
378
379         /* Applying damping to frequencies */
380         res = ((damp * res) + ((10 - damp)*podgov->freq_avg)) / 10;
381
382         /* Convert to hz, limit, and apply */
383         res = res * 1000000;
384         scaling_limit(df, &res);
385         trace_podgov_scaling_state_check(df->dev.parent,
386                                          df->previous_freq, res);
387         return res;
388 }
389
390 /*******************************************************************************
391  * freqlist_up(podgov, target, steps)
392  *
393  * This function determines the frequency that is "steps" frequency steps
394  * higher compared to the target frequency.
395  ******************************************************************************/
396
397 int freqlist_up(struct podgov_info_rec *podgov, unsigned long target, int steps)
398 {
399         int i, pos;
400
401         for (i = 0; i < podgov->freq_count; i++)
402                 if (podgov->freqlist[i] >= target)
403                         break;
404
405         pos = min(podgov->freq_count - 1, i + steps);
406         return podgov->freqlist[pos];
407 }
408
409 /*******************************************************************************
410  * freqlist_down(podgov, target, steps)
411  *
412  * This function determines the frequency that is "steps" frequency steps
413  * lower compared to the target frequency.
414  ******************************************************************************/
415
416 int freqlist_down(struct podgov_info_rec *podgov, unsigned long target,
417                   int steps)
418 {
419         int i, pos;
420
421         for (i = podgov->freq_count - 1; i >= 0; i--)
422                 if (podgov->freqlist[i] <= target)
423                         break;
424
425         pos = max(0, i - steps);
426         return podgov->freqlist[pos];
427 }
428
429 /*******************************************************************************
430  * podgov_idle_handler(work)
431  *
432  * This handler is called after the device has been idle long enough. This
433  * handler forms a (positive) feedback loop by notifying idle to the device.
434  ******************************************************************************/
435
436 static void podgov_idle_handler(struct work_struct *work)
437 {
438         struct delayed_work *idle_timer =
439                 container_of(work, struct delayed_work, work);
440         struct podgov_info_rec *podgov =
441                 container_of(idle_timer, struct podgov_info_rec, idle_timer);
442         struct devfreq *df = podgov->power_manager;
443
444         mutex_lock(&df->lock);
445
446         if (!podgov->enable) {
447                 mutex_unlock(&df->lock);
448                 return;
449         }
450
451         if (!podgov->last_event_type &&
452             df->previous_freq > df->min_freq &&
453             podgov->p_user == false)
454                 update_devfreq(df);
455
456         mutex_unlock(&df->lock);
457 }
458
459 #ifdef CONFIG_TEGRA_THROUGHPUT
460 /*******************************************************************************
461  * nvhost_scale3d_set_throughput_hint(hint)
462  *
463  * This function can be used to request scaling up or down based on the
464  * required throughput
465  ******************************************************************************/
466
467 static int nvhost_scale3d_set_throughput_hint(struct notifier_block *nb,
468                                               unsigned long action, void *data)
469 {
470         struct podgov_info_rec *podgov =
471                 container_of(nb, struct podgov_info_rec,
472                              throughput_hint_notifier);
473         struct devfreq *df = podgov->power_manager;
474         struct device *dev = df->dev.parent;
475         int hint = tegra_throughput_get_hint();
476         long idle;
477         unsigned long curr, target;
478         int avg_idle, avg_hint, scale_score;
479         unsigned int smooth;
480
481         /* make sure the device is alive before doing any scaling */
482         pm_runtime_get_noresume(dev);
483         if (!pm_runtime_active(dev)) {
484                 pm_runtime_put(dev);
485                 return 0;
486         }
487
488         mutex_lock(&df->lock);
489
490         podgov->block--;
491
492         if (!podgov->enable ||
493                 !podgov->p_use_throughput_hint ||
494                 podgov->block > 0)
495                 goto exit_unlock;
496
497         trace_podgov_hint(df->dev.parent, podgov->idle, hint);
498         podgov->last_throughput_hint = ktime_get();
499
500         curr = df->previous_freq;
501         idle = podgov->idle;
502         avg_idle = podgov->idle_avg;
503         smooth = podgov->p_smooth;
504
505         /* compute averages usings exponential-moving-average */
506         avg_hint = ((smooth*podgov->hint_avg + hint)/(smooth+1));
507         podgov->hint_avg = avg_hint;
508
509         /* set the target using avg_hint and avg_idle */
510         target = curr;
511         if (avg_hint < podgov->p_hint_lo_limit) {
512                 target = freqlist_up(podgov, curr, 1);
513         } else {
514                 scale_score = avg_idle + avg_hint;
515                 if (scale_score > podgov->p_scaledown_limit)
516                         target = freqlist_down(podgov, curr, 1);
517                 else if (scale_score < podgov->p_scaleup_limit
518                                 && hint < podgov->p_hint_hi_limit)
519                         target = freqlist_up(podgov, curr, 1);
520         }
521
522         /* clamp and apply target */
523         scaling_limit(df, &target);
524         if (target != curr) {
525                 podgov->block = podgov->p_smooth;
526                 trace_podgov_do_scale(df->dev.parent,
527                                       df->previous_freq, target);
528                 podgov->adjustment_frequency = target;
529                 podgov->adjustment_type = ADJUSTMENT_LOCAL;
530                 update_devfreq(df);
531         }
532
533         trace_podgov_print_target(df->dev.parent, idle, avg_idle,
534                                   curr / 1000000, target, hint, avg_hint);
535
536 exit_unlock:
537         mutex_unlock(&df->lock);
538         pm_runtime_put(dev);
539         return NOTIFY_OK;
540 }
541 #endif
542
543 /*******************************************************************************
544  * debugfs interface for controlling 3d clock scaling on the fly
545  ******************************************************************************/
546
547 #ifdef CONFIG_DEBUG_FS
548
549 static void nvhost_scale3d_debug_init(struct devfreq *df)
550 {
551         struct podgov_info_rec *podgov = df->data;
552         struct dentry *f;
553         char dirname[128];
554
555         snprintf(dirname, sizeof(dirname), "%s_scaling",
556                 to_platform_device(df->dev.parent)->name);
557
558         if (!podgov)
559                 return;
560
561         podgov->debugdir = debugfs_create_dir(dirname, NULL);
562         if (!podgov->debugdir) {
563                 pr_err("podgov: can\'t create debugfs directory\n");
564                 return;
565         }
566
567 #define CREATE_PODGOV_FILE(fname) \
568         do {\
569                 f = debugfs_create_u32(#fname, S_IRUGO | S_IWUSR, \
570                         podgov->debugdir, &podgov->p_##fname); \
571                 if (NULL == f) { \
572                         pr_err("podgov: can\'t create file " #fname "\n"); \
573                         return; \
574                 } \
575         } while (0)
576
577         CREATE_PODGOV_FILE(block_window);
578         CREATE_PODGOV_FILE(load_max);
579         CREATE_PODGOV_FILE(load_target);
580         CREATE_PODGOV_FILE(bias);
581         CREATE_PODGOV_FILE(damp);
582         CREATE_PODGOV_FILE(use_throughput_hint);
583         CREATE_PODGOV_FILE(hint_hi_limit);
584         CREATE_PODGOV_FILE(hint_lo_limit);
585         CREATE_PODGOV_FILE(scaleup_limit);
586         CREATE_PODGOV_FILE(scaledown_limit);
587         CREATE_PODGOV_FILE(smooth);
588         CREATE_PODGOV_FILE(slowdown_delay);
589 #undef CREATE_PODGOV_FILE
590 }
591
592 static void nvhost_scale3d_debug_deinit(struct devfreq *df)
593 {
594         struct podgov_info_rec *podgov = df->data;
595
596         debugfs_remove_recursive(podgov->debugdir);
597 }
598
599 #else
600 static void nvhost_scale3d_debug_init(struct devfreq *df)
601 {
602         (void)df;
603 }
604
605 static void nvhost_scale3d_debug_deinit(struct devfreq *df)
606 {
607         (void)df;
608 }
609 #endif
610
611 /*******************************************************************************
612  * sysfs interface for enabling/disabling 3d scaling
613  ******************************************************************************/
614
615 static ssize_t enable_3d_scaling_show(struct kobject *kobj,
616                                       struct kobj_attribute *attr,
617                                       char *buf)
618 {
619         struct podgov_info_rec *podgov = container_of(attr,
620                                                       struct podgov_info_rec,
621                                                       enable_3d_scaling_attr);
622         ssize_t res;
623
624         res = snprintf(buf, PAGE_SIZE, "%d\n", podgov->enable);
625
626         return res;
627 }
628
629 static ssize_t enable_3d_scaling_store(struct kobject *kobj,
630                                        struct kobj_attribute *attr,
631                                        const char *buf, size_t count)
632 {
633         struct podgov_info_rec *podgov = container_of(attr,
634                                                       struct podgov_info_rec,
635                                                       enable_3d_scaling_attr);
636         unsigned long val = 0;
637
638         if (kstrtoul(buf, 10, &val) < 0)
639                 return -EINVAL;
640
641         podgov_enable(podgov->power_manager, val);
642
643         return count;
644 }
645
646 /*******************************************************************************
647  * sysfs interface for user space control
648  * user = [0,1] disables / enabled user space control
649  * freq_request is the sysfs node user space writes frequency requests to
650  ******************************************************************************/
651
652 static ssize_t user_show(struct kobject *kobj,
653                          struct kobj_attribute *attr,
654                          char *buf)
655 {
656         struct podgov_info_rec *podgov =
657                 container_of(attr, struct podgov_info_rec, user_attr);
658         ssize_t res;
659
660         res = snprintf(buf, PAGE_SIZE, "%d\n", podgov->p_user);
661
662         return res;
663 }
664
665 static ssize_t user_store(struct kobject *kobj,
666                           struct kobj_attribute *attr,
667                           const char *buf, size_t count)
668 {
669         struct podgov_info_rec *podgov =
670                 container_of(attr, struct podgov_info_rec, user_attr);
671         unsigned long val = 0;
672
673         if (kstrtoul(buf, 10, &val) < 0)
674                 return -EINVAL;
675
676         podgov_set_user_ctl(podgov->power_manager, val);
677
678         return count;
679 }
680
681 static ssize_t freq_request_show(struct kobject *kobj,
682                                  struct kobj_attribute *attr,
683                                  char *buf)
684 {
685         struct podgov_info_rec *podgov =
686                 container_of(attr, struct podgov_info_rec, freq_request_attr);
687         ssize_t res;
688
689         res = snprintf(buf, PAGE_SIZE, "%d\n", podgov->p_freq_request);
690
691         return res;
692 }
693
694 static ssize_t freq_request_store(struct kobject *kobj,
695                                   struct kobj_attribute *attr,
696                                   const char *buf, size_t count)
697 {
698         struct podgov_info_rec *podgov =
699                 container_of(attr, struct podgov_info_rec, freq_request_attr);
700         unsigned long val = 0;
701
702         if (kstrtoul(buf, 10, &val) < 0)
703                 return -EINVAL;
704
705         podgov_set_freq_request(podgov->power_manager, val);
706
707         return count;
708 }
709
710 /*******************************************************************************
711  * nvhost_pod_estimate_freq(df, freq)
712  *
713  * This function is called for re-estimating the frequency. The function is
714  * called in three conditions:
715  *
716  *  (1) Internal request to change the frequency. In this case a new clock
717  *      target is immediately set for the device.
718  *  (2) Call from the client (something has happened and re-estimation
719  *      is required).
720  *  (3) Some other reason (i.e. periodic call)
721  *
722  ******************************************************************************/
723
724 static int nvhost_pod_estimate_freq(struct devfreq *df,
725                                     unsigned long *freq)
726 {
727         struct podgov_info_rec *podgov = df->data;
728         struct devfreq_dev_status dev_stat;
729         int stat;
730         ktime_t now;
731
732         stat = df->profile->get_dev_status(df->dev.parent, &dev_stat);
733         if (stat < 0)
734                 return stat;
735
736         /* Ensure maximal clock when scaling is disabled */
737         if (!podgov->enable) {
738                 *freq = df->max_freq;
739                 return 0;
740         }
741
742         if (podgov->p_user) {
743                 *freq = podgov->p_freq_request;
744                 return 0;
745         }
746
747         stat = 0;
748         now = ktime_get();
749
750         /* Local adjustments (i.e. requests from kernel threads) are
751          * handled here */
752
753         if (podgov->adjustment_type == ADJUSTMENT_LOCAL) {
754
755                 podgov->adjustment_type = ADJUSTMENT_DEVICE_REQ;
756
757                 /* Do not do unnecessary scaling */
758                 scaling_limit(df, &podgov->adjustment_frequency);
759
760                 /* Round the frequency and check if we're already there */
761                 if (freqlist_up(podgov, podgov->adjustment_frequency, 0) ==
762                     dev_stat.current_frequency)
763                         return GET_TARGET_FREQ_DONTSCALE;
764
765                 trace_podgov_estimate_freq(df->dev.parent,
766                                            df->previous_freq,
767                                            podgov->adjustment_frequency);
768
769                 *freq = podgov->adjustment_frequency;
770                 return 0;
771         }
772
773         *freq = dev_stat.current_frequency;
774
775         /* Sustain local variables */
776         podgov->last_event_type = dev_stat.busy;
777         podgov->idle = 1000 * (dev_stat.total_time - dev_stat.busy_time);
778         podgov->idle = podgov->idle / dev_stat.total_time;
779         podgov->idle_avg = (podgov->p_smooth * podgov->idle_avg) +
780                 podgov->idle;
781         podgov->idle_avg = podgov->idle_avg / (podgov->p_smooth + 1);
782
783         /* if throughput hint enabled, and last hint is recent enough, return */
784         if (podgov->p_use_throughput_hint &&
785                 ktime_us_delta(now, podgov->last_throughput_hint) < 1000000)
786                 return GET_TARGET_FREQ_DONTSCALE;
787
788         if (dev_stat.busy) {
789                 cancel_delayed_work(&podgov->idle_timer);
790                 *freq = scaling_state_check(df, now);
791         } else {
792                 /* Launch a work to slowdown the gpu */
793                 *freq = scaling_state_check(df, now);
794                 schedule_delayed_work(&podgov->idle_timer,
795                         msecs_to_jiffies(podgov->p_slowdown_delay));
796         }
797
798         if (!(*freq) ||
799             (freqlist_up(podgov, *freq, 0) == dev_stat.current_frequency))
800                 return GET_TARGET_FREQ_DONTSCALE;
801
802         podgov->last_scale = now;
803
804         trace_podgov_estimate_freq(df->dev.parent, df->previous_freq, *freq);
805
806
807         return 0;
808 }
809
810 /*******************************************************************************
811  * nvhost_pod_init(struct devfreq *df)
812  *
813  * Governor initialisation.
814  ******************************************************************************/
815
816 static int nvhost_pod_init(struct devfreq *df)
817 {
818         struct podgov_info_rec *podgov;
819         struct platform_device *d = to_platform_device(df->dev.parent);
820         ktime_t now = ktime_get();
821         enum tegra_chipid cid = tegra_get_chipid();
822
823         struct devfreq_dev_status dev_stat;
824         int stat = 0;
825
826         struct kobj_attribute *attr = NULL;
827
828         podgov = kzalloc(sizeof(struct podgov_info_rec), GFP_KERNEL);
829         if (!podgov)
830                 goto err_alloc_podgov;
831         df->data = (void *)podgov;
832
833         /* Initialise workers */
834         INIT_DELAYED_WORK(&podgov->idle_timer, podgov_idle_handler);
835
836         /* Set scaling parameter defaults */
837         podgov->enable = 1;
838         podgov->block = 0;
839         podgov->p_use_throughput_hint = 1;
840
841         if (!strcmp(d->name, "vic03.0")) {
842                 podgov->p_load_max = 100;
843                 podgov->p_load_target = 10;
844                 podgov->p_bias = 80;
845                 podgov->p_hint_lo_limit = 500;
846                 podgov->p_hint_hi_limit = 997;
847                 podgov->p_scaleup_limit = 1100;
848                 podgov->p_scaledown_limit = 1300;
849                 podgov->p_smooth = 30;
850                 podgov->p_damp = 9;
851         } else {
852                 switch (cid) {
853                 case TEGRA_CHIPID_TEGRA14:
854                 case TEGRA_CHIPID_TEGRA11:
855                 case TEGRA_CHIPID_TEGRA12:
856                 case TEGRA_CHIPID_TEGRA13:
857                         podgov->p_load_max = 900;
858                         podgov->p_load_target = 700;
859                         podgov->p_bias = 80;
860                         podgov->p_hint_lo_limit = 500;
861                         podgov->p_hint_hi_limit = 997;
862                         podgov->p_scaleup_limit = 1100;
863                         podgov->p_scaledown_limit = 1300;
864                         podgov->p_smooth = 10;
865                         podgov->p_damp = 7;
866                         podgov->p_use_throughput_hint = 0;
867                         break;
868                 default:
869                         pr_err("%s: un-supported chip id\n", __func__);
870                         goto err_unsupported_chip_id;
871                         break;
872                 }
873         }
874
875         podgov->p_slowdown_delay = 10;
876         podgov->p_block_window = 50000;
877         podgov->adjustment_type = ADJUSTMENT_DEVICE_REQ;
878         podgov->p_user = 0;
879
880         /* Reset clock counters */
881         podgov->last_throughput_hint = now;
882         podgov->last_scale = now;
883
884         podgov->power_manager = df;
885
886         /* Get the current status of the device */
887         stat = df->profile->get_dev_status(df->dev.parent, &dev_stat);
888
889         attr = &podgov->enable_3d_scaling_attr;
890         attr->attr.name = "enable_3d_scaling";
891         attr->attr.mode = S_IWUSR | S_IRUGO;
892         attr->show = enable_3d_scaling_show;
893         attr->store = enable_3d_scaling_store;
894         sysfs_attr_init(&attr->attr);
895         if (sysfs_create_file(&df->dev.parent->kobj, &attr->attr))
896                 goto err_create_enable_sysfs_entry;
897
898         attr = &podgov->freq_request_attr;
899         attr->attr.name = "freq_request";
900         attr->attr.mode = S_IWUSR | S_IRUGO;
901         attr->show = freq_request_show;
902         attr->store = freq_request_store;
903         sysfs_attr_init(&attr->attr);
904         if (sysfs_create_file(&df->dev.parent->kobj, &attr->attr))
905                 goto err_create_request_sysfs_entry;
906
907         attr = &podgov->user_attr;
908         attr->attr.name = "user";
909         attr->attr.mode = S_IWUSR | S_IRUGO;
910         attr->show = user_show;
911         attr->store = user_store;
912         sysfs_attr_init(&attr->attr);
913         if (sysfs_create_file(&df->dev.parent->kobj, &attr->attr))
914                 goto err_create_user_sysfs_entry;
915
916         podgov->freq_count = df->profile->max_state;
917         podgov->freqlist = df->profile->freq_table;
918         if (!podgov->freq_count || !podgov->freqlist)
919                 goto err_get_freqs;
920
921         /* store the limits */
922         df->min_freq = podgov->freqlist[0];
923         df->max_freq = podgov->freqlist[podgov->freq_count - 1];
924         podgov->p_freq_request = df->max_freq;
925
926         podgov->idle_avg = 0;
927         podgov->freq_avg = 0;
928         podgov->hint_avg = 0;
929
930         nvhost_scale3d_debug_init(df);
931
932         /* register the governor to throughput hint notifier chain */
933 #ifdef CONFIG_TEGRA_THROUGHPUT
934         podgov->throughput_hint_notifier.notifier_call =
935                 &nvhost_scale3d_set_throughput_hint;
936         blocking_notifier_chain_register(&throughput_notifier_list,
937                                          &podgov->throughput_hint_notifier);
938 #endif
939
940         return 0;
941
942 err_get_freqs:
943         sysfs_remove_file(&df->dev.parent->kobj, &podgov->user_attr.attr);
944 err_create_user_sysfs_entry:
945         sysfs_remove_file(&df->dev.parent->kobj,
946                           &podgov->freq_request_attr.attr);
947 err_create_request_sysfs_entry:
948         sysfs_remove_file(&df->dev.parent->kobj,
949                           &podgov->enable_3d_scaling_attr.attr);
950 err_create_enable_sysfs_entry:
951         dev_err(&d->dev, "failed to create sysfs attributes");
952 err_unsupported_chip_id:
953         kfree(podgov);
954 err_alloc_podgov:
955         return -ENOMEM;
956 }
957
958 /*******************************************************************************
959  * nvhost_pod_exit(struct devfreq *df)
960  *
961  * Clean up governor data structures
962  ******************************************************************************/
963
964 static void nvhost_pod_exit(struct devfreq *df)
965 {
966         struct podgov_info_rec *podgov = df->data;
967
968 #ifdef CONFIG_TEGRA_THROUGHPUT
969         blocking_notifier_chain_unregister(&throughput_notifier_list,
970                                            &podgov->throughput_hint_notifier);
971 #endif
972         cancel_delayed_work(&podgov->idle_timer);
973
974         sysfs_remove_file(&df->dev.parent->kobj, &podgov->user_attr.attr);
975         sysfs_remove_file(&df->dev.parent->kobj,
976                           &podgov->freq_request_attr.attr);
977         sysfs_remove_file(&df->dev.parent->kobj,
978                           &podgov->enable_3d_scaling_attr.attr);
979
980         nvhost_scale3d_debug_deinit(df);
981
982         kfree(podgov);
983 }
984
985 static int nvhost_pod_event_handler(struct devfreq *df,
986                         unsigned int event, void *data)
987 {
988         int ret = 0;
989
990         switch (event) {
991         case DEVFREQ_GOV_START:
992                 ret = nvhost_pod_init(df);
993                 break;
994         case DEVFREQ_GOV_STOP:
995                 nvhost_pod_exit(df);
996                 break;
997         case DEVFREQ_GOV_SUSPEND:
998                 nvhost_pod_suspend(df);
999                 break;
1000         default:
1001                 break;
1002         }
1003
1004         return ret;
1005 }
1006
1007 static struct devfreq_governor nvhost_podgov = {
1008         .name = "nvhost_podgov",
1009         .get_target_freq = nvhost_pod_estimate_freq,
1010         .event_handler = nvhost_pod_event_handler,
1011 };
1012
1013
1014 static int __init podgov_init(void)
1015 {
1016         return devfreq_add_governor(&nvhost_podgov);
1017 }
1018
1019 static void __exit podgov_exit(void)
1020 {
1021         devfreq_remove_governor(&nvhost_podgov);
1022 }
1023
1024 /* governor must be registered before initialising client devices */
1025 rootfs_initcall(podgov_init);
1026 module_exit(podgov_exit);
1027