blob: 6b4ea2591b898ba0bbd20825d763cb9eebf1ecd3 [file] [log] [blame]
Akshu Agrawal0337d9b2016-07-28 15:35:45 +05301/*
Daniele Castagna7a755de2016-12-16 17:32:30 -05002 * Copyright 2016 The Chromium OS Authors. All rights reserved.
Akshu Agrawal0337d9b2016-07-28 15:35:45 +05303 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6#ifdef DRV_AMDGPU
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -08007#include <amdgpu.h>
8#include <amdgpu_drm.h>
Bas Nieuwenhuizen4c0371b2021-08-10 03:37:00 +02009#include <assert.h>
10#include <drm_fourcc.h>
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053011#include <errno.h>
Bas Nieuwenhuizen4c0371b2021-08-10 03:37:00 +020012#include <fcntl.h>
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053013#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +053016#include <sys/mman.h>
Bas Nieuwenhuizen4c0371b2021-08-10 03:37:00 +020017#include <unistd.h>
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053018#include <xf86drm.h>
Bas Nieuwenhuizen4c0371b2021-08-10 03:37:00 +020019#include <xf86drmMode.h>
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053020
Satyajitcdcebd82018-01-12 14:49:05 +053021#include "dri.h"
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053022#include "drv_priv.h"
23#include "helpers.h"
24#include "util.h"
25
Gurchetan Singhcf9ed9d2019-12-13 09:37:01 -080026// clang-format off
27#define DRI_PATH STRINGIZE(DRI_DRIVER_DIR/radeonsi_dri.so)
28// clang-format on
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053029
Satyajitcdcebd82018-01-12 14:49:05 +053030#define TILE_TYPE_LINEAR 0
Bas Nieuwenhuizen4c0371b2021-08-10 03:37:00 +020031/* We decide a modifier and then use DRI to manage allocation */
32#define TILE_TYPE_DRI_MODIFIER 1
Satyajitcdcebd82018-01-12 14:49:05 +053033/* DRI backend decides tiling in this case. */
Bas Nieuwenhuizen4c0371b2021-08-10 03:37:00 +020034#define TILE_TYPE_DRI 2
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053035
Ikshwaku Chauhan047df2b2020-06-29 16:44:57 +053036/* Height alignement for Encoder/Decoder buffers */
37#define CHROME_HEIGHT_ALIGN 16
38
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +010039struct amdgpu_priv {
Satyajitcdcebd82018-01-12 14:49:05 +053040 struct dri_driver dri;
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +010041 int drm_version;
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +020042
43 /* sdma */
44 struct drm_amdgpu_info_device dev_info;
45 uint32_t sdma_ctx;
46 uint32_t sdma_cmdbuf_bo;
47 uint64_t sdma_cmdbuf_addr;
48 uint64_t sdma_cmdbuf_size;
49 uint32_t *sdma_cmdbuf_map;
50};
51
52struct amdgpu_linear_vma_priv {
53 uint32_t handle;
54 uint32_t map_flags;
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +010055};
56
Bas Nieuwenhuizenb16076b2020-06-29 12:41:18 +020057const static uint32_t render_target_formats[] = {
Gurchetan Singh45ca4492021-04-28 17:12:52 -070058 DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888, DRM_FORMAT_RGB565,
59 DRM_FORMAT_XBGR8888, DRM_FORMAT_XRGB8888, DRM_FORMAT_ABGR2101010,
60 DRM_FORMAT_ARGB2101010, DRM_FORMAT_XBGR2101010, DRM_FORMAT_XRGB2101010,
Lepton Wuc83116f2021-04-26 12:26:56 -070061 DRM_FORMAT_ABGR16161616F,
Bas Nieuwenhuizenb16076b2020-06-29 12:41:18 +020062};
Gurchetan Singh179687e2016-10-28 10:07:35 -070063
Bas Nieuwenhuizenb5e0f2d2020-09-29 16:02:18 +020064const static uint32_t texture_source_formats[] = {
65 DRM_FORMAT_GR88, DRM_FORMAT_R8, DRM_FORMAT_NV21, DRM_FORMAT_NV12,
66 DRM_FORMAT_YVU420_ANDROID, DRM_FORMAT_YVU420, DRM_FORMAT_P010
67};
Shirish Sdf423df2017-04-18 16:21:59 +053068
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +020069static int query_dev_info(int fd, struct drm_amdgpu_info_device *dev_info)
70{
71 struct drm_amdgpu_info info_args = { 0 };
72
73 info_args.return_pointer = (uintptr_t)dev_info;
74 info_args.return_size = sizeof(*dev_info);
75 info_args.query = AMDGPU_INFO_DEV_INFO;
76
77 return drmCommandWrite(fd, DRM_AMDGPU_INFO, &info_args, sizeof(info_args));
78}
79
80static int sdma_init(struct amdgpu_priv *priv, int fd)
81{
82 union drm_amdgpu_ctx ctx_args = { { 0 } };
83 union drm_amdgpu_gem_create gem_create = { { 0 } };
84 struct drm_amdgpu_gem_va va_args = { 0 };
85 union drm_amdgpu_gem_mmap gem_map = { { 0 } };
86 struct drm_gem_close gem_close = { 0 };
87 int ret;
88
89 /* Ensure we can make a submission without BO lists. */
90 if (priv->drm_version < 27)
91 return 0;
92
93 /* Anything outside this range needs adjustments to the SDMA copy commands */
94 if (priv->dev_info.family < AMDGPU_FAMILY_CI || priv->dev_info.family > AMDGPU_FAMILY_NV)
95 return 0;
96
97 ctx_args.in.op = AMDGPU_CTX_OP_ALLOC_CTX;
98
99 ret = drmCommandWriteRead(fd, DRM_AMDGPU_CTX, &ctx_args, sizeof(ctx_args));
100 if (ret < 0)
101 return ret;
102
103 priv->sdma_ctx = ctx_args.out.alloc.ctx_id;
104
105 priv->sdma_cmdbuf_size = ALIGN(4096, priv->dev_info.virtual_address_alignment);
106 gem_create.in.bo_size = priv->sdma_cmdbuf_size;
107 gem_create.in.alignment = 4096;
108 gem_create.in.domains = AMDGPU_GEM_DOMAIN_GTT;
109
110 ret = drmCommandWriteRead(fd, DRM_AMDGPU_GEM_CREATE, &gem_create, sizeof(gem_create));
111 if (ret < 0)
112 goto fail_ctx;
113
114 priv->sdma_cmdbuf_bo = gem_create.out.handle;
115
116 priv->sdma_cmdbuf_addr =
117 ALIGN(priv->dev_info.virtual_address_offset, priv->dev_info.virtual_address_alignment);
118
119 /* Map the buffer into the GPU address space so we can use it from the GPU */
120 va_args.handle = priv->sdma_cmdbuf_bo;
121 va_args.operation = AMDGPU_VA_OP_MAP;
122 va_args.flags = AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_EXECUTABLE;
123 va_args.va_address = priv->sdma_cmdbuf_addr;
124 va_args.offset_in_bo = 0;
125 va_args.map_size = priv->sdma_cmdbuf_size;
126
127 ret = drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
128 if (ret)
129 goto fail_bo;
130
131 gem_map.in.handle = priv->sdma_cmdbuf_bo;
132 ret = drmIoctl(fd, DRM_IOCTL_AMDGPU_GEM_MMAP, &gem_map);
133 if (ret)
134 goto fail_va;
135
136 priv->sdma_cmdbuf_map = mmap(0, priv->sdma_cmdbuf_size, PROT_READ | PROT_WRITE, MAP_SHARED,
137 fd, gem_map.out.addr_ptr);
138 if (priv->sdma_cmdbuf_map == MAP_FAILED) {
139 priv->sdma_cmdbuf_map = NULL;
140 ret = -ENOMEM;
141 goto fail_va;
142 }
143
144 return 0;
145fail_va:
146 va_args.operation = AMDGPU_VA_OP_UNMAP;
147 va_args.flags = 0;
148 drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
149fail_bo:
150 gem_close.handle = priv->sdma_cmdbuf_bo;
151 drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
152fail_ctx:
153 memset(&ctx_args, 0, sizeof(ctx_args));
154 ctx_args.in.op = AMDGPU_CTX_OP_FREE_CTX;
155 ctx_args.in.ctx_id = priv->sdma_ctx;
156 drmCommandWriteRead(fd, DRM_AMDGPU_CTX, &ctx_args, sizeof(ctx_args));
157 return ret;
158}
159
160static void sdma_finish(struct amdgpu_priv *priv, int fd)
161{
162 union drm_amdgpu_ctx ctx_args = { { 0 } };
163 struct drm_amdgpu_gem_va va_args = { 0 };
164 struct drm_gem_close gem_close = { 0 };
165
166 if (!priv->sdma_cmdbuf_map)
167 return;
168
169 va_args.handle = priv->sdma_cmdbuf_bo;
170 va_args.operation = AMDGPU_VA_OP_UNMAP;
171 va_args.flags = 0;
172 va_args.va_address = priv->sdma_cmdbuf_addr;
173 va_args.offset_in_bo = 0;
174 va_args.map_size = priv->sdma_cmdbuf_size;
175 drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
176
177 gem_close.handle = priv->sdma_cmdbuf_bo;
178 drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
179
180 ctx_args.in.op = AMDGPU_CTX_OP_FREE_CTX;
181 ctx_args.in.ctx_id = priv->sdma_ctx;
182 drmCommandWriteRead(fd, DRM_AMDGPU_CTX, &ctx_args, sizeof(ctx_args));
183}
184
185static int sdma_copy(struct amdgpu_priv *priv, int fd, uint32_t src_handle, uint32_t dst_handle,
186 uint64_t size)
187{
188 const uint64_t max_size_per_cmd = 0x3fff00;
189 const uint32_t cmd_size = 7 * sizeof(uint32_t); /* 7 dwords, see loop below. */
190 const uint64_t max_commands = priv->sdma_cmdbuf_size / cmd_size;
191 uint64_t src_addr = priv->sdma_cmdbuf_addr + priv->sdma_cmdbuf_size;
192 uint64_t dst_addr = src_addr + size;
193 struct drm_amdgpu_gem_va va_args = { 0 };
194 unsigned cmd = 0;
195 uint64_t remaining_size = size;
196 uint64_t cur_src_addr = src_addr;
197 uint64_t cur_dst_addr = dst_addr;
198 struct drm_amdgpu_cs_chunk_ib ib = { 0 };
199 struct drm_amdgpu_cs_chunk chunks[2] = { { 0 } };
200 uint64_t chunk_ptrs[2];
201 union drm_amdgpu_cs cs = { { 0 } };
202 struct drm_amdgpu_bo_list_in bo_list = { 0 };
203 struct drm_amdgpu_bo_list_entry bo_list_entries[3] = { { 0 } };
204 union drm_amdgpu_wait_cs wait_cs = { { 0 } };
205 int ret = 0;
206
207 if (size > UINT64_MAX - max_size_per_cmd ||
208 DIV_ROUND_UP(size, max_size_per_cmd) > max_commands)
209 return -ENOMEM;
210
211 /* Map both buffers into the GPU address space so we can access them from the GPU. */
212 va_args.handle = src_handle;
213 va_args.operation = AMDGPU_VA_OP_MAP;
214 va_args.flags = AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_DELAY_UPDATE;
215 va_args.va_address = src_addr;
216 va_args.map_size = size;
217
218 ret = drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
219 if (ret)
220 return ret;
221
222 va_args.handle = dst_handle;
223 va_args.flags = AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE | AMDGPU_VM_DELAY_UPDATE;
224 va_args.va_address = dst_addr;
225
226 ret = drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
227 if (ret)
228 goto unmap_src;
229
230 while (remaining_size) {
231 uint64_t cur_size = remaining_size;
232 if (cur_size > max_size_per_cmd)
233 cur_size = max_size_per_cmd;
234
235 priv->sdma_cmdbuf_map[cmd++] = 0x01; /* linear copy */
236 priv->sdma_cmdbuf_map[cmd++] =
237 priv->dev_info.family >= AMDGPU_FAMILY_AI ? (cur_size - 1) : cur_size;
238 priv->sdma_cmdbuf_map[cmd++] = 0;
239 priv->sdma_cmdbuf_map[cmd++] = cur_src_addr;
240 priv->sdma_cmdbuf_map[cmd++] = cur_src_addr >> 32;
241 priv->sdma_cmdbuf_map[cmd++] = cur_dst_addr;
242 priv->sdma_cmdbuf_map[cmd++] = cur_dst_addr >> 32;
243
244 remaining_size -= cur_size;
245 cur_src_addr += cur_size;
246 cur_dst_addr += cur_size;
247 }
248
249 ib.va_start = priv->sdma_cmdbuf_addr;
250 ib.ib_bytes = cmd * 4;
251 ib.ip_type = AMDGPU_HW_IP_DMA;
252
253 chunks[1].chunk_id = AMDGPU_CHUNK_ID_IB;
254 chunks[1].length_dw = sizeof(ib) / 4;
255 chunks[1].chunk_data = (uintptr_t)&ib;
256
257 bo_list_entries[0].bo_handle = priv->sdma_cmdbuf_bo;
258 bo_list_entries[0].bo_priority = 8; /* Middle of range, like RADV. */
259 bo_list_entries[1].bo_handle = src_handle;
260 bo_list_entries[1].bo_priority = 8;
261 bo_list_entries[2].bo_handle = dst_handle;
262 bo_list_entries[2].bo_priority = 8;
263
264 bo_list.bo_number = 3;
265 bo_list.bo_info_size = sizeof(bo_list_entries[0]);
266 bo_list.bo_info_ptr = (uintptr_t)bo_list_entries;
267
268 chunks[0].chunk_id = AMDGPU_CHUNK_ID_BO_HANDLES;
269 chunks[0].length_dw = sizeof(bo_list) / 4;
270 chunks[0].chunk_data = (uintptr_t)&bo_list;
271
272 chunk_ptrs[0] = (uintptr_t)&chunks[0];
273 chunk_ptrs[1] = (uintptr_t)&chunks[1];
274
275 cs.in.ctx_id = priv->sdma_ctx;
276 cs.in.num_chunks = 2;
277 cs.in.chunks = (uintptr_t)chunk_ptrs;
278
279 ret = drmCommandWriteRead(fd, DRM_AMDGPU_CS, &cs, sizeof(cs));
280 if (ret) {
281 drv_log("SDMA copy command buffer submission failed %d\n", ret);
282 goto unmap_dst;
283 }
284
285 wait_cs.in.handle = cs.out.handle;
286 wait_cs.in.ip_type = AMDGPU_HW_IP_DMA;
287 wait_cs.in.ctx_id = priv->sdma_ctx;
288 wait_cs.in.timeout = INT64_MAX;
289
290 ret = drmCommandWriteRead(fd, DRM_AMDGPU_WAIT_CS, &wait_cs, sizeof(wait_cs));
291 if (ret) {
292 drv_log("Could not wait for CS to finish\n");
293 } else if (wait_cs.out.status) {
294 drv_log("Infinite wait timed out, likely GPU hang.\n");
295 ret = -ENODEV;
296 }
297
298unmap_dst:
299 va_args.handle = dst_handle;
300 va_args.operation = AMDGPU_VA_OP_UNMAP;
301 va_args.flags = AMDGPU_VM_DELAY_UPDATE;
302 va_args.va_address = dst_addr;
303 drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
304
305unmap_src:
306 va_args.handle = src_handle;
307 va_args.operation = AMDGPU_VA_OP_UNMAP;
308 va_args.flags = AMDGPU_VM_DELAY_UPDATE;
309 va_args.va_address = src_addr;
310 drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
311
312 return ret;
313}
314
Bas Nieuwenhuizen4c0371b2021-08-10 03:37:00 +0200315static bool is_modifier_scanout_capable(struct amdgpu_priv *priv, uint32_t format,
316 uint64_t modifier)
317{
318 unsigned bytes_per_pixel = drv_stride_from_format(format, 1, 0);
319
320 if (modifier == DRM_FORMAT_MOD_LINEAR)
321 return true;
322
323 if ((modifier >> 56) != DRM_FORMAT_MOD_VENDOR_AMD)
324 return false;
325
326 unsigned swizzle = AMD_FMT_MOD_GET(TILE, modifier);
327 if (priv->dev_info.family >= AMDGPU_FAMILY_RV) { /* DCN based GPUs */
328 /* D swizzle only supported for 64 bpp */
329 if ((swizzle & 3) == 2 && bytes_per_pixel != 8)
330 return false;
331
332 /* S swizzle not supported for 64 bpp */
333 if ((swizzle & 3) == 1 && bytes_per_pixel == 8)
334 return false;
335 } else { /* DCE based GPUs with GFX9 based modifier swizzling. */
336 assert(priv->dev_info.family == AMDGPU_FAMILY_AI);
337 /* Only D swizzles are allowed for display */
338 if ((swizzle & 3) != 2)
339 return false;
340 }
341
342 if (AMD_FMT_MOD_GET(DCC, modifier) &&
343 (AMD_FMT_MOD_GET(DCC_PIPE_ALIGN, modifier) || !AMD_FMT_MOD_GET(DCC_RETILE, modifier)))
344 return false;
345 return true;
346}
347
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530348static int amdgpu_init(struct driver *drv)
349{
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +0100350 struct amdgpu_priv *priv;
351 drmVersionPtr drm_version;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800352 struct format_metadata metadata;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700353 uint64_t use_flags = BO_USE_RENDER_MASK;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530354
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +0100355 priv = calloc(1, sizeof(struct amdgpu_priv));
356 if (!priv)
Satyajitcdcebd82018-01-12 14:49:05 +0530357 return -ENOMEM;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530358
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +0100359 drm_version = drmGetVersion(drv_get_fd(drv));
360 if (!drm_version) {
361 free(priv);
Satyajitcdcebd82018-01-12 14:49:05 +0530362 return -ENODEV;
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +0100363 }
364
365 priv->drm_version = drm_version->version_minor;
366 drmFreeVersion(drm_version);
367
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +0100368 drv->priv = priv;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530369
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200370 if (query_dev_info(drv_get_fd(drv), &priv->dev_info)) {
371 free(priv);
372 drv->priv = NULL;
373 return -ENODEV;
374 }
Satyajitcdcebd82018-01-12 14:49:05 +0530375 if (dri_init(drv, DRI_PATH, "radeonsi")) {
376 free(priv);
377 drv->priv = NULL;
378 return -ENODEV;
379 }
Shirish Sdf423df2017-04-18 16:21:59 +0530380
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800381 /* Continue on failure, as we can still succesfully map things without SDMA. */
382 if (sdma_init(priv, drv_get_fd(drv)))
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200383 drv_log("SDMA init failed\n");
384
Satyajitcdcebd82018-01-12 14:49:05 +0530385 metadata.tiling = TILE_TYPE_LINEAR;
386 metadata.priority = 1;
Kristian H. Kristensenbc8c5932017-10-24 18:36:32 -0700387 metadata.modifier = DRM_FORMAT_MOD_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800388
Gurchetan Singhd3001452017-11-03 17:18:36 -0700389 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
390 &metadata, use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800391
Satyajitcdcebd82018-01-12 14:49:05 +0530392 drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
393 &metadata, BO_USE_TEXTURE_MASK);
394
Hirokazu Honda3bd681c2020-06-23 17:52:20 +0900395 /* NV12 format for camera, display, decoding and encoding. */
396 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
397 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SCANOUT |
Gurchetan Singh45ca4492021-04-28 17:12:52 -0700398 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER |
399 BO_USE_PROTECTED);
Hirokazu Honda3b8d4d02019-07-31 16:35:52 +0900400
Bas Nieuwenhuizenb5e0f2d2020-09-29 16:02:18 +0200401 drv_modify_combination(drv, DRM_FORMAT_P010, &metadata,
Ikshwaku Chauhan4b69e282021-01-28 23:56:12 +0530402 BO_USE_SCANOUT | BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER |
Gurchetan Singh45ca4492021-04-28 17:12:52 -0700403 BO_USE_PROTECTED);
Bas Nieuwenhuizenb5e0f2d2020-09-29 16:02:18 +0200404
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700405 /* Android CTS tests require this. */
406 drv_add_combination(drv, DRM_FORMAT_BGR888, &metadata, BO_USE_SW_MASK);
407
Satyajitcdcebd82018-01-12 14:49:05 +0530408 /* Linear formats supported by display. */
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800409 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
410 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
Drew Davenport5d215242019-03-25 09:18:42 -0600411 drv_modify_combination(drv, DRM_FORMAT_ABGR8888, &metadata, BO_USE_SCANOUT);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800412 drv_modify_combination(drv, DRM_FORMAT_XBGR8888, &metadata, BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800413
Bas Nieuwenhuizenb16076b2020-06-29 12:41:18 +0200414 drv_modify_combination(drv, DRM_FORMAT_ABGR2101010, &metadata, BO_USE_SCANOUT);
415 drv_modify_combination(drv, DRM_FORMAT_ARGB2101010, &metadata, BO_USE_SCANOUT);
416 drv_modify_combination(drv, DRM_FORMAT_XBGR2101010, &metadata, BO_USE_SCANOUT);
417 drv_modify_combination(drv, DRM_FORMAT_XRGB2101010, &metadata, BO_USE_SCANOUT);
418
Satyajitcdcebd82018-01-12 14:49:05 +0530419 drv_modify_combination(drv, DRM_FORMAT_NV21, &metadata, BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800420
Satyajitcdcebd82018-01-12 14:49:05 +0530421 /*
422 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
David Stevens49518142020-06-15 13:48:48 +0900423 * from camera and input/output from hardware decoder/encoder.
Satyajitcdcebd82018-01-12 14:49:05 +0530424 */
425 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
David Stevens49518142020-06-15 13:48:48 +0900426 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
427 BO_USE_HW_VIDEO_ENCODER);
Satyajitcdcebd82018-01-12 14:49:05 +0530428
429 /*
430 * The following formats will be allocated by the DRI backend and may be potentially tiled.
431 * Since format modifier support hasn't been implemented fully yet, it's not
432 * possible to enumerate the different types of buffers (like i915 can).
433 */
434 use_flags &= ~BO_USE_RENDERSCRIPT;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700435 use_flags &= ~BO_USE_SW_WRITE_OFTEN;
436 use_flags &= ~BO_USE_SW_READ_OFTEN;
Drew Davenportf1a7dfc2021-06-15 00:56:30 -0600437#if __ANDROID__
438 use_flags &= ~BO_USE_SW_WRITE_RARELY;
439 use_flags &= ~BO_USE_SW_READ_RARELY;
440#endif
Gurchetan Singha1892b22017-09-28 16:40:52 -0700441 use_flags &= ~BO_USE_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800442
Satyajitcdcebd82018-01-12 14:49:05 +0530443 metadata.priority = 2;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800444
Bas Nieuwenhuizen4c0371b2021-08-10 03:37:00 +0200445 for (unsigned f = 0; f < ARRAY_SIZE(render_target_formats); ++f) {
446 uint32_t format = render_target_formats[f];
447 int mod_cnt;
448 if (dri_query_modifiers(drv, format, 0, NULL, &mod_cnt) && mod_cnt) {
449 uint64_t *modifiers = calloc(mod_cnt, sizeof(uint64_t));
450 dri_query_modifiers(drv, format, mod_cnt, modifiers, &mod_cnt);
451 metadata.tiling = TILE_TYPE_DRI_MODIFIER;
452 for (int i = 0; i < mod_cnt; ++i) {
453 bool scanout =
454 is_modifier_scanout_capable(drv->priv, format, modifiers[i]);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800455
Bas Nieuwenhuizen4c0371b2021-08-10 03:37:00 +0200456 /* LINEAR will be handled using the LINEAR metadata. */
457 if (modifiers[i] == DRM_FORMAT_MOD_LINEAR)
458 continue;
Bas Nieuwenhuizenb16076b2020-06-29 12:41:18 +0200459
Bas Nieuwenhuizen4c0371b2021-08-10 03:37:00 +0200460 /* The virtgpu minigbm can't handle auxiliary planes in the host. */
461 if (dri_num_planes_from_modifier(drv, format, modifiers[i]) !=
462 drv_num_planes_from_format(format))
463 continue;
464
465 metadata.modifier = modifiers[i];
466 drv_add_combination(drv, format, &metadata,
467 use_flags | (scanout ? BO_USE_SCANOUT : 0));
468 }
469 free(modifiers);
470 } else {
471 bool scanout = false;
472 switch (format) {
473 case DRM_FORMAT_ARGB8888:
474 case DRM_FORMAT_XRGB8888:
475 case DRM_FORMAT_ABGR8888:
476 case DRM_FORMAT_XBGR8888:
477 case DRM_FORMAT_ABGR2101010:
478 case DRM_FORMAT_ARGB2101010:
479 case DRM_FORMAT_XBGR2101010:
480 case DRM_FORMAT_XRGB2101010:
481 scanout = true;
482 break;
483 default:
484 break;
485 }
486 metadata.tiling = TILE_TYPE_DRI;
487 drv_add_combination(drv, format, &metadata,
488 use_flags | (scanout ? BO_USE_SCANOUT : 0));
489 }
490 }
Gurchetan Singhd3001452017-11-03 17:18:36 -0700491 return 0;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530492}
493
494static void amdgpu_close(struct driver *drv)
495{
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200496 sdma_finish(drv->priv, drv_get_fd(drv));
Satyajitcdcebd82018-01-12 14:49:05 +0530497 dri_close(drv);
498 free(drv->priv);
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530499 drv->priv = NULL;
500}
501
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100502static int amdgpu_create_bo_linear(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
503 uint64_t use_flags)
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530504{
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530505 int ret;
Drew Davenport8db36fe2020-10-15 22:18:00 -0600506 size_t num_planes;
Satyajitcdcebd82018-01-12 14:49:05 +0530507 uint32_t plane, stride;
Gurchetan Singh99644382020-10-07 15:28:11 -0700508 union drm_amdgpu_gem_create gem_create = { { 0 } };
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200509 struct amdgpu_priv *priv = bo->drv->priv;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530510
Satyajitcdcebd82018-01-12 14:49:05 +0530511 stride = drv_stride_from_format(format, width, 0);
Drew Davenport8db36fe2020-10-15 22:18:00 -0600512 num_planes = drv_num_planes_from_format(format);
513
514 /*
515 * For multiplane formats, align the stride to 512 to ensure that subsample strides are 256
516 * aligned. This uses more memory than necessary since the first plane only needs to be
517 * 256 aligned, but it's acceptable for a short-term fix. It's probably safe for other gpu
Drew Davenport49b804b2021-07-29 19:52:02 -0600518 * families, but let's restrict it to Raven and Stoney for now (b/171013552, b/190484589).
Drew Davenport8db36fe2020-10-15 22:18:00 -0600519 * */
Drew Davenporte1178652021-08-16 09:36:24 -0600520 if (num_planes > 1 &&
521 (priv->dev_info.family == AMDGPU_FAMILY_RV ||
522 (priv->dev_info.family == AMDGPU_FAMILY_CZ && !(use_flags & BO_USE_HW_VIDEO_ENCODER))))
Drew Davenport8db36fe2020-10-15 22:18:00 -0600523 stride = ALIGN(stride, 512);
524 else
525 stride = ALIGN(stride, 256);
Satyajitcdcebd82018-01-12 14:49:05 +0530526
Ikshwaku Chauhan047df2b2020-06-29 16:44:57 +0530527 /*
Gurchetan Singh9b4c8b72020-08-20 14:25:43 -0700528 * Currently, allocator used by chrome aligns the height for Encoder/
529 * Decoder buffers while allocator used by android(gralloc/minigbm)
530 * doesn't provide any aligment.
531 *
532 * See b/153130069
533 */
Ikshwaku Chauhan047df2b2020-06-29 16:44:57 +0530534 if (use_flags & (BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER))
535 height = ALIGN(height, CHROME_HEIGHT_ALIGN);
536
Satyajitcdcebd82018-01-12 14:49:05 +0530537 drv_bo_from_format(bo, stride, height, format);
Shirish Sdf423df2017-04-18 16:21:59 +0530538
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200539 gem_create.in.bo_size =
540 ALIGN(bo->meta.total_size, priv->dev_info.virtual_address_alignment);
Satyajitcdcebd82018-01-12 14:49:05 +0530541 gem_create.in.alignment = 256;
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800542 gem_create.in.domain_flags = 0;
Satyajitcdcebd82018-01-12 14:49:05 +0530543
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700544 if (use_flags & (BO_USE_LINEAR | BO_USE_SW_MASK))
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800545 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
546
Deepak Sharma62a9c4e2018-05-01 12:11:27 -0700547 gem_create.in.domains = AMDGPU_GEM_DOMAIN_GTT;
Bas Nieuwenhuizen4daf12c2020-06-04 23:11:27 +0200548
549 /* Scanout in GTT requires USWC, otherwise try to use cachable memory
550 * for buffers that are read often, because uncacheable reads can be
551 * very slow. USWC should be faster on the GPU though. */
552 if ((use_flags & BO_USE_SCANOUT) || !(use_flags & BO_USE_SW_READ_OFTEN))
Jao-ke Chin-Lee5481e3c2020-04-10 00:12:12 +0000553 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_CPU_GTT_USWC;
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800554
Ikshwaku Chauhan4b69e282021-01-28 23:56:12 +0530555 /* For protected data Buffer needs to be allocated from TMZ */
556 if (use_flags & BO_USE_PROTECTED)
557 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_ENCRYPTED;
558
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530559 /* Allocate the buffer with the preferred heap. */
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800560 ret = drmCommandWriteRead(drv_get_fd(bo->drv), DRM_AMDGPU_GEM_CREATE, &gem_create,
561 sizeof(gem_create));
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530562 if (ret < 0)
563 return ret;
564
Gurchetan Singh298b7572019-09-19 09:55:18 -0700565 for (plane = 0; plane < bo->meta.num_planes; plane++)
Shirish Sdf423df2017-04-18 16:21:59 +0530566 bo->handles[plane].u32 = gem_create.out.handle;
567
Gurchetan Singh52155b42021-01-27 17:55:17 -0800568 bo->meta.format_modifier = DRM_FORMAT_MOD_LINEAR;
Bas Nieuwenhuizen7119d332020-02-07 20:20:30 +0100569
Satyajitcdcebd82018-01-12 14:49:05 +0530570 return 0;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530571}
572
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100573static int amdgpu_create_bo(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
574 uint64_t use_flags)
Satyajitcdcebd82018-01-12 14:49:05 +0530575{
576 struct combination *combo;
Drew Davenport8ed9b312021-05-06 17:08:27 -0600577 struct amdgpu_priv *priv = bo->drv->priv;
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100578
579 combo = drv_get_combination(bo->drv, format, use_flags);
Satyajitcdcebd82018-01-12 14:49:05 +0530580 if (!combo)
581 return -EINVAL;
582
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100583 if (combo->metadata.tiling == TILE_TYPE_DRI) {
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100584 // See b/122049612
Drew Davenport5ebd19f2021-06-09 00:17:05 -0600585 if (use_flags & (BO_USE_SCANOUT) && priv->dev_info.family == AMDGPU_FAMILY_CZ) {
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100586 uint32_t bytes_per_pixel = drv_bytes_per_pixel_from_format(format, 0);
587 width = ALIGN(width, 256 / bytes_per_pixel);
588 }
589
590 return dri_bo_create(bo, width, height, format, use_flags);
Bas Nieuwenhuizen4c0371b2021-08-10 03:37:00 +0200591 } else if (combo->metadata.tiling == TILE_TYPE_DRI_MODIFIER) {
592 return dri_bo_create_with_modifiers(bo, width, height, format,
593 &combo->metadata.modifier, 1);
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100594 }
595
596 return amdgpu_create_bo_linear(bo, width, height, format, use_flags);
597}
598
599static int amdgpu_create_bo_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
600 uint32_t format, const uint64_t *modifiers,
601 uint32_t count)
602{
603 bool only_use_linear = true;
604
605 for (uint32_t i = 0; i < count; ++i)
606 if (modifiers[i] != DRM_FORMAT_MOD_LINEAR)
607 only_use_linear = false;
608
609 if (only_use_linear)
610 return amdgpu_create_bo_linear(bo, width, height, format, BO_USE_SCANOUT);
611
612 return dri_bo_create_with_modifiers(bo, width, height, format, modifiers, count);
613}
614
615static int amdgpu_import_bo(struct bo *bo, struct drv_import_fd_data *data)
616{
Gurchetan Singh52155b42021-01-27 17:55:17 -0800617 bool dri_tiling = data->format_modifier != DRM_FORMAT_MOD_LINEAR;
618 if (data->format_modifier == DRM_FORMAT_MOD_INVALID) {
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100619 struct combination *combo;
620 combo = drv_get_combination(bo->drv, data->format, data->use_flags);
621 if (!combo)
622 return -EINVAL;
623
Bas Nieuwenhuizen4c0371b2021-08-10 03:37:00 +0200624 dri_tiling = combo->metadata.tiling != TILE_TYPE_LINEAR;
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100625 }
626
627 if (dri_tiling)
Satyajitcdcebd82018-01-12 14:49:05 +0530628 return dri_bo_import(bo, data);
629 else
630 return drv_prime_bo_import(bo, data);
631}
632
633static int amdgpu_destroy_bo(struct bo *bo)
634{
635 if (bo->priv)
636 return dri_bo_destroy(bo);
637 else
638 return drv_gem_bo_destroy(bo);
639}
640
641static void *amdgpu_map_bo(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530642{
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200643 void *addr = MAP_FAILED;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530644 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700645 union drm_amdgpu_gem_mmap gem_map = { { 0 } };
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200646 struct drm_amdgpu_gem_create_in bo_info = { 0 };
647 struct drm_amdgpu_gem_op gem_op = { 0 };
648 uint32_t handle = bo->handles[plane].u32;
649 struct amdgpu_linear_vma_priv *priv = NULL;
650 struct amdgpu_priv *drv_priv;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530651
Satyajitcdcebd82018-01-12 14:49:05 +0530652 if (bo->priv)
653 return dri_bo_map(bo, vma, plane, map_flags);
654
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200655 drv_priv = bo->drv->priv;
656 gem_op.handle = handle;
657 gem_op.op = AMDGPU_GEM_OP_GET_GEM_CREATE_INFO;
658 gem_op.value = (uintptr_t)&bo_info;
659
660 ret = drmCommandWriteRead(bo->drv->fd, DRM_AMDGPU_GEM_OP, &gem_op, sizeof(gem_op));
661 if (ret)
662 return MAP_FAILED;
663
664 vma->length = bo_info.bo_size;
665
666 if (((bo_info.domains & AMDGPU_GEM_DOMAIN_VRAM) ||
667 (bo_info.domain_flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)) &&
668 drv_priv->sdma_cmdbuf_map) {
669 union drm_amdgpu_gem_create gem_create = { { 0 } };
670
671 priv = calloc(1, sizeof(struct amdgpu_linear_vma_priv));
672 if (!priv)
673 return MAP_FAILED;
674
675 gem_create.in.bo_size = bo_info.bo_size;
676 gem_create.in.alignment = 4096;
677 gem_create.in.domains = AMDGPU_GEM_DOMAIN_GTT;
678
679 ret = drmCommandWriteRead(bo->drv->fd, DRM_AMDGPU_GEM_CREATE, &gem_create,
680 sizeof(gem_create));
681 if (ret < 0) {
682 drv_log("GEM create failed\n");
683 free(priv);
684 return MAP_FAILED;
685 }
686
687 priv->map_flags = map_flags;
688 handle = priv->handle = gem_create.out.handle;
689
690 ret = sdma_copy(bo->drv->priv, bo->drv->fd, bo->handles[0].u32, priv->handle,
691 bo_info.bo_size);
692 if (ret) {
693 drv_log("SDMA copy for read failed\n");
694 goto fail;
695 }
696 }
697
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200698 gem_map.in.handle = handle;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530699 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_AMDGPU_GEM_MMAP, &gem_map);
700 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700701 drv_log("DRM_IOCTL_AMDGPU_GEM_MMAP failed\n");
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200702 goto fail;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530703 }
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700704
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200705 addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700706 gem_map.out.addr_ptr);
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200707 if (addr == MAP_FAILED)
708 goto fail;
709
710 vma->priv = priv;
711 return addr;
712
713fail:
714 if (priv) {
715 struct drm_gem_close gem_close = { 0 };
716 gem_close.handle = priv->handle;
717 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
718 free(priv);
719 }
720 return MAP_FAILED;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530721}
722
Satyajitcdcebd82018-01-12 14:49:05 +0530723static int amdgpu_unmap_bo(struct bo *bo, struct vma *vma)
724{
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800725 if (bo->priv) {
Satyajitcdcebd82018-01-12 14:49:05 +0530726 return dri_bo_unmap(bo, vma);
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800727 } else {
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200728 int r = munmap(vma->addr, vma->length);
729 if (r)
730 return r;
731
732 if (vma->priv) {
733 struct amdgpu_linear_vma_priv *priv = vma->priv;
734 struct drm_gem_close gem_close = { 0 };
735
736 if (BO_MAP_WRITE & priv->map_flags) {
737 r = sdma_copy(bo->drv->priv, bo->drv->fd, priv->handle,
738 bo->handles[0].u32, vma->length);
739 if (r)
740 return r;
741 }
742
743 gem_close.handle = priv->handle;
744 r = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
745 }
746
747 return 0;
748 }
Satyajitcdcebd82018-01-12 14:49:05 +0530749}
750
Deepak Sharmaff66c802018-11-16 12:10:54 -0800751static int amdgpu_bo_invalidate(struct bo *bo, struct mapping *mapping)
752{
753 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700754 union drm_amdgpu_gem_wait_idle wait_idle = { { 0 } };
Deepak Sharmaff66c802018-11-16 12:10:54 -0800755
756 if (bo->priv)
757 return 0;
758
Deepak Sharmaff66c802018-11-16 12:10:54 -0800759 wait_idle.in.handle = bo->handles[0].u32;
760 wait_idle.in.timeout = AMDGPU_TIMEOUT_INFINITE;
761
762 ret = drmCommandWriteRead(bo->drv->fd, DRM_AMDGPU_GEM_WAIT_IDLE, &wait_idle,
763 sizeof(wait_idle));
764
765 if (ret < 0) {
766 drv_log("DRM_AMDGPU_GEM_WAIT_IDLE failed with %d\n", ret);
767 return ret;
768 }
769
770 if (ret == 0 && wait_idle.out.status)
771 drv_log("DRM_AMDGPU_GEM_WAIT_IDLE BO is busy\n");
772
773 return 0;
774}
775
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700776const struct backend backend_amdgpu = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530777 .name = "amdgpu",
778 .init = amdgpu_init,
779 .close = amdgpu_close,
Satyajitcdcebd82018-01-12 14:49:05 +0530780 .bo_create = amdgpu_create_bo,
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100781 .bo_create_with_modifiers = amdgpu_create_bo_with_modifiers,
Satyajitcdcebd82018-01-12 14:49:05 +0530782 .bo_destroy = amdgpu_destroy_bo,
783 .bo_import = amdgpu_import_bo,
784 .bo_map = amdgpu_map_bo,
785 .bo_unmap = amdgpu_unmap_bo,
Deepak Sharmaff66c802018-11-16 12:10:54 -0800786 .bo_invalidate = amdgpu_bo_invalidate,
Gurchetan Singh695125c2021-02-03 08:44:09 -0800787 .resolve_format = drv_resolve_format_helper,
ChromeOS Developer44588bb2020-03-02 16:32:09 +0100788 .num_planes_from_modifier = dri_num_planes_from_modifier,
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530789};
790
791#endif