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