irq.c 13.7 KB
Newer Older
J
Jeff Dike 已提交
1
/*
A
Anton Ivanov 已提交
2 3
 * Copyright (C) 2017 - Cambridge Greys Ltd
 * Copyright (C) 2011 - 2014 Cisco Systems Inc
J
Jeff Dike 已提交
4
 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
L
Linus Torvalds 已提交
5 6 7 8 9
 * Licensed under the GPL
 * Derived (i.e. mostly copied) from arch/i386/kernel/irq.c:
 *	Copyright (C) 1992, 1998 Linus Torvalds, Ingo Molnar
 */

10 11 12 13 14 15 16 17 18 19 20
#include <linux/cpumask.h>
#include <linux/hardirq.h>
#include <linux/interrupt.h>
#include <linux/kernel_stat.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/seq_file.h>
#include <linux/slab.h>
#include <as-layout.h>
#include <kern_util.h>
#include <os.h>
A
Anton Ivanov 已提交
21
#include <irq_user.h>
L
Linus Torvalds 已提交
22

A
Anton Ivanov 已提交
23 24 25 26 27

/* When epoll triggers we do not know why it did so
 * we can also have different IRQs for read and write.
 * This is why we keep a small irq_fd array for each fd -
 * one entry per IRQ type
J
Jeff Dike 已提交
28
 */
29

A
Anton Ivanov 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
struct irq_entry {
	struct irq_entry *next;
	int fd;
	struct irq_fd *irq_array[MAX_IRQ_TYPE + 1];
};

static struct irq_entry *active_fds;

static DEFINE_SPINLOCK(irq_lock);

static void irq_io_loop(struct irq_fd *irq, struct uml_pt_regs *regs)
{
/*
 * irq->active guards against reentry
 * irq->pending accumulates pending requests
 * if pending is raised the irq_handler is re-run
 * until pending is cleared
 */
	if (irq->active) {
		irq->active = false;
		do {
			irq->pending = false;
			do_IRQ(irq->irq, regs);
		} while (irq->pending && (!irq->purge));
		if (!irq->purge)
			irq->active = true;
	} else {
		irq->pending = true;
	}
}
60

61
void sigio_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
62
{
A
Anton Ivanov 已提交
63 64 65 66
	struct irq_entry *irq_entry;
	struct irq_fd *irq;

	int n, i, j;
67

68
	while (1) {
A
Anton Ivanov 已提交
69 70 71 72 73 74 75 76
		/* This is now lockless - epoll keeps back-referencesto the irqs
		 * which have trigger it so there is no need to walk the irq
		 * list and lock it every time. We avoid locking by turning off
		 * IO for a specific fd by executing os_del_epoll_fd(fd) before
		 * we do any changes to the actual data structures
		 */
		n = os_waiting_for_events_epoll();

77
		if (n <= 0) {
J
Jeff Dike 已提交
78 79
			if (n == -EINTR)
				continue;
A
Anton Ivanov 已提交
80 81
			else
				break;
82 83
		}

A
Anton Ivanov 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
		for (i = 0; i < n ; i++) {
			/* Epoll back reference is the entry with 3 irq_fd
			 * leaves - one for each irq type.
			 */
			irq_entry = (struct irq_entry *)
				os_epoll_get_data_pointer(i);
			for (j = 0; j < MAX_IRQ_TYPE ; j++) {
				irq = irq_entry->irq_array[j];
				if (irq == NULL)
					continue;
				if (os_epoll_triggered(i, irq->events) > 0)
					irq_io_loop(irq, regs);
				if (irq->purge) {
					irq_entry->irq_array[j] = NULL;
					kfree(irq);
				}
100 101 102
			}
		}
	}
A
Anton Ivanov 已提交
103 104 105 106 107 108 109
}

static int assign_epoll_events_to_irq(struct irq_entry *irq_entry)
{
	int i;
	int events = 0;
	struct irq_fd *irq;
110

A
Anton Ivanov 已提交
111 112 113 114 115 116 117 118 119 120 121
	for (i = 0; i < MAX_IRQ_TYPE ; i++) {
		irq = irq_entry->irq_array[i];
		if (irq != NULL)
			events = irq->events | events;
	}
	if (events > 0) {
	/* os_add_epoll will call os_mod_epoll if this already exists */
		return os_add_epoll_fd(events, irq_entry->fd, irq_entry);
	}
	/* No events - delete */
	return os_del_epoll_fd(irq_entry->fd);
122 123
}

A
Anton Ivanov 已提交
124

125

