Skip to content
Open
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
476 changes: 388 additions & 88 deletions drivers/gpu/drm/asahi/g17/completion/publication.rs

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions drivers/gpu/drm/asahi/g17/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub(crate) struct Packet {
pub(crate) footprint: Option<Arc<super::hazard::Footprint>>,
/// Number of render passes in the submit ioctl that created this packet.
pub(crate) batch_renders: u16,
inputs: KVec<Fence>,
inputs: KVVec<Fence>,
/// The submission's own input fences ([`super::sync_input::Inputs`]), unless compute sources
/// take them over in firmware.
shared_inputs: Option<super::sync_input::CommandInputs>,
Expand Down Expand Up @@ -169,7 +169,7 @@ impl Packet {
order: Order,
completion: Arc<Completion>,
batch_renders: u16,
inputs: KVec<Fence>,
inputs: KVVec<Fence>,
shared_inputs: Option<super::sync_input::CommandInputs>,
producers: Option<Producers>,
) -> Result<Arc<Self>> {
Expand Down Expand Up @@ -663,7 +663,7 @@ pub(crate) struct Fences {
/// timestamp-free publication chaining. `None` for commands that write their
/// own timestamps (covered by `completed`) or have no pending timestamp
/// ancestors.
pub(crate) timestamp_waits: Option<Arc<KVec<Fence>>>,
pub(crate) timestamp_waits: Option<Arc<KVVec<Fence>>>,
/// `(since, prefix)` of [`Producers`] when the command waits for the other engine's
/// producers in firmware: what that engine's failure log must keep for it.
pub(crate) producers: Option<(u64, Option<u64>)>,
Expand Down Expand Up @@ -743,7 +743,7 @@ impl<B: Backend> Scheduler<B> {
pub(crate) fn enqueue(
&mut self,
packet: Arc<Packet>,
dependencies: KVec<Fence>,
dependencies: KVVec<Fence>,
early: bool,
publication: Option<super::completion::PreparedPublication>,
) -> Result<Fences> {
Expand Down
68 changes: 37 additions & 31 deletions drivers/gpu/drm/asahi/g17/queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,26 +406,29 @@ impl<B: Backend> Queue<B> {
)?;
let input_dependencies =
command_inputs.as_ref().map_or(&[][..], |inputs| inputs.dependencies());
let mut dependencies = KVec::with_capacity(
// These grow with the producers this command waits for, which userspace decides: the
// other engine's unfinished commands alone can number thousands. So they need no
// contiguous pages and are charged to the caller.
let mut dependencies = KVVec::with_capacity(
// The firmware-ordered producers add one scheduled fence between them.
input_dependencies.len()
+ ordering_host_waits.len()
+ usize::from(!ordering_firmware_waits.is_empty())
+ failure_count,
GFP_KERNEL,
GFP_KERNEL_ACCOUNT,
)?;
// Host-waited producers are checked on the host instead of in firmware.
let mut checked_inputs = KVec::with_capacity(
let mut checked_inputs = KVVec::with_capacity(
ordering_host_waits.len() + failure_count,
GFP_KERNEL,
GFP_KERNEL_ACCOUNT,
)?;
for fence in failures.into_iter().flatten() {
checked_inputs.push(fence.clone(), GFP_KERNEL)?;
dependencies.push(fence.clone(), GFP_KERNEL)?;
checked_inputs.push(fence.clone(), GFP_KERNEL_ACCOUNT)?;
dependencies.push(fence.clone(), GFP_KERNEL_ACCOUNT)?;
}
for fence in ordering_host_waits {
dependencies.push(fence.clone(), GFP_KERNEL)?;
checked_inputs.push(fence, GFP_KERNEL)?;
dependencies.push(fence.clone(), GFP_KERNEL_ACCOUNT)?;
checked_inputs.push(fence, GFP_KERNEL_ACCOUNT)?;
}
// Firmware orders execution after each producer; the scheduler needs only the latest
// scheduled fence of producers that have not already been published into firmware.
Expand All @@ -436,7 +439,7 @@ impl<B: Backend> Queue<B> {
{
if !*published && job::fence_status(scheduled) <= 0 {
producer_waits.producer(scheduled, |fence| {
Ok(dependencies.push(fence.clone(), GFP_KERNEL)?)
Ok(dependencies.push(fence.clone(), GFP_KERNEL_ACCOUNT)?)
})?;
}
}
Expand All @@ -449,16 +452,16 @@ impl<B: Backend> Queue<B> {
since,
prefix,
});
producer_waits.finish(|fence| Ok(dependencies.push(fence.clone(), GFP_KERNEL)?))?;
producer_waits.finish(|fence| Ok(dependencies.push(fence.clone(), GFP_KERNEL_ACCOUNT)?))?;
// Independent engines may publish in either order and must each consume an input's error
// status before running, so every command keeps the inputs.
for fence in input_dependencies {
dependencies.push(fence.clone(), GFP_KERNEL)?;
dependencies.push(fence.clone(), GFP_KERNEL_ACCOUNT)?;
}
if let Some(sources) = &sources {
for ready in sources.publication_waits() {
dependencies.push(ready.clone(), GFP_KERNEL)?;
checked_inputs.push(ready.clone(), GFP_KERNEL)?;
dependencies.push(ready.clone(), GFP_KERNEL_ACCOUNT)?;
checked_inputs.push(ready.clone(), GFP_KERNEL_ACCOUNT)?;
}
}
// Publish from the ioctl whenever nothing has to wait, at any depth.
Expand Down Expand Up @@ -878,17 +881,17 @@ impl FrontierEntry for Fences {
}
}

fn push_unique_fence(out: &mut KVec<Fence>, fence: &Fence) -> Result {
fn push_unique_fence(out: &mut KVVec<Fence>, fence: &Fence) -> Result {
if !out.iter().any(|existing| existing.raw() == fence.raw()) {
out.push(fence.clone(), GFP_KERNEL)?;
out.push(fence.clone(), GFP_KERNEL_ACCOUNT)?;
}
Ok(())
}

/// Collect the actual pending host-timestamp writer fences represented by `entry`:
/// its own `completed` fence when it writes host timestamps, otherwise its still-pending
/// inherited timestamp-writer fences.
fn collect_timestamp_waits<E: FrontierEntry>(entry: &E, out: &mut KVec<Fence>) -> Result {
fn collect_timestamp_waits<E: FrontierEntry>(entry: &E, out: &mut KVVec<Fence>) -> Result {
let Some(pub_ref) = entry.pending_publication() else {
return Ok(());
};
Expand All @@ -904,15 +907,18 @@ fn collect_timestamp_waits<E: FrontierEntry>(entry: &E, out: &mut KVec<Fence>) -
Ok(())
}

/// Each of these grows with the covered prefixes, which userspace sizes: the other engine's
/// unfinished commands alone can number thousands. So they need no contiguous pages and are
/// charged to the caller.
struct EnqueueOrdering<'a> {
predecessors: KVec<&'a Arc<Publication>>,
predecessors: KVVec<&'a Arc<Publication>>,
/// How far the command's ancestors reach on each engine, for its publication
/// ([`Publication::set_reach`]).
reach: [u64; 2],
host_waits: KVec<Fence>,
firmware_waits: KVec<(Fence, Fence)>,
firmware_published: KVec<bool>,
inherited_timestamp_waits: KVec<Fence>,
host_waits: KVVec<Fence>,
firmware_waits: KVVec<(Fence, Fence)>,
firmware_published: KVVec<bool>,
inherited_timestamp_waits: KVVec<Fence>,
}

impl<'a> EnqueueOrdering<'a> {
Expand Down Expand Up @@ -940,11 +946,11 @@ impl<'a> EnqueueOrdering<'a> {
// deferred error published between pruning and link selection,
// `add_successor()` still delivers that failure immediately instead of
// letting the successor signal success.
let mut predecessors = KVec::new();
let mut host = KVec::new();
let mut firmware = KVec::new();
let mut firmware_published = KVec::new();
let mut inherited_timestamp_waits = KVec::new();
let mut predecessors = KVVec::new();
let mut host = KVVec::new();
let mut firmware = KVVec::new();
let mut firmware_published = KVVec::new();
let mut inherited_timestamp_waits = KVVec::new();
// A covered publication some kept predecessor already has as an ancestor needs no link of
// its own: it fires before that predecessor, which fires before this command, and its
// error reaches this command through it. Newest first, so the kept ones reach furthest.
Expand All @@ -953,7 +959,7 @@ impl<'a> EnqueueOrdering<'a> {
-> Result
{
if sequence > covered[engine_index] {
predecessors.push(publication, GFP_KERNEL)?;
predecessors.push(publication, GFP_KERNEL_ACCOUNT)?;
for (reach, slot) in covered.iter_mut().enumerate() {
*slot = (*slot).max(publication.reach(reach));
}
Expand Down Expand Up @@ -1040,16 +1046,16 @@ impl<'a> EnqueueOrdering<'a> {
collect_timestamp_waits(entry, &mut host)?;
firmware.push(
(entry.scheduled().clone(), entry.completed().clone()),
GFP_KERNEL,
GFP_KERNEL_ACCOUNT,
)?;
firmware_published.push(entry.inline_published(), GFP_KERNEL)?;
firmware_published.push(entry.inline_published(), GFP_KERNEL_ACCOUNT)?;
}
} else {
firmware.push(
(entry.scheduled().clone(), entry.completed().clone()),
GFP_KERNEL,
GFP_KERNEL_ACCOUNT,
)?;
firmware_published.push(entry.inline_published(), GFP_KERNEL)?;
firmware_published.push(entry.inline_published(), GFP_KERNEL_ACCOUNT)?;
}
}

Expand Down
5 changes: 3 additions & 2 deletions drivers/gpu/drm/asahi/g17/sync_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,12 @@ struct dma_fence *asahi_g17_merge_fences(struct dma_fence *const *fences, size_t
struct dma_fence_unwrap *cursors;
struct dma_fence *merged;

cursors = kmalloc_array(count, sizeof(*cursors), GFP_KERNEL);
/* One cursor per fence the command waits for: no contiguous pages needed. */
cursors = kvmalloc_array(count, sizeof(*cursors), GFP_KERNEL_ACCOUNT);
if (!cursors)
return NULL;
merged = __dma_fence_unwrap_merge(count, (struct dma_fence **)fences, cursors);
kfree(cursors);
kvfree(cursors);
return merged;
}

Expand Down
5 changes: 3 additions & 2 deletions drivers/gpu/drm/asahi/g17/sync_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ impl super::dependency::FenceOrder for Fence {

/// One fence that signals once all of `fences` have (see `job::MERGED_DEPENDENCIES`).
pub(super) fn merge(fences: &[Fence]) -> Result<Fence> {
let mut raw = KVec::with_capacity(fences.len(), GFP_KERNEL)?;
// As many as the command waits for: no contiguous pages, charged to the caller.
let mut raw = KVVec::with_capacity(fences.len(), GFP_KERNEL_ACCOUNT)?;
for fence in fences {
raw.push(fence.raw(), GFP_KERNEL)?;
raw.push(fence.raw(), GFP_KERNEL_ACCOUNT)?;
}
// SAFETY: Every pointer is a live fence borrowed from `fences` for this call;
// the helper only reads them and returns a new reference of its own.
Expand Down
6 changes: 3 additions & 3 deletions tools/asahi/g17-inline-cross-engine-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def build_harness(mutation=None):
# Verify structural pieces that feed into inline_published.
fences_has_inline = (
'pub(crate) inline_published: bool,' in job_src
and 'firmware_published.push(entry.inline_published(), GFP_KERNEL)?;' in queue_src
and 'firmware_published.push(entry.inline_published(), GFP_KERNEL_ACCOUNT)?;' in queue_src
)

# Extract the guard prefix of try_compute_early and try_render_early up to begin_publication.
Expand All @@ -122,9 +122,9 @@ def build_harness(mutation=None):
rend_guard_end = rend_early_fn.index('let admission =', rend_guard_start)
rend_guard = rend_early_fn[rend_guard_start:rend_guard_end]

# Normalize loop_block for std Vec instead of KVec(..., GFP_KERNEL)?.
# Normalize loop_block for std Vec instead of KVVec(..., GFP_KERNEL_ACCOUNT)?.
loop_std = loop_block.replace(
'Ok(dependencies.push(fence.clone(), GFP_KERNEL)?)',
'Ok(dependencies.push(fence.clone(), GFP_KERNEL_ACCOUNT)?)',
'{ dependencies.push(fence.clone()); Ok::<(), i32>(()) }',
)

Expand Down
20 changes: 10 additions & 10 deletions tools/asahi/g17-publication-lazy-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ def build_harness(mutation=None):
claim_fn = item(pub_src, 'fn claim_locked(&self, state: &mut State)')
publish_fn = item(pub_src, 'fn publish(&self, result: Result<[u64; 4]>) -> Links')
is_pending_fn = item(pub_src, 'fn pending_within(&self, depth: usize) -> bool')
add_succ_fn = item(pub_src, 'fn add_successor(&self, mut link: KBox<Link>) -> bool')
add_succ_fn = item(pub_src, 'fn add_successor(&self, link: NonNull<Link>) -> bool')
subscribe_fn = item(pub_src, 'fn subscribe(mut stack: Option<Arc<Publication>>)')
link_fn = item(pub_src, 'fn link_deferred(')

if mutation == 'eager-activate':
activate_fn = activate_fn.replace(
'state.deferred_links = self.links;',
'for (p, l) in self.links { p.add_successor(l); }',
'state.deferred_links = links;',
'predecessor.add_successor(links);',
1,
)
elif mutation == 'cycle-target-some':
Expand Down Expand Up @@ -88,22 +88,22 @@ def build_harness(mutation=None):
1,
)
elif mutation == 'no-subscribe':
add_succ_fn = add_succ_fn.replace('&& !state.deferred_links.is_empty();', '&& false;', 1)
add_succ_fn = add_succ_fn.replace('&& state.deferred_links.is_some();', '&& false;', 1)
elif mutation == 'skip-fast-check':
link_fn = link_fn.replace(
'if predecessor.is_settled_clean() {',
'if false {',
'Some(predecessor) if !predecessor.is_settled_clean() =>',
'Some(predecessor) if true =>',
1,
)

src_checks = {
'single_atomic_phase': 'phase: AtomicU8,' in pub_src
and 'pending: AtomicBool' not in pub_src
and 'settled_clean: AtomicBool' not in pub_src,
'lazy_activate': 'state.deferred_links = self.links;' in activate_fn
'lazy_activate': 'state.deferred_links = links;' in activate_fn
and 'predecessor.add_successor' not in activate_fn,
'no_cycle_prep': 'target: None,' in new_prep_fn,
'fast_settled_check': 'if predecessor.is_settled_clean() {' in link_fn,
'fast_settled_check': 'Some(predecessor) if !predecessor.is_settled_clean() =>' in link_fn,
'error_phase_on_publish': 'PHASE_FAILED_AFTER_SETTLE' in publish_fn
and 'PHASE_FAILED_PENDING' in publish_fn,
'revoke_clean_on_claim_err': 'state.upstream_released = false;' in claim_fn
Expand All @@ -114,9 +114,9 @@ def build_harness(mutation=None):
and 'self.phase.store(PHASE_SETTLED_CLEAN, Ordering::Release);' in is_pending_fn,
'bounded_probe': 'pred.pending_within(depth - 1)' in is_pending_fn,
'consume_failed': 'Some((index, PHASE_FAILED_PENDING | PHASE_FAILED_AFTER_SETTLE)) => {' in is_pending_fn
and 'Probe::Consumed(state.deferred_links.swap_remove(index))' in is_pending_fn
and 'Probe::Consumed(state.unlink_deferred(index))' in is_pending_fn
and 'let first_unclean =' in is_pending_fn,
'subscribe_deferred': '&& !state.deferred_links.is_empty();' in add_succ_fn
'subscribe_deferred': '&& state.deferred_links.is_some();' in add_succ_fn
and 'state.subscribe_queued |= subscribe;' in add_succ_fn
and '*stack = Some(predecessor);' in link_fn
and 'node.link_deferred(deferred, &mut stack);' in subscribe_fn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ def extract_function(text, name):
static inline void smp_rmb(void)
{
}
#define GFP_KERNEL_ACCOUNT 0

struct dma_fence {
uint64_t context;
Expand Down Expand Up @@ -641,13 +642,13 @@ def extract_function(text, name):
for ((fence) = dma_fence_unwrap_first((head), (cursor)); (fence); \
(fence) = dma_fence_unwrap_next((cursor)))

static inline void *kmalloc_array(size_t n, size_t size, int flags)
static inline void *kvmalloc_array(size_t n, size_t size, int flags)
{
(void)flags;
return calloc(n, size);
}

static inline void kfree(void *p)
static inline void kvfree(void *p)
{
free(p);
}
Expand Down
Loading