1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2019 Collabora, Ltd.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *   Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "util/u_math.h"
28bf215546Sopenharmony_ci#include "util/macros.h"
29bf215546Sopenharmony_ci#include "pan_encoder.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci/* Midgard has a small register file, so shaders with high register pressure
32bf215546Sopenharmony_ci * need to spill from the register file onto the stack. In addition to
33bf215546Sopenharmony_ci * spilling, it is desireable to allocate temporary arrays on the stack (for
34bf215546Sopenharmony_ci * instance because the register file does not support indirect access but the
35bf215546Sopenharmony_ci * stack does).
36bf215546Sopenharmony_ci *
37bf215546Sopenharmony_ci * The stack is located in "Thread Local Storage", sometimes abbreviated TLS in
38bf215546Sopenharmony_ci * the kernel source code. Thread local storage is allocated per-thread,
39bf215546Sopenharmony_ci * per-core, so threads executing concurrently do not interfere with each
40bf215546Sopenharmony_ci * other's stacks. On modern kernels, we may query
41bf215546Sopenharmony_ci * DRM_PANFROST_PARAM_THREAD_TLS_ALLOC for the number of threads per core we
42bf215546Sopenharmony_ci * must allocate for, and DRM_PANFROST_PARAM_SHADER_PRESENT for a bitmask of
43bf215546Sopenharmony_ci * shader cores (so take a popcount of that mask for the number of shader
44bf215546Sopenharmony_ci * cores). On older kernels that do not support querying these values,
45bf215546Sopenharmony_ci * following kbase, we may use the worst-case value of 256 threads for
46bf215546Sopenharmony_ci * THREAD_TLS_ALLOC, and the worst-case value of 16 cores for Midgard per the
47bf215546Sopenharmony_ci * "shader core count" column of the implementations table in
48bf215546Sopenharmony_ci * https://en.wikipedia.org/wiki/Mali_%28GPU% [citation needed]
49bf215546Sopenharmony_ci *
50bf215546Sopenharmony_ci * Within a particular thread, there is stack allocated. If it is present, its
51bf215546Sopenharmony_ci * size is a power-of-two, and it is at least 16 bytes. Stack is allocated
52bf215546Sopenharmony_ci * with the shared memory descriptor used for all shaders within a frame (note
53bf215546Sopenharmony_ci * that they don't execute concurrently so it's fine). So, consider the maximum
54bf215546Sopenharmony_ci * stack size used by any shader within a job, and then compute (where npot
55bf215546Sopenharmony_ci * denotes the next power of two):
56bf215546Sopenharmony_ci *
57bf215546Sopenharmony_ci *      bytes/thread = npot(max(size, 16))
58bf215546Sopenharmony_ci *      allocated = (# of bytes/thread) * (# of threads/core) * (# of cores)
59bf215546Sopenharmony_ci *
60bf215546Sopenharmony_ci * The size of Thread Local Storage is signaled to the GPU in the tls_size
61bf215546Sopenharmony_ci * field, which has a log2 modifier and is in units of 16 bytes.
62bf215546Sopenharmony_ci */
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci/* Computes log_stack_size = log2(ceil(s / 16)) */
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ciunsigned
67bf215546Sopenharmony_cipanfrost_get_stack_shift(unsigned stack_size)
68bf215546Sopenharmony_ci{
69bf215546Sopenharmony_ci        if (stack_size)
70bf215546Sopenharmony_ci                return util_logbase2_ceil(DIV_ROUND_UP(stack_size, 16));
71bf215546Sopenharmony_ci        else
72bf215546Sopenharmony_ci                return 0;
73bf215546Sopenharmony_ci}
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci/* Computes the aligned stack size given the shift and thread count. */
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ciunsigned
78bf215546Sopenharmony_cipanfrost_get_total_stack_size(
79bf215546Sopenharmony_ci                unsigned thread_size,
80bf215546Sopenharmony_ci                unsigned threads_per_core,
81bf215546Sopenharmony_ci                unsigned core_id_range)
82bf215546Sopenharmony_ci{
83bf215546Sopenharmony_ci        unsigned size_per_thread = (thread_size == 0) ? 0 :
84bf215546Sopenharmony_ci                util_next_power_of_two(ALIGN_POT(thread_size, 16));
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci        return size_per_thread * threads_per_core * core_id_range;
87bf215546Sopenharmony_ci}
88