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
handle_env_var_force_family(struct amdgpu_winsys * ws)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_SIENNA_CICHLID)
67 ws->info.chip_class = GFX10_3;
68 else if (i >= CHIP_NAVI10)
69 ws->info.chip_class = GFX10;
70 else if (i >= CHIP_VEGA10)
71 ws->info.chip_class = GFX9;
72 else if (i >= CHIP_TONGA)
73 ws->info.chip_class = GFX8;
74 else if (i >= CHIP_BONAIRE)
75 ws->info.chip_class = GFX7;
76 else
77 ws->info.chip_class = GFX6;
78
79 /* Don't submit any IBs. */
80 setenv("RADEON_NOOP", "1", 1);
81 return;
82 }
83 }
84
85 fprintf(stderr, "radeonsi: Unknown family: %s\n", family);
86 exit(1);
87 }
88
89 /* Helper function to do the ioctls needed for setup and init. */
do_winsys_init(struct amdgpu_winsys * ws,const struct pipe_screen_config * config,int fd)90 static bool do_winsys_init(struct amdgpu_winsys *ws,
91 const struct pipe_screen_config *config,
92 int fd)
93 {
94 if (!ac_query_gpu_info(fd, ws->dev, &ws->info, &ws->amdinfo))
95 goto fail;
96
97 /* TODO: Enable this once the kernel handles it efficiently. */
98 if (ws->info.has_dedicated_vram)
99 ws->info.has_local_buffers = false;
100
101 handle_env_var_force_family(ws);
102
103 ws->addrlib = ac_addrlib_create(&ws->info, &ws->amdinfo, &ws->info.max_alignment);
104 if (!ws->addrlib) {
105 fprintf(stderr, "amdgpu: Cannot create addrlib.\n");
106 goto fail;
107 }
108
109 ws->check_vm = strstr(debug_get_option("R600_DEBUG", ""), "check_vm") != NULL ||
110 strstr(debug_get_option("AMD_DEBUG", ""), "check_vm") != NULL;
111 ws->debug_all_bos = debug_get_option_all_bos();
112 ws->reserve_vmid = strstr(debug_get_option("R600_DEBUG", ""), "reserve_vmid") != NULL ||
113 strstr(debug_get_option("AMD_DEBUG", ""), "reserve_vmid") != NULL;
114 ws->zero_all_vram_allocs = strstr(debug_get_option("R600_DEBUG", ""), "zerovram") != NULL ||
115 strstr(debug_get_option("AMD_DEBUG", ""), "zerovram") != NULL ||
116 driQueryOptionb(config->options, "radeonsi_zerovram");
117
118 return true;
119
120 fail:
121 amdgpu_device_deinitialize(ws->dev);
122 ws->dev = NULL;
123 return false;
124 }
125
do_winsys_deinit(struct amdgpu_winsys * ws)126 static void do_winsys_deinit(struct amdgpu_winsys *ws)
127 {
128 if (ws->reserve_vmid)
129 amdgpu_vm_unreserve_vmid(ws->dev, 0);
130
131 if (util_queue_is_initialized(&ws->cs_queue))
132 util_queue_destroy(&ws->cs_queue);
133
134 simple_mtx_destroy(&ws->bo_fence_lock);
135 for (unsigned i = 0; i < NUM_SLAB_ALLOCATORS; i++) {
136 if (ws->bo_slabs[i].groups)
137 pb_slabs_deinit(&ws->bo_slabs[i]);
138 }
139 pb_cache_deinit(&ws->bo_cache);
140 _mesa_hash_table_destroy(ws->bo_export_table, NULL);
141 simple_mtx_destroy(&ws->sws_list_lock);
142 simple_mtx_destroy(&ws->global_bo_list_lock);
143 simple_mtx_destroy(&ws->bo_export_table_lock);
144
145 ac_addrlib_destroy(ws->addrlib);
146 amdgpu_device_deinitialize(ws->dev);
147 FREE(ws);
148 }
149
amdgpu_winsys_destroy(struct radeon_winsys * rws)150 static void amdgpu_winsys_destroy(struct radeon_winsys *rws)
151 {
152 struct amdgpu_screen_winsys *sws = amdgpu_screen_winsys(rws);
153 struct amdgpu_winsys *ws = sws->aws;
154 bool destroy;
155
156 /* When the reference counter drops to zero, remove the device pointer
157 * from the table.
158 * This must happen while the mutex is locked, so that
159 * amdgpu_winsys_create in another thread doesn't get the winsys
160 * from the table when the counter drops to 0.
161 */
162 simple_mtx_lock(&dev_tab_mutex);
163
164 destroy = pipe_reference(&ws->reference, NULL);
165 if (destroy && dev_tab) {
166 _mesa_hash_table_remove_key(dev_tab, ws->dev);
167 if (_mesa_hash_table_num_entries(dev_tab) == 0) {
168 _mesa_hash_table_destroy(dev_tab, NULL);
169 dev_tab = NULL;
170 }
171 }
172
173 simple_mtx_unlock(&dev_tab_mutex);
174
175 if (destroy)
176 do_winsys_deinit(ws);
177
178 close(sws->fd);
179 FREE(rws);
180 }
181
amdgpu_winsys_query_info(struct radeon_winsys * rws,struct radeon_info * info)182 static void amdgpu_winsys_query_info(struct radeon_winsys *rws,
183 struct radeon_info *info)
184 {
185 *info = amdgpu_winsys(rws)->info;
186 }
187
amdgpu_cs_request_feature(struct radeon_cmdbuf * rcs,enum radeon_feature_id fid,bool enable)188 static bool amdgpu_cs_request_feature(struct radeon_cmdbuf *rcs,
189 enum radeon_feature_id fid,
190 bool enable)
191 {
192 return false;
193 }
194
amdgpu_query_value(struct radeon_winsys * rws,enum radeon_value_id value)195 static uint64_t amdgpu_query_value(struct radeon_winsys *rws,
196 enum radeon_value_id value)
197 {
198 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
199 struct amdgpu_heap_info heap;
200 uint64_t retval = 0;
201
202 switch (value) {
203 case RADEON_REQUESTED_VRAM_MEMORY:
204 return ws->allocated_vram;
205 case RADEON_REQUESTED_GTT_MEMORY:
206 return ws->allocated_gtt;
207 case RADEON_MAPPED_VRAM:
208 return ws->mapped_vram;
209 case RADEON_MAPPED_GTT:
210 return ws->mapped_gtt;
211 case RADEON_BUFFER_WAIT_TIME_NS:
212 return ws->buffer_wait_time;
213 case RADEON_NUM_MAPPED_BUFFERS:
214 return ws->num_mapped_buffers;
215 case RADEON_TIMESTAMP:
216 amdgpu_query_info(ws->dev, AMDGPU_INFO_TIMESTAMP, 8, &retval);
217 return retval;
218 case RADEON_NUM_GFX_IBS:
219 return ws->num_gfx_IBs;
220 case RADEON_NUM_SDMA_IBS:
221 return ws->num_sdma_IBs;
222 case RADEON_GFX_BO_LIST_COUNTER:
223 return ws->gfx_bo_list_counter;
224 case RADEON_GFX_IB_SIZE_COUNTER:
225 return ws->gfx_ib_size_counter;
226 case RADEON_NUM_BYTES_MOVED:
227 amdgpu_query_info(ws->dev, AMDGPU_INFO_NUM_BYTES_MOVED, 8, &retval);
228 return retval;
229 case RADEON_NUM_EVICTIONS:
230 amdgpu_query_info(ws->dev, AMDGPU_INFO_NUM_EVICTIONS, 8, &retval);
231 return retval;
232 case RADEON_NUM_VRAM_CPU_PAGE_FAULTS:
233 amdgpu_query_info(ws->dev, AMDGPU_INFO_NUM_VRAM_CPU_PAGE_FAULTS, 8, &retval);
234 return retval;
235 case RADEON_VRAM_USAGE:
236 amdgpu_query_heap_info(ws->dev, AMDGPU_GEM_DOMAIN_VRAM, 0, &heap);
237 return heap.heap_usage;
238 case RADEON_VRAM_VIS_USAGE:
239 amdgpu_query_heap_info(ws->dev, AMDGPU_GEM_DOMAIN_VRAM,
240 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED, &heap);
241 return heap.heap_usage;
242 case RADEON_GTT_USAGE:
243 amdgpu_query_heap_info(ws->dev, AMDGPU_GEM_DOMAIN_GTT, 0, &heap);
244 return heap.heap_usage;
245 case RADEON_GPU_TEMPERATURE:
246 amdgpu_query_sensor_info(ws->dev, AMDGPU_INFO_SENSOR_GPU_TEMP, 4, &retval);
247 return retval;
248 case RADEON_CURRENT_SCLK:
249 amdgpu_query_sensor_info(ws->dev, AMDGPU_INFO_SENSOR_GFX_SCLK, 4, &retval);
250 return retval;
251 case RADEON_CURRENT_MCLK:
252 amdgpu_query_sensor_info(ws->dev, AMDGPU_INFO_SENSOR_GFX_MCLK, 4, &retval);
253 return retval;
254 case RADEON_CS_THREAD_TIME:
255 return util_queue_get_thread_time_nano(&ws->cs_queue, 0);
256 }
257 return 0;
258 }
259
amdgpu_read_registers(struct radeon_winsys * rws,unsigned reg_offset,unsigned num_registers,uint32_t * out)260 static bool amdgpu_read_registers(struct radeon_winsys *rws,
261 unsigned reg_offset,
262 unsigned num_registers, uint32_t *out)
263 {
264 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
265
266 return amdgpu_read_mm_registers(ws->dev, reg_offset / 4, num_registers,
267 0xffffffff, 0, out) == 0;
268 }
269
amdgpu_winsys_unref(struct radeon_winsys * rws)270 static bool amdgpu_winsys_unref(struct radeon_winsys *rws)
271 {
272 struct amdgpu_screen_winsys *sws = amdgpu_screen_winsys(rws);
273 struct amdgpu_winsys *aws = sws->aws;
274 bool ret;
275
276 simple_mtx_lock(&aws->sws_list_lock);
277
278 ret = pipe_reference(&sws->reference, NULL);
279 if (ret) {
280 struct amdgpu_screen_winsys **sws_iter;
281 struct amdgpu_winsys *aws = sws->aws;
282
283 /* Remove this amdgpu_screen_winsys from amdgpu_winsys' list, so that
284 * amdgpu_winsys_create can't re-use it anymore
285 */
286 for (sws_iter = &aws->sws_list; *sws_iter; sws_iter = &(*sws_iter)->next) {
287 if (*sws_iter == sws) {
288 *sws_iter = sws->next;
289 break;
290 }
291 }
292 }
293
294 simple_mtx_unlock(&aws->sws_list_lock);
295
296 if (ret && sws->kms_handles) {
297 struct drm_gem_close args;
298
299 hash_table_foreach(sws->kms_handles, entry) {
300 args.handle = (uintptr_t)entry->data;
301 drmIoctl(sws->fd, DRM_IOCTL_GEM_CLOSE, &args);
302 }
303 _mesa_hash_table_destroy(sws->kms_handles, NULL);
304 }
305
306 return ret;
307 }
308
amdgpu_pin_threads_to_L3_cache(struct radeon_winsys * rws,unsigned cache)309 static void amdgpu_pin_threads_to_L3_cache(struct radeon_winsys *rws,
310 unsigned cache)
311 {
312 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
313
314 util_set_thread_affinity(ws->cs_queue.threads[0],
315 util_cpu_caps.L3_affinity_mask[cache],
316 NULL, UTIL_MAX_CPUS);
317 }
318
kms_handle_hash(const void * key)319 static uint32_t kms_handle_hash(const void *key)
320 {
321 const struct amdgpu_winsys_bo *bo = key;
322
323 return bo->u.real.kms_handle;
324 }
325
kms_handle_equals(const void * a,const void * b)326 static bool kms_handle_equals(const void *a, const void *b)
327 {
328 return a == b;
329 }
330
amdgpu_cs_is_secure(struct radeon_cmdbuf * rcs)331 static bool amdgpu_cs_is_secure(struct radeon_cmdbuf *rcs)
332 {
333 struct amdgpu_cs *cs = amdgpu_cs(rcs);
334 return cs->csc->secure;
335 }
336
337 PUBLIC struct radeon_winsys *
amdgpu_winsys_create(int fd,const struct pipe_screen_config * config,radeon_screen_create_t screen_create)338 amdgpu_winsys_create(int fd, const struct pipe_screen_config *config,
339 radeon_screen_create_t screen_create)
340 {
341 struct amdgpu_screen_winsys *ws;
342 struct amdgpu_winsys *aws;
343 amdgpu_device_handle dev;
344 uint32_t drm_major, drm_minor;
345 int r;
346
347 ws = CALLOC_STRUCT(amdgpu_screen_winsys);
348 if (!ws)
349 return NULL;
350
351 pipe_reference_init(&ws->reference, 1);
352 ws->fd = os_dupfd_cloexec(fd);
353
354 /* Look up the winsys from the dev table. */
355 simple_mtx_lock(&dev_tab_mutex);
356 if (!dev_tab)
357 dev_tab = util_hash_table_create_ptr_keys();
358
359 /* Initialize the amdgpu device. This should always return the same pointer
360 * for the same fd. */
361 r = amdgpu_device_initialize(ws->fd, &drm_major, &drm_minor, &dev);
362 if (r) {
363 fprintf(stderr, "amdgpu: amdgpu_device_initialize failed.\n");
364 goto fail;
365 }
366
367 /* Lookup a winsys if we have already created one for this device. */
368 aws = util_hash_table_get(dev_tab, dev);
369 if (aws) {
370 struct amdgpu_screen_winsys *sws_iter;
371
372 /* Release the device handle, because we don't need it anymore.
373 * This function is returning an existing winsys instance, which
374 * has its own device handle.
375 */
376 amdgpu_device_deinitialize(dev);
377
378 simple_mtx_lock(&aws->sws_list_lock);
379 for (sws_iter = aws->sws_list; sws_iter; sws_iter = sws_iter->next) {
380 r = os_same_file_description(sws_iter->fd, ws->fd);
381
382 if (r == 0) {
383 close(ws->fd);
384 FREE(ws);
385 ws = sws_iter;
386 pipe_reference(NULL, &ws->reference);
387 simple_mtx_unlock(&aws->sws_list_lock);
388 goto unlock;
389 } else if (r < 0) {
390 static bool logged;
391
392 if (!logged) {
393 os_log_message("amdgpu: os_same_file_description couldn't "
394 "determine if two DRM fds reference the same "
395 "file description.\n"
396 "If they do, bad things may happen!\n");
397 logged = true;
398 }
399 }
400 }
401 simple_mtx_unlock(&aws->sws_list_lock);
402
403 ws->kms_handles = _mesa_hash_table_create(NULL, kms_handle_hash,
404 kms_handle_equals);
405 if (!ws->kms_handles)
406 goto fail;
407
408 pipe_reference(NULL, &aws->reference);
409 } else {
410 /* Create a new winsys. */
411 aws = CALLOC_STRUCT(amdgpu_winsys);
412 if (!aws)
413 goto fail;
414
415 aws->dev = dev;
416 aws->fd = ws->fd;
417 aws->info.drm_major = drm_major;
418 aws->info.drm_minor = drm_minor;
419
420 if (!do_winsys_init(aws, config, fd))
421 goto fail_alloc;
422
423 /* Create managers. */
424 pb_cache_init(&aws->bo_cache, RADEON_MAX_CACHED_HEAPS,
425 500000, aws->check_vm ? 1.0f : 2.0f, 0,
426 (aws->info.vram_size + aws->info.gart_size) / 8,
427 amdgpu_bo_destroy, amdgpu_bo_can_reclaim);
428
429 unsigned min_slab_order = 9; /* 512 bytes */
430 unsigned max_slab_order = 18; /* 256 KB - higher numbers increase memory usage */
431 unsigned num_slab_orders_per_allocator = (max_slab_order - min_slab_order) /
432 NUM_SLAB_ALLOCATORS;
433
434 /* Divide the size order range among slab managers. */
435 for (unsigned i = 0; i < NUM_SLAB_ALLOCATORS; i++) {
436 unsigned min_order = min_slab_order;
437 unsigned max_order = MIN2(min_order + num_slab_orders_per_allocator,
438 max_slab_order);
439
440 if (!pb_slabs_init(&aws->bo_slabs[i],
441 min_order, max_order,
442 RADEON_MAX_SLAB_HEAPS,
443 aws,
444 amdgpu_bo_can_reclaim_slab,
445 amdgpu_bo_slab_alloc_normal,
446 amdgpu_bo_slab_free)) {
447 amdgpu_winsys_destroy(&ws->base);
448 simple_mtx_unlock(&dev_tab_mutex);
449 return NULL;
450 }
451
452 if (aws->info.has_tmz_support &&
453 !pb_slabs_init(&aws->bo_slabs_encrypted[i],
454 min_order, max_order,
455 RADEON_MAX_SLAB_HEAPS,
456 aws,
457 amdgpu_bo_can_reclaim_slab,
458 amdgpu_bo_slab_alloc_encrypted,
459 amdgpu_bo_slab_free)) {
460 amdgpu_winsys_destroy(&ws->base);
461 simple_mtx_unlock(&dev_tab_mutex);
462 return NULL;
463 }
464
465 min_slab_order = max_order + 1;
466 }
467
468 aws->info.min_alloc_size = 1 << aws->bo_slabs[0].min_order;
469
470 /* init reference */
471 pipe_reference_init(&aws->reference, 1);
472
473 list_inithead(&aws->global_bo_list);
474 aws->bo_export_table = util_hash_table_create_ptr_keys();
475
476 (void) simple_mtx_init(&aws->sws_list_lock, mtx_plain);
477 (void) simple_mtx_init(&aws->global_bo_list_lock, mtx_plain);
478 (void) simple_mtx_init(&aws->bo_fence_lock, mtx_plain);
479 (void) simple_mtx_init(&aws->bo_export_table_lock, mtx_plain);
480
481 if (!util_queue_init(&aws->cs_queue, "cs", 8, 1,
482 UTIL_QUEUE_INIT_RESIZE_IF_FULL)) {
483 amdgpu_winsys_destroy(&ws->base);
484 simple_mtx_unlock(&dev_tab_mutex);
485 return NULL;
486 }
487
488 _mesa_hash_table_insert(dev_tab, dev, aws);
489
490 if (aws->reserve_vmid) {
491 r = amdgpu_vm_reserve_vmid(dev, 0);
492 if (r) {
493 amdgpu_winsys_destroy(&ws->base);
494 simple_mtx_unlock(&dev_tab_mutex);
495 return NULL;
496 }
497 }
498 }
499
500 ws->aws = aws;
501
502 /* Set functions. */
503 ws->base.unref = amdgpu_winsys_unref;
504 ws->base.destroy = amdgpu_winsys_destroy;
505 ws->base.query_info = amdgpu_winsys_query_info;
506 ws->base.cs_request_feature = amdgpu_cs_request_feature;
507 ws->base.query_value = amdgpu_query_value;
508 ws->base.read_registers = amdgpu_read_registers;
509 ws->base.pin_threads_to_L3_cache = amdgpu_pin_threads_to_L3_cache;
510 ws->base.cs_is_secure = amdgpu_cs_is_secure;
511
512 amdgpu_bo_init_functions(ws);
513 amdgpu_cs_init_functions(ws);
514 amdgpu_surface_init_functions(ws);
515
516 /* Create the screen at the end. The winsys must be initialized
517 * completely.
518 *
519 * Alternatively, we could create the screen based on "ws->gen"
520 * and link all drivers into one binary blob. */
521 ws->base.screen = screen_create(&ws->base, config);
522 if (!ws->base.screen) {
523 amdgpu_winsys_destroy(&ws->base);
524 simple_mtx_unlock(&dev_tab_mutex);
525 return NULL;
526 }
527
528 simple_mtx_lock(&aws->sws_list_lock);
529 ws->next = aws->sws_list;
530 aws->sws_list = ws;
531 simple_mtx_unlock(&aws->sws_list_lock);
532
533 unlock:
534 /* We must unlock the mutex once the winsys is fully initialized, so that
535 * other threads attempting to create the winsys from the same fd will
536 * get a fully initialized winsys and not just half-way initialized. */
537 simple_mtx_unlock(&dev_tab_mutex);
538
539 return &ws->base;
540
541 fail_alloc:
542 FREE(aws);
543 fail:
544 if (ws->kms_handles)
545 _mesa_hash_table_destroy(ws->kms_handles, NULL);
546 close(ws->fd);
547 FREE(ws);
548 simple_mtx_unlock(&dev_tab_mutex);
549 return NULL;
550 }
551