blob: c3d8ffa88a2a17348bdeddfa8424bd069d032356 [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
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +010029struct amdgpu_priv {
Satyajitcdcebd82018-01-12 14:49:05 +053030 struct dri_driver dri;
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +010031 int drm_version;
32};
33
Gurchetan Singh767c5382018-05-05 00:42:12 +000034const static uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
Drew Davenport293d9e32018-06-20 15:46:50 -060035 DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
36 DRM_FORMAT_XRGB8888 };
Gurchetan Singh179687e2016-10-28 10:07:35 -070037
Gurchetan Singh8d884742020-03-24 13:48:54 -070038const static uint32_t texture_source_formats[] = { DRM_FORMAT_GR88, DRM_FORMAT_R8,
39 DRM_FORMAT_NV21, DRM_FORMAT_NV12,
Hirokazu Honda3b8d4d02019-07-31 16:35:52 +090040 DRM_FORMAT_YVU420_ANDROID, DRM_FORMAT_YVU420 };
Shirish Sdf423df2017-04-18 16:21:59 +053041
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053042static int amdgpu_init(struct driver *drv)
43{
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +010044 struct amdgpu_priv *priv;
45 drmVersionPtr drm_version;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080046 struct format_metadata metadata;
Gurchetan Singha1892b22017-09-28 16:40:52 -070047 uint64_t use_flags = BO_USE_RENDER_MASK;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053048
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +010049 priv = calloc(1, sizeof(struct amdgpu_priv));
50 if (!priv)
Satyajitcdcebd82018-01-12 14:49:05 +053051 return -ENOMEM;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053052
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +010053 drm_version = drmGetVersion(drv_get_fd(drv));
54 if (!drm_version) {
55 free(priv);
Satyajitcdcebd82018-01-12 14:49:05 +053056 return -ENODEV;
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +010057 }
58
59 priv->drm_version = drm_version->version_minor;
60 drmFreeVersion(drm_version);
61
Bas Nieuwenhuizen3cf8c922018-03-23 17:21:37 +010062 drv->priv = priv;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053063
Satyajitcdcebd82018-01-12 14:49:05 +053064 if (dri_init(drv, DRI_PATH, "radeonsi")) {
65 free(priv);
66 drv->priv = NULL;
67 return -ENODEV;
68 }
Shirish Sdf423df2017-04-18 16:21:59 +053069
Satyajitcdcebd82018-01-12 14:49:05 +053070 metadata.tiling = TILE_TYPE_LINEAR;
71 metadata.priority = 1;
Kristian H. Kristensenbc8c5932017-10-24 18:36:32 -070072 metadata.modifier = DRM_FORMAT_MOD_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080073
Gurchetan Singhd3001452017-11-03 17:18:36 -070074 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
75 &metadata, use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080076
Satyajitcdcebd82018-01-12 14:49:05 +053077 drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
78 &metadata, BO_USE_TEXTURE_MASK);
79
Hirokazu Honda3b8d4d02019-07-31 16:35:52 +090080 /*
81 * Chrome uses DMA-buf mmap to write to YV12 buffers, which are then accessed by the
82 * Video Encoder Accelerator (VEA). It could also support NV12 potentially in the future.
83 */
84 drv_modify_combination(drv, DRM_FORMAT_YVU420, &metadata, BO_USE_HW_VIDEO_ENCODER);
85 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata, BO_USE_HW_VIDEO_ENCODER);
86
Gurchetan Singh71bc6652018-09-17 17:42:05 -070087 /* Android CTS tests require this. */
88 drv_add_combination(drv, DRM_FORMAT_BGR888, &metadata, BO_USE_SW_MASK);
89
Satyajitcdcebd82018-01-12 14:49:05 +053090 /* Linear formats supported by display. */
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080091 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
92 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
Drew Davenport5d215242019-03-25 09:18:42 -060093 drv_modify_combination(drv, DRM_FORMAT_ABGR8888, &metadata, BO_USE_SCANOUT);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080094 drv_modify_combination(drv, DRM_FORMAT_XBGR8888, &metadata, BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080095
Satyajitcdcebd82018-01-12 14:49:05 +053096 /* YUV formats for camera and display. */
97 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
Miguel Casasdea0ccb2018-07-02 09:40:25 -040098 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SCANOUT |
99 BO_USE_HW_VIDEO_DECODER);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800100
Satyajitcdcebd82018-01-12 14:49:05 +0530101 drv_modify_combination(drv, DRM_FORMAT_NV21, &metadata, BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800102
Satyajitcdcebd82018-01-12 14:49:05 +0530103 /*
104 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
David Stevens49518142020-06-15 13:48:48 +0900105 * from camera and input/output from hardware decoder/encoder.
Satyajitcdcebd82018-01-12 14:49:05 +0530106 */
107 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
David Stevens49518142020-06-15 13:48:48 +0900108 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
109 BO_USE_HW_VIDEO_ENCODER);
Satyajitcdcebd82018-01-12 14:49:05 +0530110
111 /*
112 * The following formats will be allocated by the DRI backend and may be potentially tiled.
113 * Since format modifier support hasn't been implemented fully yet, it's not
114 * possible to enumerate the different types of buffers (like i915 can).
115 */
116 use_flags &= ~BO_USE_RENDERSCRIPT;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700117 use_flags &= ~BO_USE_SW_WRITE_OFTEN;
118 use_flags &= ~BO_USE_SW_READ_OFTEN;
119 use_flags &= ~BO_USE_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800120
Satyajitcdcebd82018-01-12 14:49:05 +0530121 metadata.tiling = TILE_TYPE_DRI;
122 metadata.priority = 2;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800123
Gurchetan Singhd3001452017-11-03 17:18:36 -0700124 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
125 &metadata, use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800126
Satyajitcdcebd82018-01-12 14:49:05 +0530127 /* Potentially tiled formats supported by display. */
128 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
129 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
Bas Nieuwenhuizen582bdbf2019-04-03 18:12:12 +0200130 drv_modify_combination(drv, DRM_FORMAT_ABGR8888, &metadata, BO_USE_SCANOUT);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800131 drv_modify_combination(drv, DRM_FORMAT_XBGR8888, &metadata, BO_USE_SCANOUT);
Gurchetan Singhd3001452017-11-03 17:18:36 -0700132 return 0;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530133}
134
135static void amdgpu_close(struct driver *drv)
136{
Satyajitcdcebd82018-01-12 14:49:05 +0530137 dri_close(drv);
138 free(drv->priv);
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530139 drv->priv = NULL;
140}
141
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100142static int amdgpu_create_bo_linear(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
143 uint64_t use_flags)
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530144{
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530145 int ret;
Satyajitcdcebd82018-01-12 14:49:05 +0530146 uint32_t plane, stride;
Satyajitcdcebd82018-01-12 14:49:05 +0530147 union drm_amdgpu_gem_create gem_create;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530148
Satyajitcdcebd82018-01-12 14:49:05 +0530149 stride = drv_stride_from_format(format, width, 0);
Keiichi Watanabe79155d72018-08-13 16:44:54 +0900150 stride = ALIGN(stride, 256);
Satyajitcdcebd82018-01-12 14:49:05 +0530151
152 drv_bo_from_format(bo, stride, height, format);
Shirish Sdf423df2017-04-18 16:21:59 +0530153
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530154 memset(&gem_create, 0, sizeof(gem_create));
Gurchetan Singh298b7572019-09-19 09:55:18 -0700155 gem_create.in.bo_size = bo->meta.total_size;
Satyajitcdcebd82018-01-12 14:49:05 +0530156 gem_create.in.alignment = 256;
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800157 gem_create.in.domain_flags = 0;
Satyajitcdcebd82018-01-12 14:49:05 +0530158
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700159 if (use_flags & (BO_USE_LINEAR | BO_USE_SW_MASK))
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800160 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
161
Deepak Sharma62a9c4e2018-05-01 12:11:27 -0700162 gem_create.in.domains = AMDGPU_GEM_DOMAIN_GTT;
Bas Nieuwenhuizen91d36972020-04-27 19:59:29 +0000163 if (!(use_flags & (BO_USE_SW_READ_OFTEN | BO_USE_SCANOUT)))
Jao-ke Chin-Lee5481e3c2020-04-10 00:12:12 +0000164 gem_create.in.domain_flags |= AMDGPU_GEM_CREATE_CPU_GTT_USWC;
Dominik Behrfa17cdd2017-11-30 12:23:06 -0800165
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530166 /* Allocate the buffer with the preferred heap. */
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800167 ret = drmCommandWriteRead(drv_get_fd(bo->drv), DRM_AMDGPU_GEM_CREATE, &gem_create,
168 sizeof(gem_create));
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530169 if (ret < 0)
170 return ret;
171
Gurchetan Singh298b7572019-09-19 09:55:18 -0700172 for (plane = 0; plane < bo->meta.num_planes; plane++)
Shirish Sdf423df2017-04-18 16:21:59 +0530173 bo->handles[plane].u32 = gem_create.out.handle;
174
Bas Nieuwenhuizen7119d332020-02-07 20:20:30 +0100175 bo->meta.format_modifiers[0] = DRM_FORMAT_MOD_LINEAR;
176
Satyajitcdcebd82018-01-12 14:49:05 +0530177 return 0;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530178}
179
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100180static int amdgpu_create_bo(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
181 uint64_t use_flags)
Satyajitcdcebd82018-01-12 14:49:05 +0530182{
183 struct combination *combo;
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100184
185 combo = drv_get_combination(bo->drv, format, use_flags);
Satyajitcdcebd82018-01-12 14:49:05 +0530186 if (!combo)
187 return -EINVAL;
188
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100189 if (combo->metadata.tiling == TILE_TYPE_DRI) {
190 bool needs_alignment = false;
191#ifdef __ANDROID__
192 /*
193 * Currently, the gralloc API doesn't differentiate between allocation time and map
194 * time strides. A workaround for amdgpu DRI buffers is to always to align to 256 at
195 * allocation time.
196 *
197 * See b/115946221,b/117942643
198 */
199 if (use_flags & (BO_USE_SW_MASK))
200 needs_alignment = true;
201#endif
202 // See b/122049612
203 if (use_flags & (BO_USE_SCANOUT))
204 needs_alignment = true;
205
206 if (needs_alignment) {
207 uint32_t bytes_per_pixel = drv_bytes_per_pixel_from_format(format, 0);
208 width = ALIGN(width, 256 / bytes_per_pixel);
209 }
210
211 return dri_bo_create(bo, width, height, format, use_flags);
212 }
213
214 return amdgpu_create_bo_linear(bo, width, height, format, use_flags);
215}
216
217static int amdgpu_create_bo_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
218 uint32_t format, const uint64_t *modifiers,
219 uint32_t count)
220{
221 bool only_use_linear = true;
222
223 for (uint32_t i = 0; i < count; ++i)
224 if (modifiers[i] != DRM_FORMAT_MOD_LINEAR)
225 only_use_linear = false;
226
227 if (only_use_linear)
228 return amdgpu_create_bo_linear(bo, width, height, format, BO_USE_SCANOUT);
229
230 return dri_bo_create_with_modifiers(bo, width, height, format, modifiers, count);
231}
232
233static int amdgpu_import_bo(struct bo *bo, struct drv_import_fd_data *data)
234{
235 bool dri_tiling = data->format_modifiers[0] != DRM_FORMAT_MOD_LINEAR;
236 if (data->format_modifiers[0] == DRM_FORMAT_MOD_INVALID) {
237 struct combination *combo;
238 combo = drv_get_combination(bo->drv, data->format, data->use_flags);
239 if (!combo)
240 return -EINVAL;
241
242 dri_tiling = combo->metadata.tiling == TILE_TYPE_DRI;
243 }
244
245 if (dri_tiling)
Satyajitcdcebd82018-01-12 14:49:05 +0530246 return dri_bo_import(bo, data);
247 else
248 return drv_prime_bo_import(bo, data);
249}
250
251static int amdgpu_destroy_bo(struct bo *bo)
252{
253 if (bo->priv)
254 return dri_bo_destroy(bo);
255 else
256 return drv_gem_bo_destroy(bo);
257}
258
259static void *amdgpu_map_bo(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530260{
261 int ret;
262 union drm_amdgpu_gem_mmap gem_map;
263
Satyajitcdcebd82018-01-12 14:49:05 +0530264 if (bo->priv)
265 return dri_bo_map(bo, vma, plane, map_flags);
266
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530267 memset(&gem_map, 0, sizeof(gem_map));
Shirish Sdf423df2017-04-18 16:21:59 +0530268 gem_map.in.handle = bo->handles[plane].u32;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530269
270 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_AMDGPU_GEM_MMAP, &gem_map);
271 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700272 drv_log("DRM_IOCTL_AMDGPU_GEM_MMAP failed\n");
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530273 return MAP_FAILED;
274 }
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700275
Gurchetan Singh298b7572019-09-19 09:55:18 -0700276 vma->length = bo->meta.total_size;
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530277
Gurchetan Singh298b7572019-09-19 09:55:18 -0700278 return mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700279 gem_map.out.addr_ptr);
Pratik Vishwakarmabc1b5352016-12-12 14:22:10 +0530280}
281
Satyajitcdcebd82018-01-12 14:49:05 +0530282static int amdgpu_unmap_bo(struct bo *bo, struct vma *vma)
283{
284 if (bo->priv)
285 return dri_bo_unmap(bo, vma);
286 else
287 return munmap(vma->addr, vma->length);
288}
289
Deepak Sharmaff66c802018-11-16 12:10:54 -0800290static int amdgpu_bo_invalidate(struct bo *bo, struct mapping *mapping)
291{
292 int ret;
293 union drm_amdgpu_gem_wait_idle wait_idle;
294
295 if (bo->priv)
296 return 0;
297
298 memset(&wait_idle, 0, sizeof(wait_idle));
299 wait_idle.in.handle = bo->handles[0].u32;
300 wait_idle.in.timeout = AMDGPU_TIMEOUT_INFINITE;
301
302 ret = drmCommandWriteRead(bo->drv->fd, DRM_AMDGPU_GEM_WAIT_IDLE, &wait_idle,
303 sizeof(wait_idle));
304
305 if (ret < 0) {
306 drv_log("DRM_AMDGPU_GEM_WAIT_IDLE failed with %d\n", ret);
307 return ret;
308 }
309
310 if (ret == 0 && wait_idle.out.status)
311 drv_log("DRM_AMDGPU_GEM_WAIT_IDLE BO is busy\n");
312
313 return 0;
314}
315
Gurchetan Singh0d44d482019-06-04 19:39:51 -0700316static uint32_t amdgpu_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Shirish Sdf423df2017-04-18 16:21:59 +0530317{
318 switch (format) {
Ricky Liang0b78e072017-11-10 09:17:17 +0800319 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
320 /* Camera subsystem requires NV12. */
321 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
322 return DRM_FORMAT_NV12;
323 /*HACK: See b/28671744 */
324 return DRM_FORMAT_XBGR8888;
Shirish Sdf423df2017-04-18 16:21:59 +0530325 case DRM_FORMAT_FLEX_YCbCr_420_888:
326 return DRM_FORMAT_NV12;
327 default:
328 return format;
329 }
330}
331
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700332const struct backend backend_amdgpu = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530333 .name = "amdgpu",
334 .init = amdgpu_init,
335 .close = amdgpu_close,
Satyajitcdcebd82018-01-12 14:49:05 +0530336 .bo_create = amdgpu_create_bo,
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100337 .bo_create_with_modifiers = amdgpu_create_bo_with_modifiers,
Satyajitcdcebd82018-01-12 14:49:05 +0530338 .bo_destroy = amdgpu_destroy_bo,
339 .bo_import = amdgpu_import_bo,
340 .bo_map = amdgpu_map_bo,
341 .bo_unmap = amdgpu_unmap_bo,
Deepak Sharmaff66c802018-11-16 12:10:54 -0800342 .bo_invalidate = amdgpu_bo_invalidate,
Shirish Sdf423df2017-04-18 16:21:59 +0530343 .resolve_format = amdgpu_resolve_format,
ChromeOS Developer44588bb2020-03-02 16:32:09 +0100344 .num_planes_from_modifier = dri_num_planes_from_modifier,
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530345};
346
347#endif