]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/video/tegra/host/gr3d/pod_scaling.c
7575571184e768cb17505d51beb650a370dbb286
[sojka/nv-tegra/linux-3.10.git] / drivers / video / tegra / host / gr3d / 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-2014, 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(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(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(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(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->previous_freq, res);
386         return res;
387 }
388
389 /*******************************************************************************
390  * freqlist_up(podgov, target, steps)
391  *
392  * This function determines the frequency that is "steps" frequency steps
393  * higher compared to the target frequency.
394  ******************************************************************************/
395
396 int freqlist_up(struct podgov_info_rec *podgov, unsigned long target, int steps)
397 {
398         int i, pos;
399
400         for (i = 0; i < podgov->freq_count; i++)
401                 if (podgov->freqlist[i] >= target)
402                         break;
403
404         pos = min(podgov->freq_count - 1, i + steps);
405         return podgov->freqlist[pos];
406 }
407
408 /*******************************************************************************
409  * freqlist_down(podgov, target, steps)
410  *
411  * This function determines the frequency that is "steps" frequency steps
412  * lower compared to the target frequency.
413  ******************************************************************************/
414
415 int freqlist_down(struct podgov_info_rec *podgov, unsigned long target, int steps)
416 {
417         int i, pos;
418
419         for (i = podgov->freq_count - 1; i >= 0; i--)
420                 if (podgov->freqlist[i] <= target)
421                         break;
422
423         pos = max(0, i - steps);
424         return podgov->freqlist[pos];
425 }
426
427 /*******************************************************************************
428  * podgov_idle_handler(work)
429  *
430  * This handler is called after the device has been idle long enough. This
431  * handler forms a (positive) feedback loop by notifying idle to the device.
432  ******************************************************************************/
433
434 static void podgov_idle_handler(struct work_struct *work)
435 {
436         struct delayed_work *idle_timer =
437                 container_of(work, struct delayed_work, work);
438         struct podgov_info_rec *podgov =
439                 container_of(idle_timer, struct podgov_info_rec, idle_timer);
440         struct devfreq *df = podgov->power_manager;
441
442         mutex_lock(&df->lock);
443
444         if (!podgov->enable) {
445                 mutex_unlock(&df->lock);
446                 return;
447         }
448
449         if (!podgov->last_event_type &&
450             df->previous_freq > df->min_freq &&
451             podgov->p_user == false)
452                 update_devfreq(df);
453
454         mutex_unlock(&df->lock);
455 }
456
457 #ifdef CONFIG_TEGRA_THROUGHPUT
458 /*******************************************************************************
459  * nvhost_scale3d_set_throughput_hint(hint)
460  *
461  * This function can be used to request scaling up or down based on the
462  * required throughput
463  ******************************************************************************/
464
465 static int nvhost_scale3d_set_throughput_hint(struct notifier_block *nb,
466                                               unsigned long action, void *data)
467 {
468         struct podgov_info_rec *podgov =
469                 container_of(nb, struct podgov_info_rec,
470                              throughput_hint_notifier);
471         struct devfreq *df = podgov->power_manager;
472         struct device *dev = df->dev.parent;
473         int hint = tegra_throughput_get_hint();
474         long idle;
475         unsigned long curr, target;
476         int avg_idle, avg_hint, scale_score;
477         unsigned int smooth;
478
479         /* make sure the device is alive before doing any scaling */
480         pm_runtime_get_noresume(dev);
481         if (!pm_runtime_active(dev)) {
482                 pm_runtime_put(dev);
483                 return 0;
484         }
485
486         mutex_lock(&df->lock);
487
488         podgov->block--;
489
490         if (!podgov->enable ||
491                 !podgov->p_use_throughput_hint ||
492                 podgov->block > 0)
493                 goto exit_unlock;
494
495         trace_podgov_hint(podgov->idle, hint);
496         podgov->last_throughput_hint = ktime_get();
497
498         curr = df->previous_freq;
499         idle = podgov->idle;
500         avg_idle = podgov->idle_avg;
501         smooth = podgov->p_smooth;
502
503         /* compute averages usings exponential-moving-average */
504         avg_hint = ((smooth*podgov->hint_avg + hint)/(smooth+1));
505         podgov->hint_avg = avg_hint;
506
507         /* set the target using avg_hint and avg_idle */
508         target = curr;
509         if (avg_hint < podgov->p_hint_lo_limit) {
510                 target = freqlist_up(podgov, curr, 1);
511         } else {
512                 scale_score = avg_idle + avg_hint;
513                 if (scale_score > podgov->p_scaledown_limit)
514                         target = freqlist_down(podgov, curr, 1);
515                 else if (scale_score < podgov->p_scaleup_limit
516                                 && hint < podgov->p_hint_hi_limit)
517                         target = freqlist_up(podgov, curr, 1);
518         }
519
520         /* clamp and apply target */
521         scaling_limit(df, &target);
522         if (target != curr) {
523                 podgov->block = podgov->p_smooth;
524                 trace_podgov_do_scale(df->previous_freq, target);
525                 podgov->adjustment_frequency = target;
526                 podgov->adjustment_type = ADJUSTMENT_LOCAL;
527                 update_devfreq(df);
528         }
529
530         trace_podgov_print_target(idle, avg_idle, curr / 1000000, target, hint,
531                 avg_hint);
532
533 exit_unlock:
534         mutex_unlock(&df->lock);
535         pm_runtime_put(dev);
536         return NOTIFY_OK;
537 }
538 #endif
539
540 /*******************************************************************************
541  * debugfs interface for controlling 3d clock scaling on the fly
542  ******************************************************************************/
543
544 #ifdef CONFIG_DEBUG_FS
545
546 static void nvhost_scale3d_debug_init(struct devfreq *df)
547 {
548         struct podgov_info_rec *podgov = df->data;
549         struct dentry *f;
550         char dirname[128];
551
552         snprintf(dirname, sizeof(dirname), "%s_scaling",
553                 to_platform_device(df->dev.parent)->name);
554
555         if (!podgov)
556                 return;
557
558         podgov->debugdir = debugfs_create_dir(dirname, NULL);
559         if (!podgov->debugdir) {
560                 pr_err("podgov: can\'t create debugfs directory\n");
561                 return;
562         }
563
564 #define CREATE_PODGOV_FILE(fname) \
565         do {\
566                 f = debugfs_create_u32(#fname, S_IRUGO | S_IWUSR, \
567                         podgov->debugdir, &podgov->p_##fname); \
568                 if (NULL == f) { \
569                         pr_err("podgov: can\'t create file " #fname "\n"); \
570                         return; \
571                 } \
572         } while (0)
573
574         CREATE_PODGOV_FILE(block_window);
575         CREATE_PODGOV_FILE(load_max);
576         CREATE_PODGOV_FILE(load_target);
577         CREATE_PODGOV_FILE(bias);
578         CREATE_PODGOV_FILE(damp);
579         CREATE_PODGOV_FILE(use_throughput_hint);
580         CREATE_PODGOV_FILE(hint_hi_limit);
581         CREATE_PODGOV_FILE(hint_lo_limit);
582         CREATE_PODGOV_FILE(scaleup_limit);
583         CREATE_PODGOV_FILE(scaledown_limit);
584         CREATE_PODGOV_FILE(smooth);
585         CREATE_PODGOV_FILE(slowdown_delay);
586 #undef CREATE_PODGOV_FILE
587 }
588
589 static void nvhost_scale3d_debug_deinit(struct devfreq *df)
590 {
591         struct podgov_info_rec *podgov = df->data;
592
593         debugfs_remove_recursive(podgov->debugdir);
594 }
595
596 #else
597 static void nvhost_scale3d_debug_init(struct devfreq *df)
598 {
599         (void)df;
600 }
601
602 static void nvhost_scale3d_debug_deinit(struct devfreq *df)
603 {
604         (void)df;
605 }
606 #endif
607
608 /*******************************************************************************
609  * sysfs interface for enabling/disabling 3d scaling
610  ******************************************************************************/
611
612 static ssize_t enable_3d_scaling_show(struct kobject *kobj,
613                                       struct kobj_attribute *attr,
614                                       char *buf)
615 {
616         struct podgov_info_rec *podgov = container_of(attr,
617                                                       struct podgov_info_rec,
618                                                       enable_3d_scaling_attr);
619         ssize_t res;
620
621         res = snprintf(buf, PAGE_SIZE, "%d\n", podgov->enable);
622
623         return res;
624 }
625
626 static ssize_t enable_3d_scaling_store(struct kobject *kobj,
627                                        struct kobj_attribute *attr,
628                                        const char *buf, size_t count)
629 {
630         struct podgov_info_rec *podgov = container_of(attr,
631                                                       struct podgov_info_rec,
632                                                       enable_3d_scaling_attr);
633         unsigned long val = 0;
634
635         if (kstrtoul(buf, 10, &val) < 0)
636                 return -EINVAL;
637
638         podgov_enable(podgov->power_manager, val);
639
640         return count;
641 }
642
643 /*******************************************************************************
644  * sysfs interface for user space control
645  * user = [0,1] disables / enabled user space control
646  * freq_request is the sysfs node user space writes frequency requests to
647  ******************************************************************************/
648
649 static ssize_t user_show(struct kobject *kobj,
650                          struct kobj_attribute *attr,
651                          char *buf)
652 {
653         struct podgov_info_rec *podgov =
654                 container_of(attr, struct podgov_info_rec, user_attr);
655         ssize_t res;
656
657         res = snprintf(buf, PAGE_SIZE, "%d\n", podgov->p_user);
658
659         return res;
660 }
661
662 static ssize_t user_store(struct kobject *kobj,
663                           struct kobj_attribute *attr,
664                           const char *buf, size_t count)
665 {
666         struct podgov_info_rec *podgov =
667                 container_of(attr, struct podgov_info_rec, user_attr);
668         unsigned long val = 0;
669
670         if (kstrtoul(buf, 10, &val) < 0)
671                 return -EINVAL;
672
673         podgov_set_user_ctl(podgov->power_manager, val);
674
675         return count;
676 }
677
678 static ssize_t freq_request_show(struct kobject *kobj,
679                                  struct kobj_attribute *attr,
680                                  char *buf)
681 {
682         struct podgov_info_rec *podgov =
683                 container_of(attr, struct podgov_info_rec, freq_request_attr);
684         ssize_t res;
685
686         res = snprintf(buf, PAGE_SIZE, "%d\n", podgov->p_freq_request);
687
688         return res;
689 }
690
691 static ssize_t freq_request_store(struct kobject *kobj,
692                                   struct kobj_attribute *attr,
693                                   const char *buf, size_t count)
694 {
695         struct podgov_info_rec *podgov =
696                 container_of(attr, struct podgov_info_rec, freq_request_attr);
697         unsigned long val = 0;
698
699         if (kstrtoul(buf, 10, &val) < 0)
700                 return -EINVAL;
701
702         podgov_set_freq_request(podgov->power_manager, val);
703
704         return count;
705 }
706
707 /*******************************************************************************
708  * nvhost_pod_estimate_freq(df, freq)
709  *
710  * This function is called for re-estimating the frequency. The function is
711  * called in three conditions:
712  *
713  *  (1) Internal request to change the frequency. In this case a new clock
714  *      target is immediately set for the device.
715  *  (2) Call from the client (something has happened and re-estimation
716  *      is required).
717  *  (3) Some other reason (i.e. periodic call)
718  *
719  ******************************************************************************/
720
721 static int nvhost_pod_estimate_freq(struct devfreq *df,
722                                     unsigned long *freq)
723 {
724         struct podgov_info_rec *podgov = df->data;
725         struct devfreq_dev_status dev_stat;
726         int stat;
727         ktime_t now;
728
729         stat = df->profile->get_dev_status(df->dev.parent, &dev_stat);
730         if (stat < 0)
731                 return stat;
732
733         /* Ensure maximal clock when scaling is disabled */
734         if (!podgov->enable) {
735                 *freq = df->max_freq;
736                 return 0;
737         }
738
739         if (podgov->p_user) {
740                 *freq = podgov->p_freq_request;
741                 return 0;
742         }
743
744         stat = 0;
745         now = ktime_get();
746
747         /* Local adjustments (i.e. requests from kernel threads) are
748          * handled here */
749
750         if (podgov->adjustment_type == ADJUSTMENT_LOCAL) {
751
752                 podgov->adjustment_type = ADJUSTMENT_DEVICE_REQ;
753
754                 /* Do not do unnecessary scaling */
755                 scaling_limit(df, &podgov->adjustment_frequency);
756
757                 /* Round the frequency and check if we're already there */
758                 if (freqlist_up(podgov, podgov->adjustment_frequency, 0) ==
759                     dev_stat.current_frequency)
760                         return GET_TARGET_FREQ_DONTSCALE;
761
762                 trace_podgov_estimate_freq(df->previous_freq,
763                         podgov->adjustment_frequency);
764
765                 *freq = podgov->adjustment_frequency;
766                 return 0;
767         }
768
769         *freq = dev_stat.current_frequency;
770
771         /* Sustain local variables */
772         podgov->last_event_type = dev_stat.busy;
773         podgov->idle = 1000 * (dev_stat.total_time - dev_stat.busy_time);
774         podgov->idle = podgov->idle / dev_stat.total_time;
775         podgov->idle_avg = (podgov->p_smooth * podgov->idle_avg) +
776                 podgov->idle;
777         podgov->idle_avg = podgov->idle_avg / (podgov->p_smooth + 1);
778
779         /* if throughput hint enabled, and last hint is recent enough, return */
780         if (podgov->p_use_throughput_hint &&
781                 ktime_us_delta(now, podgov->last_throughput_hint) < 1000000)
782                 return GET_TARGET_FREQ_DONTSCALE;
783
784         if (dev_stat.busy) {
785                 cancel_delayed_work(&podgov->idle_timer);
786                 *freq = scaling_state_check(df, now);
787         } else {
788                 /* Launch a work to slowdown the gpu */
789                 *freq = scaling_state_check(df, now);
790                 schedule_delayed_work(&podgov->idle_timer,
791                         msecs_to_jiffies(podgov->p_slowdown_delay));
792         }
793
794         if (!(*freq) ||
795             (freqlist_up(podgov, *freq, 0) == dev_stat.current_frequency))
796                 return GET_TARGET_FREQ_DONTSCALE;
797
798         podgov->last_scale = now;
799
800         trace_podgov_estimate_freq(df->previous_freq, *freq);
801
802
803         return 0;
804 }
805
806 /*******************************************************************************
807  * nvhost_pod_init(struct devfreq *df)
808  *
809  * Governor initialisation.
810  ******************************************************************************/
811
812 static int nvhost_pod_init(struct devfreq *df)
813 {
814         struct podgov_info_rec *podgov;
815         struct platform_device *d = to_platform_device(df->dev.parent);
816         ktime_t now = ktime_get();
817         enum tegra_chipid cid = tegra_get_chipid();
818
819         struct devfreq_dev_status dev_stat;
820         int stat = 0;
821
822         struct kobj_attribute *attr = NULL;
823
824         podgov = kzalloc(sizeof(struct podgov_info_rec), GFP_KERNEL);
825         if (!podgov)
826                 goto err_alloc_podgov;
827         df->data = (void *)podgov;
828
829         /* Initialise workers */
830         INIT_DELAYED_WORK(&podgov->idle_timer, podgov_idle_handler);
831
832         /* Set scaling parameter defaults */
833         podgov->enable = 1;
834         podgov->block = 0;
835         podgov->p_use_throughput_hint = 1;
836
837         if (!strcmp(d->name, "vic03.0")) {
838                 podgov->p_load_max = 990;
839                 podgov->p_load_target = 100;
840                 podgov->p_bias = 80;
841                 podgov->p_hint_lo_limit = 500;
842                 podgov->p_hint_hi_limit = 997;
843                 podgov->p_scaleup_limit = 1100;
844                 podgov->p_scaledown_limit = 1300;
845                 podgov->p_smooth = 30;
846                 podgov->p_damp = 7;
847         } else {
848                 switch (cid) {
849                 case TEGRA_CHIPID_TEGRA14:
850                 case TEGRA_CHIPID_TEGRA11:
851                 case TEGRA_CHIPID_TEGRA12:
852                 case TEGRA_CHIPID_TEGRA13:
853                         podgov->p_load_max = 900;
854                         podgov->p_load_target = 700;
855                         podgov->p_bias = 80;
856                         podgov->p_hint_lo_limit = 500;
857                         podgov->p_hint_hi_limit = 997;
858                         podgov->p_scaleup_limit = 1100;
859                         podgov->p_scaledown_limit = 1300;
860                         podgov->p_smooth = 10;
861                         podgov->p_damp = 7;
862                         podgov->p_use_throughput_hint = 0;
863                         break;
864                 default:
865                         pr_err("%s: un-supported chip id\n", __func__);
866                         goto err_unsupported_chip_id;
867                         break;
868                 }
869         }
870
871         podgov->p_slowdown_delay = 10;
872         podgov->p_block_window = 50000;
873         podgov->adjustment_type = ADJUSTMENT_DEVICE_REQ;
874         podgov->p_user = 0;
875
876         /* Reset clock counters */
877         podgov->last_throughput_hint = now;
878         podgov->last_scale = now;
879
880         podgov->power_manager = df;
881
882         /* Get the current status of the device */
883         stat = df->profile->get_dev_status(df->dev.parent, &dev_stat);
884
885         attr = &podgov->enable_3d_scaling_attr;
886         attr->attr.name = "enable_3d_scaling";
887         attr->attr.mode = S_IWUSR | S_IRUGO;
888         attr->show = enable_3d_scaling_show;
889         attr->store = enable_3d_scaling_store;
890         sysfs_attr_init(&attr->attr);
891         if (sysfs_create_file(&df->dev.parent->kobj, &attr->attr))
892                 goto err_create_enable_sysfs_entry;
893
894         attr = &podgov->freq_request_attr;
895         attr->attr.name = "freq_request";
896         attr->attr.mode = S_IWUSR | S_IRUGO;
897         attr->show = freq_request_show;
898         attr->store = freq_request_store;
899         sysfs_attr_init(&attr->attr);
900         if (sysfs_create_file(&df->dev.parent->kobj, &attr->attr))
901                 goto err_create_request_sysfs_entry;
902
903         attr = &podgov->user_attr;
904         attr->attr.name = "user";
905         attr->attr.mode = S_IWUSR | S_IRUGO;
906         attr->show = user_show;
907         attr->store = user_store;
908         sysfs_attr_init(&attr->attr);
909         if (sysfs_create_file(&df->dev.parent->kobj, &attr->attr))
910                 goto err_create_user_sysfs_entry;
911
912         podgov->freq_count = df->profile->max_state;
913         podgov->freqlist = df->profile->freq_table;
914         if (!podgov->freq_count || !podgov->freqlist)
915                 goto err_get_freqs;
916
917         /* store the limits */
918         df->min_freq = podgov->freqlist[0];
919         df->max_freq = podgov->freqlist[podgov->freq_count - 1];
920         podgov->p_freq_request = df->max_freq;
921
922         podgov->idle_avg = 0;
923         podgov->freq_avg = 0;
924         podgov->hint_avg = 0;
925
926         nvhost_scale3d_debug_init(df);
927
928         /* register the governor to throughput hint notifier chain */
929 #ifdef CONFIG_TEGRA_THROUGHPUT
930         podgov->throughput_hint_notifier.notifier_call =
931                 &nvhost_scale3d_set_throughput_hint;
932         blocking_notifier_chain_register(&throughput_notifier_list,
933                                          &podgov->throughput_hint_notifier);
934 #endif
935
936         return 0;
937
938 err_get_freqs:
939         sysfs_remove_file(&df->dev.parent->kobj, &podgov->user_attr.attr);
940 err_create_user_sysfs_entry:
941         sysfs_remove_file(&df->dev.parent->kobj,
942                           &podgov->freq_request_attr.attr);
943 err_create_request_sysfs_entry:
944         sysfs_remove_file(&df->dev.parent->kobj,
945                           &podgov->enable_3d_scaling_attr.attr);
946 err_create_enable_sysfs_entry:
947         dev_err(&d->dev, "failed to create sysfs attributes");
948 err_unsupported_chip_id:
949         kfree(podgov);
950 err_alloc_podgov:
951         return -ENOMEM;
952 }
953
954 /*******************************************************************************
955  * nvhost_pod_exit(struct devfreq *df)
956  *
957  * Clean up governor data structures
958  ******************************************************************************/
959
960 static void nvhost_pod_exit(struct devfreq *df)
961 {
962         struct podgov_info_rec *podgov = df->data;
963
964 #ifdef CONFIG_TEGRA_THROUGHPUT
965         blocking_notifier_chain_unregister(&throughput_notifier_list,
966                                            &podgov->throughput_hint_notifier);
967 #endif
968         cancel_delayed_work(&podgov->idle_timer);
969
970         sysfs_remove_file(&df->dev.parent->kobj, &podgov->user_attr.attr);
971         sysfs_remove_file(&df->dev.parent->kobj,
972                           &podgov->freq_request_attr.attr);
973         sysfs_remove_file(&df->dev.parent->kobj,
974                           &podgov->enable_3d_scaling_attr.attr);
975
976         nvhost_scale3d_debug_deinit(df);
977
978         kfree(podgov);
979 }
980
981 static int nvhost_pod_event_handler(struct devfreq *df,
982                         unsigned int event, void *data)
983 {
984         int ret = 0;
985
986         switch (event) {
987         case DEVFREQ_GOV_START:
988                 ret = nvhost_pod_init(df);
989                 break;
990         case DEVFREQ_GOV_STOP:
991                 nvhost_pod_exit(df);
992                 break;
993         case DEVFREQ_GOV_SUSPEND:
994                 nvhost_pod_suspend(df);
995                 break;
996         default:
997                 break;
998         }
999
1000         return ret;
1001 }
1002
1003 static struct devfreq_governor nvhost_podgov = {
1004         .name = "nvhost_podgov",
1005         .get_target_freq = nvhost_pod_estimate_freq,
1006         .event_handler = nvhost_pod_event_handler,
1007 };
1008
1009
1010 static int __init podgov_init(void)
1011 {
1012         return devfreq_add_governor(&nvhost_podgov);
1013 }
1014
1015 static void __exit podgov_exit(void)
1016 {
1017         devfreq_remove_governor(&nvhost_podgov);
1018 }
1019
1020 /* governor must be registered before initialising client devices */
1021 rootfs_initcall(podgov_init);
1022 module_exit(podgov_exit);
1023