xref: /DragonOS/kernel/src/exception/irqdesc.rs (revision 0102d69fdd231e472d7bb3d609a41ae56a3799ee)
1 use core::{
2     any::Any,
3     fmt::Debug,
4     sync::atomic::{AtomicI64, Ordering},
5 };
6 
7 use alloc::{
8     collections::{btree_map, BTreeMap},
9     string::{String, ToString},
10     sync::{Arc, Weak},
11     vec::Vec,
12 };
13 use system_error::SystemError;
14 
15 use crate::{
16     arch::{interrupt::TrapFrame, CurrentIrqArch},
17     driver::base::{
18         device::DeviceId,
19         kobject::{KObjType, KObject, KObjectState, LockedKObjectState},
20         kset::KSet,
21     },
22     filesystem::kernfs::KernFSInode,
23     libs::{
24         cpumask::CpuMask,
25         mutex::{Mutex, MutexGuard},
26         rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard},
27         spinlock::{SpinLock, SpinLockGuard},
28     },
29     mm::percpu::PerCpuVar,
30     process::ProcessControlBlock,
31     sched::completion::Completion,
32     smp::cpu::smp_cpu_manager,
33 };
34 
35 use super::{
36     dummychip::no_irq_chip,
37     handle::bad_irq_handler,
38     irqchip::IrqChip,
39     irqdata::{IrqCommonData, IrqData, IrqHandlerData, IrqLineStatus, IrqStatus},
40     irqdomain::{irq_domain_manager, IrqDomain},
41     sysfs::{irq_sysfs_del, IrqKObjType},
42     HardwareIrqNumber, InterruptArch, IrqNumber,
43 };
44 
45 /// 中断流处理程序
46 pub trait IrqFlowHandler: Debug + Send + Sync + Any {
handle(&self, irq_desc: &Arc<IrqDesc>, trap_frame: &mut TrapFrame)47     fn handle(&self, irq_desc: &Arc<IrqDesc>, trap_frame: &mut TrapFrame);
48 }
49 
50 /// 中断处理程序
51 pub trait IrqHandler: Debug + Send + Sync + Any {
handle( &self, irq: IrqNumber, static_data: Option<&dyn IrqHandlerData>, dynamic_data: Option<Arc<dyn IrqHandlerData>>, ) -> Result<IrqReturn, SystemError>52     fn handle(
53         &self,
54         irq: IrqNumber,
55         static_data: Option<&dyn IrqHandlerData>,
56         dynamic_data: Option<Arc<dyn IrqHandlerData>>,
57     ) -> Result<IrqReturn, SystemError>;
58 }
59 
60 /// 中断处理函数返回值
61 ///
62 /// 用于指示中断处理函数是否处理了中断
63 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
64 pub enum IrqReturn {
65     /// 中断未被处理
66     NotHandled,
67     /// 中断已被处理
68     Handled,
69     /// 中断已被处理,并且需要唤醒中断线程
70     WakeThread,
71 }
72 
73 /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/include/linux/irqdesc.h#55
74 #[derive(Debug)]
75 pub struct IrqDesc {
76     inner: SpinLock<InnerIrqDesc>,
77 
78     handler: RwLock<Option<&'static dyn IrqFlowHandler>>,
79     /// 一个用于串行化 request_irq()和free_irq() 的互斥锁
80     request_mutex: Mutex<()>,
81     kobj_state: LockedKObjectState,
82     /// 当前描述符内正在运行的中断线程数
83     threads_active: AtomicI64,
84 }
85 
86 impl IrqDesc {
87     #[inline(never)]
new(irq: IrqNumber, name: Option<String>, irqd_flags: IrqStatus) -> Arc<Self>88     pub fn new(irq: IrqNumber, name: Option<String>, irqd_flags: IrqStatus) -> Arc<Self> {
89         // 初始化的过程参考 https://code.dragonos.org.cn/xref/linux-6.1.9/kernel/irq/irqdesc.c#392
90         let common_data = Arc::new(IrqCommonData::new());
91         let irq_data = Arc::new(IrqData::new(
92             irq,
93             HardwareIrqNumber::new(irq.data()),
94             common_data.clone(),
95             no_irq_chip(),
96         ));
97 
98         irq_data.irqd_set(IrqStatus::IRQD_IRQ_DISABLED);
99         common_data.insert_status(IrqStatus::IRQD_IRQ_MASKED);
100 
101         let irq_desc = IrqDesc {
102             inner: SpinLock::new(InnerIrqDesc {
103                 percpu_affinity: None,
104                 percpu_enabled: None,
105                 common_data,
106                 irq_data,
107                 desc_internal_state: IrqDescState::empty(),
108                 line_status: IrqLineStatus::empty(),
109                 actions: Vec::new(),
110                 name,
111                 parent_irq: None,
112                 depth: 1,
113                 wake_depth: 0,
114                 kern_inode: None,
115                 kset: None,
116                 parent_kobj: None,
117                 threads_oneshot: 0,
118             }),
119             request_mutex: Mutex::new(()),
120             handler: RwLock::new(None),
121             kobj_state: LockedKObjectState::new(Some(KObjectState::INITIALIZED)),
122             threads_active: AtomicI64::new(0),
123         };
124         let irq_desc = Arc::new(irq_desc);
125         irq_desc.irq_data().set_irq_desc(Arc::downgrade(&irq_desc));
126         irq_desc.set_handler(bad_irq_handler());
127         irq_desc.inner().irq_data.irqd_set(irqd_flags);
128 
129         return irq_desc;
130     }
131 
132     /// 返回当前活跃的中断线程数量
133     #[allow(dead_code)]
threads_active(&self) -> i64134     pub fn threads_active(&self) -> i64 {
135         self.threads_active.load(Ordering::SeqCst)
136     }
137 
138     /// 增加当前活跃的中断线程数量, 返回增加前的值
inc_threads_active(&self) -> i64139     pub fn inc_threads_active(&self) -> i64 {
140         self.threads_active.fetch_add(1, Ordering::SeqCst)
141     }
142 
143     /// 减少当前活跃的中断线程数量, 返回减少前的值
144     #[allow(dead_code)]
dec_threads_active(&self) -> i64145     pub fn dec_threads_active(&self) -> i64 {
146         self.threads_active.fetch_sub(1, Ordering::SeqCst)
147     }
148 
set_handler(&self, handler: &'static dyn IrqFlowHandler)149     pub fn set_handler(&self, handler: &'static dyn IrqFlowHandler) {
150         self.chip_bus_lock();
151         let mut guard = self.handler.write_irqsave();
152         *guard = Some(handler);
153         self.chip_bus_sync_unlock();
154     }
155 
156     /// 设置中断处理程序(不对desc->inner)
157     ///
158     ///
159     /// ## Safety
160     ///
161     /// 需要保证irq_data和chip是当前irqdesc的
set_handler_no_lock_inner( &self, handler: &'static dyn IrqFlowHandler, irq_data: &Arc<IrqData>, chip: &Arc<dyn IrqChip>, )162     pub fn set_handler_no_lock_inner(
163         &self,
164         handler: &'static dyn IrqFlowHandler,
165         irq_data: &Arc<IrqData>,
166         chip: &Arc<dyn IrqChip>,
167     ) {
168         chip.irq_bus_lock(irq_data).ok();
169         let mut guard = self.handler.write_irqsave();
170         *guard = Some(handler);
171         chip.irq_bus_sync_unlock(irq_data).ok();
172     }
173 
handler(&self) -> Option<&'static dyn IrqFlowHandler>174     pub fn handler(&self) -> Option<&'static dyn IrqFlowHandler> {
175         let guard = self.handler.read_irqsave();
176         *guard
177     }
178 
inner(&self) -> SpinLockGuard<InnerIrqDesc>179     pub fn inner(&self) -> SpinLockGuard<InnerIrqDesc> {
180         self.inner.lock_irqsave()
181     }
182 
actions(&self) -> Vec<Arc<IrqAction>>183     pub fn actions(&self) -> Vec<Arc<IrqAction>> {
184         self.inner().actions.clone()
185     }
186 
187     /// 对中断请求过程加锁
request_mutex_lock(&self) -> MutexGuard<()>188     pub fn request_mutex_lock(&self) -> MutexGuard<()> {
189         self.request_mutex.lock()
190     }
191 
irq(&self) -> IrqNumber192     pub fn irq(&self) -> IrqNumber {
193         self.inner().irq_data.irq()
194     }
195 
hardware_irq(&self) -> HardwareIrqNumber196     pub fn hardware_irq(&self) -> HardwareIrqNumber {
197         self.inner().irq_data.hardware_irq()
198     }
199 
irq_data(&self) -> Arc<IrqData>200     pub fn irq_data(&self) -> Arc<IrqData> {
201         self.inner().irq_data.clone()
202     }
203 
204     /// 标记当前irq描述符已经被添加到sysfs
mark_in_sysfs(&self)205     pub fn mark_in_sysfs(&self) {
206         self.inner()
207             .desc_internal_state
208             .insert(IrqDescState::IRQS_SYSFS);
209     }
210 
mark_not_in_sysfs(&self)211     pub fn mark_not_in_sysfs(&self) {
212         self.inner()
213             .desc_internal_state
214             .remove(IrqDescState::IRQS_SYSFS);
215     }
216 
217     /// 判断当前描述符是否已经添加到了sysfs
in_sysfs(&self) -> bool218     pub fn in_sysfs(&self) -> bool {
219         self.inner()
220             .desc_internal_state
221             .contains(IrqDescState::IRQS_SYSFS)
222     }
223 
name(&self) -> Option<String>224     pub fn name(&self) -> Option<String> {
225         self.inner().name.clone()
226     }
227 
can_request(&self) -> bool228     pub fn can_request(&self) -> bool {
229         self.inner().can_request()
230     }
231 
232     #[allow(dead_code)]
set_norequest(&self)233     pub fn set_norequest(&self) {
234         self.inner().set_norequest();
235     }
236 
237     #[allow(dead_code)]
clear_norequest(&self)238     pub fn clear_norequest(&self) {
239         self.inner().clear_norequest();
240     }
241 
nested_thread(&self) -> bool242     pub fn nested_thread(&self) -> bool {
243         self.inner().nested_thread()
244     }
245 
246     /// 中断是否可以线程化
can_thread(&self) -> bool247     pub fn can_thread(&self) -> bool {
248         !self
249             .inner()
250             .line_status
251             .contains(IrqLineStatus::IRQ_NOTHREAD)
252     }
253 
chip_bus_lock(&self)254     pub fn chip_bus_lock(&self) {
255         let irq_data = self.inner().irq_data.clone();
256         irq_data
257             .chip_info_read_irqsave()
258             .chip()
259             .irq_bus_lock(&irq_data)
260             .ok();
261     }
262 
263     /// 同步释放低速总线锁
264     ///
265     /// ## 锁
266     ///
267     /// 进入此函数时,必须持有低速总线锁,并且desc的inner锁和irqdata的inner锁
268     ///     必须已经释放。否则将死锁。
chip_bus_sync_unlock(&self)269     pub fn chip_bus_sync_unlock(&self) {
270         let irq_data = self.inner().irq_data.clone();
271         irq_data
272             .chip_info_write_irqsave()
273             .chip()
274             .irq_bus_sync_unlock(&irq_data)
275             .ok();
276     }
277 
set_percpu_devid_flags(&self)278     pub fn set_percpu_devid_flags(&self) {
279         self.modify_status(
280             IrqLineStatus::empty(),
281             IrqLineStatus::IRQ_NOAUTOEN
282                 | IrqLineStatus::IRQ_PER_CPU
283                 | IrqLineStatus::IRQ_NOTHREAD
284                 | IrqLineStatus::IRQ_NOPROBE
285                 | IrqLineStatus::IRQ_PER_CPU_DEVID,
286         );
287     }
288 
set_probe(&self)289     pub fn set_probe(&self) {
290         self.modify_status(IrqLineStatus::IRQ_NOPROBE, IrqLineStatus::empty());
291     }
292 
set_noprobe(&self)293     pub fn set_noprobe(&self) {
294         self.modify_status(IrqLineStatus::empty(), IrqLineStatus::IRQ_NOPROBE);
295     }
296 
modify_status(&self, clear: IrqLineStatus, set: IrqLineStatus)297     pub fn modify_status(&self, clear: IrqLineStatus, set: IrqLineStatus) {
298         let mut desc_guard = self.inner();
299         desc_guard.line_status.remove(clear);
300         desc_guard.line_status.insert(set);
301 
302         let mut trigger = desc_guard.common_data().trigger_type();
303 
304         desc_guard.common_data().clear_status(
305             IrqStatus::IRQD_NO_BALANCING
306                 | IrqStatus::IRQD_PER_CPU
307                 | IrqStatus::IRQD_TRIGGER_MASK
308                 | IrqStatus::IRQD_LEVEL
309                 | IrqStatus::IRQD_MOVE_PCNTXT,
310         );
311 
312         if desc_guard
313             .line_status
314             .contains(IrqLineStatus::IRQ_NO_BALANCING)
315         {
316             desc_guard
317                 .common_data()
318                 .insert_status(IrqStatus::IRQD_NO_BALANCING);
319         }
320 
321         if desc_guard.line_status.contains(IrqLineStatus::IRQ_PER_CPU) {
322             desc_guard
323                 .common_data()
324                 .insert_status(IrqStatus::IRQD_PER_CPU);
325         }
326 
327         if desc_guard
328             .line_status
329             .contains(IrqLineStatus::IRQ_MOVE_PCNTXT)
330         {
331             desc_guard
332                 .common_data()
333                 .insert_status(IrqStatus::IRQD_MOVE_PCNTXT);
334         }
335 
336         if desc_guard.line_status.is_level_type() {
337             desc_guard
338                 .common_data()
339                 .insert_status(IrqStatus::IRQD_LEVEL);
340         }
341 
342         let tmp = desc_guard.line_status.trigger_type();
343 
344         if tmp != IrqLineStatus::IRQ_TYPE_NONE {
345             trigger = tmp;
346         }
347 
348         desc_guard.common_data().set_trigger_type(trigger);
349     }
350 }
351 
352 #[allow(dead_code)]
353 #[derive(Debug)]
354 pub struct InnerIrqDesc {
355     /// per irq and chip data passed down to chip functions
356     common_data: Arc<IrqCommonData>,
357     irq_data: Arc<IrqData>,
358     actions: Vec<Arc<IrqAction>>,
359     name: Option<String>,
360     parent_irq: Option<IrqNumber>,
361     /// nested irq disables
362     depth: u32,
363     /// nested wake enables
364     wake_depth: u32,
365     desc_internal_state: IrqDescState,
366     /// 中断线的状态
367     line_status: IrqLineStatus,
368 
369     kern_inode: Option<Arc<KernFSInode>>,
370     kset: Option<Arc<KSet>>,
371     parent_kobj: Option<Weak<dyn KObject>>,
372     /// per-cpu enabled mask
373     percpu_enabled: Option<CpuMask>,
374     /// per-cpu affinity
375     percpu_affinity: Option<CpuMask>,
376     // wait_for_threads: EventWaitQueue
377     /// bitfield to handle shared oneshot threads
378     threads_oneshot: u64,
379 }
380 
381 impl InnerIrqDesc {
name(&self) -> Option<&String>382     pub fn name(&self) -> Option<&String> {
383         self.name.as_ref()
384     }
385 
386     #[allow(dead_code)]
set_name(&mut self, name: Option<String>)387     pub fn set_name(&mut self, name: Option<String>) {
388         self.name = name;
389     }
390 
can_request(&self) -> bool391     pub fn can_request(&self) -> bool {
392         !self.line_status.contains(IrqLineStatus::IRQ_NOREQUEST)
393     }
394 
395     #[allow(dead_code)]
set_norequest(&mut self)396     pub fn set_norequest(&mut self) {
397         self.line_status.insert(IrqLineStatus::IRQ_NOREQUEST);
398     }
399 
400     #[allow(dead_code)]
clear_norequest(&mut self)401     pub fn clear_norequest(&mut self) {
402         self.line_status.remove(IrqLineStatus::IRQ_NOREQUEST);
403     }
404 
405     #[allow(dead_code)]
set_noprobe(&mut self)406     pub fn set_noprobe(&mut self) {
407         self.line_status.insert(IrqLineStatus::IRQ_NOPROBE);
408     }
409 
410     #[allow(dead_code)]
clear_noprobe(&mut self)411     pub fn clear_noprobe(&mut self) {
412         self.line_status.remove(IrqLineStatus::IRQ_NOPROBE);
413     }
414 
set_nothread(&mut self)415     pub fn set_nothread(&mut self) {
416         self.line_status.insert(IrqLineStatus::IRQ_NOTHREAD);
417     }
418 
clear_nothread(&mut self)419     pub fn clear_nothread(&mut self) {
420         self.line_status.remove(IrqLineStatus::IRQ_NOTHREAD);
421     }
422 
nested_thread(&self) -> bool423     pub fn nested_thread(&self) -> bool {
424         self.line_status.contains(IrqLineStatus::IRQ_NESTED_THREAD)
425     }
426 
line_status_set_per_cpu(&mut self)427     pub fn line_status_set_per_cpu(&mut self) {
428         self.line_status.insert(IrqLineStatus::IRQ_PER_CPU);
429     }
430 
431     #[allow(dead_code)]
line_status_clear_per_cpu(&mut self)432     pub fn line_status_clear_per_cpu(&mut self) {
433         self.line_status.remove(IrqLineStatus::IRQ_PER_CPU);
434     }
435 
436     #[allow(dead_code)]
line_status(&self) -> &IrqLineStatus437     pub fn line_status(&self) -> &IrqLineStatus {
438         &self.line_status
439     }
440 
line_status_set_no_debug(&mut self)441     pub fn line_status_set_no_debug(&mut self) {
442         self.line_status.insert(IrqLineStatus::IRQ_NO_BALANCING);
443     }
444 
445     #[allow(dead_code)]
line_status_clear_no_debug(&mut self)446     pub fn line_status_clear_no_debug(&mut self) {
447         self.line_status.remove(IrqLineStatus::IRQ_NO_BALANCING);
448     }
449 
can_autoenable(&self) -> bool450     pub fn can_autoenable(&self) -> bool {
451         !self.line_status.contains(IrqLineStatus::IRQ_NOAUTOEN)
452     }
453 
can_thread(&self) -> bool454     pub fn can_thread(&self) -> bool {
455         !self.line_status.contains(IrqLineStatus::IRQ_NOTHREAD)
456     }
457 
threads_oneshot(&self) -> u64458     pub fn threads_oneshot(&self) -> u64 {
459         self.threads_oneshot
460     }
461 
462     /// 中断是否可以设置CPU亲和性
can_set_affinity(&self) -> bool463     pub fn can_set_affinity(&self) -> bool {
464         if !self.common_data.status().can_balance()
465             || !self
466                 .irq_data()
467                 .chip_info_read_irqsave()
468                 .chip()
469                 .can_set_affinity()
470         {
471             return false;
472         }
473 
474         return true;
475     }
476 
actions(&self) -> &Vec<Arc<IrqAction>>477     pub fn actions(&self) -> &Vec<Arc<IrqAction>> {
478         &self.actions
479     }
480 
add_action(&mut self, action: Arc<IrqAction>)481     pub fn add_action(&mut self, action: Arc<IrqAction>) {
482         self.actions.push(action);
483     }
484 
clear_actions(&mut self)485     pub fn clear_actions(&mut self) {
486         self.actions.clear();
487     }
488 
remove_action(&mut self, action: &Arc<IrqAction>)489     pub fn remove_action(&mut self, action: &Arc<IrqAction>) {
490         self.actions.retain(|a| !Arc::ptr_eq(a, action));
491     }
492 
internal_state(&self) -> &IrqDescState493     pub fn internal_state(&self) -> &IrqDescState {
494         &self.desc_internal_state
495     }
496 
internal_state_mut(&mut self) -> &mut IrqDescState497     pub(super) fn internal_state_mut(&mut self) -> &mut IrqDescState {
498         &mut self.desc_internal_state
499     }
500 
irq_data(&self) -> &Arc<IrqData>501     pub fn irq_data(&self) -> &Arc<IrqData> {
502         &self.irq_data
503     }
504 
common_data(&self) -> &Arc<IrqCommonData>505     pub fn common_data(&self) -> &Arc<IrqCommonData> {
506         &self.common_data
507     }
508 
depth(&self) -> u32509     pub fn depth(&self) -> u32 {
510         self.depth
511     }
512 
wake_depth(&self) -> u32513     pub fn wake_depth(&self) -> u32 {
514         self.wake_depth
515     }
516 
set_depth(&mut self, depth: u32)517     pub fn set_depth(&mut self, depth: u32) {
518         self.depth = depth;
519     }
520 
set_trigger_type(&mut self, trigger: IrqLineStatus)521     pub fn set_trigger_type(&mut self, trigger: IrqLineStatus) {
522         self.line_status.remove(IrqLineStatus::IRQ_TYPE_SENSE_MASK);
523         self.line_status
524             .insert(trigger & IrqLineStatus::IRQ_TYPE_SENSE_MASK);
525     }
526 
clear_level(&mut self)527     pub fn clear_level(&mut self) {
528         self.line_status.remove(IrqLineStatus::IRQ_LEVEL);
529     }
530 
set_level(&mut self)531     pub fn set_level(&mut self) {
532         self.line_status.insert(IrqLineStatus::IRQ_LEVEL);
533     }
534 
percpu_enabled(&self) -> &Option<CpuMask>535     pub fn percpu_enabled(&self) -> &Option<CpuMask> {
536         &self.percpu_enabled
537     }
538 
percpu_enabled_mut(&mut self) -> &mut Option<CpuMask>539     pub fn percpu_enabled_mut(&mut self) -> &mut Option<CpuMask> {
540         &mut self.percpu_enabled
541     }
542 
percpu_affinity(&self) -> &Option<CpuMask>543     pub fn percpu_affinity(&self) -> &Option<CpuMask> {
544         &self.percpu_affinity
545     }
546 
percpu_affinity_mut(&mut self) -> &mut Option<CpuMask>547     pub fn percpu_affinity_mut(&mut self) -> &mut Option<CpuMask> {
548         &mut self.percpu_affinity
549     }
550 }
551 
552 impl KObject for IrqDesc {
as_any_ref(&self) -> &dyn Any553     fn as_any_ref(&self) -> &dyn Any {
554         self
555     }
556 
set_inode(&self, inode: Option<Arc<KernFSInode>>)557     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
558         self.inner().kern_inode = inode;
559     }
560 
inode(&self) -> Option<Arc<KernFSInode>>561     fn inode(&self) -> Option<Arc<KernFSInode>> {
562         self.inner().kern_inode.clone()
563     }
564 
parent(&self) -> Option<Weak<dyn KObject>>565     fn parent(&self) -> Option<Weak<dyn KObject>> {
566         self.inner().parent_kobj.clone()
567     }
568 
set_parent(&self, parent: Option<Weak<dyn KObject>>)569     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
570         self.inner().parent_kobj = parent;
571     }
572 
kset(&self) -> Option<Arc<KSet>>573     fn kset(&self) -> Option<Arc<KSet>> {
574         self.inner().kset.clone()
575     }
576 
set_kset(&self, kset: Option<Arc<KSet>>)577     fn set_kset(&self, kset: Option<Arc<KSet>>) {
578         self.inner().kset = kset;
579     }
580 
kobj_type(&self) -> Option<&'static dyn KObjType>581     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
582         Some(&IrqKObjType)
583     }
584 
set_kobj_type(&self, _ktype: Option<&'static dyn KObjType>)585     fn set_kobj_type(&self, _ktype: Option<&'static dyn KObjType>) {}
586 
name(&self) -> String587     fn name(&self) -> String {
588         self.inner().irq_data.irq().data().to_string()
589     }
590 
set_name(&self, _name: String)591     fn set_name(&self, _name: String) {}
592 
kobj_state(&self) -> RwLockReadGuard<KObjectState>593     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
594         self.kobj_state.read()
595     }
596 
kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState>597     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
598         self.kobj_state.write()
599     }
600 
set_kobj_state(&self, state: KObjectState)601     fn set_kobj_state(&self, state: KObjectState) {
602         *self.kobj_state_mut() = state;
603     }
604 }
605 
606 bitflags! {
607     /// Bit masks for desc->desc_internal_state
608     pub struct IrqDescState: u32 {
609         /// autodetection in progress
610         const IRQS_AUTODETECT = 0x00000001;
611         /// was disabled due to spurious interrupt detection
612         const IRQS_SPURIOUS_DISABLED = 0x00000002;
613         /// polling in progress
614         const IRQS_POLL_INPROGRESS = 0x00000008;
615         /// irq is not unmasked in primary handler
616         const IRQS_ONESHOT = 0x00000020;
617         /// irq is replayed
618         const IRQS_REPLAY = 0x00000040;
619         /// irq is waiting
620         const IRQS_WAITING = 0x00000080;
621         /// irq is pending and replayed later
622         const IRQS_PENDING = 0x00000200;
623         /// irq is suspended
624         const IRQS_SUSPENDED = 0x00000800;
625         /// irq line is used to deliver NMIs
626         const IRQS_NMI = 0x00002000;
627         /// descriptor has been added to sysfs
628         const IRQS_SYSFS = 0x00004000;
629     }
630 }
631 
632 /// 每个中断的响应动作的描述符
633 /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/include/linux/interrupt.h#118
634 #[allow(dead_code)]
635 #[derive(Debug)]
636 pub struct IrqAction {
637     inner: SpinLock<InnerIrqAction>,
638     /// 用于等待线程被创建的完成量
639     thread_completion: Completion,
640 }
641 
642 impl IrqAction {
643     #[allow(dead_code)]
new( irq: IrqNumber, name: String, handler: Option<&'static dyn IrqHandler>, thread_fn: Option<&'static dyn IrqHandler>, ) -> Arc<Self>644     pub fn new(
645         irq: IrqNumber,
646         name: String,
647         handler: Option<&'static dyn IrqHandler>,
648         thread_fn: Option<&'static dyn IrqHandler>,
649     ) -> Arc<Self> {
650         let action: IrqAction = IrqAction {
651             inner: SpinLock::new(InnerIrqAction {
652                 dev_id: None,
653                 per_cpu_dev_id: None,
654                 handler,
655                 thread_fn,
656                 thread: None,
657                 secondary: None,
658                 irq,
659                 flags: IrqHandleFlags::empty(),
660                 name,
661                 thread_flags: ThreadedHandlerFlags::empty(),
662             }),
663             thread_completion: Completion::new(),
664         };
665 
666         return Arc::new(action);
667     }
668 
inner(&self) -> SpinLockGuard<InnerIrqAction>669     pub fn inner(&self) -> SpinLockGuard<InnerIrqAction> {
670         self.inner.lock_irqsave()
671     }
672 
thread_completion(&self) -> &Completion673     pub fn thread_completion(&self) -> &Completion {
674         &self.thread_completion
675     }
676 }
677 
678 #[allow(dead_code)]
679 #[derive(Debug)]
680 pub struct InnerIrqAction {
681     /// cookie to identify the device
682     dev_id: Option<Arc<DeviceId>>,
683     /// cookie to identify the device (per cpu)
684     per_cpu_dev_id: Option<PerCpuVar<Arc<DeviceId>>>,
685     /// 中断处理程序
686     handler: Option<&'static dyn IrqHandler>,
687     /// interrupt handler function for threaded interrupts
688     thread_fn: Option<&'static dyn IrqHandler>,
689     /// thread pointer for threaded interrupts
690     thread: Option<Arc<ProcessControlBlock>>,
691     /// pointer to secondary irqaction (force threading)
692     secondary: Option<Arc<IrqAction>>,
693     /// 中断号
694     irq: IrqNumber,
695     flags: IrqHandleFlags,
696     /// 中断线程的标志
697     thread_flags: ThreadedHandlerFlags,
698     /// name of the device
699     name: String,
700 }
701 
702 impl InnerIrqAction {
dev_id(&self) -> &Option<Arc<DeviceId>>703     pub fn dev_id(&self) -> &Option<Arc<DeviceId>> {
704         &self.dev_id
705     }
706 
dev_id_mut(&mut self) -> &mut Option<Arc<DeviceId>>707     pub fn dev_id_mut(&mut self) -> &mut Option<Arc<DeviceId>> {
708         &mut self.dev_id
709     }
710 
per_cpu_dev_id(&self) -> Option<&Arc<DeviceId>>711     pub fn per_cpu_dev_id(&self) -> Option<&Arc<DeviceId>> {
712         self.per_cpu_dev_id.as_ref().map(|v| v.get())
713     }
714 
715     #[allow(dead_code)]
per_cpu_dev_id_mut(&mut self) -> Option<&mut Arc<DeviceId>>716     pub fn per_cpu_dev_id_mut(&mut self) -> Option<&mut Arc<DeviceId>> {
717         self.per_cpu_dev_id.as_mut().map(|v| v.get_mut())
718     }
719 
handler(&self) -> Option<&'static dyn IrqHandler>720     pub fn handler(&self) -> Option<&'static dyn IrqHandler> {
721         self.handler
722     }
723 
set_handler(&mut self, handler: Option<&'static dyn IrqHandler>)724     pub fn set_handler(&mut self, handler: Option<&'static dyn IrqHandler>) {
725         self.handler = handler;
726     }
727 
thread_fn(&self) -> Option<&'static dyn IrqHandler>728     pub fn thread_fn(&self) -> Option<&'static dyn IrqHandler> {
729         self.thread_fn
730     }
731 
thread(&self) -> Option<Arc<ProcessControlBlock>>732     pub fn thread(&self) -> Option<Arc<ProcessControlBlock>> {
733         self.thread.clone()
734     }
735 
set_thread(&mut self, thread: Option<Arc<ProcessControlBlock>>)736     pub fn set_thread(&mut self, thread: Option<Arc<ProcessControlBlock>>) {
737         self.thread = thread;
738     }
739 
740     #[allow(dead_code)]
thread_flags(&self) -> &ThreadedHandlerFlags741     pub fn thread_flags(&self) -> &ThreadedHandlerFlags {
742         &self.thread_flags
743     }
744 
thread_flags_mut(&mut self) -> &mut ThreadedHandlerFlags745     pub fn thread_flags_mut(&mut self) -> &mut ThreadedHandlerFlags {
746         &mut self.thread_flags
747     }
748 
secondary(&self) -> Option<Arc<IrqAction>>749     pub fn secondary(&self) -> Option<Arc<IrqAction>> {
750         self.secondary.clone()
751     }
752 
753     #[allow(dead_code)]
irq(&self) -> IrqNumber754     pub fn irq(&self) -> IrqNumber {
755         self.irq
756     }
757 
758     #[allow(dead_code)]
set_irq(&mut self, irq: IrqNumber)759     pub fn set_irq(&mut self, irq: IrqNumber) {
760         self.irq = irq;
761     }
762 
flags(&self) -> &IrqHandleFlags763     pub fn flags(&self) -> &IrqHandleFlags {
764         &self.flags
765     }
766 
flags_mut(&mut self) -> &mut IrqHandleFlags767     pub fn flags_mut(&mut self) -> &mut IrqHandleFlags {
768         &mut self.flags
769     }
770 
name(&self) -> &String771     pub fn name(&self) -> &String {
772         &self.name
773     }
774 }
775 
776 bitflags! {
777     /// 这些标志由线程处理程序使用
778     pub struct ThreadedHandlerFlags: u32 {
779         /// IRQTF_RUNTHREAD - 表示应运行中断处理程序线程
780         const IRQTF_RUNTHREAD = 1 << 0;
781         /// IRQTF_WARNED - 已打印警告 "IRQ_WAKE_THREAD w/o thread_fn"
782         const IRQTF_WARNED = 1 << 1;
783         /// IRQTF_AFFINITY - 请求irq线程调整亲和性
784         const IRQTF_AFFINITY = 1 << 2;
785         /// IRQTF_FORCED_THREAD - irq操作被强制线程化
786         const IRQTF_FORCED_THREAD = 1 << 3;
787         /// IRQTF_READY - 表示irq线程已准备就绪
788         const IRQTF_READY = 1 << 4;
789     }
790 }
791 
792 /// Implements the `ThreadedHandlerFlags` structure.
793 impl ThreadedHandlerFlags {
794     /// 在 `ThreadedHandlerFlags` 结构中测试并设置特定的位。
795     ///
796     /// # 参数
797     ///
798     /// * `bit` - 要测试并设置的位。
799     ///
800     /// # 返回
801     ///
802     /// 如果操作前该位已被设置,则返回 `true`,否则返回 `false`。
test_and_set_bit(&mut self, bit: ThreadedHandlerFlags) -> bool803     pub fn test_and_set_bit(&mut self, bit: ThreadedHandlerFlags) -> bool {
804         let res = (self.bits & bit.bits) != 0;
805         self.bits |= bit.bits;
806         return res;
807     }
808 }
809 
810 // 定义IrqFlags位标志
811 
812 bitflags! {
813     /// 这些标志仅由内核在中断处理例程中使用。
814     #[allow(clippy::bad_bit_mask)]
815     pub struct IrqHandleFlags: u32 {
816 
817         const IRQF_TRIGGER_NONE = IrqLineStatus::IRQ_TYPE_NONE.bits();
818         const IRQF_TRIGGER_RISING = IrqLineStatus::IRQ_TYPE_EDGE_RISING.bits();
819         const IRQF_TRIGGER_FALLING = IrqLineStatus::IRQ_TYPE_EDGE_FALLING.bits();
820         const IRQF_TRIGGER_HIGH = IrqLineStatus::IRQ_TYPE_LEVEL_HIGH.bits();
821         const IRQF_TRIGGER_LOW = IrqLineStatus::IRQ_TYPE_LEVEL_LOW.bits();
822         const IRQF_TRIGGER_MASK = Self::IRQF_TRIGGER_HIGH.bits | Self::IRQF_TRIGGER_LOW.bits | Self::IRQF_TRIGGER_RISING.bits | Self::IRQF_TRIGGER_FALLING.bits;
823         /// IRQF_SHARED - 允许多个设备共享中断
824         const IRQF_SHARED = 0x00000080;
825         /// IRQF_PROBE_SHARED - 当预期出现共享不匹配时,由调用者设置
826         const IRQF_PROBE_SHARED = 0x00000100;
827         /// IRQF_TIMER - 标记此中断为定时器中断
828         const __IRQF_TIMER = 0x00000200;
829         /// IRQF_PERCPU - 中断是每个CPU的
830         const IRQF_PERCPU = 0x00000400;
831         /// IRQF_NOBALANCING - 将此中断从中断平衡中排除
832         const IRQF_NOBALANCING = 0x00000800;
833         /// IRQF_IRQPOLL - 中断用于轮询(出于性能原因,只有在共享中断中首次注册的中断会被考虑)
834         const IRQF_IRQPOLL = 0x00001000;
835         /// IRQF_ONESHOT - 在硬中断处理程序完成后,不会重新启用中断。由需要在运行线程处理程序之前保持中断线路禁用的线程中断使用。
836         const IRQF_ONESHOT = 0x00002000;
837         /// IRQF_NO_SUSPEND - 在挂起期间不禁用此IRQ。不能保证此中断会从挂起状态唤醒系统。
838         const IRQF_NO_SUSPEND = 0x00004000;
839         /// IRQF_FORCE_RESUME - 即使设置了IRQF_NO_SUSPEND,也强制在恢复时启用它
840         const IRQF_FORCE_RESUME = 0x00008000;
841         /// IRQF_NO_THREAD - 中断不能被线程化
842         const IRQF_NO_THREAD = 0x00010000;
843         /// IRQF_EARLY_RESUME - 在syscore而不是在设备恢复时间早期恢复IRQ。
844         const IRQF_EARLY_RESUME = 0x00020000;
845         /// IRQF_COND_SUSPEND - 如果IRQ与NO_SUSPEND用户共享,则在挂起中断后执行此中断处理程序。对于系统唤醒设备用户,需要在他们的中断处理程序中实现唤醒检测。
846         const IRQF_COND_SUSPEND = 0x00040000;
847         /// IRQF_NO_AUTOEN - 当用户请求时,不会自动启用IRQ或NMI。用户稍后会通过enable_irq()或enable_nmi()显式启用它。
848         const IRQF_NO_AUTOEN = 0x00080000;
849         /// IRQF_NO_DEBUG - 从IPI和类似处理程序的逃逸检测中排除,取决于IRQF_PERCPU。
850         const IRQF_NO_DEBUG = 0x00100000;
851         const IRQF_TIMER = Self::__IRQF_TIMER.bits | Self::IRQF_NO_SUSPEND.bits | Self::IRQF_NO_THREAD.bits;
852     }
853 }
854 
855 impl IrqHandleFlags {
856     /// 检查是否指定了触发类型
857     #[inline(always)]
trigger_type_specified(&self) -> bool858     pub fn trigger_type_specified(&self) -> bool {
859         (self.bits & Self::IRQF_TRIGGER_MASK.bits) != 0
860     }
861 
862     /// 插入触发类型
insert_trigger_type(&mut self, trigger: IrqLineStatus)863     pub fn insert_trigger_type(&mut self, trigger: IrqLineStatus) {
864         self.bits |= trigger.trigger_bits() & IrqHandleFlags::IRQF_TRIGGER_MASK.bits;
865     }
866 
867     #[allow(dead_code)]
remove_trigger_type(&mut self, trigger: IrqLineStatus)868     pub fn remove_trigger_type(&mut self, trigger: IrqLineStatus) {
869         self.bits &= !(trigger.trigger_bits() & IrqHandleFlags::IRQF_TRIGGER_MASK.bits);
870     }
871 
trigger_type(&self) -> IrqLineStatus872     pub fn trigger_type(&self) -> IrqLineStatus {
873         IrqLineStatus::from_bits_truncate(self.bits & IrqHandleFlags::IRQF_TRIGGER_MASK.bits)
874     }
875 }
876 
877 #[inline(never)]
early_irq_init() -> Result<(), SystemError>878 pub(super) fn early_irq_init() -> Result<(), SystemError> {
879     let irqcnt = CurrentIrqArch::probe_total_irq_num();
880     let mut manager = IrqDescManager::new();
881     for i in 0..irqcnt {
882         let irq_desc = IrqDesc::new(IrqNumber::new(i), None, IrqStatus::empty());
883         manager.insert(IrqNumber::new(i), irq_desc);
884     }
885 
886     unsafe {
887         IRQ_DESC_MANAGER = Some(manager);
888     }
889 
890     return CurrentIrqArch::arch_early_irq_init();
891 }
892 
893 static mut IRQ_DESC_MANAGER: Option<IrqDescManager> = None;
894 
895 /// 获取中断描述符管理器的引用
896 #[inline(always)]
irq_desc_manager() -> &'static IrqDescManager897 pub fn irq_desc_manager() -> &'static IrqDescManager {
898     return unsafe { IRQ_DESC_MANAGER.as_ref().unwrap() };
899 }
900 
901 pub struct IrqDescManager {
902     irq_descs: BTreeMap<IrqNumber, Arc<IrqDesc>>,
903 }
904 
905 impl IrqDescManager {
new() -> Self906     fn new() -> Self {
907         IrqDescManager {
908             irq_descs: BTreeMap::new(),
909         }
910     }
911 
912     /// 查找中断描述符
lookup(&self, irq: IrqNumber) -> Option<Arc<IrqDesc>>913     pub fn lookup(&self, irq: IrqNumber) -> Option<Arc<IrqDesc>> {
914         self.irq_descs.get(&irq).cloned()
915     }
916 
917     /// 查找中断描述符并锁定总线(没有对irqdesc进行加锁)
918     #[allow(dead_code)]
lookup_and_lock_bus( &self, irq: IrqNumber, check_global: bool, check_percpu: bool, ) -> Option<Arc<IrqDesc>>919     pub fn lookup_and_lock_bus(
920         &self,
921         irq: IrqNumber,
922         check_global: bool,
923         check_percpu: bool,
924     ) -> Option<Arc<IrqDesc>> {
925         self.do_lookup_and_lock(irq, true, check_global, check_percpu)
926     }
927 
do_lookup_and_lock( &self, irq: IrqNumber, lock_bus: bool, check_global: bool, check_percpu: bool, ) -> Option<Arc<IrqDesc>>928     fn do_lookup_and_lock(
929         &self,
930         irq: IrqNumber,
931         lock_bus: bool,
932         check_global: bool,
933         check_percpu: bool,
934     ) -> Option<Arc<IrqDesc>> {
935         let desc = self.lookup(irq)?;
936         if check_global || check_percpu {
937             if check_percpu && !desc.inner().line_status().is_per_cpu_devid() {
938                 return None;
939             }
940 
941             if check_global && desc.inner().line_status().is_per_cpu_devid() {
942                 return None;
943             }
944         }
945 
946         if lock_bus {
947             desc.chip_bus_lock();
948         }
949 
950         return Some(desc);
951     }
952 
insert(&mut self, irq: IrqNumber, desc: Arc<IrqDesc>)953     fn insert(&mut self, irq: IrqNumber, desc: Arc<IrqDesc>) {
954         self.irq_descs.insert(irq, desc);
955     }
956 
957     /// 释放中断描述符
958     #[allow(dead_code)]
free_desc(&mut self, irq: IrqNumber)959     fn free_desc(&mut self, irq: IrqNumber) {
960         if let Some(desc) = self.irq_descs.get(&irq) {
961             irq_sysfs_del(desc);
962             self.irq_descs.remove(&irq);
963         }
964     }
965 
966     /// 迭代中断描述符
iter_descs(&self) -> btree_map::Iter<'_, IrqNumber, Arc<IrqDesc>>967     pub fn iter_descs(&self) -> btree_map::Iter<'_, IrqNumber, Arc<IrqDesc>> {
968         self.irq_descs.iter()
969     }
970 
971     /// 设置指定irq的可用cpu为所有cpu
set_percpu_devid_all(&self, irq: IrqNumber) -> Result<(), SystemError>972     pub fn set_percpu_devid_all(&self, irq: IrqNumber) -> Result<(), SystemError> {
973         self.set_percpu_devid(irq, None)
974     }
975 
976     /// 设置指定irq的可用cpu
977     ///
978     /// 如果affinity为None,则表示设置为所有cpu
set_percpu_devid( &self, irq: IrqNumber, affinity: Option<&CpuMask>, ) -> Result<(), SystemError>979     pub fn set_percpu_devid(
980         &self,
981         irq: IrqNumber,
982         affinity: Option<&CpuMask>,
983     ) -> Result<(), SystemError> {
984         let desc = self.lookup(irq).ok_or(SystemError::EINVAL)?;
985         let mut desc_inner = desc.inner();
986 
987         if desc_inner.percpu_enabled().is_some() {
988             return Err(SystemError::EINVAL);
989         }
990 
991         *desc_inner.percpu_enabled_mut() = Some(CpuMask::new());
992 
993         if let Some(affinity) = affinity {
994             desc_inner.percpu_affinity_mut().replace(affinity.clone());
995         } else {
996             desc_inner
997                 .percpu_affinity_mut()
998                 .replace(smp_cpu_manager().possible_cpus().clone());
999         }
1000 
1001         drop(desc_inner);
1002 
1003         desc.set_percpu_devid_flags();
1004 
1005         return Ok(());
1006     }
1007 }
1008 
1009 pub struct GenericIrqHandler;
1010 
1011 #[allow(dead_code)]
1012 impl GenericIrqHandler {
1013     /// `handle_domain_irq` - 调用属于某个中断域的硬件中断的处理程序
1014     ///
1015     /// # 参数
1016     ///
1017     /// * `domain`: 执行查找的域
1018     /// * `hwirq`: 要转换为逻辑中断的硬件中断号
1019     ///
1020     /// # 返回
1021     ///
1022     /// 成功时返回 `Ok(())`,如果转换失败则返回 `Err(SystemError)`
1023     ///
1024     /// 此函数必须在初始化了中断寄存器的中断上下文中调用
1025     ///
1026     /// 参考 https://code.dragonos.org.cn/xref/linux-6.6.21/kernel/irq/irqdesc.c?fi=generic_handle_domain_irq#726
handle_domain_irq( domain: Arc<IrqDomain>, hwirq: HardwareIrqNumber, trap_frame: &mut TrapFrame, ) -> Result<(), SystemError>1027     pub fn handle_domain_irq(
1028         domain: Arc<IrqDomain>,
1029         hwirq: HardwareIrqNumber,
1030         trap_frame: &mut TrapFrame,
1031     ) -> Result<(), SystemError> {
1032         let (irq_desc, _) =
1033             irq_domain_manager().resolve_irq_mapping(Some(domain.clone()), hwirq)?;
1034 
1035         irq_desc.handler().unwrap().handle(&irq_desc, trap_frame);
1036 
1037         return Ok(());
1038     }
1039 }
1040