Skip to content

Commit ce16802

Browse files
committed
drm/asahi: Switch to the kernel crate Zeroable trait
Signed-off-by: Asahi Lina <[email protected]>
1 parent e8f05cf commit ce16802

7 files changed

Lines changed: 38 additions & 58 deletions

File tree

drivers/gpu/drm/asahi/alloc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use kernel::{c_str, drm::mm, error::Result, prelude::*, str::CString, sync::Lock
1616

1717
use crate::debug::*;
1818
use crate::driver::AsahiDevice;
19-
use crate::fw::types::Zeroed;
19+
use crate::fw::types::Zeroable;
2020
use crate::mmu;
2121
use crate::object::{GpuArray, GpuObject, GpuOnlyArray, GpuStruct, GpuWeakPointer};
2222

@@ -244,7 +244,7 @@ pub(crate) trait Allocator {
244244
&mut self,
245245
) -> Result<GpuObject<T, GenericAlloc<T, Self::Raw>>>
246246
where
247-
for<'a> <T as GpuStruct>::Raw<'a>: Default + Zeroed,
247+
for<'a> <T as GpuStruct>::Raw<'a>: Default + Zeroable,
248248
{
249249
GpuObject::<T, GenericAlloc<T, Self::Raw>>::new_default(self.alloc_object()?)
250250
}

drivers/gpu/drm/asahi/channel.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub(crate) use crate::fw::channels::PipeType;
2222
/// A receive (FW->driver) channel.
2323
pub(crate) struct RxChannel<T: RxChannelState, U: Copy + Default>
2424
where
25-
for<'a> <T as GpuStruct>::Raw<'a>: Debug + Default + Zeroed,
25+
for<'a> <T as GpuStruct>::Raw<'a>: Debug + Default + Zeroable,
2626
{
2727
ring: ChannelRing<T, U>,
2828
// FIXME: needs feature(generic_const_exprs)
@@ -33,7 +33,7 @@ where
3333

3434
impl<T: RxChannelState, U: Copy + Default> RxChannel<T, U>
3535
where
36-
for<'a> <T as GpuStruct>::Raw<'a>: Debug + Default + Zeroed,
36+
for<'a> <T as GpuStruct>::Raw<'a>: Debug + Default + Zeroable,
3737
{
3838
/// Allocates a new receive channel with a given message count.
3939
pub(crate) fn new(alloc: &mut gpu::KernelAllocators, count: usize) -> Result<RxChannel<T, U>> {
@@ -87,7 +87,7 @@ where
8787
/// A transmit (driver->FW) channel.
8888
pub(crate) struct TxChannel<T: TxChannelState, U: Copy + Default>
8989
where
90-
for<'a> <T as GpuStruct>::Raw<'a>: Debug + Default + Zeroed,
90+
for<'a> <T as GpuStruct>::Raw<'a>: Debug + Default + Zeroable,
9191
{
9292
ring: ChannelRing<T, U>,
9393
wptr: u32,
@@ -96,7 +96,7 @@ where
9696

9797
impl<T: TxChannelState, U: Copy + Default> TxChannel<T, U>
9898
where
99-
for<'a> <T as GpuStruct>::Raw<'a>: Debug + Default + Zeroed,
99+
for<'a> <T as GpuStruct>::Raw<'a>: Debug + Default + Zeroable,
100100
{
101101
/// Allocates a new cached transmit channel with a given message count.
102102
pub(crate) fn new(alloc: &mut gpu::KernelAllocators, count: usize) -> Result<TxChannel<T, U>> {

drivers/gpu/drm/asahi/float.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323
//! related to slightly non-compliant rounding.
2424
2525
use core::ops;
26-
use kernel::{of, prelude::*};
26+
use kernel::{init::Zeroable, of, prelude::*};
2727

2828
/// An IEEE754-compatible floating point number implemented in software.
2929
#[derive(Default, Debug, Copy, Clone)]
3030
pub(crate) struct F32(u32);
3131

32+
unsafe impl Zeroable for F32 {}
33+
3234
#[derive(Default, Debug, Copy, Clone)]
3335
struct F32U {
3436
sign: bool,

drivers/gpu/drm/asahi/fw/channels.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub(crate) mod raw {
3434

3535
pub(crate) trait RxChannelState: GpuStruct + Debug + Default
3636
where
37-
for<'a> <Self as GpuStruct>::Raw<'a>: Default + Zeroed,
37+
for<'a> <Self as GpuStruct>::Raw<'a>: Default + Zeroable,
3838
{
3939
const SUB_CHANNELS: usize;
4040

drivers/gpu/drm/asahi/fw/types.rs

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub(crate) use ::alloc::boxed::Box;
1414
pub(crate) use core::fmt::Debug;
1515
pub(crate) use core::marker::PhantomData;
1616
pub(crate) use core::sync::atomic::{AtomicI32, AtomicU32, AtomicU64};
17+
pub(crate) use kernel::init::Zeroable;
1718
pub(crate) use kernel::macros::versions;
1819

1920
// Make the trait visible
@@ -53,7 +54,7 @@ pub(crate) struct FwStamp(pub(crate) AtomicU32);
5354
#[repr(C, packed(1))]
5455
pub(crate) struct U64(pub(crate) u64);
5556

56-
unsafe impl Zeroed for U64 {}
57+
unsafe impl Zeroable for U64 {}
5758

5859
impl fmt::Debug for U64 {
5960
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -70,7 +71,7 @@ impl fmt::Debug for U64 {
7071
#[repr(C, packed(1))]
7172
pub(crate) struct U32(pub(crate) u32);
7273

73-
unsafe impl Zeroed for U32 {}
74+
unsafe impl Zeroable for U32 {}
7475

7576
impl fmt::Debug for U32 {
7677
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -79,16 +80,6 @@ impl fmt::Debug for U32 {
7980
}
8081
}
8182

82-
unsafe impl Zeroed for u8 {}
83-
unsafe impl Zeroed for u16 {}
84-
unsafe impl Zeroed for u32 {}
85-
unsafe impl Zeroed for u64 {}
86-
unsafe impl Zeroed for i8 {}
87-
unsafe impl Zeroed for i16 {}
88-
unsafe impl Zeroed for i32 {}
89-
unsafe impl Zeroed for i64 {}
90-
unsafe impl Zeroed for F32 {}
91-
9283
/// Create a dummy `Debug` implementation, for when we need it but it's too painful to write by
9384
/// hand or not very useful.
9485
#[macro_export]
@@ -102,46 +93,32 @@ macro_rules! no_debug {
10293
};
10394
}
10495

105-
/// Types which can be safely initialized with an all-zero bit pattern.
106-
///
107-
/// See: https://github.com/rust-lang/rfcs/issues/2626
108-
///
109-
/// # Safety
110-
///
111-
/// This trait must only be implemented if a type only contains primitive types which can be
112-
/// zero-initialized, FFI structs intended to be zero-initialized, or other types which impl Zeroed.
113-
pub(crate) unsafe trait Zeroed: Default {
114-
fn zeroed() -> Self {
115-
// SAFETY: The user is responsible for ensuring this is safe.
116-
unsafe { core::mem::zeroed() }
117-
}
118-
}
119-
120-
/// Implement Zeroed for a given type (and Default along with it).
96+
/// Implement Zeroable for a given type (and Default along with it).
12197
///
12298
/// # Safety
12399
///
124100
/// This macro must only be used if a type only contains primitive types which can be
125-
/// zero-initialized, FFI structs intended to be zero-initialized, or other types which impl Zeroed.
101+
/// zero-initialized, FFI structs intended to be zero-initialized, or other types which
102+
/// impl Zeroable.
126103
#[macro_export]
127104
macro_rules! default_zeroed {
128105
(<$($lt:lifetime),*>, $type:ty) => {
129106
impl<$($lt),*> Default for $type {
130107
fn default() -> $type {
131-
Zeroed::zeroed()
108+
::kernel::init::Zeroable::zeroed()
132109
}
133110
}
134111
// SAFETY: The user is responsible for ensuring this is safe.
135-
unsafe impl<$($lt),*> Zeroed for $type {}
112+
unsafe impl<$($lt),*> ::kernel::init::Zeroable for $type {}
136113
};
137114
($type:ty) => {
138115
impl Default for $type {
139116
fn default() -> $type {
140-
Zeroed::zeroed()
117+
::kernel::init::Zeroable::zeroed()
141118
}
142119
}
143120
// SAFETY: The user is responsible for ensuring this is safe.
144-
unsafe impl Zeroed for $type {}
121+
unsafe impl ::kernel::init::Zeroable for $type {}
145122
};
146123
}
147124

@@ -151,11 +128,11 @@ macro_rules! default_zeroed {
151128
pub(crate) struct Pad<const N: usize>([u8; N]);
152129

153130
/// SAFETY: Primitive type, safe to zero-init.
154-
unsafe impl<const N: usize> Zeroed for Pad<N> {}
131+
unsafe impl<const N: usize> Zeroable for Pad<N> {}
155132

156133
impl<const N: usize> Default for Pad<N> {
157134
fn default() -> Self {
158-
Zeroed::zeroed()
135+
Zeroable::zeroed()
159136
}
160137
}
161138

@@ -165,7 +142,7 @@ impl<const N: usize> fmt::Debug for Pad<N> {
165142
}
166143
}
167144

168-
/// A convenience type for a fixed-sized array with Default/Zeroed impls.
145+
/// A convenience type for a fixed-sized array with Default/Zeroable impls.
169146
#[derive(Copy, Clone)]
170147
#[repr(C)]
171148
pub(crate) struct Array<const N: usize, T>([T; N]);
@@ -176,12 +153,12 @@ impl<const N: usize, T> Array<N, T> {
176153
}
177154
}
178155

179-
// SAFETY: Arrays of Zeroed values can be safely Zeroed.
180-
unsafe impl<const N: usize, T: Zeroed> Zeroed for Array<N, T> {}
156+
// SAFETY: Arrays of Zeroable values can be safely Zeroable.
157+
unsafe impl<const N: usize, T: Zeroable> Zeroable for Array<N, T> {}
181158

182-
impl<const N: usize, T: Zeroed> Default for Array<N, T> {
159+
impl<const N: usize, T: Zeroable> Default for Array<N, T> {
183160
fn default() -> Self {
184-
Zeroed::zeroed()
161+
Zeroable::zeroed()
185162
}
186163
}
187164

@@ -224,11 +201,12 @@ impl<const N: usize, T: Sized + fmt::Debug> fmt::Debug for Array<N, T> {
224201
#[macro_export]
225202
macro_rules! trivial_gpustruct {
226203
($type:ident) => {
227-
#[derive(Debug, Default)]
204+
#[derive(Debug)]
228205
pub(crate) struct $type {}
229206

230207
impl GpuStruct for $type {
231208
type Raw<'a> = raw::$type;
232209
}
210+
$crate::default_zeroed!($type);
233211
};
234212
}

drivers/gpu/drm/asahi/object.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ use core::{mem, ptr, slice};
5757

5858
use crate::alloc::Allocation;
5959
use crate::debug::*;
60-
use crate::fw::types::Zeroed;
60+
use crate::fw::types::Zeroable;
6161

6262
const DEBUG_CLASS: DebugFlags = DebugFlags::Object;
6363

@@ -450,13 +450,13 @@ where
450450

451451
impl<T: GpuStruct + Default, U: Allocation<T>> GpuObject<T, U>
452452
where
453-
for<'a> <T as GpuStruct>::Raw<'a>: Default + Zeroed,
453+
for<'a> <T as GpuStruct>::Raw<'a>: Default + Zeroable,
454454
{
455455
/// Create a new GpuObject with default data. `T` must implement `Default` and `T::Raw` must
456-
/// implement `Zeroed`, since the GPU-side memory is initialized by zeroing.
456+
/// implement `Zeroable`, since the GPU-side memory is initialized by zeroing.
457457
pub(crate) fn new_default(alloc: U) -> Result<Self> {
458458
GpuObject::<T, U>::new_inplace(alloc, Default::default(), |_inner, raw| {
459-
// SAFETY: `raw` is valid here, and `T::Raw` implements `Zeroed`.
459+
// SAFETY: `raw` is valid here, and `T::Raw` implements `Zeroable`.
460460
Ok(unsafe {
461461
ptr::write_bytes(raw, 0, 1);
462462
(*raw).assume_init_mut()

drivers/gpu/drm/asahi/place.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
///
3131
/// The macro supports most Rust initialization syntax including type paths, generic arguments,
3232
/// and nested structures. Nested structures are themselves initialized in-place field by field.
33-
/// `..Default::default()` is supported, but this macro converts it to `..Zeroed::zeroed()`, as it
33+
/// `..Default::default()` is supported, but this macro converts it to `..Zeroable::zeroed()`, as it
3434
/// initializes those structs by zero-initializing the underlying memory. Usage of
35-
/// `..Default::default()` with a type not implementing `Zeroed` will result in a compile error.
35+
/// `..Default::default()` with a type not implementing `Zeroable` will result in a compile error.
3636
///
3737
/// Usage:
3838
/// ```
@@ -81,11 +81,11 @@ macro_rules! place {
8181
// Zero-initialize structure if the initializer ends in ..default::Default()
8282
(@STRUCT_ZERO $ptr:ident, {$($typ_init:tt)*} { $($f:ident $(: $v:expr)?),* $(,)? }) => {};
8383
(@STRUCT_ZERO $ptr:ident, {$($typ_init:tt)*} { $($($f:ident $(: $v:expr)?),*,)? ..Default::default() }) => {{
84-
// Check that the structure actually implements Zeroed
84+
// Check that the structure actually implements Zeroable
8585
const _: () = {
8686
fn _check_default() {
8787
let _ = $($typ_init)* {
88-
..Zeroed::zeroed()
88+
..Zeroable::zeroed()
8989
};
9090
}
9191
};
@@ -103,7 +103,7 @@ macro_rules! place {
103103
$f $(: $v)?
104104
),*
105105
,)?
106-
..Zeroed::zeroed()
106+
..Zeroable::zeroed()
107107
};
108108
} else {
109109
{$($body)*}

0 commit comments

Comments
 (0)