pci_irq.rs 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. #![allow(dead_code)]
  2. use core::mem::size_of;
  3. use core::ptr::NonNull;
  4. use alloc::ffi::CString;
  5. use alloc::vec::Vec;
  6. use super::pci::{PciDeviceStructure, PciDeviceStructureGeneralDevice, PciError};
  7. use crate::arch::msi::{ia64_pci_get_arch_msi_message_address, ia64_pci_get_arch_msi_message_data};
  8. use crate::arch::{PciArch, TraitPciArch};
  9. use crate::include::bindings::bindings::{
  10. c_irq_install, c_irq_uninstall, pt_regs, ul, EAGAIN, EINVAL,
  11. };
  12. use crate::libs::volatile::{volread, volwrite, Volatile, VolatileReadable, VolatileWritable};
  13. /// MSIX表的一项
  14. #[repr(C)]
  15. struct MsixEntry {
  16. vector_control: Volatile<u32>,
  17. msg_data: Volatile<u32>,
  18. msg_upper_addr: Volatile<u32>,
  19. msg_addr: Volatile<u32>,
  20. }
  21. /// Pending表的一项
  22. #[repr(C)]
  23. struct PendingEntry {
  24. entry: Volatile<u64>,
  25. }
  26. /// PCI设备中断错误
  27. #[derive(Copy, Clone, Debug, Eq, PartialEq)]
  28. pub enum PciIrqError {
  29. IrqTypeNotSupported,
  30. PciDeviceNotSupportIrq,
  31. IrqTypeUnmatch,
  32. InvalidIrqIndex(u16),
  33. InvalidIrqNum(u16),
  34. IrqNumOccupied(u16),
  35. DeviceIrqOverflow,
  36. MxiIrqNumWrong,
  37. PciBarNotInited,
  38. BarGetVaddrFailed,
  39. MaskNotSupported,
  40. IrqNotInited,
  41. }
  42. /// PCI设备的中断类型
  43. #[derive(Copy, Clone, Debug)]
  44. pub enum IrqType {
  45. Msi {
  46. address_64: bool,
  47. maskable: bool,
  48. irq_max_num: u16,
  49. cap_offset: u8,
  50. },
  51. Msix {
  52. msix_table_bar: u8,
  53. msix_table_offset: u32,
  54. pending_table_bar: u8,
  55. pending_table_offset: u32,
  56. irq_max_num: u16,
  57. cap_offset: u8,
  58. },
  59. Legacy,
  60. Unused,
  61. }
  62. // PCI设备install中断时需要传递的参数
  63. #[derive(Clone, Debug)]
  64. pub struct IrqMsg {
  65. irq_common_message: IrqCommonMsg,
  66. irq_specific_message: IrqSpecificMsg,
  67. }
  68. // PCI设备install中断时需要传递的共同参数
  69. #[derive(Clone, Debug)]
  70. pub struct IrqCommonMsg {
  71. irq_index: u16, //要install的中断号在PCI设备中的irq_vector的index
  72. irq_name: CString, //中断名字
  73. irq_parameter: u16, //中断额外参数,可传入中断处理函数
  74. irq_hander: unsafe extern "C" fn(irq_num: ul, parameter: ul, regs: *mut pt_regs), // 中断处理函数
  75. irq_ack: Option<unsafe extern "C" fn(irq_num: ul)>, // 中断的ack,可为None,若为None则中断处理中会正常通知中断结束,不为None则调用传入的函数进行回复
  76. }
  77. // PCI设备install中断时需要传递的特有参数,Msi代表MSI与MSIX
  78. #[derive(Clone, Debug)]
  79. pub enum IrqSpecificMsg {
  80. Legacy,
  81. Msi {
  82. processor: u16,
  83. trigger_mode: TriggerMode,
  84. },
  85. }
  86. impl IrqSpecificMsg {
  87. fn msi_default() -> Self {
  88. IrqSpecificMsg::Msi {
  89. processor: 0,
  90. trigger_mode: TriggerMode::EdgeTrigger,
  91. }
  92. }
  93. }
  94. // 申请中断的触发模式,MSI默认为边沿触发
  95. #[derive(Copy, Clone, Debug)]
  96. pub enum TriggerMode {
  97. EdgeTrigger,
  98. AssertHigh,
  99. AssertLow,
  100. }
  101. bitflags! {
  102. /// 设备中断类型,使用bitflag使得中断类型的选择更多元化
  103. pub struct IRQ: u8{
  104. const PCI_IRQ_LEGACY = 1 << 0;
  105. const PCI_IRQ_MSI = 1 << 1;
  106. const PCI_IRQ_MSIX = 1 << 2;
  107. const PCI_IRQ_ALL_TYPES=IRQ::PCI_IRQ_LEGACY.bits|IRQ::PCI_IRQ_MSI.bits|IRQ::PCI_IRQ_MSIX.bits;
  108. }
  109. }
  110. /// PciDeviceStructure的子trait,使用继承以直接使用PciDeviceStructure里的接口
  111. pub trait PciInterrupt: PciDeviceStructure {
  112. /// @brief PCI设备调用该函数选择中断类型
  113. /// @param self PCI设备的可变引用
  114. /// @param flag 选择的中断类型(支持多个选择),如PCI_IRQ_ALL_TYPES表示所有中断类型均可,让系统按顺序进行选择
  115. /// @return Option<IrqType> 失败返回None,成功则返回对应中断类型
  116. fn irq_init(&mut self, flag: IRQ) -> Option<IrqType> {
  117. // MSIX中断优先
  118. if flag.contains(IRQ::PCI_IRQ_MSIX) {
  119. if let Some(cap_offset) = self.msix_capability_offset() {
  120. let data =
  121. PciArch::read_config(&self.common_header().bus_device_function, cap_offset + 4);
  122. let irq_max_num = ((data >> 16) & 0x07ff) as u16;
  123. let data =
  124. PciArch::read_config(&self.common_header().bus_device_function, cap_offset + 4);
  125. let msix_table_bar = (data & 0x01) as u8;
  126. let msix_table_offset = data & 0xfffe;
  127. let data =
  128. PciArch::read_config(&self.common_header().bus_device_function, cap_offset + 8);
  129. let pending_table_bar = (data & 0x01) as u8;
  130. let pending_table_offset = data & 0xfffe;
  131. *self.irq_type_mut()? = IrqType::Msix {
  132. msix_table_bar,
  133. msix_table_offset,
  134. pending_table_bar,
  135. pending_table_offset,
  136. irq_max_num,
  137. cap_offset,
  138. };
  139. return Some(IrqType::Msix {
  140. msix_table_bar,
  141. msix_table_offset,
  142. pending_table_bar,
  143. pending_table_offset,
  144. irq_max_num,
  145. cap_offset,
  146. });
  147. }
  148. }
  149. // 其次MSI
  150. if flag.contains(IRQ::PCI_IRQ_MSI) {
  151. if let Some(cap_offset) = self.msi_capability_offset() {
  152. let data =
  153. PciArch::read_config(&self.common_header().bus_device_function, cap_offset);
  154. let message_control = (data >> 16) as u16;
  155. let maskable = (message_control & 0x0100) != 0;
  156. let address_64 = (message_control & 0x0080) != 0;
  157. let irq_max_num = (1 << (((message_control & 0x000e) >> 1) + 1)) as u16;
  158. *self.irq_type_mut()? = IrqType::Msi {
  159. address_64,
  160. maskable,
  161. irq_max_num,
  162. cap_offset,
  163. };
  164. return Some(IrqType::Msi {
  165. address_64,
  166. maskable,
  167. irq_max_num,
  168. cap_offset,
  169. });
  170. }
  171. }
  172. // 最后选择legacy#
  173. if flag.contains(IRQ::PCI_IRQ_LEGACY) {
  174. *self.irq_type_mut()? = IrqType::Legacy;
  175. return Some(IrqType::Legacy);
  176. }
  177. None
  178. }
  179. /// @brief 启动/关闭设备中断
  180. /// @param self PCI设备的可变引用
  181. /// @param enable 开启/关闭
  182. fn irq_enable(&mut self, enable: bool) -> Result<u8, PciError> {
  183. if let Some(irq_type) = self.irq_type_mut() {
  184. match *irq_type {
  185. IrqType::Msix { .. } => {
  186. return self.msix_enable(enable);
  187. }
  188. IrqType::Msi { .. } => {
  189. return self.msi_enable(enable);
  190. }
  191. IrqType::Legacy => {
  192. return Err(PciError::PciIrqError(PciIrqError::IrqTypeNotSupported));
  193. }
  194. IrqType::Unused => {
  195. return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
  196. }
  197. }
  198. }
  199. return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
  200. }
  201. /// @brief 启动/关闭设备MSIX中断
  202. /// @param self PCI设备的可变引用
  203. /// @param enable 开启/关闭
  204. fn msix_enable(&mut self, enable: bool) -> Result<u8, PciError> {
  205. if let Some(irq_type) = self.irq_type_mut() {
  206. match *irq_type {
  207. IrqType::Msix { cap_offset, .. } => {
  208. let mut message =
  209. PciArch::read_config(&self.common_header().bus_device_function, cap_offset);
  210. if enable {
  211. message |= 1 << 31;
  212. } else {
  213. message &= !(1 << 31);
  214. }
  215. PciArch::write_config(
  216. &self.common_header().bus_device_function,
  217. cap_offset,
  218. message,
  219. );
  220. return Ok(0);
  221. }
  222. IrqType::Unused => {
  223. return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
  224. }
  225. _ => {
  226. return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
  227. }
  228. }
  229. }
  230. return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
  231. }
  232. /// @brief 启动/关闭设备MSI中断
  233. /// @param self PCI设备的可变引用
  234. /// @param enable 开启/关闭
  235. fn msi_enable(&mut self, enable: bool) -> Result<u8, PciError> {
  236. if let Some(irq_type) = self.irq_type_mut() {
  237. match *irq_type {
  238. IrqType::Msi { cap_offset, .. } => {
  239. let mut message =
  240. PciArch::read_config(&self.common_header().bus_device_function, cap_offset);
  241. if enable {
  242. message |= 1 << 16;
  243. } else {
  244. message &= !(1 << 16);
  245. }
  246. PciArch::write_config(
  247. &self.common_header().bus_device_function,
  248. cap_offset,
  249. message,
  250. );
  251. return Ok(0);
  252. }
  253. IrqType::Unused => {
  254. return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
  255. }
  256. _ => {
  257. return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
  258. }
  259. }
  260. }
  261. return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
  262. }
  263. /// @brief 获取指定数量的中断号 todo 需要中断重构支持
  264. fn irq_alloc(_num: u16) -> Option<Vec<u16>> {
  265. None
  266. }
  267. /// @brief 进行PCI设备中断的安装
  268. /// @param self PCI设备的可变引用
  269. /// @param msg PCI设备install中断时需要传递的共同参数
  270. /// @return 一切正常返回Ok(0),有错误返回对应错误原因
  271. fn irq_install(&mut self, msg: IrqMsg) -> Result<u8, PciError> {
  272. if let Some(irq_vector) = self.irq_vector_mut() {
  273. if msg.irq_common_message.irq_index as usize > irq_vector.len() {
  274. return Err(PciError::PciIrqError(PciIrqError::InvalidIrqIndex(
  275. msg.irq_common_message.irq_index,
  276. )));
  277. }
  278. }
  279. self.irq_enable(false)?; //中断设置更改前先关闭对应PCI设备的中断
  280. if let Some(irq_type) = self.irq_type_mut() {
  281. match *irq_type {
  282. IrqType::Msix { .. } => {
  283. return self.msix_install(msg);
  284. }
  285. IrqType::Msi { .. } => {
  286. return self.msi_install(msg);
  287. }
  288. IrqType::Unused => {
  289. return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
  290. }
  291. _ => {
  292. return Err(PciError::PciIrqError(PciIrqError::IrqTypeNotSupported));
  293. }
  294. }
  295. }
  296. return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
  297. }
  298. /// @brief 进行PCI设备中断的安装(MSI)
  299. /// @param self PCI设备的可变引用
  300. /// @param msg PCI设备install中断时需要传递的共同参数
  301. /// @return 一切正常返回Ok(0),有错误返回对应错误原因
  302. fn msi_install(&mut self, msg: IrqMsg) -> Result<u8, PciError> {
  303. if let Some(irq_type) = self.irq_type_mut() {
  304. match *irq_type {
  305. IrqType::Msi {
  306. address_64,
  307. irq_max_num,
  308. cap_offset,
  309. ..
  310. } => {
  311. // 注意:MSI中断分配的中断号必须连续且大小为2的倍数
  312. if self.irq_vector_mut().unwrap().len() > irq_max_num as usize {
  313. return Err(PciError::PciIrqError(PciIrqError::DeviceIrqOverflow));
  314. }
  315. let irq_num =
  316. self.irq_vector_mut().unwrap()[msg.irq_common_message.irq_index as usize];
  317. let common_msg = &msg.irq_common_message;
  318. let result = unsafe {
  319. c_irq_install(
  320. irq_num as u64,
  321. Some(common_msg.irq_hander),
  322. common_msg.irq_parameter as u64,
  323. common_msg.irq_name.as_ptr(),
  324. common_msg.irq_ack,
  325. )
  326. };
  327. match result as u32 {
  328. EINVAL => {
  329. return Err(PciError::PciIrqError(PciIrqError::InvalidIrqNum(irq_num)));
  330. }
  331. EAGAIN => {
  332. return Err(PciError::PciIrqError(PciIrqError::IrqNumOccupied(
  333. irq_num,
  334. )));
  335. }
  336. _ => {}
  337. }
  338. //MSI中断只需配置一次PCI寄存器
  339. if common_msg.irq_index == 0 {
  340. let msg_address = ia64_pci_get_arch_msi_message_address(0);
  341. let trigger = match msg.irq_specific_message {
  342. IrqSpecificMsg::Legacy => {
  343. return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
  344. }
  345. IrqSpecificMsg::Msi { trigger_mode, .. } => trigger_mode,
  346. };
  347. let msg_data = ia64_pci_get_arch_msi_message_data(irq_num, 0, trigger);
  348. //写入Message Data和Message Address
  349. if address_64 {
  350. PciArch::write_config(
  351. &self.common_header().bus_device_function,
  352. cap_offset + 4,
  353. msg_address,
  354. );
  355. PciArch::write_config(
  356. &self.common_header().bus_device_function,
  357. cap_offset + 8,
  358. 0,
  359. );
  360. PciArch::write_config(
  361. &self.common_header().bus_device_function,
  362. cap_offset + 12,
  363. msg_data,
  364. );
  365. } else {
  366. PciArch::write_config(
  367. &self.common_header().bus_device_function,
  368. cap_offset + 4,
  369. msg_address,
  370. );
  371. PciArch::write_config(
  372. &self.common_header().bus_device_function,
  373. cap_offset + 8,
  374. msg_data,
  375. );
  376. }
  377. let data = PciArch::read_config(
  378. &self.common_header().bus_device_function,
  379. cap_offset,
  380. );
  381. let message_control = (data >> 16) as u16;
  382. match self.irq_vector_mut().unwrap().len() {
  383. 1 => {
  384. let temp = message_control & (!0x0070);
  385. PciArch::write_config(
  386. &self.common_header().bus_device_function,
  387. cap_offset,
  388. (temp as u32) << 16,
  389. );
  390. }
  391. 2 => {
  392. let temp = message_control & (!0x0070);
  393. PciArch::write_config(
  394. &self.common_header().bus_device_function,
  395. cap_offset,
  396. ((temp | (0x0001 << 4)) as u32) << 16,
  397. );
  398. }
  399. 4 => {
  400. let temp = message_control & (!0x0070);
  401. PciArch::write_config(
  402. &self.common_header().bus_device_function,
  403. cap_offset,
  404. ((temp | (0x0002 << 4)) as u32) << 16,
  405. );
  406. }
  407. 8 => {
  408. let temp = message_control & (!0x0070);
  409. PciArch::write_config(
  410. &self.common_header().bus_device_function,
  411. cap_offset,
  412. ((temp | (0x0003 << 4)) as u32) << 16,
  413. );
  414. }
  415. 16 => {
  416. let temp = message_control & (!0x0070);
  417. PciArch::write_config(
  418. &self.common_header().bus_device_function,
  419. cap_offset,
  420. ((temp | (0x0004 << 4)) as u32) << 16,
  421. );
  422. }
  423. 32 => {
  424. let temp = message_control & (!0x0070);
  425. PciArch::write_config(
  426. &self.common_header().bus_device_function,
  427. cap_offset,
  428. ((temp | (0x0005 << 4)) as u32) << 16,
  429. );
  430. }
  431. _ => {
  432. return Err(PciError::PciIrqError(PciIrqError::MxiIrqNumWrong));
  433. }
  434. }
  435. }
  436. return Ok(0);
  437. }
  438. IrqType::Unused => {
  439. return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
  440. }
  441. _ => {
  442. return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
  443. }
  444. }
  445. }
  446. return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
  447. }
  448. /// @brief 进行PCI设备中断的安装(MSIX)
  449. /// @param self PCI设备的可变引用
  450. /// @param msg PCI设备install中断时需要传递的共同参数
  451. /// @return 一切正常返回Ok(0),有错误返回对应错误原因
  452. fn msix_install(&mut self, msg: IrqMsg) -> Result<u8, PciError> {
  453. if let Some(irq_type) = self.irq_type_mut() {
  454. match *irq_type {
  455. IrqType::Msix {
  456. irq_max_num,
  457. msix_table_bar,
  458. msix_table_offset,
  459. ..
  460. } => {
  461. if self.irq_vector_mut().unwrap().len() > irq_max_num as usize {
  462. return Err(PciError::PciIrqError(PciIrqError::DeviceIrqOverflow));
  463. }
  464. let irq_num =
  465. self.irq_vector_mut().unwrap()[msg.irq_common_message.irq_index as usize];
  466. let common_msg = &msg.irq_common_message;
  467. let result = unsafe {
  468. c_irq_install(
  469. irq_num as u64,
  470. Some(common_msg.irq_hander),
  471. common_msg.irq_parameter as u64,
  472. common_msg.irq_name.as_ptr(),
  473. common_msg.irq_ack,
  474. )
  475. };
  476. match result as u32 {
  477. EINVAL => {
  478. return Err(PciError::PciIrqError(PciIrqError::InvalidIrqNum(irq_num)));
  479. }
  480. EAGAIN => {
  481. return Err(PciError::PciIrqError(PciIrqError::IrqNumOccupied(
  482. irq_num,
  483. )));
  484. }
  485. _ => {}
  486. }
  487. let msg_address = ia64_pci_get_arch_msi_message_address(0);
  488. let trigger = match msg.irq_specific_message {
  489. IrqSpecificMsg::Legacy => {
  490. return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
  491. }
  492. IrqSpecificMsg::Msi { trigger_mode, .. } => trigger_mode,
  493. };
  494. let msg_data = ia64_pci_get_arch_msi_message_data(irq_num, 0, trigger);
  495. //写入Message Data和Message Address
  496. let pcistandardbar = self
  497. .bar()
  498. .ok_or(PciError::PciIrqError(PciIrqError::PciBarNotInited))?;
  499. let msix_bar = pcistandardbar.get_bar(msix_table_bar)?;
  500. let vaddr = msix_bar
  501. .virtual_address()
  502. .ok_or(PciError::PciIrqError(PciIrqError::BarGetVaddrFailed))?
  503. as usize
  504. + msix_table_offset as usize
  505. + msg.irq_common_message.irq_index as usize * size_of::<MsixEntry>();
  506. let msix_entry = NonNull::new(vaddr as *mut MsixEntry).unwrap();
  507. unsafe {
  508. volwrite!(msix_entry, vector_control, 0);
  509. volwrite!(msix_entry, msg_data, msg_data);
  510. volwrite!(msix_entry, msg_upper_addr, 0);
  511. volwrite!(msix_entry, msg_addr, msg_address);
  512. }
  513. return Ok(0);
  514. }
  515. IrqType::Unused => {
  516. return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
  517. }
  518. _ => {
  519. return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
  520. }
  521. }
  522. }
  523. return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
  524. }
  525. /// @brief 进行PCI设备中断的卸载
  526. /// @param self PCI设备的可变引用
  527. fn irq_uninstall(&mut self) -> Result<u8, PciError> {
  528. self.irq_enable(false)?; //中断设置更改前先关闭对应PCI设备的中断
  529. if let Some(irq_type) = self.irq_type_mut() {
  530. match *irq_type {
  531. IrqType::Msix { .. } => {
  532. return self.msix_uninstall();
  533. }
  534. IrqType::Msi { .. } => {
  535. return self.msi_uninstall();
  536. }
  537. IrqType::Unused => {
  538. return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
  539. }
  540. _ => {
  541. return Err(PciError::PciIrqError(PciIrqError::IrqTypeNotSupported));
  542. }
  543. }
  544. }
  545. return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
  546. }
  547. /// @brief 进行PCI设备中断的卸载(MSI)
  548. /// @param self PCI设备的可变引用
  549. fn msi_uninstall(&mut self) -> Result<u8, PciError> {
  550. if let Some(irq_type) = self.irq_type_mut() {
  551. match *irq_type {
  552. IrqType::Msi {
  553. address_64,
  554. cap_offset,
  555. ..
  556. } => {
  557. for vector in self.irq_vector_mut().unwrap() {
  558. unsafe {
  559. c_irq_uninstall(vector.clone() as u64);
  560. }
  561. }
  562. PciArch::write_config(&self.common_header().bus_device_function, cap_offset, 0);
  563. PciArch::write_config(
  564. &self.common_header().bus_device_function,
  565. cap_offset + 4,
  566. 0,
  567. );
  568. PciArch::write_config(
  569. &self.common_header().bus_device_function,
  570. cap_offset + 8,
  571. 0,
  572. );
  573. if address_64 {
  574. PciArch::write_config(
  575. &self.common_header().bus_device_function,
  576. cap_offset + 12,
  577. 0,
  578. );
  579. }
  580. return Ok(0);
  581. }
  582. IrqType::Unused => {
  583. return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
  584. }
  585. _ => {
  586. return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
  587. }
  588. }
  589. }
  590. return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
  591. }
  592. /// @brief 进行PCI设备中断的卸载(MSIX)
  593. /// @param self PCI设备的可变引用
  594. fn msix_uninstall(&mut self) -> Result<u8, PciError> {
  595. if let Some(irq_type) = self.irq_type_mut() {
  596. match *irq_type {
  597. IrqType::Msix {
  598. irq_max_num,
  599. cap_offset,
  600. msix_table_bar,
  601. msix_table_offset,
  602. ..
  603. } => {
  604. for vector in self.irq_vector_mut().unwrap() {
  605. unsafe {
  606. c_irq_uninstall(vector.clone() as u64);
  607. }
  608. }
  609. PciArch::write_config(&self.common_header().bus_device_function, cap_offset, 0);
  610. let pcistandardbar = self
  611. .bar()
  612. .ok_or(PciError::PciIrqError(PciIrqError::PciBarNotInited))
  613. .unwrap();
  614. let msix_bar = pcistandardbar.get_bar(msix_table_bar).unwrap();
  615. for index in 0..irq_max_num {
  616. let vaddr = msix_bar
  617. .virtual_address()
  618. .ok_or(PciError::PciIrqError(PciIrqError::BarGetVaddrFailed))
  619. .unwrap() as usize
  620. + msix_table_offset as usize
  621. + index as usize * size_of::<MsixEntry>();
  622. let msix_entry = NonNull::new(vaddr as *mut MsixEntry).unwrap();
  623. unsafe {
  624. volwrite!(msix_entry, vector_control, 0);
  625. volwrite!(msix_entry, msg_data, 0);
  626. volwrite!(msix_entry, msg_upper_addr, 0);
  627. volwrite!(msix_entry, msg_addr, 0);
  628. }
  629. }
  630. return Ok(0);
  631. }
  632. IrqType::Unused => {
  633. return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
  634. }
  635. _ => {
  636. return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
  637. }
  638. }
  639. }
  640. return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
  641. }
  642. /// @brief 屏蔽相应位置的中断
  643. /// @param self PCI设备的可变引用
  644. /// @param irq_index 中断的位置(在vec中的index和安装的index相同)
  645. fn irq_mask(&mut self, irq_index: u16) -> Result<u8, PciError> {
  646. if let Some(irq_type) = self.irq_type_mut() {
  647. match *irq_type {
  648. IrqType::Msix { .. } => {
  649. return self.msix_mask(irq_index);
  650. }
  651. IrqType::Msi { .. } => {
  652. return self.msi_mask(irq_index);
  653. }
  654. IrqType::Unused => {
  655. return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
  656. }
  657. _ => {
  658. return Err(PciError::PciIrqError(PciIrqError::IrqTypeNotSupported));
  659. }
  660. }
  661. }
  662. return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
  663. }
  664. /// @brief 屏蔽相应位置的中断(MSI)
  665. /// @param self PCI设备的可变引用
  666. /// @param irq_index 中断的位置(在vec中的index和安装的index相同)
  667. fn msi_mask(&mut self, irq_index: u16) -> Result<u8, PciError> {
  668. if let Some(irq_type) = self.irq_type_mut() {
  669. match *irq_type {
  670. IrqType::Msi {
  671. maskable,
  672. address_64,
  673. cap_offset,
  674. irq_max_num,
  675. } => {
  676. if irq_index >= irq_max_num {
  677. return Err(PciError::PciIrqError(PciIrqError::InvalidIrqIndex(
  678. irq_index,
  679. )));
  680. }
  681. if maskable {
  682. match address_64 {
  683. true => {
  684. let mut mask = PciArch::read_config(
  685. &self.common_header().bus_device_function,
  686. cap_offset + 16,
  687. );
  688. mask |= 1 << irq_index;
  689. PciArch::write_config(
  690. &self.common_header().bus_device_function,
  691. cap_offset,
  692. mask,
  693. );
  694. }
  695. false => {
  696. let mut mask = PciArch::read_config(
  697. &self.common_header().bus_device_function,
  698. cap_offset + 12,
  699. );
  700. mask |= 1 << irq_index;
  701. PciArch::write_config(
  702. &self.common_header().bus_device_function,
  703. cap_offset,
  704. mask,
  705. );
  706. }
  707. }
  708. return Ok(0);
  709. }
  710. return Err(PciError::PciIrqError(PciIrqError::MaskNotSupported));
  711. }
  712. IrqType::Unused => {
  713. return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
  714. }
  715. _ => {
  716. return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
  717. }
  718. }
  719. }
  720. return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
  721. }
  722. /// @brief 屏蔽相应位置的中断(MSIX)
  723. /// @param self PCI设备的可变引用
  724. /// @param irq_index 中断的位置(在vec中的index和安装的index相同)
  725. fn msix_mask(&mut self, irq_index: u16) -> Result<u8, PciError> {
  726. if let Some(irq_type) = self.irq_type_mut() {
  727. match *irq_type {
  728. IrqType::Msix {
  729. irq_max_num,
  730. msix_table_bar,
  731. msix_table_offset,
  732. ..
  733. } => {
  734. if irq_index >= irq_max_num {
  735. return Err(PciError::PciIrqError(PciIrqError::InvalidIrqIndex(
  736. irq_index,
  737. )));
  738. }
  739. let pcistandardbar = self
  740. .bar()
  741. .ok_or(PciError::PciIrqError(PciIrqError::PciBarNotInited))
  742. .unwrap();
  743. let msix_bar = pcistandardbar.get_bar(msix_table_bar).unwrap();
  744. let vaddr = msix_bar.virtual_address().unwrap() as usize
  745. + msix_table_offset as usize
  746. + irq_index as usize * size_of::<MsixEntry>();
  747. let msix_entry = NonNull::new(vaddr as *mut MsixEntry).unwrap();
  748. unsafe {
  749. volwrite!(msix_entry, vector_control, 1);
  750. }
  751. return Ok(0);
  752. }
  753. IrqType::Unused => {
  754. return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
  755. }
  756. _ => {
  757. return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
  758. }
  759. }
  760. }
  761. return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
  762. }
  763. /// @brief 解除屏蔽相应位置的中断
  764. /// @param self PCI设备的可变引用
  765. /// @param irq_index 中断的位置(在vec中的index和安装的index相同)
  766. fn irq_unmask(&mut self, irq_index: u16) -> Result<u8, PciError> {
  767. if let Some(irq_type) = self.irq_type_mut() {
  768. match *irq_type {
  769. IrqType::Msix { .. } => {
  770. return self.msix_unmask(irq_index);
  771. }
  772. IrqType::Msi { .. } => {
  773. return self.msi_unmask(irq_index);
  774. }
  775. IrqType::Unused => {
  776. return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
  777. }
  778. _ => {
  779. return Err(PciError::PciIrqError(PciIrqError::IrqTypeNotSupported));
  780. }
  781. }
  782. }
  783. return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
  784. }
  785. /// @brief 解除屏蔽相应位置的中断(MSI)
  786. /// @param self PCI设备的可变引用
  787. /// @param irq_index 中断的位置(在vec中的index和安装的index相同)
  788. fn msi_unmask(&mut self, irq_index: u16) -> Result<u8, PciError> {
  789. if let Some(irq_type) = self.irq_type_mut() {
  790. match *irq_type {
  791. IrqType::Msi {
  792. maskable,
  793. address_64,
  794. cap_offset,
  795. irq_max_num,
  796. } => {
  797. if irq_index >= irq_max_num {
  798. return Err(PciError::PciIrqError(PciIrqError::InvalidIrqIndex(
  799. irq_index,
  800. )));
  801. }
  802. if maskable {
  803. match address_64 {
  804. true => {
  805. let mut mask = PciArch::read_config(
  806. &self.common_header().bus_device_function,
  807. cap_offset + 16,
  808. );
  809. mask &= !(1 << irq_index);
  810. PciArch::write_config(
  811. &self.common_header().bus_device_function,
  812. cap_offset,
  813. mask,
  814. );
  815. }
  816. false => {
  817. let mut mask = PciArch::read_config(
  818. &self.common_header().bus_device_function,
  819. cap_offset + 12,
  820. );
  821. mask &= !(1 << irq_index);
  822. PciArch::write_config(
  823. &self.common_header().bus_device_function,
  824. cap_offset,
  825. mask,
  826. );
  827. }
  828. }
  829. }
  830. return Err(PciError::PciIrqError(PciIrqError::MaskNotSupported));
  831. }
  832. IrqType::Unused => {
  833. return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
  834. }
  835. _ => {
  836. return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
  837. }
  838. }
  839. }
  840. return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
  841. }
  842. /// @brief 解除屏蔽相应位置的中断(MSIX)
  843. /// @param self PCI设备的可变引用
  844. /// @param irq_index 中断的位置(在vec中的index和安装的index相同)
  845. fn msix_unmask(&mut self, irq_index: u16) -> Result<u8, PciError> {
  846. if let Some(irq_type) = self.irq_type_mut() {
  847. match *irq_type {
  848. IrqType::Msix {
  849. irq_max_num,
  850. msix_table_bar,
  851. msix_table_offset,
  852. ..
  853. } => {
  854. if irq_index >= irq_max_num {
  855. return Err(PciError::PciIrqError(PciIrqError::InvalidIrqIndex(
  856. irq_index,
  857. )));
  858. }
  859. let pcistandardbar = self
  860. .bar()
  861. .ok_or(PciError::PciIrqError(PciIrqError::PciBarNotInited))
  862. .unwrap();
  863. let msix_bar = pcistandardbar.get_bar(msix_table_bar).unwrap();
  864. let vaddr = msix_bar.virtual_address().unwrap() as usize
  865. + msix_table_offset as usize
  866. + irq_index as usize * size_of::<MsixEntry>();
  867. let msix_entry = NonNull::new(vaddr as *mut MsixEntry).unwrap();
  868. unsafe {
  869. volwrite!(msix_entry, vector_control, 0);
  870. }
  871. return Ok(0);
  872. }
  873. IrqType::Unused => {
  874. return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
  875. }
  876. _ => {
  877. return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
  878. }
  879. }
  880. }
  881. return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
  882. }
  883. /// @brief 检查被挂起的中断是否在挂起的时候产生了
  884. /// @param self PCI设备的可变引用
  885. /// @param irq_index 中断的位置(在vec中的index和安装的index相同)
  886. /// @return 是否在挂起过程中产生中断(异常情况也返回false)
  887. fn irq_check_pending(&mut self, irq_index: u16) -> Result<bool, PciError> {
  888. if let Some(irq_type) = self.irq_type_mut() {
  889. match *irq_type {
  890. IrqType::Msix { .. } => {
  891. return self.msix_check_pending(irq_index);
  892. }
  893. IrqType::Msi { .. } => {
  894. return self.msi_check_pending(irq_index);
  895. }
  896. IrqType::Unused => {
  897. return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
  898. }
  899. _ => {
  900. return Err(PciError::PciIrqError(PciIrqError::IrqTypeNotSupported));
  901. }
  902. }
  903. }
  904. return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
  905. }
  906. /// @brief 检查被挂起的中断是否在挂起的时候产生了(MSI)
  907. /// @param self PCI设备的可变引用
  908. /// @param irq_index 中断的位置(在vec中的index和安装的index相同)
  909. /// @return 是否在挂起过程中产生中断(异常情况也返回false)
  910. fn msi_check_pending(&mut self, irq_index: u16) -> Result<bool, PciError> {
  911. if let Some(irq_type) = self.irq_type_mut() {
  912. match *irq_type {
  913. IrqType::Msi {
  914. maskable,
  915. address_64,
  916. cap_offset,
  917. irq_max_num,
  918. } => {
  919. if irq_index >= irq_max_num {
  920. return Err(PciError::PciIrqError(PciIrqError::InvalidIrqIndex(
  921. irq_index,
  922. )));
  923. }
  924. if maskable {
  925. match address_64 {
  926. true => {
  927. let mut pend = PciArch::read_config(
  928. &self.common_header().bus_device_function,
  929. cap_offset + 20,
  930. );
  931. pend &= 1 << irq_index;
  932. return Ok(pend != 0);
  933. }
  934. false => {
  935. let mut pend = PciArch::read_config(
  936. &self.common_header().bus_device_function,
  937. cap_offset + 16,
  938. );
  939. pend &= 1 << irq_index;
  940. return Ok(pend != 0);
  941. }
  942. }
  943. }
  944. }
  945. IrqType::Unused => {
  946. return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
  947. }
  948. _ => {
  949. return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
  950. }
  951. }
  952. }
  953. return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
  954. }
  955. /// @brief 检查被挂起的中断是否在挂起的时候产生了(MSIX)
  956. /// @param self PCI设备的可变引用
  957. /// @param irq_index 中断的位置(在vec中的index和安装的index相同)
  958. /// @return 是否在挂起过程中产生中断(异常情况也返回false)
  959. fn msix_check_pending(&mut self, irq_index: u16) -> Result<bool, PciError> {
  960. if let Some(irq_type) = self.irq_type_mut() {
  961. match *irq_type {
  962. IrqType::Msix {
  963. irq_max_num,
  964. pending_table_bar,
  965. pending_table_offset,
  966. ..
  967. } => {
  968. if irq_index >= irq_max_num {
  969. return Err(PciError::PciIrqError(PciIrqError::InvalidIrqIndex(
  970. irq_index,
  971. )));
  972. }
  973. let pcistandardbar = self
  974. .bar()
  975. .ok_or(PciError::PciIrqError(PciIrqError::PciBarNotInited))
  976. .unwrap();
  977. let pending_bar = pcistandardbar.get_bar(pending_table_bar).unwrap();
  978. let vaddr = pending_bar.virtual_address().unwrap() as usize
  979. + pending_table_offset as usize
  980. + (irq_index as usize / 64) * size_of::<PendingEntry>();
  981. let pending_entry = NonNull::new(vaddr as *mut PendingEntry).unwrap();
  982. let pending_entry = unsafe { volread!(pending_entry, entry) };
  983. return Ok(pending_entry & (1 << (irq_index as u64 % 64)) != 0);
  984. }
  985. IrqType::Unused => {
  986. return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
  987. }
  988. _ => {
  989. return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
  990. }
  991. }
  992. }
  993. return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
  994. }
  995. }
  996. /// PCI标准设备的msi/msix中断相关函数块
  997. impl PciInterrupt for PciDeviceStructureGeneralDevice {}