forked from syswonder/ruxos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
64 lines (57 loc) · 2.11 KB
/
Copy pathlib.rs
File metadata and controls
64 lines (57 loc) · 2.11 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
/* Copyright (c) [2023] [Syswonder Community]
* [Ruxos] is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
//! Structures and functions for PCI bus operations.
//!
//! Currently, it just re-exports structures from the crate [virtio-drivers][1]
//! and its module [`virtio_drivers::transport::pci::bus`][2].
//!
//! [1]: https://docs.rs/virtio-drivers/latest/virtio_drivers/
//! [2]: https://docs.rs/virtio-drivers/latest/virtio_drivers/transport/pci/bus/index.html
#![no_std]
pub use virtio_drivers::transport::pci::bus::ConfigurationAccess;
pub use virtio_drivers::transport::pci::bus::{
BarInfo, Cam, HeaderType, MemoryBarType, MmioCam, PciError,
};
pub use virtio_drivers::transport::pci::bus::{
CapabilityInfo, Command, DeviceFunction, DeviceFunctionInfo, PciRoot, Status,
};
/// Used to allocate MMIO regions for PCI BARs.
pub struct PciRangeAllocator {
_start: u64,
end: u64,
current: u64,
}
impl PciRangeAllocator {
/// Creates a new allocator from a memory range.
pub const fn new(base: u64, size: u64) -> Self {
Self {
_start: base,
end: base + size,
current: base,
}
}
/// Allocates a memory region with the given size.
///
/// The `size` should be a power of 2, and the returned value is also a
/// multiple of `size`.
pub fn alloc(&mut self, size: u64) -> Option<u64> {
if !size.is_power_of_two() {
return None;
}
let ret = align_up(self.current, size);
if ret + size > self.end {
return None;
}
self.current = ret + size;
Some(ret)
}
}
const fn align_up(addr: u64, align: u64) -> u64 {
(addr + align - 1) & !(align - 1)
}