Skip to content

Commit c30c0c6

Browse files
committed
drm/asahi: driver: Port to new device/driver registration
As proposed in https://lore.kernel.org/rust-for-linux/20241022213221.2383-1-dakr@kernel.org/ Signed-off-by: Janne Grunau <j@jannau.net>
1 parent dfeb451 commit c30c0c6

1 file changed

Lines changed: 89 additions & 67 deletions

File tree

drivers/gpu/drm/asahi/driver.rs

Lines changed: 89 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,53 @@
33
//! Top-level GPU driver implementation.
44
55
use kernel::{
6-
c_str, device, drm, drm::drv, drm::ioctl, error::code, error::Result, of, platform, prelude::*,
7-
sync::Arc,
6+
c_str, drm, drm::drv, drm::ioctl, error::Result, of, platform, prelude::*, sync::Arc,
87
};
98

109
use crate::{debug, file, gem, gpu, hw, regs};
1110

1211
use kernel::macros::vtable;
1312
use kernel::types::ARef;
1413

15-
/// Driver metadata
16-
const INFO: drv::DriverInfo = drv::DriverInfo {
17-
major: 0,
18-
minor: 0,
19-
patchlevel: 0,
20-
name: c_str!("asahi"),
21-
desc: c_str!("Apple AGX Graphics"),
22-
date: c_str!("20220831"),
23-
};
14+
/// Convenience type alias for the `device::Data` type for this driver.
15+
// type DeviceData = device::Data<drv::Registration<AsahiDriver>, regs::Resources, AsahiData>;
2416

25-
/// Device data for the driver registration.
26-
///
2717
/// Holds a reference to the top-level `GpuManager` object.
18+
// pub(crate) struct AsahiData {
19+
// pub(crate) dev: ARef<device::Device>,
20+
// pub(crate) gpu: Arc<dyn gpu::GpuManager>,
21+
// }
22+
23+
#[pin_data]
2824
pub(crate) struct AsahiData {
29-
pub(crate) dev: ARef<device::Device>,
25+
#[pin]
26+
// pub(crate) dev: ARef<device::Device>,
3027
pub(crate) gpu: Arc<dyn gpu::GpuManager>,
28+
pub(crate) pdev: platform::Device,
29+
pub(crate) resources: regs::Resources,
3130
}
3231

33-
/// Convenience type alias for the `device::Data` type for this driver.
34-
type DeviceData = device::Data<drv::Registration<AsahiDriver>, regs::Resources, AsahiData>;
35-
36-
/// Empty struct representing this driver.
37-
pub(crate) struct AsahiDriver;
32+
pub(crate) struct AsahiDriver(ARef<AsahiDevice>);
3833

3934
/// Convenience type alias for the DRM device type for this driver.
4035
pub(crate) type AsahiDevice = kernel::drm::device::Device<AsahiDriver>;
4136
pub(crate) type AsahiDevRef = ARef<AsahiDevice>;
4237

38+
/// DRM Driver metadata
39+
const INFO: drv::DriverInfo = drv::DriverInfo {
40+
major: 0,
41+
minor: 0,
42+
patchlevel: 0,
43+
name: c_str!("asahi"),
44+
desc: c_str!("Apple AGX Graphics"),
45+
date: c_str!("20220831"),
46+
};
47+
4348
/// DRM Driver implementation for `AsahiDriver`.
4449
#[vtable]
4550
impl drv::Driver for AsahiDriver {
4651
/// Our `DeviceData` type, reference-counted
47-
type Data = Arc<DeviceData>;
52+
type Data = Arc<AsahiData>;
4853
/// Our `File` type.
4954
type File = file::File;
5055
/// Our `Object` type.
@@ -81,40 +86,59 @@ impl drv::Driver for AsahiDriver {
8186
}
8287
}
8388

