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> |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 11 | #include <stdio.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 | 68af9c2 | 2017-01-18 13:48:11 -0800 | [diff] [blame] | 20 | #define I915_CACHELINE_SIZE 64 |
| 21 | #define I915_CACHELINE_MASK (I915_CACHELINE_SIZE - 1) |
| 22 | |
Gurchetan Singh | 8ac0c9a | 2017-05-15 09:34:22 -0700 | [diff] [blame] | 23 | static const uint32_t render_target_formats[] = { DRM_FORMAT_ARGB1555, DRM_FORMAT_ABGR8888, |
| 24 | DRM_FORMAT_ARGB8888, DRM_FORMAT_RGB565, |
| 25 | DRM_FORMAT_XBGR8888, DRM_FORMAT_XRGB1555, |
| 26 | DRM_FORMAT_XRGB8888 }; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 27 | |
Dongseong Hwang | 750e0b9 | 2017-06-07 15:17:25 -0700 | [diff] [blame] | 28 | static const uint32_t tileable_texture_source_formats[] = { DRM_FORMAT_GR88, DRM_FORMAT_NV12, |
| 29 | DRM_FORMAT_R8, DRM_FORMAT_UYVY, |
| 30 | DRM_FORMAT_YUYV }; |
Gurchetan Singh | 8ac0c9a | 2017-05-15 09:34:22 -0700 | [diff] [blame] | 31 | |
| 32 | static const uint32_t texture_source_formats[] = { DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID }; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 33 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 34 | struct i915_device { |
Gurchetan Singh | 68af9c2 | 2017-01-18 13:48:11 -0800 | [diff] [blame] | 35 | uint32_t gen; |
| 36 | int32_t has_llc; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
Gurchetan Singh | 68af9c2 | 2017-01-18 13:48:11 -0800 | [diff] [blame] | 39 | static uint32_t i915_get_gen(int device_id) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 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]; |
Tomasz Figa | e821cc2 | 2017-07-08 15:53:11 +0900 | [diff] [blame] | 62 | if (combo->format != item->format) |
| 63 | continue; |
| 64 | |
| 65 | if (item->modifier == DRM_FORMAT_MOD_NONE && |
| 66 | combo->metadata.tiling == I915_TILING_X) { |
| 67 | /* |
| 68 | * FIXME: drv_query_kms() does not report the available modifiers |
| 69 | * yet, but we know that all hardware can scanout from X-tiled |
| 70 | * buffers, so let's add this to our combinations, except for |
| 71 | * cursor, which must not be tiled. |
| 72 | */ |
| 73 | combo->usage |= item->usage & ~BO_USE_CURSOR; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 74 | } |
Tomasz Figa | e821cc2 | 2017-07-08 15:53:11 +0900 | [diff] [blame] | 75 | |
| 76 | if (combo->metadata.modifier == item->modifier) |
| 77 | combo->usage |= item->usage; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static int i915_add_combinations(struct driver *drv) |
| 84 | { |
| 85 | int ret; |
| 86 | uint32_t i, num_items; |
| 87 | struct kms_item *items; |
| 88 | struct format_metadata metadata; |
Gurchetan Singh | 8ac0c9a | 2017-05-15 09:34:22 -0700 | [diff] [blame] | 89 | uint64_t render_flags, texture_flags; |
| 90 | |
| 91 | render_flags = BO_USE_RENDER_MASK; |
| 92 | texture_flags = BO_USE_TEXTURE_MASK; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 93 | |
| 94 | metadata.tiling = I915_TILING_NONE; |
| 95 | metadata.priority = 1; |
| 96 | metadata.modifier = DRM_FORMAT_MOD_NONE; |
| 97 | |
Gurchetan Singh | 8ac0c9a | 2017-05-15 09:34:22 -0700 | [diff] [blame] | 98 | ret = drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats), |
| 99 | &metadata, render_flags); |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 100 | if (ret) |
| 101 | return ret; |
| 102 | |
Gurchetan Singh | 8ac0c9a | 2017-05-15 09:34:22 -0700 | [diff] [blame] | 103 | ret = drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats), |
| 104 | &metadata, texture_flags); |
| 105 | if (ret) |
| 106 | return ret; |
| 107 | |
| 108 | ret = drv_add_combinations(drv, tileable_texture_source_formats, |
Dongseong Hwang | 3c5be5a | 2017-06-14 10:47:11 -0700 | [diff] [blame] | 109 | ARRAY_SIZE(tileable_texture_source_formats), &metadata, |
| 110 | texture_flags); |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 111 | if (ret) |
| 112 | return ret; |
| 113 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 114 | drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT); |
| 115 | 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] | 116 | |
Tomasz Figa | d30c0a5 | 2017-07-05 17:50:18 +0900 | [diff] [blame^] | 117 | /* IPU3 camera ISP supports only NV12 output. */ |
| 118 | drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata, |
| 119 | BO_USE_HW_CAMERA_READ | BO_USE_HW_CAMERA_WRITE); |
| 120 | /* |
| 121 | * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots |
| 122 | * from camera. |
| 123 | */ |
| 124 | drv_modify_combination(drv, DRM_FORMAT_R8, &metadata, |
| 125 | BO_USE_HW_CAMERA_READ | BO_USE_HW_CAMERA_WRITE); |
| 126 | |
Gurchetan Singh | 8ac0c9a | 2017-05-15 09:34:22 -0700 | [diff] [blame] | 127 | render_flags &= ~BO_USE_SW_WRITE_OFTEN; |
| 128 | render_flags &= ~BO_USE_SW_READ_OFTEN; |
| 129 | render_flags &= ~BO_USE_LINEAR; |
| 130 | |
| 131 | texture_flags &= ~BO_USE_SW_WRITE_OFTEN; |
| 132 | texture_flags &= ~BO_USE_SW_READ_OFTEN; |
| 133 | texture_flags &= ~BO_USE_LINEAR; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 134 | |
| 135 | metadata.tiling = I915_TILING_X; |
| 136 | metadata.priority = 2; |
Tomasz Figa | e821cc2 | 2017-07-08 15:53:11 +0900 | [diff] [blame] | 137 | metadata.modifier = I915_FORMAT_MOD_X_TILED; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 138 | |
Gurchetan Singh | 8ac0c9a | 2017-05-15 09:34:22 -0700 | [diff] [blame] | 139 | ret = drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats), |
| 140 | &metadata, render_flags); |
| 141 | if (ret) |
| 142 | return ret; |
| 143 | |
| 144 | ret = drv_add_combinations(drv, tileable_texture_source_formats, |
| 145 | ARRAY_SIZE(tileable_texture_source_formats), &metadata, |
| 146 | texture_flags); |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 147 | if (ret) |
| 148 | return ret; |
| 149 | |
| 150 | metadata.tiling = I915_TILING_Y; |
| 151 | metadata.priority = 3; |
Tomasz Figa | e821cc2 | 2017-07-08 15:53:11 +0900 | [diff] [blame] | 152 | metadata.modifier = I915_FORMAT_MOD_Y_TILED; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 153 | |
Gurchetan Singh | 8ac0c9a | 2017-05-15 09:34:22 -0700 | [diff] [blame] | 154 | ret = drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats), |
| 155 | &metadata, render_flags); |
| 156 | if (ret) |
| 157 | return ret; |
| 158 | |
| 159 | ret = drv_add_combinations(drv, tileable_texture_source_formats, |
| 160 | ARRAY_SIZE(tileable_texture_source_formats), &metadata, |
| 161 | texture_flags); |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 162 | if (ret) |
| 163 | return ret; |
| 164 | |
| 165 | items = drv_query_kms(drv, &num_items); |
| 166 | if (!items || !num_items) |
| 167 | return 0; |
| 168 | |
| 169 | for (i = 0; i < num_items; i++) { |
| 170 | ret = i915_add_kms_item(drv, &items[i]); |
| 171 | if (ret) { |
| 172 | free(items); |
| 173 | return ret; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | free(items); |
| 178 | return 0; |
| 179 | } |
| 180 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 181 | static int i915_align_dimensions(struct bo *bo, uint32_t tiling, uint32_t *stride, |
| 182 | uint32_t *aligned_height) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 183 | { |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 184 | struct i915_device *i915 = bo->drv->priv; |
| 185 | uint32_t horizontal_alignment = 4; |
| 186 | uint32_t vertical_alignment = 4; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 187 | |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 188 | switch (tiling) { |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 189 | default: |
| 190 | case I915_TILING_NONE: |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 191 | horizontal_alignment = 64; |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 192 | break; |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 193 | |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 194 | case I915_TILING_X: |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 195 | horizontal_alignment = 512; |
| 196 | vertical_alignment = 8; |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 197 | break; |
| 198 | |
| 199 | case I915_TILING_Y: |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 200 | if (i915->gen == 3) { |
| 201 | horizontal_alignment = 512; |
| 202 | vertical_alignment = 8; |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 203 | } else { |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 204 | horizontal_alignment = 128; |
| 205 | vertical_alignment = 32; |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 206 | } |
| 207 | break; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 208 | } |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 209 | |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 210 | *aligned_height = ALIGN(bo->height, vertical_alignment); |
| 211 | if (i915->gen > 3) { |
| 212 | *stride = ALIGN(*stride, horizontal_alignment); |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 213 | } else { |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 214 | while (*stride > horizontal_alignment) |
| 215 | horizontal_alignment <<= 1; |
| 216 | |
| 217 | *stride = horizontal_alignment; |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 218 | } |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 219 | |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 220 | if (i915->gen <= 3 && *stride > 8192) |
| 221 | return -EINVAL; |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 222 | |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 223 | return 0; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Gurchetan Singh | 68af9c2 | 2017-01-18 13:48:11 -0800 | [diff] [blame] | 226 | static void i915_clflush(void *start, size_t size) |
| 227 | { |
| 228 | void *p = (void *)(((uintptr_t)start) & ~I915_CACHELINE_MASK); |
| 229 | void *end = (void *)((uintptr_t)start + size); |
| 230 | |
| 231 | __builtin_ia32_mfence(); |
| 232 | while (p < end) { |
| 233 | __builtin_ia32_clflush(p); |
| 234 | p = (void *)((uintptr_t)p + I915_CACHELINE_SIZE); |
| 235 | } |
| 236 | } |
| 237 | |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 238 | static int i915_init(struct driver *drv) |
| 239 | { |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 240 | int ret; |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 241 | int device_id; |
| 242 | struct i915_device *i915; |
| 243 | drm_i915_getparam_t get_param; |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 244 | |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 245 | i915 = calloc(1, sizeof(*i915)); |
| 246 | if (!i915) |
| 247 | return -ENOMEM; |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 248 | |
| 249 | memset(&get_param, 0, sizeof(get_param)); |
| 250 | get_param.param = I915_PARAM_CHIPSET_ID; |
| 251 | get_param.value = &device_id; |
| 252 | ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param); |
| 253 | if (ret) { |
Gurchetan Singh | 68af9c2 | 2017-01-18 13:48:11 -0800 | [diff] [blame] | 254 | fprintf(stderr, "drv: Failed to get I915_PARAM_CHIPSET_ID\n"); |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 255 | free(i915); |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 256 | return -EINVAL; |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 257 | } |
| 258 | |
Gurchetan Singh | 68af9c2 | 2017-01-18 13:48:11 -0800 | [diff] [blame] | 259 | i915->gen = i915_get_gen(device_id); |
| 260 | |
| 261 | memset(&get_param, 0, sizeof(get_param)); |
| 262 | get_param.param = I915_PARAM_HAS_LLC; |
| 263 | get_param.value = &i915->has_llc; |
| 264 | ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param); |
| 265 | if (ret) { |
| 266 | fprintf(stderr, "drv: Failed to get I915_PARAM_HAS_LLC\n"); |
| 267 | free(i915); |
| 268 | return -EINVAL; |
| 269 | } |
| 270 | |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 271 | drv->priv = i915; |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 272 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 273 | return i915_add_combinations(drv); |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 276 | static int i915_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format, |
| 277 | uint32_t flags) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 278 | { |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 279 | int ret; |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 280 | size_t plane; |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 281 | uint32_t stride; |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 282 | struct drm_i915_gem_create gem_create; |
| 283 | struct drm_i915_gem_set_tiling gem_set_tiling; |
Tomasz Figa | 7ec0788 | 2017-06-23 18:04:02 +0900 | [diff] [blame] | 284 | struct combination *combo; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 285 | |
Tomasz Figa | 7ec0788 | 2017-06-23 18:04:02 +0900 | [diff] [blame] | 286 | combo = drv_get_combination(bo->drv, format, flags); |
| 287 | if (!combo) |
| 288 | return -EINVAL; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 289 | |
Tomasz Figa | 7ec0788 | 2017-06-23 18:04:02 +0900 | [diff] [blame] | 290 | bo->tiling = combo->metadata.tiling; |
Owen Lin | bbb69fd | 2017-06-05 14:33:08 +0800 | [diff] [blame] | 291 | |
| 292 | stride = drv_stride_from_format(format, width, 0); |
Gurchetan Singh | 507f5dd | 2017-03-16 13:14:30 -0700 | [diff] [blame] | 293 | |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 294 | ret = i915_align_dimensions(bo, bo->tiling, &stride, &height); |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 295 | if (ret) |
| 296 | return ret; |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 297 | |
Owen Lin | bbb69fd | 2017-06-05 14:33:08 +0800 | [diff] [blame] | 298 | /* |
| 299 | * Align the Y plane to 128 bytes so the chroma planes would be aligned |
| 300 | * to 64 byte boundaries. This is an Intel HW requirement. |
| 301 | */ |
| 302 | if (format == DRM_FORMAT_YVU420) |
| 303 | stride = ALIGN(stride, 128); |
| 304 | |
| 305 | /* |
| 306 | * HAL_PIXEL_FORMAT_YV12 requires that the buffer's height not be aligned. |
| 307 | */ |
| 308 | if (format == DRM_FORMAT_YVU420_ANDROID) |
| 309 | height = bo->height; |
| 310 | |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 311 | drv_bo_from_format(bo, stride, height, format); |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 312 | |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 313 | memset(&gem_create, 0, sizeof(gem_create)); |
| 314 | gem_create.size = bo->total_size; |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 315 | |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 316 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create); |
| 317 | if (ret) { |
| 318 | fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_CREATE failed (size=%llu)\n", |
| 319 | gem_create.size); |
| 320 | return ret; |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 321 | } |
Gurchetan Singh | 83dc4fb | 2016-07-19 15:52:33 -0700 | [diff] [blame] | 322 | |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 323 | for (plane = 0; plane < bo->num_planes; plane++) |
| 324 | bo->handles[plane].u32 = gem_create.handle; |
Daniel Nicoara | 1de26dc | 2014-09-25 18:53:19 -0400 | [diff] [blame] | 325 | |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 326 | memset(&gem_set_tiling, 0, sizeof(gem_set_tiling)); |
| 327 | gem_set_tiling.handle = bo->handles[0].u32; |
| 328 | gem_set_tiling.tiling_mode = bo->tiling; |
| 329 | gem_set_tiling.stride = bo->strides[0]; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 330 | |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 331 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_TILING, &gem_set_tiling); |
| 332 | if (ret) { |
| 333 | struct drm_gem_close gem_close; |
| 334 | memset(&gem_close, 0, sizeof(gem_close)); |
| 335 | gem_close.handle = bo->handles[0].u32; |
| 336 | drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close); |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 337 | |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 338 | fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_SET_TILING failed with %d", errno); |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 339 | return -errno; |
| 340 | } |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 345 | static void i915_close(struct driver *drv) |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 346 | { |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 347 | free(drv->priv); |
| 348 | drv->priv = NULL; |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 351 | static int i915_bo_import(struct bo *bo, struct drv_import_fd_data *data) |
| 352 | { |
| 353 | int ret; |
| 354 | struct drm_i915_gem_get_tiling gem_get_tiling; |
| 355 | |
| 356 | ret = drv_prime_bo_import(bo, data); |
| 357 | if (ret) |
| 358 | return ret; |
| 359 | |
| 360 | /* TODO(gsingh): export modifiers and get rid of backdoor tiling. */ |
| 361 | memset(&gem_get_tiling, 0, sizeof(gem_get_tiling)); |
| 362 | gem_get_tiling.handle = bo->handles[0].u32; |
| 363 | |
| 364 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_GET_TILING, &gem_get_tiling); |
| 365 | if (ret) { |
Joe Kniss | 9e5d12a | 2017-06-29 11:54:22 -0700 | [diff] [blame] | 366 | drv_gem_bo_destroy(bo); |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 367 | fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_GET_TILING failed."); |
| 368 | return ret; |
| 369 | } |
| 370 | |
| 371 | bo->tiling = gem_get_tiling.tiling_mode; |
| 372 | return 0; |
| 373 | } |
| 374 | |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 375 | 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] | 376 | { |
| 377 | int ret; |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 378 | void *addr; |
| 379 | struct drm_i915_gem_set_domain set_domain; |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 380 | |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 381 | memset(&set_domain, 0, sizeof(set_domain)); |
| 382 | set_domain.handle = bo->handles[0].u32; |
| 383 | if (bo->tiling == I915_TILING_NONE) { |
| 384 | struct drm_i915_gem_mmap gem_map; |
| 385 | memset(&gem_map, 0, sizeof(gem_map)); |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 386 | |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 387 | gem_map.handle = bo->handles[0].u32; |
| 388 | gem_map.offset = 0; |
| 389 | gem_map.size = bo->total_size; |
| 390 | |
| 391 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_map); |
| 392 | if (ret) { |
| 393 | fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_MMAP failed\n"); |
| 394 | return MAP_FAILED; |
| 395 | } |
| 396 | |
| 397 | addr = (void *)(uintptr_t)gem_map.addr_ptr; |
| 398 | set_domain.read_domains = I915_GEM_DOMAIN_CPU; |
| 399 | set_domain.write_domain = I915_GEM_DOMAIN_CPU; |
| 400 | |
| 401 | } else { |
| 402 | struct drm_i915_gem_mmap_gtt gem_map; |
| 403 | memset(&gem_map, 0, sizeof(gem_map)); |
| 404 | |
| 405 | gem_map.handle = bo->handles[0].u32; |
| 406 | |
| 407 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &gem_map); |
| 408 | if (ret) { |
| 409 | fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_MMAP_GTT failed\n"); |
| 410 | return MAP_FAILED; |
| 411 | } |
| 412 | |
| 413 | addr = mmap(0, bo->total_size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->drv->fd, |
| 414 | gem_map.offset); |
| 415 | |
| 416 | set_domain.read_domains = I915_GEM_DOMAIN_GTT; |
| 417 | set_domain.write_domain = I915_GEM_DOMAIN_GTT; |
| 418 | } |
| 419 | |
| 420 | if (addr == MAP_FAILED) { |
| 421 | fprintf(stderr, "drv: i915 GEM mmap failed\n"); |
| 422 | return addr; |
| 423 | } |
| 424 | |
| 425 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain); |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 426 | if (ret) { |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 427 | fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_SET_DOMAIN failed\n"); |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 428 | return MAP_FAILED; |
| 429 | } |
| 430 | |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 431 | data->length = bo->total_size; |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 432 | return addr; |
| 433 | } |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 434 | |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 435 | static int i915_bo_unmap(struct bo *bo, struct map_info *data) |
| 436 | { |
Gurchetan Singh | 68af9c2 | 2017-01-18 13:48:11 -0800 | [diff] [blame] | 437 | struct i915_device *i915 = bo->drv->priv; |
| 438 | if (!i915->has_llc && bo->tiling == I915_TILING_NONE) |
| 439 | i915_clflush(data->addr, data->length); |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 440 | |
| 441 | return munmap(data->addr, data->length); |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Tomasz Figa | ce1ae02 | 2017-07-05 18:15:06 +0900 | [diff] [blame] | 444 | static uint32_t i915_resolve_format(uint32_t format, uint64_t usage) |
Gurchetan Singh | bfba8c2 | 2016-08-16 17:57:10 -0700 | [diff] [blame] | 445 | { |
| 446 | switch (format) { |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 447 | case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED: |
Tomasz Figa | d30c0a5 | 2017-07-05 17:50:18 +0900 | [diff] [blame^] | 448 | /* KBL camera subsystem requires NV12. */ |
| 449 | if (usage & (BO_USE_HW_CAMERA_READ | BO_USE_HW_CAMERA_WRITE)) |
| 450 | return DRM_FORMAT_NV12; |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 451 | /*HACK: See b/28671744 */ |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 452 | return DRM_FORMAT_XBGR8888; |
| 453 | case DRM_FORMAT_FLEX_YCbCr_420_888: |
Tomasz Figa | d30c0a5 | 2017-07-05 17:50:18 +0900 | [diff] [blame^] | 454 | /* KBL camera subsystem requires NV12. */ |
| 455 | if (usage & (BO_USE_HW_CAMERA_READ | BO_USE_HW_CAMERA_WRITE)) |
| 456 | return DRM_FORMAT_NV12; |
Owen Lin | bbb69fd | 2017-06-05 14:33:08 +0800 | [diff] [blame] | 457 | return DRM_FORMAT_YVU420; |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 458 | default: |
| 459 | return format; |
Gurchetan Singh | bfba8c2 | 2016-08-16 17:57:10 -0700 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 463 | struct backend backend_i915 = { |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 464 | .name = "i915", |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame] | 465 | .init = i915_init, |
| 466 | .close = i915_close, |
| 467 | .bo_create = i915_bo_create, |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 468 | .bo_destroy = drv_gem_bo_destroy, |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 469 | .bo_import = i915_bo_import, |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame] | 470 | .bo_map = i915_bo_map, |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 471 | .bo_unmap = i915_bo_unmap, |
Gurchetan Singh | bfba8c2 | 2016-08-16 17:57:10 -0700 | [diff] [blame] | 472 | .resolve_format = i915_resolve_format, |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 473 | }; |
| 474 | |
| 475 | #endif |