xref: /DragonOS/kernel/src/arch/x86_64/driver/apic/lapic_vector.rs (revision a17651b14b86dd70655090381db4a2f710853aa1)
1 use core::intrinsics::unlikely;
2 
3 use alloc::{string::ToString, sync::Arc};
4 use intertrait::CastFrom;
5 use system_error::SystemError;
6 
7 use crate::{
8     arch::{
9         driver::apic::{
10             apic_timer::{local_apic_timer_irq_desc_init, APIC_TIMER_IRQ_NUM},
11             ioapic::ioapic_init,
12         },
13         interrupt::{
14             entry::arch_setup_interrupt_gate,
15             ipi::{arch_ipi_handler_init, send_ipi, IPI_NUM_FLUSH_TLB, IPI_NUM_KICK_CPU},
16             msi::{X86MsiAddrHi, X86MsiAddrLoNormal, X86MsiDataNormal, X86_MSI_BASE_ADDRESS_LOW},
17         },
18     },
19     driver::open_firmware::device_node::DeviceNode,
20     exception::{
21         ipi::{IpiKind, IpiTarget},
22         irqchip::{IrqChip, IrqChipData, IrqChipFlags},
23         irqdata::IrqData,
24         irqdomain::{irq_domain_manager, IrqDomain, IrqDomainBusToken, IrqDomainOps},
25         msi::MsiMsg,
26         HardwareIrqNumber, IrqNumber,
27     },
28     kwarn,
29     libs::spinlock::{SpinLock, SpinLockGuard},
30     smp::{core::smp_get_processor_id, cpu::ProcessorId},
31 };
32 
33 use super::{hw_irq::HardwareIrqConfig, CurrentApic, LocalAPIC};
34 
35 static mut LOCAL_APIC_CHIP: Option<Arc<LocalApicChip>> = None;
36 
37 pub fn local_apic_chip() -> &'static Arc<LocalApicChip> {
38     unsafe { LOCAL_APIC_CHIP.as_ref().unwrap() }
39 }
40 
41 #[derive(Debug)]
42 pub struct LocalApicChip {
43     inner: SpinLock<InnerIrqChip>,
44 }
45 
46 impl LocalApicChip {
47     pub fn new() -> Self {
48         Self {
49             inner: SpinLock::new(InnerIrqChip {
50                 flags: IrqChipFlags::empty(),
51             }),
52         }
53     }
54 }
55 
56 impl IrqChip for LocalApicChip {
57     fn name(&self) -> &'static str {
58         "APIC"
59     }
60 
61     fn can_set_flow_type(&self) -> bool {
62         false
63     }
64 
65     fn irq_disable(&self, _irq: &Arc<IrqData>) {}
66 
67     fn irq_ack(&self, _irq: &Arc<IrqData>) {
68         CurrentApic.send_eoi();
69     }
70 
71     fn can_set_affinity(&self) -> bool {
72         false
73     }
74 
75     fn can_mask_ack(&self) -> bool {
76         false
77     }
78 
79     fn irq_enable(&self, _irq: &Arc<IrqData>) -> Result<(), SystemError> {
80         // 这里临时处理,后续需要修改
81         return Ok(());
82     }
83 
84     fn irq_unmask(&self, _irq: &Arc<IrqData>) -> Result<(), SystemError> {
85         Ok(())
86     }
87 
88     fn irq_compose_msi_msg(&self, irq: &Arc<IrqData>, msg: &mut MsiMsg) {
89         let chip_data = irq.chip_info_read_irqsave().chip_data().unwrap();
90         let apicd = chip_data.ref_any().downcast_ref::<ApicChipData>().unwrap();
91         let cfg = &apicd.inner().hw_irq_cfg;
92         irq_msi_compose_msg(cfg, msg, false);
93     }
94 
95     fn retrigger(&self, irq: &Arc<IrqData>) -> Result<(), SystemError> {
96         let chip_data = irq
97             .chip_info_read_irqsave()
98             .chip_data()
99             .ok_or(SystemError::EINVAL)?;
100         let apicd = chip_data
101             .ref_any()
102             .downcast_ref::<ApicChipData>()
103             .ok_or(SystemError::EINVAL)?;
104         let inner = apicd.inner();
105 
106         send_ipi(
107             IpiKind::SpecVector(inner.vector),
108             IpiTarget::Specified(inner.cpu),
109         );
110 
111         Ok(())
112     }
113 
114     fn flags(&self) -> IrqChipFlags {
115         self.inner.lock_irqsave().flags
116     }
117 }
118 
119 #[derive(Debug)]
120 struct InnerIrqChip {
121     flags: IrqChipFlags,
122 }
123 
124 #[derive(Debug)]
125 struct ApicChipData {
126     inner: SpinLock<InnerApicChipData>,
127 }
128 
129 impl ApicChipData {
130     #[allow(dead_code)]
131     pub fn new(
132         hw_irq_cfg: HardwareIrqConfig,
133         irq: IrqNumber,
134         vector: HardwareIrqNumber,
135         cpu: ProcessorId,
136     ) -> Self {
137         Self {
138             inner: SpinLock::new(InnerApicChipData {
139                 hw_irq_cfg,
140                 irq,
141                 vector,
142                 prev_vector: None,
143                 cpu,
144                 prev_cpu: None,
145                 status: ApicChipStatus::empty(),
146             }),
147         }
148     }
149 
150     pub fn inner(&self) -> SpinLockGuard<InnerApicChipData> {
151         self.inner.lock_irqsave()
152     }
153 }
154 
155 #[allow(dead_code)]
156 #[derive(Debug)]
157 struct InnerApicChipData {
158     hw_irq_cfg: HardwareIrqConfig,
159     irq: IrqNumber,
160     vector: HardwareIrqNumber,
161     prev_vector: Option<HardwareIrqNumber>,
162     cpu: ProcessorId,
163     prev_cpu: Option<ProcessorId>,
164     status: ApicChipStatus,
165 }
166 
167 impl IrqChipData for ApicChipData {
168     fn as_any_ref(&self) -> &dyn core::any::Any {
169         self
170     }
171 }
172 
173 bitflags! {
174     pub struct ApicChipStatus: u32 {
175         const MOVE_IN_PROGRESS = 1 << 0;
176         const IS_MANAGED = 1 << 1;
177         const CAN_RESERVE = 1 << 2;
178         const HAS_RESERVED = 1 << 3;
179     }
180 }
181 
182 pub(super) fn irq_msi_compose_msg(cfg: &HardwareIrqConfig, msg: &mut MsiMsg, dmar: bool) {
183     *msg = MsiMsg::new_zeroed();
184 
185     let arch_data = X86MsiDataNormal::new()
186         .with_delivery_mode(x86::apic::DeliveryMode::Fixed as u8)
187         .with_vector((cfg.vector.data() & 0xff) as u8);
188     let mut address_lo = X86MsiAddrLoNormal::new()
189         .with_base_address(X86_MSI_BASE_ADDRESS_LOW)
190         .with_dest_mode_logical(false)
191         .with_destid_0_7(cfg.apic_id.data() & 0xff);
192 
193     let mut address_hi = X86MsiAddrHi::new();
194 
195     /*
196      * 只有IOMMU本身可以使用将目标APIC ID放入地址的高位的技术。
197      * 任何其他尝试这样做的东西都只是在写内存,并且需要IR来
198      * 寻址不能在正常的32位地址范围内0xFFExxxxx寻址的APIC。
199      * 这通常是8位,但一些虚拟化程序允许在位5-11使用扩展的目的地ID字段,
200      * 总共支持15位的APIC ID。
201      */
202     if dmar {
203         address_hi.set_destid_8_31(cfg.apic_id.data() >> 8);
204     } else if cfg.apic_id.data() < 0x8000 {
205         // todo: 判断vmx是否支持 extended destination mode
206         // 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/arch/x86/kernel/apic/apic.c?fi=__irq_msi_compose_msg#2580
207         address_lo.set_virt_destid_8_14(cfg.apic_id.data() >> 8);
208     } else if unlikely(cfg.apic_id.data() > 0xff) {
209         kwarn!(
210             "irq_msi_compose_msg: Invalid APIC ID: {}",
211             cfg.apic_id.data()
212         );
213     }
214 
215     msg.address_hi = address_hi.into();
216     msg.address_lo = address_lo.into();
217     msg.data = arch_data.into();
218 }
219 
220 static mut X86_VECTOR_DOMAIN: Option<Arc<IrqDomain>> = None;
221 
222 #[inline(always)]
223 #[allow(dead_code)]
224 pub fn x86_vector_domain() -> &'static Arc<IrqDomain> {
225     unsafe { X86_VECTOR_DOMAIN.as_ref().unwrap() }
226 }
227 
228 #[inline(never)]
229 pub fn arch_early_irq_init() -> Result<(), SystemError> {
230     let vec_domain = irq_domain_manager()
231         .create_and_add(
232             "VECTOR".to_string(),
233             &X86VectorDomainOps,
234             IrqNumber::new(32),
235             HardwareIrqNumber::new(32),
236             223,
237         )
238         .ok_or(SystemError::ENOMEM)?;
239     irq_domain_manager().set_default_domain(vec_domain.clone());
240     unsafe { X86_VECTOR_DOMAIN = Some(vec_domain) };
241 
242     let apic_chip = Arc::new(LocalApicChip::new());
243 
244     unsafe { LOCAL_APIC_CHIP = Some(apic_chip) };
245 
246     // todo: add vector matrix
247     // 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/arch/x86/kernel/apic/vector.c#803
248     kwarn!("arch_early_irq_init: todo: add vector matrix");
249 
250     local_apic_timer_irq_desc_init();
251     arch_ipi_handler_init();
252     CurrentApic.init_current_cpu();
253     if smp_get_processor_id().data() == 0 {
254         unsafe { arch_setup_interrupt_gate() };
255         ioapic_init(&[APIC_TIMER_IRQ_NUM, IPI_NUM_KICK_CPU, IPI_NUM_FLUSH_TLB]);
256     }
257     return Ok(());
258 }
259 
260 /// x86的中断域操作
261 ///
262 /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/arch/x86/kernel/apic/vector.c#693
263 #[derive(Debug)]
264 struct X86VectorDomainOps;
265 
266 impl IrqDomainOps for X86VectorDomainOps {
267     fn match_node(
268         &self,
269         _irq_domain: &Arc<IrqDomain>,
270         _device_node: &Arc<DeviceNode>,
271         _bus_token: IrqDomainBusToken,
272     ) -> bool {
273         todo!()
274     }
275 
276     fn map(
277         &self,
278         _irq_domain: &Arc<IrqDomain>,
279         _hwirq: HardwareIrqNumber,
280         _virq: IrqNumber,
281     ) -> Result<(), SystemError> {
282         Err(SystemError::ENOSYS)
283     }
284 
285     fn unmap(&self, _irq_domain: &Arc<IrqDomain>, _virq: IrqNumber) {
286         todo!()
287     }
288 }
289