blob: e70dd1478d6e325859555c9a1f4a3fab6494714f [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
Drew Davenport49b804b2021-07-29 19:52:02 -0600446 * families, but let's restrict it to Raven and Stoney for now (b/171013552, b/190484589).
Drew Davenport8db36fe2020-10-15 22:18:00 -0600447 * */
Drew Davenporte1178652021-08-16 09:36:24 -0600448 if (num_planes > 1 &&
449 (priv->dev_info.family == AMDGPU_FAMILY_RV ||
450 (priv->dev_info.family == AMDGPU_FAMILY_CZ && !(use_flags & BO_USE_HW_VIDEO_ENCODER))))
Drew Davenport8db36fe2020-10-15 22:18:00 -0600451 stride = ALIGN(stride, 512);
452 else
453 stride = ALIGN(stride, 256);
Satyajitcdcebd82018-01-12 14:49:05 +0530454
Ikshwaku Chauhan047df2b2020-06-29 16:44:57 +0530455 /*
Gurchetan Singh9b4c8b72020-08-20 14:25:43 -0700456 * Currently, allocator used by chrome aligns the height for Encoder/
457 * Decoder buffers while allocator used by android(gralloc/minigbm)
458 * doesn't provide any aligment.
459 *
460 * See b/153130069
461 */
Ikshwaku Chauhan047df2b2020-06-29 16:44:57 +0530462 if (use_flags & (BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER))
463 height = ALIGN(height, CHROME_HEIGHT_ALIGN);
464
Satyajitcdcebd82018-01-12 14:49:05 +0530465 drv_bo_from_format(bo, stride, height, format);
Shirish Sdf423df2017-04-18 16:21:59 +0530466
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200467 gem_create.in.bo_size =
468 ALIGN(bo->meta.total_size, priv->dev_info.virtual_address_alignment);
Satyajitcdcebd82018-01-12 14:49:05 +0530469 gem_create.in.alignment = 256;
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800470 gem_create.in.domain_flags = 0;
Satyajitcdcebd82018-01-12 14:49:05 +0530471
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700472 if (use_flags & (BO_USE_LINEAR | BO_USE_SW_MASK))
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800473 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
474
Deepak Sharma62a9c4e2018-05-01 12:11:27 -0700475 gem_create.in.domains = AMDGPU_GEM_DOMAIN_GTT;
Bas Nieuwenhuizen4daf12c2020-06-04 23:11:27 +0200476
477 /* Scanout in GTT requires USWC, otherwise try to use cachable memory
478 * for buffers that are read often, because uncacheable reads can be
479 * very slow. USWC should be faster on the GPU though. */
480 if ((use_flags & BO_USE_SCANOUT) || !(use_flags & BO_USE_SW_READ_OFTEN))
Jao-ke Chin-Lee5481e3c2020-04-10 00:12:12 +0000481 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_CPU_GTT_USWC;
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800482
Ikshwaku Chauhan4b69e282021-01-28 23:56:12 +0530483 /* For protected data Buffer needs to be allocated from TMZ */
484 if (use_flags & BO_USE_PROTECTED)
485 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_ENCRYPTED;
486
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530487 /* Allocate the buffer with the preferred heap. */
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800488 ret = drmCommandWriteRead(drv_get_fd(bo->drv), DRM_AMDGPU_GEM_CREATE, &gem_create,
489 sizeof(gem_create));
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530490 if (ret < 0)
491 return ret;
492
Gurchetan Singh298b7572019-09-19 09:55:18 -0700493 for (plane = 0; plane < bo->meta.num_planes; plane++)
Shirish Sdf423df2017-04-18 16:21:59 +0530494 bo->handles[plane].u32 = gem_create.out.handle;
495
Gurchetan Singh52155b42021-01-27 17:55:17 -0800496 bo->meta.format_modifier = DRM_FORMAT_MOD_LINEAR;
Bas Nieuwenhuizen7119d332020-02-07 20:20:30 +0100497
Satyajitcdcebd82018-01-12 14:49:05 +0530498 return 0;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530499}
500
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100501static int amdgpu_create_bo(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
502 uint64_t use_flags)
Satyajitcdcebd82018-01-12 14:49:05 +0530503{
504 struct combination *combo;
Drew Davenport8ed9b312021-05-06 17:08:27 -0600505 struct amdgpu_priv *priv = bo->drv->priv;
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100506
507 combo = drv_get_combination(bo->drv, format, use_flags);
Satyajitcdcebd82018-01-12 14:49:05 +0530508 if (!combo)
509 return -EINVAL;
510
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100511 if (combo->metadata.tiling == TILE_TYPE_DRI) {
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100512 // See b/122049612
Drew Davenport5ebd19f2021-06-09 00:17:05 -0600513 if (use_flags & (BO_USE_SCANOUT) && priv->dev_info.family == AMDGPU_FAMILY_CZ) {
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100514 uint32_t bytes_per_pixel = drv_bytes_per_pixel_from_format(format, 0);
515 width = ALIGN(width, 256 / bytes_per_pixel);
516 }
517
518 return dri_bo_create(bo, width, height, format, use_flags);
519 }
520
521 return amdgpu_create_bo_linear(bo, width, height, format, use_flags);
522}
523
524static int amdgpu_create_bo_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
525 uint32_t format, const uint64_t *modifiers,
526 uint32_t count)
527{
528 bool only_use_linear = true;
529
530 for (uint32_t i = 0; i < count; ++i)
531 if (modifiers[i] != DRM_FORMAT_MOD_LINEAR)
532 only_use_linear = false;
533
534 if (only_use_linear)
535 return amdgpu_create_bo_linear(bo, width, height, format, BO_USE_SCANOUT);
536
537 return dri_bo_create_with_modifiers(bo, width, height, format, modifiers, count);
538}
539
540static int amdgpu_import_bo(struct bo *bo, struct drv_import_fd_data *data)
541{
Gurchetan Singh52155b42021-01-27 17:55:17 -0800542 bool dri_tiling = data->format_modifier != DRM_FORMAT_MOD_LINEAR;
543 if (data->format_modifier == DRM_FORMAT_MOD_INVALID) {
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100544 struct combination *combo;
545 combo = drv_get_combination(bo->drv, data->format, data->use_flags);
546 if (!combo)
547 return -EINVAL;
548
549 dri_tiling = combo->metadata.tiling == TILE_TYPE_DRI;
550 }
551
552 if (dri_tiling)
Satyajitcdcebd82018-01-12 14:49:05 +0530553 return dri_bo_import(bo, data);
554 else
555 return drv_prime_bo_import(bo, data);
556}
557
558static int amdgpu_destroy_bo(struct bo *bo)
559{
560 if (bo->priv)
561 return dri_bo_destroy(bo);
562 else
563 return drv_gem_bo_destroy(bo);
564}
565
566static void *amdgpu_map_bo(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530567{
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200568 void *addr = MAP_FAILED;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530569 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700570 union drm_amdgpu_gem_mmap gem_map = { { 0 } };
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200571 struct drm_amdgpu_gem_create_in bo_info = { 0 };
572 struct drm_amdgpu_gem_op gem_op = { 0 };
573 uint32_t handle = bo->handles[plane].u32;
574 struct amdgpu_linear_vma_priv *priv = NULL;
575 struct amdgpu_priv *drv_priv;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530576
Satyajitcdcebd82018-01-12 14:49:05 +0530577 if (bo->priv)
578 return dri_bo_map(bo, vma, plane, map_flags);
579
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200580 drv_priv = bo->drv->priv;
581 gem_op.handle = handle;
582 gem_op.op = AMDGPU_GEM_OP_GET_GEM_CREATE_INFO;
583 gem_op.value = (uintptr_t)&bo_info;
584
585 ret = drmCommandWriteRead(bo->drv->fd, DRM_AMDGPU_GEM_OP, &gem_op, sizeof(gem_op));
586 if (ret)
587 return MAP_FAILED;
588
589 vma->length = bo_info.bo_size;
590
591 if (((bo_info.domains & AMDGPU_GEM_DOMAIN_VRAM) ||
592 (bo_info.domain_flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)) &&
593 drv_priv->sdma_cmdbuf_map) {
594 union drm_amdgpu_gem_create gem_create = { { 0 } };
595
596 priv = calloc(1, sizeof(struct amdgpu_linear_vma_priv));
597 if (!priv)
598 return MAP_FAILED;
599
600 gem_create.in.bo_size = bo_info.bo_size;
601 gem_create.in.alignment = 4096;
602 gem_create.in.domains = AMDGPU_GEM_DOMAIN_GTT;
603
604 ret = drmCommandWriteRead(bo->drv->fd, DRM_AMDGPU_GEM_CREATE, &gem_create,
605 sizeof(gem_create));
606 if (ret < 0) {
607 drv_log("GEM create failed\n");
608 free(priv);
609 return MAP_FAILED;
610 }
611
612 priv->map_flags = map_flags;
613 handle = priv->handle = gem_create.out.handle;
614
615 ret = sdma_copy(bo->drv->priv, bo->drv->fd, bo->handles[0].u32, priv->handle,
616 bo_info.bo_size);
617 if (ret) {
618 drv_log("SDMA copy for read failed\n");
619 goto fail;
620 }
621 }
622
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200623 gem_map.in.handle = handle;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530624 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_AMDGPU_GEM_MMAP, &gem_map);
625 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700626 drv_log("DRM_IOCTL_AMDGPU_GEM_MMAP failed\n");
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200627 goto fail;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530628 }
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700629
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200630 addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700631 gem_map.out.addr_ptr);
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200632 if (addr == MAP_FAILED)
633 goto fail;
634
635 vma->priv = priv;
636 return addr;
637
638fail:
639 if (priv) {
640 struct drm_gem_close gem_close = { 0 };
641 gem_close.handle = priv->handle;
642 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
643 free(priv);
644 }
645 return MAP_FAILED;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530646}
647
Satyajitcdcebd82018-01-12 14:49:05 +0530648static int amdgpu_unmap_bo(struct bo *bo, struct vma *vma)
649{
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800650 if (bo->priv) {
Satyajitcdcebd82018-01-12 14:49:05 +0530651 return dri_bo_unmap(bo, vma);
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800652 } else {
Bas Nieuwenhuizen4a3f98c2020-06-20 03:50:34 +0200653 int r = munmap(vma->addr, vma->length);
654 if (r)
655 return r;
656
657 if (vma->priv) {
658 struct amdgpu_linear_vma_priv *priv = vma->priv;
659 struct drm_gem_close gem_close = { 0 };
660
661 if (BO_MAP_WRITE & priv->map_flags) {
662 r = sdma_copy(bo->drv->priv, bo->drv->fd, priv->handle,
663 bo->handles[0].u32, vma->length);
664 if (r)
665 return r;
666 }
667
668 gem_close.handle = priv->handle;
669 r = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
670 }
671
672 return 0;
673 }
Satyajitcdcebd82018-01-12 14:49:05 +0530674}
675
Deepak Sharmaff66c802018-11-16 12:10:54 -0800676static int amdgpu_bo_invalidate(struct bo *bo, struct mapping *mapping)
677{
678 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700679 union drm_amdgpu_gem_wait_idle wait_idle = { { 0 } };
Deepak Sharmaff66c802018-11-16 12:10:54 -0800680
681 if (bo->priv)
682 return 0;
683
Deepak Sharmaff66c802018-11-16 12:10:54 -0800684 wait_idle.in.handle = bo->handles[0].u32;
685 wait_idle.in.timeout = AMDGPU_TIMEOUT_INFINITE;
686
687 ret = drmCommandWriteRead(bo->drv->fd, DRM_AMDGPU_GEM_WAIT_IDLE, &wait_idle,
688 sizeof(wait_idle));
689
690 if (ret < 0) {
691 drv_log("DRM_AMDGPU_GEM_WAIT_IDLE failed with %d\n", ret);
692 return ret;
693 }
694
695 if (ret == 0 && wait_idle.out.status)
696 drv_log("DRM_AMDGPU_GEM_WAIT_IDLE BO is busy\n");
697
698 return 0;
699}
700
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700701const struct backend backend_amdgpu = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530702 .name = "amdgpu",
703 .init = amdgpu_init,
704 .close = amdgpu_close,
Satyajitcdcebd82018-01-12 14:49:05 +0530705 .bo_create = amdgpu_create_bo,
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100706 .bo_create_with_modifiers = amdgpu_create_bo_with_modifiers,
Satyajitcdcebd82018-01-12 14:49:05 +0530707 .bo_destroy = amdgpu_destroy_bo,
708 .bo_import = amdgpu_import_bo,
709 .bo_map = amdgpu_map_bo,
710 .bo_unmap = amdgpu_unmap_bo,
Deepak Sharmaff66c802018-11-16 12:10:54 -0800711 .bo_invalidate = amdgpu_bo_invalidate,
Gurchetan Singh695125c2021-02-03 08:44:09 -0800712 .resolve_format = drv_resolve_format_helper,
ChromeOS Developer44588bb2020-03-02 16:32:09 +0100713 .num_planes_from_modifier = dri_num_planes_from_modifier,
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530714};
715
716#endif