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