Skip to content

Commit effd690

Browse files
committed
drm/asahi: Adapt to v6.15 + v6.16-rc1 rust / driver-core(rust)
Signed-off-by: Janne Grunau <j@jannau.net>
1 parent 9139d2e commit effd690

18 files changed

Lines changed: 839 additions & 798 deletions

File tree

drivers/gpu/drm/asahi/buffer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use crate::fw::types::*;
3838
use crate::util::*;
3939
use crate::{alloc, fw, gpu, hw, mmu, slotalloc};
4040
use core::sync::atomic::Ordering;
41+
use kernel::new_mutex;
4142
use kernel::prelude::*;
4243
use kernel::sync::{Arc, Mutex};
4344
use kernel::{c_str, static_lock_class};
@@ -398,7 +399,7 @@ impl Buffer::ver {
398399

399400
Ok(Buffer::ver {
400401
inner: Arc::pin_init(
401-
Mutex::new(BufferInner::ver {
402+
new_mutex!(BufferInner::ver {
402403
info,
403404
ualloc,
404405
ualloc_priv,

drivers/gpu/drm/asahi/channel.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ use crate::fw::channels::*;
1414
use crate::fw::initdata::{raw, ChannelRing};
1515
use crate::fw::types::*;
1616
use crate::{buffer, event, gpu, mem};
17-
use core::time::Duration;
1817
use kernel::{
1918
c_str,
20-
delay::coarse_sleep,
2119
prelude::*,
2220
sync::Arc,
23-
time::{clock, Now},
21+
time::{delay::fsleep, Delta, Instant},
2422
};
2523

2624
pub(crate) use crate::fw::channels::PipeType;
@@ -146,7 +144,7 @@ where
146144
);
147145
// TODO: block properly on incoming messages?
148146
while next_wptr == rptr {
149-
coarse_sleep(Duration::from_millis(8));
147+
fsleep(Delta::from_millis(8));
150148
rptr = T::rptr(raw);
151149
}
152150
}
@@ -164,11 +162,12 @@ where
164162
/// completion of a cache management or invalidation operation synchronously (which
165163
/// the firmware normally completes fast enough not to be worth sleeping for).
166164
/// If the poll takes longer than 10ms, this switches to sleeping between polls.
167-
pub(crate) fn wait_for(&mut self, wptr: u32, timeout_ms: u64) -> Result {
168-
const MAX_FAST_POLL: u64 = 10;
169-
let start = clock::KernelTime::now();
170-
let timeout_fast = Duration::from_millis(timeout_ms.min(MAX_FAST_POLL));
171-
let timeout_slow = Duration::from_millis(timeout_ms);
165+
pub(crate) fn wait_for(&mut self, wptr: u32, timeout_ms: i64) -> Result {
166+
const MAX_FAST_POLL: i64 = 10;
167+
let start = Instant::now();
168+
let timeout_ms = timeout_ms.max(1);
169+
let timeout_fast = Delta::from_millis(timeout_ms.min(MAX_FAST_POLL));
170+
let timeout_slow = Delta::from_millis(timeout_ms);
172171
self.ring.state.with(|raw, _inner| {
173172
while start.elapsed() < timeout_fast {
174173
if T::rptr(raw) == wptr {
@@ -180,7 +179,7 @@ where
180179
if T::rptr(raw) == wptr {
181180
return Ok(());
182181
}
183-
coarse_sleep(Duration::from_millis(5));
182+
fsleep(Delta::from_millis(5));
184183
mem::sync();
185184
}
186185
Err(ETIMEDOUT)
@@ -197,7 +196,7 @@ pub(crate) struct DeviceControlChannel {
197196

198197
#[versions(AGX)]
199198
impl DeviceControlChannel::ver {
200-
const COMMAND_TIMEOUT_MS: u64 = 1000;
199+
const COMMAND_TIMEOUT_MS: i64 = 1000;
201200

202201
/// Allocate a new Device Control channel.
203202
pub(crate) fn new(
@@ -266,7 +265,7 @@ pub(crate) struct FwCtlChannel {
266265
}
267266

268267
impl FwCtlChannel {
269-
const COMMAND_TIMEOUT_MS: u64 = 1000;
268+
const COMMAND_TIMEOUT_MS: i64 = 1000;
270269

271270
/// Allocate a new Firmware Control channel.
272271
pub(crate) fn new(

drivers/gpu/drm/asahi/driver.rs

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,14 @@
33
//! Top-level GPU driver implementation.
44
55
use kernel::{
6-
c_str, drm, drm::drv, drm::ioctl, error::Result, of, platform, prelude::*, sync::Arc,
6+
c_str, device::Core, drm, drm::ioctl, error::Result, of, platform, prelude::*, sync::Arc,
77
};
88

99
use crate::{debug, file, gem, gpu, hw, regs};
1010

11-
use kernel::dma::Device;
1211
use kernel::macros::vtable;
1312
use kernel::types::ARef;
1413

15-
/// Convenience type alias for the `device::Data` type for this driver.
16-
// type DeviceData = device::Data<drv::Registration<AsahiDriver>, regs::Resources, AsahiData>;
17-
1814
/// Holds a reference to the top-level `GpuManager` object.
1915
// pub(crate) struct AsahiData {
2016
// pub(crate) dev: ARef<device::Device>,
@@ -25,45 +21,50 @@ use kernel::types::ARef;
2521
pub(crate) struct AsahiData {
2622
#[pin]
2723
pub(crate) gpu: Arc<dyn gpu::GpuManager>,
28-
pub(crate) pdev: platform::Device,
24+
pub(crate) pdev: ARef<platform::Device>,
2925
pub(crate) resources: regs::Resources,
3026
}
3127

28+
unsafe impl Send for AsahiData {}
29+
unsafe impl Sync for AsahiData {}
30+
3231
pub(crate) struct AsahiDriver {
33-
_reg: drm::drv::Registration<Self>,
34-
pub(crate) data: Arc<AsahiData>,
32+
#[allow(dead_code)]
33+
drm: ARef<drm::Device<Self>>,
3534
}
3635

36+
unsafe impl Send for AsahiDriver {}
37+
unsafe impl Sync for AsahiDriver {}
38+
3739
/// Convenience type alias for the DRM device type for this driver.
38-
pub(crate) type AsahiDevice = kernel::drm::device::Device<AsahiDriver>;
40+
pub(crate) type AsahiDevice = drm::device::Device<AsahiDriver>;
3941
pub(crate) type AsahiDevRef = ARef<AsahiDevice>;
4042

4143
/// DRM Driver metadata
42-
const INFO: drv::DriverInfo = drv::DriverInfo {
44+
const INFO: drm::driver::DriverInfo = drm::driver::DriverInfo {
4345
major: 0,
4446
minor: 0,
4547
patchlevel: 0,
4648
name: c_str!("asahi"),
4749
desc: c_str!("Apple AGX Graphics"),
48-
date: c_str!("20220831"),
4950
};
5051

5152
/// DRM Driver implementation for `AsahiDriver`.
5253
#[vtable]
53-
impl drv::Driver for AsahiDriver {
54+
impl drm::driver::Driver for AsahiDriver {
5455
/// Our `DeviceData` type, reference-counted
55-
type Data = Arc<AsahiData>;
56+
type Data = AsahiData;
5657
/// Our `File` type.
5758
type File = file::File;
5859
/// Our `Object` type.
59-
type Object = gem::Object;
60+
type Object = gem::AsahiObject;
6061

61-
const INFO: drv::DriverInfo = INFO;
62-
const FEATURES: u32 = drv::FEAT_GEM
63-
| drv::FEAT_RENDER
64-
| drv::FEAT_SYNCOBJ
65-
| drv::FEAT_SYNCOBJ_TIMELINE
66-
| drv::FEAT_GEM_GPUVA;
62+
const INFO: drm::driver::DriverInfo = INFO;
63+
const FEATURES: u32 = drm::driver::FEAT_GEM
64+
| drm::driver::FEAT_RENDER
65+
| drm::driver::FEAT_SYNCOBJ
66+
| drm::driver::FEAT_SYNCOBJ_TIMELINE
67+
| drm::driver::FEAT_GEM_GPUVA;
6768

6869
kernel::declare_drm_ioctls! {
6970
(ASAHI_GET_PARAMS, drm_asahi_get_params,
@@ -138,16 +139,18 @@ impl platform::Driver for AsahiDriver {
138139
const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
139140

140141
/// Device probe function.
141-
fn probe(pdev: &mut platform::Device, info: Option<&Self::IdInfo>) -> Result<Pin<KBox<Self>>> {
142+
fn probe(
143+
pdev: &platform::Device<Core>,
144+
info: Option<&Self::IdInfo>,
145+
) -> Result<Pin<KBox<Self>>> {
142146
debug::update_debug_flags();
143147

144-
let dev = pdev.clone();
145-
146148
dev_info!(pdev.as_ref(), "Probing...\n");
147149

148150
let cfg = info.ok_or(ENODEV)?;
149151

150-
pdev.dma_set_mask_and_coherent((1 << cfg.uat_oas) - 1)?;
152+
pdev.as_ref()
153+
.dma_set_mask_and_coherent((1 << cfg.uat_oas) - 1)?;
151154

152155
let res = regs::Resources::new(pdev)?;
153156

@@ -160,7 +163,9 @@ impl platform::Driver for AsahiDriver {
160163
let node = pdev.as_ref().of_node().ok_or(EIO)?;
161164
let compat: KVec<u32> = node.get_property(c_str!("apple,firmware-compat"))?;
162165

163-
let drm = drm::device::Device::<AsahiDriver>::new(dev.as_ref())?;
166+
let raw_drm = unsafe { drm::device::Device::<AsahiDriver>::new_uninit(pdev.as_ref())? };
167+
168+
let drm: AsahiDevRef = unsafe { ARef::from_raw(raw_drm) };
164169

165170
let gpu = match (cfg.gpu_gen, cfg.gpu_variant, compat.as_slice()) {
166171
(hw::GpuGen::G13, _, &[12, 3, 0]) => {
@@ -190,19 +195,18 @@ impl platform::Driver for AsahiDriver {
190195
}
191196
};
192197

193-
let data = Arc::pin_init(
194-
try_pin_init!(AsahiData {
195-
gpu,
196-
pdev: pdev.clone(),
197-
resources: res,
198-
}),
199-
GFP_KERNEL,
200-
)?;
198+
let data = try_pin_init!(AsahiData {
199+
gpu,
200+
pdev: pdev.into(),
201+
resources: res,
202+
});
203+
204+
let drm = unsafe { AsahiDevice::init_data(raw_drm, data)? };
201205

202-
data.gpu.init()?;
206+
(*drm).gpu.init()?;
203207

204-
let reg = drm::drv::Registration::new(drm, data.clone(), 0)?;
208+
drm::driver::Registration::new_foreign_owned(&drm, pdev.as_ref(), 0)?;
205209

206-
Ok(KBox::new(Self { _reg: reg, data }, GFP_KERNEL)?.into())
210+
Ok(KBox::new(Self { drm }, GFP_KERNEL)?.into())
207211
}
208212
}

0 commit comments

Comments
 (0)