pci.rs75.00%
1
// Copyright 2024 Google LLC2
//3
// Licensed under the Apache License, Version 2.0 (the "License");4
// you may not use this file except in compliance with the License.5
// You may obtain a copy of the License at6
//7
// https://www.apache.org/licenses/LICENSE-2.08
//9
// Unless required by applicable law or agreed to in writing, software10
// distributed under the License is distributed on an "AS IS" BASIS,11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12
// See the License for the specific language governing permissions and13
// limitations under the License.14
15
pub mod bus;16
pub mod cap;17
pub mod config;18
pub mod host_bridge;19
pub mod pvpanic;20
pub mod segment;21
22
use std::fmt::{Debug, Display, Formatter};23
use std::sync::Arc;24
25
use bitfield::bitfield;26
use snafu::Snafu;27
28
use crate::device::Pause;29
use crate::errors::{DebugTrace, trace_error};30
use crate::mem::{IoRegion, MemRegion};31
32
use self::config::PciConfig;33
34
bitfield! {35
#[derive(Copy, Clone, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]36
pub struct Bdf(u16);37
impl Debug;38
impl new;39
pub u8, bus, set_bus: 15, 8;40
pub u8, dev, set_dev: 7, 3;41
pub u8, func, set_func: 2, 0;42
}43
44
impl Display for Bdf {45
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {114x46
write!(f, "{:02x}:{:02x}.{:x}", self.bus(), self.dev(), self.func())114x47
}114x48
}49
50
#[trace_error]51
#[derive(Snafu, DebugTrace)]52
#[snafu(module, visibility(pub(crate)), context(suffix(false)))]53
pub enum Error {54
#[snafu(display("Failed to access guest memory"), context(false))]55
Memory { source: Box<crate::mem::Error> },56
#[snafu(display("Failed to reset device"))]57
Reset {58
source: Box<dyn DebugTrace + Send + Sync + 'static>,59
},60
}61
62
pub type Result<T, E = Error> = std::result::Result<T, E>;63
64
pub trait Pci: Debug + Send + Sync + Pause + 'static {65
fn name(&self) -> &str;66
fn config(&self) -> &dyn PciConfig;67
fn reset(&self) -> Result<()>;68
}69
70
#[derive(Debug, Clone)]71
pub enum PciBar {72
Empty,73
Mem(Arc<MemRegion>),74
Io(Arc<IoRegion>),75
}76