nmi_int.c 10.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/**
 * @file nmi_int.c
 *
4
 * @remark Copyright 2002-2008 OProfile authors
L
Linus Torvalds 已提交
5 6 7
 * @remark Read the file COPYING
 *
 * @author John Levon <levon@movementarian.org>
8
 * @author Robert Richter <robert.richter@amd.com>
L
Linus Torvalds 已提交
9 10 11 12 13 14 15 16
 */

#include <linux/init.h>
#include <linux/notifier.h>
#include <linux/smp.h>
#include <linux/oprofile.h>
#include <linux/sysdev.h>
#include <linux/slab.h>
17
#include <linux/moduleparam.h>
18
#include <linux/kdebug.h>
L
Linus Torvalds 已提交
19 20 21
#include <asm/nmi.h>
#include <asm/msr.h>
#include <asm/apic.h>
22

L
Linus Torvalds 已提交
23 24
#include "op_counter.h"
#include "op_x86_model.h"
25

26
static struct op_x86_model_spec const *model;
27 28
static DEFINE_PER_CPU(struct op_msrs, cpu_msrs);
static DEFINE_PER_CPU(unsigned long, saved_lvtpc);
29

L
Linus Torvalds 已提交
30 31 32 33 34 35 36 37
static int nmi_start(void);
static void nmi_stop(void);

/* 0 == registered but off, 1 == registered and on */
static int nmi_enabled = 0;

#ifdef CONFIG_PM

38
static int nmi_suspend(struct sys_device *dev, pm_message_t state)
L
Linus Torvalds 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52
{
	if (nmi_enabled == 1)
		nmi_stop();
	return 0;
}

static int nmi_resume(struct sys_device *dev)
{
	if (nmi_enabled == 1)
		nmi_start();
	return 0;
}

static struct sysdev_class oprofile_sysclass = {
53
	.name		= "oprofile",
L
Linus Torvalds 已提交
54 55 56 57 58 59 60 61 62
	.resume		= nmi_resume,
	.suspend	= nmi_suspend,
};

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

63
static int __init init_sysfs(void)
L
Linus Torvalds 已提交
64 65
{
	int error;
66 67 68

	error = sysdev_class_register(&oprofile_sysclass);
	if (!error)
L
Linus Torvalds 已提交
69 70 71 72
		error = sysdev_register(&device_oprofile);
	return error;
}

73
static void exit_sysfs(void)
L
Linus Torvalds 已提交
74 75 76 77 78 79
{
	sysdev_unregister(&device_oprofile);
	sysdev_class_unregister(&oprofile_sysclass);
}

#else
80 81
#define init_sysfs() do { } while (0)
#define exit_sysfs() do { } while (0)
L
Linus Torvalds 已提交
82 83
#endif /* CONFIG_PM */

84 85
static int profile_exceptions_notify(struct notifier_block *self,
				     unsigned long val, void *data)
L
Linus Torvalds 已提交
86
{
87 88 89 90
	struct die_args *args = (struct die_args *)data;
	int ret = NOTIFY_DONE;
	int cpu = smp_processor_id();

91
	switch (val) {
92
	case DIE_NMI:
93
		if (model->check_ctrs(args->regs, &per_cpu(cpu_msrs, cpu)))
94 95 96 97 98 99
			ret = NOTIFY_STOP;
		break;
	default:
		break;
	}
	return ret;
L
Linus Torvalds 已提交
100
}
101

102
static void nmi_cpu_save_registers(struct op_msrs *msrs)
L
Linus Torvalds 已提交
103 104
{
	unsigned int const nr_ctrs = model->num_counters;
105 106 107
	unsigned int const nr_ctrls = model->num_controls;
	struct op_msr *counters = msrs->counters;
	struct op_msr *controls = msrs->controls;
L
Linus Torvalds 已提交
108 109 110
	unsigned int i;

	for (i = 0; i < nr_ctrs; ++i) {
111
		if (counters[i].addr) {
112 113 114 115
			rdmsr(counters[i].addr,
				counters[i].saved.low,
				counters[i].saved.high);
		}
L
Linus Torvalds 已提交
116
	}
117

L
Linus Torvalds 已提交
118
	for (i = 0; i < nr_ctrls; ++i) {
119
		if (controls[i].addr) {
120 121 122 123
			rdmsr(controls[i].addr,
				controls[i].saved.low,
				controls[i].saved.high);
		}
L
Linus Torvalds 已提交
124 125 126
	}
}