84-
// OF Device ID table.
85-
kernel::define_of_id_table! {ASAHI_ID_TABLE, &'static hw::HwConfig, [
86-
(of::DeviceId::Compatible(b"apple,agx-t8103"), Some(&hw::t8103::HWCONFIG)),
87-
(of::DeviceId::Compatible(b"apple,agx-t8112"), Some(&hw::t8112::HWCONFIG)),
88-
(of::DeviceId::Compatible(b"apple,agx-t6000"), Some(&hw::t600x::HWCONFIG_T6000)),
89-
(of::DeviceId::Compatible(b"apple,agx-t6001"), Some(&hw::t600x::HWCONFIG_T6001)),
90-
(of::DeviceId::Compatible(b"apple,agx-t6002"), Some(&hw::t600x::HWCONFIG_T6002)),
91-
(of::DeviceId::Compatible(b"apple,agx-t6020"), Some(&hw::t602x::HWCONFIG_T6020)),
92-
(of::DeviceId::Compatible(b"apple,agx-t6021"), Some(&hw::t602x::HWCONFIG_T6021)),
93-
(of::DeviceId::Compatible(b"apple,agx-t6022"), Some(&hw::t602x::HWCONFIG_T6022)),
94-
]}
89+
// OF Device ID table.s
90+
kernel::of_device_table!(
91+
OF_TABLE,
92+
MODULE_OF_TABLE,
93+
<AsahiDriver as platform::Driver>::IdInfo,
94+
[
95+
(
96+
of::DeviceId::new(c_str!("apple,agx-t8103")),
97+
&hw::t8103::HWCONFIG
98+
),
99+
(
100+
of::DeviceId::new(c_str!("apple,agx-t8112")),
101+
&hw::t8112::HWCONFIG
102+
),
103+
(
104+
of::DeviceId::new(c_str!("apple,agx-t6000")),
105+
&hw::t600x::HWCONFIG_T6000
106+
),
107+
(
108+
of::DeviceId::new(c_str!("apple,agx-t6001")),
109+
&hw::t600x::HWCONFIG_T6001
110+
),
111+
(
112+
of::DeviceId::new(c_str!("apple,agx-t6002")),
113+
&hw::t600x::HWCONFIG_T6002
114+
),
115+
(
116+
of::DeviceId::new(c_str!("apple,agx-t6020")),
117+
&hw::t602x::HWCONFIG_T6020
118+
),
119+
(
120+
of::DeviceId::new(c_str!("apple,agx-t6021")),
121+
&hw::t602x::HWCONFIG_T6021
122+
),
123+
(
124+
of::DeviceId::new(c_str!("apple,agx-t6022")),
125+
&hw::t602x::HWCONFIG_T6022
126+
),
127+
]
128+
);
95129

