xref: /aosp_15_r20/external/crosvm/resources/src/lib.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1 // Copyright 2018 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 //! Manages system resources that can be allocated to VMs and their devices.
6 
7 use remain::sorted;
8 use serde::Deserialize;
9 use serde::Serialize;
10 use thiserror::Error;
11 
12 pub use crate::address_range::AddressRange;
13 pub use crate::system_allocator::AllocOptions;
14 pub use crate::system_allocator::MmioType;
15 pub use crate::system_allocator::SystemAllocator;
16 pub use crate::system_allocator::SystemAllocatorConfig;
17 
18 pub mod address_allocator;
19 mod address_range;
20 mod system_allocator;
21 
22 /// Used to tag SystemAllocator allocations.
23 #[derive(Debug, Eq, PartialEq, Hash, Copy, Clone, Serialize, Deserialize)]
24 pub enum Alloc {
25     /// An anonymous resource allocation.
26     /// Should only be instantiated through `SystemAllocator::get_anon_alloc()`.
27     /// Avoid using these. Instead, use / create a more descriptive Alloc variant.
28     Anon(usize),
29     /// A PCI BAR region with associated bus, device, function and bar numbers.
30     PciBar { bus: u8, dev: u8, func: u8, bar: u8 },
31     /// GPU render node region.
32     GpuRenderNode,
33     /// Pmem device region with associated device index.
34     PmemDevice(usize),
35     /// pstore region.
36     Pstore,
37     /// A PCI bridge window with associated bus, dev, function.
38     PciBridgeWindow { bus: u8, dev: u8, func: u8 },
39     /// A PCI bridge prefetch window with associated bus, dev, function.
40     PciBridgePrefetchWindow { bus: u8, dev: u8, func: u8 },
41     /// File-backed memory mapping.
42     FileBacked(u64),
43 }
44 
45 #[sorted]
46 #[derive(Error, Debug, Eq, PartialEq)]
47 pub enum Error {
48     #[error("Allocation cannot have size of 0")]
49     AllocSizeZero,
50     #[error("Pool alignment must be a power of 2")]
51     BadAlignment,
52     #[error("Alloc does not exist: {0:?}")]
53     BadAlloc(Alloc),
54     #[error("Alloc already exists: {0:?}")]
55     ExistingAlloc(Alloc),
56     #[error("Invalid Alloc: {0:?}")]
57     InvalidAlloc(Alloc),
58     #[error("IO port out of range: {0}")]
59     IOPortOutOfRange(AddressRange),
60     #[error("Platform MMIO address range not specified")]
61     MissingPlatformMMIOAddresses,
62     #[error("No IO address range specified")]
63     NoIoAllocator,
64     #[error("Out of bounds")]
65     OutOfBounds,
66     #[error("Out of space")]
67     OutOfSpace,
68     #[error("base={base} + size={size} overflows")]
69     PoolOverflow { base: u64, size: u64 },
70     #[error("Overlapping region {0}")]
71     RegionOverlap(AddressRange),
72 }
73 
74 pub type Result<T> = std::result::Result<T, Error>;
75