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