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::{ 24338f6903SLoGin cpumask::CpuMask, 25e2841179SLoGin mutex::{Mutex, MutexGuard}, 26e2841179SLoGin rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard}, 273bc96fa4SLoGin spinlock::{SpinLock, SpinLockGuard}, 283bc96fa4SLoGin }, 29338f6903SLoGin mm::percpu::PerCpuVar, 303bc96fa4SLoGin process::ProcessControlBlock, 31e2841179SLoGin sched::completion::Completion, 32338f6903SLoGin smp::cpu::smp_cpu_manager, 333bc96fa4SLoGin }; 343bc96fa4SLoGin 353bc96fa4SLoGin use super::{ 363bc96fa4SLoGin dummychip::no_irq_chip, 373bc96fa4SLoGin handle::bad_irq_handler, 38338f6903SLoGin irqchip::IrqChip, 39e2841179SLoGin irqdata::{IrqCommonData, IrqData, IrqHandlerData, IrqLineStatus, IrqStatus}, 40*f049d1afSLoGin irqdomain::{irq_domain_manager, IrqDomain}, 41196b75dcSLoGin sysfs::{irq_sysfs_del, IrqKObjType}, 423bc96fa4SLoGin HardwareIrqNumber, InterruptArch, IrqNumber, 433bc96fa4SLoGin }; 443bc96fa4SLoGin 453bc96fa4SLoGin /// 中断流处理程序 46e2841179SLoGin pub trait IrqFlowHandler: Debug + Send + Sync + Any { 47e2841179SLoGin fn handle(&self, irq_desc: &Arc<IrqDesc>, trap_frame: &mut TrapFrame); 48e2841179SLoGin } 49e2841179SLoGin 50e2841179SLoGin /// 中断处理程序 51e2841179SLoGin pub trait IrqHandler: Debug + Send + Sync + Any { 52e2841179SLoGin fn handle( 53e2841179SLoGin &self, 54e2841179SLoGin irq: IrqNumber, 55e2841179SLoGin static_data: Option<&dyn IrqHandlerData>, 56e2841179SLoGin dynamic_data: Option<Arc<dyn IrqHandlerData>>, 57e2841179SLoGin ) -> Result<IrqReturn, SystemError>; 58e2841179SLoGin } 59e2841179SLoGin 60e2841179SLoGin /// 中断处理函数返回值 61e2841179SLoGin /// 62e2841179SLoGin /// 用于指示中断处理函数是否处理了中断 63e2841179SLoGin #[derive(Debug, Clone, Copy, PartialEq, Eq)] 64e2841179SLoGin pub enum IrqReturn { 65e2841179SLoGin /// 中断未被处理 66e2841179SLoGin NotHandled, 67e2841179SLoGin /// 中断已被处理 68e2841179SLoGin Handled, 69e2841179SLoGin /// 中断已被处理,并且需要唤醒中断线程 70e2841179SLoGin WakeThread, 713bc96fa4SLoGin } 723bc96fa4SLoGin 733bc96fa4SLoGin /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/include/linux/irqdesc.h#55 743bc96fa4SLoGin #[derive(Debug)] 753bc96fa4SLoGin pub struct IrqDesc { 763bc96fa4SLoGin inner: SpinLock<InnerIrqDesc>, 773bc96fa4SLoGin 78e2841179SLoGin handler: RwLock<Option<&'static dyn IrqFlowHandler>>, 79e2841179SLoGin /// 一个用于串行化 request_irq()和free_irq() 的互斥锁 80e2841179SLoGin request_mutex: Mutex<()>, 813bc96fa4SLoGin kobj_state: LockedKObjectState, 82e2841179SLoGin /// 当前描述符内正在运行的中断线程数 83e2841179SLoGin threads_active: AtomicI64, 843bc96fa4SLoGin } 853bc96fa4SLoGin 863bc96fa4SLoGin impl IrqDesc { 873bc96fa4SLoGin #[inline(never)] 883bc96fa4SLoGin pub fn new(irq: IrqNumber, name: Option<String>, irqd_flags: IrqStatus) -> Arc<Self> { 893bc96fa4SLoGin // 初始化的过程参考 https://code.dragonos.org.cn/xref/linux-6.1.9/kernel/irq/irqdesc.c#392 903bc96fa4SLoGin let common_data = Arc::new(IrqCommonData::new()); 913bc96fa4SLoGin let irq_data = Arc::new(IrqData::new( 923bc96fa4SLoGin irq, 933bc96fa4SLoGin HardwareIrqNumber::new(irq.data()), 943bc96fa4SLoGin common_data.clone(), 953bc96fa4SLoGin no_irq_chip(), 963bc96fa4SLoGin )); 973bc96fa4SLoGin 983bc96fa4SLoGin irq_data.irqd_set(IrqStatus::IRQD_IRQ_DISABLED); 99e2841179SLoGin common_data.insert_status(IrqStatus::IRQD_IRQ_MASKED); 1003bc96fa4SLoGin 1013bc96fa4SLoGin let irq_desc = IrqDesc { 1023bc96fa4SLoGin inner: SpinLock::new(InnerIrqDesc { 103338f6903SLoGin percpu_affinity: None, 104338f6903SLoGin percpu_enabled: None, 1053bc96fa4SLoGin common_data, 1063bc96fa4SLoGin irq_data, 107196b75dcSLoGin desc_internal_state: IrqDescState::empty(), 108e2841179SLoGin line_status: IrqLineStatus::empty(), 1093bc96fa4SLoGin actions: Vec::new(), 1103bc96fa4SLoGin name, 1113bc96fa4SLoGin parent_irq: None, 1123bc96fa4SLoGin depth: 1, 1133bc96fa4SLoGin wake_depth: 0, 1143bc96fa4SLoGin kern_inode: None, 1153bc96fa4SLoGin kset: None, 1163bc96fa4SLoGin parent_kobj: None, 1173bc96fa4SLoGin }), 118e2841179SLoGin request_mutex: Mutex::new(()), 119e2841179SLoGin handler: RwLock::new(None), 1203bc96fa4SLoGin kobj_state: LockedKObjectState::new(Some(KObjectState::INITIALIZED)), 121e2841179SLoGin threads_active: AtomicI64::new(0), 1223bc96fa4SLoGin }; 123*f049d1afSLoGin let irq_desc = Arc::new(irq_desc); 124*f049d1afSLoGin irq_desc.irq_data().set_irq_desc(Arc::downgrade(&irq_desc)); 1253bc96fa4SLoGin irq_desc.set_handler(bad_irq_handler()); 1263bc96fa4SLoGin irq_desc.inner().irq_data.irqd_set(irqd_flags); 1273bc96fa4SLoGin 128*f049d1afSLoGin return irq_desc; 1293bc96fa4SLoGin } 1303bc96fa4SLoGin 131e2841179SLoGin /// 返回当前活跃的中断线程数量 132e2841179SLoGin #[allow(dead_code)] 133e2841179SLoGin pub fn threads_active(&self) -> i64 { 134e2841179SLoGin self.threads_active.load(Ordering::SeqCst) 1353bc96fa4SLoGin } 1363bc96fa4SLoGin 137e2841179SLoGin /// 增加当前活跃的中断线程数量, 返回增加前的值 138e2841179SLoGin pub fn inc_threads_active(&self) -> i64 { 139e2841179SLoGin self.threads_active.fetch_add(1, Ordering::SeqCst) 140e2841179SLoGin } 141e2841179SLoGin 142e2841179SLoGin /// 减少当前活跃的中断线程数量, 返回减少前的值 143e2841179SLoGin #[allow(dead_code)] 144e2841179SLoGin pub fn dec_threads_active(&self) -> i64 { 145e2841179SLoGin self.threads_active.fetch_sub(1, Ordering::SeqCst) 146e2841179SLoGin } 147e2841179SLoGin 148e2841179SLoGin pub fn set_handler(&self, handler: &'static dyn IrqFlowHandler) { 149e2841179SLoGin self.chip_bus_lock(); 150e2841179SLoGin let mut guard = self.handler.write_irqsave(); 151e2841179SLoGin *guard = Some(handler); 152e2841179SLoGin self.chip_bus_sync_unlock(); 153e2841179SLoGin } 154e2841179SLoGin 155338f6903SLoGin /// 设置中断处理程序(不对desc->inner) 156338f6903SLoGin /// 157338f6903SLoGin /// 158338f6903SLoGin /// ## Safety 159338f6903SLoGin /// 160338f6903SLoGin /// 需要保证irq_data和chip是当前irqdesc的 161338f6903SLoGin pub fn set_handler_no_lock_inner( 162338f6903SLoGin &self, 163338f6903SLoGin handler: &'static dyn IrqFlowHandler, 164338f6903SLoGin irq_data: &Arc<IrqData>, 165338f6903SLoGin chip: &Arc<dyn IrqChip>, 166338f6903SLoGin ) { 167338f6903SLoGin chip.irq_bus_lock(irq_data).ok(); 168338f6903SLoGin let mut guard = self.handler.write_irqsave(); 169338f6903SLoGin *guard = Some(handler); 170338f6903SLoGin chip.irq_bus_sync_unlock(irq_data).ok(); 171338f6903SLoGin } 172338f6903SLoGin 173e2841179SLoGin pub fn handler(&self) -> Option<&'static dyn IrqFlowHandler> { 174e2841179SLoGin let guard = self.handler.read_irqsave(); 175e2841179SLoGin *guard 176e2841179SLoGin } 177e2841179SLoGin 178e2841179SLoGin pub fn inner(&self) -> SpinLockGuard<InnerIrqDesc> { 1793bc96fa4SLoGin self.inner.lock_irqsave() 1803bc96fa4SLoGin } 1813bc96fa4SLoGin 182196b75dcSLoGin pub fn actions(&self) -> Vec<Arc<IrqAction>> { 183196b75dcSLoGin self.inner().actions.clone() 184196b75dcSLoGin } 185196b75dcSLoGin 186e2841179SLoGin /// 对中断请求过程加锁 187e2841179SLoGin pub fn request_mutex_lock(&self) -> MutexGuard<()> { 188e2841179SLoGin self.request_mutex.lock() 189e2841179SLoGin } 190e2841179SLoGin 1913bc96fa4SLoGin pub fn irq(&self) -> IrqNumber { 1923bc96fa4SLoGin self.inner().irq_data.irq() 1933bc96fa4SLoGin } 194196b75dcSLoGin 195196b75dcSLoGin pub fn hardware_irq(&self) -> HardwareIrqNumber { 196196b75dcSLoGin self.inner().irq_data.hardware_irq() 197196b75dcSLoGin } 198196b75dcSLoGin 199196b75dcSLoGin pub fn irq_data(&self) -> Arc<IrqData> { 200196b75dcSLoGin self.inner().irq_data.clone() 201196b75dcSLoGin } 202196b75dcSLoGin 203196b75dcSLoGin /// 标记当前irq描述符已经被添加到sysfs 204196b75dcSLoGin pub fn mark_in_sysfs(&self) { 205196b75dcSLoGin self.inner() 206196b75dcSLoGin .desc_internal_state 207196b75dcSLoGin .insert(IrqDescState::IRQS_SYSFS); 208196b75dcSLoGin } 209196b75dcSLoGin 210196b75dcSLoGin pub fn mark_not_in_sysfs(&self) { 211196b75dcSLoGin self.inner() 212196b75dcSLoGin .desc_internal_state 213196b75dcSLoGin .remove(IrqDescState::IRQS_SYSFS); 214196b75dcSLoGin } 215196b75dcSLoGin 216196b75dcSLoGin /// 判断当前描述符是否已经添加到了sysfs 217196b75dcSLoGin pub fn in_sysfs(&self) -> bool { 218196b75dcSLoGin self.inner() 219196b75dcSLoGin .desc_internal_state 220196b75dcSLoGin .contains(IrqDescState::IRQS_SYSFS) 221196b75dcSLoGin } 222196b75dcSLoGin 223196b75dcSLoGin pub fn name(&self) -> Option<String> { 224196b75dcSLoGin self.inner().name.clone() 225196b75dcSLoGin } 226e2841179SLoGin 227e2841179SLoGin pub fn can_request(&self) -> bool { 228e2841179SLoGin self.inner().can_request() 229e2841179SLoGin } 230e2841179SLoGin 231e2841179SLoGin #[allow(dead_code)] 232e2841179SLoGin pub fn set_norequest(&self) { 233e2841179SLoGin self.inner().set_norequest(); 234e2841179SLoGin } 235e2841179SLoGin 236e2841179SLoGin #[allow(dead_code)] 237e2841179SLoGin pub fn clear_norequest(&self) { 238e2841179SLoGin self.inner().clear_norequest(); 239e2841179SLoGin } 240e2841179SLoGin 241e2841179SLoGin pub fn nested_thread(&self) -> bool { 242e2841179SLoGin self.inner().nested_thread() 243e2841179SLoGin } 244e2841179SLoGin 245e2841179SLoGin /// 中断是否可以线程化 246e2841179SLoGin pub fn can_thread(&self) -> bool { 247e2841179SLoGin !self 248e2841179SLoGin .inner() 249e2841179SLoGin .line_status 250e2841179SLoGin .contains(IrqLineStatus::IRQ_NOTHREAD) 251e2841179SLoGin } 252e2841179SLoGin 253e2841179SLoGin pub fn chip_bus_lock(&self) { 254e2841179SLoGin let irq_data = self.inner().irq_data.clone(); 255e2841179SLoGin irq_data 256e2841179SLoGin .chip_info_read_irqsave() 257e2841179SLoGin .chip() 258e2841179SLoGin .irq_bus_lock(&irq_data) 259e2841179SLoGin .ok(); 260e2841179SLoGin } 261e2841179SLoGin 262e2841179SLoGin /// 同步释放低速总线锁 263e2841179SLoGin /// 264e2841179SLoGin /// ## 锁 265e2841179SLoGin /// 266e2841179SLoGin /// 进入此函数时,必须持有低速总线锁,并且desc的inner锁和irqdata的inner锁 267e2841179SLoGin /// 必须已经释放。否则将死锁。 268e2841179SLoGin pub fn chip_bus_sync_unlock(&self) { 269e2841179SLoGin let irq_data = self.inner().irq_data.clone(); 270e2841179SLoGin irq_data 271e2841179SLoGin .chip_info_write_irqsave() 272e2841179SLoGin .chip() 273e2841179SLoGin .irq_bus_sync_unlock(&irq_data) 274e2841179SLoGin .ok(); 275e2841179SLoGin } 276e2841179SLoGin 277338f6903SLoGin pub fn set_percpu_devid_flags(&self) { 278338f6903SLoGin self.modify_status( 279338f6903SLoGin IrqLineStatus::empty(), 280338f6903SLoGin IrqLineStatus::IRQ_NOAUTOEN 281338f6903SLoGin | IrqLineStatus::IRQ_PER_CPU 282338f6903SLoGin | IrqLineStatus::IRQ_NOTHREAD 283338f6903SLoGin | IrqLineStatus::IRQ_NOPROBE 284338f6903SLoGin | IrqLineStatus::IRQ_PER_CPU_DEVID, 285338f6903SLoGin ); 286338f6903SLoGin } 287338f6903SLoGin 288e2841179SLoGin pub fn modify_status(&self, clear: IrqLineStatus, set: IrqLineStatus) { 289e2841179SLoGin let mut desc_guard = self.inner(); 290e2841179SLoGin desc_guard.line_status.remove(clear); 291e2841179SLoGin desc_guard.line_status.insert(set); 292e2841179SLoGin 293e2841179SLoGin let mut trigger = desc_guard.common_data().trigger_type(); 294e2841179SLoGin 295e2841179SLoGin desc_guard.common_data().clear_status( 296e2841179SLoGin IrqStatus::IRQD_NO_BALANCING 297e2841179SLoGin | IrqStatus::IRQD_PER_CPU 298e2841179SLoGin | IrqStatus::IRQD_TRIGGER_MASK 299e2841179SLoGin | IrqStatus::IRQD_LEVEL 300e2841179SLoGin | IrqStatus::IRQD_MOVE_PCNTXT, 301e2841179SLoGin ); 302e2841179SLoGin 303e2841179SLoGin if desc_guard 304e2841179SLoGin .line_status 305e2841179SLoGin .contains(IrqLineStatus::IRQ_NO_BALANCING) 306e2841179SLoGin { 307e2841179SLoGin desc_guard 308e2841179SLoGin .common_data() 309e2841179SLoGin .insert_status(IrqStatus::IRQD_NO_BALANCING); 310e2841179SLoGin } 311e2841179SLoGin 312e2841179SLoGin if desc_guard.line_status.contains(IrqLineStatus::IRQ_PER_CPU) { 313e2841179SLoGin desc_guard 314e2841179SLoGin .common_data() 315e2841179SLoGin .insert_status(IrqStatus::IRQD_PER_CPU); 316e2841179SLoGin } 317e2841179SLoGin 318e2841179SLoGin if desc_guard 319e2841179SLoGin .line_status 320e2841179SLoGin .contains(IrqLineStatus::IRQ_MOVE_PCNTXT) 321e2841179SLoGin { 322e2841179SLoGin desc_guard 323e2841179SLoGin .common_data() 324e2841179SLoGin .insert_status(IrqStatus::IRQD_MOVE_PCNTXT); 325e2841179SLoGin } 326e2841179SLoGin 327e2841179SLoGin if desc_guard.line_status.is_level_type() { 328e2841179SLoGin desc_guard 329e2841179SLoGin .common_data() 330e2841179SLoGin .insert_status(IrqStatus::IRQD_LEVEL); 331e2841179SLoGin } 332e2841179SLoGin 333e2841179SLoGin let tmp = desc_guard.line_status.trigger_type(); 334e2841179SLoGin 335e2841179SLoGin if tmp != IrqLineStatus::IRQ_TYPE_NONE { 336e2841179SLoGin trigger = tmp; 337e2841179SLoGin } 338e2841179SLoGin 339e2841179SLoGin desc_guard.common_data().set_trigger_type(trigger); 340e2841179SLoGin } 3413bc96fa4SLoGin } 3423bc96fa4SLoGin 3433bc96fa4SLoGin #[allow(dead_code)] 3443bc96fa4SLoGin #[derive(Debug)] 345e2841179SLoGin pub struct InnerIrqDesc { 3463bc96fa4SLoGin /// per irq and chip data passed down to chip functions 3473bc96fa4SLoGin common_data: Arc<IrqCommonData>, 3483bc96fa4SLoGin irq_data: Arc<IrqData>, 3493bc96fa4SLoGin actions: Vec<Arc<IrqAction>>, 3503bc96fa4SLoGin name: Option<String>, 3513bc96fa4SLoGin parent_irq: Option<IrqNumber>, 3523bc96fa4SLoGin /// nested irq disables 3533bc96fa4SLoGin depth: u32, 3543bc96fa4SLoGin /// nested wake enables 3553bc96fa4SLoGin wake_depth: u32, 356196b75dcSLoGin desc_internal_state: IrqDescState, 357e2841179SLoGin /// 中断线的状态 358e2841179SLoGin line_status: IrqLineStatus, 3593bc96fa4SLoGin 3603bc96fa4SLoGin kern_inode: Option<Arc<KernFSInode>>, 3613bc96fa4SLoGin kset: Option<Arc<KSet>>, 3623bc96fa4SLoGin parent_kobj: Option<Weak<dyn KObject>>, 363338f6903SLoGin /// per-cpu enabled mask 364338f6903SLoGin percpu_enabled: Option<CpuMask>, 365338f6903SLoGin /// per-cpu affinity 366338f6903SLoGin percpu_affinity: Option<CpuMask>, 367e2841179SLoGin // wait_for_threads: EventWaitQueue 368e2841179SLoGin } 369e2841179SLoGin 370e2841179SLoGin impl InnerIrqDesc { 371e2841179SLoGin pub fn name(&self) -> Option<&String> { 372e2841179SLoGin self.name.as_ref() 373e2841179SLoGin } 374e2841179SLoGin 375338f6903SLoGin #[allow(dead_code)] 376338f6903SLoGin pub fn set_name(&mut self, name: Option<String>) { 377338f6903SLoGin self.name = name; 378338f6903SLoGin } 379338f6903SLoGin 380e2841179SLoGin pub fn can_request(&self) -> bool { 381e2841179SLoGin !self.line_status.contains(IrqLineStatus::IRQ_NOREQUEST) 382e2841179SLoGin } 383e2841179SLoGin 384e2841179SLoGin #[allow(dead_code)] 385e2841179SLoGin pub fn set_norequest(&mut self) { 386e2841179SLoGin self.line_status.insert(IrqLineStatus::IRQ_NOREQUEST); 387e2841179SLoGin } 388e2841179SLoGin 389e2841179SLoGin #[allow(dead_code)] 390e2841179SLoGin pub fn clear_norequest(&mut self) { 391e2841179SLoGin self.line_status.remove(IrqLineStatus::IRQ_NOREQUEST); 392e2841179SLoGin } 393e2841179SLoGin 394338f6903SLoGin #[allow(dead_code)] 395338f6903SLoGin pub fn set_noprobe(&mut self) { 396338f6903SLoGin self.line_status.insert(IrqLineStatus::IRQ_NOPROBE); 397338f6903SLoGin } 398338f6903SLoGin 399338f6903SLoGin #[allow(dead_code)] 400338f6903SLoGin pub fn clear_noprobe(&mut self) { 401338f6903SLoGin self.line_status.remove(IrqLineStatus::IRQ_NOPROBE); 402338f6903SLoGin } 403338f6903SLoGin 404338f6903SLoGin pub fn set_nothread(&mut self) { 405338f6903SLoGin self.line_status.insert(IrqLineStatus::IRQ_NOTHREAD); 406338f6903SLoGin } 407338f6903SLoGin 408338f6903SLoGin pub fn clear_nothread(&mut self) { 409338f6903SLoGin self.line_status.remove(IrqLineStatus::IRQ_NOTHREAD); 410338f6903SLoGin } 411338f6903SLoGin 412e2841179SLoGin pub fn nested_thread(&self) -> bool { 413e2841179SLoGin self.line_status.contains(IrqLineStatus::IRQ_NESTED_THREAD) 414e2841179SLoGin } 415e2841179SLoGin 416e2841179SLoGin pub fn line_status_set_per_cpu(&mut self) { 417e2841179SLoGin self.line_status.insert(IrqLineStatus::IRQ_PER_CPU); 418e2841179SLoGin } 419e2841179SLoGin 420e2841179SLoGin #[allow(dead_code)] 421e2841179SLoGin pub fn line_status_clear_per_cpu(&mut self) { 422e2841179SLoGin self.line_status.remove(IrqLineStatus::IRQ_PER_CPU); 423e2841179SLoGin } 424e2841179SLoGin 425e2841179SLoGin #[allow(dead_code)] 426e2841179SLoGin pub fn line_status(&self) -> &IrqLineStatus { 427e2841179SLoGin &self.line_status 428e2841179SLoGin } 429e2841179SLoGin 430e2841179SLoGin pub fn line_status_set_no_debug(&mut self) { 431e2841179SLoGin self.line_status.insert(IrqLineStatus::IRQ_NO_BALANCING); 432e2841179SLoGin } 433e2841179SLoGin 434e2841179SLoGin #[allow(dead_code)] 435e2841179SLoGin pub fn line_status_clear_no_debug(&mut self) { 436e2841179SLoGin self.line_status.remove(IrqLineStatus::IRQ_NO_BALANCING); 437e2841179SLoGin } 438e2841179SLoGin 439e2841179SLoGin pub fn can_autoenable(&self) -> bool { 440e2841179SLoGin !self.line_status.contains(IrqLineStatus::IRQ_NOAUTOEN) 441e2841179SLoGin } 442e2841179SLoGin 443e2841179SLoGin pub fn can_thread(&self) -> bool { 444e2841179SLoGin !self.line_status.contains(IrqLineStatus::IRQ_NOTHREAD) 445e2841179SLoGin } 446e2841179SLoGin 447e2841179SLoGin /// 中断是否可以设置CPU亲和性 448e2841179SLoGin pub fn can_set_affinity(&self) -> bool { 449b5b571e0SLoGin if !self.common_data.status().can_balance() 450b5b571e0SLoGin || !self 451e2841179SLoGin .irq_data() 452e2841179SLoGin .chip_info_read_irqsave() 453e2841179SLoGin .chip() 454e2841179SLoGin .can_set_affinity() 455e2841179SLoGin { 456e2841179SLoGin return false; 457e2841179SLoGin } 458e2841179SLoGin 459e2841179SLoGin return true; 460e2841179SLoGin } 461e2841179SLoGin 462e2841179SLoGin pub fn actions(&self) -> &Vec<Arc<IrqAction>> { 463e2841179SLoGin &self.actions 464e2841179SLoGin } 465e2841179SLoGin 466e2841179SLoGin pub fn add_action(&mut self, action: Arc<IrqAction>) { 467e2841179SLoGin self.actions.push(action); 468e2841179SLoGin } 469e2841179SLoGin 470338f6903SLoGin pub fn clear_actions(&mut self) { 471338f6903SLoGin self.actions.clear(); 472338f6903SLoGin } 473338f6903SLoGin 474338f6903SLoGin pub fn remove_action(&mut self, action: &Arc<IrqAction>) { 475338f6903SLoGin self.actions.retain(|a| !Arc::ptr_eq(a, action)); 476338f6903SLoGin } 477338f6903SLoGin 478e2841179SLoGin pub fn internal_state(&self) -> &IrqDescState { 479e2841179SLoGin &self.desc_internal_state 480e2841179SLoGin } 481e2841179SLoGin 482e2841179SLoGin pub(super) fn internal_state_mut(&mut self) -> &mut IrqDescState { 483e2841179SLoGin &mut self.desc_internal_state 484e2841179SLoGin } 485e2841179SLoGin 486e2841179SLoGin pub fn irq_data(&self) -> &Arc<IrqData> { 487e2841179SLoGin &self.irq_data 488e2841179SLoGin } 489e2841179SLoGin 490e2841179SLoGin pub fn common_data(&self) -> &Arc<IrqCommonData> { 491e2841179SLoGin &self.common_data 492e2841179SLoGin } 493e2841179SLoGin 494e2841179SLoGin pub fn depth(&self) -> u32 { 495e2841179SLoGin self.depth 496e2841179SLoGin } 497e2841179SLoGin 498e2841179SLoGin pub fn wake_depth(&self) -> u32 { 499e2841179SLoGin self.wake_depth 500e2841179SLoGin } 501e2841179SLoGin 502e2841179SLoGin pub fn set_depth(&mut self, depth: u32) { 503e2841179SLoGin self.depth = depth; 504e2841179SLoGin } 505e2841179SLoGin 506e2841179SLoGin pub fn set_trigger_type(&mut self, trigger: IrqLineStatus) { 507e2841179SLoGin self.line_status.remove(IrqLineStatus::IRQ_TYPE_SENSE_MASK); 508e2841179SLoGin self.line_status 509e2841179SLoGin .insert(trigger & IrqLineStatus::IRQ_TYPE_SENSE_MASK); 510e2841179SLoGin } 511e2841179SLoGin 512e2841179SLoGin pub fn clear_level(&mut self) { 513e2841179SLoGin self.line_status.remove(IrqLineStatus::IRQ_LEVEL); 514e2841179SLoGin } 515e2841179SLoGin 516e2841179SLoGin pub fn set_level(&mut self) { 517e2841179SLoGin self.line_status.insert(IrqLineStatus::IRQ_LEVEL); 518e2841179SLoGin } 519338f6903SLoGin 520338f6903SLoGin pub fn percpu_enabled(&self) -> &Option<CpuMask> { 521338f6903SLoGin &self.percpu_enabled 522338f6903SLoGin } 523338f6903SLoGin 524338f6903SLoGin pub fn percpu_enabled_mut(&mut self) -> &mut Option<CpuMask> { 525338f6903SLoGin &mut self.percpu_enabled 526338f6903SLoGin } 527338f6903SLoGin 528338f6903SLoGin pub fn percpu_affinity(&self) -> &Option<CpuMask> { 529338f6903SLoGin &self.percpu_affinity 530338f6903SLoGin } 531338f6903SLoGin 532338f6903SLoGin pub fn percpu_affinity_mut(&mut self) -> &mut Option<CpuMask> { 533338f6903SLoGin &mut self.percpu_affinity 534338f6903SLoGin } 5353bc96fa4SLoGin } 5363bc96fa4SLoGin 5373bc96fa4SLoGin impl KObject for IrqDesc { 5383bc96fa4SLoGin fn as_any_ref(&self) -> &dyn Any { 5393bc96fa4SLoGin self 5403bc96fa4SLoGin } 5413bc96fa4SLoGin 5423bc96fa4SLoGin fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 5433bc96fa4SLoGin self.inner().kern_inode = inode; 5443bc96fa4SLoGin } 5453bc96fa4SLoGin 5463bc96fa4SLoGin fn inode(&self) -> Option<Arc<KernFSInode>> { 5473bc96fa4SLoGin self.inner().kern_inode.clone() 5483bc96fa4SLoGin } 5493bc96fa4SLoGin 5503bc96fa4SLoGin fn parent(&self) -> Option<Weak<dyn KObject>> { 5513bc96fa4SLoGin self.inner().parent_kobj.clone() 5523bc96fa4SLoGin } 5533bc96fa4SLoGin 5543bc96fa4SLoGin fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 5553bc96fa4SLoGin self.inner().parent_kobj = parent; 5563bc96fa4SLoGin } 5573bc96fa4SLoGin 5583bc96fa4SLoGin fn kset(&self) -> Option<Arc<KSet>> { 5593bc96fa4SLoGin self.inner().kset.clone() 5603bc96fa4SLoGin } 5613bc96fa4SLoGin 5623bc96fa4SLoGin fn set_kset(&self, kset: Option<Arc<KSet>>) { 5633bc96fa4SLoGin self.inner().kset = kset; 5643bc96fa4SLoGin } 5653bc96fa4SLoGin 5663bc96fa4SLoGin fn kobj_type(&self) -> Option<&'static dyn KObjType> { 5673bc96fa4SLoGin Some(&IrqKObjType) 5683bc96fa4SLoGin } 5693bc96fa4SLoGin 5703bc96fa4SLoGin fn set_kobj_type(&self, _ktype: Option<&'static dyn KObjType>) {} 5713bc96fa4SLoGin 5723bc96fa4SLoGin fn name(&self) -> String { 573196b75dcSLoGin self.inner().irq_data.irq().data().to_string() 5743bc96fa4SLoGin } 5753bc96fa4SLoGin 5763bc96fa4SLoGin fn set_name(&self, _name: String) {} 5773bc96fa4SLoGin 5783bc96fa4SLoGin fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 5793bc96fa4SLoGin self.kobj_state.read() 5803bc96fa4SLoGin } 5813bc96fa4SLoGin 5823bc96fa4SLoGin fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 5833bc96fa4SLoGin self.kobj_state.write() 5843bc96fa4SLoGin } 5853bc96fa4SLoGin 5863bc96fa4SLoGin fn set_kobj_state(&self, state: KObjectState) { 5873bc96fa4SLoGin *self.kobj_state_mut() = state; 5883bc96fa4SLoGin } 5893bc96fa4SLoGin } 5903bc96fa4SLoGin 591196b75dcSLoGin bitflags! { 592196b75dcSLoGin /// Bit masks for desc->desc_internal_state 593e2841179SLoGin pub struct IrqDescState: u32 { 594196b75dcSLoGin /// autodetection in progress 595196b75dcSLoGin const IRQS_AUTODETECT = 0x00000001; 596196b75dcSLoGin /// was disabled due to spurious interrupt detection 597196b75dcSLoGin const IRQS_SPURIOUS_DISABLED = 0x00000002; 598196b75dcSLoGin /// polling in progress 599196b75dcSLoGin const IRQS_POLL_INPROGRESS = 0x00000008; 600196b75dcSLoGin /// irq is not unmasked in primary handler 601196b75dcSLoGin const IRQS_ONESHOT = 0x00000020; 602196b75dcSLoGin /// irq is replayed 603196b75dcSLoGin const IRQS_REPLAY = 0x00000040; 604196b75dcSLoGin /// irq is waiting 605196b75dcSLoGin const IRQS_WAITING = 0x00000080; 606196b75dcSLoGin /// irq is pending and replayed later 607196b75dcSLoGin const IRQS_PENDING = 0x00000200; 608196b75dcSLoGin /// irq is suspended 609196b75dcSLoGin const IRQS_SUSPENDED = 0x00000800; 610196b75dcSLoGin /// irq line is used to deliver NMIs 611196b75dcSLoGin const IRQS_NMI = 0x00002000; 612196b75dcSLoGin /// descriptor has been added to sysfs 613196b75dcSLoGin const IRQS_SYSFS = 0x00004000; 614196b75dcSLoGin } 615196b75dcSLoGin } 616196b75dcSLoGin 6173bc96fa4SLoGin /// 每个中断的响应动作的描述符 6183bc96fa4SLoGin /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/include/linux/interrupt.h#118 6193bc96fa4SLoGin #[allow(dead_code)] 6203bc96fa4SLoGin #[derive(Debug)] 6213bc96fa4SLoGin pub struct IrqAction { 6223bc96fa4SLoGin inner: SpinLock<InnerIrqAction>, 623e2841179SLoGin /// 用于等待线程被创建的完成量 624e2841179SLoGin thread_completion: Completion, 6253bc96fa4SLoGin } 6263bc96fa4SLoGin 6273bc96fa4SLoGin impl IrqAction { 6283bc96fa4SLoGin #[allow(dead_code)] 6293bc96fa4SLoGin pub fn new( 6303bc96fa4SLoGin irq: IrqNumber, 6313bc96fa4SLoGin name: String, 632e2841179SLoGin handler: Option<&'static dyn IrqHandler>, 633e2841179SLoGin thread_fn: Option<&'static dyn IrqHandler>, 6343bc96fa4SLoGin ) -> Arc<Self> { 635e2841179SLoGin let action: IrqAction = IrqAction { 6363bc96fa4SLoGin inner: SpinLock::new(InnerIrqAction { 6373bc96fa4SLoGin dev_id: None, 638338f6903SLoGin per_cpu_dev_id: None, 6393bc96fa4SLoGin handler, 640e2841179SLoGin thread_fn, 6413bc96fa4SLoGin thread: None, 6423bc96fa4SLoGin secondary: None, 6433bc96fa4SLoGin irq, 6443bc96fa4SLoGin flags: IrqHandleFlags::empty(), 6453bc96fa4SLoGin name, 646e2841179SLoGin thread_flags: ThreadedHandlerFlags::empty(), 6473bc96fa4SLoGin }), 648e2841179SLoGin thread_completion: Completion::new(), 6493bc96fa4SLoGin }; 6503bc96fa4SLoGin 6513bc96fa4SLoGin return Arc::new(action); 6523bc96fa4SLoGin } 653196b75dcSLoGin 654e2841179SLoGin pub fn inner(&self) -> SpinLockGuard<InnerIrqAction> { 655e2841179SLoGin self.inner.lock_irqsave() 656196b75dcSLoGin } 657196b75dcSLoGin 658e2841179SLoGin pub fn thread_completion(&self) -> &Completion { 659e2841179SLoGin &self.thread_completion 660196b75dcSLoGin } 6613bc96fa4SLoGin } 6623bc96fa4SLoGin 6633bc96fa4SLoGin #[allow(dead_code)] 6643bc96fa4SLoGin #[derive(Debug)] 665e2841179SLoGin pub struct InnerIrqAction { 6663bc96fa4SLoGin /// cookie to identify the device 667e2841179SLoGin dev_id: Option<Arc<DeviceId>>, 668338f6903SLoGin /// cookie to identify the device (per cpu) 669338f6903SLoGin per_cpu_dev_id: Option<PerCpuVar<Arc<DeviceId>>>, 6703bc96fa4SLoGin /// 中断处理程序 671e2841179SLoGin handler: Option<&'static dyn IrqHandler>, 6723bc96fa4SLoGin /// interrupt handler function for threaded interrupts 673e2841179SLoGin thread_fn: Option<&'static dyn IrqHandler>, 6743bc96fa4SLoGin /// thread pointer for threaded interrupts 6753bc96fa4SLoGin thread: Option<Arc<ProcessControlBlock>>, 6763bc96fa4SLoGin /// pointer to secondary irqaction (force threading) 6773bc96fa4SLoGin secondary: Option<Arc<IrqAction>>, 6783bc96fa4SLoGin /// 中断号 6793bc96fa4SLoGin irq: IrqNumber, 6803bc96fa4SLoGin flags: IrqHandleFlags, 681e2841179SLoGin /// 中断线程的标志 682e2841179SLoGin thread_flags: ThreadedHandlerFlags, 6833bc96fa4SLoGin /// name of the device 6843bc96fa4SLoGin name: String, 6853bc96fa4SLoGin } 6863bc96fa4SLoGin 687e2841179SLoGin impl InnerIrqAction { 688e2841179SLoGin pub fn dev_id(&self) -> &Option<Arc<DeviceId>> { 689e2841179SLoGin &self.dev_id 690e2841179SLoGin } 691e2841179SLoGin 692e2841179SLoGin pub fn dev_id_mut(&mut self) -> &mut Option<Arc<DeviceId>> { 693e2841179SLoGin &mut self.dev_id 694e2841179SLoGin } 695e2841179SLoGin 696338f6903SLoGin pub fn per_cpu_dev_id(&self) -> Option<&Arc<DeviceId>> { 697338f6903SLoGin self.per_cpu_dev_id.as_ref().map(|v| v.get()) 698338f6903SLoGin } 699338f6903SLoGin 700338f6903SLoGin #[allow(dead_code)] 701338f6903SLoGin pub fn per_cpu_dev_id_mut(&mut self) -> Option<&mut Arc<DeviceId>> { 702338f6903SLoGin self.per_cpu_dev_id.as_mut().map(|v| v.get_mut()) 703338f6903SLoGin } 704338f6903SLoGin 705e2841179SLoGin pub fn handler(&self) -> Option<&'static dyn IrqHandler> { 706e2841179SLoGin self.handler 707e2841179SLoGin } 708e2841179SLoGin 709e2841179SLoGin pub fn set_handler(&mut self, handler: Option<&'static dyn IrqHandler>) { 710e2841179SLoGin self.handler = handler; 711e2841179SLoGin } 712e2841179SLoGin 713e2841179SLoGin pub fn thread_fn(&self) -> Option<&'static dyn IrqHandler> { 714e2841179SLoGin self.thread_fn 715e2841179SLoGin } 716e2841179SLoGin 717e2841179SLoGin pub fn thread(&self) -> Option<Arc<ProcessControlBlock>> { 718e2841179SLoGin self.thread.clone() 719e2841179SLoGin } 720e2841179SLoGin 721e2841179SLoGin pub fn set_thread(&mut self, thread: Option<Arc<ProcessControlBlock>>) { 722e2841179SLoGin self.thread = thread; 723e2841179SLoGin } 724e2841179SLoGin 725e2841179SLoGin #[allow(dead_code)] 726e2841179SLoGin pub fn thread_flags(&self) -> &ThreadedHandlerFlags { 727e2841179SLoGin &self.thread_flags 728e2841179SLoGin } 729e2841179SLoGin 730e2841179SLoGin pub fn thread_flags_mut(&mut self) -> &mut ThreadedHandlerFlags { 731e2841179SLoGin &mut self.thread_flags 732e2841179SLoGin } 733e2841179SLoGin 734e2841179SLoGin pub fn secondary(&self) -> Option<Arc<IrqAction>> { 735e2841179SLoGin self.secondary.clone() 736e2841179SLoGin } 737e2841179SLoGin 738e2841179SLoGin #[allow(dead_code)] 739e2841179SLoGin pub fn irq(&self) -> IrqNumber { 740e2841179SLoGin self.irq 741e2841179SLoGin } 742e2841179SLoGin 743e2841179SLoGin #[allow(dead_code)] 744e2841179SLoGin pub fn set_irq(&mut self, irq: IrqNumber) { 745e2841179SLoGin self.irq = irq; 746e2841179SLoGin } 747e2841179SLoGin 748e2841179SLoGin pub fn flags(&self) -> &IrqHandleFlags { 749e2841179SLoGin &self.flags 750e2841179SLoGin } 751e2841179SLoGin 752e2841179SLoGin pub fn flags_mut(&mut self) -> &mut IrqHandleFlags { 753e2841179SLoGin &mut self.flags 754e2841179SLoGin } 755e2841179SLoGin 756e2841179SLoGin pub fn name(&self) -> &String { 757e2841179SLoGin &self.name 758e2841179SLoGin } 759e2841179SLoGin } 760e2841179SLoGin 761e2841179SLoGin bitflags! { 762e2841179SLoGin /// 这些标志由线程处理程序使用 763e2841179SLoGin pub struct ThreadedHandlerFlags: u32 { 764e2841179SLoGin /// IRQTF_RUNTHREAD - 表示应运行中断处理程序线程 765e2841179SLoGin const IRQTF_RUNTHREAD = 1 << 0; 766e2841179SLoGin /// IRQTF_WARNED - 已打印警告 "IRQ_WAKE_THREAD w/o thread_fn" 767e2841179SLoGin const IRQTF_WARNED = 1 << 1; 768e2841179SLoGin /// IRQTF_AFFINITY - 请求irq线程调整亲和性 769e2841179SLoGin const IRQTF_AFFINITY = 1 << 2; 770e2841179SLoGin /// IRQTF_FORCED_THREAD - irq操作被强制线程化 771e2841179SLoGin const IRQTF_FORCED_THREAD = 1 << 3; 772e2841179SLoGin /// IRQTF_READY - 表示irq线程已准备就绪 773e2841179SLoGin const IRQTF_READY = 1 << 4; 774e2841179SLoGin } 775e2841179SLoGin } 776e2841179SLoGin 777e2841179SLoGin /// Implements the `ThreadedHandlerFlags` structure. 778e2841179SLoGin impl ThreadedHandlerFlags { 779e2841179SLoGin /// 在 `ThreadedHandlerFlags` 结构中测试并设置特定的位。 780e2841179SLoGin /// 781e2841179SLoGin /// # 参数 782e2841179SLoGin /// 783e2841179SLoGin /// * `bit` - 要测试并设置的位。 784e2841179SLoGin /// 785e2841179SLoGin /// # 返回 786e2841179SLoGin /// 787e2841179SLoGin /// 如果操作前该位已被设置,则返回 `true`,否则返回 `false`。 788e2841179SLoGin pub fn test_and_set_bit(&mut self, bit: ThreadedHandlerFlags) -> bool { 789e2841179SLoGin let res = (self.bits & bit.bits) != 0; 790e2841179SLoGin self.bits |= bit.bits; 791e2841179SLoGin return res; 792e2841179SLoGin } 793e2841179SLoGin } 794e2841179SLoGin 7953bc96fa4SLoGin // 定义IrqFlags位标志 796b5b571e0SLoGin 7973bc96fa4SLoGin bitflags! { 7983bc96fa4SLoGin /// 这些标志仅由内核在中断处理例程中使用。 799b5b571e0SLoGin #[allow(clippy::bad_bit_mask)] 8003bc96fa4SLoGin pub struct IrqHandleFlags: u32 { 801e2841179SLoGin 802e2841179SLoGin const IRQF_TRIGGER_NONE = IrqLineStatus::IRQ_TYPE_NONE.bits(); 803e2841179SLoGin const IRQF_TRIGGER_RISING = IrqLineStatus::IRQ_TYPE_EDGE_RISING.bits(); 804e2841179SLoGin const IRQF_TRIGGER_FALLING = IrqLineStatus::IRQ_TYPE_EDGE_FALLING.bits(); 805e2841179SLoGin const IRQF_TRIGGER_HIGH = IrqLineStatus::IRQ_TYPE_LEVEL_HIGH.bits(); 806e2841179SLoGin const IRQF_TRIGGER_LOW = IrqLineStatus::IRQ_TYPE_LEVEL_LOW.bits(); 807e2841179SLoGin const IRQF_TRIGGER_MASK = Self::IRQF_TRIGGER_HIGH.bits | Self::IRQF_TRIGGER_LOW.bits | Self::IRQF_TRIGGER_RISING.bits | Self::IRQF_TRIGGER_FALLING.bits; 8083bc96fa4SLoGin /// IRQF_SHARED - 允许多个设备共享中断 8093bc96fa4SLoGin const IRQF_SHARED = 0x00000080; 8103bc96fa4SLoGin /// IRQF_PROBE_SHARED - 当预期出现共享不匹配时,由调用者设置 8113bc96fa4SLoGin const IRQF_PROBE_SHARED = 0x00000100; 8123bc96fa4SLoGin /// IRQF_TIMER - 标记此中断为定时器中断 8133bc96fa4SLoGin const __IRQF_TIMER = 0x00000200; 8143bc96fa4SLoGin /// IRQF_PERCPU - 中断是每个CPU的 8153bc96fa4SLoGin const IRQF_PERCPU = 0x00000400; 8163bc96fa4SLoGin /// IRQF_NOBALANCING - 将此中断从中断平衡中排除 8173bc96fa4SLoGin const IRQF_NOBALANCING = 0x00000800; 8183bc96fa4SLoGin /// IRQF_IRQPOLL - 中断用于轮询(出于性能原因,只有在共享中断中首次注册的中断会被考虑) 8193bc96fa4SLoGin const IRQF_IRQPOLL = 0x00001000; 8203bc96fa4SLoGin /// IRQF_ONESHOT - 在硬中断处理程序完成后,不会重新启用中断。由需要在运行线程处理程序之前保持中断线路禁用的线程中断使用。 8213bc96fa4SLoGin const IRQF_ONESHOT = 0x00002000; 8223bc96fa4SLoGin /// IRQF_NO_SUSPEND - 在挂起期间不禁用此IRQ。不能保证此中断会从挂起状态唤醒系统。 8233bc96fa4SLoGin const IRQF_NO_SUSPEND = 0x00004000; 8243bc96fa4SLoGin /// IRQF_FORCE_RESUME - 即使设置了IRQF_NO_SUSPEND,也强制在恢复时启用它 8253bc96fa4SLoGin const IRQF_FORCE_RESUME = 0x00008000; 8263bc96fa4SLoGin /// IRQF_NO_THREAD - 中断不能被线程化 8273bc96fa4SLoGin const IRQF_NO_THREAD = 0x00010000; 8283bc96fa4SLoGin /// IRQF_EARLY_RESUME - 在syscore而不是在设备恢复时间早期恢复IRQ。 8293bc96fa4SLoGin const IRQF_EARLY_RESUME = 0x00020000; 8303bc96fa4SLoGin /// IRQF_COND_SUSPEND - 如果IRQ与NO_SUSPEND用户共享,则在挂起中断后执行此中断处理程序。对于系统唤醒设备用户,需要在他们的中断处理程序中实现唤醒检测。 8313bc96fa4SLoGin const IRQF_COND_SUSPEND = 0x00040000; 8323bc96fa4SLoGin /// IRQF_NO_AUTOEN - 当用户请求时,不会自动启用IRQ或NMI。用户稍后会通过enable_irq()或enable_nmi()显式启用它。 8333bc96fa4SLoGin const IRQF_NO_AUTOEN = 0x00080000; 8343bc96fa4SLoGin /// IRQF_NO_DEBUG - 从IPI和类似处理程序的逃逸检测中排除,取决于IRQF_PERCPU。 8353bc96fa4SLoGin const IRQF_NO_DEBUG = 0x00100000; 8363bc96fa4SLoGin const IRQF_TIMER = Self::__IRQF_TIMER.bits | Self::IRQF_NO_SUSPEND.bits | Self::IRQF_NO_THREAD.bits; 8373bc96fa4SLoGin } 8383bc96fa4SLoGin } 8393bc96fa4SLoGin 840e2841179SLoGin impl IrqHandleFlags { 841e2841179SLoGin /// 检查是否指定了触发类型 842e2841179SLoGin #[inline(always)] 843e2841179SLoGin pub fn trigger_type_specified(&self) -> bool { 844e2841179SLoGin (self.bits & Self::IRQF_TRIGGER_MASK.bits) != 0 845e2841179SLoGin } 846e2841179SLoGin 847e2841179SLoGin /// 插入触发类型 848e2841179SLoGin pub fn insert_trigger_type(&mut self, trigger: IrqLineStatus) { 849e2841179SLoGin self.bits |= trigger.trigger_bits() & IrqHandleFlags::IRQF_TRIGGER_MASK.bits; 850e2841179SLoGin } 851e2841179SLoGin 852e2841179SLoGin #[allow(dead_code)] 853e2841179SLoGin pub fn remove_trigger_type(&mut self, trigger: IrqLineStatus) { 854e2841179SLoGin self.bits &= !(trigger.trigger_bits() & IrqHandleFlags::IRQF_TRIGGER_MASK.bits); 855e2841179SLoGin } 856e2841179SLoGin 857e2841179SLoGin pub fn trigger_type(&self) -> IrqLineStatus { 858e2841179SLoGin IrqLineStatus::from_bits_truncate(self.bits & IrqHandleFlags::IRQF_TRIGGER_MASK.bits) 859e2841179SLoGin } 860e2841179SLoGin } 861e2841179SLoGin 8623bc96fa4SLoGin #[inline(never)] 8633bc96fa4SLoGin pub(super) fn early_irq_init() -> Result<(), SystemError> { 8643bc96fa4SLoGin let irqcnt = CurrentIrqArch::probe_total_irq_num(); 8653bc96fa4SLoGin let mut manager = IrqDescManager::new(); 8663bc96fa4SLoGin for i in 0..irqcnt { 8673bc96fa4SLoGin let irq_desc = IrqDesc::new(IrqNumber::new(i), None, IrqStatus::empty()); 8683bc96fa4SLoGin manager.insert(IrqNumber::new(i), irq_desc); 8693bc96fa4SLoGin } 8703bc96fa4SLoGin 871196b75dcSLoGin unsafe { 872196b75dcSLoGin IRQ_DESC_MANAGER = Some(manager); 873196b75dcSLoGin } 874196b75dcSLoGin 8753bc96fa4SLoGin return CurrentIrqArch::arch_early_irq_init(); 8763bc96fa4SLoGin } 8773bc96fa4SLoGin 878196b75dcSLoGin static mut IRQ_DESC_MANAGER: Option<IrqDescManager> = None; 879196b75dcSLoGin 880196b75dcSLoGin /// 获取中断描述符管理器的引用 881196b75dcSLoGin #[inline(always)] 882e2841179SLoGin pub fn irq_desc_manager() -> &'static IrqDescManager { 883196b75dcSLoGin return unsafe { IRQ_DESC_MANAGER.as_ref().unwrap() }; 884196b75dcSLoGin } 885196b75dcSLoGin 886e2841179SLoGin pub struct IrqDescManager { 8873bc96fa4SLoGin irq_descs: BTreeMap<IrqNumber, Arc<IrqDesc>>, 8883bc96fa4SLoGin } 8893bc96fa4SLoGin 8903bc96fa4SLoGin impl IrqDescManager { 8913bc96fa4SLoGin fn new() -> Self { 8923bc96fa4SLoGin IrqDescManager { 8933bc96fa4SLoGin irq_descs: BTreeMap::new(), 8943bc96fa4SLoGin } 8953bc96fa4SLoGin } 8963bc96fa4SLoGin 8973bc96fa4SLoGin /// 查找中断描述符 8983bc96fa4SLoGin pub fn lookup(&self, irq: IrqNumber) -> Option<Arc<IrqDesc>> { 899b5b571e0SLoGin self.irq_descs.get(&irq).cloned() 9003bc96fa4SLoGin } 9013bc96fa4SLoGin 902338f6903SLoGin /// 查找中断描述符并锁定总线(没有对irqdesc进行加锁) 903338f6903SLoGin #[allow(dead_code)] 904338f6903SLoGin pub fn lookup_and_lock_bus( 905338f6903SLoGin &self, 906338f6903SLoGin irq: IrqNumber, 907338f6903SLoGin check_global: bool, 908338f6903SLoGin check_percpu: bool, 909338f6903SLoGin ) -> Option<Arc<IrqDesc>> { 910338f6903SLoGin self.do_lookup_and_lock(irq, true, check_global, check_percpu) 911338f6903SLoGin } 912338f6903SLoGin 913338f6903SLoGin fn do_lookup_and_lock( 914338f6903SLoGin &self, 915338f6903SLoGin irq: IrqNumber, 916338f6903SLoGin lock_bus: bool, 917338f6903SLoGin check_global: bool, 918338f6903SLoGin check_percpu: bool, 919338f6903SLoGin ) -> Option<Arc<IrqDesc>> { 920338f6903SLoGin let desc = self.lookup(irq)?; 921338f6903SLoGin if check_global || check_percpu { 922338f6903SLoGin if check_percpu && !desc.inner().line_status().is_per_cpu_devid() { 923338f6903SLoGin return None; 924338f6903SLoGin } 925338f6903SLoGin 926338f6903SLoGin if check_global && desc.inner().line_status().is_per_cpu_devid() { 927338f6903SLoGin return None; 928338f6903SLoGin } 929338f6903SLoGin } 930338f6903SLoGin 931338f6903SLoGin if lock_bus { 932338f6903SLoGin desc.chip_bus_lock(); 933338f6903SLoGin } 934338f6903SLoGin 935338f6903SLoGin return Some(desc); 936338f6903SLoGin } 937338f6903SLoGin 9383bc96fa4SLoGin fn insert(&mut self, irq: IrqNumber, desc: Arc<IrqDesc>) { 9393bc96fa4SLoGin self.irq_descs.insert(irq, desc); 9403bc96fa4SLoGin } 941196b75dcSLoGin 942196b75dcSLoGin /// 释放中断描述符 943196b75dcSLoGin #[allow(dead_code)] 944196b75dcSLoGin fn free_desc(&mut self, irq: IrqNumber) { 945196b75dcSLoGin if let Some(desc) = self.irq_descs.get(&irq) { 946196b75dcSLoGin irq_sysfs_del(desc); 947196b75dcSLoGin self.irq_descs.remove(&irq); 948196b75dcSLoGin } 949196b75dcSLoGin } 950196b75dcSLoGin 951196b75dcSLoGin /// 迭代中断描述符 952196b75dcSLoGin pub fn iter_descs(&self) -> btree_map::Iter<'_, IrqNumber, Arc<IrqDesc>> { 953196b75dcSLoGin self.irq_descs.iter() 954196b75dcSLoGin } 955338f6903SLoGin 956338f6903SLoGin /// 设置指定irq的可用cpu为所有cpu 957338f6903SLoGin pub fn set_percpu_devid_all(&self, irq: IrqNumber) -> Result<(), SystemError> { 958338f6903SLoGin self.set_percpu_devid(irq, None) 959338f6903SLoGin } 960338f6903SLoGin 961338f6903SLoGin /// 设置指定irq的可用cpu 962338f6903SLoGin /// 963338f6903SLoGin /// 如果affinity为None,则表示设置为所有cpu 964338f6903SLoGin pub fn set_percpu_devid( 965338f6903SLoGin &self, 966338f6903SLoGin irq: IrqNumber, 967338f6903SLoGin affinity: Option<&CpuMask>, 968338f6903SLoGin ) -> Result<(), SystemError> { 969338f6903SLoGin let desc = self.lookup(irq).ok_or(SystemError::EINVAL)?; 970338f6903SLoGin let mut desc_inner = desc.inner(); 971338f6903SLoGin 972338f6903SLoGin if desc_inner.percpu_enabled().is_some() { 973338f6903SLoGin return Err(SystemError::EINVAL); 974338f6903SLoGin } 975338f6903SLoGin 976338f6903SLoGin *desc_inner.percpu_enabled_mut() = Some(CpuMask::new()); 977338f6903SLoGin 978338f6903SLoGin if let Some(affinity) = affinity { 979338f6903SLoGin desc_inner.percpu_affinity_mut().replace(affinity.clone()); 980338f6903SLoGin } else { 981338f6903SLoGin desc_inner 982338f6903SLoGin .percpu_affinity_mut() 983338f6903SLoGin .replace(smp_cpu_manager().possible_cpus().clone()); 984338f6903SLoGin } 985338f6903SLoGin 986338f6903SLoGin drop(desc_inner); 987338f6903SLoGin 988338f6903SLoGin desc.set_percpu_devid_flags(); 989338f6903SLoGin 990338f6903SLoGin return Ok(()); 991338f6903SLoGin } 9923bc96fa4SLoGin } 993*f049d1afSLoGin 994*f049d1afSLoGin pub struct GenericIrqHandler; 995*f049d1afSLoGin 996*f049d1afSLoGin #[allow(dead_code)] 997*f049d1afSLoGin impl GenericIrqHandler { 998*f049d1afSLoGin /// `handle_domain_irq` - 调用属于某个中断域的硬件中断的处理程序 999*f049d1afSLoGin /// 1000*f049d1afSLoGin /// # 参数 1001*f049d1afSLoGin /// 1002*f049d1afSLoGin /// * `domain`: 执行查找的域 1003*f049d1afSLoGin /// * `hwirq`: 要转换为逻辑中断的硬件中断号 1004*f049d1afSLoGin /// 1005*f049d1afSLoGin /// # 返回 1006*f049d1afSLoGin /// 1007*f049d1afSLoGin /// 成功时返回 `Ok(())`,如果转换失败则返回 `Err(SystemError)` 1008*f049d1afSLoGin /// 1009*f049d1afSLoGin /// 此函数必须在初始化了中断寄存器的中断上下文中调用 1010*f049d1afSLoGin /// 1011*f049d1afSLoGin /// 参考 https://code.dragonos.org.cn/xref/linux-6.6.21/kernel/irq/irqdesc.c?fi=generic_handle_domain_irq#726 1012*f049d1afSLoGin pub fn handle_domain_irq( 1013*f049d1afSLoGin domain: Arc<IrqDomain>, 1014*f049d1afSLoGin hwirq: HardwareIrqNumber, 1015*f049d1afSLoGin trap_frame: &mut TrapFrame, 1016*f049d1afSLoGin ) -> Result<(), SystemError> { 1017*f049d1afSLoGin let (irq_desc, _) = 1018*f049d1afSLoGin irq_domain_manager().resolve_irq_mapping(Some(domain.clone()), hwirq)?; 1019*f049d1afSLoGin 1020*f049d1afSLoGin irq_desc.handler().unwrap().handle(&irq_desc, trap_frame); 1021*f049d1afSLoGin 1022*f049d1afSLoGin return Ok(()); 1023*f049d1afSLoGin } 1024*f049d1afSLoGin } 1025