blob: bb9342eb47217930a139ed1c8c6255fe8ba61de6 [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
Satyajitcdcebd82018-01-12 14:49:05 +053021#ifdef __ANDROID__
22#define DRI_PATH "/vendor/lib/dri/radeonsi_dri.so"
23#else
Gurchetan Singhcf9ed9d2019-12-13 09:37:01 -080024// clang-format off
25#define DRI_PATH STRINGIZE(DRI_DRIVER_DIR/radeonsi_dri.so)
26// clang-format on
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053027#endif
28
Satyajitcdcebd82018-01-12 14:49:05 +053029#define TILE_TYPE_LINEAR 0
30/* DRI backend decides tiling in this case. */
31#define TILE_TYPE_DRI 1
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053032
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +010033struct amdgpu_priv {
Satyajitcdcebd82018-01-12 14:49:05 +053034 struct dri_driver dri;
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +010035 int drm_version;
36};
37
Gurchetan Singh767c5382018-05-05 00:42:12 +000038const static uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
Drew Davenport293d9e32018-06-20 15:46:50 -060039 DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
40 DRM_FORMAT_XRGB8888 };
Gurchetan Singh179687e2016-10-28 10:07:35 -070041
Junichi Uekawad441f4e2020-01-29 15:45:16 +090042const static uint32_t texture_source_formats[] = { DRM_FORMAT_GR88, DRM_FORMAT_R8,
43 DRM_FORMAT_NV21, DRM_FORMAT_NV12,
Hirokazu Honda3b8d4d02019-07-31 16:35:52 +090044 DRM_FORMAT_YVU420_ANDROID, DRM_FORMAT_YVU420 };
Shirish Sdf423df2017-04-18 16:21:59 +053045
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053046static int amdgpu_init(struct driver *drv)
47{
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +010048 struct amdgpu_priv *priv;
49 drmVersionPtr drm_version;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080050 struct format_metadata metadata;
Gurchetan Singha1892b22017-09-28 16:40:52 -070051 uint64_t use_flags = BO_USE_RENDER_MASK;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053052
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +010053 priv = calloc(1, sizeof(struct amdgpu_priv));
54 if (!priv)
Satyajitcdcebd82018-01-12 14:49:05 +053055 return -ENOMEM;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053056
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +010057 drm_version = drmGetVersion(drv_get_fd(drv));
58 if (!drm_version) {
59 free(priv);
Satyajitcdcebd82018-01-12 14:49:05 +053060 return -ENODEV;
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +010061 }
62
63 priv->drm_version = drm_version->version_minor;
64 drmFreeVersion(drm_version);
65
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +010066 drv->priv = priv;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053067
Satyajitcdcebd82018-01-12 14:49:05 +053068 if (dri_init(drv, DRI_PATH, "radeonsi")) {
69 free(priv);
70 drv->priv = NULL;
71 return -ENODEV;
72 }
Shirish Sdf423df2017-04-18 16:21:59 +053073
Satyajitcdcebd82018-01-12 14:49:05 +053074 metadata.tiling = TILE_TYPE_LINEAR;
75 metadata.priority = 1;
Kristian H. Kristensenbc8c5932017-10-24 18:36:32 -070076 metadata.modifier = DRM_FORMAT_MOD_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080077
Gurchetan Singhd3001452017-11-03 17:18:36 -070078 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
79 &metadata, use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080080
Satyajitcdcebd82018-01-12 14:49:05 +053081 drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
82 &metadata, BO_USE_TEXTURE_MASK);
83
Hirokazu Honda3b8d4d02019-07-31 16:35:52 +090084 /*
85 * Chrome uses DMA-buf mmap to write to YV12 buffers, which are then accessed by the
86 * Video Encoder Accelerator (VEA). It could also support NV12 potentially in the future.
87 */
88 drv_modify_combination(drv, DRM_FORMAT_YVU420, &metadata, BO_USE_HW_VIDEO_ENCODER);
89 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata, BO_USE_HW_VIDEO_ENCODER);
90
Gurchetan Singh71bc6652018-09-17 17:42:05 -070091 /* Android CTS tests require this. */
92 drv_add_combination(drv, DRM_FORMAT_BGR888, &metadata, BO_USE_SW_MASK);
93
Satyajitcdcebd82018-01-12 14:49:05 +053094 /* Linear formats supported by display. */
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080095 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
96 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
Drew Davenport5d215242019-03-25 09:18:42 -060097 drv_modify_combination(drv, DRM_FORMAT_ABGR8888, &metadata, BO_USE_SCANOUT);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080098 drv_modify_combination(drv, DRM_FORMAT_XBGR8888, &metadata, BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080099
Satyajitcdcebd82018-01-12 14:49:05 +0530100 /* YUV formats for camera and display. */
101 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
Miguel Casasdea0ccb2018-07-02 09:40:25 -0400102 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SCANOUT |
103 BO_USE_HW_VIDEO_DECODER);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800104
Satyajitcdcebd82018-01-12 14:49:05 +0530105 drv_modify_combination(drv, DRM_FORMAT_NV21, &metadata, BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800106
Satyajitcdcebd82018-01-12 14:49:05 +0530107 /*
108 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
109 * from camera.
110 */
111 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
112 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
113
114 /*
115 * The following formats will be allocated by the DRI backend and may be potentially tiled.
116 * Since format modifier support hasn't been implemented fully yet, it's not
117 * possible to enumerate the different types of buffers (like i915 can).
118 */
119 use_flags &= ~BO_USE_RENDERSCRIPT;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700120 use_flags &= ~BO_USE_SW_WRITE_OFTEN;
121 use_flags &= ~BO_USE_SW_READ_OFTEN;
122 use_flags &= ~BO_USE_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800123
Satyajitcdcebd82018-01-12 14:49:05 +0530124 metadata.tiling = TILE_TYPE_DRI;
125 metadata.priority = 2;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800126
Gurchetan Singhd3001452017-11-03 17:18:36 -0700127 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
128 &metadata, use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800129
Satyajitcdcebd82018-01-12 14:49:05 +0530130 /* Potentially tiled formats supported by display. */
131 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
132 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
Bas Nieuwenhuizen582bdbf2019-04-03 18:12:12 +0200133 drv_modify_combination(drv, DRM_FORMAT_ABGR8888, &metadata, BO_USE_SCANOUT);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800134 drv_modify_combination(drv, DRM_FORMAT_XBGR8888, &metadata, BO_USE_SCANOUT);
Gurchetan Singhd3001452017-11-03 17:18:36 -0700135 return 0;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530136}
137
138static void amdgpu_close(struct driver *drv)
139{
Satyajitcdcebd82018-01-12 14:49:05 +0530140 dri_close(drv);
141 free(drv->priv);
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530142 drv->priv = NULL;
143}
144
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100145static int amdgpu_create_bo_linear(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
146 uint64_t use_flags)
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530147{
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530148 int ret;
Satyajitcdcebd82018-01-12 14:49:05 +0530149 uint32_t plane, stride;
Satyajitcdcebd82018-01-12 14:49:05 +0530150 union drm_amdgpu_gem_create gem_create;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530151
Satyajitcdcebd82018-01-12 14:49:05 +0530152 stride = drv_stride_from_format(format, width, 0);
Keiichi Watanabe79155d72018-08-13 16:44:54 +0900153 stride = ALIGN(stride, 256);
Satyajitcdcebd82018-01-12 14:49:05 +0530154
155 drv_bo_from_format(bo, stride, height, format);
Shirish Sdf423df2017-04-18 16:21:59 +0530156
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530157 memset(&gem_create, 0, sizeof(gem_create));
Gurchetan Singh298b7572019-09-19 09:55:18 -0700158 gem_create.in.bo_size = bo->meta.total_size;
Satyajitcdcebd82018-01-12 14:49:05 +0530159 gem_create.in.alignment = 256;
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800160 gem_create.in.domain_flags = 0;
Satyajitcdcebd82018-01-12 14:49:05 +0530161
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700162 if (use_flags & (BO_USE_LINEAR | BO_USE_SW_MASK))
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800163 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
164
Deepak Sharma62a9c4e2018-05-01 12:11:27 -0700165 gem_create.in.domains = AMDGPU_GEM_DOMAIN_GTT;
166 if (!(use_flags & (BO_USE_SW_READ_OFTEN | BO_USE_SCANOUT)))
167 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_CPU_GTT_USWC;
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800168
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530169 /* Allocate the buffer with the preferred heap. */
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800170 ret = drmCommandWriteRead(drv_get_fd(bo->drv), DRM_AMDGPU_GEM_CREATE, &gem_create,
171 sizeof(gem_create));
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530172 if (ret < 0)
173 return ret;
174
Gurchetan Singh298b7572019-09-19 09:55:18 -0700175 for (plane = 0; plane < bo->meta.num_planes; plane++)
Shirish Sdf423df2017-04-18 16:21:59 +0530176 bo->handles[plane].u32 = gem_create.out.handle;
177
Bas Nieuwenhuizen7119d332020-02-07 20:20:30 +0100178 bo->meta.format_modifiers[0] = DRM_FORMAT_MOD_LINEAR;
179
Satyajitcdcebd82018-01-12 14:49:05 +0530180 return 0;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530181}
182
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100183static int amdgpu_create_bo(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
184 uint64_t use_flags)
Satyajitcdcebd82018-01-12 14:49:05 +0530185{
186 struct combination *combo;
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100187
188 combo = drv_get_combination(bo->drv, format, use_flags);
Satyajitcdcebd82018-01-12 14:49:05 +0530189 if (!combo)
190 return -EINVAL;
191
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100192 if (combo->metadata.tiling == TILE_TYPE_DRI) {
193 bool needs_alignment = false;
194#ifdef __ANDROID__
195 /*
196 * Currently, the gralloc API doesn't differentiate between allocation time and map
197 * time strides. A workaround for amdgpu DRI buffers is to always to align to 256 at
198 * allocation time.
199 *
200 * See b/115946221,b/117942643
201 */
202 if (use_flags & (BO_USE_SW_MASK))
203 needs_alignment = true;
204#endif
205 // See b/122049612
206 if (use_flags & (BO_USE_SCANOUT))
207 needs_alignment = true;
208
209 if (needs_alignment) {
210 uint32_t bytes_per_pixel = drv_bytes_per_pixel_from_format(format, 0);
211 width = ALIGN(width, 256 / bytes_per_pixel);
212 }
213
214 return dri_bo_create(bo, width, height, format, use_flags);
215 }
216
217 return amdgpu_create_bo_linear(bo, width, height, format, use_flags);
218}
219
220static int amdgpu_create_bo_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
221 uint32_t format, const uint64_t *modifiers,
222 uint32_t count)
223{
224 bool only_use_linear = true;
225
226 for (uint32_t i = 0; i < count; ++i)
227 if (modifiers[i] != DRM_FORMAT_MOD_LINEAR)
228 only_use_linear = false;
229
230 if (only_use_linear)
231 return amdgpu_create_bo_linear(bo, width, height, format, BO_USE_SCANOUT);
232
233 return dri_bo_create_with_modifiers(bo, width, height, format, modifiers, count);
234}
235
236static int amdgpu_import_bo(struct bo *bo, struct drv_import_fd_data *data)
237{
238 bool dri_tiling = data->format_modifiers[0] != DRM_FORMAT_MOD_LINEAR;
239 if (data->format_modifiers[0] == DRM_FORMAT_MOD_INVALID) {
240 struct combination *combo;
241 combo = drv_get_combination(bo->drv, data->format, data->use_flags);
242 if (!combo)
243 return -EINVAL;
244
245 dri_tiling = combo->metadata.tiling == TILE_TYPE_DRI;
246 }
247
248 if (dri_tiling)
Satyajitcdcebd82018-01-12 14:49:05 +0530249 return dri_bo_import(bo, data);
250 else
251 return drv_prime_bo_import(bo, data);
252}
253
254static int amdgpu_destroy_bo(struct bo *bo)
255{
256 if (bo->priv)
257 return dri_bo_destroy(bo);
258 else
259 return drv_gem_bo_destroy(bo);
260}
261
262static void *amdgpu_map_bo(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530263{
264 int ret;
265 union drm_amdgpu_gem_mmap gem_map;
266
Satyajitcdcebd82018-01-12 14:49:05 +0530267 if (bo->priv)
268 return dri_bo_map(bo, vma, plane, map_flags);
269
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530270 memset(&gem_map, 0, sizeof(gem_map));
Shirish Sdf423df2017-04-18 16:21:59 +0530271 gem_map.in.handle = bo->handles[plane].u32;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530272
273 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_AMDGPU_GEM_MMAP, &gem_map);
274 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700275 drv_log("DRM_IOCTL_AMDGPU_GEM_MMAP failed\n");
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530276 return MAP_FAILED;
277 }
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700278
Gurchetan Singh298b7572019-09-19 09:55:18 -0700279 vma->length = bo->meta.total_size;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530280
Gurchetan Singh298b7572019-09-19 09:55:18 -0700281 return mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700282 gem_map.out.addr_ptr);
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530283}
284
Satyajitcdcebd82018-01-12 14:49:05 +0530285static int amdgpu_unmap_bo(struct bo *bo, struct vma *vma)
286{
287 if (bo->priv)
288 return dri_bo_unmap(bo, vma);
289 else
290 return munmap(vma->addr, vma->length);
291}
292
Deepak Sharmaff66c802018-11-16 12:10:54 -0800293static int amdgpu_bo_invalidate(struct bo *bo, struct mapping *mapping)
294{
295 int ret;
296 union drm_amdgpu_gem_wait_idle wait_idle;
297
298 if (bo->priv)
299 return 0;
300
301 memset(&wait_idle, 0, sizeof(wait_idle));
302 wait_idle.in.handle = bo->handles[0].u32;
303 wait_idle.in.timeout = AMDGPU_TIMEOUT_INFINITE;
304
305 ret = drmCommandWriteRead(bo->drv->fd, DRM_AMDGPU_GEM_WAIT_IDLE, &wait_idle,
306 sizeof(wait_idle));
307
308 if (ret < 0) {
309 drv_log("DRM_AMDGPU_GEM_WAIT_IDLE failed with %d\n", ret);
310 return ret;
311 }
312
313 if (ret == 0 && wait_idle.out.status)
314 drv_log("DRM_AMDGPU_GEM_WAIT_IDLE BO is busy\n");
315
316 return 0;
317}
318
Gurchetan Singh0d44d482019-06-04 19:39:51 -0700319static uint32_t amdgpu_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Shirish Sdf423df2017-04-18 16:21:59 +0530320{
321 switch (format) {
Ricky Liang0b78e072017-11-10 09:17:17 +0800322 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
323 /* Camera subsystem requires NV12. */
324 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
325 return DRM_FORMAT_NV12;
326 /*HACK: See b/28671744 */
327 return DRM_FORMAT_XBGR8888;
Shirish Sdf423df2017-04-18 16:21:59 +0530328 case DRM_FORMAT_FLEX_YCbCr_420_888:
329 return DRM_FORMAT_NV12;
330 default:
331 return format;
332 }
333}
334
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700335const struct backend backend_amdgpu = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530336 .name = "amdgpu",
337 .init = amdgpu_init,
338 .close = amdgpu_close,
Satyajitcdcebd82018-01-12 14:49:05 +0530339 .bo_create = amdgpu_create_bo,
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100340 .bo_create_with_modifiers = amdgpu_create_bo_with_modifiers,
Satyajitcdcebd82018-01-12 14:49:05 +0530341 .bo_destroy = amdgpu_destroy_bo,
342 .bo_import = amdgpu_import_bo,
343 .bo_map = amdgpu_map_bo,
344 .bo_unmap = amdgpu_unmap_bo,
Deepak Sharmaff66c802018-11-16 12:10:54 -0800345 .bo_invalidate = amdgpu_bo_invalidate,
Shirish Sdf423df2017-04-18 16:21:59 +0530346 .resolve_format = amdgpu_resolve_format,
ChromeOS Developer44588bb2020-03-02 16:32:09 +0100347 .num_planes_from_modifier = dri_num_planes_from_modifier,
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530348};
349
350#endif