2002-09-26 05:01:54 +04:00
|
|
|
/*
|
|
|
|
** Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
|
|
|
|
** Distributed under the terms of the NewOS License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <kernel.h>
|
|
|
|
#include <int.h>
|
|
|
|
#include <debug.h>
|
2002-10-30 02:07:06 +03:00
|
|
|
#include <malloc.h>
|
2002-09-26 05:01:54 +04:00
|
|
|
#include <smp.h>
|
|
|
|
#include <arch/int.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stage2.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2002-10-29 06:42:40 +03:00
|
|
|
#include <kqueue.h>
|
2002-09-26 05:01:54 +04:00
|
|
|
|
|
|
|
#define NUM_IO_VECTORS 256
|
|
|
|
|
|
|
|
struct io_handler {
|
2002-10-29 06:42:40 +03:00
|
|
|
struct io_handler *next;
|
|
|
|
struct io_handler *prev;
|
|
|
|
interrupt_handler func;
|
|
|
|
void *data;
|
2002-09-26 05:01:54 +04:00
|
|
|
};
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-26 05:01:54 +04:00
|
|
|
struct io_vector {
|
2002-10-29 06:42:40 +03:00
|
|
|
struct io_handler handler_list;
|
|
|
|
spinlock vector_lock;
|
2002-09-26 05:01:54 +04:00
|
|
|
};
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-19 20:07:36 +04:00
|
|
|
static struct io_vector *io_vectors = NULL;
|
2002-07-18 19:03:13 +04:00
|
|
|
|
2002-09-26 05:01:54 +04:00
|
|
|
|
2002-10-26 20:13:36 +04:00
|
|
|
cpu_status
|
|
|
|
disable_interrupts()
|
|
|
|
{
|
|
|
|
return arch_int_disable_interrupts();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
restore_interrupts(cpu_status status)
|
|
|
|
{
|
|
|
|
arch_int_restore_interrupts(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-18 19:03:13 +04:00
|
|
|
int
|
2002-09-26 05:01:54 +04:00
|
|
|
int_init(kernel_args *ka)
|
|
|
|
{
|
|
|
|
dprintf("init_int_handlers: entry\n");
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-26 05:01:54 +04:00
|
|
|
return arch_int_init(ka);
|
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-18 19:03:13 +04:00
|
|
|
|
|
|
|
int
|
2002-09-26 05:01:54 +04:00
|
|
|
int_init2(kernel_args *ka)
|
|
|
|
{
|
2002-10-30 02:07:06 +03:00
|
|
|
io_vectors = (struct io_vector *)malloc(sizeof(struct io_vector) * NUM_IO_VECTORS);
|
2002-09-26 05:01:54 +04:00
|
|
|
if (io_vectors == NULL)
|
|
|
|
panic("int_init2: could not create io vector table!\n");
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-26 05:01:54 +04:00
|
|
|
memset(io_vectors, 0, sizeof(struct io_vector) * NUM_IO_VECTORS);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-26 05:01:54 +04:00
|
|
|
return arch_int_init2(ka);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** This function is used internally to install a handler on the given vector.
|
|
|
|
* NB this does NOT take an IRQ, but a system interrupt value.
|
|
|
|
* As this is intended for system use this function does NOT call
|
|
|
|
* arch_int_enable_io_interrupt() as it only works for IRQ values
|
|
|
|
*/
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-26 05:01:54 +04:00
|
|
|
long
|
|
|
|
install_interrupt_handler(long vector, interrupt_handler handler, void *data)
|
2002-07-19 22:33:41 +04:00
|
|
|
{
|
2002-07-19 20:07:36 +04:00
|
|
|
struct io_handler *io = NULL;
|
2002-07-19 22:33:41 +04:00
|
|
|
int state;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-26 05:01:54 +04:00
|
|
|
if (vector < 0 || vector >= NUM_IO_VECTORS)
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
2002-07-19 20:07:36 +04:00
|
|
|
/* find the chain of handlers for this irq.
|
|
|
|
* NB there can be multiple handlers for the same IRQ, especially for
|
|
|
|
* PCI drivers. Where we have multiple handlers we will call each in turn
|
|
|
|
* until one returns a value other than B_UNHANDLED_INTERRUPT.
|
|
|
|
*/
|
2002-10-30 02:07:06 +03:00
|
|
|
io = (struct io_handler *)malloc(sizeof(struct io_handler));
|
2002-09-26 05:01:54 +04:00
|
|
|
if (io == NULL)
|
|
|
|
return ENOMEM;
|
2002-10-30 02:07:06 +03:00
|
|
|
|
2002-09-26 05:01:54 +04:00
|
|
|
io->func = handler;
|
|
|
|
io->data = data;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-19 20:07:36 +04:00
|
|
|
/* Disable the interrupts, get the spinlock for this irq only
|
|
|
|
* and then insert the handler */
|
2002-07-25 05:05:51 +04:00
|
|
|
state = disable_interrupts();
|
2002-09-26 05:01:54 +04:00
|
|
|
acquire_spinlock(&io_vectors[vector].vector_lock);
|
|
|
|
|
2002-11-17 04:38:35 +03:00
|
|
|
/* The list must be inited before the first item is inserted */
|
|
|
|
if (io_vectors[vector].handler_list.next == NULL)
|
|
|
|
initque(&io_vectors[vector].handler_list);
|
2002-11-17 03:52:43 +03:00
|
|
|
|
2002-07-19 22:33:41 +04:00
|
|
|
insque(io, &io_vectors[vector].handler_list);
|
2002-09-26 05:01:54 +04:00
|
|
|
|
|
|
|
release_spinlock(&io_vectors[vector].vector_lock);
|
|
|
|
restore_interrupts(state);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-19 22:33:41 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-09-26 05:01:54 +04:00
|
|
|
|
|
|
|
/** install a handler to be called when an interrupt is triggered
|
|
|
|
* for the given irq with data as the argument
|
2002-07-19 22:33:41 +04:00
|
|
|
*/
|
2002-09-26 05:01:54 +04:00
|
|
|
|
|
|
|
long
|
|
|
|
install_io_interrupt_handler(long irq, interrupt_handler handler, void *data, ulong flags)
|
|
|
|
{
|
2002-07-19 22:33:41 +04:00
|
|
|
long vector = irq + 0x20;
|
2002-07-19 23:25:37 +04:00
|
|
|
long rv = install_interrupt_handler(vector, handler, data);
|
2002-09-26 05:01:54 +04:00
|
|
|
|
2002-07-19 22:33:41 +04:00
|
|
|
if (rv != 0)
|
|
|
|
return rv;
|
|
|
|
|
2002-09-26 05:01:54 +04:00
|
|
|
/* If we were passed the bit-flag B_NO_ENABLE_COUNTER then
|
2002-07-19 20:07:36 +04:00
|
|
|
* we're being asked to not alter whether the interrupt is set
|
|
|
|
* regardless of setting.
|
|
|
|
*/
|
|
|
|
if ((flags & B_NO_ENABLE_COUNTER) == 0)
|
2002-09-26 05:01:54 +04:00
|
|
|
arch_int_enable_io_interrupt(irq);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-26 05:01:54 +04:00
|
|
|
return 0;
|
2002-07-18 19:03:13 +04:00
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-26 05:01:54 +04:00
|
|
|
|
2002-11-17 04:59:31 +03:00
|
|
|
/** Removes and interrupt handler.
|
|
|
|
* Read the notes for install_interrupt_handler!
|
|
|
|
*/
|
2002-09-26 05:01:54 +04:00
|
|
|
|
|
|
|
long
|
|
|
|
remove_interrupt_handler(long vector, interrupt_handler handler, void *data)
|
|
|
|
{
|
|
|
|
struct io_handler *io = NULL;
|
2002-11-17 04:59:31 +03:00
|
|
|
long status = EINVAL;
|
2002-09-26 05:01:54 +04:00
|
|
|
int state;
|
|
|
|
|
|
|
|
if (vector < 0 || vector >= NUM_IO_VECTORS)
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
2002-07-19 22:33:41 +04:00
|
|
|
/* lock the structures down so it is not modified while we search */
|
2002-09-26 05:01:54 +04:00
|
|
|
state = disable_interrupts();
|
|
|
|
acquire_spinlock(&io_vectors[vector].vector_lock);
|
2002-07-19 20:07:36 +04:00
|
|
|
|
|
|
|
/* loop through the available handlers and try to find a match.
|
2002-09-26 05:01:54 +04:00
|
|
|
* We go forward through the list but this means we start with the
|
2002-07-19 20:07:36 +04:00
|
|
|
* most recently added handlers.
|
|
|
|
*/
|
2002-07-19 22:33:41 +04:00
|
|
|
for (io = io_vectors[vector].handler_list.next;
|
|
|
|
io != &io_vectors[vector].handler_list;
|
2002-09-26 05:01:54 +04:00
|
|
|
io = io->next) {
|
|
|
|
/* we have to match both function and data */
|
2002-07-19 23:25:37 +04:00
|
|
|
if (io->func == handler && io->data == data) {
|
|
|
|
remque(io);
|
2002-11-17 04:59:31 +03:00
|
|
|
status = B_OK;
|
2002-07-19 23:25:37 +04:00
|
|
|
break;
|
|
|
|
}
|
2002-09-26 05:01:54 +04:00
|
|
|
}
|
2002-07-18 19:03:13 +04:00
|
|
|
|
2002-07-19 23:25:37 +04:00
|
|
|
/* to finish we need to release our locks and return
|
|
|
|
* the value rv
|
|
|
|
*/
|
2002-09-26 05:01:54 +04:00
|
|
|
release_spinlock(&io_vectors[vector].vector_lock);
|
|
|
|
restore_interrupts(state);
|
2002-07-18 19:03:13 +04:00
|
|
|
|
2002-11-17 04:59:31 +03:00
|
|
|
/* if the handler could be found and removed, we still have to free it */
|
|
|
|
if (status == B_OK)
|
|
|
|
free(io);
|
|
|
|
|
|
|
|
return status;
|
2002-07-19 22:33:41 +04:00
|
|
|
}
|
|
|
|
|
2002-09-26 05:01:54 +04:00
|
|
|
|
|
|
|
/** remove an interrupt handler previously inserted */
|
|
|
|
|
|
|
|
long
|
|
|
|
remove_io_interrupt_handler(long irq, interrupt_handler handler, void *data)
|
|
|
|
{
|
2002-07-19 22:33:41 +04:00
|
|
|
long vector = irq + 0x20;
|
|
|
|
long rv = remove_interrupt_handler(vector, handler, data);
|
2002-09-26 05:01:54 +04:00
|
|
|
|
2002-07-19 22:33:41 +04:00
|
|
|
if (rv < 0)
|
|
|
|
return rv;
|
2002-07-18 19:03:13 +04:00
|
|
|
|
2002-07-19 22:33:41 +04:00
|
|
|
/* Check if we need to disable interrupts... */
|
2002-09-26 05:01:54 +04:00
|
|
|
if (io_vectors[vector].handler_list.next != &io_vectors[vector].handler_list)
|
|
|
|
arch_int_disable_io_interrupt(irq);
|
2002-07-19 22:33:41 +04:00
|
|
|
|
|
|
|
return 0;
|
2002-07-18 19:03:13 +04:00
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-26 05:01:54 +04:00
|
|
|
|
|
|
|
/** actually process an interrupt via the handlers registered for that
|
|
|
|
* vector (irq)
|
2002-07-19 20:07:36 +04:00
|
|
|
*/
|
2002-09-26 05:01:54 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
int_io_interrupt_handler(int vector)
|
2002-11-17 04:59:31 +03:00
|
|
|
{
|
|
|
|
int status = B_UNHANDLED_INTERRUPT;
|
|
|
|
struct io_handler *io;
|
|
|
|
|
|
|
|
acquire_spinlock(&io_vectors[vector].vector_lock);
|
|
|
|
|
|
|
|
// The list can be empty, or not initialized at this place
|
|
|
|
if (io_vectors[vector].handler_list.next == &io_vectors[vector].handler_list
|
|
|
|
|| io_vectors[vector].handler_list.next == NULL) {
|
|
|
|
dprintf("unhandled io interrupt %d\n", vector);
|
|
|
|
return B_UNHANDLED_INTERRUPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop through the list of handlers.
|
|
|
|
* each handler returns as follows...
|
|
|
|
* - B_UNHANDLED_INTERRUPT, the interrupt wasn't processed by the
|
|
|
|
* fucntion, so try the next available.
|
|
|
|
* - B_HANDLED_INTERRUPT, the interrupt has been handled and no further
|
|
|
|
* attention is required
|
|
|
|
* - B_INVOKE_SCHEDULER, the interrupt has been handled, but the function wants
|
|
|
|
* the scheduler to be invoked
|
|
|
|
*
|
|
|
|
* This is a change of behaviour from newos where every handler registered
|
|
|
|
* be called, even if the interrupt had been "handled" by a previous
|
|
|
|
* function.
|
|
|
|
* The logic now is that if there are no handlers then we return
|
|
|
|
* B_UNHANDLED_INTERRUPT and let the system do as it will.
|
|
|
|
* When we have the first function that claims to have "handled" the
|
|
|
|
* interrupt, by returning B_HANDLED_... or B_INVOKE_SCHEDULER we simply
|
|
|
|
* stop calling further handlers and return the value from that
|
|
|
|
* handler.
|
|
|
|
* This may not be correct but appears to be what BeOS did and seems
|
|
|
|
* right.
|
|
|
|
*
|
|
|
|
* ToDo: we might want to reenable calling all registered handlers depending
|
|
|
|
* on a flag somewhere, so that we can deal with buggy drivers
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (io = io_vectors[vector].handler_list.next;
|
|
|
|
io != &io_vectors[vector].handler_list; // Are we already at the end of the list?
|
|
|
|
io = io->next) {
|
|
|
|
if ((status = io->func(io->data)) != B_UNHANDLED_INTERRUPT)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
release_spinlock(&io_vectors[vector].vector_lock);
|
|
|
|
|
|
|
|
return status;
|
2002-07-10 19:48:19 +04:00
|
|
|
}
|
|
|
|
|