• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 crocus_screen.c
25  *
26  * Screen related driver hooks and capability lists.
27  *
28  * A program may use multiple rendering contexts (crocus_context), but
29  * they all share a common screen (crocus_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 "crocus_context.h"
49 #include "crocus_defines.h"
50 #include "crocus_fence.h"
51 #include "crocus_pipe.h"
52 #include "crocus_resource.h"
53 #include "crocus_screen.h"
54 #include "intel/compiler/brw_compiler.h"
55 #include "intel/common/intel_gem.h"
56 #include "intel/common/intel_l3_config.h"
57 #include "intel/common/intel_uuid.h"
58 #include "crocus_monitor.h"
59 
60 #define genX_call(devinfo, func, ...)                   \
61    switch ((devinfo)->verx10) {                         \
62    case 80:                                             \
63       gfx8_##func(__VA_ARGS__);                         \
64       break;                                            \
65    case 75:                                             \
66       gfx75_##func(__VA_ARGS__);                        \
67       break;                                            \
68    case 70:                                             \
69       gfx7_##func(__VA_ARGS__);                         \
70       break;                                            \
71    case 60:                                             \
72       gfx6_##func(__VA_ARGS__);                         \
73       break;                                            \
74    case 50:                                             \
75       gfx5_##func(__VA_ARGS__);                         \
76       break;                                            \
77    case 45:                                             \
78       gfx45_##func(__VA_ARGS__);                        \
79       break;                                            \
80    case 40:                                             \
81       gfx4_##func(__VA_ARGS__);                         \
82       break;                                            \
83    default:                                             \
84       unreachable("Unknown hardware generation");       \
85    }
86 
87 static void
crocus_flush_frontbuffer(struct pipe_screen * _screen,struct pipe_context * _pipe,struct pipe_resource * resource,unsigned level,unsigned layer,void * context_private,struct pipe_box * box)88 crocus_flush_frontbuffer(struct pipe_screen *_screen,
89                          struct pipe_context *_pipe,
90                          struct pipe_resource *resource,
91                          unsigned level, unsigned layer,
92                          void *context_private, struct pipe_box *box)
93 {
94 }
95 
96 static const char *
crocus_get_vendor(struct pipe_screen * pscreen)97 crocus_get_vendor(struct pipe_screen *pscreen)
98 {
99    return "Intel";
100 }
101 
102 static const char *
crocus_get_device_vendor(struct pipe_screen * pscreen)103 crocus_get_device_vendor(struct pipe_screen *pscreen)
104 {
105    return "Intel";
106 }
107 
108 static void
crocus_get_device_uuid(struct pipe_screen * pscreen,char * uuid)109 crocus_get_device_uuid(struct pipe_screen *pscreen, char *uuid)
110 {
111    struct crocus_screen *screen = (struct crocus_screen *)pscreen;
112    const struct isl_device *isldev = &screen->isl_dev;
113 
114    intel_uuid_compute_device_id((uint8_t *)uuid, isldev, PIPE_UUID_SIZE);
115 }
116 
117 static void
crocus_get_driver_uuid(struct pipe_screen * pscreen,char * uuid)118 crocus_get_driver_uuid(struct pipe_screen *pscreen, char *uuid)
119 {
120    struct crocus_screen *screen = (struct crocus_screen *)pscreen;
121    const struct intel_device_info *devinfo = &screen->devinfo;
122 
123    intel_uuid_compute_driver_id((uint8_t *)uuid, devinfo, PIPE_UUID_SIZE);
124 }
125 
126 static const char *
crocus_get_name(struct pipe_screen * pscreen)127 crocus_get_name(struct pipe_screen *pscreen)
128 {
129    struct crocus_screen *screen = (struct crocus_screen *)pscreen;
130    const struct intel_device_info *devinfo = &screen->devinfo;
131    static char buf[128];
132 
133    snprintf(buf, sizeof(buf), "Mesa %s", devinfo->name);
134    return buf;
135 }
136 
137 static uint64_t
get_aperture_size(int fd)138 get_aperture_size(int fd)
139 {
140    struct drm_i915_gem_get_aperture aperture = {};
141    intel_ioctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture);
142    return aperture.aper_size;
143 }
144 
145 static int
crocus_get_param(struct pipe_screen * pscreen,enum pipe_cap param)146 crocus_get_param(struct pipe_screen *pscreen, enum pipe_cap param)
147 {
148    struct crocus_screen *screen = (struct crocus_screen *)pscreen;
149    const struct intel_device_info *devinfo = &screen->devinfo;
150 
151    switch (param) {
152    case PIPE_CAP_NPOT_TEXTURES:
153    case PIPE_CAP_ANISOTROPIC_FILTER:
154    case PIPE_CAP_POINT_SPRITE:
155    case PIPE_CAP_OCCLUSION_QUERY:
156    case PIPE_CAP_TEXTURE_SWIZZLE:
157    case PIPE_CAP_TEXTURE_MIRROR_CLAMP_TO_EDGE:
158    case PIPE_CAP_BLEND_EQUATION_SEPARATE:
159    case PIPE_CAP_FRAGMENT_SHADER_TEXTURE_LOD:
160    case PIPE_CAP_FRAGMENT_SHADER_DERIVATIVES:
161    case PIPE_CAP_VERTEX_SHADER_SATURATE:
162    case PIPE_CAP_PRIMITIVE_RESTART:
163    case PIPE_CAP_PRIMITIVE_RESTART_FIXED_INDEX:
164    case PIPE_CAP_INDEP_BLEND_ENABLE:
165    case PIPE_CAP_RGB_OVERRIDE_DST_ALPHA_BLEND:
166    case PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT:
167    case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER:
168    case PIPE_CAP_DEPTH_CLIP_DISABLE:
169    case PIPE_CAP_TGSI_INSTANCEID:
170    case PIPE_CAP_VERTEX_ELEMENT_INSTANCE_DIVISOR:
171    case PIPE_CAP_MIXED_COLORBUFFER_FORMATS:
172    case PIPE_CAP_SEAMLESS_CUBE_MAP:
173    case PIPE_CAP_SEAMLESS_CUBE_MAP_PER_TEXTURE:
174    case PIPE_CAP_CONDITIONAL_RENDER:
175    case PIPE_CAP_TEXTURE_BARRIER:
176    case PIPE_CAP_VERTEX_COLOR_UNCLAMPED:
177    case PIPE_CAP_START_INSTANCE:
178    case PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT:
179    case PIPE_CAP_FORCE_PERSAMPLE_INTERP:
180    case PIPE_CAP_MIXED_FRAMEBUFFER_SIZES:
181    case PIPE_CAP_TGSI_VS_LAYER_VIEWPORT:
182    case PIPE_CAP_TGSI_TES_LAYER_VIEWPORT:
183    case PIPE_CAP_ACCELERATED:
184    case PIPE_CAP_UMA:
185    case PIPE_CAP_CLIP_HALFZ:
186    case PIPE_CAP_TGSI_TEXCOORD:
187    case PIPE_CAP_DEVICE_RESET_STATUS_QUERY:
188    case PIPE_CAP_COPY_BETWEEN_COMPRESSED_AND_PLAIN_FORMATS:
189    case PIPE_CAP_SIGNED_VERTEX_BUFFER_OFFSET:
190    case PIPE_CAP_TEXTURE_FLOAT_LINEAR:
191    case PIPE_CAP_TEXTURE_HALF_FLOAT_LINEAR:
192    case PIPE_CAP_POLYGON_OFFSET_CLAMP:
193    case PIPE_CAP_TGSI_TEX_TXF_LZ:
194    case PIPE_CAP_MULTISAMPLE_Z_RESOLVE:
195    case PIPE_CAP_CLEAR_TEXTURE:
196    case PIPE_CAP_TGSI_VOTE:
197    case PIPE_CAP_TGSI_VS_WINDOW_SPACE_POSITION:
198    case PIPE_CAP_TEXTURE_GATHER_SM5:
199    case PIPE_CAP_TGSI_ARRAY_COMPONENTS:
200    case PIPE_CAP_GLSL_TESS_LEVELS_AS_INPUTS:
201    case PIPE_CAP_NIR_COMPACT_ARRAYS:
202    case PIPE_CAP_TGSI_FS_POSITION_IS_SYSVAL:
203    case PIPE_CAP_TGSI_FS_FACE_IS_INTEGER_SYSVAL:
204    case PIPE_CAP_INVALIDATE_BUFFER:
205    case PIPE_CAP_SURFACE_REINTERPRET_BLOCKS:
206    case PIPE_CAP_CS_DERIVED_SYSTEM_VALUES_SUPPORTED:
207    case PIPE_CAP_FENCE_SIGNAL:
208    case PIPE_CAP_DEMOTE_TO_HELPER_INVOCATION:
209    case PIPE_CAP_GL_CLAMP:
210       return true;
211    case PIPE_CAP_INT64:
212    case PIPE_CAP_INT64_DIVMOD:
213    case PIPE_CAP_TGSI_BALLOT:
214    case PIPE_CAP_PACKED_UNIFORMS:
215       return devinfo->ver == 8;
216    case PIPE_CAP_QUADS_FOLLOW_PROVOKING_VERTEX_CONVENTION:
217       return devinfo->ver <= 5;
218    case PIPE_CAP_TEXTURE_QUERY_LOD:
219    case PIPE_CAP_QUERY_TIME_ELAPSED:
220       return devinfo->ver >= 5;
221    case PIPE_CAP_DRAW_INDIRECT:
222    case PIPE_CAP_MULTI_DRAW_INDIRECT:
223    case PIPE_CAP_MULTI_DRAW_INDIRECT_PARAMS:
224    case PIPE_CAP_FRAMEBUFFER_NO_ATTACHMENT:
225    case PIPE_CAP_TGSI_FS_FINE_DERIVATIVE:
226    case PIPE_CAP_STREAM_OUTPUT_INTERLEAVE_BUFFERS:
227    case PIPE_CAP_TGSI_CLOCK:
228    case PIPE_CAP_TGSI_TXQS:
229    case PIPE_CAP_COMPUTE:
230    case PIPE_CAP_SAMPLER_VIEW_TARGET:
231    case PIPE_CAP_SHADER_SAMPLES_IDENTICAL:
232    case PIPE_CAP_TGSI_PACK_HALF_FLOAT:
233    case PIPE_CAP_GL_SPIRV:
234    case PIPE_CAP_GL_SPIRV_VARIABLE_POINTERS:
235    case PIPE_CAP_COMPUTE_SHADER_DERIVATIVES:
236    case PIPE_CAP_DOUBLES:
237    case PIPE_CAP_MEMOBJ:
238       return devinfo->ver >= 7;
239    case PIPE_CAP_QUERY_BUFFER_OBJECT:
240    case PIPE_CAP_ROBUST_BUFFER_ACCESS_BEHAVIOR:
241       return devinfo->verx10 >= 75;
242    case PIPE_CAP_CULL_DISTANCE:
243    case PIPE_CAP_QUERY_PIPELINE_STATISTICS_SINGLE:
244    case PIPE_CAP_STREAM_OUTPUT_PAUSE_RESUME:
245    case PIPE_CAP_SAMPLE_SHADING:
246    case PIPE_CAP_CUBE_MAP_ARRAY:
247    case PIPE_CAP_QUERY_SO_OVERFLOW:
248    case PIPE_CAP_TEXTURE_MULTISAMPLE:
249    case PIPE_CAP_CONDITIONAL_RENDER_INVERTED:
250    case PIPE_CAP_QUERY_TIMESTAMP:
251    case PIPE_CAP_TEXTURE_BUFFER_OBJECTS:
252    case PIPE_CAP_INDEP_BLEND_FUNC:
253    case PIPE_CAP_TEXTURE_SHADOW_LOD:
254    case PIPE_CAP_LOAD_CONSTBUF:
255    case PIPE_CAP_DRAW_PARAMETERS:
256    case PIPE_CAP_CLEAR_SCISSORED:
257       return devinfo->ver >= 6;
258    case PIPE_CAP_FBFETCH:
259       return devinfo->verx10 >= 45 ? BRW_MAX_DRAW_BUFFERS : 0;
260    case PIPE_CAP_MAX_DUAL_SOURCE_RENDER_TARGETS:
261       /* in theory CL (965gm) can do this */
262       return devinfo->verx10 >= 45 ? 1 : 0;
263    case PIPE_CAP_MAX_RENDER_TARGETS:
264       return BRW_MAX_DRAW_BUFFERS;
265    case PIPE_CAP_MAX_TEXTURE_2D_SIZE:
266       if (devinfo->ver >= 7)
267          return 16384;
268       else
269          return 8192;
270    case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
271       if (devinfo->ver >= 7)
272          return CROCUS_MAX_MIPLEVELS; /* 16384x16384 */
273       else
274          return CROCUS_MAX_MIPLEVELS - 1; /* 8192x8192 */
275    case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
276       return 12; /* 2048x2048 */
277    case PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS:
278       return (devinfo->ver >= 6) ? 4 : 0;
279    case PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS:
280       return devinfo->ver >= 7 ? 2048 : 512;
281    case PIPE_CAP_MAX_STREAM_OUTPUT_SEPARATE_COMPONENTS:
282       return BRW_MAX_SOL_BINDINGS / CROCUS_MAX_SOL_BUFFERS;
283    case PIPE_CAP_MAX_STREAM_OUTPUT_INTERLEAVED_COMPONENTS:
284       return BRW_MAX_SOL_BINDINGS;
285    case PIPE_CAP_GLSL_FEATURE_LEVEL: {
286       if (devinfo->verx10 >= 75)
287          return 460;
288       else if (devinfo->ver >= 7)
289          return 420;
290       else if (devinfo->ver >= 6)
291          return 330;
292       return 140;
293    }
294    case PIPE_CAP_GLSL_FEATURE_LEVEL_COMPATIBILITY:
295       return 140;
296 
297    case PIPE_CAP_CONSTANT_BUFFER_OFFSET_ALIGNMENT:
298       /* 3DSTATE_CONSTANT_XS requires the start of UBOs to be 32B aligned */
299       return 32;
300    case PIPE_CAP_MIN_MAP_BUFFER_ALIGNMENT:
301       return CROCUS_MAP_BUFFER_ALIGNMENT;
302    case PIPE_CAP_SHADER_BUFFER_OFFSET_ALIGNMENT:
303       return devinfo->ver >= 7 ? 4 : 0;
304    case PIPE_CAP_MAX_SHADER_BUFFER_SIZE:
305       return devinfo->ver >= 7 ? (1 << 27) : 0;
306    case PIPE_CAP_TEXTURE_BUFFER_OFFSET_ALIGNMENT:
307       return 16; // XXX: u_screen says 256 is the minimum value...
308    case PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER:
309       return true;
310    case PIPE_CAP_MAX_TEXTURE_BUFFER_SIZE:
311       return CROCUS_MAX_TEXTURE_BUFFER_SIZE;
312    case PIPE_CAP_MAX_VIEWPORTS:
313       return devinfo->ver >= 6 ? 16 : 1;
314    case PIPE_CAP_MAX_GEOMETRY_OUTPUT_VERTICES:
315       return devinfo->ver >= 6 ? 256 : 0;
316    case PIPE_CAP_MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS:
317       return devinfo->ver >= 6 ? 1024 : 0;
318    case PIPE_CAP_MAX_GS_INVOCATIONS:
319       return devinfo->ver >= 7 ? 32 : 1;
320    case PIPE_CAP_MAX_TEXTURE_GATHER_COMPONENTS:
321       if (devinfo->ver >= 7)
322          return 4;
323       else if (devinfo->ver == 6)
324          return 1;
325       else
326          return 0;
327    case PIPE_CAP_MIN_TEXTURE_GATHER_OFFSET:
328       if (devinfo->ver >= 7)
329          return -32;
330       else if (devinfo->ver == 6)
331          return -8;
332       else
333          return 0;
334    case PIPE_CAP_MAX_TEXTURE_GATHER_OFFSET:
335       if (devinfo->ver >= 7)
336          return 31;
337       else if (devinfo->ver == 6)
338          return 7;
339       else
340          return 0;
341    case PIPE_CAP_MAX_VERTEX_STREAMS:
342       return devinfo->ver >= 7 ? 4 : 1;
343    case PIPE_CAP_VENDOR_ID:
344       return 0x8086;
345    case PIPE_CAP_DEVICE_ID:
346       return screen->pci_id;
347    case PIPE_CAP_VIDEO_MEMORY: {
348       /* Once a batch uses more than 75% of the maximum mappable size, we
349        * assume that there's some fragmentation, and we start doing extra
350        * flushing, etc.  That's the big cliff apps will care about.
351        */
352       const unsigned gpu_mappable_megabytes =
353          (screen->aperture_bytes * 3 / 4) / (1024 * 1024);
354 
355       const long system_memory_pages = sysconf(_SC_PHYS_PAGES);
356       const long system_page_size = sysconf(_SC_PAGE_SIZE);
357 
358       if (system_memory_pages <= 0 || system_page_size <= 0)
359          return -1;
360 
361       const uint64_t system_memory_bytes =
362          (uint64_t) system_memory_pages * (uint64_t) system_page_size;
363 
364       const unsigned system_memory_megabytes =
365          (unsigned) (system_memory_bytes / (1024 * 1024));
366 
367       return MIN2(system_memory_megabytes, gpu_mappable_megabytes);
368    }
369    case PIPE_CAP_MAX_SHADER_PATCH_VARYINGS:
370    case PIPE_CAP_MAX_VARYINGS:
371       return (screen->devinfo.ver >= 6) ? 32 : 16;
372    case PIPE_CAP_RESOURCE_FROM_USER_MEMORY:
373       /* AMD_pinned_memory assumes the flexibility of using client memory
374        * for any buffer (incl. vertex buffers) which rules out the prospect
375        * of using snooped buffers, as using snooped buffers without
376        * cogniscience is likely to be detrimental to performance and require
377        * extensive checking in the driver for correctness, e.g. to prevent
378        * illegal snoop <-> snoop transfers.
379        */
380       return devinfo->has_llc;
381    case PIPE_CAP_THROTTLE:
382       return screen->driconf.disable_throttling ? 0 : 1;
383 
384    case PIPE_CAP_CONTEXT_PRIORITY_MASK:
385       return PIPE_CONTEXT_PRIORITY_LOW |
386              PIPE_CONTEXT_PRIORITY_MEDIUM |
387              PIPE_CONTEXT_PRIORITY_HIGH;
388 
389    case PIPE_CAP_FRONTEND_NOOP:
390       return true;
391       // XXX: don't hardcode 00:00:02.0 PCI here
392    case PIPE_CAP_PCI_GROUP:
393       return 0;
394    case PIPE_CAP_PCI_BUS:
395       return 0;
396    case PIPE_CAP_PCI_DEVICE:
397       return 2;
398    case PIPE_CAP_PCI_FUNCTION:
399       return 0;
400 
401    default:
402       return u_pipe_screen_get_param_defaults(pscreen, param);
403    }
404    return 0;
405 }
406 
407 static float
crocus_get_paramf(struct pipe_screen * pscreen,enum pipe_capf param)408 crocus_get_paramf(struct pipe_screen *pscreen, enum pipe_capf param)
409 {
410    struct crocus_screen *screen = (struct crocus_screen *)pscreen;
411    const struct intel_device_info *devinfo = &screen->devinfo;
412 
413    switch (param) {
414    case PIPE_CAPF_MAX_LINE_WIDTH:
415    case PIPE_CAPF_MAX_LINE_WIDTH_AA:
416       if (devinfo->ver >= 6)
417          return 7.375f;
418       else
419          return 7.0f;
420 
421    case PIPE_CAPF_MAX_POINT_WIDTH:
422    case PIPE_CAPF_MAX_POINT_WIDTH_AA:
423       return 255.0f;
424 
425    case PIPE_CAPF_MAX_TEXTURE_ANISOTROPY:
426       return 16.0f;
427    case PIPE_CAPF_MAX_TEXTURE_LOD_BIAS:
428       return 15.0f;
429    case PIPE_CAPF_MIN_CONSERVATIVE_RASTER_DILATE:
430    case PIPE_CAPF_MAX_CONSERVATIVE_RASTER_DILATE:
431    case PIPE_CAPF_CONSERVATIVE_RASTER_DILATE_GRANULARITY:
432       return 0.0f;
433    default:
434       unreachable("unknown param");
435    }
436 }
437 
438 static int
crocus_get_shader_param(struct pipe_screen * pscreen,enum pipe_shader_type p_stage,enum pipe_shader_cap param)439 crocus_get_shader_param(struct pipe_screen *pscreen,
440                         enum pipe_shader_type p_stage,
441                         enum pipe_shader_cap param)
442 {
443    gl_shader_stage stage = stage_from_pipe(p_stage);
444    struct crocus_screen *screen = (struct crocus_screen *)pscreen;
445    const struct intel_device_info *devinfo = &screen->devinfo;
446 
447    if (devinfo->ver < 6 &&
448        p_stage != PIPE_SHADER_VERTEX &&
449        p_stage != PIPE_SHADER_FRAGMENT)
450       return 0;
451 
452    if (devinfo->ver == 6 &&
453        p_stage != PIPE_SHADER_VERTEX &&
454        p_stage != PIPE_SHADER_FRAGMENT &&
455        p_stage != PIPE_SHADER_GEOMETRY)
456       return 0;
457 
458    /* this is probably not totally correct.. but it's a start: */
459    switch (param) {
460    case PIPE_SHADER_CAP_MAX_INSTRUCTIONS:
461       return stage == MESA_SHADER_FRAGMENT ? 1024 : 16384;
462    case PIPE_SHADER_CAP_MAX_ALU_INSTRUCTIONS:
463    case PIPE_SHADER_CAP_MAX_TEX_INSTRUCTIONS:
464    case PIPE_SHADER_CAP_MAX_TEX_INDIRECTIONS:
465       return stage == MESA_SHADER_FRAGMENT ? 1024 : 0;
466 
467    case PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH:
468       return UINT_MAX;
469 
470    case PIPE_SHADER_CAP_MAX_INPUTS:
471       if (stage == MESA_SHADER_VERTEX ||
472           stage == MESA_SHADER_GEOMETRY)
473          return 16; /* Gen7 vec4 geom backend */
474       return 32;
475    case PIPE_SHADER_CAP_MAX_OUTPUTS:
476       return 32;
477    case PIPE_SHADER_CAP_MAX_CONST_BUFFER_SIZE:
478       return 16 * 1024 * sizeof(float);
479    case PIPE_SHADER_CAP_MAX_CONST_BUFFERS:
480       return devinfo->ver >= 6 ? 16 : 1;
481    case PIPE_SHADER_CAP_MAX_TEMPS:
482       return 256; /* GL_MAX_PROGRAM_TEMPORARIES_ARB */
483    case PIPE_SHADER_CAP_TGSI_CONT_SUPPORTED:
484       return 0;
485    case PIPE_SHADER_CAP_INDIRECT_INPUT_ADDR:
486    case PIPE_SHADER_CAP_INDIRECT_OUTPUT_ADDR:
487    case PIPE_SHADER_CAP_INDIRECT_TEMP_ADDR:
488    case PIPE_SHADER_CAP_INDIRECT_CONST_ADDR:
489       /* Lie about these to avoid st/mesa's GLSL IR lowering of indirects,
490        * which we don't want.  Our compiler backend will check brw_compiler's
491        * options and call nir_lower_indirect_derefs appropriately anyway.
492        */
493       return true;
494    case PIPE_SHADER_CAP_SUBROUTINES:
495       return 0;
496    case PIPE_SHADER_CAP_INTEGERS:
497       return 1;
498    case PIPE_SHADER_CAP_INT64_ATOMICS:
499    case PIPE_SHADER_CAP_FP16:
500       return 0;
501    case PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS:
502    case PIPE_SHADER_CAP_MAX_SAMPLER_VIEWS:
503       return (devinfo->verx10 >= 75) ? CROCUS_MAX_TEXTURE_SAMPLERS : 16;
504    case PIPE_SHADER_CAP_MAX_SHADER_IMAGES:
505       if (devinfo->ver >= 7 &&
506           (p_stage == PIPE_SHADER_FRAGMENT ||
507            p_stage == PIPE_SHADER_COMPUTE))
508          return CROCUS_MAX_TEXTURE_SAMPLERS;
509       return 0;
510    case PIPE_SHADER_CAP_MAX_SHADER_BUFFERS:
511       return devinfo->ver >= 7 ? (CROCUS_MAX_ABOS + CROCUS_MAX_SSBOS) : 0;
512    case PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTERS:
513    case PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTER_BUFFERS:
514       return 0;
515    case PIPE_SHADER_CAP_PREFERRED_IR:
516       return PIPE_SHADER_IR_NIR;
517    case PIPE_SHADER_CAP_SUPPORTED_IRS:
518       return 1 << PIPE_SHADER_IR_NIR;
519    case PIPE_SHADER_CAP_TGSI_DROUND_SUPPORTED:
520    case PIPE_SHADER_CAP_TGSI_LDEXP_SUPPORTED:
521       return 1;
522    case PIPE_SHADER_CAP_LOWER_IF_THRESHOLD:
523    case PIPE_SHADER_CAP_TGSI_SKIP_MERGE_REGISTERS:
524    case PIPE_SHADER_CAP_TGSI_DFRACEXP_DLDEXP_SUPPORTED:
525    case PIPE_SHADER_CAP_TGSI_FMA_SUPPORTED:
526    case PIPE_SHADER_CAP_TGSI_ANY_INOUT_DECL_RANGE:
527    case PIPE_SHADER_CAP_TGSI_SQRT_SUPPORTED:
528    case PIPE_SHADER_CAP_MAX_UNROLL_ITERATIONS_HINT:
529    case PIPE_SHADER_CAP_FP16_DERIVATIVES:
530    case PIPE_SHADER_CAP_INT16:
531    case PIPE_SHADER_CAP_GLSL_16BIT_CONSTS:
532    case PIPE_SHADER_CAP_FP16_CONST_BUFFERS:
533       return 0;
534    default:
535       unreachable("unknown shader param");
536    }
537 }
538 
539 static int
crocus_get_compute_param(struct pipe_screen * pscreen,enum pipe_shader_ir ir_type,enum pipe_compute_cap param,void * ret)540 crocus_get_compute_param(struct pipe_screen *pscreen,
541                          enum pipe_shader_ir ir_type,
542                          enum pipe_compute_cap param,
543                          void *ret)
544 {
545    struct crocus_screen *screen = (struct crocus_screen *)pscreen;
546    const struct intel_device_info *devinfo = &screen->devinfo;
547 
548    const uint32_t max_invocations = 32 * devinfo->max_cs_workgroup_threads;
549 
550    if (devinfo->ver < 7)
551       return 0;
552 #define RET(x) do {                  \
553    if (ret)                          \
554       memcpy(ret, x, sizeof(x));     \
555    return sizeof(x);                 \
556 } while (0)
557 
558    switch (param) {
559    case PIPE_COMPUTE_CAP_ADDRESS_BITS:
560       RET((uint32_t []){ 32 });
561 
562    case PIPE_COMPUTE_CAP_IR_TARGET:
563       if (ret)
564          strcpy(ret, "gen");
565       return 4;
566 
567    case PIPE_COMPUTE_CAP_GRID_DIMENSION:
568       RET((uint64_t []) { 3 });
569 
570    case PIPE_COMPUTE_CAP_MAX_GRID_SIZE:
571       RET(((uint64_t []) { 65535, 65535, 65535 }));
572 
573    case PIPE_COMPUTE_CAP_MAX_BLOCK_SIZE:
574       /* MaxComputeWorkGroupSize[0..2] */
575       RET(((uint64_t []) {max_invocations, max_invocations, max_invocations}));
576 
577    case PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK:
578       /* MaxComputeWorkGroupInvocations */
579       RET((uint64_t []) { max_invocations });
580 
581    case PIPE_COMPUTE_CAP_MAX_LOCAL_SIZE:
582       /* MaxComputeSharedMemorySize */
583       RET((uint64_t []) { 64 * 1024 });
584 
585    case PIPE_COMPUTE_CAP_IMAGES_SUPPORTED:
586       RET((uint32_t []) { 1 });
587 
588    case PIPE_COMPUTE_CAP_SUBGROUP_SIZE:
589       RET((uint32_t []) { BRW_SUBGROUP_SIZE });
590 
591    case PIPE_COMPUTE_CAP_MAX_VARIABLE_THREADS_PER_BLOCK:
592       RET((uint64_t []) { max_invocations });
593 
594    case PIPE_COMPUTE_CAP_MAX_MEM_ALLOC_SIZE:
595    case PIPE_COMPUTE_CAP_MAX_CLOCK_FREQUENCY:
596    case PIPE_COMPUTE_CAP_MAX_COMPUTE_UNITS:
597    case PIPE_COMPUTE_CAP_MAX_GLOBAL_SIZE:
598    case PIPE_COMPUTE_CAP_MAX_PRIVATE_SIZE:
599    case PIPE_COMPUTE_CAP_MAX_INPUT_SIZE:
600 
601       // XXX: I think these are for Clover...
602       return 0;
603 
604    default:
605       unreachable("unknown compute param");
606    }
607 }
608 
609 static uint64_t
crocus_get_timestamp(struct pipe_screen * pscreen)610 crocus_get_timestamp(struct pipe_screen *pscreen)
611 {
612    struct crocus_screen *screen = (struct crocus_screen *) pscreen;
613    const unsigned TIMESTAMP = 0x2358;
614    uint64_t result;
615 
616    crocus_reg_read(screen->bufmgr, TIMESTAMP | 1, &result);
617 
618    result = intel_device_info_timebase_scale(&screen->devinfo, result);
619    result &= (1ull << TIMESTAMP_BITS) - 1;
620 
621    return result;
622 }
623 
624 void
crocus_screen_destroy(struct crocus_screen * screen)625 crocus_screen_destroy(struct crocus_screen *screen)
626 {
627    u_transfer_helper_destroy(screen->base.transfer_helper);
628    crocus_bufmgr_unref(screen->bufmgr);
629    disk_cache_destroy(screen->disk_cache);
630    close(screen->winsys_fd);
631    ralloc_free(screen);
632 }
633 
634 static void
crocus_screen_unref(struct pipe_screen * pscreen)635 crocus_screen_unref(struct pipe_screen *pscreen)
636 {
637    crocus_pscreen_unref(pscreen);
638 }
639 
640 static void
crocus_query_memory_info(struct pipe_screen * pscreen,struct pipe_memory_info * info)641 crocus_query_memory_info(struct pipe_screen *pscreen,
642                          struct pipe_memory_info *info)
643 {
644 }
645 
646 static const void *
crocus_get_compiler_options(struct pipe_screen * pscreen,enum pipe_shader_ir ir,enum pipe_shader_type pstage)647 crocus_get_compiler_options(struct pipe_screen *pscreen,
648                             enum pipe_shader_ir ir,
649                             enum pipe_shader_type pstage)
650 {
651    struct crocus_screen *screen = (struct crocus_screen *) pscreen;
652    gl_shader_stage stage = stage_from_pipe(pstage);
653    assert(ir == PIPE_SHADER_IR_NIR);
654 
655    return screen->compiler->glsl_compiler_options[stage].NirOptions;
656 }
657 
658 static struct disk_cache *
crocus_get_disk_shader_cache(struct pipe_screen * pscreen)659 crocus_get_disk_shader_cache(struct pipe_screen *pscreen)
660 {
661    struct crocus_screen *screen = (struct crocus_screen *) pscreen;
662    return screen->disk_cache;
663 }
664 
665 static const struct intel_l3_config *
crocus_get_default_l3_config(const struct intel_device_info * devinfo,bool compute)666 crocus_get_default_l3_config(const struct intel_device_info *devinfo,
667                              bool compute)
668 {
669    bool wants_dc_cache = true;
670    bool has_slm = compute;
671    const struct intel_l3_weights w =
672       intel_get_default_l3_weights(devinfo, wants_dc_cache, has_slm);
673    return intel_get_l3_config(devinfo, w);
674 }
675 
676 static void
crocus_shader_debug_log(void * data,unsigned * id,const char * fmt,...)677 crocus_shader_debug_log(void *data, unsigned *id, const char *fmt, ...)
678 {
679    struct pipe_debug_callback *dbg = data;
680    va_list args;
681 
682    if (!dbg->debug_message)
683       return;
684 
685    va_start(args, fmt);
686    dbg->debug_message(dbg->data, id, PIPE_DEBUG_TYPE_SHADER_INFO, fmt, args);
687    va_end(args);
688 }
689 
690 static void
crocus_shader_perf_log(void * data,unsigned * id,const char * fmt,...)691 crocus_shader_perf_log(void *data, unsigned *id, const char *fmt, ...)
692 {
693    struct pipe_debug_callback *dbg = data;
694    va_list args;
695    va_start(args, fmt);
696 
697    if (INTEL_DEBUG(DEBUG_PERF)) {
698       va_list args_copy;
699       va_copy(args_copy, args);
700       vfprintf(stderr, fmt, args_copy);
701       va_end(args_copy);
702    }
703 
704    if (dbg->debug_message) {
705       dbg->debug_message(dbg->data, id, PIPE_DEBUG_TYPE_PERF_INFO, fmt, args);
706    }
707 
708    va_end(args);
709 }
710 
711 static bool
crocus_detect_swizzling(struct crocus_screen * screen)712 crocus_detect_swizzling(struct crocus_screen *screen)
713 {
714    /* Broadwell PRM says:
715     *
716     *   "Before Gen8, there was a historical configuration control field to
717     *    swizzle address bit[6] for in X/Y tiling modes. This was set in three
718     *    different places: TILECTL[1:0], ARB_MODE[5:4], and
719     *    DISP_ARB_CTL[14:13].
720     *
721     *    For Gen8 and subsequent generations, the swizzle fields are all
722     *    reserved, and the CPU's memory controller performs all address
723     *    swizzling modifications."
724     */
725    uint32_t tiling = I915_TILING_X;
726    uint32_t swizzle_mode = 0;
727    struct crocus_bo *buffer =
728       crocus_bo_alloc_tiled(screen->bufmgr, "swizzle test", 32768,
729                             0, tiling, 512, 0);
730    if (buffer == NULL)
731       return false;
732 
733    crocus_bo_get_tiling(buffer, &tiling, &swizzle_mode);
734    crocus_bo_unreference(buffer);
735 
736    return swizzle_mode != I915_BIT_6_SWIZZLE_NONE;
737 }
738 
739 struct pipe_screen *
crocus_screen_create(int fd,const struct pipe_screen_config * config)740 crocus_screen_create(int fd, const struct pipe_screen_config *config)
741 {
742    struct crocus_screen *screen = rzalloc(NULL, struct crocus_screen);
743    if (!screen)
744       return NULL;
745 
746    if (!intel_get_device_info_from_fd(fd, &screen->devinfo))
747       return NULL;
748    screen->pci_id = screen->devinfo.chipset_id;
749 
750    if (screen->devinfo.ver > 8)
751       return NULL;
752 
753    if (screen->devinfo.ver == 8) {
754       /* bind to cherryview or bdw if forced */
755       if (!screen->devinfo.is_cherryview &&
756           !getenv("CROCUS_GEN8"))
757          return NULL;
758    }
759 
760    p_atomic_set(&screen->refcount, 1);
761 
762    screen->aperture_bytes = get_aperture_size(fd);
763 
764    driParseConfigFiles(config->options, config->options_info, 0, "crocus",
765                        NULL, NULL, NULL, 0, NULL, 0);
766 
767    bool bo_reuse = false;
768    int bo_reuse_mode = driQueryOptioni(config->options, "bo_reuse");
769    switch (bo_reuse_mode) {
770    case DRI_CONF_BO_REUSE_DISABLED:
771       break;
772    case DRI_CONF_BO_REUSE_ALL:
773       bo_reuse = true;
774       break;
775    }
776 
777    screen->bufmgr = crocus_bufmgr_get_for_fd(&screen->devinfo, fd, bo_reuse);
778    if (!screen->bufmgr)
779       return NULL;
780    screen->fd = crocus_bufmgr_get_fd(screen->bufmgr);
781    screen->winsys_fd = fd;
782 
783    screen->has_swizzling = crocus_detect_swizzling(screen);
784    brw_process_intel_debug_variable();
785 
786    screen->driconf.dual_color_blend_by_location =
787       driQueryOptionb(config->options, "dual_color_blend_by_location");
788    screen->driconf.disable_throttling =
789       driQueryOptionb(config->options, "disable_throttling");
790    screen->driconf.always_flush_cache =
791       driQueryOptionb(config->options, "always_flush_cache");
792 
793    screen->precompile = env_var_as_boolean("shader_precompile", true);
794 
795    isl_device_init(&screen->isl_dev, &screen->devinfo,
796                    screen->has_swizzling);
797 
798    screen->compiler = brw_compiler_create(screen, &screen->devinfo);
799    screen->compiler->shader_debug_log = crocus_shader_debug_log;
800    screen->compiler->shader_perf_log = crocus_shader_perf_log;
801    screen->compiler->supports_pull_constants = false;
802    screen->compiler->supports_shader_constants = false;
803    screen->compiler->compact_params = false;
804    screen->compiler->constant_buffer_0_is_relative = true;
805 
806    if (screen->devinfo.ver >= 7) {
807       screen->l3_config_3d = crocus_get_default_l3_config(&screen->devinfo, false);
808       screen->l3_config_cs = crocus_get_default_l3_config(&screen->devinfo, true);
809    }
810 
811    crocus_disk_cache_init(screen);
812 
813    slab_create_parent(&screen->transfer_pool,
814                       sizeof(struct crocus_transfer), 64);
815 
816    struct pipe_screen *pscreen = &screen->base;
817 
818    crocus_init_screen_fence_functions(pscreen);
819    crocus_init_screen_resource_functions(pscreen);
820 
821    pscreen->destroy = crocus_screen_unref;
822    pscreen->get_name = crocus_get_name;
823    pscreen->get_vendor = crocus_get_vendor;
824    pscreen->get_device_vendor = crocus_get_device_vendor;
825    pscreen->get_param = crocus_get_param;
826    pscreen->get_shader_param = crocus_get_shader_param;
827    pscreen->get_compute_param = crocus_get_compute_param;
828    pscreen->get_paramf = crocus_get_paramf;
829    pscreen->get_compiler_options = crocus_get_compiler_options;
830    pscreen->get_device_uuid = crocus_get_device_uuid;
831    pscreen->get_driver_uuid = crocus_get_driver_uuid;
832    pscreen->get_disk_shader_cache = crocus_get_disk_shader_cache;
833    pscreen->is_format_supported = crocus_is_format_supported;
834    pscreen->context_create = crocus_create_context;
835    pscreen->flush_frontbuffer = crocus_flush_frontbuffer;
836    pscreen->get_timestamp = crocus_get_timestamp;
837    pscreen->query_memory_info = crocus_query_memory_info;
838    pscreen->get_driver_query_group_info = crocus_get_monitor_group_info;
839    pscreen->get_driver_query_info = crocus_get_monitor_info;
840 
841    genX_call(&screen->devinfo, crocus_init_screen_state, screen);
842    genX_call(&screen->devinfo, crocus_init_screen_query, screen);
843    return pscreen;
844 }
845