Skip to content

Commit 1c54660

Browse files
committed
drm/asahi: Convert more ranges to Range<>
Signed-off-by: Asahi Lina <[email protected]>
1 parent e735054 commit 1c54660

5 files changed

Lines changed: 37 additions & 72 deletions

File tree

drivers/gpu/drm/asahi/alloc.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ use crate::driver::{AsahiDevRef, AsahiDevice};
1919
use crate::fw::types::Zeroable;
2020
use crate::mmu;
2121
use crate::object::{GpuArray, GpuObject, GpuOnlyArray, GpuStruct, GpuWeakPointer};
22+
use crate::util::RangeExt;
2223

2324
use core::cmp::Ordering;
2425
use core::fmt;
2526
use core::fmt::{Debug, Formatter};
2627
use core::marker::PhantomData;
2728
use core::mem;
2829
use core::mem::MaybeUninit;
30+
use core::ops::Range;
2931
use core::ptr::NonNull;
3032

3133
const DEBUG_CLASS: DebugFlags = DebugFlags::Alloc;
@@ -491,8 +493,7 @@ impl RawAllocation for SimpleAllocation {
491493
/// GPU's idea of object size what we expect.
492494
pub(crate) struct SimpleAllocator {
493495
dev: AsahiDevRef,
494-
start: u64,
495-
end: u64,
496+
range: Range<u64>,
496497
prot: u32,
497498
vm: mmu::Vm,
498499
min_align: usize,
@@ -506,8 +507,7 @@ impl SimpleAllocator {
506507
pub(crate) fn new(
507508
dev: &AsahiDevice,
508509
vm: &mmu::Vm,
509-
start: u64,
510-
end: u64,
510+
range: Range<u64>,
511511
min_align: usize,
512512
prot: u32,
513513
_block_size: usize,
@@ -521,8 +521,7 @@ impl SimpleAllocator {
521521
Ok(SimpleAllocator {
522522
dev: dev.into(),
523523
vm: vm.clone(),
524-
start,
525-
end,
524+
range,
526525
prot,
527526
min_align,
528527
cpu_maps,
@@ -567,8 +566,7 @@ impl Allocator for SimpleAllocator {
567566
}
568567
let mapping = obj.map_into_range(
569568
&self.vm,
570-
self.start,
571-
self.end,
569+
self.range.clone(),
572570
self.min_align.max(mmu::UAT_PGSZ) as u64,
573571
self.prot,
574572
true,
@@ -709,8 +707,7 @@ struct HeapAllocatorInner {
709707
/// never shrinks it.
710708
pub(crate) struct HeapAllocator {
711709
dev: AsahiDevRef,
712-
start: u64,
713-
end: u64,
710+
range: Range<u64>,
714711
top: u64,
715712
prot: u32,
716713
vm: mmu::Vm,
@@ -729,8 +726,7 @@ impl HeapAllocator {
729726
pub(crate) fn new(
730727
dev: &AsahiDevice,
731728
vm: &mmu::Vm,
732-
start: u64,
733-
end: u64,
729+
range: Range<u64>,
734730
min_align: usize,
735731
prot: u32,
736732
block_size: usize,
@@ -757,14 +753,13 @@ impl HeapAllocator {
757753
total_garbage: 0,
758754
};
759755

760-
let mm = mm::Allocator::new(start, end - start + 1, inner)?;
756+
let mm = mm::Allocator::new(range.start, range.range(), inner)?;
761757

762758
Ok(HeapAllocator {
763759
dev: dev.into(),
764760
vm: vm.clone(),
765-
start,
766-
end,
767-
top: start,
761+
top: range.start,
762+
range,
768763
prot,
769764
min_align,
770765
block_size: block_size.max(min_align),
@@ -792,7 +787,7 @@ impl HeapAllocator {
792787
size_aligned,
793788
);
794789

795-
if self.top.saturating_add(size_aligned as u64) >= self.end {
790+
if self.top.saturating_add(size_aligned as u64) > self.range.end {
796791
dev_err!(
797792
&self.dev,
798793
"HeapAllocator[{}]::add_block: Exhausted VA space\n",
@@ -875,7 +870,7 @@ impl HeapAllocator {
875870
&self.dev,
876871
"{} Heap: grow to {} bytes\n",
877872
&*self.name,
878-
self.top - self.start
873+
self.top - self.range.start
879874
);
880875

881876
Ok(())

drivers/gpu/drm/asahi/file.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,7 @@ impl File {
316316
let ualloc = Arc::pin_init(Mutex::new(alloc::DefaultAllocator::new(
317317
device,
318318
&vm,
319-
VM_DRV_GPU_START,
320-
VM_DRV_GPU_END,
319+
VM_DRV_GPU_START..VM_DRV_GPU_END,
321320
buffer::PAGE_SIZE,
322321
mmu::PROT_GPU_SHARED_RW,
323322
512 * 1024,
@@ -328,8 +327,7 @@ impl File {
328327
let ualloc_priv = Arc::pin_init(Mutex::new(alloc::DefaultAllocator::new(
329328
device,
330329
&vm,
331-
VM_DRV_GPUFW_START,
332-
VM_DRV_GPUFW_END,
330+
VM_DRV_GPUFW_START..VM_DRV_GPUFW_END,
333331
buffer::PAGE_SIZE,
334332
mmu::PROT_GPU_FW_PRIV_RW,
335333
64 * 1024,

drivers/gpu/drm/asahi/gem.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use kernel::{
1616

1717
use kernel::drm::gem::BaseObject;
1818

19+
use core::ops::Range;
1920
use core::sync::atomic::{AtomicU64, Ordering};
2021

2122
use crate::{debug::*, driver::AsahiDevice, file, file::DrmFile, mmu, util::*};
@@ -76,8 +77,7 @@ impl ObjectRef {
7677
pub(crate) fn map_into_range(
7778
&mut self,
7879
vm: &crate::mmu::Vm,
79-
start: u64,
80-
end: u64,
80+
range: Range<u64>,
8181
alignment: u64,
8282
prot: u32,
8383
guard: bool,
@@ -88,15 +88,7 @@ impl ObjectRef {
8888
return Err(EINVAL);
8989
}
9090

91-
vm.map_in_range(
92-
self.gem.size(),
93-
&self.gem,
94-
alignment,
95-
start,
96-
end,
97-
prot,
98-
guard,
99-
)
91+
vm.map_in_range(self.gem.size(), &self.gem, alignment, range, prot, guard)
10092
}
10193

10294
/// Maps an object into a given `Vm` at a specific address.

drivers/gpu/drm/asahi/gpu.rs

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//! itself with version dependence.
1313
1414
use core::any::Any;
15+
use core::ops::Range;
1516
use core::sync::atomic::{AtomicBool, AtomicU64, Ordering};
1617
use core::time::Duration;
1718

@@ -69,33 +70,19 @@ const DOORBELL_DEVCTRL: u64 = 0x11;
6970

7071
// Upper kernel half VA address ranges.
7172
/// Private (cached) firmware structure VA range base.
72-
const IOVA_KERN_PRIV_BASE: u64 = 0xffffffa000000000;
73-
/// Private (cached) firmware structure VA range top.
74-
const IOVA_KERN_PRIV_TOP: u64 = 0xffffffa5ffffffff;
73+
const IOVA_KERN_PRIV_RANGE: Range<u64> = 0xffffffa000000000..0xffffffa600000000;
7574
/// Private (cached) GPU-RO firmware structure VA range base.
76-
const IOVA_KERN_GPU_RO_BASE: u64 = 0xffffffa600000000;
77-
/// Private (cached) GPU-RO firmware structure VA range top.
78-
const IOVA_KERN_GPU_RO_TOP: u64 = 0xffffffa7ffffffff;
75+
const IOVA_KERN_GPU_RO_RANGE: Range<u64> = 0xffffffa600000000..0xffffffa800000000;
7976
/// Shared (uncached) firmware structure VA range base.
80-
const IOVA_KERN_SHARED_BASE: u64 = 0xffffffa800000000;
81-
/// Shared (uncached) firmware structure VA range top.
82-
const IOVA_KERN_SHARED_TOP: u64 = 0xffffffa9ffffffff;
77+
const IOVA_KERN_SHARED_RANGE: Range<u64> = 0xffffffa800000000..0xffffffaa00000000;
8378
/// Shared (uncached) read-only firmware structure VA range base.
84-
const IOVA_KERN_SHARED_RO_BASE: u64 = 0xffffffaa00000000;
85-
/// Shared (uncached) read-only firmware structure VA range top.
86-
const IOVA_KERN_SHARED_RO_TOP: u64 = 0xffffffabffffffff;
79+
const IOVA_KERN_SHARED_RO_RANGE: Range<u64> = 0xffffffaa00000000..0xffffffac00000000;
8780
/// GPU/FW shared structure VA range base.
88-
const IOVA_KERN_GPU_BASE: u64 = 0xffffffac00000000;
89-
/// GPU/FW shared structure VA range top.
90-
const IOVA_KERN_GPU_TOP: u64 = 0xffffffadffffffff;
81+
const IOVA_KERN_GPU_RANGE: Range<u64> = 0xffffffac00000000..0xffffffae00000000;
9182
/// GPU/FW shared structure VA range base.
92-
const IOVA_KERN_RTKIT_BASE: u64 = 0xffffffae00000000;
93-
/// GPU/FW shared structure VA range top.
94-
const IOVA_KERN_RTKIT_TOP: u64 = 0xffffffae0fffffff;
83+
const IOVA_KERN_RTKIT_RANGE: Range<u64> = 0xffffffae00000000..0xffffffae10000000;
9584
/// FW MMIO VA range base.
96-
const IOVA_KERN_MMIO_BASE: u64 = 0xffffffaf00000000;
97-
/// FW MMIO VA range top.
98-
const IOVA_KERN_MMIO_TOP: u64 = 0xffffffafffffffff;
85+
const IOVA_KERN_MMIO_RANGE: Range<u64> = 0xffffffaf00000000..0xffffffb000000000;
9986

10087
/// GPU/FW buffer manager control address (context 0 low)
10188
pub(crate) const IOVA_KERN_GPU_BUFMGR_LOW: u64 = 0x20_0000_0000;
@@ -355,8 +342,7 @@ impl rtkit::Operations for GpuManager::ver {
355342
obj.vmap()?;
356343
let mapping = obj.map_into_range(
357344
data.uat.kernel_vm(),
358-
IOVA_KERN_RTKIT_BASE,
359-
IOVA_KERN_RTKIT_TOP,
345+
IOVA_KERN_RTKIT_RANGE,
360346
mmu::UAT_PGSZ as u64,
361347
mmu::PROT_FW_SHARED_RW,
362348
true,
@@ -382,8 +368,7 @@ impl GpuManager::ver {
382368
private: alloc::DefaultAllocator::new(
383369
dev,
384370
uat.kernel_vm(),
385-
IOVA_KERN_PRIV_BASE,
386-
IOVA_KERN_PRIV_TOP,
371+
IOVA_KERN_PRIV_RANGE,
387372
0x80,
388373
mmu::PROT_FW_PRIV_RW,
389374
1024 * 1024,
@@ -394,8 +379,7 @@ impl GpuManager::ver {
394379
shared: alloc::DefaultAllocator::new(
395380
dev,
396381
uat.kernel_vm(),
397-
IOVA_KERN_SHARED_BASE,
398-
IOVA_KERN_SHARED_TOP,
382+
IOVA_KERN_SHARED_RANGE,
399383
0x80,
400384
mmu::PROT_FW_SHARED_RW,
401385
1024 * 1024,
@@ -406,8 +390,7 @@ impl GpuManager::ver {
406390
shared_ro: alloc::DefaultAllocator::new(
407391
dev,
408392
uat.kernel_vm(),
409-
IOVA_KERN_SHARED_RO_BASE,
410-
IOVA_KERN_SHARED_RO_TOP,
393+
IOVA_KERN_SHARED_RO_RANGE,
411394
0x80,
412395
mmu::PROT_FW_SHARED_RO,
413396
64 * 1024,
@@ -418,8 +401,7 @@ impl GpuManager::ver {
418401
gpu: alloc::DefaultAllocator::new(
419402
dev,
420403
uat.kernel_vm(),
421-
IOVA_KERN_GPU_BASE,
422-
IOVA_KERN_GPU_TOP,
404+
IOVA_KERN_GPU_RANGE,
423405
0x80,
424406
mmu::PROT_GPU_FW_SHARED_RW,
425407
64 * 1024,
@@ -430,8 +412,7 @@ impl GpuManager::ver {
430412
gpu_ro: alloc::DefaultAllocator::new(
431413
dev,
432414
uat.kernel_vm(),
433-
IOVA_KERN_GPU_RO_BASE,
434-
IOVA_KERN_GPU_RO_TOP,
415+
IOVA_KERN_GPU_RO_RANGE,
435416
0x80,
436417
mmu::PROT_GPU_RO_FW_PRIV_RW,
437418
1024 * 1024,
@@ -590,7 +571,7 @@ impl GpuManager::ver {
590571
let addr = *next_ref;
591572
let next = addr + (size + mmu::UAT_PGSZ) as u64;
592573

593-
assert!(next - 1 <= IOVA_KERN_MMIO_TOP);
574+
assert!(next <= IOVA_KERN_MMIO_RANGE.end);
594575

595576
*next_ref = next;
596577

@@ -687,7 +668,7 @@ impl GpuManager::ver {
687668
initdata: *initdata,
688669
uat: *uat,
689670
io_mappings: Vec::new(),
690-
next_mmio_iova: IOVA_KERN_MMIO_BASE,
671+
next_mmio_iova: IOVA_KERN_MMIO_RANGE.start,
691672
rtkit <- Mutex::new_named(None, c_str!("rtkit")),
692673
crashed: AtomicBool::new(false),
693674
event_manager,

drivers/gpu/drm/asahi/mmu.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,8 +1069,7 @@ impl Vm {
10691069
size: usize,
10701070
gem: &gem::Object,
10711071
alignment: u64,
1072-
start: u64,
1073-
end: u64,
1072+
range: Range<u64>,
10741073
prot: u32,
10751074
guard: bool,
10761075
) -> Result<KernelMapping> {
@@ -1096,8 +1095,8 @@ impl Vm {
10961095
(size + if guard { UAT_PGSZ } else { 0 }) as u64, // Add guard page
10971096
alignment,
10981097
0,
1099-
start,
1100-
end,
1098+
range.start,
1099+
range.end,
11011100
mm::InsertMode::Best,
11021101
)?;
11031102

0 commit comments

Comments
 (0)