From nobody Sun Feb 8 21:48:07 2026 Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) (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 6B5852BE7A8 for ; Fri, 11 Apr 2025 10:59:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744369198; cv=none; b=mqq2NMdgav2KTt1yx8qW3FhRr1PZvBpbfW4dpyAmwun0Ae3SBdnDb7VXhQvLVYXooua83nxhsFHKxk63tH1oy/CLUVuQgZIE0gyAISTxKp834LCOM2Z2JeQ4ZL/aO+oEMwPMd2aO+4SdIRfmRxb6QSw52UdjIjHS/Wgl6/NZsus= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744369198; c=relaxed/simple; bh=x9ocZuCMF6s1tLJISfuyOmdKP9oOJA6RAJRtbMhG13w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hG7fIY/bhvu/M1ElqaQjGp0a9v+rWlI3WveB/vN+w/nMSOwgssl0mkZdrjx82/Zgvhj9u3S5ik8OjObd51jL4Xu4btPlZ8t4cj3k4ANqiavHFhwJbfEq+LN+dW4e7MUmFJRh6JSuF/hLWaS/5MJiUbfLgGZNxxHFWbw4/YVY8cE= 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=RKiz/d2g; arc=none smtp.client-ip=209.85.210.176 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="RKiz/d2g" Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-736c1138ae5so1825111b3a.3 for ; Fri, 11 Apr 2025 03:59:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1744369196; x=1744973996; 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=8icndP/zvg1XI2+CBWAxu2KWgnBqQFP1Hmt5Q3cH4NQ=; b=RKiz/d2gCQT0s82PQvVOOUSxPOzKBP6CRu/jQZSmQQmNklutc5MDp20PHBp70FCrb4 p2vWAI0gAsjUwpOkro1tOFL/FfnFGjrCKSwTbjfaGP21NW86R3f5MSOUpUYINqfFttrs pRQfuXN0SD0uiGawJivS0RohKy1JnadUJ3MLixwm/HDICVQ3fZrjBsyGII11sO/QnWI/ uaI9urpUOjeuhjVJyKXczRvZoZiEqj/qf8eo1xbBUk31cKFiWrzUbdyjBeSwRYpijoKe fx4K/MV56YbWAK5AALzfc+DTcRD0P+kO7yw/lv/OrzQp1RtSQrzebuu0RD9vguGs5Ars tqyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744369196; x=1744973996; 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=8icndP/zvg1XI2+CBWAxu2KWgnBqQFP1Hmt5Q3cH4NQ=; b=iJ/kugnF7SY/XoYseZW0flQzqZUoKftlK6QMUxr1ajg1nqgAxiollT9nKt58h2KCDC ubOCtMRCYMkQoMkQYc3Y2grGF2QYdK85V11PltDUEmZsZj9fFq9+Q6riuHQNsmhNCwcd iUTqNcYEaBlmN8eBkCxYbsRJnLFbstfa/I73/gQblqf/VXabtG5IyL/OeOgeJj/+//Gb wM/Jl6gCA6ip40uwn1bh0F/OlQPVJ2kWqcTu7ifExWcrZn0lii95kf3izeBv3xzoLmjJ rchUGLgg+yQP8lUjW/p7K4+wSTLGno4XzpWqRXrG22MxQjCDD9yX8QM4qO5u6v3LczGJ i6xw== X-Forwarded-Encrypted: i=1; AJvYcCUFRbgOv0yL4DtoDzKH6EYkvz02jqXkrreQzeifQ/BnsX+E6iB05qJOMij5inJkOV392P29sTztY10JRUU=@vger.kernel.org X-Gm-Message-State: AOJu0Yz2nCSMVZu51qu7sCsjpj2BiW7v/YUtVY9ZRNfSkW7vduOycjLL s5r0wtLfOmMy7QKcDHcsgQeLoKShq+R23QJWvTGu0ZmLzM1U9aPNtGZz+paED04= X-Gm-Gg: ASbGncudG4X5NhnlkzjlUYBBttHuhZXGXqMMv1Aiv9rvkuSEeqNxCc4e98AerVwzOQW 0901bDBjV7Tc7EwGpVqKJS0rJrCkWT0tcO+tT7g0HzBhEGjGuDGvjqvCXSZzGtYzXgtxyFkggdK O0AnGqkRdffpPpWLTp/7dd7nywJRXpN9Fhm0tQhHQWS9/9LLwV5c2+3WIXTPoncga0okVc7UMAg X0RrGJpXpjgP2i+ssA26qByK94xjYBffaAX0/ryhjD/hn/GVZkdvnT0infXz2REKJU13h2cpsDm MUMWBIf+f0Vx2CfqSVhVqI10DAZtz41NHbtlSn9oNQ== X-Google-Smtp-Source: AGHT+IHOlqPXQbP/NH5YoopqX8rs99m8OO1ecidoEx4lWwHl9VBhh51/az5okTqwk9eilX+LPwMurg== X-Received: by 2002:a05:6a00:1147:b0:736:a638:7f9e with SMTP id d2e1a72fcca58-73bd11dc3bamr4082106b3a.8.1744369195524; Fri, 11 Apr 2025 03:59:55 -0700 (PDT) Received: from localhost ([122.172.83.32]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73bd21c62f7sm1226889b3a.65.2025.04.11.03.59.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Apr 2025 03:59:54 -0700 (PDT) From: Viresh Kumar To: "Rafael J. Wysocki" , Miguel Ojeda , Danilo Krummrich , Viresh Kumar , 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 Cc: linux-pm@vger.kernel.org, Vincent Guittot , Stephen Boyd , Nishanth Menon , rust-for-linux@vger.kernel.org, Manos Pitsidianakis , Erik Schilling , =?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 , linux-kernel@vger.kernel.org Subject: [PATCH V9 15/17] rust: cpufreq: Extend abstractions for driver registration Date: Fri, 11 Apr 2025 16:25:14 +0530 Message-Id: <2f7a1331ad513b94fb47c05bf1d0f5c3fa803858.1744366572.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" Extend the cpufreq abstractions to support driver registration from Rust. Signed-off-by: Viresh Kumar --- rust/kernel/cpufreq.rs | 530 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 528 insertions(+), 2 deletions(-) diff --git a/rust/kernel/cpufreq.rs b/rust/kernel/cpufreq.rs index 3e9ded655d46..4194b9558413 100644 --- a/rust/kernel/cpufreq.rs +++ b/rust/kernel/cpufreq.rs @@ -13,7 +13,8 @@ clk::{Clk, Hertz}, cpumask, device::Device, - error::{code::*, from_err_ptr, to_result, Result, VTABLE_DEFAULT_ERROR= }, + devres::Devres, + error::{code::*, from_err_ptr, from_result, to_result, Result, VTABLE_= DEFAULT_ERROR}, ffi::c_ulong, prelude::*, types::ForeignOwnable, @@ -21,9 +22,11 @@ }; =20 use core::{ + cell::UnsafeCell, + marker::PhantomData, ops::{Deref, DerefMut}, pin::Pin, - ptr::self, + ptr, }; =20 use macros::vtable; @@ -792,3 +795,526 @@ fn register_em(_policy: &mut Policy) { build_error!(VTABLE_DEFAULT_ERROR) } } + +/// CPU frequency driver Registration. +/// +/// ## Examples +/// +/// The following example demonstrates how to register a cpufreq driver. +/// +/// ``` +/// use kernel::{ +/// c_str, +/// cpu, cpufreq, +/// device::Device, +/// macros::vtable, +/// sync::Arc, +/// }; +/// struct FooDevice; +/// +/// #[derive(Default)] +/// struct FooDriver; +/// +/// #[vtable] +/// impl cpufreq::Driver for FooDriver { +/// type Data =3D (); +/// type PData =3D Arc; +/// +/// fn init(policy: &mut cpufreq::Policy) -> Result { +/// // Initialize here +/// Ok(Arc::new(FooDevice, GFP_KERNEL)?) +/// } +/// +/// fn exit(_policy: &mut cpufreq::Policy, _data: Option)= -> Result<()> { +/// Ok(()) +/// } +/// +/// fn suspend(policy: &mut cpufreq::Policy) -> Result<()> { +/// policy.generic_suspend() +/// } +/// +/// fn verify(data: &mut cpufreq::PolicyData) -> Result<()> { +/// data.generic_verify() +/// } +/// +/// fn target_index(policy: &mut cpufreq::Policy, index: u32) -> Resul= t<()> { +/// // Update CPU frequency +/// Ok(()) +/// } +/// +/// fn get(policy: &mut cpufreq::Policy) -> Result { +/// policy.generic_get() +/// } +/// } +/// +/// fn foo_probe(dev: &Device) { +/// cpufreq::Registration::::new_foreign_owned( +/// dev, +/// c_str!("cpufreq-foo"), +/// (), +/// cpufreq::flags::NEED_INITIAL_FREQ_CHECK | cpufreq::flags::IS_C= OOLING_DEV, +/// true, +/// ).unwrap(); +/// } +/// ``` +pub struct Registration { + drv: KBox>, + _p: PhantomData, +} + +// SAFETY: `Registration` doesn't offer any methods or access to fields wh= en shared between threads +// or CPUs, so it is safe to share it. +unsafe impl Sync for Registration {} + +#[allow(clippy::non_send_fields_in_send_ty)] +// SAFETY: Registration with and unregistration from the cpufreq subsystem= can happen from any +// thread. Additionally, `T::Data` (which is dropped during unregistratio= n) is `Send`, so it is +// okay to move `Registration` to different threads. +unsafe impl Send for Registration {} + +impl Registration { + /// Registers a CPU frequency driver with the cpufreq core. + pub fn new(name: &'static CStr, data: T::Data, flags: u16, boost: bool= ) -> Result { + // Required due to Rust 1.82's stricter handling of `unsafe` in mu= table statics. The + // `unsafe` blocks aren't required anymore with later versions. + #![allow(unused_unsafe)] + + let mut drv =3D KBox::new( + UnsafeCell::new(bindings::cpufreq_driver::default()), + GFP_KERNEL, + )?; + let drv_ref =3D drv.get_mut(); + + // Account for the trailing null byte. + let len =3D name.len() + 1; + if len > drv_ref.name.len() { + return Err(EINVAL); + }; + + // SAFETY: `name` is a valid `CStr`, and we are copying it to an a= rray of equal or larger + // size. + let name =3D unsafe { &*(name.as_bytes_with_nul() as *const [u8]) = }; + drv_ref.name[..len].copy_from_slice(name); + + drv_ref.boost_enabled =3D boost; + drv_ref.flags =3D flags; + + // Initialize mandatory callbacks. + drv_ref.init =3D Some(Self::init_callback); + drv_ref.verify =3D Some(Self::verify_callback); + + // Initialize optional callbacks based on the traits of `T`. + drv_ref.setpolicy =3D if T::HAS_SETPOLICY { + Some(Self::setpolicy_callback) + } else { + None + }; + drv_ref.target =3D if T::HAS_TARGET { + Some(Self::target_callback) + } else { + None + }; + drv_ref.target_index =3D if T::HAS_TARGET_INDEX { + Some(Self::target_index_callback) + } else { + None + }; + drv_ref.fast_switch =3D if T::HAS_FAST_SWITCH { + Some(Self::fast_switch_callback) + } else { + None + }; + drv_ref.adjust_perf =3D if T::HAS_ADJUST_PERF { + Some(Self::adjust_perf_callback) + } else { + None + }; + drv_ref.get_intermediate =3D if T::HAS_GET_INTERMEDIATE { + Some(Self::get_intermediate_callback) + } else { + None + }; + drv_ref.target_intermediate =3D if T::HAS_TARGET_INTERMEDIATE { + Some(Self::target_intermediate_callback) + } else { + None + }; + drv_ref.get =3D if T::HAS_GET { + Some(Self::get_callback) + } else { + None + }; + drv_ref.update_limits =3D if T::HAS_UPDATE_LIMITS { + Some(Self::update_limits_callback) + } else { + None + }; + drv_ref.bios_limit =3D if T::HAS_BIOS_LIMIT { + Some(Self::bios_limit_callback) + } else { + None + }; + drv_ref.online =3D if T::HAS_ONLINE { + Some(Self::online_callback) + } else { + None + }; + drv_ref.offline =3D if T::HAS_OFFLINE { + Some(Self::offline_callback) + } else { + None + }; + drv_ref.exit =3D if T::HAS_EXIT { + Some(Self::exit_callback) + } else { + None + }; + drv_ref.suspend =3D if T::HAS_SUSPEND { + Some(Self::suspend_callback) + } else { + None + }; + drv_ref.resume =3D if T::HAS_RESUME { + Some(Self::resume_callback) + } else { + None + }; + drv_ref.ready =3D if T::HAS_READY { + Some(Self::ready_callback) + } else { + None + }; + drv_ref.set_boost =3D if T::HAS_SET_BOOST { + Some(Self::set_boost_callback) + } else { + None + }; + drv_ref.register_em =3D if T::HAS_REGISTER_EM { + Some(Self::register_em_callback) + } else { + None + }; + + // Set driver data before registering the driver, as the cpufreq c= ore calls few callbacks + // before `cpufreq_register_driver` returns. + Self::set_data(drv_ref, data)?; + + // SAFETY: It is safe to register the driver with the cpufreq core= in the kernel C code. + to_result(unsafe { bindings::cpufreq_register_driver(drv_ref) })?; + + Ok(Self { + drv, + _p: PhantomData, + }) + } + + /// Same as [`Registration::new`], but does not return a [`Registratio= n`] instance. + /// + /// Instead the [`Registration`] is owned by [`Devres`] and will be re= voked / dropped, once the + /// device is detached. + pub fn new_foreign_owned( + dev: &Device, + name: &'static CStr, + data: T::Data, + flags: u16, + boost: bool, + ) -> Result<()> { + Devres::new_foreign_owned(dev, Self::new(name, data, flags, boost)= ?, GFP_KERNEL)?; + Ok(()) + } + + // Sets the `Data` for the CPU frequency driver. + fn set_data(drv: &mut bindings::cpufreq_driver, data: T::Data) -> Resu= lt<()> { + if drv.driver_data.is_null() { + // Transfer the ownership of the data to the C code. + drv.driver_data =3D ::into_foreign(= data) as _; + Ok(()) + } else { + Err(EBUSY) + } + } + + /// Returns borrowed `Data` previously set for the CPU frequency drive= r. + pub fn data(&mut self) -> Option<::Borrowed= <'static>> { + let drv =3D self.drv.get_mut(); + + if drv.driver_data.is_null() { + None + } else { + // SAFETY: The data is earlier set by us from `set_data`. + Some(unsafe { ::borrow(drv.driver_d= ata) }) + } + } + + // Clears and returns the `Data` for the CPU frequency driver. + fn clear_data(&mut self) -> Option { + let drv =3D self.drv.get_mut(); + + if drv.driver_data.is_null() { + None + } else { + // SAFETY: The data is earlier set by us from `set_data`. + let data =3D Some(unsafe { ::from_f= oreign(drv.driver_data) }); + drv.driver_data =3D ptr::null_mut(); + data + } + } +} + +// CPU frequency driver callbacks. +impl Registration { + // Driver's `init` callback. + // + // SAFETY: Called from C. Inputs must be valid pointers. + extern "C" fn init_callback(ptr: *mut bindings::cpufreq_policy) -> ker= nel::ffi::c_int { + from_result(|| { + // SAFETY: The `ptr` is guaranteed to be valid by the contract= with the C code for the + // lifetime of `policy`. + let policy =3D unsafe { Policy::from_raw_mut(ptr) }; + + let data =3D T::init(policy)?; + policy.set_data(data)?; + Ok(0) + }) + } + + // Driver's `exit` callback. + // + // SAFETY: Called from C. Inputs must be valid pointers. + extern "C" fn exit_callback(ptr: *mut bindings::cpufreq_policy) { + // SAFETY: The `ptr` is guaranteed to be valid by the contract wit= h the C code for the + // lifetime of `policy`. + let policy =3D unsafe { Policy::from_raw_mut(ptr) }; + + let data =3D policy.clear_data(); + let _ =3D T::exit(policy, data); + } + + // Driver's `online` callback. + // + // SAFETY: Called from C. Inputs must be valid pointers. + extern "C" fn online_callback(ptr: *mut bindings::cpufreq_policy) -> k= ernel::ffi::c_int { + from_result(|| { + // SAFETY: The `ptr` is guaranteed to be valid by the contract= with the C code for the + // lifetime of `policy`. + let policy =3D unsafe { Policy::from_raw_mut(ptr) }; + T::online(policy).map(|()| 0) + }) + } + + // Driver's `offline` callback. + // + // SAFETY: Called from C. Inputs must be valid pointers. + extern "C" fn offline_callback(ptr: *mut bindings::cpufreq_policy) -> = kernel::ffi::c_int { + from_result(|| { + // SAFETY: The `ptr` is guaranteed to be valid by the contract= with the C code for the + // lifetime of `policy`. + let policy =3D unsafe { Policy::from_raw_mut(ptr) }; + T::offline(policy).map(|()| 0) + }) + } + + // Driver's `suspend` callback. + // + // SAFETY: Called from C. Inputs must be valid pointers. + extern "C" fn suspend_callback(ptr: *mut bindings::cpufreq_policy) -> = kernel::ffi::c_int { + from_result(|| { + // SAFETY: The `ptr` is guaranteed to be valid by the contract= with the C code for the + // lifetime of `policy`. + let policy =3D unsafe { Policy::from_raw_mut(ptr) }; + T::suspend(policy).map(|()| 0) + }) + } + + // Driver's `resume` callback. + // + // SAFETY: Called from C. Inputs must be valid pointers. + extern "C" fn resume_callback(ptr: *mut bindings::cpufreq_policy) -> k= ernel::ffi::c_int { + from_result(|| { + // SAFETY: The `ptr` is guaranteed to be valid by the contract= with the C code for the + // lifetime of `policy`. + let policy =3D unsafe { Policy::from_raw_mut(ptr) }; + T::resume(policy).map(|()| 0) + }) + } + + // Driver's `ready` callback. + // + // SAFETY: Called from C. Inputs must be valid pointers. + extern "C" fn ready_callback(ptr: *mut bindings::cpufreq_policy) { + // SAFETY: The `ptr` is guaranteed to be valid by the contract wit= h the C code for the + // lifetime of `policy`. + let policy =3D unsafe { Policy::from_raw_mut(ptr) }; + T::ready(policy); + } + + // Driver's `verify` callback. + // + // SAFETY: Called from C. Inputs must be valid pointers. + extern "C" fn verify_callback(ptr: *mut bindings::cpufreq_policy_data)= -> kernel::ffi::c_int { + from_result(|| { + // SAFETY: The `ptr` is guaranteed to be valid by the contract= with the C code for the + // lifetime of `policy`. + let data =3D unsafe { PolicyData::from_raw_mut(ptr) }; + T::verify(data).map(|()| 0) + }) + } + + // Driver's `setpolicy` callback. + // + // SAFETY: Called from C. Inputs must be valid pointers. + extern "C" fn setpolicy_callback(ptr: *mut bindings::cpufreq_policy) -= > kernel::ffi::c_int { + from_result(|| { + // SAFETY: The `ptr` is guaranteed to be valid by the contract= with the C code for the + // lifetime of `policy`. + let policy =3D unsafe { Policy::from_raw_mut(ptr) }; + T::setpolicy(policy).map(|()| 0) + }) + } + + // Driver's `target` callback. + // + // SAFETY: Called from C. Inputs must be valid pointers. + extern "C" fn target_callback( + ptr: *mut bindings::cpufreq_policy, + target_freq: u32, + relation: u32, + ) -> kernel::ffi::c_int { + from_result(|| { + // SAFETY: The `ptr` is guaranteed to be valid by the contract= with the C code for the + // lifetime of `policy`. + let policy =3D unsafe { Policy::from_raw_mut(ptr) }; + T::target(policy, target_freq, Relation::new(relation)?).map(|= ()| 0) + }) + } + + // Driver's `target_index` callback. + // + // SAFETY: Called from C. Inputs must be valid pointers. + extern "C" fn target_index_callback( + ptr: *mut bindings::cpufreq_policy, + index: u32, + ) -> kernel::ffi::c_int { + from_result(|| { + // SAFETY: The `ptr` is guaranteed to be valid by the contract= with the C code for the + // lifetime of `policy`. + let policy =3D unsafe { Policy::from_raw_mut(ptr) }; + T::target_index(policy, index).map(|()| 0) + }) + } + + // Driver's `fast_switch` callback. + // + // SAFETY: Called from C. Inputs must be valid pointers. + extern "C" fn fast_switch_callback( + ptr: *mut bindings::cpufreq_policy, + target_freq: u32, + ) -> kernel::ffi::c_uint { + // SAFETY: The `ptr` is guaranteed to be valid by the contract wit= h the C code for the + // lifetime of `policy`. + let policy =3D unsafe { Policy::from_raw_mut(ptr) }; + T::fast_switch(policy, target_freq) + } + + // Driver's `adjust_perf` callback. + extern "C" fn adjust_perf_callback( + cpu: u32, + min_perf: usize, + target_perf: usize, + capacity: usize, + ) { + if let Ok(mut policy) =3D PolicyCpu::from_cpu(cpu) { + T::adjust_perf(&mut policy, min_perf, target_perf, capacity); + } + } + + // Driver's `get_intermediate` callback. + // + // SAFETY: Called from C. Inputs must be valid pointers. + extern "C" fn get_intermediate_callback( + ptr: *mut bindings::cpufreq_policy, + index: u32, + ) -> kernel::ffi::c_uint { + // SAFETY: The `ptr` is guaranteed to be valid by the contract wit= h the C code for the + // lifetime of `policy`. + let policy =3D unsafe { Policy::from_raw_mut(ptr) }; + T::get_intermediate(policy, index) + } + + // Driver's `target_intermediate` callback. + // + // SAFETY: Called from C. Inputs must be valid pointers. + extern "C" fn target_intermediate_callback( + ptr: *mut bindings::cpufreq_policy, + index: u32, + ) -> kernel::ffi::c_int { + from_result(|| { + // SAFETY: The `ptr` is guaranteed to be valid by the contract= with the C code for the + // lifetime of `policy`. + let policy =3D unsafe { Policy::from_raw_mut(ptr) }; + T::target_intermediate(policy, index).map(|()| 0) + }) + } + + // Driver's `get` callback. + extern "C" fn get_callback(cpu: u32) -> kernel::ffi::c_uint { + PolicyCpu::from_cpu(cpu).map_or(0, |mut policy| T::get(&mut policy= ).map_or(0, |f| f)) + } + + // Driver's `update_limit` callback. + extern "C" fn update_limits_callback(cpu: u32) { + if let Ok(mut policy) =3D PolicyCpu::from_cpu(cpu) { + T::update_limits(&mut policy); + } + } + + // Driver's `bios_limit` callback. + // + // SAFETY: Called from C. Inputs must be valid pointers. + extern "C" fn bios_limit_callback(cpu: i32, limit: *mut u32) -> kernel= ::ffi::c_int { + from_result(|| { + let mut policy =3D PolicyCpu::from_cpu(cpu as u32)?; + + // SAFETY: `limit` is guaranteed by the C code to be valid. + T::bios_limit(&mut policy, &mut (unsafe { *limit })).map(|()| = 0) + }) + } + + // Driver's `set_boost` callback. + // + // SAFETY: Called from C. Inputs must be valid pointers. + extern "C" fn set_boost_callback( + ptr: *mut bindings::cpufreq_policy, + state: i32, + ) -> kernel::ffi::c_int { + from_result(|| { + // SAFETY: The `ptr` is guaranteed to be valid by the contract= with the C code for the + // lifetime of `policy`. + let policy =3D unsafe { Policy::from_raw_mut(ptr) }; + T::set_boost(policy, state).map(|()| 0) + }) + } + + // Driver's `register_em` callback. + // + // SAFETY: Called from C. Inputs must be valid pointers. + extern "C" fn register_em_callback(ptr: *mut bindings::cpufreq_policy)= { + // SAFETY: The `ptr` is guaranteed to be valid by the contract wit= h the C code for the + // lifetime of `policy`. + let policy =3D unsafe { Policy::from_raw_mut(ptr) }; + T::register_em(policy); + } +} + +impl Drop for Registration { + // Removes the `Registration` from the kernel, if it has initialized s= uccessfully earlier. + fn drop(&mut self) { + let drv =3D self.drv.get_mut(); + + // SAFETY: The driver was earlier registered from `new`. + unsafe { bindings::cpufreq_unregister_driver(drv) }; + + // Free data + drop(self.clear_data()); + } +} --=20 2.31.1.272.g89b43f80a514