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>
49 * this are copy from radeon_drm, once an updated libdrm is released
50 * we should bump configure.ac requirement for it and remove the following
53 #ifndef RADEON_INFO_TILING_CONFIG
54 #define RADEON_INFO_TILING_CONFIG 6
57 #ifndef RADEON_INFO_WANT_HYPERZ
58 #define RADEON_INFO_WANT_HYPERZ 7
61 #ifndef RADEON_INFO_WANT_CMASK
62 #define RADEON_INFO_WANT_CMASK 8
65 #ifndef RADEON_INFO_CLOCK_CRYSTAL_FREQ
66 #define RADEON_INFO_CLOCK_CRYSTAL_FREQ 9
69 #ifndef RADEON_INFO_NUM_BACKENDS
70 #define RADEON_INFO_NUM_BACKENDS 0xa
73 #ifndef RADEON_INFO_NUM_TILE_PIPES
74 #define RADEON_INFO_NUM_TILE_PIPES 0xb
77 #ifndef RADEON_INFO_BACKEND_MAP
78 #define RADEON_INFO_BACKEND_MAP 0xd
81 #ifndef RADEON_INFO_VA_START
82 /* virtual address start, va < start are reserved by the kernel */
83 #define RADEON_INFO_VA_START 0x0e
84 /* maximum size of ib using the virtual memory cs */
85 #define RADEON_INFO_IB_VM_MAX_SIZE 0x0f
88 #ifndef RADEON_INFO_MAX_PIPES
89 #define RADEON_INFO_MAX_PIPES 0x10
92 #ifndef RADEON_INFO_TIMESTAMP
93 #define RADEON_INFO_TIMESTAMP 0x11
96 #ifndef RADEON_INFO_RING_WORKING
97 #define RADEON_INFO_RING_WORKING 0x15
100 #ifndef RADEON_CS_RING_UVD
101 #define RADEON_CS_RING_UVD 3
104 static struct util_hash_table
*fd_tab
= NULL
;
106 /* Enable/disable feature access for one command stream.
107 * If enable == TRUE, return TRUE on success.
108 * Otherwise, return FALSE.
110 * We basically do the same thing kernel does, because we have to deal
111 * with multiple contexts (here command streams) backed by one winsys. */
112 static boolean
radeon_set_fd_access(struct radeon_drm_cs
*applier
,
113 struct radeon_drm_cs
**owner
,
115 unsigned request
, const char *request_name
,
118 struct drm_radeon_info info
;
119 unsigned value
= enable
? 1 : 0;
121 memset(&info
, 0, sizeof(info
));
123 pipe_mutex_lock(*mutex
);
125 /* Early exit if we are sure the request will fail. */
128 pipe_mutex_unlock(*mutex
);
132 if (*owner
!= applier
) {
133 pipe_mutex_unlock(*mutex
);
138 /* Pass through the request to the kernel. */
139 info
.value
= (unsigned long)&value
;
140 info
.request
= request
;
141 if (drmCommandWriteRead(applier
->ws
->fd
, DRM_RADEON_INFO
,
142 &info
, sizeof(info
)) != 0) {
143 pipe_mutex_unlock(*mutex
);
147 /* Update the rights in the winsys. */
151 printf("radeon: Acquired access to %s.\n", request_name
);
152 pipe_mutex_unlock(*mutex
);
157 printf("radeon: Released access to %s.\n", request_name
);
160 pipe_mutex_unlock(*mutex
);
164 static boolean
radeon_get_drm_value(int fd
, unsigned request
,
165 const char *errname
, uint32_t *out
)
167 struct drm_radeon_info info
;
170 memset(&info
, 0, sizeof(info
));
172 info
.value
= (unsigned long)out
;
173 info
.request
= request
;
175 retval
= drmCommandWriteRead(fd
, DRM_RADEON_INFO
, &info
, sizeof(info
));
178 fprintf(stderr
, "radeon: Failed to get %s, error number %d\n",
186 /* Helper function to do the ioctls needed for setup and init. */
187 static boolean
do_winsys_init(struct radeon_drm_winsys
*ws
)
189 struct drm_radeon_gem_info gem_info
;
191 drmVersionPtr version
;
193 memset(&gem_info
, 0, sizeof(gem_info
));
195 /* We do things in a specific order here.
197 * DRM version first. We need to be sure we're running on a KMS chipset.
198 * This is also for some features.
200 * Then, the PCI ID. This is essential and should return usable numbers
201 * for all Radeons. If this fails, we probably got handed an FD for some
204 * The GEM info is actually bogus on the kernel side, as well as our side
205 * (see radeon_gem_info_ioctl in radeon_gem.c) but that's alright because
206 * we don't actually use the info for anything yet.
208 * The GB and Z pipe requests should always succeed, but they might not
209 * return sensical values for all chipsets, but that's alright because
210 * the pipe drivers already know that.
213 /* Get DRM version. */
214 version
= drmGetVersion(ws
->fd
);
215 if (version
->version_major
!= 2 ||
216 version
->version_minor
< 3) {
217 fprintf(stderr
, "%s: DRM version is %d.%d.%d but this driver is "
218 "only compatible with 2.3.x (kernel 2.6.34) or later.\n",
220 version
->version_major
,
221 version
->version_minor
,
222 version
->version_patchlevel
);
223 drmFreeVersion(version
);
227 ws
->info
.drm_major
= version
->version_major
;
228 ws
->info
.drm_minor
= version
->version_minor
;
229 ws
->info
.drm_patchlevel
= version
->version_patchlevel
;
230 drmFreeVersion(version
);
233 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_DEVICE_ID
, "PCI ID",
238 switch (ws
->info
.pci_id
) {
239 #define CHIPSET(pci_id, name, cfamily) case pci_id: ws->info.family = CHIP_##cfamily; ws->gen = DRV_R300; break;
240 #include "pci_ids/r300_pci_ids.h"
243 #define CHIPSET(pci_id, name, cfamily) case pci_id: ws->info.family = CHIP_##cfamily; ws->gen = DRV_R600; break;
244 #include "pci_ids/r600_pci_ids.h"
247 #define CHIPSET(pci_id, name, cfamily) case pci_id: ws->info.family = CHIP_##cfamily; ws->gen = DRV_SI; break;
248 #include "pci_ids/radeonsi_pci_ids.h"
252 fprintf(stderr
, "radeon: Invalid PCI ID.\n");
256 switch (ws
->info
.family
) {
259 fprintf(stderr
, "radeon: Unknown family.\n");
269 ws
->info
.chip_class
= R300
;
271 case CHIP_R420
: /* R4xx-based cores. */
280 ws
->info
.chip_class
= R400
;
282 case CHIP_RV515
: /* R5xx-based cores. */
288 ws
->info
.chip_class
= R500
;
298 ws
->info
.chip_class
= R600
;
304 ws
->info
.chip_class
= R700
;
317 ws
->info
.chip_class
= EVERGREEN
;
321 ws
->info
.chip_class
= CAYMAN
;
328 ws
->info
.chip_class
= SI
;
334 ws
->info
.chip_class
= CIK
;
339 ws
->info
.r600_has_dma
= FALSE
;
340 if (ws
->info
.chip_class
>= R700
&& ws
->info
.drm_minor
>= 27) {
341 ws
->info
.r600_has_dma
= TRUE
;
345 ws
->info
.has_uvd
= FALSE
;
346 if (ws
->info
.drm_minor
>= 32) {
347 uint32_t value
= RADEON_CS_RING_UVD
;
348 if (radeon_get_drm_value(ws
->fd
, RADEON_INFO_RING_WORKING
,
349 "UVD Ring working", &value
))
350 ws
->info
.has_uvd
= value
;
354 retval
= drmCommandWriteRead(ws
->fd
, DRM_RADEON_GEM_INFO
,
355 &gem_info
, sizeof(gem_info
));
357 fprintf(stderr
, "radeon: Failed to get MM info, error number %d\n",
361 ws
->info
.gart_size
= gem_info
.gart_size
;
362 ws
->info
.vram_size
= gem_info
.vram_size
;
364 ws
->num_cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
366 /* Generation-specific queries. */
367 if (ws
->gen
== DRV_R300
) {
368 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_NUM_GB_PIPES
,
370 &ws
->info
.r300_num_gb_pipes
))
373 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_NUM_Z_PIPES
,
375 &ws
->info
.r300_num_z_pipes
))
378 else if (ws
->gen
>= DRV_R600
) {
379 if (ws
->info
.drm_minor
>= 9 &&
380 !radeon_get_drm_value(ws
->fd
, RADEON_INFO_NUM_BACKENDS
,
382 &ws
->info
.r600_num_backends
))
385 /* get the GPU counter frequency, failure is not fatal */
386 radeon_get_drm_value(ws
->fd
, RADEON_INFO_CLOCK_CRYSTAL_FREQ
, NULL
,
387 &ws
->info
.r600_clock_crystal_freq
);
389 radeon_get_drm_value(ws
->fd
, RADEON_INFO_TILING_CONFIG
, NULL
,
390 &ws
->info
.r600_tiling_config
);
392 if (ws
->info
.drm_minor
>= 11) {
393 radeon_get_drm_value(ws
->fd
, RADEON_INFO_NUM_TILE_PIPES
, NULL
,
394 &ws
->info
.r600_num_tile_pipes
);
396 if (radeon_get_drm_value(ws
->fd
, RADEON_INFO_BACKEND_MAP
, NULL
,
397 &ws
->info
.r600_backend_map
))
398 ws
->info
.r600_backend_map_valid
= TRUE
;
401 ws
->info
.r600_virtual_address
= FALSE
;
402 if (ws
->info
.drm_minor
>= 13) {
403 ws
->info
.r600_virtual_address
= TRUE
;
404 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_VA_START
, NULL
,
405 &ws
->info
.r600_va_start
))
406 ws
->info
.r600_virtual_address
= FALSE
;
407 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_IB_VM_MAX_SIZE
, NULL
,
408 &ws
->info
.r600_ib_vm_max_size
))
409 ws
->info
.r600_virtual_address
= FALSE
;
411 if (ws
->gen
== DRV_R600
&& !debug_get_bool_option("RADEON_VA", FALSE
))
412 ws
->info
.r600_virtual_address
= FALSE
;
415 /* Get max pipes, this is only needed for compute shaders. All evergreen+
416 * chips have at least 2 pipes, so we use 2 as a default. */
417 ws
->info
.r600_max_pipes
= 2;
418 radeon_get_drm_value(ws
->fd
, RADEON_INFO_MAX_PIPES
, NULL
,
419 &ws
->info
.r600_max_pipes
);
421 if (radeon_get_drm_value(ws
->fd
, RADEON_INFO_SI_TILE_MODE_ARRAY
, NULL
,
422 ws
->info
.si_tile_mode_array
)) {
423 ws
->info
.si_tile_mode_array_valid
= TRUE
;
426 if (radeon_get_drm_value(ws
->fd
, RADEON_INFO_CIK_MACROTILE_MODE_ARRAY
, NULL
,
427 ws
->info
.cik_macrotile_mode_array
)) {
428 ws
->info
.cik_macrotile_mode_array_valid
= TRUE
;
434 static void radeon_winsys_destroy(struct radeon_winsys
*rws
)
436 struct radeon_drm_winsys
*ws
= (struct radeon_drm_winsys
*)rws
;
440 pipe_semaphore_signal(&ws
->cs_queued
);
441 pipe_thread_wait(ws
->thread
);
443 pipe_semaphore_destroy(&ws
->cs_queued
);
445 pipe_mutex_destroy(ws
->hyperz_owner_mutex
);
446 pipe_mutex_destroy(ws
->cmask_owner_mutex
);
447 pipe_mutex_destroy(ws
->cs_stack_lock
);
449 ws
->cman
->destroy(ws
->cman
);
450 ws
->kman
->destroy(ws
->kman
);
451 if (ws
->gen
>= DRV_R600
) {
452 radeon_surface_manager_free(ws
->surf_man
);
455 util_hash_table_remove(fd_tab
, intptr_to_pointer(ws
->fd
));
460 static void radeon_query_info(struct radeon_winsys
*rws
,
461 struct radeon_info
*info
)
463 *info
= ((struct radeon_drm_winsys
*)rws
)->info
;
466 static boolean
radeon_cs_request_feature(struct radeon_winsys_cs
*rcs
,
467 enum radeon_feature_id fid
,
470 struct radeon_drm_cs
*cs
= radeon_drm_cs(rcs
);
473 case RADEON_FID_R300_HYPERZ_ACCESS
:
474 return radeon_set_fd_access(cs
, &cs
->ws
->hyperz_owner
,
475 &cs
->ws
->hyperz_owner_mutex
,
476 RADEON_INFO_WANT_HYPERZ
, "Hyper-Z",
479 case RADEON_FID_R300_CMASK_ACCESS
:
480 return radeon_set_fd_access(cs
, &cs
->ws
->cmask_owner
,
481 &cs
->ws
->cmask_owner_mutex
,
482 RADEON_INFO_WANT_CMASK
, "AA optimizations",
488 static int radeon_drm_winsys_surface_init(struct radeon_winsys
*rws
,
489 struct radeon_surface
*surf
)
491 struct radeon_drm_winsys
*ws
= (struct radeon_drm_winsys
*)rws
;
493 return radeon_surface_init(ws
->surf_man
, surf
);
496 static int radeon_drm_winsys_surface_best(struct radeon_winsys
*rws
,
497 struct radeon_surface
*surf
)
499 struct radeon_drm_winsys
*ws
= (struct radeon_drm_winsys
*)rws
;
501 return radeon_surface_best(ws
->surf_man
, surf
);
504 static uint64_t radeon_query_value(struct radeon_winsys
*rws
,
505 enum radeon_value_id value
)
507 struct radeon_drm_winsys
*ws
= (struct radeon_drm_winsys
*)rws
;
511 case RADEON_REQUESTED_VRAM_MEMORY
:
512 return ws
->allocated_vram
;
513 case RADEON_REQUESTED_GTT_MEMORY
:
514 return ws
->allocated_gtt
;
515 case RADEON_BUFFER_WAIT_TIME_NS
:
516 return ws
->buffer_wait_time
;
517 case RADEON_TIMESTAMP
:
518 if (ws
->info
.drm_minor
< 20 || ws
->gen
< DRV_R600
) {
523 radeon_get_drm_value(ws
->fd
, RADEON_INFO_TIMESTAMP
, "timestamp",
530 static unsigned hash_fd(void *key
)
532 int fd
= pointer_to_intptr(key
);
536 return stat
.st_dev
^ stat
.st_ino
^ stat
.st_rdev
;
539 static int compare_fd(void *key1
, void *key2
)
541 int fd1
= pointer_to_intptr(key1
);
542 int fd2
= pointer_to_intptr(key2
);
543 struct stat stat1
, stat2
;
547 return stat1
.st_dev
!= stat2
.st_dev
||
548 stat1
.st_ino
!= stat2
.st_ino
||
549 stat1
.st_rdev
!= stat2
.st_rdev
;
552 void radeon_drm_ws_queue_cs(struct radeon_drm_winsys
*ws
, struct radeon_drm_cs
*cs
)
555 pipe_mutex_lock(ws
->cs_stack_lock
);
556 if (ws
->ncs
>= RING_LAST
) {
557 /* no room left for a flush */
558 pipe_mutex_unlock(ws
->cs_stack_lock
);
561 ws
->cs_stack
[ws
->ncs
++] = cs
;
562 pipe_mutex_unlock(ws
->cs_stack_lock
);
563 pipe_semaphore_signal(&ws
->cs_queued
);
566 static PIPE_THREAD_ROUTINE(radeon_drm_cs_emit_ioctl
, param
)
568 struct radeon_drm_winsys
*ws
= (struct radeon_drm_winsys
*)param
;
569 struct radeon_drm_cs
*cs
;
573 pipe_semaphore_wait(&ws
->cs_queued
);
577 pipe_mutex_lock(ws
->cs_stack_lock
);
578 cs
= ws
->cs_stack
[0];
579 for (i
= 1; i
< ws
->ncs
; i
++)
580 ws
->cs_stack
[i
- 1] = ws
->cs_stack
[i
];
581 ws
->cs_stack
[--ws
->ncs
] = NULL
;
582 pipe_mutex_unlock(ws
->cs_stack_lock
);
585 radeon_drm_cs_emit_ioctl_oneshot(cs
, cs
->cst
);
586 pipe_semaphore_signal(&cs
->flush_completed
);
589 pipe_mutex_lock(ws
->cs_stack_lock
);
590 for (i
= 0; i
< ws
->ncs
; i
++) {
591 pipe_semaphore_signal(&ws
->cs_stack
[i
]->flush_completed
);
592 ws
->cs_stack
[i
] = NULL
;
595 pipe_mutex_unlock(ws
->cs_stack_lock
);
599 DEBUG_GET_ONCE_BOOL_OPTION(thread
, "RADEON_THREAD", TRUE
)
600 static PIPE_THREAD_ROUTINE(radeon_drm_cs_emit_ioctl
, param
);
602 PUBLIC
struct radeon_winsys
*radeon_drm_winsys_create(int fd
)
604 struct radeon_drm_winsys
*ws
;
607 fd_tab
= util_hash_table_create(hash_fd
, compare_fd
);
610 ws
= util_hash_table_get(fd_tab
, intptr_to_pointer(fd
));
612 pipe_reference(NULL
, &ws
->base
.reference
);
616 ws
= CALLOC_STRUCT(radeon_drm_winsys
);
621 util_hash_table_set(fd_tab
, intptr_to_pointer(fd
), ws
);
623 if (!do_winsys_init(ws
))
626 /* Create managers. */
627 ws
->kman
= radeon_bomgr_create(ws
);
630 ws
->cman
= pb_cache_manager_create(ws
->kman
, 1000000);
634 if (ws
->gen
>= DRV_R600
) {
635 ws
->surf_man
= radeon_surface_manager_new(fd
);
641 pipe_reference_init(&ws
->base
.reference
, 1);
644 ws
->base
.destroy
= radeon_winsys_destroy
;
645 ws
->base
.query_info
= radeon_query_info
;
646 ws
->base
.cs_request_feature
= radeon_cs_request_feature
;
647 ws
->base
.surface_init
= radeon_drm_winsys_surface_init
;
648 ws
->base
.surface_best
= radeon_drm_winsys_surface_best
;
649 ws
->base
.query_value
= radeon_query_value
;
651 radeon_bomgr_init_functions(ws
);
652 radeon_drm_cs_init_functions(ws
);
654 pipe_mutex_init(ws
->hyperz_owner_mutex
);
655 pipe_mutex_init(ws
->cmask_owner_mutex
);
656 pipe_mutex_init(ws
->cs_stack_lock
);
659 pipe_semaphore_init(&ws
->cs_queued
, 0);
660 if (ws
->num_cpus
> 1 && debug_get_option_thread())
661 ws
->thread
= pipe_thread_create(radeon_drm_cs_emit_ioctl
, ws
);
667 ws
->cman
->destroy(ws
->cman
);
669 ws
->kman
->destroy(ws
->kman
);
671 radeon_surface_manager_free(ws
->surf_man
);