Path: blob/21.2-virgl/src/gallium/winsys/amdgpu/drm/amdgpu_winsys.c
4561 views
/*1* Copyright © 2009 Corbin Simpson <[email protected]>2* Copyright © 2009 Joakim Sindholt <[email protected]>3* Copyright © 2011 Marek Olšák <[email protected]>4* Copyright © 2015 Advanced Micro Devices, Inc.5* All Rights Reserved.6*7* Permission is hereby granted, free of charge, to any person obtaining8* a copy of this software and associated documentation files (the9* "Software"), to deal in the Software without restriction, including10* without limitation the rights to use, copy, modify, merge, publish,11* distribute, sub license, and/or sell copies of the Software, and to12* permit persons to whom the Software is furnished to do so, subject to13* the following conditions:14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,16* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES17* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND18* NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS19* AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER20* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,21* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE22* USE OR OTHER DEALINGS IN THE SOFTWARE.23*24* The above copyright notice and this permission notice (including the25* next paragraph) shall be included in all copies or substantial portions26* of the Software.27*/2829#include "amdgpu_cs.h"30#include "amdgpu_public.h"3132#include "util/os_file.h"33#include "util/os_misc.h"34#include "util/u_cpu_detect.h"35#include "util/u_hash_table.h"36#include "util/hash_table.h"37#include "util/xmlconfig.h"38#include "drm-uapi/amdgpu_drm.h"39#include <xf86drm.h>40#include <stdio.h>41#include <sys/stat.h>42#include <fcntl.h>43#include "ac_llvm_util.h"44#include "sid.h"4546static struct hash_table *dev_tab = NULL;47static simple_mtx_t dev_tab_mutex = _SIMPLE_MTX_INITIALIZER_NP;4849#if DEBUG50DEBUG_GET_ONCE_BOOL_OPTION(all_bos, "RADEON_ALL_BOS", false)51#endif5253static void handle_env_var_force_family(struct amdgpu_winsys *ws)54{55const char *family = debug_get_option("SI_FORCE_FAMILY", NULL);56unsigned i;5758if (!family)59return;6061for (i = CHIP_TAHITI; i < CHIP_LAST; i++) {62if (!strcmp(family, ac_get_llvm_processor_name(i))) {63/* Override family and chip_class. */64ws->info.family = i;65ws->info.name = "GCN-NOOP";6667if (i >= CHIP_SIENNA_CICHLID)68ws->info.chip_class = GFX10_3;69else if (i >= CHIP_NAVI10)70ws->info.chip_class = GFX10;71else if (i >= CHIP_VEGA10)72ws->info.chip_class = GFX9;73else if (i >= CHIP_TONGA)74ws->info.chip_class = GFX8;75else if (i >= CHIP_BONAIRE)76ws->info.chip_class = GFX7;77else78ws->info.chip_class = GFX6;7980/* Don't submit any IBs. */81setenv("RADEON_NOOP", "1", 1);82return;83}84}8586fprintf(stderr, "radeonsi: Unknown family: %s\n", family);87exit(1);88}8990/* Helper function to do the ioctls needed for setup and init. */91static bool do_winsys_init(struct amdgpu_winsys *ws,92const struct pipe_screen_config *config,93int fd)94{95if (!ac_query_gpu_info(fd, ws->dev, &ws->info, &ws->amdinfo))96goto fail;9798/* TODO: Enable this once the kernel handles it efficiently. */99if (ws->info.has_dedicated_vram)100ws->info.has_local_buffers = false;101102handle_env_var_force_family(ws);103104ws->addrlib = ac_addrlib_create(&ws->info, &ws->info.max_alignment);105if (!ws->addrlib) {106fprintf(stderr, "amdgpu: Cannot create addrlib.\n");107goto fail;108}109110ws->check_vm = strstr(debug_get_option("R600_DEBUG", ""), "check_vm") != NULL ||111strstr(debug_get_option("AMD_DEBUG", ""), "check_vm") != NULL;112ws->noop_cs = debug_get_bool_option("RADEON_NOOP", false);113#if DEBUG114ws->debug_all_bos = debug_get_option_all_bos();115#endif116ws->reserve_vmid = strstr(debug_get_option("R600_DEBUG", ""), "reserve_vmid") != NULL ||117strstr(debug_get_option("AMD_DEBUG", ""), "reserve_vmid") != NULL;118ws->zero_all_vram_allocs = strstr(debug_get_option("R600_DEBUG", ""), "zerovram") != NULL ||119driQueryOptionb(config->options, "radeonsi_zerovram");120121return true;122123fail:124amdgpu_device_deinitialize(ws->dev);125ws->dev = NULL;126return false;127}128129static void do_winsys_deinit(struct amdgpu_winsys *ws)130{131if (ws->reserve_vmid)132amdgpu_vm_unreserve_vmid(ws->dev, 0);133134if (util_queue_is_initialized(&ws->cs_queue))135util_queue_destroy(&ws->cs_queue);136137simple_mtx_destroy(&ws->bo_fence_lock);138for (unsigned i = 0; i < NUM_SLAB_ALLOCATORS; i++) {139if (ws->bo_slabs[i].groups)140pb_slabs_deinit(&ws->bo_slabs[i]);141}142pb_cache_deinit(&ws->bo_cache);143_mesa_hash_table_destroy(ws->bo_export_table, NULL);144simple_mtx_destroy(&ws->sws_list_lock);145#if DEBUG146simple_mtx_destroy(&ws->global_bo_list_lock);147#endif148simple_mtx_destroy(&ws->bo_export_table_lock);149150ac_addrlib_destroy(ws->addrlib);151amdgpu_device_deinitialize(ws->dev);152FREE(ws);153}154155static void amdgpu_winsys_destroy(struct radeon_winsys *rws)156{157struct amdgpu_screen_winsys *sws = amdgpu_screen_winsys(rws);158struct amdgpu_winsys *ws = sws->aws;159bool destroy;160161/* When the reference counter drops to zero, remove the device pointer162* from the table.163* This must happen while the mutex is locked, so that164* amdgpu_winsys_create in another thread doesn't get the winsys165* from the table when the counter drops to 0.166*/167simple_mtx_lock(&dev_tab_mutex);168169destroy = pipe_reference(&ws->reference, NULL);170if (destroy && dev_tab) {171_mesa_hash_table_remove_key(dev_tab, ws->dev);172if (_mesa_hash_table_num_entries(dev_tab) == 0) {173_mesa_hash_table_destroy(dev_tab, NULL);174dev_tab = NULL;175}176}177178simple_mtx_unlock(&dev_tab_mutex);179180if (destroy)181do_winsys_deinit(ws);182183close(sws->fd);184FREE(rws);185}186187static void amdgpu_winsys_query_info(struct radeon_winsys *rws,188struct radeon_info *info,189bool enable_smart_access_memory,190bool disable_smart_access_memory)191{192struct amdgpu_winsys *ws = amdgpu_winsys(rws);193194if (disable_smart_access_memory)195ws->info.smart_access_memory = false;196else if (enable_smart_access_memory && ws->info.all_vram_visible)197ws->info.smart_access_memory = true;198199*info = ws->info;200}201202static bool amdgpu_cs_request_feature(struct radeon_cmdbuf *rcs,203enum radeon_feature_id fid,204bool enable)205{206return false;207}208209static uint64_t amdgpu_query_value(struct radeon_winsys *rws,210enum radeon_value_id value)211{212struct amdgpu_winsys *ws = amdgpu_winsys(rws);213struct amdgpu_heap_info heap;214uint64_t retval = 0;215216switch (value) {217case RADEON_REQUESTED_VRAM_MEMORY:218return ws->allocated_vram;219case RADEON_REQUESTED_GTT_MEMORY:220return ws->allocated_gtt;221case RADEON_MAPPED_VRAM:222return ws->mapped_vram;223case RADEON_MAPPED_GTT:224return ws->mapped_gtt;225case RADEON_SLAB_WASTED_VRAM:226return ws->slab_wasted_vram;227case RADEON_SLAB_WASTED_GTT:228return ws->slab_wasted_gtt;229case RADEON_BUFFER_WAIT_TIME_NS:230return ws->buffer_wait_time;231case RADEON_NUM_MAPPED_BUFFERS:232return ws->num_mapped_buffers;233case RADEON_TIMESTAMP:234amdgpu_query_info(ws->dev, AMDGPU_INFO_TIMESTAMP, 8, &retval);235return retval;236case RADEON_NUM_GFX_IBS:237return ws->num_gfx_IBs;238case RADEON_NUM_SDMA_IBS:239return ws->num_sdma_IBs;240case RADEON_GFX_BO_LIST_COUNTER:241return ws->gfx_bo_list_counter;242case RADEON_GFX_IB_SIZE_COUNTER:243return ws->gfx_ib_size_counter;244case RADEON_NUM_BYTES_MOVED:245amdgpu_query_info(ws->dev, AMDGPU_INFO_NUM_BYTES_MOVED, 8, &retval);246return retval;247case RADEON_NUM_EVICTIONS:248amdgpu_query_info(ws->dev, AMDGPU_INFO_NUM_EVICTIONS, 8, &retval);249return retval;250case RADEON_NUM_VRAM_CPU_PAGE_FAULTS:251amdgpu_query_info(ws->dev, AMDGPU_INFO_NUM_VRAM_CPU_PAGE_FAULTS, 8, &retval);252return retval;253case RADEON_VRAM_USAGE:254amdgpu_query_heap_info(ws->dev, AMDGPU_GEM_DOMAIN_VRAM, 0, &heap);255return heap.heap_usage;256case RADEON_VRAM_VIS_USAGE:257amdgpu_query_heap_info(ws->dev, AMDGPU_GEM_DOMAIN_VRAM,258AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED, &heap);259return heap.heap_usage;260case RADEON_GTT_USAGE:261amdgpu_query_heap_info(ws->dev, AMDGPU_GEM_DOMAIN_GTT, 0, &heap);262return heap.heap_usage;263case RADEON_GPU_TEMPERATURE:264amdgpu_query_sensor_info(ws->dev, AMDGPU_INFO_SENSOR_GPU_TEMP, 4, &retval);265return retval;266case RADEON_CURRENT_SCLK:267amdgpu_query_sensor_info(ws->dev, AMDGPU_INFO_SENSOR_GFX_SCLK, 4, &retval);268return retval;269case RADEON_CURRENT_MCLK:270amdgpu_query_sensor_info(ws->dev, AMDGPU_INFO_SENSOR_GFX_MCLK, 4, &retval);271return retval;272case RADEON_CS_THREAD_TIME:273return util_queue_get_thread_time_nano(&ws->cs_queue, 0);274}275return 0;276}277278static bool amdgpu_read_registers(struct radeon_winsys *rws,279unsigned reg_offset,280unsigned num_registers, uint32_t *out)281{282struct amdgpu_winsys *ws = amdgpu_winsys(rws);283284return amdgpu_read_mm_registers(ws->dev, reg_offset / 4, num_registers,2850xffffffff, 0, out) == 0;286}287288static bool amdgpu_winsys_unref(struct radeon_winsys *rws)289{290struct amdgpu_screen_winsys *sws = amdgpu_screen_winsys(rws);291struct amdgpu_winsys *aws = sws->aws;292bool ret;293294simple_mtx_lock(&aws->sws_list_lock);295296ret = pipe_reference(&sws->reference, NULL);297if (ret) {298struct amdgpu_screen_winsys **sws_iter;299struct amdgpu_winsys *aws = sws->aws;300301/* Remove this amdgpu_screen_winsys from amdgpu_winsys' list, so that302* amdgpu_winsys_create can't re-use it anymore303*/304for (sws_iter = &aws->sws_list; *sws_iter; sws_iter = &(*sws_iter)->next) {305if (*sws_iter == sws) {306*sws_iter = sws->next;307break;308}309}310}311312simple_mtx_unlock(&aws->sws_list_lock);313314if (ret && sws->kms_handles) {315struct drm_gem_close args;316317hash_table_foreach(sws->kms_handles, entry) {318args.handle = (uintptr_t)entry->data;319drmIoctl(sws->fd, DRM_IOCTL_GEM_CLOSE, &args);320}321_mesa_hash_table_destroy(sws->kms_handles, NULL);322}323324return ret;325}326327static void amdgpu_pin_threads_to_L3_cache(struct radeon_winsys *rws,328unsigned cache)329{330struct amdgpu_winsys *ws = amdgpu_winsys(rws);331332util_set_thread_affinity(ws->cs_queue.threads[0],333util_get_cpu_caps()->L3_affinity_mask[cache],334NULL, util_get_cpu_caps()->num_cpu_mask_bits);335}336337static uint32_t kms_handle_hash(const void *key)338{339const struct amdgpu_winsys_bo *bo = key;340341return bo->u.real.kms_handle;342}343344static bool kms_handle_equals(const void *a, const void *b)345{346return a == b;347}348349static bool amdgpu_cs_is_secure(struct radeon_cmdbuf *rcs)350{351struct amdgpu_cs *cs = amdgpu_cs(rcs);352return cs->csc->secure;353}354355PUBLIC struct radeon_winsys *356amdgpu_winsys_create(int fd, const struct pipe_screen_config *config,357radeon_screen_create_t screen_create)358{359struct amdgpu_screen_winsys *ws;360struct amdgpu_winsys *aws;361amdgpu_device_handle dev;362uint32_t drm_major, drm_minor;363int r;364365ws = CALLOC_STRUCT(amdgpu_screen_winsys);366if (!ws)367return NULL;368369pipe_reference_init(&ws->reference, 1);370ws->fd = os_dupfd_cloexec(fd);371372/* Look up the winsys from the dev table. */373simple_mtx_lock(&dev_tab_mutex);374if (!dev_tab)375dev_tab = util_hash_table_create_ptr_keys();376377/* Initialize the amdgpu device. This should always return the same pointer378* for the same fd. */379r = amdgpu_device_initialize(ws->fd, &drm_major, &drm_minor, &dev);380if (r) {381fprintf(stderr, "amdgpu: amdgpu_device_initialize failed.\n");382goto fail;383}384385/* Lookup a winsys if we have already created one for this device. */386aws = util_hash_table_get(dev_tab, dev);387if (aws) {388struct amdgpu_screen_winsys *sws_iter;389390/* Release the device handle, because we don't need it anymore.391* This function is returning an existing winsys instance, which392* has its own device handle.393*/394amdgpu_device_deinitialize(dev);395396simple_mtx_lock(&aws->sws_list_lock);397for (sws_iter = aws->sws_list; sws_iter; sws_iter = sws_iter->next) {398r = os_same_file_description(sws_iter->fd, ws->fd);399400if (r == 0) {401close(ws->fd);402FREE(ws);403ws = sws_iter;404pipe_reference(NULL, &ws->reference);405simple_mtx_unlock(&aws->sws_list_lock);406goto unlock;407} else if (r < 0) {408static bool logged;409410if (!logged) {411os_log_message("amdgpu: os_same_file_description couldn't "412"determine if two DRM fds reference the same "413"file description.\n"414"If they do, bad things may happen!\n");415logged = true;416}417}418}419simple_mtx_unlock(&aws->sws_list_lock);420421ws->kms_handles = _mesa_hash_table_create(NULL, kms_handle_hash,422kms_handle_equals);423if (!ws->kms_handles)424goto fail;425426pipe_reference(NULL, &aws->reference);427} else {428/* Create a new winsys. */429aws = CALLOC_STRUCT(amdgpu_winsys);430if (!aws)431goto fail;432433aws->dev = dev;434aws->fd = ws->fd;435aws->info.drm_major = drm_major;436aws->info.drm_minor = drm_minor;437aws->dummy_ws.aws = aws; /* only the pointer is used */438439if (!do_winsys_init(aws, config, fd))440goto fail_alloc;441442/* Create managers. */443pb_cache_init(&aws->bo_cache, RADEON_MAX_CACHED_HEAPS,444500000, aws->check_vm ? 1.0f : 2.0f, 0,445(aws->info.vram_size + aws->info.gart_size) / 8, aws,446/* Cast to void* because one of the function parameters447* is a struct pointer instead of void*. */448(void*)amdgpu_bo_destroy, (void*)amdgpu_bo_can_reclaim);449450unsigned min_slab_order = 8; /* 256 bytes */451unsigned max_slab_order = 20; /* 1 MB (slab size = 2 MB) */452unsigned num_slab_orders_per_allocator = (max_slab_order - min_slab_order) /453NUM_SLAB_ALLOCATORS;454455/* Divide the size order range among slab managers. */456for (unsigned i = 0; i < NUM_SLAB_ALLOCATORS; i++) {457unsigned min_order = min_slab_order;458unsigned max_order = MIN2(min_order + num_slab_orders_per_allocator,459max_slab_order);460461if (!pb_slabs_init(&aws->bo_slabs[i],462min_order, max_order,463RADEON_MAX_SLAB_HEAPS, true,464aws,465amdgpu_bo_can_reclaim_slab,466amdgpu_bo_slab_alloc_normal,467/* Cast to void* because one of the function parameters468* is a struct pointer instead of void*. */469(void*)amdgpu_bo_slab_free)) {470amdgpu_winsys_destroy(&ws->base);471simple_mtx_unlock(&dev_tab_mutex);472return NULL;473}474475if (aws->info.has_tmz_support &&476!pb_slabs_init(&aws->bo_slabs_encrypted[i],477min_order, max_order,478RADEON_MAX_SLAB_HEAPS, true,479aws,480amdgpu_bo_can_reclaim_slab,481amdgpu_bo_slab_alloc_encrypted,482/* Cast to void* because one of the function parameters483* is a struct pointer instead of void*. */484(void*)amdgpu_bo_slab_free)) {485amdgpu_winsys_destroy(&ws->base);486simple_mtx_unlock(&dev_tab_mutex);487return NULL;488}489490min_slab_order = max_order + 1;491}492493aws->info.min_alloc_size = 1 << aws->bo_slabs[0].min_order;494495/* init reference */496pipe_reference_init(&aws->reference, 1);497#if DEBUG498list_inithead(&aws->global_bo_list);499#endif500aws->bo_export_table = util_hash_table_create_ptr_keys();501502(void) simple_mtx_init(&aws->sws_list_lock, mtx_plain);503#if DEBUG504(void) simple_mtx_init(&aws->global_bo_list_lock, mtx_plain);505#endif506(void) simple_mtx_init(&aws->bo_fence_lock, mtx_plain);507(void) simple_mtx_init(&aws->bo_export_table_lock, mtx_plain);508509if (!util_queue_init(&aws->cs_queue, "cs", 8, 1,510UTIL_QUEUE_INIT_RESIZE_IF_FULL, NULL)) {511amdgpu_winsys_destroy(&ws->base);512simple_mtx_unlock(&dev_tab_mutex);513return NULL;514}515516_mesa_hash_table_insert(dev_tab, dev, aws);517518if (aws->reserve_vmid) {519r = amdgpu_vm_reserve_vmid(dev, 0);520if (r) {521amdgpu_winsys_destroy(&ws->base);522simple_mtx_unlock(&dev_tab_mutex);523return NULL;524}525}526}527528ws->aws = aws;529530/* Set functions. */531ws->base.unref = amdgpu_winsys_unref;532ws->base.destroy = amdgpu_winsys_destroy;533ws->base.query_info = amdgpu_winsys_query_info;534ws->base.cs_request_feature = amdgpu_cs_request_feature;535ws->base.query_value = amdgpu_query_value;536ws->base.read_registers = amdgpu_read_registers;537ws->base.pin_threads_to_L3_cache = amdgpu_pin_threads_to_L3_cache;538ws->base.cs_is_secure = amdgpu_cs_is_secure;539540amdgpu_bo_init_functions(ws);541amdgpu_cs_init_functions(ws);542amdgpu_surface_init_functions(ws);543544simple_mtx_lock(&aws->sws_list_lock);545ws->next = aws->sws_list;546aws->sws_list = ws;547simple_mtx_unlock(&aws->sws_list_lock);548549/* Create the screen at the end. The winsys must be initialized550* completely.551*552* Alternatively, we could create the screen based on "ws->gen"553* and link all drivers into one binary blob. */554ws->base.screen = screen_create(&ws->base, config);555if (!ws->base.screen) {556amdgpu_winsys_destroy(&ws->base);557simple_mtx_unlock(&dev_tab_mutex);558return NULL;559}560561unlock:562/* We must unlock the mutex once the winsys is fully initialized, so that563* other threads attempting to create the winsys from the same fd will564* get a fully initialized winsys and not just half-way initialized. */565simple_mtx_unlock(&dev_tab_mutex);566567return &ws->base;568569fail_alloc:570FREE(aws);571fail:572if (ws->kms_handles)573_mesa_hash_table_destroy(ws->kms_handles, NULL);574close(ws->fd);575FREE(ws);576simple_mtx_unlock(&dev_tab_mutex);577return NULL;578}579580581