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