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
#![doc = include_str!("./README.md")]

#![cfg_attr(not(feature = "std"), no_std)]

#![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(feature = "buildtime_bindgen"))]
mod bindings {
    mod drm;
    pub use drm::*;

    mod amdgpu_ids;
    pub use amdgpu_ids::AMDGPU_IDS;

    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(not(feature = "buildtime_bindgen"))]
mod amdgpu;
#[cfg(not(feature = "buildtime_bindgen"))]
pub mod AMDGPU {
    pub use super::amdgpu::*;
}

#[cfg(not(feature = "buildtime_bindgen"))]
mod pci;
#[cfg(not(feature = "buildtime_bindgen"))]
pub mod PCI {
    pub use super::pci::*;
}

#[cfg(not(feature = "buildtime_bindgen"))]
mod drm_version;
#[cfg(not(feature = "buildtime_bindgen"))]
pub use drm_version::*;

#[cfg(not(feature = "buildtime_bindgen"))]
#[cfg(feature = "std")]
mod drm_mode;
#[cfg(feature = "std")]
#[cfg(not(feature = "buildtime_bindgen"))]
pub use drm_mode::*;

/// Convert `errno` to `Err(i32)`
#[macro_export]
macro_rules! query_error {
    ($r: expr) => {
        if $r != 0 {
            return Err($r);
        }
    };
}

#[cfg(feature = "std")]
use std::path::PathBuf;

#[cfg(feature = "std")]
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),
    ])
}