blob: 66dbc2abc63b19a6a2f4fea033043af1653d37bf [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[] = {
Gurchetan Singh45ca4492021-04-28 17:12:52 -070051 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,
Lepton Wuc83116f2021-04-26 12:26:56 -070054 DRM_FORMAT_ABGR16161616F,
Bas Nieuwenhuizenb16076b2020-06-29 12:41:18 +020055};
Gurchetan Singh179687e2016-10-28 10:07:35 -070056
Bas Nieuwenhuizenb5e0f2d2020-09-29 16:02:18 +020057const static uint32_t texture_source_formats[] = {
58 DRM_FORMAT_GR88, DRM_FORMAT_R8, DRM_FORMAT_NV21, DRM_FORMAT_NV12,
59 DRM_FORMAT_YVU420_ANDROID, DRM_FORMAT_YVU420, DRM_FORMAT_P010
60};
Shirish Sdf423df2017-04-18 16:21:59 +053061
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +020062static int query_dev_info(int fd, struct drm_amdgpu_info_device *dev_info)
63{
64 struct drm_amdgpu_info info_args = { 0 };
65
66 info_args.return_pointer = (uintptr_t)dev_info;
67 info_args.return_size = sizeof(*dev_info);
68 info_args.query = AMDGPU_INFO_DEV_INFO;
69
70 return drmCommandWrite(fd, DRM_AMDGPU_INFO, &info_args, sizeof(info_args));
71}
72
73static int sdma_init(struct amdgpu_priv *priv, int fd)
74{
75 union drm_amdgpu_ctx ctx_args = { { 0 } };
76 union drm_amdgpu_gem_create gem_create = { { 0 } };
77 struct drm_amdgpu_gem_va va_args = { 0 };
78 union drm_amdgpu_gem_mmap gem_map = { { 0 } };
79 struct drm_gem_close gem_close = { 0 };
80 int ret;
81
82 /* Ensure we can make a submission without BO lists. */
83 if (priv->drm_version < 27)
84 return 0;
85
86 /* Anything outside this range needs adjustments to the SDMA copy commands */
87 if (priv->dev_info.family < AMDGPU_FAMILY_CI || priv->dev_info.family > AMDGPU_FAMILY_NV)
88 return 0;
89
90 ctx_args.in.op = AMDGPU_CTX_OP_ALLOC_CTX;
91
92 ret = drmCommandWriteRead(fd, DRM_AMDGPU_CTX, &ctx_args, sizeof(ctx_args));
93 if (ret < 0)
94 return ret;
95
96 priv->sdma_ctx = ctx_args.out.alloc.ctx_id;
97
98 priv->sdma_cmdbuf_size = ALIGN(4096, priv->dev_info.virtual_address_alignment);
99 gem_create.in.bo_size = priv->sdma_cmdbuf_size;
100 gem_create.in.alignment = 4096;
101 gem_create.in.domains = AMDGPU_GEM_DOMAIN_GTT;
102
103 ret = drmCommandWriteRead(fd, DRM_AMDGPU_GEM_CREATE, &gem_create, sizeof(gem_create));
104 if (ret < 0)
105 goto fail_ctx;
106
107 priv->sdma_cmdbuf_bo = gem_create.out.handle;
108
109 priv->sdma_cmdbuf_addr =
110 ALIGN(priv->dev_info.virtual_address_offset, priv->dev_info.virtual_address_alignment);
111
112 /* Map the buffer into the GPU address space so we can use it from the GPU */
113 va_args.handle = priv->sdma_cmdbuf_bo;
114 va_args.operation = AMDGPU_VA_OP_MAP;
115 va_args.flags = AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_EXECUTABLE;
116 va_args.va_address = priv->sdma_cmdbuf_addr;
117 va_args.offset_in_bo = 0;
118 va_args.map_size = priv->sdma_cmdbuf_size;
119
120 ret = drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
121 if (ret)
122 goto fail_bo;
123
124 gem_map.in.handle = priv->sdma_cmdbuf_bo;
125 ret = drmIoctl(fd, DRM_IOCTL_AMDGPU_GEM_MMAP, &gem_map);
126 if (ret)
127 goto fail_va;
128
129 priv->sdma_cmdbuf_map = mmap(0, priv->sdma_cmdbuf_size, PROT_READ | PROT_WRITE, MAP_SHARED,
130 fd, gem_map.out.addr_ptr);
131 if (priv->sdma_cmdbuf_map == MAP_FAILED) {
132 priv->sdma_cmdbuf_map = NULL;
133 ret = -ENOMEM;
134 goto fail_va;
135 }
136
137 return 0;
138fail_va:
139 va_args.operation = AMDGPU_VA_OP_UNMAP;
140 va_args.flags = 0;
141 drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
142fail_bo:
143 gem_close.handle = priv->sdma_cmdbuf_bo;
144 drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
145fail_ctx:
146 memset(&ctx_args, 0, sizeof(ctx_args));
147 ctx_args.in.op = AMDGPU_CTX_OP_FREE_CTX;
148 ctx_args.in.ctx_id = priv->sdma_ctx;
149 drmCommandWriteRead(fd, DRM_AMDGPU_CTX, &ctx_args, sizeof(ctx_args));
150 return ret;
151}
152
153static void sdma_finish(struct amdgpu_priv *priv, int fd)
154{
155 union drm_amdgpu_ctx ctx_args = { { 0 } };
156 struct drm_amdgpu_gem_va va_args = { 0 };
157 struct drm_gem_close gem_close = { 0 };
158
159 if (!priv->sdma_cmdbuf_map)
160 return;
161
162 va_args.handle = priv->sdma_cmdbuf_bo;
163 va_args.operation = AMDGPU_VA_OP_UNMAP;
164 va_args.flags = 0;
165 va_args.va_address = priv->sdma_cmdbuf_addr;
166 va_args.offset_in_bo = 0;
167 va_args.map_size = priv->sdma_cmdbuf_size;
168 drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
169
170 gem_close.handle = priv->sdma_cmdbuf_bo;
171 drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
172
173 ctx_args.in.op = AMDGPU_CTX_OP_FREE_CTX;
174 ctx_args.in.ctx_id = priv->sdma_ctx;
175 drmCommandWriteRead(fd, DRM_AMDGPU_CTX, &ctx_args, sizeof(ctx_args));
176}
177
178static int sdma_copy(struct amdgpu_priv *priv, int fd, uint32_t src_handle, uint32_t dst_handle,
179 uint64_t size)
180{
181 const uint64_t max_size_per_cmd = 0x3fff00;
182 const uint32_t cmd_size = 7 * sizeof(uint32_t); /* 7 dwords, see loop below. */
183 const uint64_t max_commands = priv->sdma_cmdbuf_size / cmd_size;
184 uint64_t src_addr = priv->sdma_cmdbuf_addr + priv->sdma_cmdbuf_size;
185 uint64_t dst_addr = src_addr + size;
186 struct drm_amdgpu_gem_va va_args = { 0 };
187 unsigned cmd = 0;
188 uint64_t remaining_size = size;
189 uint64_t cur_src_addr = src_addr;
190 uint64_t cur_dst_addr = dst_addr;
191 struct drm_amdgpu_cs_chunk_ib ib = { 0 };
192 struct drm_amdgpu_cs_chunk chunks[2] = { { 0 } };
193 uint64_t chunk_ptrs[2];
194 union drm_amdgpu_cs cs = { { 0 } };
195 struct drm_amdgpu_bo_list_in bo_list = { 0 };
196 struct drm_amdgpu_bo_list_entry bo_list_entries[3] = { { 0 } };
197 union drm_amdgpu_wait_cs wait_cs = { { 0 } };
198 int ret = 0;
199
200 if (size > UINT64_MAX - max_size_per_cmd ||
201 DIV_ROUND_UP(size, max_size_per_cmd) > max_commands)
202 return -ENOMEM;
203
204 /* Map both buffers into the GPU address space so we can access them from the GPU. */
205 va_args.handle = src_handle;
206 va_args.operation = AMDGPU_VA_OP_MAP;
207 va_args.flags = AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_DELAY_UPDATE;
208 va_args.va_address = src_addr;
209 va_args.map_size = size;
210
211 ret = drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
212 if (ret)
213 return ret;
214
215 va_args.handle = dst_handle;
216 va_args.flags = AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE | AMDGPU_VM_DELAY_UPDATE;
217 va_args.va_address = dst_addr;
218
219 ret = drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
220 if (ret)
221 goto unmap_src;
222
223 while (remaining_size) {
224 uint64_t cur_size = remaining_size;
225 if (cur_size > max_size_per_cmd)
226 cur_size = max_size_per_cmd;
227
228 priv->sdma_cmdbuf_map[cmd++] = 0x01; /* linear copy */
229 priv->sdma_cmdbuf_map[cmd++] =
230 priv->dev_info.family >= AMDGPU_FAMILY_AI ? (cur_size - 1) : cur_size;
231 priv->sdma_cmdbuf_map[cmd++] = 0;
232 priv->sdma_cmdbuf_map[cmd++] = cur_src_addr;
233 priv->sdma_cmdbuf_map[cmd++] = cur_src_addr >> 32;
234 priv->sdma_cmdbuf_map[cmd++] = cur_dst_addr;
235 priv->sdma_cmdbuf_map[cmd++] = cur_dst_addr >> 32;
236
237 remaining_size -= cur_size;
238 cur_src_addr += cur_size;
239 cur_dst_addr += cur_size;
240 }
241
242 ib.va_start = priv->sdma_cmdbuf_addr;
243 ib.ib_bytes = cmd * 4;
244 ib.ip_type = AMDGPU_HW_IP_DMA;
245
246 chunks[1].chunk_id = AMDGPU_CHUNK_ID_IB;
247 chunks[1].length_dw = sizeof(ib) / 4;
248 chunks[1].chunk_data = (uintptr_t)&ib;
249
250 bo_list_entries[0].bo_handle = priv->sdma_cmdbuf_bo;
251 bo_list_entries[0].bo_priority = 8; /* Middle of range, like RADV. */
252 bo_list_entries[1].bo_handle = src_handle;
253 bo_list_entries[1].bo_priority = 8;
254 bo_list_entries[2].bo_handle = dst_handle;
255 bo_list_entries[2].bo_priority = 8;
256
257 bo_list.bo_number = 3;
258 bo_list.bo_info_size = sizeof(bo_list_entries[0]);
259 bo_list.bo_info_ptr = (uintptr_t)bo_list_entries;
260
261 chunks[0].chunk_id = AMDGPU_CHUNK_ID_BO_HANDLES;
262 chunks[0].length_dw = sizeof(bo_list) / 4;
263 chunks[0].chunk_data = (uintptr_t)&bo_list;
264
265 chunk_ptrs[0] = (uintptr_t)&chunks[0];
266 chunk_ptrs[1] = (uintptr_t)&chunks[1];
267
268 cs.in.ctx_id = priv->sdma_ctx;
269 cs.in.num_chunks = 2;
270 cs.in.chunks = (uintptr_t)chunk_ptrs;
271
272 ret = drmCommandWriteRead(fd, DRM_AMDGPU_CS, &cs, sizeof(cs));
273 if (ret) {
274 drv_log("SDMA copy command buffer submission failed %d\n", ret);
275 goto unmap_dst;
276 }
277
278 wait_cs.in.handle = cs.out.handle;
279 wait_cs.in.ip_type = AMDGPU_HW_IP_DMA;
280 wait_cs.in.ctx_id = priv->sdma_ctx;
281 wait_cs.in.timeout = INT64_MAX;
282
283 ret = drmCommandWriteRead(fd, DRM_AMDGPU_WAIT_CS, &wait_cs, sizeof(wait_cs));
284 if (ret) {
285 drv_log("Could not wait for CS to finish\n");
286 } else if (wait_cs.out.status) {
287 drv_log("Infinite wait timed out, likely GPU hang.\n");
288 ret = -ENODEV;
289 }
290
291unmap_dst:
292 va_args.handle = dst_handle;
293 va_args.operation = AMDGPU_VA_OP_UNMAP;
294 va_args.flags = AMDGPU_VM_DELAY_UPDATE;
295 va_args.va_address = dst_addr;
296 drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
297
298unmap_src:
299 va_args.handle = src_handle;
300 va_args.operation = AMDGPU_VA_OP_UNMAP;
301 va_args.flags = AMDGPU_VM_DELAY_UPDATE;
302 va_args.va_address = src_addr;
303 drmCommandWrite(fd, DRM_AMDGPU_GEM_VA, &va_args, sizeof(va_args));
304
305 return ret;
306}
307
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530308static int amdgpu_init(struct driver *drv)
309{
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +0100310 struct amdgpu_priv *priv;
311 drmVersionPtr drm_version;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800312 struct format_metadata metadata;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700313 uint64_t use_flags = BO_USE_RENDER_MASK;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530314
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +0100315 priv = calloc(1, sizeof(struct amdgpu_priv));
316 if (!priv)
Satyajitcdcebd82018-01-12 14:49:05 +0530317 return -ENOMEM;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530318
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +0100319 drm_version = drmGetVersion(drv_get_fd(drv));
320 if (!drm_version) {
321 free(priv);
Satyajitcdcebd82018-01-12 14:49:05 +0530322 return -ENODEV;
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +0100323 }
324
325 priv->drm_version = drm_version->version_minor;
326 drmFreeVersion(drm_version);
327
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +0100328 drv->priv = priv;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530329
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200330 if (query_dev_info(drv_get_fd(drv), &priv->dev_info)) {
331 free(priv);
332 drv->priv = NULL;
333 return -ENODEV;
334 }
Satyajitcdcebd82018-01-12 14:49:05 +0530335 if (dri_init(drv, DRI_PATH, "radeonsi")) {
336 free(priv);
337 drv->priv = NULL;
338 return -ENODEV;
339 }
Shirish Sdf423df2017-04-18 16:21:59 +0530340
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800341 /* Continue on failure, as we can still succesfully map things without SDMA. */
342 if (sdma_init(priv, drv_get_fd(drv)))
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200343 drv_log("SDMA init failed\n");
344
Satyajitcdcebd82018-01-12 14:49:05 +0530345 metadata.tiling = TILE_TYPE_LINEAR;
346 metadata.priority = 1;
Kristian H. Kristensenbc8c5932017-10-24 18:36:32 -0700347 metadata.modifier = DRM_FORMAT_MOD_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800348
Gurchetan Singhd3001452017-11-03 17:18:36 -0700349 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
350 &metadata, use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800351
Satyajitcdcebd82018-01-12 14:49:05 +0530352 drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
353 &metadata, BO_USE_TEXTURE_MASK);
354
Hirokazu Honda3bd681c2020-06-23 17:52:20 +0900355 /* NV12 format for camera, display, decoding and encoding. */
356 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
357 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SCANOUT |
Gurchetan Singh45ca4492021-04-28 17:12:52 -0700358 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER |
359 BO_USE_PROTECTED);
Hirokazu Honda3b8d4d02019-07-31 16:35:52 +0900360
Bas Nieuwenhuizenb5e0f2d2020-09-29 16:02:18 +0200361 drv_modify_combination(drv, DRM_FORMAT_P010, &metadata,
Ikshwaku Chauhan4b69e282021-01-28 23:56:12 +0530362 BO_USE_SCANOUT | BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER |
Gurchetan Singh45ca4492021-04-28 17:12:52 -0700363 BO_USE_PROTECTED);
Bas Nieuwenhuizenb5e0f2d2020-09-29 16:02:18 +0200364
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700365 /* Android CTS tests require this. */
366 drv_add_combination(drv, DRM_FORMAT_BGR888, &metadata, BO_USE_SW_MASK);
367
Satyajitcdcebd82018-01-12 14:49:05 +0530368 /* Linear formats supported by display. */
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800369 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
370 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
Drew Davenport5d215242019-03-25 09:18:42 -0600371 drv_modify_combination(drv, DRM_FORMAT_ABGR8888, &metadata, BO_USE_SCANOUT);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800372 drv_modify_combination(drv, DRM_FORMAT_XBGR8888, &metadata, BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800373
Bas Nieuwenhuizenb16076b2020-06-29 12:41:18 +0200374 drv_modify_combination(drv, DRM_FORMAT_ABGR2101010, &metadata, BO_USE_SCANOUT);
375 drv_modify_combination(drv, DRM_FORMAT_ARGB2101010, &metadata, BO_USE_SCANOUT);
376 drv_modify_combination(drv, DRM_FORMAT_XBGR2101010, &metadata, BO_USE_SCANOUT);
377 drv_modify_combination(drv, DRM_FORMAT_XRGB2101010, &metadata, BO_USE_SCANOUT);
378
Satyajitcdcebd82018-01-12 14:49:05 +0530379 drv_modify_combination(drv, DRM_FORMAT_NV21, &metadata, BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800380
Satyajitcdcebd82018-01-12 14:49:05 +0530381 /*
382 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
David Stevens49518142020-06-15 13:48:48 +0900383 * from camera and input/output from hardware decoder/encoder.
Satyajitcdcebd82018-01-12 14:49:05 +0530384 */
385 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
David Stevens49518142020-06-15 13:48:48 +0900386 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
387 BO_USE_HW_VIDEO_ENCODER);
Satyajitcdcebd82018-01-12 14:49:05 +0530388
389 /*
390 * The following formats will be allocated by the DRI backend and may be potentially tiled.
391 * Since format modifier support hasn't been implemented fully yet, it's not
392 * possible to enumerate the different types of buffers (like i915 can).
393 */
394 use_flags &= ~BO_USE_RENDERSCRIPT;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700395 use_flags &= ~BO_USE_SW_WRITE_OFTEN;
396 use_flags &= ~BO_USE_SW_READ_OFTEN;
Drew Davenportf1a7dfc2021-06-15 00:56:30 -0600397#if __ANDROID__
398 use_flags &= ~BO_USE_SW_WRITE_RARELY;
399 use_flags &= ~BO_USE_SW_READ_RARELY;
400#endif
Gurchetan Singha1892b22017-09-28 16:40:52 -0700401 use_flags &= ~BO_USE_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800402
Satyajitcdcebd82018-01-12 14:49:05 +0530403 metadata.tiling = TILE_TYPE_DRI;
404 metadata.priority = 2;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800405
Gurchetan Singhd3001452017-11-03 17:18:36 -0700406 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
407 &metadata, use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800408
Satyajitcdcebd82018-01-12 14:49:05 +0530409 /* Potentially tiled formats supported by display. */
410 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
411 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
Bas Nieuwenhuizen582bdbf2019-04-03 18:12:12 +0200412 drv_modify_combination(drv, DRM_FORMAT_ABGR8888, &metadata, BO_USE_SCANOUT);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800413 drv_modify_combination(drv, DRM_FORMAT_XBGR8888, &metadata, BO_USE_SCANOUT);
Bas Nieuwenhuizenb16076b2020-06-29 12:41:18 +0200414
415 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);
Gurchetan Singhd3001452017-11-03 17:18:36 -0700419 return 0;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530420}
421
422static void amdgpu_close(struct driver *drv)
423{
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200424 sdma_finish(drv->priv, drv_get_fd(drv));
Satyajitcdcebd82018-01-12 14:49:05 +0530425 dri_close(drv);
426 free(drv->priv);
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530427 drv->priv = NULL;
428}
429
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100430static int amdgpu_create_bo_linear(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
431 uint64_t use_flags)
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530432{
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530433 int ret;
Drew Davenport8db36fe2020-10-15 22:18:00 -0600434 size_t num_planes;
Satyajitcdcebd82018-01-12 14:49:05 +0530435 uint32_t plane, stride;
Gurchetan Singh99644382020-10-07 15:28:11 -0700436 union drm_amdgpu_gem_create gem_create = { { 0 } };
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200437 struct amdgpu_priv *priv = bo->drv->priv;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530438
Satyajitcdcebd82018-01-12 14:49:05 +0530439 stride = drv_stride_from_format(format, width, 0);
Drew Davenport8db36fe2020-10-15 22:18:00 -0600440 num_planes = drv_num_planes_from_format(format);
441
442 /*
443 * For multiplane formats, align the stride to 512 to ensure that subsample strides are 256
444 * aligned. This uses more memory than necessary since the first plane only needs to be
445 * 256 aligned, but it's acceptable for a short-term fix. It's probably safe for other gpu
446 * families, but let's restrict it to Raven for now (b/171013552).
447 * */
448 if (priv->dev_info.family == AMDGPU_FAMILY_RV && num_planes > 1)
449 stride = ALIGN(stride, 512);
450 else
451 stride = ALIGN(stride, 256);
Satyajitcdcebd82018-01-12 14:49:05 +0530452
Ikshwaku Chauhan047df2b2020-06-29 16:44:57 +0530453 /*
Gurchetan Singh9b4c8b72020-08-20 14:25:43 -0700454 * Currently, allocator used by chrome aligns the height for Encoder/
455 * Decoder buffers while allocator used by android(gralloc/minigbm)
456 * doesn't provide any aligment.
457 *
458 * See b/153130069
459 */
Ikshwaku Chauhan047df2b2020-06-29 16:44:57 +0530460 if (use_flags & (BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER))
461 height = ALIGN(height, CHROME_HEIGHT_ALIGN);
462
Satyajitcdcebd82018-01-12 14:49:05 +0530463 drv_bo_from_format(bo, stride, height, format);
Shirish Sdf423df2017-04-18 16:21:59 +0530464
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200465 gem_create.in.bo_size =
466 ALIGN(bo->meta.total_size, priv->dev_info.virtual_address_alignment);
Satyajitcdcebd82018-01-12 14:49:05 +0530467 gem_create.in.alignment = 256;
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800468 gem_create.in.domain_flags = 0;
Satyajitcdcebd82018-01-12 14:49:05 +0530469
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700470 if (use_flags & (BO_USE_LINEAR | BO_USE_SW_MASK))
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800471 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
472
Deepak Sharma62a9c4e2018-05-01 12:11:27 -0700473 gem_create.in.domains = AMDGPU_GEM_DOMAIN_GTT;
Bas Nieuwenhuizen4daf12c2020-06-04 23:11:27 +0200474
475 /* Scanout in GTT requires USWC, otherwise try to use cachable memory
476 * for buffers that are read often, because uncacheable reads can be
477 * very slow. USWC should be faster on the GPU though. */
478 if ((use_flags & BO_USE_SCANOUT) || !(use_flags & BO_USE_SW_READ_OFTEN))
Jao-ke Chin-Lee5481e3c2020-04-10 00:12:12 +0000479 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_CPU_GTT_USWC;
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800480
Ikshwaku Chauhan4b69e282021-01-28 23:56:12 +0530481 /* For protected data Buffer needs to be allocated from TMZ */
482 if (use_flags & BO_USE_PROTECTED)
483 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_ENCRYPTED;
484
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530485 /* Allocate the buffer with the preferred heap. */
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800486 ret = drmCommandWriteRead(drv_get_fd(bo->drv), DRM_AMDGPU_GEM_CREATE, &gem_create,
487 sizeof(gem_create));
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530488 if (ret < 0)
489 return ret;
490
Gurchetan Singh298b7572019-09-19 09:55:18 -0700491 for (plane = 0; plane < bo->meta.num_planes; plane++)
Shirish Sdf423df2017-04-18 16:21:59 +0530492 bo->handles[plane].u32 = gem_create.out.handle;
493
Gurchetan Singh52155b42021-01-27 17:55:17 -0800494 bo->meta.format_modifier = DRM_FORMAT_MOD_LINEAR;
Bas Nieuwenhuizen7119d332020-02-07 20:20:30 +0100495
Satyajitcdcebd82018-01-12 14:49:05 +0530496 return 0;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530497}
498
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100499static int amdgpu_create_bo(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
500 uint64_t use_flags)
Satyajitcdcebd82018-01-12 14:49:05 +0530501{
502 struct combination *combo;
Drew Davenport8ed9b312021-05-06 17:08:27 -0600503 struct amdgpu_priv *priv = bo->drv->priv;
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100504
505 combo = drv_get_combination(bo->drv, format, use_flags);
Satyajitcdcebd82018-01-12 14:49:05 +0530506 if (!combo)
507 return -EINVAL;
508
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100509 if (combo->metadata.tiling == TILE_TYPE_DRI) {
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100510 // See b/122049612
Drew Davenport5ebd19f2021-06-09 00:17:05 -0600511 if (use_flags & (BO_USE_SCANOUT) && priv->dev_info.family == AMDGPU_FAMILY_CZ) {
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100512 uint32_t bytes_per_pixel = drv_bytes_per_pixel_from_format(format, 0);
513 width = ALIGN(width, 256 / bytes_per_pixel);
514 }
515
516 return dri_bo_create(bo, width, height, format, use_flags);
517 }
518
519 return amdgpu_create_bo_linear(bo, width, height, format, use_flags);
520}
521
522static int amdgpu_create_bo_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
523 uint32_t format, const uint64_t *modifiers,
524 uint32_t count)
525{
526 bool only_use_linear = true;
527
528 for (uint32_t i = 0; i < count; ++i)
529 if (modifiers[i] != DRM_FORMAT_MOD_LINEAR)
530 only_use_linear = false;
531
532 if (only_use_linear)
533 return amdgpu_create_bo_linear(bo, width, height, format, BO_USE_SCANOUT);
534
535 return dri_bo_create_with_modifiers(bo, width, height, format, modifiers, count);
536}
537
538static int amdgpu_import_bo(struct bo *bo, struct drv_import_fd_data *data)
539{
Gurchetan Singh52155b42021-01-27 17:55:17 -0800540 bool dri_tiling = data->format_modifier != DRM_FORMAT_MOD_LINEAR;
541 if (data->format_modifier == DRM_FORMAT_MOD_INVALID) {
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100542 struct combination *combo;
543 combo = drv_get_combination(bo->drv, data->format, data->use_flags);
544 if (!combo)
545 return -EINVAL;
546
547 dri_tiling = combo->metadata.tiling == TILE_TYPE_DRI;
548 }
549
550 if (dri_tiling)
Satyajitcdcebd82018-01-12 14:49:05 +0530551 return dri_bo_import(bo, data);
552 else
553 return drv_prime_bo_import(bo, data);
554}
555
556static int amdgpu_destroy_bo(struct bo *bo)
557{
558 if (bo->priv)
559 return dri_bo_destroy(bo);
560 else
561 return drv_gem_bo_destroy(bo);
562}
563
564static void *amdgpu_map_bo(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530565{
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200566 void *addr = MAP_FAILED;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530567 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700568 union drm_amdgpu_gem_mmap gem_map = { { 0 } };
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200569 struct drm_amdgpu_gem_create_in bo_info = { 0 };
570 struct drm_amdgpu_gem_op gem_op = { 0 };
571 uint32_t handle = bo->handles[plane].u32;
572 struct amdgpu_linear_vma_priv *priv = NULL;
573 struct amdgpu_priv *drv_priv;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530574
Satyajitcdcebd82018-01-12 14:49:05 +0530575 if (bo->priv)
576 return dri_bo_map(bo, vma, plane, map_flags);
577
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200578 drv_priv = bo->drv->priv;
579 gem_op.handle = handle;
580 gem_op.op = AMDGPU_GEM_OP_GET_GEM_CREATE_INFO;
581 gem_op.value = (uintptr_t)&bo_info;
582
583 ret = drmCommandWriteRead(bo->drv->fd, DRM_AMDGPU_GEM_OP, &gem_op, sizeof(gem_op));
584 if (ret)
585 return MAP_FAILED;
586
587 vma->length = bo_info.bo_size;
588
589 if (((bo_info.domains & AMDGPU_GEM_DOMAIN_VRAM) ||
590 (bo_info.domain_flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)) &&
591 drv_priv->sdma_cmdbuf_map) {
592 union drm_amdgpu_gem_create gem_create = { { 0 } };
593
594 priv = calloc(1, sizeof(struct amdgpu_linear_vma_priv));
595 if (!priv)
596 return MAP_FAILED;
597
598 gem_create.in.bo_size = bo_info.bo_size;
599 gem_create.in.alignment = 4096;
600 gem_create.in.domains = AMDGPU_GEM_DOMAIN_GTT;
601
602 ret = drmCommandWriteRead(bo->drv->fd, DRM_AMDGPU_GEM_CREATE, &gem_create,
603 sizeof(gem_create));
604 if (ret < 0) {
605 drv_log("GEM create failed\n");
606 free(priv);
607 return MAP_FAILED;
608 }
609
610 priv->map_flags = map_flags;
611 handle = priv->handle = gem_create.out.handle;
612
613 ret = sdma_copy(bo->drv->priv, bo->drv->fd, bo->handles[0].u32, priv->handle,
614 bo_info.bo_size);
615 if (ret) {
616 drv_log("SDMA copy for read failed\n");
617 goto fail;
618 }
619 }
620
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200621 gem_map.in.handle = handle;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530622 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_AMDGPU_GEM_MMAP, &gem_map);
623 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700624 drv_log("DRM_IOCTL_AMDGPU_GEM_MMAP failed\n");
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200625 goto fail;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530626 }
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700627
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200628 addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700629 gem_map.out.addr_ptr);
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200630 if (addr == MAP_FAILED)
631 goto fail;
632
633 vma->priv = priv;
634 return addr;
635
636fail:
637 if (priv) {
638 struct drm_gem_close gem_close = { 0 };
639 gem_close.handle = priv->handle;
640 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
641 free(priv);
642 }
643 return MAP_FAILED;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530644}
645
Satyajitcdcebd82018-01-12 14:49:05 +0530646static int amdgpu_unmap_bo(struct bo *bo, struct vma *vma)
647{
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800648 if (bo->priv) {
Satyajitcdcebd82018-01-12 14:49:05 +0530649 return dri_bo_unmap(bo, vma);
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800650 } else {
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200651 int r = munmap(vma->addr, vma->length);
652 if (r)
653 return r;
654
655 if (vma->priv) {
656 struct amdgpu_linear_vma_priv *priv = vma->priv;
657 struct drm_gem_close gem_close = { 0 };
658
659 if (BO_MAP_WRITE & priv->map_flags) {
660 r = sdma_copy(bo->drv->priv, bo->drv->fd, priv->handle,
661 bo->handles[0].u32, vma->length);
662 if (r)
663 return r;
664 }
665
666 gem_close.handle = priv->handle;
667 r = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
668 }
669
670 return 0;
671 }
Satyajitcdcebd82018-01-12 14:49:05 +0530672}
673
Deepak Sharmaff66c802018-11-16 12:10:54 -0800674static int amdgpu_bo_invalidate(struct bo *bo, struct mapping *mapping)
675{
676 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700677 union drm_amdgpu_gem_wait_idle wait_idle = { { 0 } };
Deepak Sharmaff66c802018-11-16 12:10:54 -0800678
679 if (bo->priv)
680 return 0;
681
Deepak Sharmaff66c802018-11-16 12:10:54 -0800682 wait_idle.in.handle = bo->handles[0].u32;
683 wait_idle.in.timeout = AMDGPU_TIMEOUT_INFINITE;
684
685 ret = drmCommandWriteRead(bo->drv->fd, DRM_AMDGPU_GEM_WAIT_IDLE, &wait_idle,
686 sizeof(wait_idle));
687
688 if (ret < 0) {
689 drv_log("DRM_AMDGPU_GEM_WAIT_IDLE failed with %d\n", ret);
690 return ret;
691 }
692
693 if (ret == 0 && wait_idle.out.status)
694 drv_log("DRM_AMDGPU_GEM_WAIT_IDLE BO is busy\n");
695
696 return 0;
697}
698
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700699const struct backend backend_amdgpu = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530700 .name = "amdgpu",
701 .init = amdgpu_init,
702 .close = amdgpu_close,
Satyajitcdcebd82018-01-12 14:49:05 +0530703 .bo_create = amdgpu_create_bo,
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100704 .bo_create_with_modifiers = amdgpu_create_bo_with_modifiers,
Satyajitcdcebd82018-01-12 14:49:05 +0530705 .bo_destroy = amdgpu_destroy_bo,
706 .bo_import = amdgpu_import_bo,
707 .bo_map = amdgpu_map_bo,
708 .bo_unmap = amdgpu_unmap_bo,
Deepak Sharmaff66c802018-11-16 12:10:54 -0800709 .bo_invalidate = amdgpu_bo_invalidate,
Gurchetan Singh695125c2021-02-03 08:44:09 -0800710 .resolve_format = drv_resolve_format_helper,
ChromeOS Developer44588bb2020-03-02 16:32:09 +0100711 .num_planes_from_modifier = dri_num_planes_from_modifier,
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530712};
713
714#endif