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 | |
Yiwei Zhang | b7a6444 | 2021-09-30 05:13:10 +0000 | [diff] [blame] | 18 | #include "drv_helpers.h" |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 19 | #include "drv_priv.h" |
Gurchetan Singh | 13b0012 | 2020-10-07 14:31:20 -0700 | [diff] [blame] | 20 | #include "external/i915_drm.h" |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 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 | |
Vipin Anand | a0af309 | 2020-06-17 10:53:02 +0530 | [diff] [blame] | 40 | static const uint64_t gen12_modifier_order[] = { I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS, |
| 41 | I915_FORMAT_MOD_Y_TILED, I915_FORMAT_MOD_X_TILED, |
| 42 | DRM_FORMAT_MOD_LINEAR }; |
| 43 | |
Cooper Chiou | c8c61d3 | 2021-10-27 12:18:30 +0800 | [diff] [blame] | 44 | static const uint64_t gen11_modifier_order[] = { I915_FORMAT_MOD_Y_TILED, I915_FORMAT_MOD_X_TILED, |
| 45 | DRM_FORMAT_MOD_LINEAR }; |
| 46 | |
Binu R S | 8d70518 | 2020-07-20 10:36:53 +0530 | [diff] [blame] | 47 | struct modifier_support_t { |
| 48 | const uint64_t *order; |
| 49 | uint32_t count; |
| 50 | }; |
| 51 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 52 | struct i915_device { |
Gurchetan Singh | 68af9c2 | 2017-01-18 13:48:11 -0800 | [diff] [blame] | 53 | uint32_t gen; |
| 54 | int32_t has_llc; |
Gurchetan Singh | f98d1c1 | 2020-10-07 15:46:23 -0700 | [diff] [blame] | 55 | int32_t has_hw_protection; |
Binu R S | 8d70518 | 2020-07-20 10:36:53 +0530 | [diff] [blame] | 56 | struct modifier_support_t modifier; |
Nathan Ciobanu | 87ec79b | 2021-01-21 21:26:54 -0800 | [diff] [blame] | 57 | int device_id; |
| 58 | bool is_adlp; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Nathan Ciobanu | 87ec79b | 2021-01-21 21:26:54 -0800 | [diff] [blame] | 61 | static void i915_info_from_device_id(struct i915_device *i915) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 62 | { |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 63 | const uint16_t gen3_ids[] = { 0x2582, 0x2592, 0x2772, 0x27A2, 0x27AE, |
| 64 | 0x29C2, 0x29B2, 0x29D2, 0xA001, 0xA011 }; |
Nicholas Bishop | a204724 | 2021-10-29 12:42:33 -0400 | [diff] [blame] | 65 | const uint16_t gen4_ids[] = { 0x29A2, 0x2992, 0x2982, 0x2972, 0x2A02, 0x2A12, 0x2A42, |
| 66 | 0x2E02, 0x2E12, 0x2E22, 0x2E32, 0x2E42, 0x2E92 }; |
| 67 | const uint16_t gen5_ids[] = { 0x0042, 0x0046 }; |
| 68 | const uint16_t gen6_ids[] = { 0x0102, 0x0112, 0x0122, 0x0106, 0x0116, 0x0126, 0x010A }; |
| 69 | const uint16_t gen7_ids[] = { |
| 70 | 0x0152, 0x0162, 0x0156, 0x0166, 0x015a, 0x016a, 0x0402, 0x0412, 0x0422, |
| 71 | 0x0406, 0x0416, 0x0426, 0x040A, 0x041A, 0x042A, 0x040B, 0x041B, 0x042B, |
| 72 | 0x040E, 0x041E, 0x042E, 0x0C02, 0x0C12, 0x0C22, 0x0C06, 0x0C16, 0x0C26, |
| 73 | 0x0C0A, 0x0C1A, 0x0C2A, 0x0C0B, 0x0C1B, 0x0C2B, 0x0C0E, 0x0C1E, 0x0C2E, |
| 74 | 0x0A02, 0x0A12, 0x0A22, 0x0A06, 0x0A16, 0x0A26, 0x0A0A, 0x0A1A, 0x0A2A, |
| 75 | 0x0A0B, 0x0A1B, 0x0A2B, 0x0A0E, 0x0A1E, 0x0A2E, 0x0D02, 0x0D12, 0x0D22, |
| 76 | 0x0D06, 0x0D16, 0x0D26, 0x0D0A, 0x0D1A, 0x0D2A, 0x0D0B, 0x0D1B, 0x0D2B, |
| 77 | 0x0D0E, 0x0D1E, 0x0D2E, 0x0F31, 0x0F32, 0x0F33, 0x0157, 0x0155 |
| 78 | }; |
| 79 | const uint16_t gen8_ids[] = { 0x22B0, 0x22B1, 0x22B2, 0x22B3, 0x1602, 0x1606, |
| 80 | 0x160A, 0x160B, 0x160D, 0x160E, 0x1612, 0x1616, |
| 81 | 0x161A, 0x161B, 0x161D, 0x161E, 0x1622, 0x1626, |
| 82 | 0x162A, 0x162B, 0x162D, 0x162E }; |
| 83 | const uint16_t gen9_ids[] = { |
| 84 | 0x1902, 0x1906, 0x190A, 0x190B, 0x190E, 0x1912, 0x1913, 0x1915, 0x1916, 0x1917, |
| 85 | 0x191A, 0x191B, 0x191D, 0x191E, 0x1921, 0x1923, 0x1926, 0x1927, 0x192A, 0x192B, |
| 86 | 0x192D, 0x1932, 0x193A, 0x193B, 0x193D, 0x0A84, 0x1A84, 0x1A85, 0x5A84, 0x5A85, |
| 87 | 0x3184, 0x3185, 0x5902, 0x5906, 0x590A, 0x5908, 0x590B, 0x590E, 0x5913, 0x5915, |
| 88 | 0x5917, 0x5912, 0x5916, 0x591A, 0x591B, 0x591D, 0x591E, 0x5921, 0x5923, 0x5926, |
| 89 | 0x5927, 0x593B, 0x591C, 0x87C0, 0x87CA, 0x3E90, 0x3E93, 0x3E99, 0x3E9C, 0x3E91, |
| 90 | 0x3E92, 0x3E96, 0x3E98, 0x3E9A, 0x3E9B, 0x3E94, 0x3EA9, 0x3EA5, 0x3EA6, 0x3EA7, |
| 91 | 0x3EA8, 0x3EA1, 0x3EA4, 0x3EA0, 0x3EA3, 0x3EA2, 0x9B21, 0x9BA0, 0x9BA2, 0x9BA4, |
| 92 | 0x9BA5, 0x9BA8, 0x9BAA, 0x9BAB, 0x9BAC, 0x9B41, 0x9BC0, 0x9BC2, 0x9BC4, 0x9BC5, |
| 93 | 0x9BC6, 0x9BC8, 0x9BCA, 0x9BCB, 0x9BCC, 0x9BE6, 0x9BF6 |
| 94 | }; |
| 95 | const uint16_t gen11_ids[] = { 0x8A50, 0x8A51, 0x8A52, 0x8A53, 0x8A54, 0x8A56, 0x8A57, |
| 96 | 0x8A58, 0x8A59, 0x8A5A, 0x8A5B, 0x8A5C, 0x8A5D, 0x8A71, |
| 97 | 0x4500, 0x4541, 0x4551, 0x4555, 0x4557, 0x4571, 0x4E51, |
| 98 | 0x4E55, 0x4E57, 0x4E61, 0x4E71 }; |
| 99 | const uint16_t gen12_ids[] = { |
| 100 | 0x4c8a, 0x4c8b, 0x4c8c, 0x4c90, 0x4c9a, 0x4680, 0x4681, 0x4682, 0x4683, 0x4688, |
| 101 | 0x4689, 0x4690, 0x4691, 0x4692, 0x4693, 0x4698, 0x4699, 0x4626, 0x4628, 0x462a, |
| 102 | 0x46a0, 0x46a1, 0x46a2, 0x46a3, 0x46a6, 0x46a8, 0x46aa, 0x46b0, 0x46b1, 0x46b2, |
| 103 | 0x46b3, 0x46c0, 0x46c1, 0x46c2, 0x46c3, 0x9A40, 0x9A49, 0x9A59, 0x9A60, 0x9A68, |
| 104 | 0x9A70, 0x9A78, 0x9AC0, 0x9AC9, 0x9AD9, 0x9AF8, 0x4905, 0x4906, 0x4907, 0x4908 |
| 105 | }; |
Drew Davenport | 862d36f | 2021-06-09 19:54:04 -0600 | [diff] [blame] | 106 | const uint16_t adlp_ids[] = { 0x46A0, 0x46A1, 0x46A2, 0x46A3, 0x46A6, 0x46A8, |
| 107 | 0x46AA, 0x462A, 0x4626, 0x4628, 0x46B0, 0x46B1, |
Sudarshan S | 69ccacd | 2022-02-18 07:31:46 +0530 | [diff] [blame] | 108 | 0x46B2, 0x46B3, 0x46C0, 0x46C1, 0x46C2, 0x46C3, |
| 109 | 0x46D0, 0x46D1, 0x46D2 }; |
Stéphane Marchesin | a39dfde | 2014-09-15 15:38:25 -0700 | [diff] [blame] | 110 | unsigned i; |
Nathan Ciobanu | 87ec79b | 2021-01-21 21:26:54 -0800 | [diff] [blame] | 111 | i915->gen = 4; |
| 112 | i915->is_adlp = false; |
| 113 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 114 | for (i = 0; i < ARRAY_SIZE(gen3_ids); i++) |
Nathan Ciobanu | 87ec79b | 2021-01-21 21:26:54 -0800 | [diff] [blame] | 115 | if (gen3_ids[i] == i915->device_id) |
| 116 | i915->gen = 3; |
| 117 | |
Nicholas Bishop | a204724 | 2021-10-29 12:42:33 -0400 | [diff] [blame] | 118 | /* Gen 4 */ |
| 119 | for (i = 0; i < ARRAY_SIZE(gen4_ids); i++) |
| 120 | if (gen4_ids[i] == i915->device_id) |
| 121 | i915->gen = 4; |
| 122 | |
| 123 | /* Gen 5 */ |
| 124 | for (i = 0; i < ARRAY_SIZE(gen5_ids); i++) |
| 125 | if (gen5_ids[i] == i915->device_id) |
| 126 | i915->gen = 5; |
| 127 | |
| 128 | /* Gen 6 */ |
| 129 | for (i = 0; i < ARRAY_SIZE(gen6_ids); i++) |
| 130 | if (gen6_ids[i] == i915->device_id) |
| 131 | i915->gen = 6; |
| 132 | |
| 133 | /* Gen 7 */ |
| 134 | for (i = 0; i < ARRAY_SIZE(gen7_ids); i++) |
| 135 | if (gen7_ids[i] == i915->device_id) |
| 136 | i915->gen = 7; |
| 137 | |
| 138 | /* Gen 8 */ |
| 139 | for (i = 0; i < ARRAY_SIZE(gen8_ids); i++) |
| 140 | if (gen8_ids[i] == i915->device_id) |
| 141 | i915->gen = 8; |
| 142 | |
| 143 | /* Gen 9 */ |
| 144 | for (i = 0; i < ARRAY_SIZE(gen9_ids); i++) |
| 145 | if (gen9_ids[i] == i915->device_id) |
| 146 | i915->gen = 9; |
| 147 | |
Binu R S | 8d70518 | 2020-07-20 10:36:53 +0530 | [diff] [blame] | 148 | /* Gen 11 */ |
| 149 | for (i = 0; i < ARRAY_SIZE(gen11_ids); i++) |
Nathan Ciobanu | 87ec79b | 2021-01-21 21:26:54 -0800 | [diff] [blame] | 150 | if (gen11_ids[i] == i915->device_id) |
| 151 | i915->gen = 11; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 152 | |
Sushma Venkatesh Reddy | 20604be | 2020-10-08 10:18:01 -0700 | [diff] [blame] | 153 | /* Gen 12 */ |
| 154 | for (i = 0; i < ARRAY_SIZE(gen12_ids); i++) |
Nathan Ciobanu | 87ec79b | 2021-01-21 21:26:54 -0800 | [diff] [blame] | 155 | if (gen12_ids[i] == i915->device_id) |
| 156 | i915->gen = 12; |
Sushma Venkatesh Reddy | 20604be | 2020-10-08 10:18:01 -0700 | [diff] [blame] | 157 | |
Nathan Ciobanu | 87ec79b | 2021-01-21 21:26:54 -0800 | [diff] [blame] | 158 | for (i = 0; i < ARRAY_SIZE(adlp_ids); i++) |
| 159 | if (adlp_ids[i] == i915->device_id) { |
| 160 | i915->is_adlp = true; |
| 161 | i915->gen = 12; |
| 162 | } |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Binu R S | 8d70518 | 2020-07-20 10:36:53 +0530 | [diff] [blame] | 165 | static void i915_get_modifier_order(struct i915_device *i915) |
| 166 | { |
Vipin Anand | a0af309 | 2020-06-17 10:53:02 +0530 | [diff] [blame] | 167 | if (i915->gen == 12) { |
| 168 | i915->modifier.order = gen12_modifier_order; |
| 169 | i915->modifier.count = ARRAY_SIZE(gen12_modifier_order); |
Cooper Chiou | c8c61d3 | 2021-10-27 12:18:30 +0800 | [diff] [blame] | 170 | } |
| 171 | else if (i915->gen == 11) { |
| 172 | i915->modifier.order = gen11_modifier_order; |
| 173 | i915->modifier.count = ARRAY_SIZE(gen11_modifier_order); |
Vipin Anand | a0af309 | 2020-06-17 10:53:02 +0530 | [diff] [blame] | 174 | } else { |
| 175 | i915->modifier.order = gen_modifier_order; |
| 176 | i915->modifier.count = ARRAY_SIZE(gen_modifier_order); |
| 177 | } |
Binu R S | 8d70518 | 2020-07-20 10:36:53 +0530 | [diff] [blame] | 178 | } |
| 179 | |
Ilja H. Friedel | f39dcbc | 2020-02-26 02:50:51 +0000 | [diff] [blame] | 180 | 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] | 181 | { |
Ilja H. Friedel | f39dcbc | 2020-02-26 02:50:51 +0000 | [diff] [blame] | 182 | uint64_t value = current_flags & ~mask; |
| 183 | return value; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static int i915_add_combinations(struct driver *drv) |
| 187 | { |
Gurchetan Singh | f98d1c1 | 2020-10-07 15:46:23 -0700 | [diff] [blame] | 188 | struct i915_device *i915 = drv->priv; |
Gurchetan Singh | 8ac0c9a | 2017-05-15 09:34:22 -0700 | [diff] [blame] | 189 | |
Miguel Casas | da47b7d | 2021-04-15 21:46:33 -0400 | [diff] [blame] | 190 | const uint64_t scanout_and_render = BO_USE_RENDER_MASK | BO_USE_SCANOUT; |
| 191 | const uint64_t render = BO_USE_RENDER_MASK; |
| 192 | const uint64_t texture_only = BO_USE_TEXTURE_MASK; |
Jeffrey Kardatzke | dba1987 | 2020-12-04 16:58:28 -0800 | [diff] [blame] | 193 | // HW protected buffers also need to be scanned out. |
Miguel Casas | da47b7d | 2021-04-15 21:46:33 -0400 | [diff] [blame] | 194 | const uint64_t hw_protected = |
| 195 | i915->has_hw_protection ? (BO_USE_PROTECTED | BO_USE_SCANOUT) : 0; |
Gurchetan Singh | f98d1c1 | 2020-10-07 15:46:23 -0700 | [diff] [blame] | 196 | |
Miguel Casas | abeadde | 2021-04-16 14:49:18 -0400 | [diff] [blame] | 197 | const uint64_t linear_mask = BO_USE_RENDERSCRIPT | BO_USE_LINEAR | BO_USE_SW_READ_OFTEN | |
| 198 | BO_USE_SW_WRITE_OFTEN | BO_USE_SW_READ_RARELY | |
| 199 | BO_USE_SW_WRITE_RARELY; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 200 | |
Gurchetan Singh | 45ca449 | 2021-04-28 17:12:52 -0700 | [diff] [blame] | 201 | struct format_metadata metadata_linear = { .tiling = I915_TILING_NONE, |
| 202 | .priority = 1, |
| 203 | .modifier = DRM_FORMAT_MOD_LINEAR }; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 204 | |
Ilja H. Friedel | f39dcbc | 2020-02-26 02:50:51 +0000 | [diff] [blame] | 205 | drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats), |
Miguel Casas | f2dc08e | 2021-04-15 20:51:24 -0400 | [diff] [blame] | 206 | &metadata_linear, scanout_and_render); |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 207 | |
Gurchetan Singh | 45ca449 | 2021-04-28 17:12:52 -0700 | [diff] [blame] | 208 | drv_add_combinations(drv, render_formats, ARRAY_SIZE(render_formats), &metadata_linear, |
| 209 | render); |
Gurchetan Singh | 8ac0c9a | 2017-05-15 09:34:22 -0700 | [diff] [blame] | 210 | |
Gurchetan Singh | 45ca449 | 2021-04-28 17:12:52 -0700 | [diff] [blame] | 211 | drv_add_combinations(drv, texture_only_formats, ARRAY_SIZE(texture_only_formats), |
| 212 | &metadata_linear, texture_only); |
Ilja H. Friedel | f39dcbc | 2020-02-26 02:50:51 +0000 | [diff] [blame] | 213 | |
| 214 | drv_modify_linear_combinations(drv); |
Hirokazu Honda | fd8b8ab | 2020-06-16 15:28:56 +0900 | [diff] [blame] | 215 | |
Hirokazu Honda | 3bd681c | 2020-06-23 17:52:20 +0900 | [diff] [blame] | 216 | /* NV12 format for camera, display, decoding and encoding. */ |
Ilja H. Friedel | f39dcbc | 2020-02-26 02:50:51 +0000 | [diff] [blame] | 217 | /* IPU3 camera ISP supports only NV12 output. */ |
Miguel Casas | f2dc08e | 2021-04-15 20:51:24 -0400 | [diff] [blame] | 218 | drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata_linear, |
Hirokazu Honda | 3bd681c | 2020-06-23 17:52:20 +0900 | [diff] [blame] | 219 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SCANOUT | |
Gurchetan Singh | f98d1c1 | 2020-10-07 15:46:23 -0700 | [diff] [blame] | 220 | BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER | |
| 221 | hw_protected); |
Hirokazu Honda | 3b8d4d0 | 2019-07-31 16:35:52 +0900 | [diff] [blame] | 222 | |
Gurchetan Singh | 71bc665 | 2018-09-17 17:42:05 -0700 | [diff] [blame] | 223 | /* Android CTS tests require this. */ |
Miguel Casas | f2dc08e | 2021-04-15 20:51:24 -0400 | [diff] [blame] | 224 | 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] | 225 | |
Tomasz Figa | d30c0a5 | 2017-07-05 17:50:18 +0900 | [diff] [blame] | 226 | /* |
| 227 | * 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] | 228 | * from camera and input/output from hardware decoder/encoder. |
Tomasz Figa | d30c0a5 | 2017-07-05 17:50:18 +0900 | [diff] [blame] | 229 | */ |
Miguel Casas | f2dc08e | 2021-04-15 20:51:24 -0400 | [diff] [blame] | 230 | drv_modify_combination(drv, DRM_FORMAT_R8, &metadata_linear, |
David Stevens | 4951814 | 2020-06-15 13:48:48 +0900 | [diff] [blame] | 231 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER | |
| 232 | BO_USE_HW_VIDEO_ENCODER); |
Tomasz Figa | d30c0a5 | 2017-07-05 17:50:18 +0900 | [diff] [blame] | 233 | |
Miguel Casas | da47b7d | 2021-04-15 21:46:33 -0400 | [diff] [blame] | 234 | const uint64_t render_not_linear = unset_flags(render, linear_mask); |
Miguel Casas | b5a95bb | 2021-04-16 14:52:59 -0400 | [diff] [blame] | 235 | const uint64_t scanout_and_render_not_linear = render_not_linear | BO_USE_SCANOUT; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 236 | |
Gurchetan Singh | 45ca449 | 2021-04-28 17:12:52 -0700 | [diff] [blame] | 237 | struct format_metadata metadata_x_tiled = { .tiling = I915_TILING_X, |
| 238 | .priority = 2, |
| 239 | .modifier = I915_FORMAT_MOD_X_TILED }; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 240 | |
Gurchetan Singh | 45ca449 | 2021-04-28 17:12:52 -0700 | [diff] [blame] | 241 | drv_add_combinations(drv, render_formats, ARRAY_SIZE(render_formats), &metadata_x_tiled, |
| 242 | render_not_linear); |
Ilja H. Friedel | f39dcbc | 2020-02-26 02:50:51 +0000 | [diff] [blame] | 243 | drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats), |
Miguel Casas | da47b7d | 2021-04-15 21:46:33 -0400 | [diff] [blame] | 244 | &metadata_x_tiled, scanout_and_render_not_linear); |
Gurchetan Singh | 8ac0c9a | 2017-05-15 09:34:22 -0700 | [diff] [blame] | 245 | |
Gurchetan Singh | 45ca449 | 2021-04-28 17:12:52 -0700 | [diff] [blame] | 246 | struct format_metadata metadata_y_tiled = { .tiling = I915_TILING_Y, |
| 247 | .priority = 3, |
| 248 | .modifier = I915_FORMAT_MOD_Y_TILED }; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 249 | |
Ilja H. Friedel | f39dcbc | 2020-02-26 02:50:51 +0000 | [diff] [blame] | 250 | /* Support y-tiled NV12 and P010 for libva */ |
| 251 | #ifdef I915_SCANOUT_Y_TILED |
Miguel Casas | 231913e | 2021-04-06 19:17:25 -0400 | [diff] [blame] | 252 | const uint64_t nv12_usage = |
Jeffrey Kardatzke | dba1987 | 2020-12-04 16:58:28 -0800 | [diff] [blame] | 253 | 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] | 254 | const uint64_t p010_usage = BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER | hw_protected | |
| 255 | (i915->gen >= 11 ? BO_USE_SCANOUT : 0); |
Ilja H. Friedel | f39dcbc | 2020-02-26 02:50:51 +0000 | [diff] [blame] | 256 | #else |
Miguel Casas | 231913e | 2021-04-06 19:17:25 -0400 | [diff] [blame] | 257 | const uint64_t nv12_usage = BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER; |
| 258 | const uint64_t p010_usage = nv12_usage; |
Ilja H. Friedel | f39dcbc | 2020-02-26 02:50:51 +0000 | [diff] [blame] | 259 | #endif |
Miguel Casas | f2dc08e | 2021-04-15 20:51:24 -0400 | [diff] [blame] | 260 | drv_add_combination(drv, DRM_FORMAT_NV12, &metadata_y_tiled, nv12_usage); |
| 261 | drv_add_combination(drv, DRM_FORMAT_P010, &metadata_y_tiled, p010_usage); |
Jeffrey Kardatzke | dba1987 | 2020-12-04 16:58:28 -0800 | [diff] [blame] | 262 | |
Gurchetan Singh | 45ca449 | 2021-04-28 17:12:52 -0700 | [diff] [blame] | 263 | drv_add_combinations(drv, render_formats, ARRAY_SIZE(render_formats), &metadata_y_tiled, |
| 264 | render_not_linear); |
Miguel Casas | b5a95bb | 2021-04-16 14:52:59 -0400 | [diff] [blame] | 265 | |
| 266 | // Y-tiled scanout isn't available on old platforms so we add |
| 267 | // |scanout_render_formats| without that USE flag. |
Ilja H. Friedel | f39dcbc | 2020-02-26 02:50:51 +0000 | [diff] [blame] | 268 | drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats), |
Miguel Casas | b5a95bb | 2021-04-16 14:52:59 -0400 | [diff] [blame] | 269 | &metadata_y_tiled, render_not_linear); |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 270 | return 0; |
| 271 | } |
| 272 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 273 | static int i915_align_dimensions(struct bo *bo, uint32_t tiling, uint32_t *stride, |
| 274 | uint32_t *aligned_height) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 275 | { |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 276 | struct i915_device *i915 = bo->drv->priv; |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 277 | uint32_t horizontal_alignment; |
| 278 | uint32_t vertical_alignment; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 279 | |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 280 | switch (tiling) { |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 281 | default: |
| 282 | case I915_TILING_NONE: |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 283 | /* |
| 284 | * The Intel GPU doesn't need any alignment in linear mode, |
| 285 | * but libva requires the allocation stride to be aligned to |
| 286 | * 16 bytes and height to 4 rows. Further, we round up the |
| 287 | * horizontal alignment so that row start on a cache line (64 |
| 288 | * bytes). |
| 289 | */ |
Dominik Behr | 1c6e70a | 2020-11-05 18:58:06 -0800 | [diff] [blame] | 290 | #ifdef LINEAR_ALIGN_256 |
| 291 | /* |
| 292 | * If we want to import these buffers to amdgpu they need to |
| 293 | * their match LINEAR_ALIGNED requirement of 256 byte alignement. |
| 294 | */ |
| 295 | horizontal_alignment = 256; |
| 296 | #else |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 297 | horizontal_alignment = 64; |
Dominik Behr | 1c6e70a | 2020-11-05 18:58:06 -0800 | [diff] [blame] | 298 | #endif |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 299 | vertical_alignment = 4; |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 300 | break; |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 301 | |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 302 | case I915_TILING_X: |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 303 | horizontal_alignment = 512; |
| 304 | vertical_alignment = 8; |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 305 | break; |
| 306 | |
| 307 | case I915_TILING_Y: |
Yiwei Zhang | 17fa1a8 | 2022-02-04 00:39:06 +0000 | [diff] [blame] | 308 | if (i915->gen == 3) { |
Chad Versace | 0f9bd72 | 2022-01-20 11:36:02 -0800 | [diff] [blame] | 309 | horizontal_alignment = 512; |
| 310 | vertical_alignment = 8; |
Yiwei Zhang | 17fa1a8 | 2022-02-04 00:39:06 +0000 | [diff] [blame] | 311 | } else { |
| 312 | horizontal_alignment = 128; |
| 313 | vertical_alignment = 32; |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 314 | } |
| 315 | break; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 316 | } |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 317 | |
David Stevens | 793675a | 2019-09-25 11:17:48 +0900 | [diff] [blame] | 318 | *aligned_height = ALIGN(*aligned_height, vertical_alignment); |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 319 | if (i915->gen > 3) { |
| 320 | *stride = ALIGN(*stride, horizontal_alignment); |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 321 | } else { |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 322 | while (*stride > horizontal_alignment) |
| 323 | horizontal_alignment <<= 1; |
| 324 | |
| 325 | *stride = horizontal_alignment; |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 326 | } |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 327 | |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 328 | if (i915->gen <= 3 && *stride > 8192) |
| 329 | return -EINVAL; |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 330 | |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 331 | return 0; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Gurchetan Singh | 68af9c2 | 2017-01-18 13:48:11 -0800 | [diff] [blame] | 334 | static void i915_clflush(void *start, size_t size) |
| 335 | { |
| 336 | void *p = (void *)(((uintptr_t)start) & ~I915_CACHELINE_MASK); |
| 337 | void *end = (void *)((uintptr_t)start + size); |
| 338 | |
| 339 | __builtin_ia32_mfence(); |
| 340 | while (p < end) { |
| 341 | __builtin_ia32_clflush(p); |
| 342 | p = (void *)((uintptr_t)p + I915_CACHELINE_SIZE); |
| 343 | } |
| 344 | } |
| 345 | |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 346 | static int i915_init(struct driver *drv) |
| 347 | { |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 348 | int ret; |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 349 | struct i915_device *i915; |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 350 | drm_i915_getparam_t get_param = { 0 }; |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 351 | |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 352 | i915 = calloc(1, sizeof(*i915)); |
| 353 | if (!i915) |
| 354 | return -ENOMEM; |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 355 | |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 356 | get_param.param = I915_PARAM_CHIPSET_ID; |
Nathan Ciobanu | 87ec79b | 2021-01-21 21:26:54 -0800 | [diff] [blame] | 357 | get_param.value = &(i915->device_id); |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 358 | ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param); |
| 359 | if (ret) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 360 | drv_log("Failed to get I915_PARAM_CHIPSET_ID\n"); |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 361 | free(i915); |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 362 | return -EINVAL; |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 363 | } |
Nathan Ciobanu | 87ec79b | 2021-01-21 21:26:54 -0800 | [diff] [blame] | 364 | /* must call before i915->gen is used anywhere else */ |
| 365 | i915_info_from_device_id(i915); |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 366 | |
Binu R S | 8d70518 | 2020-07-20 10:36:53 +0530 | [diff] [blame] | 367 | i915_get_modifier_order(i915); |
Gurchetan Singh | 68af9c2 | 2017-01-18 13:48:11 -0800 | [diff] [blame] | 368 | |
| 369 | memset(&get_param, 0, sizeof(get_param)); |
| 370 | get_param.param = I915_PARAM_HAS_LLC; |
| 371 | get_param.value = &i915->has_llc; |
| 372 | ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param); |
| 373 | if (ret) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 374 | drv_log("Failed to get I915_PARAM_HAS_LLC\n"); |
Gurchetan Singh | 68af9c2 | 2017-01-18 13:48:11 -0800 | [diff] [blame] | 375 | free(i915); |
| 376 | return -EINVAL; |
| 377 | } |
| 378 | |
Gurchetan Singh | f98d1c1 | 2020-10-07 15:46:23 -0700 | [diff] [blame] | 379 | if (i915->gen >= 12) |
| 380 | i915->has_hw_protection = 1; |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 381 | |
Gurchetan Singh | f98d1c1 | 2020-10-07 15:46:23 -0700 | [diff] [blame] | 382 | drv->priv = i915; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 383 | return i915_add_combinations(drv); |
Gurchetan Singh | 3eb8d8f | 2017-01-03 13:36:13 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Sushma Venkatesh Reddy | 0b57ebf | 2021-03-22 17:10:05 -0700 | [diff] [blame] | 386 | /* |
| 387 | * Returns true if the height of a buffer of the given format should be aligned |
| 388 | * to the largest coded unit (LCU) assuming that it will be used for video. This |
| 389 | * is based on gmmlib's GmmIsYUVFormatLCUAligned(). |
| 390 | */ |
Drew Davenport | 862d36f | 2021-06-09 19:54:04 -0600 | [diff] [blame] | 391 | static bool i915_format_needs_LCU_alignment(uint32_t format, size_t plane, |
| 392 | const struct i915_device *i915) |
Sushma Venkatesh Reddy | 0b57ebf | 2021-03-22 17:10:05 -0700 | [diff] [blame] | 393 | { |
| 394 | switch (format) { |
| 395 | case DRM_FORMAT_NV12: |
| 396 | case DRM_FORMAT_P010: |
| 397 | case DRM_FORMAT_P016: |
| 398 | return (i915->gen == 11 || i915->gen == 12) && plane == 1; |
| 399 | } |
| 400 | return false; |
| 401 | } |
| 402 | |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 403 | static int i915_bo_from_format(struct bo *bo, uint32_t width, uint32_t height, uint32_t format) |
| 404 | { |
| 405 | uint32_t offset; |
| 406 | size_t plane; |
Gurchetan Singh | cc35e69 | 2019-02-28 15:44:54 -0800 | [diff] [blame] | 407 | int ret, pagesize; |
Sushma Venkatesh Reddy | 0b57ebf | 2021-03-22 17:10:05 -0700 | [diff] [blame] | 408 | struct i915_device *i915 = bo->drv->priv; |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 409 | |
| 410 | offset = 0; |
Gurchetan Singh | cc35e69 | 2019-02-28 15:44:54 -0800 | [diff] [blame] | 411 | pagesize = getpagesize(); |
Sushma Venkatesh Reddy | 0b57ebf | 2021-03-22 17:10:05 -0700 | [diff] [blame] | 412 | |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 413 | for (plane = 0; plane < drv_num_planes_from_format(format); plane++) { |
| 414 | uint32_t stride = drv_stride_from_format(format, width, plane); |
| 415 | uint32_t plane_height = drv_height_from_format(format, height, plane); |
| 416 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 417 | if (bo->meta.tiling != I915_TILING_NONE) |
Gurchetan Singh | cc35e69 | 2019-02-28 15:44:54 -0800 | [diff] [blame] | 418 | assert(IS_ALIGNED(offset, pagesize)); |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 419 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 420 | ret = i915_align_dimensions(bo, bo->meta.tiling, &stride, &plane_height); |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 421 | if (ret) |
| 422 | return ret; |
| 423 | |
Sushma Venkatesh Reddy | 0b57ebf | 2021-03-22 17:10:05 -0700 | [diff] [blame] | 424 | if (i915_format_needs_LCU_alignment(format, plane, i915)) { |
| 425 | /* |
| 426 | * Align the height of the V plane for certain formats to the |
| 427 | * largest coded unit (assuming that this BO may be used for video) |
| 428 | * to be consistent with gmmlib. |
| 429 | */ |
| 430 | plane_height = ALIGN(plane_height, 64); |
| 431 | } |
| 432 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 433 | bo->meta.strides[plane] = stride; |
| 434 | bo->meta.sizes[plane] = stride * plane_height; |
| 435 | bo->meta.offsets[plane] = offset; |
| 436 | offset += bo->meta.sizes[plane]; |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 439 | bo->meta.total_size = ALIGN(offset, pagesize); |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | |
Robert Mader | 96058f9 | 2022-04-05 12:15:11 +0200 | [diff] [blame] | 444 | static size_t i915_num_planes_from_modifier(struct driver *drv, uint32_t format, |
| 445 | uint64_t modifier) |
| 446 | { |
| 447 | size_t num_planes = drv_num_planes_from_format(format); |
| 448 | if (modifier == I915_FORMAT_MOD_Y_TILED_CCS || |
| 449 | modifier == I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS) { |
| 450 | assert(num_planes == 1); |
| 451 | return 2; |
| 452 | } |
| 453 | |
| 454 | return num_planes; |
| 455 | } |
| 456 | |
David Stevens | 26fe682 | 2020-03-09 12:23:42 +0000 | [diff] [blame] | 457 | static int i915_bo_compute_metadata(struct bo *bo, uint32_t width, uint32_t height, uint32_t format, |
| 458 | uint64_t use_flags, const uint64_t *modifiers, uint32_t count) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 459 | { |
David Stevens | 26fe682 | 2020-03-09 12:23:42 +0000 | [diff] [blame] | 460 | uint64_t modifier; |
Sean Paul | a9d3f77 | 2020-05-19 10:17:07 -0400 | [diff] [blame] | 461 | struct i915_device *i915 = bo->drv->priv; |
Abhishek Kumar | d39fe4e | 2020-10-09 16:08:01 +0530 | [diff] [blame] | 462 | bool huge_bo = (i915->gen < 11) && (width > 4096); |
David Stevens | 26fe682 | 2020-03-09 12:23:42 +0000 | [diff] [blame] | 463 | |
| 464 | if (modifiers) { |
| 465 | modifier = |
Binu R S | 8d70518 | 2020-07-20 10:36:53 +0530 | [diff] [blame] | 466 | drv_pick_modifier(modifiers, count, i915->modifier.order, i915->modifier.count); |
David Stevens | 26fe682 | 2020-03-09 12:23:42 +0000 | [diff] [blame] | 467 | } else { |
| 468 | struct combination *combo = drv_get_combination(bo->drv, format, use_flags); |
| 469 | if (!combo) |
| 470 | return -EINVAL; |
| 471 | modifier = combo->metadata.modifier; |
| 472 | } |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 473 | |
Sean Paul | a9d3f77 | 2020-05-19 10:17:07 -0400 | [diff] [blame] | 474 | /* |
Abhishek Kumar | 6085bf3 | 2020-10-12 16:24:03 +0530 | [diff] [blame] | 475 | * i915 only supports linear/x-tiled above 4096 wide on Gen9/Gen10 GPU. |
| 476 | * 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] | 477 | */ |
Abhishek Kumar | 6085bf3 | 2020-10-12 16:24:03 +0530 | [diff] [blame] | 478 | if (huge_bo && format != DRM_FORMAT_NV12 && format != DRM_FORMAT_P010 && |
| 479 | modifier != I915_FORMAT_MOD_X_TILED && modifier != DRM_FORMAT_MOD_LINEAR) { |
Sean Paul | a9d3f77 | 2020-05-19 10:17:07 -0400 | [diff] [blame] | 480 | uint32_t i; |
| 481 | for (i = 0; modifiers && i < count; i++) { |
| 482 | if (modifiers[i] == I915_FORMAT_MOD_X_TILED) |
| 483 | break; |
| 484 | } |
| 485 | if (i == count) |
| 486 | modifier = DRM_FORMAT_MOD_LINEAR; |
| 487 | else |
| 488 | modifier = I915_FORMAT_MOD_X_TILED; |
| 489 | } |
| 490 | |
Pilar Molina Lopez | 28cf2f1 | 2020-11-12 18:19:42 -0500 | [diff] [blame] | 491 | /* |
| 492 | * Skip I915_FORMAT_MOD_Y_TILED_CCS modifier if compression is disabled |
| 493 | * Pick y tiled modifier if it has been passed in, otherwise use linear |
| 494 | */ |
| 495 | if (!bo->drv->compression && modifier == I915_FORMAT_MOD_Y_TILED_CCS) { |
| 496 | uint32_t i; |
| 497 | for (i = 0; modifiers && i < count; i++) { |
| 498 | if (modifiers[i] == I915_FORMAT_MOD_Y_TILED) |
| 499 | break; |
| 500 | } |
| 501 | if (i == count) |
| 502 | modifier = DRM_FORMAT_MOD_LINEAR; |
| 503 | else |
| 504 | modifier = I915_FORMAT_MOD_Y_TILED; |
| 505 | } |
| 506 | |
Nicholas Bishop | a204724 | 2021-10-29 12:42:33 -0400 | [diff] [blame] | 507 | /* Prevent gen 8 and earlier from trying to use a tiling modifier */ |
| 508 | if (i915->gen <= 8 && format == DRM_FORMAT_ARGB8888) { |
| 509 | modifier = DRM_FORMAT_MOD_LINEAR; |
| 510 | } |
| 511 | |
Kristian H. Kristensen | 6061eab | 2017-10-03 13:53:19 -0700 | [diff] [blame] | 512 | switch (modifier) { |
| 513 | case DRM_FORMAT_MOD_LINEAR: |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 514 | bo->meta.tiling = I915_TILING_NONE; |
Kristian H. Kristensen | 6061eab | 2017-10-03 13:53:19 -0700 | [diff] [blame] | 515 | break; |
| 516 | case I915_FORMAT_MOD_X_TILED: |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 517 | bo->meta.tiling = I915_TILING_X; |
Kristian H. Kristensen | 6061eab | 2017-10-03 13:53:19 -0700 | [diff] [blame] | 518 | break; |
| 519 | case I915_FORMAT_MOD_Y_TILED: |
Mark Yacoub | c956564 | 2020-02-07 11:02:22 -0500 | [diff] [blame] | 520 | case I915_FORMAT_MOD_Y_TILED_CCS: |
Vipin Anand | a0af309 | 2020-06-17 10:53:02 +0530 | [diff] [blame] | 521 | /* For now support only I915_TILING_Y as this works with all |
| 522 | * IPs(render/media/display) |
| 523 | */ |
| 524 | case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS: |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 525 | bo->meta.tiling = I915_TILING_Y; |
Kristian H. Kristensen | 6061eab | 2017-10-03 13:53:19 -0700 | [diff] [blame] | 526 | break; |
| 527 | } |
Owen Lin | bbb69fd | 2017-06-05 14:33:08 +0800 | [diff] [blame] | 528 | |
Gurchetan Singh | 52155b4 | 2021-01-27 17:55:17 -0800 | [diff] [blame] | 529 | bo->meta.format_modifier = modifier; |
Kristian H. Kristensen | 2b8f89e | 2018-02-07 16:10:06 -0800 | [diff] [blame] | 530 | |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 531 | if (format == DRM_FORMAT_YVU420_ANDROID) { |
| 532 | /* |
| 533 | * We only need to be able to use this as a linear texture, |
| 534 | * which doesn't put any HW restrictions on how we lay it |
| 535 | * out. The Android format does require the stride to be a |
| 536 | * multiple of 16 and expects the Cr and Cb stride to be |
| 537 | * ALIGN(Y_stride / 2, 16), which we can make happen by |
| 538 | * aligning to 32 bytes here. |
| 539 | */ |
| 540 | uint32_t stride = ALIGN(width, 32); |
| 541 | drv_bo_from_format(bo, stride, height, format); |
Mark Yacoub | c956564 | 2020-02-07 11:02:22 -0500 | [diff] [blame] | 542 | } else if (modifier == I915_FORMAT_MOD_Y_TILED_CCS) { |
| 543 | /* |
| 544 | * For compressed surfaces, we need a color control surface |
| 545 | * (CCS). Color compression is only supported for Y tiled |
| 546 | * surfaces, and for each 32x16 tiles in the main surface we |
| 547 | * need a tile in the control surface. Y tiles are 128 bytes |
| 548 | * wide and 32 lines tall and we use that to first compute the |
| 549 | * width and height in tiles of the main surface. stride and |
| 550 | * height are already multiples of 128 and 32, respectively: |
| 551 | */ |
| 552 | uint32_t stride = drv_stride_from_format(format, width, 0); |
| 553 | uint32_t width_in_tiles = DIV_ROUND_UP(stride, 128); |
| 554 | uint32_t height_in_tiles = DIV_ROUND_UP(height, 32); |
| 555 | uint32_t size = width_in_tiles * height_in_tiles * 4096; |
| 556 | uint32_t offset = 0; |
| 557 | |
| 558 | bo->meta.strides[0] = width_in_tiles * 128; |
| 559 | bo->meta.sizes[0] = size; |
| 560 | bo->meta.offsets[0] = offset; |
| 561 | offset += size; |
| 562 | |
| 563 | /* |
| 564 | * Now, compute the width and height in tiles of the control |
| 565 | * surface by dividing and rounding up. |
| 566 | */ |
| 567 | uint32_t ccs_width_in_tiles = DIV_ROUND_UP(width_in_tiles, 32); |
| 568 | uint32_t ccs_height_in_tiles = DIV_ROUND_UP(height_in_tiles, 16); |
| 569 | uint32_t ccs_size = ccs_width_in_tiles * ccs_height_in_tiles * 4096; |
| 570 | |
| 571 | /* |
| 572 | * With stride and height aligned to y tiles, offset is |
| 573 | * already a multiple of 4096, which is the required alignment |
| 574 | * of the CCS. |
| 575 | */ |
| 576 | bo->meta.strides[1] = ccs_width_in_tiles * 128; |
| 577 | bo->meta.sizes[1] = ccs_size; |
| 578 | bo->meta.offsets[1] = offset; |
| 579 | offset += ccs_size; |
| 580 | |
Robert Mader | 96058f9 | 2022-04-05 12:15:11 +0200 | [diff] [blame] | 581 | bo->meta.num_planes = i915_num_planes_from_modifier(bo->drv, format, modifier); |
Mark Yacoub | c956564 | 2020-02-07 11:02:22 -0500 | [diff] [blame] | 582 | bo->meta.total_size = offset; |
Vipin Anand | a0af309 | 2020-06-17 10:53:02 +0530 | [diff] [blame] | 583 | } else if (modifier == I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS) { |
| 584 | |
| 585 | /* |
| 586 | * considering only 128 byte compression and one cache line of |
| 587 | * aux buffer(64B) contains compression status of 4-Y tiles. |
| 588 | * Which is 4 * (128B * 32L). |
| 589 | * line stride(bytes) is 4 * 128B |
| 590 | * and tile stride(lines) is 32L |
| 591 | */ |
| 592 | uint32_t stride = ALIGN(drv_stride_from_format(format, width, 0), 512); |
| 593 | |
| 594 | height = ALIGN(drv_height_from_format(format, height, 0), 32); |
| 595 | |
| 596 | if (i915->is_adlp && (stride > 1)) { |
| 597 | stride = 1 << (32 - __builtin_clz(stride - 1)); |
| 598 | height = ALIGN(drv_height_from_format(format, height, 0), 128); |
| 599 | } |
| 600 | |
| 601 | bo->meta.strides[0] = stride; |
| 602 | /* size calculation and alignment are 64KB aligned |
| 603 | * size as per spec |
| 604 | */ |
| 605 | bo->meta.sizes[0] = ALIGN(stride * height, 65536); |
| 606 | bo->meta.offsets[0] = 0; |
| 607 | |
| 608 | /* Aux buffer is linear and page aligned. It is placed after |
| 609 | * other planes and aligned to main buffer stride. |
| 610 | */ |
| 611 | bo->meta.strides[1] = bo->meta.strides[0] / 8; |
| 612 | /* Aligned to page size */ |
| 613 | bo->meta.sizes[1] = ALIGN(bo->meta.sizes[0] / 256, getpagesize()); |
| 614 | bo->meta.offsets[1] = bo->meta.sizes[0]; |
| 615 | /* Total number of planes & sizes */ |
Robert Mader | 96058f9 | 2022-04-05 12:15:11 +0200 | [diff] [blame] | 616 | bo->meta.num_planes = i915_num_planes_from_modifier(bo->drv, format, modifier); |
Vipin Anand | a0af309 | 2020-06-17 10:53:02 +0530 | [diff] [blame] | 617 | bo->meta.total_size = bo->meta.sizes[0] + bo->meta.sizes[1]; |
Kristian H. Kristensen | e8778f0 | 2018-04-04 14:21:41 -0700 | [diff] [blame] | 618 | } else { |
| 619 | i915_bo_from_format(bo, width, height, format); |
| 620 | } |
David Stevens | 26fe682 | 2020-03-09 12:23:42 +0000 | [diff] [blame] | 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | static int i915_bo_create_from_metadata(struct bo *bo) |
| 625 | { |
| 626 | int ret; |
| 627 | size_t plane; |
Gurchetan Singh | f98d1c1 | 2020-10-07 15:46:23 -0700 | [diff] [blame] | 628 | uint32_t gem_handle; |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 629 | struct drm_i915_gem_set_tiling gem_set_tiling = { 0 }; |
Gurchetan Singh | f98d1c1 | 2020-10-07 15:46:23 -0700 | [diff] [blame] | 630 | struct i915_device *i915 = bo->drv->priv; |
Stéphane Marchesin | 5d867a4 | 2014-11-24 17:09:49 -0800 | [diff] [blame] | 631 | |
Gurchetan Singh | f98d1c1 | 2020-10-07 15:46:23 -0700 | [diff] [blame] | 632 | if (i915->has_hw_protection && (bo->meta.use_flags & BO_USE_PROTECTED)) { |
Juston Li | ef852e0 | 2021-08-02 11:02:45 -0700 | [diff] [blame] | 633 | struct drm_i915_gem_create_ext_protected_content protected_content = { |
| 634 | .base = { .name = I915_GEM_CREATE_EXT_PROTECTED_CONTENT }, |
| 635 | .flags = 0, |
Gurchetan Singh | f98d1c1 | 2020-10-07 15:46:23 -0700 | [diff] [blame] | 636 | }; |
| 637 | |
| 638 | struct drm_i915_gem_create_ext create_ext = { |
| 639 | .size = bo->meta.total_size, |
Juston Li | ef852e0 | 2021-08-02 11:02:45 -0700 | [diff] [blame] | 640 | .extensions = (uintptr_t)&protected_content, |
Gurchetan Singh | f98d1c1 | 2020-10-07 15:46:23 -0700 | [diff] [blame] | 641 | }; |
| 642 | |
| 643 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE_EXT, &create_ext); |
| 644 | if (ret) { |
Juston Li | ef852e0 | 2021-08-02 11:02:45 -0700 | [diff] [blame] | 645 | drv_log("DRM_IOCTL_I915_GEM_CREATE_EXT failed (size=%llu) (ret=%d) \n", |
| 646 | create_ext.size, ret); |
Gurchetan Singh | f98d1c1 | 2020-10-07 15:46:23 -0700 | [diff] [blame] | 647 | return -errno; |
| 648 | } |
| 649 | |
| 650 | gem_handle = create_ext.handle; |
| 651 | } else { |
| 652 | struct drm_i915_gem_create gem_create = { 0 }; |
| 653 | gem_create.size = bo->meta.total_size; |
| 654 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create); |
| 655 | if (ret) { |
| 656 | drv_log("DRM_IOCTL_I915_GEM_CREATE failed (size=%llu)\n", gem_create.size); |
| 657 | return -errno; |
| 658 | } |
| 659 | |
| 660 | gem_handle = gem_create.handle; |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 661 | } |
Gurchetan Singh | 83dc4fb | 2016-07-19 15:52:33 -0700 | [diff] [blame] | 662 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 663 | for (plane = 0; plane < bo->meta.num_planes; plane++) |
Gurchetan Singh | f98d1c1 | 2020-10-07 15:46:23 -0700 | [diff] [blame] | 664 | bo->handles[plane].u32 = gem_handle; |
Daniel Nicoara | 1de26dc | 2014-09-25 18:53:19 -0400 | [diff] [blame] | 665 | |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 666 | gem_set_tiling.handle = bo->handles[0].u32; |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 667 | gem_set_tiling.tiling_mode = bo->meta.tiling; |
| 668 | gem_set_tiling.stride = bo->meta.strides[0]; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 669 | |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 670 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_TILING, &gem_set_tiling); |
| 671 | if (ret) { |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 672 | struct drm_gem_close gem_close = { 0 }; |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 673 | gem_close.handle = bo->handles[0].u32; |
| 674 | drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close); |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 675 | |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 676 | 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] | 677 | return -errno; |
| 678 | } |
| 679 | |
| 680 | return 0; |
| 681 | } |
| 682 | |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 683 | static void i915_close(struct driver *drv) |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 684 | { |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 685 | free(drv->priv); |
| 686 | drv->priv = NULL; |
Gurchetan Singh | 82a8eed | 2017-01-03 13:01:37 -0800 | [diff] [blame] | 687 | } |
| 688 | |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 689 | static int i915_bo_import(struct bo *bo, struct drv_import_fd_data *data) |
| 690 | { |
| 691 | int ret; |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 692 | struct drm_i915_gem_get_tiling gem_get_tiling = { 0 }; |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 693 | |
Robert Mader | 96058f9 | 2022-04-05 12:15:11 +0200 | [diff] [blame] | 694 | bo->meta.num_planes = i915_num_planes_from_modifier(bo->drv, data->format, |
| 695 | data->format_modifier); |
| 696 | |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 697 | ret = drv_prime_bo_import(bo, data); |
| 698 | if (ret) |
| 699 | return ret; |
| 700 | |
| 701 | /* TODO(gsingh): export modifiers and get rid of backdoor tiling. */ |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 702 | gem_get_tiling.handle = bo->handles[0].u32; |
| 703 | |
| 704 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_GET_TILING, &gem_get_tiling); |
| 705 | if (ret) { |
Joe Kniss | 9e5d12a | 2017-06-29 11:54:22 -0700 | [diff] [blame] | 706 | drv_gem_bo_destroy(bo); |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 707 | drv_log("DRM_IOCTL_I915_GEM_GET_TILING failed.\n"); |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 708 | return ret; |
| 709 | } |
| 710 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 711 | bo->meta.tiling = gem_get_tiling.tiling_mode; |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 712 | return 0; |
| 713 | } |
| 714 | |
Gurchetan Singh | ee43c30 | 2017-11-14 18:20:27 -0800 | [diff] [blame] | 715 | 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] | 716 | { |
| 717 | int ret; |
Kristian H. Kristensen | 8e9c241 | 2020-11-19 19:20:04 +0000 | [diff] [blame] | 718 | void *addr = MAP_FAILED; |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 719 | |
Gurchetan Singh | 52155b4 | 2021-01-27 17:55:17 -0800 | [diff] [blame] | 720 | if (bo->meta.format_modifier == I915_FORMAT_MOD_Y_TILED_CCS) |
Mark Yacoub | c956564 | 2020-02-07 11:02:22 -0500 | [diff] [blame] | 721 | return MAP_FAILED; |
| 722 | |
Vipin Anand | a0af309 | 2020-06-17 10:53:02 +0530 | [diff] [blame] | 723 | if (bo->meta.format_modifier == I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS) |
| 724 | return MAP_FAILED; |
| 725 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 726 | if (bo->meta.tiling == I915_TILING_NONE) { |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 727 | struct drm_i915_gem_mmap gem_map = { 0 }; |
Tomasz Figa | 39eb951 | 2018-11-01 00:45:31 +0900 | [diff] [blame] | 728 | /* TODO(b/118799155): We don't seem to have a good way to |
| 729 | * detect the use cases for which WC mapping is really needed. |
| 730 | * The current heuristic seems overly coarse and may be slowing |
| 731 | * down some other use cases unnecessarily. |
| 732 | * |
| 733 | * For now, care must be taken not to use WC mappings for |
| 734 | * Renderscript and camera use cases, as they're |
| 735 | * performance-sensitive. */ |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 736 | if ((bo->meta.use_flags & BO_USE_SCANOUT) && |
| 737 | !(bo->meta.use_flags & |
Tomasz Figa | 39eb951 | 2018-11-01 00:45:31 +0900 | [diff] [blame] | 738 | (BO_USE_RENDERSCRIPT | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))) |
Gurchetan Singh | 5af2023 | 2017-09-19 15:10:58 -0700 | [diff] [blame] | 739 | gem_map.flags = I915_MMAP_WC; |
| 740 | |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 741 | gem_map.handle = bo->handles[0].u32; |
| 742 | gem_map.offset = 0; |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 743 | gem_map.size = bo->meta.total_size; |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 744 | |
| 745 | 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] | 746 | /* DRM_IOCTL_I915_GEM_MMAP mmaps the underlying shm |
| 747 | * file and returns a user space address directly, ie, |
| 748 | * doesn't go through mmap. If we try that on a |
| 749 | * dma-buf that doesn't have a shm file, i915.ko |
| 750 | * returns ENXIO. Fall through to |
| 751 | * DRM_IOCTL_I915_GEM_MMAP_GTT in that case, which |
| 752 | * will mmap on the drm fd instead. */ |
| 753 | if (ret == 0) |
| 754 | addr = (void *)(uintptr_t)gem_map.addr_ptr; |
| 755 | } |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 756 | |
Kristian H. Kristensen | 8e9c241 | 2020-11-19 19:20:04 +0000 | [diff] [blame] | 757 | if (addr == MAP_FAILED) { |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 758 | struct drm_i915_gem_mmap_gtt gem_map = { 0 }; |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 759 | |
| 760 | gem_map.handle = bo->handles[0].u32; |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 761 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &gem_map); |
| 762 | if (ret) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 763 | drv_log("DRM_IOCTL_I915_GEM_MMAP_GTT failed\n"); |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 764 | return MAP_FAILED; |
| 765 | } |
| 766 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 767 | addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, |
| 768 | bo->drv->fd, gem_map.offset); |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | if (addr == MAP_FAILED) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 772 | drv_log("i915 GEM mmap failed\n"); |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 773 | return addr; |
| 774 | } |
| 775 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 776 | vma->length = bo->meta.total_size; |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 777 | return addr; |
| 778 | } |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 779 | |
Gurchetan Singh | 47e629b | 2017-11-02 14:07:18 -0700 | [diff] [blame] | 780 | static int i915_bo_invalidate(struct bo *bo, struct mapping *mapping) |
Gurchetan Singh | 2d1877f | 2017-10-10 14:12:46 -0700 | [diff] [blame] | 781 | { |
| 782 | int ret; |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 783 | struct drm_i915_gem_set_domain set_domain = { 0 }; |
Gurchetan Singh | 2d1877f | 2017-10-10 14:12:46 -0700 | [diff] [blame] | 784 | |
Gurchetan Singh | 2d1877f | 2017-10-10 14:12:46 -0700 | [diff] [blame] | 785 | set_domain.handle = bo->handles[0].u32; |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 786 | if (bo->meta.tiling == I915_TILING_NONE) { |
Gurchetan Singh | 2d1877f | 2017-10-10 14:12:46 -0700 | [diff] [blame] | 787 | set_domain.read_domains = I915_GEM_DOMAIN_CPU; |
Gurchetan Singh | 47e629b | 2017-11-02 14:07:18 -0700 | [diff] [blame] | 788 | if (mapping->vma->map_flags & BO_MAP_WRITE) |
Gurchetan Singh | 2d1877f | 2017-10-10 14:12:46 -0700 | [diff] [blame] | 789 | set_domain.write_domain = I915_GEM_DOMAIN_CPU; |
| 790 | } else { |
| 791 | set_domain.read_domains = I915_GEM_DOMAIN_GTT; |
Gurchetan Singh | 47e629b | 2017-11-02 14:07:18 -0700 | [diff] [blame] | 792 | if (mapping->vma->map_flags & BO_MAP_WRITE) |
Gurchetan Singh | 2d1877f | 2017-10-10 14:12:46 -0700 | [diff] [blame] | 793 | set_domain.write_domain = I915_GEM_DOMAIN_GTT; |
| 794 | } |
| 795 | |
| 796 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain); |
| 797 | if (ret) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 798 | drv_log("DRM_IOCTL_I915_GEM_SET_DOMAIN with %d\n", ret); |
Gurchetan Singh | 2d1877f | 2017-10-10 14:12:46 -0700 | [diff] [blame] | 799 | return ret; |
| 800 | } |
| 801 | |
| 802 | return 0; |
| 803 | } |
| 804 | |
Gurchetan Singh | 47e629b | 2017-11-02 14:07:18 -0700 | [diff] [blame] | 805 | static int i915_bo_flush(struct bo *bo, struct mapping *mapping) |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 806 | { |
Gurchetan Singh | 68af9c2 | 2017-01-18 13:48:11 -0800 | [diff] [blame] | 807 | struct i915_device *i915 = bo->drv->priv; |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 808 | if (!i915->has_llc && bo->meta.tiling == I915_TILING_NONE) |
Gurchetan Singh | 47e629b | 2017-11-02 14:07:18 -0700 | [diff] [blame] | 809 | i915_clflush(mapping->vma->addr, mapping->vma->length); |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 810 | |
Gurchetan Singh | 8e02e05 | 2017-09-14 14:18:43 -0700 | [diff] [blame] | 811 | return 0; |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 812 | } |
| 813 | |
Gurchetan Singh | 3e9d383 | 2017-10-31 10:36:25 -0700 | [diff] [blame] | 814 | const struct backend backend_i915 = { |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 815 | .name = "i915", |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame] | 816 | .init = i915_init, |
| 817 | .close = i915_close, |
David Stevens | 26fe682 | 2020-03-09 12:23:42 +0000 | [diff] [blame] | 818 | .bo_compute_metadata = i915_bo_compute_metadata, |
| 819 | .bo_create_from_metadata = i915_bo_create_from_metadata, |
Gurchetan Singh | cc015e8 | 2017-01-17 16:15:25 -0800 | [diff] [blame] | 820 | .bo_destroy = drv_gem_bo_destroy, |
Gurchetan Singh | fcad5ad | 2017-01-05 20:39:31 -0800 | [diff] [blame] | 821 | .bo_import = i915_bo_import, |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame] | 822 | .bo_map = i915_bo_map, |
Gurchetan Singh | 8e02e05 | 2017-09-14 14:18:43 -0700 | [diff] [blame] | 823 | .bo_unmap = drv_bo_munmap, |
Gurchetan Singh | 2d1877f | 2017-10-10 14:12:46 -0700 | [diff] [blame] | 824 | .bo_invalidate = i915_bo_invalidate, |
Gurchetan Singh | 8e02e05 | 2017-09-14 14:18:43 -0700 | [diff] [blame] | 825 | .bo_flush = i915_bo_flush, |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 826 | .resolve_format_and_use_flags = drv_resolve_format_and_use_flags_helper, |
Robert Mader | 96058f9 | 2022-04-05 12:15:11 +0200 | [diff] [blame] | 827 | .num_planes_from_modifier = i915_num_planes_from_modifier, |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 828 | }; |
| 829 | |
| 830 | #endif |