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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "audio_core/renderer/memory/address_info.h"
#include "audio_core/renderer/memory/pool_mapper.h"
#include "core/hle/kernel/k_process.h"
#include "core/hle/kernel/svc.h"
namespace AudioCore::Renderer {
PoolMapper::PoolMapper(Kernel::KProcess* process_handle_, bool force_map_)
: process_handle{process_handle_}, force_map{force_map_} {}
PoolMapper::PoolMapper(Kernel::KProcess* process_handle_, std::span<MemoryPoolInfo> pool_infos_,
u32 pool_count_, bool force_map_)
: process_handle{process_handle_}, pool_infos{pool_infos_.data()},
pool_count{pool_count_}, force_map{force_map_} {}
void PoolMapper::ClearUseState(std::span<MemoryPoolInfo> pools, const u32 count) {
for (u32 i = 0; i < count; i++) {
pools[i].SetUsed(false);
}
}
MemoryPoolInfo* PoolMapper::FindMemoryPool(MemoryPoolInfo* pools, const u64 count,
const CpuAddr address, const u64 size) const {
auto pool{pools};
for (u64 i = 0; i < count; i++, pool++) {
if (pool->Contains(address, size)) {
return pool;
}
}
return nullptr;
}
MemoryPoolInfo* PoolMapper::FindMemoryPool(const CpuAddr address, const u64 size) const {
auto pool{pool_infos};
for (u64 i = 0; i < pool_count; i++, pool++) {
if (pool->Contains(address, size)) {
return pool;
}
}
return nullptr;
}
bool PoolMapper::FillDspAddr(AddressInfo& address_info, MemoryPoolInfo* pools,
const u32 count) const {
if (address_info.GetCpuAddr() == 0) {
address_info.SetPool(nullptr);
return false;
}
auto found_pool{
FindMemoryPool(pools, count, address_info.GetCpuAddr(), address_info.GetSize())};
if (found_pool != nullptr) {
address_info.SetPool(found_pool);
return true;
}
if (force_map) {
address_info.SetForceMappedDspAddr(address_info.GetCpuAddr());
} else {
address_info.SetPool(nullptr);
}
return false;
}
bool PoolMapper::FillDspAddr(AddressInfo& address_info) const {
if (address_info.GetCpuAddr() == 0) {
address_info.SetPool(nullptr);
return false;
}
auto found_pool{FindMemoryPool(address_info.GetCpuAddr(), address_info.GetSize())};
if (found_pool != nullptr) {
address_info.SetPool(found_pool);
return true;
}
if (force_map) {
address_info.SetForceMappedDspAddr(address_info.GetCpuAddr());
} else {
address_info.SetPool(nullptr);
}
return false;
}
bool PoolMapper::TryAttachBuffer(BehaviorInfo::ErrorInfo& error_info, AddressInfo& address_info,
const CpuAddr address, const u64 size) const {
address_info.Setup(address, size);
if (!FillDspAddr(address_info)) {
error_info.error_code = Service::Audio::ResultInvalidAddressInfo;
error_info.address = address;
return force_map;
}
error_info.error_code = ResultSuccess;
error_info.address = CpuAddr(0);
return true;
}
bool PoolMapper::IsForceMapEnabled() const {
return force_map;
}
Kernel::KProcess* PoolMapper::GetProcessHandle(const MemoryPoolInfo* pool) const {
switch (pool->GetLocation()) {
case MemoryPoolInfo::Location::CPU:
return process_handle;
case MemoryPoolInfo::Location::DSP:
// return Kernel::Svc::CurrentProcess;
return nullptr;
}
LOG_WARNING(Service_Audio, "Invalid MemoryPoolInfo location!");
// return Kernel::Svc::CurrentProcess;
return nullptr;
}
bool PoolMapper::Map([[maybe_unused]] const u32 handle, [[maybe_unused]] const CpuAddr cpu_addr,
[[maybe_unused]] const u64 size) const {
// nn::audio::dsp::MapUserPointer(handle, cpu_addr, size);
return true;
}
bool PoolMapper::Map(MemoryPoolInfo& pool) const {
switch (pool.GetLocation()) {
case MemoryPoolInfo::Location::CPU:
// Map with process_handle
pool.SetDspAddress(pool.GetCpuAddress());
return true;
case MemoryPoolInfo::Location::DSP:
// Map with Kernel::Svc::CurrentProcess
pool.SetDspAddress(pool.GetCpuAddress());
return true;
default:
LOG_WARNING(Service_Audio, "Invalid MemoryPoolInfo location={}!",
static_cast<u32>(pool.GetLocation()));
return false;
}
}
bool PoolMapper::Unmap([[maybe_unused]] const u32 handle, [[maybe_unused]] const CpuAddr cpu_addr,
[[maybe_unused]] const u64 size) const {
// nn::audio::dsp::UnmapUserPointer(handle, cpu_addr, size);
return true;
}
bool PoolMapper::Unmap(MemoryPoolInfo& pool) const {
[[maybe_unused]] Kernel::KProcess* handle{};
switch (pool.GetLocation()) {
case MemoryPoolInfo::Location::CPU:
handle = process_handle;
break;
case MemoryPoolInfo::Location::DSP:
// handle = Kernel::Svc::CurrentProcess;
break;
}
// nn::audio::dsp::UnmapUserPointer(handle, pool->cpu_address, pool->size);
pool.SetCpuAddress(0, 0);
pool.SetDspAddress(0);
return true;
}
void PoolMapper::ForceUnmapPointer(const AddressInfo& address_info) const {
if (force_map) {
[[maybe_unused]] auto found_pool{
FindMemoryPool(address_info.GetCpuAddr(), address_info.GetSize())};
// nn::audio::dsp::UnmapUserPointer(this->processHandle, address_info.GetCpuAddr(), 0);
}
}
MemoryPoolInfo::ResultState PoolMapper::Update(MemoryPoolInfo& pool,
const MemoryPoolInfo::InParameter& in_params,
MemoryPoolInfo::OutStatus& out_params) const {
if (in_params.state != MemoryPoolInfo::State::RequestAttach &&
in_params.state != MemoryPoolInfo::State::RequestDetach) {
return MemoryPoolInfo::ResultState::Success;
}
if (in_params.address == 0 || in_params.size == 0 || !Common::Is4KBAligned(in_params.address) ||
!Common::Is4KBAligned(in_params.size)) {
return MemoryPoolInfo::ResultState::BadParam;
}
switch (in_params.state) {
case MemoryPoolInfo::State::RequestAttach:
pool.SetCpuAddress(in_params.address, in_params.size);
Map(pool);
if (pool.IsMapped()) {
out_params.state = MemoryPoolInfo::State::Attached;
return MemoryPoolInfo::ResultState::Success;
}
pool.SetCpuAddress(0, 0);
return MemoryPoolInfo::ResultState::MapFailed;
case MemoryPoolInfo::State::RequestDetach:
if (pool.GetCpuAddress() != in_params.address || pool.GetSize() != in_params.size) {
return MemoryPoolInfo::ResultState::BadParam;
}
if (pool.IsUsed()) {
return MemoryPoolInfo::ResultState::InUse;
}
Unmap(pool);
pool.SetCpuAddress(0, 0);
pool.SetDspAddress(0);
out_params.state = MemoryPoolInfo::State::Detached;
return MemoryPoolInfo::ResultState::Success;
default:
LOG_ERROR(Service_Audio, "Invalid MemoryPoolInfo::State!");
break;
}
return MemoryPoolInfo::ResultState::Success;
}
bool PoolMapper::InitializeSystemPool(MemoryPoolInfo& pool, const u8* memory,
const u64 size_) const {
switch (pool.GetLocation()) {
case MemoryPoolInfo::Location::CPU:
return false;
case MemoryPoolInfo::Location::DSP:
pool.SetCpuAddress(reinterpret_cast<u64>(memory), size_);
if (Map(Kernel::Svc::CurrentProcess, reinterpret_cast<u64>(memory), size_)) {
pool.SetDspAddress(pool.GetCpuAddress());
return true;
}
return false;
default:
LOG_WARNING(Service_Audio, "Invalid MemoryPoolInfo location={}!",
static_cast<u32>(pool.GetLocation()));
return false;
}
}
} // namespace AudioCore::Renderer
|