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