blob: af2e44173a1e9aee348c71203bec6a641714b83b [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 Singh8ac0c9a2017-05-15 09:34:22 -070021static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
22 DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
23 DRM_FORMAT_XRGB8888 };
24
25static const uint32_t texture_source_formats[] = { DRM_FORMAT_R8, DRM_FORMAT_YVU420,
26 DRM_FORMAT_YVU420_ANDROID };
Gurchetan Singh179687e2016-10-28 10:07:35 -070027
28static int mediatek_init(struct driver *drv)
29{
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070030 int ret;
31 ret = drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
32 &LINEAR_METADATA, BO_USE_RENDER_MASK);
33 if (ret)
34 return ret;
35
36 ret = drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
37 &LINEAR_METADATA, BO_USE_TEXTURE_MASK);
38 if (ret)
39 return ret;
40
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,
45 uint32_t 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) {
Gurchetan Singh085bff12017-03-20 13:05:49 -070065 fprintf(stderr, "drv: DRM_IOCTL_MTK_GEM_CREATE failed (size=%llu)\n",
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080066 gem_create.size);
JB Tsai0c16a0f2015-03-19 14:30:31 +080067 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -070068 }
JB Tsai0c16a0f2015-03-19 14:30:31 +080069
Gurchetan Singh42cc6d62016-08-29 18:19:19 -070070 for (plane = 0; plane < bo->num_planes; plane++)
71 bo->handles[plane].u32 = gem_create.handle;
JB Tsai0c16a0f2015-03-19 14:30:31 +080072
73 return 0;
74}
75
Gurchetan Singh1a31e602016-10-06 10:58:00 -070076static void *mediatek_bo_map(struct bo *bo, struct map_info *data, size_t plane)
Gurchetan Singhef920532016-08-12 16:38:25 -070077{
78 int ret;
79 struct drm_mtk_gem_map_off gem_map;
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) {
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080086 fprintf(stderr, "drv: DRM_IOCTL_MTK_GEM_MAP_OFFSET failed\n");
Gurchetan Singhef920532016-08-12 16:38:25 -070087 return MAP_FAILED;
88 }
89
Gurchetan Singh1a31e602016-10-06 10:58:00 -070090 data->length = bo->total_size;
91
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080092 return mmap(0, bo->total_size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->drv->fd,
93 gem_map.offset);
Gurchetan Singhef920532016-08-12 16:38:25 -070094}
95
Tomasz Figace1ae022017-07-05 18:15:06 +090096static uint32_t mediatek_resolve_format(uint32_t format, uint64_t usage)
Gurchetan Singh42cc6d62016-08-29 18:19:19 -070097{
98 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080099 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700100 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800101 return DRM_FORMAT_XBGR8888;
102 case DRM_FORMAT_FLEX_YCbCr_420_888:
Owen Linbbb69fd2017-06-05 14:33:08 +0800103 return DRM_FORMAT_YVU420;
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700104 default:
105 return format;
106 }
107}
108
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800109struct backend backend_mediatek = {
JB Tsai0c16a0f2015-03-19 14:30:31 +0800110 .name = "mediatek",
Gurchetan Singh179687e2016-10-28 10:07:35 -0700111 .init = mediatek_init,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700112 .bo_create = mediatek_bo_create,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700113 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singh71611d62017-01-03 16:49:56 -0800114 .bo_import = drv_prime_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700115 .bo_map = mediatek_bo_map,
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700116 .resolve_format = mediatek_resolve_format,
JB Tsai0c16a0f2015-03-19 14:30:31 +0800117};
118
119#endif