Skip to content

Commit

Permalink
drm/asahi: It begins (in Rust)
Browse files Browse the repository at this point in the history
This implements basic RTKit bring-up and UAT page table management.

Signed-off-by: Asahi Lina <lina@asahilina.net>
  • Loading branch information
asahilina committed Nov 4, 2022
1 parent e53f673 commit 1608019
Show file tree
Hide file tree
Showing 6 changed files with 568 additions and 0 deletions.
2 changes: 2 additions & 0 deletions drivers/gpu/drm/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,8 @@ source "drivers/gpu/drm/sprd/Kconfig"

source "drivers/gpu/drm/apple/Kconfig"

source "drivers/gpu/drm/asahi/Kconfig"

config DRM_HYPERV
tristate "DRM Support for Hyper-V synthetic video device"
depends on DRM && PCI && MMU && HYPERV
Expand Down
1 change: 1 addition & 0 deletions drivers/gpu/drm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,4 @@ obj-y += gud/
obj-$(CONFIG_DRM_HYPERV) += hyperv/
obj-y += solomon/
obj-$(CONFIG_DRM_SPRD) += sprd/
obj-$(CONFIG_DRM_ASAHI) += asahi/
14 changes: 14 additions & 0 deletions drivers/gpu/drm/asahi/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# SPDX-License-Identifier: GPL-2.0

config DRM_ASAHI
tristate "Asahi (DRM support for Apple AGX GPUs)"
depends on RUST
depends on DRM
depends on (ARM64 && ARCH_APPLE) || (COMPILE_TEST && !GENERIC_ATOMIC64)
depends on MMU
select DRM_SCHED
select IOMMU_SUPPORT
select IOMMU_IO_PGTABLE_LPAE
select DRM_GEM_SHMEM_HELPER
help
DRM driver for Apple AGX GPUs (G13x, found in the M1 SoC family)
7 changes: 7 additions & 0 deletions drivers/gpu/drm/asahi/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPDX-License-Identifier: GPL-2.0

asahi-y := \
asahi_drv.o \
asahi_mmu.o

obj-$(CONFIG_DRM_ASAHI) += asahi.o
105 changes: 105 additions & 0 deletions drivers/gpu/drm/asahi/asahi_drv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT

//! Driver for the Apple AGX GPUs found in Apple Silicon SoCs.

use kernel::{
bindings, device,
error::{to_result, Result},
io_mem::IoMem,
module_platform_driver, of, platform,
prelude::*,
soc::apple::rtkit,
sync::smutex::Mutex,
sync::{Arc, ArcBorrow},
};

const ASC_CTL_SIZE: usize = 0x4000;
const CPU_CONTROL: usize = 0x44;
const CPU_RUN: u32 = 0x1 << 4; // BIT(4)

struct AsahiData {
dev: device::Device,
rtkit: Mutex<Option<rtkit::RTKit<AsahiDevice>>>,
}

struct AsahiResources {
asc: IoMem<ASC_CTL_SIZE>,
}

type DeviceData = device::Data<(), AsahiResources, AsahiData>;

struct AsahiDevice;

impl AsahiDevice {
fn start_cpu(data: ArcBorrow<'_, DeviceData>) -> Result {
let res = data.resources().ok_or(ENXIO)?;
let val = res.asc.readl_relaxed(CPU_CONTROL);

res.asc.writel_relaxed(val | CPU_RUN, CPU_CONTROL);

Ok(())
}
}

#[vtable]
impl rtkit::Operations for AsahiDevice {
type Data = ();
}

extern "C" {
pub fn asahi_mmu_init(dev: *mut bindings::device) -> core::ffi::c_int;
}

impl platform::Driver for AsahiDevice {
type Data = Arc<DeviceData>;

kernel::define_of_id_table! {(), [
(of::DeviceId::Compatible(b"apple,agx-t8103"), None),
]}

fn probe(
pdev: &mut platform::Device,
_id_info: Option<&Self::IdInfo>,
) -> Result<Arc<DeviceData>> {
let dev = device::Device::from_dev(pdev);

dev_info!(dev, "probing!\n");

// TODO: add device abstraction to ioremap by name
// SAFETY: AGX does DMA via the UAT IOMMU (mostly)
let asc_reg = unsafe { pdev.ioremap_resource(0)? };

let data = kernel::new_device_data!(
(),
AsahiResources {
// SAFETY: This device does DMA via the UAT IOMMU.
asc: asc_reg,
},
AsahiData {
dev: dev,
rtkit: Mutex::new(None),
},
"Asahi::Registrations"
)?;

let data = Arc::<DeviceData>::from(data);

AsahiDevice::start_cpu(data.as_ref_borrow())?;

to_result(unsafe { asahi_mmu_init((&data.dev as &dyn device::RawDevice).raw_device()) })?;

let mut rtkit = unsafe { rtkit::RTKit::<AsahiDevice>::new(&data.dev, None, 0, ()) }?;

rtkit.boot()?;
*data.rtkit.lock() = Some(rtkit);

dev_info!(data.dev, "probed!\n");
Ok(data)
}
}

module_platform_driver! {
type: AsahiDevice,
name: "asahi",
license: "Dual MIT/GPL",
}
Loading

0 comments on commit 1608019

Please sign in to comment.