127
static void nmi_save_registers(void *dummy)
L
Linus Torvalds 已提交
128 129
{
	int cpu = smp_processor_id();
130
	struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
L
Linus Torvalds 已提交
131 132 133 134 135 136
	nmi_cpu_save_registers(msrs);
}

static void free_msrs(void)
{
	int i;
137
	for_each_possible_cpu(i) {
138 139 140 141
		kfree(per_cpu(cpu_msrs, i).counters);
		per_cpu(cpu_msrs, i).counters = NULL;
		kfree(per_cpu(cpu_msrs, i).controls);
		per_cpu(cpu_msrs, i).controls = NULL;
L
Linus Torvalds 已提交
142 143 144 145 146 147 148 149 150 151
	}
}

static int allocate_msrs(void)
{
	int success = 1;
	size_t controls_size = sizeof(struct op_msr) * model->num_controls;
	size_t counters_size = sizeof(struct op_msr) * model->num_counters;

	int i;
C
Chris Wright 已提交
152
	for_each_possible_cpu(i) {
153 154 155
		per_cpu(cpu_msrs, i).counters = kmalloc(counters_size,
								GFP_KERNEL);
		if (!per_cpu(cpu_msrs, i).counters) {
L
Linus Torvalds 已提交
156 157 158
			success = 0;
			break;
		}
159 160 161
		per_cpu(cpu_msrs, i).controls = kmalloc(controls_size,
								GFP_KERNEL);
		if (!per_cpu(cpu_msrs, i).controls) {
L
Linus Torvalds 已提交
162 163 164 165 166 167 168 169 170 171 172
			success = 0;
			break;
		}
	}

	if (!success)
		free_msrs();

	return success;
}

173
static void nmi_cpu_setup(void *dummy)
L
Linus Torvalds 已提交
174 175
{
	int cpu = smp_processor_id();
176
	struct op_msrs *msrs = &per_cpu(cpu_msrs, cpu);
L
Linus Torvalds 已提交
177 178 179
	spin_lock(&oprofilefs_lock);
	model->setup_ctrs(msrs);
	spin_unlock(&oprofilefs_lock);
180
	per_cpu(saved_lvtpc, cpu) = apic_read(APIC_LVTPC);
L
Linus Torvalds 已提交
181 182 183
	apic_write(APIC_LVTPC, APIC_DM_NMI);
}

184 185 186 187 188
static struct notifier_block profile_exceptions_nb = {
	.notifier_call = profile_exceptions_notify,
	.next = NULL,
	.priority = 0
};
L
Linus Torvalds 已提交
189 190 191

static int nmi_setup(void)
{
192
	int err = 0;
193
	int cpu;
194

L
Linus Torvalds 已提交
195 196 197
	if (!allocate_msrs())
		return -ENOMEM;

198 199
	err = register_die_notifier(&profile_exceptions_nb);
	if (err) {
L
Linus Torvalds 已提交
200
		free_msrs();
201
		return err;
L
Linus Torvalds 已提交
202
	}
203

L
Linus Torvalds 已提交
204 205 206
	/* We need to serialize save and setup for HT because the subset
	 * of msrs are distinct for save and setup operations
	 */
207 208

	/* Assume saved/restored counters are the same on all CPUs */
209
	model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
210
	for_each_possible_cpu(cpu) {
C
Chris Wright 已提交
211
		if (cpu != 0) {
212 213
			memcpy(per_cpu(cpu_msrs, cpu).counters,
				per_cpu(cpu_msrs, 0).counters,
C
Chris Wright 已提交
214 215
				sizeof(struct op_msr) * model->num_counters);

216 217
			memcpy(per_cpu(cpu_msrs, cpu).controls,
				per_cpu(cpu_msrs, 0).controls,
C
Chris Wright 已提交
218 219 220
				sizeof(struct op_msr) * model->num_controls);
		}

221
	}
222 223
	on_each_cpu(nmi_save_registers, NULL, 1);
	on_each_cpu(nmi_cpu_setup, NULL, 1);
L
Linus Torvalds 已提交
224 225 226 227
	nmi_enabled = 1;
	return 0;
}

