Debian Codegraph @

src/imagination/vulkan/pvr_spm.c

raw
1 /*
2 * Copyright © 2023 Imagination Technologies Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include <stdint.h>
25 #include <vulkan/vulkan_core.h>
26
27 #include "hwdef/rogue_hw_utils.h"
28 #include "pvr_bo.h"
29 #include "pvr_csb.h"
30 #include "pvr_device.h"
31 #include "pvr_device_info.h"
32 #include "pvr_hw_pass.h"
33 #include "pvr_macros.h"
34 #include "pvr_pass.h"
35 #include "pvr_physical_device.h"
36 #include "pvr_spm.h"
37 #include "util/macros.h"
38 #include "util/simple_mtx.h"
39 #include "util/u_atomic.h"
40 #include "vk_alloc.h"
41 #include "vk_log.h"
42
43 struct pvr_spm_scratch_buffer {
44 uint32_t ref_count;
45 struct pvr_bo *bo;
46 uint64_t size;
47 };
48
49 void pvr_spm_init_scratch_buffer_store(struct pvr_device *device)
50 {
51 struct pvr_spm_scratch_buffer_store *store =
52 &device->spm_scratch_buffer_store;
53
54 simple_mtx_init(&store->mtx, mtx_plain);
55 store->head_ref = NULL;
56 }
57
58 void pvr_spm_finish_scratch_buffer_store(struct pvr_device *device)
59 {
60 struct pvr_spm_scratch_buffer_store *store =
61 &device->spm_scratch_buffer_store;
62
63 /* Either a framebuffer was never created so no scratch buffer was ever
64 * created or all framebuffers have been freed so only the store's reference
65 * remains.
66 */
67 assert(!store->head_ref || p_atomic_read(&store->head_ref->ref_count) == 1);
68
69 simple_mtx_destroy(&store->mtx);
70
71 if (store->head_ref) {
72 pvr_bo_free(device, store->head_ref->bo);
73 vk_free(&device->vk.alloc, store->head_ref);
74 }
75 }
76
77 static VkResult
78 pvr_spm_scratch_buffer_alloc(struct pvr_device *device,
79 uint64_t size,
80 struct pvr_spm_scratch_buffer **const buffer_out)
81 {
82 const uint32_t cache_line_size =
83 pvr_get_slc_cache_line_size(&device->pdevice->dev_info);
84 struct pvr_spm_scratch_buffer *scratch_buffer;
85 struct pvr_bo *bo;
86 VkResult result;
87
88 result = pvr_bo_alloc(device,
89 device->heaps.general_heap,
90 size,
91 cache_line_size,
92 0,
93 &bo);
94 if (result != VK_SUCCESS) {
95 *buffer_out = NULL;
96 return result;
97 }
98
99 scratch_buffer = vk_alloc(&device->vk.alloc,
100 sizeof(*scratch_buffer),
101 4,
102 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
103 if (!scratch_buffer) {
104 pvr_bo_free(device, bo);
105 *buffer_out = NULL;
106 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
107 }
108
109 *scratch_buffer = (struct pvr_spm_scratch_buffer){
110 .bo = bo,
111 .size = size,
112 };
113
114 *buffer_out = scratch_buffer;
115
116 return VK_SUCCESS;
117 }
118
119 static void
120 pvr_spm_scratch_buffer_release_locked(struct pvr_device *device,
121 struct pvr_spm_scratch_buffer *buffer)
122 {
123 struct pvr_spm_scratch_buffer_store *store =
124 &device->spm_scratch_buffer_store;
125
126 simple_mtx_assert_locked(&store->mtx);
127
128 if (p_atomic_dec_zero(&buffer->ref_count)) {
129 pvr_bo_free(device, buffer->bo);
130 vk_free(&device->vk.alloc, buffer);
131 }
132 }
133
134 void pvr_spm_scratch_buffer_release(struct pvr_device *device,
135 struct pvr_spm_scratch_buffer *buffer)
136 {
137 struct pvr_spm_scratch_buffer_store *store =
138 &device->spm_scratch_buffer_store;
139
140 simple_mtx_lock(&store->mtx);
141
142 pvr_spm_scratch_buffer_release_locked(device, buffer);
143
144 simple_mtx_unlock(&store->mtx);
145 }
146
147 static void pvr_spm_scratch_buffer_store_set_head_ref_locked(
148 struct pvr_spm_scratch_buffer_store *store,
149 struct pvr_spm_scratch_buffer *buffer)
150 {
151 simple_mtx_assert_locked(&store->mtx);
152 assert(!store->head_ref);
153
154 p_atomic_inc(&buffer->ref_count);
155 store->head_ref = buffer;
156 }
157
158 static void pvr_spm_scratch_buffer_store_release_head_ref_locked(
159 struct pvr_device *device,
160 struct pvr_spm_scratch_buffer_store *store)
161 {
162 simple_mtx_assert_locked(&store->mtx);
163
164 pvr_spm_scratch_buffer_release_locked(device, store->head_ref);
165
166 store->head_ref = NULL;
167 }
168
169 VkResult pvr_spm_scratch_buffer_get_buffer(
170 struct pvr_device *device,
171 uint64_t size,
172 struct pvr_spm_scratch_buffer **const buffer_out)
173 {
174 struct pvr_spm_scratch_buffer_store *store =
175 &device->spm_scratch_buffer_store;
176 struct pvr_spm_scratch_buffer *buffer;
177
178 simple_mtx_lock(&store->mtx);
179
180 /* When a render requires a PR the fw will wait for other renders to end,
181 * free the PB space, unschedule any other vert/frag jobs and solely run the
182 * PR on the whole device until completion.
183 * Thus we can safely use the same scratch buffer across multiple
184 * framebuffers as the scratch buffer is only used during PRs and only one PR
185 * can ever be executed at any one time.
186 */
187 if (store->head_ref && store->head_ref->size == size) {
188 buffer = store->head_ref;
189 } else {
190 VkResult result;
191
192 if (store->head_ref)
193 pvr_spm_scratch_buffer_store_release_head_ref_locked(device, store);
194
195 result = pvr_spm_scratch_buffer_alloc(device, size, &buffer);
196 if (result != VK_SUCCESS) {
197 simple_mtx_unlock(&store->mtx);
198 *buffer_out = NULL;
199
200 return result;
201 }
202
203 pvr_spm_scratch_buffer_store_set_head_ref_locked(store, buffer);
204 }
205
206 p_atomic_inc(&buffer->ref_count);
207 simple_mtx_unlock(&store->mtx);
208 *buffer_out = buffer;
209
210 return VK_SUCCESS;
211 }
212
213 void pvr_spm_finish_eot_state(struct pvr_device *device,
214 struct pvr_spm_eot_state *spm_eot_state)
215 {
216 pvr_bo_suballoc_free(spm_eot_state->pixel_event_program_data_upload);
217 pvr_bo_suballoc_free(spm_eot_state->usc_eot_program);
218 }
219
220 void pvr_spm_finish_bgobj_state(struct pvr_device *device,
221 struct pvr_spm_bgobj_state *spm_bgobj_state)
222 {
223 pvr_bo_suballoc_free(spm_bgobj_state->pds_texture_data_upload);
224 pvr_bo_free(device, spm_bgobj_state->consts_buffer);
225 }