aboutsummaryrefslogtreecommitdiff
path: root/src/tests/core/native_gpu_characteristics.cpp
blob: cb79853ac27e87e4cecc3111385066831d69b5ac (plain) (blame)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later

#include <algorithm>
#include <array>
#include <cstring>
#include <memory>
#include "video_core/macro.h"
#include "video_core/native_qmd.h"
#include "video_core/dirty_flags.h"
#include <catch2/catch_test_macros.hpp>
#include "core/hle/kernel/k_memory_block.h"
#include "core/hle/service/nvdrv/devices/native_gpu_kind.h"
#include "core/hle/service/nvdrv/devices/native_gpu_context.h"
#include "core/hle/service/nvdrv/devices/native_gpu_characteristics.h"
#include "core/hle/service/nvdrv/devices/gpu_address_space_layout.h"

TEST_CASE("Native GPU characteristics replace topology with ioctl interface levels",
          "[native-gpu]") {
    std::array<u8, 0xa0> legacy;
    legacy.fill(0xcc);
    const u32_le arch{0x120};
    const u32_le impl{0xb};
    std::memcpy(legacy.data(), &arch, sizeof(arch));
    std::memcpy(legacy.data() + 4, &impl, sizeof(impl));
    const u32_le available{0x30000};
    std::memcpy(legacy.data() + 0x34, &available, sizeof(available));
    const auto native = Service::Nvidia::Devices::MakeNativeGpuCharacteristics(legacy);
    REQUIRE(native.size() == 0xd0);
    u32_le native_arch, native_impl;
    std::memcpy(&native_arch, native.data(), sizeof(native_arch));
    std::memcpy(&native_impl, native.data() + 4, sizeof(native_impl));
    REQUIRE(static_cast<u32>(native_arch) == 0x170);
    REQUIRE(static_cast<u32>(native_impl) == 0xe);
    REQUIRE(std::equal(legacy.begin() + 8, legacy.begin() + 0x28, native.begin() + 8));
    REQUIRE(std::equal(legacy.begin() + 0x2c, legacy.begin() + 0x34, native.begin() + 0x2c));
    REQUIRE(std::equal(legacy.begin() + 0x38, legacy.begin() + 0x3c, native.begin() + 0x38));
    REQUIRE(std::equal(legacy.begin() + 0x44, legacy.begin() + 0x50, native.begin() + 0x44));
    REQUIRE(std::equal(legacy.begin() + 0x58, legacy.begin() + 0x70, native.begin() + 0x58));
    u32_le sm_version, spa_version;
    std::memcpy(&sm_version, native.data() + 0x3c, sizeof(sm_version));
    std::memcpy(&spa_version, native.data() + 0x40, sizeof(spa_version));
    REQUIRE(static_cast<u32>(sm_version) == 0x807);
    REQUIRE(static_cast<u32>(spa_version) == 0x807);
    u32_le available_sizes;
    std::memcpy(&available_sizes, native.data() + 0x34, sizeof(available_sizes));
    REQUIRE(static_cast<u32>(available_sizes) == 0x11000);
    u32_le big_page_size;
    std::memcpy(&big_page_size, native.data() + 0x28, sizeof(big_page_size));
    REQUIRE(static_cast<u32>(big_page_size) == 0x1000);
    const auto level = [&](size_t offset) {
        u16_le value;
        std::memcpy(&value, native.data() + offset, sizeof(value));
        return static_cast<s16>(static_cast<u16>(value));
    };
    REQUIRE(level(0x70) == 0x1c);
    REQUIRE(level(0x72) == 2);
    u64_le flags;
    std::memcpy(&flags, native.data() + 0x50, sizeof(flags));
    REQUIRE((static_cast<u64>(flags) & (1ULL << 8)) != 0);
    REQUIRE((static_cast<u64>(flags) & ~((1ULL << 8) | (1ULL << 26) | (1ULL << 34) | (0xfULL << 40))) == (0xccccccccccccccccULL & ~((1ULL << 8) | (1ULL << 26) | (1ULL << 34) | (0xfULL << 40))));
    REQUIRE((static_cast<u64>(flags) & (0xfULL << 40)) == (0xfULL << 40));
    REQUIRE((static_cast<u64>(flags) & (1ULL << 34)) != 0);
    REQUIRE(level(0x74) == -1);
    REQUIRE(level(0x76) == 0x1d);
    // The captured SDK rejects the old topology value 2 here, requiring >= 6.
    REQUIRE(level(0x78) == 0x14);
    REQUIRE(std::all_of(native.begin() + 0x7a, native.end(), [](u8 b) { return b == 0; }));
}

