1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 * based on amdgpu winsys.
5 * Copyright © 2011 Marek Olšák <maraeo@gmail.com>
6 * Copyright © 2015 Advanced Micro Devices, Inc.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * IN THE SOFTWARE.
26 */
27 #include "radv_amdgpu_winsys.h"
28 #include <assert.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include "drm-uapi/amdgpu_drm.h"
33 #include "ac_surface.h"
34 #include "radv_amdgpu_bo.h"
35 #include "radv_amdgpu_cs.h"
36 #include "radv_amdgpu_surface.h"
37 #include "radv_amdgpu_winsys_public.h"
38 #include "radv_debug.h"
39 #include "vk_drm_syncobj.h"
40 #include "xf86drm.h"
41
42 static bool
do_winsys_init(struct radv_amdgpu_winsys * ws,int fd)43 do_winsys_init(struct radv_amdgpu_winsys *ws, int fd)
44 {
45 if (!ac_query_gpu_info(fd, ws->dev, &ws->info, true))
46 return false;
47
48 /*
49 * Override the max submits on video queues.
50 * If you submit multiple session contexts in the same IB sequence the
51 * hardware gets upset as it expects a kernel fence to be emitted to reset
52 * the session context in the hardware.
53 * Avoid this problem by never submitted more than one IB at a time.
54 * This possibly should be fixed in the kernel, and if it is this can be
55 * resolved.
56 */
57 for (enum amd_ip_type ip_type = AMD_IP_UVD; ip_type <= AMD_IP_VCN_ENC; ip_type++)
58 ws->info.max_submitted_ibs[ip_type] = 1;
59
60 ws->addrlib = ac_addrlib_create(&ws->info, &ws->info.max_alignment);
61 if (!ws->addrlib) {
62 fprintf(stderr, "radv/amdgpu: Cannot create addrlib.\n");
63 return false;
64 }
65
66 ws->info.ip[AMD_IP_SDMA].num_queues = MIN2(ws->info.ip[AMD_IP_SDMA].num_queues, MAX_RINGS_PER_TYPE);
67 ws->info.ip[AMD_IP_COMPUTE].num_queues = MIN2(ws->info.ip[AMD_IP_COMPUTE].num_queues, MAX_RINGS_PER_TYPE);
68
69 ws->use_ib_bos = true;
70 return true;
71 }
72
73 static void
radv_amdgpu_winsys_query_info(struct radeon_winsys * rws,struct radeon_info * info)74 radv_amdgpu_winsys_query_info(struct radeon_winsys *rws, struct radeon_info *info)
75 {
76 *info = ((struct radv_amdgpu_winsys *)rws)->info;
77 }
78
79 static uint64_t
radv_amdgpu_winsys_query_value(struct radeon_winsys * rws,enum radeon_value_id value)80 radv_amdgpu_winsys_query_value(struct radeon_winsys *rws, enum radeon_value_id value)
81 {
82 struct radv_amdgpu_winsys *ws = (struct radv_amdgpu_winsys *)rws;
83 struct amdgpu_heap_info heap;
84 uint64_t retval = 0;
85
86 switch (value) {
87 case RADEON_ALLOCATED_VRAM:
88 return ws->allocated_vram;
89 case RADEON_ALLOCATED_VRAM_VIS:
90 return ws->allocated_vram_vis;
91 case RADEON_ALLOCATED_GTT:
92 return ws->allocated_gtt;
93 case RADEON_TIMESTAMP:
94 amdgpu_query_info(ws->dev, AMDGPU_INFO_TIMESTAMP, 8, &retval);
95 return retval;
96 case RADEON_NUM_BYTES_MOVED:
97 amdgpu_query_info(ws->dev, AMDGPU_INFO_NUM_BYTES_MOVED, 8, &retval);
98 return retval;
99 case RADEON_NUM_EVICTIONS:
100 amdgpu_query_info(ws->dev, AMDGPU_INFO_NUM_EVICTIONS, 8, &retval);
101 return retval;
102 case RADEON_NUM_VRAM_CPU_PAGE_FAULTS:
103 amdgpu_query_info(ws->dev, AMDGPU_INFO_NUM_VRAM_CPU_PAGE_FAULTS, 8, &retval);
104 return retval;
105 case RADEON_VRAM_USAGE:
106 amdgpu_query_heap_info(ws->dev, AMDGPU_GEM_DOMAIN_VRAM, 0, &heap);
107 return heap.heap_usage;
108 case RADEON_VRAM_VIS_USAGE:
109 amdgpu_query_heap_info(ws->dev, AMDGPU_GEM_DOMAIN_VRAM, AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED, &heap);
110 return heap.heap_usage;
111 case RADEON_GTT_USAGE:
112 amdgpu_query_heap_info(ws->dev, AMDGPU_GEM_DOMAIN_GTT, 0, &heap);
113 return heap.heap_usage;
114 case RADEON_GPU_TEMPERATURE:
115 amdgpu_query_sensor_info(ws->dev, AMDGPU_INFO_SENSOR_GPU_TEMP, 4, &retval);
116 return retval;
117 case RADEON_CURRENT_SCLK:
118 amdgpu_query_sensor_info(ws->dev, AMDGPU_INFO_SENSOR_GFX_SCLK, 4, &retval);
119 return retval;
120 case RADEON_CURRENT_MCLK:
121 amdgpu_query_sensor_info(ws->dev, AMDGPU_INFO_SENSOR_GFX_MCLK, 4, &retval);
122 return retval;
123 default:
124 unreachable("invalid query value");
125 }
126
127 return 0;
128 }
129
130 static bool
radv_amdgpu_winsys_read_registers(struct radeon_winsys * rws,unsigned reg_offset,unsigned num_registers,uint32_t * out)131 radv_amdgpu_winsys_read_registers(struct radeon_winsys *rws, unsigned reg_offset, unsigned num_registers, uint32_t *out)
132 {
133 struct radv_amdgpu_winsys *ws = (struct radv_amdgpu_winsys *)rws;
134
135 return amdgpu_read_mm_registers(ws->dev, reg_offset / 4, num_registers, 0xffffffff, 0, out) == 0;
136 }
137
138 static const char *
radv_amdgpu_winsys_get_chip_name(struct radeon_winsys * rws)139 radv_amdgpu_winsys_get_chip_name(struct radeon_winsys *rws)
140 {
141 amdgpu_device_handle dev = ((struct radv_amdgpu_winsys *)rws)->dev;
142
143 return amdgpu_get_marketing_name(dev);
144 }
145
146 static bool
radv_amdgpu_winsys_query_gpuvm_fault(struct radeon_winsys * rws,struct radv_winsys_gpuvm_fault_info * fault_info)147 radv_amdgpu_winsys_query_gpuvm_fault(struct radeon_winsys *rws, struct radv_winsys_gpuvm_fault_info *fault_info)
148 {
149 struct radv_amdgpu_winsys *ws = (struct radv_amdgpu_winsys *)rws;
150 struct drm_amdgpu_info_gpuvm_fault gpuvm_fault = {0};
151 int r;
152
153 r = amdgpu_query_info(ws->dev, AMDGPU_INFO_GPUVM_FAULT, sizeof(gpuvm_fault), &gpuvm_fault);
154 if (r < 0) {
155 fprintf(stderr, "radv/amdgpu: Failed to query the last GPUVM fault (%d).\n", r);
156 return false;
157 }
158
159 /* When the GPUVM fault status is 0, no faults happened. */
160 if (!gpuvm_fault.status)
161 return false;
162
163 fault_info->addr = gpuvm_fault.addr;
164 fault_info->status = gpuvm_fault.status;
165 fault_info->vmhub = gpuvm_fault.vmhub;
166
167 return true;
168 }
169
170 static simple_mtx_t winsys_creation_mutex = SIMPLE_MTX_INITIALIZER;
171 static struct hash_table *winsyses = NULL;
172
173 static void
radv_amdgpu_winsys_destroy(struct radeon_winsys * rws)174 radv_amdgpu_winsys_destroy(struct radeon_winsys *rws)
175 {
176 struct radv_amdgpu_winsys *ws = (struct radv_amdgpu_winsys *)rws;
177 bool destroy = false;
178
179 simple_mtx_lock(&winsys_creation_mutex);
180 if (!--ws->refcount) {
181 _mesa_hash_table_remove_key(winsyses, ws->dev);
182
183 /* Clean the hashtable up if empty, though there is no
184 * empty function. */
185 if (_mesa_hash_table_num_entries(winsyses) == 0) {
186 _mesa_hash_table_destroy(winsyses, NULL);
187 winsyses = NULL;
188 }
189
190 destroy = true;
191 }
192 simple_mtx_unlock(&winsys_creation_mutex);
193 if (!destroy)
194 return;
195
196 u_rwlock_destroy(&ws->global_bo_list.lock);
197 free(ws->global_bo_list.bos);
198
199 if (ws->reserve_vmid)
200 amdgpu_vm_unreserve_vmid(ws->dev, 0);
201
202 u_rwlock_destroy(&ws->log_bo_list_lock);
203 ac_addrlib_destroy(ws->addrlib);
204 amdgpu_device_deinitialize(ws->dev);
205 FREE(rws);
206 }
207
208 static int
radv_amdgpu_winsys_get_fd(struct radeon_winsys * rws)209 radv_amdgpu_winsys_get_fd(struct radeon_winsys *rws)
210 {
211 struct radv_amdgpu_winsys *ws = (struct radv_amdgpu_winsys *)rws;
212 return amdgpu_device_get_fd(ws->dev);
213 }
214
215 static const struct vk_sync_type *const *
radv_amdgpu_winsys_get_sync_types(struct radeon_winsys * rws)216 radv_amdgpu_winsys_get_sync_types(struct radeon_winsys *rws)
217 {
218 struct radv_amdgpu_winsys *ws = (struct radv_amdgpu_winsys *)rws;
219 return ws->sync_types;
220 }
221
222 struct radeon_winsys *
radv_amdgpu_winsys_create(int fd,uint64_t debug_flags,uint64_t perftest_flags,bool reserve_vmid)223 radv_amdgpu_winsys_create(int fd, uint64_t debug_flags, uint64_t perftest_flags, bool reserve_vmid)
224 {
225 uint32_t drm_major, drm_minor, r;
226 amdgpu_device_handle dev;
227 struct radv_amdgpu_winsys *ws = NULL;
228
229 r = amdgpu_device_initialize(fd, &drm_major, &drm_minor, &dev);
230 if (r) {
231 fprintf(stderr, "radv/amdgpu: failed to initialize device.\n");
232 return NULL;
233 }
234
235 /* We have to keep this lock till insertion. */
236 simple_mtx_lock(&winsys_creation_mutex);
237 if (!winsyses)
238 winsyses = _mesa_pointer_hash_table_create(NULL);
239 if (!winsyses) {
240 fprintf(stderr, "radv/amdgpu: failed to alloc winsys hash table.\n");
241 goto fail;
242 }
243
244 struct hash_entry *entry = _mesa_hash_table_search(winsyses, dev);
245 if (entry) {
246 ws = (struct radv_amdgpu_winsys *)entry->data;
247 ++ws->refcount;
248 }
249
250 if (ws) {
251 simple_mtx_unlock(&winsys_creation_mutex);
252 amdgpu_device_deinitialize(dev);
253
254 /* Check that options don't differ from the existing winsys. */
255 if (((debug_flags & RADV_DEBUG_ALL_BOS) && !ws->debug_all_bos) ||
256 ((debug_flags & RADV_DEBUG_HANG) && !ws->debug_log_bos) ||
257 ((debug_flags & RADV_DEBUG_NO_IBS) && ws->use_ib_bos) || (perftest_flags != ws->perftest)) {
258 fprintf(stderr, "radv/amdgpu: Found options that differ from the existing winsys.\n");
259 return NULL;
260 }
261
262 /* RADV_DEBUG_ZERO_VRAM is the only option that is allowed to be set again. */
263 if (debug_flags & RADV_DEBUG_ZERO_VRAM)
264 ws->zero_all_vram_allocs = true;
265
266 return &ws->base;
267 }
268
269 ws = calloc(1, sizeof(struct radv_amdgpu_winsys));
270 if (!ws)
271 goto fail;
272
273 ws->refcount = 1;
274 ws->dev = dev;
275 ws->info.drm_major = drm_major;
276 ws->info.drm_minor = drm_minor;
277 if (!do_winsys_init(ws, fd))
278 goto winsys_fail;
279
280 ws->debug_all_bos = !!(debug_flags & RADV_DEBUG_ALL_BOS);
281 ws->debug_log_bos = debug_flags & RADV_DEBUG_HANG;
282 if (debug_flags & RADV_DEBUG_NO_IBS)
283 ws->use_ib_bos = false;
284
285 ws->reserve_vmid = reserve_vmid;
286 if (ws->reserve_vmid) {
287 r = amdgpu_vm_reserve_vmid(dev, 0);
288 if (r) {
289 fprintf(stderr, "radv/amdgpu: failed to reserve vmid.\n");
290 goto vmid_fail;
291 }
292 }
293 int num_sync_types = 0;
294
295 ws->syncobj_sync_type = vk_drm_syncobj_get_type(amdgpu_device_get_fd(ws->dev));
296 if (ws->syncobj_sync_type.features) {
297 ws->sync_types[num_sync_types++] = &ws->syncobj_sync_type;
298 if (!(ws->syncobj_sync_type.features & VK_SYNC_FEATURE_TIMELINE)) {
299 ws->emulated_timeline_sync_type = vk_sync_timeline_get_type(&ws->syncobj_sync_type);
300 ws->sync_types[num_sync_types++] = &ws->emulated_timeline_sync_type.sync;
301 }
302 }
303
304 ws->sync_types[num_sync_types++] = NULL;
305 assert(num_sync_types <= ARRAY_SIZE(ws->sync_types));
306
307 ws->perftest = perftest_flags;
308 ws->zero_all_vram_allocs = debug_flags & RADV_DEBUG_ZERO_VRAM;
309 u_rwlock_init(&ws->global_bo_list.lock);
310 list_inithead(&ws->log_bo_list);
311 u_rwlock_init(&ws->log_bo_list_lock);
312 ws->base.query_info = radv_amdgpu_winsys_query_info;
313 ws->base.query_value = radv_amdgpu_winsys_query_value;
314 ws->base.read_registers = radv_amdgpu_winsys_read_registers;
315 ws->base.get_chip_name = radv_amdgpu_winsys_get_chip_name;
316 ws->base.query_gpuvm_fault = radv_amdgpu_winsys_query_gpuvm_fault;
317 ws->base.destroy = radv_amdgpu_winsys_destroy;
318 ws->base.get_fd = radv_amdgpu_winsys_get_fd;
319 ws->base.get_sync_types = radv_amdgpu_winsys_get_sync_types;
320 radv_amdgpu_bo_init_functions(ws);
321 radv_amdgpu_cs_init_functions(ws);
322 radv_amdgpu_surface_init_functions(ws);
323
324 _mesa_hash_table_insert(winsyses, dev, ws);
325 simple_mtx_unlock(&winsys_creation_mutex);
326
327 return &ws->base;
328
329 vmid_fail:
330 ac_addrlib_destroy(ws->addrlib);
331 winsys_fail:
332 free(ws);
333 fail:
334 if (winsyses && _mesa_hash_table_num_entries(winsyses) == 0) {
335 _mesa_hash_table_destroy(winsyses, NULL);
336 winsyses = NULL;
337 }
338 simple_mtx_unlock(&winsys_creation_mutex);
339 amdgpu_device_deinitialize(dev);
340 return NULL;
341 }
342