Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @file common.c |
| 3 | * |
| 4 | * @remark Copyright 2004 Oprofile Authors |
| 5 | * @remark Read the file COPYING |
| 6 | * |
| 7 | * @author Zwane Mwaikambo |
| 8 | */ |
| 9 | |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/oprofile.h> |
| 12 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/sysdev.h> |
Russell King | 93ad794 | 2006-03-16 11:38:16 +0000 | [diff] [blame^] | 14 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | #include "op_counter.h" |
| 17 | #include "op_arm_model.h" |
| 18 | |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 19 | static struct op_arm_model_spec *op_arm_model; |
| 20 | static int op_arm_enabled; |
Russell King | 93ad794 | 2006-03-16 11:38:16 +0000 | [diff] [blame^] | 21 | static DEFINE_MUTEX(op_arm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | struct op_counter_config counter_config[OP_MAX_COUNTER]; |
| 24 | |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 25 | static int op_arm_create_files(struct super_block *sb, struct dentry *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | { |
| 27 | unsigned int i; |
| 28 | |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 29 | for (i = 0; i < op_arm_model->num_counters; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | struct dentry *dir; |
| 31 | char buf[2]; |
| 32 | |
| 33 | snprintf(buf, sizeof buf, "%d", i); |
| 34 | dir = oprofilefs_mkdir(sb, root, buf); |
| 35 | oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled); |
| 36 | oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event); |
| 37 | oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count); |
| 38 | oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask); |
| 39 | oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel); |
| 40 | oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user); |
| 41 | } |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 46 | static int op_arm_setup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { |
| 48 | int ret; |
| 49 | |
| 50 | spin_lock(&oprofilefs_lock); |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 51 | ret = op_arm_model->setup_ctrs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | spin_unlock(&oprofilefs_lock); |
| 53 | return ret; |
| 54 | } |
| 55 | |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 56 | static int op_arm_start(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
| 58 | int ret = -EBUSY; |
| 59 | |
Russell King | 93ad794 | 2006-03-16 11:38:16 +0000 | [diff] [blame^] | 60 | mutex_lock(&op_arm_mutex); |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 61 | if (!op_arm_enabled) { |
| 62 | ret = op_arm_model->start(); |
| 63 | op_arm_enabled = !ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | } |
Russell King | 93ad794 | 2006-03-16 11:38:16 +0000 | [diff] [blame^] | 65 | mutex_unlock(&op_arm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | return ret; |
| 67 | } |
| 68 | |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 69 | static void op_arm_stop(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { |
Russell King | 93ad794 | 2006-03-16 11:38:16 +0000 | [diff] [blame^] | 71 | mutex_lock(&op_arm_mutex); |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 72 | if (op_arm_enabled) |
| 73 | op_arm_model->stop(); |
| 74 | op_arm_enabled = 0; |
Russell King | 93ad794 | 2006-03-16 11:38:16 +0000 | [diff] [blame^] | 75 | mutex_unlock(&op_arm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Russell King | b5893c5 | 2005-10-28 14:51:15 +0100 | [diff] [blame] | 78 | #ifdef CONFIG_PM |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 79 | static int op_arm_suspend(struct sys_device *dev, pm_message_t state) |
Russell King | b5893c5 | 2005-10-28 14:51:15 +0100 | [diff] [blame] | 80 | { |
Russell King | 93ad794 | 2006-03-16 11:38:16 +0000 | [diff] [blame^] | 81 | mutex_lock(&op_arm_mutex); |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 82 | if (op_arm_enabled) |
| 83 | op_arm_model->stop(); |
Russell King | 93ad794 | 2006-03-16 11:38:16 +0000 | [diff] [blame^] | 84 | mutex_unlock(&op_arm_mutex); |
Russell King | b5893c5 | 2005-10-28 14:51:15 +0100 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 88 | static int op_arm_resume(struct sys_device *dev) |
Russell King | b5893c5 | 2005-10-28 14:51:15 +0100 | [diff] [blame] | 89 | { |
Russell King | 93ad794 | 2006-03-16 11:38:16 +0000 | [diff] [blame^] | 90 | mutex_lock(&op_arm_mutex); |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 91 | if (op_arm_enabled && op_arm_model->start()) |
| 92 | op_arm_enabled = 0; |
Russell King | 93ad794 | 2006-03-16 11:38:16 +0000 | [diff] [blame^] | 93 | mutex_unlock(&op_arm_mutex); |
Russell King | b5893c5 | 2005-10-28 14:51:15 +0100 | [diff] [blame] | 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static struct sysdev_class oprofile_sysclass = { |
| 98 | set_kset_name("oprofile"), |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 99 | .resume = op_arm_resume, |
| 100 | .suspend = op_arm_suspend, |
Russell King | b5893c5 | 2005-10-28 14:51:15 +0100 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | static struct sys_device device_oprofile = { |
| 104 | .id = 0, |
| 105 | .cls = &oprofile_sysclass, |
| 106 | }; |
| 107 | |
| 108 | static int __init init_driverfs(void) |
| 109 | { |
| 110 | int ret; |
| 111 | |
| 112 | if (!(ret = sysdev_class_register(&oprofile_sysclass))) |
| 113 | ret = sysdev_register(&device_oprofile); |
| 114 | |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | static void exit_driverfs(void) |
| 119 | { |
| 120 | sysdev_unregister(&device_oprofile); |
| 121 | sysdev_class_unregister(&oprofile_sysclass); |
| 122 | } |
| 123 | #else |
| 124 | #define init_driverfs() do { } while (0) |
| 125 | #define exit_driverfs() do { } while (0) |
| 126 | #endif /* CONFIG_PM */ |
| 127 | |
Russell King | c6b9daf | 2005-10-28 14:56:04 +0100 | [diff] [blame] | 128 | int __init oprofile_arch_init(struct oprofile_operations *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
Russell King | c6b9daf | 2005-10-28 14:56:04 +0100 | [diff] [blame] | 130 | struct op_arm_model_spec *spec = NULL; |
| 131 | int ret = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Russell King | c6b9daf | 2005-10-28 14:56:04 +0100 | [diff] [blame] | 133 | #ifdef CONFIG_CPU_XSCALE |
| 134 | spec = &op_xscale_spec; |
| 135 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Russell King | c6b9daf | 2005-10-28 14:56:04 +0100 | [diff] [blame] | 137 | if (spec) { |
Russ Dill | 7610dfa | 2006-02-01 21:07:28 +0000 | [diff] [blame] | 138 | ret = spec->init(); |
| 139 | if (ret < 0) |
| 140 | return ret; |
Russell King | c6b9daf | 2005-10-28 14:56:04 +0100 | [diff] [blame] | 141 | |
| 142 | op_arm_model = spec; |
| 143 | init_driverfs(); |
| 144 | ops->create_files = op_arm_create_files; |
| 145 | ops->setup = op_arm_setup; |
| 146 | ops->shutdown = op_arm_stop; |
| 147 | ops->start = op_arm_start; |
| 148 | ops->stop = op_arm_stop; |
| 149 | ops->cpu_type = op_arm_model->name; |
| 150 | ops->backtrace = arm_backtrace; |
| 151 | printk(KERN_INFO "oprofile: using %s\n", spec->name); |
| 152 | } |
| 153 | |
| 154 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Russell King | c6b9daf | 2005-10-28 14:56:04 +0100 | [diff] [blame] | 157 | void oprofile_arch_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 159 | if (op_arm_model) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | exit_driverfs(); |
Russell King | 55f0523 | 2005-10-28 14:54:21 +0100 | [diff] [blame] | 161 | op_arm_model = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
| 163 | } |