126
static int activate_fd(int irq, int fd, int type, void *dev_id)
127
{
A
Anton Ivanov 已提交
128 129 130
	struct irq_fd *new_fd;
	struct irq_entry *irq_entry;
	int i, err, events;
131 132
	unsigned long flags;

J
Jeff Dike 已提交
133
	err = os_set_fd_async(fd);
134
	if (err < 0)
135 136
		goto out;

A
Anton Ivanov 已提交
137
	spin_lock_irqsave(&irq_lock, flags);
138

A
Anton Ivanov 已提交
139
	/* Check if we have an entry for this fd */
140

141
	err = -EBUSY;
A
Anton Ivanov 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155
	for (irq_entry = active_fds;
		irq_entry != NULL; irq_entry = irq_entry->next) {
		if (irq_entry->fd == fd)
			break;
	}

	if (irq_entry == NULL) {
		/* This needs to be atomic as it may be called from an
		 * IRQ context.
		 */
		irq_entry = kmalloc(sizeof(struct irq_entry), GFP_ATOMIC);
		if (irq_entry == NULL) {
			printk(KERN_ERR
				"Failed to allocate new IRQ entry\n");
156 157
			goto out_unlock;
		}
A
Anton Ivanov 已提交
158 159 160 161 162
		irq_entry->fd = fd;
		for (i = 0; i < MAX_IRQ_TYPE; i++)
			irq_entry->irq_array[i] = NULL;
		irq_entry->next = active_fds;
		active_fds = irq_entry;
163 164
	}

A
Anton Ivanov 已提交
165 166 167
	/* Check if we are trying to re-register an interrupt for a
	 * particular fd
	 */
168

A
Anton Ivanov 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181
	if (irq_entry->irq_array[type] != NULL) {
		printk(KERN_ERR
			"Trying to reregister IRQ %d FD %d TYPE %d ID %p\n",
			irq, fd, type, dev_id
		);
		goto out_unlock;
	} else {
		/* New entry for this fd */

		err = -ENOMEM;
		new_fd = kmalloc(sizeof(struct irq_fd), GFP_ATOMIC);
		if (new_fd == NULL)
			goto out_unlock;
182

A
Anton Ivanov 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195
		events = os_event_mask(type);

		*new_fd = ((struct irq_fd) {
			.id		= dev_id,
			.irq		= irq,
			.type		= type,
			.events		= events,
			.active		= true,
			.pending	= false,
			.purge		= false
		});
		/* Turn off any IO on this fd - allows us to
		 * avoid locking the IRQ loop
196
		 */
A
Anton Ivanov 已提交
197 198
		os_del_epoll_fd(irq_entry->fd);
		irq_entry->irq_array[type] = new_fd;
199 200
	}

A
Anton Ivanov 已提交
201 202
	/* Turn back IO on with the correct (new) IO event mask */
	assign_epoll_events_to_irq(irq_entry);
203
	spin_unlock_irqrestore(&irq_lock, flags);
A
Anton Ivanov 已提交
204
	maybe_sigio_broken(fd, (type != IRQ_NONE));
205

J
Jeff Dike 已提交
206
	return 0;
A
Anton Ivanov 已提交
207
out_unlock:
208
	spin_unlock_irqrestore(&irq_lock, flags);
A
Anton Ivanov 已提交
209
out:
J
Jeff Dike 已提交
210
	return err;
211 212
}

A
Anton Ivanov 已提交
213 214 215 216 217 218
/*
 * Walk the IRQ list and dispose of any unused entries.
 * Should be done under irq_lock.
 */

static void garbage_collect_irq_entries(void)
219
{
A
Anton Ivanov 已提交
220 221 222 223 224
	int i;
	bool reap;
	struct irq_entry *walk;
	struct irq_entry *previous = NULL;
	struct irq_entry *to_free;
225

A
Anton Ivanov 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
	if (active_fds == NULL)
		return;
	walk = active_fds;
	while (walk != NULL) {
		reap = true;
		for (i = 0; i < MAX_IRQ_TYPE ; i++) {
			if (walk->irq_array[i] != NULL) {
				reap = false;
				break;
			}
		}
		if (reap) {
			if (previous == NULL)
				active_fds = walk->next;
			else
				previous->next = walk->next;
			to_free = walk;
		} else {
			to_free = NULL;
		}
		walk = walk->next;
		if (to_free != NULL)
			kfree(to_free);
	}
250 251
}

A
Anton Ivanov 已提交
252 253 254
/*
 * Walk the IRQ list and get the descriptor for our FD
 */
255

