dri: Add param driCreateConfigs(mutable_render_buffer)
[mesa.git] / src / mesa / drivers / dri / i965 / intel_screen.c
1 /*
2 * Copyright 2003 VMware, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include <drm_fourcc.h>
27 #include <errno.h>
28 #include <time.h>
29 #include <unistd.h>
30 #include "main/context.h"
31 #include "main/framebuffer.h"
32 #include "main/renderbuffer.h"
33 #include "main/texobj.h"
34 #include "main/hash.h"
35 #include "main/fbobject.h"
36 #include "main/version.h"
37 #include "swrast/s_renderbuffer.h"
38 #include "util/ralloc.h"
39 #include "util/disk_cache.h"
40 #include "brw_defines.h"
41 #include "brw_state.h"
42 #include "compiler/nir/nir.h"
43
44 #include "utils.h"
45 #include "util/disk_cache.h"
46 #include "util/xmlpool.h"
47
48 #include "common/gen_defines.h"
49
50 static const __DRIconfigOptionsExtension brw_config_options = {
51 .base = { __DRI_CONFIG_OPTIONS, 1 },
52 .xml =
53 DRI_CONF_BEGIN
54 DRI_CONF_SECTION_PERFORMANCE
55 /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
56 * DRI_CONF_BO_REUSE_ALL
57 */
58 DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 1, "0:1")
59 DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
60 DRI_CONF_ENUM(0, "Disable buffer object reuse")
61 DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
62 DRI_CONF_DESC_END
63 DRI_CONF_OPT_END
64 DRI_CONF_MESA_NO_ERROR("false")
65 DRI_CONF_SECTION_END
66
67 DRI_CONF_SECTION_QUALITY
68 DRI_CONF_PRECISE_TRIG("false")
69
70 DRI_CONF_OPT_BEGIN(clamp_max_samples, int, -1)
71 DRI_CONF_DESC(en, "Clamp the value of GL_MAX_SAMPLES to the "
72 "given integer. If negative, then do not clamp.")
73 DRI_CONF_OPT_END
74 DRI_CONF_SECTION_END
75
76 DRI_CONF_SECTION_DEBUG
77 DRI_CONF_NO_RAST("false")
78 DRI_CONF_ALWAYS_FLUSH_BATCH("false")
79 DRI_CONF_ALWAYS_FLUSH_CACHE("false")
80 DRI_CONF_DISABLE_THROTTLING("false")
81 DRI_CONF_FORCE_GLSL_EXTENSIONS_WARN("false")
82 DRI_CONF_FORCE_GLSL_VERSION(0)
83 DRI_CONF_DISABLE_GLSL_LINE_CONTINUATIONS("false")
84 DRI_CONF_DISABLE_BLEND_FUNC_EXTENDED("false")
85 DRI_CONF_DUAL_COLOR_BLEND_BY_LOCATION("false")
86 DRI_CONF_ALLOW_GLSL_EXTENSION_DIRECTIVE_MIDSHADER("false")
87 DRI_CONF_ALLOW_GLSL_BUILTIN_VARIABLE_REDECLARATION("false")
88 DRI_CONF_ALLOW_GLSL_CROSS_STAGE_INTERPOLATION_MISMATCH("false")
89 DRI_CONF_ALLOW_HIGHER_COMPAT_VERSION("false")
90 DRI_CONF_FORCE_GLSL_ABS_SQRT("false")
91
92 DRI_CONF_OPT_BEGIN_B(shader_precompile, "true")
93 DRI_CONF_DESC(en, "Perform code generation at shader link time.")
94 DRI_CONF_OPT_END
95 DRI_CONF_SECTION_END
96
97 DRI_CONF_SECTION_MISCELLANEOUS
98 DRI_CONF_GLSL_ZERO_INIT("false")
99 DRI_CONF_ALLOW_RGB10_CONFIGS("false")
100 DRI_CONF_SECTION_END
101 DRI_CONF_END
102 };
103
104 #include "intel_batchbuffer.h"
105 #include "intel_buffers.h"
106 #include "brw_bufmgr.h"
107 #include "intel_fbo.h"
108 #include "intel_mipmap_tree.h"
109 #include "intel_screen.h"
110 #include "intel_tex.h"
111 #include "intel_image.h"
112
113 #include "brw_context.h"
114
115 #include "i915_drm.h"
116
117 /**
118 * For debugging purposes, this returns a time in seconds.
119 */
120 double
121 get_time(void)
122 {
123 struct timespec tp;
124
125 clock_gettime(CLOCK_MONOTONIC, &tp);
126
127 return tp.tv_sec + tp.tv_nsec / 1000000000.0;
128 }
129
130 static const __DRItexBufferExtension intelTexBufferExtension = {
131 .base = { __DRI_TEX_BUFFER, 3 },
132
133 .setTexBuffer = intelSetTexBuffer,
134 .setTexBuffer2 = intelSetTexBuffer2,
135 .releaseTexBuffer = intelReleaseTexBuffer,
136 };
137
138 static void
139 intel_dri2_flush_with_flags(__DRIcontext *cPriv,
140 __DRIdrawable *dPriv,
141 unsigned flags,
142 enum __DRI2throttleReason reason)
143 {
144 struct brw_context *brw = cPriv->driverPrivate;
145
146 if (!brw)
147 return;
148
149 struct gl_context *ctx = &brw->ctx;
150
151 FLUSH_VERTICES(ctx, 0);
152
153 if (flags & __DRI2_FLUSH_DRAWABLE)
154 intel_resolve_for_dri2_flush(brw, dPriv);
155
156 if (reason == __DRI2_THROTTLE_SWAPBUFFER)
157 brw->need_swap_throttle = true;
158 if (reason == __DRI2_THROTTLE_FLUSHFRONT)
159 brw->need_flush_throttle = true;
160
161 intel_batchbuffer_flush(brw);
162 }
163
164 /**
165 * Provides compatibility with loaders that only support the older (version
166 * 1-3) flush interface.
167 *
168 * That includes libGL up to Mesa 9.0, and the X Server at least up to 1.13.
169 */
170 static void
171 intel_dri2_flush(__DRIdrawable *drawable)
172 {
173 intel_dri2_flush_with_flags(drawable->driContextPriv, drawable,
174 __DRI2_FLUSH_DRAWABLE,
175 __DRI2_THROTTLE_SWAPBUFFER);
176 }
177
178 static const struct __DRI2flushExtensionRec intelFlushExtension = {
179 .base = { __DRI2_FLUSH, 4 },
180
181 .flush = intel_dri2_flush,
182 .invalidate = dri2InvalidateDrawable,
183 .flush_with_flags = intel_dri2_flush_with_flags,
184 };
185
186 static const struct intel_image_format intel_image_formats[] = {
187 { __DRI_IMAGE_FOURCC_ARGB2101010, __DRI_IMAGE_COMPONENTS_RGBA, 1,
188 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB2101010, 4 } } },
189
190 { __DRI_IMAGE_FOURCC_XRGB2101010, __DRI_IMAGE_COMPONENTS_RGB, 1,
191 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB2101010, 4 } } },
192
193 { __DRI_IMAGE_FOURCC_ABGR2101010, __DRI_IMAGE_COMPONENTS_RGBA, 1,
194 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR2101010, 4 } } },
195
196 { __DRI_IMAGE_FOURCC_XBGR2101010, __DRI_IMAGE_COMPONENTS_RGB, 1,
197 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR2101010, 4 } } },
198
199 { __DRI_IMAGE_FOURCC_ARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
200 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } },
201
202 { __DRI_IMAGE_FOURCC_ABGR8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
203 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR8888, 4 } } },
204
205 { __DRI_IMAGE_FOURCC_SARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
206 { { 0, 0, 0, __DRI_IMAGE_FORMAT_SARGB8, 4 } } },
207
208 { __DRI_IMAGE_FOURCC_XRGB8888, __DRI_IMAGE_COMPONENTS_RGB, 1,
209 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB8888, 4 }, } },
210
211 { __DRI_IMAGE_FOURCC_XBGR8888, __DRI_IMAGE_COMPONENTS_RGB, 1,
212 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR8888, 4 }, } },
213
214 { __DRI_IMAGE_FOURCC_ARGB1555, __DRI_IMAGE_COMPONENTS_RGBA, 1,
215 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB1555, 2 } } },
216
217 { __DRI_IMAGE_FOURCC_RGB565, __DRI_IMAGE_COMPONENTS_RGB, 1,
218 { { 0, 0, 0, __DRI_IMAGE_FORMAT_RGB565, 2 } } },
219
220 { __DRI_IMAGE_FOURCC_R8, __DRI_IMAGE_COMPONENTS_R, 1,
221 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 }, } },
222
223 { __DRI_IMAGE_FOURCC_R16, __DRI_IMAGE_COMPONENTS_R, 1,
224 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R16, 1 }, } },
225
226 { __DRI_IMAGE_FOURCC_GR88, __DRI_IMAGE_COMPONENTS_RG, 1,
227 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 }, } },
228
229 { __DRI_IMAGE_FOURCC_GR1616, __DRI_IMAGE_COMPONENTS_RG, 1,
230 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR1616, 2 }, } },
231
232 { __DRI_IMAGE_FOURCC_YUV410, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
233 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
234 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 },
235 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 } } },
236
237 { __DRI_IMAGE_FOURCC_YUV411, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
238 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
239 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 },
240 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
241
242 { __DRI_IMAGE_FOURCC_YUV420, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
243 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
244 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 },
245 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 } } },
246
247 { __DRI_IMAGE_FOURCC_YUV422, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
248 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
249 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 },
250 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
251
252 { __DRI_IMAGE_FOURCC_YUV444, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
253 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
254 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
255 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
256
257 { __DRI_IMAGE_FOURCC_YVU410, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
258 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
259 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 },
260 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 } } },
261
262 { __DRI_IMAGE_FOURCC_YVU411, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
263 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
264 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 },
265 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
266
267 { __DRI_IMAGE_FOURCC_YVU420, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
268 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
269 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 },
270 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 } } },
271
272 { __DRI_IMAGE_FOURCC_YVU422, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
273 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
274 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 },
275 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
276
277 { __DRI_IMAGE_FOURCC_YVU444, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
278 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
279 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
280 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
281
282 { __DRI_IMAGE_FOURCC_NV12, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
283 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
284 { 1, 1, 1, __DRI_IMAGE_FORMAT_GR88, 2 } } },
285
286 { __DRI_IMAGE_FOURCC_NV16, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
287 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
288 { 1, 1, 0, __DRI_IMAGE_FORMAT_GR88, 2 } } },
289
290 /* For YUYV and UYVY buffers, we set up two overlapping DRI images
291 * and treat them as planar buffers in the compositors.
292 * Plane 0 is GR88 and samples YU or YV pairs and places Y into
293 * the R component, while plane 1 is ARGB/ABGR and samples YUYV/UYVY
294 * clusters and places pairs and places U into the G component and
295 * V into A. This lets the texture sampler interpolate the Y
296 * components correctly when sampling from plane 0, and interpolate
297 * U and V correctly when sampling from plane 1. */
298 { __DRI_IMAGE_FOURCC_YUYV, __DRI_IMAGE_COMPONENTS_Y_XUXV, 2,
299 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 },
300 { 0, 1, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } },
301 { __DRI_IMAGE_FOURCC_UYVY, __DRI_IMAGE_COMPONENTS_Y_UXVX, 2,
302 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 },
303 { 0, 1, 0, __DRI_IMAGE_FORMAT_ABGR8888, 4 } } }
304 };
305
306 static const struct {
307 uint64_t modifier;
308 unsigned since_gen;
309 } supported_modifiers[] = {
310 { .modifier = DRM_FORMAT_MOD_LINEAR , .since_gen = 1 },
311 { .modifier = I915_FORMAT_MOD_X_TILED , .since_gen = 1 },
312 { .modifier = I915_FORMAT_MOD_Y_TILED , .since_gen = 6 },
313 { .modifier = I915_FORMAT_MOD_Y_TILED_CCS , .since_gen = 9 },
314 };
315
316 static bool
317 modifier_is_supported(const struct gen_device_info *devinfo,
318 const struct intel_image_format *fmt, int dri_format,
319 uint64_t modifier)
320 {
321 const struct isl_drm_modifier_info *modinfo =
322 isl_drm_modifier_get_info(modifier);
323 int i;
324
325 /* ISL had better know about the modifier */
326 if (!modinfo)
327 return false;
328
329 if (modinfo->aux_usage == ISL_AUX_USAGE_CCS_E) {
330 /* If INTEL_DEBUG=norbc is set, don't support any CCS_E modifiers */
331 if (unlikely(INTEL_DEBUG & DEBUG_NO_RBC))
332 return false;
333
334 /* CCS_E is not supported for planar images */
335 if (fmt && fmt->nplanes > 1)
336 return false;
337
338 if (fmt) {
339 assert(dri_format == 0);
340 dri_format = fmt->planes[0].dri_format;
341 }
342
343 mesa_format format = driImageFormatToGLFormat(dri_format);
344 /* Whether or not we support compression is based on the RGBA non-sRGB
345 * version of the format.
346 */
347 format = _mesa_format_fallback_rgbx_to_rgba(format);
348 format = _mesa_get_srgb_format_linear(format);
349 if (!isl_format_supports_ccs_e(devinfo,
350 brw_isl_format_for_mesa_format(format)))
351 return false;
352 }
353
354 for (i = 0; i < ARRAY_SIZE(supported_modifiers); i++) {
355 if (supported_modifiers[i].modifier != modifier)
356 continue;
357
358 return supported_modifiers[i].since_gen <= devinfo->gen;
359 }
360
361 return false;
362 }
363
364 static uint64_t
365 tiling_to_modifier(uint32_t tiling)
366 {
367 static const uint64_t map[] = {
368 [I915_TILING_NONE] = DRM_FORMAT_MOD_LINEAR,
369 [I915_TILING_X] = I915_FORMAT_MOD_X_TILED,
370 [I915_TILING_Y] = I915_FORMAT_MOD_Y_TILED,
371 };
372
373 assert(tiling < ARRAY_SIZE(map));
374
375 return map[tiling];
376 }
377
378 static void
379 intel_image_warn_if_unaligned(__DRIimage *image, const char *func)
380 {
381 uint32_t tiling, swizzle;
382 brw_bo_get_tiling(image->bo, &tiling, &swizzle);
383
384 if (tiling != I915_TILING_NONE && (image->offset & 0xfff)) {
385 _mesa_warning(NULL, "%s: offset 0x%08x not on tile boundary",
386 func, image->offset);
387 }
388 }
389
390 static const struct intel_image_format *
391 intel_image_format_lookup(int fourcc)
392 {
393 for (unsigned i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
394 if (intel_image_formats[i].fourcc == fourcc)
395 return &intel_image_formats[i];
396 }
397
398 return NULL;
399 }
400
401 static boolean
402 intel_image_get_fourcc(__DRIimage *image, int *fourcc)
403 {
404 if (image->planar_format) {
405 *fourcc = image->planar_format->fourcc;
406 return true;
407 }
408
409 for (unsigned i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
410 if (intel_image_formats[i].planes[0].dri_format == image->dri_format) {
411 *fourcc = intel_image_formats[i].fourcc;
412 return true;
413 }
414 }
415 return false;
416 }
417
418 static __DRIimage *
419 intel_allocate_image(struct intel_screen *screen, int dri_format,
420 void *loaderPrivate)
421 {
422 __DRIimage *image;
423
424 image = calloc(1, sizeof *image);
425 if (image == NULL)
426 return NULL;
427
428 image->screen = screen;
429 image->dri_format = dri_format;
430 image->offset = 0;
431
432 image->format = driImageFormatToGLFormat(dri_format);
433 if (dri_format != __DRI_IMAGE_FORMAT_NONE &&
434 image->format == MESA_FORMAT_NONE) {
435 free(image);
436 return NULL;
437 }
438
439 image->internal_format = _mesa_get_format_base_format(image->format);
440 image->data = loaderPrivate;
441
442 return image;
443 }
444
445 /**
446 * Sets up a DRIImage structure to point to a slice out of a miptree.
447 */
448 static void
449 intel_setup_image_from_mipmap_tree(struct brw_context *brw, __DRIimage *image,
450 struct intel_mipmap_tree *mt, GLuint level,
451 GLuint zoffset)
452 {
453 intel_miptree_make_shareable(brw, mt);
454
455 intel_miptree_check_level_layer(mt, level, zoffset);
456
457 image->width = minify(mt->surf.phys_level0_sa.width,
458 level - mt->first_level);
459 image->height = minify(mt->surf.phys_level0_sa.height,
460 level - mt->first_level);
461 image->pitch = mt->surf.row_pitch;
462
463 image->offset = intel_miptree_get_tile_offsets(mt, level, zoffset,
464 &image->tile_x,
465 &image->tile_y);
466
467 brw_bo_unreference(image->bo);
468 image->bo = mt->bo;
469 brw_bo_reference(mt->bo);
470 }
471
472 static __DRIimage *
473 intel_create_image_from_name(__DRIscreen *dri_screen,
474 int width, int height, int format,
475 int name, int pitch, void *loaderPrivate)
476 {
477 struct intel_screen *screen = dri_screen->driverPrivate;
478 __DRIimage *image;
479 int cpp;
480
481 image = intel_allocate_image(screen, format, loaderPrivate);
482 if (image == NULL)
483 return NULL;
484
485 if (image->format == MESA_FORMAT_NONE)
486 cpp = 1;
487 else
488 cpp = _mesa_get_format_bytes(image->format);
489
490 image->width = width;
491 image->height = height;
492 image->pitch = pitch * cpp;
493 image->bo = brw_bo_gem_create_from_name(screen->bufmgr, "image",
494 name);
495 if (!image->bo) {
496 free(image);
497 return NULL;
498 }
499 image->modifier = tiling_to_modifier(image->bo->tiling_mode);
500
501 return image;
502 }
503
504 static __DRIimage *
505 intel_create_image_from_renderbuffer(__DRIcontext *context,
506 int renderbuffer, void *loaderPrivate)
507 {
508 __DRIimage *image;
509 struct brw_context *brw = context->driverPrivate;
510 struct gl_context *ctx = &brw->ctx;
511 struct gl_renderbuffer *rb;
512 struct intel_renderbuffer *irb;
513
514 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
515 if (!rb) {
516 _mesa_error(ctx, GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
517 return NULL;
518 }
519
520 irb = intel_renderbuffer(rb);
521 intel_miptree_make_shareable(brw, irb->mt);
522 image = calloc(1, sizeof *image);
523 if (image == NULL)
524 return NULL;
525
526 image->internal_format = rb->InternalFormat;
527 image->format = rb->Format;
528 image->modifier = tiling_to_modifier(
529 isl_tiling_to_i915_tiling(irb->mt->surf.tiling));
530 image->offset = 0;
531 image->data = loaderPrivate;
532 brw_bo_unreference(image->bo);
533 image->bo = irb->mt->bo;
534 brw_bo_reference(irb->mt->bo);
535 image->width = rb->Width;
536 image->height = rb->Height;
537 image->pitch = irb->mt->surf.row_pitch;
538 image->dri_format = driGLFormatToImageFormat(image->format);
539 image->has_depthstencil = irb->mt->stencil_mt? true : false;
540
541 rb->NeedsFinishRenderTexture = true;
542 return image;
543 }
544
545 static __DRIimage *
546 intel_create_image_from_texture(__DRIcontext *context, int target,
547 unsigned texture, int zoffset,
548 int level,
549 unsigned *error,
550 void *loaderPrivate)
551 {
552 __DRIimage *image;
553 struct brw_context *brw = context->driverPrivate;
554 struct gl_texture_object *obj;
555 struct intel_texture_object *iobj;
556 GLuint face = 0;
557
558 obj = _mesa_lookup_texture(&brw->ctx, texture);
559 if (!obj || obj->Target != target) {
560 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
561 return NULL;
562 }
563
564 if (target == GL_TEXTURE_CUBE_MAP)
565 face = zoffset;
566
567 _mesa_test_texobj_completeness(&brw->ctx, obj);
568 iobj = intel_texture_object(obj);
569 if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
570 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
571 return NULL;
572 }
573
574 if (level < obj->BaseLevel || level > obj->_MaxLevel) {
575 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
576 return NULL;
577 }
578
579 if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < zoffset) {
580 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
581 return NULL;
582 }
583 image = calloc(1, sizeof *image);
584 if (image == NULL) {
585 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
586 return NULL;
587 }
588
589 image->internal_format = obj->Image[face][level]->InternalFormat;
590 image->format = obj->Image[face][level]->TexFormat;
591 image->modifier = tiling_to_modifier(
592 isl_tiling_to_i915_tiling(iobj->mt->surf.tiling));
593 image->data = loaderPrivate;
594 intel_setup_image_from_mipmap_tree(brw, image, iobj->mt, level, zoffset);
595 image->dri_format = driGLFormatToImageFormat(image->format);
596 image->has_depthstencil = iobj->mt->stencil_mt? true : false;
597 image->planar_format = iobj->planar_format;
598 if (image->dri_format == __DRI_IMAGE_FORMAT_NONE) {
599 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
600 free(image);
601 return NULL;
602 }
603
604 *error = __DRI_IMAGE_ERROR_SUCCESS;
605 return image;
606 }
607
608 static void
609 intel_destroy_image(__DRIimage *image)
610 {
611 brw_bo_unreference(image->bo);
612 free(image);
613 }
614
615 enum modifier_priority {
616 MODIFIER_PRIORITY_INVALID = 0,
617 MODIFIER_PRIORITY_LINEAR,
618 MODIFIER_PRIORITY_X,
619 MODIFIER_PRIORITY_Y,
620 MODIFIER_PRIORITY_Y_CCS,
621 };
622
623 const uint64_t priority_to_modifier[] = {
624 [MODIFIER_PRIORITY_INVALID] = DRM_FORMAT_MOD_INVALID,
625 [MODIFIER_PRIORITY_LINEAR] = DRM_FORMAT_MOD_LINEAR,
626 [MODIFIER_PRIORITY_X] = I915_FORMAT_MOD_X_TILED,
627 [MODIFIER_PRIORITY_Y] = I915_FORMAT_MOD_Y_TILED,
628 [MODIFIER_PRIORITY_Y_CCS] = I915_FORMAT_MOD_Y_TILED_CCS,
629 };
630
631 static uint64_t
632 select_best_modifier(struct gen_device_info *devinfo,
633 int dri_format,
634 const uint64_t *modifiers,
635 const unsigned count)
636 {
637 enum modifier_priority prio = MODIFIER_PRIORITY_INVALID;
638
639 for (int i = 0; i < count; i++) {
640 if (!modifier_is_supported(devinfo, NULL, dri_format, modifiers[i]))
641 continue;
642
643 switch (modifiers[i]) {
644 case I915_FORMAT_MOD_Y_TILED_CCS:
645 prio = MAX2(prio, MODIFIER_PRIORITY_Y_CCS);
646 break;
647 case I915_FORMAT_MOD_Y_TILED:
648 prio = MAX2(prio, MODIFIER_PRIORITY_Y);
649 break;
650 case I915_FORMAT_MOD_X_TILED:
651 prio = MAX2(prio, MODIFIER_PRIORITY_X);
652 break;
653 case DRM_FORMAT_MOD_LINEAR:
654 prio = MAX2(prio, MODIFIER_PRIORITY_LINEAR);
655 break;
656 case DRM_FORMAT_MOD_INVALID:
657 default:
658 break;
659 }
660 }
661
662 return priority_to_modifier[prio];
663 }
664
665 static __DRIimage *
666 intel_create_image_common(__DRIscreen *dri_screen,
667 int width, int height, int format,
668 unsigned int use,
669 const uint64_t *modifiers,
670 unsigned count,
671 void *loaderPrivate)
672 {
673 __DRIimage *image;
674 struct intel_screen *screen = dri_screen->driverPrivate;
675 uint64_t modifier = DRM_FORMAT_MOD_INVALID;
676 bool ok;
677
678 /* Callers of this may specify a modifier, or a dri usage, but not both. The
679 * newer modifier interface deprecates the older usage flags newer modifier
680 * interface deprecates the older usage flags.
681 */
682 assert(!(use && count));
683
684 if (use & __DRI_IMAGE_USE_CURSOR) {
685 if (width != 64 || height != 64)
686 return NULL;
687 modifier = DRM_FORMAT_MOD_LINEAR;
688 }
689
690 if (use & __DRI_IMAGE_USE_LINEAR)
691 modifier = DRM_FORMAT_MOD_LINEAR;
692
693 if (modifier == DRM_FORMAT_MOD_INVALID) {
694 if (modifiers) {
695 /* User requested specific modifiers */
696 modifier = select_best_modifier(&screen->devinfo, format,
697 modifiers, count);
698 if (modifier == DRM_FORMAT_MOD_INVALID)
699 return NULL;
700 } else {
701 /* Historically, X-tiled was the default, and so lack of modifier means
702 * X-tiled.
703 */
704 modifier = I915_FORMAT_MOD_X_TILED;
705 }
706 }
707
708 image = intel_allocate_image(screen, format, loaderPrivate);
709 if (image == NULL)
710 return NULL;
711
712 const struct isl_drm_modifier_info *mod_info =
713 isl_drm_modifier_get_info(modifier);
714
715 struct isl_surf surf;
716 ok = isl_surf_init(&screen->isl_dev, &surf,
717 .dim = ISL_SURF_DIM_2D,
718 .format = brw_isl_format_for_mesa_format(image->format),
719 .width = width,
720 .height = height,
721 .depth = 1,
722 .levels = 1,
723 .array_len = 1,
724 .samples = 1,
725 .usage = ISL_SURF_USAGE_RENDER_TARGET_BIT |
726 ISL_SURF_USAGE_TEXTURE_BIT |
727 ISL_SURF_USAGE_STORAGE_BIT,
728 .tiling_flags = (1 << mod_info->tiling));
729 assert(ok);
730 if (!ok) {
731 free(image);
732 return NULL;
733 }
734
735 struct isl_surf aux_surf;
736 if (mod_info->aux_usage == ISL_AUX_USAGE_CCS_E) {
737 ok = isl_surf_get_ccs_surf(&screen->isl_dev, &surf, &aux_surf, 0);
738 if (!ok) {
739 free(image);
740 return NULL;
741 }
742 } else {
743 assert(mod_info->aux_usage == ISL_AUX_USAGE_NONE);
744 aux_surf.size = 0;
745 }
746
747 /* We request that the bufmgr zero the buffer for us for two reasons:
748 *
749 * 1) If a buffer gets re-used from the pool, we don't want to leak random
750 * garbage from our process to some other.
751 *
752 * 2) For images with CCS_E, we want to ensure that the CCS starts off in
753 * a valid state. A CCS value of 0 indicates that the given block is
754 * in the pass-through state which is what we want.
755 */
756 image->bo = brw_bo_alloc_tiled(screen->bufmgr, "image",
757 surf.size + aux_surf.size,
758 BRW_MEMZONE_OTHER,
759 isl_tiling_to_i915_tiling(mod_info->tiling),
760 surf.row_pitch, BO_ALLOC_ZEROED);
761 if (image->bo == NULL) {
762 free(image);
763 return NULL;
764 }
765 image->width = width;
766 image->height = height;
767 image->pitch = surf.row_pitch;
768 image->modifier = modifier;
769
770 if (aux_surf.size) {
771 image->aux_offset = surf.size;
772 image->aux_pitch = aux_surf.row_pitch;
773 image->aux_size = aux_surf.size;
774 }
775
776 return image;
777 }
778
779 static __DRIimage *
780 intel_create_image(__DRIscreen *dri_screen,
781 int width, int height, int format,
782 unsigned int use,
783 void *loaderPrivate)
784 {
785 return intel_create_image_common(dri_screen, width, height, format, use, NULL, 0,
786 loaderPrivate);
787 }
788
789 static void *
790 intel_map_image(__DRIcontext *context, __DRIimage *image,
791 int x0, int y0, int width, int height,
792 unsigned int flags, int *stride, void **map_info)
793 {
794 struct brw_context *brw = NULL;
795 struct brw_bo *bo = NULL;
796 void *raw_data = NULL;
797 GLuint pix_w = 1;
798 GLuint pix_h = 1;
799 GLint pix_bytes = 1;
800
801 if (!context || !image || !stride || !map_info || *map_info)
802 return NULL;
803
804 if (x0 < 0 || x0 >= image->width || width > image->width - x0)
805 return NULL;
806
807 if (y0 < 0 || y0 >= image->height || height > image->height - y0)
808 return NULL;
809
810 if (flags & MAP_INTERNAL_MASK)
811 return NULL;
812
813 brw = context->driverPrivate;
814 bo = image->bo;
815
816 assert(brw);
817 assert(bo);
818
819 /* DRI flags and GL_MAP.*_BIT flags are the same, so just pass them on. */
820 raw_data = brw_bo_map(brw, bo, flags);
821 if (!raw_data)
822 return NULL;
823
824 _mesa_get_format_block_size(image->format, &pix_w, &pix_h);
825 pix_bytes = _mesa_get_format_bytes(image->format);
826
827 assert(pix_w);
828 assert(pix_h);
829 assert(pix_bytes > 0);
830
831 raw_data += (x0 / pix_w) * pix_bytes + (y0 / pix_h) * image->pitch;
832
833 brw_bo_reference(bo);
834
835 *stride = image->pitch;
836 *map_info = bo;
837
838 return raw_data;
839 }
840
841 static void
842 intel_unmap_image(__DRIcontext *context, __DRIimage *image, void *map_info)
843 {
844 struct brw_bo *bo = map_info;
845
846 brw_bo_unmap(bo);
847 brw_bo_unreference(bo);
848 }
849
850 static __DRIimage *
851 intel_create_image_with_modifiers(__DRIscreen *dri_screen,
852 int width, int height, int format,
853 const uint64_t *modifiers,
854 const unsigned count,
855 void *loaderPrivate)
856 {
857 return intel_create_image_common(dri_screen, width, height, format, 0,
858 modifiers, count, loaderPrivate);
859 }
860
861 static GLboolean
862 intel_query_image(__DRIimage *image, int attrib, int *value)
863 {
864 switch (attrib) {
865 case __DRI_IMAGE_ATTRIB_STRIDE:
866 *value = image->pitch;
867 return true;
868 case __DRI_IMAGE_ATTRIB_HANDLE:
869 *value = brw_bo_export_gem_handle(image->bo);
870 return true;
871 case __DRI_IMAGE_ATTRIB_NAME:
872 return !brw_bo_flink(image->bo, (uint32_t *) value);
873 case __DRI_IMAGE_ATTRIB_FORMAT:
874 *value = image->dri_format;
875 return true;
876 case __DRI_IMAGE_ATTRIB_WIDTH:
877 *value = image->width;
878 return true;
879 case __DRI_IMAGE_ATTRIB_HEIGHT:
880 *value = image->height;
881 return true;
882 case __DRI_IMAGE_ATTRIB_COMPONENTS:
883 if (image->planar_format == NULL)
884 return false;
885 *value = image->planar_format->components;
886 return true;
887 case __DRI_IMAGE_ATTRIB_FD:
888 return !brw_bo_gem_export_to_prime(image->bo, value);
889 case __DRI_IMAGE_ATTRIB_FOURCC:
890 return intel_image_get_fourcc(image, value);
891 case __DRI_IMAGE_ATTRIB_NUM_PLANES:
892 if (isl_drm_modifier_has_aux(image->modifier)) {
893 assert(!image->planar_format || image->planar_format->nplanes == 1);
894 *value = 2;
895 } else if (image->planar_format) {
896 *value = image->planar_format->nplanes;
897 } else {
898 *value = 1;
899 }
900 return true;
901 case __DRI_IMAGE_ATTRIB_OFFSET:
902 *value = image->offset;
903 return true;
904 case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER:
905 *value = (image->modifier & 0xffffffff);
906 return true;
907 case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER:
908 *value = ((image->modifier >> 32) & 0xffffffff);
909 return true;
910
911 default:
912 return false;
913 }
914 }
915
916 static GLboolean
917 intel_query_format_modifier_attribs(__DRIscreen *dri_screen,
918 uint32_t fourcc, uint64_t modifier,
919 int attrib, uint64_t *value)
920 {
921 struct intel_screen *screen = dri_screen->driverPrivate;
922 const struct intel_image_format *f = intel_image_format_lookup(fourcc);
923
924 if (!modifier_is_supported(&screen->devinfo, f, 0, modifier))
925 return false;
926
927 switch (attrib) {
928 case __DRI_IMAGE_FORMAT_MODIFIER_ATTRIB_PLANE_COUNT:
929 *value = isl_drm_modifier_has_aux(modifier) ? 2 : f->nplanes;
930 return true;
931
932 default:
933 return false;
934 }
935 }
936
937 static __DRIimage *
938 intel_dup_image(__DRIimage *orig_image, void *loaderPrivate)
939 {
940 __DRIimage *image;
941
942 image = calloc(1, sizeof *image);
943 if (image == NULL)
944 return NULL;
945
946 brw_bo_reference(orig_image->bo);
947 image->bo = orig_image->bo;
948 image->internal_format = orig_image->internal_format;
949 image->planar_format = orig_image->planar_format;
950 image->dri_format = orig_image->dri_format;
951 image->format = orig_image->format;
952 image->modifier = orig_image->modifier;
953 image->offset = orig_image->offset;
954 image->width = orig_image->width;
955 image->height = orig_image->height;
956 image->pitch = orig_image->pitch;
957 image->tile_x = orig_image->tile_x;
958 image->tile_y = orig_image->tile_y;
959 image->has_depthstencil = orig_image->has_depthstencil;
960 image->data = loaderPrivate;
961 image->dma_buf_imported = orig_image->dma_buf_imported;
962 image->aux_offset = orig_image->aux_offset;
963 image->aux_pitch = orig_image->aux_pitch;
964
965 memcpy(image->strides, orig_image->strides, sizeof(image->strides));
966 memcpy(image->offsets, orig_image->offsets, sizeof(image->offsets));
967
968 return image;
969 }
970
971 static GLboolean
972 intel_validate_usage(__DRIimage *image, unsigned int use)
973 {
974 if (use & __DRI_IMAGE_USE_CURSOR) {
975 if (image->width != 64 || image->height != 64)
976 return GL_FALSE;
977 }
978
979 return GL_TRUE;
980 }
981
982 static __DRIimage *
983 intel_create_image_from_names(__DRIscreen *dri_screen,
984 int width, int height, int fourcc,
985 int *names, int num_names,
986 int *strides, int *offsets,
987 void *loaderPrivate)
988 {
989 const struct intel_image_format *f = NULL;
990 __DRIimage *image;
991 int i, index;
992
993 if (dri_screen == NULL || names == NULL || num_names != 1)
994 return NULL;
995
996 f = intel_image_format_lookup(fourcc);
997 if (f == NULL)
998 return NULL;
999
1000 image = intel_create_image_from_name(dri_screen, width, height,
1001 __DRI_IMAGE_FORMAT_NONE,
1002 names[0], strides[0],
1003 loaderPrivate);
1004
1005 if (image == NULL)
1006 return NULL;
1007
1008 image->planar_format = f;
1009 for (i = 0; i < f->nplanes; i++) {
1010 index = f->planes[i].buffer_index;
1011 image->offsets[index] = offsets[index];
1012 image->strides[index] = strides[index];
1013 }
1014
1015 return image;
1016 }
1017
1018 static __DRIimage *
1019 intel_create_image_from_fds_common(__DRIscreen *dri_screen,
1020 int width, int height, int fourcc,
1021 uint64_t modifier, int *fds, int num_fds,
1022 int *strides, int *offsets,
1023 void *loaderPrivate)
1024 {
1025 struct intel_screen *screen = dri_screen->driverPrivate;
1026 const struct intel_image_format *f;
1027 __DRIimage *image;
1028 int i, index;
1029 bool ok;
1030
1031 if (fds == NULL || num_fds < 1)
1032 return NULL;
1033
1034 f = intel_image_format_lookup(fourcc);
1035 if (f == NULL)
1036 return NULL;
1037
1038 if (modifier != DRM_FORMAT_MOD_INVALID &&
1039 !modifier_is_supported(&screen->devinfo, f, 0, modifier))
1040 return NULL;
1041
1042 if (f->nplanes == 1)
1043 image = intel_allocate_image(screen, f->planes[0].dri_format,
1044 loaderPrivate);
1045 else
1046 image = intel_allocate_image(screen, __DRI_IMAGE_FORMAT_NONE,
1047 loaderPrivate);
1048
1049 if (image == NULL)
1050 return NULL;
1051
1052 image->width = width;
1053 image->height = height;
1054 image->pitch = strides[0];
1055
1056 image->planar_format = f;
1057
1058 if (modifier != DRM_FORMAT_MOD_INVALID) {
1059 const struct isl_drm_modifier_info *mod_info =
1060 isl_drm_modifier_get_info(modifier);
1061 uint32_t tiling = isl_tiling_to_i915_tiling(mod_info->tiling);
1062 image->bo = brw_bo_gem_create_from_prime_tiled(screen->bufmgr, fds[0],
1063 tiling, strides[0]);
1064 } else {
1065 image->bo = brw_bo_gem_create_from_prime(screen->bufmgr, fds[0]);
1066 }
1067
1068 if (image->bo == NULL) {
1069 free(image);
1070 return NULL;
1071 }
1072
1073 /* We only support all planes from the same bo.
1074 * brw_bo_gem_create_from_prime() should return the same pointer for all
1075 * fds received here */
1076 for (i = 1; i < num_fds; i++) {
1077 struct brw_bo *aux = brw_bo_gem_create_from_prime(screen->bufmgr, fds[i]);
1078 brw_bo_unreference(aux);
1079 if (aux != image->bo) {
1080 brw_bo_unreference(image->bo);
1081 free(image);
1082 return NULL;
1083 }
1084 }
1085
1086 if (modifier != DRM_FORMAT_MOD_INVALID)
1087 image->modifier = modifier;
1088 else
1089 image->modifier = tiling_to_modifier(image->bo->tiling_mode);
1090
1091 const struct isl_drm_modifier_info *mod_info =
1092 isl_drm_modifier_get_info(image->modifier);
1093
1094 int size = 0;
1095 struct isl_surf surf;
1096 for (i = 0; i < f->nplanes; i++) {
1097 index = f->planes[i].buffer_index;
1098 image->offsets[index] = offsets[index];
1099 image->strides[index] = strides[index];
1100
1101 mesa_format format = driImageFormatToGLFormat(f->planes[i].dri_format);
1102 /* The images we will create are actually based on the RGBA non-sRGB
1103 * version of the format.
1104 */
1105 format = _mesa_format_fallback_rgbx_to_rgba(format);
1106 format = _mesa_get_srgb_format_linear(format);
1107
1108 ok = isl_surf_init(&screen->isl_dev, &surf,
1109 .dim = ISL_SURF_DIM_2D,
1110 .format = brw_isl_format_for_mesa_format(format),
1111 .width = image->width >> f->planes[i].width_shift,
1112 .height = image->height >> f->planes[i].height_shift,
1113 .depth = 1,
1114 .levels = 1,
1115 .array_len = 1,
1116 .samples = 1,
1117 .row_pitch = strides[index],
1118 .usage = ISL_SURF_USAGE_RENDER_TARGET_BIT |
1119 ISL_SURF_USAGE_TEXTURE_BIT |
1120 ISL_SURF_USAGE_STORAGE_BIT,
1121 .tiling_flags = (1 << mod_info->tiling));
1122 if (!ok) {
1123 brw_bo_unreference(image->bo);
1124 free(image);
1125 return NULL;
1126 }
1127
1128 const int end = offsets[index] + surf.size;
1129 if (size < end)
1130 size = end;
1131 }
1132
1133 if (mod_info->aux_usage == ISL_AUX_USAGE_CCS_E) {
1134 /* Even though we initialize surf in the loop above, we know that
1135 * anything with CCS_E will have exactly one plane so surf is properly
1136 * initialized when we get here.
1137 */
1138 assert(f->nplanes == 1);
1139
1140 image->aux_offset = offsets[1];
1141 image->aux_pitch = strides[1];
1142
1143 /* Scanout hardware requires that the CCS be placed after the main
1144 * surface in memory. We consider any CCS that is placed any earlier in
1145 * memory to be invalid and reject it.
1146 *
1147 * At some point in the future, this restriction may be relaxed if the
1148 * hardware becomes less strict but we may need a new modifier for that.
1149 */
1150 assert(size > 0);
1151 if (image->aux_offset < size) {
1152 brw_bo_unreference(image->bo);
1153 free(image);
1154 return NULL;
1155 }
1156
1157 struct isl_surf aux_surf;
1158 ok = isl_surf_get_ccs_surf(&screen->isl_dev, &surf, &aux_surf,
1159 image->aux_pitch);
1160 if (!ok) {
1161 brw_bo_unreference(image->bo);
1162 free(image);
1163 return NULL;
1164 }
1165
1166 image->aux_size = aux_surf.size;
1167
1168 const int end = image->aux_offset + aux_surf.size;
1169 if (size < end)
1170 size = end;
1171 } else {
1172 assert(mod_info->aux_usage == ISL_AUX_USAGE_NONE);
1173 }
1174
1175 /* Check that the requested image actually fits within the BO. 'size'
1176 * is already relative to the offsets, so we don't need to add that. */
1177 if (image->bo->size == 0) {
1178 image->bo->size = size;
1179 } else if (size > image->bo->size) {
1180 brw_bo_unreference(image->bo);
1181 free(image);
1182 return NULL;
1183 }
1184
1185 if (f->nplanes == 1) {
1186 image->offset = image->offsets[0];
1187 intel_image_warn_if_unaligned(image, __func__);
1188 }
1189
1190 return image;
1191 }
1192
1193 static __DRIimage *
1194 intel_create_image_from_fds(__DRIscreen *dri_screen,
1195 int width, int height, int fourcc,
1196 int *fds, int num_fds, int *strides, int *offsets,
1197 void *loaderPrivate)
1198 {
1199 return intel_create_image_from_fds_common(dri_screen, width, height, fourcc,
1200 DRM_FORMAT_MOD_INVALID,
1201 fds, num_fds, strides, offsets,
1202 loaderPrivate);
1203 }
1204
1205 static __DRIimage *
1206 intel_create_image_from_dma_bufs2(__DRIscreen *dri_screen,
1207 int width, int height,
1208 int fourcc, uint64_t modifier,
1209 int *fds, int num_fds,
1210 int *strides, int *offsets,
1211 enum __DRIYUVColorSpace yuv_color_space,
1212 enum __DRISampleRange sample_range,
1213 enum __DRIChromaSiting horizontal_siting,
1214 enum __DRIChromaSiting vertical_siting,
1215 unsigned *error,
1216 void *loaderPrivate)
1217 {
1218 __DRIimage *image;
1219 const struct intel_image_format *f = intel_image_format_lookup(fourcc);
1220
1221 if (!f) {
1222 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1223 return NULL;
1224 }
1225
1226 image = intel_create_image_from_fds_common(dri_screen, width, height,
1227 fourcc, modifier,
1228 fds, num_fds, strides, offsets,
1229 loaderPrivate);
1230
1231 /*
1232 * Invalid parameters and any inconsistencies between are assumed to be
1233 * checked by the caller. Therefore besides unsupported formats one can fail
1234 * only in allocation.
1235 */
1236 if (!image) {
1237 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
1238 return NULL;
1239 }
1240
1241 image->dma_buf_imported = true;
1242 image->yuv_color_space = yuv_color_space;
1243 image->sample_range = sample_range;
1244 image->horizontal_siting = horizontal_siting;
1245 image->vertical_siting = vertical_siting;
1246
1247 *error = __DRI_IMAGE_ERROR_SUCCESS;
1248 return image;
1249 }
1250
1251 static __DRIimage *
1252 intel_create_image_from_dma_bufs(__DRIscreen *dri_screen,
1253 int width, int height, int fourcc,
1254 int *fds, int num_fds,
1255 int *strides, int *offsets,
1256 enum __DRIYUVColorSpace yuv_color_space,
1257 enum __DRISampleRange sample_range,
1258 enum __DRIChromaSiting horizontal_siting,
1259 enum __DRIChromaSiting vertical_siting,
1260 unsigned *error,
1261 void *loaderPrivate)
1262 {
1263 return intel_create_image_from_dma_bufs2(dri_screen, width, height,
1264 fourcc, DRM_FORMAT_MOD_INVALID,
1265 fds, num_fds, strides, offsets,
1266 yuv_color_space,
1267 sample_range,
1268 horizontal_siting,
1269 vertical_siting,
1270 error,
1271 loaderPrivate);
1272 }
1273
1274 static bool
1275 intel_image_format_is_supported(const struct gen_device_info *devinfo,
1276 const struct intel_image_format *fmt)
1277 {
1278 if (fmt->fourcc == __DRI_IMAGE_FOURCC_SARGB8888 ||
1279 fmt->fourcc == __DRI_IMAGE_FOURCC_SABGR8888)
1280 return false;
1281
1282 #ifndef NDEBUG
1283 if (fmt->nplanes == 1) {
1284 mesa_format format = driImageFormatToGLFormat(fmt->planes[0].dri_format);
1285 /* The images we will create are actually based on the RGBA non-sRGB
1286 * version of the format.
1287 */
1288 format = _mesa_format_fallback_rgbx_to_rgba(format);
1289 format = _mesa_get_srgb_format_linear(format);
1290 enum isl_format isl_format = brw_isl_format_for_mesa_format(format);
1291 assert(isl_format_supports_rendering(devinfo, isl_format));
1292 }
1293 #endif
1294
1295 return true;
1296 }
1297
1298 static GLboolean
1299 intel_query_dma_buf_formats(__DRIscreen *_screen, int max,
1300 int *formats, int *count)
1301 {
1302 struct intel_screen *screen = _screen->driverPrivate;
1303 int num_formats = 0, i;
1304
1305 for (i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
1306 if (!intel_image_format_is_supported(&screen->devinfo,
1307 &intel_image_formats[i]))
1308 continue;
1309
1310 num_formats++;
1311 if (max == 0)
1312 continue;
1313
1314 formats[num_formats - 1] = intel_image_formats[i].fourcc;
1315 if (num_formats >= max)
1316 break;
1317 }
1318
1319 *count = num_formats;
1320 return true;
1321 }
1322
1323 static GLboolean
1324 intel_query_dma_buf_modifiers(__DRIscreen *_screen, int fourcc, int max,
1325 uint64_t *modifiers,
1326 unsigned int *external_only,
1327 int *count)
1328 {
1329 struct intel_screen *screen = _screen->driverPrivate;
1330 const struct intel_image_format *f;
1331 int num_mods = 0, i;
1332
1333 f = intel_image_format_lookup(fourcc);
1334 if (f == NULL)
1335 return false;
1336
1337 if (!intel_image_format_is_supported(&screen->devinfo, f))
1338 return false;
1339
1340 for (i = 0; i < ARRAY_SIZE(supported_modifiers); i++) {
1341 uint64_t modifier = supported_modifiers[i].modifier;
1342 if (!modifier_is_supported(&screen->devinfo, f, 0, modifier))
1343 continue;
1344
1345 num_mods++;
1346 if (max == 0)
1347 continue;
1348
1349 modifiers[num_mods - 1] = modifier;
1350 if (num_mods >= max)
1351 break;
1352 }
1353
1354 if (external_only != NULL) {
1355 for (i = 0; i < num_mods && i < max; i++) {
1356 if (f->components == __DRI_IMAGE_COMPONENTS_Y_U_V ||
1357 f->components == __DRI_IMAGE_COMPONENTS_Y_UV ||
1358 f->components == __DRI_IMAGE_COMPONENTS_Y_XUXV) {
1359 external_only[i] = GL_TRUE;
1360 }
1361 else {
1362 external_only[i] = GL_FALSE;
1363 }
1364 }
1365 }
1366
1367 *count = num_mods;
1368 return true;
1369 }
1370
1371 static __DRIimage *
1372 intel_from_planar(__DRIimage *parent, int plane, void *loaderPrivate)
1373 {
1374 int width, height, offset, stride, size, dri_format;
1375 __DRIimage *image;
1376
1377 if (parent == NULL)
1378 return NULL;
1379
1380 width = parent->width;
1381 height = parent->height;
1382
1383 const struct intel_image_format *f = parent->planar_format;
1384
1385 if (f && plane < f->nplanes) {
1386 /* Use the planar format definition. */
1387 width >>= f->planes[plane].width_shift;
1388 height >>= f->planes[plane].height_shift;
1389 dri_format = f->planes[plane].dri_format;
1390 int index = f->planes[plane].buffer_index;
1391 offset = parent->offsets[index];
1392 stride = parent->strides[index];
1393 size = height * stride;
1394 } else if (plane == 0) {
1395 /* The only plane of a non-planar image: copy the parent definition
1396 * directly. */
1397 dri_format = parent->dri_format;
1398 offset = parent->offset;
1399 stride = parent->pitch;
1400 size = height * stride;
1401 } else if (plane == 1 && parent->modifier != DRM_FORMAT_MOD_INVALID &&
1402 isl_drm_modifier_has_aux(parent->modifier)) {
1403 /* Auxiliary plane */
1404 dri_format = parent->dri_format;
1405 offset = parent->aux_offset;
1406 stride = parent->aux_pitch;
1407 size = parent->aux_size;
1408 } else {
1409 return NULL;
1410 }
1411
1412 if (offset + size > parent->bo->size) {
1413 _mesa_warning(NULL, "intel_from_planar: subimage out of bounds");
1414 return NULL;
1415 }
1416
1417 image = intel_allocate_image(parent->screen, dri_format, loaderPrivate);
1418 if (image == NULL)
1419 return NULL;
1420
1421 image->bo = parent->bo;
1422 brw_bo_reference(parent->bo);
1423 image->modifier = parent->modifier;
1424
1425 image->width = width;
1426 image->height = height;
1427 image->pitch = stride;
1428 image->offset = offset;
1429
1430 intel_image_warn_if_unaligned(image, __func__);
1431
1432 return image;
1433 }
1434
1435 static const __DRIimageExtension intelImageExtension = {
1436 .base = { __DRI_IMAGE, 16 },
1437
1438 .createImageFromName = intel_create_image_from_name,
1439 .createImageFromRenderbuffer = intel_create_image_from_renderbuffer,
1440 .destroyImage = intel_destroy_image,
1441 .createImage = intel_create_image,
1442 .queryImage = intel_query_image,
1443 .dupImage = intel_dup_image,
1444 .validateUsage = intel_validate_usage,
1445 .createImageFromNames = intel_create_image_from_names,
1446 .fromPlanar = intel_from_planar,
1447 .createImageFromTexture = intel_create_image_from_texture,
1448 .createImageFromFds = intel_create_image_from_fds,
1449 .createImageFromDmaBufs = intel_create_image_from_dma_bufs,
1450 .blitImage = NULL,
1451 .getCapabilities = NULL,
1452 .mapImage = intel_map_image,
1453 .unmapImage = intel_unmap_image,
1454 .createImageWithModifiers = intel_create_image_with_modifiers,
1455 .createImageFromDmaBufs2 = intel_create_image_from_dma_bufs2,
1456 .queryDmaBufFormats = intel_query_dma_buf_formats,
1457 .queryDmaBufModifiers = intel_query_dma_buf_modifiers,
1458 .queryDmaBufFormatModifierAttribs = intel_query_format_modifier_attribs,
1459 };
1460
1461 static uint64_t
1462 get_aperture_size(int fd)
1463 {
1464 struct drm_i915_gem_get_aperture aperture;
1465
1466 if (drmIoctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture) != 0)
1467 return 0;
1468
1469 return aperture.aper_size;
1470 }
1471
1472 static int
1473 brw_query_renderer_integer(__DRIscreen *dri_screen,
1474 int param, unsigned int *value)
1475 {
1476 const struct intel_screen *const screen =
1477 (struct intel_screen *) dri_screen->driverPrivate;
1478
1479 switch (param) {
1480 case __DRI2_RENDERER_VENDOR_ID:
1481 value[0] = 0x8086;
1482 return 0;
1483 case __DRI2_RENDERER_DEVICE_ID:
1484 value[0] = screen->deviceID;
1485 return 0;
1486 case __DRI2_RENDERER_ACCELERATED:
1487 value[0] = 1;
1488 return 0;
1489 case __DRI2_RENDERER_VIDEO_MEMORY: {
1490 /* Once a batch uses more than 75% of the maximum mappable size, we
1491 * assume that there's some fragmentation, and we start doing extra
1492 * flushing, etc. That's the big cliff apps will care about.
1493 */
1494 const unsigned gpu_mappable_megabytes =
1495 screen->aperture_threshold / (1024 * 1024);
1496
1497 const long system_memory_pages = sysconf(_SC_PHYS_PAGES);
1498 const long system_page_size = sysconf(_SC_PAGE_SIZE);
1499
1500 if (system_memory_pages <= 0 || system_page_size <= 0)
1501 return -1;
1502
1503 const uint64_t system_memory_bytes = (uint64_t) system_memory_pages
1504 * (uint64_t) system_page_size;
1505
1506 const unsigned system_memory_megabytes =
1507 (unsigned) (system_memory_bytes / (1024 * 1024));
1508
1509 value[0] = MIN2(system_memory_megabytes, gpu_mappable_megabytes);
1510 return 0;
1511 }
1512 case __DRI2_RENDERER_UNIFIED_MEMORY_ARCHITECTURE:
1513 value[0] = 1;
1514 return 0;
1515 case __DRI2_RENDERER_HAS_TEXTURE_3D:
1516 value[0] = 1;
1517 return 0;
1518 case __DRI2_RENDERER_HAS_CONTEXT_PRIORITY:
1519 value[0] = 0;
1520 if (brw_hw_context_set_priority(screen->bufmgr,
1521 0, GEN_CONTEXT_HIGH_PRIORITY) == 0)
1522 value[0] |= __DRI2_RENDERER_HAS_CONTEXT_PRIORITY_HIGH;
1523 if (brw_hw_context_set_priority(screen->bufmgr,
1524 0, GEN_CONTEXT_LOW_PRIORITY) == 0)
1525 value[0] |= __DRI2_RENDERER_HAS_CONTEXT_PRIORITY_LOW;
1526 /* reset to default last, just in case */
1527 if (brw_hw_context_set_priority(screen->bufmgr,
1528 0, GEN_CONTEXT_MEDIUM_PRIORITY) == 0)
1529 value[0] |= __DRI2_RENDERER_HAS_CONTEXT_PRIORITY_MEDIUM;
1530 return 0;
1531 case __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB:
1532 value[0] = 1;
1533 return 0;
1534 default:
1535 return driQueryRendererIntegerCommon(dri_screen, param, value);
1536 }
1537
1538 return -1;
1539 }
1540
1541 static int
1542 brw_query_renderer_string(__DRIscreen *dri_screen,
1543 int param, const char **value)
1544 {
1545 const struct intel_screen *screen =
1546 (struct intel_screen *) dri_screen->driverPrivate;
1547
1548 switch (param) {
1549 case __DRI2_RENDERER_VENDOR_ID:
1550 value[0] = brw_vendor_string;
1551 return 0;
1552 case __DRI2_RENDERER_DEVICE_ID:
1553 value[0] = brw_get_renderer_string(screen);
1554 return 0;
1555 default:
1556 break;
1557 }
1558
1559 return -1;
1560 }
1561
1562 static void
1563 brw_set_cache_funcs(__DRIscreen *dri_screen,
1564 __DRIblobCacheSet set, __DRIblobCacheGet get)
1565 {
1566 const struct intel_screen *const screen =
1567 (struct intel_screen *) dri_screen->driverPrivate;
1568
1569 if (!screen->disk_cache)
1570 return;
1571
1572 disk_cache_set_callbacks(screen->disk_cache, set, get);
1573 }
1574
1575 static const __DRI2rendererQueryExtension intelRendererQueryExtension = {
1576 .base = { __DRI2_RENDERER_QUERY, 1 },
1577
1578 .queryInteger = brw_query_renderer_integer,
1579 .queryString = brw_query_renderer_string
1580 };
1581
1582 static const __DRIrobustnessExtension dri2Robustness = {
1583 .base = { __DRI2_ROBUSTNESS, 1 }
1584 };
1585
1586 static const __DRI2blobExtension intelBlobExtension = {
1587 .base = { __DRI2_BLOB, 1 },
1588 .set_cache_funcs = brw_set_cache_funcs
1589 };
1590
1591 static const __DRIextension *screenExtensions[] = {
1592 &intelTexBufferExtension.base,
1593 &intelFenceExtension.base,
1594 &intelFlushExtension.base,
1595 &intelImageExtension.base,
1596 &intelRendererQueryExtension.base,
1597 &dri2ConfigQueryExtension.base,
1598 &dri2NoErrorExtension.base,
1599 &intelBlobExtension.base,
1600 NULL
1601 };
1602
1603 static const __DRIextension *intelRobustScreenExtensions[] = {
1604 &intelTexBufferExtension.base,
1605 &intelFenceExtension.base,
1606 &intelFlushExtension.base,
1607 &intelImageExtension.base,
1608 &intelRendererQueryExtension.base,
1609 &dri2ConfigQueryExtension.base,
1610 &dri2Robustness.base,
1611 &dri2NoErrorExtension.base,
1612 &intelBlobExtension.base,
1613 NULL
1614 };
1615
1616 static int
1617 intel_get_param(struct intel_screen *screen, int param, int *value)
1618 {
1619 int ret = 0;
1620 struct drm_i915_getparam gp;
1621
1622 memset(&gp, 0, sizeof(gp));
1623 gp.param = param;
1624 gp.value = value;
1625
1626 if (drmIoctl(screen->driScrnPriv->fd, DRM_IOCTL_I915_GETPARAM, &gp) == -1) {
1627 ret = -errno;
1628 if (ret != -EINVAL)
1629 _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
1630 }
1631
1632 return ret;
1633 }
1634
1635 static bool
1636 intel_get_boolean(struct intel_screen *screen, int param)
1637 {
1638 int value = 0;
1639 return (intel_get_param(screen, param, &value) == 0) && value;
1640 }
1641
1642 static int
1643 intel_get_integer(struct intel_screen *screen, int param)
1644 {
1645 int value = -1;
1646
1647 if (intel_get_param(screen, param, &value) == 0)
1648 return value;
1649
1650 return -1;
1651 }
1652
1653 static void
1654 intelDestroyScreen(__DRIscreen * sPriv)
1655 {
1656 struct intel_screen *screen = sPriv->driverPrivate;
1657
1658 brw_bufmgr_destroy(screen->bufmgr);
1659 driDestroyOptionInfo(&screen->optionCache);
1660
1661 disk_cache_destroy(screen->disk_cache);
1662
1663 ralloc_free(screen);
1664 sPriv->driverPrivate = NULL;
1665 }
1666
1667
1668 /**
1669 * Create a gl_framebuffer and attach it to __DRIdrawable::driverPrivate.
1670 *
1671 *_This implements driDriverAPI::createNewDrawable, which the DRI layer calls
1672 * when creating a EGLSurface, GLXDrawable, or GLXPixmap. Despite the name,
1673 * this does not allocate GPU memory.
1674 */
1675 static GLboolean
1676 intelCreateBuffer(__DRIscreen *dri_screen,
1677 __DRIdrawable * driDrawPriv,
1678 const struct gl_config * mesaVis, GLboolean isPixmap)
1679 {
1680 struct intel_renderbuffer *rb;
1681 struct intel_screen *screen = (struct intel_screen *)
1682 dri_screen->driverPrivate;
1683 mesa_format rgbFormat;
1684 unsigned num_samples =
1685 intel_quantize_num_samples(screen, mesaVis->samples);
1686
1687 if (isPixmap)
1688 return false;
1689
1690 struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
1691 if (!fb)
1692 return false;
1693
1694 _mesa_initialize_window_framebuffer(fb, mesaVis);
1695
1696 if (screen->winsys_msaa_samples_override != -1) {
1697 num_samples = screen->winsys_msaa_samples_override;
1698 fb->Visual.samples = num_samples;
1699 }
1700
1701 if (mesaVis->redBits == 10 && mesaVis->alphaBits > 0) {
1702 rgbFormat = mesaVis->redMask == 0x3ff00000 ? MESA_FORMAT_B10G10R10A2_UNORM
1703 : MESA_FORMAT_R10G10B10A2_UNORM;
1704 } else if (mesaVis->redBits == 10) {
1705 rgbFormat = mesaVis->redMask == 0x3ff00000 ? MESA_FORMAT_B10G10R10X2_UNORM
1706 : MESA_FORMAT_R10G10B10X2_UNORM;
1707 } else if (mesaVis->redBits == 5) {
1708 rgbFormat = mesaVis->redMask == 0x1f ? MESA_FORMAT_R5G6B5_UNORM
1709 : MESA_FORMAT_B5G6R5_UNORM;
1710 } else if (mesaVis->sRGBCapable) {
1711 rgbFormat = mesaVis->redMask == 0xff ? MESA_FORMAT_R8G8B8A8_SRGB
1712 : MESA_FORMAT_B8G8R8A8_SRGB;
1713 } else if (mesaVis->alphaBits == 0) {
1714 rgbFormat = mesaVis->redMask == 0xff ? MESA_FORMAT_R8G8B8X8_UNORM
1715 : MESA_FORMAT_B8G8R8X8_UNORM;
1716 } else {
1717 rgbFormat = mesaVis->redMask == 0xff ? MESA_FORMAT_R8G8B8A8_SRGB
1718 : MESA_FORMAT_B8G8R8A8_SRGB;
1719 fb->Visual.sRGBCapable = true;
1720 }
1721
1722 /* mesaVis->sRGBCapable was set, user is asking for sRGB */
1723 bool srgb_cap_set = mesaVis->redBits >= 8 && mesaVis->sRGBCapable;
1724
1725 /* setup the hardware-based renderbuffers */
1726 rb = intel_create_winsys_renderbuffer(screen, rgbFormat, num_samples);
1727 _mesa_attach_and_own_rb(fb, BUFFER_FRONT_LEFT, &rb->Base.Base);
1728 rb->need_srgb = srgb_cap_set;
1729
1730 if (mesaVis->doubleBufferMode) {
1731 rb = intel_create_winsys_renderbuffer(screen, rgbFormat, num_samples);
1732 _mesa_attach_and_own_rb(fb, BUFFER_BACK_LEFT, &rb->Base.Base);
1733 rb->need_srgb = srgb_cap_set;
1734 }
1735
1736 /*
1737 * Assert here that the gl_config has an expected depth/stencil bit
1738 * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
1739 * which constructs the advertised configs.)
1740 */
1741 if (mesaVis->depthBits == 24) {
1742 assert(mesaVis->stencilBits == 8);
1743
1744 if (screen->devinfo.has_hiz_and_separate_stencil) {
1745 rb = intel_create_private_renderbuffer(screen,
1746 MESA_FORMAT_Z24_UNORM_X8_UINT,
1747 num_samples);
1748 _mesa_attach_and_own_rb(fb, BUFFER_DEPTH, &rb->Base.Base);
1749 rb = intel_create_private_renderbuffer(screen, MESA_FORMAT_S_UINT8,
1750 num_samples);
1751 _mesa_attach_and_own_rb(fb, BUFFER_STENCIL, &rb->Base.Base);
1752 } else {
1753 /*
1754 * Use combined depth/stencil. Note that the renderbuffer is
1755 * attached to two attachment points.
1756 */
1757 rb = intel_create_private_renderbuffer(screen,
1758 MESA_FORMAT_Z24_UNORM_S8_UINT,
1759 num_samples);
1760 _mesa_attach_and_own_rb(fb, BUFFER_DEPTH, &rb->Base.Base);
1761 _mesa_attach_and_reference_rb(fb, BUFFER_STENCIL, &rb->Base.Base);
1762 }
1763 }
1764 else if (mesaVis->depthBits == 16) {
1765 assert(mesaVis->stencilBits == 0);
1766 rb = intel_create_private_renderbuffer(screen, MESA_FORMAT_Z_UNORM16,
1767 num_samples);
1768 _mesa_attach_and_own_rb(fb, BUFFER_DEPTH, &rb->Base.Base);
1769 }
1770 else {
1771 assert(mesaVis->depthBits == 0);
1772 assert(mesaVis->stencilBits == 0);
1773 }
1774
1775 /* now add any/all software-based renderbuffers we may need */
1776 _swrast_add_soft_renderbuffers(fb,
1777 false, /* never sw color */
1778 false, /* never sw depth */
1779 false, /* never sw stencil */
1780 mesaVis->accumRedBits > 0,
1781 false, /* never sw alpha */
1782 false /* never sw aux */ );
1783 driDrawPriv->driverPrivate = fb;
1784
1785 return true;
1786 }
1787
1788 static void
1789 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
1790 {
1791 struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
1792
1793 _mesa_reference_framebuffer(&fb, NULL);
1794 }
1795
1796 static void
1797 intel_cs_timestamp_frequency(struct intel_screen *screen)
1798 {
1799 /* We shouldn't need to update gen_device_info.timestamp_frequency prior to
1800 * gen10, PCI-id is enough to figure it out.
1801 */
1802 assert(screen->devinfo.gen >= 10);
1803
1804 int ret, freq;
1805
1806 ret = intel_get_param(screen, I915_PARAM_CS_TIMESTAMP_FREQUENCY,
1807 &freq);
1808 if (ret < 0) {
1809 _mesa_warning(NULL,
1810 "Kernel 4.15 required to read the CS timestamp frequency.\n");
1811 return;
1812 }
1813
1814 screen->devinfo.timestamp_frequency = freq;
1815 }
1816
1817 static void
1818 intel_detect_sseu(struct intel_screen *screen)
1819 {
1820 assert(screen->devinfo.gen >= 8);
1821 int ret;
1822
1823 screen->subslice_total = -1;
1824 screen->eu_total = -1;
1825
1826 ret = intel_get_param(screen, I915_PARAM_SUBSLICE_TOTAL,
1827 &screen->subslice_total);
1828 if (ret < 0 && ret != -EINVAL)
1829 goto err_out;
1830
1831 ret = intel_get_param(screen,
1832 I915_PARAM_EU_TOTAL, &screen->eu_total);
1833 if (ret < 0 && ret != -EINVAL)
1834 goto err_out;
1835
1836 /* Without this information, we cannot get the right Braswell brandstrings,
1837 * and we have to use conservative numbers for GPGPU on many platforms, but
1838 * otherwise, things will just work.
1839 */
1840 if (screen->subslice_total < 1 || screen->eu_total < 1)
1841 _mesa_warning(NULL,
1842 "Kernel 4.1 required to properly query GPU properties.\n");
1843
1844 return;
1845
1846 err_out:
1847 screen->subslice_total = -1;
1848 screen->eu_total = -1;
1849 _mesa_warning(NULL, "Failed to query GPU properties (%s).\n", strerror(-ret));
1850 }
1851
1852 static bool
1853 intel_init_bufmgr(struct intel_screen *screen)
1854 {
1855 __DRIscreen *dri_screen = screen->driScrnPriv;
1856
1857 if (getenv("INTEL_NO_HW") != NULL)
1858 screen->no_hw = true;
1859
1860 screen->bufmgr = brw_bufmgr_init(&screen->devinfo, dri_screen->fd);
1861 if (screen->bufmgr == NULL) {
1862 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
1863 __func__, __LINE__);
1864 return false;
1865 }
1866
1867 if (!intel_get_boolean(screen, I915_PARAM_HAS_EXEC_NO_RELOC)) {
1868 fprintf(stderr, "[%s: %u] Kernel 3.9 required.\n", __func__, __LINE__);
1869 return false;
1870 }
1871
1872 return true;
1873 }
1874
1875 static bool
1876 intel_detect_swizzling(struct intel_screen *screen)
1877 {
1878 uint32_t tiling = I915_TILING_X;
1879 uint32_t swizzle_mode = 0;
1880 struct brw_bo *buffer =
1881 brw_bo_alloc_tiled(screen->bufmgr, "swizzle test", 32768,
1882 BRW_MEMZONE_OTHER, tiling, 512, 0);
1883 if (buffer == NULL)
1884 return false;
1885
1886 brw_bo_get_tiling(buffer, &tiling, &swizzle_mode);
1887 brw_bo_unreference(buffer);
1888
1889 return swizzle_mode != I915_BIT_6_SWIZZLE_NONE;
1890 }
1891
1892 static int
1893 intel_detect_timestamp(struct intel_screen *screen)
1894 {
1895 uint64_t dummy = 0, last = 0;
1896 int upper, lower, loops;
1897
1898 /* On 64bit systems, some old kernels trigger a hw bug resulting in the
1899 * TIMESTAMP register being shifted and the low 32bits always zero.
1900 *
1901 * More recent kernels offer an interface to read the full 36bits
1902 * everywhere.
1903 */
1904 if (brw_reg_read(screen->bufmgr, TIMESTAMP | 1, &dummy) == 0)
1905 return 3;
1906
1907 /* Determine if we have a 32bit or 64bit kernel by inspecting the
1908 * upper 32bits for a rapidly changing timestamp.
1909 */
1910 if (brw_reg_read(screen->bufmgr, TIMESTAMP, &last))
1911 return 0;
1912
1913 upper = lower = 0;
1914 for (loops = 0; loops < 10; loops++) {
1915 /* The TIMESTAMP should change every 80ns, so several round trips
1916 * through the kernel should be enough to advance it.
1917 */
1918 if (brw_reg_read(screen->bufmgr, TIMESTAMP, &dummy))
1919 return 0;
1920
1921 upper += (dummy >> 32) != (last >> 32);
1922 if (upper > 1) /* beware 32bit counter overflow */
1923 return 2; /* upper dword holds the low 32bits of the timestamp */
1924
1925 lower += (dummy & 0xffffffff) != (last & 0xffffffff);
1926 if (lower > 1)
1927 return 1; /* timestamp is unshifted */
1928
1929 last = dummy;
1930 }
1931
1932 /* No advancement? No timestamp! */
1933 return 0;
1934 }
1935
1936 /**
1937 * Test if we can use MI_LOAD_REGISTER_MEM from an untrusted batchbuffer.
1938 *
1939 * Some combinations of hardware and kernel versions allow this feature,
1940 * while others don't. Instead of trying to enumerate every case, just
1941 * try and write a register and see if works.
1942 */
1943 static bool
1944 intel_detect_pipelined_register(struct intel_screen *screen,
1945 int reg, uint32_t expected_value, bool reset)
1946 {
1947 if (screen->no_hw)
1948 return false;
1949
1950 struct brw_bo *results, *bo;
1951 uint32_t *batch;
1952 uint32_t offset = 0;
1953 void *map;
1954 bool success = false;
1955
1956 /* Create a zero'ed temporary buffer for reading our results */
1957 results = brw_bo_alloc(screen->bufmgr, "registers", 4096, BRW_MEMZONE_OTHER);
1958 if (results == NULL)
1959 goto err;
1960
1961 bo = brw_bo_alloc(screen->bufmgr, "batchbuffer", 4096, BRW_MEMZONE_OTHER);
1962 if (bo == NULL)
1963 goto err_results;
1964
1965 map = brw_bo_map(NULL, bo, MAP_WRITE);
1966 if (!map)
1967 goto err_batch;
1968
1969 batch = map;
1970
1971 /* Write the register. */
1972 *batch++ = MI_LOAD_REGISTER_IMM | (3 - 2);
1973 *batch++ = reg;
1974 *batch++ = expected_value;
1975
1976 /* Save the register's value back to the buffer. */
1977 *batch++ = MI_STORE_REGISTER_MEM | (3 - 2);
1978 *batch++ = reg;
1979 struct drm_i915_gem_relocation_entry reloc = {
1980 .offset = (char *) batch - (char *) map,
1981 .delta = offset * sizeof(uint32_t),
1982 .target_handle = results->gem_handle,
1983 .read_domains = I915_GEM_DOMAIN_INSTRUCTION,
1984 .write_domain = I915_GEM_DOMAIN_INSTRUCTION,
1985 };
1986 *batch++ = reloc.presumed_offset + reloc.delta;
1987
1988 /* And afterwards clear the register */
1989 if (reset) {
1990 *batch++ = MI_LOAD_REGISTER_IMM | (3 - 2);
1991 *batch++ = reg;
1992 *batch++ = 0;
1993 }
1994
1995 *batch++ = MI_BATCH_BUFFER_END;
1996
1997 struct drm_i915_gem_exec_object2 exec_objects[2] = {
1998 {
1999 .handle = results->gem_handle,
2000 },
2001 {
2002 .handle = bo->gem_handle,
2003 .relocation_count = 1,
2004 .relocs_ptr = (uintptr_t) &reloc,
2005 }
2006 };
2007
2008 struct drm_i915_gem_execbuffer2 execbuf = {
2009 .buffers_ptr = (uintptr_t) exec_objects,
2010 .buffer_count = 2,
2011 .batch_len = ALIGN((char *) batch - (char *) map, 8),
2012 .flags = I915_EXEC_RENDER,
2013 };
2014
2015 /* Don't bother with error checking - if the execbuf fails, the
2016 * value won't be written and we'll just report that there's no access.
2017 */
2018 __DRIscreen *dri_screen = screen->driScrnPriv;
2019 drmIoctl(dri_screen->fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
2020
2021 /* Check whether the value got written. */
2022 void *results_map = brw_bo_map(NULL, results, MAP_READ);
2023 if (results_map) {
2024 success = *((uint32_t *)results_map + offset) == expected_value;
2025 brw_bo_unmap(results);
2026 }
2027
2028 err_batch:
2029 brw_bo_unreference(bo);
2030 err_results:
2031 brw_bo_unreference(results);
2032 err:
2033 return success;
2034 }
2035
2036 static bool
2037 intel_detect_pipelined_so(struct intel_screen *screen)
2038 {
2039 const struct gen_device_info *devinfo = &screen->devinfo;
2040
2041 /* Supposedly, Broadwell just works. */
2042 if (devinfo->gen >= 8)
2043 return true;
2044
2045 if (devinfo->gen <= 6)
2046 return false;
2047
2048 /* See the big explanation about command parser versions below */
2049 if (screen->cmd_parser_version >= (devinfo->is_haswell ? 7 : 2))
2050 return true;
2051
2052 /* We use SO_WRITE_OFFSET0 since you're supposed to write it (unlike the
2053 * statistics registers), and we already reset it to zero before using it.
2054 */
2055 return intel_detect_pipelined_register(screen,
2056 GEN7_SO_WRITE_OFFSET(0),
2057 0x1337d0d0,
2058 false);
2059 }
2060
2061 /**
2062 * Return array of MSAA modes supported by the hardware. The array is
2063 * zero-terminated and sorted in decreasing order.
2064 */
2065 const int*
2066 intel_supported_msaa_modes(const struct intel_screen *screen)
2067 {
2068 static const int gen9_modes[] = {16, 8, 4, 2, 0, -1};
2069 static const int gen8_modes[] = {8, 4, 2, 0, -1};
2070 static const int gen7_modes[] = {8, 4, 0, -1};
2071 static const int gen6_modes[] = {4, 0, -1};
2072 static const int gen4_modes[] = {0, -1};
2073
2074 if (screen->devinfo.gen >= 9) {
2075 return gen9_modes;
2076 } else if (screen->devinfo.gen >= 8) {
2077 return gen8_modes;
2078 } else if (screen->devinfo.gen >= 7) {
2079 return gen7_modes;
2080 } else if (screen->devinfo.gen == 6) {
2081 return gen6_modes;
2082 } else {
2083 return gen4_modes;
2084 }
2085 }
2086
2087 static unsigned
2088 intel_loader_get_cap(const __DRIscreen *dri_screen, enum dri_loader_cap cap)
2089 {
2090 if (dri_screen->dri2.loader && dri_screen->dri2.loader->base.version >= 4 &&
2091 dri_screen->dri2.loader->getCapability)
2092 return dri_screen->dri2.loader->getCapability(dri_screen->loaderPrivate, cap);
2093
2094 if (dri_screen->image.loader && dri_screen->image.loader->base.version >= 2 &&
2095 dri_screen->image.loader->getCapability)
2096 return dri_screen->image.loader->getCapability(dri_screen->loaderPrivate, cap);
2097
2098 return 0;
2099 }
2100
2101 static __DRIconfig**
2102 intel_screen_make_configs(__DRIscreen *dri_screen)
2103 {
2104 static const mesa_format formats[] = {
2105 MESA_FORMAT_B5G6R5_UNORM,
2106 MESA_FORMAT_B8G8R8A8_UNORM,
2107 MESA_FORMAT_B8G8R8X8_UNORM,
2108
2109 MESA_FORMAT_B8G8R8A8_SRGB,
2110
2111 /* For 10 bpc, 30 bit depth framebuffers. */
2112 MESA_FORMAT_B10G10R10A2_UNORM,
2113 MESA_FORMAT_B10G10R10X2_UNORM,
2114
2115 /* The 32-bit RGBA format must not precede the 32-bit BGRA format.
2116 * Likewise for RGBX and BGRX. Otherwise, the GLX client and the GLX
2117 * server may disagree on which format the GLXFBConfig represents,
2118 * resulting in swapped color channels.
2119 *
2120 * The problem, as of 2017-05-30:
2121 * When matching a GLXFBConfig to a __DRIconfig, GLX ignores the channel
2122 * order and chooses the first __DRIconfig with the expected channel
2123 * sizes. Specifically, GLX compares the GLXFBConfig's and __DRIconfig's
2124 * __DRI_ATTRIB_{CHANNEL}_SIZE but ignores __DRI_ATTRIB_{CHANNEL}_MASK.
2125 *
2126 * EGL does not suffer from this problem. It correctly compares the
2127 * channel masks when matching EGLConfig to __DRIconfig.
2128 */
2129
2130 /* Required by Android, for HAL_PIXEL_FORMAT_RGBA_8888. */
2131 MESA_FORMAT_R8G8B8A8_UNORM,
2132
2133 /* Required by Android, for HAL_PIXEL_FORMAT_RGBX_8888. */
2134 MESA_FORMAT_R8G8B8X8_UNORM,
2135
2136 MESA_FORMAT_R8G8B8A8_SRGB,
2137 };
2138
2139 /* GLX_SWAP_COPY_OML is not supported due to page flipping. */
2140 static const GLenum back_buffer_modes[] = {
2141 __DRI_ATTRIB_SWAP_UNDEFINED, __DRI_ATTRIB_SWAP_NONE
2142 };
2143
2144 static const uint8_t singlesample_samples[1] = {0};
2145
2146 struct intel_screen *screen = dri_screen->driverPrivate;
2147 const struct gen_device_info *devinfo = &screen->devinfo;
2148 uint8_t depth_bits[4], stencil_bits[4];
2149 __DRIconfig **configs = NULL;
2150
2151 /* Expose only BGRA ordering if the loader doesn't support RGBA ordering. */
2152 unsigned num_formats;
2153 if (intel_loader_get_cap(dri_screen, DRI_LOADER_CAP_RGBA_ORDERING))
2154 num_formats = ARRAY_SIZE(formats);
2155 else
2156 num_formats = ARRAY_SIZE(formats) - 3; /* all - RGBA_ORDERING formats */
2157
2158 /* Shall we expose 10 bpc formats? */
2159 bool allow_rgb10_configs = driQueryOptionb(&screen->optionCache,
2160 "allow_rgb10_configs");
2161
2162 /* Generate singlesample configs without accumulation buffer. */
2163 for (unsigned i = 0; i < num_formats; i++) {
2164 __DRIconfig **new_configs;
2165 int num_depth_stencil_bits = 2;
2166
2167 if (!allow_rgb10_configs &&
2168 (formats[i] == MESA_FORMAT_B10G10R10A2_UNORM ||
2169 formats[i] == MESA_FORMAT_B10G10R10X2_UNORM))
2170 continue;
2171
2172 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
2173 * buffer that has a different number of bits per pixel than the color
2174 * buffer, gen >= 6 supports this.
2175 */
2176 depth_bits[0] = 0;
2177 stencil_bits[0] = 0;
2178
2179 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
2180 depth_bits[1] = 16;
2181 stencil_bits[1] = 0;
2182 if (devinfo->gen >= 6) {
2183 depth_bits[2] = 24;
2184 stencil_bits[2] = 8;
2185 num_depth_stencil_bits = 3;
2186 }
2187 } else {
2188 depth_bits[1] = 24;
2189 stencil_bits[1] = 8;
2190 }
2191
2192 new_configs = driCreateConfigs(formats[i],
2193 depth_bits,
2194 stencil_bits,
2195 num_depth_stencil_bits,
2196 back_buffer_modes, 2,
2197 singlesample_samples, 1,
2198 false, false, false);
2199 configs = driConcatConfigs(configs, new_configs);
2200 }
2201
2202 /* Generate the minimum possible set of configs that include an
2203 * accumulation buffer.
2204 */
2205 for (unsigned i = 0; i < num_formats; i++) {
2206 __DRIconfig **new_configs;
2207
2208 if (!allow_rgb10_configs &&
2209 (formats[i] == MESA_FORMAT_B10G10R10A2_UNORM ||
2210 formats[i] == MESA_FORMAT_B10G10R10X2_UNORM))
2211 continue;
2212
2213 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
2214 depth_bits[0] = 16;
2215 stencil_bits[0] = 0;
2216 } else {
2217 depth_bits[0] = 24;
2218 stencil_bits[0] = 8;
2219 }
2220
2221 new_configs = driCreateConfigs(formats[i],
2222 depth_bits, stencil_bits, 1,
2223 back_buffer_modes, 1,
2224 singlesample_samples, 1,
2225 true, false, false);
2226 configs = driConcatConfigs(configs, new_configs);
2227 }
2228
2229 /* Generate multisample configs.
2230 *
2231 * This loop breaks early, and hence is a no-op, on gen < 6.
2232 *
2233 * Multisample configs must follow the singlesample configs in order to
2234 * work around an X server bug present in 1.12. The X server chooses to
2235 * associate the first listed RGBA888-Z24S8 config, regardless of its
2236 * sample count, with the 32-bit depth visual used for compositing.
2237 *
2238 * Only doublebuffer configs with GLX_SWAP_UNDEFINED_OML behavior are
2239 * supported. Singlebuffer configs are not supported because no one wants
2240 * them.
2241 */
2242 for (unsigned i = 0; i < num_formats; i++) {
2243 if (devinfo->gen < 6)
2244 break;
2245
2246 if (!allow_rgb10_configs &&
2247 (formats[i] == MESA_FORMAT_B10G10R10A2_UNORM ||
2248 formats[i] == MESA_FORMAT_B10G10R10X2_UNORM))
2249 continue;
2250
2251 __DRIconfig **new_configs;
2252 const int num_depth_stencil_bits = 2;
2253 int num_msaa_modes = 0;
2254 const uint8_t *multisample_samples = NULL;
2255
2256 depth_bits[0] = 0;
2257 stencil_bits[0] = 0;
2258
2259 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
2260 depth_bits[1] = 16;
2261 stencil_bits[1] = 0;
2262 } else {
2263 depth_bits[1] = 24;
2264 stencil_bits[1] = 8;
2265 }
2266
2267 if (devinfo->gen >= 9) {
2268 static const uint8_t multisample_samples_gen9[] = {2, 4, 8, 16};
2269 multisample_samples = multisample_samples_gen9;
2270 num_msaa_modes = ARRAY_SIZE(multisample_samples_gen9);
2271 } else if (devinfo->gen == 8) {
2272 static const uint8_t multisample_samples_gen8[] = {2, 4, 8};
2273 multisample_samples = multisample_samples_gen8;
2274 num_msaa_modes = ARRAY_SIZE(multisample_samples_gen8);
2275 } else if (devinfo->gen == 7) {
2276 static const uint8_t multisample_samples_gen7[] = {4, 8};
2277 multisample_samples = multisample_samples_gen7;
2278 num_msaa_modes = ARRAY_SIZE(multisample_samples_gen7);
2279 } else if (devinfo->gen == 6) {
2280 static const uint8_t multisample_samples_gen6[] = {4};
2281 multisample_samples = multisample_samples_gen6;
2282 num_msaa_modes = ARRAY_SIZE(multisample_samples_gen6);
2283 }
2284
2285 new_configs = driCreateConfigs(formats[i],
2286 depth_bits,
2287 stencil_bits,
2288 num_depth_stencil_bits,
2289 back_buffer_modes, 1,
2290 multisample_samples,
2291 num_msaa_modes,
2292 false, false, false);
2293 configs = driConcatConfigs(configs, new_configs);
2294 }
2295
2296 if (configs == NULL) {
2297 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
2298 __LINE__);
2299 return NULL;
2300 }
2301
2302 return configs;
2303 }
2304
2305 static void
2306 set_max_gl_versions(struct intel_screen *screen)
2307 {
2308 __DRIscreen *dri_screen = screen->driScrnPriv;
2309 const bool has_astc = screen->devinfo.gen >= 9;
2310
2311 switch (screen->devinfo.gen) {
2312 case 11:
2313 case 10:
2314 case 9:
2315 case 8:
2316 dri_screen->max_gl_core_version = 45;
2317 dri_screen->max_gl_compat_version = 30;
2318 dri_screen->max_gl_es1_version = 11;
2319 dri_screen->max_gl_es2_version = has_astc ? 32 : 31;
2320 break;
2321 case 7:
2322 dri_screen->max_gl_core_version = 33;
2323 if (can_do_pipelined_register_writes(screen)) {
2324 dri_screen->max_gl_core_version = 42;
2325 if (screen->devinfo.is_haswell && can_do_compute_dispatch(screen))
2326 dri_screen->max_gl_core_version = 43;
2327 if (screen->devinfo.is_haswell && can_do_mi_math_and_lrr(screen))
2328 dri_screen->max_gl_core_version = 45;
2329 }
2330 dri_screen->max_gl_compat_version = 30;
2331 dri_screen->max_gl_es1_version = 11;
2332 dri_screen->max_gl_es2_version = screen->devinfo.is_haswell ? 31 : 30;
2333 break;
2334 case 6:
2335 dri_screen->max_gl_core_version = 33;
2336 dri_screen->max_gl_compat_version = 30;
2337 dri_screen->max_gl_es1_version = 11;
2338 dri_screen->max_gl_es2_version = 30;
2339 break;
2340 case 5:
2341 case 4:
2342 dri_screen->max_gl_core_version = 0;
2343 dri_screen->max_gl_compat_version = 21;
2344 dri_screen->max_gl_es1_version = 11;
2345 dri_screen->max_gl_es2_version = 20;
2346 break;
2347 default:
2348 unreachable("unrecognized intel_screen::gen");
2349 }
2350 }
2351
2352 /**
2353 * Return the revision (generally the revid field of the PCI header) of the
2354 * graphics device.
2355 */
2356 int
2357 intel_device_get_revision(int fd)
2358 {
2359 struct drm_i915_getparam gp;
2360 int revision;
2361 int ret;
2362
2363 memset(&gp, 0, sizeof(gp));
2364 gp.param = I915_PARAM_REVISION;
2365 gp.value = &revision;
2366
2367 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
2368 if (ret)
2369 revision = -1;
2370
2371 return revision;
2372 }
2373
2374 static void
2375 shader_debug_log_mesa(void *data, const char *fmt, ...)
2376 {
2377 struct brw_context *brw = (struct brw_context *)data;
2378 va_list args;
2379
2380 va_start(args, fmt);
2381 GLuint msg_id = 0;
2382 _mesa_gl_vdebug(&brw->ctx, &msg_id,
2383 MESA_DEBUG_SOURCE_SHADER_COMPILER,
2384 MESA_DEBUG_TYPE_OTHER,
2385 MESA_DEBUG_SEVERITY_NOTIFICATION, fmt, args);
2386 va_end(args);
2387 }
2388
2389 static void
2390 shader_perf_log_mesa(void *data, const char *fmt, ...)
2391 {
2392 struct brw_context *brw = (struct brw_context *)data;
2393
2394 va_list args;
2395 va_start(args, fmt);
2396
2397 if (unlikely(INTEL_DEBUG & DEBUG_PERF)) {
2398 va_list args_copy;
2399 va_copy(args_copy, args);
2400 vfprintf(stderr, fmt, args_copy);
2401 va_end(args_copy);
2402 }
2403
2404 if (brw->perf_debug) {
2405 GLuint msg_id = 0;
2406 _mesa_gl_vdebug(&brw->ctx, &msg_id,
2407 MESA_DEBUG_SOURCE_SHADER_COMPILER,
2408 MESA_DEBUG_TYPE_PERFORMANCE,
2409 MESA_DEBUG_SEVERITY_MEDIUM, fmt, args);
2410 }
2411 va_end(args);
2412 }
2413
2414 /**
2415 * This is the driver specific part of the createNewScreen entry point.
2416 * Called when using DRI2.
2417 *
2418 * \return the struct gl_config supported by this driver
2419 */
2420 static const
2421 __DRIconfig **intelInitScreen2(__DRIscreen *dri_screen)
2422 {
2423 struct intel_screen *screen;
2424
2425 if (dri_screen->image.loader) {
2426 } else if (dri_screen->dri2.loader->base.version <= 2 ||
2427 dri_screen->dri2.loader->getBuffersWithFormat == NULL) {
2428 fprintf(stderr,
2429 "\nERROR! DRI2 loader with getBuffersWithFormat() "
2430 "support required\n");
2431 return NULL;
2432 }
2433
2434 /* Allocate the private area */
2435 screen = rzalloc(NULL, struct intel_screen);
2436 if (!screen) {
2437 fprintf(stderr, "\nERROR! Allocating private area failed\n");
2438 return NULL;
2439 }
2440 /* parse information in __driConfigOptions */
2441 driOptionCache options;
2442 memset(&options, 0, sizeof(options));
2443
2444 driParseOptionInfo(&options, brw_config_options.xml);
2445 driParseConfigFiles(&screen->optionCache, &options, dri_screen->myNum, "i965");
2446 driDestroyOptionCache(&options);
2447
2448 screen->driScrnPriv = dri_screen;
2449 dri_screen->driverPrivate = (void *) screen;
2450
2451 screen->deviceID = gen_get_pci_device_id_override();
2452 if (screen->deviceID < 0)
2453 screen->deviceID = intel_get_integer(screen, I915_PARAM_CHIPSET_ID);
2454 else
2455 screen->no_hw = true;
2456
2457 if (!gen_get_device_info(screen->deviceID, &screen->devinfo))
2458 return NULL;
2459
2460 if (!intel_init_bufmgr(screen))
2461 return NULL;
2462
2463 const struct gen_device_info *devinfo = &screen->devinfo;
2464
2465 brw_process_intel_debug_variable();
2466
2467 if ((INTEL_DEBUG & DEBUG_SHADER_TIME) && devinfo->gen < 7) {
2468 fprintf(stderr,
2469 "shader_time debugging requires gen7 (Ivybridge) or better.\n");
2470 INTEL_DEBUG &= ~DEBUG_SHADER_TIME;
2471 }
2472
2473 if (intel_get_integer(screen, I915_PARAM_MMAP_GTT_VERSION) >= 1) {
2474 /* Theorectically unlimited! At least for individual objects...
2475 *
2476 * Currently the entire (global) address space for all GTT maps is
2477 * limited to 64bits. That is all objects on the system that are
2478 * setup for GTT mmapping must fit within 64bits. An attempt to use
2479 * one that exceeds the limit with fail in brw_bo_map_gtt().
2480 *
2481 * Long before we hit that limit, we will be practically limited by
2482 * that any single object must fit in physical memory (RAM). The upper
2483 * limit on the CPU's address space is currently 48bits (Skylake), of
2484 * which only 39bits can be physical memory. (The GPU itself also has
2485 * a 48bit addressable virtual space.) We can fit over 32 million
2486 * objects of the current maximum allocable size before running out
2487 * of mmap space.
2488 */
2489 screen->max_gtt_map_object_size = UINT64_MAX;
2490 } else {
2491 /* Estimate the size of the mappable aperture into the GTT. There's an
2492 * ioctl to get the whole GTT size, but not one to get the mappable subset.
2493 * It turns out it's basically always 256MB, though some ancient hardware
2494 * was smaller.
2495 */
2496 uint32_t gtt_size = 256 * 1024 * 1024;
2497
2498 /* We don't want to map two objects such that a memcpy between them would
2499 * just fault one mapping in and then the other over and over forever. So
2500 * we would need to divide the GTT size by 2. Additionally, some GTT is
2501 * taken up by things like the framebuffer and the ringbuffer and such, so
2502 * be more conservative.
2503 */
2504 screen->max_gtt_map_object_size = gtt_size / 4;
2505 }
2506
2507 screen->aperture_threshold = get_aperture_size(dri_screen->fd) * 3 / 4;
2508
2509 screen->hw_has_swizzling = intel_detect_swizzling(screen);
2510 screen->hw_has_timestamp = intel_detect_timestamp(screen);
2511
2512 isl_device_init(&screen->isl_dev, &screen->devinfo,
2513 screen->hw_has_swizzling);
2514
2515 if (devinfo->gen >= 10)
2516 intel_cs_timestamp_frequency(screen);
2517
2518 /* GENs prior to 8 do not support EU/Subslice info */
2519 if (devinfo->gen >= 8) {
2520 intel_detect_sseu(screen);
2521 } else if (devinfo->gen == 7) {
2522 screen->subslice_total = 1 << (devinfo->gt - 1);
2523 }
2524
2525 /* Gen7-7.5 kernel requirements / command parser saga:
2526 *
2527 * - pre-v3.16:
2528 * Haswell and Baytrail cannot use any privileged batchbuffer features.
2529 *
2530 * Ivybridge has aliasing PPGTT on by default, which accidentally marks
2531 * all batches secure, allowing them to use any feature with no checking.
2532 * This is effectively equivalent to a command parser version of
2533 * \infinity - everything is possible.
2534 *
2535 * The command parser does not exist, and querying the version will
2536 * return -EINVAL.
2537 *
2538 * - v3.16:
2539 * The kernel enables the command parser by default, for systems with
2540 * aliasing PPGTT enabled (Ivybridge and Haswell). However, the
2541 * hardware checker is still enabled, so Haswell and Baytrail cannot
2542 * do anything.
2543 *
2544 * Ivybridge goes from "everything is possible" to "only what the
2545 * command parser allows" (if the user boots with i915.cmd_parser=0,
2546 * then everything is possible again). We can only safely use features
2547 * allowed by the supported command parser version.
2548 *
2549 * Annoyingly, I915_PARAM_CMD_PARSER_VERSION reports the static version
2550 * implemented by the kernel, even if it's turned off. So, checking
2551 * for version > 0 does not mean that you can write registers. We have
2552 * to try it and see. The version does, however, indicate the age of
2553 * the kernel.
2554 *
2555 * Instead of matching the hardware checker's behavior of converting
2556 * privileged commands to MI_NOOP, it makes execbuf2 start returning
2557 * -EINVAL, making it dangerous to try and use privileged features.
2558 *
2559 * Effective command parser versions:
2560 * - Haswell: 0 (reporting 1, writes don't work)
2561 * - Baytrail: 0 (reporting 1, writes don't work)
2562 * - Ivybridge: 1 (enabled) or infinite (disabled)
2563 *
2564 * - v3.17:
2565 * Baytrail aliasing PPGTT is enabled, making it like Ivybridge:
2566 * effectively version 1 (enabled) or infinite (disabled).
2567 *
2568 * - v3.19: f1f55cc0556031c8ee3fe99dae7251e78b9b653b
2569 * Command parser v2 supports predicate writes.
2570 *
2571 * - Haswell: 0 (reporting 1, writes don't work)
2572 * - Baytrail: 2 (enabled) or infinite (disabled)
2573 * - Ivybridge: 2 (enabled) or infinite (disabled)
2574 *
2575 * So version >= 2 is enough to know that Ivybridge and Baytrail
2576 * will work. Haswell still can't do anything.
2577 *
2578 * - v4.0: Version 3 happened. Largely not relevant.
2579 *
2580 * - v4.1: 6702cf16e0ba8b0129f5aa1b6609d4e9c70bc13b
2581 * L3 config registers are properly saved and restored as part
2582 * of the hardware context. We can approximately detect this point
2583 * in time by checking if I915_PARAM_REVISION is recognized - it
2584 * landed in a later commit, but in the same release cycle.
2585 *
2586 * - v4.2: 245054a1fe33c06ad233e0d58a27ec7b64db9284
2587 * Command parser finally gains secure batch promotion. On Haswell,
2588 * the hardware checker gets disabled, which finally allows it to do
2589 * privileged commands.
2590 *
2591 * I915_PARAM_CMD_PARSER_VERSION reports 3. Effective versions:
2592 * - Haswell: 3 (enabled) or 0 (disabled)
2593 * - Baytrail: 3 (enabled) or infinite (disabled)
2594 * - Ivybridge: 3 (enabled) or infinite (disabled)
2595 *
2596 * Unfortunately, detecting this point in time is tricky, because
2597 * no version bump happened when this important change occurred.
2598 * On Haswell, if we can write any register, then the kernel is at
2599 * least this new, and we can start trusting the version number.
2600 *
2601 * - v4.4: 2bbe6bbb0dc94fd4ce287bdac9e1bd184e23057b and
2602 * Command parser reaches version 4, allowing access to Haswell
2603 * atomic scratch and chicken3 registers. If version >= 4, we know
2604 * the kernel is new enough to support privileged features on all
2605 * hardware. However, the user might have disabled it...and the
2606 * kernel will still report version 4. So we still have to guess
2607 * and check.
2608 *
2609 * - v4.4: 7b9748cb513a6bef4af87b79f0da3ff7e8b56cd8
2610 * Command parser v5 whitelists indirect compute shader dispatch
2611 * registers, needed for OpenGL 4.3 and later.
2612 *
2613 * - v4.8:
2614 * Command parser v7 lets us use MI_MATH on Haswell.
2615 *
2616 * Additionally, the kernel begins reporting version 0 when
2617 * the command parser is disabled, allowing us to skip the
2618 * guess-and-check step on Haswell. Unfortunately, this also
2619 * means that we can no longer use it as an indicator of the
2620 * age of the kernel.
2621 */
2622 if (intel_get_param(screen, I915_PARAM_CMD_PARSER_VERSION,
2623 &screen->cmd_parser_version) < 0) {
2624 /* Command parser does not exist - getparam is unrecognized */
2625 screen->cmd_parser_version = 0;
2626 }
2627
2628 /* Kernel 4.13 retuired for exec object capture */
2629 if (intel_get_boolean(screen, I915_PARAM_HAS_EXEC_CAPTURE)) {
2630 screen->kernel_features |= KERNEL_ALLOWS_EXEC_CAPTURE;
2631 }
2632
2633 if (intel_get_boolean(screen, I915_PARAM_HAS_EXEC_BATCH_FIRST)) {
2634 screen->kernel_features |= KERNEL_ALLOWS_EXEC_BATCH_FIRST;
2635 }
2636
2637 if (!intel_detect_pipelined_so(screen)) {
2638 /* We can't do anything, so the effective version is 0. */
2639 screen->cmd_parser_version = 0;
2640 } else {
2641 screen->kernel_features |= KERNEL_ALLOWS_SOL_OFFSET_WRITES;
2642 }
2643
2644 if (devinfo->gen >= 8 || screen->cmd_parser_version >= 2)
2645 screen->kernel_features |= KERNEL_ALLOWS_PREDICATE_WRITES;
2646
2647 /* Haswell requires command parser version 4 in order to have L3
2648 * atomic scratch1 and chicken3 bits
2649 */
2650 if (devinfo->is_haswell && screen->cmd_parser_version >= 4) {
2651 screen->kernel_features |=
2652 KERNEL_ALLOWS_HSW_SCRATCH1_AND_ROW_CHICKEN3;
2653 }
2654
2655 /* Haswell requires command parser version 6 in order to write to the
2656 * MI_MATH GPR registers, and version 7 in order to use
2657 * MI_LOAD_REGISTER_REG (which all users of MI_MATH use).
2658 */
2659 if (devinfo->gen >= 8 ||
2660 (devinfo->is_haswell && screen->cmd_parser_version >= 7)) {
2661 screen->kernel_features |= KERNEL_ALLOWS_MI_MATH_AND_LRR;
2662 }
2663
2664 /* Gen7 needs at least command parser version 5 to support compute */
2665 if (devinfo->gen >= 8 || screen->cmd_parser_version >= 5)
2666 screen->kernel_features |= KERNEL_ALLOWS_COMPUTE_DISPATCH;
2667
2668 if (intel_get_boolean(screen, I915_PARAM_HAS_CONTEXT_ISOLATION))
2669 screen->kernel_features |= KERNEL_ALLOWS_CONTEXT_ISOLATION;
2670
2671 const char *force_msaa = getenv("INTEL_FORCE_MSAA");
2672 if (force_msaa) {
2673 screen->winsys_msaa_samples_override =
2674 intel_quantize_num_samples(screen, atoi(force_msaa));
2675 printf("Forcing winsys sample count to %d\n",
2676 screen->winsys_msaa_samples_override);
2677 } else {
2678 screen->winsys_msaa_samples_override = -1;
2679 }
2680
2681 set_max_gl_versions(screen);
2682
2683 /* Notification of GPU resets requires hardware contexts and a kernel new
2684 * enough to support DRM_IOCTL_I915_GET_RESET_STATS. If the ioctl is
2685 * supported, calling it with a context of 0 will either generate EPERM or
2686 * no error. If the ioctl is not supported, it always generate EINVAL.
2687 * Use this to determine whether to advertise the __DRI2_ROBUSTNESS
2688 * extension to the loader.
2689 *
2690 * Don't even try on pre-Gen6, since we don't attempt to use contexts there.
2691 */
2692 if (devinfo->gen >= 6) {
2693 struct drm_i915_reset_stats stats;
2694 memset(&stats, 0, sizeof(stats));
2695
2696 const int ret = drmIoctl(dri_screen->fd, DRM_IOCTL_I915_GET_RESET_STATS, &stats);
2697
2698 screen->has_context_reset_notification =
2699 (ret != -1 || errno != EINVAL);
2700 }
2701
2702 dri_screen->extensions = !screen->has_context_reset_notification
2703 ? screenExtensions : intelRobustScreenExtensions;
2704
2705 screen->compiler = brw_compiler_create(screen, devinfo);
2706 screen->compiler->shader_debug_log = shader_debug_log_mesa;
2707 screen->compiler->shader_perf_log = shader_perf_log_mesa;
2708
2709 /* Changing the meaning of constant buffer pointers from a dynamic state
2710 * offset to an absolute address is only safe if the kernel isolates other
2711 * contexts from our changes.
2712 */
2713 screen->compiler->constant_buffer_0_is_relative = devinfo->gen < 8 ||
2714 !(screen->kernel_features & KERNEL_ALLOWS_CONTEXT_ISOLATION);
2715
2716 screen->compiler->supports_pull_constants = true;
2717
2718 screen->has_exec_fence =
2719 intel_get_boolean(screen, I915_PARAM_HAS_EXEC_FENCE);
2720
2721 intel_screen_init_surface_formats(screen);
2722
2723 if (INTEL_DEBUG & (DEBUG_BATCH | DEBUG_SUBMIT)) {
2724 unsigned int caps = intel_get_integer(screen, I915_PARAM_HAS_SCHEDULER);
2725 if (caps) {
2726 fprintf(stderr, "Kernel scheduler detected: %08x\n", caps);
2727 if (caps & I915_SCHEDULER_CAP_PRIORITY)
2728 fprintf(stderr, " - User priority sorting enabled\n");
2729 if (caps & I915_SCHEDULER_CAP_PREEMPTION)
2730 fprintf(stderr, " - Preemption enabled\n");
2731 }
2732 }
2733
2734 brw_disk_cache_init(screen);
2735
2736 return (const __DRIconfig**) intel_screen_make_configs(dri_screen);
2737 }
2738
2739 struct intel_buffer {
2740 __DRIbuffer base;
2741 struct brw_bo *bo;
2742 };
2743
2744 static __DRIbuffer *
2745 intelAllocateBuffer(__DRIscreen *dri_screen,
2746 unsigned attachment, unsigned format,
2747 int width, int height)
2748 {
2749 struct intel_buffer *intelBuffer;
2750 struct intel_screen *screen = dri_screen->driverPrivate;
2751
2752 assert(attachment == __DRI_BUFFER_FRONT_LEFT ||
2753 attachment == __DRI_BUFFER_BACK_LEFT);
2754
2755 intelBuffer = calloc(1, sizeof *intelBuffer);
2756 if (intelBuffer == NULL)
2757 return NULL;
2758
2759 /* The front and back buffers are color buffers, which are X tiled. GEN9+
2760 * supports Y tiled and compressed buffers, but there is no way to plumb that
2761 * through to here. */
2762 uint32_t pitch;
2763 int cpp = format / 8;
2764 intelBuffer->bo = brw_bo_alloc_tiled_2d(screen->bufmgr,
2765 "intelAllocateBuffer",
2766 width,
2767 height,
2768 cpp,
2769 BRW_MEMZONE_OTHER,
2770 I915_TILING_X, &pitch,
2771 BO_ALLOC_BUSY);
2772
2773 if (intelBuffer->bo == NULL) {
2774 free(intelBuffer);
2775 return NULL;
2776 }
2777
2778 brw_bo_flink(intelBuffer->bo, &intelBuffer->base.name);
2779
2780 intelBuffer->base.attachment = attachment;
2781 intelBuffer->base.cpp = cpp;
2782 intelBuffer->base.pitch = pitch;
2783
2784 return &intelBuffer->base;
2785 }
2786
2787 static void
2788 intelReleaseBuffer(__DRIscreen *dri_screen, __DRIbuffer *buffer)
2789 {
2790 struct intel_buffer *intelBuffer = (struct intel_buffer *) buffer;
2791
2792 brw_bo_unreference(intelBuffer->bo);
2793 free(intelBuffer);
2794 }
2795
2796 static const struct __DriverAPIRec brw_driver_api = {
2797 .InitScreen = intelInitScreen2,
2798 .DestroyScreen = intelDestroyScreen,
2799 .CreateContext = brwCreateContext,
2800 .DestroyContext = intelDestroyContext,
2801 .CreateBuffer = intelCreateBuffer,
2802 .DestroyBuffer = intelDestroyBuffer,
2803 .MakeCurrent = intelMakeCurrent,
2804 .UnbindContext = intelUnbindContext,
2805 .AllocateBuffer = intelAllocateBuffer,
2806 .ReleaseBuffer = intelReleaseBuffer
2807 };
2808
2809 static const struct __DRIDriverVtableExtensionRec brw_vtable = {
2810 .base = { __DRI_DRIVER_VTABLE, 1 },
2811 .vtable = &brw_driver_api,
2812 };
2813
2814 static const __DRIextension *brw_driver_extensions[] = {
2815 &driCoreExtension.base,
2816 &driImageDriverExtension.base,
2817 &driDRI2Extension.base,
2818 &brw_vtable.base,
2819 &brw_config_options.base,
2820 NULL
2821 };
2822
2823 PUBLIC const __DRIextension **__driDriverGetExtensions_i965(void)
2824 {
2825 globalDriverAPI = &brw_driver_api;
2826
2827 return brw_driver_extensions;
2828 }