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