-
Notifications
You must be signed in to change notification settings - Fork 0
/
i915.c
268 lines (228 loc) · 7.9 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
/*
* Copyright (c) 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 <string.h>
#include <stdio.h>
#include <sys/mman.h>
#include <xf86drm.h>
#include <i915_drm.h>
#include "drv_priv.h"
#include "helpers.h"
#include "util.h"
struct i915_device
{
int gen;
};
static int 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_init(struct driver *drv)
{
struct i915_device *i915_drv;
drm_i915_getparam_t get_param;
int device_id;
int ret;
i915_drv = (struct i915_device*)malloc(sizeof(*i915_drv));
if (!i915_drv)
return -1;
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: DRM_IOCTL_I915_GETPARAM failed\n");
free(i915_drv);
return -1;
}
i915_drv->gen = get_gen(device_id);
drv->priv = i915_drv;
return 0;
}
static void i915_close(struct driver *drv)
{
free(drv->priv);
drv->priv = NULL;
}
static void i915_align_dimensions(struct driver *drv, uint32_t tiling_mode,
uint32_t *width, uint32_t *height, int bpp)
{
struct i915_device *i915_drv = (struct i915_device *)drv->priv;
uint32_t width_alignment = 4, height_alignment = 4;
switch (tiling_mode) {
default:
case I915_TILING_NONE:
width_alignment = 64 / bpp;
break;
case I915_TILING_X:
width_alignment = 512 / bpp;
height_alignment = 8;
break;
case I915_TILING_Y:
if (i915_drv->gen == 3) {
width_alignment = 512 / bpp;
height_alignment = 8;
} else {
width_alignment = 128 / bpp;
height_alignment = 32;
}
break;
}
if (i915_drv->gen > 3) {
*width = ALIGN(*width, width_alignment);
*height = ALIGN(*height, height_alignment);
} else {
uint32_t w;
for (w = width_alignment; w < *width; w <<= 1)
;
*width = w;
*height = ALIGN(*height, height_alignment);
}
}
static int i915_verify_dimensions(struct driver *drv, uint32_t stride,
uint32_t height)
{
struct i915_device *i915_drv = (struct i915_device *)drv->priv;
if (i915_drv->gen <= 3 && stride > 8192)
return 0;
return 1;
}
static int i915_bo_create(struct bo *bo, uint32_t width, uint32_t height,
uint32_t format, uint32_t flags)
{
struct driver *drv = bo->drv;
int bpp = drv_stride_from_format(format, 1, 0);
struct drm_i915_gem_create gem_create;
struct drm_i915_gem_set_tiling gem_set_tiling;
uint32_t tiling_mode = I915_TILING_NONE;
size_t plane;
int ret;
if (flags & (DRV_BO_USE_CURSOR | DRV_BO_USE_LINEAR |
DRV_BO_USE_SW_READ_OFTEN | DRV_BO_USE_SW_WRITE_OFTEN))
tiling_mode = I915_TILING_NONE;
else if (flags & DRV_BO_USE_SCANOUT)
tiling_mode = I915_TILING_X;
else
tiling_mode = I915_TILING_Y;
i915_align_dimensions(drv, tiling_mode, &width, &height, bpp);
drv_bo_from_format(bo, width, height, format);
if (!i915_verify_dimensions(drv, bo->strides[0], height))
return EINVAL;
memset(&gem_create, 0, sizeof(gem_create));
gem_create.size = bo->total_size;
ret = drmIoctl(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));
do {
gem_set_tiling.handle = bo->handles[0].u32;
gem_set_tiling.tiling_mode = tiling_mode;
gem_set_tiling.stride = bo->strides[0];
ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GEM_SET_TILING,
&gem_set_tiling);
} while (ret == -1 && (errno == EINTR || errno == EAGAIN));
if (ret == -1) {
struct drm_gem_close gem_close;
gem_close.handle = bo->handles[0].u32;
fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_SET_TILING failed "
"errno=%x (handle=%x, tiling=%x, stride=%x)\n",
errno,
gem_set_tiling.handle,
gem_set_tiling.tiling_mode,
gem_set_tiling.stride);
drmIoctl(drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
return -errno;
}
return 0;
}
static void *i915_bo_map(struct bo *bo, struct map_info *data, size_t plane)
{
int ret;
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;
}
data->length = bo->total_size;
return mmap(0, bo->total_size, PROT_READ | PROT_WRITE, MAP_SHARED,
bo->drv->fd, gem_map.offset);
}
static drv_format_t i915_resolve_format(drv_format_t format)
{
switch (format) {
case DRV_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
/*HACK: See b/28671744 */
return DRV_FORMAT_XBGR8888;
case DRV_FORMAT_FLEX_YCbCr_420_888:
return DRV_FORMAT_YVU420;
default:
return format;
}
}
const struct backend backend_i915 =
{
.name = "i915",
.init = i915_init,
.close = i915_close,
.bo_create = i915_bo_create,
.bo_destroy = drv_gem_bo_destroy,
.bo_map = i915_bo_map,
.resolve_format = i915_resolve_format,
.format_list = {
{DRV_FORMAT_XRGB8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_RENDERING
| DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
{DRV_FORMAT_XRGB8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_LINEAR |
DRV_BO_USE_SW_READ_OFTEN | DRV_BO_USE_SW_WRITE_OFTEN},
{DRV_FORMAT_ARGB8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_RENDERING
| DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
{DRV_FORMAT_ARGB8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_LINEAR |
DRV_BO_USE_SW_READ_OFTEN | DRV_BO_USE_SW_WRITE_OFTEN},
{DRV_FORMAT_XBGR8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_RENDERING
| DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
{DRV_FORMAT_XBGR8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_RENDERING |
DRV_BO_USE_SW_READ_OFTEN | DRV_BO_USE_SW_WRITE_OFTEN},
{DRV_FORMAT_ABGR8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_RENDERING
| DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
{DRV_FORMAT_ABGR8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_RENDERING | DRV_BO_USE_CURSOR
| DRV_BO_USE_SW_READ_OFTEN | DRV_BO_USE_SW_WRITE_OFTEN},
{DRV_FORMAT_XRGB1555, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_RENDERING
| DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
{DRV_FORMAT_ARGB1555, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_RENDERING
| DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
{DRV_FORMAT_RGB565, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_RENDERING
| DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
{DRV_FORMAT_UYVY, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_RENDERING
| DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
{DRV_FORMAT_UYVY, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_LINEAR |
DRV_BO_USE_SW_READ_OFTEN | DRV_BO_USE_SW_WRITE_OFTEN},
{DRV_FORMAT_YUYV, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_RENDERING
| DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
{DRV_FORMAT_YUYV, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_LINEAR |
DRV_BO_USE_SW_READ_OFTEN | DRV_BO_USE_SW_WRITE_OFTEN},
{DRV_FORMAT_R8, DRV_BO_USE_SCANOUT | DRV_BO_USE_LINEAR |
DRV_BO_USE_SW_READ_OFTEN | DRV_BO_USE_SW_WRITE_OFTEN},
{DRV_FORMAT_GR88, DRV_BO_USE_SCANOUT | DRV_BO_USE_LINEAR |
DRV_BO_USE_SW_READ_OFTEN | DRV_BO_USE_SW_WRITE_OFTEN},
{DRV_FORMAT_YVU420, DRV_BO_USE_RENDERING | DRV_BO_USE_SW_READ_RARELY |
DRV_BO_USE_SW_WRITE_RARELY},
}
};
#endif