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 | |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 9 | #include <assert.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 10 | #include <errno.h> |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 11 | #include <i915_drm.h> |
Kristian H. Kristensen | 9c3fb32 | 2018-04-11 15:55:13 -0700 | [diff] [blame] | 12 | #include <stdbool.h> |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 13 | #include <stdio.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 14 | #include <string.h> |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 15 | #include <sys/mman.h> |
Gurchetan Singh | cc35e69 | 2019-02-28 15:44:54 -0800 | [diff] [blame] | 16 | #include <unistd.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 17 | #include <xf86drm.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 18 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 19 | #include "drv_priv.h" |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 20 | #include "helpers.h" |
| 21 | #include "util.h" |
| 22 | |
Gurchetan Singh | 68af9c2 | 2017-01-18 13:48:11 -0800 | [diff] [blame] | 23 | #define I915_CACHELINE_SIZE 64 |
| 24 | #define I915_CACHELINE_MASK (I915_CACHELINE_SIZE - 1) |
| 25 | |
Nataraj Deshpande | 586e2d6 | 2019-08-21 15:19:46 -0700 | [diff] [blame] | 26 | static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR16161616F, DRM_FORMAT_ABGR2101010, |
Junichi Uekawa | d441f4e | 2020-01-29 15:45:16 +0900 | [diff] [blame] | 27 | DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB2101010, |
| 28 | DRM_FORMAT_ARGB8888, DRM_FORMAT_RGB565, |
Gurchetan Singh | 9d20b9e | 2019-12-19 09:48:21 -0800 | [diff] [blame] | 29 | DRM_FORMAT_XBGR2101010, DRM_FORMAT_XBGR8888, |
Nataraj Deshpande | 586e2d6 | 2019-08-21 15:19:46 -0700 | [diff] [blame] | 30 | DRM_FORMAT_XRGB2101010, DRM_FORMAT_XRGB8888 }; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 31 | |
Gurchetan Singh | 9d20b9e | 2019-12-19 09:48:21 -0800 | [diff] [blame] | 32 | static const uint32_t texture_source_formats[] = { DRM_FORMAT_R8, DRM_FORMAT_NV12, DRM_FORMAT_P010, |
| 33 | DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID }; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 34 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 35 | struct i915_device { |
Gurchetan Singh | 68af9c2 | 2017-01-18 13:48:11 -0800 | [diff] [blame] | 36 | uint32_t gen; |
| 37 | int32_t has_llc; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
Gurchetan Singh | 68af9c2 | 2017-01-18 13:48:11 -0800 | [diff] [blame] | 40 | static uint32_t i915_get_gen(int device_id) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 41 | { |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 42 | const uint16_t gen3_ids[] = { 0x2582, 0x2592, 0x2772, 0x27A2, 0x27AE, |
| 43 | 0x29C2, 0x29B2, 0x29D2, 0xA001, 0xA011 }; |
Stéphane Marchesin | a39dfde | 2014-09-15 15:38:25 -0700 | [diff] [blame] | 44 | unsigned i; |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 45 | for (i = 0; i < ARRAY_SIZE(gen3_ids); i++) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 46 | if (gen3_ids[i] == device_id) |
| 47 | return 3; |
| 48 | |
| 49 | return 4; |
| 50 | } |
| 51 | |
Kristian H. Kristensen | 9c3fb32 | 2018-04-11 15:55:13 -0700 | [diff] [blame] | 52 | /* |
| 53 | * We allow allocation of ARGB formats for SCANOUT if the corresponding XRGB |
| 54 | * formats supports it. It's up to the caller (chrome ozone) to ultimately not |
| 55 | * scan out ARGB if the display controller only supports XRGB, but we'll allow |
| 56 | * the allocation of the bo here. |
| 57 | */ |
| 58 | static bool format_compatible(const struct combination *combo, uint32_t format) |
| 59 | { |
| 60 | if (combo->format == format) |
| 61 | return true; |
| 62 | |
| 63 | switch (format) { |
| 64 | case DRM_FORMAT_XRGB8888: |
| 65 | return combo->format == DRM_FORMAT_ARGB8888; |
| 66 | case DRM_FORMAT_XBGR8888: |
| 67 | return combo->format == DRM_FORMAT_ABGR8888; |
| 68 | case DRM_FORMAT_RGBX8888: |
| 69 | return combo->format == DRM_FORMAT_RGBA8888; |
| 70 | case DRM_FORMAT_BGRX8888: |
| 71 | return combo->format == DRM_FORMAT_BGRA8888; |
Miguel Casas | 5548540 | 2019-11-19 16:10:17 -0500 | [diff] [blame] | 72 | case DRM_FORMAT_XRGB2101010: |
| 73 | return combo->format == DRM_FORMAT_ARGB2101010; |
| 74 | case DRM_FORMAT_XBGR2101010: |
| 75 | return combo->format == DRM_FORMAT_ABGR2101010; |
Kristian H. Kristensen | 9c3fb32 | 2018-04-11 15:55:13 -0700 | [diff] [blame] | 76 | default: |
| 77 | return false; |
| 78 | } |
| 79 | } |
| 80 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 81 | static int i915_add_kms_item(struct driver *drv, const struct kms_item *item) |
| 82 | { |
| 83 | uint32_t i; |
| 84 | struct combination *combo; |
| 85 | |
| 86 | /* |
| 87 | * Older hardware can't scanout Y-tiled formats. Newer devices can, and |
| 88 | * report this functionality via format modifiers. |
| 89 | */ |
Gurchetan Singh | bc9a87d | 2017-11-03 17:17:35 -0700 | [diff] [blame] | 90 | for (i = 0; i < drv_array_size(drv->combos); i++) { |
| 91 | combo = (struct combination *)drv_array_at_idx(drv->combos, i); |
Kristian H. Kristensen | 9c3fb32 | 2018-04-11 15:55:13 -0700 | [diff] [blame] | 92 | if (!format_compatible(combo, item->format)) |
Tomasz Figa | e821cc2 | 2017-07-08 15:53:11 +0900 | [diff] [blame] | 93 | continue; |
| 94 | |
Gurchetan Singh | d118a0e | 2018-01-12 23:31:50 +0000 | [diff] [blame] | 95 | if (item->modifier == DRM_FORMAT_MOD_LINEAR && |
Tomasz Figa | e821cc2 | 2017-07-08 15:53:11 +0900 | [diff] [blame] | 96 | combo->metadata.tiling == I915_TILING_X) { |
| 97 | /* |
| 98 | * FIXME: drv_query_kms() does not report the available modifiers |
| 99 | * yet, but we know that all hardware can scanout from X-tiled |
| 100 | * buffers, so let's add this to our combinations, except for |
| 101 | * cursor, which must not be tiled. |
| 102 | */ |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 103 | combo->use_flags |= item->use_flags & ~BO_USE_CURSOR; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 104 | } |
Tomasz Figa | e821cc2 | 2017-07-08 15:53:11 +0900 | [diff] [blame] | 105 | |
Kristian H. Kristensen | 3cb5bba | 2018-04-04 16:10:42 -0700 | [diff] [blame] | 106 | /* If we can scanout NV12, we support all tiling modes. */ |
| 107 | if (item->format == DRM_FORMAT_NV12) |
| 108 | combo->use_flags |= item->use_flags; |
| 109 | |
Tomasz Figa | e821cc2 | 2017-07-08 15:53:11 +0900 | [diff] [blame] | 110 | if (combo->metadata.modifier == item->modifier) |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 111 | combo->use_flags |= item->use_flags; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static int i915_add_combinations(struct driver *drv) |
| 118 | { |
| 119 | int ret; |
Gurchetan Singh | bc9a87d | 2017-11-03 17:17:35 -0700 | [diff] [blame] | 120 | uint32_t i; |
| 121 | struct drv_array *kms_items; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 122 | struct format_metadata metadata; |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 123 | uint64_t render_use_flags, texture_use_flags; |
Gurchetan Singh | 8ac0c9a | 2017-05-15 09:34:22 -0700 | [diff] [blame] | 124 | |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 125 | render_use_flags = BO_USE_RENDER_MASK; |
| 126 | texture_use_flags = BO_USE_TEXTURE_MASK; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 127 | |
| 128 | metadata.tiling = I915_TILING_NONE; |
| 129 | metadata.priority = 1; |
Kristian H. Kristensen | bc8c593 | 2017-10-24 18:36:32 -0700 | [diff] [blame] | 130 | metadata.modifier = DRM_FORMAT_MOD_LINEAR; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 131 | |
Gurchetan Singh | d300145 | 2017-11-03 17:18:36 -0700 | [diff] [blame] | 132 | drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats), |
| 133 | &metadata, render_use_flags); |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 134 | |
Gurchetan Singh | d300145 | 2017-11-03 17:18:36 -0700 | [diff] [blame] | 135 | drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats), |
| 136 | &metadata, texture_use_flags); |
Gurchetan Singh | 8ac0c9a | 2017-05-15 09:34:22 -0700 | [diff] [blame] | 137 | |
Hirokazu Honda | 3b8d4d0 | 2019-07-31 16:35:52 +0900 | [diff] [blame] | 138 | /* |
| 139 | * Chrome uses DMA-buf mmap to write to YV12 buffers, which are then accessed by the |
| 140 | * Video Encoder Accelerator (VEA). It could also support NV12 potentially in the future. |
| 141 | */ |
| 142 | drv_modify_combination(drv, DRM_FORMAT_YVU420, &metadata, BO_USE_HW_VIDEO_ENCODER); |
David Stevens | 6116b31 | 2019-09-03 10:49:50 +0900 | [diff] [blame] | 143 | drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata, |
| 144 | BO_USE_HW_VIDEO_ENCODER | BO_USE_HW_VIDEO_DECODER); |
Hirokazu Honda | 3b8d4d0 | 2019-07-31 16:35:52 +0900 | [diff] [blame] | 145 | |
Gurchetan Singh | 71bc665 | 2018-09-17 17:42:05 -0700 | [diff] [blame] | 146 | /* Android CTS tests require this. */ |
| 147 | drv_add_combination(drv, DRM_FORMAT_BGR888, &metadata, BO_USE_SW_MASK); |
| 148 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 149 | drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT); |
| 150 | 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] | 151 | |
Tomasz Figa | d30c0a5 | 2017-07-05 17:50:18 +0900 | [diff] [blame] | 152 | /* IPU3 camera ISP supports only NV12 output. */ |
| 153 | drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata, |
Tomasz Figa | fd0b016 | 2017-07-11 18:28:02 +0900 | [diff] [blame] | 154 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE); |
Tomasz Figa | d30c0a5 | 2017-07-05 17:50:18 +0900 | [diff] [blame] | 155 | /* |
| 156 | * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots |
| 157 | * from camera. |
| 158 | */ |
| 159 | drv_modify_combination(drv, DRM_FORMAT_R8, &metadata, |
Tomasz Figa | fd0b016 | 2017-07-11 18:28:02 +0900 | [diff] [blame] | 160 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE); |
Tomasz Figa | d30c0a5 | 2017-07-05 17:50:18 +0900 | [diff] [blame] | 161 | |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 162 | render_use_flags &= ~BO_USE_RENDERSCRIPT; |
| 163 | render_use_flags &= ~BO_USE_SW_WRITE_OFTEN; |
| 164 | render_use_flags &= ~BO_USE_SW_READ_OFTEN; |
| 165 | render_use_flags &= ~BO_USE_LINEAR; |
Gurchetan Singh | 2b1d689 | 2018-09-17 16:58:16 -0700 | [diff] [blame] | 166 | render_use_flags &= ~BO_USE_PROTECTED; |
Gurchetan Singh | 8ac0c9a | 2017-05-15 09:34:22 -0700 | [diff] [blame] | 167 | |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 168 | texture_use_flags &= ~BO_USE_RENDERSCRIPT; |
| 169 | texture_use_flags &= ~BO_USE_SW_WRITE_OFTEN; |
| 170 | texture_use_flags &= ~BO_USE_SW_READ_OFTEN; |
| 171 | texture_use_flags &= ~BO_USE_LINEAR; |
Gurchetan Singh | 2b1d689 | 2018-09-17 16:58:16 -0700 | [diff] [blame] | 172 | texture_use_flags &= ~BO_USE_PROTECTED; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 173 | |
| 174 | metadata.tiling = I915_TILING_X; |
| 175 | metadata.priority = 2; |
Tomasz Figa | e821cc2 | 2017-07-08 15:53:11 +0900 | [diff] [blame] | 176 | metadata.modifier = I915_FORMAT_MOD_X_TILED; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 177 | |
Gurchetan Singh | d300145 | 2017-11-03 17:18:36 -0700 | [diff] [blame] | 178 | drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats), |
| 179 | &metadata, render_use_flags); |
Gurchetan Singh | 8ac0c9a | 2017-05-15 09:34:22 -0700 | [diff] [blame] | 180 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 181 | metadata.tiling = I915_TILING_Y; |
| 182 | metadata.priority = 3; |
Tomasz Figa | e821cc2 | 2017-07-08 15:53:11 +0900 | [diff] [blame] | 183 | metadata.modifier = I915_FORMAT_MOD_Y_TILED; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 184 | |
Gurchetan Singh | d300145 | 2017-11-03 17:18:36 -0700 | [diff] [blame] | 185 | drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats), |
| 186 | &metadata, render_use_flags); |
Gurchetan Singh | 8ac0c9a | 2017-05-15 09:34:22 -0700 | [diff] [blame] | 187 | |
Miguel Casas | cdb2554 | 2019-07-18 13:07:30 -0400 | [diff] [blame] | 188 | /* Support y-tiled NV12 and P010 for libva */ |
Gurchetan Singh | 86ddfdc | 2018-09-17 17:13:45 -0700 | [diff] [blame] | 189 | drv_add_combination(drv, DRM_FORMAT_NV12, &metadata, |
| 190 | BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER); |
Miguel Casas | cdb2554 | 2019-07-18 13:07:30 -0400 | [diff] [blame] | 191 | drv_add_combination(drv, DRM_FORMAT_P010, &metadata, |
| 192 | BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER); |
Kristian H. Kristensen | 3cb5bba | 2018-04-04 16:10:42 -0700 | [diff] [blame] | 193 | |
Gurchetan Singh | bc9a87d | 2017-11-03 17:17:35 -0700 | [diff] [blame] | 194 | kms_items = drv_query_kms(drv); |
| 195 | if (!kms_items) |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 196 | return 0; |
| 197 | |
Gurchetan Singh | bc9a87d | 2017-11-03 17:17:35 -0700 | [diff] [blame] | 198 | for (i = 0; i < drv_array_size(kms_items); i++) { |
| 199 | ret = i915_add_kms_item(drv, (struct kms_item *)drv_array_at_idx(kms_items, i)); |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 200 | if (ret) { |
Gurchetan Singh | bc9a87d | 2017-11-03 17:17:35 -0700 | [diff] [blame] | 201 | drv_array_destroy(kms_items); |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 202 | return ret; |
| 203 | } |
| 204 | } |
| 205 | |
Gurchetan Singh | bc9a87d | 2017-11-03 17:17:35 -0700 | [diff] [blame] | 206 | drv_array_destroy(kms_items); |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 210 | static int i915_align_dimensions(struct bo *bo, uint32_t tiling, uint32_t *stride, |
| 211 | uint32_t *aligned_height) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 212 | { |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 213 | struct i915_device *i915 = bo->drv->priv; |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 214 | uint32_t horizontal_alignment; |
| 215 | uint32_t vertical_alignment; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 216 | |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 217 | switch (tiling) { |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 218 | default: |
| 219 | case I915_TILING_NONE: |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 220 | /* |
| 221 | * The Intel GPU doesn't need any alignment in linear mode, |
| 222 | * but libva requires the allocation stride to be aligned to |
| 223 | * 16 bytes and height to 4 rows. Further, we round up the |
| 224 | * horizontal alignment so that row start on a cache line (64 |
| 225 | * bytes). |
| 226 | */ |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 227 | horizontal_alignment = 64; |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 228 | vertical_alignment = 4; |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 229 | break; |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 230 | |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 231 | case I915_TILING_X: |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 232 | horizontal_alignment = 512; |
| 233 | vertical_alignment = 8; |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 234 | break; |
| 235 | |
| 236 | case I915_TILING_Y: |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 237 | if (i915->gen == 3) { |
| 238 | horizontal_alignment = 512; |
| 239 | vertical_alignment = 8; |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 240 | } else { |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 241 | horizontal_alignment = 128; |
| 242 | vertical_alignment = 32; |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 243 | } |
| 244 | break; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 245 | } |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 246 | |
David Stevens | 793675a | 2019-09-25 11:17:48 +0900 | [diff] [blame] | 247 | *aligned_height = ALIGN(*aligned_height, vertical_alignment); |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 248 | if (i915->gen > 3) { |
| 249 | *stride = ALIGN(*stride, horizontal_alignment); |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 250 | } else { |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 251 | while (*stride > horizontal_alignment) |
| 252 | horizontal_alignment <<= 1; |
| 253 | |
| 254 | *stride = horizontal_alignment; |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 255 | } |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 256 | |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 257 | if (i915->gen <= 3 && *stride > 8192) |
| 258 | return -EINVAL; |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 259 | |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 260 | return 0; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Gurchetan Singh | 68af9c2 | 2017-01-18 13:48:11 -0800 | [diff] [blame] | 263 | static void i915_clflush(void *start, size_t size) |
| 264 | { |
| 265 | void *p = (void *)(((uintptr_t)start) & ~I915_CACHELINE_MASK); |
| 266 | void *end = (void *)((uintptr_t)start + size); |
| 267 | |
| 268 | __builtin_ia32_mfence(); |
| 269 | while (p < end) { |
| 270 | __builtin_ia32_clflush(p); |
| 271 | p = (void *)((uintptr_t)p + I915_CACHELINE_SIZE); |
| 272 | } |
| 273 | } |
| 274 | |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 275 | static int i915_init(struct driver *drv) |
| 276 | { |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 277 | int ret; |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 278 | int device_id; |
| 279 | struct i915_device *i915; |
| 280 | drm_i915_getparam_t get_param; |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 281 | |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 282 | i915 = calloc(1, sizeof(*i915)); |
| 283 | if (!i915) |
| 284 | return -ENOMEM; |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 285 | |
| 286 | memset(&get_param, 0, sizeof(get_param)); |
| 287 | get_param.param = I915_PARAM_CHIPSET_ID; |
| 288 | get_param.value = &device_id; |
| 289 | ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param); |
| 290 | if (ret) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 291 | drv_log("Failed to get I915_PARAM_CHIPSET_ID\n"); |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 292 | free(i915); |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 293 | return -EINVAL; |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Gurchetan Singh | 68af9c2 | 2017-01-18 13:48:11 -0800 | [diff] [blame] | 296 | i915->gen = i915_get_gen(device_id); |
| 297 | |
| 298 | memset(&get_param, 0, sizeof(get_param)); |
| 299 | get_param.param = I915_PARAM_HAS_LLC; |
| 300 | get_param.value = &i915->has_llc; |
| 301 | ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param); |
| 302 | if (ret) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 303 | drv_log("Failed to get I915_PARAM_HAS_LLC\n"); |
Gurchetan Singh | 68af9c2 | 2017-01-18 13:48:11 -0800 | [diff] [blame] | 304 | free(i915); |
| 305 | return -EINVAL; |
| 306 | } |
| 307 | |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 308 | drv->priv = i915; |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 309 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 310 | return i915_add_combinations(drv); |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 313 | static int i915_bo_from_format(struct bo *bo, uint32_t width, uint32_t height, uint32_t format) |
| 314 | { |
| 315 | uint32_t offset; |
| 316 | size_t plane; |
Gurchetan Singh | cc35e69 | 2019-02-28 15:44:54 -0800 | [diff] [blame] | 317 | int ret, pagesize; |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 318 | |
| 319 | offset = 0; |
Gurchetan Singh | cc35e69 | 2019-02-28 15:44:54 -0800 | [diff] [blame] | 320 | pagesize = getpagesize(); |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 321 | for (plane = 0; plane < drv_num_planes_from_format(format); plane++) { |
| 322 | uint32_t stride = drv_stride_from_format(format, width, plane); |
| 323 | uint32_t plane_height = drv_height_from_format(format, height, plane); |
| 324 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 325 | if (bo->meta.tiling != I915_TILING_NONE) |
Gurchetan Singh | cc35e69 | 2019-02-28 15:44:54 -0800 | [diff] [blame] | 326 | assert(IS_ALIGNED(offset, pagesize)); |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 327 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 328 | ret = i915_align_dimensions(bo, bo->meta.tiling, &stride, &plane_height); |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 329 | if (ret) |
| 330 | return ret; |
| 331 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 332 | bo->meta.strides[plane] = stride; |
| 333 | bo->meta.sizes[plane] = stride * plane_height; |
| 334 | bo->meta.offsets[plane] = offset; |
| 335 | offset += bo->meta.sizes[plane]; |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 338 | bo->meta.total_size = ALIGN(offset, pagesize); |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
Kristian H. Kristensen | 6061eab | 2017-10-03 13:53:19 -0700 | [diff] [blame] | 343 | static int i915_bo_create_for_modifier(struct bo *bo, uint32_t width, uint32_t height, |
| 344 | uint32_t format, uint64_t modifier) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 345 | { |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 346 | int ret; |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 347 | size_t plane; |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 348 | struct drm_i915_gem_create gem_create; |
| 349 | struct drm_i915_gem_set_tiling gem_set_tiling; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 350 | |
Kristian H. Kristensen | 6061eab | 2017-10-03 13:53:19 -0700 | [diff] [blame] | 351 | switch (modifier) { |
| 352 | case DRM_FORMAT_MOD_LINEAR: |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 353 | bo->meta.tiling = I915_TILING_NONE; |
Kristian H. Kristensen | 6061eab | 2017-10-03 13:53:19 -0700 | [diff] [blame] | 354 | break; |
| 355 | case I915_FORMAT_MOD_X_TILED: |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 356 | bo->meta.tiling = I915_TILING_X; |
Kristian H. Kristensen | 6061eab | 2017-10-03 13:53:19 -0700 | [diff] [blame] | 357 | break; |
| 358 | case I915_FORMAT_MOD_Y_TILED: |
Mark Yacoub | c956564 | 2020-02-07 11:02:22 -0500 | [diff] [blame^] | 359 | case I915_FORMAT_MOD_Y_TILED_CCS: |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 360 | bo->meta.tiling = I915_TILING_Y; |
Kristian H. Kristensen | 6061eab | 2017-10-03 13:53:19 -0700 | [diff] [blame] | 361 | break; |
| 362 | } |
Owen Lin | bbb69fd | 2017-06-05 14:33:08 +0800 | [diff] [blame] | 363 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 364 | bo->meta.format_modifiers[0] = modifier; |
Kristian H. Kristensen | 2b8f89e | 2018-02-07 16:10:06 -0800 | [diff] [blame] | 365 | |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 366 | if (format == DRM_FORMAT_YVU420_ANDROID) { |
| 367 | /* |
| 368 | * We only need to be able to use this as a linear texture, |
| 369 | * which doesn't put any HW restrictions on how we lay it |
| 370 | * out. The Android format does require the stride to be a |
| 371 | * multiple of 16 and expects the Cr and Cb stride to be |
| 372 | * ALIGN(Y_stride / 2, 16), which we can make happen by |
| 373 | * aligning to 32 bytes here. |
| 374 | */ |
| 375 | uint32_t stride = ALIGN(width, 32); |
| 376 | drv_bo_from_format(bo, stride, height, format); |
Mark Yacoub | c956564 | 2020-02-07 11:02:22 -0500 | [diff] [blame^] | 377 | } else if (modifier == I915_FORMAT_MOD_Y_TILED_CCS) { |
| 378 | /* |
| 379 | * For compressed surfaces, we need a color control surface |
| 380 | * (CCS). Color compression is only supported for Y tiled |
| 381 | * surfaces, and for each 32x16 tiles in the main surface we |
| 382 | * need a tile in the control surface. Y tiles are 128 bytes |
| 383 | * wide and 32 lines tall and we use that to first compute the |
| 384 | * width and height in tiles of the main surface. stride and |
| 385 | * height are already multiples of 128 and 32, respectively: |
| 386 | */ |
| 387 | uint32_t stride = drv_stride_from_format(format, width, 0); |
| 388 | uint32_t width_in_tiles = DIV_ROUND_UP(stride, 128); |
| 389 | uint32_t height_in_tiles = DIV_ROUND_UP(height, 32); |
| 390 | uint32_t size = width_in_tiles * height_in_tiles * 4096; |
| 391 | uint32_t offset = 0; |
| 392 | |
| 393 | bo->meta.strides[0] = width_in_tiles * 128; |
| 394 | bo->meta.sizes[0] = size; |
| 395 | bo->meta.offsets[0] = offset; |
| 396 | offset += size; |
| 397 | |
| 398 | /* |
| 399 | * Now, compute the width and height in tiles of the control |
| 400 | * surface by dividing and rounding up. |
| 401 | */ |
| 402 | uint32_t ccs_width_in_tiles = DIV_ROUND_UP(width_in_tiles, 32); |
| 403 | uint32_t ccs_height_in_tiles = DIV_ROUND_UP(height_in_tiles, 16); |
| 404 | uint32_t ccs_size = ccs_width_in_tiles * ccs_height_in_tiles * 4096; |
| 405 | |
| 406 | /* |
| 407 | * With stride and height aligned to y tiles, offset is |
| 408 | * already a multiple of 4096, which is the required alignment |
| 409 | * of the CCS. |
| 410 | */ |
| 411 | bo->meta.strides[1] = ccs_width_in_tiles * 128; |
| 412 | bo->meta.sizes[1] = ccs_size; |
| 413 | bo->meta.offsets[1] = offset; |
| 414 | offset += ccs_size; |
| 415 | |
| 416 | bo->meta.num_planes = 2; |
| 417 | bo->meta.total_size = offset; |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 418 | } else { |
| 419 | i915_bo_from_format(bo, width, height, format); |
| 420 | } |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 421 | |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 422 | memset(&gem_create, 0, sizeof(gem_create)); |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 423 | gem_create.size = bo->meta.total_size; |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 424 | |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 425 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create); |
| 426 | if (ret) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 427 | drv_log("DRM_IOCTL_I915_GEM_CREATE failed (size=%llu)\n", gem_create.size); |
Stéphane Marchesin | 6ac299f | 2019-03-21 12:23:29 -0700 | [diff] [blame] | 428 | return -errno; |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 429 | } |
Gurchetan Singh | 83dc4fb | 2016-07-19 15:52:33 -0700 | [diff] [blame] | 430 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 431 | for (plane = 0; plane < bo->meta.num_planes; plane++) |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 432 | bo->handles[plane].u32 = gem_create.handle; |
Daniel Nicoara | 1de26dc | 2014-09-25 18:53:19 -0400 | [diff] [blame] | 433 | |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 434 | memset(&gem_set_tiling, 0, sizeof(gem_set_tiling)); |
| 435 | gem_set_tiling.handle = bo->handles[0].u32; |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 436 | gem_set_tiling.tiling_mode = bo->meta.tiling; |
| 437 | gem_set_tiling.stride = bo->meta.strides[0]; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 438 | |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 439 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_TILING, &gem_set_tiling); |
| 440 | if (ret) { |
| 441 | struct drm_gem_close gem_close; |
| 442 | memset(&gem_close, 0, sizeof(gem_close)); |
| 443 | gem_close.handle = bo->handles[0].u32; |
| 444 | drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close); |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 445 | |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 446 | drv_log("DRM_IOCTL_I915_GEM_SET_TILING failed with %d\n", errno); |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 447 | return -errno; |
| 448 | } |
| 449 | |
| 450 | return 0; |
| 451 | } |
| 452 | |
Kristian H. Kristensen | 6061eab | 2017-10-03 13:53:19 -0700 | [diff] [blame] | 453 | static int i915_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format, |
| 454 | uint64_t use_flags) |
| 455 | { |
| 456 | struct combination *combo; |
| 457 | |
| 458 | combo = drv_get_combination(bo->drv, format, use_flags); |
| 459 | if (!combo) |
| 460 | return -EINVAL; |
| 461 | |
| 462 | return i915_bo_create_for_modifier(bo, width, height, format, combo->metadata.modifier); |
| 463 | } |
| 464 | |
| 465 | static int i915_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height, |
| 466 | uint32_t format, const uint64_t *modifiers, uint32_t count) |
| 467 | { |
| 468 | static const uint64_t modifier_order[] = { |
Mark Yacoub | c956564 | 2020-02-07 11:02:22 -0500 | [diff] [blame^] | 469 | I915_FORMAT_MOD_Y_TILED_CCS, |
Gurchetan Singh | 2b1d689 | 2018-09-17 16:58:16 -0700 | [diff] [blame] | 470 | I915_FORMAT_MOD_Y_TILED, |
| 471 | I915_FORMAT_MOD_X_TILED, |
| 472 | DRM_FORMAT_MOD_LINEAR, |
Kristian H. Kristensen | 6061eab | 2017-10-03 13:53:19 -0700 | [diff] [blame] | 473 | }; |
| 474 | uint64_t modifier; |
| 475 | |
| 476 | modifier = drv_pick_modifier(modifiers, count, modifier_order, ARRAY_SIZE(modifier_order)); |
| 477 | |
Kristian H. Kristensen | 6061eab | 2017-10-03 13:53:19 -0700 | [diff] [blame] | 478 | return i915_bo_create_for_modifier(bo, width, height, format, modifier); |
| 479 | } |
| 480 | |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 481 | static void i915_close(struct driver *drv) |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 482 | { |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 483 | free(drv->priv); |
| 484 | drv->priv = NULL; |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 485 | } |
| 486 | |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 487 | static int i915_bo_import(struct bo *bo, struct drv_import_fd_data *data) |
| 488 | { |
| 489 | int ret; |
| 490 | struct drm_i915_gem_get_tiling gem_get_tiling; |
| 491 | |
| 492 | ret = drv_prime_bo_import(bo, data); |
| 493 | if (ret) |
| 494 | return ret; |
| 495 | |
| 496 | /* TODO(gsingh): export modifiers and get rid of backdoor tiling. */ |
| 497 | memset(&gem_get_tiling, 0, sizeof(gem_get_tiling)); |
| 498 | gem_get_tiling.handle = bo->handles[0].u32; |
| 499 | |
| 500 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_GET_TILING, &gem_get_tiling); |
| 501 | if (ret) { |
Joe Kniss | 9e5d12a | 2017-06-29 11:54:22 -0700 | [diff] [blame] | 502 | drv_gem_bo_destroy(bo); |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 503 | drv_log("DRM_IOCTL_I915_GEM_GET_TILING failed.\n"); |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 504 | return ret; |
| 505 | } |
| 506 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 507 | bo->meta.tiling = gem_get_tiling.tiling_mode; |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 508 | return 0; |
| 509 | } |
| 510 | |
Gurchetan Singh | ee43c30 | 2017-11-14 18:20:27 -0800 | [diff] [blame] | 511 | static void *i915_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags) |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 512 | { |
| 513 | int ret; |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 514 | void *addr; |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 515 | |
Mark Yacoub | c956564 | 2020-02-07 11:02:22 -0500 | [diff] [blame^] | 516 | if (bo->meta.format_modifiers[0] == I915_FORMAT_MOD_Y_TILED_CCS) |
| 517 | return MAP_FAILED; |
| 518 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 519 | if (bo->meta.tiling == I915_TILING_NONE) { |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 520 | struct drm_i915_gem_mmap gem_map; |
| 521 | memset(&gem_map, 0, sizeof(gem_map)); |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 522 | |
Tomasz Figa | 39eb951 | 2018-11-01 00:45:31 +0900 | [diff] [blame] | 523 | /* TODO(b/118799155): We don't seem to have a good way to |
| 524 | * detect the use cases for which WC mapping is really needed. |
| 525 | * The current heuristic seems overly coarse and may be slowing |
| 526 | * down some other use cases unnecessarily. |
| 527 | * |
| 528 | * For now, care must be taken not to use WC mappings for |
| 529 | * Renderscript and camera use cases, as they're |
| 530 | * performance-sensitive. */ |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 531 | if ((bo->meta.use_flags & BO_USE_SCANOUT) && |
| 532 | !(bo->meta.use_flags & |
Tomasz Figa | 39eb951 | 2018-11-01 00:45:31 +0900 | [diff] [blame] | 533 | (BO_USE_RENDERSCRIPT | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))) |
Gurchetan Singh | 5af2023 | 2017-09-19 15:10:58 -0700 | [diff] [blame] | 534 | gem_map.flags = I915_MMAP_WC; |
| 535 | |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 536 | gem_map.handle = bo->handles[0].u32; |
| 537 | gem_map.offset = 0; |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 538 | gem_map.size = bo->meta.total_size; |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 539 | |
| 540 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_map); |
| 541 | if (ret) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 542 | drv_log("DRM_IOCTL_I915_GEM_MMAP failed\n"); |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 543 | return MAP_FAILED; |
| 544 | } |
| 545 | |
| 546 | addr = (void *)(uintptr_t)gem_map.addr_ptr; |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 547 | } else { |
| 548 | struct drm_i915_gem_mmap_gtt gem_map; |
| 549 | memset(&gem_map, 0, sizeof(gem_map)); |
| 550 | |
| 551 | gem_map.handle = bo->handles[0].u32; |
| 552 | |
| 553 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &gem_map); |
| 554 | if (ret) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 555 | drv_log("DRM_IOCTL_I915_GEM_MMAP_GTT failed\n"); |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 556 | return MAP_FAILED; |
| 557 | } |
| 558 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 559 | addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, |
| 560 | bo->drv->fd, gem_map.offset); |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | if (addr == MAP_FAILED) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 564 | drv_log("i915 GEM mmap failed\n"); |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 565 | return addr; |
| 566 | } |
| 567 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 568 | vma->length = bo->meta.total_size; |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 569 | return addr; |
| 570 | } |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 571 | |
Gurchetan Singh | 47e629b | 2017-11-02 14:07:18 -0700 | [diff] [blame] | 572 | static int i915_bo_invalidate(struct bo *bo, struct mapping *mapping) |
Gurchetan Singh | 2d1877f | 2017-10-10 14:12:46 -0700 | [diff] [blame] | 573 | { |
| 574 | int ret; |
| 575 | struct drm_i915_gem_set_domain set_domain; |
| 576 | |
| 577 | memset(&set_domain, 0, sizeof(set_domain)); |
| 578 | set_domain.handle = bo->handles[0].u32; |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 579 | if (bo->meta.tiling == I915_TILING_NONE) { |
Gurchetan Singh | 2d1877f | 2017-10-10 14:12:46 -0700 | [diff] [blame] | 580 | set_domain.read_domains = I915_GEM_DOMAIN_CPU; |
Gurchetan Singh | 47e629b | 2017-11-02 14:07:18 -0700 | [diff] [blame] | 581 | if (mapping->vma->map_flags & BO_MAP_WRITE) |
Gurchetan Singh | 2d1877f | 2017-10-10 14:12:46 -0700 | [diff] [blame] | 582 | set_domain.write_domain = I915_GEM_DOMAIN_CPU; |
| 583 | } else { |
| 584 | set_domain.read_domains = I915_GEM_DOMAIN_GTT; |
Gurchetan Singh | 47e629b | 2017-11-02 14:07:18 -0700 | [diff] [blame] | 585 | if (mapping->vma->map_flags & BO_MAP_WRITE) |
Gurchetan Singh | 2d1877f | 2017-10-10 14:12:46 -0700 | [diff] [blame] | 586 | set_domain.write_domain = I915_GEM_DOMAIN_GTT; |
| 587 | } |
| 588 | |
| 589 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain); |
| 590 | if (ret) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 591 | drv_log("DRM_IOCTL_I915_GEM_SET_DOMAIN with %d\n", ret); |
Gurchetan Singh | 2d1877f | 2017-10-10 14:12:46 -0700 | [diff] [blame] | 592 | return ret; |
| 593 | } |
| 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
Gurchetan Singh | 47e629b | 2017-11-02 14:07:18 -0700 | [diff] [blame] | 598 | static int i915_bo_flush(struct bo *bo, struct mapping *mapping) |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 599 | { |
Gurchetan Singh | 68af9c2 | 2017-01-18 13:48:11 -0800 | [diff] [blame] | 600 | struct i915_device *i915 = bo->drv->priv; |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 601 | if (!i915->has_llc && bo->meta.tiling == I915_TILING_NONE) |
Gurchetan Singh | 47e629b | 2017-11-02 14:07:18 -0700 | [diff] [blame] | 602 | i915_clflush(mapping->vma->addr, mapping->vma->length); |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 603 | |
Gurchetan Singh | 8e02e05 | 2017-09-14 14:18:43 -0700 | [diff] [blame] | 604 | return 0; |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Gurchetan Singh | 0d44d48 | 2019-06-04 19:39:51 -0700 | [diff] [blame] | 607 | static uint32_t i915_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags) |
Gurchetan Singh | bfba8c2 | 2016-08-16 17:57:10 -0700 | [diff] [blame] | 608 | { |
| 609 | switch (format) { |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 610 | case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED: |
Tomasz Figa | d30c0a5 | 2017-07-05 17:50:18 +0900 | [diff] [blame] | 611 | /* KBL camera subsystem requires NV12. */ |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 612 | if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)) |
Tomasz Figa | d30c0a5 | 2017-07-05 17:50:18 +0900 | [diff] [blame] | 613 | return DRM_FORMAT_NV12; |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 614 | /*HACK: See b/28671744 */ |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 615 | return DRM_FORMAT_XBGR8888; |
| 616 | case DRM_FORMAT_FLEX_YCbCr_420_888: |
Tomasz Figa | b92e4f8 | 2017-06-22 16:52:43 +0900 | [diff] [blame] | 617 | /* |
| 618 | * KBL camera subsystem requires NV12. Our other use cases |
| 619 | * don't care: |
| 620 | * - Hardware video supports NV12, |
| 621 | * - USB Camera HALv3 supports NV12, |
| 622 | * - USB Camera HALv1 doesn't use this format. |
| 623 | * Moreover, NV12 is preferred for video, due to overlay |
| 624 | * support on SKL+. |
| 625 | */ |
| 626 | return DRM_FORMAT_NV12; |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 627 | default: |
| 628 | return format; |
Gurchetan Singh | bfba8c2 | 2016-08-16 17:57:10 -0700 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | |
Gurchetan Singh | 3e9d383 | 2017-10-31 10:36:25 -0700 | [diff] [blame] | 632 | const struct backend backend_i915 = { |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 633 | .name = "i915", |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame] | 634 | .init = i915_init, |
| 635 | .close = i915_close, |
| 636 | .bo_create = i915_bo_create, |
Kristian H. Kristensen | 6061eab | 2017-10-03 13:53:19 -0700 | [diff] [blame] | 637 | .bo_create_with_modifiers = i915_bo_create_with_modifiers, |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 638 | .bo_destroy = drv_gem_bo_destroy, |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 639 | .bo_import = i915_bo_import, |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame] | 640 | .bo_map = i915_bo_map, |
Gurchetan Singh | 8e02e05 | 2017-09-14 14:18:43 -0700 | [diff] [blame] | 641 | .bo_unmap = drv_bo_munmap, |
Gurchetan Singh | 2d1877f | 2017-10-10 14:12:46 -0700 | [diff] [blame] | 642 | .bo_invalidate = i915_bo_invalidate, |
Gurchetan Singh | 8e02e05 | 2017-09-14 14:18:43 -0700 | [diff] [blame] | 643 | .bo_flush = i915_bo_flush, |
Gurchetan Singh | bfba8c2 | 2016-08-16 17:57:10 -0700 | [diff] [blame] | 644 | .resolve_format = i915_resolve_format, |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 645 | }; |
| 646 | |
| 647 | #endif |