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