228
static void nmi_restore_registers(struct op_msrs *msrs)
L
Linus Torvalds 已提交
229 230
{
	unsigned int const nr_ctrs = model->num_counters;
231 232 233
	unsigned int const nr_ctrls = model->num_controls;
	struct op_msr *counters = msrs->counters;
	struct op_msr *controls = msrs->controls;
L
Linus Torvalds 已提交
234 235 236
	unsigned int i;

	for (i = 0; i < nr_ctrls; ++i) {
237
		if (controls[i].addr) {
238 239 240 241
			wrmsr(controls[i].addr,
				controls[i].saved.low,
				controls[i].saved.high);
		}
L
Linus Torvalds 已提交
242
	}
243

L
Linus Torvalds 已提交
244
	for (i = 0; i < nr_ctrs; ++i) {
245
		if (counters[i].addr) {
246 247 248 249
			wrmsr(counters[i].addr,
				counters[i].saved.low,
				counters[i].saved.high);
		}
L
Linus Torvalds 已提交
250 251 252
	}
}

253
static void nmi_cpu_shutdown(void *dummy)
L
Linus Torvalds 已提交
254 255 256
{
	unsigned int v;
	int cpu = smp_processor_id();
257
	struct op_msrs *msrs = &__get_cpu_var(cpu_msrs);
258

L
Linus Torvalds 已提交
259 260 261 262 263 264 265
	/* restoring APIC_LVTPC can trigger an apic error because the delivery
	 * mode and vector nr combination can be illegal. That's by design: on
	 * power on apic lvt contain a zero vector nr which are legal only for
	 * NMI delivery mode. So inhibit apic err before restoring lvtpc
	 */
	v = apic_read(APIC_LVTERR);
	apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
266
	apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
L
Linus Torvalds 已提交
267 268 269 270 271 272
	apic_write(APIC_LVTERR, v);
	nmi_restore_registers(msrs);
}

static void nmi_shutdown(void)
{
273
	struct op_msrs *msrs = &get_cpu_var(cpu_msrs);
L
Linus Torvalds 已提交
274
	nmi_enabled = 0;
275
	on_each_cpu(nmi_cpu_shutdown, NULL, 1);
276
	unregister_die_notifier(&profile_exceptions_nb);
277
	model->shutdown(msrs);
L
Linus Torvalds 已提交
278
	free_msrs();
279
	put_cpu_var(cpu_msrs);
L
Linus Torvalds 已提交
280 281
}

282
static void nmi_cpu_start(void *dummy)
L
Linus Torvalds 已提交
283
{
284
	struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
L
Linus Torvalds 已提交
285 286 287 288 289
	model->start(msrs);
}

static int nmi_start(void)
{
290
	on_each_cpu(nmi_cpu_start, NULL, 1);
L
Linus Torvalds 已提交
291 292
	return 0;
}
293 294

static void nmi_cpu_stop(void *dummy)
L
Linus Torvalds 已提交
295
{
296
	struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
L
Linus Torvalds 已提交
297 298
	model->stop(msrs);
}
299

L
Linus Torvalds 已提交
300 301
static void nmi_stop(void)
{
302
	on_each_cpu(nmi_cpu_stop, NULL, 1);
L
Linus Torvalds 已提交
303 304 305 306
}

struct op_counter_config counter_config[OP_MAX_COUNTER];

307
static int nmi_create_files(struct super_block *sb, struct dentry *root)
L
Linus Torvalds 已提交
308 309 310 311
{
	unsigned int i;

	for (i = 0; i < model->num_counters; ++i) {
312
		struct dentry *dir;
313
		char buf[4];
314 315

		/* quick little hack to _not_ expose a counter if it is not
316 317 318 319 320 321 322
		 * available for use.  This should protect userspace app.
		 * NOTE:  assumes 1:1 mapping here (that counters are organized
		 *        sequentially in their struct assignment).
		 */
		if (unlikely(!avail_to_resrv_perfctr_nmi_bit(i)))
			continue;

323
		snprintf(buf,  sizeof(buf), "%d", i);
L
Linus Torvalds 已提交
324
		dir = oprofilefs_mkdir(sb, root, buf);
325 326 327 328 329 330
		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);
L
Linus Torvalds 已提交
331 332 333 334
	}

	return 0;
}
335

336 337
static int p4force;
module_param(p4force, int, 0);
338 339

static int __init p4_init(char **cpu_type)
L
Linus Torvalds 已提交
340 341 342
{
	__u8 cpu_model = boot_cpu_data.x86_model;

343
	if (!p4force && (cpu_model > 6 || cpu_model == 5))
L
Linus Torvalds 已提交
344 345 346 347 348 349 350 351
		return 0;

#ifndef CONFIG_SMP
	*cpu_type = "i386/p4";
	model = &op_p4_spec;
	return 1;
#else
	switch (smp_num_siblings) {
352 353 354 355 356 357 358 359 360
	case 1:
		*cpu_type = "i386/p4";
		model = &op_p4_spec;
		return 1;

	case 2:
		*cpu_type = "i386/p4-ht";
		model = &op_p4_ht2_spec;
		return 1;
L
Linus Torvalds 已提交
361 362 363 364 365 366 367 368
	}
#endif

	printk(KERN_INFO "oprofile: P4 HyperThreading detected with > 2 threads\n");
	printk(KERN_INFO "oprofile: Reverting to timer mode.\n");
	return 0;
}

