ostd/bus/pci/capability/msix.rs
1// SPDX-License-Identifier: MPL-2.0
2//! MSI-X capability support.
3#![expect(dead_code)]
4#![expect(unused_variables)]
5
6use vstd::prelude::*;
7
8/*
9use alloc::{sync::Arc, vec::Vec};
10
11use crate::{
12 arch::pci::{MSIX_DEFAULT_MSG_ADDR, construct_remappable_msix_address},
13 bus::pci::{
14 cfg_space::{Bar, Command, MemoryBar},
15 common_device::PciCommonDevice,
16 device_info::PciDeviceLocation,
17 },
18 mm::VmIoOnce,
19 trap::irq::IrqLine,
20};
21
22/// MSI-X capability. It will set the BAR space it uses to be hidden.
23#[derive(Debug)]
24#[repr(C)]
25pub struct CapabilityMsixData {
26 loc: PciDeviceLocation,
27 ptr: u16,
28 table_size: u16,
29 /// MSIX table entry content:
30 /// | Vector Control: u32 | Msg Data: u32 | Msg Upper Addr: u32 | Msg Addr: u32 |
31 table_bar: Arc<MemoryBar>,
32 /// Pending bits table.
33 pending_table_bar: Arc<MemoryBar>,
34 table_offset: usize,
35 pending_table_offset: usize,
36 irqs: Vec<Option<IrqLine>>,
37}
38
39impl Clone for CapabilityMsixData {
40 fn clone(&self) -> Self {
41 let new_vec = self.irqs.clone().to_vec();
42 Self {
43 loc: self.loc,
44 ptr: self.ptr,
45 table_size: self.table_size,
46 table_bar: self.table_bar.clone(),
47 pending_table_bar: self.pending_table_bar.clone(),
48 irqs: new_vec,
49 table_offset: self.table_offset,
50 pending_table_offset: self.pending_table_offset,
51 }
52 }
53}
54
55impl CapabilityMsixData {
56 pub(super) fn new(dev: &mut PciCommonDevice, cap_ptr: u16) -> Self {
57 // Get Table and PBA offset, provide functions to modify them
58 let table_info = dev.location().read32(cap_ptr + 4);
59 let pba_info = dev.location().read32(cap_ptr + 8);
60
61 let table_bar;
62 let pba_bar;
63
64 let bar_manager = dev.bar_manager_mut();
65 match bar_manager
66 .bar((pba_info & 0b111) as u8)
67 .clone()
68 .expect("MSIX cfg:pba BAR is none")
69 {
70 Bar::Memory(memory) => {
71 pba_bar = memory;
72 }
73 Bar::Io(_) => {
74 panic!("MSIX cfg:pba BAR is IO type")
75 }
76 };
77 match bar_manager
78 .bar((table_info & 0b111) as u8)
79 .clone()
80 .expect("MSIX cfg:table BAR is none")
81 {
82 Bar::Memory(memory) => {
83 table_bar = memory;
84 }
85 Bar::Io(_) => {
86 panic!("MSIX cfg:table BAR is IO type")
87 }
88 }
89
90 let pba_offset = (pba_info & !(0b111u32)) as usize;
91 let table_offset = (table_info & !(0b111u32)) as usize;
92
93 let table_size = (dev.location().read16(cap_ptr + 2) & 0b11_1111_1111) + 1;
94 // TODO: Different architecture seems to have different, so we should set different address here.
95 let message_address = MSIX_DEFAULT_MSG_ADDR;
96 let message_upper_address = 0u32;
97
98 // Set message address 0xFEE0_0000
99 for i in 0..table_size {
100 // Set message address and disable this msix entry
101 table_bar
102 .io_mem()
103 .write_once((16 * i) as usize + table_offset, &message_address)
104 .unwrap();
105 table_bar
106 .io_mem()
107 .write_once((16 * i + 4) as usize + table_offset, &message_upper_address)
108 .unwrap();
109 table_bar
110 .io_mem()
111 .write_once((16 * i + 12) as usize + table_offset, &1_u32)
112 .unwrap();
113 }
114
115 // enable MSI-X, bit15: MSI-X Enable
116 dev.location()
117 .write16(cap_ptr + 2, dev.location().read16(cap_ptr + 2) | 0x8000);
118 // disable INTx, enable Bus master.
119 dev.set_command(dev.command() | Command::INTERRUPT_DISABLE | Command::BUS_MASTER);
120
121 let mut irqs = Vec::with_capacity(table_size as usize);
122 for i in 0..table_size {
123 irqs.push(None);
124 }
125
126 Self {
127 loc: *dev.location(),
128 ptr: cap_ptr,
129 table_size: (dev.location().read16(cap_ptr + 2) & 0b11_1111_1111) + 1,
130 table_bar,
131 pending_table_bar: pba_bar,
132 irqs,
133 table_offset,
134 pending_table_offset: pba_offset,
135 }
136 }
137
138 /// MSI-X Table size
139 pub fn table_size(&self) -> u16 {
140 // bit 10:0 table size
141 (self.loc.read16(self.ptr + 2) & 0b11_1111_1111) + 1
142 }
143
144 /// Enables an interrupt line, it will replace the old handle with the new handle.
145 pub fn set_interrupt_vector(&mut self, irq: IrqLine, index: u16) {
146 if index >= self.table_size {
147 return;
148 }
149
150 // If interrupt remapping is enabled, then we need to change the value of the message address.
151 if let Some(remapping_index) = irq.remapping_index() {
152 let address = construct_remappable_msix_address(remapping_index as u32);
153
154 self.table_bar
155 .io_mem()
156 .write_once((16 * index) as usize + self.table_offset, &address)
157 .unwrap();
158 self.table_bar
159 .io_mem()
160 .write_once((16 * index + 8) as usize + self.table_offset, &0)
161 .unwrap();
162 } else {
163 self.table_bar
164 .io_mem()
165 .write_once(
166 (16 * index + 8) as usize + self.table_offset,
167 &(irq.num() as u32),
168 )
169 .unwrap();
170 }
171
172 let _old_irq = core::mem::replace(&mut self.irqs[index as usize], Some(irq));
173 // Enable this msix vector
174 self.table_bar
175 .io_mem()
176 .write_once((16 * index + 12) as usize + self.table_offset, &0_u32)
177 .unwrap();
178 }
179
180 /// Gets mutable IrqLine. User can register callbacks by using this function.
181 pub fn irq_mut(&mut self, index: usize) -> Option<&mut IrqLine> {
182 self.irqs[index].as_mut()
183 }
184
185 /// Returns true if MSI-X Enable bit is set.
186 pub fn is_enabled(&self) -> bool {
187 let msg_ctrl = self.loc.read16(self.ptr + 2);
188 msg_ctrl & 0x8000 != 0
189 }
190}
191
192*/
193
194#[verus_verify(dual_spec)]
195#[verus_spec(
196 requires
197 offset < 16,
198 returns
199 set_bit(origin_value, offset, set),
200)]
201fn set_bit(origin_value: u16, offset: usize, set: bool) -> u16 {
202 (origin_value & (!(1 << offset))) | ((set as u16) << offset)
203}