1*e2841179SLoGin use core::{ 2*e2841179SLoGin any::Any, 3*e2841179SLoGin fmt::Debug, 4*e2841179SLoGin sync::atomic::{AtomicI64, Ordering}, 5*e2841179SLoGin }; 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::{ 16*e2841179SLoGin 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*e2841179SLoGin mutex::{Mutex, MutexGuard}, 25*e2841179SLoGin rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard}, 263bc96fa4SLoGin spinlock::{SpinLock, SpinLockGuard}, 273bc96fa4SLoGin }, 283bc96fa4SLoGin process::ProcessControlBlock, 29*e2841179SLoGin sched::completion::Completion, 303bc96fa4SLoGin }; 313bc96fa4SLoGin 323bc96fa4SLoGin use super::{ 333bc96fa4SLoGin dummychip::no_irq_chip, 343bc96fa4SLoGin handle::bad_irq_handler, 35*e2841179SLoGin irqdata::{IrqCommonData, IrqData, IrqHandlerData, IrqLineStatus, IrqStatus}, 36196b75dcSLoGin sysfs::{irq_sysfs_del, IrqKObjType}, 373bc96fa4SLoGin HardwareIrqNumber, InterruptArch, IrqNumber, 383bc96fa4SLoGin }; 393bc96fa4SLoGin 403bc96fa4SLoGin /// 中断流处理程序 41*e2841179SLoGin pub trait IrqFlowHandler: Debug + Send + Sync + Any { 42*e2841179SLoGin fn handle(&self, irq_desc: &Arc<IrqDesc>, trap_frame: &mut TrapFrame); 43*e2841179SLoGin } 44*e2841179SLoGin 45*e2841179SLoGin /// 中断处理程序 46*e2841179SLoGin pub trait IrqHandler: Debug + Send + Sync + Any { 47*e2841179SLoGin fn handle( 48*e2841179SLoGin &self, 49*e2841179SLoGin irq: IrqNumber, 50*e2841179SLoGin static_data: Option<&dyn IrqHandlerData>, 51*e2841179SLoGin dynamic_data: Option<Arc<dyn IrqHandlerData>>, 52*e2841179SLoGin ) -> Result<IrqReturn, SystemError>; 53*e2841179SLoGin } 54*e2841179SLoGin 55*e2841179SLoGin /// 中断处理函数返回值 56*e2841179SLoGin /// 57*e2841179SLoGin /// 用于指示中断处理函数是否处理了中断 58*e2841179SLoGin #[derive(Debug, Clone, Copy, PartialEq, Eq)] 59*e2841179SLoGin pub enum IrqReturn { 60*e2841179SLoGin /// 中断未被处理 61*e2841179SLoGin NotHandled, 62*e2841179SLoGin /// 中断已被处理 63*e2841179SLoGin Handled, 64*e2841179SLoGin /// 中断已被处理,并且需要唤醒中断线程 65*e2841179SLoGin WakeThread, 663bc96fa4SLoGin } 673bc96fa4SLoGin 683bc96fa4SLoGin /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/include/linux/irqdesc.h#55 693bc96fa4SLoGin #[derive(Debug)] 703bc96fa4SLoGin pub struct IrqDesc { 713bc96fa4SLoGin inner: SpinLock<InnerIrqDesc>, 723bc96fa4SLoGin 73*e2841179SLoGin handler: RwLock<Option<&'static dyn IrqFlowHandler>>, 74*e2841179SLoGin /// 一个用于串行化 request_irq()和free_irq() 的互斥锁 75*e2841179SLoGin request_mutex: Mutex<()>, 763bc96fa4SLoGin kobj_state: LockedKObjectState, 77*e2841179SLoGin /// 当前描述符内正在运行的中断线程数 78*e2841179SLoGin threads_active: AtomicI64, 793bc96fa4SLoGin } 803bc96fa4SLoGin 813bc96fa4SLoGin impl IrqDesc { 823bc96fa4SLoGin #[inline(never)] 833bc96fa4SLoGin pub fn new(irq: IrqNumber, name: Option<String>, irqd_flags: IrqStatus) -> Arc<Self> { 843bc96fa4SLoGin // 初始化的过程参考 https://code.dragonos.org.cn/xref/linux-6.1.9/kernel/irq/irqdesc.c#392 853bc96fa4SLoGin let common_data = Arc::new(IrqCommonData::new()); 863bc96fa4SLoGin let irq_data = Arc::new(IrqData::new( 873bc96fa4SLoGin irq, 883bc96fa4SLoGin HardwareIrqNumber::new(irq.data()), 893bc96fa4SLoGin common_data.clone(), 903bc96fa4SLoGin no_irq_chip(), 913bc96fa4SLoGin )); 923bc96fa4SLoGin 933bc96fa4SLoGin irq_data.irqd_set(IrqStatus::IRQD_IRQ_DISABLED); 94*e2841179SLoGin common_data.insert_status(IrqStatus::IRQD_IRQ_MASKED); 953bc96fa4SLoGin 963bc96fa4SLoGin let irq_desc = IrqDesc { 973bc96fa4SLoGin inner: SpinLock::new(InnerIrqDesc { 983bc96fa4SLoGin common_data, 993bc96fa4SLoGin irq_data, 100196b75dcSLoGin desc_internal_state: IrqDescState::empty(), 101*e2841179SLoGin line_status: IrqLineStatus::empty(), 1023bc96fa4SLoGin actions: Vec::new(), 1033bc96fa4SLoGin name, 1043bc96fa4SLoGin parent_irq: None, 1053bc96fa4SLoGin depth: 1, 1063bc96fa4SLoGin wake_depth: 0, 1073bc96fa4SLoGin kern_inode: None, 1083bc96fa4SLoGin kset: None, 1093bc96fa4SLoGin parent_kobj: None, 1103bc96fa4SLoGin }), 111*e2841179SLoGin request_mutex: Mutex::new(()), 112*e2841179SLoGin handler: RwLock::new(None), 1133bc96fa4SLoGin kobj_state: LockedKObjectState::new(Some(KObjectState::INITIALIZED)), 114*e2841179SLoGin threads_active: AtomicI64::new(0), 1153bc96fa4SLoGin }; 1163bc96fa4SLoGin 1173bc96fa4SLoGin irq_desc.set_handler(bad_irq_handler()); 1183bc96fa4SLoGin irq_desc.inner().irq_data.irqd_set(irqd_flags); 1193bc96fa4SLoGin 1203bc96fa4SLoGin return Arc::new(irq_desc); 1213bc96fa4SLoGin } 1223bc96fa4SLoGin 123*e2841179SLoGin /// 返回当前活跃的中断线程数量 124*e2841179SLoGin #[allow(dead_code)] 125*e2841179SLoGin pub fn threads_active(&self) -> i64 { 126*e2841179SLoGin self.threads_active.load(Ordering::SeqCst) 1273bc96fa4SLoGin } 1283bc96fa4SLoGin 129*e2841179SLoGin /// 增加当前活跃的中断线程数量, 返回增加前的值 130*e2841179SLoGin pub fn inc_threads_active(&self) -> i64 { 131*e2841179SLoGin self.threads_active.fetch_add(1, Ordering::SeqCst) 132*e2841179SLoGin } 133*e2841179SLoGin 134*e2841179SLoGin /// 减少当前活跃的中断线程数量, 返回减少前的值 135*e2841179SLoGin #[allow(dead_code)] 136*e2841179SLoGin pub fn dec_threads_active(&self) -> i64 { 137*e2841179SLoGin self.threads_active.fetch_sub(1, Ordering::SeqCst) 138*e2841179SLoGin } 139*e2841179SLoGin 140*e2841179SLoGin pub fn set_handler(&self, handler: &'static dyn IrqFlowHandler) { 141*e2841179SLoGin self.chip_bus_lock(); 142*e2841179SLoGin let mut guard = self.handler.write_irqsave(); 143*e2841179SLoGin *guard = Some(handler); 144*e2841179SLoGin self.chip_bus_sync_unlock(); 145*e2841179SLoGin } 146*e2841179SLoGin 147*e2841179SLoGin pub fn handler(&self) -> Option<&'static dyn IrqFlowHandler> { 148*e2841179SLoGin let guard = self.handler.read_irqsave(); 149*e2841179SLoGin *guard 150*e2841179SLoGin } 151*e2841179SLoGin 152*e2841179SLoGin pub fn inner(&self) -> SpinLockGuard<InnerIrqDesc> { 1533bc96fa4SLoGin self.inner.lock_irqsave() 1543bc96fa4SLoGin } 1553bc96fa4SLoGin 156196b75dcSLoGin pub fn actions(&self) -> Vec<Arc<IrqAction>> { 157196b75dcSLoGin self.inner().actions.clone() 158196b75dcSLoGin } 159196b75dcSLoGin 160*e2841179SLoGin /// 对中断请求过程加锁 161*e2841179SLoGin pub fn request_mutex_lock(&self) -> MutexGuard<()> { 162*e2841179SLoGin self.request_mutex.lock() 163*e2841179SLoGin } 164*e2841179SLoGin 1653bc96fa4SLoGin pub fn irq(&self) -> IrqNumber { 1663bc96fa4SLoGin self.inner().irq_data.irq() 1673bc96fa4SLoGin } 168196b75dcSLoGin 169196b75dcSLoGin pub fn hardware_irq(&self) -> HardwareIrqNumber { 170196b75dcSLoGin self.inner().irq_data.hardware_irq() 171196b75dcSLoGin } 172196b75dcSLoGin 173196b75dcSLoGin pub fn irq_data(&self) -> Arc<IrqData> { 174196b75dcSLoGin self.inner().irq_data.clone() 175196b75dcSLoGin } 176196b75dcSLoGin 177196b75dcSLoGin /// 标记当前irq描述符已经被添加到sysfs 178196b75dcSLoGin pub fn mark_in_sysfs(&self) { 179196b75dcSLoGin self.inner() 180196b75dcSLoGin .desc_internal_state 181196b75dcSLoGin .insert(IrqDescState::IRQS_SYSFS); 182196b75dcSLoGin } 183196b75dcSLoGin 184196b75dcSLoGin pub fn mark_not_in_sysfs(&self) { 185196b75dcSLoGin self.inner() 186196b75dcSLoGin .desc_internal_state 187196b75dcSLoGin .remove(IrqDescState::IRQS_SYSFS); 188196b75dcSLoGin } 189196b75dcSLoGin 190196b75dcSLoGin /// 判断当前描述符是否已经添加到了sysfs 191196b75dcSLoGin pub fn in_sysfs(&self) -> bool { 192196b75dcSLoGin self.inner() 193196b75dcSLoGin .desc_internal_state 194196b75dcSLoGin .contains(IrqDescState::IRQS_SYSFS) 195196b75dcSLoGin } 196196b75dcSLoGin 197196b75dcSLoGin pub fn name(&self) -> Option<String> { 198196b75dcSLoGin self.inner().name.clone() 199196b75dcSLoGin } 200*e2841179SLoGin 201*e2841179SLoGin pub fn can_request(&self) -> bool { 202*e2841179SLoGin self.inner().can_request() 203*e2841179SLoGin } 204*e2841179SLoGin 205*e2841179SLoGin #[allow(dead_code)] 206*e2841179SLoGin pub fn set_norequest(&self) { 207*e2841179SLoGin self.inner().set_norequest(); 208*e2841179SLoGin } 209*e2841179SLoGin 210*e2841179SLoGin #[allow(dead_code)] 211*e2841179SLoGin pub fn clear_norequest(&self) { 212*e2841179SLoGin self.inner().clear_norequest(); 213*e2841179SLoGin } 214*e2841179SLoGin 215*e2841179SLoGin pub fn nested_thread(&self) -> bool { 216*e2841179SLoGin self.inner().nested_thread() 217*e2841179SLoGin } 218*e2841179SLoGin 219*e2841179SLoGin /// 中断是否可以线程化 220*e2841179SLoGin pub fn can_thread(&self) -> bool { 221*e2841179SLoGin !self 222*e2841179SLoGin .inner() 223*e2841179SLoGin .line_status 224*e2841179SLoGin .contains(IrqLineStatus::IRQ_NOTHREAD) 225*e2841179SLoGin } 226*e2841179SLoGin 227*e2841179SLoGin pub fn chip_bus_lock(&self) { 228*e2841179SLoGin let irq_data = self.inner().irq_data.clone(); 229*e2841179SLoGin irq_data 230*e2841179SLoGin .chip_info_read_irqsave() 231*e2841179SLoGin .chip() 232*e2841179SLoGin .irq_bus_lock(&irq_data) 233*e2841179SLoGin .ok(); 234*e2841179SLoGin } 235*e2841179SLoGin 236*e2841179SLoGin /// 同步释放低速总线锁 237*e2841179SLoGin /// 238*e2841179SLoGin /// ## 锁 239*e2841179SLoGin /// 240*e2841179SLoGin /// 进入此函数时,必须持有低速总线锁,并且desc的inner锁和irqdata的inner锁 241*e2841179SLoGin /// 必须已经释放。否则将死锁。 242*e2841179SLoGin pub fn chip_bus_sync_unlock(&self) { 243*e2841179SLoGin let irq_data = self.inner().irq_data.clone(); 244*e2841179SLoGin irq_data 245*e2841179SLoGin .chip_info_write_irqsave() 246*e2841179SLoGin .chip() 247*e2841179SLoGin .irq_bus_sync_unlock(&irq_data) 248*e2841179SLoGin .ok(); 249*e2841179SLoGin } 250*e2841179SLoGin 251*e2841179SLoGin pub fn modify_status(&self, clear: IrqLineStatus, set: IrqLineStatus) { 252*e2841179SLoGin let mut desc_guard = self.inner(); 253*e2841179SLoGin desc_guard.line_status.remove(clear); 254*e2841179SLoGin desc_guard.line_status.insert(set); 255*e2841179SLoGin 256*e2841179SLoGin let mut trigger = desc_guard.common_data().trigger_type(); 257*e2841179SLoGin 258*e2841179SLoGin desc_guard.common_data().clear_status( 259*e2841179SLoGin IrqStatus::IRQD_NO_BALANCING 260*e2841179SLoGin | IrqStatus::IRQD_PER_CPU 261*e2841179SLoGin | IrqStatus::IRQD_TRIGGER_MASK 262*e2841179SLoGin | IrqStatus::IRQD_LEVEL 263*e2841179SLoGin | IrqStatus::IRQD_MOVE_PCNTXT, 264*e2841179SLoGin ); 265*e2841179SLoGin 266*e2841179SLoGin if desc_guard 267*e2841179SLoGin .line_status 268*e2841179SLoGin .contains(IrqLineStatus::IRQ_NO_BALANCING) 269*e2841179SLoGin { 270*e2841179SLoGin desc_guard 271*e2841179SLoGin .common_data() 272*e2841179SLoGin .insert_status(IrqStatus::IRQD_NO_BALANCING); 273*e2841179SLoGin } 274*e2841179SLoGin 275*e2841179SLoGin if desc_guard.line_status.contains(IrqLineStatus::IRQ_PER_CPU) { 276*e2841179SLoGin desc_guard 277*e2841179SLoGin .common_data() 278*e2841179SLoGin .insert_status(IrqStatus::IRQD_PER_CPU); 279*e2841179SLoGin } 280*e2841179SLoGin 281*e2841179SLoGin if desc_guard 282*e2841179SLoGin .line_status 283*e2841179SLoGin .contains(IrqLineStatus::IRQ_MOVE_PCNTXT) 284*e2841179SLoGin { 285*e2841179SLoGin desc_guard 286*e2841179SLoGin .common_data() 287*e2841179SLoGin .insert_status(IrqStatus::IRQD_MOVE_PCNTXT); 288*e2841179SLoGin } 289*e2841179SLoGin 290*e2841179SLoGin if desc_guard.line_status.is_level_type() { 291*e2841179SLoGin desc_guard 292*e2841179SLoGin .common_data() 293*e2841179SLoGin .insert_status(IrqStatus::IRQD_LEVEL); 294*e2841179SLoGin } 295*e2841179SLoGin 296*e2841179SLoGin let tmp = desc_guard.line_status.trigger_type(); 297*e2841179SLoGin 298*e2841179SLoGin if tmp != IrqLineStatus::IRQ_TYPE_NONE { 299*e2841179SLoGin trigger = tmp; 300*e2841179SLoGin } 301*e2841179SLoGin 302*e2841179SLoGin desc_guard.common_data().set_trigger_type(trigger); 303*e2841179SLoGin } 3043bc96fa4SLoGin } 3053bc96fa4SLoGin 3063bc96fa4SLoGin #[allow(dead_code)] 3073bc96fa4SLoGin #[derive(Debug)] 308*e2841179SLoGin pub struct InnerIrqDesc { 3093bc96fa4SLoGin /// per irq and chip data passed down to chip functions 3103bc96fa4SLoGin common_data: Arc<IrqCommonData>, 3113bc96fa4SLoGin irq_data: Arc<IrqData>, 3123bc96fa4SLoGin actions: Vec<Arc<IrqAction>>, 3133bc96fa4SLoGin name: Option<String>, 3143bc96fa4SLoGin parent_irq: Option<IrqNumber>, 3153bc96fa4SLoGin /// nested irq disables 3163bc96fa4SLoGin depth: u32, 3173bc96fa4SLoGin /// nested wake enables 3183bc96fa4SLoGin wake_depth: u32, 319196b75dcSLoGin desc_internal_state: IrqDescState, 320*e2841179SLoGin /// 中断线的状态 321*e2841179SLoGin line_status: IrqLineStatus, 3223bc96fa4SLoGin 3233bc96fa4SLoGin kern_inode: Option<Arc<KernFSInode>>, 3243bc96fa4SLoGin kset: Option<Arc<KSet>>, 3253bc96fa4SLoGin parent_kobj: Option<Weak<dyn KObject>>, 326*e2841179SLoGin // wait_for_threads: EventWaitQueue 327*e2841179SLoGin } 328*e2841179SLoGin 329*e2841179SLoGin impl InnerIrqDesc { 330*e2841179SLoGin pub fn name(&self) -> Option<&String> { 331*e2841179SLoGin self.name.as_ref() 332*e2841179SLoGin } 333*e2841179SLoGin 334*e2841179SLoGin pub fn can_request(&self) -> bool { 335*e2841179SLoGin !self.line_status.contains(IrqLineStatus::IRQ_NOREQUEST) 336*e2841179SLoGin } 337*e2841179SLoGin 338*e2841179SLoGin #[allow(dead_code)] 339*e2841179SLoGin pub fn set_norequest(&mut self) { 340*e2841179SLoGin self.line_status.insert(IrqLineStatus::IRQ_NOREQUEST); 341*e2841179SLoGin } 342*e2841179SLoGin 343*e2841179SLoGin #[allow(dead_code)] 344*e2841179SLoGin pub fn clear_norequest(&mut self) { 345*e2841179SLoGin self.line_status.remove(IrqLineStatus::IRQ_NOREQUEST); 346*e2841179SLoGin } 347*e2841179SLoGin 348*e2841179SLoGin pub fn nested_thread(&self) -> bool { 349*e2841179SLoGin self.line_status.contains(IrqLineStatus::IRQ_NESTED_THREAD) 350*e2841179SLoGin } 351*e2841179SLoGin 352*e2841179SLoGin pub fn line_status_set_per_cpu(&mut self) { 353*e2841179SLoGin self.line_status.insert(IrqLineStatus::IRQ_PER_CPU); 354*e2841179SLoGin } 355*e2841179SLoGin 356*e2841179SLoGin #[allow(dead_code)] 357*e2841179SLoGin pub fn line_status_clear_per_cpu(&mut self) { 358*e2841179SLoGin self.line_status.remove(IrqLineStatus::IRQ_PER_CPU); 359*e2841179SLoGin } 360*e2841179SLoGin 361*e2841179SLoGin #[allow(dead_code)] 362*e2841179SLoGin pub fn line_status(&self) -> &IrqLineStatus { 363*e2841179SLoGin &self.line_status 364*e2841179SLoGin } 365*e2841179SLoGin 366*e2841179SLoGin pub fn line_status_set_no_debug(&mut self) { 367*e2841179SLoGin self.line_status.insert(IrqLineStatus::IRQ_NO_BALANCING); 368*e2841179SLoGin } 369*e2841179SLoGin 370*e2841179SLoGin #[allow(dead_code)] 371*e2841179SLoGin pub fn line_status_clear_no_debug(&mut self) { 372*e2841179SLoGin self.line_status.remove(IrqLineStatus::IRQ_NO_BALANCING); 373*e2841179SLoGin } 374*e2841179SLoGin 375*e2841179SLoGin pub fn can_autoenable(&self) -> bool { 376*e2841179SLoGin !self.line_status.contains(IrqLineStatus::IRQ_NOAUTOEN) 377*e2841179SLoGin } 378*e2841179SLoGin 379*e2841179SLoGin pub fn can_thread(&self) -> bool { 380*e2841179SLoGin !self.line_status.contains(IrqLineStatus::IRQ_NOTHREAD) 381*e2841179SLoGin } 382*e2841179SLoGin 383*e2841179SLoGin /// 中断是否可以设置CPU亲和性 384*e2841179SLoGin pub fn can_set_affinity(&self) -> bool { 385*e2841179SLoGin if self.common_data.status().can_balance() == false 386*e2841179SLoGin || self 387*e2841179SLoGin .irq_data() 388*e2841179SLoGin .chip_info_read_irqsave() 389*e2841179SLoGin .chip() 390*e2841179SLoGin .can_set_affinity() 391*e2841179SLoGin == false 392*e2841179SLoGin { 393*e2841179SLoGin return false; 394*e2841179SLoGin } 395*e2841179SLoGin 396*e2841179SLoGin return true; 397*e2841179SLoGin } 398*e2841179SLoGin 399*e2841179SLoGin pub fn actions(&self) -> &Vec<Arc<IrqAction>> { 400*e2841179SLoGin &self.actions 401*e2841179SLoGin } 402*e2841179SLoGin 403*e2841179SLoGin pub fn add_action(&mut self, action: Arc<IrqAction>) { 404*e2841179SLoGin self.actions.push(action); 405*e2841179SLoGin } 406*e2841179SLoGin 407*e2841179SLoGin pub fn internal_state(&self) -> &IrqDescState { 408*e2841179SLoGin &self.desc_internal_state 409*e2841179SLoGin } 410*e2841179SLoGin 411*e2841179SLoGin pub(super) fn internal_state_mut(&mut self) -> &mut IrqDescState { 412*e2841179SLoGin &mut self.desc_internal_state 413*e2841179SLoGin } 414*e2841179SLoGin 415*e2841179SLoGin pub fn irq_data(&self) -> &Arc<IrqData> { 416*e2841179SLoGin &self.irq_data 417*e2841179SLoGin } 418*e2841179SLoGin 419*e2841179SLoGin pub fn common_data(&self) -> &Arc<IrqCommonData> { 420*e2841179SLoGin &self.common_data 421*e2841179SLoGin } 422*e2841179SLoGin 423*e2841179SLoGin pub fn depth(&self) -> u32 { 424*e2841179SLoGin self.depth 425*e2841179SLoGin } 426*e2841179SLoGin 427*e2841179SLoGin pub fn wake_depth(&self) -> u32 { 428*e2841179SLoGin self.wake_depth 429*e2841179SLoGin } 430*e2841179SLoGin 431*e2841179SLoGin pub fn set_depth(&mut self, depth: u32) { 432*e2841179SLoGin self.depth = depth; 433*e2841179SLoGin } 434*e2841179SLoGin 435*e2841179SLoGin pub fn set_trigger_type(&mut self, trigger: IrqLineStatus) { 436*e2841179SLoGin self.line_status.remove(IrqLineStatus::IRQ_TYPE_SENSE_MASK); 437*e2841179SLoGin self.line_status 438*e2841179SLoGin .insert(trigger & IrqLineStatus::IRQ_TYPE_SENSE_MASK); 439*e2841179SLoGin } 440*e2841179SLoGin 441*e2841179SLoGin pub fn clear_level(&mut self) { 442*e2841179SLoGin self.line_status.remove(IrqLineStatus::IRQ_LEVEL); 443*e2841179SLoGin } 444*e2841179SLoGin 445*e2841179SLoGin pub fn set_level(&mut self) { 446*e2841179SLoGin self.line_status.insert(IrqLineStatus::IRQ_LEVEL); 447*e2841179SLoGin } 4483bc96fa4SLoGin } 4493bc96fa4SLoGin 4503bc96fa4SLoGin impl KObject for IrqDesc { 4513bc96fa4SLoGin fn as_any_ref(&self) -> &dyn Any { 4523bc96fa4SLoGin self 4533bc96fa4SLoGin } 4543bc96fa4SLoGin 4553bc96fa4SLoGin fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 4563bc96fa4SLoGin self.inner().kern_inode = inode; 4573bc96fa4SLoGin } 4583bc96fa4SLoGin 4593bc96fa4SLoGin fn inode(&self) -> Option<Arc<KernFSInode>> { 4603bc96fa4SLoGin self.inner().kern_inode.clone() 4613bc96fa4SLoGin } 4623bc96fa4SLoGin 4633bc96fa4SLoGin fn parent(&self) -> Option<Weak<dyn KObject>> { 4643bc96fa4SLoGin self.inner().parent_kobj.clone() 4653bc96fa4SLoGin } 4663bc96fa4SLoGin 4673bc96fa4SLoGin fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 4683bc96fa4SLoGin self.inner().parent_kobj = parent; 4693bc96fa4SLoGin } 4703bc96fa4SLoGin 4713bc96fa4SLoGin fn kset(&self) -> Option<Arc<KSet>> { 4723bc96fa4SLoGin self.inner().kset.clone() 4733bc96fa4SLoGin } 4743bc96fa4SLoGin 4753bc96fa4SLoGin fn set_kset(&self, kset: Option<Arc<KSet>>) { 4763bc96fa4SLoGin self.inner().kset = kset; 4773bc96fa4SLoGin } 4783bc96fa4SLoGin 4793bc96fa4SLoGin fn kobj_type(&self) -> Option<&'static dyn KObjType> { 4803bc96fa4SLoGin Some(&IrqKObjType) 4813bc96fa4SLoGin } 4823bc96fa4SLoGin 4833bc96fa4SLoGin fn set_kobj_type(&self, _ktype: Option<&'static dyn KObjType>) {} 4843bc96fa4SLoGin 4853bc96fa4SLoGin fn name(&self) -> String { 486196b75dcSLoGin self.inner().irq_data.irq().data().to_string() 4873bc96fa4SLoGin } 4883bc96fa4SLoGin 4893bc96fa4SLoGin fn set_name(&self, _name: String) {} 4903bc96fa4SLoGin 4913bc96fa4SLoGin fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 4923bc96fa4SLoGin self.kobj_state.read() 4933bc96fa4SLoGin } 4943bc96fa4SLoGin 4953bc96fa4SLoGin fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 4963bc96fa4SLoGin self.kobj_state.write() 4973bc96fa4SLoGin } 4983bc96fa4SLoGin 4993bc96fa4SLoGin fn set_kobj_state(&self, state: KObjectState) { 5003bc96fa4SLoGin *self.kobj_state_mut() = state; 5013bc96fa4SLoGin } 5023bc96fa4SLoGin } 5033bc96fa4SLoGin 504196b75dcSLoGin bitflags! { 505196b75dcSLoGin /// Bit masks for desc->desc_internal_state 506*e2841179SLoGin pub struct IrqDescState: u32 { 507196b75dcSLoGin /// autodetection in progress 508196b75dcSLoGin const IRQS_AUTODETECT = 0x00000001; 509196b75dcSLoGin /// was disabled due to spurious interrupt detection 510196b75dcSLoGin const IRQS_SPURIOUS_DISABLED = 0x00000002; 511196b75dcSLoGin /// polling in progress 512196b75dcSLoGin const IRQS_POLL_INPROGRESS = 0x00000008; 513196b75dcSLoGin /// irq is not unmasked in primary handler 514196b75dcSLoGin const IRQS_ONESHOT = 0x00000020; 515196b75dcSLoGin /// irq is replayed 516196b75dcSLoGin const IRQS_REPLAY = 0x00000040; 517196b75dcSLoGin /// irq is waiting 518196b75dcSLoGin const IRQS_WAITING = 0x00000080; 519196b75dcSLoGin /// irq is pending and replayed later 520196b75dcSLoGin const IRQS_PENDING = 0x00000200; 521196b75dcSLoGin /// irq is suspended 522196b75dcSLoGin const IRQS_SUSPENDED = 0x00000800; 523196b75dcSLoGin /// irq line is used to deliver NMIs 524196b75dcSLoGin const IRQS_NMI = 0x00002000; 525196b75dcSLoGin /// descriptor has been added to sysfs 526196b75dcSLoGin const IRQS_SYSFS = 0x00004000; 527196b75dcSLoGin } 528196b75dcSLoGin } 529196b75dcSLoGin 5303bc96fa4SLoGin /// 每个中断的响应动作的描述符 5313bc96fa4SLoGin /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/include/linux/interrupt.h#118 5323bc96fa4SLoGin #[allow(dead_code)] 5333bc96fa4SLoGin #[derive(Debug)] 5343bc96fa4SLoGin pub struct IrqAction { 5353bc96fa4SLoGin inner: SpinLock<InnerIrqAction>, 536*e2841179SLoGin /// 用于等待线程被创建的完成量 537*e2841179SLoGin thread_completion: Completion, 5383bc96fa4SLoGin } 5393bc96fa4SLoGin 5403bc96fa4SLoGin impl IrqAction { 5413bc96fa4SLoGin #[allow(dead_code)] 5423bc96fa4SLoGin pub fn new( 5433bc96fa4SLoGin irq: IrqNumber, 5443bc96fa4SLoGin name: String, 545*e2841179SLoGin handler: Option<&'static dyn IrqHandler>, 546*e2841179SLoGin thread_fn: Option<&'static dyn IrqHandler>, 5473bc96fa4SLoGin ) -> Arc<Self> { 548*e2841179SLoGin let action: IrqAction = IrqAction { 5493bc96fa4SLoGin inner: SpinLock::new(InnerIrqAction { 5503bc96fa4SLoGin dev_id: None, 5513bc96fa4SLoGin handler, 552*e2841179SLoGin thread_fn, 5533bc96fa4SLoGin thread: None, 5543bc96fa4SLoGin secondary: None, 5553bc96fa4SLoGin irq, 5563bc96fa4SLoGin flags: IrqHandleFlags::empty(), 5573bc96fa4SLoGin name, 558*e2841179SLoGin thread_flags: ThreadedHandlerFlags::empty(), 5593bc96fa4SLoGin }), 560*e2841179SLoGin thread_completion: Completion::new(), 5613bc96fa4SLoGin }; 5623bc96fa4SLoGin 5633bc96fa4SLoGin return Arc::new(action); 5643bc96fa4SLoGin } 565196b75dcSLoGin 566*e2841179SLoGin pub fn inner(&self) -> SpinLockGuard<InnerIrqAction> { 567*e2841179SLoGin self.inner.lock_irqsave() 568196b75dcSLoGin } 569196b75dcSLoGin 570*e2841179SLoGin pub fn thread_completion(&self) -> &Completion { 571*e2841179SLoGin &self.thread_completion 572196b75dcSLoGin } 5733bc96fa4SLoGin } 5743bc96fa4SLoGin 5753bc96fa4SLoGin #[allow(dead_code)] 5763bc96fa4SLoGin #[derive(Debug)] 577*e2841179SLoGin pub struct InnerIrqAction { 5783bc96fa4SLoGin /// cookie to identify the device 579*e2841179SLoGin dev_id: Option<Arc<DeviceId>>, 5803bc96fa4SLoGin /// 中断处理程序 581*e2841179SLoGin handler: Option<&'static dyn IrqHandler>, 5823bc96fa4SLoGin /// interrupt handler function for threaded interrupts 583*e2841179SLoGin thread_fn: Option<&'static dyn IrqHandler>, 5843bc96fa4SLoGin /// thread pointer for threaded interrupts 5853bc96fa4SLoGin thread: Option<Arc<ProcessControlBlock>>, 5863bc96fa4SLoGin /// pointer to secondary irqaction (force threading) 5873bc96fa4SLoGin secondary: Option<Arc<IrqAction>>, 5883bc96fa4SLoGin /// 中断号 5893bc96fa4SLoGin irq: IrqNumber, 5903bc96fa4SLoGin flags: IrqHandleFlags, 591*e2841179SLoGin /// 中断线程的标志 592*e2841179SLoGin thread_flags: ThreadedHandlerFlags, 5933bc96fa4SLoGin /// name of the device 5943bc96fa4SLoGin name: String, 5953bc96fa4SLoGin } 5963bc96fa4SLoGin 597*e2841179SLoGin impl InnerIrqAction { 598*e2841179SLoGin pub fn dev_id(&self) -> &Option<Arc<DeviceId>> { 599*e2841179SLoGin &self.dev_id 600*e2841179SLoGin } 601*e2841179SLoGin 602*e2841179SLoGin pub fn dev_id_mut(&mut self) -> &mut Option<Arc<DeviceId>> { 603*e2841179SLoGin &mut self.dev_id 604*e2841179SLoGin } 605*e2841179SLoGin 606*e2841179SLoGin pub fn handler(&self) -> Option<&'static dyn IrqHandler> { 607*e2841179SLoGin self.handler 608*e2841179SLoGin } 609*e2841179SLoGin 610*e2841179SLoGin pub fn set_handler(&mut self, handler: Option<&'static dyn IrqHandler>) { 611*e2841179SLoGin self.handler = handler; 612*e2841179SLoGin } 613*e2841179SLoGin 614*e2841179SLoGin pub fn thread_fn(&self) -> Option<&'static dyn IrqHandler> { 615*e2841179SLoGin self.thread_fn 616*e2841179SLoGin } 617*e2841179SLoGin 618*e2841179SLoGin pub fn thread(&self) -> Option<Arc<ProcessControlBlock>> { 619*e2841179SLoGin self.thread.clone() 620*e2841179SLoGin } 621*e2841179SLoGin 622*e2841179SLoGin pub fn set_thread(&mut self, thread: Option<Arc<ProcessControlBlock>>) { 623*e2841179SLoGin self.thread = thread; 624*e2841179SLoGin } 625*e2841179SLoGin 626*e2841179SLoGin #[allow(dead_code)] 627*e2841179SLoGin pub fn thread_flags(&self) -> &ThreadedHandlerFlags { 628*e2841179SLoGin &self.thread_flags 629*e2841179SLoGin } 630*e2841179SLoGin 631*e2841179SLoGin pub fn thread_flags_mut(&mut self) -> &mut ThreadedHandlerFlags { 632*e2841179SLoGin &mut self.thread_flags 633*e2841179SLoGin } 634*e2841179SLoGin 635*e2841179SLoGin pub fn secondary(&self) -> Option<Arc<IrqAction>> { 636*e2841179SLoGin self.secondary.clone() 637*e2841179SLoGin } 638*e2841179SLoGin 639*e2841179SLoGin #[allow(dead_code)] 640*e2841179SLoGin pub fn irq(&self) -> IrqNumber { 641*e2841179SLoGin self.irq 642*e2841179SLoGin } 643*e2841179SLoGin 644*e2841179SLoGin #[allow(dead_code)] 645*e2841179SLoGin pub fn set_irq(&mut self, irq: IrqNumber) { 646*e2841179SLoGin self.irq = irq; 647*e2841179SLoGin } 648*e2841179SLoGin 649*e2841179SLoGin pub fn flags(&self) -> &IrqHandleFlags { 650*e2841179SLoGin &self.flags 651*e2841179SLoGin } 652*e2841179SLoGin 653*e2841179SLoGin pub fn flags_mut(&mut self) -> &mut IrqHandleFlags { 654*e2841179SLoGin &mut self.flags 655*e2841179SLoGin } 656*e2841179SLoGin 657*e2841179SLoGin pub fn name(&self) -> &String { 658*e2841179SLoGin &self.name 659*e2841179SLoGin } 660*e2841179SLoGin } 661*e2841179SLoGin 662*e2841179SLoGin bitflags! { 663*e2841179SLoGin /// 这些标志由线程处理程序使用 664*e2841179SLoGin pub struct ThreadedHandlerFlags: u32 { 665*e2841179SLoGin /// IRQTF_RUNTHREAD - 表示应运行中断处理程序线程 666*e2841179SLoGin const IRQTF_RUNTHREAD = 1 << 0; 667*e2841179SLoGin /// IRQTF_WARNED - 已打印警告 "IRQ_WAKE_THREAD w/o thread_fn" 668*e2841179SLoGin const IRQTF_WARNED = 1 << 1; 669*e2841179SLoGin /// IRQTF_AFFINITY - 请求irq线程调整亲和性 670*e2841179SLoGin const IRQTF_AFFINITY = 1 << 2; 671*e2841179SLoGin /// IRQTF_FORCED_THREAD - irq操作被强制线程化 672*e2841179SLoGin const IRQTF_FORCED_THREAD = 1 << 3; 673*e2841179SLoGin /// IRQTF_READY - 表示irq线程已准备就绪 674*e2841179SLoGin const IRQTF_READY = 1 << 4; 675*e2841179SLoGin } 676*e2841179SLoGin } 677*e2841179SLoGin 678*e2841179SLoGin /// Implements the `ThreadedHandlerFlags` structure. 679*e2841179SLoGin impl ThreadedHandlerFlags { 680*e2841179SLoGin /// 在 `ThreadedHandlerFlags` 结构中测试并设置特定的位。 681*e2841179SLoGin /// 682*e2841179SLoGin /// # 参数 683*e2841179SLoGin /// 684*e2841179SLoGin /// * `bit` - 要测试并设置的位。 685*e2841179SLoGin /// 686*e2841179SLoGin /// # 返回 687*e2841179SLoGin /// 688*e2841179SLoGin /// 如果操作前该位已被设置,则返回 `true`,否则返回 `false`。 689*e2841179SLoGin pub fn test_and_set_bit(&mut self, bit: ThreadedHandlerFlags) -> bool { 690*e2841179SLoGin let res = (self.bits & bit.bits) != 0; 691*e2841179SLoGin self.bits |= bit.bits; 692*e2841179SLoGin return res; 693*e2841179SLoGin } 694*e2841179SLoGin } 695*e2841179SLoGin 6963bc96fa4SLoGin // 定义IrqFlags位标志 6973bc96fa4SLoGin bitflags! { 6983bc96fa4SLoGin /// 这些标志仅由内核在中断处理例程中使用。 6993bc96fa4SLoGin pub struct IrqHandleFlags: u32 { 700*e2841179SLoGin 701*e2841179SLoGin const IRQF_TRIGGER_NONE = IrqLineStatus::IRQ_TYPE_NONE.bits(); 702*e2841179SLoGin const IRQF_TRIGGER_RISING = IrqLineStatus::IRQ_TYPE_EDGE_RISING.bits(); 703*e2841179SLoGin const IRQF_TRIGGER_FALLING = IrqLineStatus::IRQ_TYPE_EDGE_FALLING.bits(); 704*e2841179SLoGin const IRQF_TRIGGER_HIGH = IrqLineStatus::IRQ_TYPE_LEVEL_HIGH.bits(); 705*e2841179SLoGin const IRQF_TRIGGER_LOW = IrqLineStatus::IRQ_TYPE_LEVEL_LOW.bits(); 706*e2841179SLoGin const IRQF_TRIGGER_MASK = Self::IRQF_TRIGGER_HIGH.bits | Self::IRQF_TRIGGER_LOW.bits | Self::IRQF_TRIGGER_RISING.bits | Self::IRQF_TRIGGER_FALLING.bits; 7073bc96fa4SLoGin /// IRQF_SHARED - 允许多个设备共享中断 7083bc96fa4SLoGin const IRQF_SHARED = 0x00000080; 7093bc96fa4SLoGin /// IRQF_PROBE_SHARED - 当预期出现共享不匹配时,由调用者设置 7103bc96fa4SLoGin const IRQF_PROBE_SHARED = 0x00000100; 7113bc96fa4SLoGin /// IRQF_TIMER - 标记此中断为定时器中断 7123bc96fa4SLoGin const __IRQF_TIMER = 0x00000200; 7133bc96fa4SLoGin /// IRQF_PERCPU - 中断是每个CPU的 7143bc96fa4SLoGin const IRQF_PERCPU = 0x00000400; 7153bc96fa4SLoGin /// IRQF_NOBALANCING - 将此中断从中断平衡中排除 7163bc96fa4SLoGin const IRQF_NOBALANCING = 0x00000800; 7173bc96fa4SLoGin /// IRQF_IRQPOLL - 中断用于轮询(出于性能原因,只有在共享中断中首次注册的中断会被考虑) 7183bc96fa4SLoGin const IRQF_IRQPOLL = 0x00001000; 7193bc96fa4SLoGin /// IRQF_ONESHOT - 在硬中断处理程序完成后,不会重新启用中断。由需要在运行线程处理程序之前保持中断线路禁用的线程中断使用。 7203bc96fa4SLoGin const IRQF_ONESHOT = 0x00002000; 7213bc96fa4SLoGin /// IRQF_NO_SUSPEND - 在挂起期间不禁用此IRQ。不能保证此中断会从挂起状态唤醒系统。 7223bc96fa4SLoGin const IRQF_NO_SUSPEND = 0x00004000; 7233bc96fa4SLoGin /// IRQF_FORCE_RESUME - 即使设置了IRQF_NO_SUSPEND,也强制在恢复时启用它 7243bc96fa4SLoGin const IRQF_FORCE_RESUME = 0x00008000; 7253bc96fa4SLoGin /// IRQF_NO_THREAD - 中断不能被线程化 7263bc96fa4SLoGin const IRQF_NO_THREAD = 0x00010000; 7273bc96fa4SLoGin /// IRQF_EARLY_RESUME - 在syscore而不是在设备恢复时间早期恢复IRQ。 7283bc96fa4SLoGin const IRQF_EARLY_RESUME = 0x00020000; 7293bc96fa4SLoGin /// IRQF_COND_SUSPEND - 如果IRQ与NO_SUSPEND用户共享,则在挂起中断后执行此中断处理程序。对于系统唤醒设备用户,需要在他们的中断处理程序中实现唤醒检测。 7303bc96fa4SLoGin const IRQF_COND_SUSPEND = 0x00040000; 7313bc96fa4SLoGin /// IRQF_NO_AUTOEN - 当用户请求时,不会自动启用IRQ或NMI。用户稍后会通过enable_irq()或enable_nmi()显式启用它。 7323bc96fa4SLoGin const IRQF_NO_AUTOEN = 0x00080000; 7333bc96fa4SLoGin /// IRQF_NO_DEBUG - 从IPI和类似处理程序的逃逸检测中排除,取决于IRQF_PERCPU。 7343bc96fa4SLoGin const IRQF_NO_DEBUG = 0x00100000; 7353bc96fa4SLoGin const IRQF_TIMER = Self::__IRQF_TIMER.bits | Self::IRQF_NO_SUSPEND.bits | Self::IRQF_NO_THREAD.bits; 7363bc96fa4SLoGin } 7373bc96fa4SLoGin } 7383bc96fa4SLoGin 739*e2841179SLoGin impl IrqHandleFlags { 740*e2841179SLoGin /// 检查是否指定了触发类型 741*e2841179SLoGin #[inline(always)] 742*e2841179SLoGin pub fn trigger_type_specified(&self) -> bool { 743*e2841179SLoGin (self.bits & Self::IRQF_TRIGGER_MASK.bits) != 0 744*e2841179SLoGin } 745*e2841179SLoGin 746*e2841179SLoGin /// 插入触发类型 747*e2841179SLoGin pub fn insert_trigger_type(&mut self, trigger: IrqLineStatus) { 748*e2841179SLoGin self.bits |= trigger.trigger_bits() & IrqHandleFlags::IRQF_TRIGGER_MASK.bits; 749*e2841179SLoGin } 750*e2841179SLoGin 751*e2841179SLoGin #[allow(dead_code)] 752*e2841179SLoGin pub fn remove_trigger_type(&mut self, trigger: IrqLineStatus) { 753*e2841179SLoGin self.bits &= !(trigger.trigger_bits() & IrqHandleFlags::IRQF_TRIGGER_MASK.bits); 754*e2841179SLoGin } 755*e2841179SLoGin 756*e2841179SLoGin pub fn trigger_type(&self) -> IrqLineStatus { 757*e2841179SLoGin IrqLineStatus::from_bits_truncate(self.bits & IrqHandleFlags::IRQF_TRIGGER_MASK.bits) 758*e2841179SLoGin } 759*e2841179SLoGin } 760*e2841179SLoGin 7613bc96fa4SLoGin #[inline(never)] 7623bc96fa4SLoGin pub(super) fn early_irq_init() -> Result<(), SystemError> { 7633bc96fa4SLoGin let irqcnt = CurrentIrqArch::probe_total_irq_num(); 7643bc96fa4SLoGin let mut manager = IrqDescManager::new(); 7653bc96fa4SLoGin for i in 0..irqcnt { 7663bc96fa4SLoGin let irq_desc = IrqDesc::new(IrqNumber::new(i), None, IrqStatus::empty()); 7673bc96fa4SLoGin manager.insert(IrqNumber::new(i), irq_desc); 7683bc96fa4SLoGin } 7693bc96fa4SLoGin 770196b75dcSLoGin unsafe { 771196b75dcSLoGin IRQ_DESC_MANAGER = Some(manager); 772196b75dcSLoGin } 773196b75dcSLoGin 7743bc96fa4SLoGin return CurrentIrqArch::arch_early_irq_init(); 7753bc96fa4SLoGin } 7763bc96fa4SLoGin 777196b75dcSLoGin static mut IRQ_DESC_MANAGER: Option<IrqDescManager> = None; 778196b75dcSLoGin 779196b75dcSLoGin /// 获取中断描述符管理器的引用 780196b75dcSLoGin #[inline(always)] 781*e2841179SLoGin pub fn irq_desc_manager() -> &'static IrqDescManager { 782196b75dcSLoGin return unsafe { IRQ_DESC_MANAGER.as_ref().unwrap() }; 783196b75dcSLoGin } 784196b75dcSLoGin 785*e2841179SLoGin pub struct IrqDescManager { 7863bc96fa4SLoGin irq_descs: BTreeMap<IrqNumber, Arc<IrqDesc>>, 7873bc96fa4SLoGin } 7883bc96fa4SLoGin 7893bc96fa4SLoGin impl IrqDescManager { 7903bc96fa4SLoGin fn new() -> Self { 7913bc96fa4SLoGin IrqDescManager { 7923bc96fa4SLoGin irq_descs: BTreeMap::new(), 7933bc96fa4SLoGin } 7943bc96fa4SLoGin } 7953bc96fa4SLoGin 7963bc96fa4SLoGin /// 查找中断描述符 7973bc96fa4SLoGin pub fn lookup(&self, irq: IrqNumber) -> Option<Arc<IrqDesc>> { 7983bc96fa4SLoGin self.irq_descs.get(&irq).map(|desc| desc.clone()) 7993bc96fa4SLoGin } 8003bc96fa4SLoGin 8013bc96fa4SLoGin fn insert(&mut self, irq: IrqNumber, desc: Arc<IrqDesc>) { 8023bc96fa4SLoGin self.irq_descs.insert(irq, desc); 8033bc96fa4SLoGin } 804196b75dcSLoGin 805196b75dcSLoGin /// 释放中断描述符 806196b75dcSLoGin #[allow(dead_code)] 807196b75dcSLoGin fn free_desc(&mut self, irq: IrqNumber) { 808196b75dcSLoGin if let Some(desc) = self.irq_descs.get(&irq) { 809196b75dcSLoGin irq_sysfs_del(desc); 810196b75dcSLoGin self.irq_descs.remove(&irq); 811196b75dcSLoGin } 812196b75dcSLoGin } 813196b75dcSLoGin 814196b75dcSLoGin /// 迭代中断描述符 815196b75dcSLoGin pub fn iter_descs(&self) -> btree_map::Iter<'_, IrqNumber, Arc<IrqDesc>> { 816196b75dcSLoGin self.irq_descs.iter() 817196b75dcSLoGin } 8183bc96fa4SLoGin } 819