369
static int __init ppro_init(char **cpu_type)
L
Linus Torvalds 已提交
370 371 372
{
	__u8 cpu_model = boot_cpu_data.x86_model;

373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
	switch (cpu_model) {
	case 0 ... 2:
		*cpu_type = "i386/ppro";
		break;
	case 3 ... 5:
		*cpu_type = "i386/pii";
		break;
	case 6 ... 8:
		*cpu_type = "i386/piii";
		break;
	case 9:
		*cpu_type = "i386/p6_mobile";
		break;
	case 10 ... 13:
		*cpu_type = "i386/p6";
		break;
	case 14:
390
		*cpu_type = "i386/core";
391 392 393 394 395
		break;
	case 15: case 23:
		*cpu_type = "i386/core_2";
		break;
	case 26:
396
		*cpu_type = "i386/core_2";
397 398 399
		break;
	default:
		/* Unknown */
L
Linus Torvalds 已提交
400 401 402 403 404 405 406
		return 0;
	}

	model = &op_ppro_spec;
	return 1;
}

407
/* in order to get sysfs right */
L
Linus Torvalds 已提交
408 409
static int using_nmi;

410
int __init op_nmi_init(struct oprofile_operations *ops)
L
Linus Torvalds 已提交
411 412 413 414
{
	__u8 vendor = boot_cpu_data.x86_vendor;
	__u8 family = boot_cpu_data.x86;
	char *cpu_type;
415
	int ret = 0;
L
Linus Torvalds 已提交
416 417 418

	if (!cpu_has_apic)
		return -ENODEV;
419

L
Linus Torvalds 已提交
420
	switch (vendor) {
421 422
	case X86_VENDOR_AMD:
		/* Needs to be at least an Athlon (or hammer in 32bit mode) */
L
Linus Torvalds 已提交
423

424 425 426 427
		switch (family) {
		default:
			return -ENODEV;
		case 6:
428
			model = &op_amd_spec;
429 430 431
			cpu_type = "i386/athlon";
			break;
		case 0xf:
432
			model = &op_amd_spec;
433 434 435 436 437
			/* Actually it could be i386/hammer too, but give
			 user space an consistent name. */
			cpu_type = "x86-64/hammer";
			break;
		case 0x10:
438
			model = &op_amd_spec;
439 440
			cpu_type = "x86-64/family10";
			break;
441
		case 0x11:
442
			model = &op_amd_spec;
443 444
			cpu_type = "x86-64/family11h";
			break;
445 446 447 448 449 450 451 452
		}
		break;

	case X86_VENDOR_INTEL:
		switch (family) {
			/* Pentium IV */
		case 0xf:
			if (!p4_init(&cpu_type))
L
Linus Torvalds 已提交
453 454
				return -ENODEV;
			break;
455 456 457 458 459

			/* A P6-class processor */
		case 6:
			if (!ppro_init(&cpu_type))
				return -ENODEV;
L
Linus Torvalds 已提交
460 461 462 463
			break;

		default:
			return -ENODEV;
464 465 466 467 468
		}
		break;

	default:
		return -ENODEV;
L
Linus Torvalds 已提交
469 470
	}

471 472 473 474 475
	if (model->init)
		ret = model->init(ops);
	if (ret)
		return ret;

476
	init_sysfs();
L
Linus Torvalds 已提交
477 478 479 480 481 482 483 484 485 486 487
	using_nmi = 1;
	ops->create_files = nmi_create_files;
	ops->setup = nmi_setup;
	ops->shutdown = nmi_shutdown;
	ops->start = nmi_start;
	ops->stop = nmi_stop;
	ops->cpu_type = cpu_type;
	printk(KERN_INFO "oprofile: using NMI interrupt.\n");
	return 0;
}

488
void op_nmi_exit(void)
L
Linus Torvalds 已提交
489 490
{
	if (using_nmi)
491
		exit_sysfs();
492 493
	if (model->exit)
		model->exit();
L
Linus Torvalds 已提交
494
}