1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003 |
- #![allow(dead_code)]
- use core::mem::size_of;
- use core::ptr::NonNull;
- use alloc::ffi::CString;
- use alloc::vec::Vec;
- use super::pci::{PciDeviceStructure, PciDeviceStructureGeneralDevice, PciError};
- use crate::arch::msi::{ia64_pci_get_arch_msi_message_address, ia64_pci_get_arch_msi_message_data};
- use crate::arch::{PciArch, TraitPciArch};
- use crate::include::bindings::bindings::{
- c_irq_install, c_irq_uninstall, pt_regs, ul, EAGAIN, EINVAL,
- };
- use crate::libs::volatile::{volread, volwrite, Volatile, VolatileReadable, VolatileWritable};
- /// MSIX表的一项
- #[repr(C)]
- struct MsixEntry {
- vector_control: Volatile<u32>,
- msg_data: Volatile<u32>,
- msg_upper_addr: Volatile<u32>,
- msg_addr: Volatile<u32>,
- }
- /// Pending表的一项
- #[repr(C)]
- struct PendingEntry {
- entry: Volatile<u64>,
- }
- /// PCI设备中断错误
- #[derive(Copy, Clone, Debug, Eq, PartialEq)]
- pub enum PciIrqError {
- IrqTypeNotSupported,
- PciDeviceNotSupportIrq,
- IrqTypeUnmatch,
- InvalidIrqIndex(u16),
- InvalidIrqNum(u16),
- IrqNumOccupied(u16),
- DeviceIrqOverflow,
- MxiIrqNumWrong,
- PciBarNotInited,
- BarGetVaddrFailed,
- MaskNotSupported,
- IrqNotInited,
- }
- /// PCI设备的中断类型
- #[derive(Copy, Clone, Debug)]
- pub enum IrqType {
- Msi {
- address_64: bool,
- maskable: bool,
- irq_max_num: u16,
- cap_offset: u8,
- },
- Msix {
- msix_table_bar: u8,
- msix_table_offset: u32,
- pending_table_bar: u8,
- pending_table_offset: u32,
- irq_max_num: u16,
- cap_offset: u8,
- },
- Legacy,
- Unused,
- }
- // PCI设备install中断时需要传递的参数
- #[derive(Clone, Debug)]
- pub struct IrqMsg {
- irq_common_message: IrqCommonMsg,
- irq_specific_message: IrqSpecificMsg,
- }
- // PCI设备install中断时需要传递的共同参数
- #[derive(Clone, Debug)]
- pub struct IrqCommonMsg {
- irq_index: u16, //要install的中断号在PCI设备中的irq_vector的index
- irq_name: CString, //中断名字
- irq_parameter: u16, //中断额外参数,可传入中断处理函数
- irq_hander: unsafe extern "C" fn(irq_num: ul, parameter: ul, regs: *mut pt_regs), // 中断处理函数
- irq_ack: Option<unsafe extern "C" fn(irq_num: ul)>, // 中断的ack,可为None,若为None则中断处理中会正常通知中断结束,不为None则调用传入的函数进行回复
- }
- // PCI设备install中断时需要传递的特有参数,Msi代表MSI与MSIX
- #[derive(Clone, Debug)]
- pub enum IrqSpecificMsg {
- Legacy,
- Msi {
- processor: u16,
- trigger_mode: TriggerMode,
- },
- }
- impl IrqSpecificMsg {
- fn msi_default() -> Self {
- IrqSpecificMsg::Msi {
- processor: 0,
- trigger_mode: TriggerMode::EdgeTrigger,
- }
- }
- }
- // 申请中断的触发模式,MSI默认为边沿触发
- #[derive(Copy, Clone, Debug)]
- pub enum TriggerMode {
- EdgeTrigger,
- AssertHigh,
- AssertLow,
- }
- bitflags! {
- /// 设备中断类型,使用bitflag使得中断类型的选择更多元化
- pub struct IRQ: u8{
- const PCI_IRQ_LEGACY = 1 << 0;
- const PCI_IRQ_MSI = 1 << 1;
- const PCI_IRQ_MSIX = 1 << 2;
- const PCI_IRQ_ALL_TYPES=IRQ::PCI_IRQ_LEGACY.bits|IRQ::PCI_IRQ_MSI.bits|IRQ::PCI_IRQ_MSIX.bits;
- }
- }
- /// PciDeviceStructure的子trait,使用继承以直接使用PciDeviceStructure里的接口
- pub trait PciInterrupt: PciDeviceStructure {
- /// @brief PCI设备调用该函数选择中断类型
- /// @param self PCI设备的可变引用
- /// @param flag 选择的中断类型(支持多个选择),如PCI_IRQ_ALL_TYPES表示所有中断类型均可,让系统按顺序进行选择
- /// @return Option<IrqType> 失败返回None,成功则返回对应中断类型
- fn irq_init(&mut self, flag: IRQ) -> Option<IrqType> {
- // MSIX中断优先
- if flag.contains(IRQ::PCI_IRQ_MSIX) {
- if let Some(cap_offset) = self.msix_capability_offset() {
- let data =
- PciArch::read_config(&self.common_header().bus_device_function, cap_offset + 4);
- let irq_max_num = ((data >> 16) & 0x07ff) as u16;
- let data =
- PciArch::read_config(&self.common_header().bus_device_function, cap_offset + 4);
- let msix_table_bar = (data & 0x01) as u8;
- let msix_table_offset = data & 0xfffe;
- let data =
- PciArch::read_config(&self.common_header().bus_device_function, cap_offset + 8);
- let pending_table_bar = (data & 0x01) as u8;
- let pending_table_offset = data & 0xfffe;
- *self.irq_type_mut()? = IrqType::Msix {
- msix_table_bar,
- msix_table_offset,
- pending_table_bar,
- pending_table_offset,
- irq_max_num,
- cap_offset,
- };
- return Some(IrqType::Msix {
- msix_table_bar,
- msix_table_offset,
- pending_table_bar,
- pending_table_offset,
- irq_max_num,
- cap_offset,
- });
- }
- }
- // 其次MSI
- if flag.contains(IRQ::PCI_IRQ_MSI) {
- if let Some(cap_offset) = self.msi_capability_offset() {
- let data =
- PciArch::read_config(&self.common_header().bus_device_function, cap_offset);
- let message_control = (data >> 16) as u16;
- let maskable = (message_control & 0x0100) != 0;
- let address_64 = (message_control & 0x0080) != 0;
- let irq_max_num = (1 << (((message_control & 0x000e) >> 1) + 1)) as u16;
- *self.irq_type_mut()? = IrqType::Msi {
- address_64,
- maskable,
- irq_max_num,
- cap_offset,
- };
- return Some(IrqType::Msi {
- address_64,
- maskable,
- irq_max_num,
- cap_offset,
- });
- }
- }
- // 最后选择legacy#
- if flag.contains(IRQ::PCI_IRQ_LEGACY) {
- *self.irq_type_mut()? = IrqType::Legacy;
- return Some(IrqType::Legacy);
- }
- None
- }
- /// @brief 启动/关闭设备中断
- /// @param self PCI设备的可变引用
- /// @param enable 开启/关闭
- fn irq_enable(&mut self, enable: bool) -> Result<u8, PciError> {
- if let Some(irq_type) = self.irq_type_mut() {
- match *irq_type {
- IrqType::Msix { .. } => {
- return self.msix_enable(enable);
- }
- IrqType::Msi { .. } => {
- return self.msi_enable(enable);
- }
- IrqType::Legacy => {
- return Err(PciError::PciIrqError(PciIrqError::IrqTypeNotSupported));
- }
- IrqType::Unused => {
- return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
- }
- }
- }
- return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
- }
- /// @brief 启动/关闭设备MSIX中断
- /// @param self PCI设备的可变引用
- /// @param enable 开启/关闭
- fn msix_enable(&mut self, enable: bool) -> Result<u8, PciError> {
- if let Some(irq_type) = self.irq_type_mut() {
- match *irq_type {
- IrqType::Msix { cap_offset, .. } => {
- let mut message =
- PciArch::read_config(&self.common_header().bus_device_function, cap_offset);
- if enable {
- message |= 1 << 31;
- } else {
- message &= !(1 << 31);
- }
- PciArch::write_config(
- &self.common_header().bus_device_function,
- cap_offset,
- message,
- );
- return Ok(0);
- }
- IrqType::Unused => {
- return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
- }
- _ => {
- return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
- }
- }
- }
- return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
- }
- /// @brief 启动/关闭设备MSI中断
- /// @param self PCI设备的可变引用
- /// @param enable 开启/关闭
- fn msi_enable(&mut self, enable: bool) -> Result<u8, PciError> {
- if let Some(irq_type) = self.irq_type_mut() {
- match *irq_type {
- IrqType::Msi { cap_offset, .. } => {
- let mut message =
- PciArch::read_config(&self.common_header().bus_device_function, cap_offset);
- if enable {
- message |= 1 << 16;
- } else {
- message &= !(1 << 16);
- }
- PciArch::write_config(
- &self.common_header().bus_device_function,
- cap_offset,
- message,
- );
- return Ok(0);
- }
- IrqType::Unused => {
- return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
- }
- _ => {
- return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
- }
- }
- }
- return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
- }
- /// @brief 获取指定数量的中断号 todo 需要中断重构支持
- fn irq_alloc(_num: u16) -> Option<Vec<u16>> {
- None
- }
- /// @brief 进行PCI设备中断的安装
- /// @param self PCI设备的可变引用
- /// @param msg PCI设备install中断时需要传递的共同参数
- /// @return 一切正常返回Ok(0),有错误返回对应错误原因
- fn irq_install(&mut self, msg: IrqMsg) -> Result<u8, PciError> {
- if let Some(irq_vector) = self.irq_vector_mut() {
- if msg.irq_common_message.irq_index as usize > irq_vector.len() {
- return Err(PciError::PciIrqError(PciIrqError::InvalidIrqIndex(
- msg.irq_common_message.irq_index,
- )));
- }
- }
- self.irq_enable(false)?; //中断设置更改前先关闭对应PCI设备的中断
- if let Some(irq_type) = self.irq_type_mut() {
- match *irq_type {
- IrqType::Msix { .. } => {
- return self.msix_install(msg);
- }
- IrqType::Msi { .. } => {
- return self.msi_install(msg);
- }
- IrqType::Unused => {
- return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
- }
- _ => {
- return Err(PciError::PciIrqError(PciIrqError::IrqTypeNotSupported));
- }
- }
- }
- return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
- }
- /// @brief 进行PCI设备中断的安装(MSI)
- /// @param self PCI设备的可变引用
- /// @param msg PCI设备install中断时需要传递的共同参数
- /// @return 一切正常返回Ok(0),有错误返回对应错误原因
- fn msi_install(&mut self, msg: IrqMsg) -> Result<u8, PciError> {
- if let Some(irq_type) = self.irq_type_mut() {
- match *irq_type {
- IrqType::Msi {
- address_64,
- irq_max_num,
- cap_offset,
- ..
- } => {
- // 注意:MSI中断分配的中断号必须连续且大小为2的倍数
- if self.irq_vector_mut().unwrap().len() > irq_max_num as usize {
- return Err(PciError::PciIrqError(PciIrqError::DeviceIrqOverflow));
- }
- let irq_num =
- self.irq_vector_mut().unwrap()[msg.irq_common_message.irq_index as usize];
- let common_msg = &msg.irq_common_message;
- let result = unsafe {
- c_irq_install(
- irq_num as u64,
- Some(common_msg.irq_hander),
- common_msg.irq_parameter as u64,
- common_msg.irq_name.as_ptr(),
- common_msg.irq_ack,
- )
- };
- match result as u32 {
- EINVAL => {
- return Err(PciError::PciIrqError(PciIrqError::InvalidIrqNum(irq_num)));
- }
- EAGAIN => {
- return Err(PciError::PciIrqError(PciIrqError::IrqNumOccupied(
- irq_num,
- )));
- }
- _ => {}
- }
- //MSI中断只需配置一次PCI寄存器
- if common_msg.irq_index == 0 {
- let msg_address = ia64_pci_get_arch_msi_message_address(0);
- let trigger = match msg.irq_specific_message {
- IrqSpecificMsg::Legacy => {
- return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
- }
- IrqSpecificMsg::Msi { trigger_mode, .. } => trigger_mode,
- };
- let msg_data = ia64_pci_get_arch_msi_message_data(irq_num, 0, trigger);
- //写入Message Data和Message Address
- if address_64 {
- PciArch::write_config(
- &self.common_header().bus_device_function,
- cap_offset + 4,
- msg_address,
- );
- PciArch::write_config(
- &self.common_header().bus_device_function,
- cap_offset + 8,
- 0,
- );
- PciArch::write_config(
- &self.common_header().bus_device_function,
- cap_offset + 12,
- msg_data,
- );
- } else {
- PciArch::write_config(
- &self.common_header().bus_device_function,
- cap_offset + 4,
- msg_address,
- );
- PciArch::write_config(
- &self.common_header().bus_device_function,
- cap_offset + 8,
- msg_data,
- );
- }
- let data = PciArch::read_config(
- &self.common_header().bus_device_function,
- cap_offset,
- );
- let message_control = (data >> 16) as u16;
- match self.irq_vector_mut().unwrap().len() {
- 1 => {
- let temp = message_control & (!0x0070);
- PciArch::write_config(
- &self.common_header().bus_device_function,
- cap_offset,
- (temp as u32) << 16,
- );
- }
- 2 => {
- let temp = message_control & (!0x0070);
- PciArch::write_config(
- &self.common_header().bus_device_function,
- cap_offset,
- ((temp | (0x0001 << 4)) as u32) << 16,
- );
- }
- 4 => {
- let temp = message_control & (!0x0070);
- PciArch::write_config(
- &self.common_header().bus_device_function,
- cap_offset,
- ((temp | (0x0002 << 4)) as u32) << 16,
- );
- }
- 8 => {
- let temp = message_control & (!0x0070);
- PciArch::write_config(
- &self.common_header().bus_device_function,
- cap_offset,
- ((temp | (0x0003 << 4)) as u32) << 16,
- );
- }
- 16 => {
- let temp = message_control & (!0x0070);
- PciArch::write_config(
- &self.common_header().bus_device_function,
- cap_offset,
- ((temp | (0x0004 << 4)) as u32) << 16,
- );
- }
- 32 => {
- let temp = message_control & (!0x0070);
- PciArch::write_config(
- &self.common_header().bus_device_function,
- cap_offset,
- ((temp | (0x0005 << 4)) as u32) << 16,
- );
- }
- _ => {
- return Err(PciError::PciIrqError(PciIrqError::MxiIrqNumWrong));
- }
- }
- }
- return Ok(0);
- }
- IrqType::Unused => {
- return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
- }
- _ => {
- return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
- }
- }
- }
- return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
- }
- /// @brief 进行PCI设备中断的安装(MSIX)
- /// @param self PCI设备的可变引用
- /// @param msg PCI设备install中断时需要传递的共同参数
- /// @return 一切正常返回Ok(0),有错误返回对应错误原因
- fn msix_install(&mut self, msg: IrqMsg) -> Result<u8, PciError> {
- if let Some(irq_type) = self.irq_type_mut() {
- match *irq_type {
- IrqType::Msix {
- irq_max_num,
- msix_table_bar,
- msix_table_offset,
- ..
- } => {
- if self.irq_vector_mut().unwrap().len() > irq_max_num as usize {
- return Err(PciError::PciIrqError(PciIrqError::DeviceIrqOverflow));
- }
- let irq_num =
- self.irq_vector_mut().unwrap()[msg.irq_common_message.irq_index as usize];
- let common_msg = &msg.irq_common_message;
- let result = unsafe {
- c_irq_install(
- irq_num as u64,
- Some(common_msg.irq_hander),
- common_msg.irq_parameter as u64,
- common_msg.irq_name.as_ptr(),
- common_msg.irq_ack,
- )
- };
- match result as u32 {
- EINVAL => {
- return Err(PciError::PciIrqError(PciIrqError::InvalidIrqNum(irq_num)));
- }
- EAGAIN => {
- return Err(PciError::PciIrqError(PciIrqError::IrqNumOccupied(
- irq_num,
- )));
- }
- _ => {}
- }
- let msg_address = ia64_pci_get_arch_msi_message_address(0);
- let trigger = match msg.irq_specific_message {
- IrqSpecificMsg::Legacy => {
- return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
- }
- IrqSpecificMsg::Msi { trigger_mode, .. } => trigger_mode,
- };
- let msg_data = ia64_pci_get_arch_msi_message_data(irq_num, 0, trigger);
- //写入Message Data和Message Address
- let pcistandardbar = self
- .bar()
- .ok_or(PciError::PciIrqError(PciIrqError::PciBarNotInited))?;
- let msix_bar = pcistandardbar.get_bar(msix_table_bar)?;
- let vaddr = msix_bar
- .virtual_address()
- .ok_or(PciError::PciIrqError(PciIrqError::BarGetVaddrFailed))?
- as usize
- + msix_table_offset as usize
- + msg.irq_common_message.irq_index as usize * size_of::<MsixEntry>();
- let msix_entry = NonNull::new(vaddr as *mut MsixEntry).unwrap();
- unsafe {
- volwrite!(msix_entry, vector_control, 0);
- volwrite!(msix_entry, msg_data, msg_data);
- volwrite!(msix_entry, msg_upper_addr, 0);
- volwrite!(msix_entry, msg_addr, msg_address);
- }
- return Ok(0);
- }
- IrqType::Unused => {
- return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
- }
- _ => {
- return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
- }
- }
- }
- return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
- }
- /// @brief 进行PCI设备中断的卸载
- /// @param self PCI设备的可变引用
- fn irq_uninstall(&mut self) -> Result<u8, PciError> {
- self.irq_enable(false)?; //中断设置更改前先关闭对应PCI设备的中断
- if let Some(irq_type) = self.irq_type_mut() {
- match *irq_type {
- IrqType::Msix { .. } => {
- return self.msix_uninstall();
- }
- IrqType::Msi { .. } => {
- return self.msi_uninstall();
- }
- IrqType::Unused => {
- return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
- }
- _ => {
- return Err(PciError::PciIrqError(PciIrqError::IrqTypeNotSupported));
- }
- }
- }
- return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
- }
- /// @brief 进行PCI设备中断的卸载(MSI)
- /// @param self PCI设备的可变引用
- fn msi_uninstall(&mut self) -> Result<u8, PciError> {
- if let Some(irq_type) = self.irq_type_mut() {
- match *irq_type {
- IrqType::Msi {
- address_64,
- cap_offset,
- ..
- } => {
- for vector in self.irq_vector_mut().unwrap() {
- unsafe {
- c_irq_uninstall(vector.clone() as u64);
- }
- }
- PciArch::write_config(&self.common_header().bus_device_function, cap_offset, 0);
- PciArch::write_config(
- &self.common_header().bus_device_function,
- cap_offset + 4,
- 0,
- );
- PciArch::write_config(
- &self.common_header().bus_device_function,
- cap_offset + 8,
- 0,
- );
- if address_64 {
- PciArch::write_config(
- &self.common_header().bus_device_function,
- cap_offset + 12,
- 0,
- );
- }
- return Ok(0);
- }
- IrqType::Unused => {
- return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
- }
- _ => {
- return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
- }
- }
- }
- return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
- }
- /// @brief 进行PCI设备中断的卸载(MSIX)
- /// @param self PCI设备的可变引用
- fn msix_uninstall(&mut self) -> Result<u8, PciError> {
- if let Some(irq_type) = self.irq_type_mut() {
- match *irq_type {
- IrqType::Msix {
- irq_max_num,
- cap_offset,
- msix_table_bar,
- msix_table_offset,
- ..
- } => {
- for vector in self.irq_vector_mut().unwrap() {
- unsafe {
- c_irq_uninstall(vector.clone() as u64);
- }
- }
- PciArch::write_config(&self.common_header().bus_device_function, cap_offset, 0);
- let pcistandardbar = self
- .bar()
- .ok_or(PciError::PciIrqError(PciIrqError::PciBarNotInited))
- .unwrap();
- let msix_bar = pcistandardbar.get_bar(msix_table_bar).unwrap();
- for index in 0..irq_max_num {
- let vaddr = msix_bar
- .virtual_address()
- .ok_or(PciError::PciIrqError(PciIrqError::BarGetVaddrFailed))
- .unwrap() as usize
- + msix_table_offset as usize
- + index as usize * size_of::<MsixEntry>();
- let msix_entry = NonNull::new(vaddr as *mut MsixEntry).unwrap();
- unsafe {
- volwrite!(msix_entry, vector_control, 0);
- volwrite!(msix_entry, msg_data, 0);
- volwrite!(msix_entry, msg_upper_addr, 0);
- volwrite!(msix_entry, msg_addr, 0);
- }
- }
- return Ok(0);
- }
- IrqType::Unused => {
- return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
- }
- _ => {
- return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
- }
- }
- }
- return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
- }
- /// @brief 屏蔽相应位置的中断
- /// @param self PCI设备的可变引用
- /// @param irq_index 中断的位置(在vec中的index和安装的index相同)
- fn irq_mask(&mut self, irq_index: u16) -> Result<u8, PciError> {
- if let Some(irq_type) = self.irq_type_mut() {
- match *irq_type {
- IrqType::Msix { .. } => {
- return self.msix_mask(irq_index);
- }
- IrqType::Msi { .. } => {
- return self.msi_mask(irq_index);
- }
- IrqType::Unused => {
- return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
- }
- _ => {
- return Err(PciError::PciIrqError(PciIrqError::IrqTypeNotSupported));
- }
- }
- }
- return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
- }
- /// @brief 屏蔽相应位置的中断(MSI)
- /// @param self PCI设备的可变引用
- /// @param irq_index 中断的位置(在vec中的index和安装的index相同)
- fn msi_mask(&mut self, irq_index: u16) -> Result<u8, PciError> {
- if let Some(irq_type) = self.irq_type_mut() {
- match *irq_type {
- IrqType::Msi {
- maskable,
- address_64,
- cap_offset,
- irq_max_num,
- } => {
- if irq_index >= irq_max_num {
- return Err(PciError::PciIrqError(PciIrqError::InvalidIrqIndex(
- irq_index,
- )));
- }
- if maskable {
- match address_64 {
- true => {
- let mut mask = PciArch::read_config(
- &self.common_header().bus_device_function,
- cap_offset + 16,
- );
- mask |= 1 << irq_index;
- PciArch::write_config(
- &self.common_header().bus_device_function,
- cap_offset,
- mask,
- );
- }
- false => {
- let mut mask = PciArch::read_config(
- &self.common_header().bus_device_function,
- cap_offset + 12,
- );
- mask |= 1 << irq_index;
- PciArch::write_config(
- &self.common_header().bus_device_function,
- cap_offset,
- mask,
- );
- }
- }
- return Ok(0);
- }
- return Err(PciError::PciIrqError(PciIrqError::MaskNotSupported));
- }
- IrqType::Unused => {
- return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
- }
- _ => {
- return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
- }
- }
- }
- return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
- }
- /// @brief 屏蔽相应位置的中断(MSIX)
- /// @param self PCI设备的可变引用
- /// @param irq_index 中断的位置(在vec中的index和安装的index相同)
- fn msix_mask(&mut self, irq_index: u16) -> Result<u8, PciError> {
- if let Some(irq_type) = self.irq_type_mut() {
- match *irq_type {
- IrqType::Msix {
- irq_max_num,
- msix_table_bar,
- msix_table_offset,
- ..
- } => {
- if irq_index >= irq_max_num {
- return Err(PciError::PciIrqError(PciIrqError::InvalidIrqIndex(
- irq_index,
- )));
- }
- let pcistandardbar = self
- .bar()
- .ok_or(PciError::PciIrqError(PciIrqError::PciBarNotInited))
- .unwrap();
- let msix_bar = pcistandardbar.get_bar(msix_table_bar).unwrap();
- let vaddr = msix_bar.virtual_address().unwrap() as usize
- + msix_table_offset as usize
- + irq_index as usize * size_of::<MsixEntry>();
- let msix_entry = NonNull::new(vaddr as *mut MsixEntry).unwrap();
- unsafe {
- volwrite!(msix_entry, vector_control, 1);
- }
- return Ok(0);
- }
- IrqType::Unused => {
- return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
- }
- _ => {
- return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
- }
- }
- }
- return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
- }
- /// @brief 解除屏蔽相应位置的中断
- /// @param self PCI设备的可变引用
- /// @param irq_index 中断的位置(在vec中的index和安装的index相同)
- fn irq_unmask(&mut self, irq_index: u16) -> Result<u8, PciError> {
- if let Some(irq_type) = self.irq_type_mut() {
- match *irq_type {
- IrqType::Msix { .. } => {
- return self.msix_unmask(irq_index);
- }
- IrqType::Msi { .. } => {
- return self.msi_unmask(irq_index);
- }
- IrqType::Unused => {
- return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
- }
- _ => {
- return Err(PciError::PciIrqError(PciIrqError::IrqTypeNotSupported));
- }
- }
- }
- return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
- }
- /// @brief 解除屏蔽相应位置的中断(MSI)
- /// @param self PCI设备的可变引用
- /// @param irq_index 中断的位置(在vec中的index和安装的index相同)
- fn msi_unmask(&mut self, irq_index: u16) -> Result<u8, PciError> {
- if let Some(irq_type) = self.irq_type_mut() {
- match *irq_type {
- IrqType::Msi {
- maskable,
- address_64,
- cap_offset,
- irq_max_num,
- } => {
- if irq_index >= irq_max_num {
- return Err(PciError::PciIrqError(PciIrqError::InvalidIrqIndex(
- irq_index,
- )));
- }
- if maskable {
- match address_64 {
- true => {
- let mut mask = PciArch::read_config(
- &self.common_header().bus_device_function,
- cap_offset + 16,
- );
- mask &= !(1 << irq_index);
- PciArch::write_config(
- &self.common_header().bus_device_function,
- cap_offset,
- mask,
- );
- }
- false => {
- let mut mask = PciArch::read_config(
- &self.common_header().bus_device_function,
- cap_offset + 12,
- );
- mask &= !(1 << irq_index);
- PciArch::write_config(
- &self.common_header().bus_device_function,
- cap_offset,
- mask,
- );
- }
- }
- }
- return Err(PciError::PciIrqError(PciIrqError::MaskNotSupported));
- }
- IrqType::Unused => {
- return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
- }
- _ => {
- return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
- }
- }
- }
- return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
- }
- /// @brief 解除屏蔽相应位置的中断(MSIX)
- /// @param self PCI设备的可变引用
- /// @param irq_index 中断的位置(在vec中的index和安装的index相同)
- fn msix_unmask(&mut self, irq_index: u16) -> Result<u8, PciError> {
- if let Some(irq_type) = self.irq_type_mut() {
- match *irq_type {
- IrqType::Msix {
- irq_max_num,
- msix_table_bar,
- msix_table_offset,
- ..
- } => {
- if irq_index >= irq_max_num {
- return Err(PciError::PciIrqError(PciIrqError::InvalidIrqIndex(
- irq_index,
- )));
- }
- let pcistandardbar = self
- .bar()
- .ok_or(PciError::PciIrqError(PciIrqError::PciBarNotInited))
- .unwrap();
- let msix_bar = pcistandardbar.get_bar(msix_table_bar).unwrap();
- let vaddr = msix_bar.virtual_address().unwrap() as usize
- + msix_table_offset as usize
- + irq_index as usize * size_of::<MsixEntry>();
- let msix_entry = NonNull::new(vaddr as *mut MsixEntry).unwrap();
- unsafe {
- volwrite!(msix_entry, vector_control, 0);
- }
- return Ok(0);
- }
- IrqType::Unused => {
- return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
- }
- _ => {
- return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
- }
- }
- }
- return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
- }
- /// @brief 检查被挂起的中断是否在挂起的时候产生了
- /// @param self PCI设备的可变引用
- /// @param irq_index 中断的位置(在vec中的index和安装的index相同)
- /// @return 是否在挂起过程中产生中断(异常情况也返回false)
- fn irq_check_pending(&mut self, irq_index: u16) -> Result<bool, PciError> {
- if let Some(irq_type) = self.irq_type_mut() {
- match *irq_type {
- IrqType::Msix { .. } => {
- return self.msix_check_pending(irq_index);
- }
- IrqType::Msi { .. } => {
- return self.msi_check_pending(irq_index);
- }
- IrqType::Unused => {
- return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
- }
- _ => {
- return Err(PciError::PciIrqError(PciIrqError::IrqTypeNotSupported));
- }
- }
- }
- return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
- }
- /// @brief 检查被挂起的中断是否在挂起的时候产生了(MSI)
- /// @param self PCI设备的可变引用
- /// @param irq_index 中断的位置(在vec中的index和安装的index相同)
- /// @return 是否在挂起过程中产生中断(异常情况也返回false)
- fn msi_check_pending(&mut self, irq_index: u16) -> Result<bool, PciError> {
- if let Some(irq_type) = self.irq_type_mut() {
- match *irq_type {
- IrqType::Msi {
- maskable,
- address_64,
- cap_offset,
- irq_max_num,
- } => {
- if irq_index >= irq_max_num {
- return Err(PciError::PciIrqError(PciIrqError::InvalidIrqIndex(
- irq_index,
- )));
- }
- if maskable {
- match address_64 {
- true => {
- let mut pend = PciArch::read_config(
- &self.common_header().bus_device_function,
- cap_offset + 20,
- );
- pend &= 1 << irq_index;
- return Ok(pend != 0);
- }
- false => {
- let mut pend = PciArch::read_config(
- &self.common_header().bus_device_function,
- cap_offset + 16,
- );
- pend &= 1 << irq_index;
- return Ok(pend != 0);
- }
- }
- }
- }
- IrqType::Unused => {
- return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
- }
- _ => {
- return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
- }
- }
- }
- return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
- }
- /// @brief 检查被挂起的中断是否在挂起的时候产生了(MSIX)
- /// @param self PCI设备的可变引用
- /// @param irq_index 中断的位置(在vec中的index和安装的index相同)
- /// @return 是否在挂起过程中产生中断(异常情况也返回false)
- fn msix_check_pending(&mut self, irq_index: u16) -> Result<bool, PciError> {
- if let Some(irq_type) = self.irq_type_mut() {
- match *irq_type {
- IrqType::Msix {
- irq_max_num,
- pending_table_bar,
- pending_table_offset,
- ..
- } => {
- if irq_index >= irq_max_num {
- return Err(PciError::PciIrqError(PciIrqError::InvalidIrqIndex(
- irq_index,
- )));
- }
- let pcistandardbar = self
- .bar()
- .ok_or(PciError::PciIrqError(PciIrqError::PciBarNotInited))
- .unwrap();
- let pending_bar = pcistandardbar.get_bar(pending_table_bar).unwrap();
- let vaddr = pending_bar.virtual_address().unwrap() as usize
- + pending_table_offset as usize
- + (irq_index as usize / 64) * size_of::<PendingEntry>();
- let pending_entry = NonNull::new(vaddr as *mut PendingEntry).unwrap();
- let pending_entry = unsafe { volread!(pending_entry, entry) };
- return Ok(pending_entry & (1 << (irq_index as u64 % 64)) != 0);
- }
- IrqType::Unused => {
- return Err(PciError::PciIrqError(PciIrqError::IrqNotInited));
- }
- _ => {
- return Err(PciError::PciIrqError(PciIrqError::IrqTypeUnmatch));
- }
- }
- }
- return Err(PciError::PciIrqError(PciIrqError::PciDeviceNotSupportIrq));
- }
- }
- /// PCI标准设备的msi/msix中断相关函数块
- impl PciInterrupt for PciDeviceStructureGeneralDevice {}
|