blob: b2da076f1688f9e69a72b57a305c4ffce0ec3b4c [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>
Akshu Agrawal0337d9b2016-07-28 15:35:45 +05309#include <errno.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +053013#include <sys/mman.h>
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053014#include <xf86drm.h>
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053015
Satyajitcdcebd82018-01-12 14:49:05 +053016#include "dri.h"
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053017#include "drv_priv.h"
18#include "helpers.h"
19#include "util.h"
20
Gurchetan Singhcf9ed9d2019-12-13 09:37:01 -080021// clang-format off
22#define DRI_PATH STRINGIZE(DRI_DRIVER_DIR/radeonsi_dri.so)
23// clang-format on
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053024
Satyajitcdcebd82018-01-12 14:49:05 +053025#define TILE_TYPE_LINEAR 0
26/* DRI backend decides tiling in this case. */
27#define TILE_TYPE_DRI 1
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053028
Ikshwaku Chauhan047df2b2020-06-29 16:44:57 +053029/* Height alignement for Encoder/Decoder buffers */
30#define CHROME_HEIGHT_ALIGN 16
31
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +010032struct amdgpu_priv {
Satyajitcdcebd82018-01-12 14:49:05 +053033 struct dri_driver dri;
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +010034 int drm_version;
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +020035
36 /* sdma */
37 struct drm_amdgpu_info_device dev_info;
38 uint32_t sdma_ctx;
39 uint32_t sdma_cmdbuf_bo;
40 uint64_t sdma_cmdbuf_addr;
41 uint64_t sdma_cmdbuf_size;
42 uint32_t *sdma_cmdbuf_map;
43};
44
45struct amdgpu_linear_vma_priv {
46 uint32_t handle;
47 uint32_t map_flags;
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +010048};
49
Bas Nieuwenhuizenb16076b2020-06-29 12:41:18 +020050const static uint32_t render_target_formats[] = {
51 DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888, DRM_FORMAT_RGB565,
52 DRM_FORMAT_XBGR8888, DRM_FORMAT_XRGB8888, DRM_FORMAT_ABGR2101010,
53 DRM_FORMAT_ARGB2101010, DRM_FORMAT_XBGR2101010, DRM_FORMAT_XRGB2101010,
54};
Gurchetan Singh179687e2016-10-28 10:07:35 -070055
Bas Nieuwenhuizenb5e0f2d2020-09-29 16:02:18 +020056const static uint32_t texture_source_formats[] = {
57 DRM_FORMAT_GR88, DRM_FORMAT_R8, DRM_FORMAT_NV21, DRM_FORMAT_NV12,
58 DRM_FORMAT_YVU420_ANDROID, DRM_FORMAT_YVU420, DRM_FORMAT_P010
59};
Shirish Sdf423df2017-04-18 16:21:59 +053060
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +020061static int query_dev_info(int fd, struct drm_amdgpu_info_device *dev_info)
62{
63 struct drm_amdgpu_info info_args = { 0 };
64
65 info_args.return_pointer = (uintptr_t)dev_info;
66 info_args.return_size = sizeof(*dev_info);
67 info_args.query = AMDGPU_INFO_DEV_INFO;
68
69 return drmCommandWrite(fd, DRM_AMDGPU_INFO, &info_args, sizeof(info_args));
70}
71
72static int sdma_init(struct amdgpu_priv *priv, int fd)
73{
74 union drm_amdgpu_ctx ctx_args = { { 0 } };
75 union drm_amdgpu_gem_create gem_create = { { 0 } };
76 struct drm_amdgpu_gem_va va_args = { 0 };
77 union drm_amdgpu_gem_mmap gem_map = { { 0 } };
78 struct drm_gem_close gem_close = { 0 };
79 int ret;
80
81 /* Ensure we can make a submission without BO lists. */
82 if (priv->drm_version < 27)
83 return 0;
84
85 /* Anything outside this range needs adjustments to the SDMA copy commands */
86 if (priv->dev_info.family < AMDGPU_FAMILY_CI || priv->dev_info.family > AMDGPU_FAMILY_NV)
87 return 0;
88
89 ctx_args.in.op = AMDGPU_CTX_OP_ALLOC_CTX;
90
91 ret = drmCommandWriteRead(fd, DRM_AMDGPU_CTX, &ctx_args, sizeof(ctx_args));
92 if (ret < 0)
93 return ret;
94
95 priv->sdma_ctx = ctx_args.out.alloc.ctx_id;
96
97 priv->sdma_cmdbuf_size = ALIGN(4096, priv->dev_info.virtual_address_alignment);
98 gem_create.in.bo_size = priv->sdma_cmdbuf_size;
99 gem_create.in.alignment = 4096;
100 gem_create.in.domains = AMDGPU_GEM_DOMAIN_GTT;
101
102 ret = drmCommandWriteRead(fd, DRM_AMDGPU_GEM_CREATE, &gem_create, sizeof(gem_create));
103 if (ret < 0)
104 goto fail_ctx;
105
106 priv->sdma_cmdbuf_bo = gem_create.out.handle;
107
108 priv->sdma_cmdbuf_addr =
109 ALIGN(priv->dev_info.virtual_address_offset, priv->dev_info.virtual_address_alignment);
110
111 /* Map the buffer into the GPU address space so we can use it from the GPU */
112 va_args.handle = priv->sdma_cmdbuf_bo;
113 va_args.operation = AMDGPU_VA_OP_MAP;
114 va_args.flags = AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_EXECUTABLE;
115 va_args.va_address = priv->sdma_cmdbuf_addr;
116 va_args.offset_in_bo = 0;
117 va_args.map_size = priv->sdma_cmdbuf_size;
118
119 ret = drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
120 if (ret)
121 goto fail_bo;
122
123 gem_map.in.handle = priv->sdma_cmdbuf_bo;
124 ret = drmIoctl(fd, DRM_IOCTL_AMDGPU_GEM_MMAP, &gem_map);
125 if (ret)
126 goto fail_va;
127
128 priv->sdma_cmdbuf_map = mmap(0, priv->sdma_cmdbuf_size, PROT_READ | PROT_WRITE, MAP_SHARED,
129 fd, gem_map.out.addr_ptr);
130 if (priv->sdma_cmdbuf_map == MAP_FAILED) {
131 priv->sdma_cmdbuf_map = NULL;
132 ret = -ENOMEM;
133 goto fail_va;
134 }
135
136 return 0;
137fail_va:
138 va_args.operation = AMDGPU_VA_OP_UNMAP;
139 va_args.flags = 0;
140 drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
141fail_bo:
142 gem_close.handle = priv->sdma_cmdbuf_bo;
143 drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
144fail_ctx:
145 memset(&ctx_args, 0, sizeof(ctx_args));
146 ctx_args.in.op = AMDGPU_CTX_OP_FREE_CTX;
147 ctx_args.in.ctx_id = priv->sdma_ctx;
148 drmCommandWriteRead(fd, DRM_AMDGPU_CTX, &ctx_args, sizeof(ctx_args));
149 return ret;
150}
151
152static void sdma_finish(struct amdgpu_priv *priv, int fd)
153{
154 union drm_amdgpu_ctx ctx_args = { { 0 } };
155 struct drm_amdgpu_gem_va va_args = { 0 };
156 struct drm_gem_close gem_close = { 0 };
157
158 if (!priv->sdma_cmdbuf_map)
159 return;
160
161 va_args.handle = priv->sdma_cmdbuf_bo;
162 va_args.operation = AMDGPU_VA_OP_UNMAP;
163 va_args.flags = 0;
164 va_args.va_address = priv->sdma_cmdbuf_addr;
165 va_args.offset_in_bo = 0;
166 va_args.map_size = priv->sdma_cmdbuf_size;
167 drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
168
169 gem_close.handle = priv->sdma_cmdbuf_bo;
170 drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
171
172 ctx_args.in.op = AMDGPU_CTX_OP_FREE_CTX;
173 ctx_args.in.ctx_id = priv->sdma_ctx;
174 drmCommandWriteRead(fd, DRM_AMDGPU_CTX, &ctx_args, sizeof(ctx_args));
175}
176
177static int sdma_copy(struct amdgpu_priv *priv, int fd, uint32_t src_handle, uint32_t dst_handle,
178 uint64_t size)
179{
180 const uint64_t max_size_per_cmd = 0x3fff00;
181 const uint32_t cmd_size = 7 * sizeof(uint32_t); /* 7 dwords, see loop below. */
182 const uint64_t max_commands = priv->sdma_cmdbuf_size / cmd_size;
183 uint64_t src_addr = priv->sdma_cmdbuf_addr + priv->sdma_cmdbuf_size;
184 uint64_t dst_addr = src_addr + size;
185 struct drm_amdgpu_gem_va va_args = { 0 };
186 unsigned cmd = 0;
187 uint64_t remaining_size = size;
188 uint64_t cur_src_addr = src_addr;
189 uint64_t cur_dst_addr = dst_addr;
190 struct drm_amdgpu_cs_chunk_ib ib = { 0 };
191 struct drm_amdgpu_cs_chunk chunks[2] = { { 0 } };
192 uint64_t chunk_ptrs[2];
193 union drm_amdgpu_cs cs = { { 0 } };
194 struct drm_amdgpu_bo_list_in bo_list = { 0 };
195 struct drm_amdgpu_bo_list_entry bo_list_entries[3] = { { 0 } };
196 union drm_amdgpu_wait_cs wait_cs = { { 0 } };
197 int ret = 0;
198
199 if (size > UINT64_MAX - max_size_per_cmd ||
200 DIV_ROUND_UP(size, max_size_per_cmd) > max_commands)
201 return -ENOMEM;
202
203 /* Map both buffers into the GPU address space so we can access them from the GPU. */
204 va_args.handle = src_handle;
205 va_args.operation = AMDGPU_VA_OP_MAP;
206 va_args.flags = AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_DELAY_UPDATE;
207 va_args.va_address = src_addr;
208 va_args.map_size = size;
209
210 ret = drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
211 if (ret)
212 return ret;
213
214 va_args.handle = dst_handle;
215 va_args.flags = AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE | AMDGPU_VM_DELAY_UPDATE;
216 va_args.va_address = dst_addr;
217
218 ret = drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
219 if (ret)
220 goto unmap_src;
221
222 while (remaining_size) {
223 uint64_t cur_size = remaining_size;
224 if (cur_size > max_size_per_cmd)
225 cur_size = max_size_per_cmd;
226
227 priv->sdma_cmdbuf_map[cmd++] = 0x01; /* linear copy */
228 priv->sdma_cmdbuf_map[cmd++] =
229 priv->dev_info.family >= AMDGPU_FAMILY_AI ? (cur_size - 1) : cur_size;
230 priv->sdma_cmdbuf_map[cmd++] = 0;
231 priv->sdma_cmdbuf_map[cmd++] = cur_src_addr;
232 priv->sdma_cmdbuf_map[cmd++] = cur_src_addr >> 32;
233 priv->sdma_cmdbuf_map[cmd++] = cur_dst_addr;
234 priv->sdma_cmdbuf_map[cmd++] = cur_dst_addr >> 32;
235
236 remaining_size -= cur_size;
237 cur_src_addr += cur_size;
238 cur_dst_addr += cur_size;
239 }
240
241 ib.va_start = priv->sdma_cmdbuf_addr;
242 ib.ib_bytes = cmd * 4;
243 ib.ip_type = AMDGPU_HW_IP_DMA;
244
245 chunks[1].chunk_id = AMDGPU_CHUNK_ID_IB;
246 chunks[1].length_dw = sizeof(ib) / 4;
247 chunks[1].chunk_data = (uintptr_t)&ib;
248
249 bo_list_entries[0].bo_handle = priv->sdma_cmdbuf_bo;
250 bo_list_entries[0].bo_priority = 8; /* Middle of range, like RADV. */
251 bo_list_entries[1].bo_handle = src_handle;
252 bo_list_entries[1].bo_priority = 8;
253 bo_list_entries[2].bo_handle = dst_handle;
254 bo_list_entries[2].bo_priority = 8;
255
256 bo_list.bo_number = 3;
257 bo_list.bo_info_size = sizeof(bo_list_entries[0]);
258 bo_list.bo_info_ptr = (uintptr_t)bo_list_entries;
259
260 chunks[0].chunk_id = AMDGPU_CHUNK_ID_BO_HANDLES;
261 chunks[0].length_dw = sizeof(bo_list) / 4;
262 chunks[0].chunk_data = (uintptr_t)&bo_list;
263
264 chunk_ptrs[0] = (uintptr_t)&chunks[0];
265 chunk_ptrs[1] = (uintptr_t)&chunks[1];
266
267 cs.in.ctx_id = priv->sdma_ctx;
268 cs.in.num_chunks = 2;
269 cs.in.chunks = (uintptr_t)chunk_ptrs;
270
271 ret = drmCommandWriteRead(fd, DRM_AMDGPU_CS, &cs, sizeof(cs));
272 if (ret) {
273 drv_log("SDMA copy command buffer submission failed %d\n", ret);
274 goto unmap_dst;
275 }
276
277 wait_cs.in.handle = cs.out.handle;
278 wait_cs.in.ip_type = AMDGPU_HW_IP_DMA;
279 wait_cs.in.ctx_id = priv->sdma_ctx;
280 wait_cs.in.timeout = INT64_MAX;
281
282 ret = drmCommandWriteRead(fd, DRM_AMDGPU_WAIT_CS, &wait_cs, sizeof(wait_cs));
283 if (ret) {
284 drv_log("Could not wait for CS to finish\n");
285 } else if (wait_cs.out.status) {
286 drv_log("Infinite wait timed out, likely GPU hang.\n");
287 ret = -ENODEV;
288 }
289
290unmap_dst:
291 va_args.handle = dst_handle;
292 va_args.operation = AMDGPU_VA_OP_UNMAP;
293 va_args.flags = AMDGPU_VM_DELAY_UPDATE;
294 va_args.va_address = dst_addr;
295 drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
296
297unmap_src:
298 va_args.handle = src_handle;
299 va_args.operation = AMDGPU_VA_OP_UNMAP;
300 va_args.flags = AMDGPU_VM_DELAY_UPDATE;
301 va_args.va_address = src_addr;
302 drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
303
304 return ret;
305}
306
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530307static int amdgpu_init(struct driver *drv)
308{
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +0100309 struct amdgpu_priv *priv;
310 drmVersionPtr drm_version;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800311 struct format_metadata metadata;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700312 uint64_t use_flags = BO_USE_RENDER_MASK;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530313
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +0100314 priv = calloc(1, sizeof(struct amdgpu_priv));
315 if (!priv)
Satyajitcdcebd82018-01-12 14:49:05 +0530316 return -ENOMEM;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530317
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +0100318 drm_version = drmGetVersion(drv_get_fd(drv));
319 if (!drm_version) {
320 free(priv);
Satyajitcdcebd82018-01-12 14:49:05 +0530321 return -ENODEV;
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +0100322 }
323
324 priv->drm_version = drm_version->version_minor;
325 drmFreeVersion(drm_version);
326
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +0100327 drv->priv = priv;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530328
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200329 if (query_dev_info(drv_get_fd(drv), &priv->dev_info)) {
330 free(priv);
331 drv->priv = NULL;
332 return -ENODEV;
333 }
Satyajitcdcebd82018-01-12 14:49:05 +0530334 if (dri_init(drv, DRI_PATH, "radeonsi")) {
335 free(priv);
336 drv->priv = NULL;
337 return -ENODEV;
338 }
Shirish Sdf423df2017-04-18 16:21:59 +0530339
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800340 /* Continue on failure, as we can still succesfully map things without SDMA. */
341 if (sdma_init(priv, drv_get_fd(drv)))
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200342 drv_log("SDMA init failed\n");
343
Satyajitcdcebd82018-01-12 14:49:05 +0530344 metadata.tiling = TILE_TYPE_LINEAR;
345 metadata.priority = 1;
Kristian H. Kristensenbc8c5932017-10-24 18:36:32 -0700346 metadata.modifier = DRM_FORMAT_MOD_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800347
Gurchetan Singhd3001452017-11-03 17:18:36 -0700348 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
349 &metadata, use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800350
Satyajitcdcebd82018-01-12 14:49:05 +0530351 drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
352 &metadata, BO_USE_TEXTURE_MASK);
353
Hirokazu Honda3bd681c2020-06-23 17:52:20 +0900354 /* NV12 format for camera, display, decoding and encoding. */
355 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
356 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SCANOUT |
Ikshwaku Chauhan4b69e282021-01-28 23:56:12 +0530357 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER |
358 BO_USE_PROTECTED);
Hirokazu Honda3b8d4d02019-07-31 16:35:52 +0900359
Bas Nieuwenhuizenb5e0f2d2020-09-29 16:02:18 +0200360 drv_modify_combination(drv, DRM_FORMAT_P010, &metadata,
Ikshwaku Chauhan4b69e282021-01-28 23:56:12 +0530361 BO_USE_SCANOUT | BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER |
362 BO_USE_PROTECTED);
Bas Nieuwenhuizenb5e0f2d2020-09-29 16:02:18 +0200363
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700364 /* Android CTS tests require this. */
365 drv_add_combination(drv, DRM_FORMAT_BGR888, &metadata, BO_USE_SW_MASK);
366
Satyajitcdcebd82018-01-12 14:49:05 +0530367 /* Linear formats supported by display. */
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800368 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
369 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
Drew Davenport5d215242019-03-25 09:18:42 -0600370 drv_modify_combination(drv, DRM_FORMAT_ABGR8888, &metadata, BO_USE_SCANOUT);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800371 drv_modify_combination(drv, DRM_FORMAT_XBGR8888, &metadata, BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800372
Bas Nieuwenhuizenb16076b2020-06-29 12:41:18 +0200373 drv_modify_combination(drv, DRM_FORMAT_ABGR2101010, &metadata, BO_USE_SCANOUT);
374 drv_modify_combination(drv, DRM_FORMAT_ARGB2101010, &metadata, BO_USE_SCANOUT);
375 drv_modify_combination(drv, DRM_FORMAT_XBGR2101010, &metadata, BO_USE_SCANOUT);
376 drv_modify_combination(drv, DRM_FORMAT_XRGB2101010, &metadata, BO_USE_SCANOUT);
377
Satyajitcdcebd82018-01-12 14:49:05 +0530378 drv_modify_combination(drv, DRM_FORMAT_NV21, &metadata, BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800379
Satyajitcdcebd82018-01-12 14:49:05 +0530380 /*
381 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
David Stevens49518142020-06-15 13:48:48 +0900382 * from camera and input/output from hardware decoder/encoder.
Satyajitcdcebd82018-01-12 14:49:05 +0530383 */
384 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
David Stevens49518142020-06-15 13:48:48 +0900385 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
386 BO_USE_HW_VIDEO_ENCODER);
Satyajitcdcebd82018-01-12 14:49:05 +0530387
388 /*
389 * The following formats will be allocated by the DRI backend and may be potentially tiled.
390 * Since format modifier support hasn't been implemented fully yet, it's not
391 * possible to enumerate the different types of buffers (like i915 can).
392 */
393 use_flags &= ~BO_USE_RENDERSCRIPT;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700394 use_flags &= ~BO_USE_SW_WRITE_OFTEN;
395 use_flags &= ~BO_USE_SW_READ_OFTEN;
396 use_flags &= ~BO_USE_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800397
Satyajitcdcebd82018-01-12 14:49:05 +0530398 metadata.tiling = TILE_TYPE_DRI;
399 metadata.priority = 2;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800400
Gurchetan Singhd3001452017-11-03 17:18:36 -0700401 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
402 &metadata, use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800403
Satyajitcdcebd82018-01-12 14:49:05 +0530404 /* Potentially tiled formats supported by display. */
405 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
406 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
Bas Nieuwenhuizen582bdbf2019-04-03 18:12:12 +0200407 drv_modify_combination(drv, DRM_FORMAT_ABGR8888, &metadata, BO_USE_SCANOUT);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800408 drv_modify_combination(drv, DRM_FORMAT_XBGR8888, &metadata, BO_USE_SCANOUT);
Bas Nieuwenhuizenb16076b2020-06-29 12:41:18 +0200409
410 drv_modify_combination(drv, DRM_FORMAT_ABGR2101010, &metadata, BO_USE_SCANOUT);
411 drv_modify_combination(drv, DRM_FORMAT_ARGB2101010, &metadata, BO_USE_SCANOUT);
412 drv_modify_combination(drv, DRM_FORMAT_XBGR2101010, &metadata, BO_USE_SCANOUT);
413 drv_modify_combination(drv, DRM_FORMAT_XRGB2101010, &metadata, BO_USE_SCANOUT);
Gurchetan Singhd3001452017-11-03 17:18:36 -0700414 return 0;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530415}
416
417static void amdgpu_close(struct driver *drv)
418{
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200419 sdma_finish(drv->priv, drv_get_fd(drv));
Satyajitcdcebd82018-01-12 14:49:05 +0530420 dri_close(drv);
421 free(drv->priv);
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530422 drv->priv = NULL;
423}
424
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100425static int amdgpu_create_bo_linear(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
426 uint64_t use_flags)
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530427{
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530428 int ret;
Drew Davenport8db36fe2020-10-15 22:18:00 -0600429 size_t num_planes;
Satyajitcdcebd82018-01-12 14:49:05 +0530430 uint32_t plane, stride;
Gurchetan Singh99644382020-10-07 15:28:11 -0700431 union drm_amdgpu_gem_create gem_create = { { 0 } };
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200432 struct amdgpu_priv *priv = bo->drv->priv;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530433
Satyajitcdcebd82018-01-12 14:49:05 +0530434 stride = drv_stride_from_format(format, width, 0);
Drew Davenport8db36fe2020-10-15 22:18:00 -0600435 num_planes = drv_num_planes_from_format(format);
436
437 /*
438 * For multiplane formats, align the stride to 512 to ensure that subsample strides are 256
439 * aligned. This uses more memory than necessary since the first plane only needs to be
440 * 256 aligned, but it's acceptable for a short-term fix. It's probably safe for other gpu
441 * families, but let's restrict it to Raven for now (b/171013552).
442 * */
443 if (priv->dev_info.family == AMDGPU_FAMILY_RV && num_planes > 1)
444 stride = ALIGN(stride, 512);
445 else
446 stride = ALIGN(stride, 256);
Satyajitcdcebd82018-01-12 14:49:05 +0530447
Ikshwaku Chauhan047df2b2020-06-29 16:44:57 +0530448 /*
Gurchetan Singh9b4c8b72020-08-20 14:25:43 -0700449 * Currently, allocator used by chrome aligns the height for Encoder/
450 * Decoder buffers while allocator used by android(gralloc/minigbm)
451 * doesn't provide any aligment.
452 *
453 * See b/153130069
454 */
Ikshwaku Chauhan047df2b2020-06-29 16:44:57 +0530455 if (use_flags & (BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER))
456 height = ALIGN(height, CHROME_HEIGHT_ALIGN);
457
Satyajitcdcebd82018-01-12 14:49:05 +0530458 drv_bo_from_format(bo, stride, height, format);
Shirish Sdf423df2017-04-18 16:21:59 +0530459
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200460 gem_create.in.bo_size =
461 ALIGN(bo->meta.total_size, priv->dev_info.virtual_address_alignment);
Satyajitcdcebd82018-01-12 14:49:05 +0530462 gem_create.in.alignment = 256;
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800463 gem_create.in.domain_flags = 0;
Satyajitcdcebd82018-01-12 14:49:05 +0530464
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700465 if (use_flags & (BO_USE_LINEAR | BO_USE_SW_MASK))
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800466 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
467
Deepak Sharma62a9c4e2018-05-01 12:11:27 -0700468 gem_create.in.domains = AMDGPU_GEM_DOMAIN_GTT;
Bas Nieuwenhuizen4daf12c2020-06-04 23:11:27 +0200469
470 /* Scanout in GTT requires USWC, otherwise try to use cachable memory
471 * for buffers that are read often, because uncacheable reads can be
472 * very slow. USWC should be faster on the GPU though. */
473 if ((use_flags & BO_USE_SCANOUT) || !(use_flags & BO_USE_SW_READ_OFTEN))
Jao-ke Chin-Lee5481e3c2020-04-10 00:12:12 +0000474 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_CPU_GTT_USWC;
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800475
Ikshwaku Chauhan4b69e282021-01-28 23:56:12 +0530476 /* For protected data Buffer needs to be allocated from TMZ */
477 if (use_flags & BO_USE_PROTECTED)
478 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_ENCRYPTED;
479
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530480 /* Allocate the buffer with the preferred heap. */
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800481 ret = drmCommandWriteRead(drv_get_fd(bo->drv), DRM_AMDGPU_GEM_CREATE, &gem_create,
482 sizeof(gem_create));
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530483 if (ret < 0)
484 return ret;
485
Gurchetan Singh298b7572019-09-19 09:55:18 -0700486 for (plane = 0; plane < bo->meta.num_planes; plane++)
Shirish Sdf423df2017-04-18 16:21:59 +0530487 bo->handles[plane].u32 = gem_create.out.handle;
488
Gurchetan Singh52155b42021-01-27 17:55:17 -0800489 bo->meta.format_modifier = DRM_FORMAT_MOD_LINEAR;
Bas Nieuwenhuizen7119d332020-02-07 20:20:30 +0100490
Satyajitcdcebd82018-01-12 14:49:05 +0530491 return 0;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530492}
493
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100494static int amdgpu_create_bo(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
495 uint64_t use_flags)
Satyajitcdcebd82018-01-12 14:49:05 +0530496{
497 struct combination *combo;
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100498
499 combo = drv_get_combination(bo->drv, format, use_flags);
Satyajitcdcebd82018-01-12 14:49:05 +0530500 if (!combo)
501 return -EINVAL;
502
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100503 if (combo->metadata.tiling == TILE_TYPE_DRI) {
504 bool needs_alignment = false;
505#ifdef __ANDROID__
506 /*
507 * Currently, the gralloc API doesn't differentiate between allocation time and map
508 * time strides. A workaround for amdgpu DRI buffers is to always to align to 256 at
509 * allocation time.
510 *
511 * See b/115946221,b/117942643
512 */
513 if (use_flags & (BO_USE_SW_MASK))
514 needs_alignment = true;
515#endif
516 // See b/122049612
517 if (use_flags & (BO_USE_SCANOUT))
518 needs_alignment = true;
519
520 if (needs_alignment) {
521 uint32_t bytes_per_pixel = drv_bytes_per_pixel_from_format(format, 0);
522 width = ALIGN(width, 256 / bytes_per_pixel);
523 }
524
525 return dri_bo_create(bo, width, height, format, use_flags);
526 }
527
528 return amdgpu_create_bo_linear(bo, width, height, format, use_flags);
529}
530
531static int amdgpu_create_bo_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
532 uint32_t format, const uint64_t *modifiers,
533 uint32_t count)
534{
535 bool only_use_linear = true;
536
537 for (uint32_t i = 0; i < count; ++i)
538 if (modifiers[i] != DRM_FORMAT_MOD_LINEAR)
539 only_use_linear = false;
540
541 if (only_use_linear)
542 return amdgpu_create_bo_linear(bo, width, height, format, BO_USE_SCANOUT);
543
544 return dri_bo_create_with_modifiers(bo, width, height, format, modifiers, count);
545}
546
547static int amdgpu_import_bo(struct bo *bo, struct drv_import_fd_data *data)
548{
Gurchetan Singh52155b42021-01-27 17:55:17 -0800549 bool dri_tiling = data->format_modifier != DRM_FORMAT_MOD_LINEAR;
550 if (data->format_modifier == DRM_FORMAT_MOD_INVALID) {
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100551 struct combination *combo;
552 combo = drv_get_combination(bo->drv, data->format, data->use_flags);
553 if (!combo)
554 return -EINVAL;
555
556 dri_tiling = combo->metadata.tiling == TILE_TYPE_DRI;
557 }
558
559 if (dri_tiling)
Satyajitcdcebd82018-01-12 14:49:05 +0530560 return dri_bo_import(bo, data);
561 else
562 return drv_prime_bo_import(bo, data);
563}
564
565static int amdgpu_destroy_bo(struct bo *bo)
566{
567 if (bo->priv)
568 return dri_bo_destroy(bo);
569 else
570 return drv_gem_bo_destroy(bo);
571}
572
573static void *amdgpu_map_bo(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530574{
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200575 void *addr = MAP_FAILED;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530576 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700577 union drm_amdgpu_gem_mmap gem_map = { { 0 } };
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200578 struct drm_amdgpu_gem_create_in bo_info = { 0 };
579 struct drm_amdgpu_gem_op gem_op = { 0 };
580 uint32_t handle = bo->handles[plane].u32;
581 struct amdgpu_linear_vma_priv *priv = NULL;
582 struct amdgpu_priv *drv_priv;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530583
Satyajitcdcebd82018-01-12 14:49:05 +0530584 if (bo->priv)
585 return dri_bo_map(bo, vma, plane, map_flags);
586
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200587 drv_priv = bo->drv->priv;
588 gem_op.handle = handle;
589 gem_op.op = AMDGPU_GEM_OP_GET_GEM_CREATE_INFO;
590 gem_op.value = (uintptr_t)&bo_info;
591
592 ret = drmCommandWriteRead(bo->drv->fd, DRM_AMDGPU_GEM_OP, &gem_op, sizeof(gem_op));
593 if (ret)
594 return MAP_FAILED;
595
596 vma->length = bo_info.bo_size;
597
598 if (((bo_info.domains & AMDGPU_GEM_DOMAIN_VRAM) ||
599 (bo_info.domain_flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)) &&
600 drv_priv->sdma_cmdbuf_map) {
601 union drm_amdgpu_gem_create gem_create = { { 0 } };
602
603 priv = calloc(1, sizeof(struct amdgpu_linear_vma_priv));
604 if (!priv)
605 return MAP_FAILED;
606
607 gem_create.in.bo_size = bo_info.bo_size;
608 gem_create.in.alignment = 4096;
609 gem_create.in.domains = AMDGPU_GEM_DOMAIN_GTT;
610
611 ret = drmCommandWriteRead(bo->drv->fd, DRM_AMDGPU_GEM_CREATE, &gem_create,
612 sizeof(gem_create));
613 if (ret < 0) {
614 drv_log("GEM create failed\n");
615 free(priv);
616 return MAP_FAILED;
617 }
618
619 priv->map_flags = map_flags;
620 handle = priv->handle = gem_create.out.handle;
621
622 ret = sdma_copy(bo->drv->priv, bo->drv->fd, bo->handles[0].u32, priv->handle,
623 bo_info.bo_size);
624 if (ret) {
625 drv_log("SDMA copy for read failed\n");
626 goto fail;
627 }
628 }
629
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200630 gem_map.in.handle = handle;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530631 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_AMDGPU_GEM_MMAP, &gem_map);
632 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700633 drv_log("DRM_IOCTL_AMDGPU_GEM_MMAP failed\n");
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200634 goto fail;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530635 }
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700636
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200637 addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700638 gem_map.out.addr_ptr);
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200639 if (addr == MAP_FAILED)
640 goto fail;
641
642 vma->priv = priv;
643 return addr;
644
645fail:
646 if (priv) {
647 struct drm_gem_close gem_close = { 0 };
648 gem_close.handle = priv->handle;
649 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
650 free(priv);
651 }
652 return MAP_FAILED;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530653}
654
Satyajitcdcebd82018-01-12 14:49:05 +0530655static int amdgpu_unmap_bo(struct bo *bo, struct vma *vma)
656{
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800657 if (bo->priv) {
Satyajitcdcebd82018-01-12 14:49:05 +0530658 return dri_bo_unmap(bo, vma);
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800659 } else {
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200660 int r = munmap(vma->addr, vma->length);
661 if (r)
662 return r;
663
664 if (vma->priv) {
665 struct amdgpu_linear_vma_priv *priv = vma->priv;
666 struct drm_gem_close gem_close = { 0 };
667
668 if (BO_MAP_WRITE & priv->map_flags) {
669 r = sdma_copy(bo->drv->priv, bo->drv->fd, priv->handle,
670 bo->handles[0].u32, vma->length);
671 if (r)
672 return r;
673 }
674
675 gem_close.handle = priv->handle;
676 r = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
677 }
678
679 return 0;
680 }
Satyajitcdcebd82018-01-12 14:49:05 +0530681}
682
Deepak Sharmaff66c802018-11-16 12:10:54 -0800683static int amdgpu_bo_invalidate(struct bo *bo, struct mapping *mapping)
684{
685 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700686 union drm_amdgpu_gem_wait_idle wait_idle = { { 0 } };
Deepak Sharmaff66c802018-11-16 12:10:54 -0800687
688 if (bo->priv)
689 return 0;
690
Deepak Sharmaff66c802018-11-16 12:10:54 -0800691 wait_idle.in.handle = bo->handles[0].u32;
692 wait_idle.in.timeout = AMDGPU_TIMEOUT_INFINITE;
693
694 ret = drmCommandWriteRead(bo->drv->fd, DRM_AMDGPU_GEM_WAIT_IDLE, &wait_idle,
695 sizeof(wait_idle));
696
697 if (ret < 0) {
698 drv_log("DRM_AMDGPU_GEM_WAIT_IDLE failed with %d\n", ret);
699 return ret;
700 }
701
702 if (ret == 0 && wait_idle.out.status)
703 drv_log("DRM_AMDGPU_GEM_WAIT_IDLE BO is busy\n");
704
705 return 0;
706}
707
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700708const struct backend backend_amdgpu = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530709 .name = "amdgpu",
710 .init = amdgpu_init,
711 .close = amdgpu_close,
Satyajitcdcebd82018-01-12 14:49:05 +0530712 .bo_create = amdgpu_create_bo,
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100713 .bo_create_with_modifiers = amdgpu_create_bo_with_modifiers,
Satyajitcdcebd82018-01-12 14:49:05 +0530714 .bo_destroy = amdgpu_destroy_bo,
715 .bo_import = amdgpu_import_bo,
716 .bo_map = amdgpu_map_bo,
717 .bo_unmap = amdgpu_unmap_bo,
Deepak Sharmaff66c802018-11-16 12:10:54 -0800718 .bo_invalidate = amdgpu_bo_invalidate,
Gurchetan Singh695125c2021-02-03 08:44:09 -0800719 .resolve_format = drv_resolve_format_helper,
ChromeOS Developer44588bb2020-03-02 16:32:09 +0100720 .num_planes_from_modifier = dri_num_planes_from_modifier,
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530721};
722
723#endif