2 * Copyright © 2009 Corbin Simpson
3 * Copyright © 2011 Marek Olšák <maraeo@gmail.com>
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:
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.
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
29 * Corbin Simpson <MostAwesomeDude@gmail.com>
30 * Joakim Sindholt <opensource@zhasha.com>
31 * Marek Olšák <maraeo@gmail.com>
34 #include "radeon_drm_bo.h"
35 #include "radeon_drm_cs.h"
36 #include "radeon_drm_public.h"
38 #include "util/u_memory.h"
39 #include "util/u_hash_table.h"
43 #include <sys/types.h>
47 #include <radeon_surface.h>
49 static struct util_hash_table
*fd_tab
= NULL
;
50 static mtx_t fd_tab_mutex
= _MTX_INITIALIZER_NP
;
52 /* Enable/disable feature access for one command stream.
53 * If enable == true, return true on success.
54 * Otherwise, return false.
56 * We basically do the same thing kernel does, because we have to deal
57 * with multiple contexts (here command streams) backed by one winsys. */
58 static bool radeon_set_fd_access(struct radeon_drm_cs
*applier
,
59 struct radeon_drm_cs
**owner
,
61 unsigned request
, const char *request_name
,
64 struct drm_radeon_info info
;
65 unsigned value
= enable
? 1 : 0;
67 memset(&info
, 0, sizeof(info
));
69 pipe_mutex_lock(*mutex
);
71 /* Early exit if we are sure the request will fail. */
74 pipe_mutex_unlock(*mutex
);
78 if (*owner
!= applier
) {
79 pipe_mutex_unlock(*mutex
);
84 /* Pass through the request to the kernel. */
85 info
.value
= (unsigned long)&value
;
86 info
.request
= request
;
87 if (drmCommandWriteRead(applier
->ws
->fd
, DRM_RADEON_INFO
,
88 &info
, sizeof(info
)) != 0) {
89 pipe_mutex_unlock(*mutex
);
93 /* Update the rights in the winsys. */
97 pipe_mutex_unlock(*mutex
);
104 pipe_mutex_unlock(*mutex
);
108 static bool radeon_get_drm_value(int fd
, unsigned request
,
109 const char *errname
, uint32_t *out
)
111 struct drm_radeon_info info
;
114 memset(&info
, 0, sizeof(info
));
116 info
.value
= (unsigned long)out
;
117 info
.request
= request
;
119 retval
= drmCommandWriteRead(fd
, DRM_RADEON_INFO
, &info
, sizeof(info
));
122 fprintf(stderr
, "radeon: Failed to get %s, error number %d\n",
130 /* Helper function to do the ioctls needed for setup and init. */
131 static bool do_winsys_init(struct radeon_drm_winsys
*ws
)
133 struct drm_radeon_gem_info gem_info
;
135 drmVersionPtr version
;
137 memset(&gem_info
, 0, sizeof(gem_info
));
139 /* We do things in a specific order here.
141 * DRM version first. We need to be sure we're running on a KMS chipset.
142 * This is also for some features.
144 * Then, the PCI ID. This is essential and should return usable numbers
145 * for all Radeons. If this fails, we probably got handed an FD for some
148 * The GEM info is actually bogus on the kernel side, as well as our side
149 * (see radeon_gem_info_ioctl in radeon_gem.c) but that's alright because
150 * we don't actually use the info for anything yet.
152 * The GB and Z pipe requests should always succeed, but they might not
153 * return sensical values for all chipsets, but that's alright because
154 * the pipe drivers already know that.
157 /* Get DRM version. */
158 version
= drmGetVersion(ws
->fd
);
159 if (version
->version_major
!= 2 ||
160 version
->version_minor
< 12) {
161 fprintf(stderr
, "%s: DRM version is %d.%d.%d but this driver is "
162 "only compatible with 2.12.0 (kernel 3.2) or later.\n",
164 version
->version_major
,
165 version
->version_minor
,
166 version
->version_patchlevel
);
167 drmFreeVersion(version
);
171 ws
->info
.drm_major
= version
->version_major
;
172 ws
->info
.drm_minor
= version
->version_minor
;
173 ws
->info
.drm_patchlevel
= version
->version_patchlevel
;
174 drmFreeVersion(version
);
177 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_DEVICE_ID
, "PCI ID",
182 switch (ws
->info
.pci_id
) {
183 #define CHIPSET(pci_id, name, cfamily) case pci_id: ws->info.family = CHIP_##cfamily; ws->gen = DRV_R300; break;
184 #include "pci_ids/r300_pci_ids.h"
187 #define CHIPSET(pci_id, name, cfamily) case pci_id: ws->info.family = CHIP_##cfamily; ws->gen = DRV_R600; break;
188 #include "pci_ids/r600_pci_ids.h"
191 #define CHIPSET(pci_id, name, cfamily) case pci_id: ws->info.family = CHIP_##cfamily; ws->gen = DRV_SI; break;
192 #include "pci_ids/radeonsi_pci_ids.h"
196 fprintf(stderr
, "radeon: Invalid PCI ID.\n");
200 switch (ws
->info
.family
) {
203 fprintf(stderr
, "radeon: Unknown family.\n");
213 ws
->info
.chip_class
= R300
;
215 case CHIP_R420
: /* R4xx-based cores. */
224 ws
->info
.chip_class
= R400
;
226 case CHIP_RV515
: /* R5xx-based cores. */
232 ws
->info
.chip_class
= R500
;
242 ws
->info
.chip_class
= R600
;
248 ws
->info
.chip_class
= R700
;
261 ws
->info
.chip_class
= EVERGREEN
;
265 ws
->info
.chip_class
= CAYMAN
;
272 ws
->info
.chip_class
= SI
;
279 ws
->info
.chip_class
= CIK
;
283 /* Set which chips don't have dedicated VRAM. */
284 switch (ws
->info
.family
) {
300 ws
->info
.has_dedicated_vram
= false;
304 ws
->info
.has_dedicated_vram
= true;
308 ws
->info
.has_sdma
= false;
309 /* DMA is disabled on R700. There is IB corruption and hangs. */
310 if (ws
->info
.chip_class
>= EVERGREEN
&& ws
->info
.drm_minor
>= 27) {
311 ws
->info
.has_sdma
= true;
314 /* Check for UVD and VCE */
315 ws
->info
.has_uvd
= false;
316 ws
->info
.vce_fw_version
= 0x00000000;
317 if (ws
->info
.drm_minor
>= 32) {
318 uint32_t value
= RADEON_CS_RING_UVD
;
319 if (radeon_get_drm_value(ws
->fd
, RADEON_INFO_RING_WORKING
,
320 "UVD Ring working", &value
))
321 ws
->info
.has_uvd
= value
;
323 value
= RADEON_CS_RING_VCE
;
324 if (radeon_get_drm_value(ws
->fd
, RADEON_INFO_RING_WORKING
,
325 NULL
, &value
) && value
) {
327 if (radeon_get_drm_value(ws
->fd
, RADEON_INFO_VCE_FW_VERSION
,
328 "VCE FW version", &value
))
329 ws
->info
.vce_fw_version
= value
;
333 /* Check for userptr support. */
335 struct drm_radeon_gem_userptr args
= {0};
337 /* If the ioctl doesn't exist, -EINVAL is returned.
339 * If the ioctl exists, it should return -EACCES
340 * if RADEON_GEM_USERPTR_READONLY or RADEON_GEM_USERPTR_REGISTER
343 ws
->info
.has_userptr
=
344 drmCommandWriteRead(ws
->fd
, DRM_RADEON_GEM_USERPTR
,
345 &args
, sizeof(args
)) == -EACCES
;
349 retval
= drmCommandWriteRead(ws
->fd
, DRM_RADEON_GEM_INFO
,
350 &gem_info
, sizeof(gem_info
));
352 fprintf(stderr
, "radeon: Failed to get MM info, error number %d\n",
356 ws
->info
.gart_size
= gem_info
.gart_size
;
357 ws
->info
.vram_size
= gem_info
.vram_size
;
358 ws
->info
.vram_vis_size
= gem_info
.vram_visible
;
359 /* Older versions of the kernel driver reported incorrect values, and
360 * didn't support more than 256MB of visible VRAM anyway
362 if (ws
->info
.drm_minor
< 49)
363 ws
->info
.vram_vis_size
= MIN2(ws
->info
.vram_vis_size
, 256*1024*1024);
365 /* Radeon allocates all buffers as contigous, which makes large allocations
366 * unlikely to succeed. */
367 ws
->info
.max_alloc_size
= MAX2(ws
->info
.vram_size
, ws
->info
.gart_size
) * 0.7;
368 if (ws
->info
.drm_minor
< 40)
369 ws
->info
.max_alloc_size
= MIN2(ws
->info
.max_alloc_size
, 256*1024*1024);
371 /* Get max clock frequency info and convert it to MHz */
372 radeon_get_drm_value(ws
->fd
, RADEON_INFO_MAX_SCLK
, NULL
,
373 &ws
->info
.max_shader_clock
);
374 ws
->info
.max_shader_clock
/= 1000;
377 ws
->info
.enabled_rb_mask
= u_bit_consecutive(0, ws
->info
.num_render_backends
);
378 /* This fails on non-GCN or older kernels: */
379 radeon_get_drm_value(ws
->fd
, RADEON_INFO_SI_BACKEND_ENABLED_MASK
, NULL
,
380 &ws
->info
.enabled_rb_mask
);
382 ws
->num_cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
384 /* Generation-specific queries. */
385 if (ws
->gen
== DRV_R300
) {
386 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_NUM_GB_PIPES
,
388 &ws
->info
.r300_num_gb_pipes
))
391 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_NUM_Z_PIPES
,
393 &ws
->info
.r300_num_z_pipes
))
396 else if (ws
->gen
>= DRV_R600
) {
397 uint32_t tiling_config
= 0;
399 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_NUM_BACKENDS
,
401 &ws
->info
.num_render_backends
))
404 /* get the GPU counter frequency, failure is not fatal */
405 radeon_get_drm_value(ws
->fd
, RADEON_INFO_CLOCK_CRYSTAL_FREQ
, NULL
,
406 &ws
->info
.clock_crystal_freq
);
408 radeon_get_drm_value(ws
->fd
, RADEON_INFO_TILING_CONFIG
, NULL
,
411 ws
->info
.r600_num_banks
=
412 ws
->info
.chip_class
>= EVERGREEN
?
413 4 << ((tiling_config
& 0xf0) >> 4) :
414 4 << ((tiling_config
& 0x30) >> 4);
416 ws
->info
.pipe_interleave_bytes
=
417 ws
->info
.chip_class
>= EVERGREEN
?
418 256 << ((tiling_config
& 0xf00) >> 8) :
419 256 << ((tiling_config
& 0xc0) >> 6);
421 if (!ws
->info
.pipe_interleave_bytes
)
422 ws
->info
.pipe_interleave_bytes
=
423 ws
->info
.chip_class
>= EVERGREEN
? 512 : 256;
425 radeon_get_drm_value(ws
->fd
, RADEON_INFO_NUM_TILE_PIPES
, NULL
,
426 &ws
->info
.num_tile_pipes
);
428 /* "num_tiles_pipes" must be equal to the number of pipes (Px) in the
429 * pipe config field of the GB_TILE_MODE array. Only one card (Tahiti)
430 * reports a different value (12). Fix it by setting what's in the
431 * GB_TILE_MODE array (8).
433 if (ws
->gen
== DRV_SI
&& ws
->info
.num_tile_pipes
== 12)
434 ws
->info
.num_tile_pipes
= 8;
436 if (radeon_get_drm_value(ws
->fd
, RADEON_INFO_BACKEND_MAP
, NULL
,
437 &ws
->info
.r600_gb_backend_map
))
438 ws
->info
.r600_gb_backend_map_valid
= true;
440 ws
->info
.has_virtual_memory
= false;
441 if (ws
->info
.drm_minor
>= 13) {
442 uint32_t ib_vm_max_size
;
444 ws
->info
.has_virtual_memory
= true;
445 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_VA_START
, NULL
,
447 ws
->info
.has_virtual_memory
= false;
448 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_IB_VM_MAX_SIZE
, NULL
,
450 ws
->info
.has_virtual_memory
= false;
451 radeon_get_drm_value(ws
->fd
, RADEON_INFO_VA_UNMAP_WORKING
, NULL
,
452 &ws
->va_unmap_working
);
454 if (ws
->gen
== DRV_R600
&& !debug_get_bool_option("RADEON_VA", false))
455 ws
->info
.has_virtual_memory
= false;
458 /* Get max pipes, this is only needed for compute shaders. All evergreen+
459 * chips have at least 2 pipes, so we use 2 as a default. */
460 ws
->info
.r600_max_quad_pipes
= 2;
461 radeon_get_drm_value(ws
->fd
, RADEON_INFO_MAX_PIPES
, NULL
,
462 &ws
->info
.r600_max_quad_pipes
);
464 /* All GPUs have at least one compute unit */
465 ws
->info
.num_good_compute_units
= 1;
466 radeon_get_drm_value(ws
->fd
, RADEON_INFO_ACTIVE_CU_COUNT
, NULL
,
467 &ws
->info
.num_good_compute_units
);
469 radeon_get_drm_value(ws
->fd
, RADEON_INFO_MAX_SE
, NULL
,
472 if (!ws
->info
.max_se
) {
473 switch (ws
->info
.family
) {
492 radeon_get_drm_value(ws
->fd
, RADEON_INFO_MAX_SH_PER_SE
, NULL
,
493 &ws
->info
.max_sh_per_se
);
495 radeon_get_drm_value(ws
->fd
, RADEON_INFO_ACCEL_WORKING2
, NULL
,
496 &ws
->accel_working2
);
497 if (ws
->info
.family
== CHIP_HAWAII
&& ws
->accel_working2
< 2) {
498 fprintf(stderr
, "radeon: GPU acceleration for Hawaii disabled, "
499 "returned accel_working2 value %u is smaller than 2. "
500 "Please install a newer kernel.\n",
505 if (ws
->info
.chip_class
== CIK
) {
506 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_CIK_MACROTILE_MODE_ARRAY
, NULL
,
507 ws
->info
.cik_macrotile_mode_array
)) {
508 fprintf(stderr
, "radeon: Kernel 3.13 is required for CIK support.\n");
513 if (ws
->info
.chip_class
>= SI
) {
514 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_SI_TILE_MODE_ARRAY
, NULL
,
515 ws
->info
.si_tile_mode_array
)) {
516 fprintf(stderr
, "radeon: Kernel 3.10 is required for SI support.\n");
521 /* Hawaii with old firmware needs type2 nop packet.
522 * accel_working2 with value 3 indicates the new firmware.
524 ws
->info
.gfx_ib_pad_with_type2
= ws
->info
.chip_class
<= SI
||
525 (ws
->info
.family
== CHIP_HAWAII
&&
526 ws
->accel_working2
< 3);
527 ws
->info
.tcc_cache_line_size
= 64; /* TC L2 line size on GCN */
529 ws
->check_vm
= strstr(debug_get_option("R600_DEBUG", ""), "check_vm") != NULL
;
534 static void radeon_winsys_destroy(struct radeon_winsys
*rws
)
536 struct radeon_drm_winsys
*ws
= (struct radeon_drm_winsys
*)rws
;
538 if (util_queue_is_initialized(&ws
->cs_queue
))
539 util_queue_destroy(&ws
->cs_queue
);
541 mtx_destroy(&ws
->hyperz_owner_mutex
);
542 mtx_destroy(&ws
->cmask_owner_mutex
);
544 if (ws
->info
.has_virtual_memory
)
545 pb_slabs_deinit(&ws
->bo_slabs
);
546 pb_cache_deinit(&ws
->bo_cache
);
548 if (ws
->gen
>= DRV_R600
) {
549 radeon_surface_manager_free(ws
->surf_man
);
552 util_hash_table_destroy(ws
->bo_names
);
553 util_hash_table_destroy(ws
->bo_handles
);
554 util_hash_table_destroy(ws
->bo_vas
);
555 mtx_destroy(&ws
->bo_handles_mutex
);
556 mtx_destroy(&ws
->bo_va_mutex
);
557 mtx_destroy(&ws
->bo_fence_lock
);
565 static void radeon_query_info(struct radeon_winsys
*rws
,
566 struct radeon_info
*info
)
568 *info
= ((struct radeon_drm_winsys
*)rws
)->info
;
571 static bool radeon_cs_request_feature(struct radeon_winsys_cs
*rcs
,
572 enum radeon_feature_id fid
,
575 struct radeon_drm_cs
*cs
= radeon_drm_cs(rcs
);
578 case RADEON_FID_R300_HYPERZ_ACCESS
:
579 return radeon_set_fd_access(cs
, &cs
->ws
->hyperz_owner
,
580 &cs
->ws
->hyperz_owner_mutex
,
581 RADEON_INFO_WANT_HYPERZ
, "Hyper-Z",
584 case RADEON_FID_R300_CMASK_ACCESS
:
585 return radeon_set_fd_access(cs
, &cs
->ws
->cmask_owner
,
586 &cs
->ws
->cmask_owner_mutex
,
587 RADEON_INFO_WANT_CMASK
, "AA optimizations",
593 static uint64_t radeon_query_value(struct radeon_winsys
*rws
,
594 enum radeon_value_id value
)
596 struct radeon_drm_winsys
*ws
= (struct radeon_drm_winsys
*)rws
;
600 case RADEON_REQUESTED_VRAM_MEMORY
:
601 return ws
->allocated_vram
;
602 case RADEON_REQUESTED_GTT_MEMORY
:
603 return ws
->allocated_gtt
;
604 case RADEON_MAPPED_VRAM
:
605 return ws
->mapped_vram
;
606 case RADEON_MAPPED_GTT
:
607 return ws
->mapped_gtt
;
608 case RADEON_BUFFER_WAIT_TIME_NS
:
609 return ws
->buffer_wait_time
;
610 case RADEON_NUM_MAPPED_BUFFERS
:
611 return ws
->num_mapped_buffers
;
612 case RADEON_TIMESTAMP
:
613 if (ws
->info
.drm_minor
< 20 || ws
->gen
< DRV_R600
) {
618 radeon_get_drm_value(ws
->fd
, RADEON_INFO_TIMESTAMP
, "timestamp",
621 case RADEON_NUM_GFX_IBS
:
622 return ws
->num_gfx_IBs
;
623 case RADEON_NUM_SDMA_IBS
:
624 return ws
->num_sdma_IBs
;
625 case RADEON_NUM_BYTES_MOVED
:
626 radeon_get_drm_value(ws
->fd
, RADEON_INFO_NUM_BYTES_MOVED
,
627 "num-bytes-moved", (uint32_t*)&retval
);
629 case RADEON_NUM_EVICTIONS
:
630 case RADEON_VRAM_VIS_USAGE
:
631 return 0; /* unimplemented */
632 case RADEON_VRAM_USAGE
:
633 radeon_get_drm_value(ws
->fd
, RADEON_INFO_VRAM_USAGE
,
634 "vram-usage", (uint32_t*)&retval
);
636 case RADEON_GTT_USAGE
:
637 radeon_get_drm_value(ws
->fd
, RADEON_INFO_GTT_USAGE
,
638 "gtt-usage", (uint32_t*)&retval
);
640 case RADEON_GPU_TEMPERATURE
:
641 radeon_get_drm_value(ws
->fd
, RADEON_INFO_CURRENT_GPU_TEMP
,
642 "gpu-temp", (uint32_t*)&retval
);
644 case RADEON_CURRENT_SCLK
:
645 radeon_get_drm_value(ws
->fd
, RADEON_INFO_CURRENT_GPU_SCLK
,
646 "current-gpu-sclk", (uint32_t*)&retval
);
648 case RADEON_CURRENT_MCLK
:
649 radeon_get_drm_value(ws
->fd
, RADEON_INFO_CURRENT_GPU_MCLK
,
650 "current-gpu-mclk", (uint32_t*)&retval
);
652 case RADEON_GPU_RESET_COUNTER
:
653 radeon_get_drm_value(ws
->fd
, RADEON_INFO_GPU_RESET_COUNTER
,
654 "gpu-reset-counter", (uint32_t*)&retval
);
656 case RADEON_CS_THREAD_TIME
:
657 return util_queue_get_thread_time_nano(&ws
->cs_queue
, 0);
662 static bool radeon_read_registers(struct radeon_winsys
*rws
,
664 unsigned num_registers
, uint32_t *out
)
666 struct radeon_drm_winsys
*ws
= (struct radeon_drm_winsys
*)rws
;
669 for (i
= 0; i
< num_registers
; i
++) {
670 uint32_t reg
= reg_offset
+ i
*4;
672 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_READ_REG
, NULL
, ®
))
679 static unsigned hash_fd(void *key
)
681 int fd
= pointer_to_intptr(key
);
685 return stat
.st_dev
^ stat
.st_ino
^ stat
.st_rdev
;
688 static int compare_fd(void *key1
, void *key2
)
690 int fd1
= pointer_to_intptr(key1
);
691 int fd2
= pointer_to_intptr(key2
);
692 struct stat stat1
, stat2
;
696 return stat1
.st_dev
!= stat2
.st_dev
||
697 stat1
.st_ino
!= stat2
.st_ino
||
698 stat1
.st_rdev
!= stat2
.st_rdev
;
701 DEBUG_GET_ONCE_BOOL_OPTION(thread
, "RADEON_THREAD", true)
703 static bool radeon_winsys_unref(struct radeon_winsys
*ws
)
705 struct radeon_drm_winsys
*rws
= (struct radeon_drm_winsys
*)ws
;
708 /* When the reference counter drops to zero, remove the fd from the table.
709 * This must happen while the mutex is locked, so that
710 * radeon_drm_winsys_create in another thread doesn't get the winsys
711 * from the table when the counter drops to 0. */
712 pipe_mutex_lock(fd_tab_mutex
);
714 destroy
= pipe_reference(&rws
->reference
, NULL
);
715 if (destroy
&& fd_tab
)
716 util_hash_table_remove(fd_tab
, intptr_to_pointer(rws
->fd
));
718 pipe_mutex_unlock(fd_tab_mutex
);
722 #define PTR_TO_UINT(x) ((unsigned)((intptr_t)(x)))
724 static unsigned handle_hash(void *key
)
726 return PTR_TO_UINT(key
);
729 static int handle_compare(void *key1
, void *key2
)
731 return PTR_TO_UINT(key1
) != PTR_TO_UINT(key2
);
734 PUBLIC
struct radeon_winsys
*
735 radeon_drm_winsys_create(int fd
, radeon_screen_create_t screen_create
)
737 struct radeon_drm_winsys
*ws
;
739 pipe_mutex_lock(fd_tab_mutex
);
741 fd_tab
= util_hash_table_create(hash_fd
, compare_fd
);
744 ws
= util_hash_table_get(fd_tab
, intptr_to_pointer(fd
));
746 pipe_reference(NULL
, &ws
->reference
);
747 pipe_mutex_unlock(fd_tab_mutex
);
751 ws
= CALLOC_STRUCT(radeon_drm_winsys
);
753 pipe_mutex_unlock(fd_tab_mutex
);
757 ws
->fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
759 if (!do_winsys_init(ws
))
762 pb_cache_init(&ws
->bo_cache
, 500000, ws
->check_vm
? 1.0f
: 2.0f
, 0,
763 MIN2(ws
->info
.vram_size
, ws
->info
.gart_size
),
765 radeon_bo_can_reclaim
);
767 if (ws
->info
.has_virtual_memory
) {
768 /* There is no fundamental obstacle to using slab buffer allocation
769 * without GPUVM, but enabling it requires making sure that the drivers
770 * honor the address offset.
772 if (!pb_slabs_init(&ws
->bo_slabs
,
773 RADEON_SLAB_MIN_SIZE_LOG2
, RADEON_SLAB_MAX_SIZE_LOG2
,
776 radeon_bo_can_reclaim_slab
,
777 radeon_bo_slab_alloc
,
778 radeon_bo_slab_free
))
781 ws
->info
.min_alloc_size
= 1 << RADEON_SLAB_MIN_SIZE_LOG2
;
783 ws
->info
.min_alloc_size
= ws
->info
.gart_page_size
;
786 if (ws
->gen
>= DRV_R600
) {
787 ws
->surf_man
= radeon_surface_manager_new(ws
->fd
);
793 pipe_reference_init(&ws
->reference
, 1);
796 ws
->base
.unref
= radeon_winsys_unref
;
797 ws
->base
.destroy
= radeon_winsys_destroy
;
798 ws
->base
.query_info
= radeon_query_info
;
799 ws
->base
.cs_request_feature
= radeon_cs_request_feature
;
800 ws
->base
.query_value
= radeon_query_value
;
801 ws
->base
.read_registers
= radeon_read_registers
;
803 radeon_drm_bo_init_functions(ws
);
804 radeon_drm_cs_init_functions(ws
);
805 radeon_surface_init_functions(ws
);
807 (void) mtx_init(&ws
->hyperz_owner_mutex
, mtx_plain
);
808 (void) mtx_init(&ws
->cmask_owner_mutex
, mtx_plain
);
810 ws
->bo_names
= util_hash_table_create(handle_hash
, handle_compare
);
811 ws
->bo_handles
= util_hash_table_create(handle_hash
, handle_compare
);
812 ws
->bo_vas
= util_hash_table_create(handle_hash
, handle_compare
);
813 (void) mtx_init(&ws
->bo_handles_mutex
, mtx_plain
);
814 (void) mtx_init(&ws
->bo_va_mutex
, mtx_plain
);
815 (void) mtx_init(&ws
->bo_fence_lock
, mtx_plain
);
816 ws
->va_offset
= ws
->va_start
;
817 list_inithead(&ws
->va_holes
);
819 /* TTM aligns the BO size to the CPU page size */
820 ws
->info
.gart_page_size
= sysconf(_SC_PAGESIZE
);
822 if (ws
->num_cpus
> 1 && debug_get_option_thread())
823 util_queue_init(&ws
->cs_queue
, "radeon_cs", 8, 1);
825 /* Create the screen at the end. The winsys must be initialized
828 * Alternatively, we could create the screen based on "ws->gen"
829 * and link all drivers into one binary blob. */
830 ws
->base
.screen
= screen_create(&ws
->base
);
831 if (!ws
->base
.screen
) {
832 radeon_winsys_destroy(&ws
->base
);
833 pipe_mutex_unlock(fd_tab_mutex
);
837 util_hash_table_set(fd_tab
, intptr_to_pointer(ws
->fd
), ws
);
839 /* We must unlock the mutex once the winsys is fully initialized, so that
840 * other threads attempting to create the winsys from the same fd will
841 * get a fully initialized winsys and not just half-way initialized. */
842 pipe_mutex_unlock(fd_tab_mutex
);
847 if (ws
->info
.has_virtual_memory
)
848 pb_slabs_deinit(&ws
->bo_slabs
);
850 pb_cache_deinit(&ws
->bo_cache
);
852 pipe_mutex_unlock(fd_tab_mutex
);
854 radeon_surface_manager_free(ws
->surf_man
);