Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 1 | /* |
| 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 Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 9 | #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 Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 16 | #include "drv_priv.h" |
| 17 | #include "helpers.h" |
| 18 | #include "util.h" |
| 19 | |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 20 | #define DEFAULT_ALIGNMENT 64 |
Stéphane Marchesin | 23e006a | 2018-02-28 16:37:46 -0800 | [diff] [blame] | 21 | |
Gurchetan Singh | b131c9d | 2018-08-28 14:17:05 -0700 | [diff] [blame] | 22 | static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888, |
Gurchetan Singh | 71bc665 | 2018-09-17 17:42:05 -0700 | [diff] [blame] | 23 | DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888, |
| 24 | DRM_FORMAT_XRGB8888 }; |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 25 | |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 26 | static const uint32_t texture_source_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_R8, |
| 27 | DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID }; |
Alexandre Courbot | 1805a9b | 2018-05-21 19:05:10 +0900 | [diff] [blame] | 28 | |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 29 | static 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 Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 34 | drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats), |
Alexandre Courbot | 1805a9b | 2018-05-21 19:05:10 +0900 | [diff] [blame] | 35 | &LINEAR_METADATA, BO_USE_TEXTURE_MASK | BO_USE_HW_VIDEO_DECODER); |
| 36 | |
Gurchetan Singh | 71bc665 | 2018-09-17 17:42:05 -0700 | [diff] [blame] | 37 | /* Android CTS tests require this. */ |
| 38 | drv_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK); |
| 39 | |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 40 | return drv_modify_linear_combinations(drv); |
| 41 | } |
| 42 | |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 43 | /* msm_bo_create will create linear buffers for now */ |
Stéphane Marchesin | 23e006a | 2018-02-28 16:37:46 -0800 | [diff] [blame] | 44 | static int msm_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format, |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 45 | uint64_t flags) |
Stéphane Marchesin | 23e006a | 2018-02-28 16:37:46 -0800 | [diff] [blame] | 46 | { |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 47 | 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 Marchesin | 23e006a | 2018-02-28 16:37:46 -0800 | [diff] [blame] | 59 | |
Keiichi Watanabe | af94db9 | 2018-08-06 18:37:21 +0900 | [diff] [blame] | 60 | /* |
| 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 Courbot | 1805a9b | 2018-05-21 19:05:10 +0900 | [diff] [blame] | 65 | if (bo->format == DRM_FORMAT_NV12) |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 66 | alignh += 2 * DIV_ROUND_UP(0x3000, 3 * alignw); |
Alexandre Courbot | 1805a9b | 2018-05-21 19:05:10 +0900 | [diff] [blame] | 67 | |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 68 | 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 | |
| 94 | static 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 Marchesin | 23e006a | 2018-02-28 16:37:46 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 113 | const struct backend backend_msm = { |
| 114 | .name = "msm", |
| 115 | .init = msm_init, |
Stéphane Marchesin | 23e006a | 2018-02-28 16:37:46 -0800 | [diff] [blame] | 116 | .bo_create = msm_bo_create, |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 117 | .bo_destroy = drv_gem_bo_destroy, |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 118 | .bo_import = drv_prime_bo_import, |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 119 | .bo_map = msm_bo_map, |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 120 | .bo_unmap = drv_bo_munmap, |
| 121 | }; |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 122 | #endif /* DRV_MSM */ |