init.c 12.1 KB
Newer Older
1
/*
L
Linus Torvalds 已提交
2
 * S390 Version
3
 *   Copyright IBM Corp. 2002, 2011
L
Linus Torvalds 已提交
4
 *   Author(s): Thomas Spatzier (tspat@de.ibm.com)
5 6
 *   Author(s): Mahesh Salgaonkar (mahesh@linux.vnet.ibm.com)
 *   Author(s): Heinz Graalfs (graalfs@linux.vnet.ibm.com)
7
 *   Author(s): Andreas Krebbel (krebbel@linux.vnet.ibm.com)
L
Linus Torvalds 已提交
8
 *
9
 * @remark Copyright 2002-2011 OProfile authors
L
Linus Torvalds 已提交
10 11 12
 */

#include <linux/oprofile.h>
13
#include <linux/perf_event.h>
L
Linus Torvalds 已提交
14 15
#include <linux/init.h>
#include <linux/errno.h>
16
#include <linux/fs.h>
17 18
#include <linux/module.h>
#include <asm/processor.h>
S
Sebastian Ott 已提交
19
#include <asm/perf_event.h>
20 21

#include "../../../drivers/oprofile/oprof.h"
22

23
#include "hwsampler.h"
24
#include "op_counter.h"
25

26
#define DEFAULT_INTERVAL	4127518
27 28 29 30 31 32 33 34 35 36

#define DEFAULT_SDBT_BLOCKS	1
#define DEFAULT_SDB_BLOCKS	511

static unsigned long oprofile_hw_interval = DEFAULT_INTERVAL;
static unsigned long oprofile_min_interval;
static unsigned long oprofile_max_interval;

static unsigned long oprofile_sdbt_blocks = DEFAULT_SDBT_BLOCKS;
static unsigned long oprofile_sdb_blocks = DEFAULT_SDB_BLOCKS;
L
Linus Torvalds 已提交
37

38
static int hwsampler_enabled;
39
static int hwsampler_running;	/* start_mutex must be held to change */
40
static int hwsampler_available;
41 42

static struct oprofile_operations timer_ops;
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
struct op_counter_config counter_config;

enum __force_cpu_type {
	reserved = 0,		/* do not force */
	timer,
};
static int force_cpu_type;

static int set_cpu_type(const char *str, struct kernel_param *kp)
{
	if (!strcmp(str, "timer")) {
		force_cpu_type = timer;
		printk(KERN_INFO "oprofile: forcing timer to be returned "
		                 "as cpu type\n");
	} else {
		force_cpu_type = 0;
	}

	return 0;
}
module_param_call(cpu_type, set_cpu_type, NULL, NULL, 0);
MODULE_PARM_DESC(cpu_type, "Force legacy basic mode sampling"
		           "(report cpu_type \"timer\"");

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
static int __oprofile_hwsampler_start(void)
{
	int retval;

	retval = hwsampler_allocate(oprofile_sdbt_blocks, oprofile_sdb_blocks);
	if (retval)
		return retval;

	retval = hwsampler_start_all(oprofile_hw_interval);
	if (retval)
		hwsampler_deallocate();

	return retval;
}

83 84 85 86
static int oprofile_hwsampler_start(void)
{
	int retval;

87
	hwsampler_running = hwsampler_enabled;
88 89 90 91

	if (!hwsampler_running)
		return timer_ops.start();

92
	retval = perf_reserve_sampling();
93 94 95
	if (retval)
		return retval;

96
	retval = __oprofile_hwsampler_start();
97
	if (retval)
98
		perf_release_sampling();
99 100 101 102 103 104 105 106 107 108 109 110 111

	return retval;
}

static void oprofile_hwsampler_stop(void)
{
	if (!hwsampler_running) {
		timer_ops.stop();
		return;
	}

	hwsampler_stop_all();
	hwsampler_deallocate();
112
	perf_release_sampling();
113 114 115
	return;
}

116 117 118 119 120 121
/*
 * File ops used for:
 * /dev/oprofile/0/enabled
 * /dev/oprofile/hwsampling/hwsampler  (cpu_type = timer)
 */

