use crate::{
clk::Hertz,
cpumask::{Cpumask, CpumaskVar},
device::Device,
error::{code::*, from_err_ptr, from_result, to_result, Result, VTABLE_DEFAULT_ERROR},
ffi::{c_char, c_ulong},
prelude::*,
str::CString,
sync::aref::{ARef, AlwaysRefCounted},
types::Opaque,
};
#[cfg(CONFIG_CPU_FREQ)]
mod freq {
use super::*;
use crate::cpufreq;
use core::ops::Deref;
pub struct FreqTable {
dev: ARef<Device>,
ptr: *mut bindings::cpufreq_frequency_table,
}
impl FreqTable {
pub(crate) fn new(table: &Table) -> Result<Self> {
let mut ptr: *mut bindings::cpufreq_frequency_table = ptr::null_mut();
to_result(unsafe {
bindings::dev_pm_opp_init_cpufreq_table(table.dev.as_raw(), &mut ptr)
})?;
Ok(Self {
dev: table.dev.clone(),
ptr,
})
}
#[inline]
fn table(&self) -> &cpufreq::Table {
unsafe { cpufreq::Table::from_raw(self.ptr) }
}
}
impl Deref for FreqTable {
type Target = cpufreq::Table;
#[inline]
fn deref(&self) -> &Self::Target {
self.table()
}
}
impl Drop for FreqTable {
fn drop(&mut self) {
unsafe {
bindings::dev_pm_opp_free_cpufreq_table(self.dev.as_raw(), &mut self.as_raw())
};
}
}
}
#[cfg(CONFIG_CPU_FREQ)]
pub use freq::FreqTable;
use core::{marker::PhantomData, ptr};
use macros::vtable;
fn to_c_str_array(names: &[CString]) -> Result<KVec<*const c_char>> {
let mut list = KVec::with_capacity(names.len() + 1, GFP_KERNEL)?;
for name in names.iter() {
list.push(name.as_char_ptr(), GFP_KERNEL)?;
}
list.push(ptr::null(), GFP_KERNEL)?;
Ok(list)
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct MicroVolt(pub c_ulong);
impl From<MicroVolt> for c_ulong {
#[inline]
fn from(volt: MicroVolt) -> Self {
volt.0
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct MicroWatt(pub c_ulong);
impl From<MicroWatt> for c_ulong {
#[inline]
fn from(power: MicroWatt) -> Self {
power.0
}
}
pub struct Token {
dev: ARef<Device>,
freq: Hertz,
}
impl Token {
fn new(dev: &ARef<Device>, mut data: Data) -> Result<Self> {
to_result(unsafe { bindings::dev_pm_opp_add_dynamic(dev.as_raw(), &mut data.0) })?;
Ok(Self {
dev: dev.clone(),
freq: data.freq(),
})
}
}
impl Drop for Token {
fn drop(&mut self) {
unsafe { bindings::dev_pm_opp_remove(self.dev.as_raw(), self.freq.into()) };
}
}
#[repr(transparent)]
pub struct Data(bindings::dev_pm_opp_data);
impl Data {
pub fn new(freq: Hertz, volt: MicroVolt, level: u32, turbo: bool) -> Self {
Self(bindings::dev_pm_opp_data {
turbo,
freq: freq.into(),
u_volt: volt.into(),
level,
})
}
#[inline]
pub fn add_opp(self, dev: &ARef<Device>) -> Result<Token> {
Token::new(dev, self)
}
#[inline]
fn freq(&self) -> Hertz {
Hertz(self.0.freq)
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum SearchType {
Exact,
Floor,
Ceil,
}
#[vtable]
pub trait ConfigOps {
#[inline]
fn config_clks(_dev: &Device, _table: &Table, _opp: &OPP, _scaling_down: bool) -> Result {
build_error!(VTABLE_DEFAULT_ERROR)
}
#[inline]
fn config_regulators(
_dev: &Device,
_opp_old: &OPP,
_opp_new: &OPP,
_data: *mut *mut bindings::regulator,
_count: u32,
) -> Result {
build_error!(VTABLE_DEFAULT_ERROR)
}
}
pub struct ConfigToken(i32);
impl Drop for ConfigToken {
fn drop(&mut self) {
unsafe { bindings::dev_pm_opp_clear_config(self.0) };
}
}
#[derive(Default)]
pub struct Config<T: ConfigOps>
where
T: Default,
{
clk_names: Option<KVec<CString>>,
prop_name: Option<CString>,
regulator_names: Option<KVec<CString>>,
supported_hw: Option<KVec<u32>>,
required_dev: Option<(ARef<Device>, u32)>,
_data: PhantomData<T>,
}
impl<T: ConfigOps + Default> Config<T> {
#[inline]
pub fn new() -> Self {
Self::default()
}
pub fn set_clk_names(mut self, names: KVec<CString>) -> Result<Self> {
if self.clk_names.is_some() {
return Err(EBUSY);
}
if names.is_empty() {
return Err(EINVAL);
}
self.clk_names = Some(names);
Ok(self)
}
pub fn set_prop_name(mut self, name: CString) -> Result<Self> {
if self.prop_name.is_some() {
return Err(EBUSY);
}
self.prop_name = Some(name);
Ok(self)
}
pub fn set_regulator_names(mut self, names: KVec<CString>) -> Result<Self> {
if self.regulator_names.is_some() {
return Err(EBUSY);
}
if names.is_empty() {
return Err(EINVAL);
}
self.regulator_names = Some(names);
Ok(self)
}
pub fn set_required_dev(mut self, dev: ARef<Device>, index: u32) -> Result<Self> {
if self.required_dev.is_some() {
return Err(EBUSY);
}
self.required_dev = Some((dev, index));
Ok(self)
}
pub fn set_supported_hw(mut self, hw: KVec<u32>) -> Result<Self> {
if self.supported_hw.is_some() {
return Err(EBUSY);
}
if hw.is_empty() {
return Err(EINVAL);
}
self.supported_hw = Some(hw);
Ok(self)
}
pub fn set(self, dev: &Device) -> Result<ConfigToken> {
let clk_names = self.clk_names.as_deref().map(to_c_str_array).transpose()?;
let regulator_names = self
.regulator_names
.as_deref()
.map(to_c_str_array)
.transpose()?;
let set_config = || {
let clk_names = clk_names.as_ref().map_or(ptr::null(), |c| c.as_ptr());
let regulator_names = regulator_names.as_ref().map_or(ptr::null(), |c| c.as_ptr());
let prop_name = self
.prop_name
.as_ref()
.map_or(ptr::null(), |p| p.as_char_ptr());
let (supported_hw, supported_hw_count) = self
.supported_hw
.as_ref()
.map_or((ptr::null(), 0), |hw| (hw.as_ptr(), hw.len() as u32));
let (required_dev, required_dev_index) = self
.required_dev
.as_ref()
.map_or((ptr::null_mut(), 0), |(dev, idx)| (dev.as_raw(), *idx));
let mut config = bindings::dev_pm_opp_config {
clk_names,
config_clks: if T::HAS_CONFIG_CLKS {
Some(Self::config_clks)
} else {
None
},
prop_name,
regulator_names,
config_regulators: if T::HAS_CONFIG_REGULATORS {
Some(Self::config_regulators)
} else {
None
},
supported_hw,
supported_hw_count,
required_dev,
required_dev_index,
};
let ret = unsafe { bindings::dev_pm_opp_set_config(dev.as_raw(), &mut config) };
to_result(ret).map(|()| ConfigToken(ret))
};
let _: &dyn Fn() -> _ = &set_config;
set_config()
}
extern "C" fn config_clks(
dev: *mut bindings::device,
opp_table: *mut bindings::opp_table,
opp: *mut bindings::dev_pm_opp,
_data: *mut c_void,
scaling_down: bool,
) -> c_int {
from_result(|| {
let dev = unsafe { Device::get_device(dev) };
T::config_clks(
&dev,
&unsafe { Table::from_raw_table(opp_table, &dev) },
unsafe { OPP::from_raw_opp(opp)? },
scaling_down,
)
.map(|()| 0)
})
}
extern "C" fn config_regulators(
dev: *mut bindings::device,
old_opp: *mut bindings::dev_pm_opp,
new_opp: *mut bindings::dev_pm_opp,
regulators: *mut *mut bindings::regulator,
count: c_uint,
) -> c_int {
from_result(|| {
let dev = unsafe { Device::get_device(dev) };
T::config_regulators(
&dev,
unsafe { OPP::from_raw_opp(old_opp)? },
unsafe { OPP::from_raw_opp(new_opp)? },
regulators,
count,
)
.map(|()| 0)
})
}
}
pub struct Table {
ptr: *mut bindings::opp_table,
dev: ARef<Device>,
#[allow(dead_code)]
em: bool,
#[allow(dead_code)]
of: bool,
cpus: Option<CpumaskVar>,
}
unsafe impl Send for Table {}
unsafe impl Sync for Table {}
impl Table {
unsafe fn from_raw_table(ptr: *mut bindings::opp_table, dev: &ARef<Device>) -> Self {
unsafe { bindings::dev_pm_opp_get_opp_table_ref(ptr) };
Self {
ptr,
dev: dev.clone(),
em: false,
of: false,
cpus: None,
}
}
pub fn from_dev(dev: &Device) -> Result<Self> {
let ptr = from_err_ptr(unsafe { bindings::dev_pm_opp_get_opp_table(dev.as_raw()) })?;
Ok(Self {
ptr,
dev: dev.into(),
em: false,
of: false,
cpus: None,
})
}
#[cfg(CONFIG_OF)]
pub fn from_of(dev: &ARef<Device>, index: i32) -> Result<Self> {
to_result(unsafe { bindings::dev_pm_opp_of_add_table_indexed(dev.as_raw(), index) })?;
let mut table = Self::from_dev(dev)?;
table.of = true;
Ok(table)
}
#[cfg(CONFIG_OF)]
#[inline]
fn remove_of(&self) {
unsafe { bindings::dev_pm_opp_of_remove_table(self.dev.as_raw()) };
}
#[cfg(CONFIG_OF)]
pub fn from_of_cpumask(dev: &Device, cpumask: &mut Cpumask) -> Result<Self> {
to_result(unsafe { bindings::dev_pm_opp_of_cpumask_add_table(cpumask.as_raw()) })?;
let mut table = Self::from_dev(dev)?;
table.cpus = Some(CpumaskVar::try_clone(cpumask)?);
Ok(table)
}
#[cfg(CONFIG_OF)]
#[inline]
fn remove_of_cpumask(&self, cpumask: &Cpumask) {
unsafe { bindings::dev_pm_opp_of_cpumask_remove_table(cpumask.as_raw()) };
}
pub fn opp_count(&self) -> Result<u32> {
let ret = unsafe { bindings::dev_pm_opp_get_opp_count(self.dev.as_raw()) };
to_result(ret).map(|()| ret as u32)
}
#[inline]
pub fn max_clock_latency_ns(&self) -> usize {
unsafe { bindings::dev_pm_opp_get_max_clock_latency(self.dev.as_raw()) }
}
#[inline]
pub fn max_volt_latency_ns(&self) -> usize {
unsafe { bindings::dev_pm_opp_get_max_volt_latency(self.dev.as_raw()) }
}
#[inline]
pub fn max_transition_latency_ns(&self) -> usize {
unsafe { bindings::dev_pm_opp_get_max_transition_latency(self.dev.as_raw()) }
}
#[inline]
pub fn suspend_freq(&self) -> Hertz {
Hertz(unsafe { bindings::dev_pm_opp_get_suspend_opp_freq(self.dev.as_raw()) })
}
#[inline]
pub fn sync_regulators(&self) -> Result {
to_result(unsafe { bindings::dev_pm_opp_sync_regulators(self.dev.as_raw()) })
}
#[inline]
pub fn sharing_cpus(dev: &Device, cpumask: &mut Cpumask) -> Result {
to_result(unsafe { bindings::dev_pm_opp_get_sharing_cpus(dev.as_raw(), cpumask.as_raw()) })
}
pub fn set_sharing_cpus(&mut self, cpumask: &mut Cpumask) -> Result {
to_result(unsafe {
bindings::dev_pm_opp_set_sharing_cpus(self.dev.as_raw(), cpumask.as_raw())
})?;
if let Some(mask) = self.cpus.as_mut() {
cpumask.copy(mask);
}
Ok(())
}
#[cfg(CONFIG_OF)]
#[inline]
pub fn of_sharing_cpus(dev: &Device, cpumask: &mut Cpumask) -> Result {
to_result(unsafe {
bindings::dev_pm_opp_of_get_sharing_cpus(dev.as_raw(), cpumask.as_raw())
})
}
#[inline]
pub fn adjust_voltage(
&self,
freq: Hertz,
volt: MicroVolt,
volt_min: MicroVolt,
volt_max: MicroVolt,
) -> Result {
to_result(unsafe {
bindings::dev_pm_opp_adjust_voltage(
self.dev.as_raw(),
freq.into(),
volt.into(),
volt_min.into(),
volt_max.into(),
)
})
}
#[cfg(CONFIG_CPU_FREQ)]
#[inline]
pub fn cpufreq_table(&mut self) -> Result<FreqTable> {
FreqTable::new(self)
}
#[inline]
pub fn set_rate(&self, freq: Hertz) -> Result {
to_result(unsafe { bindings::dev_pm_opp_set_rate(self.dev.as_raw(), freq.into()) })
}
#[inline]
pub fn set_opp(&self, opp: &OPP) -> Result {
to_result(unsafe { bindings::dev_pm_opp_set_opp(self.dev.as_raw(), opp.as_raw()) })
}
pub fn opp_from_freq(
&self,
freq: Hertz,
available: Option<bool>,
index: Option<u32>,
stype: SearchType,
) -> Result<ARef<OPP>> {
let raw_dev = self.dev.as_raw();
let index = index.unwrap_or(0);
let mut rate = freq.into();
let ptr = from_err_ptr(match stype {
SearchType::Exact => {
if let Some(available) = available {
unsafe {
bindings::dev_pm_opp_find_freq_exact_indexed(
raw_dev, rate, index, available,
)
}
} else {
return Err(EINVAL);
}
}
SearchType::Ceil => unsafe {
bindings::dev_pm_opp_find_freq_ceil_indexed(raw_dev, &mut rate, index)
},
SearchType::Floor => unsafe {
bindings::dev_pm_opp_find_freq_floor_indexed(raw_dev, &mut rate, index)
},
})?;
unsafe { OPP::from_raw_opp_owned(ptr) }
}
pub fn opp_from_level(&self, mut level: u32, stype: SearchType) -> Result<ARef<OPP>> {
let raw_dev = self.dev.as_raw();
let ptr = from_err_ptr(match stype {
SearchType::Exact => unsafe { bindings::dev_pm_opp_find_level_exact(raw_dev, level) },
SearchType::Ceil => unsafe {
bindings::dev_pm_opp_find_level_ceil(raw_dev, &mut level)
},
SearchType::Floor => unsafe {
bindings::dev_pm_opp_find_level_floor(raw_dev, &mut level)
},
})?;
unsafe { OPP::from_raw_opp_owned(ptr) }
}
pub fn opp_from_bw(&self, mut bw: u32, index: i32, stype: SearchType) -> Result<ARef<OPP>> {
let raw_dev = self.dev.as_raw();
let ptr = from_err_ptr(match stype {
SearchType::Exact => return Err(EINVAL),
SearchType::Ceil => unsafe {
bindings::dev_pm_opp_find_bw_ceil(raw_dev, &mut bw, index)
},
SearchType::Floor => unsafe {
bindings::dev_pm_opp_find_bw_floor(raw_dev, &mut bw, index)
},
})?;
unsafe { OPP::from_raw_opp_owned(ptr) }
}
#[inline]
pub fn enable_opp(&self, freq: Hertz) -> Result {
to_result(unsafe { bindings::dev_pm_opp_enable(self.dev.as_raw(), freq.into()) })
}
#[inline]
pub fn disable_opp(&self, freq: Hertz) -> Result {
to_result(unsafe { bindings::dev_pm_opp_disable(self.dev.as_raw(), freq.into()) })
}
#[cfg(CONFIG_OF)]
pub fn of_register_em(&mut self, cpumask: &mut Cpumask) -> Result {
to_result(unsafe {
bindings::dev_pm_opp_of_register_em(self.dev.as_raw(), cpumask.as_raw())
})?;
self.em = true;
Ok(())
}
#[cfg(all(CONFIG_OF, CONFIG_ENERGY_MODEL))]
#[inline]
fn of_unregister_em(&self) {
unsafe { bindings::em_dev_unregister_perf_domain(self.dev.as_raw()) };
}
}
impl Drop for Table {
fn drop(&mut self) {
unsafe { bindings::dev_pm_opp_put_opp_table(self.ptr) };
#[cfg(CONFIG_OF)]
{
#[cfg(CONFIG_ENERGY_MODEL)]
if self.em {
self.of_unregister_em();
}
if self.of {
self.remove_of();
} else if let Some(cpumask) = self.cpus.take() {
self.remove_of_cpumask(&cpumask);
}
}
}
}
#[repr(transparent)]
pub struct OPP(Opaque<bindings::dev_pm_opp>);
unsafe impl Send for OPP {}
unsafe impl Sync for OPP {}
unsafe impl AlwaysRefCounted for OPP {
fn inc_ref(&self) {
unsafe { bindings::dev_pm_opp_get(self.0.get()) };
}
unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
unsafe { bindings::dev_pm_opp_put(obj.cast().as_ptr()) }
}
}
impl OPP {
pub unsafe fn from_raw_opp_owned(ptr: *mut bindings::dev_pm_opp) -> Result<ARef<Self>> {
let ptr = ptr::NonNull::new(ptr).ok_or(ENODEV)?;
Ok(unsafe { ARef::from_raw(ptr.cast()) })
}
#[inline]
pub unsafe fn from_raw_opp<'a>(ptr: *mut bindings::dev_pm_opp) -> Result<&'a Self> {
Ok(unsafe { &*ptr.cast() })
}
#[inline]
fn as_raw(&self) -> *mut bindings::dev_pm_opp {
self.0.get()
}
pub fn freq(&self, index: Option<u32>) -> Hertz {
let index = index.unwrap_or(0);
Hertz(unsafe { bindings::dev_pm_opp_get_freq_indexed(self.as_raw(), index) })
}
#[inline]
pub fn voltage(&self) -> MicroVolt {
MicroVolt(unsafe { bindings::dev_pm_opp_get_voltage(self.as_raw()) })
}
#[inline]
pub fn level(&self) -> u32 {
unsafe { bindings::dev_pm_opp_get_level(self.as_raw()) }
}
#[inline]
pub fn power(&self) -> MicroWatt {
MicroWatt(unsafe { bindings::dev_pm_opp_get_power(self.as_raw()) })
}
#[inline]
pub fn required_pstate(&self, index: u32) -> u32 {
unsafe { bindings::dev_pm_opp_get_required_pstate(self.as_raw(), index) }
}
#[inline]
pub fn is_turbo(&self) -> bool {
unsafe { bindings::dev_pm_opp_is_turbo(self.as_raw()) }
}
}