Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/panfrost/lib/pan_props.c
4560 views
1
/*
2
* Copyright (C) 2019 Collabora, Ltd.
3
*
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Software"),
6
* to deal in the Software without restriction, including without limitation
7
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
* and/or sell copies of the Software, and to permit persons to whom the
9
* Software is furnished to do so, subject to the following conditions:
10
*
11
* The above copyright notice and this permission notice (including the next
12
* paragraph) shall be included in all copies or substantial portions of the
13
* Software.
14
*
15
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
* SOFTWARE.
22
*
23
* Authors:
24
* Alyssa Rosenzweig <[email protected]>
25
*/
26
27
#include <xf86drm.h>
28
29
#include "util/u_math.h"
30
#include "util/macros.h"
31
#include "util/hash_table.h"
32
#include "util/u_thread.h"
33
#include "drm-uapi/panfrost_drm.h"
34
#include "pan_encoder.h"
35
#include "pan_device.h"
36
#include "panfrost-quirks.h"
37
#include "pan_bo.h"
38
#include "pan_texture.h"
39
#include "wrap.h"
40
#include "pan_util.h"
41
42
/* Abstraction over the raw drm_panfrost_get_param ioctl for fetching
43
* information about devices */
44
45
static __u64
46
panfrost_query_raw(
47
int fd,
48
enum drm_panfrost_param param,
49
bool required,
50
unsigned default_value)
51
{
52
struct drm_panfrost_get_param get_param = {0,};
53
ASSERTED int ret;
54
55
get_param.param = param;
56
ret = drmIoctl(fd, DRM_IOCTL_PANFROST_GET_PARAM, &get_param);
57
58
if (ret) {
59
assert(!required);
60
return default_value;
61
}
62
63
return get_param.value;
64
}
65
66
static unsigned
67
panfrost_query_gpu_version(int fd)
68
{
69
#ifndef NDEBUG
70
/* In debug builds, allow overriding the GPU ID, for example to run
71
* Bifrost shader-db on a Midgard machine. This is a bit less heavy
72
* handed than setting up the entirety of drm-shim */
73
char *override_version = getenv("PAN_GPU_ID");
74
75
if (override_version)
76
return strtol(override_version, NULL, 16);
77
#endif
78
79
return panfrost_query_raw(fd, DRM_PANFROST_PARAM_GPU_PROD_ID, true, 0);
80
}
81
82
static unsigned
83
panfrost_query_gpu_revision(int fd)
84
{
85
return panfrost_query_raw(fd, DRM_PANFROST_PARAM_GPU_REVISION, true, 0);
86
}
87
88
static struct panfrost_tiler_features
89
panfrost_query_tiler_features(int fd)
90
{
91
/* Default value (2^9 bytes and 8 levels) to match old behaviour */
92
uint32_t raw = panfrost_query_raw(fd, DRM_PANFROST_PARAM_TILER_FEATURES,
93
false, 0x809);
94
95
/* Bin size is log2 in the first byte, max levels in the second byte */
96
return (struct panfrost_tiler_features) {
97
.bin_size = (1 << (raw & BITFIELD_MASK(5))),
98
.max_levels = (raw >> 8) & BITFIELD_MASK(4)
99
};
100
}
101
102
static unsigned
103
panfrost_query_core_count(int fd)
104
{
105
/* On older kernels, worst-case to 16 cores */
106
107
unsigned mask = panfrost_query_raw(fd,
108
DRM_PANFROST_PARAM_SHADER_PRESENT, false, 0xffff);
109
110
/* Some cores might be absent. For TLS computation purposes, we care
111
* about the greatest ID + 1, which equals the core count if all cores
112
* are present, but allocates space for absent cores if needed.
113
* util_last_bit is defined to return the greatest bit set + 1, which
114
* is exactly what we need. */
115
116
return util_last_bit(mask);
117
}
118
119
/* Architectural maximums, since this register may be not implemented
120
* by a given chip. G31 is actually 512 instead of 768 but it doesn't
121
* really matter. */
122
123
static unsigned
124
panfrost_max_thread_count(unsigned arch)
125
{
126
switch (arch) {
127
/* Midgard */
128
case 4:
129
case 5:
130
return 256;
131
132
/* Bifrost, first generation */
133
case 6:
134
return 384;
135
136
/* Bifrost, second generation (G31 is 512 but it doesn't matter) */
137
case 7:
138
return 768;
139
140
/* Valhall (for completeness) */
141
default:
142
return 1024;
143
}
144
}
145
146
static unsigned
147
panfrost_query_thread_tls_alloc(int fd, unsigned major)
148
{
149
unsigned tls = panfrost_query_raw(fd,
150
DRM_PANFROST_PARAM_THREAD_TLS_ALLOC, false, 0);
151
152
return (tls > 0) ? tls : panfrost_max_thread_count(major);
153
}
154
155
static uint32_t
156
panfrost_query_compressed_formats(int fd)
157
{
158
/* If unspecified, assume ASTC/ETC only. Factory default for Juno, and
159
* should exist on any Mali configuration. All hardware should report
160
* these texture formats but the kernel might not be new enough. */
161
162
uint32_t default_set =
163
(1 << MALI_ETC2_RGB8) |
164
(1 << MALI_ETC2_R11_UNORM) |
165
(1 << MALI_ETC2_RGBA8) |
166
(1 << MALI_ETC2_RG11_UNORM) |
167
(1 << MALI_ETC2_R11_SNORM) |
168
(1 << MALI_ETC2_RG11_SNORM) |
169
(1 << MALI_ETC2_RGB8A1) |
170
(1 << MALI_ASTC_3D_LDR) |
171
(1 << MALI_ASTC_3D_HDR) |
172
(1 << MALI_ASTC_2D_LDR) |
173
(1 << MALI_ASTC_2D_HDR);
174
175
return panfrost_query_raw(fd, DRM_PANFROST_PARAM_TEXTURE_FEATURES0,
176
false, default_set);
177
}
178
179
/* DRM_PANFROST_PARAM_TEXTURE_FEATURES0 will return a bitmask of supported
180
* compressed formats, so we offer a helper to test if a format is supported */
181
182
bool
183
panfrost_supports_compressed_format(struct panfrost_device *dev, unsigned fmt)
184
{
185
if (MALI_EXTRACT_TYPE(fmt) != MALI_FORMAT_COMPRESSED)
186
return true;
187
188
unsigned idx = fmt & ~MALI_FORMAT_COMPRESSED;
189
assert(idx < 32);
190
191
return dev->compressed_formats & (1 << idx);
192
}
193
194
/* Returns the architecture version given a GPU ID, either from a table for
195
* old-style Midgard versions or directly for new-style Bifrost/Valhall
196
* versions */
197
198
static unsigned
199
panfrost_major_version(unsigned gpu_id)
200
{
201
switch (gpu_id) {
202
case 0x600:
203
case 0x620:
204
case 0x720:
205
return 4;
206
case 0x750:
207
case 0x820:
208
case 0x830:
209
case 0x860:
210
case 0x880:
211
return 5;
212
default:
213
return gpu_id >> 12;
214
}
215
}
216
217
/* Given a GPU ID like 0x860, return a prettified model name */
218
219
const char *
220
panfrost_model_name(unsigned gpu_id)
221
{
222
switch (gpu_id) {
223
case 0x600: return "Mali T600 (Panfrost)";
224
case 0x620: return "Mali T620 (Panfrost)";
225
case 0x720: return "Mali T720 (Panfrost)";
226
case 0x820: return "Mali T820 (Panfrost)";
227
case 0x830: return "Mali T830 (Panfrost)";
228
case 0x750: return "Mali T760 (Panfrost)";
229
case 0x860: return "Mali T860 (Panfrost)";
230
case 0x880: return "Mali T880 (Panfrost)";
231
case 0x6221: return "Mali G72 (Panfrost)";
232
case 0x7093: return "Mali G31 (Panfrost)";
233
case 0x7212: return "Mali G52 (Panfrost)";
234
case 0x7402: return "Mali G52r1 (Panfrost)";
235
default:
236
unreachable("Invalid GPU ID");
237
}
238
}
239
240
void
241
panfrost_open_device(void *memctx, int fd, struct panfrost_device *dev)
242
{
243
dev->fd = fd;
244
dev->memctx = memctx;
245
dev->gpu_id = panfrost_query_gpu_version(fd);
246
dev->arch = panfrost_major_version(dev->gpu_id);
247
dev->core_count = panfrost_query_core_count(fd);
248
dev->thread_tls_alloc = panfrost_query_thread_tls_alloc(fd, dev->arch);
249
dev->kernel_version = drmGetVersion(fd);
250
unsigned revision = panfrost_query_gpu_revision(fd);
251
dev->quirks = panfrost_get_quirks(dev->gpu_id, revision);
252
dev->compressed_formats = panfrost_query_compressed_formats(fd);
253
dev->tiler_features = panfrost_query_tiler_features(fd);
254
255
if (dev->quirks & HAS_SWIZZLES)
256
dev->formats = panfrost_pipe_format_v6;
257
else
258
dev->formats = panfrost_pipe_format_v7;
259
260
util_sparse_array_init(&dev->bo_map, sizeof(struct panfrost_bo), 512);
261
262
pthread_mutex_init(&dev->bo_cache.lock, NULL);
263
list_inithead(&dev->bo_cache.lru);
264
265
for (unsigned i = 0; i < ARRAY_SIZE(dev->bo_cache.buckets); ++i)
266
list_inithead(&dev->bo_cache.buckets[i]);
267
268
/* Initialize pandecode before we start allocating */
269
if (dev->debug & (PAN_DBG_TRACE | PAN_DBG_SYNC))
270
pandecode_initialize(!(dev->debug & PAN_DBG_TRACE));
271
272
/* Tiler heap is internally required by the tiler, which can only be
273
* active for a single job chain at once, so a single heap can be
274
* shared across batches/contextes */
275
276
dev->tiler_heap = panfrost_bo_create(dev, 64 * 1024 * 1024,
277
PAN_BO_INVISIBLE | PAN_BO_GROWABLE, "Tiler heap");
278
279
pthread_mutex_init(&dev->submit_lock, NULL);
280
281
/* Done once on init */
282
panfrost_upload_sample_positions(dev);
283
}
284
285
void
286
panfrost_close_device(struct panfrost_device *dev)
287
{
288
pthread_mutex_destroy(&dev->submit_lock);
289
panfrost_bo_unreference(dev->tiler_heap);
290
panfrost_bo_cache_evict_all(dev);
291
pthread_mutex_destroy(&dev->bo_cache.lock);
292
drmFreeVersion(dev->kernel_version);
293
util_sparse_array_finish(&dev->bo_map);
294
close(dev->fd);
295
}
296
297