Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp956060lqt; Fri, 7 Jun 2024 04:04:34 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWOdVJw22b87GxIkWrHDxzqQbaTNiPv/6CxlyrbZ+n6eI1nkvSnuBA6EJiMgmM2zFOx78+kG0EvjSNPWbfuRR8K+LiXl7OZXcVCfoZt9g== X-Google-Smtp-Source: AGHT+IEgLfHpVfVAPd+WKRIqLnuMToEsLKeykZt5ntwvEiBWW9Td1Cra3YGh0WzjaTnln3GJrVsZ X-Received: by 2002:a05:6214:5d0b:b0:6af:c111:63b5 with SMTP id 6a1803df08f44-6b059b709c5mr22995366d6.12.1717758273988; Fri, 07 Jun 2024 04:04:33 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717758273; cv=pass; d=google.com; s=arc-20160816; b=X7UJHo5+ny4xAdrqtw34RmiqMUevzy/U3TClmXAxMLl41mf2rLnHvqtzUcN/Kklgfo bjeAxAfnQ2P++iAeooPBXT0Svn+33ENSfEQhu4I1oUOBXm6c9SijOiGr/RB6P5OIZI6B jVJF4e26XHOBz/e24HQMWkuLaQi+/DdGv+SPC0PEBpMSA+IiyY9jQ1wiD6C6yfWFOIbs r6pJet1GPgiarDMVXgwuULKs4pkVl9ebZqqYD/wCKVxoBXPhjqPTQGlN3j9qjAVclpnB 5BeTnWMHYv9s6HG56BwaIZyc+nhkcn505Wgb2jW8Ft+e2+U6fL+ng5nhg4FlwlVYwwDi VPiw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:in-reply-to:references :user-agent:subject:cc:to:from:date:dkim-signature; bh=dlz1M1Q96MoJTbBHiqN6xZFkx4A4jAugPwmqHEAfQlM=; fh=vhdxzVKNJXV3a8F4JMFSgaCxGaxL5Bw6piAgg034GOs=; b=nup/NAHMUB0JzwV8fBX6Ch2K7r9bUd0SjVh9WBxk6yk4H2I5jxbX5raQLYMZBEpWSR gfOu+TT5zWg4df9uBWHnIgZHh15OYUM6SJ1WAKXucymx3rdH1ioUUTCvYutG1RBzanRx 3bsslgsFljsBGMLa066bS9aj7M1jBKtLn73tlbqyfdgUz6eZZR8MbUY15rICZOYfV/5D B6ntSewWYNwNDiVZKB3lUyI7bx+1C2cdEPAaeJW5cNPHZigre4s7XkqoUBEBVVTUwI1H La1UcdDUpqPUHT9GC9SszGPyzbyDDJQQosY+kSqtgc/etes/JWpvzoMP0kvO9BUOi257 QLew==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=sx8ROLsc; arc=pass (i=1 spf=pass spfdomain=linaro.org dkim=pass dkdomain=linaro.org dmarc=pass fromdomain=linaro.org); spf=pass (google.com: domain of linux-kernel+bounces-205866-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205866-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id 6a1803df08f44-6b04f998355si37592596d6.353.2024.06.07.04.04.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 04:04:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-205866-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=sx8ROLsc; arc=pass (i=1 spf=pass spfdomain=linaro.org dkim=pass dkdomain=linaro.org dmarc=pass fromdomain=linaro.org); spf=pass (google.com: domain of linux-kernel+bounces-205866-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205866-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 8D76D1C2321F for ; Fri, 7 Jun 2024 11:04:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 728B718732F; Fri, 7 Jun 2024 11:04:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="sx8ROLsc" Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (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 C0E531862B8 for ; Fri, 7 Jun 2024 11:04:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717758261; cv=none; b=S7870jDu5k9eurMQSbXdxLNher6QprO/2WCT6T1TzN7U9BLfM7YRrpzwUyu3Dgn6ylA8Nkr0g2GwbaqBNGos4zVWhtglZPQc7voaLwQ9NGRZlKpbQ3qck5PuWvaOf2i93BGZ+yr0Nv3q0RD0Smil5kWhWlQtkvspcCDKMFwcuUo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717758261; c=relaxed/simple; bh=AN/mXFLmmOOxh4ohQqh0I5EZsgTEbPwsV7LU8Ha71SI=; h=Date:From:To:Cc:Subject:References:In-Reply-To:Message-ID: MIME-Version:Content-Type; b=s7IwsCJ2ddaZfB/06T6KiF7+a9PDf1i8JsDhwCMo/20+Z2U+94tonD2wmGSqbDQiVe2K8Yvl8x7OVVu73culdDwTTE5AR1w4tdveM3QkJ+M3a6QMkRgpN4PjCk3+M6O40tCHlNWOBDZ5kAp6/dYTazl34HOP3fZ8Bgj5Dawevf4= 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=sx8ROLsc; arc=none smtp.client-ip=209.85.128.48 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 Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-4217136a74dso2771585e9.2 for ; Fri, 07 Jun 2024 04:04:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1717758258; x=1718363058; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:in-reply-to :references:user-agent:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to; bh=dlz1M1Q96MoJTbBHiqN6xZFkx4A4jAugPwmqHEAfQlM=; b=sx8ROLscim+16/5RnC+EJHtkqutKqN66BekfmIea1CLQhllcXogYZaoxQi8OF/AFFb LdpWUCQFpqsnLrV5UlB2nE3NOkXOxvhgnlViZjHiyQxI6wEbEy/RZGJvt3Fc2JixA7gP cIy67SSNzLIGMq8jmWLPb1uuYlgiDIOoIXuWzYBT0fVeHMrzeUVNzOS/57nTalCcXOxC ti8HfZnqu7yar+kDZ154joNwXa8SWV9heVAVfXVM4rGrBdii1yK2E7+jzIMuDH/G8IeL vcrqYUIcv8nrBx8cWUZRRcgVeT1zSM2opxHYaE2AdckFqqpzQe9JxLgYZ4GqD4/BOCXh chKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717758258; x=1718363058; h=content-transfer-encoding:mime-version:message-id:in-reply-to :references:user-agent:subject:cc:to:from:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=dlz1M1Q96MoJTbBHiqN6xZFkx4A4jAugPwmqHEAfQlM=; b=nG/OIY+MrfIG/9y3dWA0B8Z8GbRIgfkkHDKxdc2DUv42g5LTaa6xVdQIJ+kNzZvcz9 DHgi8Zlgj6oC+f2aopSacIBQa0tsIxLMMZUIKIAvSaOT3VcW57D7dcv7UFGZr5XAZDDd /AgryC8h5xhXO1MQBCnz57PbI1aeGcf5901Iwj1bdglUVrcllEHMUlE8f5UWtEDSbiGd Nr9hWLQLb4PHaSW73uZEjvvvUsnx/JItaI4FmgnLOAShQrOgzaE4Uci3y8j4DFqwv8dq FyV1BRBmOjayNBedoIGUt0ueAhBK3c5ASbjM0C/KSiQx7redQEqslayZnjLQY2NQRC8o 9Wzg== X-Forwarded-Encrypted: i=1; AJvYcCUmkR6ShsbtPm+C6Quoc7lvo5ooA6xg1VxIcs7FJvSd29JhKjvYnpJx00r2CpeMbZhSIP9hAQqdSEjGmsGPzbF/Jg7ltvcgax9WXVFK X-Gm-Message-State: AOJu0Yz4eoQERadHiPoxnH+5nE6XWusLdkLaD9W6A8fLVVwmbjCzug1V 5Lnip1V9VT3z30tYo2eT4rv965M8Z1aVq2w7z453unIzQ8jaihcioz0/p6mOJbA= X-Received: by 2002:a05:600c:4f8e:b0:41b:f022:8e69 with SMTP id 5b1f17b1804b1-42164a03160mr19707055e9.19.1717758258064; Fri, 07 Jun 2024 04:04:18 -0700 (PDT) Received: from meli-email.org (adsl-105.37.6.1.tellas.gr. [37.6.1.105]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4215809fcbesm84707525e9.0.2024.06.07.04.04.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 04:04:17 -0700 (PDT) Date: Fri, 07 Jun 2024 13:38:17 +0300 From: Manos Pitsidianakis To: Viresh Kumar , "Rafael J. Wysocki" , Miguel Ojeda , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , Bj=?UTF-8?B?w7Y=?= rn Roy Baron , Benno Lossin , Andreas Hindborg , Alice Ryhl Cc: Viresh Kumar , linux-pm@vger.kernel.org, Vincent Guittot , Stephen Boyd , Nishanth Menon , rust-for-linux@vger.kernel.org, Manos Pitsidianakis , Erik Schilling , Alex Benn=?UTF-8?B?w6k=?= e , Joakim Bech , Rob Herring , linux-kernel@vger.kernel.org Subject: Re: [RFC PATCH V2 2/8] rust: Extend OPP bindings for the OPP table User-Agent: meli 0.8.5 References: In-Reply-To: Message-ID: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=utf-8; format=flowed On Fri, 07 Jun 2024 12:12, Viresh Kumar wrote: >This extends OPP bindings with the bindings for the `struct opp_table`. > >Signed-off-by: Viresh Kumar >--- > rust/kernel/opp.rs | 374 ++++++++++++++++++++++++++++++++++++++++++++- > 1 file changed, 372 insertions(+), 2 deletions(-) > >diff --git a/rust/kernel/opp.rs b/rust/kernel/opp.rs >index 9e5cf0412ed5..06f36845047f 100644 >--- a/rust/kernel/opp.rs >+++ b/rust/kernel/opp.rs >@@ -7,9 +7,9 @@ > //! C header: [`include/linux/pm_opp.h`](../../../../../../include/linux/pm_opp.h) > > use crate::{ >- bindings, >+ bindings, cpumask, > device::Device, >- error::{code::*, to_result, Result}, >+ error::{code::*, from_err_ptr, to_result, Error, Result}, > types::{ARef, AlwaysRefCounted, Opaque}, > }; > >@@ -31,6 +31,376 @@ pub fn new(freq: u64, u_volt: u64, level: u32, turbo: bool) -> Self { > } > } > >+/// OPP search types. >+#[derive(Copy, Clone, Debug, Eq, PartialEq)] >+pub enum SearchType { >+ /// Search for exact value. >+ Exact, >+ /// Search for highest value less than equal to value. >+ Floor, >+ /// Search for lowest value greater than equal to value. >+ Ceil, >+} Seeing this enum made me think about memory layouts which are not stable in Rust and can change between compilations unless they have a specific `repr`. Not related to this series directly, has there been discussion about guaranteeing struct layouts in kernel APIs? It'd require a lot of things to happen to cause a problem (multiple users of an API in the kernel in separate compilation units maybe even compiled with different rustc versions). >+ >+/// Operating performance point (OPP) table. >+/// >+/// # Invariants >+/// >+/// The pointer stored in `Self` is non-null and valid for the lifetime of the ARef instance. In >+/// particular, the ARef instance owns an increment on underlying object’s reference count. >+pub struct Table { >+ ptr: *mut bindings::opp_table, >+ dev: ARef, >+ em: bool, >+ of: bool, >+ cpumask: Option, >+} >+ >+// SAFETY: The fields of `Table` are safe to be used from any thread. >+unsafe impl Send for Table {} >+ >+// SAFETY: The fields of `Table` are safe to be referenced from any thread. >+unsafe impl Sync for Table {} >+ >+impl Table { >+ /// Creates a new OPP table instance from raw pointer. >+ /// >+ /// # Safety >+ /// >+ /// Callers must ensure that `ptr` is valid and non-null. >+ unsafe fn from_ptr(ptr: *mut bindings::opp_table, dev: ARef) -> Self { >+ // SAFETY: By the safety requirements, ptr is valid and its refcount will be incremented. >+ unsafe { bindings::dev_pm_opp_get_opp_table_ref(ptr) }; >+ >+ Self { >+ ptr, >+ dev, >+ em: false, >+ of: false, >+ cpumask: None, >+ } >+ } >+ >+ /// Find OPP table from device. >+ pub fn from_dev(dev: ARef) -> Result { >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety >+ // requirements. Refcount of the OPP table is incremented as well. >+ let ptr = from_err_ptr(unsafe { bindings::dev_pm_opp_get_opp_table(dev.as_raw()) })?; >+ >+ Ok(Self { >+ ptr, >+ dev: dev.clone(), Clone is not probably not needed here, right? the argument value will be dropped after this. >+ em: false, >+ of: false, >+ cpumask: None, >+ }) >+ } >+ >+ /// Add device tree based OPP table for the device. >+ #[cfg(CONFIG_OF)] >+ pub fn from_of(dev: ARef, index: i32) -> Result { >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety >+ // requirements. Refcount of the OPP table is incremented as well. >+ to_result(unsafe { bindings::dev_pm_opp_of_add_table_indexed(dev.as_raw(), index) })?; >+ >+ // Fetch the newly created table. >+ let mut table = Self::from_dev(dev)?; >+ table.of = true; >+ >+ Ok(table) >+ } >+ >+ // Remove device tree based OPP table for the device. >+ #[cfg(CONFIG_OF)] >+ fn remove_of(&self) { >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety >+ // requirements. We took the reference from `from_of` earlier, it is safe to drop the same >+ // now. >+ unsafe { bindings::dev_pm_opp_of_remove_table(self.dev.as_raw()) }; >+ } >+ >+ /// Add device tree based OPP table for CPU devices. >+ #[cfg(CONFIG_OF)] >+ pub fn from_of_cpumask(dev: ARef, cpumask: &mut cpumask::Cpumask) -> Result { >+ // SAFETY: The cpumask is valid and the returned ptr will be owned by the [`Table`] instance. >+ to_result(unsafe { bindings::dev_pm_opp_of_cpumask_add_table(cpumask.as_ptr()) })?; >+ >+ // Fetch the newly created table. >+ let mut table = Self::from_dev(dev)?; >+ // SAFETY: The `cpumask` is guaranteed by the C code to be valid. >+ table.cpumask = Some(unsafe { cpumask::Cpumask::new(cpumask.as_mut_ptr()) }); >+ >+ Ok(table) >+ } >+ >+ // Remove device tree based OPP table for CPU devices. >+ #[cfg(CONFIG_OF)] >+ fn remove_of_cpumask(&self, cpumask: &cpumask::Cpumask) { >+ // SAFETY: The cpumask is valid and we took the reference from `from_of_cpumask` earlier, >+ // it is safe to drop the same now. >+ unsafe { bindings::dev_pm_opp_of_cpumask_remove_table(cpumask.as_ptr()) }; >+ } >+ >+ /// Returns the number of OPPs in the table. >+ pub fn opp_count(&self) -> Result { >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety >+ // requirements. >+ let ret = unsafe { bindings::dev_pm_opp_get_opp_count(self.dev.as_raw()) }; >+ if ret < 0 { >+ Err(Error::from_errno(ret)) >+ } else { >+ Ok(ret as u32) >+ } >+ } >+ >+ /// Returns max clock latency of the OPPs in the table. >+ pub fn max_clock_latency(&self) -> u64 { >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety >+ // requirements. >+ unsafe { bindings::dev_pm_opp_get_max_clock_latency(self.dev.as_raw()) } >+ } >+ >+ /// Returns max volt latency of the OPPs in the table. >+ pub fn max_volt_latency(&self) -> u64 { >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety >+ // requirements. >+ unsafe { bindings::dev_pm_opp_get_max_volt_latency(self.dev.as_raw()) } >+ } >+ >+ /// Returns max transition latency of the OPPs in the table. >+ pub fn max_transition_latency(&self) -> u64 { >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety >+ // requirements. >+ unsafe { bindings::dev_pm_opp_get_max_transition_latency(self.dev.as_raw()) } >+ } >+ >+ /// Returns the suspend OPP. >+ pub fn suspend_freq(&self) -> u64 { >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety >+ // requirements. >+ unsafe { bindings::dev_pm_opp_get_suspend_opp_freq(self.dev.as_raw()) } >+ } >+ >+ /// Synchronizes regulators used by the OPP table. >+ pub fn sync_regulators(&self) -> Result<()> { >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety >+ // requirements. >+ to_result(unsafe { bindings::dev_pm_opp_sync_regulators(self.dev.as_raw()) }) >+ } >+ >+ /// Gets sharing CPUs. >+ pub fn sharing_cpus(dev: ARef, cpumask: &mut cpumask::Cpumask) -> Result<()> { >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety >+ // requirements. >+ to_result(unsafe { >+ bindings::dev_pm_opp_get_sharing_cpus(dev.as_raw(), cpumask.as_mut_ptr()) >+ }) >+ } >+ >+ /// Sets sharing CPUs. >+ pub fn set_sharing_cpus(&self, cpumask: &cpumask::Cpumask) -> Result<()> { >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety >+ // requirements. >+ to_result(unsafe { >+ bindings::dev_pm_opp_set_sharing_cpus(self.dev.as_raw(), cpumask.as_ptr()) >+ }) >+ } >+ >+ /// Gets sharing CPUs from Device tree. >+ #[cfg(CONFIG_OF)] >+ pub fn of_sharing_cpus(dev: ARef, cpumask: &mut cpumask::Cpumask) -> Result<()> { >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety >+ // requirements. >+ to_result(unsafe { >+ bindings::dev_pm_opp_of_get_sharing_cpus(dev.as_raw(), cpumask.as_mut_ptr()) >+ }) >+ } >+ >+ /// Updates the voltage value for an OPP. >+ pub fn adjust_voltage( >+ &self, >+ freq: u64, >+ u_volt: u64, >+ u_volt_min: u64, >+ u_volt_max: u64, >+ ) -> Result<()> { >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety >+ // requirements. >+ to_result(unsafe { >+ bindings::dev_pm_opp_adjust_voltage( >+ self.dev.as_raw(), >+ freq, >+ u_volt, >+ u_volt_min, >+ u_volt_max, >+ ) >+ }) >+ } >+ >+ /// Sets a matching OPP based on frequency. >+ pub fn set_rate(&self, freq: u64) -> Result<()> { >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety >+ // requirements. >+ to_result(unsafe { bindings::dev_pm_opp_set_rate(self.dev.as_raw(), freq) }) >+ } >+ >+ /// Sets exact OPP. >+ pub fn set_opp(&self, opp: ARef) -> Result<()> { >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety >+ // requirements. >+ to_result(unsafe { bindings::dev_pm_opp_set_opp(self.dev.as_raw(), opp.as_mut_ptr()) }) >+ } >+ >+ /// Finds OPP based on frequency. >+ pub fn opp_from_freq( >+ &self, >+ mut freq: u64, >+ available: Option, >+ index: Option, >+ stype: SearchType, >+ ) -> Result> { >+ let rdev = self.dev.as_raw(); >+ let index = index.unwrap_or(0); >+ >+ let ptr = from_err_ptr(match stype { >+ SearchType::Exact => { >+ if let Some(available) = available { >+ // SAFETY: The requirements are satisfied by the existence of `Device` and >+ // its safety requirements. The returned ptr will be owned by the new [`OPP`] >+ // instance. >+ unsafe { >+ bindings::dev_pm_opp_find_freq_exact_indexed(rdev, freq, index, available) >+ } >+ } else { >+ return Err(EINVAL); >+ } >+ } >+ >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its >+ // safety requirements. The returned ptr will be owned by the new [`OPP`] instance. >+ SearchType::Ceil => unsafe { >+ bindings::dev_pm_opp_find_freq_ceil_indexed(rdev, &mut freq as *mut u64, index) >+ }, >+ >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its >+ // safety requirements. The returned ptr will be owned by the new [`OPP`] instance. >+ SearchType::Floor => unsafe { >+ bindings::dev_pm_opp_find_freq_floor_indexed(rdev, &mut freq as *mut u64, index) >+ }, >+ })?; >+ >+ // SAFETY: The `ptr` is guaranteed by the C code to be valid. >+ unsafe { OPP::from_ptr_owned(ptr) } >+ } >+ >+ /// Finds OPP based on level. >+ pub fn opp_from_level(&self, mut level: u32, stype: SearchType) -> Result> { >+ let rdev = self.dev.as_raw(); >+ >+ let ptr = from_err_ptr(match stype { >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its >+ // safety requirements. The returned ptr will be owned by the new [`OPP`] instance. >+ SearchType::Exact => unsafe { bindings::dev_pm_opp_find_level_exact(rdev, level) }, >+ Minor style comment, the empty lines between match patterns are unusual >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its >+ // safety requirements. The returned ptr will be owned by the new [`OPP`] instance. >+ SearchType::Ceil => unsafe { >+ bindings::dev_pm_opp_find_level_ceil(rdev, &mut level as *mut u32) >+ }, >+ >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its >+ // safety requirements. The returned ptr will be owned by the new [`OPP`] instance. >+ SearchType::Floor => unsafe { >+ bindings::dev_pm_opp_find_level_floor(rdev, &mut level as *mut u32) >+ }, >+ })?; >+ >+ // SAFETY: The `ptr` is guaranteed by the C code to be valid. >+ unsafe { OPP::from_ptr_owned(ptr) } >+ } >+ >+ /// Finds OPP based on bandwidth. >+ pub fn opp_from_bw(&self, mut bw: u32, index: i32, stype: SearchType) -> Result> { >+ let rdev = self.dev.as_raw(); >+ >+ let ptr = from_err_ptr(match stype { >+ // The OPP core doesn't support this yet. >+ SearchType::Exact => return Err(EINVAL), >+ >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its >+ // safety requirements. The returned ptr will be owned by the new [`OPP`] instance. >+ SearchType::Ceil => unsafe { >+ bindings::dev_pm_opp_find_bw_ceil(rdev, &mut bw as *mut u32, index) >+ }, >+ >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its >+ // safety requirements. The returned ptr will be owned by the new [`OPP`] instance. >+ SearchType::Floor => unsafe { >+ bindings::dev_pm_opp_find_bw_floor(rdev, &mut bw as *mut u32, index) >+ }, >+ })?; >+ >+ // SAFETY: The `ptr` is guaranteed by the C code to be valid. >+ unsafe { OPP::from_ptr_owned(ptr) } >+ } >+ >+ /// Enable the OPP. >+ pub fn enable_opp(&self, freq: u64) -> Result<()> { >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety >+ // requirements. >+ to_result(unsafe { bindings::dev_pm_opp_enable(self.dev.as_raw(), freq) }) >+ } >+ >+ /// Disable the OPP. >+ pub fn disable_opp(&self, freq: u64) -> Result<()> { >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety >+ // requirements. >+ to_result(unsafe { bindings::dev_pm_opp_disable(self.dev.as_raw(), freq) }) >+ } >+ >+ /// Registers with Energy model. >+ #[cfg(CONFIG_OF)] >+ pub fn of_register_em(&mut self, cpumask: &mut cpumask::Cpumask) -> Result<()> { >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety >+ // requirements. >+ to_result(unsafe { >+ bindings::dev_pm_opp_of_register_em(self.dev.as_raw(), cpumask.as_mut_ptr()) >+ })?; >+ >+ self.em = true; >+ Ok(()) >+ } >+ >+ // Unregisters with Energy model. >+ #[cfg(CONFIG_OF)] >+ fn of_unregister_em(&self) { >+ // SAFETY: The requirements are satisfied by the existence of `Device` and its safety >+ // requirements. We registered with the EM framework earlier, it is safe to unregister now. >+ unsafe { bindings::em_dev_unregister_perf_domain(self.dev.as_raw()) }; >+ } >+} >+ >+impl Drop for Table { >+ fn drop(&mut self) { >+ // SAFETY: By the type invariants, we know that `self` owns a reference, so it is safe >+ // to relinquish it now. >+ unsafe { bindings::dev_pm_opp_put_opp_table(self.ptr) }; >+ >+ #[cfg(CONFIG_OF)] >+ { >+ if self.em { >+ self.of_unregister_em(); >+ } >+ >+ if self.of { >+ self.remove_of(); >+ } else if let Some(cpumask) = &self.cpumask { >+ self.remove_of_cpumask(cpumask); >+ } >+ } >+ } >+} >+ > /// Operating performance point (OPP). > /// > /// # Invariants >-- >2.31.1.272.g89b43f80a514 >