mirror of
https://github.com/torvalds/linux.git
synced 2026-09-22 20:54:03 +02:00
rust: ACPI: fix missing match data for PRP0001
Export `acpi_of_match_device` function and use it to match the of device
table against ACPI PRP0001 in Rust.
This fixes id_info being None on ACPI PRP0001 devices.
Using `device_get_match_data` is not possible, because Rust stores an
index in the of device id instead of a data pointer. This was done this
way to provide a convenient and obvious API for drivers, which can be
evaluated in const context without the use of any unstable language
features.
Fixes: 7a718a1f26 ("rust: driver: implement `Adapter`")
Signed-off-by: Markus Probst <markus.probst@posteo.de>
Acked-by: Rafael J. Wysocki (Intel) <rafael@kernel.org> # ACPI
Link: https://patch.msgid.link/20260427-rust_acpi_prp0001-v6-1-6119b2a66183@posteo.de
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
This commit is contained in:
parent
a7cc262a11
commit
2690d07158
|
|
@ -291,6 +291,7 @@ F: include/linux/acpi.h
|
||||||
F: include/linux/fwnode.h
|
F: include/linux/fwnode.h
|
||||||
F: include/linux/fw_table.h
|
F: include/linux/fw_table.h
|
||||||
F: lib/fw_table.c
|
F: lib/fw_table.c
|
||||||
|
F: rust/helpers/acpi.c
|
||||||
F: rust/kernel/acpi.rs
|
F: rust/kernel/acpi.rs
|
||||||
F: tools/power/acpi/
|
F: tools/power/acpi/
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -831,9 +831,9 @@ const struct acpi_device *acpi_companion_match(const struct device *dev)
|
||||||
* identifiers and a _DSD object with the "compatible" property, use that
|
* identifiers and a _DSD object with the "compatible" property, use that
|
||||||
* property to match against the given list of identifiers.
|
* property to match against the given list of identifiers.
|
||||||
*/
|
*/
|
||||||
static bool acpi_of_match_device(const struct acpi_device *adev,
|
bool acpi_of_match_device(const struct acpi_device *adev,
|
||||||
const struct of_device_id *of_match_table,
|
const struct of_device_id *of_match_table,
|
||||||
const struct of_device_id **of_id)
|
const struct of_device_id **of_id)
|
||||||
{
|
{
|
||||||
const union acpi_object *of_compatible, *obj;
|
const union acpi_object *of_compatible, *obj;
|
||||||
int i, nval;
|
int i, nval;
|
||||||
|
|
|
||||||
|
|
@ -187,6 +187,10 @@ struct acpi_driver {
|
||||||
* -----------
|
* -----------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
bool acpi_of_match_device(const struct acpi_device *adev,
|
||||||
|
const struct of_device_id *of_match_table,
|
||||||
|
const struct of_device_id **of_id);
|
||||||
|
|
||||||
/* Status (_STA) */
|
/* Status (_STA) */
|
||||||
|
|
||||||
struct acpi_device_status {
|
struct acpi_device_status {
|
||||||
|
|
@ -992,6 +996,13 @@ int acpi_scan_add_dep(acpi_handle handle, struct acpi_handle_list *dep_devices);
|
||||||
u32 arch_acpi_add_auto_dep(acpi_handle handle);
|
u32 arch_acpi_add_auto_dep(acpi_handle handle);
|
||||||
#else /* CONFIG_ACPI */
|
#else /* CONFIG_ACPI */
|
||||||
|
|
||||||
|
static inline bool acpi_of_match_device(const struct acpi_device *adev,
|
||||||
|
const struct of_device_id *of_match_table,
|
||||||
|
const struct of_device_id **of_id)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
static inline int register_acpi_bus_type(void *bus) { return 0; }
|
static inline int register_acpi_bus_type(void *bus) { return 0; }
|
||||||
static inline int unregister_acpi_bus_type(void *bus) { return 0; }
|
static inline int unregister_acpi_bus_type(void *bus) { return 0; }
|
||||||
|
|
||||||
|
|
|
||||||
16
rust/helpers/acpi.c
Normal file
16
rust/helpers/acpi.c
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
|
|
||||||
|
#include <linux/acpi.h>
|
||||||
|
#include <acpi/acpi_bus.h>
|
||||||
|
|
||||||
|
__rust_helper bool rust_helper_acpi_of_match_device(const struct acpi_device *adev,
|
||||||
|
const struct of_device_id *of_match_table,
|
||||||
|
const struct of_device_id **of_id)
|
||||||
|
{
|
||||||
|
return acpi_of_match_device(adev, of_match_table, of_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
__rust_helper struct acpi_device *rust_helper_to_acpi_device_node(struct fwnode_handle *fwnode)
|
||||||
|
{
|
||||||
|
return to_acpi_device_node(fwnode);
|
||||||
|
}
|
||||||
|
|
@ -38,6 +38,7 @@
|
||||||
#define __rust_helper __always_inline
|
#define __rust_helper __always_inline
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "acpi.c"
|
||||||
#include "atomic.c"
|
#include "atomic.c"
|
||||||
#include "atomic_ext.c"
|
#include "atomic_ext.c"
|
||||||
#include "auxiliary.c"
|
#include "auxiliary.c"
|
||||||
|
|
|
||||||
|
|
@ -278,6 +278,26 @@ fn init(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Calling the FFI function directly from the `Adapter` impl may result in it being called
|
||||||
|
// directly from driver modules. This happens since the Rust compiler will use monomorphisation, so
|
||||||
|
// it might happen that functions are instantiated within the calling driver module. For now, work
|
||||||
|
// around this with `#[inline(never)]` helpers.
|
||||||
|
//
|
||||||
|
// TODO: Remove once a more generic solution has been implemented. For instance, we may be able to
|
||||||
|
// leverage `bindgen` to take care of this depending on whether a symbol is (already) exported.
|
||||||
|
#[inline(never)]
|
||||||
|
#[allow(clippy::missing_safety_doc)]
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[must_use]
|
||||||
|
unsafe fn acpi_of_match_device(
|
||||||
|
adev: *const bindings::acpi_device,
|
||||||
|
of_match_table: *const bindings::of_device_id,
|
||||||
|
of_id: *mut *const bindings::of_device_id,
|
||||||
|
) -> bool {
|
||||||
|
// SAFETY: Safety requirements are the same as `bindings::acpi_of_match_device`.
|
||||||
|
unsafe { bindings::acpi_of_match_device(adev, of_match_table, of_id) }
|
||||||
|
}
|
||||||
|
|
||||||
/// The bus independent adapter to match a drivers and a devices.
|
/// The bus independent adapter to match a drivers and a devices.
|
||||||
///
|
///
|
||||||
/// This trait should be implemented by the bus specific adapter, which represents the connection
|
/// This trait should be implemented by the bus specific adapter, which represents the connection
|
||||||
|
|
@ -329,35 +349,63 @@ fn acpi_id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {
|
||||||
///
|
///
|
||||||
/// If this returns `None`, it means there is no match with an entry in the [`of::IdTable`].
|
/// If this returns `None`, it means there is no match with an entry in the [`of::IdTable`].
|
||||||
fn of_id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {
|
fn of_id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {
|
||||||
#[cfg(not(CONFIG_OF))]
|
let table = Self::of_id_table()?;
|
||||||
|
|
||||||
|
#[cfg(not(any(CONFIG_OF, CONFIG_ACPI)))]
|
||||||
{
|
{
|
||||||
let _ = dev;
|
let _ = (dev, table);
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(CONFIG_OF)]
|
#[cfg(CONFIG_OF)]
|
||||||
{
|
{
|
||||||
let table = Self::of_id_table()?;
|
|
||||||
|
|
||||||
// SAFETY:
|
// SAFETY:
|
||||||
// - `table` has static lifetime, hence it's valid for read,
|
// - `table` has static lifetime, hence it's valid for read,
|
||||||
// - `dev` is guaranteed to be valid while it's alive, and so is `dev.as_raw()`.
|
// - `dev` is guaranteed to be valid while it's alive, and so is `dev.as_raw()`.
|
||||||
let raw_id = unsafe { bindings::of_match_device(table.as_ptr(), dev.as_raw()) };
|
let raw_id = unsafe { bindings::of_match_device(table.as_ptr(), dev.as_raw()) };
|
||||||
|
|
||||||
if raw_id.is_null() {
|
if !raw_id.is_null() {
|
||||||
None
|
|
||||||
} else {
|
|
||||||
// SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `struct of_device_id`
|
// SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `struct of_device_id`
|
||||||
// and does not add additional invariants, so it's safe to transmute.
|
// and does not add additional invariants, so it's safe to transmute.
|
||||||
let id = unsafe { &*raw_id.cast::<of::DeviceId>() };
|
let id = unsafe { &*raw_id.cast::<of::DeviceId>() };
|
||||||
|
|
||||||
Some(
|
return Some(table.info(
|
||||||
table.info(<of::DeviceId as crate::device_id::RawDeviceIdIndex>::index(
|
<of::DeviceId as crate::device_id::RawDeviceIdIndex>::index(id),
|
||||||
id,
|
));
|
||||||
)),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(CONFIG_ACPI)]
|
||||||
|
{
|
||||||
|
use core::ptr;
|
||||||
|
use device::property::FwNode;
|
||||||
|
|
||||||
|
let mut raw_id = ptr::null();
|
||||||
|
|
||||||
|
let fwnode = dev.fwnode().map_or(ptr::null_mut(), FwNode::as_raw);
|
||||||
|
|
||||||
|
// SAFETY: `fwnode` is a pointer to a valid `fwnode_handle`. A null pointer will be
|
||||||
|
// passed through the function.
|
||||||
|
let adev = unsafe { bindings::to_acpi_device_node(fwnode) };
|
||||||
|
|
||||||
|
// SAFETY:
|
||||||
|
// - `adev` is a valid pointer to `acpi_device` or is null. It is guaranteed to be
|
||||||
|
// valid as long as `dev` is alive.
|
||||||
|
// - `table` has static lifetime, hence it's valid for read.
|
||||||
|
if unsafe { acpi_of_match_device(adev, table.as_ptr(), &raw mut raw_id) } {
|
||||||
|
// SAFETY:
|
||||||
|
// - the function returns true, therefore `raw_id` has been set to a pointer to a
|
||||||
|
// valid `of_device_id`.
|
||||||
|
// - `DeviceId` is a `#[repr(transparent)]` wrapper of `struct of_device_id`
|
||||||
|
// and does not add additional invariants, so it's safe to transmute.
|
||||||
|
let id = unsafe { &*raw_id.cast::<of::DeviceId>() };
|
||||||
|
|
||||||
|
return Some(table.info(
|
||||||
|
<of::DeviceId as crate::device_id::RawDeviceIdIndex>::index(id),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the driver's private data from the matching entry of any of the ID tables, if any.
|
/// Returns the driver's private data from the matching entry of any of the ID tables, if any.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user