Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2017 - Cambridge Greys Ltd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2011 - 2014 Cisco Systems Inc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Derived (i.e. mostly copied) from arch/i386/kernel/irq.c:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *	Copyright (C) 1992, 1998 Linus Torvalds, Ingo Molnar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/cpumask.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/hardirq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/kernel_stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <as-layout.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <kern_util.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <os.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <irq_user.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) extern void free_irqs(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) /* When epoll triggers we do not know why it did so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * we can also have different IRQs for read and write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * This is why we keep a small irq_fd array for each fd -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * one entry per IRQ type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) struct irq_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct irq_entry *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct irq_fd *irq_array[MAX_IRQ_TYPE + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static struct irq_entry *active_fds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) static DEFINE_SPINLOCK(irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) static void irq_io_loop(struct irq_fd *irq, struct uml_pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * irq->active guards against reentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * irq->pending accumulates pending requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * if pending is raised the irq_handler is re-run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * until pending is cleared
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	if (irq->active) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		irq->active = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 			irq->pending = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 			do_IRQ(irq->irq, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		} while (irq->pending && (!irq->purge));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		if (!irq->purge)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 			irq->active = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		irq->pending = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) void sigio_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	struct irq_entry *irq_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct irq_fd *irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	int n, i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		/* This is now lockless - epoll keeps back-referencesto the irqs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		 * which have trigger it so there is no need to walk the irq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		 * list and lock it every time. We avoid locking by turning off
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		 * IO for a specific fd by executing os_del_epoll_fd(fd) before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		 * we do any changes to the actual data structures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		n = os_waiting_for_events_epoll();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		if (n <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			if (n == -EINTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		for (i = 0; i < n ; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			/* Epoll back reference is the entry with 3 irq_fd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			 * leaves - one for each irq type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 			irq_entry = (struct irq_entry *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 				os_epoll_get_data_pointer(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			for (j = 0; j < MAX_IRQ_TYPE ; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 				irq = irq_entry->irq_array[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 				if (irq == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 				if (os_epoll_triggered(i, irq->events) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 					irq_io_loop(irq, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 				if (irq->purge) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 					irq_entry->irq_array[j] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 					kfree(irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	free_irqs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static int assign_epoll_events_to_irq(struct irq_entry *irq_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	int events = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	struct irq_fd *irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	for (i = 0; i < MAX_IRQ_TYPE ; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		irq = irq_entry->irq_array[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		if (irq != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 			events = irq->events | events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (events > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	/* os_add_epoll will call os_mod_epoll if this already exists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		return os_add_epoll_fd(events, irq_entry->fd, irq_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	/* No events - delete */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	return os_del_epoll_fd(irq_entry->fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static int activate_fd(int irq, int fd, int type, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	struct irq_fd *new_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	struct irq_entry *irq_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	int i, err, events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	err = os_set_fd_async(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	spin_lock_irqsave(&irq_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	/* Check if we have an entry for this fd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	for (irq_entry = active_fds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		irq_entry != NULL; irq_entry = irq_entry->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		if (irq_entry->fd == fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	if (irq_entry == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		/* This needs to be atomic as it may be called from an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		 * IRQ context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		irq_entry = kmalloc(sizeof(struct irq_entry), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		if (irq_entry == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			printk(KERN_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 				"Failed to allocate new IRQ entry\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		irq_entry->fd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		for (i = 0; i < MAX_IRQ_TYPE; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			irq_entry->irq_array[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		irq_entry->next = active_fds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		active_fds = irq_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	/* Check if we are trying to re-register an interrupt for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	 * particular fd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	if (irq_entry->irq_array[type] != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		printk(KERN_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 			"Trying to reregister IRQ %d FD %d TYPE %d ID %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			irq, fd, type, dev_id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		/* New entry for this fd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		new_fd = kmalloc(sizeof(struct irq_fd), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		if (new_fd == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		events = os_event_mask(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		*new_fd = ((struct irq_fd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			.id		= dev_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			.irq		= irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			.type		= type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			.events		= events,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			.active		= true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			.pending	= false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			.purge		= false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		});
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		/* Turn off any IO on this fd - allows us to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		 * avoid locking the IRQ loop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		os_del_epoll_fd(irq_entry->fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		irq_entry->irq_array[type] = new_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	/* Turn back IO on with the correct (new) IO event mask */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	assign_epoll_events_to_irq(irq_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	spin_unlock_irqrestore(&irq_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	maybe_sigio_broken(fd, (type != IRQ_NONE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	spin_unlock_irqrestore(&irq_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)  * Walk the IRQ list and dispose of any unused entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)  * Should be done under irq_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static void garbage_collect_irq_entries(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	bool reap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	struct irq_entry *walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	struct irq_entry *previous = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	struct irq_entry *to_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	if (active_fds == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	walk = active_fds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	while (walk != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		reap = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		for (i = 0; i < MAX_IRQ_TYPE ; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			if (walk->irq_array[i] != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 				reap = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		if (reap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			if (previous == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 				active_fds = walk->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 				previous->next = walk->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			to_free = walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			to_free = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		walk = walk->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		kfree(to_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)  * Walk the IRQ list and get the descriptor for our FD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) static struct irq_entry *get_irq_entry_by_fd(int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	struct irq_entry *walk = active_fds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	while (walk != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		if (walk->fd == fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			return walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		walk = walk->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)  * Walk the IRQ list and dispose of an entry for a specific
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)  * device, fd and number. Note - if sharing an IRQ for read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)  * and writefor the same FD it will be disposed in either case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)  * If this behaviour is undesirable use different IRQ ids.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) #define IGNORE_IRQ 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) #define IGNORE_DEV (1<<1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) static void do_free_by_irq_and_dev(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	struct irq_entry *irq_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	unsigned int irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	void *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	int flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	struct irq_fd *to_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	for (i = 0; i < MAX_IRQ_TYPE ; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		if (irq_entry->irq_array[i] != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			if (
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			((flags & IGNORE_IRQ) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 				(irq_entry->irq_array[i]->irq == irq)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 			((flags & IGNORE_DEV) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 				(irq_entry->irq_array[i]->id == dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 				/* Turn off any IO on this fd - allows us to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 				 * avoid locking the IRQ loop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 				os_del_epoll_fd(irq_entry->fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 				to_free = irq_entry->irq_array[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 				irq_entry->irq_array[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 				assign_epoll_events_to_irq(irq_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 				if (to_free->active)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 					to_free->purge = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 				else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 					kfree(to_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) void free_irq_by_fd(int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	struct irq_entry *to_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	spin_lock_irqsave(&irq_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	to_free = get_irq_entry_by_fd(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	if (to_free != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		do_free_by_irq_and_dev(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			to_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 			-1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 			NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			IGNORE_IRQ | IGNORE_DEV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	garbage_collect_irq_entries();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	spin_unlock_irqrestore(&irq_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) EXPORT_SYMBOL(free_irq_by_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static void free_irq_by_irq_and_dev(unsigned int irq, void *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	struct irq_entry *to_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	spin_lock_irqsave(&irq_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	to_free = active_fds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	while (to_free != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		do_free_by_irq_and_dev(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 			to_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 			0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		to_free = to_free->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	garbage_collect_irq_entries();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	spin_unlock_irqrestore(&irq_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) void deactivate_fd(int fd, int irqnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	struct irq_entry *to_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	os_del_epoll_fd(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	spin_lock_irqsave(&irq_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	to_free = get_irq_entry_by_fd(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	if (to_free != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		do_free_by_irq_and_dev(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 			to_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 			irqnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 			NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 			IGNORE_DEV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	garbage_collect_irq_entries();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	spin_unlock_irqrestore(&irq_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	ignore_sigio_fd(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) EXPORT_SYMBOL(deactivate_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)  * Called just before shutdown in order to provide a clean exec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)  * environment in case the system is rebooting.  No locking because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)  * that would cause a pointless shutdown hang if something hadn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)  * released the lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) int deactivate_all_fds(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	struct irq_entry *to_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	/* Stop IO. The IRQ loop has no lock so this is our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	 * only way of making sure we are safe to dispose
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	 * of all IRQ handlers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	os_set_ioignore();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	to_free = active_fds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	while (to_free != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		do_free_by_irq_and_dev(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 			to_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 			-1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 			NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 			IGNORE_IRQ | IGNORE_DEV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		to_free = to_free->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	/* don't garbage collect - we can no longer call kfree() here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	os_close_epoll_fd();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)  * do_IRQ handles all normal device IRQs (the special
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)  * SMP cross-CPU interrupts have their own specific
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)  * handlers).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) unsigned int do_IRQ(int irq, struct uml_pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	struct pt_regs *old_regs = set_irq_regs((struct pt_regs *)regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	irq_enter();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	generic_handle_irq(irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	irq_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	set_irq_regs(old_regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) void um_free_irq(unsigned int irq, void *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	free_irq_by_irq_and_dev(irq, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	free_irq(irq, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) EXPORT_SYMBOL(um_free_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) int um_request_irq(unsigned int irq, int fd, int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		   irq_handler_t handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		   unsigned long irqflags, const char * devname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		   void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	if (fd != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		err = activate_fd(irq, fd, type, dev_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	return request_irq(irq, handler, irqflags, devname, dev_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) EXPORT_SYMBOL(um_request_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)  * irq_chip must define at least enable/disable and ack when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)  * the edge handler is used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) static void dummy(struct irq_data *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) /* This is used for everything else than the timer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) static struct irq_chip normal_irq_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	.name = "SIGIO",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	.irq_disable = dummy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	.irq_enable = dummy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	.irq_ack = dummy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	.irq_mask = dummy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	.irq_unmask = dummy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) static struct irq_chip SIGVTALRM_irq_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	.name = "SIGVTALRM",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	.irq_disable = dummy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	.irq_enable = dummy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	.irq_ack = dummy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	.irq_mask = dummy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	.irq_unmask = dummy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) void __init init_IRQ(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	irq_set_chip_and_handler(TIMER_IRQ, &SIGVTALRM_irq_type, handle_edge_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	for (i = 1; i < NR_IRQS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		irq_set_chip_and_handler(i, &normal_irq_type, handle_edge_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	/* Initialize EPOLL Loop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	os_setup_epoll();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)  * IRQ stack entry and exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)  * Unlike i386, UML doesn't receive IRQs on the normal kernel stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)  * and switch over to the IRQ stack after some preparation.  We use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)  * sigaltstack to receive signals on a separate stack from the start.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)  * These two functions make sure the rest of the kernel won't be too
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)  * upset by being on a different stack.  The IRQ stack has a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)  * thread_info structure at the bottom so that current et al continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)  * to work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)  * to_irq_stack copies the current task's thread_info to the IRQ stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)  * thread_info and sets the tasks's stack to point to the IRQ stack.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)  * from_irq_stack copies the thread_info struct back (flags may have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)  * been modified) and resets the task's stack pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)  * Tricky bits -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)  * What happens when two signals race each other?  UML doesn't block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)  * signals with sigprocmask, SA_DEFER, or sa_mask, so a second signal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)  * could arrive while a previous one is still setting up the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)  * thread_info.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)  * There are three cases -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)  *     The first interrupt on the stack - sets up the thread_info and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)  * handles the interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)  *     A nested interrupt interrupting the copying of the thread_info -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)  * can't handle the interrupt, as the stack is in an unknown state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)  *     A nested interrupt not interrupting the copying of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)  * thread_info - doesn't do any setup, just handles the interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)  * The first job is to figure out whether we interrupted stack setup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)  * This is done by xchging the signal mask with thread_info->pending.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)  * If the value that comes back is zero, then there is no setup in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)  * progress, and the interrupt can be handled.  If the value is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)  * non-zero, then there is stack setup in progress.  In order to have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)  * the interrupt handled, we leave our signal in the mask, and it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)  * be handled by the upper handler after it has set up the stack.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)  * Next is to figure out whether we are the outer handler or a nested
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)  * one.  As part of setting up the stack, thread_info->real_thread is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)  * set to non-NULL (and is reset to NULL on exit).  This is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)  * nesting indicator.  If it is non-NULL, then the stack is already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)  * set up and the handler can run.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) static unsigned long pending_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) unsigned long to_irq_stack(unsigned long *mask_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	struct thread_info *ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	unsigned long mask, old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	int nested;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	mask = xchg(&pending_mask, *mask_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	if (mask != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		 * If any interrupts come in at this point, we want to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		 * make sure that their bits aren't lost by our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		 * putting our bit in.  So, this loop accumulates bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 		 * until xchg returns the same value that we put in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		 * When that happens, there were no new interrupts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		 * and pending_mask contains a bit for each interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		 * that came in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		old = *mask_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 			old |= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 			mask = xchg(&pending_mask, old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		} while (mask != old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	ti = current_thread_info();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	nested = (ti->real_thread != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	if (!nested) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		struct task_struct *task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		struct thread_info *tti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 		task = cpu_tasks[ti->cpu].task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 		tti = task_thread_info(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 		*ti = *tti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 		ti->real_thread = tti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 		task->stack = ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	mask = xchg(&pending_mask, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	*mask_out |= mask | nested;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) unsigned long from_irq_stack(int nested)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	struct thread_info *ti, *to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	unsigned long mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	ti = current_thread_info();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	pending_mask = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	to = ti->real_thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	current->stack = to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	ti->real_thread = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	*to = *ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	mask = xchg(&pending_mask, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	return mask & ~1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)