blob: 8eb124454cb4f07c826bfdf271e8d146d2e88383 [file] [log] [blame]
Sheetal Sahasrabudhefa003452014-01-08 16:50:42 -05001/* Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
Ashwin Chaugule4eaee1a2013-03-14 18:37:49 -04002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12#include <asm/thread_notify.h>
Sheetal Sahasrabudhe711300e2013-07-10 12:18:59 -040013#include <linux/uaccess.h>
14#include <linux/debugfs.h>
Ashwin Chaugule4eaee1a2013-03-14 18:37:49 -040015#define CREATE_TRACE_POINTS
16#include "perf_trace_counters.h"
17
Sheetal Sahasrabudhe711300e2013-07-10 12:18:59 -040018static unsigned int tp_pid_state;
19
Sheetal Sahasrabudhee2ec9802013-10-15 14:48:52 -040020DEFINE_PER_CPU(u32, previous_ccnt);
21DEFINE_PER_CPU(u32[NUM_L1_CTRS], previous_l1_cnts);
22DEFINE_PER_CPU(u32[NUM_L2_PERCPU], previous_l2_cnts);
Sheetal Sahasrabudhe0b9db082013-11-20 15:04:04 -050023DEFINE_PER_CPU(u32, old_pid);
Sheetal Sahasrabudhe954248e2014-02-07 12:21:17 -050024DEFINE_PER_CPU(u32, hotplug_flag);
Sheetal Sahasrabudhee2ec9802013-10-15 14:48:52 -040025/* Reset per_cpu variables that store counter values uppn CPU hotplug */
26static int tracectr_cpu_hotplug_notifier(struct notifier_block *self,
27 unsigned long action, void *hcpu)
28{
29 int ret = NOTIFY_OK;
30 int cpu = (int)hcpu;
Sheetal Sahasrabudhee2ec9802013-10-15 14:48:52 -040031
Sheetal Sahasrabudhe954248e2014-02-07 12:21:17 -050032 if ((action & (~CPU_TASKS_FROZEN)) == CPU_STARTING)
33 per_cpu(hotplug_flag, cpu) = 1;
34
Sheetal Sahasrabudhee2ec9802013-10-15 14:48:52 -040035 return ret;
36}
37
38static struct notifier_block tracectr_cpu_hotplug_notifier_block = {
39 .notifier_call = tracectr_cpu_hotplug_notifier,
40};
41
Sheetal Sahasrabudhe954248e2014-02-07 12:21:17 -050042static void setup_prev_cnts(u32 cpu)
43{
44 int i;
45 u32 cnten_val;
46
47 /* Read PMCNTENSET */
48 asm volatile("mrc p15, 0, %0, c9, c12, 1" : "=r"(cnten_val));
49 /* Disable all the counters that were enabled */
50 asm volatile("mcr p15, 0, %0, c9, c12, 2" : : "r"(cnten_val));
51 if (cnten_val & CC) {
52 /* Read value */
53 asm volatile("mrc p15, 0, %0, c9, c13, 0"
54 : "=r"(per_cpu(previous_ccnt, cpu)));
55 }
56
57 for (i = 0; i < NUM_L1_CTRS; i++) {
58 if (cnten_val & (1 << i)) {
59 /* Select */
60 asm volatile("mcr p15, 0, %0, c9, c12, 5"
61 : : "r"(i));
62 /* Read value */
63 asm volatile("mrc p15, 0, %0, c9, c13, 2"
64 : "=r"(per_cpu(previous_l1_cnts[i], cpu)));
65 }
66 }
67 /* Enable all the counters that were disabled */
68 asm volatile("mcr p15, 0, %0, c9, c12, 1" : : "r"(cnten_val));
69}
70
Ashwin Chaugule4eaee1a2013-03-14 18:37:49 -040071static int tracectr_notifier(struct notifier_block *self, unsigned long cmd,
72 void *v)
73{
Ashwin Chaugule4eaee1a2013-03-14 18:37:49 -040074 struct thread_info *thread = v;
75 int current_pid;
Sheetal Sahasrabudhefa003452014-01-08 16:50:42 -050076 u32 cpu = thread->cpu;
Ashwin Chaugule4eaee1a2013-03-14 18:37:49 -040077
78 if (cmd != THREAD_NOTIFY_SWITCH)
Sheetal Sahasrabudhe0b9db082013-11-20 15:04:04 -050079 return -EFAULT;
Ashwin Chaugule4eaee1a2013-03-14 18:37:49 -040080
81 current_pid = thread->task->pid;
Sheetal Sahasrabudhe954248e2014-02-07 12:21:17 -050082 if (per_cpu(old_pid, cpu) != -1) {
83 if (per_cpu(hotplug_flag, cpu) == 1) {
84 per_cpu(hotplug_flag, cpu) = 0;
85 setup_prev_cnts(cpu);
86 } else
87 trace_sched_switch_with_ctrs(per_cpu(old_pid, cpu),
88 current_pid);
89 }
Sheetal Sahasrabudhe0b9db082013-11-20 15:04:04 -050090 per_cpu(old_pid, cpu) = current_pid;
91 return NOTIFY_OK;
Ashwin Chaugule4eaee1a2013-03-14 18:37:49 -040092}
93
94static struct notifier_block tracectr_notifier_block = {
95 .notifier_call = tracectr_notifier,
96};
97
Sheetal Sahasrabudhe711300e2013-07-10 12:18:59 -040098static void enable_tp_pid(void)
99{
100 if (tp_pid_state == 0) {
101 tp_pid_state = 1;
102 thread_register_notifier(&tracectr_notifier_block);
103 }
104}
105
106static void disable_tp_pid(void)
107{
108 if (tp_pid_state == 1) {
109 tp_pid_state = 0;
110 thread_unregister_notifier(&tracectr_notifier_block);
111 }
112}
113
114static ssize_t read_enabled_perftp_file_bool(struct file *file,
115 char __user *user_buf, size_t count, loff_t *ppos)
116{
117 char buf[2];
118 buf[1] = '\n';
119 if (tp_pid_state == 0)
120 buf[0] = '0';
121 else
122 buf[0] = '1';
123 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
124}
125
126static ssize_t write_enabled_perftp_file_bool(struct file *file,
127 const char __user *user_buf, size_t count, loff_t *ppos)
128{
129 char buf[32];
130 size_t buf_size;
131
132 buf_size = min(count, (sizeof(buf)-1));
133 if (copy_from_user(buf, user_buf, buf_size))
134 return -EFAULT;
135 switch (buf[0]) {
136 case 'y':
137 case 'Y':
138 case '1':
139 enable_tp_pid();
140 break;
141 case 'n':
142 case 'N':
143 case '0':
144 disable_tp_pid();
145 break;
146 }
147
148 return count;
149}
150
151static const struct file_operations fops_perftp = {
152 .read = read_enabled_perftp_file_bool,
153 .write = write_enabled_perftp_file_bool,
154 .llseek = default_llseek,
155};
156
Ashwin Chaugule4eaee1a2013-03-14 18:37:49 -0400157int __init init_tracecounters(void)
158{
Sheetal Sahasrabudhe711300e2013-07-10 12:18:59 -0400159 struct dentry *dir;
160 struct dentry *file;
161 unsigned int value = 1;
Sheetal Sahasrabudhe0b9db082013-11-20 15:04:04 -0500162 int cpu;
Sheetal Sahasrabudhe711300e2013-07-10 12:18:59 -0400163
164 dir = debugfs_create_dir("perf_debug_tp", NULL);
165 if (!dir)
166 return -ENOMEM;
Sheetal Sahasrabudhebf3e69b2013-08-12 13:53:59 -0400167 file = debugfs_create_file("enabled", 0660, dir,
Sheetal Sahasrabudhe711300e2013-07-10 12:18:59 -0400168 &value, &fops_perftp);
169 if (!file) {
170 debugfs_remove(dir);
171 return -ENOMEM;
172 }
Sheetal Sahasrabudhee2ec9802013-10-15 14:48:52 -0400173 register_cpu_notifier(&tracectr_cpu_hotplug_notifier_block);
Sheetal Sahasrabudhe0b9db082013-11-20 15:04:04 -0500174 for_each_possible_cpu(cpu)
175 per_cpu(old_pid, cpu) = -1;
Sheetal Sahasrabudhee2ec9802013-10-15 14:48:52 -0400176 return 0;
177}
178
179int __exit exit_tracecounters(void)
180{
181 unregister_cpu_notifier(&tracectr_cpu_hotplug_notifier_block);
Ashwin Chaugule4eaee1a2013-03-14 18:37:49 -0400182 return 0;
183}
184late_initcall(init_tracecounters);