forked from intel/minigbm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i915.c
596 lines (492 loc) · 16 KB
/
i915.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
* Copyright 2014 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifdef DRV_I915
#include <errno.h>
#include <i915_drm.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <xf86drm.h>
#include "drv_priv.h"
#include "helpers.h"
#include "util.h"
#include "i915_private.h"
#define I915_CACHELINE_SIZE 64
#define I915_CACHELINE_MASK (I915_CACHELINE_SIZE - 1)
static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB1555,
DRM_FORMAT_ARGB8888, DRM_FORMAT_RGB565,
DRM_FORMAT_XBGR2101010, DRM_FORMAT_XBGR8888,
DRM_FORMAT_XRGB1555, DRM_FORMAT_XRGB2101010,
DRM_FORMAT_XRGB8888 };
static const uint32_t tileable_texture_source_formats[] = { DRM_FORMAT_GR88, DRM_FORMAT_NV12,
DRM_FORMAT_R8, DRM_FORMAT_UYVY,
DRM_FORMAT_YUYV, DRM_FORMAT_YVYU, DRM_FORMAT_VYUY };
static const uint32_t texture_source_formats[] = { DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID };
struct i915_device {
uint32_t gen;
int32_t has_llc;
uint64_t cursor_width;
uint64_t cursor_height;
};
static uint32_t i915_get_gen(int device_id)
{
const uint16_t gen3_ids[] = { 0x2582, 0x2592, 0x2772, 0x27A2, 0x27AE,
0x29C2, 0x29B2, 0x29D2, 0xA001, 0xA011 };
unsigned i;
for (i = 0; i < ARRAY_SIZE(gen3_ids); i++)
if (gen3_ids[i] == device_id)
return 3;
return 4;
}
static int i915_add_kms_item(struct driver *drv, const struct kms_item *item)
{
uint32_t i;
struct combination *combo;
/*
* Older hardware can't scanout Y-tiled formats. Newer devices can, and
* report this functionality via format modifiers.
*/
for (i = 0; i < drv->combos.size; i++) {
combo = &drv->combos.data[i];
if (combo->format != item->format)
continue;
if (item->modifier == DRM_FORMAT_MOD_INVALID &&
combo->metadata.tiling == I915_TILING_X) {
/*
* FIXME: drv_query_kms() does not report the available modifiers
* yet, but we know that all hardware can scanout from X-tiled
* buffers, so let's add this to our combinations, except for
* cursor, which must not be tiled.
*/
combo->use_flags |= item->use_flags & ~BO_USE_CURSOR;
}
if (combo->metadata.modifier == item->modifier)
combo->use_flags |= item->use_flags;
}
return 0;
}
static int i915_add_combinations(struct driver *drv)
{
int ret;
uint32_t i, num_items;
struct kms_item *items;
struct format_metadata metadata;
uint64_t render_use_flags, texture_use_flags;
render_use_flags = BO_USE_RENDER_MASK;
texture_use_flags = BO_USE_TEXTURE_MASK;
metadata.tiling = I915_TILING_NONE;
metadata.priority = 1;
metadata.modifier = DRM_FORMAT_MOD_LINEAR;
ret = drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
&metadata, render_use_flags);
if (ret)
return ret;
ret = drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
&metadata, texture_use_flags);
if (ret)
return ret;
ret = drv_add_combinations(drv, tileable_texture_source_formats,
ARRAY_SIZE(tileable_texture_source_formats), &metadata,
texture_use_flags);
if (ret)
return ret;
drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
/* IPU3 camera ISP supports only NV12 output. */
drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
/*
* R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
* from camera.
*/
drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
render_use_flags &= ~BO_USE_RENDERSCRIPT;
render_use_flags &= ~BO_USE_SW_WRITE_OFTEN;
render_use_flags &= ~BO_USE_SW_READ_OFTEN;
render_use_flags &= ~BO_USE_LINEAR;
texture_use_flags &= ~BO_USE_RENDERSCRIPT;
texture_use_flags &= ~BO_USE_SW_WRITE_OFTEN;
texture_use_flags &= ~BO_USE_SW_READ_OFTEN;
texture_use_flags &= ~BO_USE_LINEAR;
metadata.tiling = I915_TILING_X;
metadata.priority = 2;
metadata.modifier = I915_FORMAT_MOD_X_TILED;
ret = drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
&metadata, render_use_flags);
if (ret)
return ret;
ret = drv_add_combinations(drv, tileable_texture_source_formats,
ARRAY_SIZE(tileable_texture_source_formats), &metadata,
texture_use_flags);
if (ret)
return ret;
metadata.tiling = I915_TILING_Y;
metadata.priority = 3;
metadata.modifier = I915_FORMAT_MOD_Y_TILED;
ret = drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
&metadata, render_use_flags);
if (ret)
return ret;
ret = drv_add_combinations(drv, tileable_texture_source_formats,
ARRAY_SIZE(tileable_texture_source_formats), &metadata,
texture_use_flags);
if (ret)
return ret;
i915_private_add_combinations(drv);
items = drv_query_kms(drv, &num_items);
if (!items || !num_items)
return 0;
for (i = 0; i < num_items; i++) {
ret = i915_add_kms_item(drv, &items[i]);
if (ret) {
free(items);
return ret;
}
}
free(items);
return 0;
}
static int i915_align_dimensions(struct bo *bo, uint32_t tiling, uint32_t *stride,
uint32_t *aligned_height)
{
struct i915_device *i915 = bo->drv->priv;
uint32_t horizontal_alignment = 4;
uint32_t vertical_alignment = 4;
switch (tiling) {
default:
case I915_TILING_NONE:
horizontal_alignment = 64;
break;
case I915_TILING_X:
horizontal_alignment = 512;
vertical_alignment = 8;
break;
case I915_TILING_Y:
if (i915->gen == 3) {
horizontal_alignment = 512;
vertical_alignment = 8;
} else {
horizontal_alignment = 128;
vertical_alignment = 32;
}
break;
}
/*
* The alignment calculated above is based on the full size luma plane and to have chroma
* planes properly aligned with subsampled formats, we need to multiply luma alignment by
* subsampling factor.
*/
switch (bo->format) {
case DRM_FORMAT_YVU420_ANDROID:
case DRM_FORMAT_YVU420:
horizontal_alignment *= 2;
/* Fall through */
case DRM_FORMAT_NV12:
vertical_alignment *= 2;
break;
}
i915_private_align_dimensions(bo->format, &vertical_alignment);
*aligned_height = ALIGN(bo->height, vertical_alignment);
if (i915->gen > 3) {
*stride = ALIGN(*stride, horizontal_alignment);
} else {
while (*stride > horizontal_alignment)
horizontal_alignment <<= 1;
*stride = horizontal_alignment;
}
if (i915->gen <= 3 && *stride > 8192)
return -EINVAL;
return 0;
}
static void i915_clflush(void *start, size_t size)
{
void *p = (void *)(((uintptr_t)start) & ~I915_CACHELINE_MASK);
void *end = (void *)((uintptr_t)start + size);
__builtin_ia32_mfence();
while (p < end) {
__builtin_ia32_clflush(p);
p = (void *)((uintptr_t)p + I915_CACHELINE_SIZE);
}
}
static int i915_init(struct driver *drv)
{
int ret;
int device_id;
struct i915_device *i915;
drm_i915_getparam_t get_param;
i915 = calloc(1, sizeof(*i915));
if (!i915)
return -ENOMEM;
memset(&get_param, 0, sizeof(get_param));
get_param.param = I915_PARAM_CHIPSET_ID;
get_param.value = &device_id;
ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
if (ret) {
fprintf(stderr, "drv: Failed to get I915_PARAM_CHIPSET_ID\n");
free(i915);
return -EINVAL;
}
i915->gen = i915_get_gen(device_id);
memset(&get_param, 0, sizeof(get_param));
get_param.param = I915_PARAM_HAS_LLC;
get_param.value = &i915->has_llc;
ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
if (ret) {
fprintf(stderr, "drv: Failed to get I915_PARAM_HAS_LLC\n");
free(i915);
return -EINVAL;
}
drv->priv = i915;
i915_private_init(drv, &i915->cursor_width, &i915->cursor_height);
return i915_add_combinations(drv);
}
static int i915_bo_create_for_modifier(struct bo *bo, uint32_t width, uint32_t height,
uint32_t format, uint64_t modifier)
{
int ret;
size_t plane;
uint32_t stride;
struct drm_i915_gem_create gem_create;
struct drm_i915_gem_set_tiling gem_set_tiling;
struct i915_device *i915_dev = (struct i915_device *)bo->drv->priv;
switch (modifier) {
case DRM_FORMAT_MOD_LINEAR:
bo->tiling = I915_TILING_NONE;
break;
case I915_FORMAT_MOD_X_TILED:
bo->tiling = I915_TILING_X;
break;
case I915_FORMAT_MOD_Y_TILED:
bo->tiling = I915_TILING_Y;
break;
}
stride = drv_stride_from_format(format, width, 0);
/*
* Align cursor width and height to values expected by Intel
* HW.
*/
if (bo->use_flags & BO_USE_CURSOR) {
width = ALIGN(width, i915_dev->cursor_width);
height = ALIGN(height, i915_dev->cursor_height);
stride = drv_stride_from_format(format, width, 0);
} else {
ret = i915_align_dimensions(bo, bo->tiling, &stride, &height);
if (ret)
return ret;
}
/*
* HAL_PIXEL_FORMAT_YV12 requires the buffer height not be aligned, but we need to keep
* total size as with aligned height to ensure enough padding space after each plane to
* satisfy GPU alignment requirements.
*
* We do it by first calling drv_bo_from_format() with aligned height and
* DRM_FORMAT_YVU420, which allows height alignment, saving the total size it calculates
* and then calling it again with requested parameters.
*
* This relies on the fact that i965 driver uses separate surfaces for each plane and
* contents of padding bytes is not affected, as it is only used to satisfy GPU cache
* requests.
*
* This is enforced by Mesa in src/intel/isl/isl_gen8.c, inside
* isl_gen8_choose_image_alignment_el(), which is used for GEN9 and GEN8.
*/
if (format == DRM_FORMAT_YVU420_ANDROID) {
uint32_t unaligned_height = bo->height;
size_t total_size;
drv_bo_from_format(bo, stride, height, DRM_FORMAT_YVU420);
total_size = bo->total_size;
drv_bo_from_format(bo, stride, unaligned_height, format);
bo->total_size = total_size;
} else {
drv_bo_from_format(bo, stride, height, format);
}
/*
* Quoting Mesa ISL library:
*
* - For linear surfaces, additional padding of 64 bytes is required at
* the bottom of the surface. This is in addition to the padding
* required above.
*/
if (bo->tiling == I915_TILING_NONE)
bo->total_size += 64;
/*
* Ensure we pass aligned width/height.
*/
bo->width = width;
bo->height = height;
memset(&gem_create, 0, sizeof(gem_create));
gem_create.size = bo->total_size;
ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
if (ret) {
fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_CREATE failed (size=%llu)\n",
gem_create.size);
return ret;
}
for (plane = 0; plane < bo->num_planes; plane++)
bo->handles[plane].u32 = gem_create.handle;
memset(&gem_set_tiling, 0, sizeof(gem_set_tiling));
gem_set_tiling.handle = bo->handles[0].u32;
gem_set_tiling.tiling_mode = bo->tiling;
gem_set_tiling.stride = bo->strides[0];
ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_TILING, &gem_set_tiling);
if (ret) {
struct drm_gem_close gem_close;
memset(&gem_close, 0, sizeof(gem_close));
gem_close.handle = bo->handles[0].u32;
drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_SET_TILING failed with %d", errno);
return -errno;
}
return 0;
}
static int i915_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
uint64_t use_flags)
{
struct combination *combo;
combo = drv_get_combination(bo->drv, format, use_flags);
if (!combo)
return -EINVAL;
return i915_bo_create_for_modifier(bo, width, height, format, combo->metadata.modifier);
}
static int i915_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
uint32_t format, const uint64_t *modifiers, uint32_t count)
{
static const uint64_t modifier_order[] = {
I915_FORMAT_MOD_Y_TILED, I915_FORMAT_MOD_X_TILED, DRM_FORMAT_MOD_LINEAR,
};
uint64_t modifier;
modifier = drv_pick_modifier(modifiers, count, modifier_order, ARRAY_SIZE(modifier_order));
bo->format_modifiers[0] = modifier;
return i915_bo_create_for_modifier(bo, width, height, format, modifier);
}
static void i915_close(struct driver *drv)
{
free(drv->priv);
drv->priv = NULL;
}
static int i915_bo_import(struct bo *bo, struct drv_import_fd_data *data)
{
int ret;
struct drm_i915_gem_get_tiling gem_get_tiling;
ret = drv_prime_bo_import(bo, data);
if (ret)
return ret;
/* TODO(gsingh): export modifiers and get rid of backdoor tiling. */
memset(&gem_get_tiling, 0, sizeof(gem_get_tiling));
gem_get_tiling.handle = bo->handles[0].u32;
ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_GET_TILING, &gem_get_tiling);
if (ret) {
drv_gem_bo_destroy(bo);
fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_GET_TILING failed.");
return ret;
}
bo->tiling = gem_get_tiling.tiling_mode;
return 0;
}
static void *i915_bo_map(struct bo *bo, struct map_info *data, size_t plane, uint32_t map_flags)
{
int ret;
void *addr;
if (bo->tiling == I915_TILING_NONE) {
struct drm_i915_gem_mmap gem_map;
memset(&gem_map, 0, sizeof(gem_map));
if ((bo->use_flags & BO_USE_SCANOUT) && !(bo->use_flags & BO_USE_RENDERSCRIPT))
gem_map.flags = I915_MMAP_WC;
gem_map.handle = bo->handles[0].u32;
gem_map.offset = 0;
gem_map.size = bo->total_size;
ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_map);
if (ret) {
fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_MMAP failed\n");
return MAP_FAILED;
}
addr = (void *)(uintptr_t)gem_map.addr_ptr;
} else {
struct drm_i915_gem_mmap_gtt gem_map;
memset(&gem_map, 0, sizeof(gem_map));
gem_map.handle = bo->handles[0].u32;
ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &gem_map);
if (ret) {
fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_MMAP_GTT failed\n");
return MAP_FAILED;
}
addr = mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
gem_map.offset);
}
if (addr == MAP_FAILED) {
fprintf(stderr, "drv: i915 GEM mmap failed\n");
return addr;
}
data->length = bo->total_size;
return addr;
}
static int i915_bo_invalidate(struct bo *bo, struct map_info *data)
{
int ret;
struct drm_i915_gem_set_domain set_domain;
memset(&set_domain, 0, sizeof(set_domain));
set_domain.handle = bo->handles[0].u32;
if (bo->tiling == I915_TILING_NONE) {
set_domain.read_domains = I915_GEM_DOMAIN_CPU;
if (data->map_flags & BO_MAP_WRITE)
set_domain.write_domain = I915_GEM_DOMAIN_CPU;
} else {
set_domain.read_domains = I915_GEM_DOMAIN_GTT;
if (data->map_flags & BO_MAP_WRITE)
set_domain.write_domain = I915_GEM_DOMAIN_GTT;
}
ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
if (ret) {
fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_SET_DOMAIN with %d\n", ret);
return ret;
}
return 0;
}
static int i915_bo_flush(struct bo *bo, struct map_info *data)
{
struct i915_device *i915 = bo->drv->priv;
if (!i915->has_llc && bo->tiling == I915_TILING_NONE)
i915_clflush(data->addr, data->length);
return 0;
}
static uint32_t i915_resolve_format(uint32_t format, uint64_t use_flags)
{
uint32_t resolved_format;
if (i915_private_resolve_format(format, use_flags, &resolved_format)) {
return resolved_format;
}
switch (format) {
case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
/* KBL camera subsystem requires NV12. */
if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
return DRM_FORMAT_NV12;
/*HACK: See b/28671744 */
return DRM_FORMAT_XBGR8888;
case DRM_FORMAT_FLEX_YCbCr_420_888:
/* KBL camera subsystem requires NV12. */
if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
return DRM_FORMAT_NV12;
return DRM_FORMAT_YVU420;
default:
return format;
}
}
struct backend backend_i915 = {
.name = "i915",
.init = i915_init,
.close = i915_close,
.bo_create = i915_bo_create,
.bo_create_with_modifiers = i915_bo_create_with_modifiers,
.bo_destroy = drv_gem_bo_destroy,
.bo_import = i915_bo_import,
.bo_map = i915_bo_map,
.bo_unmap = drv_bo_munmap,
.bo_invalidate = i915_bo_invalidate,
.bo_flush = i915_bo_flush,
.resolve_format = i915_resolve_format,
};
#endif