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