use super::Device;
use crate::{
bindings,
device,
devres::Devres,
io::{
Io,
IoCapable,
IoKnownSize,
Mmio,
MmioRaw, },
prelude::*,
sync::aref::ARef, };
use core::{
marker::PhantomData,
ops::Deref, };
#[repr(usize)]
#[derive(Eq, PartialEq)]
pub enum ConfigSpaceSize {
Normal = 256,
Extended = 4096,
}
impl ConfigSpaceSize {
#[inline(always)]
pub const fn into_raw(self) -> usize {
self as usize
}
}
pub struct Normal;
pub struct Extended;
pub trait ConfigSpaceKind {
const SIZE: usize;
}
impl ConfigSpaceKind for Normal {
const SIZE: usize = 256;
}
impl ConfigSpaceKind for Extended {
const SIZE: usize = 4096;
}
pub struct ConfigSpace<'a, S: ConfigSpaceKind = Extended> {
pub(crate) pdev: &'a Device<device::Bound>,
_marker: PhantomData<S>,
}
macro_rules! impl_config_space_io_capable {
($ty:ty, $read_fn:ident, $write_fn:ident) => {
impl<'a, S: ConfigSpaceKind> IoCapable<$ty> for ConfigSpace<'a, S> {
unsafe fn io_read(&self, address: usize) -> $ty {
let mut val: $ty = 0;
let _ret =
unsafe { bindings::$read_fn(self.pdev.as_raw(), address as i32, &mut val) };
val
}
unsafe fn io_write(&self, value: $ty, address: usize) {
let _ret =
unsafe { bindings::$write_fn(self.pdev.as_raw(), address as i32, value) };
}
}
};
}
impl_config_space_io_capable!(u8, pci_read_config_byte, pci_write_config_byte);
impl_config_space_io_capable!(u16, pci_read_config_word, pci_write_config_word);
impl_config_space_io_capable!(u32, pci_read_config_dword, pci_write_config_dword);
impl<'a, S: ConfigSpaceKind> Io for ConfigSpace<'a, S> {
#[inline]
fn addr(&self) -> usize {
0
}
#[inline]
fn maxsize(&self) -> usize {
self.pdev.cfg_size().into_raw()
}
}
impl<'a, S: ConfigSpaceKind> IoKnownSize for ConfigSpace<'a, S> {
const MIN_SIZE: usize = S::SIZE;
}
pub struct Bar<const SIZE: usize = 0> {
pdev: ARef<Device>,
io: MmioRaw<SIZE>,
num: i32,
}
impl<const SIZE: usize> Bar<SIZE> {
pub(super) fn new(pdev: &Device, num: u32, name: &CStr) -> Result<Self> {
let len = pdev.resource_len(num)?;
if len == 0 {
return Err(ENOMEM);
}
let num = i32::try_from(num)?;
let ret = unsafe { bindings::pci_request_region(pdev.as_raw(), num, name.as_char_ptr()) };
if ret != 0 {
return Err(EBUSY);
}
let ioptr: usize = unsafe { bindings::pci_iomap(pdev.as_raw(), num, 0) } as usize;
if ioptr == 0 {
unsafe { bindings::pci_release_region(pdev.as_raw(), num) };
return Err(ENOMEM);
}
let io = match MmioRaw::new(ioptr, len as usize) {
Ok(io) => io,
Err(err) => {
unsafe { Self::do_release(pdev, ioptr, num) };
return Err(err);
}
};
Ok(Bar {
pdev: pdev.into(),
io,
num,
})
}
unsafe fn do_release(pdev: &Device, ioptr: usize, num: i32) {
unsafe {
bindings::pci_iounmap(pdev.as_raw(), ioptr as *mut c_void);
bindings::pci_release_region(pdev.as_raw(), num);
}
}
fn release(&self) {
unsafe { Self::do_release(&self.pdev, self.io.addr(), self.num) };
}
}
impl Bar {
#[inline]
pub(super) fn index_is_valid(index: u32) -> bool {
index < bindings::PCI_NUM_RESOURCES
}
}
impl<const SIZE: usize> Drop for Bar<SIZE> {
fn drop(&mut self) {
self.release();
}
}
impl<const SIZE: usize> Deref for Bar<SIZE> {
type Target = Mmio<SIZE>;
fn deref(&self) -> &Self::Target {
unsafe { Mmio::from_raw(&self.io) }
}
}
impl Device<device::Bound> {
pub fn iomap_region_sized<'a, const SIZE: usize>(
&'a self,
bar: u32,
name: &'a CStr,
) -> impl PinInit<Devres<Bar<SIZE>>, Error> + 'a {
Devres::new(self.as_ref(), Bar::<SIZE>::new(self, bar, name))
}
pub fn iomap_region<'a>(
&'a self,
bar: u32,
name: &'a CStr,
) -> impl PinInit<Devres<Bar>, Error> + 'a {
self.iomap_region_sized::<0>(bar, name)
}
pub fn cfg_size(&self) -> ConfigSpaceSize {
let size = unsafe { (*self.as_raw()).cfg_size };
match size {
256 => ConfigSpaceSize::Normal,
4096 => ConfigSpaceSize::Extended,
_ => {
unreachable!();
}
}
}
pub fn config_space<'a>(&'a self) -> ConfigSpace<'a, Normal> {
ConfigSpace {
pdev: self,
_marker: PhantomData,
}
}
pub fn config_space_extended<'a>(&'a self) -> Result<ConfigSpace<'a, Extended>> {
if self.cfg_size() != ConfigSpaceSize::Extended {
return Err(EINVAL);
}
Ok(ConfigSpace {
pdev: self,
_marker: PhantomData,
})
}
}