forked from microsoft/openvmm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdma_client.rs
More file actions
89 lines (75 loc) · 2.95 KB
/
dma_client.rs
File metadata and controls
89 lines (75 loc) · 2.95 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
86
87
88
89
use std::sync::{Arc, Weak};
use crate::dma_manager::GlobalDmaManager;
use user_driver::dma;
// DMA Client structure, representing a specific client instance
pub struct DmaClient {
manager: Weak<GlobalDmaManager>,
}
impl DmaClient {
pub fn new(manager: Weak<GlobalDmaManager>) -> Self {
Self { manager }
}
fn pin_memory(&self, range: &MemoryRange) -> Result<usize, DmaError> {
let manager = self.manager.upgrade().ok_or(DmaError::InitializationFailed)?;
let threshold = manager.get_client_threshold(self).ok_or(DmaError::InitializationFailed)?;
if range.size <= threshold && manager.is_pinned(range) {
Ok(range.start)
} else {
Err(DmaError::PinFailed)
}
}
pub fn map_dma_ranges(
&self,
ranges: &[MemoryRange],
) -> Result<DmaTransactionHandler, DmaError> {
let manager = self.manager.upgrade().ok_or(DmaError::InitializationFailed)?;
let mut dma_transactions = Vec::new();
let threshold = manager.get_client_threshold(self).ok_or(DmaError::InitializationFailed)?;
for range in ranges {
let (dma_addr, is_pinned, is_bounce_buffer) = if range.size <= threshold {
match self.pin_memory(range) {
Ok(pinned_addr) => (pinned_addr, true, false),
Err(_) => {
let bounce_addr = manager.allocate_bounce_buffer(range.size)?;
(bounce_addr, false, true)
}
}
} else {
let bounce_addr = manager.allocate_bounce_buffer(range.size)?;
(bounce_addr, false, true)
};
dma_transactions.push(DmaTransaction {
original_addr: range.start,
dma_addr,
size: range.size,
is_pinned,
is_bounce_buffer,
is_physical: !is_bounce_buffer,
is_prepinned: manager.is_pinned(range),
});
}
Ok(DmaTransactionHandler {
transactions: dma_transactions,
})
}
pub fn unmap_dma_ranges(&self, dma_transactions: &[DmaTransaction]) -> Result<(), DmaError> {
let manager = self.manager.upgrade().ok_or(DmaError::InitializationFailed)?;
for transaction in dma_transactions {
if transaction.is_bounce_buffer {
// Code to release bounce buffer
} else if transaction.is_pinned && !transaction.is_prepinned {
// Code to unpin memory
}
}
Ok(())
}
}
// Implementation of the DMA interface for `DmaClient`
impl DmaInterface for DmaClient {
fn map_dma_ranges(&self, ranges: &[MemoryRange]) -> Result<DmaTransactionHandler, DmaError> {
self.map_dma_ranges(ranges)
}
fn unmap_dma_ranges(&self, dma_transactions: &[DmaTransaction]) -> Result<(), DmaError> {
self.unmap_dma_ranges(dma_transactions)
}
}