Debian Codegraph @

src/gallium/winsys/radeon/drm/radeon_drm_winsys.c

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