1 /*
2 * Copyright 2016 The Chromium OS Authors. All rights reserved.
3 * 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
7 #include <amdgpu.h>
8 #include <amdgpu_drm.h>
9 #include <assert.h>
10 #include <drm_fourcc.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <inttypes.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/mman.h>
18 #include <unistd.h>
19 #include <xf86drm.h>
20 #include <xf86drmMode.h>
21
22 #include "dri.h"
23 #include "drv_helpers.h"
24 #include "drv_priv.h"
25 #include "util.h"
26
27 // clang-format off
28 #define DRI_PATH STRINGIZE(DRI_DRIVER_DIR/radeonsi_dri.so)
29 // clang-format on
30
31 #define TILE_TYPE_LINEAR 0
32 /* We decide a modifier and then use DRI to manage allocation */
33 #define TILE_TYPE_DRI_MODIFIER 1
34 /* DRI backend decides tiling in this case. */
35 #define TILE_TYPE_DRI 2
36
37 /* Height alignement for Encoder/Decoder buffers */
38 #define CHROME_HEIGHT_ALIGN 16
39
40 struct amdgpu_priv {
41 struct dri_driver dri;
42 int drm_version;
43
44 /* sdma */
45 struct drm_amdgpu_info_device dev_info;
46 uint32_t sdma_ctx;
47 uint32_t sdma_cmdbuf_bo;
48 uint64_t sdma_cmdbuf_addr;
49 uint64_t sdma_cmdbuf_size;
50 uint32_t *sdma_cmdbuf_map;
51 };
52
53 struct amdgpu_linear_vma_priv {
54 uint32_t handle;
55 uint32_t map_flags;
56 };
57
58 const static uint32_t render_target_formats[] = {
59 DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888, DRM_FORMAT_RGB565,
60 DRM_FORMAT_XBGR8888, DRM_FORMAT_XRGB8888, DRM_FORMAT_ABGR2101010,
61 DRM_FORMAT_ARGB2101010, DRM_FORMAT_XBGR2101010, DRM_FORMAT_XRGB2101010,
62 DRM_FORMAT_ABGR16161616F,
63 };
64
65 const static uint32_t texture_source_formats[] = {
66 DRM_FORMAT_GR88, DRM_FORMAT_R8, DRM_FORMAT_NV21, DRM_FORMAT_NV12,
67 DRM_FORMAT_YVU420_ANDROID, DRM_FORMAT_YVU420, DRM_FORMAT_P010
68 };
69
query_dev_info(int fd,struct drm_amdgpu_info_device * dev_info)70 static int query_dev_info(int fd, struct drm_amdgpu_info_device *dev_info)
71 {
72 struct drm_amdgpu_info info_args = { 0 };
73
74 info_args.return_pointer = (uintptr_t)dev_info;
75 info_args.return_size = sizeof(*dev_info);
76 info_args.query = AMDGPU_INFO_DEV_INFO;
77
78 return drmCommandWrite(fd, DRM_AMDGPU_INFO, &info_args, sizeof(info_args));
79 }
80
sdma_init(struct amdgpu_priv * priv,int fd)81 static int sdma_init(struct amdgpu_priv *priv, int fd)
82 {
83 union drm_amdgpu_ctx ctx_args = { { 0 } };
84 union drm_amdgpu_gem_create gem_create = { { 0 } };
85 struct drm_amdgpu_gem_va va_args = { 0 };
86 union drm_amdgpu_gem_mmap gem_map = { { 0 } };
87 struct drm_gem_close gem_close = { 0 };
88 int ret;
89
90 /* Ensure we can make a submission without BO lists. */
91 if (priv->drm_version < 27)
92 return 0;
93
94 /* Anything outside this range needs adjustments to the SDMA copy commands */
95 if (priv->dev_info.family < AMDGPU_FAMILY_CI || priv->dev_info.family > AMDGPU_FAMILY_NV)
96 return 0;
97
98 ctx_args.in.op = AMDGPU_CTX_OP_ALLOC_CTX;
99
100 ret = drmCommandWriteRead(fd, DRM_AMDGPU_CTX, &ctx_args, sizeof(ctx_args));
101 if (ret < 0)
102 return ret;
103
104 priv->sdma_ctx = ctx_args.out.alloc.ctx_id;
105
106 priv->sdma_cmdbuf_size = ALIGN(4096, priv->dev_info.virtual_address_alignment);
107 gem_create.in.bo_size = priv->sdma_cmdbuf_size;
108 gem_create.in.alignment = 4096;
109 gem_create.in.domains = AMDGPU_GEM_DOMAIN_GTT;
110
111 ret = drmCommandWriteRead(fd, DRM_AMDGPU_GEM_CREATE, &gem_create, sizeof(gem_create));
112 if (ret < 0)
113 goto fail_ctx;
114
115 priv->sdma_cmdbuf_bo = gem_create.out.handle;
116
117 priv->sdma_cmdbuf_addr =
118 ALIGN(priv->dev_info.virtual_address_offset, priv->dev_info.virtual_address_alignment);
119
120 /* Map the buffer into the GPU address space so we can use it from the GPU */
121 va_args.handle = priv->sdma_cmdbuf_bo;
122 va_args.operation = AMDGPU_VA_OP_MAP;
123 va_args.flags = AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_EXECUTABLE;
124 va_args.va_address = priv->sdma_cmdbuf_addr;
125 va_args.offset_in_bo = 0;
126 va_args.map_size = priv->sdma_cmdbuf_size;
127
128 ret = drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
129 if (ret)
130 goto fail_bo;
131
132 gem_map.in.handle = priv->sdma_cmdbuf_bo;
133 ret = drmIoctl(fd, DRM_IOCTL_AMDGPU_GEM_MMAP, &gem_map);
134 if (ret)
135 goto fail_va;
136
137 priv->sdma_cmdbuf_map = mmap(0, priv->sdma_cmdbuf_size, PROT_READ | PROT_WRITE, MAP_SHARED,
138 fd, gem_map.out.addr_ptr);
139 if (priv->sdma_cmdbuf_map == MAP_FAILED) {
140 priv->sdma_cmdbuf_map = NULL;
141 ret = -ENOMEM;
142 goto fail_va;
143 }
144
145 return 0;
146 fail_va:
147 va_args.operation = AMDGPU_VA_OP_UNMAP;
148 va_args.flags = 0;
149 drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
150 fail_bo:
151 gem_close.handle = priv->sdma_cmdbuf_bo;
152 drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
153 fail_ctx:
154 memset(&ctx_args, 0, sizeof(ctx_args));
155 ctx_args.in.op = AMDGPU_CTX_OP_FREE_CTX;
156 ctx_args.in.ctx_id = priv->sdma_ctx;
157 drmCommandWriteRead(fd, DRM_AMDGPU_CTX, &ctx_args, sizeof(ctx_args));
158 return ret;
159 }
160
sdma_finish(struct amdgpu_priv * priv,int fd)161 static void sdma_finish(struct amdgpu_priv *priv, int fd)
162 {
163 union drm_amdgpu_ctx ctx_args = { { 0 } };
164 struct drm_amdgpu_gem_va va_args = { 0 };
165 struct drm_gem_close gem_close = { 0 };
166
167 if (!priv->sdma_cmdbuf_map)
168 return;
169
170 va_args.handle = priv->sdma_cmdbuf_bo;
171 va_args.operation = AMDGPU_VA_OP_UNMAP;
172 va_args.flags = 0;
173 va_args.va_address = priv->sdma_cmdbuf_addr;
174 va_args.offset_in_bo = 0;
175 va_args.map_size = priv->sdma_cmdbuf_size;
176 drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
177
178 gem_close.handle = priv->sdma_cmdbuf_bo;
179 drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
180
181 ctx_args.in.op = AMDGPU_CTX_OP_FREE_CTX;
182 ctx_args.in.ctx_id = priv->sdma_ctx;
183 drmCommandWriteRead(fd, DRM_AMDGPU_CTX, &ctx_args, sizeof(ctx_args));
184 }
185
sdma_copy(struct amdgpu_priv * priv,int fd,uint32_t src_handle,uint32_t dst_handle,uint64_t size)186 static int sdma_copy(struct amdgpu_priv *priv, int fd, uint32_t src_handle, uint32_t dst_handle,
187 uint64_t size)
188 {
189 const uint64_t max_size_per_cmd = 0x3fff00;
190 const uint32_t cmd_size = 7 * sizeof(uint32_t); /* 7 dwords, see loop below. */
191 const uint64_t max_commands = priv->sdma_cmdbuf_size / cmd_size;
192 uint64_t src_addr = priv->sdma_cmdbuf_addr + priv->sdma_cmdbuf_size;
193 uint64_t dst_addr = src_addr + size;
194 struct drm_amdgpu_gem_va va_args = { 0 };
195 unsigned cmd = 0;
196 uint64_t remaining_size = size;
197 uint64_t cur_src_addr = src_addr;
198 uint64_t cur_dst_addr = dst_addr;
199 struct drm_amdgpu_cs_chunk_ib ib = { 0 };
200 struct drm_amdgpu_cs_chunk chunks[2] = { { 0 } };
201 uint64_t chunk_ptrs[2];
202 union drm_amdgpu_cs cs = { { 0 } };
203 struct drm_amdgpu_bo_list_in bo_list = { 0 };
204 struct drm_amdgpu_bo_list_entry bo_list_entries[3] = { { 0 } };
205 union drm_amdgpu_wait_cs wait_cs = { { 0 } };
206 int ret = 0;
207
208 if (size > UINT64_MAX - max_size_per_cmd ||
209 DIV_ROUND_UP(size, max_size_per_cmd) > max_commands)
210 return -ENOMEM;
211
212 /* Map both buffers into the GPU address space so we can access them from the GPU. */
213 va_args.handle = src_handle;
214 va_args.operation = AMDGPU_VA_OP_MAP;
215 va_args.flags = AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_DELAY_UPDATE;
216 va_args.va_address = src_addr;
217 va_args.map_size = size;
218
219 ret = drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
220 if (ret)
221 return ret;
222
223 va_args.handle = dst_handle;
224 va_args.flags = AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE | AMDGPU_VM_DELAY_UPDATE;
225 va_args.va_address = dst_addr;
226
227 ret = drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
228 if (ret)
229 goto unmap_src;
230
231 while (remaining_size) {
232 uint64_t cur_size = remaining_size;
233 if (cur_size > max_size_per_cmd)
234 cur_size = max_size_per_cmd;
235
236 priv->sdma_cmdbuf_map[cmd++] = 0x01; /* linear copy */
237 priv->sdma_cmdbuf_map[cmd++] =
238 priv->dev_info.family >= AMDGPU_FAMILY_AI ? (cur_size - 1) : cur_size;
239 priv->sdma_cmdbuf_map[cmd++] = 0;
240 priv->sdma_cmdbuf_map[cmd++] = cur_src_addr;
241 priv->sdma_cmdbuf_map[cmd++] = cur_src_addr >> 32;
242 priv->sdma_cmdbuf_map[cmd++] = cur_dst_addr;
243 priv->sdma_cmdbuf_map[cmd++] = cur_dst_addr >> 32;
244
245 remaining_size -= cur_size;
246 cur_src_addr += cur_size;
247 cur_dst_addr += cur_size;
248 }
249
250 ib.va_start = priv->sdma_cmdbuf_addr;
251 ib.ib_bytes = cmd * 4;
252 ib.ip_type = AMDGPU_HW_IP_DMA;
253
254 chunks[1].chunk_id = AMDGPU_CHUNK_ID_IB;
255 chunks[1].length_dw = sizeof(ib) / 4;
256 chunks[1].chunk_data = (uintptr_t)&ib;
257
258 bo_list_entries[0].bo_handle = priv->sdma_cmdbuf_bo;
259 bo_list_entries[0].bo_priority = 8; /* Middle of range, like RADV. */
260 bo_list_entries[1].bo_handle = src_handle;
261 bo_list_entries[1].bo_priority = 8;
262 bo_list_entries[2].bo_handle = dst_handle;
263 bo_list_entries[2].bo_priority = 8;
264
265 bo_list.bo_number = 3;
266 bo_list.bo_info_size = sizeof(bo_list_entries[0]);
267 bo_list.bo_info_ptr = (uintptr_t)bo_list_entries;
268
269 chunks[0].chunk_id = AMDGPU_CHUNK_ID_BO_HANDLES;
270 chunks[0].length_dw = sizeof(bo_list) / 4;
271 chunks[0].chunk_data = (uintptr_t)&bo_list;
272
273 chunk_ptrs[0] = (uintptr_t)&chunks[0];
274 chunk_ptrs[1] = (uintptr_t)&chunks[1];
275
276 cs.in.ctx_id = priv->sdma_ctx;
277 cs.in.num_chunks = 2;
278 cs.in.chunks = (uintptr_t)chunk_ptrs;
279
280 ret = drmCommandWriteRead(fd, DRM_AMDGPU_CS, &cs, sizeof(cs));
281 if (ret) {
282 drv_loge("SDMA copy command buffer submission failed %d\n", ret);
283 goto unmap_dst;
284 }
285
286 wait_cs.in.handle = cs.out.handle;
287 wait_cs.in.ip_type = AMDGPU_HW_IP_DMA;
288 wait_cs.in.ctx_id = priv->sdma_ctx;
289 wait_cs.in.timeout = INT64_MAX;
290
291 ret = drmCommandWriteRead(fd, DRM_AMDGPU_WAIT_CS, &wait_cs, sizeof(wait_cs));
292 if (ret) {
293 drv_loge("Could not wait for CS to finish\n");
294 } else if (wait_cs.out.status) {
295 drv_loge("Infinite wait timed out, likely GPU hang.\n");
296 ret = -ENODEV;
297 }
298
299 unmap_dst:
300 va_args.handle = dst_handle;
301 va_args.operation = AMDGPU_VA_OP_UNMAP;
302 va_args.flags = AMDGPU_VM_DELAY_UPDATE;
303 va_args.va_address = dst_addr;
304 drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
305
306 unmap_src:
307 va_args.handle = src_handle;
308 va_args.operation = AMDGPU_VA_OP_UNMAP;
309 va_args.flags = AMDGPU_VM_DELAY_UPDATE;
310 va_args.va_address = src_addr;
311 drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
312
313 return ret;
314 }
315
is_modifier_scanout_capable(struct amdgpu_priv * priv,uint32_t format,uint64_t modifier)316 static bool is_modifier_scanout_capable(struct amdgpu_priv *priv, uint32_t format,
317 uint64_t modifier)
318 {
319 unsigned bytes_per_pixel = drv_stride_from_format(format, 1, 0);
320
321 if (modifier == DRM_FORMAT_MOD_LINEAR)
322 return true;
323
324 if ((modifier >> 56) != DRM_FORMAT_MOD_VENDOR_AMD)
325 return false;
326
327 unsigned swizzle = AMD_FMT_MOD_GET(TILE, modifier);
328 if (priv->dev_info.family >= AMDGPU_FAMILY_RV) { /* DCN based GPUs */
329 /* D swizzle only supported for 64 bpp */
330 if ((swizzle & 3) == 2 && bytes_per_pixel != 8)
331 return false;
332
333 /* S swizzle not supported for 64 bpp */
334 if ((swizzle & 3) == 1 && bytes_per_pixel == 8)
335 return false;
336 } else { /* DCE based GPUs with GFX9 based modifier swizzling. */
337 assert(priv->dev_info.family == AMDGPU_FAMILY_AI);
338 /* Only D swizzles are allowed for display */
339 if ((swizzle & 3) != 2)
340 return false;
341 }
342
343 if (AMD_FMT_MOD_GET(DCC, modifier) &&
344 (AMD_FMT_MOD_GET(DCC_PIPE_ALIGN, modifier) || !AMD_FMT_MOD_GET(DCC_RETILE, modifier)))
345 return false;
346 return true;
347 }
348
amdgpu_preload(bool load)349 static void amdgpu_preload(bool load)
350 {
351 static void *handle;
352
353 if (load && !handle)
354 handle = dri_dlopen(DRI_PATH);
355 else if (!load && handle) {
356 dri_dlclose(handle);
357 handle = NULL;
358 }
359 }
360
amdgpu_init(struct driver * drv)361 static int amdgpu_init(struct driver *drv)
362 {
363 struct amdgpu_priv *priv;
364 drmVersionPtr drm_version;
365 struct format_metadata metadata;
366 uint64_t use_flags = BO_USE_RENDER_MASK;
367
368 priv = calloc(1, sizeof(struct amdgpu_priv));
369 if (!priv)
370 return -ENOMEM;
371
372 drm_version = drmGetVersion(drv_get_fd(drv));
373 if (!drm_version) {
374 free(priv);
375 return -ENODEV;
376 }
377
378 priv->drm_version = drm_version->version_minor;
379 drmFreeVersion(drm_version);
380
381 drv->priv = priv;
382
383 if (query_dev_info(drv_get_fd(drv), &priv->dev_info)) {
384 free(priv);
385 drv->priv = NULL;
386 return -ENODEV;
387 }
388 if (dri_init(drv, DRI_PATH, "radeonsi")) {
389 free(priv);
390 drv->priv = NULL;
391 return -ENODEV;
392 }
393
394 /* Continue on failure, as we can still succesfully map things without SDMA. */
395 if (sdma_init(priv, drv_get_fd(drv)))
396 drv_loge("SDMA init failed\n");
397
398 metadata.tiling = TILE_TYPE_LINEAR;
399 metadata.priority = 1;
400 metadata.modifier = DRM_FORMAT_MOD_LINEAR;
401
402 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
403 &metadata, use_flags);
404
405 drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
406 &metadata, BO_USE_TEXTURE_MASK);
407
408 /* NV12 format for camera, display, decoding and encoding. */
409 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
410 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SCANOUT |
411 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER |
412 BO_USE_PROTECTED);
413
414 drv_modify_combination(drv, DRM_FORMAT_P010, &metadata,
415 BO_USE_SCANOUT | BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER |
416 BO_USE_PROTECTED);
417
418 /* Android CTS tests require this. */
419 drv_add_combination(drv, DRM_FORMAT_BGR888, &metadata, BO_USE_SW_MASK);
420
421 /* Linear formats supported by display. */
422 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
423 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
424 drv_modify_combination(drv, DRM_FORMAT_ABGR8888, &metadata, BO_USE_SCANOUT);
425 drv_modify_combination(drv, DRM_FORMAT_XBGR8888, &metadata, BO_USE_SCANOUT);
426 drv_modify_combination(drv, DRM_FORMAT_RGB565, &metadata, BO_USE_SCANOUT);
427
428 drv_modify_combination(drv, DRM_FORMAT_ABGR2101010, &metadata, BO_USE_SCANOUT);
429 drv_modify_combination(drv, DRM_FORMAT_ARGB2101010, &metadata, BO_USE_SCANOUT);
430 drv_modify_combination(drv, DRM_FORMAT_XBGR2101010, &metadata, BO_USE_SCANOUT);
431 drv_modify_combination(drv, DRM_FORMAT_XRGB2101010, &metadata, BO_USE_SCANOUT);
432
433 drv_modify_combination(drv, DRM_FORMAT_NV21, &metadata, BO_USE_SCANOUT);
434
435 /*
436 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
437 * from camera and input/output from hardware decoder/encoder.
438 */
439 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
440 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
441 BO_USE_HW_VIDEO_ENCODER | BO_USE_GPU_DATA_BUFFER |
442 BO_USE_SENSOR_DIRECT_DATA);
443
444 /*
445 * The following formats will be allocated by the DRI backend and may be potentially tiled.
446 * Since format modifier support hasn't been implemented fully yet, it's not
447 * possible to enumerate the different types of buffers (like i915 can).
448 */
449 use_flags &= ~BO_USE_RENDERSCRIPT;
450 use_flags &= ~BO_USE_SW_WRITE_OFTEN;
451 use_flags &= ~BO_USE_SW_READ_OFTEN;
452 #if __ANDROID__
453 use_flags &= ~BO_USE_SW_WRITE_RARELY;
454 use_flags &= ~BO_USE_SW_READ_RARELY;
455 #endif
456 use_flags &= ~BO_USE_LINEAR;
457
458 metadata.priority = 2;
459
460 for (unsigned f = 0; f < ARRAY_SIZE(render_target_formats); ++f) {
461 uint32_t format = render_target_formats[f];
462 int mod_cnt;
463 if (dri_query_modifiers(drv, format, 0, NULL, &mod_cnt) && mod_cnt) {
464 uint64_t *modifiers = calloc(mod_cnt, sizeof(uint64_t));
465 dri_query_modifiers(drv, format, mod_cnt, modifiers, &mod_cnt);
466 metadata.tiling = TILE_TYPE_DRI_MODIFIER;
467 for (int i = 0; i < mod_cnt; ++i) {
468 bool scanout =
469 is_modifier_scanout_capable(drv->priv, format, modifiers[i]);
470
471 /* LINEAR will be handled using the LINEAR metadata. */
472 if (modifiers[i] == DRM_FORMAT_MOD_LINEAR)
473 continue;
474
475 /* The virtgpu minigbm can't handle auxiliary planes in the host. */
476 if (dri_num_planes_from_modifier(drv, format, modifiers[i]) !=
477 drv_num_planes_from_format(format))
478 continue;
479
480 metadata.modifier = modifiers[i];
481 drv_add_combination(drv, format, &metadata,
482 use_flags | (scanout ? BO_USE_SCANOUT : 0));
483 }
484 free(modifiers);
485 } else {
486 bool scanout = false;
487 switch (format) {
488 case DRM_FORMAT_ARGB8888:
489 case DRM_FORMAT_XRGB8888:
490 case DRM_FORMAT_ABGR8888:
491 case DRM_FORMAT_XBGR8888:
492 case DRM_FORMAT_ABGR2101010:
493 case DRM_FORMAT_ARGB2101010:
494 case DRM_FORMAT_XBGR2101010:
495 case DRM_FORMAT_XRGB2101010:
496 scanout = true;
497 break;
498 default:
499 break;
500 }
501 metadata.tiling = TILE_TYPE_DRI;
502 drv_add_combination(drv, format, &metadata,
503 use_flags | (scanout ? BO_USE_SCANOUT : 0));
504 }
505 }
506 return 0;
507 }
508
amdgpu_close(struct driver * drv)509 static void amdgpu_close(struct driver *drv)
510 {
511 sdma_finish(drv->priv, drv_get_fd(drv));
512 dri_close(drv);
513 free(drv->priv);
514 drv->priv = NULL;
515 }
516
amdgpu_create_bo_linear(struct bo * bo,uint32_t width,uint32_t height,uint32_t format,uint64_t use_flags)517 static int amdgpu_create_bo_linear(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
518 uint64_t use_flags)
519 {
520 int ret;
521 bool need_align = false;
522 uint32_t stride_align = 1;
523 uint32_t stride;
524 union drm_amdgpu_gem_create gem_create = { { 0 } };
525 struct amdgpu_priv *priv = bo->drv->priv;
526
527 stride = drv_stride_from_format(format, width, 0);
528
529 /* some clients (e.g., virtio-wl) set BO_USE_LINEAR to mean
530 * BO_USE_SCANOUT or BO_USE_TEXTURE
531 */
532 need_align = use_flags & (BO_USE_HW_MASK | BO_USE_LINEAR);
533
534 #if defined(ANDROID) && ANDROID_API_LEVEL < 30
535 /* work around
536 * android.hardware.camera2.cts.ImageWriterTest#testYuvImageWriterReaderOperation
537 * failure before R
538 */
539 need_align = true;
540 #endif
541
542 if (need_align) {
543 /* GFX9+ requires the stride to be aligned to 256 bytes */
544 stride_align = 256;
545 stride = ALIGN(stride, stride_align);
546
547 /* Android YV12 requires the UV stride to be half of the Y
548 * stride. Before GFX10, we can double the alignment for the
549 * Y stride, which makes sure the UV stride is still aligned
550 * to 256 bytes after halving.
551 *
552 * GFX10+ additionally requires the stride to be as small as
553 * possible. It is impossible to support the format in some
554 * cases. Instead, we force DRM_FORMAT_YVU420 and knowingly
555 * vioate Android YV12 stride requirement. This is done
556 * because
557 *
558 * - we would like to know what breaks, and
559 * - when used as a classic resource by virglrenderer, the
560 * requirement hopefully does not matter
561 */
562 bool double_align = format == DRM_FORMAT_YVU420_ANDROID;
563 if (double_align && priv->dev_info.family >= AMDGPU_FAMILY_NV &&
564 (use_flags & BO_USE_GPU_HW) && ((stride / stride_align) & 1)) {
565 drv_loge("allocating %dx%d YV12 bo (usage 0x%" PRIx64 ") with bad strides",
566 width, height, use_flags);
567 format = DRM_FORMAT_YVU420;
568 double_align = false;
569 }
570 if (double_align)
571 stride = ALIGN(stride, stride_align * 2);
572 }
573
574 /*
575 * Currently, allocator used by chrome aligns the height for Encoder/
576 * Decoder buffers while allocator used by android(gralloc/minigbm)
577 * doesn't provide any aligment.
578 *
579 * See b/153130069
580 */
581 if (use_flags & (BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER))
582 height = ALIGN(height, CHROME_HEIGHT_ALIGN);
583
584 drv_bo_from_format(bo, stride, stride_align, height, format);
585
586 gem_create.in.bo_size =
587 ALIGN(bo->meta.total_size, priv->dev_info.virtual_address_alignment);
588 gem_create.in.alignment = 256;
589 gem_create.in.domain_flags = 0;
590
591 if (use_flags & (BO_USE_LINEAR | BO_USE_SW_MASK))
592 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
593
594 gem_create.in.domains = AMDGPU_GEM_DOMAIN_GTT;
595
596 /* Scanout in GTT requires USWC, otherwise try to use cachable memory
597 * for buffers that are read often, because uncacheable reads can be
598 * very slow. USWC should be faster on the GPU though. */
599 if ((use_flags & BO_USE_SCANOUT) || !(use_flags & BO_USE_SW_READ_OFTEN))
600 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_CPU_GTT_USWC;
601
602 /* For protected data Buffer needs to be allocated from TMZ */
603 if (use_flags & BO_USE_PROTECTED)
604 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_ENCRYPTED;
605
606 /* Allocate the buffer with the preferred heap. */
607 ret = drmCommandWriteRead(drv_get_fd(bo->drv), DRM_AMDGPU_GEM_CREATE, &gem_create,
608 sizeof(gem_create));
609 if (ret < 0)
610 return ret;
611
612 bo->handle.u32 = gem_create.out.handle;
613
614 bo->meta.format_modifier = DRM_FORMAT_MOD_LINEAR;
615
616 return 0;
617 }
618
amdgpu_create_bo(struct bo * bo,uint32_t width,uint32_t height,uint32_t format,uint64_t use_flags)619 static int amdgpu_create_bo(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
620 uint64_t use_flags)
621 {
622 struct combination *combo;
623 struct amdgpu_priv *priv = bo->drv->priv;
624
625 combo = drv_get_combination(bo->drv, format, use_flags);
626 if (!combo)
627 return -EINVAL;
628
629 if (combo->metadata.tiling == TILE_TYPE_DRI) {
630 // See b/122049612
631 if (use_flags & (BO_USE_SCANOUT) && priv->dev_info.family == AMDGPU_FAMILY_CZ) {
632 uint32_t bytes_per_pixel = drv_bytes_per_pixel_from_format(format, 0);
633 width = ALIGN(width, 256 / bytes_per_pixel);
634 }
635
636 return dri_bo_create(bo, width, height, format, use_flags);
637 } else if (combo->metadata.tiling == TILE_TYPE_DRI_MODIFIER) {
638 return dri_bo_create_with_modifiers(bo, width, height, format,
639 &combo->metadata.modifier, 1);
640 }
641
642 return amdgpu_create_bo_linear(bo, width, height, format, use_flags);
643 }
644
amdgpu_create_bo_with_modifiers(struct bo * bo,uint32_t width,uint32_t height,uint32_t format,const uint64_t * modifiers,uint32_t count)645 static int amdgpu_create_bo_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
646 uint32_t format, const uint64_t *modifiers,
647 uint32_t count)
648 {
649 bool only_use_linear = true;
650
651 for (uint32_t i = 0; i < count; ++i)
652 if (modifiers[i] != DRM_FORMAT_MOD_LINEAR)
653 only_use_linear = false;
654
655 if (only_use_linear)
656 return amdgpu_create_bo_linear(bo, width, height, format, BO_USE_SCANOUT);
657
658 return dri_bo_create_with_modifiers(bo, width, height, format, modifiers, count);
659 }
660
amdgpu_import_bo(struct bo * bo,struct drv_import_fd_data * data)661 static int amdgpu_import_bo(struct bo *bo, struct drv_import_fd_data *data)
662 {
663 bool dri_tiling = data->format_modifier != DRM_FORMAT_MOD_LINEAR;
664 if (data->format_modifier == DRM_FORMAT_MOD_INVALID) {
665 struct combination *combo;
666 combo = drv_get_combination(bo->drv, data->format, data->use_flags);
667 if (!combo)
668 return -EINVAL;
669
670 dri_tiling = combo->metadata.tiling != TILE_TYPE_LINEAR;
671 }
672
673 bo->meta.num_planes =
674 dri_num_planes_from_modifier(bo->drv, data->format, data->format_modifier);
675
676 if (dri_tiling)
677 return dri_bo_import(bo, data);
678 else
679 return drv_prime_bo_import(bo, data);
680 }
681
amdgpu_release_bo(struct bo * bo)682 static int amdgpu_release_bo(struct bo *bo)
683 {
684 if (bo->priv)
685 return dri_bo_release(bo);
686
687 return 0;
688 }
689
amdgpu_destroy_bo(struct bo * bo)690 static int amdgpu_destroy_bo(struct bo *bo)
691 {
692 if (bo->priv)
693 return dri_bo_destroy(bo);
694 else
695 return drv_gem_bo_destroy(bo);
696 }
697
amdgpu_map_bo(struct bo * bo,struct vma * vma,uint32_t map_flags)698 static void *amdgpu_map_bo(struct bo *bo, struct vma *vma, uint32_t map_flags)
699 {
700 void *addr = MAP_FAILED;
701 int ret;
702 union drm_amdgpu_gem_mmap gem_map = { { 0 } };
703 struct drm_amdgpu_gem_create_in bo_info = { 0 };
704 struct drm_amdgpu_gem_op gem_op = { 0 };
705 uint32_t handle = bo->handle.u32;
706 struct amdgpu_linear_vma_priv *priv = NULL;
707 struct amdgpu_priv *drv_priv;
708
709 if (bo->priv)
710 return dri_bo_map(bo, vma, 0, map_flags);
711
712 drv_priv = bo->drv->priv;
713 gem_op.handle = handle;
714 gem_op.op = AMDGPU_GEM_OP_GET_GEM_CREATE_INFO;
715 gem_op.value = (uintptr_t)&bo_info;
716
717 ret = drmCommandWriteRead(bo->drv->fd, DRM_AMDGPU_GEM_OP, &gem_op, sizeof(gem_op));
718 if (ret)
719 return MAP_FAILED;
720
721 vma->length = bo_info.bo_size;
722
723 if (((bo_info.domains & AMDGPU_GEM_DOMAIN_VRAM) ||
724 (bo_info.domain_flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)) &&
725 drv_priv->sdma_cmdbuf_map) {
726 union drm_amdgpu_gem_create gem_create = { { 0 } };
727
728 priv = calloc(1, sizeof(struct amdgpu_linear_vma_priv));
729 if (!priv)
730 return MAP_FAILED;
731
732 gem_create.in.bo_size = bo_info.bo_size;
733 gem_create.in.alignment = 4096;
734 gem_create.in.domains = AMDGPU_GEM_DOMAIN_GTT;
735
736 ret = drmCommandWriteRead(bo->drv->fd, DRM_AMDGPU_GEM_CREATE, &gem_create,
737 sizeof(gem_create));
738 if (ret < 0) {
739 drv_loge("GEM create failed\n");
740 free(priv);
741 return MAP_FAILED;
742 }
743
744 priv->map_flags = map_flags;
745 handle = priv->handle = gem_create.out.handle;
746
747 ret = sdma_copy(bo->drv->priv, bo->drv->fd, bo->handle.u32, priv->handle,
748 bo_info.bo_size);
749 if (ret) {
750 drv_loge("SDMA copy for read failed\n");
751 goto fail;
752 }
753 }
754
755 gem_map.in.handle = handle;
756 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_AMDGPU_GEM_MMAP, &gem_map);
757 if (ret) {
758 drv_loge("DRM_IOCTL_AMDGPU_GEM_MMAP failed\n");
759 goto fail;
760 }
761
762 addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
763 gem_map.out.addr_ptr);
764 if (addr == MAP_FAILED)
765 goto fail;
766
767 vma->priv = priv;
768 return addr;
769
770 fail:
771 if (priv) {
772 struct drm_gem_close gem_close = { 0 };
773 gem_close.handle = priv->handle;
774 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
775 free(priv);
776 }
777 return MAP_FAILED;
778 }
779
amdgpu_unmap_bo(struct bo * bo,struct vma * vma)780 static int amdgpu_unmap_bo(struct bo *bo, struct vma *vma)
781 {
782 if (bo->priv) {
783 return dri_bo_unmap(bo, vma);
784 } else {
785 int r = munmap(vma->addr, vma->length);
786 if (r)
787 return r;
788
789 if (vma->priv) {
790 struct amdgpu_linear_vma_priv *priv = vma->priv;
791 struct drm_gem_close gem_close = { 0 };
792
793 if (BO_MAP_WRITE & priv->map_flags) {
794 r = sdma_copy(bo->drv->priv, bo->drv->fd, priv->handle,
795 bo->handle.u32, vma->length);
796 if (r)
797 return r;
798 }
799
800 gem_close.handle = priv->handle;
801 r = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
802 }
803
804 return 0;
805 }
806 }
807
amdgpu_bo_invalidate(struct bo * bo,struct mapping * mapping)808 static int amdgpu_bo_invalidate(struct bo *bo, struct mapping *mapping)
809 {
810 int ret;
811 union drm_amdgpu_gem_wait_idle wait_idle = { { 0 } };
812
813 if (bo->priv)
814 return 0;
815
816 wait_idle.in.handle = bo->handle.u32;
817 wait_idle.in.timeout = AMDGPU_TIMEOUT_INFINITE;
818
819 ret = drmCommandWriteRead(bo->drv->fd, DRM_AMDGPU_GEM_WAIT_IDLE, &wait_idle,
820 sizeof(wait_idle));
821
822 if (ret < 0) {
823 drv_loge("DRM_AMDGPU_GEM_WAIT_IDLE failed with %d\n", ret);
824 return ret;
825 }
826
827 if (ret == 0 && wait_idle.out.status)
828 drv_loge("DRM_AMDGPU_GEM_WAIT_IDLE BO is busy\n");
829
830 return 0;
831 }
832
833 const struct backend backend_amdgpu = {
834 .name = "amdgpu",
835 .preload = amdgpu_preload,
836 .init = amdgpu_init,
837 .close = amdgpu_close,
838 .bo_create = amdgpu_create_bo,
839 .bo_create_with_modifiers = amdgpu_create_bo_with_modifiers,
840 .bo_release = amdgpu_release_bo,
841 .bo_destroy = amdgpu_destroy_bo,
842 .bo_import = amdgpu_import_bo,
843 .bo_map = amdgpu_map_bo,
844 .bo_unmap = amdgpu_unmap_bo,
845 .bo_invalidate = amdgpu_bo_invalidate,
846 .resolve_format_and_use_flags = drv_resolve_format_and_use_flags_helper,
847 .num_planes_from_modifier = dri_num_planes_from_modifier,
848 };
849
850 #endif
851