blob: 5c891de1a13e0eb2cb59aed65c685f253a3da183 [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"
Yiwei Zhangb7a64442021-09-30 05:13:10 +000022#include "drv_helpers.h"
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053023#include "drv_priv.h"
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053024#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) {
Yiwei Zhang04954732022-07-13 23:34:33 +0000281 drv_loge("SDMA copy command buffer submission failed %d\n", ret);
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200282 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) {
Yiwei Zhang04954732022-07-13 23:34:33 +0000292 drv_loge("Could not wait for CS to finish\n");
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200293 } else if (wait_cs.out.status) {
Yiwei Zhang04954732022-07-13 23:34:33 +0000294 drv_loge("Infinite wait timed out, likely GPU hang.\n");
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200295 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)))
Yiwei Zhang04954732022-07-13 23:34:33 +0000383 drv_loge("SDMA init failed\n");
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200384
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);
Yiwei Zhang1c2a0ef2021-09-02 22:59:29 +0000413 drv_modify_combination(drv, DRM_FORMAT_RGB565, &metadata, BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800414
Bas Nieuwenhuizenb16076b2020-06-29 12:41:18 +0200415 drv_modify_combination(drv, DRM_FORMAT_ABGR2101010, &metadata, BO_USE_SCANOUT);
416 drv_modify_combination(drv, DRM_FORMAT_ARGB2101010, &metadata, BO_USE_SCANOUT);
417 drv_modify_combination(drv, DRM_FORMAT_XBGR2101010, &metadata, BO_USE_SCANOUT);
418 drv_modify_combination(drv, DRM_FORMAT_XRGB2101010, &metadata, BO_USE_SCANOUT);
419
Satyajitcdcebd82018-01-12 14:49:05 +0530420 drv_modify_combination(drv, DRM_FORMAT_NV21, &metadata, BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800421
Satyajitcdcebd82018-01-12 14:49:05 +0530422 /*
423 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
David Stevens49518142020-06-15 13:48:48 +0900424 * from camera and input/output from hardware decoder/encoder.
Satyajitcdcebd82018-01-12 14:49:05 +0530425 */
426 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
David Stevens49518142020-06-15 13:48:48 +0900427 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
Jason Macnak80f664c2022-07-19 16:44:22 -0700428 BO_USE_HW_VIDEO_ENCODER | BO_USE_GPU_DATA_BUFFER |
429 BO_USE_SENSOR_DIRECT_DATA);
Satyajitcdcebd82018-01-12 14:49:05 +0530430
431 /*
432 * The following formats will be allocated by the DRI backend and may be potentially tiled.
433 * Since format modifier support hasn't been implemented fully yet, it's not
434 * possible to enumerate the different types of buffers (like i915 can).
435 */
436 use_flags &= ~BO_USE_RENDERSCRIPT;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700437 use_flags &= ~BO_USE_SW_WRITE_OFTEN;
438 use_flags &= ~BO_USE_SW_READ_OFTEN;
Drew Davenportf1a7dfc2021-06-15 00:56:30 -0600439#if __ANDROID__
440 use_flags &= ~BO_USE_SW_WRITE_RARELY;
441 use_flags &= ~BO_USE_SW_READ_RARELY;
442#endif
Gurchetan Singha1892b22017-09-28 16:40:52 -0700443 use_flags &= ~BO_USE_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800444
Satyajitcdcebd82018-01-12 14:49:05 +0530445 metadata.priority = 2;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800446
Bas Nieuwenhuizen4c0371b2021-08-10 03:37:00 +0200447 for (unsigned f = 0; f < ARRAY_SIZE(render_target_formats); ++f) {
448 uint32_t format = render_target_formats[f];
449 int mod_cnt;
450 if (dri_query_modifiers(drv, format, 0, NULL, &mod_cnt) && mod_cnt) {
451 uint64_t *modifiers = calloc(mod_cnt, sizeof(uint64_t));
452 dri_query_modifiers(drv, format, mod_cnt, modifiers, &mod_cnt);
453 metadata.tiling = TILE_TYPE_DRI_MODIFIER;
454 for (int i = 0; i < mod_cnt; ++i) {
455 bool scanout =
456 is_modifier_scanout_capable(drv->priv, format, modifiers[i]);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800457
Bas Nieuwenhuizen4c0371b2021-08-10 03:37:00 +0200458 /* LINEAR will be handled using the LINEAR metadata. */
459 if (modifiers[i] == DRM_FORMAT_MOD_LINEAR)
460 continue;
Bas Nieuwenhuizenb16076b2020-06-29 12:41:18 +0200461
Bas Nieuwenhuizen4c0371b2021-08-10 03:37:00 +0200462 /* The virtgpu minigbm can't handle auxiliary planes in the host. */
463 if (dri_num_planes_from_modifier(drv, format, modifiers[i]) !=
464 drv_num_planes_from_format(format))
465 continue;
466
467 metadata.modifier = modifiers[i];
468 drv_add_combination(drv, format, &metadata,
469 use_flags | (scanout ? BO_USE_SCANOUT : 0));
470 }
471 free(modifiers);
472 } else {
473 bool scanout = false;
474 switch (format) {
475 case DRM_FORMAT_ARGB8888:
476 case DRM_FORMAT_XRGB8888:
477 case DRM_FORMAT_ABGR8888:
478 case DRM_FORMAT_XBGR8888:
479 case DRM_FORMAT_ABGR2101010:
480 case DRM_FORMAT_ARGB2101010:
481 case DRM_FORMAT_XBGR2101010:
482 case DRM_FORMAT_XRGB2101010:
483 scanout = true;
484 break;
485 default:
486 break;
487 }
488 metadata.tiling = TILE_TYPE_DRI;
489 drv_add_combination(drv, format, &metadata,
490 use_flags | (scanout ? BO_USE_SCANOUT : 0));
491 }
492 }
Gurchetan Singhd3001452017-11-03 17:18:36 -0700493 return 0;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530494}
495
496static void amdgpu_close(struct driver *drv)
497{
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200498 sdma_finish(drv->priv, drv_get_fd(drv));
Satyajitcdcebd82018-01-12 14:49:05 +0530499 dri_close(drv);
500 free(drv->priv);
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530501 drv->priv = NULL;
502}
503
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100504static int amdgpu_create_bo_linear(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
505 uint64_t use_flags)
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530506{
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530507 int ret;
Drew Davenport8db36fe2020-10-15 22:18:00 -0600508 size_t num_planes;
Satyajitcdcebd82018-01-12 14:49:05 +0530509 uint32_t plane, stride;
Gurchetan Singh99644382020-10-07 15:28:11 -0700510 union drm_amdgpu_gem_create gem_create = { { 0 } };
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200511 struct amdgpu_priv *priv = bo->drv->priv;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530512
Satyajitcdcebd82018-01-12 14:49:05 +0530513 stride = drv_stride_from_format(format, width, 0);
Drew Davenport8db36fe2020-10-15 22:18:00 -0600514 num_planes = drv_num_planes_from_format(format);
515
516 /*
517 * For multiplane formats, align the stride to 512 to ensure that subsample strides are 256
518 * aligned. This uses more memory than necessary since the first plane only needs to be
519 * 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 -0600520 * families, but let's restrict it to Raven and Stoney for now (b/171013552, b/190484589).
Miguel Casas26960902022-05-13 17:55:49 -0700521 * This only applies to the Android YUV (multiplane) format.
Drew Davenport8db36fe2020-10-15 22:18:00 -0600522 * */
Yiwei Zhang7648f062022-07-13 23:15:22 +0000523 if (format == DRM_FORMAT_YVU420_ANDROID && (priv->dev_info.family == AMDGPU_FAMILY_RV ||
524 priv->dev_info.family == AMDGPU_FAMILY_CZ))
Drew Davenport8db36fe2020-10-15 22:18:00 -0600525 stride = ALIGN(stride, 512);
526 else
527 stride = ALIGN(stride, 256);
Satyajitcdcebd82018-01-12 14:49:05 +0530528
Ikshwaku Chauhan047df2b2020-06-29 16:44:57 +0530529 /*
Gurchetan Singh9b4c8b72020-08-20 14:25:43 -0700530 * Currently, allocator used by chrome aligns the height for Encoder/
531 * Decoder buffers while allocator used by android(gralloc/minigbm)
532 * doesn't provide any aligment.
533 *
534 * See b/153130069
535 */
Ikshwaku Chauhan047df2b2020-06-29 16:44:57 +0530536 if (use_flags & (BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER))
537 height = ALIGN(height, CHROME_HEIGHT_ALIGN);
538
Satyajitcdcebd82018-01-12 14:49:05 +0530539 drv_bo_from_format(bo, stride, height, format);
Shirish Sdf423df2017-04-18 16:21:59 +0530540
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200541 gem_create.in.bo_size =
542 ALIGN(bo->meta.total_size, priv->dev_info.virtual_address_alignment);
Satyajitcdcebd82018-01-12 14:49:05 +0530543 gem_create.in.alignment = 256;
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800544 gem_create.in.domain_flags = 0;
Satyajitcdcebd82018-01-12 14:49:05 +0530545
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700546 if (use_flags & (BO_USE_LINEAR | BO_USE_SW_MASK))
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800547 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
548
Deepak Sharma62a9c4e2018-05-01 12:11:27 -0700549 gem_create.in.domains = AMDGPU_GEM_DOMAIN_GTT;
Bas Nieuwenhuizen4daf12c2020-06-04 23:11:27 +0200550
551 /* Scanout in GTT requires USWC, otherwise try to use cachable memory
552 * for buffers that are read often, because uncacheable reads can be
553 * very slow. USWC should be faster on the GPU though. */
554 if ((use_flags & BO_USE_SCANOUT) || !(use_flags & BO_USE_SW_READ_OFTEN))
Jao-ke Chin-Lee5481e3c2020-04-10 00:12:12 +0000555 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_CPU_GTT_USWC;
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800556
Ikshwaku Chauhan4b69e282021-01-28 23:56:12 +0530557 /* For protected data Buffer needs to be allocated from TMZ */
558 if (use_flags & BO_USE_PROTECTED)
559 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_ENCRYPTED;
560
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530561 /* Allocate the buffer with the preferred heap. */
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800562 ret = drmCommandWriteRead(drv_get_fd(bo->drv), DRM_AMDGPU_GEM_CREATE, &gem_create,
563 sizeof(gem_create));
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530564 if (ret < 0)
565 return ret;
566
Gurchetan Singh298b7572019-09-19 09:55:18 -0700567 for (plane = 0; plane < bo->meta.num_planes; plane++)
Shirish Sdf423df2017-04-18 16:21:59 +0530568 bo->handles[plane].u32 = gem_create.out.handle;
569
Gurchetan Singh52155b42021-01-27 17:55:17 -0800570 bo->meta.format_modifier = DRM_FORMAT_MOD_LINEAR;
Bas Nieuwenhuizen7119d332020-02-07 20:20:30 +0100571
Satyajitcdcebd82018-01-12 14:49:05 +0530572 return 0;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530573}
574
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100575static int amdgpu_create_bo(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
576 uint64_t use_flags)
Satyajitcdcebd82018-01-12 14:49:05 +0530577{
578 struct combination *combo;
Drew Davenport8ed9b312021-05-06 17:08:27 -0600579 struct amdgpu_priv *priv = bo->drv->priv;
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100580
581 combo = drv_get_combination(bo->drv, format, use_flags);
Satyajitcdcebd82018-01-12 14:49:05 +0530582 if (!combo)
583 return -EINVAL;
584
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100585 if (combo->metadata.tiling == TILE_TYPE_DRI) {
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100586 // See b/122049612
Drew Davenport5ebd19f2021-06-09 00:17:05 -0600587 if (use_flags & (BO_USE_SCANOUT) && priv->dev_info.family == AMDGPU_FAMILY_CZ) {
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100588 uint32_t bytes_per_pixel = drv_bytes_per_pixel_from_format(format, 0);
589 width = ALIGN(width, 256 / bytes_per_pixel);
590 }
591
592 return dri_bo_create(bo, width, height, format, use_flags);
Bas Nieuwenhuizen4c0371b2021-08-10 03:37:00 +0200593 } else if (combo->metadata.tiling == TILE_TYPE_DRI_MODIFIER) {
594 return dri_bo_create_with_modifiers(bo, width, height, format,
595 &combo->metadata.modifier, 1);
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100596 }
597
598 return amdgpu_create_bo_linear(bo, width, height, format, use_flags);
599}
600
601static int amdgpu_create_bo_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
602 uint32_t format, const uint64_t *modifiers,
603 uint32_t count)
604{
605 bool only_use_linear = true;
606
607 for (uint32_t i = 0; i < count; ++i)
608 if (modifiers[i] != DRM_FORMAT_MOD_LINEAR)
609 only_use_linear = false;
610
611 if (only_use_linear)
612 return amdgpu_create_bo_linear(bo, width, height, format, BO_USE_SCANOUT);
613
614 return dri_bo_create_with_modifiers(bo, width, height, format, modifiers, count);
615}
616
617static int amdgpu_import_bo(struct bo *bo, struct drv_import_fd_data *data)
618{
Gurchetan Singh52155b42021-01-27 17:55:17 -0800619 bool dri_tiling = data->format_modifier != DRM_FORMAT_MOD_LINEAR;
620 if (data->format_modifier == DRM_FORMAT_MOD_INVALID) {
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100621 struct combination *combo;
622 combo = drv_get_combination(bo->drv, data->format, data->use_flags);
623 if (!combo)
624 return -EINVAL;
625
Bas Nieuwenhuizen4c0371b2021-08-10 03:37:00 +0200626 dri_tiling = combo->metadata.tiling != TILE_TYPE_LINEAR;
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100627 }
628
Yiwei Zhang7648f062022-07-13 23:15:22 +0000629 bo->meta.num_planes =
630 dri_num_planes_from_modifier(bo->drv, data->format, data->format_modifier);
Robert Maderd73fa7f2022-05-03 09:24:37 +0200631
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100632 if (dri_tiling)
Satyajitcdcebd82018-01-12 14:49:05 +0530633 return dri_bo_import(bo, data);
634 else
635 return drv_prime_bo_import(bo, data);
636}
637
Bas Nieuwenhuizen136d9222021-11-10 14:04:21 +0100638static int amdgpu_release_bo(struct bo *bo)
639{
640 if (bo->priv)
641 return dri_bo_release(bo);
642
643 return 0;
644}
645
Satyajitcdcebd82018-01-12 14:49:05 +0530646static int amdgpu_destroy_bo(struct bo *bo)
647{
648 if (bo->priv)
649 return dri_bo_destroy(bo);
650 else
651 return drv_gem_bo_destroy(bo);
652}
653
654static void *amdgpu_map_bo(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530655{
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200656 void *addr = MAP_FAILED;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530657 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700658 union drm_amdgpu_gem_mmap gem_map = { { 0 } };
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200659 struct drm_amdgpu_gem_create_in bo_info = { 0 };
660 struct drm_amdgpu_gem_op gem_op = { 0 };
661 uint32_t handle = bo->handles[plane].u32;
662 struct amdgpu_linear_vma_priv *priv = NULL;
663 struct amdgpu_priv *drv_priv;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530664
Satyajitcdcebd82018-01-12 14:49:05 +0530665 if (bo->priv)
666 return dri_bo_map(bo, vma, plane, map_flags);
667
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200668 drv_priv = bo->drv->priv;
669 gem_op.handle = handle;
670 gem_op.op = AMDGPU_GEM_OP_GET_GEM_CREATE_INFO;
671 gem_op.value = (uintptr_t)&bo_info;
672
673 ret = drmCommandWriteRead(bo->drv->fd, DRM_AMDGPU_GEM_OP, &gem_op, sizeof(gem_op));
674 if (ret)
675 return MAP_FAILED;
676
677 vma->length = bo_info.bo_size;
678
679 if (((bo_info.domains & AMDGPU_GEM_DOMAIN_VRAM) ||
680 (bo_info.domain_flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)) &&
681 drv_priv->sdma_cmdbuf_map) {
682 union drm_amdgpu_gem_create gem_create = { { 0 } };
683
684 priv = calloc(1, sizeof(struct amdgpu_linear_vma_priv));
685 if (!priv)
686 return MAP_FAILED;
687
688 gem_create.in.bo_size = bo_info.bo_size;
689 gem_create.in.alignment = 4096;
690 gem_create.in.domains = AMDGPU_GEM_DOMAIN_GTT;
691
692 ret = drmCommandWriteRead(bo->drv->fd, DRM_AMDGPU_GEM_CREATE, &gem_create,
693 sizeof(gem_create));
694 if (ret < 0) {
Yiwei Zhang04954732022-07-13 23:34:33 +0000695 drv_loge("GEM create failed\n");
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200696 free(priv);
697 return MAP_FAILED;
698 }
699
700 priv->map_flags = map_flags;
701 handle = priv->handle = gem_create.out.handle;
702
703 ret = sdma_copy(bo->drv->priv, bo->drv->fd, bo->handles[0].u32, priv->handle,
704 bo_info.bo_size);
705 if (ret) {
Yiwei Zhang04954732022-07-13 23:34:33 +0000706 drv_loge("SDMA copy for read failed\n");
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200707 goto fail;
708 }
709 }
710
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200711 gem_map.in.handle = handle;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530712 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_AMDGPU_GEM_MMAP, &gem_map);
713 if (ret) {
Yiwei Zhang04954732022-07-13 23:34:33 +0000714 drv_loge("DRM_IOCTL_AMDGPU_GEM_MMAP failed\n");
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200715 goto fail;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530716 }
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700717
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200718 addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700719 gem_map.out.addr_ptr);
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200720 if (addr == MAP_FAILED)
721 goto fail;
722
723 vma->priv = priv;
724 return addr;
725
726fail:
727 if (priv) {
728 struct drm_gem_close gem_close = { 0 };
729 gem_close.handle = priv->handle;
730 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
731 free(priv);
732 }
733 return MAP_FAILED;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530734}
735
Satyajitcdcebd82018-01-12 14:49:05 +0530736static int amdgpu_unmap_bo(struct bo *bo, struct vma *vma)
737{
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800738 if (bo->priv) {
Satyajitcdcebd82018-01-12 14:49:05 +0530739 return dri_bo_unmap(bo, vma);
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800740 } else {
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200741 int r = munmap(vma->addr, vma->length);
742 if (r)
743 return r;
744
745 if (vma->priv) {
746 struct amdgpu_linear_vma_priv *priv = vma->priv;
747 struct drm_gem_close gem_close = { 0 };
748
749 if (BO_MAP_WRITE & priv->map_flags) {
750 r = sdma_copy(bo->drv->priv, bo->drv->fd, priv->handle,
751 bo->handles[0].u32, vma->length);
752 if (r)
753 return r;
754 }
755
756 gem_close.handle = priv->handle;
757 r = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
758 }
759
760 return 0;
761 }
Satyajitcdcebd82018-01-12 14:49:05 +0530762}
763
Deepak Sharmaff66c802018-11-16 12:10:54 -0800764static int amdgpu_bo_invalidate(struct bo *bo, struct mapping *mapping)
765{
766 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700767 union drm_amdgpu_gem_wait_idle wait_idle = { { 0 } };
Deepak Sharmaff66c802018-11-16 12:10:54 -0800768
769 if (bo->priv)
770 return 0;
771
Deepak Sharmaff66c802018-11-16 12:10:54 -0800772 wait_idle.in.handle = bo->handles[0].u32;
773 wait_idle.in.timeout = AMDGPU_TIMEOUT_INFINITE;
774
775 ret = drmCommandWriteRead(bo->drv->fd, DRM_AMDGPU_GEM_WAIT_IDLE, &wait_idle,
776 sizeof(wait_idle));
777
778 if (ret < 0) {
Yiwei Zhang04954732022-07-13 23:34:33 +0000779 drv_loge("DRM_AMDGPU_GEM_WAIT_IDLE failed with %d\n", ret);
Deepak Sharmaff66c802018-11-16 12:10:54 -0800780 return ret;
781 }
782
783 if (ret == 0 && wait_idle.out.status)
Yiwei Zhang04954732022-07-13 23:34:33 +0000784 drv_loge("DRM_AMDGPU_GEM_WAIT_IDLE BO is busy\n");
Deepak Sharmaff66c802018-11-16 12:10:54 -0800785
786 return 0;
787}
788
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700789const struct backend backend_amdgpu = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530790 .name = "amdgpu",
791 .init = amdgpu_init,
792 .close = amdgpu_close,
Satyajitcdcebd82018-01-12 14:49:05 +0530793 .bo_create = amdgpu_create_bo,
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100794 .bo_create_with_modifiers = amdgpu_create_bo_with_modifiers,
Bas Nieuwenhuizen136d9222021-11-10 14:04:21 +0100795 .bo_release = amdgpu_release_bo,
Satyajitcdcebd82018-01-12 14:49:05 +0530796 .bo_destroy = amdgpu_destroy_bo,
797 .bo_import = amdgpu_import_bo,
798 .bo_map = amdgpu_map_bo,
799 .bo_unmap = amdgpu_unmap_bo,
Deepak Sharmaff66c802018-11-16 12:10:54 -0800800 .bo_invalidate = amdgpu_bo_invalidate,
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000801 .resolve_format_and_use_flags = drv_resolve_format_and_use_flags_helper,
ChromeOS Developer44588bb2020-03-02 16:32:09 +0100802 .num_planes_from_modifier = dri_num_planes_from_modifier,
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530803};
804
805#endif