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