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"
46 * this are copy from radeon_drm, once an updated libdrm is released
47 * we should bump configure.ac requirement for it and remove the following
50 #ifndef RADEON_INFO_TILING_CONFIG
51 #define RADEON_INFO_TILING_CONFIG 6
54 #ifndef RADEON_INFO_WANT_HYPERZ
55 #define RADEON_INFO_WANT_HYPERZ 7
58 #ifndef RADEON_INFO_WANT_CMASK
59 #define RADEON_INFO_WANT_CMASK 8
62 #ifndef RADEON_INFO_CLOCK_CRYSTAL_FREQ
63 #define RADEON_INFO_CLOCK_CRYSTAL_FREQ 9
66 #ifndef RADEON_INFO_NUM_BACKENDS
67 #define RADEON_INFO_NUM_BACKENDS 0xa
70 #ifndef RADEON_INFO_NUM_TILE_PIPES
71 #define RADEON_INFO_NUM_TILE_PIPES 0xb
74 #ifndef RADEON_INFO_BACKEND_MAP
75 #define RADEON_INFO_BACKEND_MAP 0xd
78 #ifndef RADEON_INFO_VA_START
79 /* virtual address start, va < start are reserved by the kernel */
80 #define RADEON_INFO_VA_START 0x0e
81 /* maximum size of ib using the virtual memory cs */
82 #define RADEON_INFO_IB_VM_MAX_SIZE 0x0f
85 #ifndef RADEON_INFO_MAX_PIPES
86 #define RADEON_INFO_MAX_PIPES 0x10
89 #ifndef RADEON_INFO_TIMESTAMP
90 #define RADEON_INFO_TIMESTAMP 0x11
93 static struct util_hash_table
*fd_tab
= NULL
;
95 /* Enable/disable feature access for one command stream.
96 * If enable == TRUE, return TRUE on success.
97 * Otherwise, return FALSE.
99 * We basically do the same thing kernel does, because we have to deal
100 * with multiple contexts (here command streams) backed by one winsys. */
101 static boolean
radeon_set_fd_access(struct radeon_drm_cs
*applier
,
102 struct radeon_drm_cs
**owner
,
104 unsigned request
, boolean enable
)
106 struct drm_radeon_info info
;
107 unsigned value
= enable
? 1 : 0;
109 memset(&info
, 0, sizeof(info
));
111 pipe_mutex_lock(*mutex
);
113 /* Early exit if we are sure the request will fail. */
116 pipe_mutex_unlock(*mutex
);
120 if (*owner
!= applier
) {
121 pipe_mutex_unlock(*mutex
);
126 /* Pass through the request to the kernel. */
127 info
.value
= (unsigned long)&value
;
128 info
.request
= request
;
129 if (drmCommandWriteRead(applier
->ws
->fd
, DRM_RADEON_INFO
,
130 &info
, sizeof(info
)) != 0) {
131 pipe_mutex_unlock(*mutex
);
135 /* Update the rights in the winsys. */
139 if (request
== RADEON_INFO_WANT_HYPERZ
) {
140 printf("radeon: Acquired Hyper-Z.\n");
142 pipe_mutex_unlock(*mutex
);
147 if (request
== RADEON_INFO_WANT_HYPERZ
) {
148 printf("radeon: Released Hyper-Z.\n");
152 pipe_mutex_unlock(*mutex
);
156 static boolean
radeon_get_drm_value(int fd
, unsigned request
,
157 const char *errname
, uint32_t *out
)
159 struct drm_radeon_info info
;
162 memset(&info
, 0, sizeof(info
));
164 info
.value
= (unsigned long)out
;
165 info
.request
= request
;
167 retval
= drmCommandWriteRead(fd
, DRM_RADEON_INFO
, &info
, sizeof(info
));
170 fprintf(stderr
, "radeon: Failed to get %s, error number %d\n",
178 /* Helper function to do the ioctls needed for setup and init. */
179 static boolean
do_winsys_init(struct radeon_drm_winsys
*ws
)
181 struct drm_radeon_gem_info gem_info
;
183 drmVersionPtr version
;
185 memset(&gem_info
, 0, sizeof(gem_info
));
187 /* We do things in a specific order here.
189 * DRM version first. We need to be sure we're running on a KMS chipset.
190 * This is also for some features.
192 * Then, the PCI ID. This is essential and should return usable numbers
193 * for all Radeons. If this fails, we probably got handed an FD for some
196 * The GEM info is actually bogus on the kernel side, as well as our side
197 * (see radeon_gem_info_ioctl in radeon_gem.c) but that's alright because
198 * we don't actually use the info for anything yet.
200 * The GB and Z pipe requests should always succeed, but they might not
201 * return sensical values for all chipsets, but that's alright because
202 * the pipe drivers already know that.
205 /* Get DRM version. */
206 version
= drmGetVersion(ws
->fd
);
207 if (version
->version_major
!= 2 ||
208 version
->version_minor
< 3) {
209 fprintf(stderr
, "%s: DRM version is %d.%d.%d but this driver is "
210 "only compatible with 2.3.x (kernel 2.6.34) or later.\n",
212 version
->version_major
,
213 version
->version_minor
,
214 version
->version_patchlevel
);
215 drmFreeVersion(version
);
219 ws
->info
.drm_major
= version
->version_major
;
220 ws
->info
.drm_minor
= version
->version_minor
;
221 ws
->info
.drm_patchlevel
= version
->version_patchlevel
;
222 drmFreeVersion(version
);
225 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_DEVICE_ID
, "PCI ID",
230 switch (ws
->info
.pci_id
) {
231 #define CHIPSET(pci_id, name, cfamily) case pci_id: ws->info.family = CHIP_##cfamily; ws->gen = DRV_R300; break;
232 #include "pci_ids/r300_pci_ids.h"
235 #define CHIPSET(pci_id, name, cfamily) case pci_id: ws->info.family = CHIP_##cfamily; ws->gen = DRV_R600; break;
236 #include "pci_ids/r600_pci_ids.h"
239 #define CHIPSET(pci_id, name, cfamily) case pci_id: ws->info.family = CHIP_##cfamily; ws->gen = DRV_SI; break;
240 #include "pci_ids/radeonsi_pci_ids.h"
244 fprintf(stderr
, "radeon: Invalid PCI ID.\n");
248 switch (ws
->info
.family
) {
251 fprintf(stderr
, "radeon: Unknown family.\n");
261 ws
->info
.chip_class
= R300
;
263 case CHIP_R420
: /* R4xx-based cores. */
272 ws
->info
.chip_class
= R400
;
274 case CHIP_RV515
: /* R5xx-based cores. */
280 ws
->info
.chip_class
= R500
;
290 ws
->info
.chip_class
= R600
;
296 ws
->info
.chip_class
= R700
;
309 ws
->info
.chip_class
= EVERGREEN
;
313 ws
->info
.chip_class
= CAYMAN
;
318 ws
->info
.chip_class
= TAHITI
;
323 retval
= drmCommandWriteRead(ws
->fd
, DRM_RADEON_GEM_INFO
,
324 &gem_info
, sizeof(gem_info
));
326 fprintf(stderr
, "radeon: Failed to get MM info, error number %d\n",
330 ws
->info
.gart_size
= gem_info
.gart_size
;
331 ws
->info
.vram_size
= gem_info
.vram_size
;
333 ws
->num_cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
335 /* Generation-specific queries. */
336 if (ws
->gen
== DRV_R300
) {
337 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_NUM_GB_PIPES
,
339 &ws
->info
.r300_num_gb_pipes
))
342 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_NUM_Z_PIPES
,
344 &ws
->info
.r300_num_z_pipes
))
347 else if (ws
->gen
>= DRV_R600
) {
348 if (ws
->info
.drm_minor
>= 9 &&
349 !radeon_get_drm_value(ws
->fd
, RADEON_INFO_NUM_BACKENDS
,
351 &ws
->info
.r600_num_backends
))
354 /* get the GPU counter frequency, failure is not fatal */
355 radeon_get_drm_value(ws
->fd
, RADEON_INFO_CLOCK_CRYSTAL_FREQ
, NULL
,
356 &ws
->info
.r600_clock_crystal_freq
);
358 radeon_get_drm_value(ws
->fd
, RADEON_INFO_TILING_CONFIG
, NULL
,
359 &ws
->info
.r600_tiling_config
);
361 if (ws
->info
.drm_minor
>= 11) {
362 radeon_get_drm_value(ws
->fd
, RADEON_INFO_NUM_TILE_PIPES
, NULL
,
363 &ws
->info
.r600_num_tile_pipes
);
365 if (radeon_get_drm_value(ws
->fd
, RADEON_INFO_BACKEND_MAP
, NULL
,
366 &ws
->info
.r600_backend_map
))
367 ws
->info
.r600_backend_map_valid
= TRUE
;
370 ws
->info
.r600_virtual_address
= FALSE
;
371 if (ws
->info
.drm_minor
>= 13) {
372 ws
->info
.r600_virtual_address
= TRUE
;
373 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_VA_START
, NULL
,
374 &ws
->info
.r600_va_start
))
375 ws
->info
.r600_virtual_address
= FALSE
;
376 if (!radeon_get_drm_value(ws
->fd
, RADEON_INFO_IB_VM_MAX_SIZE
, NULL
,
377 &ws
->info
.r600_ib_vm_max_size
))
378 ws
->info
.r600_virtual_address
= FALSE
;
382 /* Get max pipes, this is only needed for compute shaders. All evergreen+
383 * chips have at least 2 pipes, so we use 2 as a default. */
384 ws
->info
.r600_max_pipes
= 2;
385 radeon_get_drm_value(ws
->fd
, RADEON_INFO_MAX_PIPES
, NULL
,
386 &ws
->info
.r600_max_pipes
);
391 static void radeon_winsys_destroy(struct radeon_winsys
*rws
)
393 struct radeon_drm_winsys
*ws
= (struct radeon_drm_winsys
*)rws
;
395 if (!pipe_reference(&ws
->base
.reference
, NULL
)) {
399 pipe_mutex_destroy(ws
->hyperz_owner_mutex
);
400 pipe_mutex_destroy(ws
->cmask_owner_mutex
);
402 ws
->cman
->destroy(ws
->cman
);
403 ws
->kman
->destroy(ws
->kman
);
404 if (ws
->gen
>= DRV_R600
) {
405 radeon_surface_manager_free(ws
->surf_man
);
408 util_hash_table_remove(fd_tab
, intptr_to_pointer(ws
->fd
));
413 static void radeon_query_info(struct radeon_winsys
*rws
,
414 struct radeon_info
*info
)
416 *info
= ((struct radeon_drm_winsys
*)rws
)->info
;
419 static boolean
radeon_cs_request_feature(struct radeon_winsys_cs
*rcs
,
420 enum radeon_feature_id fid
,
423 struct radeon_drm_cs
*cs
= radeon_drm_cs(rcs
);
426 case RADEON_FID_R300_HYPERZ_ACCESS
:
427 return radeon_set_fd_access(cs
, &cs
->ws
->hyperz_owner
,
428 &cs
->ws
->hyperz_owner_mutex
,
429 RADEON_INFO_WANT_HYPERZ
, enable
);
431 case RADEON_FID_R300_CMASK_ACCESS
:
432 if (debug_get_bool_option("RADEON_CMASK", FALSE
)) {
433 return radeon_set_fd_access(cs
, &cs
->ws
->cmask_owner
,
434 &cs
->ws
->cmask_owner_mutex
,
435 RADEON_INFO_WANT_CMASK
, enable
);
443 static int radeon_drm_winsys_surface_init(struct radeon_winsys
*rws
,
444 struct radeon_surface
*surf
)
446 struct radeon_drm_winsys
*ws
= (struct radeon_drm_winsys
*)rws
;
448 return radeon_surface_init(ws
->surf_man
, surf
);
451 static int radeon_drm_winsys_surface_best(struct radeon_winsys
*rws
,
452 struct radeon_surface
*surf
)
454 struct radeon_drm_winsys
*ws
= (struct radeon_drm_winsys
*)rws
;
456 return radeon_surface_best(ws
->surf_man
, surf
);
459 static uint64_t radeon_query_timestamp(struct radeon_winsys
*rws
)
461 struct radeon_drm_winsys
*ws
= (struct radeon_drm_winsys
*)rws
;
464 if (ws
->info
.drm_minor
< 20 ||
465 ws
->gen
< DRV_R600
) {
470 radeon_get_drm_value(ws
->fd
, RADEON_INFO_TIMESTAMP
, "timestamp",
475 static unsigned hash_fd(void *key
)
477 return pointer_to_intptr(key
);
480 static int compare_fd(void *key1
, void *key2
)
482 return pointer_to_intptr(key1
) != pointer_to_intptr(key2
);
485 struct radeon_winsys
*radeon_drm_winsys_create(int fd
)
487 struct radeon_drm_winsys
*ws
;
490 fd_tab
= util_hash_table_create(hash_fd
, compare_fd
);
493 ws
= util_hash_table_get(fd_tab
, intptr_to_pointer(fd
));
495 pipe_reference(NULL
, &ws
->base
.reference
);
499 ws
= CALLOC_STRUCT(radeon_drm_winsys
);
504 util_hash_table_set(fd_tab
, intptr_to_pointer(fd
), ws
);
506 if (!do_winsys_init(ws
))
509 /* Create managers. */
510 ws
->kman
= radeon_bomgr_create(ws
);
513 ws
->cman
= pb_cache_manager_create(ws
->kman
, 1000000);
517 if (ws
->gen
>= DRV_R600
) {
518 ws
->surf_man
= radeon_surface_manager_new(fd
);
524 pipe_reference_init(&ws
->base
.reference
, 1);
527 ws
->base
.destroy
= radeon_winsys_destroy
;
528 ws
->base
.query_info
= radeon_query_info
;
529 ws
->base
.cs_request_feature
= radeon_cs_request_feature
;
530 ws
->base
.surface_init
= radeon_drm_winsys_surface_init
;
531 ws
->base
.surface_best
= radeon_drm_winsys_surface_best
;
532 ws
->base
.query_timestamp
= radeon_query_timestamp
;
534 radeon_bomgr_init_functions(ws
);
535 radeon_drm_cs_init_functions(ws
);
537 pipe_mutex_init(ws
->hyperz_owner_mutex
);
538 pipe_mutex_init(ws
->cmask_owner_mutex
);
544 ws
->cman
->destroy(ws
->cman
);
546 ws
->kman
->destroy(ws
->kman
);
548 radeon_surface_manager_free(ws
->surf_man
);