1 /*
2  * Copyright © 2017 Intel Corporation
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 shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 /**
24  * @file iris_screen.c
25  *
26  * Screen related driver hooks and capability lists.
27  *
28  * A program may use multiple rendering contexts (iris_context), but
29  * they all share a common screen (iris_screen).  Global driver state
30  * can be stored in the screen; it may be accessed by multiple threads.
31  */
32 
33 #include <stdio.h>
34 #include <errno.h>
35 #include <sys/ioctl.h>
36 #include "pipe/p_defines.h"
37 #include "pipe/p_state.h"
38 #include "pipe/p_context.h"
39 #include "pipe/p_screen.h"
40 #include "util/debug.h"
41 #include "util/u_inlines.h"
42 #include "util/format/u_format.h"
43 #include "util/u_transfer_helper.h"
44 #include "util/u_upload_mgr.h"
45 #include "util/ralloc.h"
46 #include "util/xmlconfig.h"
47 #include "drm-uapi/i915_drm.h"
48 #include "iris_context.h"
49 #include "iris_defines.h"
50 #include "iris_fence.h"
51 #include "iris_pipe.h"
52 #include "iris_resource.h"
53 #include "iris_screen.h"
54 #include "compiler/glsl_types.h"
55 #include "intel/compiler/brw_compiler.h"
56 #include "intel/common/gen_gem.h"
57 #include "intel/common/gen_l3_config.h"
58 #include "intel/common/gen_uuid.h"
59 #include "iris_monitor.h"
60 
61 #define genX_call(devinfo, func, ...)             \
62    switch (devinfo.gen) {                        \
63    case 12:                                       \
64       gen12_##func(__VA_ARGS__);                  \
65       break;                                      \
66    case 11:                                       \
67       gen11_##func(__VA_ARGS__);                  \
68       break;                                      \
69    case 9:                                        \
70       gen9_##func(__VA_ARGS__);                   \
71       break;                                      \
72    case 8:                                        \
73       gen8_##func(__VA_ARGS__);                   \
74       break;                                      \
75    default:                                       \
76       unreachable("Unknown hardware generation"); \
77    }
78 
79 static void
iris_flush_frontbuffer(struct pipe_screen * _screen,struct pipe_resource * resource,unsigned level,unsigned layer,void * context_private,struct pipe_box * box)80 iris_flush_frontbuffer(struct pipe_screen *_screen,
81                        struct pipe_resource *resource,
82                        unsigned level, unsigned layer,
83                        void *context_private, struct pipe_box *box)
84 {
85 }
86 
87 static const char *
iris_get_vendor(struct pipe_screen * pscreen)88 iris_get_vendor(struct pipe_screen *pscreen)
89 {
90    return "Intel";
91 }
92 
93 static const char *
iris_get_device_vendor(struct pipe_screen * pscreen)94 iris_get_device_vendor(struct pipe_screen *pscreen)
95 {
96    return "Intel";
97 }
98 
99 static void
iris_get_device_uuid(struct pipe_screen * pscreen,char * uuid)100 iris_get_device_uuid(struct pipe_screen *pscreen, char *uuid)
101 {
102    struct iris_screen *screen = (struct iris_screen *)pscreen;
103    const struct isl_device *isldev = &screen->isl_dev;
104 
105    gen_uuid_compute_device_id((uint8_t *)uuid, isldev, PIPE_UUID_SIZE);
106 }
107 
108 static void
iris_get_driver_uuid(struct pipe_screen * pscreen,char * uuid)109 iris_get_driver_uuid(struct pipe_screen *pscreen, char *uuid)
110 {
111    struct iris_screen *screen = (struct iris_screen *)pscreen;
112    const struct gen_device_info *devinfo = &screen->devinfo;
113 
114    gen_uuid_compute_driver_id((uint8_t *)uuid, devinfo, PIPE_UUID_SIZE);
115 }
116 
117 static bool
iris_enable_clover()118 iris_enable_clover()
119 {
120    static int enable = -1;
121    if (enable < 0)
122       enable = env_var_as_boolean("IRIS_ENABLE_CLOVER", false);
123    return enable;
124 }
125 
126 static void
iris_warn_clover()127 iris_warn_clover()
128 {
129    static bool warned = false;
130    if (warned)
131       return;
132 
133    warned = true;
134    fprintf(stderr, "WARNING: OpenCL support via iris+clover is incomplete.\n"
135                    "For a complete and conformant OpenCL implementation, use\n"
136                    "https://github.com/intel/compute-runtime instead\n");
137 }
138 
139 static const char *
iris_get_name(struct pipe_screen * pscreen)140 iris_get_name(struct pipe_screen *pscreen)
141 {
142    struct iris_screen *screen = (struct iris_screen *)pscreen;
143    static char buf[128];
144    const char *name = gen_get_device_name(screen->pci_id);
145 
146    if (!name)
147       name = "Intel Unknown";
148 
149    snprintf(buf, sizeof(buf), "Mesa %s", name);
150    return buf;
151 }
152 
153 static int
iris_get_param(struct pipe_screen * pscreen,enum pipe_cap param)154 iris_get_param(struct pipe_screen *pscreen, enum pipe_cap param)
155 {
156    struct iris_screen *screen = (struct iris_screen *)pscreen;
157    const struct gen_device_info *devinfo = &screen->devinfo;
158 
159    switch (param) {
160    case PIPE_CAP_NPOT_TEXTURES:
161    case PIPE_CAP_ANISOTROPIC_FILTER:
162    case PIPE_CAP_POINT_SPRITE:
163    case PIPE_CAP_OCCLUSION_QUERY:
164    case PIPE_CAP_QUERY_TIME_ELAPSED:
165    case PIPE_CAP_TEXTURE_SWIZZLE:
166    case PIPE_CAP_TEXTURE_MIRROR_CLAMP_TO_EDGE:
167    case PIPE_CAP_BLEND_EQUATION_SEPARATE:
168    case PIPE_CAP_FRAGMENT_SHADER_TEXTURE_LOD:
169    case PIPE_CAP_FRAGMENT_SHADER_DERIVATIVES:
170    case PIPE_CAP_VERTEX_SHADER_SATURATE:
171    case PIPE_CAP_PRIMITIVE_RESTART:
172    case PIPE_CAP_PRIMITIVE_RESTART_FIXED_INDEX:
173    case PIPE_CAP_INDEP_BLEND_ENABLE:
174    case PIPE_CAP_INDEP_BLEND_FUNC:
175    case PIPE_CAP_RGB_OVERRIDE_DST_ALPHA_BLEND:
176    case PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT:
177    case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER:
178    case PIPE_CAP_DEPTH_CLIP_DISABLE:
179    case PIPE_CAP_TGSI_INSTANCEID:
180    case PIPE_CAP_VERTEX_ELEMENT_INSTANCE_DIVISOR:
181    case PIPE_CAP_MIXED_COLORBUFFER_FORMATS:
182    case PIPE_CAP_SEAMLESS_CUBE_MAP:
183    case PIPE_CAP_SEAMLESS_CUBE_MAP_PER_TEXTURE:
184    case PIPE_CAP_CONDITIONAL_RENDER:
185    case PIPE_CAP_TEXTURE_BARRIER:
186    case PIPE_CAP_STREAM_OUTPUT_PAUSE_RESUME:
187    case PIPE_CAP_VERTEX_COLOR_UNCLAMPED:
188    case PIPE_CAP_COMPUTE:
189    case PIPE_CAP_START_INSTANCE:
190    case PIPE_CAP_QUERY_TIMESTAMP:
191    case PIPE_CAP_TEXTURE_MULTISAMPLE:
192    case PIPE_CAP_CUBE_MAP_ARRAY:
193    case PIPE_CAP_TEXTURE_BUFFER_OBJECTS:
194    case PIPE_CAP_QUERY_PIPELINE_STATISTICS_SINGLE:
195    case PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT:
196    case PIPE_CAP_TEXTURE_QUERY_LOD:
197    case PIPE_CAP_SAMPLE_SHADING:
198    case PIPE_CAP_FORCE_PERSAMPLE_INTERP:
199    case PIPE_CAP_DRAW_INDIRECT:
200    case PIPE_CAP_MULTI_DRAW_INDIRECT:
201    case PIPE_CAP_MULTI_DRAW_INDIRECT_PARAMS:
202    case PIPE_CAP_MIXED_FRAMEBUFFER_SIZES:
203    case PIPE_CAP_TGSI_VS_LAYER_VIEWPORT:
204    case PIPE_CAP_TGSI_TES_LAYER_VIEWPORT:
205    case PIPE_CAP_TGSI_FS_FINE_DERIVATIVE:
206    case PIPE_CAP_TGSI_PACK_HALF_FLOAT:
207    case PIPE_CAP_ACCELERATED:
208    case PIPE_CAP_UMA:
209    case PIPE_CAP_CONDITIONAL_RENDER_INVERTED:
210    case PIPE_CAP_CLIP_HALFZ:
211    case PIPE_CAP_TGSI_TEXCOORD:
212    case PIPE_CAP_STREAM_OUTPUT_INTERLEAVE_BUFFERS:
213    case PIPE_CAP_DOUBLES:
214    case PIPE_CAP_INT64:
215    case PIPE_CAP_INT64_DIVMOD:
216    case PIPE_CAP_SAMPLER_VIEW_TARGET:
217    case PIPE_CAP_ROBUST_BUFFER_ACCESS_BEHAVIOR:
218    case PIPE_CAP_DEVICE_RESET_STATUS_QUERY:
219    case PIPE_CAP_COPY_BETWEEN_COMPRESSED_AND_PLAIN_FORMATS:
220    case PIPE_CAP_FRAMEBUFFER_NO_ATTACHMENT:
221    case PIPE_CAP_CULL_DISTANCE:
222    case PIPE_CAP_PACKED_UNIFORMS:
223    case PIPE_CAP_SIGNED_VERTEX_BUFFER_OFFSET:
224    case PIPE_CAP_TEXTURE_FLOAT_LINEAR:
225    case PIPE_CAP_TEXTURE_HALF_FLOAT_LINEAR:
226    case PIPE_CAP_POLYGON_OFFSET_CLAMP:
227    case PIPE_CAP_QUERY_SO_OVERFLOW:
228    case PIPE_CAP_QUERY_BUFFER_OBJECT:
229    case PIPE_CAP_TGSI_TEX_TXF_LZ:
230    case PIPE_CAP_TGSI_TXQS:
231    case PIPE_CAP_TGSI_CLOCK:
232    case PIPE_CAP_TGSI_BALLOT:
233    case PIPE_CAP_MULTISAMPLE_Z_RESOLVE:
234    case PIPE_CAP_CLEAR_TEXTURE:
235    case PIPE_CAP_CLEAR_SCISSORED:
236    case PIPE_CAP_TGSI_VOTE:
237    case PIPE_CAP_TGSI_VS_WINDOW_SPACE_POSITION:
238    case PIPE_CAP_TEXTURE_GATHER_SM5:
239    case PIPE_CAP_TGSI_ARRAY_COMPONENTS:
240    case PIPE_CAP_GLSL_TESS_LEVELS_AS_INPUTS:
241    case PIPE_CAP_LOAD_CONSTBUF:
242    case PIPE_CAP_NIR_COMPACT_ARRAYS:
243    case PIPE_CAP_DRAW_PARAMETERS:
244    case PIPE_CAP_TGSI_FS_POSITION_IS_SYSVAL:
245    case PIPE_CAP_TGSI_FS_FACE_IS_INTEGER_SYSVAL:
246    case PIPE_CAP_COMPUTE_SHADER_DERIVATIVES:
247    case PIPE_CAP_INVALIDATE_BUFFER:
248    case PIPE_CAP_SURFACE_REINTERPRET_BLOCKS:
249    case PIPE_CAP_CS_DERIVED_SYSTEM_VALUES_SUPPORTED:
250    case PIPE_CAP_TEXTURE_SHADOW_LOD:
251    case PIPE_CAP_SHADER_SAMPLES_IDENTICAL:
252    case PIPE_CAP_GL_SPIRV:
253    case PIPE_CAP_GL_SPIRV_VARIABLE_POINTERS:
254    case PIPE_CAP_DEMOTE_TO_HELPER_INVOCATION:
255    case PIPE_CAP_NATIVE_FENCE_FD:
256    case PIPE_CAP_MIXED_COLOR_DEPTH_BITS:
257    case PIPE_CAP_FENCE_SIGNAL:
258       return true;
259    case PIPE_CAP_FBFETCH:
260       return BRW_MAX_DRAW_BUFFERS;
261    case PIPE_CAP_FBFETCH_COHERENT:
262    case PIPE_CAP_CONSERVATIVE_RASTER_INNER_COVERAGE:
263    case PIPE_CAP_POST_DEPTH_COVERAGE:
264    case PIPE_CAP_SHADER_STENCIL_EXPORT:
265    case PIPE_CAP_DEPTH_CLIP_DISABLE_SEPARATE:
266    case PIPE_CAP_FRAGMENT_SHADER_INTERLOCK:
267    case PIPE_CAP_ATOMIC_FLOAT_MINMAX:
268       return devinfo->gen >= 9;
269    case PIPE_CAP_DEPTH_BOUNDS_TEST:
270       return devinfo->gen >= 12;
271    case PIPE_CAP_MAX_DUAL_SOURCE_RENDER_TARGETS:
272       return 1;
273    case PIPE_CAP_MAX_RENDER_TARGETS:
274       return BRW_MAX_DRAW_BUFFERS;
275    case PIPE_CAP_MAX_TEXTURE_2D_SIZE:
276       return 16384;
277    case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
278       return IRIS_MAX_MIPLEVELS; /* 16384x16384 */
279    case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
280       return 12; /* 2048x2048 */
281    case PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS:
282       return 4;
283    case PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS:
284       return 2048;
285    case PIPE_CAP_MAX_STREAM_OUTPUT_SEPARATE_COMPONENTS:
286       return BRW_MAX_SOL_BINDINGS / IRIS_MAX_SOL_BUFFERS;
287    case PIPE_CAP_MAX_STREAM_OUTPUT_INTERLEAVED_COMPONENTS:
288       return BRW_MAX_SOL_BINDINGS;
289    case PIPE_CAP_GLSL_FEATURE_LEVEL:
290    case PIPE_CAP_GLSL_FEATURE_LEVEL_COMPATIBILITY:
291       return 460;
292    case PIPE_CAP_CONSTANT_BUFFER_OFFSET_ALIGNMENT:
293       /* 3DSTATE_CONSTANT_XS requires the start of UBOs to be 32B aligned */
294       return 32;
295    case PIPE_CAP_MIN_MAP_BUFFER_ALIGNMENT:
296       return IRIS_MAP_BUFFER_ALIGNMENT;
297    case PIPE_CAP_SHADER_BUFFER_OFFSET_ALIGNMENT:
298       /* Choose a cacheline (64 bytes) so that we can safely have the CPU and
299        * GPU writing the same SSBO on non-coherent systems (Atom CPUs).  With
300        * UBOs, the GPU never writes, so there's no problem.  For an SSBO, the
301        * GPU and the CPU can be updating disjoint regions of the buffer
302        * simultaneously and that will break if the regions overlap the same
303        * cacheline.
304        */
305       return 64;
306    case PIPE_CAP_MAX_SHADER_BUFFER_SIZE:
307       return 1 << 27;
308    case PIPE_CAP_TEXTURE_BUFFER_OFFSET_ALIGNMENT:
309       return 16; // XXX: u_screen says 256 is the minimum value...
310    case PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER:
311       return true;
312    case PIPE_CAP_MAX_TEXTURE_BUFFER_SIZE:
313       return IRIS_MAX_TEXTURE_BUFFER_SIZE;
314    case PIPE_CAP_MAX_VIEWPORTS:
315       return 16;
316    case PIPE_CAP_MAX_GEOMETRY_OUTPUT_VERTICES:
317       return 256;
318    case PIPE_CAP_MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS:
319       return 1024;
320    case PIPE_CAP_MAX_GS_INVOCATIONS:
321       return 32;
322    case PIPE_CAP_MAX_TEXTURE_GATHER_COMPONENTS:
323       return 4;
324    case PIPE_CAP_MIN_TEXTURE_GATHER_OFFSET:
325       return -32;
326    case PIPE_CAP_MAX_TEXTURE_GATHER_OFFSET:
327       return 31;
328    case PIPE_CAP_MAX_VERTEX_STREAMS:
329       return 4;
330    case PIPE_CAP_VENDOR_ID:
331       return 0x8086;
332    case PIPE_CAP_DEVICE_ID:
333       return screen->pci_id;
334    case PIPE_CAP_VIDEO_MEMORY: {
335       /* Once a batch uses more than 75% of the maximum mappable size, we
336        * assume that there's some fragmentation, and we start doing extra
337        * flushing, etc.  That's the big cliff apps will care about.
338        */
339       const unsigned gpu_mappable_megabytes =
340          (devinfo->aperture_bytes * 3 / 4) / (1024 * 1024);
341 
342       const long system_memory_pages = sysconf(_SC_PHYS_PAGES);
343       const long system_page_size = sysconf(_SC_PAGE_SIZE);
344 
345       if (system_memory_pages <= 0 || system_page_size <= 0)
346          return -1;
347 
348       const uint64_t system_memory_bytes =
349          (uint64_t) system_memory_pages * (uint64_t) system_page_size;
350 
351       const unsigned system_memory_megabytes =
352          (unsigned) (system_memory_bytes / (1024 * 1024));
353 
354       return MIN2(system_memory_megabytes, gpu_mappable_megabytes);
355    }
356    case PIPE_CAP_MAX_SHADER_PATCH_VARYINGS:
357    case PIPE_CAP_MAX_VARYINGS:
358       return 32;
359    case PIPE_CAP_RESOURCE_FROM_USER_MEMORY:
360       /* AMD_pinned_memory assumes the flexibility of using client memory
361        * for any buffer (incl. vertex buffers) which rules out the prospect
362        * of using snooped buffers, as using snooped buffers without
363        * cogniscience is likely to be detrimental to performance and require
364        * extensive checking in the driver for correctness, e.g. to prevent
365        * illegal snoop <-> snoop transfers.
366        */
367       return devinfo->has_llc;
368    case PIPE_CAP_THROTTLE:
369       return screen->driconf.disable_throttling ? 0 : 1;
370 
371    case PIPE_CAP_CONTEXT_PRIORITY_MASK:
372       return PIPE_CONTEXT_PRIORITY_LOW |
373              PIPE_CONTEXT_PRIORITY_MEDIUM |
374              PIPE_CONTEXT_PRIORITY_HIGH;
375 
376    case PIPE_CAP_FRONTEND_NOOP:
377       return true;
378 
379    // XXX: don't hardcode 00:00:02.0 PCI here
380    case PIPE_CAP_PCI_GROUP:
381       return 0;
382    case PIPE_CAP_PCI_BUS:
383       return 0;
384    case PIPE_CAP_PCI_DEVICE:
385       return 2;
386    case PIPE_CAP_PCI_FUNCTION:
387       return 0;
388 
389    case PIPE_CAP_OPENCL_INTEGER_FUNCTIONS:
390    case PIPE_CAP_INTEGER_MULTIPLY_32X16:
391       return true;
392 
393    default:
394       return u_pipe_screen_get_param_defaults(pscreen, param);
395    }
396    return 0;
397 }
398 
399 static float
iris_get_paramf(struct pipe_screen * pscreen,enum pipe_capf param)400 iris_get_paramf(struct pipe_screen *pscreen, enum pipe_capf param)
401 {
402    switch (param) {
403    case PIPE_CAPF_MAX_LINE_WIDTH:
404    case PIPE_CAPF_MAX_LINE_WIDTH_AA:
405       return 7.375f;
406 
407    case PIPE_CAPF_MAX_POINT_WIDTH:
408    case PIPE_CAPF_MAX_POINT_WIDTH_AA:
409       return 255.0f;
410 
411    case PIPE_CAPF_MAX_TEXTURE_ANISOTROPY:
412       return 16.0f;
413    case PIPE_CAPF_MAX_TEXTURE_LOD_BIAS:
414       return 15.0f;
415    case PIPE_CAPF_MIN_CONSERVATIVE_RASTER_DILATE:
416    case PIPE_CAPF_MAX_CONSERVATIVE_RASTER_DILATE:
417    case PIPE_CAPF_CONSERVATIVE_RASTER_DILATE_GRANULARITY:
418       return 0.0f;
419    default:
420       unreachable("unknown param");
421    }
422 }
423 
424 static int
iris_get_shader_param(struct pipe_screen * pscreen,enum pipe_shader_type p_stage,enum pipe_shader_cap param)425 iris_get_shader_param(struct pipe_screen *pscreen,
426                       enum pipe_shader_type p_stage,
427                       enum pipe_shader_cap param)
428 {
429    gl_shader_stage stage = stage_from_pipe(p_stage);
430 
431    /* this is probably not totally correct.. but it's a start: */
432    switch (param) {
433    case PIPE_SHADER_CAP_MAX_INSTRUCTIONS:
434       return stage == MESA_SHADER_FRAGMENT ? 1024 : 16384;
435    case PIPE_SHADER_CAP_MAX_ALU_INSTRUCTIONS:
436    case PIPE_SHADER_CAP_MAX_TEX_INSTRUCTIONS:
437    case PIPE_SHADER_CAP_MAX_TEX_INDIRECTIONS:
438       return stage == MESA_SHADER_FRAGMENT ? 1024 : 0;
439 
440    case PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH:
441       return UINT_MAX;
442 
443    case PIPE_SHADER_CAP_MAX_INPUTS:
444       return stage == MESA_SHADER_VERTEX ? 16 : 32;
445    case PIPE_SHADER_CAP_MAX_OUTPUTS:
446       return 32;
447    case PIPE_SHADER_CAP_MAX_CONST_BUFFER_SIZE:
448       return 16 * 1024 * sizeof(float);
449    case PIPE_SHADER_CAP_MAX_CONST_BUFFERS:
450       return 16;
451    case PIPE_SHADER_CAP_MAX_TEMPS:
452       return 256; /* GL_MAX_PROGRAM_TEMPORARIES_ARB */
453    case PIPE_SHADER_CAP_TGSI_CONT_SUPPORTED:
454       return 0;
455    case PIPE_SHADER_CAP_INDIRECT_INPUT_ADDR:
456    case PIPE_SHADER_CAP_INDIRECT_OUTPUT_ADDR:
457    case PIPE_SHADER_CAP_INDIRECT_TEMP_ADDR:
458    case PIPE_SHADER_CAP_INDIRECT_CONST_ADDR:
459       /* Lie about these to avoid st/mesa's GLSL IR lowering of indirects,
460        * which we don't want.  Our compiler backend will check brw_compiler's
461        * options and call nir_lower_indirect_derefs appropriately anyway.
462        */
463       return true;
464    case PIPE_SHADER_CAP_SUBROUTINES:
465       return 0;
466    case PIPE_SHADER_CAP_INTEGERS:
467       return 1;
468    case PIPE_SHADER_CAP_INT64_ATOMICS:
469    case PIPE_SHADER_CAP_FP16:
470    case PIPE_SHADER_CAP_FP16_DERIVATIVES:
471    case PIPE_SHADER_CAP_INT16:
472    case PIPE_SHADER_CAP_GLSL_16BIT_CONSTS:
473       return 0;
474    case PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS:
475    case PIPE_SHADER_CAP_MAX_SAMPLER_VIEWS:
476    case PIPE_SHADER_CAP_MAX_SHADER_IMAGES:
477       return IRIS_MAX_TEXTURE_SAMPLERS;
478    case PIPE_SHADER_CAP_MAX_SHADER_BUFFERS:
479       return IRIS_MAX_ABOS + IRIS_MAX_SSBOS;
480    case PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTERS:
481    case PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTER_BUFFERS:
482       return 0;
483    case PIPE_SHADER_CAP_PREFERRED_IR:
484       return PIPE_SHADER_IR_NIR;
485    case PIPE_SHADER_CAP_SUPPORTED_IRS: {
486       int irs = 1 << PIPE_SHADER_IR_NIR;
487       if (iris_enable_clover())
488          irs |= 1 << PIPE_SHADER_IR_NIR_SERIALIZED;
489       return irs;
490    }
491    case PIPE_SHADER_CAP_TGSI_DROUND_SUPPORTED:
492    case PIPE_SHADER_CAP_TGSI_LDEXP_SUPPORTED:
493       return 1;
494    case PIPE_SHADER_CAP_LOWER_IF_THRESHOLD:
495    case PIPE_SHADER_CAP_TGSI_SKIP_MERGE_REGISTERS:
496    case PIPE_SHADER_CAP_TGSI_DFRACEXP_DLDEXP_SUPPORTED:
497    case PIPE_SHADER_CAP_TGSI_FMA_SUPPORTED:
498    case PIPE_SHADER_CAP_TGSI_ANY_INOUT_DECL_RANGE:
499    case PIPE_SHADER_CAP_TGSI_SQRT_SUPPORTED:
500    case PIPE_SHADER_CAP_MAX_UNROLL_ITERATIONS_HINT:
501       return 0;
502    default:
503       unreachable("unknown shader param");
504    }
505 }
506 
507 static int
iris_get_compute_param(struct pipe_screen * pscreen,enum pipe_shader_ir ir_type,enum pipe_compute_cap param,void * ret)508 iris_get_compute_param(struct pipe_screen *pscreen,
509                        enum pipe_shader_ir ir_type,
510                        enum pipe_compute_cap param,
511                        void *ret)
512 {
513    struct iris_screen *screen = (struct iris_screen *)pscreen;
514    const struct gen_device_info *devinfo = &screen->devinfo;
515 
516    /* Limit max_threads to 64 for the GPGPU_WALKER command. */
517    const unsigned max_threads = MIN2(64, devinfo->max_cs_threads);
518    const uint32_t max_invocations = 32 * max_threads;
519 
520 #define RET(x) do {                  \
521    if (ret)                          \
522       memcpy(ret, x, sizeof(x));     \
523    return sizeof(x);                 \
524 } while (0)
525 
526    switch (param) {
527    case PIPE_COMPUTE_CAP_ADDRESS_BITS:
528       /* This gets queried on clover device init and is never queried by the
529        * OpenGL state tracker.
530        */
531       iris_warn_clover();
532       RET((uint32_t []){ 64 });
533 
534    case PIPE_COMPUTE_CAP_IR_TARGET:
535       if (ret)
536          strcpy(ret, "gen");
537       return 4;
538 
539    case PIPE_COMPUTE_CAP_GRID_DIMENSION:
540       RET((uint64_t []) { 3 });
541 
542    case PIPE_COMPUTE_CAP_MAX_GRID_SIZE:
543       RET(((uint64_t []) { 65535, 65535, 65535 }));
544 
545    case PIPE_COMPUTE_CAP_MAX_BLOCK_SIZE:
546       /* MaxComputeWorkGroupSize[0..2] */
547       RET(((uint64_t []) {max_invocations, max_invocations, max_invocations}));
548 
549    case PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK:
550       /* MaxComputeWorkGroupInvocations */
551    case PIPE_COMPUTE_CAP_MAX_VARIABLE_THREADS_PER_BLOCK:
552       /* MaxComputeVariableGroupInvocations */
553       RET((uint64_t []) { max_invocations });
554 
555    case PIPE_COMPUTE_CAP_MAX_LOCAL_SIZE:
556       /* MaxComputeSharedMemorySize */
557       RET((uint64_t []) { 64 * 1024 });
558 
559    case PIPE_COMPUTE_CAP_IMAGES_SUPPORTED:
560       RET((uint32_t []) { 0 });
561 
562    case PIPE_COMPUTE_CAP_SUBGROUP_SIZE:
563       RET((uint32_t []) { BRW_SUBGROUP_SIZE });
564 
565    case PIPE_COMPUTE_CAP_MAX_MEM_ALLOC_SIZE:
566    case PIPE_COMPUTE_CAP_MAX_GLOBAL_SIZE:
567       RET((uint64_t []) { 1 << 30 }); /* TODO */
568 
569    case PIPE_COMPUTE_CAP_MAX_CLOCK_FREQUENCY:
570       RET((uint32_t []) { 400 }); /* TODO */
571 
572    case PIPE_COMPUTE_CAP_MAX_COMPUTE_UNITS: {
573       unsigned total_num_subslices = 0;
574       for (unsigned i = 0; i < devinfo->num_slices; i++)
575          total_num_subslices += devinfo->num_subslices[i];
576       RET((uint32_t []) { total_num_subslices });
577    }
578 
579    case PIPE_COMPUTE_CAP_MAX_PRIVATE_SIZE:
580       /* MaxComputeSharedMemorySize */
581       RET((uint64_t []) { 64 * 1024 });
582 
583    case PIPE_COMPUTE_CAP_MAX_INPUT_SIZE:
584       /* We could probably allow more; this is the OpenCL minimum */
585       RET((uint64_t []) { 1024 });
586 
587    default:
588       unreachable("unknown compute param");
589    }
590 }
591 
592 static uint64_t
iris_get_timestamp(struct pipe_screen * pscreen)593 iris_get_timestamp(struct pipe_screen *pscreen)
594 {
595    struct iris_screen *screen = (struct iris_screen *) pscreen;
596    const unsigned TIMESTAMP = 0x2358;
597    uint64_t result;
598 
599    iris_reg_read(screen->bufmgr, TIMESTAMP | 1, &result);
600 
601    result = gen_device_info_timebase_scale(&screen->devinfo, result);
602    result &= (1ull << TIMESTAMP_BITS) - 1;
603 
604    return result;
605 }
606 
607 void
iris_screen_destroy(struct iris_screen * screen)608 iris_screen_destroy(struct iris_screen *screen)
609 {
610    glsl_type_singleton_decref();
611    iris_bo_unreference(screen->workaround_bo);
612    u_transfer_helper_destroy(screen->base.transfer_helper);
613    iris_bufmgr_unref(screen->bufmgr);
614    disk_cache_destroy(screen->disk_cache);
615    close(screen->winsys_fd);
616    ralloc_free(screen);
617 }
618 
619 static void
iris_screen_unref(struct pipe_screen * pscreen)620 iris_screen_unref(struct pipe_screen *pscreen)
621 {
622    iris_pscreen_unref(pscreen);
623 }
624 
625 static void
iris_query_memory_info(struct pipe_screen * pscreen,struct pipe_memory_info * info)626 iris_query_memory_info(struct pipe_screen *pscreen,
627                        struct pipe_memory_info *info)
628 {
629 }
630 
631 static const void *
iris_get_compiler_options(struct pipe_screen * pscreen,enum pipe_shader_ir ir,enum pipe_shader_type pstage)632 iris_get_compiler_options(struct pipe_screen *pscreen,
633                           enum pipe_shader_ir ir,
634                           enum pipe_shader_type pstage)
635 {
636    struct iris_screen *screen = (struct iris_screen *) pscreen;
637    gl_shader_stage stage = stage_from_pipe(pstage);
638    assert(ir == PIPE_SHADER_IR_NIR);
639 
640    return screen->compiler->glsl_compiler_options[stage].NirOptions;
641 }
642 
643 static struct disk_cache *
iris_get_disk_shader_cache(struct pipe_screen * pscreen)644 iris_get_disk_shader_cache(struct pipe_screen *pscreen)
645 {
646    struct iris_screen *screen = (struct iris_screen *) pscreen;
647    return screen->disk_cache;
648 }
649 
650 static int
iris_getparam(int fd,int param,int * value)651 iris_getparam(int fd, int param, int *value)
652 {
653    struct drm_i915_getparam gp = { .param = param, .value = value };
654 
655    if (ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp) == -1)
656       return -errno;
657 
658    return 0;
659 }
660 
661 static int
iris_getparam_integer(int fd,int param)662 iris_getparam_integer(int fd, int param)
663 {
664    int value = -1;
665 
666    if (iris_getparam(fd, param, &value) == 0)
667       return value;
668 
669    return -1;
670 }
671 
672 static const struct gen_l3_config *
iris_get_default_l3_config(const struct gen_device_info * devinfo,bool compute)673 iris_get_default_l3_config(const struct gen_device_info *devinfo,
674                            bool compute)
675 {
676    bool wants_dc_cache = true;
677    bool has_slm = compute;
678    const struct gen_l3_weights w =
679       gen_get_default_l3_weights(devinfo, wants_dc_cache, has_slm);
680    return gen_get_l3_config(devinfo, w);
681 }
682 
683 static void
iris_shader_debug_log(void * data,const char * fmt,...)684 iris_shader_debug_log(void *data, const char *fmt, ...)
685 {
686    struct pipe_debug_callback *dbg = data;
687    unsigned id = 0;
688    va_list args;
689 
690    if (!dbg->debug_message)
691       return;
692 
693    va_start(args, fmt);
694    dbg->debug_message(dbg->data, &id, PIPE_DEBUG_TYPE_SHADER_INFO, fmt, args);
695    va_end(args);
696 }
697 
698 static void
iris_shader_perf_log(void * data,const char * fmt,...)699 iris_shader_perf_log(void *data, const char *fmt, ...)
700 {
701    struct pipe_debug_callback *dbg = data;
702    unsigned id = 0;
703    va_list args;
704    va_start(args, fmt);
705 
706    if (INTEL_DEBUG & DEBUG_PERF) {
707       va_list args_copy;
708       va_copy(args_copy, args);
709       vfprintf(stderr, fmt, args_copy);
710       va_end(args_copy);
711    }
712 
713    if (dbg->debug_message) {
714       dbg->debug_message(dbg->data, &id, PIPE_DEBUG_TYPE_PERF_INFO, fmt, args);
715    }
716 
717    va_end(args);
718 }
719 
720 static void
iris_detect_kernel_features(struct iris_screen * screen)721 iris_detect_kernel_features(struct iris_screen *screen)
722 {
723    /* Kernel 5.2+ */
724    if (gen_gem_supports_syncobj_wait(screen->fd))
725       screen->kernel_features |= KERNEL_HAS_WAIT_FOR_SUBMIT;
726 }
727 
728 static bool
iris_init_identifier_bo(struct iris_screen * screen)729 iris_init_identifier_bo(struct iris_screen *screen)
730 {
731    void *bo_map;
732 
733    bo_map = iris_bo_map(NULL, screen->workaround_bo, MAP_READ | MAP_WRITE);
734    if (!bo_map)
735       return false;
736 
737    screen->workaround_bo->kflags |= EXEC_OBJECT_CAPTURE;
738    screen->workaround_address = (struct iris_address) {
739       .bo = screen->workaround_bo,
740       .offset = ALIGN(
741          intel_debug_write_identifiers(bo_map, 4096, "Iris") + 8, 8),
742    };
743 
744    iris_bo_unmap(screen->workaround_bo);
745 
746    return true;
747 }
748 
749 struct pipe_screen *
iris_screen_create(int fd,const struct pipe_screen_config * config)750 iris_screen_create(int fd, const struct pipe_screen_config *config)
751 {
752    /* Here are the i915 features we need for Iris (in chronoligical order) :
753     *    - I915_PARAM_HAS_EXEC_NO_RELOC     (3.10)
754     *    - I915_PARAM_HAS_EXEC_HANDLE_LUT   (3.10)
755     *    - I915_PARAM_HAS_EXEC_BATCH_FIRST  (4.13)
756     *    - I915_PARAM_HAS_EXEC_FENCE_ARRAY  (4.14)
757     *    - I915_PARAM_HAS_CONTEXT_ISOLATION (4.16)
758     *
759     * Checking the last feature availability will include all previous ones.
760     */
761    if (iris_getparam_integer(fd, I915_PARAM_HAS_CONTEXT_ISOLATION) <= 0) {
762       debug_error("Kernel is too old for Iris. Consider upgrading to kernel v4.16.\n");
763       return NULL;
764    }
765 
766    struct iris_screen *screen = rzalloc(NULL, struct iris_screen);
767    if (!screen)
768       return NULL;
769 
770    if (!gen_get_device_info_from_fd(fd, &screen->devinfo))
771       return NULL;
772    screen->pci_id = screen->devinfo.chipset_id;
773    screen->no_hw = screen->devinfo.no_hw;
774 
775    p_atomic_set(&screen->refcount, 1);
776 
777    if (screen->devinfo.gen < 8 || screen->devinfo.is_cherryview)
778       return NULL;
779 
780    bool bo_reuse = false;
781    int bo_reuse_mode = driQueryOptioni(config->options, "bo_reuse");
782    switch (bo_reuse_mode) {
783    case DRI_CONF_BO_REUSE_DISABLED:
784       break;
785    case DRI_CONF_BO_REUSE_ALL:
786       bo_reuse = true;
787       break;
788    }
789 
790    screen->bufmgr = iris_bufmgr_get_for_fd(&screen->devinfo, fd, bo_reuse);
791    if (!screen->bufmgr)
792       return NULL;
793 
794    screen->fd = iris_bufmgr_get_fd(screen->bufmgr);
795    screen->winsys_fd = fd;
796 
797    if (getenv("INTEL_NO_HW") != NULL)
798       screen->no_hw = true;
799 
800    screen->workaround_bo =
801       iris_bo_alloc(screen->bufmgr, "workaround", 4096, IRIS_MEMZONE_OTHER);
802    if (!screen->workaround_bo)
803       return NULL;
804 
805    if (!iris_init_identifier_bo(screen))
806       return NULL;
807 
808    brw_process_intel_debug_variable();
809 
810    screen->driconf.dual_color_blend_by_location =
811       driQueryOptionb(config->options, "dual_color_blend_by_location");
812    screen->driconf.disable_throttling =
813       driQueryOptionb(config->options, "disable_throttling");
814    screen->driconf.always_flush_cache =
815       driQueryOptionb(config->options, "always_flush_cache");
816 
817    screen->precompile = env_var_as_boolean("shader_precompile", true);
818 
819    isl_device_init(&screen->isl_dev, &screen->devinfo, false);
820 
821    screen->compiler = brw_compiler_create(screen, &screen->devinfo);
822    screen->compiler->shader_debug_log = iris_shader_debug_log;
823    screen->compiler->shader_perf_log = iris_shader_perf_log;
824    screen->compiler->supports_pull_constants = false;
825    screen->compiler->supports_shader_constants = true;
826    screen->compiler->compact_params = false;
827    screen->compiler->indirect_ubos_use_sampler = screen->devinfo.gen < 12;
828 
829    screen->l3_config_3d = iris_get_default_l3_config(&screen->devinfo, false);
830    screen->l3_config_cs = iris_get_default_l3_config(&screen->devinfo, true);
831 
832    iris_disk_cache_init(screen);
833 
834    slab_create_parent(&screen->transfer_pool,
835                       sizeof(struct iris_transfer), 64);
836 
837    screen->subslice_total =
838       iris_getparam_integer(screen->fd, I915_PARAM_SUBSLICE_TOTAL);
839    assert(screen->subslice_total >= 1);
840 
841    iris_detect_kernel_features(screen);
842 
843    struct pipe_screen *pscreen = &screen->base;
844 
845    iris_init_screen_fence_functions(pscreen);
846    iris_init_screen_resource_functions(pscreen);
847 
848    pscreen->destroy = iris_screen_unref;
849    pscreen->get_name = iris_get_name;
850    pscreen->get_vendor = iris_get_vendor;
851    pscreen->get_device_vendor = iris_get_device_vendor;
852    pscreen->get_param = iris_get_param;
853    pscreen->get_shader_param = iris_get_shader_param;
854    pscreen->get_compute_param = iris_get_compute_param;
855    pscreen->get_paramf = iris_get_paramf;
856    pscreen->get_compiler_options = iris_get_compiler_options;
857    pscreen->get_device_uuid = iris_get_device_uuid;
858    pscreen->get_driver_uuid = iris_get_driver_uuid;
859    pscreen->get_disk_shader_cache = iris_get_disk_shader_cache;
860    pscreen->is_format_supported = iris_is_format_supported;
861    pscreen->context_create = iris_create_context;
862    pscreen->flush_frontbuffer = iris_flush_frontbuffer;
863    pscreen->get_timestamp = iris_get_timestamp;
864    pscreen->query_memory_info = iris_query_memory_info;
865    pscreen->get_driver_query_group_info = iris_get_monitor_group_info;
866    pscreen->get_driver_query_info = iris_get_monitor_info;
867 
868    genX_call(screen->devinfo, init_screen_state, screen);
869 
870    glsl_type_singleton_init_or_ref();
871 
872    return pscreen;
873 }
874