Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 1 | /* |
Daniele Castagna | 7a755de | 2016-12-16 17:32:30 -0500 | [diff] [blame] | 2 | * Copyright 2014 The Chromium OS Authors. All rights reserved. |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 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 Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 7 | #ifdef DRV_I915 |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 8 | |
| 9 | #include <errno.h> |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 10 | #include <i915_drm.h> |
| 11 | #include <intel_bufmgr.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 12 | #include <string.h> |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 13 | #include <sys/mman.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 14 | #include <xf86drm.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 15 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 16 | #include "drv_priv.h" |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 17 | #include "helpers.h" |
| 18 | #include "util.h" |
| 19 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame^] | 20 | static const uint32_t tileable_formats[] = { DRM_FORMAT_ARGB1555, DRM_FORMAT_ABGR8888, |
| 21 | DRM_FORMAT_ARGB8888, DRM_FORMAT_RGB565, |
| 22 | DRM_FORMAT_XBGR8888, DRM_FORMAT_XRGB1555, |
| 23 | DRM_FORMAT_XRGB8888, DRM_FORMAT_UYVY, |
| 24 | DRM_FORMAT_YUYV }; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 25 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame^] | 26 | static const uint32_t linear_only_formats[] = { DRM_FORMAT_GR88, DRM_FORMAT_R8, DRM_FORMAT_YVU420, |
| 27 | DRM_FORMAT_YVU420_ANDROID }; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 28 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame^] | 29 | struct i915_device { |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 30 | int gen; |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 31 | drm_intel_bufmgr *mgr; |
| 32 | uint32_t count; |
| 33 | }; |
| 34 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame^] | 35 | struct i915_bo { |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 36 | drm_intel_bo *ibos[DRV_MAX_PLANES]; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 39 | static int get_gen(int device_id) |
| 40 | { |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame^] | 41 | const uint16_t gen3_ids[] = { 0x2582, 0x2592, 0x2772, 0x27A2, 0x27AE, |
| 42 | 0x29C2, 0x29B2, 0x29D2, 0xA001, 0xA011 }; |
Stéphane Marchesin | a39dfde | 2014-09-15 15:38:25 -0700 | [diff] [blame] | 43 | unsigned i; |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame^] | 44 | for (i = 0; i < ARRAY_SIZE(gen3_ids); i++) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 45 | if (gen3_ids[i] == device_id) |
| 46 | return 3; |
| 47 | |
| 48 | return 4; |
| 49 | } |
| 50 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 51 | static int i915_add_kms_item(struct driver *drv, const struct kms_item *item) |
| 52 | { |
| 53 | uint32_t i; |
| 54 | struct combination *combo; |
| 55 | |
| 56 | /* |
| 57 | * Older hardware can't scanout Y-tiled formats. Newer devices can, and |
| 58 | * report this functionality via format modifiers. |
| 59 | */ |
| 60 | for (i = 0; i < drv->backend->combos.size; i++) { |
| 61 | combo = &drv->backend->combos.data[i]; |
| 62 | if (combo->format == item->format) { |
| 63 | if ((combo->metadata.tiling == I915_TILING_Y && |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame^] | 64 | item->modifier == I915_FORMAT_MOD_Y_TILED) || |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 65 | (combo->metadata.tiling == I915_TILING_X && |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame^] | 66 | item->modifier == I915_FORMAT_MOD_X_TILED)) { |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 67 | combo->metadata.modifier = item->modifier; |
| 68 | combo->usage |= item->usage; |
| 69 | } else if (combo->metadata.tiling != I915_TILING_Y) { |
| 70 | combo->usage |= item->usage; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static int i915_add_combinations(struct driver *drv) |
| 79 | { |
| 80 | int ret; |
| 81 | uint32_t i, num_items; |
| 82 | struct kms_item *items; |
| 83 | struct format_metadata metadata; |
| 84 | uint64_t flags = BO_COMMON_USE_MASK; |
| 85 | |
| 86 | metadata.tiling = I915_TILING_NONE; |
| 87 | metadata.priority = 1; |
| 88 | metadata.modifier = DRM_FORMAT_MOD_NONE; |
| 89 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame^] | 90 | ret = drv_add_combinations(drv, linear_only_formats, ARRAY_SIZE(linear_only_formats), |
| 91 | &metadata, flags); |
| 92 | if (ret) |
| 93 | return ret; |
| 94 | |
| 95 | ret = drv_add_combinations(drv, tileable_formats, ARRAY_SIZE(tileable_formats), &metadata, |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 96 | flags); |
| 97 | if (ret) |
| 98 | return ret; |
| 99 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame^] | 100 | drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT); |
| 101 | drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT); |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 102 | |
| 103 | flags &= ~BO_USE_SW_WRITE_OFTEN; |
| 104 | flags &= ~BO_USE_SW_READ_OFTEN; |
| 105 | flags &= ~BO_USE_LINEAR; |
| 106 | |
| 107 | metadata.tiling = I915_TILING_X; |
| 108 | metadata.priority = 2; |
| 109 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame^] | 110 | ret = drv_add_combinations(drv, tileable_formats, ARRAY_SIZE(tileable_formats), &metadata, |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 111 | flags); |
| 112 | if (ret) |
| 113 | return ret; |
| 114 | |
| 115 | metadata.tiling = I915_TILING_Y; |
| 116 | metadata.priority = 3; |
| 117 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame^] | 118 | ret = drv_add_combinations(drv, tileable_formats, ARRAY_SIZE(tileable_formats), &metadata, |
| 119 | flags); |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 120 | if (ret) |
| 121 | return ret; |
| 122 | |
| 123 | items = drv_query_kms(drv, &num_items); |
| 124 | if (!items || !num_items) |
| 125 | return 0; |
| 126 | |
| 127 | for (i = 0; i < num_items; i++) { |
| 128 | ret = i915_add_kms_item(drv, &items[i]); |
| 129 | if (ret) { |
| 130 | free(items); |
| 131 | return ret; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | free(items); |
| 136 | return 0; |
| 137 | } |
| 138 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame^] | 139 | static int i915_align_dimensions(struct bo *bo, uint32_t tiling, uint32_t *stride, |
| 140 | uint32_t *aligned_height) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 141 | { |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 142 | struct i915_device *i915 = bo->drv->priv; |
| 143 | uint32_t horizontal_alignment = 4; |
| 144 | uint32_t vertical_alignment = 4; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 145 | |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 146 | switch (tiling) { |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 147 | default: |
| 148 | case I915_TILING_NONE: |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 149 | horizontal_alignment = 64; |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 150 | break; |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 151 | |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 152 | case I915_TILING_X: |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 153 | horizontal_alignment = 512; |
| 154 | vertical_alignment = 8; |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 155 | break; |
| 156 | |
| 157 | case I915_TILING_Y: |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 158 | if (i915->gen == 3) { |
| 159 | horizontal_alignment = 512; |
| 160 | vertical_alignment = 8; |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame^] | 161 | } else { |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 162 | horizontal_alignment = 128; |
| 163 | vertical_alignment = 32; |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 164 | } |
| 165 | break; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 166 | } |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 167 | |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 168 | *aligned_height = ALIGN(bo->height, vertical_alignment); |
| 169 | if (i915->gen > 3) { |
| 170 | *stride = ALIGN(*stride, horizontal_alignment); |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 171 | } else { |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 172 | while (*stride > horizontal_alignment) |
| 173 | horizontal_alignment <<= 1; |
| 174 | |
| 175 | *stride = horizontal_alignment; |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 176 | } |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 177 | |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 178 | if (i915->gen <= 3 && *stride > 8192) |
| 179 | return -EINVAL; |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 180 | |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 181 | return 0; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 184 | static int i915_init(struct driver *drv) |
| 185 | { |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 186 | struct i915_device *i915_dev; |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 187 | drm_i915_getparam_t get_param; |
| 188 | int device_id; |
| 189 | int ret; |
| 190 | |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 191 | i915_dev = calloc(1, sizeof(*i915_dev)); |
| 192 | if (!i915_dev) |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 193 | return -1; |
| 194 | |
| 195 | memset(&get_param, 0, sizeof(get_param)); |
| 196 | get_param.param = I915_PARAM_CHIPSET_ID; |
| 197 | get_param.value = &device_id; |
| 198 | ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param); |
| 199 | if (ret) { |
| 200 | fprintf(stderr, "drv: DRM_IOCTL_I915_GETPARAM failed\n"); |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 201 | free(i915_dev); |
| 202 | return -EINVAL; |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 205 | i915_dev->gen = get_gen(device_id); |
| 206 | i915_dev->count = 0; |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 207 | |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 208 | i915_dev->mgr = drm_intel_bufmgr_gem_init(drv->fd, 16 * 1024); |
| 209 | if (!i915_dev->mgr) { |
| 210 | fprintf(stderr, "drv: drm_intel_bufmgr_gem_init failed\n"); |
| 211 | free(i915_dev); |
| 212 | return -EINVAL; |
| 213 | } |
| 214 | |
| 215 | drv->priv = i915_dev; |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 216 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 217 | return i915_add_combinations(drv); |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static void i915_close(struct driver *drv) |
| 221 | { |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 222 | struct i915_device *i915_dev = drv->priv; |
| 223 | drm_intel_bufmgr_destroy(i915_dev->mgr); |
| 224 | free(i915_dev); |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 225 | drv->priv = NULL; |
| 226 | } |
| 227 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame^] | 228 | static int i915_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format, |
| 229 | uint32_t flags) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 230 | { |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 231 | int ret; |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 232 | size_t plane; |
| 233 | char name[20]; |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 234 | uint32_t stride; |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 235 | uint32_t tiling_mode; |
| 236 | struct i915_bo *i915_bo; |
| 237 | |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 238 | stride = drv_stride_from_format(format, width, 0); |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 239 | struct i915_device *i915_dev = (struct i915_device *)bo->drv->priv; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 240 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame^] | 241 | if (flags & (BO_USE_CURSOR | BO_USE_LINEAR | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN)) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 242 | tiling_mode = I915_TILING_NONE; |
Gurchetan Singh | 458976f | 2016-11-23 17:32:33 -0800 | [diff] [blame] | 243 | else if (flags & BO_USE_SCANOUT) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 244 | tiling_mode = I915_TILING_X; |
Gurchetan Singh | 6bab0c1 | 2016-10-13 19:08:48 -0700 | [diff] [blame] | 245 | else |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 246 | tiling_mode = I915_TILING_Y; |
| 247 | |
Gurchetan Singh | 507f5dd | 2017-03-16 13:14:30 -0700 | [diff] [blame] | 248 | /* |
| 249 | * Align the Y plane to 128 bytes so the chroma planes would be aligned |
| 250 | * to 64 byte boundaries. This is an Intel HW requirement. |
| 251 | */ |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame^] | 252 | if (format == DRM_FORMAT_YVU420 || format == DRM_FORMAT_YVU420_ANDROID) { |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 253 | stride = ALIGN(stride, 128); |
Gurchetan Singh | 507f5dd | 2017-03-16 13:14:30 -0700 | [diff] [blame] | 254 | tiling_mode = I915_TILING_NONE; |
| 255 | } |
| 256 | |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 257 | ret = i915_align_dimensions(bo, tiling_mode, &stride, &height); |
| 258 | if (ret) |
| 259 | return ret; |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 260 | |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 261 | drv_bo_from_format(bo, stride, height, format); |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 262 | |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 263 | snprintf(name, sizeof(name), "i915-buffer-%u", i915_dev->count); |
| 264 | i915_dev->count++; |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 265 | |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 266 | i915_bo = calloc(1, sizeof(*i915_bo)); |
| 267 | if (!i915_bo) |
| 268 | return -ENOMEM; |
| 269 | |
| 270 | bo->priv = i915_bo; |
| 271 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame^] | 272 | i915_bo->ibos[0] = drm_intel_bo_alloc(i915_dev->mgr, name, bo->total_size, 0); |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 273 | if (!i915_bo->ibos[0]) { |
| 274 | fprintf(stderr, "drv: drm_intel_bo_alloc failed"); |
| 275 | free(i915_bo); |
| 276 | bo->priv = NULL; |
| 277 | return -ENOMEM; |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 278 | } |
Gurchetan Singh | 83dc4fb | 2016-07-19 15:52:33 -0700 | [diff] [blame] | 279 | |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 280 | for (plane = 0; plane < bo->num_planes; plane++) { |
| 281 | if (plane > 0) |
| 282 | drm_intel_bo_reference(i915_bo->ibos[0]); |
Daniel Nicoara | 1de26dc | 2014-09-25 18:53:19 -0400 | [diff] [blame] | 283 | |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 284 | bo->handles[plane].u32 = i915_bo->ibos[0]->handle; |
| 285 | i915_bo->ibos[plane] = i915_bo->ibos[0]; |
| 286 | } |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 287 | |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 288 | bo->tiling = tiling_mode; |
| 289 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame^] | 290 | ret = drm_intel_bo_set_tiling(i915_bo->ibos[0], &bo->tiling, bo->strides[0]); |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 291 | |
| 292 | if (ret || bo->tiling != tiling_mode) { |
| 293 | fprintf(stderr, "drv: drm_intel_gem_bo_set_tiling failed " |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame^] | 294 | "errno=%x, stride=%x\n", |
| 295 | errno, bo->strides[0]); |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 296 | /* Calls i915 bo destroy. */ |
| 297 | bo->drv->backend->bo_destroy(bo); |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 298 | return -errno; |
| 299 | } |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 304 | static int i915_bo_destroy(struct bo *bo) |
| 305 | { |
| 306 | size_t plane; |
| 307 | struct i915_bo *i915_bo = bo->priv; |
| 308 | |
| 309 | for (plane = 0; plane < bo->num_planes; plane++) |
| 310 | drm_intel_bo_unreference(i915_bo->ibos[plane]); |
| 311 | |
| 312 | free(i915_bo); |
| 313 | bo->priv = NULL; |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | static int i915_bo_import(struct bo *bo, struct drv_import_fd_data *data) |
| 319 | { |
| 320 | size_t plane; |
| 321 | uint32_t swizzling; |
| 322 | struct i915_bo *i915_bo; |
| 323 | struct i915_device *i915_dev = bo->drv->priv; |
| 324 | |
| 325 | i915_bo = calloc(1, sizeof(*i915_bo)); |
| 326 | if (!i915_bo) |
| 327 | return -ENOMEM; |
| 328 | |
| 329 | bo->priv = i915_bo; |
| 330 | |
| 331 | /* |
| 332 | * When self-importing, libdrm_intel increments the reference count |
| 333 | * on the drm_intel_bo. It also returns the same drm_intel_bo per GEM |
| 334 | * handle. Thus, we don't need to increase the reference count |
| 335 | * (i.e, drv_increment_reference_count) when importing with this |
| 336 | * backend. |
| 337 | */ |
| 338 | for (plane = 0; plane < bo->num_planes; plane++) { |
| 339 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame^] | 340 | i915_bo->ibos[plane] = drm_intel_bo_gem_create_from_prime( |
| 341 | i915_dev->mgr, data->fds[plane], data->sizes[plane]); |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 342 | |
| 343 | if (!i915_bo->ibos[plane]) { |
| 344 | /* |
| 345 | * Need to call GEM close on planes that were opened, |
| 346 | * if any. Adjust the num_planes variable to be the |
| 347 | * plane that failed, so GEM close will be called on |
| 348 | * planes before that plane. |
| 349 | */ |
| 350 | bo->num_planes = plane; |
| 351 | i915_bo_destroy(bo); |
| 352 | fprintf(stderr, "drv: i915: failed to import failed"); |
| 353 | return -EINVAL; |
| 354 | } |
| 355 | |
| 356 | bo->handles[plane].u32 = i915_bo->ibos[plane]->handle; |
| 357 | } |
| 358 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame^] | 359 | if (drm_intel_bo_get_tiling(i915_bo->ibos[0], &bo->tiling, &swizzling)) { |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 360 | fprintf(stderr, "drv: drm_intel_bo_get_tiling failed"); |
| 361 | i915_bo_destroy(bo); |
| 362 | return -EINVAL; |
| 363 | } |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 368 | static void *i915_bo_map(struct bo *bo, struct map_info *data, size_t plane) |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 369 | { |
| 370 | int ret; |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 371 | struct i915_bo *i915_bo = bo->priv; |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 372 | |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 373 | if (bo->tiling == I915_TILING_NONE) |
| 374 | /* TODO(gsingh): use bo_map flags to determine if we should |
| 375 | * enable writing. |
| 376 | */ |
| 377 | ret = drm_intel_bo_map(i915_bo->ibos[0], 1); |
| 378 | else |
| 379 | ret = drm_intel_gem_bo_map_gtt(i915_bo->ibos[0]); |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 380 | |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 381 | if (ret) { |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 382 | fprintf(stderr, "drv: i915_bo_map failed."); |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 383 | return MAP_FAILED; |
| 384 | } |
| 385 | |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 386 | return i915_bo->ibos[0]->virtual; |
| 387 | } |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 388 | |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 389 | static int i915_bo_unmap(struct bo *bo, struct map_info *data) |
| 390 | { |
| 391 | int ret; |
| 392 | struct i915_bo *i915_bo = bo->priv; |
| 393 | |
| 394 | if (bo->tiling == I915_TILING_NONE) |
| 395 | ret = drm_intel_bo_unmap(i915_bo->ibos[0]); |
| 396 | else |
| 397 | ret = drm_intel_gem_bo_unmap_gtt(i915_bo->ibos[0]); |
| 398 | |
| 399 | return ret; |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 402 | static uint32_t i915_resolve_format(uint32_t format) |
Gurchetan Singh | bfba8c2 | 2016-08-16 17:57:10 -0700 | [diff] [blame] | 403 | { |
| 404 | switch (format) { |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 405 | case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED: |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 406 | /*HACK: See b/28671744 */ |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 407 | return DRM_FORMAT_XBGR8888; |
| 408 | case DRM_FORMAT_FLEX_YCbCr_420_888: |
Gurchetan Singh | 03f1356 | 2017-02-08 15:21:14 -0800 | [diff] [blame] | 409 | return DRM_FORMAT_YVU420_ANDROID; |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 410 | default: |
| 411 | return format; |
Gurchetan Singh | bfba8c2 | 2016-08-16 17:57:10 -0700 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame^] | 415 | struct backend backend_i915 = { |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 416 | .name = "i915", |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame] | 417 | .init = i915_init, |
| 418 | .close = i915_close, |
| 419 | .bo_create = i915_bo_create, |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 420 | .bo_destroy = i915_bo_destroy, |
| 421 | .bo_import = i915_bo_import, |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame] | 422 | .bo_map = i915_bo_map, |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 423 | .bo_unmap = i915_bo_unmap, |
Gurchetan Singh | bfba8c2 | 2016-08-16 17:57:10 -0700 | [diff] [blame] | 424 | .resolve_format = i915_resolve_format, |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 425 | }; |
| 426 | |
| 427 | #endif |