122 123 124
static ssize_t hwsampler_read(struct file *file, char __user *buf,
		size_t count, loff_t *offset)
{
125
	return oprofilefs_ulong_to_user(hwsampler_enabled, buf, count, offset);
126 127 128 129 130 131 132 133 134 135 136 137
}

static ssize_t hwsampler_write(struct file *file, char const __user *buf,
		size_t count, loff_t *offset)
{
	unsigned long val;
	int retval;

	if (*offset)
		return -EINVAL;

	retval = oprofilefs_ulong_from_user(&val, buf, count);
138
	if (retval <= 0)
139 140
		return retval;

141 142 143
	if (val != 0 && val != 1)
		return -EINVAL;

144 145 146 147 148 149 150 151
	if (oprofile_started)
		/*
		 * save to do without locking as we set
		 * hwsampler_running in start() when start_mutex is
		 * held
		 */
		return -EBUSY;

152
	hwsampler_enabled = val;
153 154 155 156 157 158 159 160 161

	return count;
}

static const struct file_operations hwsampler_fops = {
	.read		= hwsampler_read,
	.write		= hwsampler_write,
};

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
/*
 * File ops used for:
 * /dev/oprofile/0/count
 * /dev/oprofile/hwsampling/hw_interval  (cpu_type = timer)
 *
 * Make sure that the value is within the hardware range.
 */

static ssize_t hw_interval_read(struct file *file, char __user *buf,
				size_t count, loff_t *offset)
{
	return oprofilefs_ulong_to_user(oprofile_hw_interval, buf,
					count, offset);
}

static ssize_t hw_interval_write(struct file *file, char const __user *buf,
				 size_t count, loff_t *offset)
{
	unsigned long val;
	int retval;

	if (*offset)
		return -EINVAL;
	retval = oprofilefs_ulong_from_user(&val, buf, count);
186
	if (retval <= 0)
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
		return retval;
	if (val < oprofile_min_interval)
		oprofile_hw_interval = oprofile_min_interval;
	else if (val > oprofile_max_interval)
		oprofile_hw_interval = oprofile_max_interval;
	else
		oprofile_hw_interval = val;

	return count;
}

static const struct file_operations hw_interval_fops = {
	.read		= hw_interval_read,
	.write		= hw_interval_write,
};

/*
 * File ops used for:
 * /dev/oprofile/0/event
 * Only a single event with number 0 is supported with this counter.
 *
 * /dev/oprofile/0/unit_mask
 * This is a dummy file needed by the user space tools.
 * No value other than 0 is accepted or returned.
 */

static ssize_t hwsampler_zero_read(struct file *file, char __user *buf,
				    size_t count, loff_t *offset)
{
	return oprofilefs_ulong_to_user(0, buf, count, offset);
}

static ssize_t hwsampler_zero_write(struct file *file, char const __user *buf,
				     size_t count, loff_t *offset)
{
	unsigned long val;
	int retval;

	if (*offset)
		return -EINVAL;

	retval = oprofilefs_ulong_from_user(&val, buf, count);
229
	if (retval <= 0)
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
		return retval;
	if (val != 0)
		return -EINVAL;
	return count;
}

static const struct file_operations zero_fops = {
	.read		= hwsampler_zero_read,
	.write		= hwsampler_zero_write,
};

/* /dev/oprofile/0/kernel file ops.  */

static ssize_t hwsampler_kernel_read(struct file *file, char __user *buf,
				     size_t count, loff_t *offset)
{
	return oprofilefs_ulong_to_user(counter_config.kernel,
					buf, count, offset);
}

static ssize_t hwsampler_kernel_write(struct file *file, char const __user *buf,
				      size_t count, loff_t *offset)
{
	unsigned long val;
	int retval;

	if (*offset)
		return -EINVAL;

	retval = oprofilefs_ulong_from_user(&val, buf, count);
260
	if (retval <= 0)
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
		return retval;

	if (val != 0 && val != 1)
		return -EINVAL;

	counter_config.kernel = val;

	return count;
}

static const struct file_operations kernel_fops = {
	.read		= hwsampler_kernel_read,
	.write		= hwsampler_kernel_write,
};

/* /dev/oprofile/0/user file ops. */