TEST_CASE("Native GPU VA layout accommodates the captured NVN bootstrap reservation",
          "[native-gpu]") {
    using Service::Nvidia::Devices::GetGpuAddressSpaceLayout;
    constexpr auto native = GetGpuAddressSpaceLayout(true);
    constexpr auto legacy = GetGpuAddressSpaceLayout(false);
    REQUIRE(native.split - native.start >= 0x7ff8000000ULL + (1ULL << 32));
    REQUIRE(native.start == legacy.start);
    REQUIRE(native.end == 1ULL << 40);
    REQUIRE(native.end > native.split);
    REQUIRE(legacy.split == 1ULL << 34);
    REQUIRE(legacy.end == 1ULL << 37);
}

TEST_CASE("GPU region addresses retain bits above the 32-bit page index", "[native-gpu]") {
    using Service::Nvidia::Devices::GpuPageAddress;
    REQUIRE(GpuPageAddress(0x400000, 17) == 0x8000000000ULL);
    REQUIRE(GpuPageAddress(0x20000, 17) == 0x400000000ULL);
    REQUIRE(GpuPageAddress(0x8000, 12) == 0x8000000ULL);
}

TEST_CASE("GPU memory eligibility survives splitting and remains separate from cache attributes",
          "[native-gpu]") {
    using namespace Kernel;
    KMemoryBlock right{KProcessAddress{0x10000}, 4, KMemoryState::Normal,
                       KMemoryPermission::UserReadWrite, KMemoryAttribute::Uncached};
    right.UpdateAttribute(KMemoryAttribute::GpuAccessible, KMemoryAttribute::GpuAccessible);
    KMemoryBlock left;
    right.Split(&left, KProcessAddress{0x12000});
    REQUIRE(left.GetNumPages() == 2);
    REQUIRE(right.GetNumPages() == 2);
    REQUIRE(left.GetMemoryInfo().GetSvcMemoryInfo().attribute ==
            (Svc::MemoryAttribute::Uncached | Svc::MemoryAttribute::GpuAccessible));
    REQUIRE(right.GetAttribute() == left.GetAttribute());
    right.UpdateAttribute(KMemoryAttribute::GpuAccessible, KMemoryAttribute::None);
    REQUIRE(right.GetAttribute() == KMemoryAttribute::Uncached);
    REQUIRE(left.GetAttribute() ==
            (KMemoryAttribute::Uncached | KMemoryAttribute::GpuAccessible));
    REQUIRE(right.GetPermission() == KMemoryPermission::UserReadWrite);
    REQUIRE(right.GetState() == KMemoryState::Normal);
}

TEST_CASE("Native 4 KiB allocation preference retains a separate large-page GPU pool", "[native-gpu]") {
    using Service::Nvidia::Devices::GetGpuAddressSpaceBigPageSize;
    REQUIRE(GetGpuAddressSpaceBigPageSize(true, 0x1000) == 0x10000);
    REQUIRE(GetGpuAddressSpaceBigPageSize(true, 0x20000) == 0x20000);
    REQUIRE(GetGpuAddressSpaceBigPageSize(false, 0x1000) == 0x1000);
    REQUIRE(GetGpuAddressSpaceBigPageSize(false, 0) == 0);
}

TEST_CASE("Native generic memory kind is translated rather than treated as legacy depth memory",
          "[native-gpu]") {
    using Service::Nvidia::Devices::TranslateNativeUncompressedKind;
    REQUIRE(TranslateNativeUncompressedKind(0, 0) == static_cast<u8>(Tegra::PTEKind::PITCH));
    REQUIRE(TranslateNativeUncompressedKind(6, 6) == static_cast<u8>(Tegra::PTEKind::GENERIC_16BX2));
    REQUIRE(TranslateNativeUncompressedKind(6, 6) != static_cast<u8>(Tegra::PTEKind::Z16_MS16_2C));
    REQUIRE_FALSE(TranslateNativeUncompressedKind(8, 6));
    REQUIRE_FALSE(TranslateNativeUncompressedKind(7, 7));
    REQUIRE_FALSE(TranslateNativeUncompressedKind(0xfe, 0xfe));
}

