Alioth Code Coverage

pci.rs75.00%

1// Copyright 2024 Google LLC
2//
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 at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// 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 and
13// limitations under the License.
14
15pub mod bus;
16pub mod cap;
17pub mod config;
18pub mod host_bridge;
19pub mod pvpanic;
20pub mod segment;
21
22use std::fmt::{Debug, Display, Formatter};
23use std::sync::Arc;
24
25use bitfield::bitfield;
26use snafu::Snafu;
27
28use crate::device::Pause;
29use crate::errors::{DebugTrace, trace_error};
30use crate::mem::{IoRegion, MemRegion};
31
32use self::config::PciConfig;
33
34bitfield! {
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
44impl Display for Bdf {
45 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {114x
46 write!(f, "{:02x}:{:02x}.{:x}", self.bus(), self.dev(), self.func())114x
47 }114x
48}
49
50#[trace_error]
51#[derive(Snafu, DebugTrace)]
52#[snafu(module, visibility(pub(crate)), context(suffix(false)))]
53pub 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
62pub type Result<T, E = Error> = std::result::Result<T, E>;
63
64pub 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)]
71pub enum PciBar {
72 Empty,
73 Mem(Arc<MemRegion>),
74 Io(Arc<IoRegion>),
75}
76