blob: 8060cea584d2b6aaa06f983ffbc05045d720de79 [file] [log] [blame]
Rajesh Yadav7f79cb52018-01-22 18:29:06 +05301/*
2 * Copyright 2018 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7#ifdef DRV_MSM
8
Tanmay Shah617ee712018-07-11 16:41:05 -07009#include <errno.h>
10#include <msm_drm.h>
11#include <stdio.h>
12#include <string.h>
13#include <sys/mman.h>
14#include <xf86drm.h>
15
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053016#include "drv_priv.h"
17#include "helpers.h"
18#include "util.h"
19
Tanmay Shah617ee712018-07-11 16:41:05 -070020#define DEFAULT_ALIGNMENT 64
Stéphane Marchesin23e006a2018-02-28 16:37:46 -080021
Gurchetan Singhb131c9d2018-08-28 14:17:05 -070022static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
Gurchetan Singh71bc6652018-09-17 17:42:05 -070023 DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
24 DRM_FORMAT_XRGB8888 };
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053025
Tanmay Shah617ee712018-07-11 16:41:05 -070026static const uint32_t texture_source_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_R8,
27 DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID };
Alexandre Courbot1805a9b2018-05-21 19:05:10 +090028
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053029static int msm_init(struct driver *drv)
30{
31 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
32 &LINEAR_METADATA, BO_USE_RENDER_MASK);
33
Tanmay Shah617ee712018-07-11 16:41:05 -070034 drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
Alexandre Courbot1805a9b2018-05-21 19:05:10 +090035 &LINEAR_METADATA, BO_USE_TEXTURE_MASK | BO_USE_HW_VIDEO_DECODER);
36
Gurchetan Singh71bc6652018-09-17 17:42:05 -070037 /* Android CTS tests require this. */
38 drv_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
39
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053040 return drv_modify_linear_combinations(drv);
41}
42
Tanmay Shah617ee712018-07-11 16:41:05 -070043/* msm_bo_create will create linear buffers for now */
Stéphane Marchesin23e006a2018-02-28 16:37:46 -080044static int msm_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
Alistair Strachan0cfaaa52018-03-19 14:03:23 -070045 uint64_t flags)
Stéphane Marchesin23e006a2018-02-28 16:37:46 -080046{
Tanmay Shah617ee712018-07-11 16:41:05 -070047 struct drm_msm_gem_new req;
48 uint32_t stride, alignw, alignh;
49 int ret;
50 size_t i;
51
52 /* will get alignment from libadreno eventually */
53 alignw = ALIGN(width, DEFAULT_ALIGNMENT);
54 alignh = ALIGN(height, DEFAULT_ALIGNMENT);
55
56 /* HAL_PIXEL_FORMAT_YV12 requires that the buffer's height not be aligned. */
57 if (bo->format == DRM_FORMAT_YVU420_ANDROID)
58 alignh = bo->height;
Stéphane Marchesin23e006a2018-02-28 16:37:46 -080059
Keiichi Watanabeaf94db92018-08-06 18:37:21 +090060 /*
61 * The extra 12KB at the end are a requirement of the Venus codec driver.
62 * Since |height| will be multiplied by 3/2 in drv_dumb_bo_create,
63 * we multiply this padding by 2/3 here.
64 */
Alexandre Courbot1805a9b2018-05-21 19:05:10 +090065 if (bo->format == DRM_FORMAT_NV12)
Tanmay Shah617ee712018-07-11 16:41:05 -070066 alignh += 2 * DIV_ROUND_UP(0x3000, 3 * alignw);
Alexandre Courbot1805a9b2018-05-21 19:05:10 +090067
Tanmay Shah617ee712018-07-11 16:41:05 -070068 stride = drv_stride_from_format(format, alignw, 0);
69
70 /* Calculate size and assign stride, size, offset to each plane based on format */
71 drv_bo_from_format(bo, stride, alignh, format);
72
73 memset(&req, 0, sizeof(req));
74 req.flags = MSM_BO_WC | MSM_BO_SCANOUT;
75 req.size = bo->total_size;
76
77 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MSM_GEM_NEW, &req);
78 if (ret) {
79 drv_log("DRM_IOCTL_MSM_GEM_NEW failed with %s\n", strerror(errno));
80 return ret;
81 }
82
83 /*
84 * Though we use only one plane, we need to set handle for
85 * all planes to pass kernel checks
86 */
87 for (i = 0; i < bo->num_planes; i++) {
88 bo->handles[i].u32 = req.handle;
89 }
90
91 return 0;
92}
93
94static void *msm_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
95{
96 int ret;
97 struct drm_msm_gem_info req;
98
99 memset(&req, 0, sizeof(req));
100 req.handle = bo->handles[0].u32;
101
102 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MSM_GEM_INFO, &req);
103 if (ret) {
104 drv_log("DRM_IOCLT_MSM_GEM_INFO failed with %s\n", strerror(errno));
105 return MAP_FAILED;
106 }
107 vma->length = bo->total_size;
108
109 return mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
110 req.offset);
Stéphane Marchesin23e006a2018-02-28 16:37:46 -0800111}
112
Rajesh Yadav7f79cb52018-01-22 18:29:06 +0530113const struct backend backend_msm = {
114 .name = "msm",
115 .init = msm_init,
Stéphane Marchesin23e006a2018-02-28 16:37:46 -0800116 .bo_create = msm_bo_create,
Tanmay Shah617ee712018-07-11 16:41:05 -0700117 .bo_destroy = drv_gem_bo_destroy,
Rajesh Yadav7f79cb52018-01-22 18:29:06 +0530118 .bo_import = drv_prime_bo_import,
Tanmay Shah617ee712018-07-11 16:41:05 -0700119 .bo_map = msm_bo_map,
Rajesh Yadav7f79cb52018-01-22 18:29:06 +0530120 .bo_unmap = drv_bo_munmap,
121};
Rajesh Yadav7f79cb52018-01-22 18:29:06 +0530122#endif /* DRV_MSM */