TEST_CASE("Native mappings report the actual selected memory kind", "[native-gpu]") {
    using Service::Nvidia::Devices::SelectNativeMemoryKind;
    const auto normal = SelectNativeMemoryKind(6, 6);
    REQUIRE(normal.has_value());
    REQUIRE(normal->selected == 6);
    REQUIRE(normal->backend == 0xfe);
    const auto fallback = SelectNativeMemoryKind(8, 6);
    REQUIRE(fallback.has_value());
    REQUIRE(fallback->selected == 6);
    REQUIRE(fallback->backend == normal->backend);
    REQUIRE(SelectNativeMemoryKind(0, 0)->selected == 0);
    const auto depth = SelectNativeMemoryKind(0xc, 3);
    REQUIRE(depth.has_value());
    REQUIRE(depth->selected == 3);
    REQUIRE(depth->backend == static_cast<u8>(Tegra::PTEKind::S8Z24));
    REQUIRE(SelectNativeMemoryKind(3, 3)->backend == depth->backend);
    const auto depth16 = SelectNativeMemoryKind(0xb, 1);
    REQUIRE(depth16.has_value());
    REQUIRE(depth16->selected == 1);
    REQUIRE(depth16->backend == static_cast<u8>(Tegra::PTEKind::Z16));
    REQUIRE(SelectNativeMemoryKind(1, 1)->backend == depth16->backend);
    REQUIRE_FALSE(SelectNativeMemoryKind(0xb, 6));
    REQUIRE_FALSE(SelectNativeMemoryKind(0xc, 6));
    REQUIRE_FALSE(SelectNativeMemoryKind(8, 8));
    REQUIRE_FALSE(SelectNativeMemoryKind(8, 0));
    REQUIRE_FALSE(SelectNativeMemoryKind(9, 6));
}

TEST_CASE("Native GPU context allocations classify explicit known engine identities", "[native-gpu]") {
    using Service::Nvidia::Devices::GetNativeGpuContextSlot;
    REQUIRE(GetNativeGpuContextSlot(0xc797) == 1U);
    REQUIRE(GetNativeGpuContextSlot(0xc7c0) == 2U);
    REQUIRE_FALSE(GetNativeGpuContextSlot(0xa143));
    REQUIRE(GetNativeGpuContextSlot(0xc7b5) == 4U);
    REQUIRE(GetNativeGpuContextSlot(0xc56f) == 5U);
    REQUIRE_FALSE(GetNativeGpuContextSlot(0));
    REQUIRE_FALSE(GetNativeGpuContextSlot(0xb197));
    REQUIRE_FALSE(GetNativeGpuContextSlot(0xffffc797));
    REQUIRE_FALSE(GetNativeGpuContextSlot(0xdeadbeef));
}

TEST_CASE("Native MME uploads use instruction addresses and bypass Maxwell code storage", "[native-gpu]") {
    Tegra::MacroEngine engine{true};
    engine.native_turing = true;
    engine.ClearCode(8);
    engine.AddCode(8, 0x11223344);
    engine.AddCode(8, 0x55667788);
    engine.AddCode(8, 0x99aabbcc);
    REQUIRE(engine.native_code[8] == 0);
    REQUIRE(engine.native_code[24] == 0x11223344);
    REQUIRE(engine.native_code[25] == 0x55667788);
    REQUIRE(engine.native_code[26] == 0x99aabbcc);
    REQUIRE(engine.uploaded_macro_code.empty());
    engine.ClearCode(0xffffffff);
    engine.AddCode(0xffffffff, 1);
    REQUIRE(engine.native_code[24] == 0x11223344);
    engine.native_turing = false;
    engine.AddCode(8, 7);
    REQUIRE(engine.uploaded_macro_code.at(8).front() == 7);
}

TEST_CASE("Native MME loops consume the exact parameter count, including zero iterations", "[native-gpu]") {
    Tegra::TuringMacroInterpreter interpreter;
    constexpr std::array<u32, 15> code{
        0x00000003U, 0x18c00000U, 0x31c00300U,
        0x00000003U, 0x18c00000U, 0xb00c5f00U,
        0x00740003U, 0x18c00740U, 0x318c0300U,
        0x00000003U, 0x18c00000U, 0x318c0301U,
        0x00000003U, 0x18c00000U, 0x318c0300U,
    };
    std::vector<u32> output;
    auto emit = [&](u32 method, u32 value) { REQUIRE(method == 0xd00); output.push_back(value); return true; };
    auto read = [](u32) { return 0U; };
    const std::array<u32, 1> zero{0};
    auto result = interpreter.Execute(code, 0, zero, read, emit);
    REQUIRE(result.success);
    REQUIRE(result.consumed == 1);
    REQUIRE(output.empty());
    const std::array<u32, 3> two{2, 99, 88};
    result = interpreter.Execute(code, 0, two, read, emit);
    REQUIRE(result.success);
    REQUIRE(result.consumed == 3);
    REQUIRE(output == std::vector<u32>{99, 88});
    result = interpreter.Execute(code, 0, std::span<const u32>{}, read, emit);
    REQUIRE_FALSE(result.success);
    REQUIRE(result.error == "parameter FIFO underflow");
    result = interpreter.Execute(code, 5, zero, read, emit);
    REQUIRE_FALSE(result.success);
    REQUIRE(result.error == "instruction outside uploaded RAM");
}

