Debian Codegraph @

src/panfrost/lib/pan_scratch.c

raw
1 /*
2 * Copyright (C) 2019 Collabora, Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "util/macros.h"
7 #include "util/u_math.h"
8 #include "pan_encoder.h"
9
10 /* Midgard has a small register file, so shaders with high register pressure
11 * need to spill from the register file onto the stack. In addition to
12 * spilling, it is desireable to allocate temporary arrays on the stack (for
13 * instance because the register file does not support indirect access but the
14 * stack does).
15 *
16 * The stack is located in "Thread Local Storage", sometimes abbreviated TLS in
17 * the kernel source code. Thread local storage is allocated per-thread,
18 * per-core, so threads executing concurrently do not interfere with each
19 * other's stacks. On modern kernels, we may query
20 * DRM_PANFROST_PARAM_THREAD_TLS_ALLOC for the number of threads per core we
21 * must allocate for, and DRM_PANFROST_PARAM_SHADER_PRESENT for a bitmask of
22 * shader cores (so take a popcount of that mask for the number of shader
23 * cores). On older kernels that do not support querying these values,
24 * following kbase, we may use the worst-case value of 256 threads for
25 * THREAD_TLS_ALLOC, and the worst-case value of 16 cores for Midgard per the
26 * "shader core count" column of the implementations table in
27 * https://en.wikipedia.org/wiki/Mali_%28GPU% [citation needed]
28 *
29 * Within a particular thread, there is stack allocated. If it is present, its
30 * size is a power-of-two, and it is at least 16 bytes. Stack is allocated
31 * with the shared memory descriptor used for all shaders within a frame (note
32 * that they don't execute concurrently so it's fine). So, consider the maximum
33 * stack size used by any shader within a job, and then compute (where npot
34 * denotes the next power of two):
35 *
36 * bytes/thread = npot(max(size, 16))
37 * allocated = (# of bytes/thread) * (# of threads/core) * (# of cores)
38 *
39 * The size of Thread Local Storage is signaled to the GPU in the tls_size
40 * field, which has a log2 modifier and is in units of 16 bytes.
41 */
42
43 /* Computes log_stack_size = log2(ceil(s / 16)) */
44
45 unsigned
46 pan_get_stack_shift(unsigned stack_size)
47 {
48 if (stack_size)
49 return util_logbase2_ceil(DIV_ROUND_UP(stack_size, 16));
50 else
51 return 0;
52 }
53
54 /* Computes the aligned stack size given the shift and thread count. */
55
56 unsigned
57 pan_get_total_stack_size(unsigned thread_size, unsigned threads_per_core,
58 unsigned core_id_range)
59 {
60 unsigned size_per_thread =
61 (thread_size == 0) ? 0
62 : util_next_power_of_two(ALIGN_POT(thread_size, 16));
63
64 return size_per_thread * threads_per_core * core_id_range;
65 }