static ssize_t hwsampler_user_read(struct file *file, char __user *buf,
				   size_t count, loff_t *offset)
{
	return oprofilefs_ulong_to_user(counter_config.user,
					buf, count, offset);
}

static ssize_t hwsampler_user_write(struct file *file, char const __user *buf,
				    size_t count, loff_t *offset)
{
	unsigned long val;
	int retval;

	if (*offset)
		return -EINVAL;

	retval = oprofilefs_ulong_from_user(&val, buf, count);
295
	if (retval <= 0)
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
		return retval;

	if (val != 0 && val != 1)
		return -EINVAL;

	counter_config.user = val;

	return count;
}

static const struct file_operations user_fops = {
	.read		= hwsampler_user_read,
	.write		= hwsampler_user_write,
};


/*
 * File ops used for: /dev/oprofile/timer/enabled
 * The value always has to be the inverted value of hwsampler_enabled. So
 * no separate variable is created. That way we do not need locking.
 */

static ssize_t timer_enabled_read(struct file *file, char __user *buf,
				  size_t count, loff_t *offset)
{
	return oprofilefs_ulong_to_user(!hwsampler_enabled, buf, count, offset);
}

static ssize_t timer_enabled_write(struct file *file, char const __user *buf,
				   size_t count, loff_t *offset)
{
	unsigned long val;
	int retval;

	if (*offset)
		return -EINVAL;

	retval = oprofilefs_ulong_from_user(&val, buf, count);
334
	if (retval <= 0)
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
		return retval;

	if (val != 0 && val != 1)
		return -EINVAL;

	/* Timer cannot be disabled without having hardware sampling.  */
	if (val == 0 && !hwsampler_available)
		return -EINVAL;

	if (oprofile_started)
		/*
		 * save to do without locking as we set
		 * hwsampler_running in start() when start_mutex is
		 * held
		 */
		return -EBUSY;

	hwsampler_enabled = !val;

	return count;
}

static const struct file_operations timer_enabled_fops = {
	.read		= timer_enabled_read,
	.write		= timer_enabled_write,
};


363
static int oprofile_create_hwsampling_files(struct dentry *root)
364
{
365 366
	struct dentry *dir;

367
	dir = oprofilefs_mkdir(root, "timer");
368 369 370
	if (!dir)
		return -EINVAL;

371
	oprofilefs_create_file(dir, "enabled", &timer_enabled_fops);
372 373 374

	if (!hwsampler_available)
		return 0;
375 376

	/* reinitialize default values */
377 378 379
	hwsampler_enabled = 1;
	counter_config.kernel = 1;
	counter_config.user = 1;
380

381 382 383 384 385 386 387 388 389 390 391
	if (!force_cpu_type) {
		/*
		 * Create the counter file system.  A single virtual
		 * counter is created which can be used to
		 * enable/disable hardware sampling dynamically from
		 * user space.  The user space will configure a single
		 * counter with a single event.  The value of 'event'
		 * and 'unit_mask' are not evaluated by the kernel code
		 * and can only be set to 0.
		 */

392
		dir = oprofilefs_mkdir(root, "0");
393 394
		if (!dir)
			return -EINVAL;
395

396 397 398 399 400 401 402
		oprofilefs_create_file(dir, "enabled", &hwsampler_fops);
		oprofilefs_create_file(dir, "event", &zero_fops);
		oprofilefs_create_file(dir, "count", &hw_interval_fops);
		oprofilefs_create_file(dir, "unit_mask", &zero_fops);
		oprofilefs_create_file(dir, "kernel", &kernel_fops);
		oprofilefs_create_file(dir, "user", &user_fops);
		oprofilefs_create_ulong(dir, "hw_sdbt_blocks",
403
					&oprofile_sdbt_blocks);
404

405 406 407 408 409 410 411
	} else {
		/*
		 * Hardware sampling can be used but the cpu_type is
		 * forced to timer in order to deal with legacy user
		 * space tools.  The /dev/oprofile/hwsampling fs is
		 * provided in that case.
		 */
412
		dir = oprofilefs_mkdir(root, "hwsampling");
413 414 415
		if (!dir)
			return -EINVAL;

416
		oprofilefs_create_file(dir, "hwsampler",
417
				       &hwsampler_fops);
418
		oprofilefs_create_file(dir, "hw_interval",
419
				       &hw_interval_fops);
420
		oprofilefs_create_ro_ulong(dir, "hw_min_interval",
421
					   &oprofile_min_interval);
422
		oprofilefs_create_ro_ulong(dir, "hw_max_interval",
423
					   &oprofile_max_interval);
424
		oprofilefs_create_ulong(dir, "hw_sdbt_blocks",
425 426
					&oprofile_sdbt_blocks);
	}
427 428 429
	return 0;
}

