common.c 3.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/**
 * @file common.c
 *
 * @remark Copyright 2004 Oprofile Authors
 * @remark Read the file COPYING
 *
 * @author Zwane Mwaikambo
 */

#include <linux/init.h>
#include <linux/oprofile.h>
#include <linux/errno.h>
13
#include <linux/slab.h>
L
Linus Torvalds 已提交
14
#include <linux/sysdev.h>
15
#include <linux/mutex.h>
L
Linus Torvalds 已提交
16 17 18 19

#include "op_counter.h"
#include "op_arm_model.h"

20 21
static struct op_arm_model_spec *op_arm_model;
static int op_arm_enabled;
22
static DEFINE_MUTEX(op_arm_mutex);
L
Linus Torvalds 已提交
23

24
struct op_counter_config *counter_config;
L
Linus Torvalds 已提交
25

26
static int op_arm_create_files(struct super_block *sb, struct dentry *root)
L
Linus Torvalds 已提交
27 28 29
{
	unsigned int i;

30
	for (i = 0; i < op_arm_model->num_counters; i++) {
L
Linus Torvalds 已提交
31
		struct dentry *dir;
32
		char buf[4];
L
Linus Torvalds 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46

		snprintf(buf, sizeof buf, "%d", i);
		dir = oprofilefs_mkdir(sb, root, buf);
		oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
		oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
		oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
		oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
		oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
		oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
	}

	return 0;
}

47
static int op_arm_setup(void)
L
Linus Torvalds 已提交
48 49 50 51
{
	int ret;

	spin_lock(&oprofilefs_lock);
52
	ret = op_arm_model->setup_ctrs();
L
Linus Torvalds 已提交
53 54 55 56
	spin_unlock(&oprofilefs_lock);
	return ret;
}

57
static int op_arm_start(void)
L
Linus Torvalds 已提交
58 59 60
{
	int ret = -EBUSY;

61
	mutex_lock(&op_arm_mutex);
62 63 64
	if (!op_arm_enabled) {
		ret = op_arm_model->start();
		op_arm_enabled = !ret;
L
Linus Torvalds 已提交
65
	}
66
	mutex_unlock(&op_arm_mutex);
L
Linus Torvalds 已提交
67 68 69
	return ret;
}

70
static void op_arm_stop(void)
L
Linus Torvalds 已提交
71
{
72
	mutex_lock(&op_arm_mutex);
73 74 75
	if (op_arm_enabled)
		op_arm_model->stop();
	op_arm_enabled = 0;
76
	mutex_unlock(&op_arm_mutex);
L
Linus Torvalds 已提交
77 78
}

79
#ifdef CONFIG_PM
80
static int op_arm_suspend(struct sys_device *dev, pm_message_t state)
81
{
82
	mutex_lock(&op_arm_mutex);
83 84
	if (op_arm_enabled)
		op_arm_model->stop();
85
	mutex_unlock(&op_arm_mutex);
86 87 88
	return 0;
}

89
static int op_arm_resume(struct sys_device *dev)
90
{
91
	mutex_lock(&op_arm_mutex);
92 93
	if (op_arm_enabled && op_arm_model->start())
		op_arm_enabled = 0;
94
	mutex_unlock(&op_arm_mutex);
95 96 97 98
	return 0;
}

static struct sysdev_class oprofile_sysclass = {
99
	.name		= "oprofile",
100 101
	.resume		= op_arm_resume,
	.suspend	= op_arm_suspend,
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
};

static struct sys_device device_oprofile = {
	.id		= 0,
	.cls		= &oprofile_sysclass,
};

static int __init init_driverfs(void)
{
	int ret;

	if (!(ret = sysdev_class_register(&oprofile_sysclass)))
		ret = sysdev_register(&device_oprofile);

	return ret;
}

static void  exit_driverfs(void)
{
	sysdev_unregister(&device_oprofile);
	sysdev_class_unregister(&oprofile_sysclass);
}
#else
#define init_driverfs()	do { } while (0)
#define exit_driverfs() do { } while (0)
#endif /* CONFIG_PM */

129
int __init oprofile_arch_init(struct oprofile_operations *ops)
L
Linus Torvalds 已提交
130
{
131 132 133
	struct op_arm_model_spec *spec = NULL;
	int ret = -ENODEV;

134 135
	ops->backtrace = arm_backtrace;

136 137 138 139
#ifdef CONFIG_CPU_XSCALE
	spec = &op_xscale_spec;
#endif

140 141 142 143
#ifdef CONFIG_OPROFILE_ARMV6
	spec = &op_armv6_spec;
#endif

144 145 146 147
#ifdef CONFIG_OPROFILE_MPCORE
	spec = &op_mpcore_spec;
#endif

148 149 150 151
#ifdef CONFIG_OPROFILE_ARMV7
	spec = &op_armv7_spec;
#endif

152
	if (spec) {
153 154 155
		ret = spec->init();
		if (ret < 0)
			return ret;
156

157
		counter_config = kcalloc(spec->num_counters, sizeof(struct op_counter_config),
158 159 160 161
					 GFP_KERNEL);
		if (!counter_config)
			return -ENOMEM;

162 163 164 165 166 167 168 169 170 171
		op_arm_model = spec;
		init_driverfs();
		ops->create_files = op_arm_create_files;
		ops->setup = op_arm_setup;
		ops->shutdown = op_arm_stop;
		ops->start = op_arm_start;
		ops->stop = op_arm_stop;
		ops->cpu_type = op_arm_model->name;
		printk(KERN_INFO "oprofile: using %s\n", spec->name);
	}
L
Linus Torvalds 已提交
172

173
	return ret;
L
Linus Torvalds 已提交
174 175
}

176
void oprofile_arch_exit(void)
L
Linus Torvalds 已提交
177
{
178
	if (op_arm_model) {
L
Linus Torvalds 已提交
179
		exit_driverfs();
180
		op_arm_model = NULL;
L
Linus Torvalds 已提交
181
	}
182
	kfree(counter_config);
L
Linus Torvalds 已提交
183
}