TEST_CASE("Native MME pairs ALU carry with two incrementing outputs", "[native-gpu]") {
    Tegra::TuringMacroInterpreter interpreter;
    constexpr std::array<u32, 6> code{
        0x20340003U, 0x18084740U, 0x3bc00301U,
        0x00000003U, 0x18c00000U, 0x318c0300U,
    };
    const std::array<u32, 2> params{0xffffffffU, 1};
    std::vector<std::pair<u32, u32>> writes;
    const auto result = interpreter.Execute(code, 0, params, [](u32) { return 0U; },
        [&](u32 method, u32 value) { writes.emplace_back(method, value); return true; });
    REQUIRE(result.success);
    REQUIRE(result.consumed == 2);
    REQUIRE(writes == std::vector<std::pair<u32, u32>>{{0xd00, 0}, {0xd01, 1}});
}

TEST_CASE("Native empty QMDs release only after their dependency signals", "[native-gpu]") {
    Tegra::NativeQmdScheduler scheduler;
    Tegra::NativeQmdScheduler::Descriptor descriptor{};
    descriptor[4] = 0x8040; // Captured QMD: two dependencies, no CTAs.
    descriptor[18] = 0x00010030;
    descriptor[24] = 0x100000;
    descriptor[25] = 0x40880021; // One-word release, sysmembar, upper address 0x21.
    descriptor[26] = 0;
    u32 releases{};
    const auto read = [&](u64, auto& out) { out = descriptor; return true; };
    const auto release = [&](u64 address, u32 value) {
        REQUIRE(address == 0x2100100000ULL);
        REQUIRE(value == 42);
        ++releases;
        return true;
    };
    REQUIRE(scheduler.Signal(0x2100200000ULL, 3, read, release).empty());
    REQUIRE(releases == 0);
    REQUIRE(scheduler.Signal(0x2100200000ULL, 7, read, release).empty());
    REQUIRE(releases == 0);
    descriptor[26] = 42;
    REQUIRE(scheduler.Signal(0x2100200000ULL, 7, read, release).empty());
    REQUIRE(releases == 1);
}

TEST_CASE("Native QMD scheduler rejects real shader work without reporting completion", "[native-gpu]") {
    Tegra::NativeQmdScheduler scheduler;
    Tegra::NativeQmdScheduler::Descriptor descriptor{};
    descriptor[4] = 1U << 14;
    descriptor[18] = 0x30;
    descriptor[12] = descriptor[13] = descriptor[14] = 1;
    u32 releases{};
    const auto result = scheduler.Signal(0x10000, 3,
        [&](u64, auto& out) { out = descriptor; return true; },
        [&](u64, u32) { ++releases; return true; });
    REQUIRE(result == "native shader dispatch required");
    REQUIRE(releases == 0);
}

TEST_CASE("Native dependent QMD signals survive until the child is uploaded", "[native-gpu]") {
    Tegra::NativeQmdScheduler scheduler;
    Tegra::NativeQmdScheduler::Descriptor parent{}, child{};
    parent[4] = 1U << 14;
    parent[18] = 0x30;
    parent[15] = 0x200;
    parent[16] = 9; // Enabled child decrement-dependence operation.
    child[4] = 2U << 14;
    child[18] = 0x30;
    child[24] = 0x30000;
    child[25] = 0x40880000;
    child[26] = 99;
    u32 releases{};
    const auto read = [&](u64 address, auto& out) { out = address == 0x10000 ? parent : child; return true; };
    const auto release = [&](u64 address, u32 value) {
        REQUIRE(address == 0x30000);
        REQUIRE(value == 99);
        ++releases;
        return true;
    };
    REQUIRE(scheduler.Signal(0x10000, 3, read, release).empty());
    REQUIRE(scheduler.Signal(0x10000, 7, read, release).empty());
    REQUIRE(releases == 0);
    REQUIRE(scheduler.Signal(0x20000, 3, read, release).empty());
    REQUIRE(releases == 0);
    REQUIRE(scheduler.Signal(0x20000, 7, read, release).empty());
    REQUIRE(releases == 1);
}