A
Anton Ivanov 已提交
256
static struct irq_entry *get_irq_entry_by_fd(int fd)
257
{
A
Anton Ivanov 已提交
258
	struct irq_entry *walk = active_fds;
259

A
Anton Ivanov 已提交
260 261 262 263 264 265
	while (walk != NULL) {
		if (walk->fd == fd)
			return walk;
		walk = walk->next;
	}
	return NULL;
266 267 268
}


A
Anton Ivanov 已提交
269 270 271 272 273 274
/*
 * Walk the IRQ list and dispose of an entry for a specific
 * device, fd and number. Note - if sharing an IRQ for read
 * and writefor the same FD it will be disposed in either case.
 * If this behaviour is undesirable use different IRQ ids.
 */
275

A
Anton Ivanov 已提交
276 277
#define IGNORE_IRQ 1
#define IGNORE_DEV (1<<1)
278

A
Anton Ivanov 已提交
279 280 281 282 283 284
static void do_free_by_irq_and_dev(
	struct irq_entry *irq_entry,
	unsigned int irq,
	void *dev,
	int flags
)
285
{
A
Anton Ivanov 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
	int i;
	struct irq_fd *to_free;

	for (i = 0; i < MAX_IRQ_TYPE ; i++) {
		if (irq_entry->irq_array[i] != NULL) {
			if (
			((flags & IGNORE_IRQ) ||
				(irq_entry->irq_array[i]->irq == irq)) &&
			((flags & IGNORE_DEV) ||
				(irq_entry->irq_array[i]->id == dev))
			) {
				/* Turn off any IO on this fd - allows us to
				 * avoid locking the IRQ loop
				 */
				os_del_epoll_fd(irq_entry->fd);
				to_free = irq_entry->irq_array[i];
				irq_entry->irq_array[i] = NULL;
				assign_epoll_events_to_irq(irq_entry);
				if (to_free->active)
					to_free->purge = true;
				else
					kfree(to_free);
			}
		}
	}
311 312
}

A
Anton Ivanov 已提交
313
void free_irq_by_fd(int fd)
314
{
A
Anton Ivanov 已提交
315 316
	struct irq_entry *to_free;
	unsigned long flags;
317

A
Anton Ivanov 已提交
318 319 320 321 322 323 324 325 326
	spin_lock_irqsave(&irq_lock, flags);
	to_free = get_irq_entry_by_fd(fd);
	if (to_free != NULL) {
		do_free_by_irq_and_dev(
			to_free,
			-1,
			NULL,
			IGNORE_IRQ | IGNORE_DEV
		);
327
	}
A
Anton Ivanov 已提交
328 329
	garbage_collect_irq_entries();
	spin_unlock_irqrestore(&irq_lock, flags);
330
}
331
EXPORT_SYMBOL(free_irq_by_fd);
332

A
Anton Ivanov 已提交
333
static void free_irq_by_irq_and_dev(unsigned int irq, void *dev)
334
{
A
Anton Ivanov 已提交
335
	struct irq_entry *to_free;
336 337
	unsigned long flags;

338
	spin_lock_irqsave(&irq_lock, flags);
A
Anton Ivanov 已提交
339 340 341 342 343 344 345 346 347
	to_free = active_fds;
	while (to_free != NULL) {
		do_free_by_irq_and_dev(
			to_free,
			irq,
			dev,
			0
		);
		to_free = to_free->next;
348
	}
A
Anton Ivanov 已提交
349
	garbage_collect_irq_entries();
350
	spin_unlock_irqrestore(&irq_lock, flags);
A
Anton Ivanov 已提交
351 352
}

353

A
Anton Ivanov 已提交
354 355 356
void reactivate_fd(int fd, int irqnum)
{
	/** NOP - we do auto-EOI now **/
357 358 359 360
}

void deactivate_fd(int fd, int irqnum)
{
A
Anton Ivanov 已提交
361
	struct irq_entry *to_free;
362 363
	unsigned long flags;

A
Anton Ivanov 已提交
364
	os_del_epoll_fd(fd);
365
	spin_lock_irqsave(&irq_lock, flags);
A
Anton Ivanov 已提交
366 367 368 369 370 371 372 373
	to_free = get_irq_entry_by_fd(fd);
	if (to_free != NULL) {
		do_free_by_irq_and_dev(
			to_free,
			irqnum,
			NULL,
			IGNORE_DEV
		);
J
Jeff Dike 已提交
374
	}
A
Anton Ivanov 已提交
375
	garbage_collect_irq_entries();
376
	spin_unlock_irqrestore(&irq_lock, flags);
J
Jeff Dike 已提交
377
	ignore_sigio_fd(fd);
378
}
379
EXPORT_SYMBOL(deactivate_fd);
380

