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 #include "radeon_drm_bo.h"
29 #include "radeon_drm_cs.h"
30 #include "radeon_drm_public.h"
31
32 #include "util/os_file.h"
33 #include "util/u_cpu_detect.h"
34 #include "util/u_memory.h"
35 #include "util/u_hash_table.h"
36 #include "util/u_pointer.h"
37
38 #include <xf86drm.h>
39 #include <stdio.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <unistd.h>
43 #include <fcntl.h>
44 #include <radeon_surface.h>
45
46 static struct hash_table *fd_tab = NULL;
47 static mtx_t fd_tab_mutex = _MTX_INITIALIZER_NP;
48
49 /* Enable/disable feature access for one command stream.
50 * If enable == true, return true on success.
51 * Otherwise, return false.
52 *
53 * We basically do the same thing kernel does, because we have to deal
54 * with multiple contexts (here command streams) backed by one winsys. */
radeon_set_fd_access(struct radeon_drm_cs * applier,struct radeon_drm_cs ** owner,mtx_t * mutex,unsigned request,const char * request_name,bool enable)55 static bool radeon_set_fd_access(struct radeon_drm_cs *applier,
56 struct radeon_drm_cs **owner,
57 mtx_t *mutex,
58 unsigned request, const char *request_name,
59 bool enable)
60 {
61 struct drm_radeon_info info;
62 unsigned value = enable ? 1 : 0;
63
64 memset(&info, 0, sizeof(info));
65
66 mtx_lock(&*mutex);
67
68 /* Early exit if we are sure the request will fail. */
69 if (enable) {
70 if (*owner) {
71 mtx_unlock(&*mutex);
72 return false;
73 }
74 } else {
75 if (*owner != applier) {
76 mtx_unlock(&*mutex);
77 return false;
78 }
79 }
80
81 /* Pass through the request to the kernel. */
82 info.value = (unsigned long)&value;
83 info.request = request;
84 if (drmCommandWriteRead(applier->ws->fd, DRM_RADEON_INFO,
85 &info, sizeof(info)) != 0) {
86 mtx_unlock(&*mutex);
87 return false;
88 }
89
90 /* Update the rights in the winsys. */
91 if (enable) {
92 if (value) {
93 *owner = applier;
94 mtx_unlock(&*mutex);
95 return true;
96 }
97 } else {
98 *owner = NULL;
99 }
100
101 mtx_unlock(&*mutex);
102 return false;
103 }
104
radeon_get_drm_value(int fd,unsigned request,const char * errname,uint32_t * out)105 static bool radeon_get_drm_value(int fd, unsigned request,
106 const char *errname, uint32_t *out)
107 {
108 struct drm_radeon_info info;
109 int retval;
110
111 memset(&info, 0, sizeof(info));
112
113 info.value = (unsigned long)out;
114 info.request = request;
115
116 retval = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
117 if (retval) {
118 if (errname) {
119 fprintf(stderr, "radeon: Failed to get %s, error number %d\n",
120 errname, retval);
121 }
122 return false;
123 }
124 return true;
125 }
126
127 /* Helper function to do the ioctls needed for setup and init. */
do_winsys_init(struct radeon_drm_winsys * ws)128 static bool do_winsys_init(struct radeon_drm_winsys *ws)
129 {
130 struct drm_radeon_gem_info gem_info;
131 int retval;
132 drmVersionPtr version;
133
134 memset(&gem_info, 0, sizeof(gem_info));
135
136 /* We do things in a specific order here.
137 *
138 * DRM version first. We need to be sure we're running on a KMS chipset.
139 * This is also for some features.
140 *
141 * Then, the PCI ID. This is essential and should return usable numbers
142 * for all Radeons. If this fails, we probably got handed an FD for some
143 * non-Radeon card.
144 *
145 * The GEM info is actually bogus on the kernel side, as well as our side
146 * (see radeon_gem_info_ioctl in radeon_gem.c) but that's alright because
147 * we don't actually use the info for anything yet.
148 *
149 * The GB and Z pipe requests should always succeed, but they might not
150 * return sensical values for all chipsets, but that's alright because
151 * the pipe drivers already know that.
152 */
153
154 /* Get DRM version. */
155 version = drmGetVersion(ws->fd);
156 if (version->version_major != 2 ||
157 version->version_minor < 12) {
158 fprintf(stderr, "%s: DRM version is %d.%d.%d but this driver is "
159 "only compatible with 2.12.0 (kernel 3.2) or later.\n",
160 __FUNCTION__,
161 version->version_major,
162 version->version_minor,
163 version->version_patchlevel);
164 drmFreeVersion(version);
165 return false;
166 }
167
168 ws->info.drm_major = version->version_major;
169 ws->info.drm_minor = version->version_minor;
170 ws->info.drm_patchlevel = version->version_patchlevel;
171 ws->info.is_amdgpu = false;
172 drmFreeVersion(version);
173
174 /* Get PCI ID. */
175 if (!radeon_get_drm_value(ws->fd, RADEON_INFO_DEVICE_ID, "PCI ID",
176 &ws->info.pci_id))
177 return false;
178
179 /* Check PCI ID. */
180 switch (ws->info.pci_id) {
181 #define CHIPSET(pci_id, name, cfamily) case pci_id: ws->info.family = CHIP_##cfamily; ws->gen = DRV_R300; break;
182 #include "pci_ids/r300_pci_ids.h"
183 #undef CHIPSET
184
185 #define CHIPSET(pci_id, name, cfamily) case pci_id: ws->info.family = CHIP_##cfamily; ws->gen = DRV_R600; break;
186 #include "pci_ids/r600_pci_ids.h"
187 #undef CHIPSET
188
189 #define CHIPSET(pci_id, cfamily) \
190 case pci_id: \
191 ws->info.family = CHIP_##cfamily; \
192 ws->info.name = #cfamily; \
193 ws->gen = DRV_SI; \
194 break;
195 #include "pci_ids/radeonsi_pci_ids.h"
196 #undef CHIPSET
197
198 default:
199 fprintf(stderr, "radeon: Invalid PCI ID.\n");
200 return false;
201 }
202
203 switch (ws->info.family) {
204 default:
205 case CHIP_UNKNOWN:
206 fprintf(stderr, "radeon: Unknown family.\n");
207 return false;
208 case CHIP_R300:
209 case CHIP_R350:
210 case CHIP_RV350:
211 case CHIP_RV370:
212 case CHIP_RV380:
213 case CHIP_RS400:
214 case CHIP_RC410:
215 case CHIP_RS480:
216 ws->info.chip_class = R300;
217 break;
218 case CHIP_R420: /* R4xx-based cores. */
219 case CHIP_R423:
220 case CHIP_R430:
221 case CHIP_R480:
222 case CHIP_R481:
223 case CHIP_RV410:
224 case CHIP_RS600:
225 case CHIP_RS690:
226 case CHIP_RS740:
227 ws->info.chip_class = R400;
228 break;
229 case CHIP_RV515: /* R5xx-based cores. */
230 case CHIP_R520:
231 case CHIP_RV530:
232 case CHIP_R580:
233 case CHIP_RV560:
234 case CHIP_RV570:
235 ws->info.chip_class = R500;
236 break;
237 case CHIP_R600:
238 case CHIP_RV610:
239 case CHIP_RV630:
240 case CHIP_RV670:
241 case CHIP_RV620:
242 case CHIP_RV635:
243 case CHIP_RS780:
244 case CHIP_RS880:
245 ws->info.chip_class = R600;
246 break;
247 case CHIP_RV770:
248 case CHIP_RV730:
249 case CHIP_RV710:
250 case CHIP_RV740:
251 ws->info.chip_class = R700;
252 break;
253 case CHIP_CEDAR:
254 case CHIP_REDWOOD:
255 case CHIP_JUNIPER:
256 case CHIP_CYPRESS:
257 case CHIP_HEMLOCK:
258 case CHIP_PALM:
259 case CHIP_SUMO:
260 case CHIP_SUMO2:
261 case CHIP_BARTS:
262 case CHIP_TURKS:
263 case CHIP_CAICOS:
264 ws->info.chip_class = EVERGREEN;
265 break;
266 case CHIP_CAYMAN:
267 case CHIP_ARUBA:
268 ws->info.chip_class = CAYMAN;
269 break;
270 case CHIP_TAHITI:
271 case CHIP_PITCAIRN:
272 case CHIP_VERDE:
273 case CHIP_OLAND:
274 case CHIP_HAINAN:
275 ws->info.chip_class = GFX6;
276 break;
277 case CHIP_BONAIRE:
278 case CHIP_KAVERI:
279 case CHIP_KABINI:
280 case CHIP_HAWAII:
281 ws->info.chip_class = GFX7;
282 break;
283 }
284
285 /* Set which chips don't have dedicated VRAM. */
286 switch (ws->info.family) {
287 case CHIP_RS400:
288 case CHIP_RC410:
289 case CHIP_RS480:
290 case CHIP_RS600:
291 case CHIP_RS690:
292 case CHIP_RS740:
293 case CHIP_RS780:
294 case CHIP_RS880:
295 case CHIP_PALM:
296 case CHIP_SUMO:
297 case CHIP_SUMO2:
298 case CHIP_ARUBA:
299 case CHIP_KAVERI:
300 case CHIP_KABINI:
301 ws->info.has_dedicated_vram = false;
302 break;
303
304 default:
305 ws->info.has_dedicated_vram = true;
306 }
307
308 ws->info.num_rings[RING_GFX] = 1;
309 /* Check for dma */
310 ws->info.num_rings[RING_DMA] = 0;
311 /* DMA is disabled on R700. There is IB corruption and hangs. */
312 if (ws->info.chip_class >= EVERGREEN && ws->info.drm_minor >= 27) {
313 ws->info.num_rings[RING_DMA] = 1;
314 }
315
316 /* Check for UVD and VCE */
317 ws->info.has_hw_decode = false;
318 ws->info.vce_fw_version = 0x00000000;
319 if (ws->info.drm_minor >= 32) {
320 uint32_t value = RADEON_CS_RING_UVD;
321 if (radeon_get_drm_value(ws->fd, RADEON_INFO_RING_WORKING,
322 "UVD Ring working", &value)) {
323 ws->info.has_hw_decode = value;
324 ws->info.num_rings[RING_UVD] = 1;
325 }
326
327 value = RADEON_CS_RING_VCE;
328 if (radeon_get_drm_value(ws->fd, RADEON_INFO_RING_WORKING,
329 NULL, &value) && value) {
330
331 if (radeon_get_drm_value(ws->fd, RADEON_INFO_VCE_FW_VERSION,
332 "VCE FW version", &value)) {
333 ws->info.vce_fw_version = value;
334 ws->info.num_rings[RING_VCE] = 1;
335 }
336 }
337 }
338
339 /* Check for userptr support. */
340 {
341 struct drm_radeon_gem_userptr args = {0};
342
343 /* If the ioctl doesn't exist, -EINVAL is returned.
344 *
345 * If the ioctl exists, it should return -EACCES
346 * if RADEON_GEM_USERPTR_READONLY or RADEON_GEM_USERPTR_REGISTER
347 * aren't set.
348 */
349 ws->info.has_userptr =
350 drmCommandWriteRead(ws->fd, DRM_RADEON_GEM_USERPTR,
351 &args, sizeof(args)) == -EACCES;
352 }
353
354 /* Get GEM info. */
355 retval = drmCommandWriteRead(ws->fd, DRM_RADEON_GEM_INFO,
356 &gem_info, sizeof(gem_info));
357 if (retval) {
358 fprintf(stderr, "radeon: Failed to get MM info, error number %d\n",
359 retval);
360 return false;
361 }
362 ws->info.gart_size = gem_info.gart_size;
363 ws->info.vram_size = gem_info.vram_size;
364 ws->info.vram_vis_size = gem_info.vram_visible;
365 /* Older versions of the kernel driver reported incorrect values, and
366 * didn't support more than 256MB of visible VRAM anyway
367 */
368 if (ws->info.drm_minor < 49)
369 ws->info.vram_vis_size = MIN2(ws->info.vram_vis_size, 256*1024*1024);
370
371 /* Radeon allocates all buffers contiguously, which makes large allocations
372 * unlikely to succeed. */
373 if (ws->info.has_dedicated_vram)
374 ws->info.max_alloc_size = ws->info.vram_size * 0.7;
375 else
376 ws->info.max_alloc_size = ws->info.gart_size * 0.7;
377
378 if (ws->info.drm_minor < 40)
379 ws->info.max_alloc_size = MIN2(ws->info.max_alloc_size, 256*1024*1024);
380 /* Both 32-bit and 64-bit address spaces only have 4GB. */
381 ws->info.max_alloc_size = MIN2(ws->info.max_alloc_size, 3ull*1024*1024*1024);
382
383 /* Get max clock frequency info and convert it to MHz */
384 radeon_get_drm_value(ws->fd, RADEON_INFO_MAX_SCLK, NULL,
385 &ws->info.max_shader_clock);
386 ws->info.max_shader_clock /= 1000;
387
388 ws->num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
389
390 /* Generation-specific queries. */
391 if (ws->gen == DRV_R300) {
392 if (!radeon_get_drm_value(ws->fd, RADEON_INFO_NUM_GB_PIPES,
393 "GB pipe count",
394 &ws->info.r300_num_gb_pipes))
395 return false;
396
397 if (!radeon_get_drm_value(ws->fd, RADEON_INFO_NUM_Z_PIPES,
398 "Z pipe count",
399 &ws->info.r300_num_z_pipes))
400 return false;
401 }
402 else if (ws->gen >= DRV_R600) {
403 uint32_t tiling_config = 0;
404
405 if (!radeon_get_drm_value(ws->fd, RADEON_INFO_NUM_BACKENDS,
406 "num backends",
407 &ws->info.num_render_backends))
408 return false;
409
410 /* get the GPU counter frequency, failure is not fatal */
411 radeon_get_drm_value(ws->fd, RADEON_INFO_CLOCK_CRYSTAL_FREQ, NULL,
412 &ws->info.clock_crystal_freq);
413
414 radeon_get_drm_value(ws->fd, RADEON_INFO_TILING_CONFIG, NULL,
415 &tiling_config);
416
417 ws->info.r600_num_banks =
418 ws->info.chip_class >= EVERGREEN ?
419 4 << ((tiling_config & 0xf0) >> 4) :
420 4 << ((tiling_config & 0x30) >> 4);
421
422 ws->info.pipe_interleave_bytes =
423 ws->info.chip_class >= EVERGREEN ?
424 256 << ((tiling_config & 0xf00) >> 8) :
425 256 << ((tiling_config & 0xc0) >> 6);
426
427 if (!ws->info.pipe_interleave_bytes)
428 ws->info.pipe_interleave_bytes =
429 ws->info.chip_class >= EVERGREEN ? 512 : 256;
430
431 radeon_get_drm_value(ws->fd, RADEON_INFO_NUM_TILE_PIPES, NULL,
432 &ws->info.num_tile_pipes);
433
434 /* "num_tiles_pipes" must be equal to the number of pipes (Px) in the
435 * pipe config field of the GB_TILE_MODE array. Only one card (Tahiti)
436 * reports a different value (12). Fix it by setting what's in the
437 * GB_TILE_MODE array (8).
438 */
439 if (ws->gen == DRV_SI && ws->info.num_tile_pipes == 12)
440 ws->info.num_tile_pipes = 8;
441
442 if (radeon_get_drm_value(ws->fd, RADEON_INFO_BACKEND_MAP, NULL,
443 &ws->info.r600_gb_backend_map))
444 ws->info.r600_gb_backend_map_valid = true;
445
446 /* Default value. */
447 ws->info.enabled_rb_mask = u_bit_consecutive(0, ws->info.num_render_backends);
448 /*
449 * This fails (silently) on non-GCN or older kernels, overwriting the
450 * default enabled_rb_mask with the result of the last query.
451 */
452 if (ws->gen >= DRV_SI)
453 radeon_get_drm_value(ws->fd, RADEON_INFO_SI_BACKEND_ENABLED_MASK, NULL,
454 &ws->info.enabled_rb_mask);
455
456 ws->info.r600_has_virtual_memory = false;
457 if (ws->info.drm_minor >= 13) {
458 uint32_t ib_vm_max_size;
459
460 ws->info.r600_has_virtual_memory = true;
461 if (!radeon_get_drm_value(ws->fd, RADEON_INFO_VA_START, NULL,
462 &ws->va_start))
463 ws->info.r600_has_virtual_memory = false;
464 if (!radeon_get_drm_value(ws->fd, RADEON_INFO_IB_VM_MAX_SIZE, NULL,
465 &ib_vm_max_size))
466 ws->info.r600_has_virtual_memory = false;
467 radeon_get_drm_value(ws->fd, RADEON_INFO_VA_UNMAP_WORKING, NULL,
468 &ws->va_unmap_working);
469 }
470 if (ws->gen == DRV_R600 && !debug_get_bool_option("RADEON_VA", false))
471 ws->info.r600_has_virtual_memory = false;
472 }
473
474 /* Get max pipes, this is only needed for compute shaders. All evergreen+
475 * chips have at least 2 pipes, so we use 2 as a default. */
476 ws->info.r600_max_quad_pipes = 2;
477 radeon_get_drm_value(ws->fd, RADEON_INFO_MAX_PIPES, NULL,
478 &ws->info.r600_max_quad_pipes);
479
480 /* All GPUs have at least one compute unit */
481 ws->info.num_good_compute_units = 1;
482 radeon_get_drm_value(ws->fd, RADEON_INFO_ACTIVE_CU_COUNT, NULL,
483 &ws->info.num_good_compute_units);
484
485 radeon_get_drm_value(ws->fd, RADEON_INFO_MAX_SE, NULL,
486 &ws->info.max_se);
487
488 switch (ws->info.family) {
489 case CHIP_HAINAN:
490 case CHIP_KABINI:
491 ws->info.num_tcc_blocks = 2;
492 break;
493 case CHIP_VERDE:
494 case CHIP_OLAND:
495 case CHIP_BONAIRE:
496 case CHIP_KAVERI:
497 ws->info.num_tcc_blocks = 4;
498 break;
499 case CHIP_PITCAIRN:
500 ws->info.num_tcc_blocks = 8;
501 break;
502 case CHIP_TAHITI:
503 ws->info.num_tcc_blocks = 12;
504 break;
505 case CHIP_HAWAII:
506 ws->info.num_tcc_blocks = 16;
507 break;
508 default:
509 ws->info.num_tcc_blocks = 0;
510 break;
511 }
512
513 if (!ws->info.max_se) {
514 switch (ws->info.family) {
515 default:
516 ws->info.max_se = 1;
517 break;
518 case CHIP_CYPRESS:
519 case CHIP_HEMLOCK:
520 case CHIP_BARTS:
521 case CHIP_CAYMAN:
522 case CHIP_TAHITI:
523 case CHIP_PITCAIRN:
524 case CHIP_BONAIRE:
525 ws->info.max_se = 2;
526 break;
527 case CHIP_HAWAII:
528 ws->info.max_se = 4;
529 break;
530 }
531 }
532
533 ws->info.num_se = ws->info.max_se;
534
535 radeon_get_drm_value(ws->fd, RADEON_INFO_MAX_SH_PER_SE, NULL,
536 &ws->info.max_sh_per_se);
537 if (ws->gen == DRV_SI) {
538 ws->info.max_good_cu_per_sa =
539 ws->info.min_good_cu_per_sa = ws->info.num_good_compute_units /
540 (ws->info.max_se * ws->info.max_sh_per_se);
541 }
542
543 radeon_get_drm_value(ws->fd, RADEON_INFO_ACCEL_WORKING2, NULL,
544 &ws->accel_working2);
545 if (ws->info.family == CHIP_HAWAII && ws->accel_working2 < 2) {
546 fprintf(stderr, "radeon: GPU acceleration for Hawaii disabled, "
547 "returned accel_working2 value %u is smaller than 2. "
548 "Please install a newer kernel.\n",
549 ws->accel_working2);
550 return false;
551 }
552
553 if (ws->info.chip_class == GFX7) {
554 if (!radeon_get_drm_value(ws->fd, RADEON_INFO_CIK_MACROTILE_MODE_ARRAY, NULL,
555 ws->info.cik_macrotile_mode_array)) {
556 fprintf(stderr, "radeon: Kernel 3.13 is required for Sea Islands support.\n");
557 return false;
558 }
559 }
560
561 if (ws->info.chip_class >= GFX6) {
562 if (!radeon_get_drm_value(ws->fd, RADEON_INFO_SI_TILE_MODE_ARRAY, NULL,
563 ws->info.si_tile_mode_array)) {
564 fprintf(stderr, "radeon: Kernel 3.10 is required for Southern Islands support.\n");
565 return false;
566 }
567 }
568
569 /* Hawaii with old firmware needs type2 nop packet.
570 * accel_working2 with value 3 indicates the new firmware.
571 */
572 ws->info.gfx_ib_pad_with_type2 = ws->info.chip_class <= GFX6 ||
573 (ws->info.family == CHIP_HAWAII &&
574 ws->accel_working2 < 3);
575 ws->info.tcc_cache_line_size = 64; /* TC L2 line size on GCN */
576 ws->info.ib_alignment = 4096;
577 ws->info.kernel_flushes_hdp_before_ib = ws->info.drm_minor >= 40;
578 /* HTILE is broken with 1D tiling on old kernels and GFX7. */
579 ws->info.htile_cmask_support_1d_tiling = ws->info.chip_class != GFX7 ||
580 ws->info.drm_minor >= 38;
581 ws->info.si_TA_CS_BC_BASE_ADDR_allowed = ws->info.drm_minor >= 48;
582 ws->info.has_bo_metadata = false;
583 ws->info.has_gpu_reset_status_query = ws->info.drm_minor >= 43;
584 ws->info.has_eqaa_surface_allocator = false;
585 ws->info.has_format_bc1_through_bc7 = ws->info.drm_minor >= 31;
586 ws->info.kernel_flushes_tc_l2_after_ib = true;
587 /* Old kernels disallowed register writes via COPY_DATA
588 * that are used for indirect compute dispatches. */
589 ws->info.has_indirect_compute_dispatch = ws->info.chip_class == GFX7 ||
590 (ws->info.chip_class == GFX6 &&
591 ws->info.drm_minor >= 45);
592 /* GFX6 doesn't support unaligned loads. */
593 ws->info.has_unaligned_shader_loads = ws->info.chip_class == GFX7 &&
594 ws->info.drm_minor >= 50;
595 ws->info.has_sparse_vm_mappings = false;
596 /* 2D tiling on GFX7 is supported since DRM 2.35.0 */
597 ws->info.has_2d_tiling = ws->info.chip_class <= GFX6 || ws->info.drm_minor >= 35;
598 ws->info.has_read_registers_query = ws->info.drm_minor >= 42;
599 ws->info.max_alignment = 1024*1024;
600 ws->info.has_graphics = true;
601 ws->info.cpdma_prefetch_writes_memory = true;
602 ws->info.max_wave64_per_simd = 10;
603 ws->info.num_physical_sgprs_per_simd = 512;
604 ws->info.num_physical_wave64_vgprs_per_simd = 256;
605 /* Potential hang on Kabini: */
606 ws->info.use_late_alloc = ws->info.family != CHIP_KABINI;
607
608 ws->check_vm = strstr(debug_get_option("R600_DEBUG", ""), "check_vm") != NULL ||
609 strstr(debug_get_option("AMD_DEBUG", ""), "check_vm") != NULL;
610
611 return true;
612 }
613
radeon_winsys_destroy(struct radeon_winsys * rws)614 static void radeon_winsys_destroy(struct radeon_winsys *rws)
615 {
616 struct radeon_drm_winsys *ws = (struct radeon_drm_winsys*)rws;
617
618 if (util_queue_is_initialized(&ws->cs_queue))
619 util_queue_destroy(&ws->cs_queue);
620
621 mtx_destroy(&ws->hyperz_owner_mutex);
622 mtx_destroy(&ws->cmask_owner_mutex);
623
624 if (ws->info.r600_has_virtual_memory)
625 pb_slabs_deinit(&ws->bo_slabs);
626 pb_cache_deinit(&ws->bo_cache);
627
628 if (ws->gen >= DRV_R600) {
629 radeon_surface_manager_free(ws->surf_man);
630 }
631
632 _mesa_hash_table_destroy(ws->bo_names, NULL);
633 _mesa_hash_table_destroy(ws->bo_handles, NULL);
634 _mesa_hash_table_u64_destroy(ws->bo_vas, NULL);
635 mtx_destroy(&ws->bo_handles_mutex);
636 mtx_destroy(&ws->vm32.mutex);
637 mtx_destroy(&ws->vm64.mutex);
638 mtx_destroy(&ws->bo_fence_lock);
639
640 if (ws->fd >= 0)
641 close(ws->fd);
642
643 FREE(rws);
644 }
645
radeon_query_info(struct radeon_winsys * rws,struct radeon_info * info)646 static void radeon_query_info(struct radeon_winsys *rws,
647 struct radeon_info *info)
648 {
649 *info = ((struct radeon_drm_winsys *)rws)->info;
650 }
651
radeon_cs_request_feature(struct radeon_cmdbuf * rcs,enum radeon_feature_id fid,bool enable)652 static bool radeon_cs_request_feature(struct radeon_cmdbuf *rcs,
653 enum radeon_feature_id fid,
654 bool enable)
655 {
656 struct radeon_drm_cs *cs = radeon_drm_cs(rcs);
657
658 switch (fid) {
659 case RADEON_FID_R300_HYPERZ_ACCESS:
660 return radeon_set_fd_access(cs, &cs->ws->hyperz_owner,
661 &cs->ws->hyperz_owner_mutex,
662 RADEON_INFO_WANT_HYPERZ, "Hyper-Z",
663 enable);
664
665 case RADEON_FID_R300_CMASK_ACCESS:
666 return radeon_set_fd_access(cs, &cs->ws->cmask_owner,
667 &cs->ws->cmask_owner_mutex,
668 RADEON_INFO_WANT_CMASK, "AA optimizations",
669 enable);
670 }
671 return false;
672 }
673
radeon_drm_get_gpu_reset_counter(struct radeon_drm_winsys * ws)674 uint32_t radeon_drm_get_gpu_reset_counter(struct radeon_drm_winsys *ws)
675 {
676 uint64_t retval = 0;
677
678 if (!ws->info.has_gpu_reset_status_query)
679 return 0;
680
681 radeon_get_drm_value(ws->fd, RADEON_INFO_GPU_RESET_COUNTER,
682 "gpu-reset-counter", (uint32_t*)&retval);
683 return retval;
684 }
685
radeon_query_value(struct radeon_winsys * rws,enum radeon_value_id value)686 static uint64_t radeon_query_value(struct radeon_winsys *rws,
687 enum radeon_value_id value)
688 {
689 struct radeon_drm_winsys *ws = (struct radeon_drm_winsys*)rws;
690 uint64_t retval = 0;
691
692 switch (value) {
693 case RADEON_REQUESTED_VRAM_MEMORY:
694 return ws->allocated_vram;
695 case RADEON_REQUESTED_GTT_MEMORY:
696 return ws->allocated_gtt;
697 case RADEON_MAPPED_VRAM:
698 return ws->mapped_vram;
699 case RADEON_MAPPED_GTT:
700 return ws->mapped_gtt;
701 case RADEON_BUFFER_WAIT_TIME_NS:
702 return ws->buffer_wait_time;
703 case RADEON_NUM_MAPPED_BUFFERS:
704 return ws->num_mapped_buffers;
705 case RADEON_TIMESTAMP:
706 if (ws->info.drm_minor < 20 || ws->gen < DRV_R600) {
707 assert(0);
708 return 0;
709 }
710
711 radeon_get_drm_value(ws->fd, RADEON_INFO_TIMESTAMP, "timestamp",
712 (uint32_t*)&retval);
713 return retval;
714 case RADEON_NUM_GFX_IBS:
715 return ws->num_gfx_IBs;
716 case RADEON_NUM_SDMA_IBS:
717 return ws->num_sdma_IBs;
718 case RADEON_NUM_BYTES_MOVED:
719 radeon_get_drm_value(ws->fd, RADEON_INFO_NUM_BYTES_MOVED,
720 "num-bytes-moved", (uint32_t*)&retval);
721 return retval;
722 case RADEON_NUM_EVICTIONS:
723 case RADEON_NUM_VRAM_CPU_PAGE_FAULTS:
724 case RADEON_VRAM_VIS_USAGE:
725 case RADEON_GFX_BO_LIST_COUNTER:
726 case RADEON_GFX_IB_SIZE_COUNTER:
727 return 0; /* unimplemented */
728 case RADEON_VRAM_USAGE:
729 radeon_get_drm_value(ws->fd, RADEON_INFO_VRAM_USAGE,
730 "vram-usage", (uint32_t*)&retval);
731 return retval;
732 case RADEON_GTT_USAGE:
733 radeon_get_drm_value(ws->fd, RADEON_INFO_GTT_USAGE,
734 "gtt-usage", (uint32_t*)&retval);
735 return retval;
736 case RADEON_GPU_TEMPERATURE:
737 radeon_get_drm_value(ws->fd, RADEON_INFO_CURRENT_GPU_TEMP,
738 "gpu-temp", (uint32_t*)&retval);
739 return retval;
740 case RADEON_CURRENT_SCLK:
741 radeon_get_drm_value(ws->fd, RADEON_INFO_CURRENT_GPU_SCLK,
742 "current-gpu-sclk", (uint32_t*)&retval);
743 return retval;
744 case RADEON_CURRENT_MCLK:
745 radeon_get_drm_value(ws->fd, RADEON_INFO_CURRENT_GPU_MCLK,
746 "current-gpu-mclk", (uint32_t*)&retval);
747 return retval;
748 case RADEON_CS_THREAD_TIME:
749 return util_queue_get_thread_time_nano(&ws->cs_queue, 0);
750 }
751 return 0;
752 }
753
radeon_read_registers(struct radeon_winsys * rws,unsigned reg_offset,unsigned num_registers,uint32_t * out)754 static bool radeon_read_registers(struct radeon_winsys *rws,
755 unsigned reg_offset,
756 unsigned num_registers, uint32_t *out)
757 {
758 struct radeon_drm_winsys *ws = (struct radeon_drm_winsys*)rws;
759 unsigned i;
760
761 for (i = 0; i < num_registers; i++) {
762 uint32_t reg = reg_offset + i*4;
763
764 if (!radeon_get_drm_value(ws->fd, RADEON_INFO_READ_REG, NULL, ®))
765 return false;
766 out[i] = reg;
767 }
768 return true;
769 }
770
771 DEBUG_GET_ONCE_BOOL_OPTION(thread, "RADEON_THREAD", true)
772
radeon_winsys_unref(struct radeon_winsys * ws)773 static bool radeon_winsys_unref(struct radeon_winsys *ws)
774 {
775 struct radeon_drm_winsys *rws = (struct radeon_drm_winsys*)ws;
776 bool destroy;
777
778 /* When the reference counter drops to zero, remove the fd from the table.
779 * This must happen while the mutex is locked, so that
780 * radeon_drm_winsys_create in another thread doesn't get the winsys
781 * from the table when the counter drops to 0. */
782 mtx_lock(&fd_tab_mutex);
783
784 destroy = pipe_reference(&rws->reference, NULL);
785 if (destroy && fd_tab) {
786 _mesa_hash_table_remove_key(fd_tab, intptr_to_pointer(rws->fd));
787 if (_mesa_hash_table_num_entries(fd_tab) == 0) {
788 _mesa_hash_table_destroy(fd_tab, NULL);
789 fd_tab = NULL;
790 }
791 }
792
793 mtx_unlock(&fd_tab_mutex);
794 return destroy;
795 }
796
radeon_pin_threads_to_L3_cache(struct radeon_winsys * ws,unsigned cache)797 static void radeon_pin_threads_to_L3_cache(struct radeon_winsys *ws,
798 unsigned cache)
799 {
800 struct radeon_drm_winsys *rws = (struct radeon_drm_winsys*)ws;
801
802 if (util_queue_is_initialized(&rws->cs_queue)) {
803 util_set_thread_affinity(rws->cs_queue.threads[0],
804 util_cpu_caps.L3_affinity_mask[cache],
805 NULL, UTIL_MAX_CPUS);
806 }
807 }
808
radeon_cs_is_secure(struct radeon_cmdbuf * cs)809 static bool radeon_cs_is_secure(struct radeon_cmdbuf* cs)
810 {
811 return false;
812 }
813
814 PUBLIC struct radeon_winsys *
radeon_drm_winsys_create(int fd,const struct pipe_screen_config * config,radeon_screen_create_t screen_create)815 radeon_drm_winsys_create(int fd, const struct pipe_screen_config *config,
816 radeon_screen_create_t screen_create)
817 {
818 struct radeon_drm_winsys *ws;
819
820 mtx_lock(&fd_tab_mutex);
821 if (!fd_tab) {
822 fd_tab = util_hash_table_create_fd_keys();
823 }
824
825 ws = util_hash_table_get(fd_tab, intptr_to_pointer(fd));
826 if (ws) {
827 pipe_reference(NULL, &ws->reference);
828 mtx_unlock(&fd_tab_mutex);
829 return &ws->base;
830 }
831
832 ws = CALLOC_STRUCT(radeon_drm_winsys);
833 if (!ws) {
834 mtx_unlock(&fd_tab_mutex);
835 return NULL;
836 }
837
838 ws->fd = os_dupfd_cloexec(fd);
839
840 if (!do_winsys_init(ws))
841 goto fail1;
842
843 pb_cache_init(&ws->bo_cache, RADEON_MAX_CACHED_HEAPS,
844 500000, ws->check_vm ? 1.0f : 2.0f, 0,
845 MIN2(ws->info.vram_size, ws->info.gart_size),
846 radeon_bo_destroy,
847 radeon_bo_can_reclaim);
848
849 if (ws->info.r600_has_virtual_memory) {
850 /* There is no fundamental obstacle to using slab buffer allocation
851 * without GPUVM, but enabling it requires making sure that the drivers
852 * honor the address offset.
853 */
854 if (!pb_slabs_init(&ws->bo_slabs,
855 RADEON_SLAB_MIN_SIZE_LOG2, RADEON_SLAB_MAX_SIZE_LOG2,
856 RADEON_MAX_SLAB_HEAPS,
857 ws,
858 radeon_bo_can_reclaim_slab,
859 radeon_bo_slab_alloc,
860 radeon_bo_slab_free))
861 goto fail_cache;
862
863 ws->info.min_alloc_size = 1 << RADEON_SLAB_MIN_SIZE_LOG2;
864 } else {
865 ws->info.min_alloc_size = ws->info.gart_page_size;
866 }
867
868 if (ws->gen >= DRV_R600) {
869 ws->surf_man = radeon_surface_manager_new(ws->fd);
870 if (!ws->surf_man)
871 goto fail_slab;
872 }
873
874 /* init reference */
875 pipe_reference_init(&ws->reference, 1);
876
877 /* Set functions. */
878 ws->base.unref = radeon_winsys_unref;
879 ws->base.destroy = radeon_winsys_destroy;
880 ws->base.query_info = radeon_query_info;
881 ws->base.pin_threads_to_L3_cache = radeon_pin_threads_to_L3_cache;
882 ws->base.cs_request_feature = radeon_cs_request_feature;
883 ws->base.query_value = radeon_query_value;
884 ws->base.read_registers = radeon_read_registers;
885 ws->base.cs_is_secure = radeon_cs_is_secure;
886
887 radeon_drm_bo_init_functions(ws);
888 radeon_drm_cs_init_functions(ws);
889 radeon_surface_init_functions(ws);
890
891 (void) mtx_init(&ws->hyperz_owner_mutex, mtx_plain);
892 (void) mtx_init(&ws->cmask_owner_mutex, mtx_plain);
893
894 ws->bo_names = util_hash_table_create_ptr_keys();
895 ws->bo_handles = util_hash_table_create_ptr_keys();
896 ws->bo_vas = _mesa_hash_table_u64_create(NULL);
897 (void) mtx_init(&ws->bo_handles_mutex, mtx_plain);
898 (void) mtx_init(&ws->vm32.mutex, mtx_plain);
899 (void) mtx_init(&ws->vm64.mutex, mtx_plain);
900 (void) mtx_init(&ws->bo_fence_lock, mtx_plain);
901 list_inithead(&ws->vm32.holes);
902 list_inithead(&ws->vm64.holes);
903
904 /* The kernel currently returns 8MB. Make sure this doesn't change. */
905 if (ws->va_start > 8 * 1024 * 1024) {
906 /* Not enough 32-bit address space. */
907 radeon_winsys_destroy(&ws->base);
908 mtx_unlock(&fd_tab_mutex);
909 return NULL;
910 }
911
912 ws->vm32.start = ws->va_start;
913 ws->vm32.end = 1ull << 32;
914
915 /* The maximum is 8GB of virtual address space limited by the kernel.
916 * It's obviously not enough for bigger cards, like Hawaiis with 4GB
917 * and 8GB of physical memory and 4GB of GART.
918 *
919 * Older kernels set the limit to 4GB, which is even worse, so they only
920 * have 32-bit address space.
921 */
922 if (ws->info.drm_minor >= 41) {
923 ws->vm64.start = 1ull << 32;
924 ws->vm64.end = 1ull << 33;
925 }
926
927 /* TTM aligns the BO size to the CPU page size */
928 ws->info.gart_page_size = sysconf(_SC_PAGESIZE);
929 ws->info.pte_fragment_size = 64 * 1024; /* GPUVM page size */
930
931 if (ws->num_cpus > 1 && debug_get_option_thread())
932 util_queue_init(&ws->cs_queue, "rcs", 8, 1, 0);
933
934 /* Create the screen at the end. The winsys must be initialized
935 * completely.
936 *
937 * Alternatively, we could create the screen based on "ws->gen"
938 * and link all drivers into one binary blob. */
939 ws->base.screen = screen_create(&ws->base, config);
940 if (!ws->base.screen) {
941 radeon_winsys_destroy(&ws->base);
942 mtx_unlock(&fd_tab_mutex);
943 return NULL;
944 }
945
946 _mesa_hash_table_insert(fd_tab, intptr_to_pointer(ws->fd), ws);
947
948 /* We must unlock the mutex once the winsys is fully initialized, so that
949 * other threads attempting to create the winsys from the same fd will
950 * get a fully initialized winsys and not just half-way initialized. */
951 mtx_unlock(&fd_tab_mutex);
952
953 return &ws->base;
954
955 fail_slab:
956 if (ws->info.r600_has_virtual_memory)
957 pb_slabs_deinit(&ws->bo_slabs);
958 fail_cache:
959 pb_cache_deinit(&ws->bo_cache);
960 fail1:
961 mtx_unlock(&fd_tab_mutex);
962 if (ws->surf_man)
963 radeon_surface_manager_free(ws->surf_man);
964 if (ws->fd >= 0)
965 close(ws->fd);
966
967 FREE(ws);
968 return NULL;
969 }
970