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