2 * Copyright (C) ARM Limited 2011-2014. All rights reserved.
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.
10 #include <linux/cpufreq.h>
11 #include <trace/events/power.h>
15 #include <asm/mach-types.h>
17 #define implements_wfi() (!machine_is_omap3_beagle())
21 #define implements_wfi() false
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
29 #if GATOR_CPU_FREQ_SUPPORT
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;
40 static int gator_trace_power_create_files(struct super_block *sb, struct dentry *root)
44 bool found_nonzero_freq = false;
46 /* Even if CONFIG_CPU_FREQ is defined, it still may not be
47 * used. Check for non-zero values from cpufreq_quick_get
49 for_each_online_cpu(cpu) {
50 if (cpufreq_quick_get(cpu) > 0) {
51 found_nonzero_freq = true;
56 if (found_nonzero_freq) {
58 dir = gatorfs_mkdir(sb, root, "Linux_power_cpu_freq");
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]);
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))
71 cpu = lcpu_to_pcpu(cpu);
72 marshal_event_single64(cpu, power_cpu_key[POWER_CPU_FREQ], frequency * 1000L);
75 GATOR_DEFINE_PROBE(cpu_idle, TP_PROTO(unsigned int state, unsigned int cpu))
77 cpu = lcpu_to_pcpu(cpu);
79 if (state == per_cpu(idle_prev_state, cpu))
82 if (implements_wfi()) {
83 if (state == PWR_EVENT_EXIT) {
84 /* transition from wfi to non-wfi */
85 marshal_idle(cpu, MESSAGE_IDLE_EXIT);
87 /* transition from non-wfi to wfi */
88 marshal_idle(cpu, MESSAGE_IDLE_ENTER);
92 per_cpu(idle_prev_state, cpu) = state;
95 static void gator_trace_power_online(void)
97 int pcpu = get_physical_cpu();
98 int lcpu = get_logical_cpu();
100 if (power_cpu_enabled[POWER_CPU_FREQ])
101 marshal_event_single64(pcpu, power_cpu_key[POWER_CPU_FREQ], cpufreq_quick_get(lcpu) * 1000L);
104 static void gator_trace_power_offline(void)
106 /* Set frequency to zero on an offline */
107 int cpu = get_physical_cpu();
109 if (power_cpu_enabled[POWER_CPU_FREQ])
110 marshal_event_single(cpu, power_cpu_key[POWER_CPU_FREQ], 0);
113 static int gator_trace_power_start(void)
117 /* register tracepoints */
118 if (power_cpu_enabled[POWER_CPU_FREQ])
119 if (GATOR_REGISTER_TRACE(cpu_frequency))
120 goto fail_cpu_frequency_exit;
122 /* Always register for cpu_idle for detecting WFI */
123 if (GATOR_REGISTER_TRACE(cpu_idle))
124 goto fail_cpu_idle_exit;
125 pr_debug("gator: registered power event tracepoints\n");
127 for_each_present_cpu(cpu) {
128 per_cpu(idle_prev_state, cpu) = 0;
133 /* unregister tracepoints on error */
135 if (power_cpu_enabled[POWER_CPU_FREQ])
136 GATOR_UNREGISTER_TRACE(cpu_frequency);
137 fail_cpu_frequency_exit:
138 pr_err("gator: power event tracepoints failed to activate, please verify that tracepoints are enabled in the linux kernel\n");
143 static void gator_trace_power_stop(void)
147 if (power_cpu_enabled[POWER_CPU_FREQ])
148 GATOR_UNREGISTER_TRACE(cpu_frequency);
149 GATOR_UNREGISTER_TRACE(cpu_idle);
150 pr_debug("gator: unregistered power event tracepoints\n");
152 for (i = 0; i < POWER_TOTAL; i++)
153 power_cpu_enabled[i] = 0;
156 static void gator_trace_power_init(void)
160 power_cpu_cores = nr_cpu_ids;
161 for (i = 0; i < POWER_TOTAL; i++) {
162 power_cpu_enabled[i] = 0;
163 power_cpu_key[i] = gator_events_get_key();
167 static int gator_trace_power_create_files(struct super_block *sb, struct dentry *root)
172 static void gator_trace_power_online(void)
176 static void gator_trace_power_offline(void)
180 static int gator_trace_power_start(void)
185 static void gator_trace_power_stop(void)
189 static void gator_trace_power_init(void)