blob: 359a8c25aa78894c0265292e14792eed6191de6f [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 |
357 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
Hirokazu Honda3b8d4d02019-07-31 16:35:52 +0900358
Bas Nieuwenhuizenb5e0f2d2020-09-29 16:02:18 +0200359 drv_modify_combination(drv, DRM_FORMAT_P010, &metadata,
360 BO_USE_SCANOUT | BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
361
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700362 /* Android CTS tests require this. */
363 drv_add_combination(drv, DRM_FORMAT_BGR888, &metadata, BO_USE_SW_MASK);
364
Satyajitcdcebd82018-01-12 14:49:05 +0530365 /* Linear formats supported by display. */
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800366 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
367 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
Drew Davenport5d215242019-03-25 09:18:42 -0600368 drv_modify_combination(drv, DRM_FORMAT_ABGR8888, &metadata, BO_USE_SCANOUT);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800369 drv_modify_combination(drv, DRM_FORMAT_XBGR8888, &metadata, BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800370
Bas Nieuwenhuizenb16076b2020-06-29 12:41:18 +0200371 drv_modify_combination(drv, DRM_FORMAT_ABGR2101010, &metadata, BO_USE_SCANOUT);
372 drv_modify_combination(drv, DRM_FORMAT_ARGB2101010, &metadata, BO_USE_SCANOUT);
373 drv_modify_combination(drv, DRM_FORMAT_XBGR2101010, &metadata, BO_USE_SCANOUT);
374 drv_modify_combination(drv, DRM_FORMAT_XRGB2101010, &metadata, BO_USE_SCANOUT);
375
Satyajitcdcebd82018-01-12 14:49:05 +0530376 drv_modify_combination(drv, DRM_FORMAT_NV21, &metadata, BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800377
Satyajitcdcebd82018-01-12 14:49:05 +0530378 /*
379 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
David Stevens49518142020-06-15 13:48:48 +0900380 * from camera and input/output from hardware decoder/encoder.
Satyajitcdcebd82018-01-12 14:49:05 +0530381 */
382 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
David Stevens49518142020-06-15 13:48:48 +0900383 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
384 BO_USE_HW_VIDEO_ENCODER);
Satyajitcdcebd82018-01-12 14:49:05 +0530385
386 /*
387 * The following formats will be allocated by the DRI backend and may be potentially tiled.
388 * Since format modifier support hasn't been implemented fully yet, it's not
389 * possible to enumerate the different types of buffers (like i915 can).
390 */
391 use_flags &= ~BO_USE_RENDERSCRIPT;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700392 use_flags &= ~BO_USE_SW_WRITE_OFTEN;
393 use_flags &= ~BO_USE_SW_READ_OFTEN;
394 use_flags &= ~BO_USE_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800395
Satyajitcdcebd82018-01-12 14:49:05 +0530396 metadata.tiling = TILE_TYPE_DRI;
397 metadata.priority = 2;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800398
Gurchetan Singhd3001452017-11-03 17:18:36 -0700399 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
400 &metadata, use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800401
Satyajitcdcebd82018-01-12 14:49:05 +0530402 /* Potentially tiled formats supported by display. */
403 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
404 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
Bas Nieuwenhuizen582bdbf2019-04-03 18:12:12 +0200405 drv_modify_combination(drv, DRM_FORMAT_ABGR8888, &metadata, BO_USE_SCANOUT);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800406 drv_modify_combination(drv, DRM_FORMAT_XBGR8888, &metadata, BO_USE_SCANOUT);
Bas Nieuwenhuizenb16076b2020-06-29 12:41:18 +0200407
408 drv_modify_combination(drv, DRM_FORMAT_ABGR2101010, &metadata, BO_USE_SCANOUT);
409 drv_modify_combination(drv, DRM_FORMAT_ARGB2101010, &metadata, BO_USE_SCANOUT);
410 drv_modify_combination(drv, DRM_FORMAT_XBGR2101010, &metadata, BO_USE_SCANOUT);
411 drv_modify_combination(drv, DRM_FORMAT_XRGB2101010, &metadata, BO_USE_SCANOUT);
Gurchetan Singhd3001452017-11-03 17:18:36 -0700412 return 0;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530413}
414
415static void amdgpu_close(struct driver *drv)
416{
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200417 sdma_finish(drv->priv, drv_get_fd(drv));
Satyajitcdcebd82018-01-12 14:49:05 +0530418 dri_close(drv);
419 free(drv->priv);
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530420 drv->priv = NULL;
421}
422
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100423static int amdgpu_create_bo_linear(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
424 uint64_t use_flags)
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530425{
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530426 int ret;
Drew Davenport8db36fe2020-10-15 22:18:00 -0600427 size_t num_planes;
Satyajitcdcebd82018-01-12 14:49:05 +0530428 uint32_t plane, stride;
Gurchetan Singh99644382020-10-07 15:28:11 -0700429 union drm_amdgpu_gem_create gem_create = { { 0 } };
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200430 struct amdgpu_priv *priv = bo->drv->priv;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530431
Satyajitcdcebd82018-01-12 14:49:05 +0530432 stride = drv_stride_from_format(format, width, 0);
Drew Davenport8db36fe2020-10-15 22:18:00 -0600433 num_planes = drv_num_planes_from_format(format);
434
435 /*
436 * For multiplane formats, align the stride to 512 to ensure that subsample strides are 256
437 * aligned. This uses more memory than necessary since the first plane only needs to be
438 * 256 aligned, but it's acceptable for a short-term fix. It's probably safe for other gpu
439 * families, but let's restrict it to Raven for now (b/171013552).
440 * */
441 if (priv->dev_info.family == AMDGPU_FAMILY_RV && num_planes > 1)
442 stride = ALIGN(stride, 512);
443 else
444 stride = ALIGN(stride, 256);
Satyajitcdcebd82018-01-12 14:49:05 +0530445
Ikshwaku Chauhan047df2b2020-06-29 16:44:57 +0530446 /*
Gurchetan Singh9b4c8b72020-08-20 14:25:43 -0700447 * Currently, allocator used by chrome aligns the height for Encoder/
448 * Decoder buffers while allocator used by android(gralloc/minigbm)
449 * doesn't provide any aligment.
450 *
451 * See b/153130069
452 */
Ikshwaku Chauhan047df2b2020-06-29 16:44:57 +0530453 if (use_flags & (BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER))
454 height = ALIGN(height, CHROME_HEIGHT_ALIGN);
455
Satyajitcdcebd82018-01-12 14:49:05 +0530456 drv_bo_from_format(bo, stride, height, format);
Shirish Sdf423df2017-04-18 16:21:59 +0530457
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200458 gem_create.in.bo_size =
459 ALIGN(bo->meta.total_size, priv->dev_info.virtual_address_alignment);
Satyajitcdcebd82018-01-12 14:49:05 +0530460 gem_create.in.alignment = 256;
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800461 gem_create.in.domain_flags = 0;
Satyajitcdcebd82018-01-12 14:49:05 +0530462
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700463 if (use_flags & (BO_USE_LINEAR | BO_USE_SW_MASK))
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800464 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
465
Deepak Sharma62a9c4e2018-05-01 12:11:27 -0700466 gem_create.in.domains = AMDGPU_GEM_DOMAIN_GTT;
Bas Nieuwenhuizen4daf12c2020-06-04 23:11:27 +0200467
468 /* Scanout in GTT requires USWC, otherwise try to use cachable memory
469 * for buffers that are read often, because uncacheable reads can be
470 * very slow. USWC should be faster on the GPU though. */
471 if ((use_flags & BO_USE_SCANOUT) || !(use_flags & BO_USE_SW_READ_OFTEN))
Jao-ke Chin-Lee5481e3c2020-04-10 00:12:12 +0000472 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_CPU_GTT_USWC;
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800473
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530474 /* Allocate the buffer with the preferred heap. */
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800475 ret = drmCommandWriteRead(drv_get_fd(bo->drv), DRM_AMDGPU_GEM_CREATE, &gem_create,
476 sizeof(gem_create));
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530477 if (ret < 0)
478 return ret;
479
Gurchetan Singh298b7572019-09-19 09:55:18 -0700480 for (plane = 0; plane < bo->meta.num_planes; plane++)
Shirish Sdf423df2017-04-18 16:21:59 +0530481 bo->handles[plane].u32 = gem_create.out.handle;
482
Gurchetan Singh52155b42021-01-27 17:55:17 -0800483 bo->meta.format_modifier = DRM_FORMAT_MOD_LINEAR;
Bas Nieuwenhuizen7119d332020-02-07 20:20:30 +0100484
Satyajitcdcebd82018-01-12 14:49:05 +0530485 return 0;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530486}
487
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100488static int amdgpu_create_bo(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
489 uint64_t use_flags)
Satyajitcdcebd82018-01-12 14:49:05 +0530490{
491 struct combination *combo;
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100492
493 combo = drv_get_combination(bo->drv, format, use_flags);
Satyajitcdcebd82018-01-12 14:49:05 +0530494 if (!combo)
495 return -EINVAL;
496
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100497 if (combo->metadata.tiling == TILE_TYPE_DRI) {
498 bool needs_alignment = false;
499#ifdef __ANDROID__
500 /*
501 * Currently, the gralloc API doesn't differentiate between allocation time and map
502 * time strides. A workaround for amdgpu DRI buffers is to always to align to 256 at
503 * allocation time.
504 *
505 * See b/115946221,b/117942643
506 */
507 if (use_flags & (BO_USE_SW_MASK))
508 needs_alignment = true;
509#endif
510 // See b/122049612
511 if (use_flags & (BO_USE_SCANOUT))
512 needs_alignment = true;
513
514 if (needs_alignment) {
515 uint32_t bytes_per_pixel = drv_bytes_per_pixel_from_format(format, 0);
516 width = ALIGN(width, 256 / bytes_per_pixel);
517 }
518
519 return dri_bo_create(bo, width, height, format, use_flags);
520 }
521
522 return amdgpu_create_bo_linear(bo, width, height, format, use_flags);
523}
524
525static int amdgpu_create_bo_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
526 uint32_t format, const uint64_t *modifiers,
527 uint32_t count)
528{
529 bool only_use_linear = true;
530
531 for (uint32_t i = 0; i < count; ++i)
532 if (modifiers[i] != DRM_FORMAT_MOD_LINEAR)
533 only_use_linear = false;
534
535 if (only_use_linear)
536 return amdgpu_create_bo_linear(bo, width, height, format, BO_USE_SCANOUT);
537
538 return dri_bo_create_with_modifiers(bo, width, height, format, modifiers, count);
539}
540
541static int amdgpu_import_bo(struct bo *bo, struct drv_import_fd_data *data)
542{
Gurchetan Singh52155b42021-01-27 17:55:17 -0800543 bool dri_tiling = data->format_modifier != DRM_FORMAT_MOD_LINEAR;
544 if (data->format_modifier == DRM_FORMAT_MOD_INVALID) {
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100545 struct combination *combo;
546 combo = drv_get_combination(bo->drv, data->format, data->use_flags);
547 if (!combo)
548 return -EINVAL;
549
550 dri_tiling = combo->metadata.tiling == TILE_TYPE_DRI;
551 }
552
553 if (dri_tiling)
Satyajitcdcebd82018-01-12 14:49:05 +0530554 return dri_bo_import(bo, data);
555 else
556 return drv_prime_bo_import(bo, data);
557}
558
559static int amdgpu_destroy_bo(struct bo *bo)
560{
561 if (bo->priv)
562 return dri_bo_destroy(bo);
563 else
564 return drv_gem_bo_destroy(bo);
565}
566
567static void *amdgpu_map_bo(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530568{
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200569 void *addr = MAP_FAILED;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530570 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700571 union drm_amdgpu_gem_mmap gem_map = { { 0 } };
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200572 struct drm_amdgpu_gem_create_in bo_info = { 0 };
573 struct drm_amdgpu_gem_op gem_op = { 0 };
574 uint32_t handle = bo->handles[plane].u32;
575 struct amdgpu_linear_vma_priv *priv = NULL;
576 struct amdgpu_priv *drv_priv;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530577
Satyajitcdcebd82018-01-12 14:49:05 +0530578 if (bo->priv)
579 return dri_bo_map(bo, vma, plane, map_flags);
580
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200581 drv_priv = bo->drv->priv;
582 gem_op.handle = handle;
583 gem_op.op = AMDGPU_GEM_OP_GET_GEM_CREATE_INFO;
584 gem_op.value = (uintptr_t)&bo_info;
585
586 ret = drmCommandWriteRead(bo->drv->fd, DRM_AMDGPU_GEM_OP, &gem_op, sizeof(gem_op));
587 if (ret)
588 return MAP_FAILED;
589
590 vma->length = bo_info.bo_size;
591
592 if (((bo_info.domains & AMDGPU_GEM_DOMAIN_VRAM) ||
593 (bo_info.domain_flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)) &&
594 drv_priv->sdma_cmdbuf_map) {
595 union drm_amdgpu_gem_create gem_create = { { 0 } };
596
597 priv = calloc(1, sizeof(struct amdgpu_linear_vma_priv));
598 if (!priv)
599 return MAP_FAILED;
600
601 gem_create.in.bo_size = bo_info.bo_size;
602 gem_create.in.alignment = 4096;
603 gem_create.in.domains = AMDGPU_GEM_DOMAIN_GTT;
604
605 ret = drmCommandWriteRead(bo->drv->fd, DRM_AMDGPU_GEM_CREATE, &gem_create,
606 sizeof(gem_create));
607 if (ret < 0) {
608 drv_log("GEM create failed\n");
609 free(priv);
610 return MAP_FAILED;
611 }
612
613 priv->map_flags = map_flags;
614 handle = priv->handle = gem_create.out.handle;
615
616 ret = sdma_copy(bo->drv->priv, bo->drv->fd, bo->handles[0].u32, priv->handle,
617 bo_info.bo_size);
618 if (ret) {
619 drv_log("SDMA copy for read failed\n");
620 goto fail;
621 }
622 }
623
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200624 gem_map.in.handle = handle;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530625 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_AMDGPU_GEM_MMAP, &gem_map);
626 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700627 drv_log("DRM_IOCTL_AMDGPU_GEM_MMAP failed\n");
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200628 goto fail;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530629 }
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700630
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200631 addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700632 gem_map.out.addr_ptr);
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200633 if (addr == MAP_FAILED)
634 goto fail;
635
636 vma->priv = priv;
637 return addr;
638
639fail:
640 if (priv) {
641 struct drm_gem_close gem_close = { 0 };
642 gem_close.handle = priv->handle;
643 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
644 free(priv);
645 }
646 return MAP_FAILED;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530647}
648
Satyajitcdcebd82018-01-12 14:49:05 +0530649static int amdgpu_unmap_bo(struct bo *bo, struct vma *vma)
650{
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800651 if (bo->priv) {
Satyajitcdcebd82018-01-12 14:49:05 +0530652 return dri_bo_unmap(bo, vma);
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800653 } else {
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200654 int r = munmap(vma->addr, vma->length);
655 if (r)
656 return r;
657
658 if (vma->priv) {
659 struct amdgpu_linear_vma_priv *priv = vma->priv;
660 struct drm_gem_close gem_close = { 0 };
661
662 if (BO_MAP_WRITE & priv->map_flags) {
663 r = sdma_copy(bo->drv->priv, bo->drv->fd, priv->handle,
664 bo->handles[0].u32, vma->length);
665 if (r)
666 return r;
667 }
668
669 gem_close.handle = priv->handle;
670 r = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
671 }
672
673 return 0;
674 }
Satyajitcdcebd82018-01-12 14:49:05 +0530675}
676
Deepak Sharmaff66c802018-11-16 12:10:54 -0800677static int amdgpu_bo_invalidate(struct bo *bo, struct mapping *mapping)
678{
679 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700680 union drm_amdgpu_gem_wait_idle wait_idle = { { 0 } };
Deepak Sharmaff66c802018-11-16 12:10:54 -0800681
682 if (bo->priv)
683 return 0;
684
Deepak Sharmaff66c802018-11-16 12:10:54 -0800685 wait_idle.in.handle = bo->handles[0].u32;
686 wait_idle.in.timeout = AMDGPU_TIMEOUT_INFINITE;
687
688 ret = drmCommandWriteRead(bo->drv->fd, DRM_AMDGPU_GEM_WAIT_IDLE, &wait_idle,
689 sizeof(wait_idle));
690
691 if (ret < 0) {
692 drv_log("DRM_AMDGPU_GEM_WAIT_IDLE failed with %d\n", ret);
693 return ret;
694 }
695
696 if (ret == 0 && wait_idle.out.status)
697 drv_log("DRM_AMDGPU_GEM_WAIT_IDLE BO is busy\n");
698
699 return 0;
700}
701
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700702const struct backend backend_amdgpu = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530703 .name = "amdgpu",
704 .init = amdgpu_init,
705 .close = amdgpu_close,
Satyajitcdcebd82018-01-12 14:49:05 +0530706 .bo_create = amdgpu_create_bo,
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100707 .bo_create_with_modifiers = amdgpu_create_bo_with_modifiers,
Satyajitcdcebd82018-01-12 14:49:05 +0530708 .bo_destroy = amdgpu_destroy_bo,
709 .bo_import = amdgpu_import_bo,
710 .bo_map = amdgpu_map_bo,
711 .bo_unmap = amdgpu_unmap_bo,
Deepak Sharmaff66c802018-11-16 12:10:54 -0800712 .bo_invalidate = amdgpu_bo_invalidate,
Gurchetan Singh695125c2021-02-03 08:44:09 -0800713 .resolve_format = drv_resolve_format_helper,
ChromeOS Developer44588bb2020-03-02 16:32:09 +0100714 .num_planes_from_modifier = dri_num_planes_from_modifier,
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530715};
716
717#endif