Arm: tegra: Enable MCCPU Actmon device
[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                         freq = static_cpu_emc_freq;
306
307         dev->target_freq = freq;
308
309         spin_unlock_irqrestore(&dev->lock, flags);
310
311         pr_debug("%s.%s(kHz): avg: %lu,  boost: %lu, target: %lu, current: %lu\n",
312         dev->dev_id, dev->con_id, dev->avg_actv_freq, dev->boost_freq,
313         dev->target_freq, dev->cur_freq);
314
315         clk_set_rate(dev->clk, freq * 1000);
316
317         return IRQ_HANDLED;
318 }
319
320 static int actmon_rate_notify_cb(
321         struct notifier_block *nb, unsigned long rate, void *v)
322 {
323         unsigned long flags;
324         struct actmon_dev *dev = container_of(
325                 nb, struct actmon_dev, rate_change_nb);
326
327         spin_lock_irqsave(&dev->lock, flags);
328
329         dev->cur_freq = rate / 1000;
330         if (dev->type == ACTMON_FREQ_SAMPLER) {
331                 actmon_dev_wmark_set(dev);
332                 actmon_wmb();
333         }
334
335         spin_unlock_irqrestore(&dev->lock, flags);
336         return NOTIFY_OK;
337 };
338
339 /* Activity monitor configuration and control */
340 static void actmon_dev_configure(struct actmon_dev *dev, unsigned long freq)
341 {
342         u32 val;
343
344         dev->cur_freq = freq;
345         dev->target_freq = freq;
346         dev->avg_actv_freq = freq;
347
348         if (dev->type == ACTMON_FREQ_SAMPLER) {
349                 dev->avg_count = dev->cur_freq * actmon_sampling_period;
350                 dev->avg_band_freq = dev->max_freq *
351                         ACTMON_DEFAULT_AVG_BAND / 1000;
352         } else {
353                 dev->avg_count = actmon_clk_freq * actmon_sampling_period;
354                 dev->avg_band_freq = actmon_clk_freq *
355                         ACTMON_DEFAULT_AVG_BAND / 1000;
356         }
357         actmon_writel(dev->avg_count, offs(ACTMON_DEV_INIT_AVG));
358
359         BUG_ON(!dev->boost_up_threshold);
360         dev->avg_sustain_coef = 100 * 100 / dev->boost_up_threshold;
361         actmon_dev_avg_wmark_set(dev);
362         actmon_dev_wmark_set(dev);
363
364         actmon_writel(dev->count_weight, offs(ACTMON_DEV_COUNT_WEGHT));
365         actmon_writel(0xffffffff, offs(ACTMON_DEV_INTR_STATUS)); /* clr all */
366
367         val = ACTMON_DEV_CTRL_PERIODIC_ENB | ACTMON_DEV_CTRL_AVG_UP_WMARK_ENB |
368                 ACTMON_DEV_CTRL_AVG_DOWN_WMARK_ENB;
369         val |= ((dev->avg_window_log2 - 1) << ACTMON_DEV_CTRL_K_VAL_SHIFT) &
370                 ACTMON_DEV_CTRL_K_VAL_MASK;
371         val |= ((dev->down_wmark_window - 1) <<
372                 ACTMON_DEV_CTRL_DOWN_WMARK_NUM_SHIFT) &
373                 ACTMON_DEV_CTRL_DOWN_WMARK_NUM_MASK;
374         val |=  ((dev->up_wmark_window - 1) <<
375                 ACTMON_DEV_CTRL_UP_WMARK_NUM_SHIFT) &
376                 ACTMON_DEV_CTRL_UP_WMARK_NUM_MASK;
377         val |= ACTMON_DEV_CTRL_DOWN_WMARK_ENB | ACTMON_DEV_CTRL_UP_WMARK_ENB;
378         actmon_writel(val, offs(ACTMON_DEV_CTRL));
379         actmon_wmb();
380 }
381
382 static void actmon_dev_enable(struct actmon_dev *dev)
383 {
384         u32 val;
385         unsigned long flags;
386
387         spin_lock_irqsave(&dev->lock, flags);
388
389         if (dev->state == ACTMON_OFF) {
390                 dev->state = ACTMON_ON;
391
392                 val = actmon_readl(offs(ACTMON_DEV_CTRL));
393                 val |= ACTMON_DEV_CTRL_ENB;
394                 actmon_writel(val, offs(ACTMON_DEV_CTRL));
395                 actmon_wmb();
396         }
397         spin_unlock_irqrestore(&dev->lock, flags);
398 }
399
400 static void actmon_dev_disable(struct actmon_dev *dev)
401 {
402         u32 val;
403         unsigned long flags;
404
405         spin_lock_irqsave(&dev->lock, flags);
406
407         if (dev->state == ACTMON_ON) {
408                 dev->state = ACTMON_OFF;
409
410                 val = actmon_readl(offs(ACTMON_DEV_CTRL));
411                 val &= ~ACTMON_DEV_CTRL_ENB;
412                 actmon_writel(val, offs(ACTMON_DEV_CTRL));
413                 actmon_writel(0xffffffff, offs(ACTMON_DEV_INTR_STATUS));
414                 actmon_wmb();
415         }
416         spin_unlock_irqrestore(&dev->lock, flags);
417 }
418
419 static void actmon_dev_suspend(struct actmon_dev *dev)
420 {
421         u32 val;
422         unsigned long flags;
423
424         spin_lock_irqsave(&dev->lock, flags);
425
426         if ((dev->state == ACTMON_ON) || (dev->state == ACTMON_OFF)){
427                 dev->saved_state = dev->state;
428                 dev->state = ACTMON_SUSPENDED;
429
430                 val = actmon_readl(offs(ACTMON_DEV_CTRL));
431                 val &= ~ACTMON_DEV_CTRL_ENB;
432                 actmon_writel(val, offs(ACTMON_DEV_CTRL));
433                 actmon_writel(0xffffffff, offs(ACTMON_DEV_INTR_STATUS));
434                 actmon_wmb();
435         }
436         spin_unlock_irqrestore(&dev->lock, flags);
437
438         if (dev->suspend_freq)
439                 clk_set_rate(dev->clk, dev->suspend_freq * 1000);
440 }
441
442 static void actmon_dev_resume(struct actmon_dev *dev)
443 {
444         u32 val;
445         unsigned long flags;
446         unsigned long freq = clk_get_rate(dev->clk) / 1000;
447
448         spin_lock_irqsave(&dev->lock, flags);
449
450         if (dev->state == ACTMON_SUSPENDED) {
451                 actmon_dev_configure(dev, freq);
452                 dev->state = dev->saved_state;
453                 if (dev->state == ACTMON_ON) {
454                         val = actmon_readl(offs(ACTMON_DEV_CTRL));
455                         val |= ACTMON_DEV_CTRL_ENB;
456                         actmon_writel(val, offs(ACTMON_DEV_CTRL));
457                         actmon_wmb();
458                 }
459         }
460         spin_unlock_irqrestore(&dev->lock, flags);
461 }
462
463 static int __init actmon_dev_init(struct actmon_dev *dev)
464 {
465         int ret;
466         struct clk *p;
467         unsigned long freq;
468
469         spin_lock_init(&dev->lock);
470
471         dev->clk = clk_get_sys(dev->dev_id, dev->con_id);
472         if (IS_ERR(dev->clk)) {
473                 pr_err("Failed to find %s.%s clock\n",
474                        dev->dev_id, dev->con_id);
475                 return -ENODEV;
476         }
477         dev->max_freq = clk_round_rate(dev->clk, ULONG_MAX);
478         clk_set_rate(dev->clk, dev->max_freq);
479         dev->max_freq /= 1000;
480         freq = clk_get_rate(dev->clk) / 1000;
481         actmon_dev_configure(dev, freq);
482
483         /* actmon device controls shared bus user clock, but rate
484            change notification should come from bus clock itself */
485         p = clk_get_parent(dev->clk);
486         BUG_ON(!p);
487
488         if (dev->rate_change_nb.notifier_call) {
489                 ret = tegra_register_clk_rate_notifier(p, &dev->rate_change_nb);
490                 if (ret) {
491                         pr_err("Failed to register %s rate change notifier"
492                                " for %s\n", p->name, dev->dev_id);
493                         return ret;
494                 }
495         }
496
497         ret = request_threaded_irq(INT_ACTMON, actmon_dev_isr, actmon_dev_fn,
498                                    IRQF_SHARED, dev->dev_id, dev);
499         if (ret) {
500                 pr_err("Failed irq %d request for %s.%s\n",
501                        INT_ACTMON, dev->dev_id, dev->con_id);
502                 tegra_unregister_clk_rate_notifier(p, &dev->rate_change_nb);
503                 return ret;
504         }
505
506         dev->state = ACTMON_OFF;
507         actmon_dev_enable(dev);
508         tegra_clk_prepare_enable(dev->clk);
509         return 0;
510 }
511
512 /* EMC activity monitor: frequency sampling device:
513  * activity counter is incremented every 256 memory transactions, and
514  * each transaction takes 2 EMC clocks; count_weight = 512 on Tegra3.
515  * On Tegra11 there is only 1 clock per transaction, hence weight = 256.
516  */
517 static struct actmon_dev actmon_dev_emc = {
518         .reg    = 0x1c0,
519         .glb_status_irq_mask = (0x1 << 26),
520         .dev_id = "tegra_actmon",
521         .con_id = "emc",
522
523         /* EMC suspend floor to guarantee suspend entry on PLLM */
524         .suspend_freq           = EMC_PLLP_FREQ_MAX + 2000,
525
526         .boost_freq_step        = 16000,
527         .boost_up_coef          = 200,
528         .boost_down_coef        = 50,
529 #if defined(CONFIG_ARCH_TEGRA_14x_SOC)
530         .boost_up_threshold     = 70,
531         .boost_down_threshold   = 50,
532 #else
533         .boost_up_threshold     = 60,
534         .boost_down_threshold   = 40,
535 #endif
536
537         .up_wmark_window        = 1,
538         .down_wmark_window      = 3,
539         .avg_window_log2        = ACTMON_DEFAULT_AVG_WINDOW_LOG2,
540 #if defined(CONFIG_ARCH_TEGRA_3x_SOC) || defined(CONFIG_ARCH_TEGRA_14x_SOC)
541         .count_weight           = 0x200,
542 #elif defined(CONFIG_ARCH_TEGRA_12x_SOC)
543         .count_weight           = 0x400,
544 #else
545         .count_weight           = 0x100,
546 #endif
547
548         .type                   = ACTMON_FREQ_SAMPLER,
549         .state                  = ACTMON_UNINITIALIZED,
550
551         .rate_change_nb = {
552                 .notifier_call = actmon_rate_notify_cb,
553         },
554 };
555
556 /* AVP activity monitor: load sampling device:
557  * activity counter is incremented on every actmon clock pulse while
558  * AVP is not halted by flow controller; count_weight = 1.
559  */
560 static struct actmon_dev actmon_dev_avp = {
561         .reg    = 0x0c0,
562         .glb_status_irq_mask = (0x1 << 30),
563         .dev_id = "tegra_actmon",
564         .con_id = "avp",
565
566         /* AVP/SCLK suspend activity floor */
567         .suspend_freq           = 40000,
568
569         .boost_freq_step        = 8000,
570         .boost_up_coef          = 200,
571         .boost_down_coef        = 50,
572         .boost_up_threshold     = 85,
573         .boost_down_threshold   = 50,
574
575         .up_wmark_window        = 1,
576         .down_wmark_window      = 3,
577         .avg_window_log2        = ACTMON_DEFAULT_AVG_WINDOW_LOG2,
578         .count_weight           = 0x1,
579
580         .type                   = ACTMON_LOAD_SAMPLER,
581         .state                  = ACTMON_UNINITIALIZED,
582
583         .rate_change_nb = {
584                 .notifier_call = actmon_rate_notify_cb,
585         },
586 };
587
588
589 #define CPU_AVG_ACT_THRESHOLD 50000
590 /* EMC-cpu activity monitor: frequency sampling device:
591  * activity counter is incremented every 256 memory transactions, and
592  * each transaction takes 2 EMC clocks; count_weight = 512 on Tegra3.
593  * On Tegra11 there is only 1 clock per transaction, hence weight = 256.
594  */
595 static struct actmon_dev actmon_dev_cpu_emc = {
596         .reg = 0x200,
597         .glb_status_irq_mask = (0x1 << 25),
598         .dev_id = "tegra_mon",
599         .con_id = "cpu_emc",
600
601         .boost_freq_step        = 792000,
602         .boost_up_coef          = 800,
603         .boost_down_coef        = 90,
604         .boost_up_threshold     = 30,
605         .boost_down_threshold   = 20,
606         .avg_dependency_threshold       = CPU_AVG_ACT_THRESHOLD,
607
608         .up_wmark_window        = 1,
609         .down_wmark_window      = 5,
610         .avg_window_log2        = ACTMON_DEFAULT_AVG_WINDOW_LOG2,
611 #if defined(CONFIG_ARCH_TEGRA_3x_SOC) || defined(CONFIG_ARCH_TEGRA_14x_SOC)
612         .count_weight           = 0x200,
613 #else
614         .count_weight           = 0x100,
615 #endif
616
617         .type                   = ACTMON_FREQ_SAMPLER,
618         .state                  = ACTMON_UNINITIALIZED,
619
620         .rate_change_nb = {
621                 .notifier_call = actmon_rate_notify_cb,
622         },
623 };
624
625 static struct actmon_dev *actmon_devices[] = {
626         &actmon_dev_emc,
627         &actmon_dev_avp,
628         &actmon_dev_cpu_emc,
629 };
630
631 /* Activity monitor suspend/resume */
632 static int actmon_pm_notify(struct notifier_block *nb,
633                             unsigned long event, void *data)
634 {
635         int i;
636
637         switch (event) {
638         case PM_SUSPEND_PREPARE:
639                 for (i = 0; i < ARRAY_SIZE(actmon_devices); i++)
640                         actmon_dev_suspend(actmon_devices[i]);
641                 break;
642         case PM_POST_SUSPEND:
643                 actmon_writel(actmon_sampling_period - 1,
644                               ACTMON_GLB_PERIOD_CTRL);
645                 for (i = 0; i < ARRAY_SIZE(actmon_devices); i++)
646                         actmon_dev_resume(actmon_devices[i]);
647                 break;
648         }
649
650         return NOTIFY_OK;
651 };
652
653 static struct notifier_block actmon_pm_nb = {
654         .notifier_call = actmon_pm_notify,
655 };
656
657 #ifdef CONFIG_DEBUG_FS
658
659 #define RW_MODE (S_IWUSR | S_IRUGO)
660 #define RO_MODE S_IRUGO
661
662 static struct dentry *clk_debugfs_root;
663
664 static int type_show(struct seq_file *s, void *data)
665 {
666         struct actmon_dev *dev = s->private;
667
668         seq_printf(s, "%s\n", (dev->type == ACTMON_LOAD_SAMPLER) ?
669                    "Load Activity Monitor" : "Frequency Activity Monitor");
670         return 0;
671 }
672 static int type_open(struct inode *inode, struct file *file)
673 {
674         return single_open(file, type_show, inode->i_private);
675 }
676 static const struct file_operations type_fops = {
677         .open           = type_open,
678         .read           = seq_read,
679         .llseek         = seq_lseek,
680         .release        = single_release,
681 };
682
683 static int actv_get(void *data, u64 *val)
684 {
685         unsigned long flags;
686         struct actmon_dev *dev = data;
687
688         spin_lock_irqsave(&dev->lock, flags);
689         *val = actmon_dev_avg_freq_get(dev);
690         spin_unlock_irqrestore(&dev->lock, flags);
691         return 0;
692 }
693 DEFINE_SIMPLE_ATTRIBUTE(actv_fops, actv_get, NULL, "%llu\n");
694
695 static int step_get(void *data, u64 *val)
696 {
697         struct actmon_dev *dev = data;
698         *val = dev->boost_freq_step * 100 / dev->max_freq;
699         return 0;
700 }
701 static int step_set(void *data, u64 val)
702 {
703         unsigned long flags;
704         struct actmon_dev *dev = data;
705
706         if (val > 100)
707                 val = 100;
708
709         spin_lock_irqsave(&dev->lock, flags);
710         dev->boost_freq_step = do_percent(dev->max_freq, (unsigned int)val);
711         spin_unlock_irqrestore(&dev->lock, flags);
712         return 0;
713 }
714 DEFINE_SIMPLE_ATTRIBUTE(step_fops, step_get, step_set, "%llu\n");
715
716 static int up_threshold_get(void *data, u64 *val)
717 {
718         struct actmon_dev *dev = data;
719         *val = dev->boost_up_threshold;
720         return 0;
721 }
722 static int up_threshold_set(void *data, u64 val)
723 {
724         unsigned long flags;
725         struct actmon_dev *dev = data;
726         unsigned int up_threshold = (unsigned int)val;
727
728         if (up_threshold > 100)
729                 up_threshold = 100;
730
731         spin_lock_irqsave(&dev->lock, flags);
732
733         if (up_threshold <= dev->boost_down_threshold)
734                 up_threshold = dev->boost_down_threshold;
735         if (up_threshold)
736                 dev->avg_sustain_coef = 100 * 100 / up_threshold;
737         dev->boost_up_threshold = up_threshold;
738
739         actmon_dev_up_wmark_set(dev);
740         actmon_wmb();
741
742         spin_unlock_irqrestore(&dev->lock, flags);
743         return 0;
744 }
745 DEFINE_SIMPLE_ATTRIBUTE(up_threshold_fops, up_threshold_get,
746                         up_threshold_set, "%llu\n");
747
748 static int down_threshold_get(void *data, u64 *val)
749 {
750         struct actmon_dev *dev = data;
751         *val = dev->boost_down_threshold;
752         return 0;
753 }
754 static int down_threshold_set(void *data, u64 val)
755 {
756         unsigned long flags;
757         struct actmon_dev *dev = data;
758         unsigned int down_threshold = (unsigned int)val;
759
760         spin_lock_irqsave(&dev->lock, flags);
761
762         if (down_threshold >= dev->boost_up_threshold)
763                 down_threshold = dev->boost_up_threshold;
764         dev->boost_down_threshold = down_threshold;
765
766         actmon_dev_down_wmark_set(dev);
767         actmon_wmb();
768
769         spin_unlock_irqrestore(&dev->lock, flags);
770         return 0;
771 }
772 DEFINE_SIMPLE_ATTRIBUTE(down_threshold_fops, down_threshold_get,
773                         down_threshold_set, "%llu\n");
774
775 static int state_get(void *data, u64 *val)
776 {
777         struct actmon_dev *dev = data;
778         *val = dev->state;
779         return 0;
780 }
781 static int state_set(void *data, u64 val)
782 {
783         struct actmon_dev *dev = data;
784
785         if (val)
786                 actmon_dev_enable(dev);
787         else
788                 actmon_dev_disable(dev);
789         return 0;
790 }
791 DEFINE_SIMPLE_ATTRIBUTE(state_fops, state_get, state_set, "%llu\n");
792
793 static int period_get(void *data, u64 *val)
794 {
795         *val = actmon_sampling_period;
796         return 0;
797 }
798 static int period_set(void *data, u64 val)
799 {
800         int i;
801         unsigned long flags;
802         u8 period = (u8)val;
803
804         if (period) {
805                 actmon_sampling_period = period;
806                 actmon_writel(period - 1, ACTMON_GLB_PERIOD_CTRL);
807
808                 for (i = 0; i < ARRAY_SIZE(actmon_devices); i++) {
809                         struct actmon_dev *dev = actmon_devices[i];
810                         spin_lock_irqsave(&dev->lock, flags);
811                         actmon_dev_wmark_set(dev);
812                         spin_unlock_irqrestore(&dev->lock, flags);
813                 }
814                 actmon_wmb();
815                 return 0;
816         }
817         return -EINVAL;
818 }
819 DEFINE_SIMPLE_ATTRIBUTE(period_fops, period_get, period_set, "%llu\n");
820
821
822 static int actmon_debugfs_create_dev(struct actmon_dev *dev)
823 {
824         struct dentry *dir, *d;
825
826         if (dev->state == ACTMON_UNINITIALIZED)
827                 return 0;
828
829         dir = debugfs_create_dir(dev->con_id, clk_debugfs_root);
830         if (!dir)
831                 return -ENOMEM;
832
833         d = debugfs_create_file(
834                 "actv_type", RO_MODE, dir, dev, &type_fops);
835         if (!d)
836                 return -ENOMEM;
837
838         d = debugfs_create_file(
839                 "avg_activity", RO_MODE, dir, dev, &actv_fops);
840         if (!d)
841                 return -ENOMEM;
842
843         d = debugfs_create_file(
844                 "boost_step", RW_MODE, dir, dev, &step_fops);
845         if (!d)
846                 return -ENOMEM;
847
848         d = debugfs_create_u32(
849                 "boost_rate_dec", RW_MODE, dir, (u32 *)&dev->boost_down_coef);
850         if (!d)
851                 return -ENOMEM;
852
853         d = debugfs_create_u32(
854                 "boost_rate_inc", RW_MODE, dir, (u32 *)&dev->boost_up_coef);
855         if (!d)
856                 return -ENOMEM;
857
858         d = debugfs_create_file(
859                 "boost_threshold_dn", RW_MODE, dir, dev, &down_threshold_fops);
860         if (!d)
861                 return -ENOMEM;
862
863         d = debugfs_create_file(
864                 "boost_threshold_up", RW_MODE, dir, dev, &up_threshold_fops);
865         if (!d)
866                 return -ENOMEM;
867
868         d = debugfs_create_file(
869                 "state", RW_MODE, dir, dev, &state_fops);
870
871         d = debugfs_create_u32(
872                 "avg_act_threshold", RW_MODE, dir,
873                 (u32 *)&dev->avg_dependency_threshold);
874
875         if (!d)
876                 return -ENOMEM;
877
878         return 0;
879 }
880
881 static int __init actmon_debugfs_init(void)
882 {
883         int i;
884         int ret = -ENOMEM;
885         struct dentry *d;
886
887         d = debugfs_create_dir("tegra_actmon", NULL);
888         if (!d)
889                 return ret;
890         clk_debugfs_root = d;
891
892         d = debugfs_create_file("period", RW_MODE, d, NULL, &period_fops);
893         if (!d)
894                 goto err_out;
895
896         for (i = 0; i < ARRAY_SIZE(actmon_devices); i++) {
897                 ret = actmon_debugfs_create_dev(actmon_devices[i]);
898                 if (ret)
899                         goto err_out;
900         }
901         return 0;
902
903 err_out:
904         debugfs_remove_recursive(clk_debugfs_root);
905         return ret;
906 }
907
908 #endif
909
910 static int __init tegra_actmon_init(void)
911 {
912         int i, ret;
913         struct clk *c = tegra_get_clock_by_name("actmon");
914
915         if (!c) {
916                 pr_err("%s: Failed to find actmon clock\n", __func__);
917                 return 0;
918         }
919         actmon_clk_freq = clk_get_rate(c) / 1000;
920         ret = tegra_clk_prepare_enable(c);
921         if (ret) {
922                 pr_err("%s: Failed to enable actmon clock\n", __func__);
923                 return 0;
924         }
925         actmon_sampling_period = ACTMON_DEFAULT_SAMPLING_PERIOD;
926         actmon_writel(actmon_sampling_period - 1, ACTMON_GLB_PERIOD_CTRL);
927
928         for (i = 0; i < ARRAY_SIZE(actmon_devices); i++) {
929                 ret = actmon_dev_init(actmon_devices[i]);
930                 pr_info("%s.%s: %s initialization (%d)\n",
931                         actmon_devices[i]->dev_id, actmon_devices[i]->con_id,
932                         ret ? "Failed" : "Completed", ret);
933         }
934         register_pm_notifier(&actmon_pm_nb);
935
936 #ifdef CONFIG_DEBUG_FS
937         actmon_debugfs_init();
938 #endif
939         return 0;
940 }
941 late_initcall(tegra_actmon_init);