missing_dev_resources/
lib.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Resources for a device that ignores accesses to specified regions.
5
6#![forbid(unsafe_code)]
7
8use mesh::MeshPayload;
9use std::ops::RangeInclusive;
10use vm_resource::ResourceId;
11use vm_resource::kind::ChipsetDeviceHandleKind;
12
13/// A handle to a device that ignores accesses to specified regions.
14#[derive(MeshPayload, Default)]
15pub struct MissingDevHandle {
16    /// The port I/O regions ignored by this device, `(name, start, end_inclusive)`.
17    pub pio: Vec<(String, u16, u16)>,
18    /// The MMIO regions ignored by this device, `(name, start, end_inclusive)`.
19    pub mmio: Vec<(String, u64, u64)>,
20}
21
22impl ResourceId<ChipsetDeviceHandleKind> for MissingDevHandle {
23    const ID: &'static str = "missing-dev";
24}
25
26impl MissingDevHandle {
27    /// Create an empty instance.
28    pub fn new() -> Self {
29        Self::default()
30    }
31
32    /// Add a PIO region to the device.
33    pub fn claim_pio(mut self, region_name: impl Into<String>, range: RangeInclusive<u16>) -> Self {
34        self.pio
35            .push((region_name.into(), *range.start(), *range.end()));
36        self
37    }
38
39    /// Add an MMIO region to the device.
40    pub fn claim_mmio(
41        mut self,
42        region_name: impl Into<String>,
43        range: RangeInclusive<u64>,
44    ) -> Self {
45        self.mmio
46            .push((region_name.into(), *range.start(), *range.end()));
47        self
48    }
49}