Skip to content

Commit ce5529b

Browse files
hoshinolinajannau
authored andcommitted
drm/asahi: Align kernel range to buffer::PAGE_SIZE
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 <lina@asahilina.net>
1 parent ea4f38d commit ce5529b

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

drivers/gpu/drm/asahi/file.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
1010
use crate::debug::*;
1111
use crate::driver::AsahiDevice;
12-
use crate::{alloc, buffer, driver, gem, hw, mmu, queue, util::RangeExt};
12+
use crate::{
13+
alloc, buffer, driver, gem, hw, mmu, queue,
14+
util::{align, align_down, RangeExt},
15+
};
1316
use core::mem::MaybeUninit;
1417
use core::ops::Range;
1518
use kernel::dma_fence::RawDmaFence;
@@ -319,7 +322,11 @@ impl File {
319322
return Err(EINVAL);
320323
}
321324

322-
let kernel_half_size = (kernel_range.range() >> 1) & !(mmu::UAT_PGMSK as u64);
325+
// Align to buffer::PAGE_SIZE so the allocators are happy
326+
let kernel_range = align(kernel_range.start, buffer::PAGE_SIZE as u64)
327+
..align_down(kernel_range.end, buffer::PAGE_SIZE as u64);
328+
329+
let kernel_half_size = align_down(kernel_range.range() >> 1, buffer::PAGE_SIZE as u64);
323330
let kernel_gpu_range = kernel_range.start..(kernel_range.start + kernel_half_size);
324331
let kernel_gpufw_range = kernel_gpu_range.end..kernel_range.end;
325332

drivers/gpu/drm/asahi/util.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,26 @@ where
2525
(a + b - one) & !(b - one)
2626
}
2727

28+
/// Aligns an integer type down to a power of two.
29+
pub(crate) fn align_down<T>(a: T, b: T) -> T
30+
where
31+
T: Copy
32+
+ Default
33+
+ BitAnd<Output = T>
34+
+ Not<Output = T>
35+
+ Sub<Output = T>
36+
+ Div<Output = T>
37+
+ core::cmp::PartialEq,
38+
{
39+
let def: T = Default::default();
40+
#[allow(clippy::eq_op)]
41+
let one: T = !def / !def;
42+
43+
assert!((b & (b - one)) == def);
44+
45+
a & !(b - one)
46+
}
47+
2848
/// Integer division rounding up.
2949
pub(crate) fn div_ceil<T>(a: T, b: T) -> T
3050
where

0 commit comments

Comments
 (0)