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