Skip to main content

kernel/
cpufreq.rs

1// SPDX-License-Identifier: GPL-2.0
2
3//! CPU frequency scaling.
4//!
5//! This module provides rust abstractions for interacting with the cpufreq subsystem.
6//!
7//! C header: [`include/linux/cpufreq.h`](srctree/include/linux/cpufreq.h)
8//!
9//! Reference: <https://docs.kernel.org/admin-guide/pm/cpufreq.html>
10
11use crate::{
12    clk::Hertz,
13    cpu::CpuId,
14    cpumask,
15    device::{Bound, Device},
16    devres,
17    error::{code::*, from_err_ptr, from_result, to_result, Result, VTABLE_DEFAULT_ERROR},
18    ffi::{c_char, c_ulong},
19    prelude::*,
20    types::ForeignOwnable,
21    types::Opaque,
22};
23
24#[cfg(CONFIG_COMMON_CLK)]
25use crate::clk::Clk;
26
27use core::{
28    cell::UnsafeCell,
29    marker::PhantomData,
30    ops::{Deref, DerefMut},
31    pin::Pin,
32    ptr,
33};
34
35use macros::vtable;
36
37/// Maximum length of CPU frequency driver's name.
38const CPUFREQ_NAME_LEN: usize = bindings::CPUFREQ_NAME_LEN as usize;
39
40/// Default transition latency value in nanoseconds.
41pub const DEFAULT_TRANSITION_LATENCY_NS: u32 = bindings::CPUFREQ_DEFAULT_TRANSITION_LATENCY_NS;
42
43/// CPU frequency driver flags.
44pub mod flags {
45    /// Driver needs to update internal limits even if frequency remains unchanged.
46    pub const NEED_UPDATE_LIMITS: u16 = 1 << 0;
47
48    /// Platform where constants like `loops_per_jiffy` are unaffected by frequency changes.
49    pub const CONST_LOOPS: u16 = 1 << 1;
50
51    /// Register driver as a thermal cooling device automatically.
52    pub const IS_COOLING_DEV: u16 = 1 << 2;
53
54    /// Supports multiple clock domains with per-policy governors in `cpu/cpuN/cpufreq/`.
55    pub const HAVE_GOVERNOR_PER_POLICY: u16 = 1 << 3;
56
57    /// Allows post-change notifications outside of the `target()` routine.
58    pub const ASYNC_NOTIFICATION: u16 = 1 << 4;
59
60    /// Ensure CPU starts at a valid frequency from the driver's freq-table.
61    pub const NEED_INITIAL_FREQ_CHECK: u16 = 1 << 5;
62
63    /// Disallow governors with `dynamic_switching` capability.
64    pub const NO_AUTO_DYNAMIC_SWITCHING: u16 = 1 << 6;
65}
66
67/// Relations from the C code.
68const CPUFREQ_RELATION_L: u32 = 0;
69const CPUFREQ_RELATION_H: u32 = 1;
70const CPUFREQ_RELATION_C: u32 = 2;
71
72/// Can be used with any of the above values.
73const CPUFREQ_RELATION_E: u32 = 1 << 2;
74
75/// CPU frequency selection relations.
76///
77/// CPU frequency selection relations, each optionally marked as "efficient".
78#[derive(Copy, Clone, Debug, Eq, PartialEq)]
79pub enum Relation {
80    /// Select the lowest frequency at or above target.
81    Low(bool),
82    /// Select the highest frequency below or at target.
83    High(bool),
84    /// Select the closest frequency to the target.
85    Close(bool),
86}
87
88impl Relation {
89    // Construct from a C-compatible `u32` value.
90    fn new(val: u32) -> Result<Self> {
91        let efficient = val & CPUFREQ_RELATION_E != 0;
92
93        Ok(match val & !CPUFREQ_RELATION_E {
94            CPUFREQ_RELATION_L => Self::Low(efficient),
95            CPUFREQ_RELATION_H => Self::High(efficient),
96            CPUFREQ_RELATION_C => Self::Close(efficient),
97            _ => return Err(EINVAL),
98        })
99    }
100}
101
102impl From<Relation> for u32 {
103    // Convert to a C-compatible `u32` value.
104    fn from(rel: Relation) -> Self {
105        let (mut val, efficient) = match rel {
106            Relation::Low(e) => (CPUFREQ_RELATION_L, e),
107            Relation::High(e) => (CPUFREQ_RELATION_H, e),
108            Relation::Close(e) => (CPUFREQ_RELATION_C, e),
109        };
110
111        if efficient {
112            val |= CPUFREQ_RELATION_E;
113        }
114
115        val
116    }
117}
118
119/// Policy data.
120///
121/// Rust abstraction for the C `struct cpufreq_policy_data`.
122///
123/// # Invariants
124///
125/// A [`PolicyData`] instance always corresponds to a valid C `struct cpufreq_policy_data`.
126///
127/// The callers must ensure that the `struct cpufreq_policy_data` is valid for access and remains
128/// valid for the lifetime of the returned reference.
129#[repr(transparent)]
130pub struct PolicyData(Opaque<bindings::cpufreq_policy_data>);
131
132impl PolicyData {
133    /// Creates a mutable reference to an existing `struct cpufreq_policy_data` pointer.
134    ///
135    /// # Safety
136    ///
137    /// The caller must ensure that `ptr` is valid for writing and remains valid for the lifetime
138    /// of the returned reference.
139    #[inline]
140    pub unsafe fn from_raw_mut<'a>(ptr: *mut bindings::cpufreq_policy_data) -> &'a mut Self {
141        // SAFETY: Guaranteed by the safety requirements of the function.
142        //
143        // INVARIANT: The caller ensures that `ptr` is valid for writing and remains valid for the
144        // lifetime of the returned reference.
145        unsafe { &mut *ptr.cast() }
146    }
147
148    /// Returns a raw pointer to the underlying C `cpufreq_policy_data`.
149    #[inline]
150    pub fn as_raw(&self) -> *mut bindings::cpufreq_policy_data {
151        let this: *const Self = self;
152        this.cast_mut().cast()
153    }
154
155    /// Wrapper for `cpufreq_generic_frequency_table_verify`.
156    #[inline]
157    pub fn generic_verify(&self) -> Result {
158        // SAFETY: By the type invariant, the pointer stored in `self` is valid.
159        to_result(unsafe { bindings::cpufreq_generic_frequency_table_verify(self.as_raw()) })
160    }
161}
162
163/// The frequency table index.
164///
165/// Represents index with a frequency table.
166///
167/// # Invariants
168///
169/// The index must correspond to a valid entry in the [`Table`] it is used for.
170#[derive(Copy, Clone, PartialEq, Eq, Debug)]
171pub struct TableIndex(usize);
172
173impl TableIndex {
174    /// Creates an instance of [`TableIndex`].
175    ///
176    /// # Safety
177    ///
178    /// The caller must ensure that `index` correspond to a valid entry in the [`Table`] it is used
179    /// for.
180    pub unsafe fn new(index: usize) -> Self {
181        // INVARIANT: The caller ensures that `index` correspond to a valid entry in the [`Table`].
182        Self(index)
183    }
184}
185
186impl From<TableIndex> for usize {
187    #[inline]
188    fn from(index: TableIndex) -> Self {
189        index.0
190    }
191}
192
193/// CPU frequency table.
194///
195/// Rust abstraction for the C `struct cpufreq_frequency_table`.
196///
197/// # Invariants
198///
199/// A [`Table`] instance always corresponds to a valid C `struct cpufreq_frequency_table`.
200///
201/// The callers must ensure that the `struct cpufreq_frequency_table` is valid for access and
202/// remains valid for the lifetime of the returned reference.
203///
204/// # Examples
205///
206/// The following example demonstrates how to read a frequency value from [`Table`].
207///
208/// ```
209/// use kernel::cpufreq::{Policy, TableIndex};
210///
211/// fn show_freq(policy: &Policy) -> Result {
212///     let table = policy.freq_table()?;
213///
214///     // SAFETY: Index is a valid entry in the table.
215///     let index = unsafe { TableIndex::new(0) };
216///
217///     pr_info!("The frequency at index 0 is: {:?}\n", table.freq(index)?);
218///     pr_info!("The flags at index 0 is: {}\n", table.flags(index));
219///     pr_info!("The data at index 0 is: {}\n", table.data(index));
220///     Ok(())
221/// }
222/// ```
223#[repr(transparent)]
224pub struct Table(Opaque<bindings::cpufreq_frequency_table>);
225
226impl Table {
227    /// Creates a reference to an existing C `struct cpufreq_frequency_table` pointer.
228    ///
229    /// # Safety
230    ///
231    /// The caller must ensure that `ptr` is valid for reading and remains valid for the lifetime
232    /// of the returned reference.
233    #[inline]
234    pub unsafe fn from_raw<'a>(ptr: *const bindings::cpufreq_frequency_table) -> &'a Self {
235        // SAFETY: Guaranteed by the safety requirements of the function.
236        //
237        // INVARIANT: The caller ensures that `ptr` is valid for reading and remains valid for the
238        // lifetime of the returned reference.
239        unsafe { &*ptr.cast() }
240    }
241
242    /// Returns the raw mutable pointer to the C `struct cpufreq_frequency_table`.
243    #[inline]
244    pub fn as_raw(&self) -> *mut bindings::cpufreq_frequency_table {
245        let this: *const Self = self;
246        this.cast_mut().cast()
247    }
248
249    /// Returns frequency at `index` in the [`Table`].
250    #[inline]
251    pub fn freq(&self, index: TableIndex) -> Result<Hertz> {
252        // SAFETY: By the type invariant, the pointer stored in `self` is valid and `index` is
253        // guaranteed to be valid by its safety requirements.
254        Ok(Hertz::from_khz(unsafe {
255            (*self.as_raw().add(index.into())).frequency.try_into()?
256        }))
257    }
258
259    /// Returns flags at `index` in the [`Table`].
260    #[inline]
261    pub fn flags(&self, index: TableIndex) -> u32 {
262        // SAFETY: By the type invariant, the pointer stored in `self` is valid and `index` is
263        // guaranteed to be valid by its safety requirements.
264        unsafe { (*self.as_raw().add(index.into())).flags }
265    }
266
267    /// Returns data at `index` in the [`Table`].
268    #[inline]
269    pub fn data(&self, index: TableIndex) -> u32 {
270        // SAFETY: By the type invariant, the pointer stored in `self` is valid and `index` is
271        // guaranteed to be valid by its safety requirements.
272        unsafe { (*self.as_raw().add(index.into())).driver_data }
273    }
274}
275
276/// CPU frequency table owned and pinned in memory, created from a [`TableBuilder`].
277pub struct TableBox {
278    entries: Pin<KVec<bindings::cpufreq_frequency_table>>,
279}
280
281impl TableBox {
282    /// Constructs a new [`TableBox`] from a [`KVec`] of entries.
283    ///
284    /// # Errors
285    ///
286    /// Returns `EINVAL` if the entries list is empty.
287    #[inline]
288    fn new(entries: KVec<bindings::cpufreq_frequency_table>) -> Result<Self> {
289        if entries.is_empty() {
290            return Err(EINVAL);
291        }
292
293        Ok(Self {
294            // Pin the entries to memory, since we are passing its pointer to the C code.
295            entries: Pin::new(entries),
296        })
297    }
298
299    /// Returns a raw pointer to the underlying C `cpufreq_frequency_table`.
300    #[inline]
301    fn as_raw(&self) -> *const bindings::cpufreq_frequency_table {
302        // The pointer is valid until the table gets dropped.
303        self.entries.as_ptr()
304    }
305}
306
307impl Deref for TableBox {
308    type Target = Table;
309
310    fn deref(&self) -> &Self::Target {
311        // SAFETY: The caller owns TableBox, it is safe to deref.
312        unsafe { Self::Target::from_raw(self.as_raw()) }
313    }
314}
315
316/// CPU frequency table builder.
317///
318/// This is used by the CPU frequency drivers to build a frequency table dynamically.
319///
320/// # Examples
321///
322/// The following example demonstrates how to create a CPU frequency table.
323///
324/// ```
325/// use kernel::cpufreq::{TableBuilder, TableIndex};
326/// use kernel::clk::Hertz;
327///
328/// let mut builder = TableBuilder::new();
329///
330/// // Adds few entries to the table.
331/// builder.add(Hertz::from_mhz(700), 0, 1).unwrap();
332/// builder.add(Hertz::from_mhz(800), 2, 3).unwrap();
333/// builder.add(Hertz::from_mhz(900), 4, 5).unwrap();
334/// builder.add(Hertz::from_ghz(1), 6, 7).unwrap();
335///
336/// let table = builder.to_table().unwrap();
337///
338/// // SAFETY: Index values correspond to valid entries in the table.
339/// let (index0, index2) = unsafe { (TableIndex::new(0), TableIndex::new(2)) };
340///
341/// assert_eq!(table.freq(index0), Ok(Hertz::from_mhz(700)));
342/// assert_eq!(table.flags(index0), 0);
343/// assert_eq!(table.data(index0), 1);
344///
345/// assert_eq!(table.freq(index2), Ok(Hertz::from_mhz(900)));
346/// assert_eq!(table.flags(index2), 4);
347/// assert_eq!(table.data(index2), 5);
348/// ```
349#[derive(Default)]
350#[repr(transparent)]
351pub struct TableBuilder {
352    entries: KVec<bindings::cpufreq_frequency_table>,
353}
354
355impl TableBuilder {
356    /// Creates a new instance of [`TableBuilder`].
357    #[inline]
358    pub fn new() -> Self {
359        Self {
360            entries: KVec::new(),
361        }
362    }
363
364    /// Adds a new entry to the table.
365    pub fn add(&mut self, freq: Hertz, flags: u32, driver_data: u32) -> Result {
366        // Adds the new entry at the end of the vector.
367        Ok(self.entries.push(
368            bindings::cpufreq_frequency_table {
369                flags,
370                driver_data,
371                frequency: freq.as_khz() as u32,
372            },
373            GFP_KERNEL,
374        )?)
375    }
376
377    /// Consumes the [`TableBuilder`] and returns [`TableBox`].
378    pub fn to_table(mut self) -> Result<TableBox> {
379        // Add last entry to the table.
380        self.add(Hertz(c_ulong::MAX), 0, 0)?;
381
382        TableBox::new(self.entries)
383    }
384}
385
386/// CPU frequency policy.
387///
388/// Rust abstraction for the C `struct cpufreq_policy`.
389///
390/// # Invariants
391///
392/// A [`Policy`] instance always corresponds to a valid C `struct cpufreq_policy`.
393///
394/// The callers must ensure that the `struct cpufreq_policy` is valid for access and remains valid
395/// for the lifetime of the returned reference.
396///
397/// # Examples
398///
399/// The following example demonstrates how to create a CPU frequency table.
400///
401/// ```
402/// use kernel::cpufreq::{DEFAULT_TRANSITION_LATENCY_NS, Policy};
403///
404/// #[allow(clippy::double_parens, reason = "False positive before 1.92.0")]
405/// fn update_policy(policy: &mut Policy) {
406///     policy
407///         .set_dvfs_possible_from_any_cpu(true)
408///         .set_fast_switch_possible(true)
409///         .set_transition_latency_ns(DEFAULT_TRANSITION_LATENCY_NS);
410///
411///     pr_info!("The policy details are: {:?}\n", (policy.cpu(), policy.cur()));
412/// }
413/// ```
414#[repr(transparent)]
415pub struct Policy(Opaque<bindings::cpufreq_policy>);
416
417impl Policy {
418    /// Creates a reference to an existing `struct cpufreq_policy` pointer.
419    ///
420    /// # Safety
421    ///
422    /// The caller must ensure that `ptr` is valid for reading and remains valid for the lifetime
423    /// of the returned reference.
424    #[inline]
425    pub unsafe fn from_raw<'a>(ptr: *const bindings::cpufreq_policy) -> &'a Self {
426        // SAFETY: Guaranteed by the safety requirements of the function.
427        //
428        // INVARIANT: The caller ensures that `ptr` is valid for reading and remains valid for the
429        // lifetime of the returned reference.
430        unsafe { &*ptr.cast() }
431    }
432
433    /// Creates a mutable reference to an existing `struct cpufreq_policy` pointer.
434    ///
435    /// # Safety
436    ///
437    /// The caller must ensure that `ptr` is valid for writing and remains valid for the lifetime
438    /// of the returned reference.
439    #[inline]
440    pub unsafe fn from_raw_mut<'a>(ptr: *mut bindings::cpufreq_policy) -> &'a mut Self {
441        // SAFETY: Guaranteed by the safety requirements of the function.
442        //
443        // INVARIANT: The caller ensures that `ptr` is valid for writing and remains valid for the
444        // lifetime of the returned reference.
445        unsafe { &mut *ptr.cast() }
446    }
447
448    /// Returns a raw mutable pointer to the C `struct cpufreq_policy`.
449    #[inline]
450    fn as_raw(&self) -> *mut bindings::cpufreq_policy {
451        let this: *const Self = self;
452        this.cast_mut().cast()
453    }
454
455    #[inline]
456    fn as_ref(&self) -> &bindings::cpufreq_policy {
457        // SAFETY: By the type invariant, the pointer stored in `self` is valid.
458        unsafe { &*self.as_raw() }
459    }
460
461    #[inline]
462    fn as_mut_ref(&mut self) -> &mut bindings::cpufreq_policy {
463        // SAFETY: By the type invariant, the pointer stored in `self` is valid.
464        unsafe { &mut *self.as_raw() }
465    }
466
467    /// Returns the primary CPU for the [`Policy`].
468    #[inline]
469    pub fn cpu(&self) -> CpuId {
470        // SAFETY: The C API guarantees that `cpu` refers to a valid CPU number.
471        unsafe { CpuId::from_u32_unchecked(self.as_ref().cpu) }
472    }
473
474    /// Returns the minimum frequency for the [`Policy`].
475    #[inline]
476    pub fn min(&self) -> Hertz {
477        Hertz::from_khz(self.as_ref().min as usize)
478    }
479
480    /// Set the minimum frequency for the [`Policy`].
481    #[inline]
482    pub fn set_min(&mut self, min: Hertz) -> &mut Self {
483        self.as_mut_ref().min = min.as_khz() as u32;
484        self
485    }
486
487    /// Returns the maximum frequency for the [`Policy`].
488    #[inline]
489    pub fn max(&self) -> Hertz {
490        Hertz::from_khz(self.as_ref().max as usize)
491    }
492
493    /// Set the maximum frequency for the [`Policy`].
494    #[inline]
495    pub fn set_max(&mut self, max: Hertz) -> &mut Self {
496        self.as_mut_ref().max = max.as_khz() as u32;
497        self
498    }
499
500    /// Returns the current frequency for the [`Policy`].
501    #[inline]
502    pub fn cur(&self) -> Hertz {
503        Hertz::from_khz(self.as_ref().cur as usize)
504    }
505
506    /// Returns the suspend frequency for the [`Policy`].
507    #[inline]
508    pub fn suspend_freq(&self) -> Hertz {
509        Hertz::from_khz(self.as_ref().suspend_freq as usize)
510    }
511
512    /// Sets the suspend frequency for the [`Policy`].
513    #[inline]
514    pub fn set_suspend_freq(&mut self, freq: Hertz) -> &mut Self {
515        self.as_mut_ref().suspend_freq = freq.as_khz() as u32;
516        self
517    }
518
519    /// Provides a wrapper to the generic suspend routine.
520    #[inline]
521    pub fn generic_suspend(&mut self) -> Result {
522        // SAFETY: By the type invariant, the pointer stored in `self` is valid.
523        to_result(unsafe { bindings::cpufreq_generic_suspend(self.as_mut_ref()) })
524    }
525
526    /// Provides a wrapper to the generic get routine.
527    #[inline]
528    pub fn generic_get(&self) -> Result<u32> {
529        // SAFETY: By the type invariant, the pointer stored in `self` is valid.
530        Ok(unsafe { bindings::cpufreq_generic_get(u32::from(self.cpu())) })
531    }
532
533    /// Provides a wrapper to the register with energy model using the OPP core.
534    #[cfg(CONFIG_PM_OPP)]
535    #[inline]
536    pub fn register_em_opp(&mut self) {
537        // SAFETY: By the type invariant, the pointer stored in `self` is valid.
538        unsafe { bindings::cpufreq_register_em_with_opp(self.as_mut_ref()) };
539    }
540
541    /// Gets [`cpumask::Cpumask`] for a cpufreq [`Policy`].
542    #[inline]
543    pub fn cpus(&mut self) -> &mut cpumask::Cpumask {
544        // SAFETY: The pointer to `cpus` is valid for writing and remains valid for the lifetime of
545        // the returned reference.
546        unsafe { cpumask::CpumaskVar::from_raw_mut(&mut self.as_mut_ref().cpus) }
547    }
548
549    /// Sets clock for the [`Policy`].
550    ///
551    /// # Safety
552    ///
553    /// The caller must guarantee that the returned [`Clk`] is not dropped while it is getting used
554    /// by the C code.
555    #[cfg(CONFIG_COMMON_CLK)]
556    pub unsafe fn set_clk(&mut self, dev: &Device, name: Option<&CStr>) -> Result<Clk> {
557        let clk = Clk::get(dev, name)?;
558        self.as_mut_ref().clk = clk.as_raw();
559        Ok(clk)
560    }
561
562    /// Allows / disallows frequency switching code to run on any CPU.
563    #[inline]
564    pub fn set_dvfs_possible_from_any_cpu(&mut self, val: bool) -> &mut Self {
565        self.as_mut_ref().dvfs_possible_from_any_cpu = val;
566        self
567    }
568
569    /// Returns if fast switching of frequencies is possible or not.
570    #[inline]
571    pub fn fast_switch_possible(&self) -> bool {
572        self.as_ref().fast_switch_possible
573    }
574
575    /// Enables / disables fast frequency switching.
576    #[inline]
577    pub fn set_fast_switch_possible(&mut self, val: bool) -> &mut Self {
578        self.as_mut_ref().fast_switch_possible = val;
579        self
580    }
581
582    /// Sets transition latency (in nanoseconds) for the [`Policy`].
583    #[inline]
584    pub fn set_transition_latency_ns(&mut self, latency_ns: u32) -> &mut Self {
585        self.as_mut_ref().cpuinfo.transition_latency = latency_ns;
586        self
587    }
588
589    /// Sets cpuinfo `min_freq`.
590    #[inline]
591    pub fn set_cpuinfo_min_freq(&mut self, min_freq: Hertz) -> &mut Self {
592        self.as_mut_ref().cpuinfo.min_freq = min_freq.as_khz() as u32;
593        self
594    }
595
596    /// Sets cpuinfo `max_freq`.
597    #[inline]
598    pub fn set_cpuinfo_max_freq(&mut self, max_freq: Hertz) -> &mut Self {
599        self.as_mut_ref().cpuinfo.max_freq = max_freq.as_khz() as u32;
600        self
601    }
602
603    /// Set `transition_delay_us`, i.e. the minimum time between successive frequency change
604    /// requests.
605    #[inline]
606    pub fn set_transition_delay_us(&mut self, transition_delay_us: u32) -> &mut Self {
607        self.as_mut_ref().transition_delay_us = transition_delay_us;
608        self
609    }
610
611    /// Returns reference to the CPU frequency [`Table`] for the [`Policy`].
612    pub fn freq_table(&self) -> Result<&Table> {
613        if self.as_ref().freq_table.is_null() {
614            return Err(EINVAL);
615        }
616
617        // SAFETY: The `freq_table` is guaranteed to be valid for reading and remains valid for the
618        // lifetime of the returned reference.
619        Ok(unsafe { Table::from_raw(self.as_ref().freq_table) })
620    }
621
622    /// Sets the CPU frequency [`Table`] for the [`Policy`].
623    ///
624    /// # Safety
625    ///
626    /// The caller must guarantee that the [`Table`] is not dropped while it is getting used by the
627    /// C code.
628    #[inline]
629    pub unsafe fn set_freq_table(&mut self, table: &Table) -> &mut Self {
630        self.as_mut_ref().freq_table = table.as_raw();
631        self
632    }
633
634    /// Returns the [`Policy`]'s private data.
635    pub fn data<T: ForeignOwnable>(&mut self) -> Option<<T>::Borrowed<'_>> {
636        if self.as_ref().driver_data.is_null() {
637            None
638        } else {
639            // SAFETY: The data is earlier set from [`set_data`].
640            Some(unsafe { T::borrow(self.as_ref().driver_data.cast()) })
641        }
642    }
643
644    /// Sets the private data of the [`Policy`] using a foreign-ownable wrapper.
645    ///
646    /// # Errors
647    ///
648    /// Returns `EBUSY` if private data is already set.
649    fn set_data<T: ForeignOwnable>(&mut self, data: T) -> Result {
650        if self.as_ref().driver_data.is_null() {
651            // Transfer the ownership of the data to the foreign interface.
652            self.as_mut_ref().driver_data = <T as ForeignOwnable>::into_foreign(data).cast();
653            Ok(())
654        } else {
655            Err(EBUSY)
656        }
657    }
658
659    /// Clears and returns ownership of the private data.
660    fn clear_data<T: ForeignOwnable>(&mut self) -> Option<T> {
661        if self.as_ref().driver_data.is_null() {
662            None
663        } else {
664            let data = Some(
665                // SAFETY: The data is earlier set by us from [`set_data`]. It is safe to take
666                // back the ownership of the data from the foreign interface.
667                unsafe { <T as ForeignOwnable>::from_foreign(self.as_ref().driver_data.cast()) },
668            );
669            self.as_mut_ref().driver_data = ptr::null_mut();
670            data
671        }
672    }
673}
674
675/// CPU frequency policy created from a CPU number.
676///
677/// This struct represents the CPU frequency policy obtained for a specific CPU, providing safe
678/// access to the underlying `cpufreq_policy` and ensuring proper cleanup when the `PolicyCpu` is
679/// dropped.
680struct PolicyCpu<'a>(&'a mut Policy);
681
682impl<'a> PolicyCpu<'a> {
683    fn from_cpu(cpu: CpuId) -> Result<Self> {
684        // SAFETY: It is safe to call `cpufreq_cpu_get` for any valid CPU.
685        let ptr = from_err_ptr(unsafe { bindings::cpufreq_cpu_get(u32::from(cpu)) })?;
686
687        Ok(Self(
688            // SAFETY: The `ptr` is guaranteed to be valid and remains valid for the lifetime of
689            // the returned reference.
690            unsafe { Policy::from_raw_mut(ptr) },
691        ))
692    }
693}
694
695impl<'a> Deref for PolicyCpu<'a> {
696    type Target = Policy;
697
698    fn deref(&self) -> &Self::Target {
699        self.0
700    }
701}
702
703impl<'a> DerefMut for PolicyCpu<'a> {
704    fn deref_mut(&mut self) -> &mut Policy {
705        self.0
706    }
707}
708
709impl<'a> Drop for PolicyCpu<'a> {
710    fn drop(&mut self) {
711        // SAFETY: The underlying pointer is guaranteed to be valid for the lifetime of `self`.
712        unsafe { bindings::cpufreq_cpu_put(self.0.as_raw()) };
713    }
714}
715
716/// CPU frequency driver.
717///
718/// Implement this trait to provide a CPU frequency driver and its callbacks.
719///
720/// Reference: <https://docs.kernel.org/cpu-freq/cpu-drivers.html>
721#[vtable]
722pub trait Driver {
723    /// Driver's name.
724    const NAME: &'static CStr;
725
726    /// Driver's flags.
727    const FLAGS: u16;
728
729    /// Boost support.
730    const BOOST_ENABLED: bool;
731
732    /// Policy specific data.
733    ///
734    /// Require that `PData` implements `ForeignOwnable`. We guarantee to never move the underlying
735    /// wrapped data structure.
736    type PData: ForeignOwnable;
737
738    /// Driver's `init` callback.
739    fn init(policy: &mut Policy) -> Result<Self::PData>;
740
741    /// Driver's `exit` callback.
742    fn exit(_policy: &mut Policy, _data: Option<Self::PData>) -> Result {
743        build_error!(VTABLE_DEFAULT_ERROR)
744    }
745
746    /// Driver's `online` callback.
747    fn online(_policy: &mut Policy) -> Result {
748        build_error!(VTABLE_DEFAULT_ERROR)
749    }
750
751    /// Driver's `offline` callback.
752    fn offline(_policy: &mut Policy) -> Result {
753        build_error!(VTABLE_DEFAULT_ERROR)
754    }
755
756    /// Driver's `suspend` callback.
757    fn suspend(_policy: &mut Policy) -> Result {
758        build_error!(VTABLE_DEFAULT_ERROR)
759    }
760
761    /// Driver's `resume` callback.
762    fn resume(_policy: &mut Policy) -> Result {
763        build_error!(VTABLE_DEFAULT_ERROR)
764    }
765
766    /// Driver's `ready` callback.
767    fn ready(_policy: &mut Policy) {
768        build_error!(VTABLE_DEFAULT_ERROR)
769    }
770
771    /// Driver's `verify` callback.
772    fn verify(data: &mut PolicyData) -> Result;
773
774    /// Driver's `setpolicy` callback.
775    fn setpolicy(_policy: &mut Policy) -> Result {
776        build_error!(VTABLE_DEFAULT_ERROR)
777    }
778
779    /// Driver's `target` callback.
780    fn target(_policy: &mut Policy, _target_freq: u32, _relation: Relation) -> Result {
781        build_error!(VTABLE_DEFAULT_ERROR)
782    }
783
784    /// Driver's `target_index` callback.
785    fn target_index(_policy: &mut Policy, _index: TableIndex) -> Result {
786        build_error!(VTABLE_DEFAULT_ERROR)
787    }
788
789    /// Driver's `fast_switch` callback.
790    fn fast_switch(_policy: &mut Policy, _target_freq: u32) -> u32 {
791        build_error!(VTABLE_DEFAULT_ERROR)
792    }
793
794    /// Driver's `adjust_perf` callback.
795    fn adjust_perf(
796        _policy: &mut Policy,
797        _min_perf: usize,
798        _target_perf: usize,
799        _max_perf: usize,
800        _capacity: usize,
801    ) {
802        build_error!(VTABLE_DEFAULT_ERROR)
803    }
804
805    /// Driver's `get_intermediate` callback.
806    fn get_intermediate(_policy: &mut Policy, _index: TableIndex) -> u32 {
807        build_error!(VTABLE_DEFAULT_ERROR)
808    }
809
810    /// Driver's `target_intermediate` callback.
811    fn target_intermediate(_policy: &mut Policy, _index: TableIndex) -> Result {
812        build_error!(VTABLE_DEFAULT_ERROR)
813    }
814
815    /// Driver's `get` callback.
816    fn get(_policy: &mut Policy) -> Result<u32> {
817        build_error!(VTABLE_DEFAULT_ERROR)
818    }
819
820    /// Driver's `update_limits` callback.
821    fn update_limits(_policy: &mut Policy) {
822        build_error!(VTABLE_DEFAULT_ERROR)
823    }
824
825    /// Driver's `bios_limit` callback.
826    fn bios_limit(_policy: &mut Policy, _limit: &mut u32) -> Result {
827        build_error!(VTABLE_DEFAULT_ERROR)
828    }
829
830    /// Driver's `set_boost` callback.
831    fn set_boost(_policy: &mut Policy, _state: i32) -> Result {
832        build_error!(VTABLE_DEFAULT_ERROR)
833    }
834
835    /// Driver's `register_em` callback.
836    fn register_em(_policy: &mut Policy) {
837        build_error!(VTABLE_DEFAULT_ERROR)
838    }
839}
840
841/// CPU frequency driver Registration.
842///
843/// # Examples
844///
845/// The following example demonstrates how to register a cpufreq driver.
846///
847/// ```
848/// use kernel::{
849///     cpufreq,
850///     device::{Core, Device},
851///     macros::vtable,
852///     of, platform,
853///     sync::Arc,
854/// };
855/// struct SampleDevice;
856///
857/// #[derive(Default)]
858/// struct SampleDriver;
859///
860/// #[vtable]
861/// impl cpufreq::Driver for SampleDriver {
862///     const NAME: &'static CStr = c"cpufreq-sample";
863///     const FLAGS: u16 = cpufreq::flags::NEED_INITIAL_FREQ_CHECK | cpufreq::flags::IS_COOLING_DEV;
864///     const BOOST_ENABLED: bool = true;
865///
866///     type PData = Arc<SampleDevice>;
867///
868///     fn init(policy: &mut cpufreq::Policy) -> Result<Self::PData> {
869///         // Initialize here
870///         Ok(Arc::new(SampleDevice, GFP_KERNEL)?)
871///     }
872///
873///     fn exit(_policy: &mut cpufreq::Policy, _data: Option<Self::PData>) -> Result {
874///         Ok(())
875///     }
876///
877///     fn suspend(policy: &mut cpufreq::Policy) -> Result {
878///         policy.generic_suspend()
879///     }
880///
881///     fn verify(data: &mut cpufreq::PolicyData) -> Result {
882///         data.generic_verify()
883///     }
884///
885///     fn target_index(policy: &mut cpufreq::Policy, index: cpufreq::TableIndex) -> Result {
886///         // Update CPU frequency
887///         Ok(())
888///     }
889///
890///     fn get(policy: &mut cpufreq::Policy) -> Result<u32> {
891///         policy.generic_get()
892///     }
893/// }
894///
895/// impl platform::Driver for SampleDriver {
896///     type IdInfo = ();
897///     type Data<'bound> = Self;
898///     const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = None;
899///
900///     fn probe<'bound>(
901///         pdev: &'bound platform::Device<Core<'_>>,
902///         _id_info: Option<&'bound Self::IdInfo>,
903///     ) -> impl PinInit<Self, Error> + 'bound {
904///         cpufreq::Registration::<SampleDriver>::new_foreign_owned(pdev.as_ref())?;
905///         Ok(Self {})
906///     }
907/// }
908/// ```
909#[repr(transparent)]
910pub struct Registration<T: Driver>(KBox<UnsafeCell<bindings::cpufreq_driver>>, PhantomData<T>);
911
912/// SAFETY: `Registration` doesn't offer any methods or access to fields when shared between threads
913/// or CPUs, so it is safe to share it.
914unsafe impl<T: Driver> Sync for Registration<T> {}
915
916#[allow(clippy::non_send_fields_in_send_ty)]
917/// SAFETY: Registration with and unregistration from the cpufreq subsystem can happen from any
918/// thread.
919unsafe impl<T: Driver> Send for Registration<T> {}
920
921impl<T: Driver> Registration<T> {
922    const VTABLE: bindings::cpufreq_driver = bindings::cpufreq_driver {
923        name: Self::copy_name(T::NAME),
924        boost_enabled: T::BOOST_ENABLED,
925        flags: T::FLAGS,
926
927        // Initialize mandatory callbacks.
928        init: Some(Self::init_callback),
929        verify: Some(Self::verify_callback),
930
931        // Initialize optional callbacks based on the traits of `T`.
932        setpolicy: if T::HAS_SETPOLICY {
933            Some(Self::setpolicy_callback)
934        } else {
935            None
936        },
937        target: if T::HAS_TARGET {
938            Some(Self::target_callback)
939        } else {
940            None
941        },
942        target_index: if T::HAS_TARGET_INDEX {
943            Some(Self::target_index_callback)
944        } else {
945            None
946        },
947        fast_switch: if T::HAS_FAST_SWITCH {
948            Some(Self::fast_switch_callback)
949        } else {
950            None
951        },
952        adjust_perf: if T::HAS_ADJUST_PERF {
953            Some(Self::adjust_perf_callback)
954        } else {
955            None
956        },
957        get_intermediate: if T::HAS_GET_INTERMEDIATE {
958            Some(Self::get_intermediate_callback)
959        } else {
960            None
961        },
962        target_intermediate: if T::HAS_TARGET_INTERMEDIATE {
963            Some(Self::target_intermediate_callback)
964        } else {
965            None
966        },
967        get: if T::HAS_GET {
968            Some(Self::get_callback)
969        } else {
970            None
971        },
972        update_limits: if T::HAS_UPDATE_LIMITS {
973            Some(Self::update_limits_callback)
974        } else {
975            None
976        },
977        bios_limit: if T::HAS_BIOS_LIMIT {
978            Some(Self::bios_limit_callback)
979        } else {
980            None
981        },
982        online: if T::HAS_ONLINE {
983            Some(Self::online_callback)
984        } else {
985            None
986        },
987        offline: if T::HAS_OFFLINE {
988            Some(Self::offline_callback)
989        } else {
990            None
991        },
992        exit: if T::HAS_EXIT {
993            Some(Self::exit_callback)
994        } else {
995            None
996        },
997        suspend: if T::HAS_SUSPEND {
998            Some(Self::suspend_callback)
999        } else {
1000            None
1001        },
1002        resume: if T::HAS_RESUME {
1003            Some(Self::resume_callback)
1004        } else {
1005            None
1006        },
1007        ready: if T::HAS_READY {
1008            Some(Self::ready_callback)
1009        } else {
1010            None
1011        },
1012        set_boost: if T::HAS_SET_BOOST {
1013            Some(Self::set_boost_callback)
1014        } else {
1015            None
1016        },
1017        register_em: if T::HAS_REGISTER_EM {
1018            Some(Self::register_em_callback)
1019        } else {
1020            None
1021        },
1022        ..pin_init::zeroed()
1023    };
1024
1025    // Always inline to optimize out error path of `build_assert`.
1026    #[inline(always)]
1027    const fn copy_name(name: &'static CStr) -> [c_char; CPUFREQ_NAME_LEN] {
1028        let src = name.to_bytes_with_nul();
1029        let mut dst = [0; CPUFREQ_NAME_LEN];
1030
1031        build_assert!(src.len() <= CPUFREQ_NAME_LEN);
1032
1033        let mut i = 0;
1034        while i < src.len() {
1035            dst[i] = src[i];
1036            i += 1;
1037        }
1038
1039        dst
1040    }
1041
1042    /// Registers a CPU frequency driver with the cpufreq core.
1043    pub fn new() -> Result<Self> {
1044        // We can't use `&Self::VTABLE` directly because the cpufreq core modifies some fields in
1045        // the C `struct cpufreq_driver`, which requires a mutable reference.
1046        let mut drv = KBox::new(UnsafeCell::new(Self::VTABLE), GFP_KERNEL)?;
1047
1048        // SAFETY: `drv` is guaranteed to be valid for the lifetime of `Registration`.
1049        to_result(unsafe { bindings::cpufreq_register_driver(drv.get_mut()) })?;
1050
1051        Ok(Self(drv, PhantomData))
1052    }
1053
1054    /// Same as [`Registration::new`], but does not return a [`Registration`] instance.
1055    ///
1056    /// Instead the [`Registration`] is owned by [`devres::register`] and will be dropped, once the
1057    /// device is detached.
1058    pub fn new_foreign_owned(dev: &Device<Bound>) -> Result
1059    where
1060        T: 'static,
1061    {
1062        devres::register(dev, Self::new()?, GFP_KERNEL)
1063    }
1064}
1065
1066/// CPU frequency driver callbacks.
1067impl<T: Driver> Registration<T> {
1068    /// Driver's `init` callback.
1069    ///
1070    /// # Safety
1071    ///
1072    /// - This function may only be called from the cpufreq C infrastructure.
1073    /// - The pointer arguments must be valid pointers.
1074    unsafe extern "C" fn init_callback(ptr: *mut bindings::cpufreq_policy) -> c_int {
1075        from_result(|| {
1076            // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
1077            // lifetime of `policy`.
1078            let policy = unsafe { Policy::from_raw_mut(ptr) };
1079
1080            let data = T::init(policy)?;
1081            policy.set_data(data)?;
1082            Ok(0)
1083        })
1084    }
1085
1086    /// Driver's `exit` callback.
1087    ///
1088    /// # Safety
1089    ///
1090    /// - This function may only be called from the cpufreq C infrastructure.
1091    /// - The pointer arguments must be valid pointers.
1092    unsafe extern "C" fn exit_callback(ptr: *mut bindings::cpufreq_policy) {
1093        // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
1094        // lifetime of `policy`.
1095        let policy = unsafe { Policy::from_raw_mut(ptr) };
1096
1097        let data = policy.clear_data();
1098        let _ = T::exit(policy, data);
1099    }
1100
1101    /// Driver's `online` callback.
1102    ///
1103    /// # Safety
1104    ///
1105    /// - This function may only be called from the cpufreq C infrastructure.
1106    /// - The pointer arguments must be valid pointers.
1107    unsafe extern "C" fn online_callback(ptr: *mut bindings::cpufreq_policy) -> c_int {
1108        from_result(|| {
1109            // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
1110            // lifetime of `policy`.
1111            let policy = unsafe { Policy::from_raw_mut(ptr) };
1112            T::online(policy).map(|()| 0)
1113        })
1114    }
1115
1116    /// Driver's `offline` callback.
1117    ///
1118    /// # Safety
1119    ///
1120    /// - This function may only be called from the cpufreq C infrastructure.
1121    /// - The pointer arguments must be valid pointers.
1122    unsafe extern "C" fn offline_callback(ptr: *mut bindings::cpufreq_policy) -> c_int {
1123        from_result(|| {
1124            // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
1125            // lifetime of `policy`.
1126            let policy = unsafe { Policy::from_raw_mut(ptr) };
1127            T::offline(policy).map(|()| 0)
1128        })
1129    }
1130
1131    /// Driver's `suspend` callback.
1132    ///
1133    /// # Safety
1134    ///
1135    /// - This function may only be called from the cpufreq C infrastructure.
1136    /// - The pointer arguments must be valid pointers.
1137    unsafe extern "C" fn suspend_callback(ptr: *mut bindings::cpufreq_policy) -> c_int {
1138        from_result(|| {
1139            // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
1140            // lifetime of `policy`.
1141            let policy = unsafe { Policy::from_raw_mut(ptr) };
1142            T::suspend(policy).map(|()| 0)
1143        })
1144    }
1145
1146    /// Driver's `resume` callback.
1147    ///
1148    /// # Safety
1149    ///
1150    /// - This function may only be called from the cpufreq C infrastructure.
1151    /// - The pointer arguments must be valid pointers.
1152    unsafe extern "C" fn resume_callback(ptr: *mut bindings::cpufreq_policy) -> c_int {
1153        from_result(|| {
1154            // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
1155            // lifetime of `policy`.
1156            let policy = unsafe { Policy::from_raw_mut(ptr) };
1157            T::resume(policy).map(|()| 0)
1158        })
1159    }
1160
1161    /// Driver's `ready` callback.
1162    ///
1163    /// # Safety
1164    ///
1165    /// - This function may only be called from the cpufreq C infrastructure.
1166    /// - The pointer arguments must be valid pointers.
1167    unsafe extern "C" fn ready_callback(ptr: *mut bindings::cpufreq_policy) {
1168        // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
1169        // lifetime of `policy`.
1170        let policy = unsafe { Policy::from_raw_mut(ptr) };
1171        T::ready(policy);
1172    }
1173
1174    /// Driver's `verify` callback.
1175    ///
1176    /// # Safety
1177    ///
1178    /// - This function may only be called from the cpufreq C infrastructure.
1179    /// - The pointer arguments must be valid pointers.
1180    unsafe extern "C" fn verify_callback(ptr: *mut bindings::cpufreq_policy_data) -> c_int {
1181        from_result(|| {
1182            // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
1183            // lifetime of `policy`.
1184            let data = unsafe { PolicyData::from_raw_mut(ptr) };
1185            T::verify(data).map(|()| 0)
1186        })
1187    }
1188
1189    /// Driver's `setpolicy` callback.
1190    ///
1191    /// # Safety
1192    ///
1193    /// - This function may only be called from the cpufreq C infrastructure.
1194    /// - The pointer arguments must be valid pointers.
1195    unsafe extern "C" fn setpolicy_callback(ptr: *mut bindings::cpufreq_policy) -> c_int {
1196        from_result(|| {
1197            // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
1198            // lifetime of `policy`.
1199            let policy = unsafe { Policy::from_raw_mut(ptr) };
1200            T::setpolicy(policy).map(|()| 0)
1201        })
1202    }
1203
1204    /// Driver's `target` callback.
1205    ///
1206    /// # Safety
1207    ///
1208    /// - This function may only be called from the cpufreq C infrastructure.
1209    /// - The pointer arguments must be valid pointers.
1210    unsafe extern "C" fn target_callback(
1211        ptr: *mut bindings::cpufreq_policy,
1212        target_freq: c_uint,
1213        relation: c_uint,
1214    ) -> c_int {
1215        from_result(|| {
1216            // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
1217            // lifetime of `policy`.
1218            let policy = unsafe { Policy::from_raw_mut(ptr) };
1219            T::target(policy, target_freq, Relation::new(relation)?).map(|()| 0)
1220        })
1221    }
1222
1223    /// Driver's `target_index` callback.
1224    ///
1225    /// # Safety
1226    ///
1227    /// - This function may only be called from the cpufreq C infrastructure.
1228    /// - The pointer arguments must be valid pointers.
1229    unsafe extern "C" fn target_index_callback(
1230        ptr: *mut bindings::cpufreq_policy,
1231        index: c_uint,
1232    ) -> c_int {
1233        from_result(|| {
1234            // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
1235            // lifetime of `policy`.
1236            let policy = unsafe { Policy::from_raw_mut(ptr) };
1237
1238            // SAFETY: The C code guarantees that `index` corresponds to a valid entry in the
1239            // frequency table.
1240            let index = unsafe { TableIndex::new(index as usize) };
1241
1242            T::target_index(policy, index).map(|()| 0)
1243        })
1244    }
1245
1246    /// Driver's `fast_switch` callback.
1247    ///
1248    /// # Safety
1249    ///
1250    /// - This function may only be called from the cpufreq C infrastructure.
1251    /// - The pointer arguments must be valid pointers.
1252    unsafe extern "C" fn fast_switch_callback(
1253        ptr: *mut bindings::cpufreq_policy,
1254        target_freq: c_uint,
1255    ) -> c_uint {
1256        // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
1257        // lifetime of `policy`.
1258        let policy = unsafe { Policy::from_raw_mut(ptr) };
1259        T::fast_switch(policy, target_freq)
1260    }
1261
1262    /// Driver's `adjust_perf` callback.
1263    ///
1264    /// # Safety
1265    ///
1266    /// - This function may only be called from the cpufreq C infrastructure.
1267    /// - The pointer arguments must be valid pointers.
1268    unsafe extern "C" fn adjust_perf_callback(
1269        ptr: *mut bindings::cpufreq_policy,
1270        min_perf: c_ulong,
1271        target_perf: c_ulong,
1272        max_perf: c_ulong,
1273        capacity: c_ulong,
1274    ) {
1275        // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
1276        // lifetime of `policy`.
1277        let policy = unsafe { Policy::from_raw_mut(ptr) };
1278        T::adjust_perf(policy, min_perf, target_perf, max_perf, capacity);
1279    }
1280
1281    /// Driver's `get_intermediate` callback.
1282    ///
1283    /// # Safety
1284    ///
1285    /// - This function may only be called from the cpufreq C infrastructure.
1286    /// - The pointer arguments must be valid pointers.
1287    unsafe extern "C" fn get_intermediate_callback(
1288        ptr: *mut bindings::cpufreq_policy,
1289        index: c_uint,
1290    ) -> c_uint {
1291        // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
1292        // lifetime of `policy`.
1293        let policy = unsafe { Policy::from_raw_mut(ptr) };
1294
1295        // SAFETY: The C code guarantees that `index` corresponds to a valid entry in the
1296        // frequency table.
1297        let index = unsafe { TableIndex::new(index as usize) };
1298
1299        T::get_intermediate(policy, index)
1300    }
1301
1302    /// Driver's `target_intermediate` callback.
1303    ///
1304    /// # Safety
1305    ///
1306    /// - This function may only be called from the cpufreq C infrastructure.
1307    /// - The pointer arguments must be valid pointers.
1308    unsafe extern "C" fn target_intermediate_callback(
1309        ptr: *mut bindings::cpufreq_policy,
1310        index: c_uint,
1311    ) -> c_int {
1312        from_result(|| {
1313            // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
1314            // lifetime of `policy`.
1315            let policy = unsafe { Policy::from_raw_mut(ptr) };
1316
1317            // SAFETY: The C code guarantees that `index` corresponds to a valid entry in the
1318            // frequency table.
1319            let index = unsafe { TableIndex::new(index as usize) };
1320
1321            T::target_intermediate(policy, index).map(|()| 0)
1322        })
1323    }
1324
1325    /// Driver's `get` callback.
1326    ///
1327    /// # Safety
1328    ///
1329    /// - This function may only be called from the cpufreq C infrastructure.
1330    unsafe extern "C" fn get_callback(cpu: c_uint) -> c_uint {
1331        // SAFETY: The C API guarantees that `cpu` refers to a valid CPU number.
1332        let cpu_id = unsafe { CpuId::from_u32_unchecked(cpu) };
1333
1334        PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).unwrap_or(0))
1335    }
1336
1337    /// Driver's `update_limit` callback.
1338    ///
1339    /// # Safety
1340    ///
1341    /// - This function may only be called from the cpufreq C infrastructure.
1342    /// - The pointer arguments must be valid pointers.
1343    unsafe extern "C" fn update_limits_callback(ptr: *mut bindings::cpufreq_policy) {
1344        // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
1345        // lifetime of `policy`.
1346        let policy = unsafe { Policy::from_raw_mut(ptr) };
1347        T::update_limits(policy);
1348    }
1349
1350    /// Driver's `bios_limit` callback.
1351    ///
1352    /// # Safety
1353    ///
1354    /// - This function may only be called from the cpufreq C infrastructure.
1355    /// - The pointer arguments must be valid pointers.
1356    unsafe extern "C" fn bios_limit_callback(cpu: c_int, limit: *mut c_uint) -> c_int {
1357        // SAFETY: The C API guarantees that `cpu` refers to a valid CPU number.
1358        let cpu_id = unsafe { CpuId::from_i32_unchecked(cpu) };
1359
1360        from_result(|| {
1361            let mut policy = PolicyCpu::from_cpu(cpu_id)?;
1362
1363            // SAFETY: `limit` is guaranteed by the C code to be valid.
1364            T::bios_limit(&mut policy, &mut (unsafe { *limit })).map(|()| 0)
1365        })
1366    }
1367
1368    /// Driver's `set_boost` callback.
1369    ///
1370    /// # Safety
1371    ///
1372    /// - This function may only be called from the cpufreq C infrastructure.
1373    /// - The pointer arguments must be valid pointers.
1374    unsafe extern "C" fn set_boost_callback(
1375        ptr: *mut bindings::cpufreq_policy,
1376        state: c_int,
1377    ) -> c_int {
1378        from_result(|| {
1379            // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
1380            // lifetime of `policy`.
1381            let policy = unsafe { Policy::from_raw_mut(ptr) };
1382            T::set_boost(policy, state).map(|()| 0)
1383        })
1384    }
1385
1386    /// Driver's `register_em` callback.
1387    ///
1388    /// # Safety
1389    ///
1390    /// - This function may only be called from the cpufreq C infrastructure.
1391    /// - The pointer arguments must be valid pointers.
1392    unsafe extern "C" fn register_em_callback(ptr: *mut bindings::cpufreq_policy) {
1393        // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
1394        // lifetime of `policy`.
1395        let policy = unsafe { Policy::from_raw_mut(ptr) };
1396        T::register_em(policy);
1397    }
1398}
1399
1400impl<T: Driver> Drop for Registration<T> {
1401    /// Unregisters with the cpufreq core.
1402    fn drop(&mut self) {
1403        // SAFETY: `self.0` is guaranteed to be valid for the lifetime of `Registration`.
1404        unsafe { bindings::cpufreq_unregister_driver(self.0.get_mut()) };
1405    }
1406}