1e2841179SLoGin use core::{ 2e2841179SLoGin any::Any, 3e2841179SLoGin fmt::Debug, 4e2841179SLoGin sync::atomic::{AtomicI64, Ordering}, 5e2841179SLoGin }; 63bc96fa4SLoGin 73bc96fa4SLoGin use alloc::{ 8196b75dcSLoGin collections::{btree_map, BTreeMap}, 9196b75dcSLoGin string::{String, ToString}, 103bc96fa4SLoGin sync::{Arc, Weak}, 113bc96fa4SLoGin vec::Vec, 123bc96fa4SLoGin }; 133bc96fa4SLoGin use system_error::SystemError; 143bc96fa4SLoGin 153bc96fa4SLoGin use crate::{ 16e2841179SLoGin arch::{interrupt::TrapFrame, CurrentIrqArch}, 173bc96fa4SLoGin driver::base::{ 183bc96fa4SLoGin device::DeviceId, 193bc96fa4SLoGin kobject::{KObjType, KObject, KObjectState, LockedKObjectState}, 203bc96fa4SLoGin kset::KSet, 213bc96fa4SLoGin }, 223bc96fa4SLoGin filesystem::kernfs::KernFSInode, 233bc96fa4SLoGin libs::{ 24*338f6903SLoGin cpumask::CpuMask, 25e2841179SLoGin mutex::{Mutex, MutexGuard}, 26e2841179SLoGin rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard}, 273bc96fa4SLoGin spinlock::{SpinLock, SpinLockGuard}, 283bc96fa4SLoGin }, 29*338f6903SLoGin mm::percpu::PerCpuVar, 303bc96fa4SLoGin process::ProcessControlBlock, 31e2841179SLoGin sched::completion::Completion, 32*338f6903SLoGin smp::cpu::smp_cpu_manager, 333bc96fa4SLoGin }; 343bc96fa4SLoGin 353bc96fa4SLoGin use super::{ 363bc96fa4SLoGin dummychip::no_irq_chip, 373bc96fa4SLoGin handle::bad_irq_handler, 38*338f6903SLoGin irqchip::IrqChip, 39e2841179SLoGin irqdata::{IrqCommonData, IrqData, IrqHandlerData, IrqLineStatus, IrqStatus}, 40196b75dcSLoGin sysfs::{irq_sysfs_del, IrqKObjType}, 413bc96fa4SLoGin HardwareIrqNumber, InterruptArch, IrqNumber, 423bc96fa4SLoGin }; 433bc96fa4SLoGin 443bc96fa4SLoGin /// 中断流处理程序 45e2841179SLoGin pub trait IrqFlowHandler: Debug + Send + Sync + Any { 46e2841179SLoGin fn handle(&self, irq_desc: &Arc<IrqDesc>, trap_frame: &mut TrapFrame); 47e2841179SLoGin } 48e2841179SLoGin 49e2841179SLoGin /// 中断处理程序 50e2841179SLoGin pub trait IrqHandler: Debug + Send + Sync + Any { 51e2841179SLoGin fn handle( 52e2841179SLoGin &self, 53e2841179SLoGin irq: IrqNumber, 54e2841179SLoGin static_data: Option<&dyn IrqHandlerData>, 55e2841179SLoGin dynamic_data: Option<Arc<dyn IrqHandlerData>>, 56e2841179SLoGin ) -> Result<IrqReturn, SystemError>; 57e2841179SLoGin } 58e2841179SLoGin 59e2841179SLoGin /// 中断处理函数返回值 60e2841179SLoGin /// 61e2841179SLoGin /// 用于指示中断处理函数是否处理了中断 62e2841179SLoGin #[derive(Debug, Clone, Copy, PartialEq, Eq)] 63e2841179SLoGin pub enum IrqReturn { 64e2841179SLoGin /// 中断未被处理 65e2841179SLoGin NotHandled, 66e2841179SLoGin /// 中断已被处理 67e2841179SLoGin Handled, 68e2841179SLoGin /// 中断已被处理,并且需要唤醒中断线程 69e2841179SLoGin WakeThread, 703bc96fa4SLoGin } 713bc96fa4SLoGin 723bc96fa4SLoGin /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/include/linux/irqdesc.h#55 733bc96fa4SLoGin #[derive(Debug)] 743bc96fa4SLoGin pub struct IrqDesc { 753bc96fa4SLoGin inner: SpinLock<InnerIrqDesc>, 763bc96fa4SLoGin 77e2841179SLoGin handler: RwLock<Option<&'static dyn IrqFlowHandler>>, 78e2841179SLoGin /// 一个用于串行化 request_irq()和free_irq() 的互斥锁 79e2841179SLoGin request_mutex: Mutex<()>, 803bc96fa4SLoGin kobj_state: LockedKObjectState, 81e2841179SLoGin /// 当前描述符内正在运行的中断线程数 82e2841179SLoGin threads_active: AtomicI64, 833bc96fa4SLoGin } 843bc96fa4SLoGin 853bc96fa4SLoGin impl IrqDesc { 863bc96fa4SLoGin #[inline(never)] 873bc96fa4SLoGin pub fn new(irq: IrqNumber, name: Option<String>, irqd_flags: IrqStatus) -> Arc<Self> { 883bc96fa4SLoGin // 初始化的过程参考 https://code.dragonos.org.cn/xref/linux-6.1.9/kernel/irq/irqdesc.c#392 893bc96fa4SLoGin let common_data = Arc::new(IrqCommonData::new()); 903bc96fa4SLoGin let irq_data = Arc::new(IrqData::new( 913bc96fa4SLoGin irq, 923bc96fa4SLoGin HardwareIrqNumber::new(irq.data()), 933bc96fa4SLoGin common_data.clone(), 943bc96fa4SLoGin no_irq_chip(), 953bc96fa4SLoGin )); 963bc96fa4SLoGin 973bc96fa4SLoGin irq_data.irqd_set(IrqStatus::IRQD_IRQ_DISABLED); 98e2841179SLoGin common_data.insert_status(IrqStatus::IRQD_IRQ_MASKED); 993bc96fa4SLoGin 1003bc96fa4SLoGin let irq_desc = IrqDesc { 1013bc96fa4SLoGin inner: SpinLock::new(InnerIrqDesc { 102*338f6903SLoGin percpu_affinity: None, 103*338f6903SLoGin percpu_enabled: None, 1043bc96fa4SLoGin common_data, 1053bc96fa4SLoGin irq_data, 106196b75dcSLoGin desc_internal_state: IrqDescState::empty(), 107e2841179SLoGin line_status: IrqLineStatus::empty(), 1083bc96fa4SLoGin actions: Vec::new(), 1093bc96fa4SLoGin name, 1103bc96fa4SLoGin parent_irq: None, 1113bc96fa4SLoGin depth: 1, 1123bc96fa4SLoGin wake_depth: 0, 1133bc96fa4SLoGin kern_inode: None, 1143bc96fa4SLoGin kset: None, 1153bc96fa4SLoGin parent_kobj: None, 1163bc96fa4SLoGin }), 117e2841179SLoGin request_mutex: Mutex::new(()), 118e2841179SLoGin handler: RwLock::new(None), 1193bc96fa4SLoGin kobj_state: LockedKObjectState::new(Some(KObjectState::INITIALIZED)), 120e2841179SLoGin threads_active: AtomicI64::new(0), 1213bc96fa4SLoGin }; 1223bc96fa4SLoGin 1233bc96fa4SLoGin irq_desc.set_handler(bad_irq_handler()); 1243bc96fa4SLoGin irq_desc.inner().irq_data.irqd_set(irqd_flags); 1253bc96fa4SLoGin 1263bc96fa4SLoGin return Arc::new(irq_desc); 1273bc96fa4SLoGin } 1283bc96fa4SLoGin 129e2841179SLoGin /// 返回当前活跃的中断线程数量 130e2841179SLoGin #[allow(dead_code)] 131e2841179SLoGin pub fn threads_active(&self) -> i64 { 132e2841179SLoGin self.threads_active.load(Ordering::SeqCst) 1333bc96fa4SLoGin } 1343bc96fa4SLoGin 135e2841179SLoGin /// 增加当前活跃的中断线程数量, 返回增加前的值 136e2841179SLoGin pub fn inc_threads_active(&self) -> i64 { 137e2841179SLoGin self.threads_active.fetch_add(1, Ordering::SeqCst) 138e2841179SLoGin } 139e2841179SLoGin 140e2841179SLoGin /// 减少当前活跃的中断线程数量, 返回减少前的值 141e2841179SLoGin #[allow(dead_code)] 142e2841179SLoGin pub fn dec_threads_active(&self) -> i64 { 143e2841179SLoGin self.threads_active.fetch_sub(1, Ordering::SeqCst) 144e2841179SLoGin } 145e2841179SLoGin 146e2841179SLoGin pub fn set_handler(&self, handler: &'static dyn IrqFlowHandler) { 147e2841179SLoGin self.chip_bus_lock(); 148e2841179SLoGin let mut guard = self.handler.write_irqsave(); 149e2841179SLoGin *guard = Some(handler); 150e2841179SLoGin self.chip_bus_sync_unlock(); 151e2841179SLoGin } 152e2841179SLoGin 153*338f6903SLoGin /// 设置中断处理程序(不对desc->inner) 154*338f6903SLoGin /// 155*338f6903SLoGin /// 156*338f6903SLoGin /// ## Safety 157*338f6903SLoGin /// 158*338f6903SLoGin /// 需要保证irq_data和chip是当前irqdesc的 159*338f6903SLoGin pub fn set_handler_no_lock_inner( 160*338f6903SLoGin &self, 161*338f6903SLoGin handler: &'static dyn IrqFlowHandler, 162*338f6903SLoGin irq_data: &Arc<IrqData>, 163*338f6903SLoGin chip: &Arc<dyn IrqChip>, 164*338f6903SLoGin ) { 165*338f6903SLoGin chip.irq_bus_lock(irq_data).ok(); 166*338f6903SLoGin let mut guard = self.handler.write_irqsave(); 167*338f6903SLoGin *guard = Some(handler); 168*338f6903SLoGin chip.irq_bus_sync_unlock(irq_data).ok(); 169*338f6903SLoGin } 170*338f6903SLoGin 171e2841179SLoGin pub fn handler(&self) -> Option<&'static dyn IrqFlowHandler> { 172e2841179SLoGin let guard = self.handler.read_irqsave(); 173e2841179SLoGin *guard 174e2841179SLoGin } 175e2841179SLoGin 176e2841179SLoGin pub fn inner(&self) -> SpinLockGuard<InnerIrqDesc> { 1773bc96fa4SLoGin self.inner.lock_irqsave() 1783bc96fa4SLoGin } 1793bc96fa4SLoGin 180196b75dcSLoGin pub fn actions(&self) -> Vec<Arc<IrqAction>> { 181196b75dcSLoGin self.inner().actions.clone() 182196b75dcSLoGin } 183196b75dcSLoGin 184e2841179SLoGin /// 对中断请求过程加锁 185e2841179SLoGin pub fn request_mutex_lock(&self) -> MutexGuard<()> { 186e2841179SLoGin self.request_mutex.lock() 187e2841179SLoGin } 188e2841179SLoGin 1893bc96fa4SLoGin pub fn irq(&self) -> IrqNumber { 1903bc96fa4SLoGin self.inner().irq_data.irq() 1913bc96fa4SLoGin } 192196b75dcSLoGin 193196b75dcSLoGin pub fn hardware_irq(&self) -> HardwareIrqNumber { 194196b75dcSLoGin self.inner().irq_data.hardware_irq() 195196b75dcSLoGin } 196196b75dcSLoGin 197196b75dcSLoGin pub fn irq_data(&self) -> Arc<IrqData> { 198196b75dcSLoGin self.inner().irq_data.clone() 199196b75dcSLoGin } 200196b75dcSLoGin 201196b75dcSLoGin /// 标记当前irq描述符已经被添加到sysfs 202196b75dcSLoGin pub fn mark_in_sysfs(&self) { 203196b75dcSLoGin self.inner() 204196b75dcSLoGin .desc_internal_state 205196b75dcSLoGin .insert(IrqDescState::IRQS_SYSFS); 206196b75dcSLoGin } 207196b75dcSLoGin 208196b75dcSLoGin pub fn mark_not_in_sysfs(&self) { 209196b75dcSLoGin self.inner() 210196b75dcSLoGin .desc_internal_state 211196b75dcSLoGin .remove(IrqDescState::IRQS_SYSFS); 212196b75dcSLoGin } 213196b75dcSLoGin 214196b75dcSLoGin /// 判断当前描述符是否已经添加到了sysfs 215196b75dcSLoGin pub fn in_sysfs(&self) -> bool { 216196b75dcSLoGin self.inner() 217196b75dcSLoGin .desc_internal_state 218196b75dcSLoGin .contains(IrqDescState::IRQS_SYSFS) 219196b75dcSLoGin } 220196b75dcSLoGin 221196b75dcSLoGin pub fn name(&self) -> Option<String> { 222196b75dcSLoGin self.inner().name.clone() 223196b75dcSLoGin } 224e2841179SLoGin 225e2841179SLoGin pub fn can_request(&self) -> bool { 226e2841179SLoGin self.inner().can_request() 227e2841179SLoGin } 228e2841179SLoGin 229e2841179SLoGin #[allow(dead_code)] 230e2841179SLoGin pub fn set_norequest(&self) { 231e2841179SLoGin self.inner().set_norequest(); 232e2841179SLoGin } 233e2841179SLoGin 234e2841179SLoGin #[allow(dead_code)] 235e2841179SLoGin pub fn clear_norequest(&self) { 236e2841179SLoGin self.inner().clear_norequest(); 237e2841179SLoGin } 238e2841179SLoGin 239e2841179SLoGin pub fn nested_thread(&self) -> bool { 240e2841179SLoGin self.inner().nested_thread() 241e2841179SLoGin } 242e2841179SLoGin 243e2841179SLoGin /// 中断是否可以线程化 244e2841179SLoGin pub fn can_thread(&self) -> bool { 245e2841179SLoGin !self 246e2841179SLoGin .inner() 247e2841179SLoGin .line_status 248e2841179SLoGin .contains(IrqLineStatus::IRQ_NOTHREAD) 249e2841179SLoGin } 250e2841179SLoGin 251e2841179SLoGin pub fn chip_bus_lock(&self) { 252e2841179SLoGin let irq_data = self.inner().irq_data.clone(); 253e2841179SLoGin irq_data 254e2841179SLoGin .chip_info_read_irqsave() 255e2841179SLoGin .chip() 256e2841179SLoGin .irq_bus_lock(&irq_data) 257e2841179SLoGin .ok(); 258e2841179SLoGin } 259e2841179SLoGin 260e2841179SLoGin /// 同步释放低速总线锁 261e2841179SLoGin /// 262e2841179SLoGin /// ## 锁 263e2841179SLoGin /// 264e2841179SLoGin /// 进入此函数时,必须持有低速总线锁,并且desc的inner锁和irqdata的inner锁 265e2841179SLoGin /// 必须已经释放。否则将死锁。 266e2841179SLoGin pub fn chip_bus_sync_unlock(&self) { 267e2841179SLoGin let irq_data = self.inner().irq_data.clone(); 268e2841179SLoGin irq_data 269e2841179SLoGin .chip_info_write_irqsave() 270e2841179SLoGin .chip() 271e2841179SLoGin .irq_bus_sync_unlock(&irq_data) 272e2841179SLoGin .ok(); 273e2841179SLoGin } 274e2841179SLoGin 275*338f6903SLoGin pub fn set_percpu_devid_flags(&self) { 276*338f6903SLoGin self.modify_status( 277*338f6903SLoGin IrqLineStatus::empty(), 278*338f6903SLoGin IrqLineStatus::IRQ_NOAUTOEN 279*338f6903SLoGin | IrqLineStatus::IRQ_PER_CPU 280*338f6903SLoGin | IrqLineStatus::IRQ_NOTHREAD 281*338f6903SLoGin | IrqLineStatus::IRQ_NOPROBE 282*338f6903SLoGin | IrqLineStatus::IRQ_PER_CPU_DEVID, 283*338f6903SLoGin ); 284*338f6903SLoGin } 285*338f6903SLoGin 286e2841179SLoGin pub fn modify_status(&self, clear: IrqLineStatus, set: IrqLineStatus) { 287e2841179SLoGin let mut desc_guard = self.inner(); 288e2841179SLoGin desc_guard.line_status.remove(clear); 289e2841179SLoGin desc_guard.line_status.insert(set); 290e2841179SLoGin 291e2841179SLoGin let mut trigger = desc_guard.common_data().trigger_type(); 292e2841179SLoGin 293e2841179SLoGin desc_guard.common_data().clear_status( 294e2841179SLoGin IrqStatus::IRQD_NO_BALANCING 295e2841179SLoGin | IrqStatus::IRQD_PER_CPU 296e2841179SLoGin | IrqStatus::IRQD_TRIGGER_MASK 297e2841179SLoGin | IrqStatus::IRQD_LEVEL 298e2841179SLoGin | IrqStatus::IRQD_MOVE_PCNTXT, 299e2841179SLoGin ); 300e2841179SLoGin 301e2841179SLoGin if desc_guard 302e2841179SLoGin .line_status 303e2841179SLoGin .contains(IrqLineStatus::IRQ_NO_BALANCING) 304e2841179SLoGin { 305e2841179SLoGin desc_guard 306e2841179SLoGin .common_data() 307e2841179SLoGin .insert_status(IrqStatus::IRQD_NO_BALANCING); 308e2841179SLoGin } 309e2841179SLoGin 310e2841179SLoGin if desc_guard.line_status.contains(IrqLineStatus::IRQ_PER_CPU) { 311e2841179SLoGin desc_guard 312e2841179SLoGin .common_data() 313e2841179SLoGin .insert_status(IrqStatus::IRQD_PER_CPU); 314e2841179SLoGin } 315e2841179SLoGin 316e2841179SLoGin if desc_guard 317e2841179SLoGin .line_status 318e2841179SLoGin .contains(IrqLineStatus::IRQ_MOVE_PCNTXT) 319e2841179SLoGin { 320e2841179SLoGin desc_guard 321e2841179SLoGin .common_data() 322e2841179SLoGin .insert_status(IrqStatus::IRQD_MOVE_PCNTXT); 323e2841179SLoGin } 324e2841179SLoGin 325e2841179SLoGin if desc_guard.line_status.is_level_type() { 326e2841179SLoGin desc_guard 327e2841179SLoGin .common_data() 328e2841179SLoGin .insert_status(IrqStatus::IRQD_LEVEL); 329e2841179SLoGin } 330e2841179SLoGin 331e2841179SLoGin let tmp = desc_guard.line_status.trigger_type(); 332e2841179SLoGin 333e2841179SLoGin if tmp != IrqLineStatus::IRQ_TYPE_NONE { 334e2841179SLoGin trigger = tmp; 335e2841179SLoGin } 336e2841179SLoGin 337e2841179SLoGin desc_guard.common_data().set_trigger_type(trigger); 338e2841179SLoGin } 3393bc96fa4SLoGin } 3403bc96fa4SLoGin 3413bc96fa4SLoGin #[allow(dead_code)] 3423bc96fa4SLoGin #[derive(Debug)] 343e2841179SLoGin pub struct InnerIrqDesc { 3443bc96fa4SLoGin /// per irq and chip data passed down to chip functions 3453bc96fa4SLoGin common_data: Arc<IrqCommonData>, 3463bc96fa4SLoGin irq_data: Arc<IrqData>, 3473bc96fa4SLoGin actions: Vec<Arc<IrqAction>>, 3483bc96fa4SLoGin name: Option<String>, 3493bc96fa4SLoGin parent_irq: Option<IrqNumber>, 3503bc96fa4SLoGin /// nested irq disables 3513bc96fa4SLoGin depth: u32, 3523bc96fa4SLoGin /// nested wake enables 3533bc96fa4SLoGin wake_depth: u32, 354196b75dcSLoGin desc_internal_state: IrqDescState, 355e2841179SLoGin /// 中断线的状态 356e2841179SLoGin line_status: IrqLineStatus, 3573bc96fa4SLoGin 3583bc96fa4SLoGin kern_inode: Option<Arc<KernFSInode>>, 3593bc96fa4SLoGin kset: Option<Arc<KSet>>, 3603bc96fa4SLoGin parent_kobj: Option<Weak<dyn KObject>>, 361*338f6903SLoGin /// per-cpu enabled mask 362*338f6903SLoGin percpu_enabled: Option<CpuMask>, 363*338f6903SLoGin /// per-cpu affinity 364*338f6903SLoGin percpu_affinity: Option<CpuMask>, 365e2841179SLoGin // wait_for_threads: EventWaitQueue 366e2841179SLoGin } 367e2841179SLoGin 368e2841179SLoGin impl InnerIrqDesc { 369e2841179SLoGin pub fn name(&self) -> Option<&String> { 370e2841179SLoGin self.name.as_ref() 371e2841179SLoGin } 372e2841179SLoGin 373*338f6903SLoGin #[allow(dead_code)] 374*338f6903SLoGin pub fn set_name(&mut self, name: Option<String>) { 375*338f6903SLoGin self.name = name; 376*338f6903SLoGin } 377*338f6903SLoGin 378e2841179SLoGin pub fn can_request(&self) -> bool { 379e2841179SLoGin !self.line_status.contains(IrqLineStatus::IRQ_NOREQUEST) 380e2841179SLoGin } 381e2841179SLoGin 382e2841179SLoGin #[allow(dead_code)] 383e2841179SLoGin pub fn set_norequest(&mut self) { 384e2841179SLoGin self.line_status.insert(IrqLineStatus::IRQ_NOREQUEST); 385e2841179SLoGin } 386e2841179SLoGin 387e2841179SLoGin #[allow(dead_code)] 388e2841179SLoGin pub fn clear_norequest(&mut self) { 389e2841179SLoGin self.line_status.remove(IrqLineStatus::IRQ_NOREQUEST); 390e2841179SLoGin } 391e2841179SLoGin 392*338f6903SLoGin #[allow(dead_code)] 393*338f6903SLoGin pub fn set_noprobe(&mut self) { 394*338f6903SLoGin self.line_status.insert(IrqLineStatus::IRQ_NOPROBE); 395*338f6903SLoGin } 396*338f6903SLoGin 397*338f6903SLoGin #[allow(dead_code)] 398*338f6903SLoGin pub fn clear_noprobe(&mut self) { 399*338f6903SLoGin self.line_status.remove(IrqLineStatus::IRQ_NOPROBE); 400*338f6903SLoGin } 401*338f6903SLoGin 402*338f6903SLoGin pub fn set_nothread(&mut self) { 403*338f6903SLoGin self.line_status.insert(IrqLineStatus::IRQ_NOTHREAD); 404*338f6903SLoGin } 405*338f6903SLoGin 406*338f6903SLoGin pub fn clear_nothread(&mut self) { 407*338f6903SLoGin self.line_status.remove(IrqLineStatus::IRQ_NOTHREAD); 408*338f6903SLoGin } 409*338f6903SLoGin 410e2841179SLoGin pub fn nested_thread(&self) -> bool { 411e2841179SLoGin self.line_status.contains(IrqLineStatus::IRQ_NESTED_THREAD) 412e2841179SLoGin } 413e2841179SLoGin 414e2841179SLoGin pub fn line_status_set_per_cpu(&mut self) { 415e2841179SLoGin self.line_status.insert(IrqLineStatus::IRQ_PER_CPU); 416e2841179SLoGin } 417e2841179SLoGin 418e2841179SLoGin #[allow(dead_code)] 419e2841179SLoGin pub fn line_status_clear_per_cpu(&mut self) { 420e2841179SLoGin self.line_status.remove(IrqLineStatus::IRQ_PER_CPU); 421e2841179SLoGin } 422e2841179SLoGin 423e2841179SLoGin #[allow(dead_code)] 424e2841179SLoGin pub fn line_status(&self) -> &IrqLineStatus { 425e2841179SLoGin &self.line_status 426e2841179SLoGin } 427e2841179SLoGin 428e2841179SLoGin pub fn line_status_set_no_debug(&mut self) { 429e2841179SLoGin self.line_status.insert(IrqLineStatus::IRQ_NO_BALANCING); 430e2841179SLoGin } 431e2841179SLoGin 432e2841179SLoGin #[allow(dead_code)] 433e2841179SLoGin pub fn line_status_clear_no_debug(&mut self) { 434e2841179SLoGin self.line_status.remove(IrqLineStatus::IRQ_NO_BALANCING); 435e2841179SLoGin } 436e2841179SLoGin 437e2841179SLoGin pub fn can_autoenable(&self) -> bool { 438e2841179SLoGin !self.line_status.contains(IrqLineStatus::IRQ_NOAUTOEN) 439e2841179SLoGin } 440e2841179SLoGin 441e2841179SLoGin pub fn can_thread(&self) -> bool { 442e2841179SLoGin !self.line_status.contains(IrqLineStatus::IRQ_NOTHREAD) 443e2841179SLoGin } 444e2841179SLoGin 445e2841179SLoGin /// 中断是否可以设置CPU亲和性 446e2841179SLoGin pub fn can_set_affinity(&self) -> bool { 447e2841179SLoGin if self.common_data.status().can_balance() == false 448e2841179SLoGin || self 449e2841179SLoGin .irq_data() 450e2841179SLoGin .chip_info_read_irqsave() 451e2841179SLoGin .chip() 452e2841179SLoGin .can_set_affinity() 453e2841179SLoGin == false 454e2841179SLoGin { 455e2841179SLoGin return false; 456e2841179SLoGin } 457e2841179SLoGin 458e2841179SLoGin return true; 459e2841179SLoGin } 460e2841179SLoGin 461e2841179SLoGin pub fn actions(&self) -> &Vec<Arc<IrqAction>> { 462e2841179SLoGin &self.actions 463e2841179SLoGin } 464e2841179SLoGin 465e2841179SLoGin pub fn add_action(&mut self, action: Arc<IrqAction>) { 466e2841179SLoGin self.actions.push(action); 467e2841179SLoGin } 468e2841179SLoGin 469*338f6903SLoGin pub fn clear_actions(&mut self) { 470*338f6903SLoGin self.actions.clear(); 471*338f6903SLoGin } 472*338f6903SLoGin 473*338f6903SLoGin pub fn remove_action(&mut self, action: &Arc<IrqAction>) { 474*338f6903SLoGin self.actions.retain(|a| !Arc::ptr_eq(a, action)); 475*338f6903SLoGin } 476*338f6903SLoGin 477e2841179SLoGin pub fn internal_state(&self) -> &IrqDescState { 478e2841179SLoGin &self.desc_internal_state 479e2841179SLoGin } 480e2841179SLoGin 481e2841179SLoGin pub(super) fn internal_state_mut(&mut self) -> &mut IrqDescState { 482e2841179SLoGin &mut self.desc_internal_state 483e2841179SLoGin } 484e2841179SLoGin 485e2841179SLoGin pub fn irq_data(&self) -> &Arc<IrqData> { 486e2841179SLoGin &self.irq_data 487e2841179SLoGin } 488e2841179SLoGin 489e2841179SLoGin pub fn common_data(&self) -> &Arc<IrqCommonData> { 490e2841179SLoGin &self.common_data 491e2841179SLoGin } 492e2841179SLoGin 493e2841179SLoGin pub fn depth(&self) -> u32 { 494e2841179SLoGin self.depth 495e2841179SLoGin } 496e2841179SLoGin 497e2841179SLoGin pub fn wake_depth(&self) -> u32 { 498e2841179SLoGin self.wake_depth 499e2841179SLoGin } 500e2841179SLoGin 501e2841179SLoGin pub fn set_depth(&mut self, depth: u32) { 502e2841179SLoGin self.depth = depth; 503e2841179SLoGin } 504e2841179SLoGin 505e2841179SLoGin pub fn set_trigger_type(&mut self, trigger: IrqLineStatus) { 506e2841179SLoGin self.line_status.remove(IrqLineStatus::IRQ_TYPE_SENSE_MASK); 507e2841179SLoGin self.line_status 508e2841179SLoGin .insert(trigger & IrqLineStatus::IRQ_TYPE_SENSE_MASK); 509e2841179SLoGin } 510e2841179SLoGin 511e2841179SLoGin pub fn clear_level(&mut self) { 512e2841179SLoGin self.line_status.remove(IrqLineStatus::IRQ_LEVEL); 513e2841179SLoGin } 514e2841179SLoGin 515e2841179SLoGin pub fn set_level(&mut self) { 516e2841179SLoGin self.line_status.insert(IrqLineStatus::IRQ_LEVEL); 517e2841179SLoGin } 518*338f6903SLoGin 519*338f6903SLoGin pub fn percpu_enabled(&self) -> &Option<CpuMask> { 520*338f6903SLoGin &self.percpu_enabled 521*338f6903SLoGin } 522*338f6903SLoGin 523*338f6903SLoGin pub fn percpu_enabled_mut(&mut self) -> &mut Option<CpuMask> { 524*338f6903SLoGin &mut self.percpu_enabled 525*338f6903SLoGin } 526*338f6903SLoGin 527*338f6903SLoGin pub fn percpu_affinity(&self) -> &Option<CpuMask> { 528*338f6903SLoGin &self.percpu_affinity 529*338f6903SLoGin } 530*338f6903SLoGin 531*338f6903SLoGin pub fn percpu_affinity_mut(&mut self) -> &mut Option<CpuMask> { 532*338f6903SLoGin &mut self.percpu_affinity 533*338f6903SLoGin } 5343bc96fa4SLoGin } 5353bc96fa4SLoGin 5363bc96fa4SLoGin impl KObject for IrqDesc { 5373bc96fa4SLoGin fn as_any_ref(&self) -> &dyn Any { 5383bc96fa4SLoGin self 5393bc96fa4SLoGin } 5403bc96fa4SLoGin 5413bc96fa4SLoGin fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 5423bc96fa4SLoGin self.inner().kern_inode = inode; 5433bc96fa4SLoGin } 5443bc96fa4SLoGin 5453bc96fa4SLoGin fn inode(&self) -> Option<Arc<KernFSInode>> { 5463bc96fa4SLoGin self.inner().kern_inode.clone() 5473bc96fa4SLoGin } 5483bc96fa4SLoGin 5493bc96fa4SLoGin fn parent(&self) -> Option<Weak<dyn KObject>> { 5503bc96fa4SLoGin self.inner().parent_kobj.clone() 5513bc96fa4SLoGin } 5523bc96fa4SLoGin 5533bc96fa4SLoGin fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 5543bc96fa4SLoGin self.inner().parent_kobj = parent; 5553bc96fa4SLoGin } 5563bc96fa4SLoGin 5573bc96fa4SLoGin fn kset(&self) -> Option<Arc<KSet>> { 5583bc96fa4SLoGin self.inner().kset.clone() 5593bc96fa4SLoGin } 5603bc96fa4SLoGin 5613bc96fa4SLoGin fn set_kset(&self, kset: Option<Arc<KSet>>) { 5623bc96fa4SLoGin self.inner().kset = kset; 5633bc96fa4SLoGin } 5643bc96fa4SLoGin 5653bc96fa4SLoGin fn kobj_type(&self) -> Option<&'static dyn KObjType> { 5663bc96fa4SLoGin Some(&IrqKObjType) 5673bc96fa4SLoGin } 5683bc96fa4SLoGin 5693bc96fa4SLoGin fn set_kobj_type(&self, _ktype: Option<&'static dyn KObjType>) {} 5703bc96fa4SLoGin 5713bc96fa4SLoGin fn name(&self) -> String { 572196b75dcSLoGin self.inner().irq_data.irq().data().to_string() 5733bc96fa4SLoGin } 5743bc96fa4SLoGin 5753bc96fa4SLoGin fn set_name(&self, _name: String) {} 5763bc96fa4SLoGin 5773bc96fa4SLoGin fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 5783bc96fa4SLoGin self.kobj_state.read() 5793bc96fa4SLoGin } 5803bc96fa4SLoGin 5813bc96fa4SLoGin fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 5823bc96fa4SLoGin self.kobj_state.write() 5833bc96fa4SLoGin } 5843bc96fa4SLoGin 5853bc96fa4SLoGin fn set_kobj_state(&self, state: KObjectState) { 5863bc96fa4SLoGin *self.kobj_state_mut() = state; 5873bc96fa4SLoGin } 5883bc96fa4SLoGin } 5893bc96fa4SLoGin 590196b75dcSLoGin bitflags! { 591196b75dcSLoGin /// Bit masks for desc->desc_internal_state 592e2841179SLoGin pub struct IrqDescState: u32 { 593196b75dcSLoGin /// autodetection in progress 594196b75dcSLoGin const IRQS_AUTODETECT = 0x00000001; 595196b75dcSLoGin /// was disabled due to spurious interrupt detection 596196b75dcSLoGin const IRQS_SPURIOUS_DISABLED = 0x00000002; 597196b75dcSLoGin /// polling in progress 598196b75dcSLoGin const IRQS_POLL_INPROGRESS = 0x00000008; 599196b75dcSLoGin /// irq is not unmasked in primary handler 600196b75dcSLoGin const IRQS_ONESHOT = 0x00000020; 601196b75dcSLoGin /// irq is replayed 602196b75dcSLoGin const IRQS_REPLAY = 0x00000040; 603196b75dcSLoGin /// irq is waiting 604196b75dcSLoGin const IRQS_WAITING = 0x00000080; 605196b75dcSLoGin /// irq is pending and replayed later 606196b75dcSLoGin const IRQS_PENDING = 0x00000200; 607196b75dcSLoGin /// irq is suspended 608196b75dcSLoGin const IRQS_SUSPENDED = 0x00000800; 609196b75dcSLoGin /// irq line is used to deliver NMIs 610196b75dcSLoGin const IRQS_NMI = 0x00002000; 611196b75dcSLoGin /// descriptor has been added to sysfs 612196b75dcSLoGin const IRQS_SYSFS = 0x00004000; 613196b75dcSLoGin } 614196b75dcSLoGin } 615196b75dcSLoGin 6163bc96fa4SLoGin /// 每个中断的响应动作的描述符 6173bc96fa4SLoGin /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/include/linux/interrupt.h#118 6183bc96fa4SLoGin #[allow(dead_code)] 6193bc96fa4SLoGin #[derive(Debug)] 6203bc96fa4SLoGin pub struct IrqAction { 6213bc96fa4SLoGin inner: SpinLock<InnerIrqAction>, 622e2841179SLoGin /// 用于等待线程被创建的完成量 623e2841179SLoGin thread_completion: Completion, 6243bc96fa4SLoGin } 6253bc96fa4SLoGin 6263bc96fa4SLoGin impl IrqAction { 6273bc96fa4SLoGin #[allow(dead_code)] 6283bc96fa4SLoGin pub fn new( 6293bc96fa4SLoGin irq: IrqNumber, 6303bc96fa4SLoGin name: String, 631e2841179SLoGin handler: Option<&'static dyn IrqHandler>, 632e2841179SLoGin thread_fn: Option<&'static dyn IrqHandler>, 6333bc96fa4SLoGin ) -> Arc<Self> { 634e2841179SLoGin let action: IrqAction = IrqAction { 6353bc96fa4SLoGin inner: SpinLock::new(InnerIrqAction { 6363bc96fa4SLoGin dev_id: None, 637*338f6903SLoGin per_cpu_dev_id: None, 6383bc96fa4SLoGin handler, 639e2841179SLoGin thread_fn, 6403bc96fa4SLoGin thread: None, 6413bc96fa4SLoGin secondary: None, 6423bc96fa4SLoGin irq, 6433bc96fa4SLoGin flags: IrqHandleFlags::empty(), 6443bc96fa4SLoGin name, 645e2841179SLoGin thread_flags: ThreadedHandlerFlags::empty(), 6463bc96fa4SLoGin }), 647e2841179SLoGin thread_completion: Completion::new(), 6483bc96fa4SLoGin }; 6493bc96fa4SLoGin 6503bc96fa4SLoGin return Arc::new(action); 6513bc96fa4SLoGin } 652196b75dcSLoGin 653e2841179SLoGin pub fn inner(&self) -> SpinLockGuard<InnerIrqAction> { 654e2841179SLoGin self.inner.lock_irqsave() 655196b75dcSLoGin } 656196b75dcSLoGin 657e2841179SLoGin pub fn thread_completion(&self) -> &Completion { 658e2841179SLoGin &self.thread_completion 659196b75dcSLoGin } 6603bc96fa4SLoGin } 6613bc96fa4SLoGin 6623bc96fa4SLoGin #[allow(dead_code)] 6633bc96fa4SLoGin #[derive(Debug)] 664e2841179SLoGin pub struct InnerIrqAction { 6653bc96fa4SLoGin /// cookie to identify the device 666e2841179SLoGin dev_id: Option<Arc<DeviceId>>, 667*338f6903SLoGin /// cookie to identify the device (per cpu) 668*338f6903SLoGin per_cpu_dev_id: Option<PerCpuVar<Arc<DeviceId>>>, 6693bc96fa4SLoGin /// 中断处理程序 670e2841179SLoGin handler: Option<&'static dyn IrqHandler>, 6713bc96fa4SLoGin /// interrupt handler function for threaded interrupts 672e2841179SLoGin thread_fn: Option<&'static dyn IrqHandler>, 6733bc96fa4SLoGin /// thread pointer for threaded interrupts 6743bc96fa4SLoGin thread: Option<Arc<ProcessControlBlock>>, 6753bc96fa4SLoGin /// pointer to secondary irqaction (force threading) 6763bc96fa4SLoGin secondary: Option<Arc<IrqAction>>, 6773bc96fa4SLoGin /// 中断号 6783bc96fa4SLoGin irq: IrqNumber, 6793bc96fa4SLoGin flags: IrqHandleFlags, 680e2841179SLoGin /// 中断线程的标志 681e2841179SLoGin thread_flags: ThreadedHandlerFlags, 6823bc96fa4SLoGin /// name of the device 6833bc96fa4SLoGin name: String, 6843bc96fa4SLoGin } 6853bc96fa4SLoGin 686e2841179SLoGin impl InnerIrqAction { 687e2841179SLoGin pub fn dev_id(&self) -> &Option<Arc<DeviceId>> { 688e2841179SLoGin &self.dev_id 689e2841179SLoGin } 690e2841179SLoGin 691e2841179SLoGin pub fn dev_id_mut(&mut self) -> &mut Option<Arc<DeviceId>> { 692e2841179SLoGin &mut self.dev_id 693e2841179SLoGin } 694e2841179SLoGin 695*338f6903SLoGin pub fn per_cpu_dev_id(&self) -> Option<&Arc<DeviceId>> { 696*338f6903SLoGin self.per_cpu_dev_id.as_ref().map(|v| v.get()) 697*338f6903SLoGin } 698*338f6903SLoGin 699*338f6903SLoGin #[allow(dead_code)] 700*338f6903SLoGin pub fn per_cpu_dev_id_mut(&mut self) -> Option<&mut Arc<DeviceId>> { 701*338f6903SLoGin self.per_cpu_dev_id.as_mut().map(|v| v.get_mut()) 702*338f6903SLoGin } 703*338f6903SLoGin 704e2841179SLoGin pub fn handler(&self) -> Option<&'static dyn IrqHandler> { 705e2841179SLoGin self.handler 706e2841179SLoGin } 707e2841179SLoGin 708e2841179SLoGin pub fn set_handler(&mut self, handler: Option<&'static dyn IrqHandler>) { 709e2841179SLoGin self.handler = handler; 710e2841179SLoGin } 711e2841179SLoGin 712e2841179SLoGin pub fn thread_fn(&self) -> Option<&'static dyn IrqHandler> { 713e2841179SLoGin self.thread_fn 714e2841179SLoGin } 715e2841179SLoGin 716e2841179SLoGin pub fn thread(&self) -> Option<Arc<ProcessControlBlock>> { 717e2841179SLoGin self.thread.clone() 718e2841179SLoGin } 719e2841179SLoGin 720e2841179SLoGin pub fn set_thread(&mut self, thread: Option<Arc<ProcessControlBlock>>) { 721e2841179SLoGin self.thread = thread; 722e2841179SLoGin } 723e2841179SLoGin 724e2841179SLoGin #[allow(dead_code)] 725e2841179SLoGin pub fn thread_flags(&self) -> &ThreadedHandlerFlags { 726e2841179SLoGin &self.thread_flags 727e2841179SLoGin } 728e2841179SLoGin 729e2841179SLoGin pub fn thread_flags_mut(&mut self) -> &mut ThreadedHandlerFlags { 730e2841179SLoGin &mut self.thread_flags 731e2841179SLoGin } 732e2841179SLoGin 733e2841179SLoGin pub fn secondary(&self) -> Option<Arc<IrqAction>> { 734e2841179SLoGin self.secondary.clone() 735e2841179SLoGin } 736e2841179SLoGin 737e2841179SLoGin #[allow(dead_code)] 738e2841179SLoGin pub fn irq(&self) -> IrqNumber { 739e2841179SLoGin self.irq 740e2841179SLoGin } 741e2841179SLoGin 742e2841179SLoGin #[allow(dead_code)] 743e2841179SLoGin pub fn set_irq(&mut self, irq: IrqNumber) { 744e2841179SLoGin self.irq = irq; 745e2841179SLoGin } 746e2841179SLoGin 747e2841179SLoGin pub fn flags(&self) -> &IrqHandleFlags { 748e2841179SLoGin &self.flags 749e2841179SLoGin } 750e2841179SLoGin 751e2841179SLoGin pub fn flags_mut(&mut self) -> &mut IrqHandleFlags { 752e2841179SLoGin &mut self.flags 753e2841179SLoGin } 754e2841179SLoGin 755e2841179SLoGin pub fn name(&self) -> &String { 756e2841179SLoGin &self.name 757e2841179SLoGin } 758e2841179SLoGin } 759e2841179SLoGin 760e2841179SLoGin bitflags! { 761e2841179SLoGin /// 这些标志由线程处理程序使用 762e2841179SLoGin pub struct ThreadedHandlerFlags: u32 { 763e2841179SLoGin /// IRQTF_RUNTHREAD - 表示应运行中断处理程序线程 764e2841179SLoGin const IRQTF_RUNTHREAD = 1 << 0; 765e2841179SLoGin /// IRQTF_WARNED - 已打印警告 "IRQ_WAKE_THREAD w/o thread_fn" 766e2841179SLoGin const IRQTF_WARNED = 1 << 1; 767e2841179SLoGin /// IRQTF_AFFINITY - 请求irq线程调整亲和性 768e2841179SLoGin const IRQTF_AFFINITY = 1 << 2; 769e2841179SLoGin /// IRQTF_FORCED_THREAD - irq操作被强制线程化 770e2841179SLoGin const IRQTF_FORCED_THREAD = 1 << 3; 771e2841179SLoGin /// IRQTF_READY - 表示irq线程已准备就绪 772e2841179SLoGin const IRQTF_READY = 1 << 4; 773e2841179SLoGin } 774e2841179SLoGin } 775e2841179SLoGin 776e2841179SLoGin /// Implements the `ThreadedHandlerFlags` structure. 777e2841179SLoGin impl ThreadedHandlerFlags { 778e2841179SLoGin /// 在 `ThreadedHandlerFlags` 结构中测试并设置特定的位。 779e2841179SLoGin /// 780e2841179SLoGin /// # 参数 781e2841179SLoGin /// 782e2841179SLoGin /// * `bit` - 要测试并设置的位。 783e2841179SLoGin /// 784e2841179SLoGin /// # 返回 785e2841179SLoGin /// 786e2841179SLoGin /// 如果操作前该位已被设置,则返回 `true`,否则返回 `false`。 787e2841179SLoGin pub fn test_and_set_bit(&mut self, bit: ThreadedHandlerFlags) -> bool { 788e2841179SLoGin let res = (self.bits & bit.bits) != 0; 789e2841179SLoGin self.bits |= bit.bits; 790e2841179SLoGin return res; 791e2841179SLoGin } 792e2841179SLoGin } 793e2841179SLoGin 7943bc96fa4SLoGin // 定义IrqFlags位标志 7953bc96fa4SLoGin bitflags! { 7963bc96fa4SLoGin /// 这些标志仅由内核在中断处理例程中使用。 7973bc96fa4SLoGin pub struct IrqHandleFlags: u32 { 798e2841179SLoGin 799e2841179SLoGin const IRQF_TRIGGER_NONE = IrqLineStatus::IRQ_TYPE_NONE.bits(); 800e2841179SLoGin const IRQF_TRIGGER_RISING = IrqLineStatus::IRQ_TYPE_EDGE_RISING.bits(); 801e2841179SLoGin const IRQF_TRIGGER_FALLING = IrqLineStatus::IRQ_TYPE_EDGE_FALLING.bits(); 802e2841179SLoGin const IRQF_TRIGGER_HIGH = IrqLineStatus::IRQ_TYPE_LEVEL_HIGH.bits(); 803e2841179SLoGin const IRQF_TRIGGER_LOW = IrqLineStatus::IRQ_TYPE_LEVEL_LOW.bits(); 804e2841179SLoGin const IRQF_TRIGGER_MASK = Self::IRQF_TRIGGER_HIGH.bits | Self::IRQF_TRIGGER_LOW.bits | Self::IRQF_TRIGGER_RISING.bits | Self::IRQF_TRIGGER_FALLING.bits; 8053bc96fa4SLoGin /// IRQF_SHARED - 允许多个设备共享中断 8063bc96fa4SLoGin const IRQF_SHARED = 0x00000080; 8073bc96fa4SLoGin /// IRQF_PROBE_SHARED - 当预期出现共享不匹配时,由调用者设置 8083bc96fa4SLoGin const IRQF_PROBE_SHARED = 0x00000100; 8093bc96fa4SLoGin /// IRQF_TIMER - 标记此中断为定时器中断 8103bc96fa4SLoGin const __IRQF_TIMER = 0x00000200; 8113bc96fa4SLoGin /// IRQF_PERCPU - 中断是每个CPU的 8123bc96fa4SLoGin const IRQF_PERCPU = 0x00000400; 8133bc96fa4SLoGin /// IRQF_NOBALANCING - 将此中断从中断平衡中排除 8143bc96fa4SLoGin const IRQF_NOBALANCING = 0x00000800; 8153bc96fa4SLoGin /// IRQF_IRQPOLL - 中断用于轮询(出于性能原因,只有在共享中断中首次注册的中断会被考虑) 8163bc96fa4SLoGin const IRQF_IRQPOLL = 0x00001000; 8173bc96fa4SLoGin /// IRQF_ONESHOT - 在硬中断处理程序完成后,不会重新启用中断。由需要在运行线程处理程序之前保持中断线路禁用的线程中断使用。 8183bc96fa4SLoGin const IRQF_ONESHOT = 0x00002000; 8193bc96fa4SLoGin /// IRQF_NO_SUSPEND - 在挂起期间不禁用此IRQ。不能保证此中断会从挂起状态唤醒系统。 8203bc96fa4SLoGin const IRQF_NO_SUSPEND = 0x00004000; 8213bc96fa4SLoGin /// IRQF_FORCE_RESUME - 即使设置了IRQF_NO_SUSPEND,也强制在恢复时启用它 8223bc96fa4SLoGin const IRQF_FORCE_RESUME = 0x00008000; 8233bc96fa4SLoGin /// IRQF_NO_THREAD - 中断不能被线程化 8243bc96fa4SLoGin const IRQF_NO_THREAD = 0x00010000; 8253bc96fa4SLoGin /// IRQF_EARLY_RESUME - 在syscore而不是在设备恢复时间早期恢复IRQ。 8263bc96fa4SLoGin const IRQF_EARLY_RESUME = 0x00020000; 8273bc96fa4SLoGin /// IRQF_COND_SUSPEND - 如果IRQ与NO_SUSPEND用户共享,则在挂起中断后执行此中断处理程序。对于系统唤醒设备用户,需要在他们的中断处理程序中实现唤醒检测。 8283bc96fa4SLoGin const IRQF_COND_SUSPEND = 0x00040000; 8293bc96fa4SLoGin /// IRQF_NO_AUTOEN - 当用户请求时,不会自动启用IRQ或NMI。用户稍后会通过enable_irq()或enable_nmi()显式启用它。 8303bc96fa4SLoGin const IRQF_NO_AUTOEN = 0x00080000; 8313bc96fa4SLoGin /// IRQF_NO_DEBUG - 从IPI和类似处理程序的逃逸检测中排除,取决于IRQF_PERCPU。 8323bc96fa4SLoGin const IRQF_NO_DEBUG = 0x00100000; 8333bc96fa4SLoGin const IRQF_TIMER = Self::__IRQF_TIMER.bits | Self::IRQF_NO_SUSPEND.bits | Self::IRQF_NO_THREAD.bits; 8343bc96fa4SLoGin } 8353bc96fa4SLoGin } 8363bc96fa4SLoGin 837e2841179SLoGin impl IrqHandleFlags { 838e2841179SLoGin /// 检查是否指定了触发类型 839e2841179SLoGin #[inline(always)] 840e2841179SLoGin pub fn trigger_type_specified(&self) -> bool { 841e2841179SLoGin (self.bits & Self::IRQF_TRIGGER_MASK.bits) != 0 842e2841179SLoGin } 843e2841179SLoGin 844e2841179SLoGin /// 插入触发类型 845e2841179SLoGin pub fn insert_trigger_type(&mut self, trigger: IrqLineStatus) { 846e2841179SLoGin self.bits |= trigger.trigger_bits() & IrqHandleFlags::IRQF_TRIGGER_MASK.bits; 847e2841179SLoGin } 848e2841179SLoGin 849e2841179SLoGin #[allow(dead_code)] 850e2841179SLoGin pub fn remove_trigger_type(&mut self, trigger: IrqLineStatus) { 851e2841179SLoGin self.bits &= !(trigger.trigger_bits() & IrqHandleFlags::IRQF_TRIGGER_MASK.bits); 852e2841179SLoGin } 853e2841179SLoGin 854e2841179SLoGin pub fn trigger_type(&self) -> IrqLineStatus { 855e2841179SLoGin IrqLineStatus::from_bits_truncate(self.bits & IrqHandleFlags::IRQF_TRIGGER_MASK.bits) 856e2841179SLoGin } 857e2841179SLoGin } 858e2841179SLoGin 8593bc96fa4SLoGin #[inline(never)] 8603bc96fa4SLoGin pub(super) fn early_irq_init() -> Result<(), SystemError> { 8613bc96fa4SLoGin let irqcnt = CurrentIrqArch::probe_total_irq_num(); 8623bc96fa4SLoGin let mut manager = IrqDescManager::new(); 8633bc96fa4SLoGin for i in 0..irqcnt { 8643bc96fa4SLoGin let irq_desc = IrqDesc::new(IrqNumber::new(i), None, IrqStatus::empty()); 8653bc96fa4SLoGin manager.insert(IrqNumber::new(i), irq_desc); 8663bc96fa4SLoGin } 8673bc96fa4SLoGin 868196b75dcSLoGin unsafe { 869196b75dcSLoGin IRQ_DESC_MANAGER = Some(manager); 870196b75dcSLoGin } 871196b75dcSLoGin 8723bc96fa4SLoGin return CurrentIrqArch::arch_early_irq_init(); 8733bc96fa4SLoGin } 8743bc96fa4SLoGin 875196b75dcSLoGin static mut IRQ_DESC_MANAGER: Option<IrqDescManager> = None; 876196b75dcSLoGin 877196b75dcSLoGin /// 获取中断描述符管理器的引用 878196b75dcSLoGin #[inline(always)] 879e2841179SLoGin pub fn irq_desc_manager() -> &'static IrqDescManager { 880196b75dcSLoGin return unsafe { IRQ_DESC_MANAGER.as_ref().unwrap() }; 881196b75dcSLoGin } 882196b75dcSLoGin 883e2841179SLoGin pub struct IrqDescManager { 8843bc96fa4SLoGin irq_descs: BTreeMap<IrqNumber, Arc<IrqDesc>>, 8853bc96fa4SLoGin } 8863bc96fa4SLoGin 8873bc96fa4SLoGin impl IrqDescManager { 8883bc96fa4SLoGin fn new() -> Self { 8893bc96fa4SLoGin IrqDescManager { 8903bc96fa4SLoGin irq_descs: BTreeMap::new(), 8913bc96fa4SLoGin } 8923bc96fa4SLoGin } 8933bc96fa4SLoGin 8943bc96fa4SLoGin /// 查找中断描述符 8953bc96fa4SLoGin pub fn lookup(&self, irq: IrqNumber) -> Option<Arc<IrqDesc>> { 8963bc96fa4SLoGin self.irq_descs.get(&irq).map(|desc| desc.clone()) 8973bc96fa4SLoGin } 8983bc96fa4SLoGin 899*338f6903SLoGin /// 查找中断描述符并锁定总线(没有对irqdesc进行加锁) 900*338f6903SLoGin #[allow(dead_code)] 901*338f6903SLoGin pub fn lookup_and_lock_bus( 902*338f6903SLoGin &self, 903*338f6903SLoGin irq: IrqNumber, 904*338f6903SLoGin check_global: bool, 905*338f6903SLoGin check_percpu: bool, 906*338f6903SLoGin ) -> Option<Arc<IrqDesc>> { 907*338f6903SLoGin self.do_lookup_and_lock(irq, true, check_global, check_percpu) 908*338f6903SLoGin } 909*338f6903SLoGin 910*338f6903SLoGin fn do_lookup_and_lock( 911*338f6903SLoGin &self, 912*338f6903SLoGin irq: IrqNumber, 913*338f6903SLoGin lock_bus: bool, 914*338f6903SLoGin check_global: bool, 915*338f6903SLoGin check_percpu: bool, 916*338f6903SLoGin ) -> Option<Arc<IrqDesc>> { 917*338f6903SLoGin let desc = self.lookup(irq)?; 918*338f6903SLoGin if check_global || check_percpu { 919*338f6903SLoGin if check_percpu && !desc.inner().line_status().is_per_cpu_devid() { 920*338f6903SLoGin return None; 921*338f6903SLoGin } 922*338f6903SLoGin 923*338f6903SLoGin if check_global && desc.inner().line_status().is_per_cpu_devid() { 924*338f6903SLoGin return None; 925*338f6903SLoGin } 926*338f6903SLoGin } 927*338f6903SLoGin 928*338f6903SLoGin if lock_bus { 929*338f6903SLoGin desc.chip_bus_lock(); 930*338f6903SLoGin } 931*338f6903SLoGin 932*338f6903SLoGin return Some(desc); 933*338f6903SLoGin } 934*338f6903SLoGin 9353bc96fa4SLoGin fn insert(&mut self, irq: IrqNumber, desc: Arc<IrqDesc>) { 9363bc96fa4SLoGin self.irq_descs.insert(irq, desc); 9373bc96fa4SLoGin } 938196b75dcSLoGin 939196b75dcSLoGin /// 释放中断描述符 940196b75dcSLoGin #[allow(dead_code)] 941196b75dcSLoGin fn free_desc(&mut self, irq: IrqNumber) { 942196b75dcSLoGin if let Some(desc) = self.irq_descs.get(&irq) { 943196b75dcSLoGin irq_sysfs_del(desc); 944196b75dcSLoGin self.irq_descs.remove(&irq); 945196b75dcSLoGin } 946196b75dcSLoGin } 947196b75dcSLoGin 948196b75dcSLoGin /// 迭代中断描述符 949196b75dcSLoGin pub fn iter_descs(&self) -> btree_map::Iter<'_, IrqNumber, Arc<IrqDesc>> { 950196b75dcSLoGin self.irq_descs.iter() 951196b75dcSLoGin } 952*338f6903SLoGin 953*338f6903SLoGin /// 设置指定irq的可用cpu为所有cpu 954*338f6903SLoGin pub fn set_percpu_devid_all(&self, irq: IrqNumber) -> Result<(), SystemError> { 955*338f6903SLoGin self.set_percpu_devid(irq, None) 956*338f6903SLoGin } 957*338f6903SLoGin 958*338f6903SLoGin /// 设置指定irq的可用cpu 959*338f6903SLoGin /// 960*338f6903SLoGin /// 如果affinity为None,则表示设置为所有cpu 961*338f6903SLoGin pub fn set_percpu_devid( 962*338f6903SLoGin &self, 963*338f6903SLoGin irq: IrqNumber, 964*338f6903SLoGin affinity: Option<&CpuMask>, 965*338f6903SLoGin ) -> Result<(), SystemError> { 966*338f6903SLoGin let desc = self.lookup(irq).ok_or(SystemError::EINVAL)?; 967*338f6903SLoGin let mut desc_inner = desc.inner(); 968*338f6903SLoGin 969*338f6903SLoGin if desc_inner.percpu_enabled().is_some() { 970*338f6903SLoGin return Err(SystemError::EINVAL); 971*338f6903SLoGin } 972*338f6903SLoGin 973*338f6903SLoGin *desc_inner.percpu_enabled_mut() = Some(CpuMask::new()); 974*338f6903SLoGin 975*338f6903SLoGin if let Some(affinity) = affinity { 976*338f6903SLoGin desc_inner.percpu_affinity_mut().replace(affinity.clone()); 977*338f6903SLoGin } else { 978*338f6903SLoGin desc_inner 979*338f6903SLoGin .percpu_affinity_mut() 980*338f6903SLoGin .replace(smp_cpu_manager().possible_cpus().clone()); 981*338f6903SLoGin } 982*338f6903SLoGin 983*338f6903SLoGin drop(desc_inner); 984*338f6903SLoGin 985*338f6903SLoGin desc.set_percpu_devid_flags(); 986*338f6903SLoGin 987*338f6903SLoGin return Ok(()); 988*338f6903SLoGin } 9893bc96fa4SLoGin } 990