Repository navigation
Expand file tree
/
Copy pathNativeAdrenoGmuMemory.cpp
More file actions
92 lines (92 loc) · 4.19 KB
/
Copy pathNativeAdrenoGmuMemory.cpp
File metadata and controls
92 lines (92 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* SPDX-License-Identifier: MIT */
#include "NativeAdrenoGmuMemory.h"
#include <IOKit/IOLib.h>
enum : size_t { PageBytes=0x1000 };
NativeAdrenoGmuSizes NativeAdrenoGmuDefaultSizes(size_t table_bytes)
{
return {0xffc000,0x4000,0x7000,0x4000,0x4000,table_bytes};
}
IOReturn NativeAdrenoGmuAllocator::allocate(const NativeAdrenoGmuSizes &sizes,unsigned address_bits)
{
if(ready_ || icache_.bytes())return kIOReturnBusy;
const uint64_t requested[]={sizes.icache,sizes.dummy,sizes.debug,sizes.log,
sizes.hfi,sizes.tables};
for(uint64_t value:requested)
if(!value || (value&(PageBytes-1)) || value>uint64_t(SIZE_MAX))
return kIOReturnBadArgument;
/* The fixed addresses of a6xx_gmu.c at the pinned tree: icache at 0x4000 below the uncached range, dummy and
* debug at 0x60000000/0x60400000. Log and HFI have no fixed address there; drm_mm gives them the lowest free
* pages after the dummy, which is where they are put here. */
if(sizes.icache>GmuUncachedBase-GmuIcacheIova ||
sizes.dummy+sizes.log+sizes.hfi>GmuDebugIova-GmuUncachedBase ||
sizes.debug>GmuApertureBytes-GmuDebugIova)return kIOReturnBadArgument;
struct Selection {NativeAdrenoMemory *memory;uint64_t bytes;uint64_t *iova;uint64_t at;};
const Selection selections[]={
{&icache_,sizes.icache,&layout_.icache_iova,GmuIcacheIova},
{&dummy_,sizes.dummy,&layout_.dummy_iova,GmuUncachedBase},
{&debug_,sizes.debug,&layout_.debug_iova,GmuDebugIova},
{&log_,sizes.log,&layout_.log_iova,GmuUncachedBase+sizes.dummy},
{&hfi_,sizes.hfi,&layout_.hfi_iova,GmuUncachedBase+sizes.dummy+sizes.log},
{&tables_,sizes.tables,&layout_.tables_iova,0}};
for(const Selection &selection:selections) {
const IOReturn status=selection.memory->allocate(static_cast<size_t>(selection.bytes),
address_bits,true,static_cast<size_t>(selection.bytes));
if(status) {
(void)releaseAll();
return status;
}
*selection.iova=selection.at;
}
layout_.tables_bytes=static_cast<size_t>(sizes.tables);
layout_.end_iova=GmuDebugIova+sizes.debug;
ready_=true;
return kIOReturnSuccess;
}
IOReturn NativeAdrenoGmuAllocator::mapInto(NativeAdrenoVm *vm)
{
if(!ready_ || !vm)return kIOReturnBadArgument;
for(uint64_t handle:handles_)if(handle)return kIOReturnBusy;
/* The table pool itself is never mapped: its physical pages are the
* translation storage and the VM refuses such an overlap. */
NativeAdrenoMemory *objects[]={&icache_,&dummy_,&debug_,&log_,&hfi_};
const uint64_t iovas[]={layout_.icache_iova,layout_.dummy_iova,layout_.debug_iova,
layout_.log_iova,layout_.hfi_iova};
for(size_t index=0;index<5;++index) {
/* a6xx_gmu.c maps the icache MAP_PRIV and every buffer without NOEXEC; here only the icache, which
* the GMU executes from, is executable, and the data buffers stay execute-never. */
const unsigned permissions=index?3u:15u;
const IOReturn status=vm->map(objects[index],0,iovas[index],permissions,0,2,&handles_[index]);
if(status) {
for(size_t previous=0;previous<index;++previous)
if(handles_[previous]) {vm->unmap(handles_[previous]);handles_[previous]=0;}
return status;
}
}
return kIOReturnSuccess;
}
IOReturn NativeAdrenoGmuAllocator::unmapFrom(NativeAdrenoVm *vm)
{
if(!vm)return kIOReturnBadArgument;
IOReturn status=kIOReturnSuccess;
for(size_t index=5;index--;)if(handles_[index]) {
const IOReturn unmapped=vm->unmap(handles_[index]);
if(unmapped)status=unmapped;else handles_[index]=0;
}
return status;
}
IOReturn NativeAdrenoGmuAllocator::releaseAll()
{
NativeAdrenoMemory *objects[]={&icache_,&dummy_,&debug_,&log_,&hfi_,&tables_};
IOReturn status=kIOReturnSuccess;
for(NativeAdrenoMemory *object:objects)
if(object->bytes() && object->release()!=kIOReturnSuccess)status=kIOReturnIOError;
return status;
}
IOReturn NativeAdrenoGmuAllocator::release()
{
if(!ready_)return kIOReturnNotReady;
const IOReturn status=releaseAll();
if(status)return status;
ready_=false;layout_={};
return kIOReturnSuccess;
}