xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/panfrost/pan_device.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2018-2019 Alyssa Rosenzweig
4  * Copyright 2018-2019 Collabora, Ltd.
5  * Copyright © 2015 Intel Corporation
6  * All Rights Reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sub license, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the
17  * next paragraph) shall be included in all copies or substantial portions
18  * of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
24  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  *
28  **************************************************************************/
29 
30 #ifndef PAN_DEVICE_H
31 #define PAN_DEVICE_H
32 
33 #include <xf86drm.h>
34 #include "renderonly/renderonly.h"
35 #include "util/bitset.h"
36 #include "util/list.h"
37 #include "util/sparse_array.h"
38 #include "util/timespec.h"
39 #include "util/u_dynarray.h"
40 
41 #include "panfrost/util/pan_ir.h"
42 #include "pan_blend.h"
43 #include "pan_blitter.h"
44 #include "pan_indirect_dispatch.h"
45 #include "pan_pool.h"
46 #include "pan_props.h"
47 #include "pan_util.h"
48 
49 #include "kmod/pan_kmod.h"
50 
51 #include <genxml/gen_macros.h>
52 
53 #if defined(__cplusplus)
54 extern "C" {
55 #endif
56 
57 /* Always reserve the lower 32MB */
58 #define PAN_VA_USER_START 0x2000000ull
59 
60 /* Max address space size allowed */
61 #define PAN_VA_USER_END (1ull << 48ull)
62 
63 /* Driver limits */
64 #define PAN_MAX_CONST_BUFFERS 16
65 
66 /* Mali hardware can texture up to 65536 x 65536 x 65536 and render up to 16384
67  * x 16384, but 8192 x 8192 should be enough for anyone.  The OpenGL game
68  * "Cathedral" requires a texture of width 8192 to start.
69  */
70 #define PAN_MAX_MIP_LEVELS 14
71 
72 #define PAN_MAX_TEXEL_BUFFER_ELEMENTS 65536
73 
74 /* How many power-of-two levels in the BO cache do we want? 2^12
75  * minimum chosen as it is the page size that all allocations are
76  * rounded to */
77 
78 #define MIN_BO_CACHE_BUCKET (12) /* 2^12 = 4KB */
79 #define MAX_BO_CACHE_BUCKET (22) /* 2^22 = 4MB */
80 
81 /* Fencepost problem, hence the off-by-one */
82 #define NR_BO_CACHE_BUCKETS (MAX_BO_CACHE_BUCKET - MIN_BO_CACHE_BUCKET + 1)
83 
84 struct panfrost_device {
85    /* For ralloc */
86    void *memctx;
87 
88    /* Kmod objects. */
89    struct {
90       /* The pan_kmod_dev object backing this device. */
91       struct pan_kmod_dev *dev;
92 
93       /* Cached pan_kmod_dev_props properties queried at device create time. */
94       struct pan_kmod_dev_props props;
95 
96       /* VM attached to this device. */
97       struct pan_kmod_vm *vm;
98    } kmod;
99 
100    /* For pandecode */
101    struct pandecode_context *decode_ctx;
102 
103    /* Properties of the GPU in use */
104    unsigned arch;
105 
106    /* Number of shader cores */
107    unsigned core_count;
108 
109    /* Range of core IDs, equal to the maximum core ID + 1. Satisfies
110     * core_id_range >= core_count.
111     */
112    unsigned core_id_range;
113 
114    /* Maximum tilebuffer size in bytes for optimal performance. */
115    unsigned optimal_tib_size;
116 
117    unsigned thread_tls_alloc;
118    struct panfrost_tiler_features tiler_features;
119    const struct panfrost_model *model;
120    bool has_afbc;
121    bool has_afrc;
122 
123    /* Table of formats, indexed by a PIPE format */
124    const struct panfrost_format *formats;
125    const struct pan_blendable_format *blendable_formats;
126 
127    /* Bitmask of supported compressed texture formats */
128    uint32_t compressed_formats;
129 
130    /* debug flags, see pan_util.h how to interpret */
131    unsigned debug;
132 
133    struct renderonly *ro;
134 
135    pthread_mutex_t bo_map_lock;
136    struct util_sparse_array bo_map;
137 
138    struct {
139       pthread_mutex_t lock;
140 
141       /* List containing all cached BOs sorted in LRU (Least
142        * Recently Used) order. This allows us to quickly evict BOs
143        * that are more than 1 second old.
144        */
145       struct list_head lru;
146 
147       /* The BO cache is a set of buckets with power-of-two sizes
148        * ranging from 2^12 (4096, the page size) to
149        * 2^(12 + MAX_BO_CACHE_BUCKETS).
150        * Each bucket is a linked list of free panfrost_bo objects. */
151 
152       struct list_head buckets[NR_BO_CACHE_BUCKETS];
153    } bo_cache;
154 
155    struct pan_blitter_cache blitter;
156    struct pan_blend_shader_cache blend_shaders;
157    struct pan_indirect_dispatch_meta indirect_dispatch;
158 
159    /* Tiler heap shared across all tiler jobs, allocated against the
160     * device since there's only a single tiler. Since this is invisible to
161     * the CPU, it's okay for multiple contexts to reference it
162     * simultaneously; by keeping on the device struct, we eliminate a
163     * costly per-context allocation. */
164 
165    struct panfrost_bo *tiler_heap;
166 
167    /* The tiler heap is shared by all contexts, and is written by tiler
168     * jobs and read by fragment job. We need to ensure that a
169     * vertex/tiler job chain from one context is not inserted between
170     * the vertex/tiler and fragment job of another context, otherwise
171     * we end up with tiler heap corruption.
172     */
173    pthread_mutex_t submit_lock;
174 
175    /* Sample positions are preloaded into a write-once constant buffer,
176     * such that they can be referenced fore free later. Needed
177     * unconditionally on Bifrost, and useful for sharing with Midgard */
178 
179    struct panfrost_bo *sample_positions;
180 };
181 
182 static inline int
panfrost_device_fd(const struct panfrost_device * dev)183 panfrost_device_fd(const struct panfrost_device *dev)
184 {
185    return dev->kmod.dev->fd;
186 }
187 
188 static inline uint32_t
panfrost_device_gpu_id(const struct panfrost_device * dev)189 panfrost_device_gpu_id(const struct panfrost_device *dev)
190 {
191    return dev->kmod.props.gpu_prod_id;
192 }
193 
194 static inline uint32_t
panfrost_device_gpu_rev(const struct panfrost_device * dev)195 panfrost_device_gpu_rev(const struct panfrost_device *dev)
196 {
197    return dev->kmod.props.gpu_revision;
198 }
199 
200 static inline int
panfrost_device_kmod_version_major(const struct panfrost_device * dev)201 panfrost_device_kmod_version_major(const struct panfrost_device *dev)
202 {
203    return dev->kmod.dev->driver.version.major;
204 }
205 
206 static inline int
panfrost_device_kmod_version_minor(const struct panfrost_device * dev)207 panfrost_device_kmod_version_minor(const struct panfrost_device *dev)
208 {
209    return dev->kmod.dev->driver.version.minor;
210 }
211 
212 void panfrost_open_device(void *memctx, int fd, struct panfrost_device *dev);
213 
214 void panfrost_close_device(struct panfrost_device *dev);
215 
216 bool panfrost_supports_compressed_format(struct panfrost_device *dev,
217                                          unsigned fmt);
218 
219 static inline struct panfrost_bo *
pan_lookup_bo(struct panfrost_device * dev,uint32_t gem_handle)220 pan_lookup_bo(struct panfrost_device *dev, uint32_t gem_handle)
221 {
222    return (struct panfrost_bo *)util_sparse_array_get(&dev->bo_map, gem_handle);
223 }
224 
225 static inline bool
pan_is_bifrost(const struct panfrost_device * dev)226 pan_is_bifrost(const struct panfrost_device *dev)
227 {
228    return dev->arch >= 6 && dev->arch <= 7;
229 }
230 
231 static inline uint64_t
pan_gpu_time_to_ns(struct panfrost_device * dev,uint64_t gpu_time)232 pan_gpu_time_to_ns(struct panfrost_device *dev, uint64_t gpu_time)
233 {
234    return (gpu_time * NSEC_PER_SEC) / dev->kmod.props.timestamp_frequency;
235 }
236 
237 #if defined(__cplusplus)
238 } // extern "C"
239 #endif
240 
241 #endif
242