From nobody Mon Dec 15 21:20:08 2025 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (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 C55E422F16E for ; Mon, 13 Jan 2025 11:23:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736767426; cv=none; b=Gye6KVxX+dzjPAj1GeVy37fQluZCf78xvPw6BLsCf8v/fSsR5uQhKzfkm4UnFWfm2cEvjY/N6A7NoHfBmBF6AFGQ17iS39eZ2w7BMAoJuIF7kEjHVnPoBc5GJX5jjt00aJSWLwCl+5a1L4xpE+Xold/js40YTfWhGlYqYrP9gDQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736767426; c=relaxed/simple; bh=Log7cXXjzMiJkcv5gL5n5WiPvymmPQBug9ZqKR86Yek=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MExGiOOHEkZq8qKd+2onu5fdgAoQrl6N9l3DgBkow919EsKChuwQyOC5vyAXu8dO+CvJRFMwkwkIn3CC+CsnUSZFkoKvFM4mX++vx5oZ6PyLtBW8X/A4qkW5uNIbnNWlywVEQxYUEUKpjZs0KHQTYNTblpeUNJvGfnKmWk0PmX0= 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=mNDWtdYm; arc=none smtp.client-ip=209.85.214.182 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="mNDWtdYm" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-21631789fcdso64510955ad.1 for ; Mon, 13 Jan 2025 03:23:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1736767424; x=1737372224; 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=tdn4m6dKlofswR8ctFShfrfS5TKeZlPBTra3kXQON/0=; b=mNDWtdYmQwesy81qm8USSreo7D38hLW3H26UTPJDW0KT9V4Xc2yow55rOKD8HrGwKK EA89HIE4gczQjdiWGS5a9AsRpkfk6j0xbI+aYf6jqAsuLfNEcazhlfzwSdE06+WJ96jh UOJdd7UGyVAKCY8h/cHkx6nyFlQOmDH8zV0nCkoL3eMUNVbCWGrO5IOKLEjtMKh2Hqge 1EbsTpNptfIbgBBoWfBocZ78KE/fNrq1JuygiMlnDwefnvKOv8k0/iPgsNQB9Y9TGum8 mIzHEIpT4Dt1F/PCVFV9nQfeMusgo3KKdQR9AU/kLpQkL4oS0WkRL7dkq+EfaXYNKCBV faBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736767424; x=1737372224; 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=tdn4m6dKlofswR8ctFShfrfS5TKeZlPBTra3kXQON/0=; b=KFwhQAejqAT/8C248SFHf7Ak/1lGLfUFhdqPLMhn5i2felhrkA7w4nb4tgp20+dCBM iL0Bus/Ryp1og8D0/lg/azPudNTqCPiAW9V5yqEghPlXgfwNSOnd0mGuwM7nLbEqvCkD EoST/eAFobynfc5cchni9mcrh6ImgbE+G9DK4mOX7oCLEjw/q31xzIfROg7Pj6ErQz/h MqoNi5t9kz0TI6L2DmjixjNdiE4e/qhD1dM11SP1kJ0v++4vMLaqdRhCGpJPsABxlK9I h8LuY12ZGwd7ehZYpYZ9ByJydr2B+Op3kfccmVapIxFqpFX0xYtu3Sg6iv5mbG1s+1Q5 UY/w== X-Forwarded-Encrypted: i=1; AJvYcCUC7tObGW2/xBi0gPBWcoDaHE5WYUIXKREJ5G7Cl76TX11QOtE1TiwE6GwszwQr5nYN2VKUMNdf/TmbAWg=@vger.kernel.org X-Gm-Message-State: AOJu0Yxy3P5KHgW3aPx+0+KwjZyH0dGamDqwZncCJiBdVbGfWkZpZv2J hKdQO7v0i96WMvGrL7DCtG4atrbjwsK/GhkjfwNjuJJ58R8pvkyQzzh3br9ADL0= X-Gm-Gg: ASbGncu45sr9WdQC+YWLAYmdJNrIzN895r9eZXpXdnvupNl6MXHXqA13BwEi1S7JPyE WX4d3bWJoUIOX2el0hdsAjgwQWPpUOGrGmpfqFvj0iPUqlqTM4HHXDIu1laG7mLBiSMfMmgCZoS h1R55uVlQOY77ruNH7hICIz1mmUlos8ySAXybHuiniULQSN4g9LLKxVu/g+MYgmO8lM0FeaQpud C4FwxSCRI8BwWgT4SYtRgyJd9yzrq7MXLKNEchYC/qj8AxmnH5usBHxXYI= X-Google-Smtp-Source: AGHT+IF0Y5yG1c5M9n48ix5v1C+TyVEgXKMj6yF6bVdvpXIdxxQSPZGVPl+AyedZLky2FVhj80qa6A== X-Received: by 2002:a05:6a00:3bc9:b0:727:39a4:30cc with SMTP id d2e1a72fcca58-72d30301d86mr19474714b3a.1.1736767424079; Mon, 13 Jan 2025 03:23:44 -0800 (PST) Received: from localhost ([122.172.84.139]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72d4067e591sm5677575b3a.126.2025.01.13.03.23.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jan 2025 03:23:43 -0800 (PST) From: Viresh Kumar To: "Rafael J. Wysocki" , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Cc: Viresh Kumar , linux-pm@vger.kernel.org, Vincent Guittot , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org Subject: [PATCH V7 03/16] rust: cpu: Add from_cpu() Date: Mon, 13 Jan 2025 16:52:58 +0530 Message-Id: <854f7b8c9cbcc7f38fe5ed548290f41224478b40.1736766672.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" This implements cpu::from_cpu(), which returns a reference to Device for a CPU. The C struct is created at initialization time for CPUs and is never freed and so ARef isn't returned from this function. The new helper will be used by Rust based cpufreq drivers. Signed-off-by: Viresh Kumar --- rust/bindings/bindings_helper.h | 1 + rust/kernel/cpu.rs | 26 ++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + 3 files changed, 28 insertions(+) create mode 100644 rust/kernel/cpu.rs diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helpe= r.h index e9fdceb568b8..d63e7f6d10ea 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include diff --git a/rust/kernel/cpu.rs b/rust/kernel/cpu.rs new file mode 100644 index 000000000000..422e874627d2 --- /dev/null +++ b/rust/kernel/cpu.rs @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Generic CPU definitions. +//! +//! C header: [`include/linux/cpu.h`](srctree/include/linux/cpu.h) + +use crate::{ + bindings, + device::Device, + error::Result, + prelude::ENODEV, +}; + +/// Creates a new instance of CPU's device. +pub fn from_cpu(cpu: u32) -> Result<&'static Device> { + // SAFETY: The pointer returned by `get_cpu_device()`, if not `NULL`, = is a valid pointer to + // a `struct device` and is never freed by the C code. + let ptr =3D unsafe { bindings::get_cpu_device(cpu) }; + if ptr.is_null() { + return Err(ENODEV); + } + + // SAFETY: The pointer returned by `get_cpu_device()`, if not `NULL`, = is a valid pointer to + // a `struct device` and is never freed by the C code. + Ok(unsafe { Device::as_ref(ptr) }) +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 2d5c3d7d2e21..e9106b29c359 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -39,6 +39,7 @@ pub mod block; #[doc(hidden)] pub mod build_assert; +pub mod cpu; pub mod cred; pub mod device; pub mod device_id; --=20 2.31.1.272.g89b43f80a514