Skip to content

Commit

Permalink
drm/asahi: Align kernel range to buffer::PAGE_SIZE
Browse files Browse the repository at this point in the history
We only require alignment to the UAT page size from userspace, but
internally we need more, so just align it if userspace gives us lower
alignment.

Signed-off-by: Asahi Lina <[email protected]>
  • Loading branch information
asahilina committed Oct 3, 2024
1 parent 1bbe2a3 commit 632de37
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
11 changes: 9 additions & 2 deletions drivers/gpu/drm/asahi/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
use crate::debug::*;
use crate::driver::AsahiDevice;
use crate::{alloc, buffer, driver, gem, hw, mmu, queue, util::RangeExt};
use crate::{
alloc, buffer, driver, gem, hw, mmu, queue,
util::{align, align_down, RangeExt},
};
use core::mem::MaybeUninit;
use core::ops::Range;
use kernel::dma_fence::RawDmaFence;
Expand Down Expand Up @@ -319,7 +322,11 @@ impl File {
return Err(EINVAL);
}

let kernel_half_size = (kernel_range.range() >> 1) & !(mmu::UAT_PGMSK as u64);
// Align to buffer::PAGE_SIZE so the allocators are happy
let kernel_range = align(kernel_range.start, buffer::PAGE_SIZE as u64)
..align_down(kernel_range.end, buffer::PAGE_SIZE as u64);

let kernel_half_size = align_down(kernel_range.range() >> 1, buffer::PAGE_SIZE as u64);
let kernel_gpu_range = kernel_range.start..(kernel_range.start + kernel_half_size);
let kernel_gpufw_range = kernel_gpu_range.end..kernel_range.end;

Expand Down
20 changes: 20 additions & 0 deletions drivers/gpu/drm/asahi/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ where
(a + b - one) & !(b - one)
}

/// Aligns an integer type down to a power of two.
pub(crate) fn align_down<T>(a: T, b: T) -> T
where
T: Copy
+ Default
+ BitAnd<Output = T>
+ Not<Output = T>
+ Sub<Output = T>
+ Div<Output = T>
+ core::cmp::PartialEq,
{
let def: T = Default::default();
#[allow(clippy::eq_op)]
let one: T = !def / !def;

assert!((b & (b - one)) == def);

a & !(b - one)
}

/// Integer division rounding up.
pub(crate) fn div_ceil<T>(a: T, b: T) -> T
where
Expand Down

0 comments on commit 632de37

Please sign in to comment.