broadcom/vc4: Native fence fd support
[mesa.git] / src / gallium / drivers / vc4 / vc4_screen.c
1 /*
2 * Copyright © 2014 Broadcom
3 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #include "os/os_misc.h"
26 #include "pipe/p_defines.h"
27 #include "pipe/p_screen.h"
28 #include "pipe/p_state.h"
29
30 #include "util/u_cpu_detect.h"
31 #include "util/u_debug.h"
32 #include "util/u_memory.h"
33 #include "util/u_format.h"
34 #include "util/u_hash_table.h"
35 #include "util/ralloc.h"
36
37 #include <xf86drm.h>
38 #include "drm_fourcc.h"
39 #include "vc4_drm.h"
40 #include "vc4_screen.h"
41 #include "vc4_context.h"
42 #include "vc4_resource.h"
43
44 static const struct debug_named_value debug_options[] = {
45 { "cl", VC4_DEBUG_CL,
46 "Dump command list during creation" },
47 { "surf", VC4_DEBUG_SURFACE,
48 "Dump surface layouts" },
49 { "qpu", VC4_DEBUG_QPU,
50 "Dump generated QPU instructions" },
51 { "qir", VC4_DEBUG_QIR,
52 "Dump QPU IR during program compile" },
53 { "nir", VC4_DEBUG_NIR,
54 "Dump NIR during program compile" },
55 { "tgsi", VC4_DEBUG_TGSI,
56 "Dump TGSI during program compile" },
57 { "shaderdb", VC4_DEBUG_SHADERDB,
58 "Dump program compile information for shader-db analysis" },
59 { "perf", VC4_DEBUG_PERF,
60 "Print during performance-related events" },
61 { "norast", VC4_DEBUG_NORAST,
62 "Skip actual hardware execution of commands" },
63 { "always_flush", VC4_DEBUG_ALWAYS_FLUSH,
64 "Flush after each draw call" },
65 { "always_sync", VC4_DEBUG_ALWAYS_SYNC,
66 "Wait for finish after each flush" },
67 #ifdef USE_VC4_SIMULATOR
68 { "dump", VC4_DEBUG_DUMP,
69 "Write a GPU command stream trace file" },
70 #endif
71 { NULL }
72 };
73
74 DEBUG_GET_ONCE_FLAGS_OPTION(vc4_debug, "VC4_DEBUG", debug_options, 0)
75 uint32_t vc4_debug;
76
77 static const char *
78 vc4_screen_get_name(struct pipe_screen *pscreen)
79 {
80 struct vc4_screen *screen = vc4_screen(pscreen);
81
82 if (!screen->name) {
83 screen->name = ralloc_asprintf(screen,
84 "VC4 V3D %d.%d",
85 screen->v3d_ver / 10,
86 screen->v3d_ver % 10);
87 }
88
89 return screen->name;
90 }
91
92 static const char *
93 vc4_screen_get_vendor(struct pipe_screen *pscreen)
94 {
95 return "Broadcom";
96 }
97
98 static void
99 vc4_screen_destroy(struct pipe_screen *pscreen)
100 {
101 struct vc4_screen *screen = vc4_screen(pscreen);
102
103 util_hash_table_destroy(screen->bo_handles);
104 vc4_bufmgr_destroy(pscreen);
105 slab_destroy_parent(&screen->transfer_pool);
106 free(screen->ro);
107
108 #ifdef USE_VC4_SIMULATOR
109 vc4_simulator_destroy(screen);
110 #endif
111
112 close(screen->fd);
113 ralloc_free(pscreen);
114 }
115
116 static bool
117 vc4_has_feature(struct vc4_screen *screen, uint32_t feature)
118 {
119 struct drm_vc4_get_param p = {
120 .param = feature,
121 };
122 int ret = vc4_ioctl(screen->fd, DRM_IOCTL_VC4_GET_PARAM, &p);
123
124 if (ret != 0)
125 return false;
126
127 return p.value;
128 }
129
130 static int
131 vc4_screen_get_param(struct pipe_screen *pscreen, enum pipe_cap param)
132 {
133 struct vc4_screen *screen = vc4_screen(pscreen);
134
135 switch (param) {
136 /* Supported features (boolean caps). */
137 case PIPE_CAP_VERTEX_COLOR_CLAMPED:
138 case PIPE_CAP_VERTEX_COLOR_UNCLAMPED:
139 case PIPE_CAP_FRAGMENT_COLOR_CLAMPED:
140 case PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT:
141 case PIPE_CAP_NPOT_TEXTURES:
142 case PIPE_CAP_SHAREABLE_SHADERS:
143 case PIPE_CAP_BLEND_EQUATION_SEPARATE:
144 case PIPE_CAP_TEXTURE_MULTISAMPLE:
145 case PIPE_CAP_TEXTURE_SWIZZLE:
146 case PIPE_CAP_GLSL_OPTIMIZE_CONSERVATIVELY:
147 case PIPE_CAP_ALLOW_MAPPED_BUFFERS_DURING_EXECUTION:
148 case PIPE_CAP_TEXTURE_BARRIER:
149 return 1;
150
151 case PIPE_CAP_NATIVE_FENCE_FD:
152 return screen->has_syncobj;
153
154 case PIPE_CAP_TILE_RASTER_ORDER:
155 return vc4_has_feature(screen,
156 DRM_VC4_PARAM_SUPPORTS_FIXED_RCL_ORDER);
157
158 /* lying for GL 2.0 */
159 case PIPE_CAP_OCCLUSION_QUERY:
160 case PIPE_CAP_POINT_SPRITE:
161 return 1;
162
163 case PIPE_CAP_CONSTANT_BUFFER_OFFSET_ALIGNMENT:
164 return 256;
165
166 case PIPE_CAP_GLSL_FEATURE_LEVEL:
167 return 120;
168
169 case PIPE_CAP_MAX_VIEWPORTS:
170 return 1;
171
172 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT:
173 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER:
174 return 1;
175
176 case PIPE_CAP_MIXED_FRAMEBUFFER_SIZES:
177 case PIPE_CAP_MIXED_COLOR_DEPTH_BITS:
178 return 1;
179
180 /* Unsupported features. */
181 case PIPE_CAP_ANISOTROPIC_FILTER:
182 case PIPE_CAP_TEXTURE_BUFFER_OBJECTS:
183 case PIPE_CAP_BUFFER_SAMPLER_VIEW_RGBA_ONLY:
184 case PIPE_CAP_CUBE_MAP_ARRAY:
185 case PIPE_CAP_TEXTURE_MIRROR_CLAMP:
186 case PIPE_CAP_VERTEX_ELEMENT_INSTANCE_DIVISOR:
187 case PIPE_CAP_MIXED_COLORBUFFER_FORMATS:
188 case PIPE_CAP_SEAMLESS_CUBE_MAP:
189 case PIPE_CAP_QUADS_FOLLOW_PROVOKING_VERTEX_CONVENTION:
190 case PIPE_CAP_TGSI_INSTANCEID:
191 case PIPE_CAP_VERTEX_BUFFER_OFFSET_4BYTE_ALIGNED_ONLY:
192 case PIPE_CAP_VERTEX_BUFFER_STRIDE_4BYTE_ALIGNED_ONLY:
193 case PIPE_CAP_VERTEX_ELEMENT_SRC_OFFSET_4BYTE_ALIGNED_ONLY:
194 case PIPE_CAP_COMPUTE:
195 case PIPE_CAP_START_INSTANCE:
196 case PIPE_CAP_MAX_DUAL_SOURCE_RENDER_TARGETS:
197 case PIPE_CAP_SHADER_STENCIL_EXPORT:
198 case PIPE_CAP_TGSI_TEXCOORD:
199 case PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER:
200 case PIPE_CAP_CONDITIONAL_RENDER:
201 case PIPE_CAP_PRIMITIVE_RESTART:
202 case PIPE_CAP_SM3:
203 case PIPE_CAP_INDEP_BLEND_ENABLE:
204 case PIPE_CAP_INDEP_BLEND_FUNC:
205 case PIPE_CAP_DEPTH_CLIP_DISABLE:
206 case PIPE_CAP_SEAMLESS_CUBE_MAP_PER_TEXTURE:
207 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT:
208 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER:
209 case PIPE_CAP_TGSI_CAN_COMPACT_CONSTANTS:
210 case PIPE_CAP_USER_VERTEX_BUFFERS:
211 case PIPE_CAP_QUERY_PIPELINE_STATISTICS:
212 case PIPE_CAP_TEXTURE_BORDER_COLOR_QUIRK:
213 case PIPE_CAP_TGSI_VS_LAYER_VIEWPORT:
214 case PIPE_CAP_MAX_TEXTURE_GATHER_COMPONENTS:
215 case PIPE_CAP_TEXTURE_GATHER_SM5:
216 case PIPE_CAP_FAKE_SW_MSAA:
217 case PIPE_CAP_TEXTURE_QUERY_LOD:
218 case PIPE_CAP_SAMPLE_SHADING:
219 case PIPE_CAP_TEXTURE_GATHER_OFFSETS:
220 case PIPE_CAP_MAX_TEXTURE_GATHER_OFFSET:
221 case PIPE_CAP_TGSI_VS_WINDOW_SPACE_POSITION:
222 case PIPE_CAP_MAX_TEXEL_OFFSET:
223 case PIPE_CAP_MAX_VERTEX_STREAMS:
224 case PIPE_CAP_DRAW_INDIRECT:
225 case PIPE_CAP_MULTI_DRAW_INDIRECT:
226 case PIPE_CAP_MULTI_DRAW_INDIRECT_PARAMS:
227 case PIPE_CAP_TGSI_FS_FINE_DERIVATIVE:
228 case PIPE_CAP_CONDITIONAL_RENDER_INVERTED:
229 case PIPE_CAP_SAMPLER_VIEW_TARGET:
230 case PIPE_CAP_CLIP_HALFZ:
231 case PIPE_CAP_VERTEXID_NOBASE:
232 case PIPE_CAP_POLYGON_OFFSET_CLAMP:
233 case PIPE_CAP_MULTISAMPLE_Z_RESOLVE:
234 case PIPE_CAP_RESOURCE_FROM_USER_MEMORY:
235 case PIPE_CAP_DEVICE_RESET_STATUS_QUERY:
236 case PIPE_CAP_MAX_SHADER_PATCH_VARYINGS:
237 case PIPE_CAP_TEXTURE_FLOAT_LINEAR:
238 case PIPE_CAP_TEXTURE_HALF_FLOAT_LINEAR:
239 case PIPE_CAP_DEPTH_BOUNDS_TEST:
240 case PIPE_CAP_TGSI_TXQS:
241 case PIPE_CAP_FORCE_PERSAMPLE_INTERP:
242 case PIPE_CAP_COPY_BETWEEN_COMPRESSED_AND_PLAIN_FORMATS:
243 case PIPE_CAP_CLEAR_TEXTURE:
244 case PIPE_CAP_DRAW_PARAMETERS:
245 case PIPE_CAP_TGSI_PACK_HALF_FLOAT:
246 case PIPE_CAP_TGSI_FS_POSITION_IS_SYSVAL:
247 case PIPE_CAP_TGSI_FS_FACE_IS_INTEGER_SYSVAL:
248 case PIPE_CAP_SHADER_BUFFER_OFFSET_ALIGNMENT:
249 case PIPE_CAP_INVALIDATE_BUFFER:
250 case PIPE_CAP_GENERATE_MIPMAP:
251 case PIPE_CAP_STRING_MARKER:
252 case PIPE_CAP_SURFACE_REINTERPRET_BLOCKS:
253 case PIPE_CAP_QUERY_BUFFER_OBJECT:
254 case PIPE_CAP_QUERY_MEMORY_INFO:
255 case PIPE_CAP_PCI_GROUP:
256 case PIPE_CAP_PCI_BUS:
257 case PIPE_CAP_PCI_DEVICE:
258 case PIPE_CAP_PCI_FUNCTION:
259 case PIPE_CAP_FRAMEBUFFER_NO_ATTACHMENT:
260 case PIPE_CAP_ROBUST_BUFFER_ACCESS_BEHAVIOR:
261 case PIPE_CAP_CULL_DISTANCE:
262 case PIPE_CAP_PRIMITIVE_RESTART_FOR_PATCHES:
263 case PIPE_CAP_TGSI_VOTE:
264 case PIPE_CAP_MAX_WINDOW_RECTANGLES:
265 case PIPE_CAP_POLYGON_OFFSET_UNITS_UNSCALED:
266 case PIPE_CAP_VIEWPORT_SUBPIXEL_BITS:
267 case PIPE_CAP_TGSI_ARRAY_COMPONENTS:
268 case PIPE_CAP_TGSI_CAN_READ_OUTPUTS:
269 case PIPE_CAP_TGSI_FS_FBFETCH:
270 case PIPE_CAP_TGSI_MUL_ZERO_WINS:
271 case PIPE_CAP_DOUBLES:
272 case PIPE_CAP_INT64:
273 case PIPE_CAP_INT64_DIVMOD:
274 case PIPE_CAP_TGSI_TEX_TXF_LZ:
275 case PIPE_CAP_TGSI_CLOCK:
276 case PIPE_CAP_POLYGON_MODE_FILL_RECTANGLE:
277 case PIPE_CAP_SPARSE_BUFFER_PAGE_SIZE:
278 case PIPE_CAP_TGSI_BALLOT:
279 case PIPE_CAP_TGSI_TES_LAYER_VIEWPORT:
280 case PIPE_CAP_CAN_BIND_CONST_BUFFER_AS_VERTEX:
281 case PIPE_CAP_POST_DEPTH_COVERAGE:
282 case PIPE_CAP_BINDLESS_TEXTURE:
283 case PIPE_CAP_NIR_SAMPLERS_AS_DEREF:
284 case PIPE_CAP_QUERY_SO_OVERFLOW:
285 case PIPE_CAP_MEMOBJ:
286 case PIPE_CAP_LOAD_CONSTBUF:
287 case PIPE_CAP_TGSI_ANY_REG_AS_ADDRESS:
288 case PIPE_CAP_MAX_COMBINED_SHADER_OUTPUT_RESOURCES:
289 case PIPE_CAP_SIGNED_VERTEX_BUFFER_OFFSET:
290 case PIPE_CAP_CONTEXT_PRIORITY_MASK:
291 case PIPE_CAP_FENCE_SIGNAL:
292 case PIPE_CAP_CONSTBUF0_FLAGS:
293 case PIPE_CAP_CONSERVATIVE_RASTER_POST_SNAP_TRIANGLES:
294 case PIPE_CAP_CONSERVATIVE_RASTER_POST_SNAP_POINTS_LINES:
295 case PIPE_CAP_CONSERVATIVE_RASTER_PRE_SNAP_TRIANGLES:
296 case PIPE_CAP_CONSERVATIVE_RASTER_PRE_SNAP_POINTS_LINES:
297 case PIPE_CAP_CONSERVATIVE_RASTER_POST_DEPTH_COVERAGE:
298 case PIPE_CAP_MAX_CONSERVATIVE_RASTER_SUBPIXEL_PRECISION_BIAS:
299 case PIPE_CAP_PACKED_UNIFORMS:
300 return 0;
301
302 /* Stream output. */
303 case PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS:
304 case PIPE_CAP_STREAM_OUTPUT_PAUSE_RESUME:
305 case PIPE_CAP_STREAM_OUTPUT_INTERLEAVE_BUFFERS:
306 case PIPE_CAP_MAX_STREAM_OUTPUT_SEPARATE_COMPONENTS:
307 case PIPE_CAP_MAX_STREAM_OUTPUT_INTERLEAVED_COMPONENTS:
308 return 0;
309
310 /* Geometry shader output, unsupported. */
311 case PIPE_CAP_MAX_GEOMETRY_OUTPUT_VERTICES:
312 case PIPE_CAP_MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS:
313 return 0;
314
315 /* Texturing. */
316 case PIPE_CAP_MAX_TEXTURE_2D_LEVELS:
317 case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
318 return VC4_MAX_MIP_LEVELS;
319 case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
320 /* Note: Not supported in hardware, just faking it. */
321 return 5;
322 case PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS:
323 return 0;
324
325 /* Render targets. */
326 case PIPE_CAP_MAX_RENDER_TARGETS:
327 return 1;
328
329 /* Queries. */
330 case PIPE_CAP_QUERY_TIME_ELAPSED:
331 case PIPE_CAP_QUERY_TIMESTAMP:
332 return 0;
333
334 case PIPE_CAP_MIN_TEXTURE_GATHER_OFFSET:
335 case PIPE_CAP_MIN_TEXEL_OFFSET:
336 return 0;
337
338 case PIPE_CAP_MAX_VERTEX_ATTRIB_STRIDE:
339 return 2048;
340
341 case PIPE_CAP_ENDIANNESS:
342 return PIPE_ENDIAN_LITTLE;
343
344 case PIPE_CAP_MIN_MAP_BUFFER_ALIGNMENT:
345 return 64;
346
347 case PIPE_CAP_VENDOR_ID:
348 return 0x14E4;
349 case PIPE_CAP_DEVICE_ID:
350 return 0xFFFFFFFF;
351 case PIPE_CAP_ACCELERATED:
352 return 1;
353 case PIPE_CAP_VIDEO_MEMORY: {
354 uint64_t system_memory;
355
356 if (!os_get_total_physical_memory(&system_memory))
357 return 0;
358
359 return (int)(system_memory >> 20);
360 }
361 case PIPE_CAP_UMA:
362 return 1;
363
364 default:
365 fprintf(stderr, "unknown param %d\n", param);
366 return 0;
367 }
368 }
369
370 static float
371 vc4_screen_get_paramf(struct pipe_screen *pscreen, enum pipe_capf param)
372 {
373 switch (param) {
374 case PIPE_CAPF_MAX_LINE_WIDTH:
375 case PIPE_CAPF_MAX_LINE_WIDTH_AA:
376 return 32;
377
378 case PIPE_CAPF_MAX_POINT_WIDTH:
379 case PIPE_CAPF_MAX_POINT_WIDTH_AA:
380 return 512.0f;
381
382 case PIPE_CAPF_MAX_TEXTURE_ANISOTROPY:
383 return 0.0f;
384 case PIPE_CAPF_MAX_TEXTURE_LOD_BIAS:
385 return 0.0f;
386
387 case PIPE_CAPF_MIN_CONSERVATIVE_RASTER_DILATE:
388 case PIPE_CAPF_MAX_CONSERVATIVE_RASTER_DILATE:
389 case PIPE_CAPF_CONSERVATIVE_RASTER_DILATE_GRANULARITY:
390 return 0.0f;
391 default:
392 fprintf(stderr, "unknown paramf %d\n", param);
393 return 0;
394 }
395 }
396
397 static int
398 vc4_screen_get_shader_param(struct pipe_screen *pscreen,
399 enum pipe_shader_type shader,
400 enum pipe_shader_cap param)
401 {
402 if (shader != PIPE_SHADER_VERTEX &&
403 shader != PIPE_SHADER_FRAGMENT) {
404 return 0;
405 }
406
407 /* this is probably not totally correct.. but it's a start: */
408 switch (param) {
409 case PIPE_SHADER_CAP_MAX_INSTRUCTIONS:
410 case PIPE_SHADER_CAP_MAX_ALU_INSTRUCTIONS:
411 case PIPE_SHADER_CAP_MAX_TEX_INSTRUCTIONS:
412 case PIPE_SHADER_CAP_MAX_TEX_INDIRECTIONS:
413 return 16384;
414
415 case PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH:
416 return vc4_screen(pscreen)->has_control_flow;
417
418 case PIPE_SHADER_CAP_MAX_INPUTS:
419 return 8;
420 case PIPE_SHADER_CAP_MAX_OUTPUTS:
421 return shader == PIPE_SHADER_FRAGMENT ? 1 : 8;
422 case PIPE_SHADER_CAP_MAX_TEMPS:
423 return 256; /* GL_MAX_PROGRAM_TEMPORARIES_ARB */
424 case PIPE_SHADER_CAP_MAX_CONST_BUFFER_SIZE:
425 return 16 * 1024 * sizeof(float);
426 case PIPE_SHADER_CAP_MAX_CONST_BUFFERS:
427 return 1;
428 case PIPE_SHADER_CAP_TGSI_CONT_SUPPORTED:
429 return 0;
430 case PIPE_SHADER_CAP_INDIRECT_INPUT_ADDR:
431 case PIPE_SHADER_CAP_INDIRECT_OUTPUT_ADDR:
432 case PIPE_SHADER_CAP_INDIRECT_TEMP_ADDR:
433 return 0;
434 case PIPE_SHADER_CAP_INDIRECT_CONST_ADDR:
435 return 1;
436 case PIPE_SHADER_CAP_SUBROUTINES:
437 return 0;
438 case PIPE_SHADER_CAP_TGSI_SQRT_SUPPORTED:
439 return 0;
440 case PIPE_SHADER_CAP_INTEGERS:
441 return 1;
442 case PIPE_SHADER_CAP_INT64_ATOMICS:
443 case PIPE_SHADER_CAP_FP16:
444 case PIPE_SHADER_CAP_TGSI_DROUND_SUPPORTED:
445 case PIPE_SHADER_CAP_TGSI_DFRACEXP_DLDEXP_SUPPORTED:
446 case PIPE_SHADER_CAP_TGSI_LDEXP_SUPPORTED:
447 case PIPE_SHADER_CAP_TGSI_FMA_SUPPORTED:
448 case PIPE_SHADER_CAP_TGSI_ANY_INOUT_DECL_RANGE:
449 return 0;
450 case PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS:
451 case PIPE_SHADER_CAP_MAX_SAMPLER_VIEWS:
452 return VC4_MAX_TEXTURE_SAMPLERS;
453 case PIPE_SHADER_CAP_PREFERRED_IR:
454 return PIPE_SHADER_IR_NIR;
455 case PIPE_SHADER_CAP_SUPPORTED_IRS:
456 return 0;
457 case PIPE_SHADER_CAP_MAX_UNROLL_ITERATIONS_HINT:
458 return 32;
459 case PIPE_SHADER_CAP_MAX_SHADER_BUFFERS:
460 case PIPE_SHADER_CAP_MAX_SHADER_IMAGES:
461 case PIPE_SHADER_CAP_LOWER_IF_THRESHOLD:
462 case PIPE_SHADER_CAP_TGSI_SKIP_MERGE_REGISTERS:
463 case PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTERS:
464 case PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTER_BUFFERS:
465 return 0;
466 default:
467 fprintf(stderr, "unknown shader param %d\n", param);
468 return 0;
469 }
470 return 0;
471 }
472
473 static boolean
474 vc4_screen_is_format_supported(struct pipe_screen *pscreen,
475 enum pipe_format format,
476 enum pipe_texture_target target,
477 unsigned sample_count,
478 unsigned usage)
479 {
480 struct vc4_screen *screen = vc4_screen(pscreen);
481
482 if (sample_count > 1 && sample_count != VC4_MAX_SAMPLES)
483 return FALSE;
484
485 if ((target >= PIPE_MAX_TEXTURE_TYPES) ||
486 !util_format_is_supported(format, usage)) {
487 return FALSE;
488 }
489
490 if (usage & PIPE_BIND_VERTEX_BUFFER) {
491 switch (format) {
492 case PIPE_FORMAT_R32G32B32A32_FLOAT:
493 case PIPE_FORMAT_R32G32B32_FLOAT:
494 case PIPE_FORMAT_R32G32_FLOAT:
495 case PIPE_FORMAT_R32_FLOAT:
496 case PIPE_FORMAT_R32G32B32A32_SNORM:
497 case PIPE_FORMAT_R32G32B32_SNORM:
498 case PIPE_FORMAT_R32G32_SNORM:
499 case PIPE_FORMAT_R32_SNORM:
500 case PIPE_FORMAT_R32G32B32A32_SSCALED:
501 case PIPE_FORMAT_R32G32B32_SSCALED:
502 case PIPE_FORMAT_R32G32_SSCALED:
503 case PIPE_FORMAT_R32_SSCALED:
504 case PIPE_FORMAT_R16G16B16A16_UNORM:
505 case PIPE_FORMAT_R16G16B16_UNORM:
506 case PIPE_FORMAT_R16G16_UNORM:
507 case PIPE_FORMAT_R16_UNORM:
508 case PIPE_FORMAT_R16G16B16A16_SNORM:
509 case PIPE_FORMAT_R16G16B16_SNORM:
510 case PIPE_FORMAT_R16G16_SNORM:
511 case PIPE_FORMAT_R16_SNORM:
512 case PIPE_FORMAT_R16G16B16A16_USCALED:
513 case PIPE_FORMAT_R16G16B16_USCALED:
514 case PIPE_FORMAT_R16G16_USCALED:
515 case PIPE_FORMAT_R16_USCALED:
516 case PIPE_FORMAT_R16G16B16A16_SSCALED:
517 case PIPE_FORMAT_R16G16B16_SSCALED:
518 case PIPE_FORMAT_R16G16_SSCALED:
519 case PIPE_FORMAT_R16_SSCALED:
520 case PIPE_FORMAT_R8G8B8A8_UNORM:
521 case PIPE_FORMAT_R8G8B8_UNORM:
522 case PIPE_FORMAT_R8G8_UNORM:
523 case PIPE_FORMAT_R8_UNORM:
524 case PIPE_FORMAT_R8G8B8A8_SNORM:
525 case PIPE_FORMAT_R8G8B8_SNORM:
526 case PIPE_FORMAT_R8G8_SNORM:
527 case PIPE_FORMAT_R8_SNORM:
528 case PIPE_FORMAT_R8G8B8A8_USCALED:
529 case PIPE_FORMAT_R8G8B8_USCALED:
530 case PIPE_FORMAT_R8G8_USCALED:
531 case PIPE_FORMAT_R8_USCALED:
532 case PIPE_FORMAT_R8G8B8A8_SSCALED:
533 case PIPE_FORMAT_R8G8B8_SSCALED:
534 case PIPE_FORMAT_R8G8_SSCALED:
535 case PIPE_FORMAT_R8_SSCALED:
536 break;
537 default:
538 return FALSE;
539 }
540 }
541
542 if ((usage & PIPE_BIND_RENDER_TARGET) &&
543 !vc4_rt_format_supported(format)) {
544 return FALSE;
545 }
546
547 if ((usage & PIPE_BIND_SAMPLER_VIEW) &&
548 (!vc4_tex_format_supported(format) ||
549 (format == PIPE_FORMAT_ETC1_RGB8 && !screen->has_etc1))) {
550 return FALSE;
551 }
552
553 if ((usage & PIPE_BIND_DEPTH_STENCIL) &&
554 format != PIPE_FORMAT_S8_UINT_Z24_UNORM &&
555 format != PIPE_FORMAT_X8Z24_UNORM) {
556 return FALSE;
557 }
558
559 if ((usage & PIPE_BIND_INDEX_BUFFER) &&
560 format != PIPE_FORMAT_I8_UINT &&
561 format != PIPE_FORMAT_I16_UINT) {
562 return FALSE;
563 }
564
565 return TRUE;
566 }
567
568 static void
569 vc4_screen_query_dmabuf_modifiers(struct pipe_screen *pscreen,
570 enum pipe_format format, int max,
571 uint64_t *modifiers,
572 unsigned int *external_only,
573 int *count)
574 {
575 int m, i;
576 uint64_t available_modifiers[] = {
577 DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED,
578 DRM_FORMAT_MOD_LINEAR,
579 };
580 struct vc4_screen *screen = vc4_screen(pscreen);
581 int num_modifiers = screen->has_tiling_ioctl ? 2 : 1;
582
583 if (!modifiers) {
584 *count = num_modifiers;
585 return;
586 }
587
588 *count = MIN2(max, num_modifiers);
589 m = screen->has_tiling_ioctl ? 0 : 1;
590 /* We support both modifiers (tiled and linear) for all sampler
591 * formats, but if we don't have the DRM_VC4_GET_TILING ioctl
592 * we shouldn't advertise the tiled formats.
593 */
594 for (i = 0; i < *count; i++) {
595 modifiers[i] = available_modifiers[m++];
596 if (external_only)
597 external_only[i] = false;
598 }
599 }
600
601 #define PTR_TO_UINT(x) ((unsigned)((intptr_t)(x)))
602
603 static unsigned handle_hash(void *key)
604 {
605 return PTR_TO_UINT(key);
606 }
607
608 static int handle_compare(void *key1, void *key2)
609 {
610 return PTR_TO_UINT(key1) != PTR_TO_UINT(key2);
611 }
612
613 static bool
614 vc4_get_chip_info(struct vc4_screen *screen)
615 {
616 struct drm_vc4_get_param ident0 = {
617 .param = DRM_VC4_PARAM_V3D_IDENT0,
618 };
619 struct drm_vc4_get_param ident1 = {
620 .param = DRM_VC4_PARAM_V3D_IDENT1,
621 };
622 int ret;
623
624 ret = vc4_ioctl(screen->fd, DRM_IOCTL_VC4_GET_PARAM, &ident0);
625 if (ret != 0) {
626 if (errno == EINVAL) {
627 /* Backwards compatibility with 2835 kernels which
628 * only do V3D 2.1.
629 */
630 screen->v3d_ver = 21;
631 return true;
632 } else {
633 fprintf(stderr, "Couldn't get V3D IDENT0: %s\n",
634 strerror(errno));
635 return false;
636 }
637 }
638 ret = vc4_ioctl(screen->fd, DRM_IOCTL_VC4_GET_PARAM, &ident1);
639 if (ret != 0) {
640 fprintf(stderr, "Couldn't get V3D IDENT1: %s\n",
641 strerror(errno));
642 return false;
643 }
644
645 uint32_t major = (ident0.value >> 24) & 0xff;
646 uint32_t minor = (ident1.value >> 0) & 0xf;
647 screen->v3d_ver = major * 10 + minor;
648
649 if (screen->v3d_ver != 21 && screen->v3d_ver != 26) {
650 fprintf(stderr,
651 "V3D %d.%d not supported by this version of Mesa.\n",
652 screen->v3d_ver / 10,
653 screen->v3d_ver % 10);
654 return false;
655 }
656
657 return true;
658 }
659
660 struct pipe_screen *
661 vc4_screen_create(int fd, struct renderonly *ro)
662 {
663 struct vc4_screen *screen = rzalloc(NULL, struct vc4_screen);
664 uint64_t syncobj_cap = 0;
665 struct pipe_screen *pscreen;
666 int err;
667
668 pscreen = &screen->base;
669
670 pscreen->destroy = vc4_screen_destroy;
671 pscreen->get_param = vc4_screen_get_param;
672 pscreen->get_paramf = vc4_screen_get_paramf;
673 pscreen->get_shader_param = vc4_screen_get_shader_param;
674 pscreen->context_create = vc4_context_create;
675 pscreen->is_format_supported = vc4_screen_is_format_supported;
676
677 screen->fd = fd;
678 if (ro) {
679 screen->ro = renderonly_dup(ro);
680 if (!screen->ro) {
681 fprintf(stderr, "Failed to dup renderonly object\n");
682 ralloc_free(screen);
683 return NULL;
684 }
685 }
686
687 list_inithead(&screen->bo_cache.time_list);
688 (void) mtx_init(&screen->bo_handles_mutex, mtx_plain);
689 screen->bo_handles = util_hash_table_create(handle_hash, handle_compare);
690
691 screen->has_control_flow =
692 vc4_has_feature(screen, DRM_VC4_PARAM_SUPPORTS_BRANCHES);
693 screen->has_etc1 =
694 vc4_has_feature(screen, DRM_VC4_PARAM_SUPPORTS_ETC1);
695 screen->has_threaded_fs =
696 vc4_has_feature(screen, DRM_VC4_PARAM_SUPPORTS_THREADED_FS);
697 screen->has_madvise =
698 vc4_has_feature(screen, DRM_VC4_PARAM_SUPPORTS_MADVISE);
699 screen->has_perfmon_ioctl =
700 vc4_has_feature(screen, DRM_VC4_PARAM_SUPPORTS_PERFMON);
701
702 err = drmGetCap(fd, DRM_CAP_SYNCOBJ, &syncobj_cap);
703 if (err == 0 && syncobj_cap)
704 screen->has_syncobj = true;
705
706 if (!vc4_get_chip_info(screen))
707 goto fail;
708
709 util_cpu_detect();
710
711 slab_create_parent(&screen->transfer_pool, sizeof(struct vc4_transfer), 16);
712
713 vc4_fence_screen_init(screen);
714
715 vc4_debug = debug_get_option_vc4_debug();
716 if (vc4_debug & VC4_DEBUG_SHADERDB)
717 vc4_debug |= VC4_DEBUG_NORAST;
718
719 #ifdef USE_VC4_SIMULATOR
720 vc4_simulator_init(screen);
721 #endif
722
723 vc4_resource_screen_init(pscreen);
724
725 pscreen->get_name = vc4_screen_get_name;
726 pscreen->get_vendor = vc4_screen_get_vendor;
727 pscreen->get_device_vendor = vc4_screen_get_vendor;
728 pscreen->get_compiler_options = vc4_screen_get_compiler_options;
729 pscreen->query_dmabuf_modifiers = vc4_screen_query_dmabuf_modifiers;
730
731 if (screen->has_perfmon_ioctl) {
732 pscreen->get_driver_query_group_info = vc4_get_driver_query_group_info;
733 pscreen->get_driver_query_info = vc4_get_driver_query_info;
734 }
735
736 return pscreen;
737
738 fail:
739 close(fd);
740 ralloc_free(pscreen);
741 return NULL;
742 }