R
Robert Richter 已提交
430
static int oprofile_hwsampler_init(struct oprofile_operations *ops)
431
{
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
	/*
	 * Initialize the timer mode infrastructure as well in order
	 * to be able to switch back dynamically.  oprofile_timer_init
	 * is not supposed to fail.
	 */
	if (oprofile_timer_init(ops))
		BUG();

	memcpy(&timer_ops, ops, sizeof(timer_ops));
	ops->create_files = oprofile_create_hwsampling_files;

	/*
	 * If the user space tools do not support newer cpu types,
	 * the force_cpu_type module parameter
	 * can be used to always return \"timer\" as cpu type.
	 */
	if (force_cpu_type != timer) {
		struct cpuid id;

		get_cpu_id (&id);

		switch (id.machine) {
		case 0x2097: case 0x2098: ops->cpu_type = "s390/z10"; break;
		case 0x2817: case 0x2818: ops->cpu_type = "s390/z196"; break;
456
		case 0x2827: case 0x2828: ops->cpu_type = "s390/zEC12"; break;
457
		case 0x2964: case 0x2965: ops->cpu_type = "s390/z13"; break;
458 459 460 461
		default: return -ENODEV;
		}
	}

462 463 464 465
	if (hwsampler_setup())
		return -ENODEV;

	/*
466 467
	 * Query the range for the sampling interval from the
	 * hardware.
468 469
	 */
	oprofile_min_interval = hwsampler_query_min_interval();
470
	if (oprofile_min_interval == 0)
471 472
		return -ENODEV;
	oprofile_max_interval = hwsampler_query_max_interval();
473
	if (oprofile_max_interval == 0)
474 475
		return -ENODEV;

476 477 478 479 480 481
	/* The initial value should be sane */
	if (oprofile_hw_interval < oprofile_min_interval)
		oprofile_hw_interval = oprofile_min_interval;
	if (oprofile_hw_interval > oprofile_max_interval)
		oprofile_hw_interval = oprofile_max_interval;

482 483
	printk(KERN_INFO "oprofile: System z hardware sampling "
	       "facility found.\n");
484 485 486 487 488 489 490

	ops->start = oprofile_hwsampler_start;
	ops->stop = oprofile_hwsampler_stop;

	return 0;
}

R
Robert Richter 已提交
491
static void oprofile_hwsampler_exit(void)
492 493 494 495
{
	hwsampler_shutdown();
}

496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
static int __s390_backtrace(void *data, unsigned long address)
{
	unsigned int *depth = data;

	if (*depth == 0)
		return 1;
	(*depth)--;
	oprofile_add_trace(address);
	return 0;
}

static void s390_backtrace(struct pt_regs *regs, unsigned int depth)
{
	if (user_mode(regs))
		return;
	dump_trace(__s390_backtrace, &depth, NULL, regs->gprs[15]);
}

R
Robert Richter 已提交
514
int __init oprofile_arch_init(struct oprofile_operations *ops)
L
Linus Torvalds 已提交
515
{
516
	ops->backtrace = s390_backtrace;
517

518 519 520 521 522 523 524 525
	/*
	 * -ENODEV is not reported to the caller.  The module itself
         * will use the timer mode sampling as fallback and this is
         * always available.
	 */
	hwsampler_available = oprofile_hwsampler_init(ops) == 0;

	return 0;
L
Linus Torvalds 已提交
526 527 528 529
}

void oprofile_arch_exit(void)
{
530
	oprofile_hwsampler_exit();
L
Linus Torvalds 已提交
531
}