J
Jeff Dike 已提交
381 382 383 384 385 386
/*
 * Called just before shutdown in order to provide a clean exec
 * environment in case the system is rebooting.  No locking because
 * that would cause a pointless shutdown hang if something hadn't
 * released the lock.
 */
387 388
int deactivate_all_fds(void)
{
A
Anton Ivanov 已提交
389 390
	unsigned long flags;
	struct irq_entry *to_free;
391

A
Anton Ivanov 已提交
392 393 394 395 396
	spin_lock_irqsave(&irq_lock, flags);
	/* Stop IO. The IRQ loop has no lock so this is our
	 * only way of making sure we are safe to dispose
	 * of all IRQ handlers
	 */
397
	os_set_ioignore();
A
Anton Ivanov 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410
	to_free = active_fds;
	while (to_free != NULL) {
		do_free_by_irq_and_dev(
			to_free,
			-1,
			NULL,
			IGNORE_IRQ | IGNORE_DEV
		);
		to_free = to_free->next;
	}
	garbage_collect_irq_entries();
	spin_unlock_irqrestore(&irq_lock, flags);
	os_close_epoll_fd();
411
	return 0;
412 413
}

L
Linus Torvalds 已提交
414
/*
S
Simon Arlott 已提交
415
 * do_IRQ handles all normal device IRQs (the special
L
Linus Torvalds 已提交
416 417 418
 * SMP cross-CPU interrupts have their own specific
 * handlers).
 */
419
unsigned int do_IRQ(int irq, struct uml_pt_regs *regs)
L
Linus Torvalds 已提交
420
{
A
Al Viro 已提交
421 422
	struct pt_regs *old_regs = set_irq_regs((struct pt_regs *)regs);
	irq_enter();
423
	generic_handle_irq(irq);
A
Al Viro 已提交
424 425 426
	irq_exit();
	set_irq_regs(old_regs);
	return 1;
L
Linus Torvalds 已提交
427 428
}

R
Richard Weinberger 已提交
429 430 431 432 433 434 435
void um_free_irq(unsigned int irq, void *dev)
{
	free_irq_by_irq_and_dev(irq, dev);
	free_irq(irq, dev);
}
EXPORT_SYMBOL(um_free_irq);

L
Linus Torvalds 已提交
436
int um_request_irq(unsigned int irq, int fd, int type,
437
		   irq_handler_t handler,
L
Linus Torvalds 已提交
438 439 440 441 442
		   unsigned long irqflags, const char * devname,
		   void *dev_id)
{
	int err;

J
Jeff Dike 已提交
443
	if (fd != -1) {
L
Linus Torvalds 已提交
444
		err = activate_fd(irq, fd, type, dev_id);
J
Jeff Dike 已提交
445 446 447 448 449
		if (err)
			return err;
	}

	return request_irq(irq, handler, irqflags, devname, dev_id);
L
Linus Torvalds 已提交
450
}
J
Jeff Dike 已提交
451

L
Linus Torvalds 已提交
452 453 454
EXPORT_SYMBOL(um_request_irq);
EXPORT_SYMBOL(reactivate_fd);

J
Jeff Dike 已提交
455
/*
456 457
 * irq_chip must define at least enable/disable and ack when
 * the edge handler is used.
J
Jeff Dike 已提交
458
 */
459
static void dummy(struct irq_data *d)
L
Linus Torvalds 已提交
460 461 462
{
}

463
/* This is used for everything else than the timer. */
464
static struct irq_chip normal_irq_type = {
465
	.name = "SIGIO",
466 467 468
	.irq_disable = dummy,
	.irq_enable = dummy,
	.irq_ack = dummy,
469 470
	.irq_mask = dummy,
	.irq_unmask = dummy,
L
Linus Torvalds 已提交
471 472
};

473
static struct irq_chip SIGVTALRM_irq_type = {
474
	.name = "SIGVTALRM",
475 476 477
	.irq_disable = dummy,
	.irq_enable = dummy,
	.irq_ack = dummy,
478 479
	.irq_mask = dummy,
	.irq_unmask = dummy,
L
Linus Torvalds 已提交
480 481 482 483 484 485
};

