proc.c 11.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9
/*
 * linux/kernel/irq/proc.c
 *
 * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
 *
 * This file contains the /proc/irq/ handling code.
 */

#include <linux/irq.h>
10
#include <linux/gfp.h>
L
Linus Torvalds 已提交
11
#include <linux/proc_fs.h>
12
#include <linux/seq_file.h>
L
Linus Torvalds 已提交
13
#include <linux/interrupt.h>
14
#include <linux/kernel_stat.h>
L
Linus Torvalds 已提交
15

A
Adrian Bunk 已提交
16 17
#include "internals.h"

18
static struct proc_dir_entry *root_irq_dir;
L
Linus Torvalds 已提交
19 20 21

#ifdef CONFIG_SMP

22
static int show_irq_affinity(int type, struct seq_file *m, void *v)
L
Linus Torvalds 已提交
23
{
24
	struct irq_desc *desc = irq_to_desc((long)m->private);
25
	const struct cpumask *mask = desc->irq_data.affinity;
26 27

#ifdef CONFIG_GENERIC_PENDING_IRQ
28
	if (irqd_is_setaffinity_pending(&desc->irq_data))
29
		mask = desc->pending_mask;
30
#endif
31 32 33 34
	if (type)
		seq_cpumask_list(m, mask);
	else
		seq_cpumask(m, mask);
35 36
	seq_putc(m, '\n');
	return 0;
L
Linus Torvalds 已提交
37 38
}

39 40 41 42 43 44
static int irq_affinity_hint_proc_show(struct seq_file *m, void *v)
{
	struct irq_desc *desc = irq_to_desc((long)m->private);
	unsigned long flags;
	cpumask_var_t mask;

45
	if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
46 47 48 49 50 51 52 53 54 55 56 57 58 59
		return -ENOMEM;

	raw_spin_lock_irqsave(&desc->lock, flags);
	if (desc->affinity_hint)
		cpumask_copy(mask, desc->affinity_hint);
	raw_spin_unlock_irqrestore(&desc->lock, flags);

	seq_cpumask(m, mask);
	seq_putc(m, '\n');
	free_cpumask_var(mask);

	return 0;
}

60 61 62 63
#ifndef is_affinity_mask_valid
#define is_affinity_mask_valid(val) 1
#endif

L
Linus Torvalds 已提交
64
int no_irq_affinity;
65 66 67 68 69 70 71 72 73 74 75 76
static int irq_affinity_proc_show(struct seq_file *m, void *v)
{
	return show_irq_affinity(0, m, v);
}

static int irq_affinity_list_proc_show(struct seq_file *m, void *v)
{
	return show_irq_affinity(1, m, v);
}


static ssize_t write_irq_affinity(int type, struct file *file,
77
		const char __user *buffer, size_t count, loff_t *pos)
L
Linus Torvalds 已提交
78
{
A
Al Viro 已提交
79
	unsigned int irq = (int)(long)PDE_DATA(file_inode(file));
80
	cpumask_var_t new_value;
81
	int err;
L
Linus Torvalds 已提交
82

83
	if (!irq_can_set_affinity(irq) || no_irq_affinity)
L
Linus Torvalds 已提交
84 85
		return -EIO;

86 87 88
	if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
		return -ENOMEM;

89 90 91 92
	if (type)
		err = cpumask_parselist_user(buffer, count, new_value);
	else
		err = cpumask_parse_user(buffer, count, new_value);
L
Linus Torvalds 已提交
93
	if (err)
94
		goto free_cpumask;
L
Linus Torvalds 已提交
95

96
	if (!is_affinity_mask_valid(new_value)) {
97 98 99
		err = -EINVAL;
		goto free_cpumask;
	}
100

L
Linus Torvalds 已提交
101 102 103 104 105
	/*
	 * Do not allow disabling IRQs completely - it's a too easy
	 * way to make the system unusable accidentally :-) At least
	 * one online CPU still has to be targeted.
	 */
106
	if (!cpumask_intersects(new_value, cpu_online_mask)) {
107 108
		/* Special case for empty set - allow the architecture
		   code to set default SMP affinity. */
109
		err = irq_select_affinity_usr(irq, new_value) ? -EINVAL : count;
110 111 112 113 114 115 116 117
	} else {
		irq_set_affinity(irq, new_value);
		err = count;
	}

free_cpumask:
	free_cpumask_var(new_value);
	return err;
L
Linus Torvalds 已提交
118 119
}

