ARM: tegra12: set CPU rate to 2.2GHz for sku 0x87
[linux-3.10.git] / arch / arm / mach-tegra / tegra3_actmon.c
1 /*
2  * Copyright (c) 2012-2013, NVIDIA CORPORATION.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16  */
17
18 #include <linux/kernel.h>
19 #include <linux/spinlock.h>
20 #include <linux/err.h>
21 #include <linux/io.h>
22 #include <linux/clk.h>
23 #include <linux/clk/tegra.h>
24 #include <linux/interrupt.h>
25 #include <linux/suspend.h>
26 #include <linux/debugfs.h>
27 #include <linux/seq_file.h>
28 #include <linux/uaccess.h>
29 #include <linux/module.h>
30
31 #include <mach/irqs.h>
32
33 #include "clock.h"
34 #include "iomap.h"
35
36 #define ACTMON_GLB_STATUS                       0x00
37 #define ACTMON_GLB_PERIOD_CTRL                  0x04
38
39 #define ACTMON_DEV_CTRL                         0x00
40 #define ACTMON_DEV_CTRL_ENB                     (0x1 << 31)
41 #define ACTMON_DEV_CTRL_UP_WMARK_ENB            (0x1 << 30)
42 #define ACTMON_DEV_CTRL_DOWN_WMARK_ENB          (0x1 << 29)
43 #define ACTMON_DEV_CTRL_UP_WMARK_NUM_SHIFT      26
44 #define ACTMON_DEV_CTRL_UP_WMARK_NUM_MASK       (0x7 << 26)
45 #define ACTMON_DEV_CTRL_DOWN_WMARK_NUM_SHIFT    23
46 #define ACTMON_DEV_CTRL_DOWN_WMARK_NUM_MASK     (0x7 << 23)
47 #define ACTMON_DEV_CTRL_AVG_UP_WMARK_ENB        (0x1 << 21)
48 #define ACTMON_DEV_CTRL_AVG_DOWN_WMARK_ENB      (0x1 << 20)
49 #define ACTMON_DEV_CTRL_PERIODIC_ENB            (0x1 << 18)
50 #define ACTMON_DEV_CTRL_K_VAL_SHIFT             10
51 #define ACTMON_DEV_CTRL_K_VAL_MASK              (0x7 << 10)
52
53 #define ACTMON_DEV_UP_WMARK                     0x04
54 #define ACTMON_DEV_DOWN_WMARK                   0x08
55 #define ACTMON_DEV_INIT_AVG                     0x0c
56 #define ACTMON_DEV_AVG_UP_WMARK                 0x10
57 #define ACTMON_DEV_AVG_DOWN_WMARK                       0x14
58
59 #define ACTMON_DEV_COUNT_WEGHT                  0x18
60 #define ACTMON_DEV_COUNT                        0x1c
61 #define ACTMON_DEV_AVG_COUNT                    0x20
62
63 #define ACTMON_DEV_INTR_STATUS                  0x24
64 #define ACTMON_DEV_INTR_UP_WMARK                (0x1 << 31)
65 #define ACTMON_DEV_INTR_DOWN_WMARK              (0x1 << 30)
66 #define ACTMON_DEV_INTR_AVG_DOWN_WMARK          (0x1 << 25)
67 #define ACTMON_DEV_INTR_AVG_UP_WMARK            (0x1 << 24)
68
69 #define ACTMON_DEFAULT_AVG_WINDOW_LOG2          6
70 #define ACTMON_DEFAULT_AVG_BAND                 6       /* 1/10 of % */
71
72 enum actmon_type {
73         ACTMON_LOAD_SAMPLER,
74         ACTMON_FREQ_SAMPLER,
75 };
76
77 enum actmon_state {
78         ACTMON_UNINITIALIZED = -1,
79         ACTMON_OFF = 0,
80         ACTMON_ON  = 1,
81         ACTMON_SUSPENDED = 2,
82 };
83
84 #define ACTMON_DEFAULT_SAMPLING_PERIOD          12
85 static u8 actmon_sampling_period;
86
87 static unsigned long actmon_clk_freq;
88
89 /* Maximum frequency EMC is running at when sourced from PLLP. This is
90  * really a short-cut, but it is true for all Tegra3  platforms
91  */
92 #define EMC_PLLP_FREQ_MAX                       204000
93
94 /* Units:
95  * - frequency in kHz
96  * - coefficients, and thresholds in %
97  * - sampling period in ms
98  * - window in sample periods (value = setting + 1)
99  */
100 struct actmon_dev {
101         u32             reg;
102         u32             glb_status_irq_mask;
103         const char      *dev_id;
104         const char      *con_id;
105         struct clk      *clk;
106
107         unsigned long   max_freq;
108         unsigned long   target_freq;
109         unsigned long   cur_freq;
110         unsigned long   suspend_freq;
111
112         unsigned long   avg_actv_freq;
113         unsigned long   avg_band_freq;
114         unsigned int    avg_sustain_coef;
115         u32             avg_count;
116         u32             avg_dependency_threshold;
117
118         unsigned long   boost_freq;
119         unsigned long   boost_freq_step;
120         unsigned int    boost_up_coef;
121         unsigned int    boost_down_coef;
122         unsigned int    boost_up_threshold;
123         unsigned int    boost_down_threshold;
124
125         u8              up_wmark_window;
126         u8              down_wmark_window;
127         u8              avg_window_log2;
128         u32             count_weight;
129
130         enum actmon_type        type;
131         enum actmon_state       state;
132         enum actmon_state       saved_state;
133
134         spinlock_t      lock;
135
136         struct notifier_block   rate_change_nb;
137 };
138
139 static void __iomem *actmon_base = IO_ADDRESS(TEGRA_ACTMON_BASE);
140
141 static inline u32 actmon_readl(u32 offset)
142 {
143         return __raw_readl(actmon_base + offset);
144 }
145 static inline void actmon_writel(u32 val, u32 offset)
146 {
147         __raw_writel(val, actmon_base + offset);
148 }
149 static inline void actmon_wmb(void)
150 {
151         wmb();
152         actmon_readl(ACTMON_GLB_STATUS);
153 }
154
155 #define offs(x)         (dev->reg + x)
156
157 static inline unsigned long do_percent(unsigned long val, unsigned int pct)
158 {
159         return val * pct / 100;
160 }
161
162 static inline void actmon_dev_up_wmark_set(struct actmon_dev *dev)
163 {
164         u32 val;
165         unsigned long freq = (dev->type == ACTMON_FREQ_SAMPLER) ?
166                 dev->cur_freq : actmon_clk_freq;
167
168         val = freq * actmon_sampling_period;
169         actmon_writel(do_percent(val, dev->boost_up_threshold),
170                       offs(ACTMON_DEV_UP_WMARK));
171 }
172
173 static inline void actmon_dev_down_wmark_set(struct actmon_dev *dev)
174 {
175         u32 val;
176         unsigned long freq = (dev->type == ACTMON_FREQ_SAMPLER) ?
177                 dev->cur_freq : actmon_clk_freq;
178
179         val = freq * actmon_sampling_period;
180         actmon_writel(do_percent(val, dev->boost_down_threshold),
181                       offs(ACTMON_DEV_DOWN_WMARK));
182 }
183
184 static inline void actmon_dev_wmark_set(struct actmon_dev *dev)
185 {
186         u32 val;
187         unsigned long freq = (dev->type == ACTMON_FREQ_SAMPLER) ?
188                 dev->cur_freq : actmon_clk_freq;
189
190         val = freq * actmon_sampling_period;
191         actmon_writel(do_percent(val, dev->boost_up_threshold),
192                       offs(ACTMON_DEV_UP_WMARK));
193         actmon_writel(do_percent(val, dev->boost_down_threshold),
194                       offs(ACTMON_DEV_DOWN_WMARK));
195 }
196
197 static inline void actmon_dev_avg_wmark_set(struct actmon_dev *dev)
198 {
199         u32 avg = dev->avg_count;
200         u32 band = dev->avg_band_freq * actmon_sampling_period;
201
202         actmon_writel(avg + band, offs(ACTMON_DEV_AVG_UP_WMARK));
203         avg = max(avg, band);
204         actmon_writel(avg - band, offs(ACTMON_DEV_AVG_DOWN_WMARK));
205 }
206
207 static unsigned long actmon_dev_avg_freq_get(struct actmon_dev *dev)
208 {
209         u64 val;
210
211         if (dev->type == ACTMON_FREQ_SAMPLER)
212                 return dev->avg_count / actmon_sampling_period;
213
214         val = (u64)dev->avg_count * dev->cur_freq;
215         do_div(val, actmon_clk_freq * actmon_sampling_period);
216         return (u32)val;
217 }
218
219 /* Activity monitor sampling operations */
220 irqreturn_t actmon_dev_isr(int irq, void *dev_id)
221 {
222         u32 val;
223         unsigned long flags;
224         struct actmon_dev *dev = (struct actmon_dev *)dev_id;
225
226         val = actmon_readl(ACTMON_GLB_STATUS) & dev->glb_status_irq_mask;
227         if (!val)
228                 return IRQ_NONE;
229
230         spin_lock_irqsave(&dev->lock, flags);
231
232         dev->avg_count = actmon_readl(offs(ACTMON_DEV_AVG_COUNT));
233         actmon_dev_avg_wmark_set(dev);
234
235         val = actmon_readl(offs(ACTMON_DEV_INTR_STATUS));
236         if (val & ACTMON_DEV_INTR_UP_WMARK) {
237                 val = actmon_readl(offs(ACTMON_DEV_CTRL)) |
238                         ACTMON_DEV_CTRL_UP_WMARK_ENB |
239                         ACTMON_DEV_CTRL_DOWN_WMARK_ENB;
240
241                 dev->boost_freq = dev->boost_freq_step +
242                         do_percent(dev->boost_freq, dev->boost_up_coef);
243                 if (dev->boost_freq >= dev->max_freq) {
244                         dev->boost_freq = dev->max_freq;
245                         val &= ~ACTMON_DEV_CTRL_UP_WMARK_ENB;
246                 }
247                 actmon_writel(val, offs(ACTMON_DEV_CTRL));
248         } else if (val & ACTMON_DEV_INTR_DOWN_WMARK) {
249                 val = actmon_readl(offs(ACTMON_DEV_CTRL)) |
250                         ACTMON_DEV_CTRL_UP_WMARK_ENB |
251                         ACTMON_DEV_CTRL_DOWN_WMARK_ENB;
252
253                 dev->boost_freq =
254                         do_percent(dev->boost_freq, dev->boost_down_coef);
255                 if (dev->boost_freq < (dev->boost_freq_step >> 1)) {
256                         dev->boost_freq = 0;
257                         val &= ~ACTMON_DEV_CTRL_DOWN_WMARK_ENB;
258                 }
259                 actmon_writel(val, offs(ACTMON_DEV_CTRL));
260         }
261         if (dev->avg_dependency_threshold) {
262                 val = actmon_readl(offs(ACTMON_DEV_CTRL));
263                 if (dev->avg_count >= dev->avg_dependency_threshold)
264                         val |= ACTMON_DEV_CTRL_DOWN_WMARK_ENB;
265                 else if (dev->boost_freq == 0)
266                         val &= ~ACTMON_DEV_CTRL_DOWN_WMARK_ENB;
267                 actmon_writel(val, offs(ACTMON_DEV_CTRL));
268         }
269
270         actmon_writel(0xffffffff, offs(ACTMON_DEV_INTR_STATUS)); /* clr all */
271         actmon_wmb();
272
273         spin_unlock_irqrestore(&dev->lock, flags);
274         return IRQ_WAKE_THREAD;
275 }
276
277 irqreturn_t actmon_dev_fn(int irq, void *dev_id)
278 {
279         unsigned long flags, freq;
280         struct actmon_dev *dev = (struct actmon_dev *)dev_id;
281         unsigned long cpu_freq = 0;
282         unsigned long static_cpu_emc_freq = 0;
283
284         if (dev->avg_dependency_threshold) {
285                 cpu_freq = clk_get_rate(tegra_get_clock_by_name("cpu")) / 1000;
286                 static_cpu_emc_freq = tegra_emc_to_cpu_ratio(cpu_freq) / 1000;
287                 pr_debug("dev->avg_count%u, cpu_freq: %lu, static_cpu_emc_freq:%lu\n",
288                         dev->avg_count, cpu_freq, static_cpu_emc_freq);
289         }
290
291         spin_lock_irqsave(&dev->lock, flags);
292
293         if (dev->state != ACTMON_ON) {
294                 spin_unlock_irqrestore(&dev->lock, flags);
295                 return IRQ_HANDLED;
296         }
297
298         freq = actmon_dev_avg_freq_get(dev);
299         dev->avg_actv_freq = freq;
300         freq = do_percent(freq, dev->avg_sustain_coef);
301         freq += dev->boost_freq;
302
303         if (dev->avg_dependency_threshold &&
304                 ((dev->avg_count >= dev->avg_dependency_threshold)
305                         || (!static_cpu_emc_freq)))
306                 freq = static_cpu_emc_freq;
307
308         dev->target_freq = freq;
309
310         spin_unlock_irqrestore(&dev->lock, flags);
311
312         pr_debug("%s.%s(kHz): avg: %lu,  boost: %lu, target: %lu, current: %lu\n",
313         dev->dev_id, dev->con_id, dev->avg_actv_freq, dev->boost_freq,
314         dev->target_freq, dev->cur_freq);
315
316         clk_set_rate(dev->clk, freq * 1000);
317
318         return IRQ_HANDLED;
319 }
320
321 static int actmon_rate_notify_cb(
322         struct notifier_block *nb, unsigned long rate, void *v)
323 {
324         unsigned long flags;
325         struct actmon_dev *dev = container_of(
326                 nb, struct actmon_dev, rate_change_nb);
327
328         spin_lock_irqsave(&dev->lock, flags);
329
330         dev->cur_freq = rate / 1000;
331         if (dev->type == ACTMON_FREQ_SAMPLER) {
332                 actmon_dev_wmark_set(dev);
333                 actmon_wmb();
334         }
335
336         spin_unlock_irqrestore(&dev->lock, flags);
337         return NOTIFY_OK;
338 };
339
340 /* Activity monitor configuration and control */
341 static void actmon_dev_configure(struct actmon_dev *dev, unsigned long freq)
342 {
343         u32 val;
344
345         dev->cur_freq = freq;
346         dev->target_freq = freq;
347         dev->avg_actv_freq = freq;
348
349         if (dev->type == ACTMON_FREQ_SAMPLER) {
350                 dev->avg_count = dev->cur_freq * actmon_sampling_period;
351                 dev->avg_band_freq = dev->max_freq *
352                         ACTMON_DEFAULT_AVG_BAND / 1000;
353         } else {
354                 dev->avg_count = actmon_clk_freq * actmon_sampling_period;
355                 dev->avg_band_freq = actmon_clk_freq *
356                         ACTMON_DEFAULT_AVG_BAND / 1000;
357         }
358         actmon_writel(dev->avg_count, offs(ACTMON_DEV_INIT_AVG));
359
360         BUG_ON(!dev->boost_up_threshold);
361         dev->avg_sustain_coef = 100 * 100 / dev->boost_up_threshold;
362         actmon_dev_avg_wmark_set(dev);
363         actmon_dev_wmark_set(dev);
364
365         actmon_writel(dev->count_weight, offs(ACTMON_DEV_COUNT_WEGHT));
366         actmon_writel(0xffffffff, offs(ACTMON_DEV_INTR_STATUS)); /* clr all */
367
368         val = ACTMON_DEV_CTRL_PERIODIC_ENB | ACTMON_DEV_CTRL_AVG_UP_WMARK_ENB |
369                 ACTMON_DEV_CTRL_AVG_DOWN_WMARK_ENB;
370         val |= ((dev->avg_window_log2 - 1) << ACTMON_DEV_CTRL_K_VAL_SHIFT) &
371                 ACTMON_DEV_CTRL_K_VAL_MASK;
372         val |= ((dev->down_wmark_window - 1) <<
373                 ACTMON_DEV_CTRL_DOWN_WMARK_NUM_SHIFT) &
374                 ACTMON_DEV_CTRL_DOWN_WMARK_NUM_MASK;
375         val |=  ((dev->up_wmark_window - 1) <<
376                 ACTMON_DEV_CTRL_UP_WMARK_NUM_SHIFT) &
377                 ACTMON_DEV_CTRL_UP_WMARK_NUM_MASK;
378         val |= ACTMON_DEV_CTRL_DOWN_WMARK_ENB | ACTMON_DEV_CTRL_UP_WMARK_ENB;
379         actmon_writel(val, offs(ACTMON_DEV_CTRL));
380         actmon_wmb();
381 }
382
383 static void actmon_dev_enable(struct actmon_dev *dev)
384 {
385         u32 val;
386         unsigned long flags;
387
388         spin_lock_irqsave(&dev->lock, flags);
389
390         if (dev->state == ACTMON_OFF) {
391                 dev->state = ACTMON_ON;
392
393                 val = actmon_readl(offs(ACTMON_DEV_CTRL));
394                 val |= ACTMON_DEV_CTRL_ENB;
395                 actmon_writel(val, offs(ACTMON_DEV_CTRL));
396                 actmon_wmb();
397         }
398         spin_unlock_irqrestore(&dev->lock, flags);
399 }
400
401 static void actmon_dev_disable(struct actmon_dev *dev)
402 {
403         u32 val;
404         unsigned long flags;
405
406         spin_lock_irqsave(&dev->lock, flags);
407
408         if (dev->state == ACTMON_ON) {
409                 dev->state = ACTMON_OFF;
410
411                 val = actmon_readl(offs(ACTMON_DEV_CTRL));
412                 val &= ~ACTMON_DEV_CTRL_ENB;
413                 actmon_writel(val, offs(ACTMON_DEV_CTRL));
414                 actmon_writel(0xffffffff, offs(ACTMON_DEV_INTR_STATUS));
415                 actmon_wmb();
416         }
417         spin_unlock_irqrestore(&dev->lock, flags);
418 }
419
420 static void actmon_dev_save(struct actmon_dev *dev)
421 {
422         u32 val;
423
424         if ((dev->state == ACTMON_ON) || (dev->state == ACTMON_OFF)){
425                 dev->saved_state = dev->state;
426                 dev->state = ACTMON_SUSPENDED;
427
428                 val = actmon_readl(offs(ACTMON_DEV_CTRL));
429                 val &= ~ACTMON_DEV_CTRL_ENB;
430                 actmon_writel(val, offs(ACTMON_DEV_CTRL));
431                 actmon_writel(0xffffffff, offs(ACTMON_DEV_INTR_STATUS));
432                 actmon_wmb();
433         }
434 }
435
436 static void actmon_dev_suspend(struct actmon_dev *dev)
437 {
438         unsigned long flags;
439
440         spin_lock_irqsave(&dev->lock, flags);
441
442         actmon_dev_save(dev);
443
444         spin_unlock_irqrestore(&dev->lock, flags);
445
446         if (dev->suspend_freq)
447                 clk_set_rate(dev->clk, dev->suspend_freq * 1000);
448 }
449
450 static void actmon_dev_restore(struct actmon_dev *dev, ulong freq)
451 {
452         u32 val;
453
454         if (dev->state == ACTMON_SUSPENDED) {
455                 actmon_dev_configure(dev, freq);
456                 dev->state = dev->saved_state;
457                 if (dev->state == ACTMON_ON) {
458                         val = actmon_readl(offs(ACTMON_DEV_CTRL));
459                         val |= ACTMON_DEV_CTRL_ENB;
460                         actmon_writel(val, offs(ACTMON_DEV_CTRL));
461                         actmon_wmb();
462                 }
463         }
464 }
465
466 static void actmon_dev_resume(struct actmon_dev *dev)
467 {
468         unsigned long flags;
469         unsigned long freq = clk_get_rate(dev->clk) / 1000;
470
471         spin_lock_irqsave(&dev->lock, flags);
472
473         actmon_dev_restore(dev, freq);
474
475         spin_unlock_irqrestore(&dev->lock, flags);
476 }
477
478 static int __init actmon_dev_init(struct actmon_dev *dev)
479 {
480         int ret;
481         struct clk *p;
482         unsigned long freq;
483
484         spin_lock_init(&dev->lock);
485
486         dev->clk = clk_get_sys(dev->dev_id, dev->con_id);
487         if (IS_ERR(dev->clk)) {
488                 pr_err("Failed to find %s.%s clock\n",
489                        dev->dev_id, dev->con_id);
490                 return -ENODEV;
491         }
492         dev->max_freq = clk_round_rate(dev->clk, ULONG_MAX);
493         clk_set_rate(dev->clk, dev->max_freq);
494         dev->max_freq /= 1000;
495         freq = clk_get_rate(dev->clk) / 1000;
496         actmon_dev_configure(dev, freq);
497
498         /* actmon device controls shared bus user clock, but rate
499            change notification should come from bus clock itself */
500         p = clk_get_parent(dev->clk);
501         BUG_ON(!p);
502
503         if (dev->rate_change_nb.notifier_call) {
504                 ret = tegra_register_clk_rate_notifier(p, &dev->rate_change_nb);
505                 if (ret) {
506                         pr_err("Failed to register %s rate change notifier"
507                                " for %s\n", p->name, dev->dev_id);
508                         return ret;
509                 }
510         }
511
512         ret = request_threaded_irq(INT_ACTMON, actmon_dev_isr, actmon_dev_fn,
513                                    IRQF_SHARED, dev->dev_id, dev);
514         if (ret) {
515                 pr_err("Failed irq %d request for %s.%s\n",
516                        INT_ACTMON, dev->dev_id, dev->con_id);
517                 tegra_unregister_clk_rate_notifier(p, &dev->rate_change_nb);
518                 return ret;
519         }
520
521         dev->state = ACTMON_OFF;
522         actmon_dev_enable(dev);
523         tegra_clk_prepare_enable(dev->clk);
524         return 0;
525 }
526
527 /* EMC activity monitor: frequency sampling device:
528  * activity counter is incremented every 256 memory transactions, and
529  * each transaction takes 2 EMC clocks; count_weight = 512 on Tegra3.
530  * On Tegra11 there is only 1 clock per transaction, hence weight = 256.
531  */
532 static struct actmon_dev actmon_dev_emc = {
533         .reg    = 0x1c0,
534         .glb_status_irq_mask = (0x1 << 26),
535         .dev_id = "tegra_actmon",
536         .con_id = "emc",
537
538         /* EMC suspend floor to guarantee suspend entry on PLLM */
539         .suspend_freq           = EMC_PLLP_FREQ_MAX + 2000,
540
541         .boost_freq_step        = 16000,
542         .boost_up_coef          = 200,
543         .boost_down_coef        = 50,
544 #if defined(CONFIG_ARCH_TEGRA_14x_SOC)
545         .boost_up_threshold     = 70,
546         .boost_down_threshold   = 50,
547 #else
548         .boost_up_threshold     = 60,
549         .boost_down_threshold   = 40,
550 #endif
551
552         .up_wmark_window        = 1,
553         .down_wmark_window      = 3,
554         .avg_window_log2        = ACTMON_DEFAULT_AVG_WINDOW_LOG2,
555 #if defined(CONFIG_ARCH_TEGRA_3x_SOC) || defined(CONFIG_ARCH_TEGRA_14x_SOC)
556         .count_weight           = 0x200,
557 #elif defined(CONFIG_ARCH_TEGRA_12x_SOC)
558         .count_weight           = 0x400,
559 #else
560         .count_weight           = 0x100,
561 #endif
562
563         .type                   = ACTMON_FREQ_SAMPLER,
564         .state                  = ACTMON_UNINITIALIZED,
565
566         .rate_change_nb = {
567                 .notifier_call = actmon_rate_notify_cb,
568         },
569 };
570
571 /* AVP activity monitor: load sampling device:
572  * activity counter is incremented on every actmon clock pulse while
573  * AVP is not halted by flow controller; count_weight = 1.
574  */
575 static struct actmon_dev actmon_dev_avp = {
576         .reg    = 0x0c0,
577         .glb_status_irq_mask = (0x1 << 30),
578         .dev_id = "tegra_actmon",
579         .con_id = "avp",
580
581         /* AVP/SCLK suspend activity floor */
582         .suspend_freq           = 40000,
583
584         .boost_freq_step        = 8000,
585         .boost_up_coef          = 200,
586         .boost_down_coef        = 50,
587         .boost_up_threshold     = 85,
588         .boost_down_threshold   = 50,
589
590         .up_wmark_window        = 1,
591         .down_wmark_window      = 3,
592         .avg_window_log2        = ACTMON_DEFAULT_AVG_WINDOW_LOG2,
593         .count_weight           = 0x1,
594
595         .type                   = ACTMON_LOAD_SAMPLER,
596         .state                  = ACTMON_UNINITIALIZED,
597
598         .rate_change_nb = {
599                 .notifier_call = actmon_rate_notify_cb,
600         },
601 };
602
603
604 #define CPU_AVG_ACT_THRESHOLD 50000
605 /* EMC-cpu activity monitor: frequency sampling device:
606  * activity counter is incremented every 256 memory transactions, and
607  * each transaction takes 2 EMC clocks; count_weight = 512 on Tegra3.
608  * On Tegra11 there is only 1 clock per transaction, hence weight = 256.
609  */
610 static struct actmon_dev actmon_dev_cpu_emc = {
611         .reg = 0x200,
612         .glb_status_irq_mask = (0x1 << 25),
613         .dev_id = "tegra_mon",
614         .con_id = "cpu_emc",
615
616         .boost_freq_step        = 16000,
617         .boost_up_coef          = 800,
618         .boost_down_coef        = 90,
619         .boost_up_threshold     = 27,
620         .boost_down_threshold   = 10,
621         .avg_dependency_threshold       = CPU_AVG_ACT_THRESHOLD,
622
623         .up_wmark_window        = 1,
624         .down_wmark_window      = 3,
625         .avg_window_log2        = ACTMON_DEFAULT_AVG_WINDOW_LOG2,
626 #if defined(CONFIG_ARCH_TEGRA_3x_SOC) || defined(CONFIG_ARCH_TEGRA_14x_SOC)
627         .count_weight           = 0x200,
628 #elif defined(CONFIG_ARCH_TEGRA_12x_SOC)
629         .count_weight           = 0x400,
630 #else
631         .count_weight           = 0x100,
632 #endif
633
634         .type                   = ACTMON_FREQ_SAMPLER,
635         .state                  = ACTMON_UNINITIALIZED,
636
637         .rate_change_nb = {
638                 .notifier_call = actmon_rate_notify_cb,
639         },
640 };
641
642 static struct actmon_dev *actmon_devices[] = {
643         &actmon_dev_emc,
644         &actmon_dev_avp,
645         &actmon_dev_cpu_emc,
646 };
647
648 int tegra_actmon_save(void)
649 {
650         int i;
651
652         for (i = 0; i < ARRAY_SIZE(actmon_devices); i++)
653                 actmon_dev_save(actmon_devices[i]);
654
655         return 0;
656 }
657 /* Needs to be called from IRQ enabled as clk_get_rate can
658  * sleep for emc.
659  */
660 int tegra_actmon_restore(void)
661 {
662         int i;
663         unsigned long flags;
664
665         actmon_writel(actmon_sampling_period - 1,
666                       ACTMON_GLB_PERIOD_CTRL);
667
668         for (i = 0; i < ARRAY_SIZE(actmon_devices); i++) {
669                 /*
670                  * Using clk_get_rate_all_locked() here, because all other cpus
671                  * except cpu0 are in LP2 state and irqs are disabled.
672                  */
673                 struct actmon_dev *dev = actmon_devices[i];
674                 unsigned long freq = clk_get_rate_all_locked(dev->clk) / 1000;
675
676                 spin_lock_irqsave(&dev->lock, flags);
677                 actmon_dev_restore(dev, freq);
678                 spin_unlock_irqrestore(&dev->lock, flags);
679         }
680
681         return 0;
682 }
683
684 /* Activity monitor suspend/resume */
685 static int actmon_pm_notify(struct notifier_block *nb,
686                             unsigned long event, void *data)
687 {
688         int i;
689
690         switch (event) {
691         case PM_SUSPEND_PREPARE:
692                 for (i = 0; i < ARRAY_SIZE(actmon_devices); i++)
693                         actmon_dev_suspend(actmon_devices[i]);
694                 break;
695         case PM_POST_SUSPEND:
696                 actmon_writel(actmon_sampling_period - 1,
697                               ACTMON_GLB_PERIOD_CTRL);
698                 for (i = 0; i < ARRAY_SIZE(actmon_devices); i++)
699                         actmon_dev_resume(actmon_devices[i]);
700                 break;
701         }
702
703         return NOTIFY_OK;
704 };
705
706 static struct notifier_block actmon_pm_nb = {
707         .notifier_call = actmon_pm_notify,
708 };
709
710 #ifdef CONFIG_DEBUG_FS
711
712 #define RW_MODE (S_IWUSR | S_IRUGO)
713 #define RO_MODE S_IRUGO
714
715 static struct dentry *clk_debugfs_root;
716
717 static int type_show(struct seq_file *s, void *data)
718 {
719         struct actmon_dev *dev = s->private;
720
721         seq_printf(s, "%s\n", (dev->type == ACTMON_LOAD_SAMPLER) ?
722                    "Load Activity Monitor" : "Frequency Activity Monitor");
723         return 0;
724 }
725 static int type_open(struct inode *inode, struct file *file)
726 {
727         return single_open(file, type_show, inode->i_private);
728 }
729 static const struct file_operations type_fops = {
730         .open           = type_open,
731         .read           = seq_read,
732         .llseek         = seq_lseek,
733         .release        = single_release,
734 };
735
736 static int actv_get(void *data, u64 *val)
737 {
738         unsigned long flags;
739         struct actmon_dev *dev = data;
740
741         spin_lock_irqsave(&dev->lock, flags);
742         *val = actmon_dev_avg_freq_get(dev);
743         spin_unlock_irqrestore(&dev->lock, flags);
744         return 0;
745 }
746 DEFINE_SIMPLE_ATTRIBUTE(actv_fops, actv_get, NULL, "%llu\n");
747
748 static int step_get(void *data, u64 *val)
749 {
750         struct actmon_dev *dev = data;
751         *val = dev->boost_freq_step * 100 / dev->max_freq;
752         return 0;
753 }
754 static int step_set(void *data, u64 val)
755 {
756         unsigned long flags;
757         struct actmon_dev *dev = data;
758
759         if (val > 100)
760                 val = 100;
761
762         spin_lock_irqsave(&dev->lock, flags);
763         dev->boost_freq_step = do_percent(dev->max_freq, (unsigned int)val);
764         spin_unlock_irqrestore(&dev->lock, flags);
765         return 0;
766 }
767 DEFINE_SIMPLE_ATTRIBUTE(step_fops, step_get, step_set, "%llu\n");
768
769 static int up_threshold_get(void *data, u64 *val)
770 {
771         struct actmon_dev *dev = data;
772         *val = dev->boost_up_threshold;
773         return 0;
774 }
775 static int up_threshold_set(void *data, u64 val)
776 {
777         unsigned long flags;
778         struct actmon_dev *dev = data;
779         unsigned int up_threshold = (unsigned int)val;
780
781         if (up_threshold > 100)
782                 up_threshold = 100;
783
784         spin_lock_irqsave(&dev->lock, flags);
785
786         if (up_threshold <= dev->boost_down_threshold)
787                 up_threshold = dev->boost_down_threshold;
788         if (up_threshold)
789                 dev->avg_sustain_coef = 100 * 100 / up_threshold;
790         dev->boost_up_threshold = up_threshold;
791
792         actmon_dev_up_wmark_set(dev);
793         actmon_wmb();
794
795         spin_unlock_irqrestore(&dev->lock, flags);
796         return 0;
797 }
798 DEFINE_SIMPLE_ATTRIBUTE(up_threshold_fops, up_threshold_get,
799                         up_threshold_set, "%llu\n");
800
801 static int down_threshold_get(void *data, u64 *val)
802 {
803         struct actmon_dev *dev = data;
804         *val = dev->boost_down_threshold;
805         return 0;
806 }
807 static int down_threshold_set(void *data, u64 val)
808 {
809         unsigned long flags;
810         struct actmon_dev *dev = data;
811         unsigned int down_threshold = (unsigned int)val;
812
813         spin_lock_irqsave(&dev->lock, flags);
814
815         if (down_threshold >= dev->boost_up_threshold)
816                 down_threshold = dev->boost_up_threshold;
817         dev->boost_down_threshold = down_threshold;
818
819         actmon_dev_down_wmark_set(dev);
820         actmon_wmb();
821
822         spin_unlock_irqrestore(&dev->lock, flags);
823         return 0;
824 }
825 DEFINE_SIMPLE_ATTRIBUTE(down_threshold_fops, down_threshold_get,
826                         down_threshold_set, "%llu\n");
827
828 static int state_get(void *data, u64 *val)
829 {
830         struct actmon_dev *dev = data;
831         *val = dev->state;
832         return 0;
833 }
834 static int state_set(void *data, u64 val)
835 {
836         struct actmon_dev *dev = data;
837
838         if (val)
839                 actmon_dev_enable(dev);
840         else
841                 actmon_dev_disable(dev);
842         return 0;
843 }
844 DEFINE_SIMPLE_ATTRIBUTE(state_fops, state_get, state_set, "%llu\n");
845
846 static int period_get(void *data, u64 *val)
847 {
848         *val = actmon_sampling_period;
849         return 0;
850 }
851 static int period_set(void *data, u64 val)
852 {
853         int i;
854         unsigned long flags;
855         u8 period = (u8)val;
856
857         if (period) {
858                 actmon_sampling_period = period;
859                 actmon_writel(period - 1, ACTMON_GLB_PERIOD_CTRL);
860
861                 for (i = 0; i < ARRAY_SIZE(actmon_devices); i++) {
862                         struct actmon_dev *dev = actmon_devices[i];
863                         spin_lock_irqsave(&dev->lock, flags);
864                         actmon_dev_wmark_set(dev);
865                         spin_unlock_irqrestore(&dev->lock, flags);
866                 }
867                 actmon_wmb();
868                 return 0;
869         }
870         return -EINVAL;
871 }
872 DEFINE_SIMPLE_ATTRIBUTE(period_fops, period_get, period_set, "%llu\n");
873
874
875 static int actmon_debugfs_create_dev(struct actmon_dev *dev)
876 {
877         struct dentry *dir, *d;
878
879         if (dev->state == ACTMON_UNINITIALIZED)
880                 return 0;
881
882         dir = debugfs_create_dir(dev->con_id, clk_debugfs_root);
883         if (!dir)
884                 return -ENOMEM;
885
886         d = debugfs_create_file(
887                 "actv_type", RO_MODE, dir, dev, &type_fops);
888         if (!d)
889                 return -ENOMEM;
890
891         d = debugfs_create_file(
892                 "avg_activity", RO_MODE, dir, dev, &actv_fops);
893         if (!d)
894                 return -ENOMEM;
895
896         d = debugfs_create_file(
897                 "boost_step", RW_MODE, dir, dev, &step_fops);
898         if (!d)
899                 return -ENOMEM;
900
901         d = debugfs_create_u32(
902                 "boost_rate_dec", RW_MODE, dir, (u32 *)&dev->boost_down_coef);
903         if (!d)
904                 return -ENOMEM;
905
906         d = debugfs_create_u32(
907                 "boost_rate_inc", RW_MODE, dir, (u32 *)&dev->boost_up_coef);
908         if (!d)
909                 return -ENOMEM;
910
911         d = debugfs_create_file(
912                 "boost_threshold_dn", RW_MODE, dir, dev, &down_threshold_fops);
913         if (!d)
914                 return -ENOMEM;
915
916         d = debugfs_create_file(
917                 "boost_threshold_up", RW_MODE, dir, dev, &up_threshold_fops);
918         if (!d)
919                 return -ENOMEM;
920
921         d = debugfs_create_file(
922                 "state", RW_MODE, dir, dev, &state_fops);
923         if (!d)
924                 return -ENOMEM;
925
926         d = debugfs_create_u32(
927                 "avg_act_threshold", RW_MODE, dir,
928                 (u32 *)&dev->avg_dependency_threshold);
929
930         if (!d)
931                 return -ENOMEM;
932
933         return 0;
934 }
935
936 static int __init actmon_debugfs_init(void)
937 {
938         int i;
939         int ret = -ENOMEM;
940         struct dentry *d;
941
942         d = debugfs_create_dir("tegra_actmon", NULL);
943         if (!d)
944                 return ret;
945         clk_debugfs_root = d;
946
947         d = debugfs_create_file("period", RW_MODE, d, NULL, &period_fops);
948         if (!d)
949                 goto err_out;
950
951         for (i = 0; i < ARRAY_SIZE(actmon_devices); i++) {
952                 ret = actmon_debugfs_create_dev(actmon_devices[i]);
953                 if (ret)
954                         goto err_out;
955         }
956         return 0;
957
958 err_out:
959         debugfs_remove_recursive(clk_debugfs_root);
960         return ret;
961 }
962
963 #endif
964
965 static int __init tegra_actmon_init(void)
966 {
967         int i, ret;
968         struct clk *c = tegra_get_clock_by_name("actmon");
969
970         if (!c) {
971                 pr_err("%s: Failed to find actmon clock\n", __func__);
972                 return 0;
973         }
974         actmon_clk_freq = clk_get_rate(c) / 1000;
975         ret = tegra_clk_prepare_enable(c);
976         if (ret) {
977                 pr_err("%s: Failed to enable actmon clock\n", __func__);
978                 return 0;
979         }
980         actmon_sampling_period = ACTMON_DEFAULT_SAMPLING_PERIOD;
981         actmon_writel(actmon_sampling_period - 1, ACTMON_GLB_PERIOD_CTRL);
982
983         for (i = 0; i < ARRAY_SIZE(actmon_devices); i++) {
984                 ret = actmon_dev_init(actmon_devices[i]);
985                 pr_info("%s.%s: %s initialization (%d)\n",
986                         actmon_devices[i]->dev_id, actmon_devices[i]->con_id,
987                         ret ? "Failed" : "Completed", ret);
988         }
989         register_pm_notifier(&actmon_pm_nb);
990
991 #ifdef CONFIG_DEBUG_FS
992         actmon_debugfs_init();
993 #endif
994         return 0;
995 }
996 late_initcall(tegra_actmon_init);