forked from microsoft/openvmm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdma_manager.rs
More file actions
85 lines (72 loc) · 2.92 KB
/
dma_manager.rs
File metadata and controls
85 lines (72 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use std::sync::{Arc, Mutex, Weak};
use memory_range::MemoryRange;
use once_cell::sync::OnceCell;
pub use dma_client::{DmaClient, DmaInterface, DmaTransaction, DmaTransactionHandler};
pub enum DmaError {
InitializationFailed,
MapFailed,
UnmapFailed,
PinFailed,
BounceBufferFailed,
}
static GLOBAL_DMA_MANAGER: OnceCell<Arc<GlobalDmaManager>> = OnceCell::new();
/// Global DMA Manager to handle resources and manage clients
pub struct GlobalDmaManager {
physical_ranges: Vec<MemoryRange>,
bounce_buffers: Vec<MemoryRange>,
clients: Mutex<Vec<Weak<DmaClient>>>,
client_thresholds: Mutex<Vec<(Weak<DmaClient>, usize)>>,
}
impl GlobalDmaManager {
/// Initializes the global DMA manager with physical ranges and bounce buffers
pub fn initialize(
physical_ranges: Vec<MemoryRange>,
bounce_buffers: Vec<MemoryRange>,
) -> Result<(), DmaError> {
let manager = Arc::new(Self {
physical_ranges,
bounce_buffers,
clients: Mutex::new(Vec::new()),
client_thresholds: Mutex::new(Vec::new()),
});
GLOBAL_DMA_MANAGER.set(manager).map_err(|_| DmaError::InitializationFailed)
}
/// Accesses the singleton instance of the global manager
pub fn get_instance() -> Arc<GlobalDmaManager> {
GLOBAL_DMA_MANAGER
.get()
.expect("GlobalDmaManager has not been initialized")
.clone()
}
/// Creates a new `DmaClient` and registers it with the global manager, along with its threshold
pub fn create_client(&self, pinning_threshold: usize) -> Arc<DmaClient> {
let client = Arc::new(DmaClient::new(Arc::downgrade(&self.get_instance())));
self.register_client(&client, pinning_threshold);
client
}
/// Adds a new client to the list and stores its pinning threshold
fn register_client(&self, client: &Arc<DmaClient>, threshold: usize) {
let mut clients = self.clients.lock().unwrap();
clients.push(Arc::downgrade(client));
let mut thresholds = self.client_thresholds.lock().unwrap();
thresholds.push((Arc::downgrade(client), threshold));
}
/// Retrieves the pinning threshold for a given client
pub fn get_client_threshold(&self, client: &Arc<DmaClient>) -> Option<usize> {
let thresholds = self.client_thresholds.lock().unwrap();
thresholds.iter().find_map(|(weak_client, threshold)| {
weak_client
.upgrade()
.filter(|c| Arc::ptr_eq(c, client))
.map(|_| *threshold)
})
}
/// Checks if the given memory range is already pinned
pub fn is_pinned(&self, range: &MemoryRange) -> bool {
false // Placeholder
}
/// Allocates a bounce buffer if available, otherwise returns an error
pub fn allocate_bounce_buffer(&self, size: usize) -> Result<usize, DmaError> {
Err(DmaError::BounceBufferFailed) // Placeholder
}
}