| 1 | |
| 2 |
|
| 3 | |
| 4 |
|
| 5 | use super::Device;
|
| 6 | use crate::{
|
| 7 | bindings,
|
| 8 | device,
|
| 9 | devres::DevresLt,
|
| 10 | io::{
|
| 11 | IoBackend,
|
| 12 | IoBase,
|
| 13 | IoCapable,
|
| 14 | Mmio,
|
| 15 | MmioBackend,
|
| 16 | MmioRaw,
|
| 17 | Region, |
| 18 | },
|
| 19 | prelude::*,
|
| 20 | ptr::KnownSize,
|
| 21 | types::{
|
| 22 | CovariantForLt,
|
| 23 | ForLt, |
| 24 | }, |
| 25 | };
|
| 26 |
|
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | #[repr(usize)]
|
| 33 | #[derive(Eq, PartialEq)]
|
| 34 | pub enum ConfigSpaceSize {
|
| 35 | |
| 36 | Normal = 256,
|
| 37 |
|
| 38 | |
| 39 | Extended = 4096,
|
| 40 | }
|
| 41 |
|
| 42 | impl ConfigSpaceSize {
|
| 43 | |
| 44 | #[inline(always)]
|
| 45 | pub const fn into_raw(self) -> usize {
|
| 46 | |
| 47 | |
| 48 | self as usize
|
| 49 | }
|
| 50 | }
|
| 51 |
|
| 52 | |
| 53 | pub type Normal = Region<256>;
|
| 54 |
|
| 55 | |
| 56 | pub type Extended = Region<4096>;
|
| 57 |
|
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | pub struct ConfigSpace<'a, T: ?Sized = Extended> {
|
| 72 | pub(crate) pdev: &'a Device<device::Bound>,
|
| 73 | ptr: *mut T,
|
| 74 | }
|
| 75 |
|
| 76 | impl<T: ?Sized> Copy for ConfigSpace<'_, T> {}
|
| 77 | impl<T: ?Sized> Clone for ConfigSpace<'_, T> {
|
| 78 | #[inline]
|
| 79 | fn clone(&self) -> Self {
|
| 80 | *self
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | |
| 85 | unsafe impl<T: ?Sized + Sync> Send for ConfigSpace<'_, T> {}
|
| 86 |
|
| 87 | |
| 88 | unsafe impl<T: ?Sized + Sync> Sync for ConfigSpace<'_, T> {}
|
| 89 |
|
| 90 | |
| 91 | pub struct ConfigSpaceBackend;
|
| 92 |
|
| 93 | impl IoBackend for ConfigSpaceBackend {
|
| 94 | type View<'a, T: ?Sized + KnownSize> = ConfigSpace<'a, T>;
|
| 95 |
|
| 96 | #[inline]
|
| 97 | fn as_ptr<'a, T: ?Sized + KnownSize>(view: ConfigSpace<'a, T>) -> *mut T {
|
| 98 | view.ptr
|
| 99 | }
|
| 100 |
|
| 101 | #[inline]
|
| 102 | unsafe fn project_view<'a, T: ?Sized + KnownSize, U: ?Sized + KnownSize>(
|
| 103 | view: Self::View<'a, T>,
|
| 104 | ptr: *mut U,
|
| 105 | ) -> Self::View<'a, U> {
|
| 106 | |
| 107 | ConfigSpace {
|
| 108 | pdev: view.pdev,
|
| 109 | ptr,
|
| 110 | }
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | |
| 115 | macro_rules! impl_config_space_io_capable {
|
| 116 | ($ty:ty, $read_fn:ident, $write_fn:ident) => {
|
| 117 | impl IoCapable<$ty> for ConfigSpaceBackend {
|
| 118 | fn io_read(view: ConfigSpace<'_, $ty>) -> $ty {
|
| 119 | |
| 120 | |
| 121 | |
| 122 | let addr = view.ptr.addr() as i32;
|
| 123 |
|
| 124 | let mut val: $ty = 0;
|
| 125 |
|
| 126 | |
| 127 | |
| 128 | let _ = unsafe { bindings::$read_fn(view.pdev.as_raw(), addr, &mut val) };
|
| 129 | val
|
| 130 | }
|
| 131 |
|
| 132 | fn io_write(view: ConfigSpace<'_, $ty>, value: $ty) {
|
| 133 | |
| 134 | |
| 135 | |
| 136 | let addr = view.ptr.addr() as i32;
|
| 137 |
|
| 138 | |
| 139 | |
| 140 | let _ = unsafe { bindings::$write_fn(view.pdev.as_raw(), addr, value) };
|
| 141 | }
|
| 142 | }
|
| 143 | };
|
| 144 | }
|
| 145 |
|
| 146 | |
| 147 | impl_config_space_io_capable!(u8, pci_read_config_byte, pci_write_config_byte);
|
| 148 | impl_config_space_io_capable!(u16, pci_read_config_word, pci_write_config_word);
|
| 149 | impl_config_space_io_capable!(u32, pci_read_config_dword, pci_write_config_dword);
|
| 150 |
|
| 151 | impl<'a, T: ?Sized + KnownSize> IoBase<'a> for ConfigSpace<'a, T> {
|
| 152 | type Backend = ConfigSpaceBackend;
|
| 153 | type Target = T;
|
| 154 |
|
| 155 | #[inline]
|
| 156 | fn as_view(self) -> ConfigSpace<'a, T> {
|
| 157 | self
|
| 158 | }
|
| 159 | }
|
| 160 |
|
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | pub struct Bar<'a, const SIZE: usize = 0> {
|
| 171 | pdev: &'a Device<device::Bound>,
|
| 172 | io: MmioRaw<crate::io::Region<SIZE>>,
|
| 173 | num: i32,
|
| 174 | }
|
| 175 |
|
| 176 | impl<const SIZE: usize> ForLt for Bar<'static, SIZE> {
|
| 177 | type Of<'a> = Bar<'a, SIZE>;
|
| 178 | }
|
| 179 |
|
| 180 | |
| 181 | |
| 182 | unsafe impl<const SIZE: usize> CovariantForLt for Bar<'static, SIZE> {}
|
| 183 |
|
| 184 | |
| 185 | |
| 186 | |
| 187 | pub type DevresBar<const SIZE: usize = 0> = DevresLt<Bar<'static, SIZE>>;
|
| 188 |
|
| 189 | impl<'a, const SIZE: usize> Bar<'a, SIZE> {
|
| 190 | pub(super) fn new(
|
| 191 | pdev: &'a Device<device::Bound>,
|
| 192 | num: u32,
|
| 193 | name: &'static CStr,
|
| 194 | ) -> Result<Self> {
|
| 195 | let len = pdev.resource_len(num)?;
|
| 196 | if len == 0 {
|
| 197 | return Err(ENOMEM);
|
| 198 | }
|
| 199 |
|
| 200 | |
| 201 | let num = i32::try_from(num)?;
|
| 202 |
|
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | let ret = unsafe { bindings::pci_request_region(pdev.as_raw(), num, name.as_char_ptr()) };
|
| 208 | if ret != 0 {
|
| 209 | return Err(EBUSY);
|
| 210 | }
|
| 211 |
|
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | let ioptr: usize = unsafe { bindings::pci_iomap(pdev.as_raw(), num, 0) } as usize;
|
| 217 | if ioptr == 0 {
|
| 218 | |
| 219 | |
| 220 | |
| 221 | unsafe { bindings::pci_release_region(pdev.as_raw(), num) };
|
| 222 | return Err(ENOMEM);
|
| 223 | }
|
| 224 |
|
| 225 | let io = match MmioRaw::new_region(ioptr, len as usize) {
|
| 226 | Ok(io) => io,
|
| 227 | Err(err) => {
|
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | unsafe { Self::do_release(pdev, ioptr, num) };
|
| 233 | return Err(err);
|
| 234 | }
|
| 235 | };
|
| 236 |
|
| 237 | Ok(Bar { pdev, io, num })
|
| 238 | }
|
| 239 |
|
| 240 | |
| 241 | |
| 242 | |
| 243 | unsafe fn do_release(pdev: &Device, ioptr: usize, num: i32) {
|
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | unsafe {
|
| 249 | bindings::pci_iounmap(pdev.as_raw(), ioptr as *mut c_void);
|
| 250 | bindings::pci_release_region(pdev.as_raw(), num);
|
| 251 | }
|
| 252 | }
|
| 253 |
|
| 254 | fn release(&self) {
|
| 255 | |
| 256 | unsafe { Self::do_release(self.pdev, self.io.addr(), self.num) };
|
| 257 | }
|
| 258 |
|
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | pub fn into_devres(self) -> Result<DevresBar<SIZE>> {
|
| 264 | let pdev = self.pdev;
|
| 265 | |
| 266 | |
| 267 | unsafe { DevresLt::new(pdev.as_ref(), self) }
|
| 268 | }
|
| 269 | }
|
| 270 |
|
| 271 | impl Bar<'_> {
|
| 272 | #[inline]
|
| 273 | pub(super) fn index_is_valid(index: u32) -> bool {
|
| 274 | |
| 275 | index < bindings::PCI_NUM_RESOURCES
|
| 276 | }
|
| 277 | }
|
| 278 |
|
| 279 | impl<const SIZE: usize> Drop for Bar<'_, SIZE> {
|
| 280 | fn drop(&mut self) {
|
| 281 | self.release();
|
| 282 | }
|
| 283 | }
|
| 284 |
|
| 285 | impl<'a, const SIZE: usize> IoBase<'a> for &'a Bar<'_, SIZE> {
|
| 286 | type Backend = MmioBackend;
|
| 287 | type Target = crate::io::Region<SIZE>;
|
| 288 |
|
| 289 | #[inline]
|
| 290 | fn as_view(self) -> Mmio<'a, Self::Target> {
|
| 291 | |
| 292 | unsafe { Mmio::from_raw(self.io) }
|
| 293 | }
|
| 294 | }
|
| 295 |
|
| 296 | impl Device<device::Bound> {
|
| 297 | |
| 298 | |
| 299 | pub fn iomap_region_sized<'a, const SIZE: usize>(
|
| 300 | &'a self,
|
| 301 | bar: u32,
|
| 302 | name: &'static CStr,
|
| 303 | ) -> Result<Bar<'a, SIZE>> {
|
| 304 | Bar::new(self, bar, name)
|
| 305 | }
|
| 306 |
|
| 307 | |
| 308 | pub fn iomap_region<'a>(&'a self, bar: u32, name: &'static CStr) -> Result<Bar<'a>> {
|
| 309 | self.iomap_region_sized::<0>(bar, name)
|
| 310 | }
|
| 311 |
|
| 312 | |
| 313 | pub fn cfg_size(&self) -> ConfigSpaceSize {
|
| 314 | |
| 315 | let size = unsafe { (*self.as_raw()).cfg_size };
|
| 316 | match size {
|
| 317 | 256 => ConfigSpaceSize::Normal,
|
| 318 | 4096 => ConfigSpaceSize::Extended,
|
| 319 | _ => {
|
| 320 | |
| 321 | |
| 322 | unreachable!();
|
| 323 | }
|
| 324 | }
|
| 325 | }
|
| 326 |
|
| 327 | |
| 328 | pub fn config_space<'a>(&'a self) -> ConfigSpace<'a, Normal> {
|
| 329 | |
| 330 | ConfigSpace {
|
| 331 | pdev: self,
|
| 332 | ptr: Normal::ptr_from_raw_parts_mut(core::ptr::null_mut(), self.cfg_size().into_raw()),
|
| 333 | }
|
| 334 | }
|
| 335 |
|
| 336 | |
| 337 | pub fn config_space_extended<'a>(&'a self) -> Result<ConfigSpace<'a, Extended>> {
|
| 338 | if self.cfg_size() != ConfigSpaceSize::Extended {
|
| 339 | return Err(EINVAL);
|
| 340 | }
|
| 341 |
|
| 342 | |
| 343 | Ok(ConfigSpace {
|
| 344 | pdev: self,
|
| 345 | ptr: Extended::ptr_from_raw_parts_mut(core::ptr::null_mut(), 4096),
|
| 346 | })
|
| 347 | }
|
| 348 | }
|