summaryrefslogtreecommitdiffstats
path: root/src/audio_core/renderer/memory/address_info.h
blob: bb5c930e1f4ba3f8250147dc78b77b2aea3afec0 (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
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include "audio_core/renderer/memory/memory_pool_info.h"
#include "common/common_types.h"

namespace AudioCore::AudioRenderer {

/**
 * Represents a region of mapped or unmapped memory.
 */
class AddressInfo {
public:
    AddressInfo() = default;
    AddressInfo(CpuAddr cpu_address_, u64 size_) : cpu_address{cpu_address_}, size{size_} {}

    /**
     * Setup a new AddressInfo.
     *
     * @param cpu_address_ - The CPU address of this region.
     * @param size_        - The size of this region.
     */
    void Setup(CpuAddr cpu_address_, u64 size_) {
        cpu_address = cpu_address_;
        size = size_;
        memory_pool = nullptr;
        dsp_address = 0;
    }

    /**
     * Get the CPU address.
     *
     * @return The CpuAddr address
     */
    CpuAddr GetCpuAddr() const {
        return cpu_address;
    }

    /**
     * Assign this region to a memory pool.
     *
     * @param memory_pool_ - Memory pool to assign.
     */
    void SetPool(MemoryPoolInfo* memory_pool_) {
        memory_pool = memory_pool_;
    }

    /**
     * Get the size of this region.
     *
     * @return The size of this region.
     */
    u64 GetSize() const {
        return size;
    }

    /**
     * Get the ADSP address for this region.
     *
     * @return The ADSP address for this region.
     */
    CpuAddr GetForceMappedDspAddr() const {
        return dsp_address;
    }

    /**
     * Set the ADSP address for this region.
     *
     * @param dsp_addr - The new ADSP address for this region.
     */
    void SetForceMappedDspAddr(CpuAddr dsp_addr) {
        dsp_address = dsp_addr;
    }

    /**
     * Check whether this region has an active memory pool.
     *
     * @return True if this region has a mapped memory pool, otherwise false.
     */
    bool HasMappedMemoryPool() const {
        return memory_pool != nullptr && memory_pool->GetDspAddress() != 0;
    }

    /**
     * Check whether this region is mapped to the ADSP.
     *
     * @return True if this region is mapped, otherwise false.
     */
    bool IsMapped() const {
        return HasMappedMemoryPool() || dsp_address != 0;
    }

    /**
     * Get a usable reference to this region of memory.
     *
     * @param mark_in_use - Whether this region should be marked as being in use.
     * @return A valid memory address if valid, otherwise 0.
     */
    CpuAddr GetReference(bool mark_in_use) {
        if (!HasMappedMemoryPool()) {
            return dsp_address;
        }

        if (mark_in_use) {
            memory_pool->SetUsed(true);
        }

        return memory_pool->Translate(cpu_address, size);
    }

private:
    /// CPU address of this region
    CpuAddr cpu_address;
    /// Size of this region
    u64 size;
    /// The memory this region is mapped to
    MemoryPoolInfo* memory_pool;
    /// ADSP address of this region
    CpuAddr dsp_address;
};

} // namespace AudioCore::AudioRenderer