Path: blob/21.2-virgl/src/panfrost/lib/pan_props.c
4560 views
/*1* Copyright (C) 2019 Collabora, Ltd.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22* Authors:23* Alyssa Rosenzweig <[email protected]>24*/2526#include <xf86drm.h>2728#include "util/u_math.h"29#include "util/macros.h"30#include "util/hash_table.h"31#include "util/u_thread.h"32#include "drm-uapi/panfrost_drm.h"33#include "pan_encoder.h"34#include "pan_device.h"35#include "panfrost-quirks.h"36#include "pan_bo.h"37#include "pan_texture.h"38#include "wrap.h"39#include "pan_util.h"4041/* Abstraction over the raw drm_panfrost_get_param ioctl for fetching42* information about devices */4344static __u6445panfrost_query_raw(46int fd,47enum drm_panfrost_param param,48bool required,49unsigned default_value)50{51struct drm_panfrost_get_param get_param = {0,};52ASSERTED int ret;5354get_param.param = param;55ret = drmIoctl(fd, DRM_IOCTL_PANFROST_GET_PARAM, &get_param);5657if (ret) {58assert(!required);59return default_value;60}6162return get_param.value;63}6465static unsigned66panfrost_query_gpu_version(int fd)67{68#ifndef NDEBUG69/* In debug builds, allow overriding the GPU ID, for example to run70* Bifrost shader-db on a Midgard machine. This is a bit less heavy71* handed than setting up the entirety of drm-shim */72char *override_version = getenv("PAN_GPU_ID");7374if (override_version)75return strtol(override_version, NULL, 16);76#endif7778return panfrost_query_raw(fd, DRM_PANFROST_PARAM_GPU_PROD_ID, true, 0);79}8081static unsigned82panfrost_query_gpu_revision(int fd)83{84return panfrost_query_raw(fd, DRM_PANFROST_PARAM_GPU_REVISION, true, 0);85}8687static struct panfrost_tiler_features88panfrost_query_tiler_features(int fd)89{90/* Default value (2^9 bytes and 8 levels) to match old behaviour */91uint32_t raw = panfrost_query_raw(fd, DRM_PANFROST_PARAM_TILER_FEATURES,92false, 0x809);9394/* Bin size is log2 in the first byte, max levels in the second byte */95return (struct panfrost_tiler_features) {96.bin_size = (1 << (raw & BITFIELD_MASK(5))),97.max_levels = (raw >> 8) & BITFIELD_MASK(4)98};99}100101static unsigned102panfrost_query_core_count(int fd)103{104/* On older kernels, worst-case to 16 cores */105106unsigned mask = panfrost_query_raw(fd,107DRM_PANFROST_PARAM_SHADER_PRESENT, false, 0xffff);108109/* Some cores might be absent. For TLS computation purposes, we care110* about the greatest ID + 1, which equals the core count if all cores111* are present, but allocates space for absent cores if needed.112* util_last_bit is defined to return the greatest bit set + 1, which113* is exactly what we need. */114115return util_last_bit(mask);116}117118/* Architectural maximums, since this register may be not implemented119* by a given chip. G31 is actually 512 instead of 768 but it doesn't120* really matter. */121122static unsigned123panfrost_max_thread_count(unsigned arch)124{125switch (arch) {126/* Midgard */127case 4:128case 5:129return 256;130131/* Bifrost, first generation */132case 6:133return 384;134135/* Bifrost, second generation (G31 is 512 but it doesn't matter) */136case 7:137return 768;138139/* Valhall (for completeness) */140default:141return 1024;142}143}144145static unsigned146panfrost_query_thread_tls_alloc(int fd, unsigned major)147{148unsigned tls = panfrost_query_raw(fd,149DRM_PANFROST_PARAM_THREAD_TLS_ALLOC, false, 0);150151return (tls > 0) ? tls : panfrost_max_thread_count(major);152}153154static uint32_t155panfrost_query_compressed_formats(int fd)156{157/* If unspecified, assume ASTC/ETC only. Factory default for Juno, and158* should exist on any Mali configuration. All hardware should report159* these texture formats but the kernel might not be new enough. */160161uint32_t default_set =162(1 << MALI_ETC2_RGB8) |163(1 << MALI_ETC2_R11_UNORM) |164(1 << MALI_ETC2_RGBA8) |165(1 << MALI_ETC2_RG11_UNORM) |166(1 << MALI_ETC2_R11_SNORM) |167(1 << MALI_ETC2_RG11_SNORM) |168(1 << MALI_ETC2_RGB8A1) |169(1 << MALI_ASTC_3D_LDR) |170(1 << MALI_ASTC_3D_HDR) |171(1 << MALI_ASTC_2D_LDR) |172(1 << MALI_ASTC_2D_HDR);173174return panfrost_query_raw(fd, DRM_PANFROST_PARAM_TEXTURE_FEATURES0,175false, default_set);176}177178/* DRM_PANFROST_PARAM_TEXTURE_FEATURES0 will return a bitmask of supported179* compressed formats, so we offer a helper to test if a format is supported */180181bool182panfrost_supports_compressed_format(struct panfrost_device *dev, unsigned fmt)183{184if (MALI_EXTRACT_TYPE(fmt) != MALI_FORMAT_COMPRESSED)185return true;186187unsigned idx = fmt & ~MALI_FORMAT_COMPRESSED;188assert(idx < 32);189190return dev->compressed_formats & (1 << idx);191}192193/* Returns the architecture version given a GPU ID, either from a table for194* old-style Midgard versions or directly for new-style Bifrost/Valhall195* versions */196197static unsigned198panfrost_major_version(unsigned gpu_id)199{200switch (gpu_id) {201case 0x600:202case 0x620:203case 0x720:204return 4;205case 0x750:206case 0x820:207case 0x830:208case 0x860:209case 0x880:210return 5;211default:212return gpu_id >> 12;213}214}215216/* Given a GPU ID like 0x860, return a prettified model name */217218const char *219panfrost_model_name(unsigned gpu_id)220{221switch (gpu_id) {222case 0x600: return "Mali T600 (Panfrost)";223case 0x620: return "Mali T620 (Panfrost)";224case 0x720: return "Mali T720 (Panfrost)";225case 0x820: return "Mali T820 (Panfrost)";226case 0x830: return "Mali T830 (Panfrost)";227case 0x750: return "Mali T760 (Panfrost)";228case 0x860: return "Mali T860 (Panfrost)";229case 0x880: return "Mali T880 (Panfrost)";230case 0x6221: return "Mali G72 (Panfrost)";231case 0x7093: return "Mali G31 (Panfrost)";232case 0x7212: return "Mali G52 (Panfrost)";233case 0x7402: return "Mali G52r1 (Panfrost)";234default:235unreachable("Invalid GPU ID");236}237}238239void240panfrost_open_device(void *memctx, int fd, struct panfrost_device *dev)241{242dev->fd = fd;243dev->memctx = memctx;244dev->gpu_id = panfrost_query_gpu_version(fd);245dev->arch = panfrost_major_version(dev->gpu_id);246dev->core_count = panfrost_query_core_count(fd);247dev->thread_tls_alloc = panfrost_query_thread_tls_alloc(fd, dev->arch);248dev->kernel_version = drmGetVersion(fd);249unsigned revision = panfrost_query_gpu_revision(fd);250dev->quirks = panfrost_get_quirks(dev->gpu_id, revision);251dev->compressed_formats = panfrost_query_compressed_formats(fd);252dev->tiler_features = panfrost_query_tiler_features(fd);253254if (dev->quirks & HAS_SWIZZLES)255dev->formats = panfrost_pipe_format_v6;256else257dev->formats = panfrost_pipe_format_v7;258259util_sparse_array_init(&dev->bo_map, sizeof(struct panfrost_bo), 512);260261pthread_mutex_init(&dev->bo_cache.lock, NULL);262list_inithead(&dev->bo_cache.lru);263264for (unsigned i = 0; i < ARRAY_SIZE(dev->bo_cache.buckets); ++i)265list_inithead(&dev->bo_cache.buckets[i]);266267/* Initialize pandecode before we start allocating */268if (dev->debug & (PAN_DBG_TRACE | PAN_DBG_SYNC))269pandecode_initialize(!(dev->debug & PAN_DBG_TRACE));270271/* Tiler heap is internally required by the tiler, which can only be272* active for a single job chain at once, so a single heap can be273* shared across batches/contextes */274275dev->tiler_heap = panfrost_bo_create(dev, 64 * 1024 * 1024,276PAN_BO_INVISIBLE | PAN_BO_GROWABLE, "Tiler heap");277278pthread_mutex_init(&dev->submit_lock, NULL);279280/* Done once on init */281panfrost_upload_sample_positions(dev);282}283284void285panfrost_close_device(struct panfrost_device *dev)286{287pthread_mutex_destroy(&dev->submit_lock);288panfrost_bo_unreference(dev->tiler_heap);289panfrost_bo_cache_evict_all(dev);290pthread_mutex_destroy(&dev->bo_cache.lock);291drmFreeVersion(dev->kernel_version);292util_sparse_array_finish(&dev->bo_map);293close(dev->fd);294}295296297