120 121 122 123 124 125 126 127 128 129 130 131
static ssize_t irq_affinity_proc_write(struct file *file,
		const char __user *buffer, size_t count, loff_t *pos)
{
	return write_irq_affinity(0, file, buffer, count, pos);
}

static ssize_t irq_affinity_list_proc_write(struct file *file,
		const char __user *buffer, size_t count, loff_t *pos)
{
	return write_irq_affinity(1, file, buffer, count, pos);
}

132
static int irq_affinity_proc_open(struct inode *inode, struct file *file)
133
{
A
Al Viro 已提交
134
	return single_open(file, irq_affinity_proc_show, PDE_DATA(inode));
135 136
}

137 138
static int irq_affinity_list_proc_open(struct inode *inode, struct file *file)
{
A
Al Viro 已提交
139
	return single_open(file, irq_affinity_list_proc_show, PDE_DATA(inode));
140 141
}

142 143
static int irq_affinity_hint_proc_open(struct inode *inode, struct file *file)
{
A
Al Viro 已提交
144
	return single_open(file, irq_affinity_hint_proc_show, PDE_DATA(inode));
145 146
}

147 148 149 150 151 152 153 154
static const struct file_operations irq_affinity_proc_fops = {
	.open		= irq_affinity_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
	.write		= irq_affinity_proc_write,
};

