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 "pipebuffer/pb_bufmgr.h"
39 #include "util/u_memory.h"
40 #include "util/u_hash_table.h"
44 #include <sys/types.h>
48 #ifndef RADEON_INFO_ACTIVE_CU_COUNT
49 #define RADEON_INFO_ACTIVE_CU_COUNT 0x20
52 static struct util_hash_table
*fd_tab
= NULL
;
53 pipe_static_mutex(fd_tab_mutex
);
55 /* Enable/disable feature access for one command stream.
56 * If enable == TRUE, return TRUE on success.
57 * Otherwise, return FALSE.
59 * We basically do the same thing kernel does, because we have to deal
60 * with multiple contexts (here command streams) backed by one winsys. */
61 static boolean
radeon_set_fd_access(struct radeon_drm_cs
*applier
,
62 struct radeon_drm_cs
**owner
,
64 unsigned request
, const char *request_name
,
67 struct drm_radeon_info info
;
68 unsigned value
= enable
? 1 : 0;
70 memset(&info
, 0, sizeof(info
));
72 pipe_mutex_lock(*mutex
);
74 /* Early exit if we are sure the request will fail. */
77 pipe_mutex_unlock(*mutex
);
81 if (*owner
!= applier
) {
82 pipe_mutex_unlock(*mutex
);
87 /* Pass through the request to the kernel. */
88 info
.value
= (unsigned long)&value
;
89 info
.request
= request
;
90 if (drmCommandWriteRead(applier
->ws
->fd
, DRM_RADEON_INFO
,
91 &info
, sizeof(info
)) != 0) {
92 pipe_mutex_unlock(*mutex
);
96 /* Update the rights in the winsys. */
100 printf("radeon: Acquired access to %s.\n", request_name
);
101 pipe_mutex_unlock(*mutex
);
106 printf("radeon: Released access to %s.\n", request_name
);
109 pipe_mutex_unlock(*mutex
);
113 static boolean
radeon_get_drm_value(int fd
, unsigned request
,
114 const char *errname
, uint32_t *out
)
116 struct drm_radeon_info info
;
119 memset(&info
, 0, sizeof(info
));
121 info
.value
= (unsigned long)out
;
122 info
.request
= request
;
124 retval
= drmCommandWriteRead(fd
, DRM_RADEON_INFO
, &info
, sizeof(info
));
127 fprintf(stderr
, "radeon: Failed to get %s, error number %d\n",
135 /* Helper function to do the ioctls needed for setup and init. */
136 static boolean
do_winsys_init(struct radeon_drm_winsys
*ws
)
138 struct drm_radeon_gem_info gem_info
;
140 drmVersionPtr version
;
142 memset(&gem_info
, 0, sizeof(gem_info
));
144 /* We do things in a specific order here.
146 * DRM version first. We need to be sure we're running on a KMS chipset.
147 * This is also for some features.
149 * Then, the PCI ID. This is essential and should return usable numbers
150 * for all Radeons. If this fails, we probably got handed an FD for some
153 * The GEM info is actually bogus on the kernel side, as well as our side
154 * (see radeon_gem_info_ioctl in radeon_gem.c) but that's alright because
155 * we don't actually use the info for anything yet.
157 * The GB and Z pipe requests should always succeed, but they might not
158 * return sensical values for all chipsets, but that's alright because
159 * the pipe drivers already know that.
162 /* Get DRM version. */
163 version
= drmGetVersion(ws
->fd
);
164 if (version
->version_major
!= 2 ||
165 version
->version_minor
< 3) {
166 fprintf(stderr
, "%s: DRM version is %d.%d.%d but this driver is "
167 "only compatible with 2.3.x (kernel 2.6.34) or later.\n",
169 version
->version_major
,
170 version
->version_minor
,
171 version
->version_patchlevel
);
172 drmFreeVersion(version
);
176 ws
->info
.drm_major
= version
->version_major
;
177 ws
->info
.drm_minor
= version
->version_minor
;
178 ws
->info
.drm_patchlevel
= version
->version_patchlevel
;
179 drmFreeVersion(version
);
182 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_DEVICE_ID
, "PCI ID",
187 switch (ws
->info
.pci_id
) {
188 #define CHIPSET(pci_id, name, cfamily) case pci_id: ws->info.family = CHIP_##cfamily; ws->gen = DRV_R300; break;
189 #include "pci_ids/r300_pci_ids.h"
192 #define CHIPSET(pci_id, name, cfamily) case pci_id: ws->info.family = CHIP_##cfamily; ws->gen = DRV_R600; break;
193 #include "pci_ids/r600_pci_ids.h"
196 #define CHIPSET(pci_id, name, cfamily) case pci_id: ws->info.family = CHIP_##cfamily; ws->gen = DRV_SI; break;
197 #include "pci_ids/radeonsi_pci_ids.h"
201 fprintf(stderr
, "radeon: Invalid PCI ID.\n");
205 switch (ws
->info
.family
) {
208 fprintf(stderr
, "radeon: Unknown family.\n");
218 ws
->info
.chip_class
= R300
;
220 case CHIP_R420
: /* R4xx-based cores. */
229 ws
->info
.chip_class
= R400
;
231 case CHIP_RV515
: /* R5xx-based cores. */
237 ws
->info
.chip_class
= R500
;
247 ws
->info
.chip_class
= R600
;
253 ws
->info
.chip_class
= R700
;
266 ws
->info
.chip_class
= EVERGREEN
;
270 ws
->info
.chip_class
= CAYMAN
;
277 ws
->info
.chip_class
= SI
;
284 ws
->info
.chip_class
= CIK
;
289 ws
->info
.r600_has_dma
= FALSE
;
290 /* DMA is disabled on R700. There is IB corruption and hangs. */
291 if (ws
->info
.chip_class
>= EVERGREEN
&& ws
->info
.drm_minor
>= 27) {
292 ws
->info
.r600_has_dma
= TRUE
;
295 /* Check for UVD and VCE */
296 ws
->info
.has_uvd
= FALSE
;
297 ws
->info
.vce_fw_version
= 0x00000000;
298 if (ws
->info
.drm_minor
>= 32) {
299 uint32_t value
= RADEON_CS_RING_UVD
;
300 if (radeon_get_drm_value(ws
->fd
, RADEON_INFO_RING_WORKING
,
301 "UVD Ring working", &value
))
302 ws
->info
.has_uvd
= value
;
304 value
= RADEON_CS_RING_VCE
;
305 if (radeon_get_drm_value(ws
->fd
, RADEON_INFO_RING_WORKING
,
306 NULL
, &value
) && value
) {
308 if (radeon_get_drm_value(ws
->fd
, RADEON_INFO_VCE_FW_VERSION
,
309 "VCE FW version", &value
))
310 ws
->info
.vce_fw_version
= value
;
315 retval
= drmCommandWriteRead(ws
->fd
, DRM_RADEON_GEM_INFO
,
316 &gem_info
, sizeof(gem_info
));
318 fprintf(stderr
, "radeon: Failed to get MM info, error number %d\n",
322 ws
->info
.gart_size
= gem_info
.gart_size
;
323 ws
->info
.vram_size
= gem_info
.vram_size
;
325 /* Get max clock frequency info and convert it to MHz */
326 radeon_get_drm_value(ws
->fd
, RADEON_INFO_MAX_SCLK
, NULL
,
328 ws
->info
.max_sclk
/= 1000;
330 ws
->num_cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
332 /* Generation-specific queries. */
333 if (ws
->gen
== DRV_R300
) {
334 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_NUM_GB_PIPES
,
336 &ws
->info
.r300_num_gb_pipes
))
339 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_NUM_Z_PIPES
,
341 &ws
->info
.r300_num_z_pipes
))
344 else if (ws
->gen
>= DRV_R600
) {
345 if (ws
->info
.drm_minor
>= 9 &&
346 !radeon_get_drm_value(ws
->fd
, RADEON_INFO_NUM_BACKENDS
,
348 &ws
->info
.r600_num_backends
))
351 /* get the GPU counter frequency, failure is not fatal */
352 radeon_get_drm_value(ws
->fd
, RADEON_INFO_CLOCK_CRYSTAL_FREQ
, NULL
,
353 &ws
->info
.r600_clock_crystal_freq
);
355 radeon_get_drm_value(ws
->fd
, RADEON_INFO_TILING_CONFIG
, NULL
,
356 &ws
->info
.r600_tiling_config
);
358 if (ws
->info
.drm_minor
>= 11) {
359 radeon_get_drm_value(ws
->fd
, RADEON_INFO_NUM_TILE_PIPES
, NULL
,
360 &ws
->info
.r600_num_tile_pipes
);
362 if (radeon_get_drm_value(ws
->fd
, RADEON_INFO_BACKEND_MAP
, NULL
,
363 &ws
->info
.r600_backend_map
))
364 ws
->info
.r600_backend_map_valid
= TRUE
;
367 ws
->info
.r600_virtual_address
= FALSE
;
368 if (ws
->info
.drm_minor
>= 13) {
369 uint32_t ib_vm_max_size
;
371 ws
->info
.r600_virtual_address
= TRUE
;
372 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_VA_START
, NULL
,
374 ws
->info
.r600_virtual_address
= FALSE
;
375 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_IB_VM_MAX_SIZE
, NULL
,
377 ws
->info
.r600_virtual_address
= FALSE
;
379 if (ws
->gen
== DRV_R600
&& !debug_get_bool_option("RADEON_VA", FALSE
))
380 ws
->info
.r600_virtual_address
= FALSE
;
383 /* Get max pipes, this is only needed for compute shaders. All evergreen+
384 * chips have at least 2 pipes, so we use 2 as a default. */
385 ws
->info
.r600_max_pipes
= 2;
386 radeon_get_drm_value(ws
->fd
, RADEON_INFO_MAX_PIPES
, NULL
,
387 &ws
->info
.r600_max_pipes
);
389 radeon_get_drm_value(ws
->fd
, RADEON_INFO_ACTIVE_CU_COUNT
, NULL
,
390 &ws
->info
.max_compute_units
);
392 radeon_get_drm_value(ws
->fd
, RADEON_INFO_MAX_SE
, NULL
,
395 if (!ws
->info
.max_se
) {
396 switch (ws
->info
.family
) {
415 radeon_get_drm_value(ws
->fd
, RADEON_INFO_MAX_SH_PER_SE
, NULL
,
416 &ws
->info
.max_sh_per_se
);
418 if (radeon_get_drm_value(ws
->fd
, RADEON_INFO_SI_TILE_MODE_ARRAY
, NULL
,
419 ws
->info
.si_tile_mode_array
)) {
420 ws
->info
.si_tile_mode_array_valid
= TRUE
;
423 if (radeon_get_drm_value(ws
->fd
, RADEON_INFO_CIK_MACROTILE_MODE_ARRAY
, NULL
,
424 ws
->info
.cik_macrotile_mode_array
)) {
425 ws
->info
.cik_macrotile_mode_array_valid
= TRUE
;
431 static void radeon_winsys_destroy(struct radeon_winsys
*rws
)
433 struct radeon_drm_winsys
*ws
= (struct radeon_drm_winsys
*)rws
;
437 pipe_semaphore_signal(&ws
->cs_queued
);
438 pipe_thread_wait(ws
->thread
);
440 pipe_semaphore_destroy(&ws
->cs_queued
);
442 pipe_mutex_destroy(ws
->hyperz_owner_mutex
);
443 pipe_mutex_destroy(ws
->cmask_owner_mutex
);
444 pipe_mutex_destroy(ws
->cs_stack_lock
);
446 ws
->cman_vram
->destroy(ws
->cman_vram
);
447 ws
->cman_vram_gtt_wc
->destroy(ws
->cman_vram_gtt_wc
);
448 ws
->cman_gtt
->destroy(ws
->cman_gtt
);
449 ws
->cman_gtt_wc
->destroy(ws
->cman_gtt_wc
);
450 ws
->kman
->destroy(ws
->kman
);
451 if (ws
->gen
>= DRV_R600
) {
452 radeon_surface_manager_free(ws
->surf_man
);
457 static void radeon_query_info(struct radeon_winsys
*rws
,
458 struct radeon_info
*info
)
460 *info
= ((struct radeon_drm_winsys
*)rws
)->info
;
463 static boolean
radeon_cs_request_feature(struct radeon_winsys_cs
*rcs
,
464 enum radeon_feature_id fid
,
467 struct radeon_drm_cs
*cs
= radeon_drm_cs(rcs
);
470 case RADEON_FID_R300_HYPERZ_ACCESS
:
471 return radeon_set_fd_access(cs
, &cs
->ws
->hyperz_owner
,
472 &cs
->ws
->hyperz_owner_mutex
,
473 RADEON_INFO_WANT_HYPERZ
, "Hyper-Z",
476 case RADEON_FID_R300_CMASK_ACCESS
:
477 return radeon_set_fd_access(cs
, &cs
->ws
->cmask_owner
,
478 &cs
->ws
->cmask_owner_mutex
,
479 RADEON_INFO_WANT_CMASK
, "AA optimizations",
485 static int radeon_drm_winsys_surface_init(struct radeon_winsys
*rws
,
486 struct radeon_surface
*surf
)
488 struct radeon_drm_winsys
*ws
= (struct radeon_drm_winsys
*)rws
;
490 return radeon_surface_init(ws
->surf_man
, surf
);
493 static int radeon_drm_winsys_surface_best(struct radeon_winsys
*rws
,
494 struct radeon_surface
*surf
)
496 struct radeon_drm_winsys
*ws
= (struct radeon_drm_winsys
*)rws
;
498 return radeon_surface_best(ws
->surf_man
, surf
);
501 static uint64_t radeon_query_value(struct radeon_winsys
*rws
,
502 enum radeon_value_id value
)
504 struct radeon_drm_winsys
*ws
= (struct radeon_drm_winsys
*)rws
;
508 case RADEON_REQUESTED_VRAM_MEMORY
:
509 return ws
->allocated_vram
;
510 case RADEON_REQUESTED_GTT_MEMORY
:
511 return ws
->allocated_gtt
;
512 case RADEON_BUFFER_WAIT_TIME_NS
:
513 return ws
->buffer_wait_time
;
514 case RADEON_TIMESTAMP
:
515 if (ws
->info
.drm_minor
< 20 || ws
->gen
< DRV_R600
) {
520 radeon_get_drm_value(ws
->fd
, RADEON_INFO_TIMESTAMP
, "timestamp",
523 case RADEON_NUM_CS_FLUSHES
:
524 return ws
->num_cs_flushes
;
525 case RADEON_NUM_BYTES_MOVED
:
526 radeon_get_drm_value(ws
->fd
, RADEON_INFO_NUM_BYTES_MOVED
,
527 "num-bytes-moved", (uint32_t*)&retval
);
529 case RADEON_VRAM_USAGE
:
530 radeon_get_drm_value(ws
->fd
, RADEON_INFO_VRAM_USAGE
,
531 "vram-usage", (uint32_t*)&retval
);
533 case RADEON_GTT_USAGE
:
534 radeon_get_drm_value(ws
->fd
, RADEON_INFO_GTT_USAGE
,
535 "gtt-usage", (uint32_t*)&retval
);
541 static unsigned hash_fd(void *key
)
543 int fd
= pointer_to_intptr(key
);
547 return stat
.st_dev
^ stat
.st_ino
^ stat
.st_rdev
;
550 static int compare_fd(void *key1
, void *key2
)
552 int fd1
= pointer_to_intptr(key1
);
553 int fd2
= pointer_to_intptr(key2
);
554 struct stat stat1
, stat2
;
558 return stat1
.st_dev
!= stat2
.st_dev
||
559 stat1
.st_ino
!= stat2
.st_ino
||
560 stat1
.st_rdev
!= stat2
.st_rdev
;
563 void radeon_drm_ws_queue_cs(struct radeon_drm_winsys
*ws
, struct radeon_drm_cs
*cs
)
566 pipe_mutex_lock(ws
->cs_stack_lock
);
567 if (ws
->ncs
>= RING_LAST
) {
568 /* no room left for a flush */
569 pipe_mutex_unlock(ws
->cs_stack_lock
);
572 ws
->cs_stack
[ws
->ncs
++] = cs
;
573 pipe_mutex_unlock(ws
->cs_stack_lock
);
574 pipe_semaphore_signal(&ws
->cs_queued
);
577 static PIPE_THREAD_ROUTINE(radeon_drm_cs_emit_ioctl
, param
)
579 struct radeon_drm_winsys
*ws
= (struct radeon_drm_winsys
*)param
;
580 struct radeon_drm_cs
*cs
;
584 pipe_semaphore_wait(&ws
->cs_queued
);
588 pipe_mutex_lock(ws
->cs_stack_lock
);
589 cs
= ws
->cs_stack
[0];
590 for (i
= 1; i
< ws
->ncs
; i
++)
591 ws
->cs_stack
[i
- 1] = ws
->cs_stack
[i
];
592 ws
->cs_stack
[--ws
->ncs
] = NULL
;
593 pipe_mutex_unlock(ws
->cs_stack_lock
);
596 radeon_drm_cs_emit_ioctl_oneshot(cs
, cs
->cst
);
597 pipe_semaphore_signal(&cs
->flush_completed
);
600 pipe_mutex_lock(ws
->cs_stack_lock
);
601 for (i
= 0; i
< ws
->ncs
; i
++) {
602 pipe_semaphore_signal(&ws
->cs_stack
[i
]->flush_completed
);
603 ws
->cs_stack
[i
] = NULL
;
606 pipe_mutex_unlock(ws
->cs_stack_lock
);
610 DEBUG_GET_ONCE_BOOL_OPTION(thread
, "RADEON_THREAD", TRUE
)
611 static PIPE_THREAD_ROUTINE(radeon_drm_cs_emit_ioctl
, param
);
613 static bool radeon_winsys_unref(struct radeon_winsys
*ws
)
615 struct radeon_drm_winsys
*rws
= (struct radeon_drm_winsys
*)ws
;
618 /* When the reference counter drops to zero, remove the fd from the table.
619 * This must happen while the mutex is locked, so that
620 * radeon_drm_winsys_create in another thread doesn't get the winsys
621 * from the table when the counter drops to 0. */
622 pipe_mutex_lock(fd_tab_mutex
);
624 destroy
= pipe_reference(&rws
->reference
, NULL
);
625 if (destroy
&& fd_tab
)
626 util_hash_table_remove(fd_tab
, intptr_to_pointer(rws
->fd
));
628 pipe_mutex_unlock(fd_tab_mutex
);
632 PUBLIC
struct radeon_winsys
*
633 radeon_drm_winsys_create(int fd
, radeon_screen_create_t screen_create
)
635 struct radeon_drm_winsys
*ws
;
637 pipe_mutex_lock(fd_tab_mutex
);
639 fd_tab
= util_hash_table_create(hash_fd
, compare_fd
);
642 ws
= util_hash_table_get(fd_tab
, intptr_to_pointer(fd
));
644 pipe_reference(NULL
, &ws
->reference
);
645 pipe_mutex_unlock(fd_tab_mutex
);
649 ws
= CALLOC_STRUCT(radeon_drm_winsys
);
651 pipe_mutex_unlock(fd_tab_mutex
);
657 if (!do_winsys_init(ws
))
660 /* Create managers. */
661 ws
->kman
= radeon_bomgr_create(ws
);
664 ws
->cman_vram
= pb_cache_manager_create(ws
->kman
, 1000000, 2.0f
, 0);
667 ws
->cman_vram_gtt_wc
= pb_cache_manager_create(ws
->kman
, 1000000, 2.0f
, 0);
668 if (!ws
->cman_vram_gtt_wc
)
670 ws
->cman_gtt
= pb_cache_manager_create(ws
->kman
, 1000000, 2.0f
, 0);
673 ws
->cman_gtt_wc
= pb_cache_manager_create(ws
->kman
, 1000000, 2.0f
, 0);
674 if (!ws
->cman_gtt_wc
)
677 if (ws
->gen
>= DRV_R600
) {
678 ws
->surf_man
= radeon_surface_manager_new(fd
);
684 pipe_reference_init(&ws
->reference
, 1);
687 ws
->base
.unref
= radeon_winsys_unref
;
688 ws
->base
.destroy
= radeon_winsys_destroy
;
689 ws
->base
.query_info
= radeon_query_info
;
690 ws
->base
.cs_request_feature
= radeon_cs_request_feature
;
691 ws
->base
.surface_init
= radeon_drm_winsys_surface_init
;
692 ws
->base
.surface_best
= radeon_drm_winsys_surface_best
;
693 ws
->base
.query_value
= radeon_query_value
;
695 radeon_bomgr_init_functions(ws
);
696 radeon_drm_cs_init_functions(ws
);
698 pipe_mutex_init(ws
->hyperz_owner_mutex
);
699 pipe_mutex_init(ws
->cmask_owner_mutex
);
700 pipe_mutex_init(ws
->cs_stack_lock
);
703 pipe_semaphore_init(&ws
->cs_queued
, 0);
704 if (ws
->num_cpus
> 1 && debug_get_option_thread())
705 ws
->thread
= pipe_thread_create(radeon_drm_cs_emit_ioctl
, ws
);
707 /* Create the screen at the end. The winsys must be initialized
710 * Alternatively, we could create the screen based on "ws->gen"
711 * and link all drivers into one binary blob. */
712 ws
->base
.screen
= screen_create(&ws
->base
);
713 if (!ws
->base
.screen
) {
714 radeon_winsys_destroy(&ws
->base
);
715 pipe_mutex_unlock(fd_tab_mutex
);
719 util_hash_table_set(fd_tab
, intptr_to_pointer(fd
), ws
);
721 /* We must unlock the mutex once the winsys is fully initialized, so that
722 * other threads attempting to create the winsys from the same fd will
723 * get a fully initialized winsys and not just half-way initialized. */
724 pipe_mutex_unlock(fd_tab_mutex
);
729 pipe_mutex_unlock(fd_tab_mutex
);
731 ws
->cman_gtt
->destroy(ws
->cman_gtt
);
733 ws
->cman_gtt_wc
->destroy(ws
->cman_gtt_wc
);
735 ws
->cman_vram
->destroy(ws
->cman_vram
);
736 if (ws
->cman_vram_gtt_wc
)
737 ws
->cman_vram_gtt_wc
->destroy(ws
->cman_vram_gtt_wc
);
739 ws
->kman
->destroy(ws
->kman
);
741 radeon_surface_manager_free(ws
->surf_man
);