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

#pragma once

#include <span>

#include "audio_core/renderer/behavior/behavior_info.h"
#include "audio_core/renderer/memory/memory_pool_info.h"
#include "common/common_types.h"
#include "core/hle/service/audio/errors.h"

namespace AudioCore::Renderer {
class AddressInfo;

/**
 * Utility functions for managing MemoryPoolInfos
 */
class PoolMapper {
public:
    explicit PoolMapper(u32 process_handle, bool force_map);
    explicit PoolMapper(u32 process_handle, std::span<MemoryPoolInfo> pool_infos, u32 pool_count,
                        bool force_map);

    /**
     * Clear the usage state for all given pools.
     *
     * @param pools - The memory pools to clear.
     * @param count - The number of pools.
     */
    static void ClearUseState(std::span<MemoryPoolInfo> pools, u32 count);

    /**
     * Find the memory pool containing the given address and size from a given list of pools.
     *
     * @param pools   - The memory pools to search within.
     * @param count   - The number of pools.
     * @param address - The address of the region to find.
     * @param size    - The size of the region to find.
     * @return Pointer to the memory pool if found, otherwise nullptr.
     */
    MemoryPoolInfo* FindMemoryPool(MemoryPoolInfo* pools, u64 count, CpuAddr address,
                                   u64 size) const;

    /**
     * Find the memory pool containing the given address and size from the PoolMapper's memory pool.
     *
     * @param address - The address of the region to find.
     * @param size    - The size of the region to find.
     * @return Pointer to the memory pool if found, otherwise nullptr.
     */
    MemoryPoolInfo* FindMemoryPool(CpuAddr address, u64 size) const;

    /**
     * Set the PoolMapper's memory pool to one in the given list of pools, which contains
     * address_info.
     *
     * @param address_info - The expected region to find within pools.
     * @param pools        - The list of pools to search within.
     * @param count        - The number of pools given.
     * @return True if successfully mapped, otherwise false.
     */
    bool FillDspAddr(AddressInfo& address_info, MemoryPoolInfo* pools, u32 count) const;

    /**
     * Set the PoolMapper's memory pool to the one containing address_info.
     *
     * @param address_info - The address to find the memory pool for.
     * @return True if successfully mapped, otherwise false.
     */
    bool FillDspAddr(AddressInfo& address_info) const;

    /**
     * Try to attach a {address, size} region to the given address_info, and map it. Fills in the
     * given error_info and address_info.
     *
     * @param error_info   - Output error info.
     * @param address_info - Output address info, initialized with the given {address, size} and
     *                       attempted to map.
     * @param address      - Address of the region to map.
     * @param size         - Size of the region to map.
     * @return True if successfully attached, otherwise false.
     */
    bool TryAttachBuffer(BehaviorInfo::ErrorInfo& error_info, AddressInfo& address_info,
                         CpuAddr address, u64 size) const;

    /**
     * Return whether force mapping is enabled.
     *
     * @return True if force mapping is enabled, otherwise false.
     */
    bool IsForceMapEnabled() const;

    /**
     * Get the process handle, depending on location.
     *
     * @param pool - The pool to check the location of.
     * @return CurrentProcessHandle if location == DSP,
     *         the PoolMapper's process_handle if location == CPU
     */
    u32 GetProcessHandle(const MemoryPoolInfo* pool) const;

    /**
     * Map the given region with the given handle. This is a no-op.
     *
     * @param handle   - The process handle to map to.
     * @param cpu_addr - Address to map.
     * @param size     - Size to map.
     * @return True if successfully mapped, otherwise false.
     */
    bool Map(u32 handle, CpuAddr cpu_addr, u64 size) const;

    /**
     * Map the given memory pool.
     *
     * @param pool - The pool to map.
     * @return True if successfully mapped, otherwise false.
     */
    bool Map(MemoryPoolInfo& pool) const;

    /**
     * Unmap the given region with the given handle.
     *
     * @param handle   - The process handle to unmap to.
     * @param cpu_addr - Address to unmap.
     * @param size     - Size to unmap.
     * @return True if successfully unmapped, otherwise false.
     */
    bool Unmap(u32 handle, CpuAddr cpu_addr, u64 size) const;

    /**
     * Unmap the given memory pool.
     *
     * @param pool - The pool to unmap.
     * @return True if successfully unmapped, otherwise false.
     */
    bool Unmap(MemoryPoolInfo& pool) const;

    /**
     * Forcibly unmap the given region.
     *
     * @param address_info - The region to unmap.
     */
    void ForceUnmapPointer(const AddressInfo& address_info) const;

    /**
     * Update the given memory pool.
     *
     * @param pool       - Pool to update.
     * @param in_params  - Input parameters for the update.
     * @param out_params - Output parameters for the update.
     * @return The result of the update. See MemoryPoolInfo::ResultState
     */
    MemoryPoolInfo::ResultState Update(MemoryPoolInfo& pool,
                                       const MemoryPoolInfo::InParameter& in_params,
                                       MemoryPoolInfo::OutStatus& out_params) const;

    /**
     * Initialize the PoolMapper's memory pool.
     *
     * @param pool   - Input pool to initialize.
     * @param memory - Pointer to the memory region for the pool.
     * @param size   - Size of the memory region for the pool.
     * @return True if initialized successfully, otherwise false.
     */
    bool InitializeSystemPool(MemoryPoolInfo& pool, const u8* memory, u64 size) const;

private:
    /// Process handle for this mapper, used when location == CPU
    u32 process_handle;
    /// List of memory pools assigned to this mapper
    MemoryPoolInfo* pool_infos{};
    /// The number of pools
    u64 pool_count{};
    /// Is forced mapping enabled
    bool force_map;
};

} // namespace AudioCore::Renderer