-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlib.rs
175 lines (146 loc) · 4.52 KB
/
lib.rs
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#![doc = include_str!("./README.md")]
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#[allow(unused_imports)]
#[allow(dead_code)]
#[allow(clippy::all)]
#[cfg(not(any(feature = "buildtime_bindgen", feature = "convert_amdgpu_ids")))]
mod bindings {
#[cfg(feature = "link_drm")]
mod drm;
#[cfg(feature = "link_drm")]
pub use drm::*;
#[cfg(feature = "dynamic_loading")]
mod dyn_drm;
#[cfg(feature = "dynamic_loading")]
pub use dyn_drm::*;
#[cfg(feature = "dynamic_loading")]
mod dyn_drm_amdgpu;
#[cfg(feature = "dynamic_loading")]
pub use dyn_drm_amdgpu::*;
mod amdgpu_ids;
pub use amdgpu_ids::AMDGPU_IDS;
mod amdgpu_ids_2;
pub use amdgpu_ids_2::AMDGPU_IDS_2;
pub mod ppt {
pub mod smu_v11_0_0_ppt;
pub mod smu_v11_0_7_ppt;
pub mod smu_v13_0_0_ppt;
pub mod smu_v13_0_7_ppt;
}
}
#[cfg(feature = "dynamic_loading")]
use std::sync::Arc;
#[cfg(feature = "dynamic_loading")]
use bindings::{DynLibDrm, DynLibDrmAmdgpu};
#[cfg(feature = "dynamic_loading")]
const LIBDRM_NAME: &str = "libdrm.so.2";
#[cfg(feature = "dynamic_loading")]
const LIBDRM_AMDGPU_NAME: &str = "libdrm_amdgpu.so.1";
#[derive(Clone)]
pub struct LibDrm {
#[cfg(feature = "dynamic_loading")]
pub(crate) libdrm: Arc<DynLibDrm>,
}
#[cfg(feature = "link_drm")]
impl LibDrm {
pub fn new() -> Result<Self, ()> {
Ok(Self {})
}
}
#[cfg(feature = "dynamic_loading")]
impl LibDrm {
pub fn new() -> Result<Self, ::libloading::Error> {
let libdrm = unsafe { Arc::new(DynLibDrm::new(LIBDRM_NAME)?) };
Ok(Self { libdrm })
}
}
impl From<LibDrmAmdgpu> for LibDrm {
fn from(_lib: LibDrmAmdgpu) -> Self {
Self {
#[cfg(feature = "dynamic_loading")]
libdrm: _lib.libdrm.clone(),
}
}
}
#[derive(Clone)]
pub struct LibDrmAmdgpu {
#[cfg(feature = "dynamic_loading")]
pub(crate) libdrm: Arc<DynLibDrm>,
#[cfg(feature = "dynamic_loading")]
pub(crate) libdrm_amdgpu: Arc<DynLibDrmAmdgpu>,
}
#[cfg(feature = "link_drm")]
impl LibDrmAmdgpu {
pub fn new() -> Result<Self, ()> {
Ok(Self {})
}
pub fn new_with_libdrm(_lib: LibDrm) -> Result<Self, ()> {
Ok(Self {})
}
}
#[cfg(feature = "dynamic_loading")]
impl LibDrmAmdgpu {
pub fn new() -> Result<Self, ::libloading::Error> {
let libdrm = unsafe { Arc::new(DynLibDrm::new(LIBDRM_NAME)?) };
let libdrm_amdgpu = unsafe { Arc::new(DynLibDrmAmdgpu::new(LIBDRM_AMDGPU_NAME)?) };
Ok(Self { libdrm, libdrm_amdgpu })
}
pub fn new_with_libdrm(lib: LibDrm) -> Result<Self, ::libloading::Error> {
let libdrm_amdgpu = unsafe { Arc::new(DynLibDrmAmdgpu::new(LIBDRM_AMDGPU_NAME)?) };
Ok(Self { libdrm: lib.libdrm.clone(), libdrm_amdgpu })
}
}
#[cfg(not(any(feature = "buildtime_bindgen", feature = "convert_amdgpu_ids")))]
mod amdgpu;
#[cfg(not(any(feature = "buildtime_bindgen", feature = "convert_amdgpu_ids")))]
pub mod AMDGPU {
pub use super::amdgpu::*;
}
#[cfg(not(any(feature = "buildtime_bindgen", feature = "convert_amdgpu_ids")))]
mod pci;
#[cfg(not(any(feature = "buildtime_bindgen", feature = "convert_amdgpu_ids")))]
pub mod PCI {
pub use super::pci::*;
}
#[cfg(not(any(feature = "buildtime_bindgen", feature = "convert_amdgpu_ids")))]
mod drm_version;
#[cfg(not(any(feature = "buildtime_bindgen", feature = "convert_amdgpu_ids")))]
pub use drm_version::*;
#[cfg(not(any(feature = "buildtime_bindgen", feature = "convert_amdgpu_ids")))]
mod drm_mode;
#[cfg(not(any(feature = "buildtime_bindgen", feature = "convert_amdgpu_ids")))]
pub use drm_mode::*;
/// Convert `errno` to `Err(i32)`
#[macro_export]
macro_rules! query_error {
($r: expr_2021) => {
if $r != 0 {
return Err($r);
}
};
}
#[cfg(not(any(feature = "buildtime_bindgen", feature = "convert_amdgpu_ids")))]
use std::path::PathBuf;
#[cfg(not(any(feature = "buildtime_bindgen", feature = "convert_amdgpu_ids")))]
pub(crate) fn get_min_max_from_dpm<
T: std::cmp::Ord + std::marker::Copy,
P: Into<PathBuf>
>(
sysfs_path: P,
parse: fn(&str) -> Option<T>,
) -> Option<[T; 2]> {
let sysfs_path = sysfs_path.into();
let s = std::fs::read_to_string(sysfs_path).ok()?;
let mut lines = s.lines();
let first = parse(lines.next()?)?;
let last = match lines.last() {
Some(last) => parse(last)?,
None => return Some([first; 2]),
};
Some([
std::cmp::min(first, last),
std::cmp::max(first, last),
])
}