1 /*
2 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
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 * Rob Clark <robclark@freedesktop.org>
25 */
26
27
28 #include "pipe/p_defines.h"
29 #include "pipe/p_screen.h"
30 #include "pipe/p_state.h"
31
32 #include "util/u_memory.h"
33 #include "util/u_inlines.h"
34 #include "util/format/u_format.h"
35 #include "util/format/u_format_s3tc.h"
36 #include "util/u_screen.h"
37 #include "util/u_string.h"
38 #include "util/u_debug.h"
39
40 #include "util/os_time.h"
41
42 #include "drm-uapi/drm_fourcc.h"
43 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <sys/sysinfo.h>
47
48 #include "freedreno_screen.h"
49 #include "freedreno_resource.h"
50 #include "freedreno_fence.h"
51 #include "freedreno_query.h"
52 #include "freedreno_util.h"
53
54 #include "a2xx/fd2_screen.h"
55 #include "a3xx/fd3_screen.h"
56 #include "a4xx/fd4_screen.h"
57 #include "a5xx/fd5_screen.h"
58 #include "a6xx/fd6_screen.h"
59
60 /* for fd_get_driver/device_uuid() */
61 #include "common/freedreno_uuid.h"
62
63 #include "ir3/ir3_nir.h"
64 #include "ir3/ir3_compiler.h"
65 #include "a2xx/ir2.h"
66
67 static const struct debug_named_value debug_options[] = {
68 {"msgs", FD_DBG_MSGS, "Print debug messages"},
69 {"disasm", FD_DBG_DISASM, "Dump TGSI and adreno shader disassembly (a2xx only, see IR3_SHADER_DEBUG)"},
70 {"dclear", FD_DBG_DCLEAR, "Mark all state dirty after clear"},
71 {"ddraw", FD_DBG_DDRAW, "Mark all state dirty after draw"},
72 {"noscis", FD_DBG_NOSCIS, "Disable scissor optimization"},
73 {"direct", FD_DBG_DIRECT, "Force inline (SS_DIRECT) state loads"},
74 {"nobypass", FD_DBG_NOBYPASS, "Disable GMEM bypass"},
75 {"log", FD_DBG_LOG, "Enable GPU timestamp based logging (a6xx+)"},
76 {"nobin", FD_DBG_NOBIN, "Disable hw binning"},
77 {"nogmem", FD_DBG_NOGMEM, "Disable GMEM rendering (bypass only)"},
78 /* BIT(10) */
79 {"shaderdb", FD_DBG_SHADERDB, "Enable shaderdb output"},
80 {"flush", FD_DBG_FLUSH, "Force flush after every draw"},
81 {"deqp", FD_DBG_DEQP, "Enable dEQP hacks"},
82 {"inorder", FD_DBG_INORDER,"Disable reordering for draws/blits"},
83 {"bstat", FD_DBG_BSTAT, "Print batch stats at context destroy"},
84 {"nogrow", FD_DBG_NOGROW, "Disable \"growable\" cmdstream buffers, even if kernel supports it"},
85 {"lrz", FD_DBG_LRZ, "Enable experimental LRZ support (a5xx)"},
86 {"noindirect",FD_DBG_NOINDR, "Disable hw indirect draws (emulate on CPU)"},
87 {"noblit", FD_DBG_NOBLIT, "Disable blitter (fallback to generic blit path)"},
88 {"hiprio", FD_DBG_HIPRIO, "Force high-priority context"},
89 {"ttile", FD_DBG_TTILE, "Enable texture tiling (a2xx/a3xx/a5xx)"},
90 {"perfcntrs", FD_DBG_PERFC, "Expose performance counters"},
91 {"noubwc", FD_DBG_NOUBWC, "Disable UBWC for all internal buffers"},
92 {"nolrz", FD_DBG_NOLRZ, "Disable LRZ (a6xx)"},
93 {"notile", FD_DBG_NOTILE, "Disable tiling for all internal buffers"},
94 {"layout", FD_DBG_LAYOUT, "Dump resource layouts"},
95 {"nofp16", FD_DBG_NOFP16, "Disable mediump precision lowering"},
96 {"nohw", FD_DBG_NOHW, "Disable submitting commands to the HW"},
97 DEBUG_NAMED_VALUE_END
98 };
99
100 DEBUG_GET_ONCE_FLAGS_OPTION(fd_mesa_debug, "FD_MESA_DEBUG", debug_options, 0)
101
102 int fd_mesa_debug = 0;
103 bool fd_binning_enabled = true;
104
105 static const char *
fd_screen_get_name(struct pipe_screen * pscreen)106 fd_screen_get_name(struct pipe_screen *pscreen)
107 {
108 static char buffer[128];
109 snprintf(buffer, sizeof(buffer), "FD%03d",
110 fd_screen(pscreen)->device_id);
111 return buffer;
112 }
113
114 static const char *
fd_screen_get_vendor(struct pipe_screen * pscreen)115 fd_screen_get_vendor(struct pipe_screen *pscreen)
116 {
117 return "freedreno";
118 }
119
120 static const char *
fd_screen_get_device_vendor(struct pipe_screen * pscreen)121 fd_screen_get_device_vendor(struct pipe_screen *pscreen)
122 {
123 return "Qualcomm";
124 }
125
126
127 static uint64_t
fd_screen_get_timestamp(struct pipe_screen * pscreen)128 fd_screen_get_timestamp(struct pipe_screen *pscreen)
129 {
130 struct fd_screen *screen = fd_screen(pscreen);
131
132 if (screen->has_timestamp) {
133 uint64_t n;
134 fd_pipe_get_param(screen->pipe, FD_TIMESTAMP, &n);
135 debug_assert(screen->max_freq > 0);
136 return n * 1000000000 / screen->max_freq;
137 } else {
138 int64_t cpu_time = os_time_get() * 1000;
139 return cpu_time + screen->cpu_gpu_time_delta;
140 }
141
142 }
143
144 static void
fd_screen_destroy(struct pipe_screen * pscreen)145 fd_screen_destroy(struct pipe_screen *pscreen)
146 {
147 struct fd_screen *screen = fd_screen(pscreen);
148
149 if (screen->pipe)
150 fd_pipe_del(screen->pipe);
151
152 if (screen->dev)
153 fd_device_del(screen->dev);
154
155 if (screen->ro)
156 FREE(screen->ro);
157
158 fd_bc_fini(&screen->batch_cache);
159 fd_gmem_screen_fini(pscreen);
160
161 slab_destroy_parent(&screen->transfer_pool);
162
163 simple_mtx_destroy(&screen->lock);
164
165 u_transfer_helper_destroy(pscreen->transfer_helper);
166
167 if (screen->compiler)
168 ir3_compiler_destroy(screen->compiler);
169
170 ralloc_free(screen->live_batches);
171
172 free(screen->perfcntr_queries);
173 free(screen);
174 }
175
176 /*
177 TODO either move caps to a2xx/a3xx specific code, or maybe have some
178 tables for things that differ if the delta is not too much..
179 */
180 static int
fd_screen_get_param(struct pipe_screen * pscreen,enum pipe_cap param)181 fd_screen_get_param(struct pipe_screen *pscreen, enum pipe_cap param)
182 {
183 struct fd_screen *screen = fd_screen(pscreen);
184
185 /* this is probably not totally correct.. but it's a start: */
186 switch (param) {
187 /* Supported features (boolean caps). */
188 case PIPE_CAP_NPOT_TEXTURES:
189 case PIPE_CAP_MIXED_FRAMEBUFFER_SIZES:
190 case PIPE_CAP_ANISOTROPIC_FILTER:
191 case PIPE_CAP_POINT_SPRITE:
192 case PIPE_CAP_BLEND_EQUATION_SEPARATE:
193 case PIPE_CAP_TEXTURE_SWIZZLE:
194 case PIPE_CAP_MIXED_COLORBUFFER_FORMATS:
195 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT:
196 case PIPE_CAP_SEAMLESS_CUBE_MAP:
197 case PIPE_CAP_VERTEX_COLOR_UNCLAMPED:
198 case PIPE_CAP_QUADS_FOLLOW_PROVOKING_VERTEX_CONVENTION:
199 case PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT:
200 case PIPE_CAP_STRING_MARKER:
201 case PIPE_CAP_MIXED_COLOR_DEPTH_BITS:
202 case PIPE_CAP_TEXTURE_BARRIER:
203 case PIPE_CAP_INVALIDATE_BUFFER:
204 case PIPE_CAP_RGB_OVERRIDE_DST_ALPHA_BLEND:
205 case PIPE_CAP_GLSL_TESS_LEVELS_AS_INPUTS:
206 case PIPE_CAP_NIR_COMPACT_ARRAYS:
207 return 1;
208
209 case PIPE_CAP_VERTEX_BUFFER_OFFSET_4BYTE_ALIGNED_ONLY:
210 case PIPE_CAP_VERTEX_BUFFER_STRIDE_4BYTE_ALIGNED_ONLY:
211 case PIPE_CAP_VERTEX_ELEMENT_SRC_OFFSET_4BYTE_ALIGNED_ONLY:
212 return !is_a2xx(screen);
213
214 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER:
215 return is_a2xx(screen);
216 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER:
217 return !is_a2xx(screen);
218
219 case PIPE_CAP_PACKED_UNIFORMS:
220 return !is_a2xx(screen);
221
222 case PIPE_CAP_ROBUST_BUFFER_ACCESS_BEHAVIOR:
223 case PIPE_CAP_DEVICE_RESET_STATUS_QUERY:
224 return screen->has_robustness;
225
226 case PIPE_CAP_VERTEXID_NOBASE:
227 return is_a3xx(screen) || is_a4xx(screen);
228
229 case PIPE_CAP_COMPUTE:
230 return has_compute(screen);
231
232 case PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER:
233 case PIPE_CAP_PCI_GROUP:
234 case PIPE_CAP_PCI_BUS:
235 case PIPE_CAP_PCI_DEVICE:
236 case PIPE_CAP_PCI_FUNCTION:
237 return 0;
238
239 case PIPE_CAP_FRAGMENT_SHADER_TEXTURE_LOD:
240 case PIPE_CAP_FRAGMENT_SHADER_DERIVATIVES:
241 case PIPE_CAP_VERTEX_SHADER_SATURATE:
242 case PIPE_CAP_PRIMITIVE_RESTART:
243 case PIPE_CAP_PRIMITIVE_RESTART_FIXED_INDEX:
244 case PIPE_CAP_TGSI_INSTANCEID:
245 case PIPE_CAP_VERTEX_ELEMENT_INSTANCE_DIVISOR:
246 case PIPE_CAP_INDEP_BLEND_ENABLE:
247 case PIPE_CAP_INDEP_BLEND_FUNC:
248 case PIPE_CAP_TEXTURE_BUFFER_OBJECTS:
249 case PIPE_CAP_TEXTURE_HALF_FLOAT_LINEAR:
250 case PIPE_CAP_CONDITIONAL_RENDER:
251 case PIPE_CAP_CONDITIONAL_RENDER_INVERTED:
252 case PIPE_CAP_SEAMLESS_CUBE_MAP_PER_TEXTURE:
253 case PIPE_CAP_CLIP_HALFZ:
254 return is_a3xx(screen) || is_a4xx(screen) || is_a5xx(screen) || is_a6xx(screen);
255
256 case PIPE_CAP_FAKE_SW_MSAA:
257 return !fd_screen_get_param(pscreen, PIPE_CAP_TEXTURE_MULTISAMPLE);
258
259 case PIPE_CAP_TEXTURE_MULTISAMPLE:
260 return is_a5xx(screen) || is_a6xx(screen);
261
262 case PIPE_CAP_SURFACE_SAMPLE_COUNT:
263 return is_a6xx(screen);
264
265 case PIPE_CAP_DEPTH_CLIP_DISABLE:
266 return is_a3xx(screen) || is_a4xx(screen) || is_a6xx(screen);
267
268 case PIPE_CAP_DEPTH_CLIP_DISABLE_SEPARATE:
269 return is_a6xx(screen);
270
271 case PIPE_CAP_POLYGON_OFFSET_CLAMP:
272 return is_a4xx(screen) || is_a5xx(screen) || is_a6xx(screen);
273
274 case PIPE_CAP_TEXTURE_BUFFER_OFFSET_ALIGNMENT:
275 if (is_a3xx(screen)) return 16;
276 if (is_a4xx(screen)) return 32;
277 if (is_a5xx(screen)) return 32;
278 if (is_a6xx(screen)) return 64;
279 return 0;
280 case PIPE_CAP_MAX_TEXTURE_BUFFER_SIZE:
281 /* We could possibly emulate more by pretending 2d/rect textures and
282 * splitting high bits of index into 2nd dimension..
283 */
284 if (is_a3xx(screen)) return 8192;
285 if (is_a4xx(screen)) return 16384;
286 if (is_a5xx(screen)) return 16384;
287 if (is_a6xx(screen)) return 1 << 27;
288 return 0;
289
290 case PIPE_CAP_TEXTURE_FLOAT_LINEAR:
291 case PIPE_CAP_CUBE_MAP_ARRAY:
292 case PIPE_CAP_SAMPLER_VIEW_TARGET:
293 case PIPE_CAP_TEXTURE_QUERY_LOD:
294 return is_a4xx(screen) || is_a5xx(screen) || is_a6xx(screen);
295
296 case PIPE_CAP_START_INSTANCE:
297 /* Note that a5xx can do this, it just can't (at least with
298 * current firmware) do draw_indirect with base_instance.
299 * Since draw_indirect is needed sooner (gles31 and gl40 vs
300 * gl42), hide base_instance on a5xx. :-/
301 */
302 return is_a4xx(screen);
303
304 case PIPE_CAP_CONSTANT_BUFFER_OFFSET_ALIGNMENT:
305 return is_a2xx(screen) ? 64 : 32;
306
307 case PIPE_CAP_GLSL_FEATURE_LEVEL:
308 case PIPE_CAP_GLSL_FEATURE_LEVEL_COMPATIBILITY:
309 return is_ir3(screen) ? 140 : 120;
310
311 case PIPE_CAP_ESSL_FEATURE_LEVEL:
312 /* we can probably enable 320 for a5xx too, but need to test: */
313 if (is_a6xx(screen)) return 320;
314 if (is_a5xx(screen)) return 310;
315 if (is_ir3(screen)) return 300;
316 return 120;
317
318 case PIPE_CAP_SHADER_BUFFER_OFFSET_ALIGNMENT:
319 if (is_a6xx(screen)) return 64;
320 if (is_a5xx(screen)) return 4;
321 return 0;
322
323 case PIPE_CAP_MAX_TEXTURE_GATHER_COMPONENTS:
324 if (is_a4xx(screen) || is_a5xx(screen) || is_a6xx(screen))
325 return 4;
326 return 0;
327
328 /* TODO if we need this, do it in nir/ir3 backend to avoid breaking precompile: */
329 case PIPE_CAP_FORCE_PERSAMPLE_INTERP:
330 return 0;
331
332 case PIPE_CAP_FBFETCH:
333 if (fd_device_version(screen->dev) >= FD_VERSION_GMEM_BASE &&
334 is_a6xx(screen))
335 return 1;
336 return 0;
337 case PIPE_CAP_SAMPLE_SHADING:
338 if (is_a6xx(screen)) return 1;
339 return 0;
340
341 case PIPE_CAP_CONTEXT_PRIORITY_MASK:
342 return screen->priority_mask;
343
344 case PIPE_CAP_DRAW_INDIRECT:
345 if (is_a4xx(screen) || is_a5xx(screen) || is_a6xx(screen))
346 return 1;
347 return 0;
348
349 case PIPE_CAP_FRAMEBUFFER_NO_ATTACHMENT:
350 if (is_a4xx(screen) || is_a5xx(screen) || is_a6xx(screen))
351 return 1;
352 return 0;
353
354 case PIPE_CAP_LOAD_CONSTBUF:
355 /* name is confusing, but this turns on std430 packing */
356 if (is_ir3(screen))
357 return 1;
358 return 0;
359
360 case PIPE_CAP_NIR_IMAGES_AS_DEREF:
361 return 0;
362
363 case PIPE_CAP_MAX_VIEWPORTS:
364 return 1;
365
366 case PIPE_CAP_MAX_VARYINGS:
367 return 16;
368
369 case PIPE_CAP_MAX_SHADER_PATCH_VARYINGS:
370 /* We don't really have a limit on this, it all goes into the main
371 * memory buffer. Needs to be at least 120 / 4 (minimum requirement
372 * for GL_MAX_TESS_PATCH_COMPONENTS).
373 */
374 return 128;
375
376 case PIPE_CAP_MAX_TEXTURE_UPLOAD_MEMORY_BUDGET:
377 return 64 * 1024 * 1024;
378
379 case PIPE_CAP_SHAREABLE_SHADERS:
380 case PIPE_CAP_GLSL_OPTIMIZE_CONSERVATIVELY:
381 /* manage the variants for these ourself, to avoid breaking precompile: */
382 case PIPE_CAP_FRAGMENT_COLOR_CLAMPED:
383 case PIPE_CAP_VERTEX_COLOR_CLAMPED:
384 if (is_ir3(screen))
385 return 1;
386 return 0;
387
388 /* Geometry shaders.. */
389 case PIPE_CAP_MAX_GEOMETRY_OUTPUT_VERTICES:
390 return 512;
391 case PIPE_CAP_MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS:
392 return 2048;
393 case PIPE_CAP_MAX_GS_INVOCATIONS:
394 return 32;
395
396 /* Stream output. */
397 case PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS:
398 if (is_ir3(screen))
399 return PIPE_MAX_SO_BUFFERS;
400 return 0;
401 case PIPE_CAP_STREAM_OUTPUT_PAUSE_RESUME:
402 case PIPE_CAP_STREAM_OUTPUT_INTERLEAVE_BUFFERS:
403 case PIPE_CAP_TGSI_FS_POSITION_IS_SYSVAL:
404 case PIPE_CAP_TGSI_TEXCOORD:
405 if (is_ir3(screen))
406 return 1;
407 return 0;
408 case PIPE_CAP_TGSI_FS_FACE_IS_INTEGER_SYSVAL:
409 return 1;
410 case PIPE_CAP_TGSI_FS_POINT_IS_SYSVAL:
411 return is_a2xx(screen);
412 case PIPE_CAP_MAX_STREAM_OUTPUT_SEPARATE_COMPONENTS:
413 case PIPE_CAP_MAX_STREAM_OUTPUT_INTERLEAVED_COMPONENTS:
414 if (is_ir3(screen))
415 return 16 * 4; /* should only be shader out limit? */
416 return 0;
417
418 /* Texturing. */
419 case PIPE_CAP_MAX_TEXTURE_2D_SIZE:
420 if (is_a6xx(screen) || is_a5xx(screen) || is_a4xx(screen))
421 return 16384;
422 else
423 return 8192;
424 case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
425 if (is_a6xx(screen) || is_a5xx(screen) || is_a4xx(screen))
426 return 15;
427 else
428 return 14;
429 case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
430 return 11;
431
432 case PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS:
433 return (is_a3xx(screen) || is_a4xx(screen) || is_a5xx(screen) || is_a6xx(screen)) ? 256 : 0;
434
435 /* Render targets. */
436 case PIPE_CAP_MAX_RENDER_TARGETS:
437 return screen->max_rts;
438 case PIPE_CAP_MAX_DUAL_SOURCE_RENDER_TARGETS:
439 return is_a3xx(screen) ? 1 : 0;
440
441 /* Queries. */
442 case PIPE_CAP_OCCLUSION_QUERY:
443 return is_a3xx(screen) || is_a4xx(screen) || is_a5xx(screen) || is_a6xx(screen);
444 case PIPE_CAP_QUERY_TIMESTAMP:
445 case PIPE_CAP_QUERY_TIME_ELAPSED:
446 /* only a4xx, requires new enough kernel so we know max_freq: */
447 return (screen->max_freq > 0) && (is_a4xx(screen) || is_a5xx(screen) || is_a6xx(screen));
448
449 case PIPE_CAP_VENDOR_ID:
450 return 0x5143;
451 case PIPE_CAP_DEVICE_ID:
452 return 0xFFFFFFFF;
453 case PIPE_CAP_ACCELERATED:
454 return 1;
455 case PIPE_CAP_VIDEO_MEMORY:
456 DBG("FINISHME: The value returned is incorrect\n");
457 return 10;
458 case PIPE_CAP_UMA:
459 return 1;
460 case PIPE_CAP_MEMOBJ:
461 return fd_device_version(screen->dev) >= FD_VERSION_MEMORY_FD;
462 case PIPE_CAP_NATIVE_FENCE_FD:
463 return fd_device_version(screen->dev) >= FD_VERSION_FENCE_FD;
464 case PIPE_CAP_FENCE_SIGNAL:
465 return screen->has_syncobj;
466 case PIPE_CAP_CULL_DISTANCE:
467 return is_a6xx(screen);
468 default:
469 return u_pipe_screen_get_param_defaults(pscreen, param);
470 }
471 }
472
473 static float
fd_screen_get_paramf(struct pipe_screen * pscreen,enum pipe_capf param)474 fd_screen_get_paramf(struct pipe_screen *pscreen, enum pipe_capf param)
475 {
476 switch (param) {
477 case PIPE_CAPF_MAX_LINE_WIDTH:
478 case PIPE_CAPF_MAX_LINE_WIDTH_AA:
479 /* NOTE: actual value is 127.0f, but this is working around a deqp
480 * bug.. dEQP-GLES3.functional.rasterization.primitives.lines_wide
481 * uses too small of a render target size, and gets confused when
482 * the lines start going offscreen.
483 *
484 * See: https://code.google.com/p/android/issues/detail?id=206513
485 */
486 if (fd_mesa_debug & FD_DBG_DEQP)
487 return 48.0f;
488 return 127.0f;
489 case PIPE_CAPF_MAX_POINT_WIDTH:
490 case PIPE_CAPF_MAX_POINT_WIDTH_AA:
491 return 4092.0f;
492 case PIPE_CAPF_MAX_TEXTURE_ANISOTROPY:
493 return 16.0f;
494 case PIPE_CAPF_MAX_TEXTURE_LOD_BIAS:
495 return 15.0f;
496 case PIPE_CAPF_MIN_CONSERVATIVE_RASTER_DILATE:
497 case PIPE_CAPF_MAX_CONSERVATIVE_RASTER_DILATE:
498 case PIPE_CAPF_CONSERVATIVE_RASTER_DILATE_GRANULARITY:
499 return 0.0f;
500 }
501 debug_printf("unknown paramf %d\n", param);
502 return 0;
503 }
504
505 static int
fd_screen_get_shader_param(struct pipe_screen * pscreen,enum pipe_shader_type shader,enum pipe_shader_cap param)506 fd_screen_get_shader_param(struct pipe_screen *pscreen,
507 enum pipe_shader_type shader,
508 enum pipe_shader_cap param)
509 {
510 struct fd_screen *screen = fd_screen(pscreen);
511
512 switch(shader)
513 {
514 case PIPE_SHADER_FRAGMENT:
515 case PIPE_SHADER_VERTEX:
516 break;
517 case PIPE_SHADER_TESS_CTRL:
518 case PIPE_SHADER_TESS_EVAL:
519 case PIPE_SHADER_GEOMETRY:
520 if (is_a6xx(screen))
521 break;
522 return 0;
523 case PIPE_SHADER_COMPUTE:
524 if (has_compute(screen))
525 break;
526 return 0;
527 default:
528 DBG("unknown shader type %d", shader);
529 return 0;
530 }
531
532 /* this is probably not totally correct.. but it's a start: */
533 switch (param) {
534 case PIPE_SHADER_CAP_MAX_INSTRUCTIONS:
535 case PIPE_SHADER_CAP_MAX_ALU_INSTRUCTIONS:
536 case PIPE_SHADER_CAP_MAX_TEX_INSTRUCTIONS:
537 case PIPE_SHADER_CAP_MAX_TEX_INDIRECTIONS:
538 return 16384;
539 case PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH:
540 return 8; /* XXX */
541 case PIPE_SHADER_CAP_MAX_INPUTS:
542 case PIPE_SHADER_CAP_MAX_OUTPUTS:
543 return 16;
544 case PIPE_SHADER_CAP_MAX_TEMPS:
545 return 64; /* Max native temporaries. */
546 case PIPE_SHADER_CAP_MAX_CONST_BUFFER_SIZE:
547 /* NOTE: seems to be limit for a3xx is actually 512 but
548 * split between VS and FS. Use lower limit of 256 to
549 * avoid getting into impossible situations:
550 */
551 return ((is_a3xx(screen) || is_a4xx(screen) || is_a5xx(screen) || is_a6xx(screen)) ? 4096 : 64) * sizeof(float[4]);
552 case PIPE_SHADER_CAP_MAX_CONST_BUFFERS:
553 return is_ir3(screen) ? 16 : 1;
554 case PIPE_SHADER_CAP_TGSI_CONT_SUPPORTED:
555 return 1;
556 case PIPE_SHADER_CAP_INDIRECT_INPUT_ADDR:
557 case PIPE_SHADER_CAP_INDIRECT_OUTPUT_ADDR:
558 /* Technically this should be the same as for TEMP/CONST, since
559 * everything is just normal registers. This is just temporary
560 * hack until load_input/store_output handle arrays in a similar
561 * way as load_var/store_var..
562 *
563 * For tessellation stages, inputs are loaded using ldlw or ldg, both
564 * of which support indirection.
565 */
566 return shader == PIPE_SHADER_TESS_CTRL || shader == PIPE_SHADER_TESS_EVAL;
567 case PIPE_SHADER_CAP_INDIRECT_TEMP_ADDR:
568 case PIPE_SHADER_CAP_INDIRECT_CONST_ADDR:
569 /* a2xx compiler doesn't handle indirect: */
570 return is_ir3(screen) ? 1 : 0;
571 case PIPE_SHADER_CAP_SUBROUTINES:
572 case PIPE_SHADER_CAP_TGSI_DROUND_SUPPORTED:
573 case PIPE_SHADER_CAP_TGSI_DFRACEXP_DLDEXP_SUPPORTED:
574 case PIPE_SHADER_CAP_TGSI_LDEXP_SUPPORTED:
575 case PIPE_SHADER_CAP_TGSI_FMA_SUPPORTED:
576 case PIPE_SHADER_CAP_TGSI_ANY_INOUT_DECL_RANGE:
577 case PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTERS:
578 case PIPE_SHADER_CAP_LOWER_IF_THRESHOLD:
579 case PIPE_SHADER_CAP_TGSI_SKIP_MERGE_REGISTERS:
580 case PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTER_BUFFERS:
581 return 0;
582 case PIPE_SHADER_CAP_TGSI_SQRT_SUPPORTED:
583 return 1;
584 case PIPE_SHADER_CAP_INTEGERS:
585 return is_ir3(screen) ? 1 : 0;
586 case PIPE_SHADER_CAP_INT64_ATOMICS:
587 case PIPE_SHADER_CAP_FP16_DERIVATIVES:
588 case PIPE_SHADER_CAP_INT16:
589 case PIPE_SHADER_CAP_GLSL_16BIT_CONSTS:
590 return 0;
591 case PIPE_SHADER_CAP_FP16:
592 return ((is_a5xx(screen) || is_a6xx(screen)) &&
593 (shader == PIPE_SHADER_COMPUTE ||
594 shader == PIPE_SHADER_FRAGMENT) &&
595 !(fd_mesa_debug & FD_DBG_NOFP16));
596 case PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS:
597 case PIPE_SHADER_CAP_MAX_SAMPLER_VIEWS:
598 return 16;
599 case PIPE_SHADER_CAP_PREFERRED_IR:
600 return PIPE_SHADER_IR_NIR;
601 case PIPE_SHADER_CAP_SUPPORTED_IRS:
602 return (1 << PIPE_SHADER_IR_NIR) | (1 << PIPE_SHADER_IR_TGSI);
603 case PIPE_SHADER_CAP_MAX_UNROLL_ITERATIONS_HINT:
604 return 32;
605 case PIPE_SHADER_CAP_MAX_SHADER_BUFFERS:
606 case PIPE_SHADER_CAP_MAX_SHADER_IMAGES:
607 if (is_a5xx(screen) || is_a6xx(screen)) {
608 /* a5xx (and a4xx for that matter) has one state-block
609 * for compute-shader SSBO's and another that is shared
610 * by VS/HS/DS/GS/FS.. so to simplify things for now
611 * just advertise SSBOs for FS and CS. We could possibly
612 * do what blob does, and partition the space for
613 * VS/HS/DS/GS/FS. The blob advertises:
614 *
615 * GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS: 4
616 * GL_MAX_GEOMETRY_SHADER_STORAGE_BLOCKS: 4
617 * GL_MAX_TESS_CONTROL_SHADER_STORAGE_BLOCKS: 4
618 * GL_MAX_TESS_EVALUATION_SHADER_STORAGE_BLOCKS: 4
619 * GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS: 4
620 * GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS: 24
621 * GL_MAX_COMBINED_SHADER_STORAGE_BLOCKS: 24
622 *
623 * I think that way we could avoid having to patch shaders
624 * for actual SSBO indexes by using a static partitioning.
625 *
626 * Note same state block is used for images and buffers,
627 * but images also need texture state for read access
628 * (isam/isam.3d)
629 */
630 switch(shader)
631 {
632 case PIPE_SHADER_FRAGMENT:
633 case PIPE_SHADER_COMPUTE:
634 return 24;
635 default:
636 return 0;
637 }
638 }
639 return 0;
640 }
641 debug_printf("unknown shader param %d\n", param);
642 return 0;
643 }
644
645 /* TODO depending on how much the limits differ for a3xx/a4xx, maybe move this
646 * into per-generation backend?
647 */
648 static int
fd_get_compute_param(struct pipe_screen * pscreen,enum pipe_shader_ir ir_type,enum pipe_compute_cap param,void * ret)649 fd_get_compute_param(struct pipe_screen *pscreen, enum pipe_shader_ir ir_type,
650 enum pipe_compute_cap param, void *ret)
651 {
652 struct fd_screen *screen = fd_screen(pscreen);
653 const char * const ir = "ir3";
654
655 if (!has_compute(screen))
656 return 0;
657
658 #define RET(x) do { \
659 if (ret) \
660 memcpy(ret, x, sizeof(x)); \
661 return sizeof(x); \
662 } while (0)
663
664 switch (param) {
665 case PIPE_COMPUTE_CAP_ADDRESS_BITS:
666 // don't expose 64b pointer support yet, until ir3 supports 64b
667 // math, otherwise spir64 target is used and we get 64b pointer
668 // calculations that we can't do yet
669 // if (is_a5xx(screen))
670 // RET((uint32_t []){ 64 });
671 RET((uint32_t []){ 32 });
672
673 case PIPE_COMPUTE_CAP_IR_TARGET:
674 if (ret)
675 sprintf(ret, "%s", ir);
676 return strlen(ir) * sizeof(char);
677
678 case PIPE_COMPUTE_CAP_GRID_DIMENSION:
679 RET((uint64_t []) { 3 });
680
681 case PIPE_COMPUTE_CAP_MAX_GRID_SIZE:
682 RET(((uint64_t []) { 65535, 65535, 65535 }));
683
684 case PIPE_COMPUTE_CAP_MAX_BLOCK_SIZE:
685 RET(((uint64_t []) { 1024, 1024, 64 }));
686
687 case PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK:
688 RET((uint64_t []) { 1024 });
689
690 case PIPE_COMPUTE_CAP_MAX_GLOBAL_SIZE:
691 RET((uint64_t []) { screen->ram_size });
692
693 case PIPE_COMPUTE_CAP_MAX_LOCAL_SIZE:
694 RET((uint64_t []) { 32768 });
695
696 case PIPE_COMPUTE_CAP_MAX_PRIVATE_SIZE:
697 case PIPE_COMPUTE_CAP_MAX_INPUT_SIZE:
698 RET((uint64_t []) { 4096 });
699
700 case PIPE_COMPUTE_CAP_MAX_MEM_ALLOC_SIZE:
701 RET((uint64_t []) { screen->ram_size });
702
703 case PIPE_COMPUTE_CAP_MAX_CLOCK_FREQUENCY:
704 RET((uint32_t []) { screen->max_freq / 1000000 });
705
706 case PIPE_COMPUTE_CAP_MAX_COMPUTE_UNITS:
707 RET((uint32_t []) { 9999 }); // TODO
708
709 case PIPE_COMPUTE_CAP_IMAGES_SUPPORTED:
710 RET((uint32_t []) { 1 });
711
712 case PIPE_COMPUTE_CAP_SUBGROUP_SIZE:
713 RET((uint32_t []) { 32 }); // TODO
714
715 case PIPE_COMPUTE_CAP_MAX_VARIABLE_THREADS_PER_BLOCK:
716 RET((uint64_t []) { 1024 }); // TODO
717 }
718
719 return 0;
720 }
721
722 static const void *
fd_get_compiler_options(struct pipe_screen * pscreen,enum pipe_shader_ir ir,unsigned shader)723 fd_get_compiler_options(struct pipe_screen *pscreen,
724 enum pipe_shader_ir ir, unsigned shader)
725 {
726 struct fd_screen *screen = fd_screen(pscreen);
727
728 if (is_ir3(screen))
729 return ir3_get_compiler_options(screen->compiler);
730
731 return ir2_get_compiler_options();
732 }
733
734 static struct disk_cache *
fd_get_disk_shader_cache(struct pipe_screen * pscreen)735 fd_get_disk_shader_cache(struct pipe_screen *pscreen)
736 {
737 struct fd_screen *screen = fd_screen(pscreen);
738
739 if (is_ir3(screen)) {
740 struct ir3_compiler *compiler = screen->compiler;
741 return compiler->disk_cache;
742 }
743
744 return NULL;
745 }
746
747 bool
fd_screen_bo_get_handle(struct pipe_screen * pscreen,struct fd_bo * bo,struct renderonly_scanout * scanout,unsigned stride,struct winsys_handle * whandle)748 fd_screen_bo_get_handle(struct pipe_screen *pscreen,
749 struct fd_bo *bo,
750 struct renderonly_scanout *scanout,
751 unsigned stride,
752 struct winsys_handle *whandle)
753 {
754 whandle->stride = stride;
755
756 if (whandle->type == WINSYS_HANDLE_TYPE_SHARED) {
757 return fd_bo_get_name(bo, &whandle->handle) == 0;
758 } else if (whandle->type == WINSYS_HANDLE_TYPE_KMS) {
759 if (renderonly_get_handle(scanout, whandle))
760 return true;
761 whandle->handle = fd_bo_handle(bo);
762 return true;
763 } else if (whandle->type == WINSYS_HANDLE_TYPE_FD) {
764 whandle->handle = fd_bo_dmabuf(bo);
765 return true;
766 } else {
767 return false;
768 }
769 }
770
771 static void
fd_screen_query_dmabuf_modifiers(struct pipe_screen * pscreen,enum pipe_format format,int max,uint64_t * modifiers,unsigned int * external_only,int * count)772 fd_screen_query_dmabuf_modifiers(struct pipe_screen *pscreen,
773 enum pipe_format format,
774 int max, uint64_t *modifiers,
775 unsigned int *external_only,
776 int *count)
777 {
778 struct fd_screen *screen = fd_screen(pscreen);
779 int i, num = 0;
780
781 max = MIN2(max, screen->num_supported_modifiers);
782
783 if (!max) {
784 max = screen->num_supported_modifiers;
785 external_only = NULL;
786 modifiers = NULL;
787 }
788
789 for (i = 0; i < max; i++) {
790 if (modifiers)
791 modifiers[num] = screen->supported_modifiers[i];
792
793 if (external_only)
794 external_only[num] = 0;
795
796 num++;
797 }
798
799 *count = num;
800 }
801
802 struct fd_bo *
fd_screen_bo_from_handle(struct pipe_screen * pscreen,struct winsys_handle * whandle)803 fd_screen_bo_from_handle(struct pipe_screen *pscreen,
804 struct winsys_handle *whandle)
805 {
806 struct fd_screen *screen = fd_screen(pscreen);
807 struct fd_bo *bo;
808
809 if (whandle->type == WINSYS_HANDLE_TYPE_SHARED) {
810 bo = fd_bo_from_name(screen->dev, whandle->handle);
811 } else if (whandle->type == WINSYS_HANDLE_TYPE_KMS) {
812 bo = fd_bo_from_handle(screen->dev, whandle->handle, 0);
813 } else if (whandle->type == WINSYS_HANDLE_TYPE_FD) {
814 bo = fd_bo_from_dmabuf(screen->dev, whandle->handle);
815 } else {
816 DBG("Attempt to import unsupported handle type %d", whandle->type);
817 return NULL;
818 }
819
820 if (!bo) {
821 DBG("ref name 0x%08x failed", whandle->handle);
822 return NULL;
823 }
824
825 return bo;
826 }
827
_fd_fence_ref(struct pipe_screen * pscreen,struct pipe_fence_handle ** ptr,struct pipe_fence_handle * pfence)828 static void _fd_fence_ref(struct pipe_screen *pscreen,
829 struct pipe_fence_handle **ptr,
830 struct pipe_fence_handle *pfence)
831 {
832 fd_fence_ref(ptr, pfence);
833 }
834
835 static void
fd_screen_get_device_uuid(struct pipe_screen * pscreen,char * uuid)836 fd_screen_get_device_uuid(struct pipe_screen *pscreen, char *uuid)
837 {
838 struct fd_screen *screen = fd_screen(pscreen);
839
840 fd_get_device_uuid(uuid, screen->gpu_id);
841 }
842
843 static void
fd_screen_get_driver_uuid(struct pipe_screen * pscreen,char * uuid)844 fd_screen_get_driver_uuid(struct pipe_screen *pscreen, char *uuid)
845 {
846 fd_get_driver_uuid(uuid);
847 }
848
849 struct pipe_screen *
fd_screen_create(struct fd_device * dev,struct renderonly * ro)850 fd_screen_create(struct fd_device *dev, struct renderonly *ro)
851 {
852 struct fd_screen *screen = CALLOC_STRUCT(fd_screen);
853 struct pipe_screen *pscreen;
854 uint64_t val;
855
856 fd_mesa_debug = debug_get_option_fd_mesa_debug();
857
858 if (fd_mesa_debug & FD_DBG_NOBIN)
859 fd_binning_enabled = false;
860
861 if (!screen)
862 return NULL;
863
864 pscreen = &screen->base;
865
866 screen->dev = dev;
867 screen->refcnt = 1;
868
869 if (ro) {
870 screen->ro = renderonly_dup(ro);
871 if (!screen->ro) {
872 DBG("could not create renderonly object");
873 goto fail;
874 }
875 }
876
877 // maybe this should be in context?
878 screen->pipe = fd_pipe_new(screen->dev, FD_PIPE_3D);
879 if (!screen->pipe) {
880 DBG("could not create 3d pipe");
881 goto fail;
882 }
883
884 if (fd_pipe_get_param(screen->pipe, FD_GMEM_SIZE, &val)) {
885 DBG("could not get GMEM size");
886 goto fail;
887 }
888 screen->gmemsize_bytes = env_var_as_unsigned("FD_MESA_GMEM", val);
889
890 if (fd_device_version(dev) >= FD_VERSION_GMEM_BASE) {
891 fd_pipe_get_param(screen->pipe, FD_GMEM_BASE, &screen->gmem_base);
892 }
893
894 if (fd_pipe_get_param(screen->pipe, FD_DEVICE_ID, &val)) {
895 DBG("could not get device-id");
896 goto fail;
897 }
898 screen->device_id = val;
899
900 if (fd_pipe_get_param(screen->pipe, FD_MAX_FREQ, &val)) {
901 DBG("could not get gpu freq");
902 /* this limits what performance related queries are
903 * supported but is not fatal
904 */
905 screen->max_freq = 0;
906 } else {
907 screen->max_freq = val;
908 if (fd_pipe_get_param(screen->pipe, FD_TIMESTAMP, &val) == 0)
909 screen->has_timestamp = true;
910 }
911
912 if (fd_pipe_get_param(screen->pipe, FD_GPU_ID, &val)) {
913 DBG("could not get gpu-id");
914 goto fail;
915 }
916 screen->gpu_id = val;
917
918 if (fd_pipe_get_param(screen->pipe, FD_CHIP_ID, &val)) {
919 DBG("could not get chip-id");
920 /* older kernels may not have this property: */
921 unsigned core = screen->gpu_id / 100;
922 unsigned major = (screen->gpu_id % 100) / 10;
923 unsigned minor = screen->gpu_id % 10;
924 unsigned patch = 0; /* assume the worst */
925 val = (patch & 0xff) | ((minor & 0xff) << 8) |
926 ((major & 0xff) << 16) | ((core & 0xff) << 24);
927 }
928 screen->chip_id = val;
929
930 if (fd_pipe_get_param(screen->pipe, FD_NR_RINGS, &val)) {
931 DBG("could not get # of rings");
932 screen->priority_mask = 0;
933 } else {
934 /* # of rings equates to number of unique priority values: */
935 screen->priority_mask = (1 << val) - 1;
936 }
937
938 if (fd_device_version(dev) >= FD_VERSION_ROBUSTNESS)
939 screen->has_robustness = true;
940
941 screen->has_syncobj = fd_has_syncobj(screen->dev);
942
943 struct sysinfo si;
944 sysinfo(&si);
945 screen->ram_size = si.totalram;
946
947 DBG("Pipe Info:");
948 DBG(" GPU-id: %d", screen->gpu_id);
949 DBG(" Chip-id: 0x%08x", screen->chip_id);
950 DBG(" GMEM size: 0x%08x", screen->gmemsize_bytes);
951
952 /* explicitly checking for GPU revisions that are known to work. This
953 * may be overly conservative for a3xx, where spoofing the gpu_id with
954 * the blob driver seems to generate identical cmdstream dumps. But
955 * on a2xx, there seem to be small differences between the GPU revs
956 * so it is probably better to actually test first on real hardware
957 * before enabling:
958 *
959 * If you have a different adreno version, feel free to add it to one
960 * of the cases below and see what happens. And if it works, please
961 * send a patch ;-)
962 */
963 switch (screen->gpu_id) {
964 case 200:
965 case 201:
966 case 205:
967 case 220:
968 fd2_screen_init(pscreen);
969 break;
970 case 305:
971 case 307:
972 case 320:
973 case 330:
974 fd3_screen_init(pscreen);
975 break;
976 case 405:
977 case 420:
978 case 430:
979 fd4_screen_init(pscreen);
980 break;
981 case 510:
982 case 530:
983 case 540:
984 fd5_screen_init(pscreen);
985 break;
986 case 618:
987 case 630:
988 case 640:
989 case 650:
990 fd6_screen_init(pscreen);
991 break;
992 default:
993 debug_printf("unsupported GPU: a%03d\n", screen->gpu_id);
994 goto fail;
995 }
996
997 freedreno_dev_info_init(&screen->info, screen->gpu_id);
998
999 if (fd_mesa_debug & FD_DBG_PERFC) {
1000 screen->perfcntr_groups = fd_perfcntrs(screen->gpu_id,
1001 &screen->num_perfcntr_groups);
1002 }
1003
1004 /* NOTE: don't enable if we have too old of a kernel to support
1005 * growable cmdstream buffers, since memory requirement for cmdstream
1006 * buffers would be too much otherwise.
1007 */
1008 if (fd_device_version(dev) >= FD_VERSION_UNLIMITED_CMDS)
1009 screen->reorder = !(fd_mesa_debug & FD_DBG_INORDER);
1010
1011 if (BATCH_DEBUG)
1012 screen->live_batches = _mesa_pointer_set_create(NULL);
1013
1014 fd_bc_init(&screen->batch_cache);
1015
1016 list_inithead(&screen->context_list);
1017
1018 (void) simple_mtx_init(&screen->lock, mtx_plain);
1019
1020 pscreen->destroy = fd_screen_destroy;
1021 pscreen->get_param = fd_screen_get_param;
1022 pscreen->get_paramf = fd_screen_get_paramf;
1023 pscreen->get_shader_param = fd_screen_get_shader_param;
1024 pscreen->get_compute_param = fd_get_compute_param;
1025 pscreen->get_compiler_options = fd_get_compiler_options;
1026 pscreen->get_disk_shader_cache = fd_get_disk_shader_cache;
1027
1028 fd_resource_screen_init(pscreen);
1029 fd_query_screen_init(pscreen);
1030 fd_gmem_screen_init(pscreen);
1031
1032 pscreen->get_name = fd_screen_get_name;
1033 pscreen->get_vendor = fd_screen_get_vendor;
1034 pscreen->get_device_vendor = fd_screen_get_device_vendor;
1035
1036 pscreen->get_timestamp = fd_screen_get_timestamp;
1037
1038 pscreen->fence_reference = _fd_fence_ref;
1039 pscreen->fence_finish = fd_fence_finish;
1040 pscreen->fence_get_fd = fd_fence_get_fd;
1041
1042 pscreen->query_dmabuf_modifiers = fd_screen_query_dmabuf_modifiers;
1043
1044 pscreen->get_device_uuid = fd_screen_get_device_uuid;
1045 pscreen->get_driver_uuid = fd_screen_get_driver_uuid;
1046
1047 slab_create_parent(&screen->transfer_pool, sizeof(struct fd_transfer), 16);
1048
1049 return pscreen;
1050
1051 fail:
1052 fd_screen_destroy(pscreen);
1053 return NULL;
1054 }
1055