155 156 157 158 159 160 161
static const struct file_operations irq_affinity_hint_proc_fops = {
	.open		= irq_affinity_hint_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

162 163 164 165 166 167 168 169
static const struct file_operations irq_affinity_list_proc_fops = {
	.open		= irq_affinity_list_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
	.write		= irq_affinity_list_proc_write,
};

170 171
static int default_affinity_show(struct seq_file *m, void *v)
{
R
Rusty Russell 已提交
172
	seq_cpumask(m, irq_default_affinity);
173 174 175 176 177 178
	seq_putc(m, '\n');
	return 0;
}

static ssize_t default_affinity_write(struct file *file,
		const char __user *buffer, size_t count, loff_t *ppos)
179
{
R
Rusty Russell 已提交
180
	cpumask_var_t new_value;
181
	int err;
182

R
Rusty Russell 已提交
183 184 185 186
	if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
		return -ENOMEM;

	err = cpumask_parse_user(buffer, count, new_value);
187
	if (err)
R
Rusty Russell 已提交
188
		goto out;
189

R
Rusty Russell 已提交
190 191 192 193
	if (!is_affinity_mask_valid(new_value)) {
		err = -EINVAL;
		goto out;
	}
194 195 196 197 198 199

	/*
	 * Do not allow disabling IRQs completely - it's a too easy
	 * way to make the system unusable accidentally :-) At least
	 * one online CPU still has to be targeted.
	 */
R
Rusty Russell 已提交
200 201 202 203
	if (!cpumask_intersects(new_value, cpu_online_mask)) {
		err = -EINVAL;
		goto out;
	}
204

R
Rusty Russell 已提交
205 206
	cpumask_copy(irq_default_affinity, new_value);
	err = count;
207

R
Rusty Russell 已提交
208 209 210
out:
	free_cpumask_var(new_value);
	return err;
211
}
212 213 214

static int default_affinity_open(struct inode *inode, struct file *file)
{
A
Al Viro 已提交
215
	return single_open(file, default_affinity_show, PDE_DATA(inode));
216 217 218 219 220 221 222 223 224
}

static const struct file_operations default_affinity_proc_fops = {
	.open		= default_affinity_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
	.write		= default_affinity_write,
};
225 226 227 228 229

static int irq_node_proc_show(struct seq_file *m, void *v)
{
	struct irq_desc *desc = irq_to_desc((long) m->private);

230
	seq_printf(m, "%d\n", desc->irq_data.node);
231 232 233 234 235
	return 0;
}

static int irq_node_proc_open(struct inode *inode, struct file *file)
{
A
Al Viro 已提交
236
	return single_open(file, irq_node_proc_show, PDE_DATA(inode));
237 238 239 240 241 242 243 244
}

static const struct file_operations irq_node_proc_fops = {
	.open		= irq_node_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};
L
Linus Torvalds 已提交
245 246
#endif

247
static int irq_spurious_proc_show(struct seq_file *m, void *v)
248
{
249 250 251 252 253 254 255 256 257 258
	struct irq_desc *desc = irq_to_desc((long) m->private);

	seq_printf(m, "count %u\n" "unhandled %u\n" "last_unhandled %u ms\n",
		   desc->irq_count, desc->irqs_unhandled,
		   jiffies_to_msecs(desc->last_unhandled));
	return 0;
}

static int irq_spurious_proc_open(struct inode *inode, struct file *file)
{
A
Al Viro 已提交
259
	return single_open(file, irq_spurious_proc_show, PDE_DATA(inode));
260 261
}

262 263 264 265 266 267 268
static const struct file_operations irq_spurious_proc_fops = {
	.open		= irq_spurious_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

L
Linus Torvalds 已提交
269 270 271 272
#define MAX_NAMELEN 128

static int name_unique(unsigned int irq, struct irqaction *new_action)
{
273
	struct irq_desc *desc = irq_to_desc(irq);
L
Linus Torvalds 已提交
274
	struct irqaction *action;
275 276
	unsigned long flags;
	int ret = 1;
L
Linus Torvalds 已提交
277

278
	raw_spin_lock_irqsave(&desc->lock, flags);
279
	for (action = desc->action ; action; action = action->next) {
L
Linus Torvalds 已提交
280
		if ((action != new_action) && action->name &&
281 282 283 284 285
				!strcmp(new_action->name, action->name)) {
			ret = 0;
			break;
		}
	}
286
	raw_spin_unlock_irqrestore(&desc->lock, flags);
287
	return ret;
L
Linus Torvalds 已提交
288 289 290 291 292
}

void register_handler_proc(unsigned int irq, struct irqaction *action)
{
	char name [MAX_NAMELEN];
293
	struct irq_desc *desc = irq_to_desc(irq);
L
Linus Torvalds 已提交
294

295
	if (!desc->dir || action->dir || !action->name ||
L
Linus Torvalds 已提交
296 297 298 299 300 301 302
					!name_unique(irq, action))
		return;

	memset(name, 0, MAX_NAMELEN);
	snprintf(name, MAX_NAMELEN, "%s", action->name);

	/* create /proc/irq/1234/handler/ */
303
	action->dir = proc_mkdir(name, desc->dir);
L
Linus Torvalds 已提交
304 305 306 307 308 309
}

#undef MAX_NAMELEN

#define MAX_NAMELEN 10

310
void register_irq_proc(unsigned int irq, struct irq_desc *desc)
L
Linus Torvalds 已提交
311 312 313
{
	char name [MAX_NAMELEN];

314
	if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip) || desc->dir)
L
Linus Torvalds 已提交
315 316 317 318 319 320
		return;

	memset(name, 0, MAX_NAMELEN);
	sprintf(name, "%d", irq);

	/* create /proc/irq/1234 */
321
	desc->dir = proc_mkdir(name, root_irq_dir);
322 323
	if (!desc->dir)
		return;
L
Linus Torvalds 已提交
324 325

#ifdef CONFIG_SMP
326
	/* create /proc/irq/<irq>/smp_affinity */
327
	proc_create_data("smp_affinity", 0600, desc->dir,
328
			 &irq_affinity_proc_fops, (void *)(long)irq);
329

330 331 332 333
	/* create /proc/irq/<irq>/affinity_hint */
	proc_create_data("affinity_hint", 0400, desc->dir,
			 &irq_affinity_hint_proc_fops, (void *)(long)irq);

334 335 336 337
	/* create /proc/irq/<irq>/smp_affinity_list */
	proc_create_data("smp_affinity_list", 0600, desc->dir,
			 &irq_affinity_list_proc_fops, (void *)(long)irq);

338 339
	proc_create_data("node", 0444, desc->dir,
			 &irq_node_proc_fops, (void *)(long)irq);
L
Linus Torvalds 已提交
340
#endif
341

342 343
	proc_create_data("spurious", 0444, desc->dir,
			 &irq_spurious_proc_fops, (void *)(long)irq);
L
Linus Torvalds 已提交
344 345
}

346 347 348 349 350 351 352 353 354
void unregister_irq_proc(unsigned int irq, struct irq_desc *desc)
{
	char name [MAX_NAMELEN];

	if (!root_irq_dir || !desc->dir)
		return;
#ifdef CONFIG_SMP
	remove_proc_entry("smp_affinity", desc->dir);
	remove_proc_entry("affinity_hint", desc->dir);
355
	remove_proc_entry("smp_affinity_list", desc->dir);
356 357 358 359 360 361 362 363 364
	remove_proc_entry("node", desc->dir);
#endif
	remove_proc_entry("spurious", desc->dir);

	memset(name, 0, MAX_NAMELEN);
	sprintf(name, "%u", irq);
	remove_proc_entry(name, root_irq_dir);
}

