From nobody Sun Feb 8 21:56:08 2026 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8C4BB26A1DB for ; Mon, 19 May 2025 07:08:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747638510; cv=none; b=oVCeuJQ47HylcjvhRuaa5Lr0LV5e96HECYE2ITb1gVa1+8CbtR4vJLqCWS8tsWvAnEWlg2NiUQ/a6sy3nT1ZYnU62EJ5x6Ex6E6jahW01x11rq6Hbg5n/rWK6W3I1FCGtxRLwOabAsfgiMEpW0lqRtvrrD6X9c4O09W+ZfugJPE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747638510; c=relaxed/simple; bh=QXMxH7iSPfBkj0Ia64GJx6G2+8KNn9rCCD8XzU/DQ38=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hzURFX+xhMTGea96h2sh7lI8ge3h81zJKXUk00v6i4jhCAPZWAWaWEGrwKNNoWLchWulFEtBWftSJ87rFq9KG58KtILRSOav55LXE8wkQfitdYT0aUmkh3z3TQZMXGuY3LWyNlSwf2Lzf7i2EYALZ6aTu+fS0TMf3Voj0VqiPD0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=gPP2Pc/I; arc=none smtp.client-ip=209.85.210.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="gPP2Pc/I" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-7370a2d1981so3194095b3a.2 for ; Mon, 19 May 2025 00:08:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1747638508; x=1748243308; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=PeAHfrp/tGXc/RJP/mrXOf/ij0zku7wQhR0SuexE3Ok=; b=gPP2Pc/IIopet5v0+azJQDtWwrfCFlgkVaGz6TR8CE53W27m/jnturTEikumvOSypJ RdNabbZ9vJBNuQAii66DtCrW6MlCdCJnS+Z+n+xYmVZbSVqZlcnEjsCoF7KoXZPMnPn1 eBKw/IDcc4NhlhocgcnNuPEgqaKEvk0sr22PB7jYdsEYf8hAA/9QRRoJuOlCdRi5LQFc BCban8AKVsOWeers59uF9rllgQMiFu+NuPHRD8W/NuJLNNOcFDFOyeEHyO6NqkK97AQ9 T9/PwfVSmBspEDCU6FrZ24C6yZ53drQy05p1gg8EPWRx+33yNl+dOaB2B0VhUrMqema5 6RTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747638508; x=1748243308; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PeAHfrp/tGXc/RJP/mrXOf/ij0zku7wQhR0SuexE3Ok=; b=cE/xOStgwb7h9GXj4sq//F3WlNleF+PGcpq7wi3XP/sKL3k6XONC+01RT/1cDHct21 nlJuv1N0pxe84qRf4MDsyIl65raaeN2KrYmE+PyImTWxoWjdLddk2Ai0eTQCXxY097R7 kabsGBW2iR/5vm2Unsdc4MVv1H1U3WVMYpFjrB69NtmdnXeV6uW7PChuo1hVOZDBm4h1 06WeeUAculvbf9p5K4XJrb6rTN6jQyS6nliSge/gHc9JjG0iTCUyLA+9vc9IV37ffv9Q fN0g3ahtex3WeUeK4qgC+XotfZBsdT5433yLOtwaFowp2uU1E/JJ9IYEi1ncSkVuKHSN LJOQ== X-Forwarded-Encrypted: i=1; AJvYcCUb6J4awV/SVgyI30fK83ixKymcylG/KuKLYwuKjSpEftYyJprmCiYwqFOs4ZZNCJH/tR6KmXvtjt65fUc=@vger.kernel.org X-Gm-Message-State: AOJu0YyeRuhMcEERXygHiwE0PYwyYM5JnLxtre9weYu3EwanSpDNbPjV fG44uTFyFu/pCtDlQNqp2VKdZ3tANoRA2YIgOgXyEOhVI8D5WfW33mVq1XKiKcK4pMg= X-Gm-Gg: ASbGncvylbi1tjN0240T1i+6oJRnt4PI3PhIf7sTJZMuVpK3xNIX07iHWgB2XHv8NUN oGqeTeOfNyWuWraxFYDlFbdUneo+Lo0xtrPkh4GQusiQeUuVLzuzcJX2anVWSmLSXBhth2ppuCz GJsLIwDwXywc4sAPMHjy4Fhps9gbP0zSasA7cq2wsd3pHLIyMqyHbSEEi4pw8VQjxbAfQuZpefW V1rRBERfifgLxVUu6/jKCpyvdTstfPuqNWnPPkKU4Zvx397KUoCDKm5X3LjWnynLOpEAKEGWbPH qRamg158+uAB/7SvLVg7MU5rn/j1z+FNP+I8oWdVtVy4UUdQZhbr X-Google-Smtp-Source: AGHT+IGia5Bc+1wwYUidEZDs6sa6shkoF8XVQlwQc4PAGsofPvtXfajZaWlVn8wBbERNG8VrD6Dv+g== X-Received: by 2002:a05:6a20:c916:b0:203:bb3b:5f1e with SMTP id adf61e73a8af0-2170c7232ffmr15972933637.1.1747638507547; Mon, 19 May 2025 00:08:27 -0700 (PDT) Received: from localhost ([122.172.81.72]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-742acb8731esm5295547b3a.168.2025.05.19.00.08.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 May 2025 00:08:27 -0700 (PDT) From: Viresh Kumar To: "Rafael J. Wysocki" , Miguel Ojeda , Danilo Krummrich , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Viresh Kumar Cc: linux-pm@vger.kernel.org, Vincent Guittot , Stephen Boyd , Nishanth Menon , rust-for-linux@vger.kernel.org, Manos Pitsidianakis , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Joakim Bech , Rob Herring , Yury Norov , Burak Emir , Rasmus Villemoes , Russell King , linux-clk@vger.kernel.org, Michael Turquette , Andrew Ballance , linux-kernel@vger.kernel.org Subject: [PATCH V12 11/15] rust: cpufreq: Add initial abstractions for cpufreq framework Date: Mon, 19 May 2025 12:37:16 +0530 Message-Id: <994733d45f0ab11efb8a0a5fd5197c9ed4cc319f.1747634382.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.31.1.272.g89b43f80a514 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce initial Rust abstractions for the cpufreq core. This includes basic representations for cpufreq flags, relation types, and the cpufreq table. Signed-off-by: Viresh Kumar --- MAINTAINERS | 1 + rust/bindings/bindings_helper.h | 1 + rust/helpers/cpufreq.c | 10 + rust/helpers/helpers.c | 1 + rust/kernel/cpufreq.rs | 364 ++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + 6 files changed, 379 insertions(+) create mode 100644 rust/helpers/cpufreq.c create mode 100644 rust/kernel/cpufreq.rs diff --git a/MAINTAINERS b/MAINTAINERS index 931e418f89ed..aa56eacbda71 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -6142,6 +6142,7 @@ F: drivers/cpufreq/ F: include/linux/cpufreq.h F: include/linux/sched/cpufreq.h F: kernel/sched/cpufreq*.c +F: rust/kernel/cpufreq.rs F: tools/testing/selftests/cpufreq/ =20 CPU HOTPLUG diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helpe= r.h index 529f22891e0b..7c1d78f68076 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include diff --git a/rust/helpers/cpufreq.c b/rust/helpers/cpufreq.c new file mode 100644 index 000000000000..7c1343c4d65e --- /dev/null +++ b/rust/helpers/cpufreq.c @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +#ifdef CONFIG_CPU_FREQ +void rust_helper_cpufreq_register_em_with_opp(struct cpufreq_policy *polic= y) +{ + cpufreq_register_em_with_opp(policy); +} +#endif diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c index ae595c9cd91b..df1fcfb3adf3 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -12,6 +12,7 @@ #include "build_assert.c" #include "build_bug.c" #include "clk.c" +#include "cpufreq.c" #include "cpumask.c" #include "cred.c" #include "device.c" diff --git a/rust/kernel/cpufreq.rs b/rust/kernel/cpufreq.rs new file mode 100644 index 000000000000..2aa024615d4d --- /dev/null +++ b/rust/kernel/cpufreq.rs @@ -0,0 +1,364 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! CPU frequency scaling. +//! +//! This module provides rust abstractions for interacting with the cpufre= q subsystem. +//! +//! C header: [`include/linux/cpufreq.h`](srctree/include/linux/cpufreq.h) +//! +//! Reference: + +use crate::{ + clk::Hertz, + error::{code::*, to_result, Result}, + ffi::c_ulong, + prelude::*, + types::Opaque, +}; + +use core::{ops::Deref, pin::Pin}; + +/// Default transition latency value in nanoseconds. +pub const ETERNAL_LATENCY_NS: u32 =3D bindings::CPUFREQ_ETERNAL as u32; + +/// CPU frequency driver flags. +pub mod flags { + /// Driver needs to update internal limits even if frequency remains u= nchanged. + pub const NEED_UPDATE_LIMITS: u16 =3D 1 << 0; + + /// Platform where constants like `loops_per_jiffy` are unaffected by = frequency changes. + pub const CONST_LOOPS: u16 =3D 1 << 1; + + /// Register driver as a thermal cooling device automatically. + pub const IS_COOLING_DEV: u16 =3D 1 << 2; + + /// Supports multiple clock domains with per-policy governors in `cpu/= cpuN/cpufreq/`. + pub const HAVE_GOVERNOR_PER_POLICY: u16 =3D 1 << 3; + + /// Allows post-change notifications outside of the `target()` routine. + pub const ASYNC_NOTIFICATION: u16 =3D 1 << 4; + + /// Ensure CPU starts at a valid frequency from the driver's freq-tabl= e. + pub const NEED_INITIAL_FREQ_CHECK: u16 =3D 1 << 5; + + /// Disallow governors with `dynamic_switching` capability. + pub const NO_AUTO_DYNAMIC_SWITCHING: u16 =3D 1 << 6; +} + +/// Relations from the C code. +const CPUFREQ_RELATION_L: u32 =3D 0; +const CPUFREQ_RELATION_H: u32 =3D 1; +const CPUFREQ_RELATION_C: u32 =3D 2; + +/// Can be used with any of the above values. +const CPUFREQ_RELATION_E: u32 =3D 1 << 2; + +/// CPU frequency selection relations. +/// +/// CPU frequency selection relations, each optionally marked as "efficien= t". +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +pub enum Relation { + /// Select the lowest frequency at or above target. + Low(bool), + /// Select the highest frequency below or at target. + High(bool), + /// Select the closest frequency to the target. + Close(bool), +} + +impl Relation { + // Construct from a C-compatible `u32` value. + fn new(val: u32) -> Result { + let efficient =3D val & CPUFREQ_RELATION_E !=3D 0; + + Ok(match val & !CPUFREQ_RELATION_E { + CPUFREQ_RELATION_L =3D> Self::Low(efficient), + CPUFREQ_RELATION_H =3D> Self::High(efficient), + CPUFREQ_RELATION_C =3D> Self::Close(efficient), + _ =3D> return Err(EINVAL), + }) + } +} + +impl From for u32 { + // Convert to a C-compatible `u32` value. + fn from(rel: Relation) -> Self { + let (mut val, efficient) =3D match rel { + Relation::Low(e) =3D> (CPUFREQ_RELATION_L, e), + Relation::High(e) =3D> (CPUFREQ_RELATION_H, e), + Relation::Close(e) =3D> (CPUFREQ_RELATION_C, e), + }; + + if efficient { + val |=3D CPUFREQ_RELATION_E; + } + + val + } +} + +/// Policy data. +/// +/// Rust abstraction for the C `struct cpufreq_policy_data`. +/// +/// # Invariants +/// +/// A [`PolicyData`] instance always corresponds to a valid C `struct cpuf= req_policy_data`. +/// +/// The callers must ensure that the `struct cpufreq_policy_data` is valid= for access and remains +/// valid for the lifetime of the returned reference. +#[repr(transparent)] +pub struct PolicyData(Opaque); + +impl PolicyData { + /// Creates a mutable reference to an existing `struct cpufreq_policy_= data` pointer. + /// + /// # Safety + /// + /// The caller must ensure that `ptr` is valid for writing and remains= valid for the lifetime + /// of the returned reference. + #[inline] + pub unsafe fn from_raw_mut<'a>(ptr: *mut bindings::cpufreq_policy_data= ) -> &'a mut Self { + // SAFETY: Guaranteed by the safety requirements of the function. + // + // INVARIANT: The caller ensures that `ptr` is valid for writing a= nd remains valid for the + // lifetime of the returned reference. + unsafe { &mut *ptr.cast() } + } + + /// Returns a raw pointer to the underlying C `cpufreq_policy_data`. + #[inline] + pub fn as_raw(&self) -> *mut bindings::cpufreq_policy_data { + let this: *const Self =3D self; + this.cast_mut().cast() + } + + /// Wrapper for `cpufreq_generic_frequency_table_verify`. + #[inline] + pub fn generic_verify(&self) -> Result<()> { + // SAFETY: By the type invariant, the pointer stored in `self` is = valid. + to_result(unsafe { bindings::cpufreq_generic_frequency_table_verif= y(self.as_raw()) }) + } +} + +/// The frequency table index. +/// +/// Represents index with a frequency table. +/// +/// # Invariants +/// +/// The index must correspond to a valid entry in the [`Table`] it is used= for. +#[derive(Copy, Clone, PartialEq, Eq, Debug)] +pub struct TableIndex(usize); + +impl TableIndex { + /// Creates an instance of [`TableIndex`]. + /// + /// # Safety + /// + /// The caller must ensure that `index` correspond to a valid entry in= the [`Table`] it is used + /// for. + pub unsafe fn new(index: usize) -> Self { + // INVARIANT: The caller ensures that `index` correspond to a vali= d entry in the [`Table`]. + Self(index) + } +} + +impl From for usize { + #[inline] + fn from(index: TableIndex) -> Self { + index.0 + } +} + +/// CPU frequency table. +/// +/// Rust abstraction for the C `struct cpufreq_frequency_table`. +/// +/// # Invariants +/// +/// A [`Table`] instance always corresponds to a valid C `struct cpufreq_f= requency_table`. +/// +/// The callers must ensure that the `struct cpufreq_frequency_table` is v= alid for access and +/// remains valid for the lifetime of the returned reference. +/// +/// ## Examples +/// +/// The following example demonstrates how to read a frequency value from = [`Table`]. +/// +/// ``` +/// use kernel::cpufreq::{Policy, TableIndex}; +/// +/// fn show_freq(policy: &Policy) { +/// let table =3D policy.freq_table().unwrap(); +/// +/// // SAFETY: Index is a valid entry in the table. +/// let index =3D unsafe { TableIndex::new(0) }; +/// +/// pr_info!("The frequency at index 0 is: {:?}\n", table.freq(index).= unwrap()); +/// pr_info!("The flags at index 0 is: {}\n", table.flags(index)); +/// pr_info!("The data at index 0 is: {}\n", table.data(index)); +/// } +/// ``` +#[repr(transparent)] +pub struct Table(Opaque); + +impl Table { + /// Creates a reference to an existing C `struct cpufreq_frequency_tab= le` pointer. + /// + /// # Safety + /// + /// The caller must ensure that `ptr` is valid for reading and remains= valid for the lifetime + /// of the returned reference. + #[inline] + pub unsafe fn from_raw<'a>(ptr: *const bindings::cpufreq_frequency_tab= le) -> &'a Self { + // SAFETY: Guaranteed by the safety requirements of the function. + // + // INVARIANT: The caller ensures that `ptr` is valid for reading a= nd remains valid for the + // lifetime of the returned reference. + unsafe { &*ptr.cast() } + } + + /// Returns the raw mutable pointer to the C `struct cpufreq_frequency= _table`. + #[inline] + pub fn as_raw(&self) -> *mut bindings::cpufreq_frequency_table { + let this: *const Self =3D self; + this.cast_mut().cast() + } + + /// Returns frequency at `index` in the [`Table`]. + #[inline] + pub fn freq(&self, index: TableIndex) -> Result { + // SAFETY: By the type invariant, the pointer stored in `self` is = valid and `index` is + // guaranteed to be valid by its safety requirements. + Ok(Hertz::from_khz(unsafe { + (*self.as_raw().add(index.into())).frequency.try_into()? + })) + } + + /// Returns flags at `index` in the [`Table`]. + #[inline] + pub fn flags(&self, index: TableIndex) -> u32 { + // SAFETY: By the type invariant, the pointer stored in `self` is = valid and `index` is + // guaranteed to be valid by its safety requirements. + unsafe { (*self.as_raw().add(index.into())).flags } + } + + /// Returns data at `index` in the [`Table`]. + #[inline] + pub fn data(&self, index: TableIndex) -> u32 { + // SAFETY: By the type invariant, the pointer stored in `self` is = valid and `index` is + // guaranteed to be valid by its safety requirements. + unsafe { (*self.as_raw().add(index.into())).driver_data } + } +} + +/// CPU frequency table owned and pinned in memory, created from a [`Table= Builder`]. +pub struct TableBox { + entries: Pin>, +} + +impl TableBox { + /// Constructs a new [`TableBox`] from a [`KVec`] of entries. + /// + /// # Errors + /// + /// Returns `EINVAL` if the entries list is empty. + #[inline] + fn new(entries: KVec) -> Result { + if entries.is_empty() { + return Err(EINVAL); + } + + Ok(Self { + // Pin the entries to memory, since we are passing its pointer= to the C code. + entries: Pin::new(entries), + }) + } + + /// Returns a raw pointer to the underlying C `cpufreq_frequency_table= `. + #[inline] + fn as_raw(&self) -> *const bindings::cpufreq_frequency_table { + // The pointer is valid until the table gets dropped. + self.entries.as_ptr() + } +} + +impl Deref for TableBox { + type Target =3D Table; + + fn deref(&self) -> &Self::Target { + // SAFETY: The caller owns TableBox, it is safe to deref. + unsafe { Self::Target::from_raw(self.as_raw()) } + } +} + +/// CPU frequency table builder. +/// +/// This is used by the CPU frequency drivers to build a frequency table d= ynamically. +/// +/// ## Examples +/// +/// The following example demonstrates how to create a CPU frequency table. +/// +/// ``` +/// use kernel::cpufreq::{TableBuilder, TableIndex}; +/// use kernel::clk::Hertz; +/// +/// let mut builder =3D TableBuilder::new(); +/// +/// // Adds few entries to the table. +/// builder.add(Hertz::from_mhz(700), 0, 1).unwrap(); +/// builder.add(Hertz::from_mhz(800), 2, 3).unwrap(); +/// builder.add(Hertz::from_mhz(900), 4, 5).unwrap(); +/// builder.add(Hertz::from_ghz(1), 6, 7).unwrap(); +/// +/// let table =3D builder.to_table().unwrap(); +/// +/// // SAFETY: Index values correspond to valid entries in the table. +/// let (index0, index2) =3D unsafe { (TableIndex::new(0), TableIndex::new= (2)) }; +/// +/// assert_eq!(table.freq(index0), Ok(Hertz::from_mhz(700))); +/// assert_eq!(table.flags(index0), 0); +/// assert_eq!(table.data(index0), 1); +/// +/// assert_eq!(table.freq(index2), Ok(Hertz::from_mhz(900))); +/// assert_eq!(table.flags(index2), 4); +/// assert_eq!(table.data(index2), 5); +/// ``` +#[derive(Default)] +#[repr(transparent)] +pub struct TableBuilder { + entries: KVec, +} + +impl TableBuilder { + /// Creates a new instance of [`TableBuilder`]. + #[inline] + pub fn new() -> Self { + Self { + entries: KVec::new(), + } + } + + /// Adds a new entry to the table. + pub fn add(&mut self, freq: Hertz, flags: u32, driver_data: u32) -> Re= sult<()> { + // Adds the new entry at the end of the vector. + Ok(self.entries.push( + bindings::cpufreq_frequency_table { + flags, + driver_data, + frequency: freq.as_khz() as u32, + }, + GFP_KERNEL, + )?) + } + + /// Consumes the [`TableBuilder`] and returns [`TableBox`]. + pub fn to_table(mut self) -> Result { + // Add last entry to the table. + self.add(Hertz(c_ulong::MAX), 0, 0)?; + + TableBox::new(self.entries) + } +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index ea589254b4ac..133ebee4f9d3 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -44,6 +44,8 @@ pub mod build_assert; pub mod clk; pub mod cpu; +#[cfg(CONFIG_CPU_FREQ)] +pub mod cpufreq; pub mod cpumask; pub mod cred; pub mod device; --=20 2.31.1.272.g89b43f80a514