Path: blob/21.2-virgl/src/panfrost/lib/pan_scratch.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 "util/u_math.h"27#include "util/macros.h"28#include "pan_encoder.h"2930/* Midgard has a small register file, so shaders with high register pressure31* need to spill from the register file onto the stack. In addition to32* spilling, it is desireable to allocate temporary arrays on the stack (for33* instance because the register file does not support indirect access but the34* stack does).35*36* The stack is located in "Thread Local Storage", sometimes abbreviated TLS in37* the kernel source code. Thread local storage is allocated per-thread,38* per-core, so threads executing concurrently do not interfere with each39* other's stacks. On modern kernels, we may query40* DRM_PANFROST_PARAM_THREAD_TLS_ALLOC for the number of threads per core we41* must allocate for, and DRM_PANFROST_PARAM_SHADER_PRESENT for a bitmask of42* shader cores (so take a popcount of that mask for the number of shader43* cores). On older kernels that do not support querying these values,44* following kbase, we may use the worst-case value of 256 threads for45* THREAD_TLS_ALLOC, and the worst-case value of 16 cores for Midgard per the46* "shader core count" column of the implementations table in47* https://en.wikipedia.org/wiki/Mali_%28GPU% [citation needed]48*49* Within a particular thread, there is stack allocated. If it is present, its50* size is a power-of-two, and it is at least 16 bytes. Stack is allocated51* with the shared memory descriptor used for all shaders within a frame (note52* that they don't execute concurrently so it's fine). So, consider the maximum53* stack size used by any shader within a job, and then compute (where npot54* denotes the next power of two):55*56* bytes/thread = npot(max(size, 16))57* allocated = (# of bytes/thread) * (# of threads/core) * (# of cores)58*59* The size of Thread Local Storage is signaled to the GPU in the tls_size60* field, which has a log2 modifier and is in units of 16 bytes.61*/6263/* Computes log_stack_size = log2(ceil(s / 16)) */6465unsigned66panfrost_get_stack_shift(unsigned stack_size)67{68if (stack_size)69return util_logbase2_ceil(DIV_ROUND_UP(stack_size, 16));70else71return 0;72}7374/* Computes the aligned stack size given the shift and thread count. */7576unsigned77panfrost_get_total_stack_size(78unsigned thread_size,79unsigned threads_per_core,80unsigned core_count)81{82unsigned size_per_thread = (thread_size == 0) ? 0 :83util_next_power_of_two(ALIGN_POT(thread_size, 16));8485return size_per_thread * threads_per_core * core_count;86}878889