winsys/radeon: fix a race condition between winsys_create and winsys_destroy
[mesa.git] / src / gallium / winsys / radeon / drm / radeon_drm_winsys.c
1 /*
2 * Copyright © 2009 Corbin Simpson
3 * Copyright © 2011 Marek Olšák <maraeo@gmail.com>
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
18 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
25 * of the Software.
26 */
27 /*
28 * Authors:
29 * Corbin Simpson <MostAwesomeDude@gmail.com>
30 * Joakim Sindholt <opensource@zhasha.com>
31 * Marek Olšák <maraeo@gmail.com>
32 */
33
34 #include "radeon_drm_bo.h"
35 #include "radeon_drm_cs.h"
36 #include "radeon_drm_public.h"
37
38 #include "pipebuffer/pb_bufmgr.h"
39 #include "util/u_memory.h"
40 #include "util/u_hash_table.h"
41
42 #include <xf86drm.h>
43 #include <stdio.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <unistd.h>
47
48 static struct util_hash_table *fd_tab = NULL;
49 pipe_static_mutex(fd_tab_mutex);
50
51 /* Enable/disable feature access for one command stream.
52 * If enable == TRUE, return TRUE on success.
53 * Otherwise, return FALSE.
54 *
55 * We basically do the same thing kernel does, because we have to deal
56 * with multiple contexts (here command streams) backed by one winsys. */
57 static boolean radeon_set_fd_access(struct radeon_drm_cs *applier,
58 struct radeon_drm_cs **owner,
59 pipe_mutex *mutex,
60 unsigned request, const char *request_name,
61 boolean enable)
62 {
63 struct drm_radeon_info info;
64 unsigned value = enable ? 1 : 0;
65
66 memset(&info, 0, sizeof(info));
67
68 pipe_mutex_lock(*mutex);
69
70 /* Early exit if we are sure the request will fail. */
71 if (enable) {
72 if (*owner) {
73 pipe_mutex_unlock(*mutex);
74 return FALSE;
75 }
76 } else {
77 if (*owner != applier) {
78 pipe_mutex_unlock(*mutex);
79 return FALSE;
80 }
81 }
82
83 /* Pass through the request to the kernel. */
84 info.value = (unsigned long)&value;
85 info.request = request;
86 if (drmCommandWriteRead(applier->ws->fd, DRM_RADEON_INFO,
87 &info, sizeof(info)) != 0) {
88 pipe_mutex_unlock(*mutex);
89 return FALSE;
90 }
91
92 /* Update the rights in the winsys. */
93 if (enable) {
94 if (value) {
95 *owner = applier;
96 printf("radeon: Acquired access to %s.\n", request_name);
97 pipe_mutex_unlock(*mutex);
98 return TRUE;
99 }
100 } else {
101 *owner = NULL;
102 printf("radeon: Released access to %s.\n", request_name);
103 }
104
105 pipe_mutex_unlock(*mutex);
106 return FALSE;
107 }
108
109 static boolean radeon_get_drm_value(int fd, unsigned request,
110 const char *errname, uint32_t *out)
111 {
112 struct drm_radeon_info info;
113 int retval;
114
115 memset(&info, 0, sizeof(info));
116
117 info.value = (unsigned long)out;
118 info.request = request;
119
120 retval = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
121 if (retval) {
122 if (errname) {
123 fprintf(stderr, "radeon: Failed to get %s, error number %d\n",
124 errname, retval);
125 }
126 return FALSE;
127 }
128 return TRUE;
129 }
130
131 /* Helper function to do the ioctls needed for setup and init. */
132 static boolean do_winsys_init(struct radeon_drm_winsys *ws)
133 {
134 struct drm_radeon_gem_info gem_info;
135 int retval;
136 drmVersionPtr version;
137
138 memset(&gem_info, 0, sizeof(gem_info));
139
140 /* We do things in a specific order here.
141 *
142 * DRM version first. We need to be sure we're running on a KMS chipset.
143 * This is also for some features.
144 *
145 * Then, the PCI ID. This is essential and should return usable numbers
146 * for all Radeons. If this fails, we probably got handed an FD for some
147 * non-Radeon card.
148 *
149 * The GEM info is actually bogus on the kernel side, as well as our side
150 * (see radeon_gem_info_ioctl in radeon_gem.c) but that's alright because
151 * we don't actually use the info for anything yet.
152 *
153 * The GB and Z pipe requests should always succeed, but they might not
154 * return sensical values for all chipsets, but that's alright because
155 * the pipe drivers already know that.
156 */
157
158 /* Get DRM version. */
159 version = drmGetVersion(ws->fd);
160 if (version->version_major != 2 ||
161 version->version_minor < 3) {
162 fprintf(stderr, "%s: DRM version is %d.%d.%d but this driver is "
163 "only compatible with 2.3.x (kernel 2.6.34) or later.\n",
164 __FUNCTION__,
165 version->version_major,
166 version->version_minor,
167 version->version_patchlevel);
168 drmFreeVersion(version);
169 return FALSE;
170 }
171
172 ws->info.drm_major = version->version_major;
173 ws->info.drm_minor = version->version_minor;
174 ws->info.drm_patchlevel = version->version_patchlevel;
175 drmFreeVersion(version);
176
177 /* Get PCI ID. */
178 if (!radeon_get_drm_value(ws->fd, RADEON_INFO_DEVICE_ID, "PCI ID",
179 &ws->info.pci_id))
180 return FALSE;
181
182 /* Check PCI ID. */
183 switch (ws->info.pci_id) {
184 #define CHIPSET(pci_id, name, cfamily) case pci_id: ws->info.family = CHIP_##cfamily; ws->gen = DRV_R300; break;
185 #include "pci_ids/r300_pci_ids.h"
186 #undef CHIPSET
187
188 #define CHIPSET(pci_id, name, cfamily) case pci_id: ws->info.family = CHIP_##cfamily; ws->gen = DRV_R600; break;
189 #include "pci_ids/r600_pci_ids.h"
190 #undef CHIPSET
191
192 #define CHIPSET(pci_id, name, cfamily) case pci_id: ws->info.family = CHIP_##cfamily; ws->gen = DRV_SI; break;
193 #include "pci_ids/radeonsi_pci_ids.h"
194 #undef CHIPSET
195
196 default:
197 fprintf(stderr, "radeon: Invalid PCI ID.\n");
198 return FALSE;
199 }
200
201 switch (ws->info.family) {
202 default:
203 case CHIP_UNKNOWN:
204 fprintf(stderr, "radeon: Unknown family.\n");
205 return FALSE;
206 case CHIP_R300:
207 case CHIP_R350:
208 case CHIP_RV350:
209 case CHIP_RV370:
210 case CHIP_RV380:
211 case CHIP_RS400:
212 case CHIP_RC410:
213 case CHIP_RS480:
214 ws->info.chip_class = R300;
215 break;
216 case CHIP_R420: /* R4xx-based cores. */
217 case CHIP_R423:
218 case CHIP_R430:
219 case CHIP_R480:
220 case CHIP_R481:
221 case CHIP_RV410:
222 case CHIP_RS600:
223 case CHIP_RS690:
224 case CHIP_RS740:
225 ws->info.chip_class = R400;
226 break;
227 case CHIP_RV515: /* R5xx-based cores. */
228 case CHIP_R520:
229 case CHIP_RV530:
230 case CHIP_R580:
231 case CHIP_RV560:
232 case CHIP_RV570:
233 ws->info.chip_class = R500;
234 break;
235 case CHIP_R600:
236 case CHIP_RV610:
237 case CHIP_RV630:
238 case CHIP_RV670:
239 case CHIP_RV620:
240 case CHIP_RV635:
241 case CHIP_RS780:
242 case CHIP_RS880:
243 ws->info.chip_class = R600;
244 break;
245 case CHIP_RV770:
246 case CHIP_RV730:
247 case CHIP_RV710:
248 case CHIP_RV740:
249 ws->info.chip_class = R700;
250 break;
251 case CHIP_CEDAR:
252 case CHIP_REDWOOD:
253 case CHIP_JUNIPER:
254 case CHIP_CYPRESS:
255 case CHIP_HEMLOCK:
256 case CHIP_PALM:
257 case CHIP_SUMO:
258 case CHIP_SUMO2:
259 case CHIP_BARTS:
260 case CHIP_TURKS:
261 case CHIP_CAICOS:
262 ws->info.chip_class = EVERGREEN;
263 break;
264 case CHIP_CAYMAN:
265 case CHIP_ARUBA:
266 ws->info.chip_class = CAYMAN;
267 break;
268 case CHIP_TAHITI:
269 case CHIP_PITCAIRN:
270 case CHIP_VERDE:
271 case CHIP_OLAND:
272 case CHIP_HAINAN:
273 ws->info.chip_class = SI;
274 break;
275 case CHIP_BONAIRE:
276 case CHIP_KAVERI:
277 case CHIP_KABINI:
278 case CHIP_HAWAII:
279 ws->info.chip_class = CIK;
280 break;
281 }
282
283 /* Check for dma */
284 ws->info.r600_has_dma = FALSE;
285 if (ws->info.chip_class >= R700 && ws->info.drm_minor >= 27) {
286 ws->info.r600_has_dma = TRUE;
287 }
288
289 /* Check for UVD and VCE */
290 ws->info.has_uvd = FALSE;
291 ws->info.vce_fw_version = 0x00000000;
292 if (ws->info.drm_minor >= 32) {
293 uint32_t value = RADEON_CS_RING_UVD;
294 if (radeon_get_drm_value(ws->fd, RADEON_INFO_RING_WORKING,
295 "UVD Ring working", &value))
296 ws->info.has_uvd = value;
297
298 value = RADEON_CS_RING_VCE;
299 if (radeon_get_drm_value(ws->fd, RADEON_INFO_RING_WORKING,
300 NULL, &value) && value) {
301
302 if (radeon_get_drm_value(ws->fd, RADEON_INFO_VCE_FW_VERSION,
303 "VCE FW version", &value))
304 ws->info.vce_fw_version = value;
305 }
306 }
307
308 /* Get GEM info. */
309 retval = drmCommandWriteRead(ws->fd, DRM_RADEON_GEM_INFO,
310 &gem_info, sizeof(gem_info));
311 if (retval) {
312 fprintf(stderr, "radeon: Failed to get MM info, error number %d\n",
313 retval);
314 return FALSE;
315 }
316 ws->info.gart_size = gem_info.gart_size;
317 ws->info.vram_size = gem_info.vram_size;
318
319 ws->num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
320
321 /* Generation-specific queries. */
322 if (ws->gen == DRV_R300) {
323 if (!radeon_get_drm_value(ws->fd, RADEON_INFO_NUM_GB_PIPES,
324 "GB pipe count",
325 &ws->info.r300_num_gb_pipes))
326 return FALSE;
327
328 if (!radeon_get_drm_value(ws->fd, RADEON_INFO_NUM_Z_PIPES,
329 "Z pipe count",
330 &ws->info.r300_num_z_pipes))
331 return FALSE;
332 }
333 else if (ws->gen >= DRV_R600) {
334 if (ws->info.drm_minor >= 9 &&
335 !radeon_get_drm_value(ws->fd, RADEON_INFO_NUM_BACKENDS,
336 "num backends",
337 &ws->info.r600_num_backends))
338 return FALSE;
339
340 /* get the GPU counter frequency, failure is not fatal */
341 radeon_get_drm_value(ws->fd, RADEON_INFO_CLOCK_CRYSTAL_FREQ, NULL,
342 &ws->info.r600_clock_crystal_freq);
343
344 radeon_get_drm_value(ws->fd, RADEON_INFO_TILING_CONFIG, NULL,
345 &ws->info.r600_tiling_config);
346
347 if (ws->info.drm_minor >= 11) {
348 radeon_get_drm_value(ws->fd, RADEON_INFO_NUM_TILE_PIPES, NULL,
349 &ws->info.r600_num_tile_pipes);
350
351 if (radeon_get_drm_value(ws->fd, RADEON_INFO_BACKEND_MAP, NULL,
352 &ws->info.r600_backend_map))
353 ws->info.r600_backend_map_valid = TRUE;
354 }
355
356 ws->info.r600_virtual_address = FALSE;
357 if (ws->info.drm_minor >= 13) {
358 uint32_t ib_vm_max_size;
359
360 ws->info.r600_virtual_address = TRUE;
361 if (!radeon_get_drm_value(ws->fd, RADEON_INFO_VA_START, NULL,
362 &ws->va_start))
363 ws->info.r600_virtual_address = FALSE;
364 if (!radeon_get_drm_value(ws->fd, RADEON_INFO_IB_VM_MAX_SIZE, NULL,
365 &ib_vm_max_size))
366 ws->info.r600_virtual_address = FALSE;
367 }
368 if (ws->gen == DRV_R600 && !debug_get_bool_option("RADEON_VA", FALSE))
369 ws->info.r600_virtual_address = FALSE;
370 }
371
372 /* Get max pipes, this is only needed for compute shaders. All evergreen+
373 * chips have at least 2 pipes, so we use 2 as a default. */
374 ws->info.r600_max_pipes = 2;
375 radeon_get_drm_value(ws->fd, RADEON_INFO_MAX_PIPES, NULL,
376 &ws->info.r600_max_pipes);
377
378 if (radeon_get_drm_value(ws->fd, RADEON_INFO_SI_TILE_MODE_ARRAY, NULL,
379 ws->info.si_tile_mode_array)) {
380 ws->info.si_tile_mode_array_valid = TRUE;
381 }
382
383 if (radeon_get_drm_value(ws->fd, RADEON_INFO_CIK_MACROTILE_MODE_ARRAY, NULL,
384 ws->info.cik_macrotile_mode_array)) {
385 ws->info.cik_macrotile_mode_array_valid = TRUE;
386 }
387
388 return TRUE;
389 }
390
391 static void radeon_winsys_destroy(struct radeon_winsys *rws)
392 {
393 struct radeon_drm_winsys *ws = (struct radeon_drm_winsys*)rws;
394
395 if (ws->thread) {
396 ws->kill_thread = 1;
397 pipe_semaphore_signal(&ws->cs_queued);
398 pipe_thread_wait(ws->thread);
399 }
400 pipe_semaphore_destroy(&ws->cs_queued);
401
402 pipe_mutex_destroy(ws->hyperz_owner_mutex);
403 pipe_mutex_destroy(ws->cmask_owner_mutex);
404 pipe_mutex_destroy(ws->cs_stack_lock);
405
406 ws->cman->destroy(ws->cman);
407 ws->kman->destroy(ws->kman);
408 if (ws->gen >= DRV_R600) {
409 radeon_surface_manager_free(ws->surf_man);
410 }
411 FREE(rws);
412 }
413
414 static void radeon_query_info(struct radeon_winsys *rws,
415 struct radeon_info *info)
416 {
417 *info = ((struct radeon_drm_winsys *)rws)->info;
418 }
419
420 static boolean radeon_cs_request_feature(struct radeon_winsys_cs *rcs,
421 enum radeon_feature_id fid,
422 boolean enable)
423 {
424 struct radeon_drm_cs *cs = radeon_drm_cs(rcs);
425
426 switch (fid) {
427 case RADEON_FID_R300_HYPERZ_ACCESS:
428 return radeon_set_fd_access(cs, &cs->ws->hyperz_owner,
429 &cs->ws->hyperz_owner_mutex,
430 RADEON_INFO_WANT_HYPERZ, "Hyper-Z",
431 enable);
432
433 case RADEON_FID_R300_CMASK_ACCESS:
434 return radeon_set_fd_access(cs, &cs->ws->cmask_owner,
435 &cs->ws->cmask_owner_mutex,
436 RADEON_INFO_WANT_CMASK, "AA optimizations",
437 enable);
438 }
439 return FALSE;
440 }
441
442 static int radeon_drm_winsys_surface_init(struct radeon_winsys *rws,
443 struct radeon_surface *surf)
444 {
445 struct radeon_drm_winsys *ws = (struct radeon_drm_winsys*)rws;
446
447 return radeon_surface_init(ws->surf_man, surf);
448 }
449
450 static int radeon_drm_winsys_surface_best(struct radeon_winsys *rws,
451 struct radeon_surface *surf)
452 {
453 struct radeon_drm_winsys *ws = (struct radeon_drm_winsys*)rws;
454
455 return radeon_surface_best(ws->surf_man, surf);
456 }
457
458 static uint64_t radeon_query_value(struct radeon_winsys *rws,
459 enum radeon_value_id value)
460 {
461 struct radeon_drm_winsys *ws = (struct radeon_drm_winsys*)rws;
462 uint64_t retval = 0;
463
464 switch (value) {
465 case RADEON_REQUESTED_VRAM_MEMORY:
466 return ws->allocated_vram;
467 case RADEON_REQUESTED_GTT_MEMORY:
468 return ws->allocated_gtt;
469 case RADEON_BUFFER_WAIT_TIME_NS:
470 return ws->buffer_wait_time;
471 case RADEON_TIMESTAMP:
472 if (ws->info.drm_minor < 20 || ws->gen < DRV_R600) {
473 assert(0);
474 return 0;
475 }
476
477 radeon_get_drm_value(ws->fd, RADEON_INFO_TIMESTAMP, "timestamp",
478 (uint32_t*)&retval);
479 return retval;
480 case RADEON_NUM_CS_FLUSHES:
481 return ws->num_cs_flushes;
482 case RADEON_NUM_BYTES_MOVED:
483 radeon_get_drm_value(ws->fd, RADEON_INFO_NUM_BYTES_MOVED,
484 "num-bytes-moved", (uint32_t*)&retval);
485 return retval;
486 case RADEON_VRAM_USAGE:
487 radeon_get_drm_value(ws->fd, RADEON_INFO_VRAM_USAGE,
488 "vram-usage", (uint32_t*)&retval);
489 return retval;
490 case RADEON_GTT_USAGE:
491 radeon_get_drm_value(ws->fd, RADEON_INFO_GTT_USAGE,
492 "gtt-usage", (uint32_t*)&retval);
493 return retval;
494 }
495 return 0;
496 }
497
498 static unsigned hash_fd(void *key)
499 {
500 int fd = pointer_to_intptr(key);
501 struct stat stat;
502 fstat(fd, &stat);
503
504 return stat.st_dev ^ stat.st_ino ^ stat.st_rdev;
505 }
506
507 static int compare_fd(void *key1, void *key2)
508 {
509 int fd1 = pointer_to_intptr(key1);
510 int fd2 = pointer_to_intptr(key2);
511 struct stat stat1, stat2;
512 fstat(fd1, &stat1);
513 fstat(fd2, &stat2);
514
515 return stat1.st_dev != stat2.st_dev ||
516 stat1.st_ino != stat2.st_ino ||
517 stat1.st_rdev != stat2.st_rdev;
518 }
519
520 void radeon_drm_ws_queue_cs(struct radeon_drm_winsys *ws, struct radeon_drm_cs *cs)
521 {
522 retry:
523 pipe_mutex_lock(ws->cs_stack_lock);
524 if (ws->ncs >= RING_LAST) {
525 /* no room left for a flush */
526 pipe_mutex_unlock(ws->cs_stack_lock);
527 goto retry;
528 }
529 ws->cs_stack[ws->ncs++] = cs;
530 pipe_mutex_unlock(ws->cs_stack_lock);
531 pipe_semaphore_signal(&ws->cs_queued);
532 }
533
534 static PIPE_THREAD_ROUTINE(radeon_drm_cs_emit_ioctl, param)
535 {
536 struct radeon_drm_winsys *ws = (struct radeon_drm_winsys *)param;
537 struct radeon_drm_cs *cs;
538 unsigned i;
539
540 while (1) {
541 pipe_semaphore_wait(&ws->cs_queued);
542 if (ws->kill_thread)
543 break;
544
545 pipe_mutex_lock(ws->cs_stack_lock);
546 cs = ws->cs_stack[0];
547 for (i = 1; i < ws->ncs; i++)
548 ws->cs_stack[i - 1] = ws->cs_stack[i];
549 ws->cs_stack[--ws->ncs] = NULL;
550 pipe_mutex_unlock(ws->cs_stack_lock);
551
552 if (cs) {
553 radeon_drm_cs_emit_ioctl_oneshot(cs, cs->cst);
554 pipe_semaphore_signal(&cs->flush_completed);
555 }
556 }
557 pipe_mutex_lock(ws->cs_stack_lock);
558 for (i = 0; i < ws->ncs; i++) {
559 pipe_semaphore_signal(&ws->cs_stack[i]->flush_completed);
560 ws->cs_stack[i] = NULL;
561 }
562 ws->ncs = 0;
563 pipe_mutex_unlock(ws->cs_stack_lock);
564 return 0;
565 }
566
567 DEBUG_GET_ONCE_BOOL_OPTION(thread, "RADEON_THREAD", TRUE)
568 static PIPE_THREAD_ROUTINE(radeon_drm_cs_emit_ioctl, param);
569
570 static bool radeon_winsys_unref(struct radeon_winsys *ws)
571 {
572 struct radeon_drm_winsys *rws = (struct radeon_drm_winsys*)ws;
573 bool destroy;
574
575 /* When the reference counter drops to zero, remove the fd from the table.
576 * This must happen while the mutex is locked, so that
577 * radeon_drm_winsys_create in another thread doesn't get the winsys
578 * from the table when the counter drops to 0. */
579 pipe_mutex_lock(fd_tab_mutex);
580
581 destroy = pipe_reference(&rws->reference, NULL);
582 if (destroy && fd_tab)
583 util_hash_table_remove(fd_tab, intptr_to_pointer(rws->fd));
584
585 pipe_mutex_unlock(fd_tab_mutex);
586 return destroy;
587 }
588
589 PUBLIC struct radeon_winsys *radeon_drm_winsys_create(int fd)
590 {
591 struct radeon_drm_winsys *ws;
592
593 pipe_mutex_lock(fd_tab_mutex);
594 if (!fd_tab) {
595 fd_tab = util_hash_table_create(hash_fd, compare_fd);
596 }
597
598 ws = util_hash_table_get(fd_tab, intptr_to_pointer(fd));
599 if (ws) {
600 pipe_reference(NULL, &ws->reference);
601 pipe_mutex_unlock(fd_tab_mutex);
602 return &ws->base;
603 }
604
605 ws = CALLOC_STRUCT(radeon_drm_winsys);
606 if (!ws) {
607 pipe_mutex_unlock(fd_tab_mutex);
608 return NULL;
609 }
610
611 ws->fd = fd;
612 util_hash_table_set(fd_tab, intptr_to_pointer(fd), ws);
613
614 if (!do_winsys_init(ws))
615 goto fail;
616
617 /* Create managers. */
618 ws->kman = radeon_bomgr_create(ws);
619 if (!ws->kman)
620 goto fail;
621 ws->cman = pb_cache_manager_create(ws->kman, 1000000, 2.0f, 0);
622 if (!ws->cman)
623 goto fail;
624
625 if (ws->gen >= DRV_R600) {
626 ws->surf_man = radeon_surface_manager_new(fd);
627 if (!ws->surf_man)
628 goto fail;
629 }
630
631 /* init reference */
632 pipe_reference_init(&ws->reference, 1);
633
634 /* Set functions. */
635 ws->base.unref = radeon_winsys_unref;
636 ws->base.destroy = radeon_winsys_destroy;
637 ws->base.query_info = radeon_query_info;
638 ws->base.cs_request_feature = radeon_cs_request_feature;
639 ws->base.surface_init = radeon_drm_winsys_surface_init;
640 ws->base.surface_best = radeon_drm_winsys_surface_best;
641 ws->base.query_value = radeon_query_value;
642
643 radeon_bomgr_init_functions(ws);
644 radeon_drm_cs_init_functions(ws);
645
646 pipe_mutex_init(ws->hyperz_owner_mutex);
647 pipe_mutex_init(ws->cmask_owner_mutex);
648 pipe_mutex_init(ws->cs_stack_lock);
649
650 ws->ncs = 0;
651 pipe_semaphore_init(&ws->cs_queued, 0);
652 if (ws->num_cpus > 1 && debug_get_option_thread())
653 ws->thread = pipe_thread_create(radeon_drm_cs_emit_ioctl, ws);
654
655 /* We must unlock the mutex once the winsys is fully initialized, so that
656 * other threads attempting to create the winsys from the same fd will
657 * get a fully initialized winsys and not just half-way initialized. */
658 pipe_mutex_unlock(fd_tab_mutex);
659
660 return &ws->base;
661
662 fail:
663 pipe_mutex_unlock(fd_tab_mutex);
664 if (ws->cman)
665 ws->cman->destroy(ws->cman);
666 if (ws->kman)
667 ws->kman->destroy(ws->kman);
668 if (ws->surf_man)
669 radeon_surface_manager_free(ws->surf_man);
670 FREE(ws);
671 return NULL;
672 }