blob: cfb60b36626da4a3dfbcd249c3a6ac3f547e357e [file] [log] [blame]
JB Tsai0c16a0f2015-03-19 14:30:31 +08001/*
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
Gurchetan Singh46faf6b2016-08-05 14:40:07 -07007#ifdef DRV_MEDIATEK
JB Tsai0c16a0f2015-03-19 14:30:31 +08008
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -08009// clang-format off
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -070010#include <stdio.h>
JB Tsai0c16a0f2015-03-19 14:30:31 +080011#include <string.h>
Gurchetan Singhef920532016-08-12 16:38:25 -070012#include <sys/mman.h>
JB Tsai0c16a0f2015-03-19 14:30:31 +080013#include <xf86drm.h>
14#include <mediatek_drm.h>
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080015// clang-format on
Gurchetan Singhef920532016-08-12 16:38:25 -070016
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070017#include "drv_priv.h"
JB Tsai0c16a0f2015-03-19 14:30:31 +080018#include "helpers.h"
Gurchetan Singh179687e2016-10-28 10:07:35 -070019#include "util.h"
20
Gurchetan Singh469a3aa2017-08-03 18:17:34 -070021struct mediatek_private_map_data {
22 void *cached_addr;
23 void *gem_addr;
24};
25
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070026static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
27 DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
28 DRM_FORMAT_XRGB8888 };
29
30static const uint32_t texture_source_formats[] = { DRM_FORMAT_R8, DRM_FORMAT_YVU420,
31 DRM_FORMAT_YVU420_ANDROID };
Gurchetan Singh179687e2016-10-28 10:07:35 -070032
33static int mediatek_init(struct driver *drv)
34{
Gurchetan Singhd3001452017-11-03 17:18:36 -070035 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
36 &LINEAR_METADATA, BO_USE_RENDER_MASK);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070037
Gurchetan Singhd3001452017-11-03 17:18:36 -070038 drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
39 &LINEAR_METADATA, BO_USE_TEXTURE_MASK);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070040
41 return drv_modify_linear_combinations(drv);
Gurchetan Singh179687e2016-10-28 10:07:35 -070042}
JB Tsai0c16a0f2015-03-19 14:30:31 +080043
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080044static int mediatek_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -070045 uint64_t use_flags)
JB Tsai0c16a0f2015-03-19 14:30:31 +080046{
JB Tsai0c16a0f2015-03-19 14:30:31 +080047 int ret;
Gurchetan Singh42cc6d62016-08-29 18:19:19 -070048 size_t plane;
Gurchetan Singh6423ecb2017-03-29 08:23:40 -070049 uint32_t stride;
Gurchetan Singh42cc6d62016-08-29 18:19:19 -070050 struct drm_mtk_gem_create gem_create;
JB Tsai0c16a0f2015-03-19 14:30:31 +080051
Gurchetan Singh6ea14ba2017-02-08 15:09:13 -080052 /*
53 * Since the ARM L1 cache line size is 64 bytes, align to that as a
54 * performance optimization.
55 */
Gurchetan Singh6423ecb2017-03-29 08:23:40 -070056 stride = drv_stride_from_format(format, width, 0);
57 stride = ALIGN(stride, 64);
58 drv_bo_from_format(bo, stride, height, format);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050059
JB Tsai0c16a0f2015-03-19 14:30:31 +080060 memset(&gem_create, 0, sizeof(gem_create));
Gurchetan Singha40ca9e2016-08-29 19:51:45 -070061 gem_create.size = bo->total_size;
JB Tsai0c16a0f2015-03-19 14:30:31 +080062
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070063 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MTK_GEM_CREATE, &gem_create);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -070064 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -070065 drv_log("DRM_IOCTL_MTK_GEM_CREATE failed (size=%llu)\n", gem_create.size);
JB Tsai0c16a0f2015-03-19 14:30:31 +080066 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -070067 }
JB Tsai0c16a0f2015-03-19 14:30:31 +080068
Gurchetan Singh42cc6d62016-08-29 18:19:19 -070069 for (plane = 0; plane < bo->num_planes; plane++)
70 bo->handles[plane].u32 = gem_create.handle;
JB Tsai0c16a0f2015-03-19 14:30:31 +080071
72 return 0;
73}
74
Gurchetan Singhee43c302017-11-14 18:20:27 -080075static void *mediatek_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Gurchetan Singhef920532016-08-12 16:38:25 -070076{
77 int ret;
78 struct drm_mtk_gem_map_off gem_map;
Gurchetan Singh469a3aa2017-08-03 18:17:34 -070079 struct mediatek_private_map_data *priv;
Gurchetan Singhef920532016-08-12 16:38:25 -070080
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) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -070086 drv_log("DRM_IOCTL_MTK_GEM_MAP_OFFSET failed\n");
Gurchetan Singhef920532016-08-12 16:38:25 -070087 return MAP_FAILED;
88 }
89
Gurchetan Singhcfb88762017-09-28 17:14:50 -070090 void *addr = mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
91 gem_map.offset);
Gurchetan Singh469a3aa2017-08-03 18:17:34 -070092
Gurchetan Singhee43c302017-11-14 18:20:27 -080093 vma->length = bo->total_size;
Gurchetan Singh1a31e602016-10-06 10:58:00 -070094
Gurchetan Singha1892b22017-09-28 16:40:52 -070095 if (bo->use_flags & BO_USE_RENDERSCRIPT) {
Gurchetan Singh469a3aa2017-08-03 18:17:34 -070096 priv = calloc(1, sizeof(*priv));
97 priv->cached_addr = calloc(1, bo->total_size);
98 priv->gem_addr = addr;
Gurchetan Singhee43c302017-11-14 18:20:27 -080099 vma->priv = priv;
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700100 addr = priv->cached_addr;
101 }
102
103 return addr;
104}
105
Gurchetan Singhee43c302017-11-14 18:20:27 -0800106static int mediatek_bo_unmap(struct bo *bo, struct vma *vma)
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700107{
Gurchetan Singhee43c302017-11-14 18:20:27 -0800108 if (vma->priv) {
109 struct mediatek_private_map_data *priv = vma->priv;
110 vma->addr = priv->gem_addr;
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700111 free(priv->cached_addr);
112 free(priv);
Gurchetan Singhee43c302017-11-14 18:20:27 -0800113 vma->priv = NULL;
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700114 }
115
Gurchetan Singhee43c302017-11-14 18:20:27 -0800116 return munmap(vma->addr, vma->length);
Gurchetan Singhef920532016-08-12 16:38:25 -0700117}
118
Gurchetan Singhef262d82017-11-28 16:56:17 -0800119static 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
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700129static int mediatek_bo_flush(struct bo *bo, struct mapping *mapping)
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700130{
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700131 struct mediatek_private_map_data *priv = mapping->vma->priv;
132 if (priv && (mapping->vma->map_flags & BO_MAP_WRITE))
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700133 memcpy(priv->gem_addr, priv->cached_addr, bo->total_size);
134
135 return 0;
136}
137
Gurchetan Singha1892b22017-09-28 16:40:52 -0700138static uint32_t mediatek_resolve_format(uint32_t format, uint64_t use_flags)
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700139{
140 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800141 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700142 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800143 return DRM_FORMAT_XBGR8888;
144 case DRM_FORMAT_FLEX_YCbCr_420_888:
Owen Linbbb69fd2017-06-05 14:33:08 +0800145 return DRM_FORMAT_YVU420;
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700146 default:
147 return format;
148 }
149}
150
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700151const struct backend backend_mediatek = {
JB Tsai0c16a0f2015-03-19 14:30:31 +0800152 .name = "mediatek",
Gurchetan Singh179687e2016-10-28 10:07:35 -0700153 .init = mediatek_init,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700154 .bo_create = mediatek_bo_create,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700155 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singh71611d62017-01-03 16:49:56 -0800156 .bo_import = drv_prime_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700157 .bo_map = mediatek_bo_map,
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700158 .bo_unmap = mediatek_bo_unmap,
Gurchetan Singhef262d82017-11-28 16:56:17 -0800159 .bo_invalidate = mediatek_bo_invalidate,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700160 .bo_flush = mediatek_bo_flush,
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700161 .resolve_format = mediatek_resolve_format,
JB Tsai0c16a0f2015-03-19 14:30:31 +0800162};
163
164#endif