• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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_MEDIATEK
8 
9 // clang-format off
10 #include <stdio.h>
11 #include <string.h>
12 #include <sys/mman.h>
13 #include <xf86drm.h>
14 #include <mediatek_drm.h>
15 // clang-format on
16 
17 #include "drv_priv.h"
18 #include "helpers.h"
19 #include "util.h"
20 
21 struct mediatek_private_map_data {
22 	void *cached_addr;
23 	void *gem_addr;
24 };
25 
26 static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
27 						  DRM_FORMAT_BGR888,   DRM_FORMAT_RGB565,
28 						  DRM_FORMAT_XBGR8888, DRM_FORMAT_XRGB8888 };
29 
30 static const uint32_t texture_source_formats[] = { DRM_FORMAT_R8, DRM_FORMAT_YVU420,
31 						   DRM_FORMAT_YVU420_ANDROID };
32 
mediatek_init(struct driver * drv)33 static int mediatek_init(struct driver *drv)
34 {
35 	drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
36 			     &LINEAR_METADATA, BO_USE_RENDER_MASK);
37 
38 	drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
39 			     &LINEAR_METADATA, BO_USE_TEXTURE_MASK);
40 
41 	return drv_modify_linear_combinations(drv);
42 }
43 
mediatek_bo_create(struct bo * bo,uint32_t width,uint32_t height,uint32_t format,uint64_t use_flags)44 static int mediatek_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
45 			      uint64_t use_flags)
46 {
47 	int ret;
48 	size_t plane;
49 	uint32_t stride;
50 	struct drm_mtk_gem_create gem_create;
51 
52 	/*
53 	 * Since the ARM L1 cache line size is 64 bytes, align to that as a
54 	 * performance optimization.
55 	 */
56 	stride = drv_stride_from_format(format, width, 0);
57 	stride = ALIGN(stride, 64);
58 	drv_bo_from_format(bo, stride, height, format);
59 
60 	memset(&gem_create, 0, sizeof(gem_create));
61 	gem_create.size = bo->total_size;
62 
63 	ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MTK_GEM_CREATE, &gem_create);
64 	if (ret) {
65 		drv_log("DRM_IOCTL_MTK_GEM_CREATE failed (size=%llu)\n", gem_create.size);
66 		return ret;
67 	}
68 
69 	for (plane = 0; plane < bo->num_planes; plane++)
70 		bo->handles[plane].u32 = gem_create.handle;
71 
72 	return 0;
73 }
74 
mediatek_bo_map(struct bo * bo,struct vma * vma,size_t plane,uint32_t map_flags)75 static void *mediatek_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
76 {
77 	int ret;
78 	struct drm_mtk_gem_map_off gem_map;
79 	struct mediatek_private_map_data *priv;
80 
81 	memset(&gem_map, 0, sizeof(gem_map));
82 	gem_map.handle = bo->handles[0].u32;
83 
84 	ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MTK_GEM_MAP_OFFSET, &gem_map);
85 	if (ret) {
86 		drv_log("DRM_IOCTL_MTK_GEM_MAP_OFFSET failed\n");
87 		return MAP_FAILED;
88 	}
89 
90 	void *addr = mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
91 			  gem_map.offset);
92 
93 	vma->length = bo->total_size;
94 
95 	if (bo->use_flags & BO_USE_RENDERSCRIPT) {
96 		priv = calloc(1, sizeof(*priv));
97 		priv->cached_addr = calloc(1, bo->total_size);
98 		priv->gem_addr = addr;
99 		vma->priv = priv;
100 		addr = priv->cached_addr;
101 	}
102 
103 	return addr;
104 }
105 
mediatek_bo_unmap(struct bo * bo,struct vma * vma)106 static int mediatek_bo_unmap(struct bo *bo, struct vma *vma)
107 {
108 	if (vma->priv) {
109 		struct mediatek_private_map_data *priv = vma->priv;
110 		vma->addr = priv->gem_addr;
111 		free(priv->cached_addr);
112 		free(priv);
113 		vma->priv = NULL;
114 	}
115 
116 	return munmap(vma->addr, vma->length);
117 }
118 
mediatek_bo_invalidate(struct bo * bo,struct mapping * mapping)119 static int mediatek_bo_invalidate(struct bo *bo, struct mapping *mapping)
120 {
121 	if (mapping->vma->priv) {
122 		struct mediatek_private_map_data *priv = mapping->vma->priv;
123 		memcpy(priv->cached_addr, priv->gem_addr, bo->total_size);
124 	}
125 
126 	return 0;
127 }
128 
mediatek_bo_flush(struct bo * bo,struct mapping * mapping)129 static int mediatek_bo_flush(struct bo *bo, struct mapping *mapping)
130 {
131 	struct mediatek_private_map_data *priv = mapping->vma->priv;
132 	if (priv && (mapping->vma->map_flags & BO_MAP_WRITE))
133 		memcpy(priv->gem_addr, priv->cached_addr, bo->total_size);
134 
135 	return 0;
136 }
137 
mediatek_resolve_format(uint32_t format,uint64_t use_flags)138 static uint32_t mediatek_resolve_format(uint32_t format, uint64_t use_flags)
139 {
140 	switch (format) {
141 	case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
142 		/*HACK: See b/28671744 */
143 		return DRM_FORMAT_XBGR8888;
144 	case DRM_FORMAT_FLEX_YCbCr_420_888:
145 		return DRM_FORMAT_YVU420;
146 	default:
147 		return format;
148 	}
149 }
150 
151 const struct backend backend_mediatek = {
152 	.name = "mediatek",
153 	.init = mediatek_init,
154 	.bo_create = mediatek_bo_create,
155 	.bo_destroy = drv_gem_bo_destroy,
156 	.bo_import = drv_prime_bo_import,
157 	.bo_map = mediatek_bo_map,
158 	.bo_unmap = mediatek_bo_unmap,
159 	.bo_invalidate = mediatek_bo_invalidate,
160 	.bo_flush = mediatek_bo_flush,
161 	.resolve_format = mediatek_resolve_format,
162 };
163 
164 #endif
165