L
Linus Torvalds 已提交
365 366 367 368
#undef MAX_NAMELEN

void unregister_handler_proc(unsigned int irq, struct irqaction *action)
{
369
	proc_remove(action->dir);
L
Linus Torvalds 已提交
370 371
}

R
roel kluin 已提交
372
static void register_default_affinity_proc(void)
373 374
{
#ifdef CONFIG_SMP
375 376
	proc_create("irq/default_smp_affinity", 0600, NULL,
		    &default_affinity_proc_fops);
377 378 379
#endif
}

L
Linus Torvalds 已提交
380 381
void init_irq_proc(void)
{
382 383
	unsigned int irq;
	struct irq_desc *desc;
L
Linus Torvalds 已提交
384 385 386 387 388 389

	/* create /proc/irq */
	root_irq_dir = proc_mkdir("irq", NULL);
	if (!root_irq_dir)
		return;

390 391
	register_default_affinity_proc();

L
Linus Torvalds 已提交
392 393 394
	/*
	 * Create entries for all existing IRQs.
	 */
395 396 397 398
	for_each_irq_desc(irq, desc) {
		if (!desc)
			continue;

399
		register_irq_proc(irq, desc);
400
	}
L
Linus Torvalds 已提交
401 402
}

403 404 405 406 407 408 409
#ifdef CONFIG_GENERIC_IRQ_SHOW

int __weak arch_show_interrupts(struct seq_file *p, int prec)
{
	return 0;
}

410 411 412 413
#ifndef ACTUAL_NR_IRQS
# define ACTUAL_NR_IRQS nr_irqs
#endif

414 415 416 417 418 419 420 421 422
int show_interrupts(struct seq_file *p, void *v)
{
	static int prec;

	unsigned long flags, any_count = 0;
	int i = *(loff_t *) v, j;
	struct irqaction *action;
	struct irq_desc *desc;

423
	if (i > ACTUAL_NR_IRQS)
424 425
		return 0;

426
	if (i == ACTUAL_NR_IRQS)
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
		return arch_show_interrupts(p, prec);

	/* print header and calculate the width of the first column */
	if (i == 0) {
		for (prec = 3, j = 1000; prec < 10 && j <= nr_irqs; ++prec)
			j *= 10;

		seq_printf(p, "%*s", prec + 8, "");
		for_each_online_cpu(j)
			seq_printf(p, "CPU%-8d", j);
		seq_putc(p, '\n');
	}

	desc = irq_to_desc(i);
	if (!desc)
		return 0;

	raw_spin_lock_irqsave(&desc->lock, flags);
	for_each_online_cpu(j)
		any_count |= kstat_irqs_cpu(i, j);
	action = desc->action;
	if (!action && !any_count)
		goto out;

	seq_printf(p, "%*d: ", prec, i);
	for_each_online_cpu(j)
		seq_printf(p, "%10u ", kstat_irqs_cpu(i, j));
454 455 456 457 458 459 460 461 462 463 464

	if (desc->irq_data.chip) {
		if (desc->irq_data.chip->irq_print_chip)
			desc->irq_data.chip->irq_print_chip(&desc->irq_data, p);
		else if (desc->irq_data.chip->name)
			seq_printf(p, " %8s", desc->irq_data.chip->name);
		else
			seq_printf(p, " %8s", "-");
	} else {
		seq_printf(p, " %8s", "None");
	}
465
#ifdef CONFIG_GENERIC_IRQ_SHOW_LEVEL
466 467
	seq_printf(p, " %-8s", irqd_is_level_type(&desc->irq_data) ? "Level" : "Edge");
#endif
468 469
	if (desc->name)
		seq_printf(p, "-%-8s", desc->name);
470 471 472 473 474 475 476 477 478 479 480 481 482

	if (action) {
		seq_printf(p, "  %s", action->name);
		while ((action = action->next) != NULL)
			seq_printf(p, ", %s", action->name);
	}

	seq_putc(p, '\n');
out:
	raw_spin_unlock_irqrestore(&desc->lock, flags);
	return 0;
}
#endif