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