util: remove LIST_INITHEAD macro
[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/u_cpu_detect.h"
34 #include "util/u_hash_table.h"
35 #include "util/hash_table.h"
36 #include "util/xmlconfig.h"
37 #include <amdgpu_drm.h>
38 #include <xf86drm.h>
39 #include <stdio.h>
40 #include <sys/stat.h>
41 #include <fcntl.h>
42 #include "ac_llvm_util.h"
43 #include "sid.h"
44
45 #ifndef AMDGPU_INFO_NUM_VRAM_CPU_PAGE_FAULTS
46 #define AMDGPU_INFO_NUM_VRAM_CPU_PAGE_FAULTS 0x1E
47 #endif
48
49 static struct util_hash_table *dev_tab = NULL;
50 static simple_mtx_t dev_tab_mutex = _SIMPLE_MTX_INITIALIZER_NP;
51
52 DEBUG_GET_ONCE_BOOL_OPTION(all_bos, "RADEON_ALL_BOS", false)
53
54 static void handle_env_var_force_family(struct amdgpu_winsys *ws)
55 {
56 const char *family = debug_get_option("SI_FORCE_FAMILY", NULL);
57 unsigned i;
58
59 if (!family)
60 return;
61
62 for (i = CHIP_TAHITI; i < CHIP_LAST; i++) {
63 if (!strcmp(family, ac_get_llvm_processor_name(i))) {
64 /* Override family and chip_class. */
65 ws->info.family = i;
66 ws->info.name = "GCN-NOOP";
67
68 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. */
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 = amdgpu_addr_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
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 util_hash_table_destroy(ws->bo_export_table);
141 simple_mtx_destroy(&ws->global_bo_list_lock);
142 simple_mtx_destroy(&ws->bo_export_table_lock);
143
144 AddrDestroy(ws->addrlib);
145 amdgpu_device_deinitialize(ws->dev);
146 FREE(ws);
147 }
148
149 static void amdgpu_winsys_destroy(struct radeon_winsys *rws)
150 {
151 struct amdgpu_screen_winsys *sws = amdgpu_screen_winsys(rws);
152 struct amdgpu_winsys *ws = sws->aws;
153 bool destroy;
154
155 /* When the reference counter drops to zero, remove the device pointer
156 * from the table.
157 * This must happen while the mutex is locked, so that
158 * amdgpu_winsys_create in another thread doesn't get the winsys
159 * from the table when the counter drops to 0.
160 */
161 simple_mtx_lock(&dev_tab_mutex);
162
163 destroy = pipe_reference(&ws->reference, NULL);
164 if (destroy && dev_tab) {
165 util_hash_table_remove(dev_tab, ws->dev);
166 if (util_hash_table_count(dev_tab) == 0) {
167 util_hash_table_destroy(dev_tab);
168 dev_tab = NULL;
169 }
170 }
171
172 simple_mtx_unlock(&dev_tab_mutex);
173
174 if (destroy)
175 do_winsys_deinit(ws);
176
177 close(sws->fd);
178 FREE(rws);
179 }
180
181 static void amdgpu_winsys_query_info(struct radeon_winsys *rws,
182 struct radeon_info *info)
183 {
184 *info = amdgpu_winsys(rws)->info;
185 }
186
187 static bool amdgpu_cs_request_feature(struct radeon_cmdbuf *rcs,
188 enum radeon_feature_id fid,
189 bool enable)
190 {
191 return false;
192 }
193
194 static uint64_t amdgpu_query_value(struct radeon_winsys *rws,
195 enum radeon_value_id value)
196 {
197 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
198 struct amdgpu_heap_info heap;
199 uint64_t retval = 0;
200
201 switch (value) {
202 case RADEON_REQUESTED_VRAM_MEMORY:
203 return ws->allocated_vram;
204 case RADEON_REQUESTED_GTT_MEMORY:
205 return ws->allocated_gtt;
206 case RADEON_MAPPED_VRAM:
207 return ws->mapped_vram;
208 case RADEON_MAPPED_GTT:
209 return ws->mapped_gtt;
210 case RADEON_BUFFER_WAIT_TIME_NS:
211 return ws->buffer_wait_time;
212 case RADEON_NUM_MAPPED_BUFFERS:
213 return ws->num_mapped_buffers;
214 case RADEON_TIMESTAMP:
215 amdgpu_query_info(ws->dev, AMDGPU_INFO_TIMESTAMP, 8, &retval);
216 return retval;
217 case RADEON_NUM_GFX_IBS:
218 return ws->num_gfx_IBs;
219 case RADEON_NUM_SDMA_IBS:
220 return ws->num_sdma_IBs;
221 case RADEON_GFX_BO_LIST_COUNTER:
222 return ws->gfx_bo_list_counter;
223 case RADEON_GFX_IB_SIZE_COUNTER:
224 return ws->gfx_ib_size_counter;
225 case RADEON_NUM_BYTES_MOVED:
226 amdgpu_query_info(ws->dev, AMDGPU_INFO_NUM_BYTES_MOVED, 8, &retval);
227 return retval;
228 case RADEON_NUM_EVICTIONS:
229 amdgpu_query_info(ws->dev, AMDGPU_INFO_NUM_EVICTIONS, 8, &retval);
230 return retval;
231 case RADEON_NUM_VRAM_CPU_PAGE_FAULTS:
232 amdgpu_query_info(ws->dev, AMDGPU_INFO_NUM_VRAM_CPU_PAGE_FAULTS, 8, &retval);
233 return retval;
234 case RADEON_VRAM_USAGE:
235 amdgpu_query_heap_info(ws->dev, AMDGPU_GEM_DOMAIN_VRAM, 0, &heap);
236 return heap.heap_usage;
237 case RADEON_VRAM_VIS_USAGE:
238 amdgpu_query_heap_info(ws->dev, AMDGPU_GEM_DOMAIN_VRAM,
239 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED, &heap);
240 return heap.heap_usage;
241 case RADEON_GTT_USAGE:
242 amdgpu_query_heap_info(ws->dev, AMDGPU_GEM_DOMAIN_GTT, 0, &heap);
243 return heap.heap_usage;
244 case RADEON_GPU_TEMPERATURE:
245 amdgpu_query_sensor_info(ws->dev, AMDGPU_INFO_SENSOR_GPU_TEMP, 4, &retval);
246 return retval;
247 case RADEON_CURRENT_SCLK:
248 amdgpu_query_sensor_info(ws->dev, AMDGPU_INFO_SENSOR_GFX_SCLK, 4, &retval);
249 return retval;
250 case RADEON_CURRENT_MCLK:
251 amdgpu_query_sensor_info(ws->dev, AMDGPU_INFO_SENSOR_GFX_MCLK, 4, &retval);
252 return retval;
253 case RADEON_CS_THREAD_TIME:
254 return util_queue_get_thread_time_nano(&ws->cs_queue, 0);
255 }
256 return 0;
257 }
258
259 static bool amdgpu_read_registers(struct radeon_winsys *rws,
260 unsigned reg_offset,
261 unsigned num_registers, uint32_t *out)
262 {
263 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
264
265 return amdgpu_read_mm_registers(ws->dev, reg_offset / 4, num_registers,
266 0xffffffff, 0, out) == 0;
267 }
268
269 static unsigned hash_pointer(void *key)
270 {
271 return _mesa_hash_pointer(key);
272 }
273
274 static int compare_pointers(void *key1, void *key2)
275 {
276 return key1 != key2;
277 }
278
279 static bool amdgpu_winsys_unref(struct radeon_winsys *rws)
280 {
281 /* radeon_winsys corresponds to amdgpu_screen_winsys, which is never
282 * referenced multiple times, so amdgpu_winsys_destroy always needs to be
283 * called. It handles reference counting for amdgpu_winsys.
284 */
285 return true;
286 }
287
288 static void amdgpu_pin_threads_to_L3_cache(struct radeon_winsys *rws,
289 unsigned cache)
290 {
291 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
292
293 util_pin_thread_to_L3(ws->cs_queue.threads[0], cache,
294 util_cpu_caps.cores_per_L3);
295 }
296
297 PUBLIC struct radeon_winsys *
298 amdgpu_winsys_create(int fd, const struct pipe_screen_config *config,
299 radeon_screen_create_t screen_create)
300 {
301 struct amdgpu_screen_winsys *ws;
302 struct amdgpu_winsys *aws;
303 amdgpu_device_handle dev;
304 uint32_t drm_major, drm_minor, r;
305
306 ws = CALLOC_STRUCT(amdgpu_screen_winsys);
307 if (!ws)
308 return NULL;
309
310 ws->fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
311
312 /* Look up the winsys from the dev table. */
313 simple_mtx_lock(&dev_tab_mutex);
314 if (!dev_tab)
315 dev_tab = util_hash_table_create(hash_pointer, compare_pointers);
316
317 /* Initialize the amdgpu device. This should always return the same pointer
318 * for the same fd. */
319 r = amdgpu_device_initialize(fd, &drm_major, &drm_minor, &dev);
320 if (r) {
321 fprintf(stderr, "amdgpu: amdgpu_device_initialize failed.\n");
322 goto fail;
323 }
324
325 /* Lookup a winsys if we have already created one for this device. */
326 aws = util_hash_table_get(dev_tab, dev);
327 if (aws) {
328 pipe_reference(NULL, &aws->reference);
329 simple_mtx_unlock(&dev_tab_mutex);
330
331 /* Release the device handle, because we don't need it anymore.
332 * This function is returning an existing winsys instance, which
333 * has its own device handle.
334 */
335 amdgpu_device_deinitialize(dev);
336 } else {
337 /* Create a new winsys. */
338 aws = CALLOC_STRUCT(amdgpu_winsys);
339 if (!aws)
340 goto fail;
341
342 aws->dev = dev;
343 aws->info.drm_major = drm_major;
344 aws->info.drm_minor = drm_minor;
345
346 if (!do_winsys_init(aws, config, fd))
347 goto fail_alloc;
348
349 /* Create managers. */
350 pb_cache_init(&aws->bo_cache, RADEON_MAX_CACHED_HEAPS,
351 500000, aws->check_vm ? 1.0f : 2.0f, 0,
352 (aws->info.vram_size + aws->info.gart_size) / 8,
353 amdgpu_bo_destroy, amdgpu_bo_can_reclaim);
354
355 unsigned min_slab_order = 9; /* 512 bytes */
356 unsigned max_slab_order = 18; /* 256 KB - higher numbers increase memory usage */
357 unsigned num_slab_orders_per_allocator = (max_slab_order - min_slab_order) /
358 NUM_SLAB_ALLOCATORS;
359
360 /* Divide the size order range among slab managers. */
361 for (unsigned i = 0; i < NUM_SLAB_ALLOCATORS; i++) {
362 unsigned min_order = min_slab_order;
363 unsigned max_order = MIN2(min_order + num_slab_orders_per_allocator,
364 max_slab_order);
365
366 if (!pb_slabs_init(&aws->bo_slabs[i],
367 min_order, max_order,
368 RADEON_MAX_SLAB_HEAPS,
369 aws,
370 amdgpu_bo_can_reclaim_slab,
371 amdgpu_bo_slab_alloc,
372 amdgpu_bo_slab_free)) {
373 amdgpu_winsys_destroy(&ws->base);
374 simple_mtx_unlock(&dev_tab_mutex);
375 return NULL;
376 }
377
378 min_slab_order = max_order + 1;
379 }
380
381 aws->info.min_alloc_size = 1 << aws->bo_slabs[0].min_order;
382
383 /* init reference */
384 pipe_reference_init(&aws->reference, 1);
385
386 list_inithead(&aws->global_bo_list);
387 aws->bo_export_table = util_hash_table_create(hash_pointer, compare_pointers);
388
389 (void) simple_mtx_init(&aws->global_bo_list_lock, mtx_plain);
390 (void) simple_mtx_init(&aws->bo_fence_lock, mtx_plain);
391 (void) simple_mtx_init(&aws->bo_export_table_lock, mtx_plain);
392
393 if (!util_queue_init(&aws->cs_queue, "cs", 8, 1,
394 UTIL_QUEUE_INIT_RESIZE_IF_FULL)) {
395 amdgpu_winsys_destroy(&ws->base);
396 simple_mtx_unlock(&dev_tab_mutex);
397 return NULL;
398 }
399
400 util_hash_table_set(dev_tab, dev, aws);
401
402 if (aws->reserve_vmid) {
403 r = amdgpu_vm_reserve_vmid(dev, 0);
404 if (r) {
405 amdgpu_winsys_destroy(&ws->base);
406 simple_mtx_unlock(&dev_tab_mutex);
407 return NULL;
408 }
409 }
410 }
411
412 ws->aws = aws;
413
414 /* Set functions. */
415 ws->base.unref = amdgpu_winsys_unref;
416 ws->base.destroy = amdgpu_winsys_destroy;
417 ws->base.query_info = amdgpu_winsys_query_info;
418 ws->base.cs_request_feature = amdgpu_cs_request_feature;
419 ws->base.query_value = amdgpu_query_value;
420 ws->base.read_registers = amdgpu_read_registers;
421 ws->base.pin_threads_to_L3_cache = amdgpu_pin_threads_to_L3_cache;
422
423 amdgpu_bo_init_functions(ws);
424 amdgpu_cs_init_functions(ws);
425 amdgpu_surface_init_functions(ws);
426
427 /* Create the screen at the end. The winsys must be initialized
428 * completely.
429 *
430 * Alternatively, we could create the screen based on "ws->gen"
431 * and link all drivers into one binary blob. */
432 ws->base.screen = screen_create(&ws->base, config);
433 if (!ws->base.screen) {
434 amdgpu_winsys_destroy(&ws->base);
435 simple_mtx_unlock(&dev_tab_mutex);
436 return NULL;
437 }
438
439 /* We must unlock the mutex once the winsys is fully initialized, so that
440 * other threads attempting to create the winsys from the same fd will
441 * get a fully initialized winsys and not just half-way initialized. */
442 simple_mtx_unlock(&dev_tab_mutex);
443
444 return &ws->base;
445
446 fail_alloc:
447 FREE(aws);
448 fail:
449 close(ws->fd);
450 FREE(ws);
451 simple_mtx_unlock(&dev_tab_mutex);
452 return NULL;
453 }