void __init init_IRQ(void)
{
	int i;

T
Thomas Gleixner 已提交
486
	irq_set_chip_and_handler(TIMER_IRQ, &SIGVTALRM_irq_type, handle_edge_irq);
487

A
Anton Ivanov 已提交
488

T
Thomas Gleixner 已提交
489 490
	for (i = 1; i < NR_IRQS; i++)
		irq_set_chip_and_handler(i, &normal_irq_type, handle_edge_irq);
A
Anton Ivanov 已提交
491 492
	/* Initialize EPOLL Loop */
	os_setup_epoll();
L
Linus Torvalds 已提交
493 494
}

J
Jeff Dike 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
/*
 * IRQ stack entry and exit:
 *
 * Unlike i386, UML doesn't receive IRQs on the normal kernel stack
 * and switch over to the IRQ stack after some preparation.  We use
 * sigaltstack to receive signals on a separate stack from the start.
 * These two functions make sure the rest of the kernel won't be too
 * upset by being on a different stack.  The IRQ stack has a
 * thread_info structure at the bottom so that current et al continue
 * to work.
 *
 * to_irq_stack copies the current task's thread_info to the IRQ stack
 * thread_info and sets the tasks's stack to point to the IRQ stack.
 *
 * from_irq_stack copies the thread_info struct back (flags may have
 * been modified) and resets the task's stack pointer.
 *
 * Tricky bits -
 *
 * What happens when two signals race each other?  UML doesn't block
 * signals with sigprocmask, SA_DEFER, or sa_mask, so a second signal
 * could arrive while a previous one is still setting up the
 * thread_info.
 *
 * There are three cases -
 *     The first interrupt on the stack - sets up the thread_info and
 * handles the interrupt
 *     A nested interrupt interrupting the copying of the thread_info -
 * can't handle the interrupt, as the stack is in an unknown state
 *     A nested interrupt not interrupting the copying of the
 * thread_info - doesn't do any setup, just handles the interrupt
 *
 * The first job is to figure out whether we interrupted stack setup.
 * This is done by xchging the signal mask with thread_info->pending.
 * If the value that comes back is zero, then there is no setup in
 * progress, and the interrupt can be handled.  If the value is
 * non-zero, then there is stack setup in progress.  In order to have
 * the interrupt handled, we leave our signal in the mask, and it will
 * be handled by the upper handler after it has set up the stack.
 *
 * Next is to figure out whether we are the outer handler or a nested
 * one.  As part of setting up the stack, thread_info->real_thread is
 * set to non-NULL (and is reset to NULL on exit).  This is the
 * nesting indicator.  If it is non-NULL, then the stack is already
 * set up and the handler can run.
 */

static unsigned long pending_mask;

J
Jeff Dike 已提交
544
unsigned long to_irq_stack(unsigned long *mask_out)
J
Jeff Dike 已提交
545 546 547 548 549
{
	struct thread_info *ti;
	unsigned long mask, old;
	int nested;

J
Jeff Dike 已提交
550
	mask = xchg(&pending_mask, *mask_out);
J
Jeff Dike 已提交
551 552 553
	if (mask != 0) {
		/*
		 * If any interrupts come in at this point, we want to
J
Jeff Dike 已提交
554 555 556 557 558 559 560
		 * make sure that their bits aren't lost by our
		 * putting our bit in.  So, this loop accumulates bits
		 * until xchg returns the same value that we put in.
		 * When that happens, there were no new interrupts,
		 * and pending_mask contains a bit for each interrupt
		 * that came in.
		 */
J
Jeff Dike 已提交
561
		old = *mask_out;
J
Jeff Dike 已提交
562 563 564
		do {
			old |= mask;
			mask = xchg(&pending_mask, old);
J
Jeff Dike 已提交
565
		} while (mask != old);
J
Jeff Dike 已提交
566 567 568 569 570
		return 1;
	}

	ti = current_thread_info();
	nested = (ti->real_thread != NULL);
J
Jeff Dike 已提交
571
	if (!nested) {
J
Jeff Dike 已提交
572 573 574 575 576
		struct task_struct *task;
		struct thread_info *tti;

		task = cpu_tasks[ti->cpu].task;
		tti = task_thread_info(task);
J
Jeff Dike 已提交
577

J
Jeff Dike 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
		*ti = *tti;
		ti->real_thread = tti;
		task->stack = ti;
	}

	mask = xchg(&pending_mask, 0);
	*mask_out |= mask | nested;
	return 0;
}

unsigned long from_irq_stack(int nested)
{
	struct thread_info *ti, *to;
	unsigned long mask;

	ti = current_thread_info();

	pending_mask = 1;

	to = ti->real_thread;
	current->stack = to;
	ti->real_thread = NULL;
	*to = *ti;

	mask = xchg(&pending_mask, 0);
	return mask & ~1;
}