3d81129a33423dc3c6a2cf0d195281942e61b772
[mesa.git] / src / gallium / winsys / amdgpu / drm / amdgpu_winsys.c
1 /*
2 * Copyright © 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright © 2009 Joakim Sindholt <opensource@zhasha.com>
4 * Copyright © 2011 Marek Olšák <maraeo@gmail.com>
5 * Copyright © 2015 Advanced Micro Devices, Inc.
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
20 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * The above copyright notice and this permission notice (including the
26 * next paragraph) shall be included in all copies or substantial portions
27 * of the Software.
28 */
29
30 #include "amdgpu_cs.h"
31 #include "amdgpu_public.h"
32
33 #include "util/os_file.h"
34 #include "util/os_misc.h"
35 #include "util/u_cpu_detect.h"
36 #include "util/u_hash_table.h"
37 #include "util/hash_table.h"
38 #include "util/xmlconfig.h"
39 #include "drm-uapi/amdgpu_drm.h"
40 #include <xf86drm.h>
41 #include <stdio.h>
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #include "ac_llvm_util.h"
45 #include "sid.h"
46
47 static struct hash_table *dev_tab = NULL;
48 static simple_mtx_t dev_tab_mutex = _SIMPLE_MTX_INITIALIZER_NP;
49
50 DEBUG_GET_ONCE_BOOL_OPTION(all_bos, "RADEON_ALL_BOS", false)
51
52 static void handle_env_var_force_family(struct amdgpu_winsys *ws)
53 {
54 const char *family = debug_get_option("SI_FORCE_FAMILY", NULL);
55 unsigned i;
56
57 if (!family)
58 return;
59
60 for (i = CHIP_TAHITI; i < CHIP_LAST; i++) {
61 if (!strcmp(family, ac_get_llvm_processor_name(i))) {
62 /* Override family and chip_class. */
63 ws->info.family = i;
64 ws->info.name = "GCN-NOOP";
65
66 if (i >= CHIP_NAVI10)
67 ws->info.chip_class = GFX10;
68 else if (i >= CHIP_VEGA10)
69 ws->info.chip_class = GFX9;
70 else if (i >= CHIP_TONGA)
71 ws->info.chip_class = GFX8;
72 else if (i >= CHIP_BONAIRE)
73 ws->info.chip_class = GFX7;
74 else
75 ws->info.chip_class = GFX6;
76
77 /* Don't submit any IBs. */
78 setenv("RADEON_NOOP", "1", 1);
79 return;
80 }
81 }
82
83 fprintf(stderr, "radeonsi: Unknown family: %s\n", family);
84 exit(1);
85 }
86
87 /* Helper function to do the ioctls needed for setup and init. */
88 static bool do_winsys_init(struct amdgpu_winsys *ws,
89 const struct pipe_screen_config *config,
90 int fd)
91 {
92 if (!ac_query_gpu_info(fd, ws->dev, &ws->info, &ws->amdinfo))
93 goto fail;
94
95 /* TODO: Enable this once the kernel handles it efficiently. */
96 if (ws->info.has_dedicated_vram)
97 ws->info.has_local_buffers = false;
98
99 handle_env_var_force_family(ws);
100
101 ws->addrlib = amdgpu_addr_create(&ws->info, &ws->amdinfo, &ws->info.max_alignment);
102 if (!ws->addrlib) {
103 fprintf(stderr, "amdgpu: Cannot create addrlib.\n");
104 goto fail;
105 }
106
107 ws->check_vm = strstr(debug_get_option("R600_DEBUG", ""), "check_vm") != NULL ||
108 strstr(debug_get_option("AMD_DEBUG", ""), "check_vm") != NULL;
109 ws->debug_all_bos = debug_get_option_all_bos();
110 ws->reserve_vmid = strstr(debug_get_option("R600_DEBUG", ""), "reserve_vmid") != NULL ||
111 strstr(debug_get_option("AMD_DEBUG", ""), "reserve_vmid") != NULL;
112 ws->zero_all_vram_allocs = strstr(debug_get_option("R600_DEBUG", ""), "zerovram") != NULL ||
113 strstr(debug_get_option("AMD_DEBUG", ""), "zerovram") != NULL ||
114 driQueryOptionb(config->options, "radeonsi_zerovram");
115
116 return true;
117
118 fail:
119 amdgpu_device_deinitialize(ws->dev);
120 ws->dev = NULL;
121 return false;
122 }
123
124 static void do_winsys_deinit(struct amdgpu_winsys *ws)
125 {
126 if (ws->reserve_vmid)
127 amdgpu_vm_unreserve_vmid(ws->dev, 0);
128
129 if (util_queue_is_initialized(&ws->cs_queue))
130 util_queue_destroy(&ws->cs_queue);
131
132 simple_mtx_destroy(&ws->bo_fence_lock);
133 for (unsigned i = 0; i < NUM_SLAB_ALLOCATORS; i++) {
134 if (ws->bo_slabs[i].groups)
135 pb_slabs_deinit(&ws->bo_slabs[i]);
136 }
137 pb_cache_deinit(&ws->bo_cache);
138 _mesa_hash_table_destroy(ws->bo_export_table, NULL);
139 simple_mtx_destroy(&ws->sws_list_lock);
140 simple_mtx_destroy(&ws->global_bo_list_lock);
141 simple_mtx_destroy(&ws->bo_export_table_lock);
142
143 AddrDestroy(ws->addrlib);
144 amdgpu_device_deinitialize(ws->dev);
145 FREE(ws);
146 }
147
148 static void amdgpu_winsys_destroy(struct radeon_winsys *rws)
149 {
150 struct amdgpu_screen_winsys *sws = amdgpu_screen_winsys(rws);
151 struct amdgpu_winsys *ws = sws->aws;
152 bool destroy;
153
154 /* When the reference counter drops to zero, remove the device pointer
155 * from the table.
156 * This must happen while the mutex is locked, so that
157 * amdgpu_winsys_create in another thread doesn't get the winsys
158 * from the table when the counter drops to 0.
159 */
160 simple_mtx_lock(&dev_tab_mutex);
161
162 destroy = pipe_reference(&ws->reference, NULL);
163 if (destroy && dev_tab) {
164 _mesa_hash_table_remove_key(dev_tab, ws->dev);
165 if (_mesa_hash_table_num_entries(dev_tab) == 0) {
166 _mesa_hash_table_destroy(dev_tab, NULL);
167 dev_tab = NULL;
168 }
169 }
170
171 simple_mtx_unlock(&dev_tab_mutex);
172
173 if (destroy)
174 do_winsys_deinit(ws);
175
176 close(sws->fd);
177 FREE(rws);
178 }
179
180 static void amdgpu_winsys_query_info(struct radeon_winsys *rws,
181 struct radeon_info *info)
182 {
183 *info = amdgpu_winsys(rws)->info;
184 }
185
186 static bool amdgpu_cs_request_feature(struct radeon_cmdbuf *rcs,
187 enum radeon_feature_id fid,
188 bool enable)
189 {
190 return false;
191 }
192
193 static uint64_t amdgpu_query_value(struct radeon_winsys *rws,
194 enum radeon_value_id value)
195 {
196 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
197 struct amdgpu_heap_info heap;
198 uint64_t retval = 0;
199
200 switch (value) {
201 case RADEON_REQUESTED_VRAM_MEMORY:
202 return ws->allocated_vram;
203 case RADEON_REQUESTED_GTT_MEMORY:
204 return ws->allocated_gtt;
205 case RADEON_MAPPED_VRAM:
206 return ws->mapped_vram;
207 case RADEON_MAPPED_GTT:
208 return ws->mapped_gtt;
209 case RADEON_BUFFER_WAIT_TIME_NS:
210 return ws->buffer_wait_time;
211 case RADEON_NUM_MAPPED_BUFFERS:
212 return ws->num_mapped_buffers;
213 case RADEON_TIMESTAMP:
214 amdgpu_query_info(ws->dev, AMDGPU_INFO_TIMESTAMP, 8, &retval);
215 return retval;
216 case RADEON_NUM_GFX_IBS:
217 return ws->num_gfx_IBs;
218 case RADEON_NUM_SDMA_IBS:
219 return ws->num_sdma_IBs;
220 case RADEON_GFX_BO_LIST_COUNTER:
221 return ws->gfx_bo_list_counter;
222 case RADEON_GFX_IB_SIZE_COUNTER:
223 return ws->gfx_ib_size_counter;
224 case RADEON_NUM_BYTES_MOVED:
225 amdgpu_query_info(ws->dev, AMDGPU_INFO_NUM_BYTES_MOVED, 8, &retval);
226 return retval;
227 case RADEON_NUM_EVICTIONS:
228 amdgpu_query_info(ws->dev, AMDGPU_INFO_NUM_EVICTIONS, 8, &retval);
229 return retval;
230 case RADEON_NUM_VRAM_CPU_PAGE_FAULTS:
231 amdgpu_query_info(ws->dev, AMDGPU_INFO_NUM_VRAM_CPU_PAGE_FAULTS, 8, &retval);
232 return retval;
233 case RADEON_VRAM_USAGE:
234 amdgpu_query_heap_info(ws->dev, AMDGPU_GEM_DOMAIN_VRAM, 0, &heap);
235 return heap.heap_usage;
236 case RADEON_VRAM_VIS_USAGE:
237 amdgpu_query_heap_info(ws->dev, AMDGPU_GEM_DOMAIN_VRAM,
238 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED, &heap);
239 return heap.heap_usage;
240 case RADEON_GTT_USAGE:
241 amdgpu_query_heap_info(ws->dev, AMDGPU_GEM_DOMAIN_GTT, 0, &heap);
242 return heap.heap_usage;
243 case RADEON_GPU_TEMPERATURE:
244 amdgpu_query_sensor_info(ws->dev, AMDGPU_INFO_SENSOR_GPU_TEMP, 4, &retval);
245 return retval;
246 case RADEON_CURRENT_SCLK:
247 amdgpu_query_sensor_info(ws->dev, AMDGPU_INFO_SENSOR_GFX_SCLK, 4, &retval);
248 return retval;
249 case RADEON_CURRENT_MCLK:
250 amdgpu_query_sensor_info(ws->dev, AMDGPU_INFO_SENSOR_GFX_MCLK, 4, &retval);
251 return retval;
252 case RADEON_CS_THREAD_TIME:
253 return util_queue_get_thread_time_nano(&ws->cs_queue, 0);
254 }
255 return 0;
256 }
257
258 static bool amdgpu_read_registers(struct radeon_winsys *rws,
259 unsigned reg_offset,
260 unsigned num_registers, uint32_t *out)
261 {
262 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
263
264 return amdgpu_read_mm_registers(ws->dev, reg_offset / 4, num_registers,
265 0xffffffff, 0, out) == 0;
266 }
267
268 static bool amdgpu_winsys_unref(struct radeon_winsys *rws)
269 {
270 struct amdgpu_screen_winsys *sws = amdgpu_screen_winsys(rws);
271 struct amdgpu_winsys *aws = sws->aws;
272 bool ret;
273
274 simple_mtx_lock(&aws->sws_list_lock);
275
276 ret = pipe_reference(&sws->reference, NULL);
277 if (ret) {
278 struct amdgpu_screen_winsys **sws_iter;
279 struct amdgpu_winsys *aws = sws->aws;
280
281 /* Remove this amdgpu_screen_winsys from amdgpu_winsys' list, so that
282 * amdgpu_winsys_create can't re-use it anymore
283 */
284 for (sws_iter = &aws->sws_list; *sws_iter; sws_iter = &(*sws_iter)->next) {
285 if (*sws_iter == sws) {
286 *sws_iter = sws->next;
287 break;
288 }
289 }
290 }
291
292 simple_mtx_unlock(&aws->sws_list_lock);
293
294 if (ret && sws->kms_handles) {
295 struct drm_gem_close args;
296
297 hash_table_foreach(sws->kms_handles, entry) {
298 args.handle = (uintptr_t)entry->data;
299 drmIoctl(sws->fd, DRM_IOCTL_GEM_CLOSE, &args);
300 }
301 _mesa_hash_table_destroy(sws->kms_handles, NULL);
302 }
303
304 return ret;
305 }
306
307 static void amdgpu_pin_threads_to_L3_cache(struct radeon_winsys *rws,
308 unsigned cache)
309 {
310 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
311
312 util_pin_thread_to_L3(ws->cs_queue.threads[0], cache,
313 util_cpu_caps.cores_per_L3);
314 }
315
316 static uint32_t kms_handle_hash(const void *key)
317 {
318 const struct amdgpu_winsys_bo *bo = key;
319
320 return bo->u.real.kms_handle;
321 }
322
323 static bool kms_handle_equals(const void *a, const void *b)
324 {
325 return a == b;
326 }
327
328 static bool amdgpu_ws_is_secure(struct radeon_winsys *rws)
329 {
330 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
331 return ws->secure;
332 }
333
334 static bool amdgpu_cs_is_secure(struct radeon_cmdbuf *rcs)
335 {
336 struct amdgpu_cs *cs = amdgpu_cs(rcs);
337 return cs->csc->secure;
338 }
339
340 static void amdgpu_cs_set_secure(struct radeon_cmdbuf *rcs, bool secure)
341 {
342 struct amdgpu_cs *cs = amdgpu_cs(rcs);
343 cs->csc->secure = secure;
344 }
345
346 PUBLIC struct radeon_winsys *
347 amdgpu_winsys_create(int fd, const struct pipe_screen_config *config,
348 radeon_screen_create_t screen_create)
349 {
350 struct amdgpu_screen_winsys *ws;
351 struct amdgpu_winsys *aws;
352 amdgpu_device_handle dev;
353 uint32_t drm_major, drm_minor;
354 int r;
355
356 ws = CALLOC_STRUCT(amdgpu_screen_winsys);
357 if (!ws)
358 return NULL;
359
360 pipe_reference_init(&ws->reference, 1);
361 ws->fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
362
363 /* Look up the winsys from the dev table. */
364 simple_mtx_lock(&dev_tab_mutex);
365 if (!dev_tab)
366 dev_tab = util_hash_table_create_ptr_keys();
367
368 /* Initialize the amdgpu device. This should always return the same pointer
369 * for the same fd. */
370 r = amdgpu_device_initialize(ws->fd, &drm_major, &drm_minor, &dev);
371 if (r) {
372 fprintf(stderr, "amdgpu: amdgpu_device_initialize failed.\n");
373 goto fail;
374 }
375
376 /* Lookup a winsys if we have already created one for this device. */
377 aws = util_hash_table_get(dev_tab, dev);
378 if (aws) {
379 struct amdgpu_screen_winsys *sws_iter;
380
381 /* Release the device handle, because we don't need it anymore.
382 * This function is returning an existing winsys instance, which
383 * has its own device handle.
384 */
385 amdgpu_device_deinitialize(dev);
386
387 simple_mtx_lock(&aws->sws_list_lock);
388 for (sws_iter = aws->sws_list; sws_iter; sws_iter = sws_iter->next) {
389 r = os_same_file_description(sws_iter->fd, ws->fd);
390
391 if (r == 0) {
392 close(ws->fd);
393 FREE(ws);
394 ws = sws_iter;
395 pipe_reference(NULL, &ws->reference);
396 simple_mtx_unlock(&aws->sws_list_lock);
397 goto unlock;
398 } else if (r < 0) {
399 static bool logged;
400
401 if (!logged) {
402 os_log_message("amdgpu: os_same_file_description couldn't "
403 "determine if two DRM fds reference the same "
404 "file description.\n"
405 "If they do, bad things may happen!\n");
406 logged = true;
407 }
408 }
409 }
410 simple_mtx_unlock(&aws->sws_list_lock);
411
412 ws->kms_handles = _mesa_hash_table_create(NULL, kms_handle_hash,
413 kms_handle_equals);
414 if (!ws->kms_handles)
415 goto fail;
416
417 pipe_reference(NULL, &aws->reference);
418 } else {
419 /* Create a new winsys. */
420 aws = CALLOC_STRUCT(amdgpu_winsys);
421 if (!aws)
422 goto fail;
423
424 aws->dev = dev;
425 aws->fd = ws->fd;
426 aws->info.drm_major = drm_major;
427 aws->info.drm_minor = drm_minor;
428
429 if (!do_winsys_init(aws, config, fd))
430 goto fail_alloc;
431
432 /* Create managers. */
433 pb_cache_init(&aws->bo_cache, RADEON_MAX_CACHED_HEAPS,
434 500000, aws->check_vm ? 1.0f : 2.0f, 0,
435 (aws->info.vram_size + aws->info.gart_size) / 8,
436 amdgpu_bo_destroy, amdgpu_bo_can_reclaim);
437
438 unsigned min_slab_order = 9; /* 512 bytes */
439 unsigned max_slab_order = 18; /* 256 KB - higher numbers increase memory usage */
440 unsigned num_slab_orders_per_allocator = (max_slab_order - min_slab_order) /
441 NUM_SLAB_ALLOCATORS;
442
443 /* Divide the size order range among slab managers. */
444 for (unsigned i = 0; i < NUM_SLAB_ALLOCATORS; i++) {
445 unsigned min_order = min_slab_order;
446 unsigned max_order = MIN2(min_order + num_slab_orders_per_allocator,
447 max_slab_order);
448
449 if (!pb_slabs_init(&aws->bo_slabs[i],
450 min_order, max_order,
451 RADEON_MAX_SLAB_HEAPS,
452 aws,
453 amdgpu_bo_can_reclaim_slab,
454 amdgpu_bo_slab_alloc,
455 amdgpu_bo_slab_free)) {
456 amdgpu_winsys_destroy(&ws->base);
457 simple_mtx_unlock(&dev_tab_mutex);
458 return NULL;
459 }
460
461 min_slab_order = max_order + 1;
462 }
463
464 aws->info.min_alloc_size = 1 << aws->bo_slabs[0].min_order;
465
466 /* init reference */
467 pipe_reference_init(&aws->reference, 1);
468
469 list_inithead(&aws->global_bo_list);
470 aws->bo_export_table = util_hash_table_create_ptr_keys();
471
472 (void) simple_mtx_init(&aws->sws_list_lock, mtx_plain);
473 (void) simple_mtx_init(&aws->global_bo_list_lock, mtx_plain);
474 (void) simple_mtx_init(&aws->bo_fence_lock, mtx_plain);
475 (void) simple_mtx_init(&aws->bo_export_table_lock, mtx_plain);
476
477 if (!util_queue_init(&aws->cs_queue, "cs", 8, 1,
478 UTIL_QUEUE_INIT_RESIZE_IF_FULL)) {
479 amdgpu_winsys_destroy(&ws->base);
480 simple_mtx_unlock(&dev_tab_mutex);
481 return NULL;
482 }
483
484 _mesa_hash_table_insert(dev_tab, dev, aws);
485
486 if (aws->reserve_vmid) {
487 r = amdgpu_vm_reserve_vmid(dev, 0);
488 if (r) {
489 amdgpu_winsys_destroy(&ws->base);
490 simple_mtx_unlock(&dev_tab_mutex);
491 return NULL;
492 }
493 }
494 }
495
496 ws->aws = aws;
497
498 /* Set functions. */
499 ws->base.unref = amdgpu_winsys_unref;
500 ws->base.destroy = amdgpu_winsys_destroy;
501 ws->base.query_info = amdgpu_winsys_query_info;
502 ws->base.cs_request_feature = amdgpu_cs_request_feature;
503 ws->base.query_value = amdgpu_query_value;
504 ws->base.read_registers = amdgpu_read_registers;
505 ws->base.pin_threads_to_L3_cache = amdgpu_pin_threads_to_L3_cache;
506 ws->base.ws_is_secure = amdgpu_ws_is_secure;
507 ws->base.cs_is_secure = amdgpu_cs_is_secure;
508 ws->base.cs_set_secure = amdgpu_cs_set_secure;
509
510 amdgpu_bo_init_functions(ws);
511 amdgpu_cs_init_functions(ws);
512 amdgpu_surface_init_functions(ws);
513
514 /* Create the screen at the end. The winsys must be initialized
515 * completely.
516 *
517 * Alternatively, we could create the screen based on "ws->gen"
518 * and link all drivers into one binary blob. */
519 ws->base.screen = screen_create(&ws->base, config);
520 if (!ws->base.screen) {
521 amdgpu_winsys_destroy(&ws->base);
522 simple_mtx_unlock(&dev_tab_mutex);
523 return NULL;
524 }
525
526 simple_mtx_lock(&aws->sws_list_lock);
527 ws->next = aws->sws_list;
528 aws->sws_list = ws;
529 simple_mtx_unlock(&aws->sws_list_lock);
530
531 unlock:
532 /* We must unlock the mutex once the winsys is fully initialized, so that
533 * other threads attempting to create the winsys from the same fd will
534 * get a fully initialized winsys and not just half-way initialized. */
535 simple_mtx_unlock(&dev_tab_mutex);
536
537 return &ws->base;
538
539 fail_alloc:
540 FREE(aws);
541 fail:
542 if (ws->kms_handles)
543 _mesa_hash_table_destroy(ws->kms_handles, NULL);
544 close(ws->fd);
545 FREE(ws);
546 simple_mtx_unlock(&dev_tab_mutex);
547 return NULL;
548 }