1#![doc = include_str!("./README.md")]
2
3#![allow(non_upper_case_globals)]
4#![allow(non_camel_case_types)]
5#![allow(non_snake_case)]
6
7#[allow(unused_imports)]
8#[allow(dead_code)]
9#[allow(clippy::all)]
10#[cfg(not(any(feature = "buildtime_bindgen", feature = "convert_amdgpu_ids")))]
11mod bindings {
12 #[cfg(feature = "link_drm")]
13 mod drm;
14 #[cfg(feature = "link_drm")]
15 pub use drm::*;
16
17 #[cfg(feature = "dynamic_loading")]
18 mod dyn_drm;
19 #[cfg(feature = "dynamic_loading")]
20 pub use dyn_drm::*;
21
22 #[cfg(feature = "dynamic_loading")]
23 mod dyn_drm_amdgpu;
24 #[cfg(feature = "dynamic_loading")]
25 pub use dyn_drm_amdgpu::*;
26
27 mod amdgpu_ids;
28 pub use amdgpu_ids::AMDGPU_IDS;
29 mod amdgpu_ids_2;
30 pub use amdgpu_ids_2::AMDGPU_IDS_2;
31
32 pub mod ppt {
33 pub mod smu_v11_0_0_ppt;
34 pub mod smu_v11_0_7_ppt;
35 pub mod smu_v13_0_0_ppt;
36 pub mod smu_v13_0_7_ppt;
37 }
38}
39
40#[cfg(feature = "dynamic_loading")]
41use std::sync::Arc;
42#[cfg(feature = "dynamic_loading")]
43use bindings::{DynLibDrm, DynLibDrmAmdgpu};
44
45#[cfg(feature = "dynamic_loading")]
46const LIBDRM_NAME: &str = "libdrm.so.2";
47#[cfg(feature = "dynamic_loading")]
48const LIBDRM_AMDGPU_NAME: &str = "libdrm_amdgpu.so.1";
49
50#[derive(Clone)]
51pub struct LibDrm {
52 #[cfg(feature = "dynamic_loading")]
53 pub(crate) libdrm: Arc<DynLibDrm>,
54}
55
56#[cfg(feature = "link_drm")]
57impl LibDrm {
58 pub fn new() -> Result<Self, ()> {
59 Ok(Self {})
60 }
61}
62
63#[cfg(feature = "dynamic_loading")]
64impl LibDrm {
65 pub fn new() -> Result<Self, ::libloading::Error> {
66 let libdrm = unsafe { Arc::new(DynLibDrm::new(LIBDRM_NAME)?) };
67
68 Ok(Self { libdrm })
69 }
70}
71
72impl From<LibDrmAmdgpu> for LibDrm {
73 fn from(_lib: LibDrmAmdgpu) -> Self {
74 Self {
75 #[cfg(feature = "dynamic_loading")]
76 libdrm: _lib.libdrm.clone(),
77 }
78 }
79}
80
81#[derive(Clone)]
82pub struct LibDrmAmdgpu {
83 #[cfg(feature = "dynamic_loading")]
84 pub(crate) libdrm: Arc<DynLibDrm>,
85 #[cfg(feature = "dynamic_loading")]
86 pub(crate) libdrm_amdgpu: Arc<DynLibDrmAmdgpu>,
87}
88
89#[cfg(feature = "link_drm")]
90impl LibDrmAmdgpu {
91 pub fn new() -> Result<Self, ()> {
92 Ok(Self {})
93 }
94
95 pub fn new_with_libdrm(_lib: LibDrm) -> Result<Self, ()> {
96 Ok(Self {})
97 }
98}
99
100#[cfg(feature = "dynamic_loading")]
101impl LibDrmAmdgpu {
102 pub fn new() -> Result<Self, ::libloading::Error> {
103 let libdrm = unsafe { Arc::new(DynLibDrm::new(LIBDRM_NAME)?) };
104 let libdrm_amdgpu = unsafe { Arc::new(DynLibDrmAmdgpu::new(LIBDRM_AMDGPU_NAME)?) };
105
106 Ok(Self { libdrm, libdrm_amdgpu })
107 }
108
109 pub fn new_with_libdrm(lib: LibDrm) -> Result<Self, ::libloading::Error> {
110 let libdrm_amdgpu = unsafe { Arc::new(DynLibDrmAmdgpu::new(LIBDRM_AMDGPU_NAME)?) };
111
112 Ok(Self { libdrm: lib.libdrm.clone(), libdrm_amdgpu })
113 }
114}
115
116#[cfg(not(any(feature = "buildtime_bindgen", feature = "convert_amdgpu_ids")))]
117mod amdgpu;
118#[cfg(not(any(feature = "buildtime_bindgen", feature = "convert_amdgpu_ids")))]
119pub mod AMDGPU {
120 pub use super::amdgpu::*;
121}
122
123#[cfg(not(any(feature = "buildtime_bindgen", feature = "convert_amdgpu_ids")))]
124mod pci;
125#[cfg(not(any(feature = "buildtime_bindgen", feature = "convert_amdgpu_ids")))]
126pub mod PCI {
127 pub use super::pci::*;
128}
129
130#[cfg(not(any(feature = "buildtime_bindgen", feature = "convert_amdgpu_ids")))]
131mod drm_version;
132#[cfg(not(any(feature = "buildtime_bindgen", feature = "convert_amdgpu_ids")))]
133pub use drm_version::*;
134
135#[cfg(not(any(feature = "buildtime_bindgen", feature = "convert_amdgpu_ids")))]
136mod drm_mode;
137#[cfg(not(any(feature = "buildtime_bindgen", feature = "convert_amdgpu_ids")))]
138pub use drm_mode::*;
139
140#[macro_export]
142macro_rules! query_error {
143 ($r: expr_2021) => {
144 if $r != 0 {
145 return Err($r);
146 }
147 };
148}
149
150#[cfg(not(any(feature = "buildtime_bindgen", feature = "convert_amdgpu_ids")))]
151use std::path::PathBuf;
152
153#[cfg(not(any(feature = "buildtime_bindgen", feature = "convert_amdgpu_ids")))]
154pub(crate) fn get_min_max_from_dpm<
155 T: std::cmp::Ord + std::marker::Copy,
156 P: Into<PathBuf>
157>(
158 sysfs_path: P,
159 parse: fn(&str) -> Option<T>,
160) -> Option<[T; 2]> {
161 let sysfs_path = sysfs_path.into();
162 let s = std::fs::read_to_string(sysfs_path).ok()?;
163 let mut lines = s.lines();
164
165 let first = parse(lines.next()?)?;
166 let last = match lines.last() {
167 Some(last) => parse(last)?,
168 None => return Some([first; 2]),
169 };
170
171 Some([
172 std::cmp::min(first, last),
173 std::cmp::max(first, last),
174 ])
175}