1 use core::ops::{BitXor, Deref, DerefMut};
2
3 use alloc::{string::String, sync::Arc};
4
5 use system_error::SystemError;
6
7 use crate::{
8 driver::base::device::DeviceId,
9 exception::{
10 irqchip::IrqChipSetMaskResult,
11 irqdesc::{irq_desc_manager, InnerIrqDesc, IrqAction},
12 },
13 libs::{cpumask::CpuMask, spinlock::SpinLockGuard},
14 process::{kthread::KernelThreadMechanism, ProcessManager},
15 smp::cpu::ProcessorId,
16 };
17
18 use super::{
19 dummychip::no_irq_chip,
20 irqchip::IrqChipFlags,
21 irqdata::{IrqData, IrqHandlerData, IrqLineStatus, IrqStatus},
22 irqdesc::{InnerIrqAction, IrqDesc, IrqDescState, IrqHandleFlags, IrqHandler, IrqReturn},
23 irqdomain::irq_domain_manager,
24 IrqNumber,
25 };
26
27 lazy_static! {
28 /// 默认的中断亲和性
29 static ref IRQ_DEFAULT_AFFINITY: CpuMask = {
30 let mut mask = CpuMask::new();
31 // 默认情况下,中断处理程序将在第一个处理器上运行
32 mask.set(ProcessorId::new(0), true);
33 mask
34 };
35 }
36
irq_manager() -> &'static IrqManager37 pub fn irq_manager() -> &'static IrqManager {
38 &IrqManager
39 }
40
41 /// 中断管理器
42 pub struct IrqManager;
43
44 impl IrqManager {
45 pub const IRQ_RESEND: bool = true;
46 #[allow(dead_code)]
47 pub const IRQ_NORESEND: bool = false;
48 #[allow(dead_code)]
49 pub const IRQ_START_FORCE: bool = true;
50 pub const IRQ_START_COND: bool = false;
51
52 /// 在中断线上添加一个处理函数
53 ///
54 /// ## 参数
55 ///
56 /// - irq: 虚拟中断号(中断线号)
57 /// - name: 生成该中断的设备名称
58 /// - handler: 中断处理函数
59 /// - flags: 中断处理标志
60 /// - dev_id: 一个用于标识设备的cookie
request_irq( &self, irq: IrqNumber, name: String, handler: &'static dyn IrqHandler, flags: IrqHandleFlags, dev_id: Option<Arc<DeviceId>>, ) -> Result<(), SystemError>61 pub fn request_irq(
62 &self,
63 irq: IrqNumber,
64 name: String,
65 handler: &'static dyn IrqHandler,
66 flags: IrqHandleFlags,
67 dev_id: Option<Arc<DeviceId>>,
68 ) -> Result<(), SystemError> {
69 return self.request_threaded_irq(irq, Some(handler), None, flags, name, dev_id);
70 }
71
72 /// 在中断线上添加一个处理函数(可以是线程化的中断)
73 ///
74 /// ## 参数
75 ///
76 /// - irq: 虚拟中断号
77 /// - handler: 当中断发生时将被调用的函数,是
78 /// 线程化中断的初级处理程序。如果handler为`None`并且thread_fn不为`None`,
79 /// 将安装默认的初级处理程序
80 /// - thread_fn: 在中断处理程序线程中调用的函数. 如果为`None`,则不会创建irq线程
81 /// - flags: 中断处理标志
82 /// - IRQF_SHARED: 中断是共享的
83 /// - IRQF_TRIGGER*: 指定中断触发方式
84 /// - IRQF_ONESHOT: 在thread_fn中运行时,中断线被遮蔽
85 /// - dev_name: 生成该中断的设备名称
86 /// - dev_id: 一个用于标识设备的cookie
87 ///
88 /// ## 说明
89 ///
90 /// 此调用分配中断资源并启用中断线和IRQ处理。
91 /// 从这一点开始,您的处理程序函数可能会被调用。
92 /// 因此,您必须确保首先初始化您的硬件,
93 /// 并确保以正确的顺序设置中断处理程序。
94 ///
95 /// 如果您想为您的设备设置线程化中断处理程序
96 /// 则需要提供@handler和@thread_fn。@handler仍然
97 /// 在硬中断上下文中调用,并且必须检查
98 /// 中断是否来自设备。如果是,它需要禁用设备上的中断
99 /// 并返回IRQ_WAKE_THREAD,这将唤醒处理程序线程并运行
100 /// @thread_fn。这种拆分处理程序设计是为了支持
101 /// 共享中断。
102 ///
103 /// dev_id必须是全局唯一的。通常使用设备数据结构的地址或者uuid
104 /// 作为cookie。由于处理程序接收这个值,因此使用它是有意义的。
105 ///
106 /// 如果您的中断是共享的,您必须传递一个非NULL的dev_id
107 /// 因为当释放中断时需要它。
request_threaded_irq( &self, irq: IrqNumber, mut handler: Option<&'static dyn IrqHandler>, thread_fn: Option<&'static dyn IrqHandler>, flags: IrqHandleFlags, dev_name: String, dev_id: Option<Arc<DeviceId>>, ) -> Result<(), SystemError>108 pub fn request_threaded_irq(
109 &self,
110 irq: IrqNumber,
111 mut handler: Option<&'static dyn IrqHandler>,
112 thread_fn: Option<&'static dyn IrqHandler>,
113 flags: IrqHandleFlags,
114 dev_name: String,
115 dev_id: Option<Arc<DeviceId>>,
116 ) -> Result<(), SystemError> {
117 if irq == IrqNumber::IRQ_NOTCONNECTED {
118 return Err(SystemError::ENOTCONN);
119 }
120
121 // 逻辑检查:共享中断必须传入一个真正的设备ID,
122 // 否则后来我们将难以确定哪个中断是哪个(会搞乱中断释放逻辑等)。
123 // 此外,共享中断与禁用自动使能不相符。 共享中断可能在仍然禁用时请求它,然后永远等待中断。
124 // 另外,IRQF_COND_SUSPEND 仅适用于共享中断,并且它不能与 IRQF_NO_SUSPEND 同时设置。
125
126 if ((flags.contains(IrqHandleFlags::IRQF_SHARED)) && dev_id.is_none())
127 || ((flags.contains(IrqHandleFlags::IRQF_SHARED))
128 && (flags.contains(IrqHandleFlags::IRQF_NO_AUTOEN)))
129 || (!(flags.contains(IrqHandleFlags::IRQF_SHARED))
130 && (flags.contains(IrqHandleFlags::IRQF_COND_SUSPEND)))
131 || ((flags.contains(IrqHandleFlags::IRQF_NO_SUSPEND))
132 && (flags.contains(IrqHandleFlags::IRQF_COND_SUSPEND)))
133 {
134 return Err(SystemError::EINVAL);
135 }
136 let desc = irq_desc_manager().lookup(irq).ok_or(SystemError::EINVAL)?;
137 if !desc.can_request() {
138 kwarn!("desc {} can not request", desc.irq().data());
139 return Err(SystemError::EINVAL);
140 }
141
142 if handler.is_none() {
143 if thread_fn.is_none() {
144 // 不允许中断处理函数和线程处理函数都为空
145 return Err(SystemError::EINVAL);
146 }
147
148 // 如果中断处理函数为空,线程处理函数不为空,则使用默认的中断处理函数
149 handler = Some(&DefaultPrimaryIrqHandler);
150 }
151
152 let irqaction = IrqAction::new(irq, dev_name, handler, thread_fn);
153
154 let mut action_guard = irqaction.inner();
155 *action_guard.flags_mut() = flags;
156 *action_guard.dev_id_mut() = dev_id;
157 drop(action_guard);
158
159 return self.inner_setup_irq(irq, irqaction, desc);
160 }
161
162 /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/kernel/irq/manage.c?r=&mo=59252&fi=2138#1497
163 #[inline(never)]
inner_setup_irq( &self, irq: IrqNumber, action: Arc<IrqAction>, desc: Arc<IrqDesc>, ) -> Result<(), SystemError>164 fn inner_setup_irq(
165 &self,
166 irq: IrqNumber,
167 action: Arc<IrqAction>,
168 desc: Arc<IrqDesc>,
169 ) -> Result<(), SystemError> {
170 // ==== 定义错误处理函数 ====
171 let err_out_thread =
172 |e: SystemError, mut action_guard: SpinLockGuard<'_, InnerIrqAction>| -> SystemError {
173 if let Some(thread_pcb) = action_guard.thread() {
174 action_guard.set_thread(None);
175 KernelThreadMechanism::stop(&thread_pcb).ok();
176 }
177
178 if let Some(secondary) = action_guard.secondary() {
179 let mut secondary_guard = secondary.inner();
180 if let Some(thread_pcb) = secondary_guard.thread() {
181 secondary_guard.set_thread(None);
182 KernelThreadMechanism::stop(&thread_pcb).ok();
183 }
184 }
185 return e;
186 };
187
188 let err_out_bus_unlock = |e: SystemError,
189 desc: Arc<IrqDesc>,
190 req_mutex_guard: crate::libs::mutex::MutexGuard<'_, ()>,
191 action_guard: SpinLockGuard<'_, InnerIrqAction>|
192 -> SystemError {
193 desc.chip_bus_sync_unlock();
194 drop(req_mutex_guard);
195 return err_out_thread(e, action_guard);
196 };
197
198 let err_out_unlock = |e: SystemError,
199 desc_guard: SpinLockGuard<'_, InnerIrqDesc>,
200 desc: Arc<IrqDesc>,
201 req_mutex_guard: crate::libs::mutex::MutexGuard<'_, ()>,
202 action_guard: SpinLockGuard<'_, InnerIrqAction>|
203 -> SystemError {
204 drop(desc_guard);
205 return err_out_bus_unlock(e, desc, req_mutex_guard, action_guard);
206 };
207
208 let err_out_mismatch = |old_action_guard: SpinLockGuard<'_, InnerIrqAction>,
209 desc_guard: SpinLockGuard<'_, InnerIrqDesc>,
210 action_guard: SpinLockGuard<'_, InnerIrqAction>,
211 desc: Arc<IrqDesc>,
212 req_mutex_guard: crate::libs::mutex::MutexGuard<'_, ()>|
213 -> SystemError {
214 if !action_guard
215 .flags()
216 .contains(IrqHandleFlags::IRQF_PROBE_SHARED)
217 {
218 kerror!("Flags mismatch for irq {} (name: {}, flags: {:?}). old action name: {}, old flags: {:?}", irq.data(), action_guard.name(), action_guard.flags(), old_action_guard.name(), old_action_guard.flags());
219 }
220 return err_out_unlock(
221 SystemError::EBUSY,
222 desc_guard,
223 desc,
224 req_mutex_guard,
225 action_guard,
226 );
227 };
228
229 // ===== 代码开始 =====
230
231 if Arc::ptr_eq(
232 &desc.irq_data().chip_info_read_irqsave().chip(),
233 &no_irq_chip(),
234 ) {
235 return Err(SystemError::ENOSYS);
236 }
237
238 let mut action_guard = action.inner();
239 if !action_guard.flags().trigger_type_specified() {
240 // 如果没有指定触发类型,则使用默认的触发类型
241 action_guard
242 .flags_mut()
243 .insert_trigger_type(desc.irq_data().common_data().trigger_type())
244 }
245
246 let nested = desc.nested_thread();
247
248 if nested {
249 if action_guard.thread_fn().is_none() {
250 return Err(SystemError::EINVAL);
251 }
252
253 action_guard.set_handler(Some(&IrqNestedPrimaryHandler));
254 } else {
255 if desc.can_thread() {
256 self.setup_forced_threading(action_guard.deref_mut())?;
257 }
258 }
259
260 // 如果具有中断线程处理程序,并且中断不是嵌套的,则设置中断线程
261 if action_guard.thread_fn().is_some() && !nested {
262 self.setup_irq_thread(irq, action_guard.deref(), false)?;
263
264 if let Some(secondary) = action_guard.secondary() {
265 let secondary_guard = secondary.inner();
266 if let Err(e) = self.setup_irq_thread(irq, secondary_guard.deref(), true) {
267 return Err(err_out_thread(e, action_guard));
268 }
269 }
270 }
271
272 // Drivers are often written to work w/o knowledge about the
273 // underlying irq chip implementation, so a request for a
274 // threaded irq without a primary hard irq context handler
275 // requires the ONESHOT flag to be set. Some irq chips like
276 // MSI based interrupts are per se one shot safe. Check the
277 // chip flags, so we can avoid the unmask dance at the end of
278 // the threaded handler for those.
279
280 if desc
281 .irq_data()
282 .chip_info_read_irqsave()
283 .chip()
284 .flags()
285 .contains(IrqChipFlags::IRQCHIP_ONESHOT_SAFE)
286 {
287 *action_guard.flags_mut() &= !IrqHandleFlags::IRQF_ONESHOT;
288 }
289
290 // Protects against a concurrent __free_irq() call which might wait
291 // for synchronize_hardirq() to complete without holding the optional
292 // chip bus lock and desc->lock. Also protects against handing out
293 // a recycled oneshot thread_mask bit while it's still in use by
294 // its previous owner.
295 let req_mutex_guard = desc.request_mutex_lock();
296
297 // Acquire bus lock as the irq_request_resources() callback below
298 // might rely on the serialization or the magic power management
299 // functions which are abusing the irq_bus_lock() callback,
300 desc.chip_bus_lock();
301
302 // 如果当前中断线上还没有irqaction, 则先为中断线申请资源
303 if desc.actions().is_empty() {
304 if let Err(e) = self.irq_request_resources(desc.clone()) {
305 kerror!(
306 "Failed to request resources for {} (irq {}) on irqchip {}, error {:?}",
307 action_guard.name(),
308 irq.data(),
309 desc.irq_data().chip_info_read_irqsave().chip().name(),
310 e
311 );
312 return Err(err_out_bus_unlock(
313 e,
314 desc.clone(),
315 req_mutex_guard,
316 action_guard,
317 ));
318 }
319 }
320
321 let mut desc_inner_guard: SpinLockGuard<'_, InnerIrqDesc> = desc.inner();
322
323 // 标记当前irq是否是共享的
324 let mut irq_shared = false;
325 if desc_inner_guard.actions().is_empty() == false {
326 // 除非双方都同意并且是相同类型(级别、边沿、极性),否则不能共享中断。
327 // 因此,两个标志字段都必须设置IRQF_SHARED,并且设置触发类型的位必须匹配。
328 // 另外,所有各方都必须就ONESHOT达成一致。
329 // NMI用途的中断线不能共享。
330 if desc_inner_guard
331 .internal_state()
332 .contains(IrqDescState::IRQS_NMI)
333 {
334 kerror!(
335 "Invalid attempt to share NMI for {} (irq {}) on irqchip {}",
336 action_guard.name(),
337 irq.data(),
338 desc_inner_guard
339 .irq_data()
340 .chip_info_read_irqsave()
341 .chip()
342 .name()
343 );
344 return Err(err_out_unlock(
345 SystemError::EINVAL,
346 desc_inner_guard,
347 desc.clone(),
348 req_mutex_guard,
349 action_guard,
350 ));
351 }
352
353 let irq_data = desc_inner_guard.irq_data();
354
355 let old_trigger_type: super::irqdata::IrqLineStatus;
356 let status = irq_data.common_data().status();
357 if status.trigger_type_was_set() {
358 old_trigger_type = status.trigger_type();
359 } else {
360 old_trigger_type = action_guard.flags().trigger_type();
361 irq_data.common_data().set_trigger_type(old_trigger_type);
362 }
363
364 let old = &desc_inner_guard.actions()[0].clone();
365 let old_guard = old.inner();
366
367 if ((old_guard
368 .flags()
369 .intersection(*action_guard.flags())
370 .contains(IrqHandleFlags::IRQF_SHARED))
371 == false)
372 || (old_trigger_type != (action_guard.flags().trigger_type()))
373 || ((old_guard.flags().bitxor(*action_guard.flags()))
374 .contains(IrqHandleFlags::IRQF_ONESHOT))
375 {
376 return Err(err_out_mismatch(
377 old_guard,
378 desc_inner_guard,
379 action_guard,
380 desc.clone(),
381 req_mutex_guard,
382 ));
383 }
384
385 // all handlers must agree on per-cpuness
386 if *old_guard.flags() & IrqHandleFlags::IRQF_PERCPU
387 != *action_guard.flags() & IrqHandleFlags::IRQF_PERCPU
388 {
389 return Err(err_out_mismatch(
390 old_guard,
391 desc_inner_guard,
392 action_guard,
393 desc.clone(),
394 req_mutex_guard,
395 ));
396 }
397
398 irq_shared = true;
399 }
400
401 if action_guard.flags().contains(IrqHandleFlags::IRQF_ONESHOT) {
402 // todo: oneshot
403 } else if action_guard.handler().is_some_and(|h| {
404 h.type_id() == (&DefaultPrimaryIrqHandler as &dyn IrqHandler).type_id()
405 }) && desc_inner_guard
406 .irq_data()
407 .chip_info_read_irqsave()
408 .chip()
409 .flags()
410 .contains(IrqChipFlags::IRQCHIP_ONESHOT_SAFE)
411 == false
412 {
413 // 请求中断时 hander = NULL,因此我们为其使用默认的主处理程序。
414 // 但它没有设置ONESHOT标志。与电平触发中断结合时,
415 // 这是致命的,因为默认的主处理程序只是唤醒线程,然后重新启用 irq 线路,
416 // 但设备仍然保持电平中断生效。周而复始....
417 // 虽然这对于边缘类型中断来说可行,但我们为了安全起见,不加条件地拒绝,
418 // 因为我们不能确定这个中断实际上具有什么类型。
419 // 由于底层芯片实现可能会覆盖它们,所以类型标志并不可靠.
420
421 kerror!(
422 "Requesting irq {} without a handler, and ONESHOT flags not set for irqaction: {}",
423 irq.data(),
424 action_guard.name()
425 );
426 return Err(err_out_unlock(
427 SystemError::EINVAL,
428 desc_inner_guard,
429 desc.clone(),
430 req_mutex_guard,
431 action_guard,
432 ));
433 }
434
435 // 第一次在当前irqdesc上注册中断处理函数
436 if !irq_shared {
437 // 设置中断触发方式
438 if action_guard.flags().trigger_type_specified() {
439 let trigger_type = action_guard.flags().trigger_type();
440 if let Err(e) =
441 self.do_set_irq_trigger(desc.clone(), &mut desc_inner_guard, trigger_type)
442 {
443 return Err(err_out_unlock(
444 e,
445 desc_inner_guard,
446 desc.clone(),
447 req_mutex_guard,
448 action_guard,
449 ));
450 }
451 }
452
453 // 激活中断。这种激活必须独立于IRQ_NOAUTOEN进行*desc_inner_guard.internal_state_mut() |= IrqDescState::IRQS_NOREQUEST;uest.
454 if let Err(e) = self.irq_activate(&desc, &mut desc_inner_guard) {
455 return Err(err_out_unlock(
456 e,
457 desc_inner_guard,
458 desc.clone(),
459 req_mutex_guard,
460 action_guard,
461 ));
462 }
463
464 *desc_inner_guard.internal_state_mut() &= !(IrqDescState::IRQS_AUTODETECT
465 | IrqDescState::IRQS_SPURIOUS_DISABLED
466 | IrqDescState::IRQS_ONESHOT
467 | IrqDescState::IRQS_WAITING);
468 desc_inner_guard
469 .common_data()
470 .clear_status(IrqStatus::IRQD_IRQ_INPROGRESS);
471
472 if action_guard.flags().contains(IrqHandleFlags::IRQF_PERCPU) {
473 desc_inner_guard
474 .common_data()
475 .insert_status(IrqStatus::IRQD_PER_CPU);
476 desc_inner_guard.line_status_set_per_cpu();
477
478 if action_guard.flags().contains(IrqHandleFlags::IRQF_NO_DEBUG) {
479 desc_inner_guard.line_status_set_no_debug();
480 }
481 }
482
483 if action_guard.flags().contains(IrqHandleFlags::IRQF_ONESHOT) {
484 *desc_inner_guard.internal_state_mut() |= IrqDescState::IRQS_ONESHOT;
485 }
486
487 // 如果有要求的话,则忽略IRQ的均衡。
488 if action_guard
489 .flags()
490 .contains(IrqHandleFlags::IRQF_NOBALANCING)
491 {
492 todo!("IRQF_NO_BALANCING");
493 }
494
495 if !action_guard
496 .flags()
497 .contains(IrqHandleFlags::IRQF_NO_AUTOEN)
498 && desc_inner_guard.can_autoenable()
499 {
500 // 如果没有设置IRQF_NOAUTOEN,则自动使能中断
501 self.irq_startup(
502 &desc,
503 &mut desc_inner_guard,
504 Self::IRQ_RESEND,
505 Self::IRQ_START_COND,
506 )
507 .ok();
508 } else {
509 // 共享中断与禁用自动使能不太兼容。
510 // 共享中断可能在它仍然被禁用时请求它,然后永远等待中断。
511
512 static mut WARNED: bool = false;
513 if action_guard.flags().contains(IrqHandleFlags::IRQF_SHARED) {
514 if unsafe { !WARNED } {
515 kwarn!(
516 "Shared interrupt {} for {} requested but not auto enabled",
517 irq.data(),
518 action_guard.name()
519 );
520 unsafe { WARNED = true };
521 }
522 }
523
524 desc_inner_guard.set_depth(1);
525 }
526 } else if action_guard.flags().trigger_type_specified() {
527 let new_trigger_type = action_guard.flags().trigger_type();
528 let old_trigger_type = desc_inner_guard.common_data().trigger_type();
529 if new_trigger_type != old_trigger_type {
530 kwarn!("Irq {} uses trigger type: {old_trigger_type:?}, but requested trigger type: {new_trigger_type:?}.", irq.data());
531 }
532 }
533
534 // 在队列末尾添加新的irqaction
535 desc_inner_guard.add_action(action.clone());
536
537 // 检查我们是否曾经通过虚构的中断处理程序禁用过irq。重新启用它并再给它一次机会。
538 if irq_shared
539 && desc_inner_guard
540 .internal_state()
541 .contains(IrqDescState::IRQS_SPURIOUS_DISABLED)
542 {
543 desc_inner_guard
544 .internal_state_mut()
545 .remove(IrqDescState::IRQS_SPURIOUS_DISABLED);
546 self.do_enable_irq(desc.clone(), &mut desc_inner_guard).ok();
547 }
548
549 drop(desc_inner_guard);
550 desc.chip_bus_sync_unlock();
551 drop(req_mutex_guard);
552
553 drop(action_guard);
554 self.wake_up_and_wait_for_irq_thread_ready(&desc, Some(action.clone()));
555 self.wake_up_and_wait_for_irq_thread_ready(&desc, action.inner().secondary());
556 return Ok(());
557 }
558
559 /// 唤醒中断线程并等待中断线程准备好
560 ///
561 /// ## 参数
562 ///
563 /// - desc: 中断描述符
564 /// - action: 要唤醒的中断处理函数
565 ///
566 /// ## 锁
567 ///
568 /// 进入当前函数时,`action`的锁需要被释放
wake_up_and_wait_for_irq_thread_ready( &self, desc: &Arc<IrqDesc>, action: Option<Arc<IrqAction>>, )569 fn wake_up_and_wait_for_irq_thread_ready(
570 &self,
571 desc: &Arc<IrqDesc>,
572 action: Option<Arc<IrqAction>>,
573 ) {
574 if action.is_none() {
575 return;
576 }
577
578 let action = action.unwrap();
579
580 let action_guard = action.inner();
581 if action_guard.thread().is_none() {
582 return;
583 }
584
585 ProcessManager::wakeup(&action_guard.thread().unwrap()).ok();
586 drop(action_guard);
587 action
588 .thread_completion()
589 .wait_for_completion()
590 .map_err(|e| {
591 kwarn!(
592 "Failed to wait for irq thread ready for {} (irq {:?}), error {:?}",
593 action.inner().name(),
594 desc.irq_data().irq(),
595 e
596 );
597 })
598 .ok();
599 }
600
irq_activate_and_startup( &self, desc: &Arc<IrqDesc>, desc_inner_guard: &mut SpinLockGuard<'_, InnerIrqDesc>, resend: bool, ) -> Result<(), SystemError>601 pub(super) fn irq_activate_and_startup(
602 &self,
603 desc: &Arc<IrqDesc>,
604 desc_inner_guard: &mut SpinLockGuard<'_, InnerIrqDesc>,
605 resend: bool,
606 ) -> Result<(), SystemError> {
607 self.irq_activate(desc, desc_inner_guard)?;
608 self.irq_startup(desc, desc_inner_guard, resend, Self::IRQ_START_FORCE)
609 }
610
irq_activate( &self, _desc: &Arc<IrqDesc>, desc_inner_guard: &mut SpinLockGuard<'_, InnerIrqDesc>, ) -> Result<(), SystemError>611 pub(super) fn irq_activate(
612 &self,
613 _desc: &Arc<IrqDesc>,
614 desc_inner_guard: &mut SpinLockGuard<'_, InnerIrqDesc>,
615 ) -> Result<(), SystemError> {
616 let irq_data = desc_inner_guard.irq_data();
617
618 if !desc_inner_guard.common_data().status().affinity_managed() {
619 return irq_domain_manager().activate_irq(irq_data, false);
620 }
621
622 return Ok(());
623 }
624
625 /// 设置CPU亲和性并开启中断
irq_startup( &self, desc: &Arc<IrqDesc>, desc_inner_guard: &mut SpinLockGuard<'_, InnerIrqDesc>, resend: bool, force: bool, ) -> Result<(), SystemError>626 pub(super) fn irq_startup(
627 &self,
628 desc: &Arc<IrqDesc>,
629 desc_inner_guard: &mut SpinLockGuard<'_, InnerIrqDesc>,
630 resend: bool,
631 force: bool,
632 ) -> Result<(), SystemError> {
633 let mut ret = Ok(());
634 let irq_data = desc_inner_guard.irq_data().clone();
635 let affinity = desc_inner_guard.common_data().affinity();
636 desc_inner_guard.set_depth(0);
637
638 if desc_inner_guard.common_data().status().started() {
639 self.irq_enable(desc_inner_guard);
640 } else {
641 match self.__irq_startup_managed(desc_inner_guard, &affinity, force) {
642 IrqStartupResult::Normal => {
643 if irq_data
644 .chip_info_read_irqsave()
645 .chip()
646 .flags()
647 .contains(IrqChipFlags::IRQCHIP_AFFINITY_PRE_STARTUP)
648 {
649 self.irq_setup_affinity(desc, desc_inner_guard).ok();
650 }
651
652 ret = self.__irq_startup(desc_inner_guard);
653
654 if !irq_data
655 .chip_info_read_irqsave()
656 .chip()
657 .flags()
658 .contains(IrqChipFlags::IRQCHIP_AFFINITY_PRE_STARTUP)
659 {
660 self.irq_setup_affinity(desc, desc_inner_guard).ok();
661 }
662 }
663 IrqStartupResult::Managed => {
664 self.irq_do_set_affinity(&irq_data, &desc_inner_guard, &affinity, false)
665 .ok();
666 ret = self.__irq_startup(desc_inner_guard);
667 }
668 IrqStartupResult::Abort => {
669 desc_inner_guard
670 .common_data()
671 .insert_status(IrqStatus::IRQD_MANAGED_SHUTDOWN);
672 return Ok(());
673 }
674 }
675 }
676
677 if resend {
678 if let Err(e) = self.irq_check_and_resend(desc_inner_guard, false) {
679 kerror!(
680 "Failed to check and resend irq {}, error {:?}",
681 irq_data.irq().data(),
682 e
683 );
684 }
685 }
686
687 return ret;
688 }
689
irq_enable(&self, desc_inner_guard: &SpinLockGuard<'_, InnerIrqDesc>)690 pub fn irq_enable(&self, desc_inner_guard: &SpinLockGuard<'_, InnerIrqDesc>) {
691 let common_data = desc_inner_guard.common_data();
692 if !common_data.status().disabled() {
693 self.unmask_irq(desc_inner_guard);
694 } else {
695 common_data.clear_disabled();
696
697 let chip = desc_inner_guard.irq_data().chip_info_read_irqsave().chip();
698
699 if let Err(e) = chip.irq_enable(&desc_inner_guard.irq_data()) {
700 if e == SystemError::ENOSYS {
701 self.unmask_irq(desc_inner_guard);
702 }
703 kerror!(
704 "Failed to enable irq {} (name: {:?}), error {:?}",
705 desc_inner_guard.irq_data().irq().data(),
706 desc_inner_guard.name(),
707 e
708 );
709 } else {
710 common_data.clear_masked();
711 }
712 }
713 }
714
715 /// 自动设置中断的CPU亲和性
716 ///
717 ///
718 /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/kernel/irq/manage.c#589
irq_setup_affinity( &self, _desc: &Arc<IrqDesc>, desc_inner_guard: &mut SpinLockGuard<'_, InnerIrqDesc>, ) -> Result<(), SystemError>719 pub fn irq_setup_affinity(
720 &self,
721 _desc: &Arc<IrqDesc>,
722 desc_inner_guard: &mut SpinLockGuard<'_, InnerIrqDesc>,
723 ) -> Result<(), SystemError> {
724 let common_data = desc_inner_guard.common_data();
725 if !desc_inner_guard.can_set_affinity() {
726 return Ok(());
727 }
728
729 let mut to_set = IRQ_DEFAULT_AFFINITY.clone();
730 if common_data.status().affinity_managed()
731 || common_data.status().contains(IrqStatus::IRQD_AFFINITY_SET)
732 {
733 // FIXME: 要判断affinity跟已上线的CPU是否有交集
734
735 let irq_aff = common_data.affinity();
736 if irq_aff.is_empty() {
737 common_data.clear_status(IrqStatus::IRQD_AFFINITY_SET);
738 } else {
739 to_set = irq_aff;
740 }
741 }
742
743 // FIXME: 求to_set和在线CPU的交集
744
745 return self.irq_do_set_affinity(
746 desc_inner_guard.irq_data(),
747 &desc_inner_guard,
748 &to_set,
749 false,
750 );
751 }
752
irq_do_set_affinity( &self, irq_data: &Arc<IrqData>, desc_inner_guard: &SpinLockGuard<'_, InnerIrqDesc>, cpumask: &CpuMask, force: bool, ) -> Result<(), SystemError>753 pub fn irq_do_set_affinity(
754 &self,
755 irq_data: &Arc<IrqData>,
756 desc_inner_guard: &SpinLockGuard<'_, InnerIrqDesc>,
757 cpumask: &CpuMask,
758 force: bool,
759 ) -> Result<(), SystemError> {
760 let chip = irq_data.chip_info_read_irqsave().chip();
761 if !chip.can_set_affinity() {
762 return Err(SystemError::EINVAL);
763 }
764
765 // todo: 处理CPU中断隔离相关的逻辑
766
767 let common_data = desc_inner_guard.common_data();
768 let r;
769 if !force && !cpumask.is_empty() {
770 r = chip.irq_set_affinity(irq_data, &cpumask, force);
771 } else if force {
772 r = chip.irq_set_affinity(irq_data, &cpumask, force);
773 } else {
774 return Err(SystemError::EINVAL);
775 }
776
777 let mut ret = Ok(());
778 if let Ok(rs) = r {
779 match rs {
780 IrqChipSetMaskResult::SetMaskOk | IrqChipSetMaskResult::SetMaskOkDone => {
781 common_data.set_affinity(cpumask.clone());
782 }
783 IrqChipSetMaskResult::SetMaskOkNoChange => {
784
785 // irq_validate_effective_affinity(data);
786 // irq_set_thread_affinity(desc);
787 }
788 }
789 } else {
790 ret = Err(r.unwrap_err());
791 }
792
793 return ret;
794 }
795
__irq_startup( &self, desc_inner_guard: &SpinLockGuard<'_, InnerIrqDesc>, ) -> Result<(), SystemError>796 fn __irq_startup(
797 &self,
798 desc_inner_guard: &SpinLockGuard<'_, InnerIrqDesc>,
799 ) -> Result<(), SystemError> {
800 let common_data = desc_inner_guard.common_data();
801
802 if let Err(e) = desc_inner_guard
803 .irq_data()
804 .chip_info_read_irqsave()
805 .chip()
806 .irq_startup(desc_inner_guard.irq_data())
807 {
808 if e == SystemError::ENOSYS {
809 self.irq_enable(desc_inner_guard);
810 } else {
811 return Err(e);
812 }
813 } else {
814 common_data.clear_disabled();
815 common_data.clear_masked();
816 }
817
818 common_data.set_started();
819
820 return Ok(());
821 }
822
__irq_startup_managed( &self, desc_inner_guard: &SpinLockGuard<'_, InnerIrqDesc>, _affinity: &CpuMask, _force: bool, ) -> IrqStartupResult823 fn __irq_startup_managed(
824 &self,
825 desc_inner_guard: &SpinLockGuard<'_, InnerIrqDesc>,
826 _affinity: &CpuMask,
827 _force: bool,
828 ) -> IrqStartupResult {
829 let irq_data = desc_inner_guard.irq_data();
830 let common_data = desc_inner_guard.common_data();
831
832 if !common_data.status().affinity_managed() {
833 return IrqStartupResult::Normal;
834 }
835
836 common_data.clear_managed_shutdown();
837
838 /*
839 - 检查Affinity掩码是否包括所有的在线CPU。如果是,这意味着有代码试图在管理的中断上使用enable_irq(),
840 这可能是非法的。在这种情况下,如果force不是真值,函数会返回IRQ_STARTUP_ABORT,表示中断处理应该被放弃。
841 - 如果Affinity掩码中没有任何在线的CPU,那么中断请求是不可用的,因为没有任何CPU可以处理它。
842 在这种情况下,如果force不是真值,函数同样会返回IRQ_STARTUP_ABORT。
843 - 如果以上条件都不满足,尝试激活中断,并将其设置为管理模式。这是通过调用 `irq_domain_manager().activate_irq()` 函数来实现的。
844 如果这个调用失败,表示有保留的资源无法访问,函数会返回IRQ_STARTUP_ABORT。
845 - 如果一切顺利,函数会返回IRQ_STARTUP_MANAGED,表示中断已经被成功管理并激活。
846 */
847
848 // if (cpumask_any_and(aff, cpu_online_mask) >= nr_cpu_ids) {
849 // /*
850 // * Catch code which fiddles with enable_irq() on a managed
851 // * and potentially shutdown IRQ. Chained interrupt
852 // * installment or irq auto probing should not happen on
853 // * managed irqs either.
854 // */
855 // if (WARN_ON_ONCE(force))
856 // return IRQ_STARTUP_ABORT;
857 // /*
858 // * The interrupt was requested, but there is no online CPU
859 // * in it's affinity mask. Put it into managed shutdown
860 // * state and let the cpu hotplug mechanism start it up once
861 // * a CPU in the mask becomes available.
862 // */
863 // return IRQ_STARTUP_ABORT;
864 // }
865
866 let r = irq_domain_manager().activate_irq(irq_data, false);
867 if r.is_err() {
868 return IrqStartupResult::Abort;
869 }
870
871 return IrqStartupResult::Managed;
872 }
873
do_enable_irq( &self, _desc: Arc<IrqDesc>, _desc_inner_guard: &mut SpinLockGuard<'_, InnerIrqDesc>, ) -> Result<(), SystemError>874 pub fn do_enable_irq(
875 &self,
876 _desc: Arc<IrqDesc>,
877 _desc_inner_guard: &mut SpinLockGuard<'_, InnerIrqDesc>,
878 ) -> Result<(), SystemError> {
879 // https://code.dragonos.org.cn/xref/linux-6.1.9/kernel/irq/manage.c?r=&mo=59252&fi=2138#776
880 todo!("do_enable_irq")
881 }
882
883 #[inline(never)]
do_set_irq_trigger( &self, _desc: Arc<IrqDesc>, desc_inner_guard: &mut SpinLockGuard<'_, InnerIrqDesc>, mut trigger_type: IrqLineStatus, ) -> Result<(), SystemError>884 pub fn do_set_irq_trigger(
885 &self,
886 _desc: Arc<IrqDesc>,
887 desc_inner_guard: &mut SpinLockGuard<'_, InnerIrqDesc>,
888 mut trigger_type: IrqLineStatus,
889 ) -> Result<(), SystemError> {
890 let chip = desc_inner_guard.irq_data().chip_info_read_irqsave().chip();
891 let mut to_unmask = false;
892
893 if !chip.can_set_flow_type() {
894 // kdebug!(
895 // "No set_irq_type function for irq {}, chip {}",
896 // desc_inner_guard.irq_data().irq().data(),
897 // chip.name()
898 // );
899 return Ok(());
900 }
901
902 if chip.flags().contains(IrqChipFlags::IRQCHIP_SET_TYPE_MASKED) {
903 if desc_inner_guard.common_data().status().masked() == false {
904 self.mask_irq(desc_inner_guard.irq_data());
905 }
906 if desc_inner_guard.common_data().status().disabled() == false {
907 to_unmask = true;
908 }
909 }
910
911 trigger_type &= IrqLineStatus::IRQ_TYPE_SENSE_MASK;
912
913 let r = chip.irq_set_type(desc_inner_guard.irq_data(), trigger_type);
914 let ret;
915 if let Ok(rs) = r {
916 match rs {
917 IrqChipSetMaskResult::SetMaskOk | IrqChipSetMaskResult::SetMaskOkDone => {
918 let common_data = desc_inner_guard.common_data();
919 common_data.clear_status(IrqStatus::IRQD_TRIGGER_MASK);
920 let mut irqstatus = IrqStatus::empty();
921 irqstatus.set_trigger_type(trigger_type);
922 common_data.insert_status(irqstatus);
923 }
924 IrqChipSetMaskResult::SetMaskOkNoChange => {
925 let flags = desc_inner_guard.common_data().trigger_type();
926 desc_inner_guard.set_trigger_type(flags);
927 desc_inner_guard
928 .common_data()
929 .clear_status(IrqStatus::IRQD_LEVEL);
930 desc_inner_guard.clear_level();
931
932 if (flags & IrqLineStatus::IRQ_TYPE_LEVEL_MASK).is_empty() == false {
933 desc_inner_guard.set_level();
934 desc_inner_guard
935 .common_data()
936 .insert_status(IrqStatus::IRQD_LEVEL);
937 }
938 }
939 }
940
941 ret = Ok(());
942 } else {
943 kerror!(
944 "Failed to set irq {} trigger type to {:?} on irqchip {}, error {:?}",
945 desc_inner_guard.irq_data().irq().data(),
946 trigger_type,
947 chip.name(),
948 r
949 );
950
951 ret = Err(r.unwrap_err());
952 }
953
954 if to_unmask {
955 self.unmask_irq(desc_inner_guard);
956 }
957 return ret;
958 }
959
irq_request_resources(&self, desc: Arc<IrqDesc>) -> Result<(), SystemError>960 fn irq_request_resources(&self, desc: Arc<IrqDesc>) -> Result<(), SystemError> {
961 let irq_data = desc.irq_data();
962 let irq_chip = irq_data.chip_info_read_irqsave().chip();
963 irq_chip.irq_request_resources(&irq_data)
964 }
965
966 /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/kernel/irq/manage.c?r=&mo=59252&fi=2138#1448
setup_irq_thread( &self, _irq: IrqNumber, _action: &InnerIrqAction, _secondary: bool, ) -> Result<(), SystemError>967 fn setup_irq_thread(
968 &self,
969 _irq: IrqNumber,
970 _action: &InnerIrqAction,
971 _secondary: bool,
972 ) -> Result<(), SystemError> {
973 // if secondary {
974 // KernelThreadMechanism::create(func, name)
975 // }
976
977 todo!("setup_irq_thread")
978 }
979
setup_forced_threading(&self, _action: &mut InnerIrqAction) -> Result<(), SystemError>980 fn setup_forced_threading(&self, _action: &mut InnerIrqAction) -> Result<(), SystemError> {
981 // todo: 处理强制线程化的逻辑,参考linux的`irq_setup_forced_threading()`
982 return Ok(());
983 }
984
irq_clear_status_flags( &self, irq: IrqNumber, status: IrqLineStatus, ) -> Result<(), SystemError>985 pub fn irq_clear_status_flags(
986 &self,
987 irq: IrqNumber,
988 status: IrqLineStatus,
989 ) -> Result<(), SystemError> {
990 let desc = irq_desc_manager().lookup(irq).ok_or(SystemError::EINVAL)?;
991 desc.modify_status(status, IrqLineStatus::empty());
992 return Ok(());
993 }
994
995 /// 屏蔽中断
mask_irq(&self, irq_data: &Arc<IrqData>)996 pub(super) fn mask_irq(&self, irq_data: &Arc<IrqData>) {
997 if irq_data.common_data().status().masked() {
998 return;
999 }
1000
1001 let chip = irq_data.chip_info_read_irqsave().chip();
1002 let r = chip.irq_mask(irq_data);
1003
1004 if r.is_ok() {
1005 irq_data.common_data().set_masked();
1006 }
1007 }
1008
1009 /// 解除屏蔽中断
unmask_irq(&self, desc_inner_guard: &SpinLockGuard<'_, InnerIrqDesc>)1010 pub(super) fn unmask_irq(&self, desc_inner_guard: &SpinLockGuard<'_, InnerIrqDesc>) {
1011 if desc_inner_guard.common_data().status().masked() == false {
1012 return;
1013 }
1014
1015 let r = desc_inner_guard
1016 .irq_data()
1017 .chip_info_read_irqsave()
1018 .chip()
1019 .irq_unmask(&desc_inner_guard.irq_data());
1020
1021 if let Err(e) = r {
1022 if e != SystemError::ENOSYS {
1023 kerror!(
1024 "Failed to unmask irq {} on irqchip {}, error {:?}",
1025 desc_inner_guard.irq_data().irq().data(),
1026 desc_inner_guard
1027 .irq_data()
1028 .chip_info_read_irqsave()
1029 .chip()
1030 .name(),
1031 e
1032 );
1033 }
1034 } else {
1035 desc_inner_guard
1036 .common_data()
1037 .clear_status(IrqStatus::IRQD_IRQ_MASKED);
1038 }
1039 }
1040
1041 /// 释放使用request_irq分配的中断
1042 ///
1043 /// ## 参数
1044 ///
1045 /// - irq: 要释放的中断线
1046 /// - dev_id: 要释放的设备身份
1047 ///
1048 /// ## 返回
1049 ///
1050 /// 返回传递给request_irq的devname参数
1051 ///
1052 /// ## 说明
1053 ///
1054 /// 移除一个中断处理程序。处理程序被移除,如果该中断线不再被任何驱动程序使用,则会被禁用。
1055 ///
1056 /// 在共享IRQ的情况下,调用者必须确保在调用此功能之前,它在所驱动的卡上禁用了中断。
1057 ///
1058 /// ## 注意
1059 ///
1060 /// 此函数不可以在中断上下文中调用。
free_irq(&self, _irq: IrqNumber, _dev_id: Option<Arc<DeviceId>>)1061 pub fn free_irq(&self, _irq: IrqNumber, _dev_id: Option<Arc<DeviceId>>) {
1062 kwarn!("Unimplemented free_irq");
1063 }
1064 }
1065
1066 enum IrqStartupResult {
1067 Normal,
1068 Managed,
1069 Abort,
1070 }
1071 /// 默认的初级中断处理函数
1072 ///
1073 /// 该处理函数仅仅返回`WakeThread`,即唤醒中断线程
1074 #[derive(Debug)]
1075 struct DefaultPrimaryIrqHandler;
1076
1077 impl IrqHandler for DefaultPrimaryIrqHandler {
handle( &self, _irq: IrqNumber, _static_data: Option<&dyn IrqHandlerData>, _dynamic_data: Option<Arc<dyn IrqHandlerData>>, ) -> Result<IrqReturn, SystemError>1078 fn handle(
1079 &self,
1080 _irq: IrqNumber,
1081 _static_data: Option<&dyn IrqHandlerData>,
1082 _dynamic_data: Option<Arc<dyn IrqHandlerData>>,
1083 ) -> Result<IrqReturn, SystemError> {
1084 return Ok(IrqReturn::WakeThread);
1085 }
1086 }
1087
1088 /// Primary handler for nested threaded interrupts.
1089 /// Should never be called.
1090 #[derive(Debug)]
1091 struct IrqNestedPrimaryHandler;
1092
1093 impl IrqHandler for IrqNestedPrimaryHandler {
handle( &self, irq: IrqNumber, _static_data: Option<&dyn IrqHandlerData>, _dynamic_data: Option<Arc<dyn IrqHandlerData>>, ) -> Result<IrqReturn, SystemError>1094 fn handle(
1095 &self,
1096 irq: IrqNumber,
1097 _static_data: Option<&dyn IrqHandlerData>,
1098 _dynamic_data: Option<Arc<dyn IrqHandlerData>>,
1099 ) -> Result<IrqReturn, SystemError> {
1100 kwarn!("Primary handler called for nested irq {}", irq.data());
1101 return Ok(IrqReturn::NotHandled);
1102 }
1103 }
1104