TEST_CASE("Native MME dual ALUs sample registers before either writeback", "[native-gpu]") {
    Tegra::TuringMacroInterpreter interpreter;
    constexpr std::array<u32, 12> code{
        0x00000003U, 0x18c00000U, 0x31c00300U,
        0x00000003U, 0x40c7c001U, 0xf1c00300U,
        0x00380483U, 0x18c00000U, 0xf190fb01U,
        0x00000003U, 0x18c00000U, 0x318c0300U,
    };
    const std::array<u32, 2> parameters{3, 0x1000000};
    std::vector<std::pair<u32, u32>> writes;
    const auto result = interpreter.Execute(code, 0, parameters, [](u32) { return 0U; },
        [&](u32 method, u32 value) { writes.emplace_back(method, value); return true; });
    REQUIRE(result.success);
    REQUIRE(result.consumed == 2);
    REQUIRE(writes == std::vector<std::pair<u32, u32>>{{0x120, 7}});
}

TEST_CASE("Native MME taken back edges suppress fallthrough termination", "[native-gpu]") {
    Tegra::TuringMacroInterpreter interpreter;
    constexpr std::array<u32, 12> code{
        0x00000003U, 0x18c00000U, 0x31c00300U,
        0x00383403U, 0x18c00000U, 0x72000b00U,
        0x00000003U, 0x18c017ffU, 0xf00c7701U,
        0x00000003U, 0x18c00000U, 0x318c0300U,
    };
    for (const u32 count : {1U, 3U, 16U}) {
        std::vector<u32> writes;
        const std::array<u32, 1> parameters{count};
        const auto result = interpreter.Execute(code, 0, parameters, [](u32) { return 0U; },
            [&](u32 method, u32 value) { REQUIRE(method == 0xd00); writes.push_back(value); return true; });
        REQUIRE(result.success);
        REQUIRE(result.consumed == 1);
        REQUIRE(writes.size() == count);
        REQUIRE(writes.front() == count - 1);
        REQUIRE(writes.back() == 0);
    }
}

TEST_CASE("Native compute QMD releases follow successful dispatch and retain reduction flags", "[native-gpu]") {
    Tegra::NativeQmdScheduler scheduler;
    Tegra::NativeQmdScheduler::Descriptor descriptor{};
    descriptor[18] = 0x30;
    descriptor[12] = descriptor[13] = descriptor[14] = 1;
    descriptor[24] = 0x10000;
    descriptor[25] = 0x44800021;
    descriptor[26] = 1;
    std::vector<int> order;
    const auto read = [&](u64, auto& out) { out = descriptor; return true; };
    const auto release = [&](u64 address, u32 value, u32 flags) {
        REQUIRE(address == 0x2100010000ULL);
        REQUIRE(value == 1);
        REQUIRE(flags == 0x44800021);
        order.push_back(2);
        return true;
    };
    REQUIRE(scheduler.Signal(0x20000, 3, read, release,
        [&](const auto&) { order.push_back(1); return false; }) == "native shader dispatch required");
    REQUIRE(order == std::vector<int>{1});
    order.clear();
    REQUIRE(scheduler.Signal(0x20000, 3, read, release,
        [&](const auto&) { order.push_back(1); return true; }).empty());
    REQUIRE(order == std::vector<int>{1, 2});
}

TEST_CASE("Native vertex stream sizes use byte counts and dirty the selected stream", "[native-gpu]") {
    using Maxwell3D = Tegra::Engines::Maxwell3D;
    auto regs = std::make_unique<Maxwell3D::Regs>();
    regs->reg_array.fill(0);
    regs->reg_array[0x600 / 4 + 1] = 0x1b4e8;
    REQUIRE(regs->NativeVertexStreamSize(0) == 111848);
    REQUIRE(regs->vertex_stream_limits[0].Address() == 0);
    regs->reg_array[0x600 / 4 + 31 * 2] = 0xffffff81;
    regs->reg_array[0x600 / 4 + 31 * 2 + 1] = 0x76543210;
    REQUIRE(regs->NativeVertexStreamSize(31) == 0x8176543210ULL);
    REQUIRE(regs->NativeVertexStreamSize(1) == 0);
    Maxwell3D::DirtyState::Tables tables{};
    VideoCommon::Dirty::SetupDirtyFlags(tables);
    for (u32 stream = 0; stream < 32; ++stream) {
        for (u32 word = 0; word < 2; ++word) {
            const u32 method = 0x600 / 4 + stream * 2 + word;
            REQUIRE(tables[0][method] == VideoCommon::Dirty::VertexBuffer0 + stream);
            REQUIRE(tables[1][method] == VideoCommon::Dirty::VertexBuffers);
        }
    }
}