mmc: dw_mmc: fix the max_blk_count in IDMAC
[firefly-linux-kernel-4.4.55.git] / drivers / gator / gator_trace_power.c
1 /**
2  * Copyright (C) ARM Limited 2011-2015. 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 version 2 as
6  * published by the Free Software Foundation.
7  *
8  */
9
10 #include <linux/cpufreq.h>
11 #include <trace/events/power.h>
12
13 #if defined(__arm__)
14
15 #include <asm/mach-types.h>
16
17 #define implements_wfi() (!machine_is_omap3_beagle())
18
19 #else
20
21 #define implements_wfi() false
22
23 #endif
24
25 /* cpu_frequency and cpu_idle trace points were introduced in Linux
26  * kernel v2.6.38 the now deprecated power_frequency trace point was
27  * available prior to 2.6.38, but only for x86
28  */
29 #if GATOR_CPU_FREQ_SUPPORT
30 enum {
31         POWER_CPU_FREQ,
32         POWER_TOTAL
33 };
34
35 static DEFINE_PER_CPU(ulong, idle_prev_state);
36 static ulong power_cpu_enabled[POWER_TOTAL];
37 static ulong power_cpu_key[POWER_TOTAL];
38 static ulong power_cpu_cores;
39
40 static int gator_trace_power_create_files(struct super_block *sb, struct dentry *root)
41 {
42         struct dentry *dir;
43         int cpu;
44         bool found_nonzero_freq = false;
45
46         /* Even if CONFIG_CPU_FREQ is defined, it still may not be
47          * used. Check for non-zero values from cpufreq_quick_get
48          */
49         for_each_online_cpu(cpu) {
50                 if (cpufreq_quick_get(cpu) > 0) {
51                         found_nonzero_freq = true;
52                         break;
53                 }
54         }
55
56         if (found_nonzero_freq) {
57                 /* cpu_frequency */
58                 dir = gatorfs_mkdir(sb, root, "Linux_power_cpu_freq");
59                 if (!dir)
60                         return -1;
61                 gatorfs_create_ulong(sb, dir, "enabled", &power_cpu_enabled[POWER_CPU_FREQ]);
62                 gatorfs_create_ro_ulong(sb, dir, "key", &power_cpu_key[POWER_CPU_FREQ]);
63         }
64
65         return 0;
66 }
67
68 /* 'cpu' may not equal smp_processor_id(), i.e. may not be running on the core that is having the freq/idle state change */
69 GATOR_DEFINE_PROBE(cpu_frequency, TP_PROTO(unsigned int frequency, unsigned int cpu))
70 {
71         cpu = lcpu_to_pcpu(cpu);
72         marshal_event_single64(cpu, power_cpu_key[POWER_CPU_FREQ], frequency * 1000L);
73 }
74
75 GATOR_DEFINE_PROBE(cpu_idle, TP_PROTO(unsigned int state, unsigned int cpu))
76 {
77         cpu = lcpu_to_pcpu(cpu);
78
79         if (state == per_cpu(idle_prev_state, cpu))
80                 return;
81
82         if (implements_wfi()) {
83                 marshal_idle(cpu, state);
84         }
85
86         per_cpu(idle_prev_state, cpu) = state;
87 }
88
89 static void gator_trace_power_online(void)
90 {
91         int pcpu = get_physical_cpu();
92         int lcpu = get_logical_cpu();
93
94         if (power_cpu_enabled[POWER_CPU_FREQ])
95                 marshal_event_single64(pcpu, power_cpu_key[POWER_CPU_FREQ], cpufreq_quick_get(lcpu) * 1000L);
96 }
97
98 static void gator_trace_power_offline(void)
99 {
100         /* Set frequency to zero on an offline */
101         int cpu = get_physical_cpu();
102
103         if (power_cpu_enabled[POWER_CPU_FREQ])
104                 marshal_event_single(cpu, power_cpu_key[POWER_CPU_FREQ], 0);
105 }
106
107 static int gator_trace_power_start(void)
108 {
109         int cpu;
110
111         /* register tracepoints */
112         if (power_cpu_enabled[POWER_CPU_FREQ])
113                 if (GATOR_REGISTER_TRACE(cpu_frequency))
114                         goto fail_cpu_frequency_exit;
115
116         /* Always register for cpu_idle for detecting WFI */
117         if (GATOR_REGISTER_TRACE(cpu_idle))
118                 goto fail_cpu_idle_exit;
119         pr_debug("gator: registered power event tracepoints\n");
120
121         for_each_present_cpu(cpu) {
122                 per_cpu(idle_prev_state, cpu) = 0;
123         }
124
125         return 0;
126
127         /* unregister tracepoints on error */
128 fail_cpu_idle_exit:
129         if (power_cpu_enabled[POWER_CPU_FREQ])
130                 GATOR_UNREGISTER_TRACE(cpu_frequency);
131 fail_cpu_frequency_exit:
132         pr_err("gator: power event tracepoints failed to activate, please verify that tracepoints are enabled in the linux kernel\n");
133
134         return -1;
135 }
136
137 static void gator_trace_power_stop(void)
138 {
139         int i;
140
141         if (power_cpu_enabled[POWER_CPU_FREQ])
142                 GATOR_UNREGISTER_TRACE(cpu_frequency);
143         GATOR_UNREGISTER_TRACE(cpu_idle);
144         pr_debug("gator: unregistered power event tracepoints\n");
145
146         for (i = 0; i < POWER_TOTAL; i++)
147                 power_cpu_enabled[i] = 0;
148 }
149
150 static void gator_trace_power_init(void)
151 {
152         int i;
153
154         power_cpu_cores = nr_cpu_ids;
155         for (i = 0; i < POWER_TOTAL; i++) {
156                 power_cpu_enabled[i] = 0;
157                 power_cpu_key[i] = gator_events_get_key();
158         }
159 }
160 #else
161 static int gator_trace_power_create_files(struct super_block *sb, struct dentry *root)
162 {
163         return 0;
164 }
165
166 static void gator_trace_power_online(void)
167 {
168 }
169
170 static void gator_trace_power_offline(void)
171 {
172 }
173
174 static int gator_trace_power_start(void)
175 {
176         return 0;
177 }
178
179 static void gator_trace_power_stop(void)
180 {
181 }
182
183 static void gator_trace_power_init(void)
184 {
185 }
186 #endif