Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions components/irq-framework/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::{
sync::atomic::AtomicBool,
};

use crate::{CpuId, CpuMask, IrqRequest, IrqScope, RawIrqHandler};
use crate::{AutoEnable, CpuId, CpuMask, IrqRequest, IrqScope, RawIrqHandler};

pub(crate) struct Action {
pub(crate) id: u64,
Expand All @@ -29,7 +29,7 @@ impl Action {
handler: request.handler,
data: request.data,
scope: request.scope,
enabled: AtomicBool::new(false),
enabled: AtomicBool::new(request.auto_enable == AutoEnable::Yes),
detached: AtomicBool::new(false),
pending_enable: UnsafeCell::new(CpuMask::empty()),
next: ptr::null_mut(),
Expand Down
143 changes: 138 additions & 5 deletions components/irq-framework/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use core::{
};

use crate::{
AutoEnable, CpuId, IrqContext, IrqError, IrqHandle, IrqNumber, IrqOps, IrqOutcome, IrqRequest,
IrqReturn, IrqScope, IrqStatus,
CpuId, IrqContext, IrqError, IrqHandle, IrqNumber, IrqOps, IrqOutcome, IrqRequest, IrqReturn,
IrqScope, IrqStatus,
action::Action,
descriptor::{Descriptor, action_matches_cpu, recompute_scope_line_desired},
lock::MetadataLock,
Expand Down Expand Up @@ -51,24 +51,26 @@ impl<O: IrqOps> Registry<O> {
pub fn request(&self, irq: IrqNumber, request: IrqRequest) -> Result<IrqHandle, IrqError> {
self.validate_request(&request)?;

let snapshot = self.snapshot_and_disable_scope_line(irq, request.scope)?;
let id = self.next_id.fetch_add(1, Ordering::Relaxed);
let action = Box::new(Action::new(id, &request));
let action = Box::into_raw(action);
let irq_state = self.lock.lock(&self.ops);
let result = self.insert_action_locked(irq, &request, action);
self.lock.unlock(&self.ops, irq_state);

let restore_result = self.restore_scope_line_snapshot(irq, request.scope, &snapshot);

if let Err(err) = result {
unsafe {
drop(Box::from_raw(action));
}
let _ = restore_result;
return Err(err);
}

let handle = IrqHandle { irq, id };
if request.auto_enable == AutoEnable::Yes
&& let Err(err) = self.enable(handle)
{
if let Err(err) = restore_result {
self.drop_detached_action(handle);
return Err(err);
}
Expand Down Expand Up @@ -266,6 +268,7 @@ impl<O: IrqOps> Registry<O> {
(*action).next = descriptor.head;
}
descriptor.head = action;
recompute_scope_line_desired(descriptor, request.scope);
Ok(())
}

Expand Down Expand Up @@ -409,6 +412,97 @@ impl<O: IrqOps> Registry<O> {
}
}

fn snapshot_and_disable_scope_line(
&self,
irq: IrqNumber,
scope: IrqScope,
) -> Result<LineStateSnapshot, IrqError> {
let mut snapshot = LineStateSnapshot::new(scope);
match scope {
IrqScope::Global => {
snapshot.global = self.snapshot_and_disable_line(irq, None)?;
}
IrqScope::PerCpu { cpus } => {
for cpu in cpus.iter() {
if !self.ops.cpu_online(cpu) {
continue;
}
match self.snapshot_and_disable_line(irq, Some(cpu)) {
Ok(was_enabled) => snapshot.percpu.push((cpu, was_enabled)),
Err(err) => {
let _ = self.restore_scope_line_snapshot(irq, scope, &snapshot);
return Err(err);
}
}
}
}
}
Ok(snapshot)
}

fn snapshot_and_disable_line(
&self,
irq: IrqNumber,
cpu: Option<CpuId>,
) -> Result<bool, IrqError> {
let was_enabled = self.controller_line_enabled(irq, cpu)?;
self.set_controller_enabled(irq, cpu, false)?;
self.set_line_applied_if_present(irq, cpu, false)?;
Ok(was_enabled)
}

fn restore_scope_line_snapshot(
&self,
irq: IrqNumber,
scope: IrqScope,
snapshot: &LineStateSnapshot,
) -> Result<(), IrqError> {
match scope {
IrqScope::Global => {
self.restore_line_snapshot(irq, None, snapshot.global)?;
}
IrqScope::PerCpu { cpus } => {
for cpu in cpus.iter() {
if let Some((_, was_enabled)) = snapshot
.percpu
.iter()
.find(|(snapshot_cpu, _)| *snapshot_cpu == cpu)
{
self.restore_line_snapshot(irq, Some(cpu), *was_enabled)?;
}
}
}
}
Ok(())
}

fn restore_line_snapshot(
&self,
irq: IrqNumber,
cpu: Option<CpuId>,
was_enabled: bool,
) -> Result<(), IrqError> {
if was_enabled {
self.set_controller_enabled(irq, cpu, true)?;
}
self.set_line_applied_if_present(irq, cpu, was_enabled)?;
Ok(())
}

fn controller_line_enabled(
&self,
irq: IrqNumber,
cpu: Option<CpuId>,
) -> Result<bool, IrqError> {
match self.ops.is_enabled(irq, cpu) {
Ok(enabled) => Ok(enabled),
Err(IrqError::Unsupported) => {
Ok(self.framework_line_enabled(irq, cpu).unwrap_or(false))
}
Err(err) => Err(err),
}
}

fn apply_line_state(&self, irq: IrqNumber, cpu: Option<CpuId>) -> Result<(), IrqError> {
loop {
if let Some(cpu) = cpu
Expand Down Expand Up @@ -552,6 +646,28 @@ impl<O: IrqOps> Registry<O> {
result
}

fn set_line_applied_if_present(
&self,
irq: IrqNumber,
cpu: Option<CpuId>,
enabled: bool,
) -> Result<(), IrqError> {
let irq_state = self.lock.lock(&self.ops);
let result = {
let state = unsafe { &mut *self.state.get() };
if let Some(descriptor) = state
.descriptors
.iter_mut()
.find(|descriptor| descriptor.irq == irq)
{
descriptor.set_line_applied(cpu, enabled);
}
Ok(())
};
self.lock.unlock(&self.ops, irq_state);
result
}

fn framework_line_enabled(&self, irq: IrqNumber, cpu: Option<CpuId>) -> Result<bool, IrqError> {
let irq_state = self.lock.lock(&self.ops);
let result = (|| {
Expand Down Expand Up @@ -581,6 +697,23 @@ impl<O: IrqOps> Registry<O> {
}
}

struct LineStateSnapshot {
global: bool,
percpu: Vec<(CpuId, bool)>,
}

impl LineStateSnapshot {
fn new(scope: IrqScope) -> Self {
Self {
global: false,
percpu: match scope {
IrqScope::Global => Vec::new(),
IrqScope::PerCpu { cpus } => Vec::with_capacity(cpus.iter().count()),
},
}
}
}

struct DispatchGuard<'a, O: IrqOps> {
registry: &'a Registry<O>,
irq: IrqNumber,
Expand Down
Loading