blob: 4df587952e7059aa641ddb2a5b5d8cf7d5834474 [file] [log] [blame]
Stéphane Marchesin25a26062014-09-12 16:18:59 -07001/*
2 * Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
Gurchetan Singh46faf6b2016-08-05 14:40:07 -07007#ifdef DRV_I915
Stéphane Marchesin25a26062014-09-12 16:18:59 -07008
9#include <errno.h>
10#include <string.h>
11#include <stdio.h>
Gurchetan Singhef920532016-08-12 16:38:25 -070012#include <sys/mman.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070013#include <xf86drm.h>
14#include <i915_drm.h>
15
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070016#include "drv_priv.h"
Stéphane Marchesin25a26062014-09-12 16:18:59 -070017#include "helpers.h"
18#include "util.h"
19
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070020struct i915_device
Stéphane Marchesin25a26062014-09-12 16:18:59 -070021{
22 int gen;
23};
24
25
26static int get_gen(int device_id)
27{
Stéphane Marchesinec88e892015-11-03 16:14:59 -080028 const uint16_t gen3_ids[] = {0x2582, 0x2592, 0x2772, 0x27A2, 0x27AE,
29 0x29C2, 0x29B2, 0x29D2, 0xA001, 0xA011};
Stéphane Marchesina39dfde2014-09-15 15:38:25 -070030 unsigned i;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070031 for(i = 0; i < ARRAY_SIZE(gen3_ids); i++)
32 if (gen3_ids[i] == device_id)
33 return 3;
34
35 return 4;
36}
37
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -070038static int i915_init(struct driver *drv)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070039{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070040 struct i915_device *i915_drv;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070041 drm_i915_getparam_t get_param;
42 int device_id;
43 int ret;
44
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070045 i915_drv = (struct i915_device*)malloc(sizeof(*i915_drv));
46 if (!i915_drv)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070047 return -1;
48
49 memset(&get_param, 0, sizeof(get_param));
50 get_param.param = I915_PARAM_CHIPSET_ID;
51 get_param.value = &device_id;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070052 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070053 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070054 fprintf(stderr, "drv: DRM_IOCTL_I915_GETPARAM failed\n");
55 free(i915_drv);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070056 return -1;
57 }
58
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070059 i915_drv->gen = get_gen(device_id);
Stéphane Marchesin25a26062014-09-12 16:18:59 -070060
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070061 drv->priv = i915_drv;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070062
63 return 0;
64}
65
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -070066static void i915_close(struct driver *drv)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070067{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070068 free(drv->priv);
69 drv->priv = NULL;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070070}
71
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070072static void i915_align_dimensions(struct driver *drv, uint32_t tiling_mode,
Stéphane Marchesinec88e892015-11-03 16:14:59 -080073 uint32_t *width, uint32_t *height, int bpp)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070074{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070075 struct i915_device *i915_drv = (struct i915_device *)drv->priv;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -080076 uint32_t width_alignment = 4, height_alignment = 4;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070077
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070078 switch (tiling_mode) {
79 default:
80 case I915_TILING_NONE:
81 width_alignment = 64 / bpp;
82 break;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -080083
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070084 case I915_TILING_X:
85 width_alignment = 512 / bpp;
86 height_alignment = 8;
87 break;
88
89 case I915_TILING_Y:
90 if (i915_drv->gen == 3) {
Stéphane Marchesin5d867a42014-11-24 17:09:49 -080091 width_alignment = 512 / bpp;
92 height_alignment = 8;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070093 } else {
94 width_alignment = 128 / bpp;
95 height_alignment = 32;
96 }
97 break;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070098 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -080099
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700100 if (i915_drv->gen > 3) {
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800101 *width = ALIGN(*width, width_alignment);
102 *height = ALIGN(*height, height_alignment);
103 } else {
104 uint32_t w;
Stéphane Marchesine3d7c1f2015-03-31 13:47:22 -0700105 for (w = width_alignment; w < *width; w <<= 1)
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800106 ;
107 *width = w;
108 *height = ALIGN(*height, height_alignment);
109 }
110}
111
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700112static int i915_verify_dimensions(struct driver *drv, uint32_t stride,
Stéphane Marchesinec88e892015-11-03 16:14:59 -0800113 uint32_t height)
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800114{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700115 struct i915_device *i915_drv = (struct i915_device *)drv->priv;
116 if (i915_drv->gen <= 3 && stride > 8192)
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800117 return 0;
118
119 return 1;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700120}
121
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700122static int i915_bo_create(struct bo *bo, uint32_t width, uint32_t height,
123 uint32_t format, uint32_t flags)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700124{
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700125 struct driver *drv = bo->drv;
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700126 int bpp = drv_stride_from_format(format, 1, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700127 struct drm_i915_gem_create gem_create;
128 struct drm_i915_gem_set_tiling gem_set_tiling;
129 uint32_t tiling_mode = I915_TILING_NONE;
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700130 size_t plane;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700131 int ret;
132
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700133 if (flags & (DRV_BO_USE_CURSOR | DRV_BO_USE_LINEAR |
134 DRV_BO_USE_SW_READ_OFTEN | DRV_BO_USE_SW_WRITE_OFTEN))
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700135 tiling_mode = I915_TILING_NONE;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700136 else if (flags & DRV_BO_USE_SCANOUT)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700137 tiling_mode = I915_TILING_X;
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700138 else if (flags & (DRV_BO_USE_RENDERING | DRV_BO_USE_HW_TEXTURE |
139 DRV_BO_USE_HW_RENDER | DRV_BO_USE_SW_READ_RARELY |
140 DRV_BO_USE_HW_2D | DRV_BO_USE_SW_WRITE_RARELY))
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700141 tiling_mode = I915_TILING_Y;
142
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700143 i915_align_dimensions(drv, tiling_mode, &width, &height, bpp);
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800144
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700145 drv_bo_from_format(bo, width, height, format);
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800146
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700147 if (!i915_verify_dimensions(drv, bo->strides[0], height))
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800148 return EINVAL;
149
150 memset(&gem_create, 0, sizeof(gem_create));
Gurchetan Singha40ca9e2016-08-29 19:51:45 -0700151 gem_create.size = bo->total_size;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800152
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700153 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700154 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700155 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_CREATE failed "
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700156 "(size=%llu)\n", gem_create.size);
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800157 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700158 }
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700159
160 for (plane = 0; plane < bo->num_planes; plane++)
161 bo->handles[plane].u32 = gem_create.handle;
Daniel Nicoara1de26dc2014-09-25 18:53:19 -0400162
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700163 memset(&gem_set_tiling, 0, sizeof(gem_set_tiling));
164 do {
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500165 gem_set_tiling.handle = bo->handles[0].u32;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700166 gem_set_tiling.tiling_mode = tiling_mode;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500167 gem_set_tiling.stride = bo->strides[0];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700168 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GEM_SET_TILING,
Stéphane Marchesinec88e892015-11-03 16:14:59 -0800169 &gem_set_tiling);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700170 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
171
172 if (ret == -1) {
173 struct drm_gem_close gem_close;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500174 gem_close.handle = bo->handles[0].u32;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700175 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_SET_TILING failed "
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700176 "errno=%x (handle=%x, tiling=%x, stride=%x)\n",
177 errno,
178 gem_set_tiling.handle,
179 gem_set_tiling.tiling_mode,
180 gem_set_tiling.stride);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700181 drmIoctl(drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700182 return -errno;
183 }
184
185 return 0;
186}
187
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700188static void *i915_bo_map(struct bo *bo, struct map_info *data, size_t plane)
Gurchetan Singhef920532016-08-12 16:38:25 -0700189{
190 int ret;
191 struct drm_i915_gem_mmap_gtt gem_map;
192
193 memset(&gem_map, 0, sizeof(gem_map));
194 gem_map.handle = bo->handles[0].u32;
195
196 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &gem_map);
197 if (ret) {
198 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_MMAP_GTT failed\n");
199 return MAP_FAILED;
200 }
201
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700202 data->length = bo->total_size;
203
Gurchetan Singha40ca9e2016-08-29 19:51:45 -0700204 return mmap(0, bo->total_size, PROT_READ | PROT_WRITE, MAP_SHARED,
Gurchetan Singhef920532016-08-12 16:38:25 -0700205 bo->drv->fd, gem_map.offset);
206}
207
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700208static drv_format_t i915_resolve_format(drv_format_t format)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700209{
210 switch (format) {
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700211 case DRV_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
212 /*HACK: See b/28671744 */
213 return DRV_FORMAT_XBGR8888;
214 case DRV_FORMAT_FLEX_YCbCr_420_888:
215 return DRV_FORMAT_YVU420;
216 default:
217 return format;
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700218 }
219}
220
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700221const struct backend backend_i915 =
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700222{
223 .name = "i915",
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700224 .init = i915_init,
225 .close = i915_close,
226 .bo_create = i915_bo_create,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700227 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700228 .bo_map = i915_bo_map,
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700229 .resolve_format = i915_resolve_format,
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700230 .format_list = {
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700231 {DRV_FORMAT_XRGB8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR |
232 DRV_BO_USE_RENDERING | DRV_BO_USE_HW_TEXTURE |
233 DRV_BO_USE_HW_RENDER | DRV_BO_USE_HW_2D |
234 DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
235 {DRV_FORMAT_XRGB8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR |
236 DRV_BO_USE_LINEAR | DRV_BO_USE_SW_READ_OFTEN |
237 DRV_BO_USE_SW_WRITE_OFTEN},
238 {DRV_FORMAT_ARGB8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR |
239 DRV_BO_USE_RENDERING | DRV_BO_USE_HW_TEXTURE |
240 DRV_BO_USE_HW_RENDER | DRV_BO_USE_HW_2D |
241 DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
242 {DRV_FORMAT_ARGB8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR |
243 DRV_BO_USE_LINEAR | DRV_BO_USE_SW_READ_OFTEN |
244 DRV_BO_USE_SW_WRITE_OFTEN},
245 {DRV_FORMAT_XBGR8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR |
246 DRV_BO_USE_RENDERING | DRV_BO_USE_HW_TEXTURE |
247 DRV_BO_USE_HW_RENDER | DRV_BO_USE_HW_2D |
248 DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
249 {DRV_FORMAT_ABGR8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR |
250 DRV_BO_USE_RENDERING | DRV_BO_USE_HW_TEXTURE |
251 DRV_BO_USE_HW_RENDER | DRV_BO_USE_HW_2D |
252 DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
253 {DRV_FORMAT_XRGB1555, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR |
254 DRV_BO_USE_RENDERING | DRV_BO_USE_HW_TEXTURE |
255 DRV_BO_USE_HW_RENDER | DRV_BO_USE_HW_2D |
256 DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
257 {DRV_FORMAT_ARGB1555, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR |
258 DRV_BO_USE_RENDERING | DRV_BO_USE_HW_TEXTURE |
259 DRV_BO_USE_HW_RENDER | DRV_BO_USE_HW_2D |
260 DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
261 {DRV_FORMAT_RGB565, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR |
262 DRV_BO_USE_RENDERING | DRV_BO_USE_HW_TEXTURE |
263 DRV_BO_USE_HW_RENDER | DRV_BO_USE_HW_2D |
264 DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
265 {DRV_FORMAT_UYVY, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR |
266 DRV_BO_USE_RENDERING | DRV_BO_USE_HW_TEXTURE |
267 DRV_BO_USE_HW_RENDER | DRV_BO_USE_HW_2D |
268 DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
269 {DRV_FORMAT_UYVY, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR |
270 DRV_BO_USE_LINEAR | DRV_BO_USE_SW_READ_OFTEN |
271 DRV_BO_USE_SW_WRITE_OFTEN},
272 {DRV_FORMAT_YUYV, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR |
273 DRV_BO_USE_RENDERING | DRV_BO_USE_HW_TEXTURE |
274 DRV_BO_USE_HW_RENDER | DRV_BO_USE_HW_2D |
275 DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
276 {DRV_FORMAT_YUYV, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR |
277 DRV_BO_USE_LINEAR | DRV_BO_USE_SW_READ_OFTEN |
278 DRV_BO_USE_SW_WRITE_OFTEN},
279 {DRV_FORMAT_R8, DRV_BO_USE_SCANOUT | DRV_BO_USE_LINEAR |
280 DRV_BO_USE_SW_READ_OFTEN | DRV_BO_USE_SW_WRITE_OFTEN},
281 {DRV_FORMAT_GR88, DRV_BO_USE_SCANOUT | DRV_BO_USE_LINEAR |
282 DRV_BO_USE_SW_READ_OFTEN | DRV_BO_USE_SW_WRITE_OFTEN},
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700283 {DRV_FORMAT_YVU420, DRV_BO_USE_LINEAR | DRV_BO_USE_SW_READ_OFTEN |
284 DRV_BO_USE_SW_WRITE_OFTEN},
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700285 }
286};
287
288#endif