96130
/// Platform Driver implementation for `AsahiDriver`.
97131
impl platform::Driver for AsahiDriver {
98-
/// Our `DeviceData` type, reference-counted
99-
type Data = Arc<DeviceData>;
100-
/// Data associated with each hardware ID.
101132
type IdInfo = &'static hw::HwConfig;
102-
103-
// Assign the above OF ID table to this driver.
104-
kernel::driver_of_id_table!(ASAHI_ID_TABLE);
133+
const ID_TABLE: platform::IdTable<Self::IdInfo> = &OF_TABLE;
105134

106135
/// Device probe function.
107-
fn probe(
108-
pdev: &mut platform::Device,
109-
id_info: Option<&Self::IdInfo>,
110-
) -> Result<Arc<DeviceData>> {
136+
fn probe(pdev: &mut platform::Device, info: Option<&Self::IdInfo>) -> Result<Pin<KBox<Self>>> {
111137
debug::update_debug_flags();
112138

113-
let dev = device::Device::from_dev(pdev);
114-
115-
dev_info!(dev, "Probing...\n");
139+
dev_info!(pdev.as_ref(), "Probing...\n");
116140

117-
let cfg = id_info.ok_or(ENODEV)?;
141+
let cfg = info.ok_or(ENODEV)?;
118142

119143
pdev.set_dma_masks((1 << cfg.uat_oas) - 1)?;
120144

@@ -126,25 +150,25 @@ impl platform::Driver for AsahiDriver {
126150
// Start the coprocessor CPU, so UAT can initialize the handoff
127151
res.start_cpu()?;
128152

129-
let node = dev.of_node().ok_or(EIO)?;
130-
let compat: Vec<u32> = node.get_property(c_str!("apple,firmware-compat"))?;
153+
let node = pdev.as_ref().of_node().ok_or(EIO)?;
154+
let compat: KVec<u32> = node.get_property(c_str!("apple,firmware-compat"))?;
131155

132-
let reg = drm::drv::Registration::<AsahiDriver>::new(&dev)?;
156+
let drm = drm::device::Device::<AsahiDriver>::new_no_data(pdev.as_ref())?;
133157
let gpu = match (cfg.gpu_gen, cfg.gpu_variant, compat.as_slice()) {
134158
(hw::GpuGen::G13, _, &[12, 3, 0]) => {
135-
gpu::GpuManagerG13V12_3::new(reg.device(), &res, cfg)? as Arc<dyn gpu::GpuManager>
159+
gpu::GpuManagerG13V12_3::new(&drm, &res, cfg)? as Arc<dyn gpu::GpuManager>
136160
}
137161
(hw::GpuGen::G14, hw::GpuVariant::G, &[12, 4, 0]) => {
138-
gpu::GpuManagerG14V12_4::new(reg.device(), &res, cfg)? as Arc<dyn gpu::GpuManager>
162+
gpu::GpuManagerG14V12_4::new(&drm, &res, cfg)? as Arc<dyn gpu::GpuManager>
139163
}
140164
(hw::GpuGen::G13, _, &[13, 5, 0]) => {
141-
gpu::GpuManagerG13V13_5::new(reg.device(), &res, cfg)? as Arc<dyn gpu::GpuManager>
165+
gpu::GpuManagerG13V13_5::new(&drm, &res, cfg)? as Arc<dyn gpu::GpuManager>
142166
}
143167
(hw::GpuGen::G14, hw::GpuVariant::G, &[13, 5, 0]) => {
144-
gpu::GpuManagerG14V13_5::new(reg.device(), &res, cfg)? as Arc<dyn gpu::GpuManager>
168+
gpu::GpuManagerG14V13_5::new(&drm, &res, cfg)? as Arc<dyn gpu::GpuManager>
145169
}
146170
(hw::GpuGen::G14, _, &[13, 5, 0]) => {
147-
gpu::GpuManagerG14XV13_5::new(reg.device(), &res, cfg)? as Arc<dyn gpu::GpuManager>
171+
gpu::GpuManagerG14XV13_5::new(&drm, &res, cfg)? as Arc<dyn gpu::GpuManager>
148172
}
149173
_ => {
150174
dev_info!(
@@ -158,24 +182,22 @@ impl platform::Driver for AsahiDriver {
158182
}
159183
};
160184

161-
let data =
162-
kernel::new_device_data!(reg, res, AsahiData { dev, gpu }, "Asahi::Registrations")?;
185+
let data = Arc::pin_init(
186+
try_pin_init!(AsahiData {
187+
gpu,
188+
pdev: pdev.clone(),
189+
resources: res,
190+
}),
191+
GFP_KERNEL,
192+
)?;
163193

164-
let data: Arc<DeviceData> = data.into();
194+
// SAFETY: The drm device is not yet registered
195+
unsafe { drm.init_data(data.clone()) };
165196

166197
data.gpu.init()?;
167198

168-
kernel::drm_device_register!(
169-
// TODO: Expose an API to get a pinned reference here
170-
unsafe { Pin::new_unchecked(&mut *data.registrations().ok_or(ENXIO)?) },
171-
data.clone(),
172-
0
173-
)?;
199+
drm::drv::Registration::new_foreign_owned(drm.clone(), 0)?;
174200

175-
dev_info!(data.dev, "Probed!\n");
176-
Ok(data)
201+
Ok(KBox::new(Self(drm), GFP_KERNEL)?.into())
177202
}
178203
}
179-
180-
// Export the OF ID table as a module ID table, to make modpost/autoloading work.
181-
kernel::module_of_id_table!(MOD_TABLE, ASAHI_ID_TABLE);

0 commit comments

Comments
 (0)