r600g: add a driver query returning the amount of requested VRAM and GTT memory
[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
45 /*
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
48 * field
49 */
50 #ifndef RADEON_INFO_TILING_CONFIG
51 #define RADEON_INFO_TILING_CONFIG 6
52 #endif
53
54 #ifndef RADEON_INFO_WANT_HYPERZ
55 #define RADEON_INFO_WANT_HYPERZ 7
56 #endif
57
58 #ifndef RADEON_INFO_WANT_CMASK
59 #define RADEON_INFO_WANT_CMASK 8
60 #endif
61
62 #ifndef RADEON_INFO_CLOCK_CRYSTAL_FREQ
63 #define RADEON_INFO_CLOCK_CRYSTAL_FREQ 9
64 #endif
65
66 #ifndef RADEON_INFO_NUM_BACKENDS
67 #define RADEON_INFO_NUM_BACKENDS 0xa
68 #endif
69
70 #ifndef RADEON_INFO_NUM_TILE_PIPES
71 #define RADEON_INFO_NUM_TILE_PIPES 0xb
72 #endif
73
74 #ifndef RADEON_INFO_BACKEND_MAP
75 #define RADEON_INFO_BACKEND_MAP 0xd
76 #endif
77
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
83 #endif
84
85 #ifndef RADEON_INFO_MAX_PIPES
86 #define RADEON_INFO_MAX_PIPES 0x10
87 #endif
88
89 #ifndef RADEON_INFO_TIMESTAMP
90 #define RADEON_INFO_TIMESTAMP 0x11
91 #endif
92
93 static struct util_hash_table *fd_tab = NULL;
94
95 /* Enable/disable feature access for one command stream.
96 * If enable == TRUE, return TRUE on success.
97 * Otherwise, return FALSE.
98 *
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,
103 pipe_mutex *mutex,
104 unsigned request, const char *request_name,
105 boolean enable)
106 {
107 struct drm_radeon_info info;
108 unsigned value = enable ? 1 : 0;
109
110 memset(&info, 0, sizeof(info));
111
112 pipe_mutex_lock(*mutex);
113
114 /* Early exit if we are sure the request will fail. */
115 if (enable) {
116 if (*owner) {
117 pipe_mutex_unlock(*mutex);
118 return FALSE;
119 }
120 } else {
121 if (*owner != applier) {
122 pipe_mutex_unlock(*mutex);
123 return FALSE;
124 }
125 }
126
127 /* Pass through the request to the kernel. */
128 info.value = (unsigned long)&value;
129 info.request = request;
130 if (drmCommandWriteRead(applier->ws->fd, DRM_RADEON_INFO,
131 &info, sizeof(info)) != 0) {
132 pipe_mutex_unlock(*mutex);
133 return FALSE;
134 }
135
136 /* Update the rights in the winsys. */
137 if (enable) {
138 if (value) {
139 *owner = applier;
140 printf("radeon: Acquired access to %s.\n", request_name);
141 pipe_mutex_unlock(*mutex);
142 return TRUE;
143 }
144 } else {
145 *owner = NULL;
146 printf("radeon: Released access to %s.\n", request_name);
147 }
148
149 pipe_mutex_unlock(*mutex);
150 return FALSE;
151 }
152
153 static boolean radeon_get_drm_value(int fd, unsigned request,
154 const char *errname, uint32_t *out)
155 {
156 struct drm_radeon_info info;
157 int retval;
158
159 memset(&info, 0, sizeof(info));
160
161 info.value = (unsigned long)out;
162 info.request = request;
163
164 retval = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
165 if (retval) {
166 if (errname) {
167 fprintf(stderr, "radeon: Failed to get %s, error number %d\n",
168 errname, retval);
169 }
170 return FALSE;
171 }
172 return TRUE;
173 }
174
175 /* Helper function to do the ioctls needed for setup and init. */
176 static boolean do_winsys_init(struct radeon_drm_winsys *ws)
177 {
178 struct drm_radeon_gem_info gem_info;
179 int retval;
180 drmVersionPtr version;
181
182 memset(&gem_info, 0, sizeof(gem_info));
183
184 /* We do things in a specific order here.
185 *
186 * DRM version first. We need to be sure we're running on a KMS chipset.
187 * This is also for some features.
188 *
189 * Then, the PCI ID. This is essential and should return usable numbers
190 * for all Radeons. If this fails, we probably got handed an FD for some
191 * non-Radeon card.
192 *
193 * The GEM info is actually bogus on the kernel side, as well as our side
194 * (see radeon_gem_info_ioctl in radeon_gem.c) but that's alright because
195 * we don't actually use the info for anything yet.
196 *
197 * The GB and Z pipe requests should always succeed, but they might not
198 * return sensical values for all chipsets, but that's alright because
199 * the pipe drivers already know that.
200 */
201
202 /* Get DRM version. */
203 version = drmGetVersion(ws->fd);
204 if (version->version_major != 2 ||
205 version->version_minor < 3) {
206 fprintf(stderr, "%s: DRM version is %d.%d.%d but this driver is "
207 "only compatible with 2.3.x (kernel 2.6.34) or later.\n",
208 __FUNCTION__,
209 version->version_major,
210 version->version_minor,
211 version->version_patchlevel);
212 drmFreeVersion(version);
213 return FALSE;
214 }
215
216 ws->info.drm_major = version->version_major;
217 ws->info.drm_minor = version->version_minor;
218 ws->info.drm_patchlevel = version->version_patchlevel;
219 drmFreeVersion(version);
220
221 /* Get PCI ID. */
222 if (!radeon_get_drm_value(ws->fd, RADEON_INFO_DEVICE_ID, "PCI ID",
223 &ws->info.pci_id))
224 return FALSE;
225
226 /* Check PCI ID. */
227 switch (ws->info.pci_id) {
228 #define CHIPSET(pci_id, name, cfamily) case pci_id: ws->info.family = CHIP_##cfamily; ws->gen = DRV_R300; break;
229 #include "pci_ids/r300_pci_ids.h"
230 #undef CHIPSET
231
232 #define CHIPSET(pci_id, name, cfamily) case pci_id: ws->info.family = CHIP_##cfamily; ws->gen = DRV_R600; break;
233 #include "pci_ids/r600_pci_ids.h"
234 #undef CHIPSET
235
236 #define CHIPSET(pci_id, name, cfamily) case pci_id: ws->info.family = CHIP_##cfamily; ws->gen = DRV_SI; break;
237 #include "pci_ids/radeonsi_pci_ids.h"
238 #undef CHIPSET
239
240 default:
241 fprintf(stderr, "radeon: Invalid PCI ID.\n");
242 return FALSE;
243 }
244
245 switch (ws->info.family) {
246 default:
247 case CHIP_UNKNOWN:
248 fprintf(stderr, "radeon: Unknown family.\n");
249 return FALSE;
250 case CHIP_R300:
251 case CHIP_R350:
252 case CHIP_RV350:
253 case CHIP_RV370:
254 case CHIP_RV380:
255 case CHIP_RS400:
256 case CHIP_RC410:
257 case CHIP_RS480:
258 ws->info.chip_class = R300;
259 break;
260 case CHIP_R420: /* R4xx-based cores. */
261 case CHIP_R423:
262 case CHIP_R430:
263 case CHIP_R480:
264 case CHIP_R481:
265 case CHIP_RV410:
266 case CHIP_RS600:
267 case CHIP_RS690:
268 case CHIP_RS740:
269 ws->info.chip_class = R400;
270 break;
271 case CHIP_RV515: /* R5xx-based cores. */
272 case CHIP_R520:
273 case CHIP_RV530:
274 case CHIP_R580:
275 case CHIP_RV560:
276 case CHIP_RV570:
277 ws->info.chip_class = R500;
278 break;
279 case CHIP_R600:
280 case CHIP_RV610:
281 case CHIP_RV630:
282 case CHIP_RV670:
283 case CHIP_RV620:
284 case CHIP_RV635:
285 case CHIP_RS780:
286 case CHIP_RS880:
287 ws->info.chip_class = R600;
288 break;
289 case CHIP_RV770:
290 case CHIP_RV730:
291 case CHIP_RV710:
292 case CHIP_RV740:
293 ws->info.chip_class = R700;
294 break;
295 case CHIP_CEDAR:
296 case CHIP_REDWOOD:
297 case CHIP_JUNIPER:
298 case CHIP_CYPRESS:
299 case CHIP_HEMLOCK:
300 case CHIP_PALM:
301 case CHIP_SUMO:
302 case CHIP_SUMO2:
303 case CHIP_BARTS:
304 case CHIP_TURKS:
305 case CHIP_CAICOS:
306 ws->info.chip_class = EVERGREEN;
307 break;
308 case CHIP_CAYMAN:
309 case CHIP_ARUBA:
310 ws->info.chip_class = CAYMAN;
311 break;
312 case CHIP_TAHITI:
313 case CHIP_PITCAIRN:
314 case CHIP_VERDE:
315 case CHIP_OLAND:
316 ws->info.chip_class = TAHITI;
317 break;
318 }
319
320 /* Check for dma */
321 ws->info.r600_has_dma = FALSE;
322 if (ws->info.chip_class >= R700 && ws->info.drm_minor >= 27) {
323 ws->info.r600_has_dma = TRUE;
324 }
325
326 /* Get GEM info. */
327 retval = drmCommandWriteRead(ws->fd, DRM_RADEON_GEM_INFO,
328 &gem_info, sizeof(gem_info));
329 if (retval) {
330 fprintf(stderr, "radeon: Failed to get MM info, error number %d\n",
331 retval);
332 return FALSE;
333 }
334 ws->info.gart_size = gem_info.gart_size;
335 ws->info.vram_size = gem_info.vram_size;
336
337 ws->num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
338
339 /* Generation-specific queries. */
340 if (ws->gen == DRV_R300) {
341 if (!radeon_get_drm_value(ws->fd, RADEON_INFO_NUM_GB_PIPES,
342 "GB pipe count",
343 &ws->info.r300_num_gb_pipes))
344 return FALSE;
345
346 if (!radeon_get_drm_value(ws->fd, RADEON_INFO_NUM_Z_PIPES,
347 "Z pipe count",
348 &ws->info.r300_num_z_pipes))
349 return FALSE;
350 }
351 else if (ws->gen >= DRV_R600) {
352 if (ws->info.drm_minor >= 9 &&
353 !radeon_get_drm_value(ws->fd, RADEON_INFO_NUM_BACKENDS,
354 "num backends",
355 &ws->info.r600_num_backends))
356 return FALSE;
357
358 /* get the GPU counter frequency, failure is not fatal */
359 radeon_get_drm_value(ws->fd, RADEON_INFO_CLOCK_CRYSTAL_FREQ, NULL,
360 &ws->info.r600_clock_crystal_freq);
361
362 radeon_get_drm_value(ws->fd, RADEON_INFO_TILING_CONFIG, NULL,
363 &ws->info.r600_tiling_config);
364
365 if (ws->info.drm_minor >= 11) {
366 radeon_get_drm_value(ws->fd, RADEON_INFO_NUM_TILE_PIPES, NULL,
367 &ws->info.r600_num_tile_pipes);
368
369 if (radeon_get_drm_value(ws->fd, RADEON_INFO_BACKEND_MAP, NULL,
370 &ws->info.r600_backend_map))
371 ws->info.r600_backend_map_valid = TRUE;
372 }
373
374 ws->info.r600_virtual_address = FALSE;
375 if (ws->info.drm_minor >= 13) {
376 ws->info.r600_virtual_address = TRUE;
377 if (!radeon_get_drm_value(ws->fd, RADEON_INFO_VA_START, NULL,
378 &ws->info.r600_va_start))
379 ws->info.r600_virtual_address = FALSE;
380 if (!radeon_get_drm_value(ws->fd, RADEON_INFO_IB_VM_MAX_SIZE, NULL,
381 &ws->info.r600_ib_vm_max_size))
382 ws->info.r600_virtual_address = FALSE;
383 }
384 }
385
386 /* Get max pipes, this is only needed for compute shaders. All evergreen+
387 * chips have at least 2 pipes, so we use 2 as a default. */
388 ws->info.r600_max_pipes = 2;
389 radeon_get_drm_value(ws->fd, RADEON_INFO_MAX_PIPES, NULL,
390 &ws->info.r600_max_pipes);
391
392 return TRUE;
393 }
394
395 static void radeon_winsys_destroy(struct radeon_winsys *rws)
396 {
397 struct radeon_drm_winsys *ws = (struct radeon_drm_winsys*)rws;
398
399 if (ws->thread) {
400 ws->kill_thread = 1;
401 pipe_semaphore_signal(&ws->cs_queued);
402 pipe_thread_wait(ws->thread);
403 }
404 pipe_semaphore_destroy(&ws->cs_queued);
405 pipe_condvar_destroy(ws->cs_queue_empty);
406
407 if (!pipe_reference(&ws->base.reference, NULL)) {
408 return;
409 }
410
411 pipe_mutex_destroy(ws->hyperz_owner_mutex);
412 pipe_mutex_destroy(ws->cmask_owner_mutex);
413 pipe_mutex_destroy(ws->cs_stack_lock);
414
415 ws->cman->destroy(ws->cman);
416 ws->kman->destroy(ws->kman);
417 if (ws->gen >= DRV_R600) {
418 radeon_surface_manager_free(ws->surf_man);
419 }
420 if (fd_tab) {
421 util_hash_table_remove(fd_tab, intptr_to_pointer(ws->fd));
422 }
423 FREE(rws);
424 }
425
426 static void radeon_query_info(struct radeon_winsys *rws,
427 struct radeon_info *info)
428 {
429 *info = ((struct radeon_drm_winsys *)rws)->info;
430 }
431
432 static boolean radeon_cs_request_feature(struct radeon_winsys_cs *rcs,
433 enum radeon_feature_id fid,
434 boolean enable)
435 {
436 struct radeon_drm_cs *cs = radeon_drm_cs(rcs);
437
438 switch (fid) {
439 case RADEON_FID_R300_HYPERZ_ACCESS:
440 return radeon_set_fd_access(cs, &cs->ws->hyperz_owner,
441 &cs->ws->hyperz_owner_mutex,
442 RADEON_INFO_WANT_HYPERZ, "Hyper-Z",
443 enable);
444
445 case RADEON_FID_R300_CMASK_ACCESS:
446 return radeon_set_fd_access(cs, &cs->ws->cmask_owner,
447 &cs->ws->cmask_owner_mutex,
448 RADEON_INFO_WANT_CMASK, "AA optimizations",
449 enable);
450 }
451 return FALSE;
452 }
453
454 static int radeon_drm_winsys_surface_init(struct radeon_winsys *rws,
455 struct radeon_surface *surf)
456 {
457 struct radeon_drm_winsys *ws = (struct radeon_drm_winsys*)rws;
458
459 return radeon_surface_init(ws->surf_man, surf);
460 }
461
462 static int radeon_drm_winsys_surface_best(struct radeon_winsys *rws,
463 struct radeon_surface *surf)
464 {
465 struct radeon_drm_winsys *ws = (struct radeon_drm_winsys*)rws;
466
467 return radeon_surface_best(ws->surf_man, surf);
468 }
469
470 static uint64_t radeon_query_timestamp(struct radeon_winsys *rws)
471 {
472 struct radeon_drm_winsys *ws = (struct radeon_drm_winsys*)rws;
473 uint64_t ts = 0;
474
475 if (ws->info.drm_minor < 20 ||
476 ws->gen < DRV_R600) {
477 assert(0);
478 return 0;
479 }
480
481 radeon_get_drm_value(ws->fd, RADEON_INFO_TIMESTAMP, "timestamp",
482 (uint32_t*)&ts);
483 return ts;
484 }
485
486 static uint64_t radeon_query_value(struct radeon_winsys *rws,
487 enum radeon_value_id value)
488 {
489 struct radeon_drm_winsys *ws = (struct radeon_drm_winsys*)rws;
490
491 switch (value) {
492 case RADEON_REQUESTED_VRAM_MEMORY:
493 return ws->allocated_vram;
494 case RADEON_REQUESTED_GTT_MEMORY:
495 return ws->allocated_gtt;
496 }
497 return 0;
498 }
499
500 static unsigned hash_fd(void *key)
501 {
502 return pointer_to_intptr(key);
503 }
504
505 static int compare_fd(void *key1, void *key2)
506 {
507 return pointer_to_intptr(key1) != pointer_to_intptr(key2);
508 }
509
510 void radeon_drm_ws_queue_cs(struct radeon_drm_winsys *ws, struct radeon_drm_cs *cs)
511 {
512 retry:
513 pipe_mutex_lock(ws->cs_stack_lock);
514 if (p_atomic_read(&ws->ncs) >= RING_LAST) {
515 /* no room left for a flush */
516 pipe_mutex_unlock(ws->cs_stack_lock);
517 goto retry;
518 }
519 ws->cs_stack[p_atomic_read(&ws->ncs)] = cs;
520 p_atomic_inc(&ws->ncs);
521 pipe_mutex_unlock(ws->cs_stack_lock);
522 pipe_semaphore_signal(&ws->cs_queued);
523 }
524
525 static PIPE_THREAD_ROUTINE(radeon_drm_cs_emit_ioctl, param)
526 {
527 struct radeon_drm_winsys *ws = (struct radeon_drm_winsys *)param;
528 struct radeon_drm_cs *cs;
529 unsigned i, empty_stack;
530
531 while (1) {
532 pipe_semaphore_wait(&ws->cs_queued);
533 if (ws->kill_thread)
534 break;
535 next:
536 pipe_mutex_lock(ws->cs_stack_lock);
537 cs = ws->cs_stack[0];
538 pipe_mutex_unlock(ws->cs_stack_lock);
539
540 if (cs) {
541 radeon_drm_cs_emit_ioctl_oneshot(cs->cst);
542
543 pipe_mutex_lock(ws->cs_stack_lock);
544 for (i = 1; i < p_atomic_read(&ws->ncs); i++) {
545 ws->cs_stack[i - 1] = ws->cs_stack[i];
546 }
547 ws->cs_stack[p_atomic_read(&ws->ncs) - 1] = NULL;
548 empty_stack = p_atomic_dec_zero(&ws->ncs);
549 if (empty_stack) {
550 pipe_condvar_signal(ws->cs_queue_empty);
551 }
552 pipe_mutex_unlock(ws->cs_stack_lock);
553
554 pipe_semaphore_signal(&cs->flush_completed);
555
556 if (!empty_stack) {
557 goto next;
558 }
559 }
560 }
561 pipe_mutex_lock(ws->cs_stack_lock);
562 for (i = 0; i < p_atomic_read(&ws->ncs); i++) {
563 pipe_semaphore_signal(&ws->cs_stack[i]->flush_completed);
564 ws->cs_stack[i] = NULL;
565 }
566 p_atomic_set(&ws->ncs, 0);
567 pipe_condvar_signal(ws->cs_queue_empty);
568 pipe_mutex_unlock(ws->cs_stack_lock);
569 return NULL;
570 }
571
572 DEBUG_GET_ONCE_BOOL_OPTION(thread, "RADEON_THREAD", TRUE)
573 static PIPE_THREAD_ROUTINE(radeon_drm_cs_emit_ioctl, param);
574
575 struct radeon_winsys *radeon_drm_winsys_create(int fd)
576 {
577 struct radeon_drm_winsys *ws;
578
579 if (!fd_tab) {
580 fd_tab = util_hash_table_create(hash_fd, compare_fd);
581 }
582
583 ws = util_hash_table_get(fd_tab, intptr_to_pointer(fd));
584 if (ws) {
585 pipe_reference(NULL, &ws->base.reference);
586 return &ws->base;
587 }
588
589 ws = CALLOC_STRUCT(radeon_drm_winsys);
590 if (!ws) {
591 return NULL;
592 }
593 ws->fd = fd;
594 util_hash_table_set(fd_tab, intptr_to_pointer(fd), ws);
595
596 if (!do_winsys_init(ws))
597 goto fail;
598
599 /* Create managers. */
600 ws->kman = radeon_bomgr_create(ws);
601 if (!ws->kman)
602 goto fail;
603 ws->cman = pb_cache_manager_create(ws->kman, 1000000);
604 if (!ws->cman)
605 goto fail;
606
607 if (ws->gen >= DRV_R600) {
608 ws->surf_man = radeon_surface_manager_new(fd);
609 if (!ws->surf_man)
610 goto fail;
611 }
612
613 /* init reference */
614 pipe_reference_init(&ws->base.reference, 1);
615
616 /* Set functions. */
617 ws->base.destroy = radeon_winsys_destroy;
618 ws->base.query_info = radeon_query_info;
619 ws->base.cs_request_feature = radeon_cs_request_feature;
620 ws->base.surface_init = radeon_drm_winsys_surface_init;
621 ws->base.surface_best = radeon_drm_winsys_surface_best;
622 ws->base.query_timestamp = radeon_query_timestamp;
623 ws->base.query_value = radeon_query_value;
624
625 radeon_bomgr_init_functions(ws);
626 radeon_drm_cs_init_functions(ws);
627
628 pipe_mutex_init(ws->hyperz_owner_mutex);
629 pipe_mutex_init(ws->cmask_owner_mutex);
630 pipe_mutex_init(ws->cs_stack_lock);
631
632 p_atomic_set(&ws->ncs, 0);
633 pipe_semaphore_init(&ws->cs_queued, 0);
634 pipe_condvar_init(ws->cs_queue_empty);
635 if (ws->num_cpus > 1 && debug_get_option_thread())
636 ws->thread = pipe_thread_create(radeon_drm_cs_emit_ioctl, ws);
637
638 return &ws->base;
639
640 fail:
641 if (ws->cman)
642 ws->cman->destroy(ws->cman);
643 if (ws->kman)
644 ws->kman->destroy(ws->kman);
645 if (ws->surf_man)
646 radeon_surface_manager_free(ws->surf_man);
647 FREE(ws);
648 return NULL;
649 }