Skip to content
Snippets Groups Projects
Commit 65e8abb4 authored by Jacob Lorentzon's avatar Jacob Lorentzon
Browse files

Allow multiple processes to share IRQs.

I haven't been able to receive xhc interrupt anyway.
parent 53570f01
No related branches found
No related tags found
1 merge request!115Extend the IRQ scheme to allow allocation of all available interrupt vectors.
use core::{mem, str}; use core::{mem, str};
use core::sync::atomic::Ordering; use core::sync::atomic::{AtomicUsize, Ordering};
use spin::Mutex; use spin::{Mutex, RwLock};
use alloc::collections::BTreeMap;
use crate::event; use crate::event;
use crate::interrupt::irq::acknowledge; use crate::interrupt::irq::acknowledge;
...@@ -12,22 +14,42 @@ use crate::syscall::scheme::Scheme; ...@@ -12,22 +14,42 @@ use crate::syscall::scheme::Scheme;
pub static IRQ_SCHEME_ID: AtomicSchemeId = AtomicSchemeId::default(); pub static IRQ_SCHEME_ID: AtomicSchemeId = AtomicSchemeId::default();
/// IRQ queues /// IRQ queues
static ACKS: Mutex<[usize; 16]> = Mutex::new([0; 16]);
static COUNTS: Mutex<[usize; 16]> = Mutex::new([0; 16]); static COUNTS: Mutex<[usize; 16]> = Mutex::new([0; 16]);
static HANDLES: RwLock<Option<BTreeMap<usize, Handle>>> = RwLock::new(None);
/// Add to the input queue /// Add to the input queue
#[no_mangle] #[no_mangle]
pub extern fn irq_trigger(irq: u8) { pub extern fn irq_trigger(irq: u8) {
COUNTS.lock()[irq as usize] += 1; COUNTS.lock()[irq as usize] += 1;
event::trigger(IRQ_SCHEME_ID.load(Ordering::SeqCst), irq as usize, EVENT_READ);
let guard = HANDLES.read();
if let Some(handles) = guard.as_ref() {
for (fd, _) in handles.iter().filter(|(_, handle)| handle.irq == irq) {
event::trigger(IRQ_SCHEME_ID.load(Ordering::SeqCst), *fd, EVENT_READ);
}
} else {
println!("Calling IRQ without triggering");
}
} }
pub struct IrqScheme; struct Handle {
ack: AtomicUsize,
irq: u8,
}
pub struct IrqScheme {
next_fd: AtomicUsize,
}
impl IrqScheme { impl IrqScheme {
pub fn new(scheme_id: SchemeId) -> IrqScheme { pub fn new(scheme_id: SchemeId) -> IrqScheme {
IRQ_SCHEME_ID.store(scheme_id, Ordering::SeqCst); IRQ_SCHEME_ID.store(scheme_id, Ordering::SeqCst);
IrqScheme
*HANDLES.write() = Some(BTreeMap::new());
IrqScheme {
next_fd: AtomicUsize::new(0),
}
} }
} }
...@@ -39,7 +61,9 @@ impl Scheme for IrqScheme { ...@@ -39,7 +61,9 @@ impl Scheme for IrqScheme {
let id = path_str.parse::<usize>().or(Err(Error::new(ENOENT)))?; let id = path_str.parse::<usize>().or(Err(Error::new(ENOENT)))?;
if id < COUNTS.lock().len() { if id < COUNTS.lock().len() {
Ok(id) let fd = self.next_fd.fetch_add(1, Ordering::Relaxed);
HANDLES.write().as_mut().unwrap().insert(fd, Handle { ack: AtomicUsize::new(0), irq: id as u8 });
Ok(fd)
} else { } else {
Err(Error::new(ENOENT)) Err(Error::new(ENOENT))
} }
...@@ -51,9 +75,11 @@ impl Scheme for IrqScheme { ...@@ -51,9 +75,11 @@ impl Scheme for IrqScheme {
fn read(&self, file: usize, buffer: &mut [u8]) -> Result<usize> { fn read(&self, file: usize, buffer: &mut [u8]) -> Result<usize> {
// Ensures that the length of the buffer is larger than the size of a usize // Ensures that the length of the buffer is larger than the size of a usize
if buffer.len() >= mem::size_of::<usize>() { if buffer.len() >= mem::size_of::<usize>() {
let ack = ACKS.lock()[file]; let handles_guard = HANDLES.read();
let current = COUNTS.lock()[file]; let handle = &handles_guard.as_ref().unwrap().get(&file).ok_or(Error::new(EBADF))?;
if ack != current {
let current = COUNTS.lock()[handle.irq as usize];
if handle.ack.load(Ordering::SeqCst) != current {
// Safe if the length of the buffer is larger than the size of a usize // Safe if the length of the buffer is larger than the size of a usize
assert!(buffer.len() >= mem::size_of::<usize>()); assert!(buffer.len() >= mem::size_of::<usize>());
unsafe { *(buffer.as_mut_ptr() as *mut usize) = current; } unsafe { *(buffer.as_mut_ptr() as *mut usize) = current; }
...@@ -69,11 +95,16 @@ impl Scheme for IrqScheme { ...@@ -69,11 +95,16 @@ impl Scheme for IrqScheme {
fn write(&self, file: usize, buffer: &[u8]) -> Result<usize> { fn write(&self, file: usize, buffer: &[u8]) -> Result<usize> {
if buffer.len() >= mem::size_of::<usize>() { if buffer.len() >= mem::size_of::<usize>() {
assert!(buffer.len() >= mem::size_of::<usize>()); assert!(buffer.len() >= mem::size_of::<usize>());
let handles_guard = HANDLES.read();
let handle = &handles_guard.as_ref().unwrap().get(&file).ok_or(Error::new(EBADF))?;
let ack = unsafe { *(buffer.as_ptr() as *const usize) }; let ack = unsafe { *(buffer.as_ptr() as *const usize) };
let current = COUNTS.lock()[file]; let current = COUNTS.lock()[handle.irq as usize];
if ack == current { if ack == current {
ACKS.lock()[file] = ack; handle.ack.store(ack, Ordering::SeqCst);
unsafe { acknowledge(file); } unsafe { acknowledge(handle.irq as usize); }
Ok(mem::size_of::<usize>()) Ok(mem::size_of::<usize>())
} else { } else {
Ok(0) Ok(0)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment