Path: blob/21.2-virgl/src/gallium/winsys/radeon/drm/radeon_drm_winsys.c
4566 views
/*1* Copyright © 2009 Corbin Simpson2* Copyright © 2011 Marek Olšák <[email protected]>3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining6* a copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,14* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES15* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND16* NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS17* AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,19* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE20* USE OR OTHER DEALINGS IN THE SOFTWARE.21*22* The above copyright notice and this permission notice (including the23* next paragraph) shall be included in all copies or substantial portions24* of the Software.25*/2627#include "radeon_drm_bo.h"28#include "radeon_drm_cs.h"29#include "radeon_drm_public.h"3031#include "util/os_file.h"32#include "util/u_cpu_detect.h"33#include "util/u_memory.h"34#include "util/u_hash_table.h"35#include "util/u_pointer.h"3637#include <xf86drm.h>38#include <stdio.h>39#include <sys/types.h>40#include <sys/stat.h>41#include <unistd.h>42#include <fcntl.h>43#include <radeon_surface.h>4445static struct hash_table *fd_tab = NULL;46static mtx_t fd_tab_mutex = _MTX_INITIALIZER_NP;4748/* Enable/disable feature access for one command stream.49* If enable == true, return true on success.50* Otherwise, return false.51*52* We basically do the same thing kernel does, because we have to deal53* with multiple contexts (here command streams) backed by one winsys. */54static bool radeon_set_fd_access(struct radeon_drm_cs *applier,55struct radeon_drm_cs **owner,56mtx_t *mutex,57unsigned request, const char *request_name,58bool enable)59{60struct drm_radeon_info info;61unsigned value = enable ? 1 : 0;6263memset(&info, 0, sizeof(info));6465mtx_lock(&*mutex);6667/* Early exit if we are sure the request will fail. */68if (enable) {69if (*owner) {70mtx_unlock(&*mutex);71return false;72}73} else {74if (*owner != applier) {75mtx_unlock(&*mutex);76return false;77}78}7980/* Pass through the request to the kernel. */81info.value = (unsigned long)&value;82info.request = request;83if (drmCommandWriteRead(applier->ws->fd, DRM_RADEON_INFO,84&info, sizeof(info)) != 0) {85mtx_unlock(&*mutex);86return false;87}8889/* Update the rights in the winsys. */90if (enable) {91if (value) {92*owner = applier;93mtx_unlock(&*mutex);94return true;95}96} else {97*owner = NULL;98}99100mtx_unlock(&*mutex);101return false;102}103104static bool radeon_get_drm_value(int fd, unsigned request,105const char *errname, uint32_t *out)106{107struct drm_radeon_info info;108int retval;109110memset(&info, 0, sizeof(info));111112info.value = (unsigned long)out;113info.request = request;114115retval = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));116if (retval) {117if (errname) {118fprintf(stderr, "radeon: Failed to get %s, error number %d\n",119errname, retval);120}121return false;122}123return true;124}125126/* Helper function to do the ioctls needed for setup and init. */127static bool do_winsys_init(struct radeon_drm_winsys *ws)128{129struct drm_radeon_gem_info gem_info;130int retval;131drmVersionPtr version;132133memset(&gem_info, 0, sizeof(gem_info));134135/* We do things in a specific order here.136*137* DRM version first. We need to be sure we're running on a KMS chipset.138* This is also for some features.139*140* Then, the PCI ID. This is essential and should return usable numbers141* for all Radeons. If this fails, we probably got handed an FD for some142* non-Radeon card.143*144* The GEM info is actually bogus on the kernel side, as well as our side145* (see radeon_gem_info_ioctl in radeon_gem.c) but that's alright because146* we don't actually use the info for anything yet.147*148* The GB and Z pipe requests should always succeed, but they might not149* return sensical values for all chipsets, but that's alright because150* the pipe drivers already know that.151*/152153/* Get DRM version. */154version = drmGetVersion(ws->fd);155if (version->version_major != 2 ||156version->version_minor < 12) {157fprintf(stderr, "%s: DRM version is %d.%d.%d but this driver is "158"only compatible with 2.12.0 (kernel 3.2) or later.\n",159__FUNCTION__,160version->version_major,161version->version_minor,162version->version_patchlevel);163drmFreeVersion(version);164return false;165}166167ws->info.drm_major = version->version_major;168ws->info.drm_minor = version->version_minor;169ws->info.drm_patchlevel = version->version_patchlevel;170ws->info.is_amdgpu = false;171drmFreeVersion(version);172173/* Get PCI ID. */174if (!radeon_get_drm_value(ws->fd, RADEON_INFO_DEVICE_ID, "PCI ID",175&ws->info.pci_id))176return false;177178/* Check PCI ID. */179switch (ws->info.pci_id) {180#define CHIPSET(pci_id, name, cfamily) case pci_id: ws->info.family = CHIP_##cfamily; ws->gen = DRV_R300; break;181#include "pci_ids/r300_pci_ids.h"182#undef CHIPSET183184#define CHIPSET(pci_id, name, cfamily) case pci_id: ws->info.family = CHIP_##cfamily; ws->gen = DRV_R600; break;185#include "pci_ids/r600_pci_ids.h"186#undef CHIPSET187188#define CHIPSET(pci_id, cfamily) \189case pci_id: \190ws->info.family = CHIP_##cfamily; \191ws->info.name = #cfamily; \192ws->gen = DRV_SI; \193break;194#include "pci_ids/radeonsi_pci_ids.h"195#undef CHIPSET196197default:198fprintf(stderr, "radeon: Invalid PCI ID.\n");199return false;200}201202switch (ws->info.family) {203default:204case CHIP_UNKNOWN:205fprintf(stderr, "radeon: Unknown family.\n");206return false;207case CHIP_R300:208case CHIP_R350:209case CHIP_RV350:210case CHIP_RV370:211case CHIP_RV380:212case CHIP_RS400:213case CHIP_RC410:214case CHIP_RS480:215ws->info.chip_class = R300;216break;217case CHIP_R420: /* R4xx-based cores. */218case CHIP_R423:219case CHIP_R430:220case CHIP_R480:221case CHIP_R481:222case CHIP_RV410:223case CHIP_RS600:224case CHIP_RS690:225case CHIP_RS740:226ws->info.chip_class = R400;227break;228case CHIP_RV515: /* R5xx-based cores. */229case CHIP_R520:230case CHIP_RV530:231case CHIP_R580:232case CHIP_RV560:233case CHIP_RV570:234ws->info.chip_class = R500;235break;236case CHIP_R600:237case CHIP_RV610:238case CHIP_RV630:239case CHIP_RV670:240case CHIP_RV620:241case CHIP_RV635:242case CHIP_RS780:243case CHIP_RS880:244ws->info.chip_class = R600;245break;246case CHIP_RV770:247case CHIP_RV730:248case CHIP_RV710:249case CHIP_RV740:250ws->info.chip_class = R700;251break;252case CHIP_CEDAR:253case CHIP_REDWOOD:254case CHIP_JUNIPER:255case CHIP_CYPRESS:256case CHIP_HEMLOCK:257case CHIP_PALM:258case CHIP_SUMO:259case CHIP_SUMO2:260case CHIP_BARTS:261case CHIP_TURKS:262case CHIP_CAICOS:263ws->info.chip_class = EVERGREEN;264break;265case CHIP_CAYMAN:266case CHIP_ARUBA:267ws->info.chip_class = CAYMAN;268break;269case CHIP_TAHITI:270case CHIP_PITCAIRN:271case CHIP_VERDE:272case CHIP_OLAND:273case CHIP_HAINAN:274ws->info.chip_class = GFX6;275break;276case CHIP_BONAIRE:277case CHIP_KAVERI:278case CHIP_KABINI:279case CHIP_HAWAII:280ws->info.chip_class = GFX7;281break;282}283284/* Set which chips don't have dedicated VRAM. */285switch (ws->info.family) {286case CHIP_RS400:287case CHIP_RC410:288case CHIP_RS480:289case CHIP_RS600:290case CHIP_RS690:291case CHIP_RS740:292case CHIP_RS780:293case CHIP_RS880:294case CHIP_PALM:295case CHIP_SUMO:296case CHIP_SUMO2:297case CHIP_ARUBA:298case CHIP_KAVERI:299case CHIP_KABINI:300ws->info.has_dedicated_vram = false;301break;302303default:304ws->info.has_dedicated_vram = true;305}306307ws->info.num_rings[RING_GFX] = 1;308/* Check for dma */309ws->info.num_rings[RING_DMA] = 0;310/* DMA is disabled on R700. There is IB corruption and hangs. */311if (ws->info.chip_class >= EVERGREEN && ws->info.drm_minor >= 27) {312ws->info.num_rings[RING_DMA] = 1;313}314315/* Check for UVD and VCE */316ws->info.has_video_hw.uvd_decode = false;317ws->info.vce_fw_version = 0x00000000;318if (ws->info.drm_minor >= 32) {319uint32_t value = RADEON_CS_RING_UVD;320if (radeon_get_drm_value(ws->fd, RADEON_INFO_RING_WORKING,321"UVD Ring working", &value)) {322ws->info.has_video_hw.uvd_decode = value;323ws->info.num_rings[RING_UVD] = 1;324}325326value = RADEON_CS_RING_VCE;327if (radeon_get_drm_value(ws->fd, RADEON_INFO_RING_WORKING,328NULL, &value) && value) {329330if (radeon_get_drm_value(ws->fd, RADEON_INFO_VCE_FW_VERSION,331"VCE FW version", &value)) {332ws->info.vce_fw_version = value;333ws->info.num_rings[RING_VCE] = 1;334}335}336}337338/* Check for userptr support. */339{340struct drm_radeon_gem_userptr args = {0};341342/* If the ioctl doesn't exist, -EINVAL is returned.343*344* If the ioctl exists, it should return -EACCES345* if RADEON_GEM_USERPTR_READONLY or RADEON_GEM_USERPTR_REGISTER346* aren't set.347*/348ws->info.has_userptr =349drmCommandWriteRead(ws->fd, DRM_RADEON_GEM_USERPTR,350&args, sizeof(args)) == -EACCES;351}352353/* Get GEM info. */354retval = drmCommandWriteRead(ws->fd, DRM_RADEON_GEM_INFO,355&gem_info, sizeof(gem_info));356if (retval) {357fprintf(stderr, "radeon: Failed to get MM info, error number %d\n",358retval);359return false;360}361ws->info.gart_size = gem_info.gart_size;362ws->info.vram_size = gem_info.vram_size;363ws->info.vram_vis_size = gem_info.vram_visible;364/* Older versions of the kernel driver reported incorrect values, and365* didn't support more than 256MB of visible VRAM anyway366*/367if (ws->info.drm_minor < 49)368ws->info.vram_vis_size = MIN2(ws->info.vram_vis_size, 256*1024*1024);369370ws->info.gart_size_kb = DIV_ROUND_UP(ws->info.gart_size, 1024);371ws->info.vram_size_kb = DIV_ROUND_UP(ws->info.vram_size, 1024);372373/* Radeon allocates all buffers contiguously, which makes large allocations374* unlikely to succeed. */375if (ws->info.has_dedicated_vram)376ws->info.max_alloc_size = ws->info.vram_size * 0.7;377else378ws->info.max_alloc_size = ws->info.gart_size * 0.7;379380if (ws->info.drm_minor < 40)381ws->info.max_alloc_size = MIN2(ws->info.max_alloc_size, 256*1024*1024);382/* Both 32-bit and 64-bit address spaces only have 4GB. */383ws->info.max_alloc_size = MIN2(ws->info.max_alloc_size, 3ull*1024*1024*1024);384385/* Get max clock frequency info and convert it to MHz */386radeon_get_drm_value(ws->fd, RADEON_INFO_MAX_SCLK, NULL,387&ws->info.max_shader_clock);388ws->info.max_shader_clock /= 1000;389390ws->num_cpus = sysconf(_SC_NPROCESSORS_ONLN);391392/* Generation-specific queries. */393if (ws->gen == DRV_R300) {394if (!radeon_get_drm_value(ws->fd, RADEON_INFO_NUM_GB_PIPES,395"GB pipe count",396&ws->info.r300_num_gb_pipes))397return false;398399if (!radeon_get_drm_value(ws->fd, RADEON_INFO_NUM_Z_PIPES,400"Z pipe count",401&ws->info.r300_num_z_pipes))402return false;403}404else if (ws->gen >= DRV_R600) {405uint32_t tiling_config = 0;406407if (!radeon_get_drm_value(ws->fd, RADEON_INFO_NUM_BACKENDS,408"num backends",409&ws->info.max_render_backends))410return false;411412/* get the GPU counter frequency, failure is not fatal */413radeon_get_drm_value(ws->fd, RADEON_INFO_CLOCK_CRYSTAL_FREQ, NULL,414&ws->info.clock_crystal_freq);415416radeon_get_drm_value(ws->fd, RADEON_INFO_TILING_CONFIG, NULL,417&tiling_config);418419ws->info.r600_num_banks =420ws->info.chip_class >= EVERGREEN ?4214 << ((tiling_config & 0xf0) >> 4) :4224 << ((tiling_config & 0x30) >> 4);423424ws->info.pipe_interleave_bytes =425ws->info.chip_class >= EVERGREEN ?426256 << ((tiling_config & 0xf00) >> 8) :427256 << ((tiling_config & 0xc0) >> 6);428429if (!ws->info.pipe_interleave_bytes)430ws->info.pipe_interleave_bytes =431ws->info.chip_class >= EVERGREEN ? 512 : 256;432433radeon_get_drm_value(ws->fd, RADEON_INFO_NUM_TILE_PIPES, NULL,434&ws->info.num_tile_pipes);435436/* "num_tiles_pipes" must be equal to the number of pipes (Px) in the437* pipe config field of the GB_TILE_MODE array. Only one card (Tahiti)438* reports a different value (12). Fix it by setting what's in the439* GB_TILE_MODE array (8).440*/441if (ws->gen == DRV_SI && ws->info.num_tile_pipes == 12)442ws->info.num_tile_pipes = 8;443444if (radeon_get_drm_value(ws->fd, RADEON_INFO_BACKEND_MAP, NULL,445&ws->info.r600_gb_backend_map))446ws->info.r600_gb_backend_map_valid = true;447448/* Default value. */449ws->info.enabled_rb_mask = u_bit_consecutive(0, ws->info.max_render_backends);450/*451* This fails (silently) on non-GCN or older kernels, overwriting the452* default enabled_rb_mask with the result of the last query.453*/454if (ws->gen >= DRV_SI)455radeon_get_drm_value(ws->fd, RADEON_INFO_SI_BACKEND_ENABLED_MASK, NULL,456&ws->info.enabled_rb_mask);457458ws->info.r600_has_virtual_memory = false;459if (ws->info.drm_minor >= 13) {460uint32_t ib_vm_max_size;461462ws->info.r600_has_virtual_memory = true;463if (!radeon_get_drm_value(ws->fd, RADEON_INFO_VA_START, NULL,464&ws->va_start))465ws->info.r600_has_virtual_memory = false;466if (!radeon_get_drm_value(ws->fd, RADEON_INFO_IB_VM_MAX_SIZE, NULL,467&ib_vm_max_size))468ws->info.r600_has_virtual_memory = false;469radeon_get_drm_value(ws->fd, RADEON_INFO_VA_UNMAP_WORKING, NULL,470&ws->va_unmap_working);471}472if (ws->gen == DRV_R600 && !debug_get_bool_option("RADEON_VA", false))473ws->info.r600_has_virtual_memory = false;474}475476/* Get max pipes, this is only needed for compute shaders. All evergreen+477* chips have at least 2 pipes, so we use 2 as a default. */478ws->info.r600_max_quad_pipes = 2;479radeon_get_drm_value(ws->fd, RADEON_INFO_MAX_PIPES, NULL,480&ws->info.r600_max_quad_pipes);481482/* All GPUs have at least one compute unit */483ws->info.num_good_compute_units = 1;484radeon_get_drm_value(ws->fd, RADEON_INFO_ACTIVE_CU_COUNT, NULL,485&ws->info.num_good_compute_units);486487radeon_get_drm_value(ws->fd, RADEON_INFO_MAX_SE, NULL,488&ws->info.max_se);489490switch (ws->info.family) {491case CHIP_HAINAN:492case CHIP_KABINI:493ws->info.max_tcc_blocks = 2;494break;495case CHIP_VERDE:496case CHIP_OLAND:497case CHIP_BONAIRE:498case CHIP_KAVERI:499ws->info.max_tcc_blocks = 4;500break;501case CHIP_PITCAIRN:502ws->info.max_tcc_blocks = 8;503break;504case CHIP_TAHITI:505ws->info.max_tcc_blocks = 12;506break;507case CHIP_HAWAII:508ws->info.max_tcc_blocks = 16;509break;510default:511ws->info.max_tcc_blocks = 0;512break;513}514515if (!ws->info.max_se) {516switch (ws->info.family) {517default:518ws->info.max_se = 1;519break;520case CHIP_CYPRESS:521case CHIP_HEMLOCK:522case CHIP_BARTS:523case CHIP_CAYMAN:524case CHIP_TAHITI:525case CHIP_PITCAIRN:526case CHIP_BONAIRE:527ws->info.max_se = 2;528break;529case CHIP_HAWAII:530ws->info.max_se = 4;531break;532}533}534535ws->info.num_se = ws->info.max_se;536537radeon_get_drm_value(ws->fd, RADEON_INFO_MAX_SH_PER_SE, NULL,538&ws->info.max_sa_per_se);539if (ws->gen == DRV_SI) {540ws->info.max_good_cu_per_sa =541ws->info.min_good_cu_per_sa = ws->info.num_good_compute_units /542(ws->info.max_se * ws->info.max_sa_per_se);543}544545radeon_get_drm_value(ws->fd, RADEON_INFO_ACCEL_WORKING2, NULL,546&ws->accel_working2);547if (ws->info.family == CHIP_HAWAII && ws->accel_working2 < 2) {548fprintf(stderr, "radeon: GPU acceleration for Hawaii disabled, "549"returned accel_working2 value %u is smaller than 2. "550"Please install a newer kernel.\n",551ws->accel_working2);552return false;553}554555if (ws->info.chip_class == GFX7) {556if (!radeon_get_drm_value(ws->fd, RADEON_INFO_CIK_MACROTILE_MODE_ARRAY, NULL,557ws->info.cik_macrotile_mode_array)) {558fprintf(stderr, "radeon: Kernel 3.13 is required for Sea Islands support.\n");559return false;560}561}562563if (ws->info.chip_class >= GFX6) {564if (!radeon_get_drm_value(ws->fd, RADEON_INFO_SI_TILE_MODE_ARRAY, NULL,565ws->info.si_tile_mode_array)) {566fprintf(stderr, "radeon: Kernel 3.10 is required for Southern Islands support.\n");567return false;568}569}570571/* Hawaii with old firmware needs type2 nop packet.572* accel_working2 with value 3 indicates the new firmware.573*/574ws->info.gfx_ib_pad_with_type2 = ws->info.chip_class <= GFX6 ||575(ws->info.family == CHIP_HAWAII &&576ws->accel_working2 < 3);577ws->info.tcc_cache_line_size = 64; /* TC L2 line size on GCN */578ws->info.ib_alignment = 4096;579ws->info.kernel_flushes_hdp_before_ib = ws->info.drm_minor >= 40;580/* HTILE is broken with 1D tiling on old kernels and GFX7. */581ws->info.htile_cmask_support_1d_tiling = ws->info.chip_class != GFX7 ||582ws->info.drm_minor >= 38;583ws->info.si_TA_CS_BC_BASE_ADDR_allowed = ws->info.drm_minor >= 48;584ws->info.has_bo_metadata = false;585ws->info.has_gpu_reset_status_query = ws->info.drm_minor >= 43;586ws->info.has_eqaa_surface_allocator = false;587ws->info.has_format_bc1_through_bc7 = ws->info.drm_minor >= 31;588ws->info.kernel_flushes_tc_l2_after_ib = true;589/* Old kernels disallowed register writes via COPY_DATA590* that are used for indirect compute dispatches. */591ws->info.has_indirect_compute_dispatch = ws->info.chip_class == GFX7 ||592(ws->info.chip_class == GFX6 &&593ws->info.drm_minor >= 45);594/* GFX6 doesn't support unaligned loads. */595ws->info.has_unaligned_shader_loads = ws->info.chip_class == GFX7 &&596ws->info.drm_minor >= 50;597ws->info.has_sparse_vm_mappings = false;598/* 2D tiling on GFX7 is supported since DRM 2.35.0 */599ws->info.has_2d_tiling = ws->info.chip_class <= GFX6 || ws->info.drm_minor >= 35;600ws->info.has_read_registers_query = ws->info.drm_minor >= 42;601ws->info.max_alignment = 1024*1024;602ws->info.has_graphics = true;603ws->info.cpdma_prefetch_writes_memory = true;604ws->info.max_wave64_per_simd = 10;605ws->info.num_physical_sgprs_per_simd = 512;606ws->info.num_physical_wave64_vgprs_per_simd = 256;607/* Potential hang on Kabini: */608ws->info.use_late_alloc = ws->info.family != CHIP_KABINI;609ws->info.has_3d_cube_border_color_mipmap = true;610611ws->check_vm = strstr(debug_get_option("R600_DEBUG", ""), "check_vm") != NULL ||612strstr(debug_get_option("AMD_DEBUG", ""), "check_vm") != NULL;613ws->noop_cs = debug_get_bool_option("RADEON_NOOP", false);614615return true;616}617618static void radeon_winsys_destroy(struct radeon_winsys *rws)619{620struct radeon_drm_winsys *ws = (struct radeon_drm_winsys*)rws;621622if (util_queue_is_initialized(&ws->cs_queue))623util_queue_destroy(&ws->cs_queue);624625mtx_destroy(&ws->hyperz_owner_mutex);626mtx_destroy(&ws->cmask_owner_mutex);627628if (ws->info.r600_has_virtual_memory)629pb_slabs_deinit(&ws->bo_slabs);630pb_cache_deinit(&ws->bo_cache);631632if (ws->gen >= DRV_R600) {633radeon_surface_manager_free(ws->surf_man);634}635636_mesa_hash_table_destroy(ws->bo_names, NULL);637_mesa_hash_table_destroy(ws->bo_handles, NULL);638_mesa_hash_table_u64_destroy(ws->bo_vas);639mtx_destroy(&ws->bo_handles_mutex);640mtx_destroy(&ws->vm32.mutex);641mtx_destroy(&ws->vm64.mutex);642mtx_destroy(&ws->bo_fence_lock);643644if (ws->fd >= 0)645close(ws->fd);646647FREE(rws);648}649650static void radeon_query_info(struct radeon_winsys *rws,651struct radeon_info *info,652bool enable_smart_access_memory,653bool disable_smart_access_memory)654{655*info = ((struct radeon_drm_winsys *)rws)->info;656}657658static bool radeon_cs_request_feature(struct radeon_cmdbuf *rcs,659enum radeon_feature_id fid,660bool enable)661{662struct radeon_drm_cs *cs = radeon_drm_cs(rcs);663664switch (fid) {665case RADEON_FID_R300_HYPERZ_ACCESS:666return radeon_set_fd_access(cs, &cs->ws->hyperz_owner,667&cs->ws->hyperz_owner_mutex,668RADEON_INFO_WANT_HYPERZ, "Hyper-Z",669enable);670671case RADEON_FID_R300_CMASK_ACCESS:672return radeon_set_fd_access(cs, &cs->ws->cmask_owner,673&cs->ws->cmask_owner_mutex,674RADEON_INFO_WANT_CMASK, "AA optimizations",675enable);676}677return false;678}679680uint32_t radeon_drm_get_gpu_reset_counter(struct radeon_drm_winsys *ws)681{682uint64_t retval = 0;683684if (!ws->info.has_gpu_reset_status_query)685return 0;686687radeon_get_drm_value(ws->fd, RADEON_INFO_GPU_RESET_COUNTER,688"gpu-reset-counter", (uint32_t*)&retval);689return retval;690}691692static uint64_t radeon_query_value(struct radeon_winsys *rws,693enum radeon_value_id value)694{695struct radeon_drm_winsys *ws = (struct radeon_drm_winsys*)rws;696uint64_t retval = 0;697698switch (value) {699case RADEON_REQUESTED_VRAM_MEMORY:700return ws->allocated_vram;701case RADEON_REQUESTED_GTT_MEMORY:702return ws->allocated_gtt;703case RADEON_MAPPED_VRAM:704return ws->mapped_vram;705case RADEON_MAPPED_GTT:706return ws->mapped_gtt;707case RADEON_BUFFER_WAIT_TIME_NS:708return ws->buffer_wait_time;709case RADEON_NUM_MAPPED_BUFFERS:710return ws->num_mapped_buffers;711case RADEON_TIMESTAMP:712if (ws->info.drm_minor < 20 || ws->gen < DRV_R600) {713assert(0);714return 0;715}716717radeon_get_drm_value(ws->fd, RADEON_INFO_TIMESTAMP, "timestamp",718(uint32_t*)&retval);719return retval;720case RADEON_NUM_GFX_IBS:721return ws->num_gfx_IBs;722case RADEON_NUM_SDMA_IBS:723return ws->num_sdma_IBs;724case RADEON_NUM_BYTES_MOVED:725radeon_get_drm_value(ws->fd, RADEON_INFO_NUM_BYTES_MOVED,726"num-bytes-moved", (uint32_t*)&retval);727return retval;728case RADEON_NUM_EVICTIONS:729case RADEON_NUM_VRAM_CPU_PAGE_FAULTS:730case RADEON_VRAM_VIS_USAGE:731case RADEON_GFX_BO_LIST_COUNTER:732case RADEON_GFX_IB_SIZE_COUNTER:733case RADEON_SLAB_WASTED_VRAM:734case RADEON_SLAB_WASTED_GTT:735return 0; /* unimplemented */736case RADEON_VRAM_USAGE:737radeon_get_drm_value(ws->fd, RADEON_INFO_VRAM_USAGE,738"vram-usage", (uint32_t*)&retval);739return retval;740case RADEON_GTT_USAGE:741radeon_get_drm_value(ws->fd, RADEON_INFO_GTT_USAGE,742"gtt-usage", (uint32_t*)&retval);743return retval;744case RADEON_GPU_TEMPERATURE:745radeon_get_drm_value(ws->fd, RADEON_INFO_CURRENT_GPU_TEMP,746"gpu-temp", (uint32_t*)&retval);747return retval;748case RADEON_CURRENT_SCLK:749radeon_get_drm_value(ws->fd, RADEON_INFO_CURRENT_GPU_SCLK,750"current-gpu-sclk", (uint32_t*)&retval);751return retval;752case RADEON_CURRENT_MCLK:753radeon_get_drm_value(ws->fd, RADEON_INFO_CURRENT_GPU_MCLK,754"current-gpu-mclk", (uint32_t*)&retval);755return retval;756case RADEON_CS_THREAD_TIME:757return util_queue_get_thread_time_nano(&ws->cs_queue, 0);758}759return 0;760}761762static bool radeon_read_registers(struct radeon_winsys *rws,763unsigned reg_offset,764unsigned num_registers, uint32_t *out)765{766struct radeon_drm_winsys *ws = (struct radeon_drm_winsys*)rws;767unsigned i;768769for (i = 0; i < num_registers; i++) {770uint32_t reg = reg_offset + i*4;771772if (!radeon_get_drm_value(ws->fd, RADEON_INFO_READ_REG, NULL, ®))773return false;774out[i] = reg;775}776return true;777}778779DEBUG_GET_ONCE_BOOL_OPTION(thread, "RADEON_THREAD", true)780781static bool radeon_winsys_unref(struct radeon_winsys *ws)782{783struct radeon_drm_winsys *rws = (struct radeon_drm_winsys*)ws;784bool destroy;785786/* When the reference counter drops to zero, remove the fd from the table.787* This must happen while the mutex is locked, so that788* radeon_drm_winsys_create in another thread doesn't get the winsys789* from the table when the counter drops to 0. */790mtx_lock(&fd_tab_mutex);791792destroy = pipe_reference(&rws->reference, NULL);793if (destroy && fd_tab) {794_mesa_hash_table_remove_key(fd_tab, intptr_to_pointer(rws->fd));795if (_mesa_hash_table_num_entries(fd_tab) == 0) {796_mesa_hash_table_destroy(fd_tab, NULL);797fd_tab = NULL;798}799}800801mtx_unlock(&fd_tab_mutex);802return destroy;803}804805static void radeon_pin_threads_to_L3_cache(struct radeon_winsys *ws,806unsigned cache)807{808struct radeon_drm_winsys *rws = (struct radeon_drm_winsys*)ws;809810if (util_queue_is_initialized(&rws->cs_queue)) {811util_set_thread_affinity(rws->cs_queue.threads[0],812util_get_cpu_caps()->L3_affinity_mask[cache],813NULL, util_get_cpu_caps()->num_cpu_mask_bits);814}815}816817static bool radeon_cs_is_secure(struct radeon_cmdbuf* cs)818{819return false;820}821822PUBLIC struct radeon_winsys *823radeon_drm_winsys_create(int fd, const struct pipe_screen_config *config,824radeon_screen_create_t screen_create)825{826struct radeon_drm_winsys *ws;827828mtx_lock(&fd_tab_mutex);829if (!fd_tab) {830fd_tab = util_hash_table_create_fd_keys();831}832833ws = util_hash_table_get(fd_tab, intptr_to_pointer(fd));834if (ws) {835pipe_reference(NULL, &ws->reference);836mtx_unlock(&fd_tab_mutex);837return &ws->base;838}839840ws = CALLOC_STRUCT(radeon_drm_winsys);841if (!ws) {842mtx_unlock(&fd_tab_mutex);843return NULL;844}845846ws->fd = os_dupfd_cloexec(fd);847848if (!do_winsys_init(ws))849goto fail1;850851pb_cache_init(&ws->bo_cache, RADEON_MAX_CACHED_HEAPS,852500000, ws->check_vm ? 1.0f : 2.0f, 0,853MIN2(ws->info.vram_size, ws->info.gart_size), NULL,854radeon_bo_destroy,855radeon_bo_can_reclaim);856857if (ws->info.r600_has_virtual_memory) {858/* There is no fundamental obstacle to using slab buffer allocation859* without GPUVM, but enabling it requires making sure that the drivers860* honor the address offset.861*/862if (!pb_slabs_init(&ws->bo_slabs,863RADEON_SLAB_MIN_SIZE_LOG2, RADEON_SLAB_MAX_SIZE_LOG2,864RADEON_MAX_SLAB_HEAPS, false,865ws,866radeon_bo_can_reclaim_slab,867radeon_bo_slab_alloc,868radeon_bo_slab_free))869goto fail_cache;870871ws->info.min_alloc_size = 1 << RADEON_SLAB_MIN_SIZE_LOG2;872} else {873ws->info.min_alloc_size = ws->info.gart_page_size;874}875876if (ws->gen >= DRV_R600) {877ws->surf_man = radeon_surface_manager_new(ws->fd);878if (!ws->surf_man)879goto fail_slab;880}881882/* init reference */883pipe_reference_init(&ws->reference, 1);884885/* Set functions. */886ws->base.unref = radeon_winsys_unref;887ws->base.destroy = radeon_winsys_destroy;888ws->base.query_info = radeon_query_info;889ws->base.pin_threads_to_L3_cache = radeon_pin_threads_to_L3_cache;890ws->base.cs_request_feature = radeon_cs_request_feature;891ws->base.query_value = radeon_query_value;892ws->base.read_registers = radeon_read_registers;893ws->base.cs_is_secure = radeon_cs_is_secure;894895radeon_drm_bo_init_functions(ws);896radeon_drm_cs_init_functions(ws);897radeon_surface_init_functions(ws);898899(void) mtx_init(&ws->hyperz_owner_mutex, mtx_plain);900(void) mtx_init(&ws->cmask_owner_mutex, mtx_plain);901902ws->bo_names = util_hash_table_create_ptr_keys();903ws->bo_handles = util_hash_table_create_ptr_keys();904ws->bo_vas = _mesa_hash_table_u64_create(NULL);905(void) mtx_init(&ws->bo_handles_mutex, mtx_plain);906(void) mtx_init(&ws->vm32.mutex, mtx_plain);907(void) mtx_init(&ws->vm64.mutex, mtx_plain);908(void) mtx_init(&ws->bo_fence_lock, mtx_plain);909list_inithead(&ws->vm32.holes);910list_inithead(&ws->vm64.holes);911912/* The kernel currently returns 8MB. Make sure this doesn't change. */913if (ws->va_start > 8 * 1024 * 1024) {914/* Not enough 32-bit address space. */915radeon_winsys_destroy(&ws->base);916mtx_unlock(&fd_tab_mutex);917return NULL;918}919920ws->vm32.start = ws->va_start;921ws->vm32.end = 1ull << 32;922923/* The maximum is 8GB of virtual address space limited by the kernel.924* It's obviously not enough for bigger cards, like Hawaiis with 4GB925* and 8GB of physical memory and 4GB of GART.926*927* Older kernels set the limit to 4GB, which is even worse, so they only928* have 32-bit address space.929*/930if (ws->info.drm_minor >= 41) {931ws->vm64.start = 1ull << 32;932ws->vm64.end = 1ull << 33;933}934935/* TTM aligns the BO size to the CPU page size */936ws->info.gart_page_size = sysconf(_SC_PAGESIZE);937ws->info.pte_fragment_size = 64 * 1024; /* GPUVM page size */938939if (ws->num_cpus > 1 && debug_get_option_thread())940util_queue_init(&ws->cs_queue, "rcs", 8, 1, 0, NULL);941942/* Create the screen at the end. The winsys must be initialized943* completely.944*945* Alternatively, we could create the screen based on "ws->gen"946* and link all drivers into one binary blob. */947ws->base.screen = screen_create(&ws->base, config);948if (!ws->base.screen) {949radeon_winsys_destroy(&ws->base);950mtx_unlock(&fd_tab_mutex);951return NULL;952}953954_mesa_hash_table_insert(fd_tab, intptr_to_pointer(ws->fd), ws);955956/* We must unlock the mutex once the winsys is fully initialized, so that957* other threads attempting to create the winsys from the same fd will958* get a fully initialized winsys and not just half-way initialized. */959mtx_unlock(&fd_tab_mutex);960961return &ws->base;962963fail_slab:964if (ws->info.r600_has_virtual_memory)965pb_slabs_deinit(&ws->bo_slabs);966fail_cache:967pb_cache_deinit(&ws->bo_cache);968fail1:969mtx_unlock(&fd_tab_mutex);970if (ws->surf_man)971radeon_surface_manager_free(ws->surf_man);972if (ws->fd >= 0)973close(ws->fd);974975FREE(ws);976return NULL;977}978979980