blob: f182fe10c03dd737f2de55090226468ece4493d0 [file] [log] [blame]
Shefali Jain0dc6e782017-11-27 13:06:27 +05301/*
2 * Copyright (C) 2007 Google, Inc.
3 * Copyright (c) 2007-2014, 2017, The Linux Foundation. All rights reserved.
4 *
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/ctype.h>
19#include <linux/debugfs.h>
20#include <linux/seq_file.h>
21#include <linux/clk.h>
22#include <linux/list.h>
23#include <linux/clkdev.h>
24#include <linux/uaccess.h>
25#include <linux/mutex.h>
26#include <linux/io.h>
27#include <linux/clk/msm-clk-provider.h>
28#include <trace/events/power.h>
29
30
31#include "clock.h"
32
33static LIST_HEAD(clk_list);
34static DEFINE_MUTEX(clk_list_lock);
35
36static struct dentry *debugfs_base;
37static u32 debug_suspend;
38
39static int clock_debug_rate_set(void *data, u64 val)
40{
41 struct clk *clock = data;
42 int ret;
43
44 /* Only increases to max rate will succeed, but that's actually good
45 * for debugging purposes so we don't check for error.
46 */
47 if (clock->flags & CLKFLAG_MAX)
48 clk_set_max_rate(clock, val);
49 ret = clk_set_rate(clock, val);
50 if (ret)
51 pr_err("clk_set_rate(%s, %lu) failed (%d)\n", clock->dbg_name,
52 (unsigned long)val, ret);
53
54 return ret;
55}
56
57static int clock_debug_rate_get(void *data, u64 *val)
58{
59 struct clk *clock = data;
60 *val = clk_get_rate(clock);
61 return 0;
62}
63
64DEFINE_SIMPLE_ATTRIBUTE(clock_rate_fops, clock_debug_rate_get,
65 clock_debug_rate_set, "%llu\n");
66
67static struct clk *measure;
68
69static int clock_debug_measure_get(void *data, u64 *val)
70{
71 struct clk *clock = data, *par;
72 int ret, is_hw_gated;
73 unsigned long meas_rate, sw_rate;
74
75 /* Check to see if the clock is in hardware gating mode */
76 if (clock->ops->in_hwcg_mode)
77 is_hw_gated = clock->ops->in_hwcg_mode(clock);
78 else
79 is_hw_gated = 0;
80
81 ret = clk_set_parent(measure, clock);
82 if (!ret) {
83 /*
84 * Disable hw gating to get accurate rate measurements. Only do
85 * this if the clock is explicitly enabled by software. This
86 * allows us to detect errors where clocks are on even though
87 * software is not requesting them to be on due to broken
88 * hardware gating signals.
89 */
90 if (is_hw_gated && clock->count)
91 clock->ops->disable_hwcg(clock);
92 par = measure;
93 while (par && par != clock) {
94 if (par->ops->enable)
95 par->ops->enable(par);
96 par = par->parent;
97 }
98 *val = clk_get_rate(measure);
99 /* Reenable hwgating if it was disabled */
100 if (is_hw_gated && clock->count)
101 clock->ops->enable_hwcg(clock);
102 }
103
104 /*
105 * If there's a divider on the path from the clock output to the
106 * measurement circuitry, account for it by dividing the original clock
107 * rate with the rate set on the parent of the measure clock.
108 */
109 meas_rate = clk_get_rate(clock);
110 sw_rate = clk_get_rate(measure->parent);
111 if (sw_rate && meas_rate >= (sw_rate * 2))
112 *val *= DIV_ROUND_CLOSEST(meas_rate, sw_rate);
113
114 return ret;
115}
116
117DEFINE_SIMPLE_ATTRIBUTE(clock_measure_fops, clock_debug_measure_get,
118 NULL, "%lld\n");
119
120static int clock_debug_enable_set(void *data, u64 val)
121{
122 struct clk *clock = data;
123 int rc = 0;
124
125 if (val)
126 rc = clk_prepare_enable(clock);
127 else
128 clk_disable_unprepare(clock);
129
130 return rc;
131}
132
133static int clock_debug_enable_get(void *data, u64 *val)
134{
135 struct clk *clock = data;
136 int enabled;
137
138 if (clock->ops->is_enabled)
139 enabled = clock->ops->is_enabled(clock);
140 else
141 enabled = !!(clock->count);
142
143 *val = enabled;
144 return 0;
145}
146
147DEFINE_SIMPLE_ATTRIBUTE(clock_enable_fops, clock_debug_enable_get,
148 clock_debug_enable_set, "%lld\n");
149
150static int clock_debug_local_get(void *data, u64 *val)
151{
152 struct clk *clock = data;
153
154 if (!clock->ops->is_local)
155 *val = true;
156 else
157 *val = clock->ops->is_local(clock);
158
159 return 0;
160}
161
162DEFINE_SIMPLE_ATTRIBUTE(clock_local_fops, clock_debug_local_get,
163 NULL, "%llu\n");
164
165static int clock_debug_hwcg_get(void *data, u64 *val)
166{
167 struct clk *clock = data;
168
169 if (clock->ops->in_hwcg_mode)
170 *val = !!clock->ops->in_hwcg_mode(clock);
171 else
172 *val = 0;
173 return 0;
174}
175
176DEFINE_SIMPLE_ATTRIBUTE(clock_hwcg_fops, clock_debug_hwcg_get,
177 NULL, "%llu\n");
178
179static void clock_print_fmax_by_level(struct seq_file *m, int level)
180{
181 struct clk *clock = m->private;
182 struct clk_vdd_class *vdd_class = clock->vdd_class;
183 int off, i, vdd_level, nregs = vdd_class->num_regulators;
184
185 vdd_level = find_vdd_level(clock, clock->rate);
186
187 seq_printf(m, "%2s%10lu", vdd_level == level ? "[" : "",
188 clock->fmax[level]);
189 for (i = 0; i < nregs; i++) {
190 off = nregs*level + i;
191 if (vdd_class->vdd_uv)
192 seq_printf(m, "%10u", vdd_class->vdd_uv[off]);
193 if (vdd_class->vdd_ua)
194 seq_printf(m, "%10u", vdd_class->vdd_ua[off]);
195 }
196
197 if (vdd_level == level)
198 seq_puts(m, "]");
199 seq_puts(m, "\n");
200}
201
202static int fmax_rates_show(struct seq_file *m, void *unused)
203{
204 struct clk *clock = m->private;
205 struct clk_vdd_class *vdd_class = clock->vdd_class;
206 int level = 0, i, nregs = vdd_class->num_regulators;
207 char reg_name[10];
208
209 int vdd_level = find_vdd_level(clock, clock->rate);
210
211 if (vdd_level < 0) {
212 seq_printf(m, "could not find_vdd_level for %s, %ld\n",
213 clock->dbg_name, clock->rate);
214 return 0;
215 }
216
217 seq_printf(m, "%12s", "");
218 for (i = 0; i < nregs; i++) {
219 snprintf(reg_name, ARRAY_SIZE(reg_name), "reg %d", i);
220 seq_printf(m, "%10s", reg_name);
221 if (vdd_class->vdd_ua)
222 seq_printf(m, "%10s", "");
223 }
224
225 seq_printf(m, "\n%12s", "freq");
226 for (i = 0; i < nregs; i++) {
227 seq_printf(m, "%10s", "uV");
228 if (vdd_class->vdd_ua)
229 seq_printf(m, "%10s", "uA");
230 }
231 seq_puts(m, "\n");
232
233 for (level = 0; level < clock->num_fmax; level++)
234 clock_print_fmax_by_level(m, level);
235
236 return 0;
237}
238
239static int fmax_rates_open(struct inode *inode, struct file *file)
240{
241 return single_open(file, fmax_rates_show, inode->i_private);
242}
243
244static const struct file_operations fmax_rates_fops = {
245 .open = fmax_rates_open,
246 .read = seq_read,
247 .llseek = seq_lseek,
248 .release = seq_release,
249};
250
251static int orphan_list_show(struct seq_file *m, void *unused)
252{
253 struct clk *c, *safe;
254
255 list_for_each_entry_safe(c, safe, &orphan_clk_list, list)
256 seq_printf(m, "%s\n", c->dbg_name);
257
258 return 0;
259}
260
261static int orphan_list_open(struct inode *inode, struct file *file)
262{
263 return single_open(file, orphan_list_show, inode->i_private);
264}
265
266static const struct file_operations orphan_list_fops = {
267 .open = orphan_list_open,
268 .read = seq_read,
269 .llseek = seq_lseek,
270 .release = seq_release,
271};
272
273#define clock_debug_output(m, c, fmt, ...) \
274do { \
275 if (m) \
276 seq_printf(m, fmt, ##__VA_ARGS__); \
277 else if (c) \
278 pr_cont(fmt, ##__VA_ARGS__); \
279 else \
280 pr_info(fmt, ##__VA_ARGS__); \
281} while (0)
282
283/*
284 * clock_debug_print_enabled_debug_suspend() - Print names of enabled clocks
285 * during suspend.
286 */
287static void clock_debug_print_enabled_debug_suspend(struct seq_file *s)
288{
289 struct clk *c;
290 int cnt = 0;
291
292 if (!mutex_trylock(&clk_list_lock))
293 return;
294
295 clock_debug_output(s, 0, "Enabled clocks:\n");
296
297 list_for_each_entry(c, &clk_list, list) {
298 if (!c || !c->prepare_count)
299 continue;
300 if (c->vdd_class)
301 clock_debug_output(s, 0, " %s:%lu:%lu [%ld, %d]",
302 c->dbg_name, c->prepare_count,
303 c->count, c->rate,
304 find_vdd_level(c, c->rate));
305 else
306 clock_debug_output(s, 0, " %s:%lu:%lu [%ld]",
307 c->dbg_name, c->prepare_count,
308 c->count, c->rate);
309 cnt++;
310 }
311
312 mutex_unlock(&clk_list_lock);
313
314 if (cnt)
315 clock_debug_output(s, 0, "Enabled clock count: %d\n", cnt);
316 else
317 clock_debug_output(s, 0, "No clocks enabled.\n");
318}
319
320static int clock_debug_print_clock(struct clk *c, struct seq_file *m)
321{
322 char *start = "";
323
324 if (!c || !c->prepare_count)
325 return 0;
326
327 clock_debug_output(m, 0, "\t");
328 do {
329 if (c->vdd_class)
330 clock_debug_output(m, 1, "%s%s:%lu:%lu [%ld, %d]",
331 start, c->dbg_name, c->prepare_count, c->count,
332 c->rate, find_vdd_level(c, c->rate));
333 else
334 clock_debug_output(m, 1, "%s%s:%lu:%lu [%ld]", start,
335 c->dbg_name, c->prepare_count, c->count,
336 c->rate);
337 start = " -> ";
338 } while ((c = clk_get_parent(c)));
339
340 clock_debug_output(m, 1, "\n");
341
342 return 1;
343}
344
345/**
346 * clock_debug_print_enabled_clocks() - Print names of enabled clocks
347 *
348 */
349static void clock_debug_print_enabled_clocks(struct seq_file *m)
350{
351 struct clk *c;
352 int cnt = 0;
353
354 if (!mutex_trylock(&clk_list_lock)) {
355 pr_err("clock-debug: Clocks are being registered. Cannot print clock state now.\n");
356 return;
357 }
358 clock_debug_output(m, 0, "Enabled clocks:\n");
359 list_for_each_entry(c, &clk_list, list) {
360 cnt += clock_debug_print_clock(c, m);
361 }
362 mutex_unlock(&clk_list_lock);
363
364 if (cnt)
365 clock_debug_output(m, 0, "Enabled clock count: %d\n", cnt);
366 else
367 clock_debug_output(m, 0, "No clocks enabled.\n");
368}
369
370static int enabled_clocks_show(struct seq_file *m, void *unused)
371{
372 clock_debug_print_enabled_clocks(m);
373 return 0;
374}
375
376static int enabled_clocks_open(struct inode *inode, struct file *file)
377{
378 return single_open(file, enabled_clocks_show, inode->i_private);
379}
380
381static const struct file_operations enabled_clocks_fops = {
382 .open = enabled_clocks_open,
383 .read = seq_read,
384 .llseek = seq_lseek,
385 .release = seq_release,
386};
387
388static int trace_clocks_show(struct seq_file *m, void *unused)
389{
390 struct clk *c;
391 int total_cnt = 0;
392
393 if (!mutex_trylock(&clk_list_lock)) {
394 pr_err("trace_clocks: Clocks are being registered. Cannot trace clock state now.\n");
395 return 1;
396 }
397 list_for_each_entry(c, &clk_list, list) {
398 trace_clock_state(c->dbg_name, c->prepare_count, c->count,
399 c->rate);
400 total_cnt++;
401 }
402 mutex_unlock(&clk_list_lock);
403 clock_debug_output(m, 0, "Total clock count: %d\n", total_cnt);
404
405 return 0;
406}
407
408static int trace_clocks_open(struct inode *inode, struct file *file)
409{
410 return single_open(file, trace_clocks_show, inode->i_private);
411}
412static const struct file_operations trace_clocks_fops = {
413 .open = trace_clocks_open,
414 .read = seq_read,
415 .llseek = seq_lseek,
416 .release = seq_release,
417};
418
419static int list_rates_show(struct seq_file *m, void *unused)
420{
421 struct clk *clock = m->private;
422 int level, i = 0;
423 unsigned long rate, fmax = 0;
424
425 /* Find max frequency supported within voltage constraints. */
426 if (!clock->vdd_class) {
427 fmax = ULONG_MAX;
428 } else {
429 for (level = 0; level < clock->num_fmax; level++)
430 if (clock->fmax[level])
431 fmax = clock->fmax[level];
432 }
433
434 /*
435 * List supported frequencies <= fmax. Higher frequencies may appear in
436 * the frequency table, but are not valid and should not be listed.
437 */
438 while (!IS_ERR_VALUE(rate = clock->ops->list_rate(clock, i++))) {
439 if (rate <= fmax)
440 seq_printf(m, "%lu\n", rate);
441 }
442
443 return 0;
444}
445
446static int list_rates_open(struct inode *inode, struct file *file)
447{
448 return single_open(file, list_rates_show, inode->i_private);
449}
450
451static const struct file_operations list_rates_fops = {
452 .open = list_rates_open,
453 .read = seq_read,
454 .llseek = seq_lseek,
455 .release = seq_release,
456};
457
458static ssize_t clock_parent_read(struct file *filp, char __user *ubuf,
459 size_t cnt, loff_t *ppos)
460{
461 struct clk *clock = filp->private_data;
462 struct clk *p = clock->parent;
463 char name[256] = {0};
464
465 snprintf(name, sizeof(name), "%s\n", p ? p->dbg_name : "None\n");
466
467 return simple_read_from_buffer(ubuf, cnt, ppos, name, strlen(name));
468}
469
470
471static ssize_t clock_parent_write(struct file *filp,
472 const char __user *ubuf, size_t cnt, loff_t *ppos)
473{
474 struct clk *clock = filp->private_data;
475 char buf[256];
476 char *cmp;
477 int ret;
478 struct clk *parent = NULL;
479
480 cnt = min(cnt, sizeof(buf) - 1);
481 if (copy_from_user(&buf, ubuf, cnt))
482 return -EFAULT;
483 buf[cnt] = '\0';
484 cmp = strstrip(buf);
485
486 mutex_lock(&clk_list_lock);
487 list_for_each_entry(parent, &clk_list, list) {
488 if (!strcmp(cmp, parent->dbg_name))
489 break;
490 }
491
492 if (&parent->list == &clk_list) {
493 ret = -EINVAL;
494 goto err;
495 }
496
497 mutex_unlock(&clk_list_lock);
498 ret = clk_set_parent(clock, parent);
499 if (ret)
500 return ret;
501
502 return cnt;
503err:
504 mutex_unlock(&clk_list_lock);
505 return ret;
506}
507
508
509static const struct file_operations clock_parent_fops = {
510 .open = simple_open,
511 .read = clock_parent_read,
512 .write = clock_parent_write,
513};
514
515void clk_debug_print_hw(struct clk *clk, struct seq_file *f)
516{
517 void __iomem *base;
518 struct clk_register_data *regs;
519 u32 i, j, size;
520
521 if (IS_ERR_OR_NULL(clk))
522 return;
523
524 clk_debug_print_hw(clk->parent, f);
525
526 clock_debug_output(f, false, "%s\n", clk->dbg_name);
527
528 if (!clk->ops->list_registers)
529 return;
530
531 j = 0;
532 base = clk->ops->list_registers(clk, j, &regs, &size);
533 while (!IS_ERR(base)) {
534 for (i = 0; i < size; i++) {
535 u32 val = readl_relaxed(base + regs[i].offset);
536
537 clock_debug_output(f, false, "%20s: 0x%.8x\n",
538 regs[i].name, val);
539 }
540 j++;
541 base = clk->ops->list_registers(clk, j, &regs, &size);
542 }
543}
544
545static int print_hw_show(struct seq_file *m, void *unused)
546{
547 struct clk *c = m->private;
548
549 clk_debug_print_hw(c, m);
550
551 return 0;
552}
553
554static int print_hw_open(struct inode *inode, struct file *file)
555{
556 return single_open(file, print_hw_show, inode->i_private);
557}
558
559static const struct file_operations clock_print_hw_fops = {
560 .open = print_hw_open,
561 .read = seq_read,
562 .llseek = seq_lseek,
563 .release = seq_release,
564};
565
566
567static void clock_measure_add(struct clk *clock)
568{
569 if (IS_ERR_OR_NULL(measure))
570 return;
571
572 if (clk_set_parent(measure, clock))
573 return;
574
575 debugfs_create_file("measure", 0444, clock->clk_dir, clock,
576 &clock_measure_fops);
577}
578
579static int clock_debug_add(struct clk *clock)
580{
581 char temp[50], *ptr;
582 struct dentry *clk_dir;
583
584 if (!debugfs_base)
585 return -ENOMEM;
586
587 strlcpy(temp, clock->dbg_name, ARRAY_SIZE(temp));
588 for (ptr = temp; *ptr; ptr++)
589 *ptr = tolower(*ptr);
590
591 clk_dir = debugfs_create_dir(temp, debugfs_base);
592 if (!clk_dir)
593 return -ENOMEM;
594
595 clock->clk_dir = clk_dir;
596
597 if (!debugfs_create_file("rate", 0644, clk_dir,
598 clock, &clock_rate_fops))
599 goto error;
600
601 if (!debugfs_create_file("enable", 0644, clk_dir,
602 clock, &clock_enable_fops))
603 goto error;
604
605 if (!debugfs_create_file("is_local", 0444, clk_dir, clock,
606 &clock_local_fops))
607 goto error;
608
609 if (!debugfs_create_file("has_hw_gating", 0444, clk_dir, clock,
610 &clock_hwcg_fops))
611 goto error;
612
613 if (clock->ops->list_rate)
614 if (!debugfs_create_file("list_rates",
615 0444, clk_dir, clock, &list_rates_fops))
616 goto error;
617
618 if (clock->vdd_class && !debugfs_create_file(
619 "fmax_rates", 0444, clk_dir, clock, &fmax_rates_fops))
620 goto error;
621
622 if (!debugfs_create_file("parent", 0444, clk_dir, clock,
623 &clock_parent_fops))
624 goto error;
625
626 if (!debugfs_create_file("print", 0444, clk_dir, clock,
627 &clock_print_hw_fops))
628 goto error;
629
630 clock_measure_add(clock);
631
632 return 0;
633error:
634 debugfs_remove_recursive(clk_dir);
635 return -ENOMEM;
636}
637static DEFINE_MUTEX(clk_debug_lock);
638static int clk_debug_init_once;
639
640/**
641 * clock_debug_init() - Initialize clock debugfs
642 * Lock clk_debug_lock before invoking this function.
643 */
644static int clock_debug_init(void)
645{
646 if (clk_debug_init_once)
647 return 0;
648
649 clk_debug_init_once = 1;
650
651 debugfs_base = debugfs_create_dir("clk", NULL);
652 if (!debugfs_base)
653 return -ENOMEM;
654
655 if (!debugfs_create_u32("debug_suspend", 0644,
656 debugfs_base, &debug_suspend)) {
657 debugfs_remove_recursive(debugfs_base);
658 return -ENOMEM;
659 }
660
661 if (!debugfs_create_file("enabled_clocks", 0444, debugfs_base, NULL,
662 &enabled_clocks_fops))
663 return -ENOMEM;
664
665 if (!debugfs_create_file("orphan_list", 0444, debugfs_base, NULL,
666 &orphan_list_fops))
667 return -ENOMEM;
668
669 if (!debugfs_create_file("trace_clocks", 0444, debugfs_base, NULL,
670 &trace_clocks_fops))
671 return -ENOMEM;
672
673 return 0;
674}
675
676/**
677 * clock_debug_register() - Add additional clocks to clock debugfs hierarchy
678 * @list: List of clocks to create debugfs nodes for
679 */
680int clock_debug_register(struct clk *clk)
681{
682 int ret = 0;
683 struct clk *c;
684
685 mutex_lock(&clk_list_lock);
686 if (!list_empty(&clk->list))
687 goto out;
688
689 ret = clock_debug_init();
690 if (ret)
691 goto out;
692
693 if (IS_ERR_OR_NULL(measure)) {
694 if (clk->flags & CLKFLAG_MEASURE)
695 measure = clk;
696 if (!IS_ERR_OR_NULL(measure)) {
697 list_for_each_entry(c, &clk_list, list)
698 clock_measure_add(c);
699 }
700 }
701
702 list_add_tail(&clk->list, &clk_list);
703 clock_debug_add(clk);
704out:
705 mutex_unlock(&clk_list_lock);
706 return ret;
707}
708
709/*
710 * Print the names of enabled clocks and their parents if debug_suspend is set
711 */
712void clock_debug_print_enabled(bool print_parent)
713{
714 if (likely(!debug_suspend))
715 return;
716 if (print_parent)
717 clock_debug_print_enabled_clocks(NULL);
718 else
719 clock_debug_print_enabled_debug_suspend(NULL);
720
721}