Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion drivers/gpu/drm/asahi/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: GPL-2.0

obj-$(CONFIG_DRM_ASAHI) += aurora.o
aurora-y := asahi_core.o fwid.o sysfs.o g17/sync_input.o g17/sync_output.o g17/sync_scope.o g17/trace.o g17/clpc.o g17/fans.o
aurora-y := asahi_core.o fwid.o legacy_sched.o sysfs.o g17/sync_input.o g17/sync_output.o g17/sync_scope.o g17/trace.o g17/clpc.o g17/fans.o
CFLAGS_g17/trace.o := -I$(src)/g17

aurora-$(CONFIG_DRM_ASAHI_G15) += g15/glue.o
Expand Down
15 changes: 15 additions & 0 deletions drivers/gpu/drm/asahi/asahi_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod gpu;
mod hw;
mod initdata;
mod m3;
mod legacy_rx;
mod mem;
mod memacct;
mod microseq;
Expand Down Expand Up @@ -90,6 +91,7 @@ struct DriverModule {
_g18: kernel::driver::Registration<kernel::platform::Adapter<g18::probe::Driver>>,
#[pin]
_driver: kernel::driver::Registration<kernel::platform::Adapter<driver::AsahiPlatformDriver>>,
_legacy_rx: legacy_rx::QueueOwner,
_cleanup: cleanup::QueueOwner,
#[cfg(CONFIG_DRM_ASAHI_G15)]
_g15_ready: (),
Expand All @@ -104,6 +106,7 @@ impl kernel::InPlaceModule for DriverModule {
try_pin_init!(Self {
// Initialize the queue first, before registration can invoke probe.
_cleanup: cleanup::QueueOwner::new()?,
_legacy_rx: legacy_rx::QueueOwner::new()?,
#[cfg(CONFIG_DRM_ASAHI_G15)]
_g15 <- kernel::driver::Registration::new(c"aurora-g15", module),
_driver <- kernel::driver::Registration::new(
Expand Down Expand Up @@ -167,6 +170,18 @@ module! {
// permissions: 0o644,
description: "Fully isolate GPU contexts (limits performance)",
},
legacy_inline_submit: u32 {
default: 1,
description: "G13/G14: publish ready in-order jobs from SUBMIT (0: off)",
},
legacy_rx_fastpath: u32 {
default: 1,
description: "G13/G14: drain GPU events on a dedicated receive worker (0: off)",
},
legacy_rx_util_min: u32 {
default: 512,
description: "G13/G14: GPU event-drain utilization minimum (0..1024)",
},
g15_deadline_ms: u32 {
default: 5000,
description: "G15: host deadline of a published prefix without progress, ms (min 100)",
Expand Down
119 changes: 114 additions & 5 deletions drivers/gpu/drm/asahi/gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use core::ops::Range;
use core::slice;
use core::sync::atomic::{
AtomicBool,
AtomicU32,
AtomicU64,
Ordering, //
};
Expand All @@ -36,6 +37,7 @@ use kernel::{
macros::versions,
new_mutex,
new_spinlock,
new_work,
prelude::*,
soc::apple::rtkit,
sync::{
Expand All @@ -55,6 +57,7 @@ use kernel::{
},
types::ForeignOwnable,
uapi, //
workqueue::{HasWork, Work, WorkItem},
};

use crate::alloc::Allocator;
Expand All @@ -81,6 +84,7 @@ use crate::{
gem,
hw,
initdata,
legacy_rx,
mem,
mmu,
queue,
Expand All @@ -102,6 +106,9 @@ const EP_DOORBELL: u8 = 0x21;
const MSG_INIT: u64 = 0x81 << 48;
const INIT_DATA_MASK: u64 = (1 << 44) - 1;

const RX_WORK_PENDING: u32 = 1 << 0;
const RX_WORK_RUNNING: u32 = 1 << 1;

/// TX channel doorbell.
const MSG_TX_DOORBELL: u64 = 0x83 << 48;
/// Firmware control channel doorbell.
Expand Down Expand Up @@ -254,6 +261,9 @@ pub(crate) struct GpuManager {
rtkit: Mutex<Option<rtkit::RtKit<GpuManager::ver>>>,
#[pin]
rx_channels: Mutex<RxChannels::ver>,
rx_work_state: AtomicU32,
#[pin]
rx_work: Work<GpuManager::ver>,
#[pin]
tx_channels: Mutex<TxChannels::ver>,
#[pin]
Expand All @@ -270,6 +280,46 @@ pub(crate) struct GpuManager {
garbage_contexts: Mutex<KVec<workqueue::RetiredContext>>,
}

#[versions(AGX)]
unsafe impl HasWork<GpuManager::ver> for GpuManager::ver {
unsafe fn raw_get_work(ptr: *mut Self) -> *mut Work<GpuManager::ver> {
// SAFETY: The caller supplies a live GpuManager allocation.
unsafe { core::ptr::addr_of_mut!((*ptr).rx_work) }
}

unsafe fn work_container_of(ptr: *mut Work<GpuManager::ver>) -> *mut Self {
// SAFETY: `ptr` came from `raw_get_work` above.
unsafe { kernel::container_of!(ptr, Self, rx_work) }
}
}

#[versions(AGX)]
impl WorkItem for GpuManager::ver {
type Pointer = Arc<Self>;

fn run(this: Arc<Self>) {
let _util_min =
legacy_rx::UtilMinGuard::new(crate::module_parameters::legacy_rx_util_min.value());

loop {
this.rx_work_state
.fetch_and(!RX_WORK_PENDING, Ordering::AcqRel);
this.poll_rx_channels();

if this.rx_work_state.load(Ordering::Acquire) & RX_WORK_PENDING != 0 {
continue;
}
if this
.rx_work_state
.compare_exchange(RX_WORK_RUNNING, 0, Ordering::AcqRel, Ordering::Acquire)
.is_ok()
{
break;
}
}
}
}

/// Operations the DRM driver performs on a GPU, independent of its firmware interface.
pub(crate) trait Gpu: Send + Sync {
/// Admission bound checked before copying the userspace command stream.
Expand Down Expand Up @@ -474,12 +524,24 @@ impl rtkit::Operations for GpuManager::ver {
return;
}

let mut ch = data.rx_channels.lock();
data.poll_rx_channels();
}

ch.fw_log.poll();
ch.ktrace.poll();
ch.stats.poll();
ch.event.poll();
fn recv_message_early(
data: <Self::Data as ForeignOwnable>::Borrowed<'_>,
ep: u8,
msg: u64,
) -> bool {
if crate::module_parameters::legacy_rx_fastpath.value() == 0
|| ep != EP_FIRMWARE
|| msg != MSG_RX_DOORBELL
|| legacy_rx::get().is_none()
{
return false;
}

let manager = Arc::from(data);
manager.queue_rx_work()
}

fn crashed(data: <Self::Data as ForeignOwnable>::Borrowed<'_>, _crashlog: Option<&[u8]>) {
Expand Down Expand Up @@ -518,6 +580,51 @@ impl rtkit::Operations for GpuManager::ver {

#[versions(AGX)]
impl GpuManager::ver {
fn poll_rx_channels(&self) {
let mut ch = self.rx_channels.lock();

ch.fw_log.poll();
ch.ktrace.poll();
ch.stats.poll();
ch.event.poll();
}

/// Coalesce doorbells without losing the one arriving as work returns.
fn queue_rx_work(self: Arc<Self>) -> bool {
loop {
let state = self
.rx_work_state
.fetch_or(RX_WORK_PENDING, Ordering::AcqRel);
if state & RX_WORK_RUNNING != 0 {
return true;
}
if self
.rx_work_state
.compare_exchange(
state | RX_WORK_PENDING,
state | RX_WORK_PENDING | RX_WORK_RUNNING,
Ordering::AcqRel,
Ordering::Acquire,
)
.is_err()
{
continue;
}

let Some(queue) = legacy_rx::get() else {
self.rx_work_state.store(0, Ordering::Release);
return false;
};
return match queue.enqueue(self) {
Ok(()) => true,
Err(manager) => {
manager.rx_work_state.store(0, Ordering::Release);
false
}
};
}
}

/// Create a new GpuManager of this version/GPU combination.
#[inline(never)]
pub(crate) fn new(
Expand Down Expand Up @@ -909,6 +1016,8 @@ impl GpuManager::ver {
alloc <- new_mutex!(alloc, "alloc"),
fwctl_channel <- new_mutex!(fwctl_channel, "fwctl_channel"),
rx_channels <- new_mutex!(KBox::<RxChannels::ver>::into_inner(rx_channels), "rx_channels"),
rx_work_state: AtomicU32::new(0),
rx_work <- new_work!("GpuManager::rx_work"),
tx_channels <- new_mutex!(KBox::<TxChannels::ver>::into_inner(tx_channels), "tx_channels"),
pipes,
buffer_mgr,
Expand Down
103 changes: 103 additions & 0 deletions drivers/gpu/drm/asahi/legacy_rx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT

//! Dedicated receive work for the legacy firmware path.
//!
//! RTKit normally allocates a work item for every mailbox message. The legacy
//! GPU only sends a doorbell here, and the event ring itself is authoritative,
//! so one preallocated, coalescing driver work item is enough.

use core::{
ptr::{self, NonNull},
sync::atomic::{AtomicPtr, Ordering},
};
use kernel::{bindings, c_str, prelude::*, workqueue::Queue};

unsafe extern "C" {
fn asahi_legacy_set_current_util_min(util_min: i32) -> i32;
}

static QUEUE: AtomicPtr<bindings::workqueue_struct> = AtomicPtr::new(ptr::null_mut());

/// Destroyed after platform-driver unregister has stopped every RTKit producer.
pub(crate) struct QueueOwner(NonNull<bindings::workqueue_struct>);

// SAFETY: QueueOwner only transfers ownership of a thread-safe kernel queue.
unsafe impl Send for QueueOwner {}
// SAFETY: The queue API synchronizes all accesses; destruction requires ownership.
unsafe impl Sync for QueueOwner {}

impl QueueOwner {
pub(crate) fn new() -> Result<Self> {
// SAFETY: The name is NUL terminated and contains no format specifiers.
let raw = unsafe {
bindings::alloc_workqueue_noprof(
c_str!("asahi-legacy-rx").as_char_ptr(),
bindings::wq_flags_WQ_UNBOUND
| bindings::wq_flags_WQ_HIGHPRI
| bindings::wq_flags_WQ_MEM_RECLAIM,
1,
)
};
let raw = NonNull::new(raw).ok_or(ENOMEM)?;
if QUEUE
.compare_exchange(
ptr::null_mut(),
raw.as_ptr(),
Ordering::AcqRel,
Ordering::Acquire,
)
.is_err()
{
// SAFETY: This fresh queue has never been published or used.
unsafe { bindings::destroy_workqueue(raw.as_ptr()) };
return Err(EBUSY);
}
Ok(Self(raw))
}
}

impl Drop for QueueOwner {
fn drop(&mut self) {
// Registration has already been dropped, so no callback can enqueue
// after this drain begins.
unsafe { bindings::destroy_workqueue(self.0.as_ptr()) };
QUEUE.store(ptr::null_mut(), Ordering::Release);
}
}

pub(crate) fn get() -> Option<&'static Queue> {
let raw = QUEUE.load(Ordering::Acquire);
if raw.is_null() {
None
} else {
// SAFETY: QueueOwner retains the allocation until driver unregister has
// joined the callback producers and drained this queue.
Some(unsafe { Queue::from_raw(raw) })
}
}

/// A scoped capacity request for the shared unbound worker executing RX work.
/// Resetting it before returning prevents the hint leaking to unrelated work.
pub(crate) struct UtilMinGuard(bool);

impl UtilMinGuard {
pub(crate) fn new(value: u32) -> Self {
Self(Self::set(value.min(1024) as i32))
}

fn set(value: i32) -> bool {
// SAFETY: The helper changes only current's utilization clamp and
// accepts either 0..1024 or -1 to restore the default.
unsafe { asahi_legacy_set_current_util_min(value) == 0 }
}
}

impl Drop for UtilMinGuard {
fn drop(&mut self) {
if self.0 {
// -1 restores the task's default clamp, matching the mailbox RX
// hint contract.
let _ = Self::set(-1);
}
}
}
21 changes: 21 additions & 0 deletions drivers/gpu/drm/asahi/legacy_sched.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT

#include <linux/sched.h>
#include <uapi/linux/sched/types.h>

#include "legacy_sched.h"

int asahi_legacy_set_current_util_min(int util_min)
{
struct sched_attr attr = {
.sched_policy = current->policy,
.sched_priority = current->rt_priority,
.sched_flags = SCHED_FLAG_KEEP_ALL | SCHED_FLAG_UTIL_CLAMP_MIN,
.sched_util_min = util_min,
};

if (util_min < -1 || util_min > SCHED_CAPACITY_SCALE)
return -EINVAL;

return sched_setattr_nocheck(current, &attr);
}
8 changes: 8 additions & 0 deletions drivers/gpu/drm/asahi/legacy_sched.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* SPDX-License-Identifier: GPL-2.0-only OR MIT */

#ifndef __ASAHI_LEGACY_SCHED_H__
#define __ASAHI_LEGACY_SCHED_H__

int asahi_legacy_set_current_util_min(int util_min);

#endif
Loading
Loading