blob: 22d89d3c611a41c0f1780eaedd25e893dc443fe7 [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
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -07009#include <stdio.h>
JB Tsai0c16a0f2015-03-19 14:30:31 +080010#include <string.h>
Gurchetan Singhef920532016-08-12 16:38:25 -070011#include <sys/mman.h>
JB Tsai0c16a0f2015-03-19 14:30:31 +080012#include <xf86drm.h>
13#include <mediatek_drm.h>
Gurchetan Singhef920532016-08-12 16:38:25 -070014
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070015#include "drv_priv.h"
JB Tsai0c16a0f2015-03-19 14:30:31 +080016#include "helpers.h"
Gurchetan Singh179687e2016-10-28 10:07:35 -070017#include "util.h"
18
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080019static const uint32_t supported_formats[] = {
20 DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888, DRM_FORMAT_RGB565,
Gurchetan Singh03f13562017-02-08 15:21:14 -080021 DRM_FORMAT_XBGR8888, DRM_FORMAT_XRGB8888, DRM_FORMAT_YVU420,
22 DRM_FORMAT_YVU420_ANDROID
Gurchetan Singh179687e2016-10-28 10:07:35 -070023};
24
25static int mediatek_init(struct driver *drv)
26{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080027 return drv_add_linear_combinations(drv, supported_formats,
28 ARRAY_SIZE(supported_formats));
Gurchetan Singh179687e2016-10-28 10:07:35 -070029}
JB Tsai0c16a0f2015-03-19 14:30:31 +080030
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -070031static int mediatek_bo_create(struct bo *bo, uint32_t width, uint32_t height,
32 uint32_t format, uint32_t flags)
JB Tsai0c16a0f2015-03-19 14:30:31 +080033{
JB Tsai0c16a0f2015-03-19 14:30:31 +080034 int ret;
Gurchetan Singh42cc6d62016-08-29 18:19:19 -070035 size_t plane;
36 struct drm_mtk_gem_create gem_create;
Gurchetan Singh6ea14ba2017-02-08 15:09:13 -080037 uint32_t bytes_per_pixel = drv_stride_from_format(format, 1, 0);
JB Tsai0c16a0f2015-03-19 14:30:31 +080038
Gurchetan Singh6ea14ba2017-02-08 15:09:13 -080039 /*
40 * Since the ARM L1 cache line size is 64 bytes, align to that as a
41 * performance optimization.
42 */
43 width = ALIGN(width, DIV_ROUND_UP(64, bytes_per_pixel));
Gurchetan Singh42cc6d62016-08-29 18:19:19 -070044 drv_bo_from_format(bo, width, height, format);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050045
JB Tsai0c16a0f2015-03-19 14:30:31 +080046 memset(&gem_create, 0, sizeof(gem_create));
Gurchetan Singha40ca9e2016-08-29 19:51:45 -070047 gem_create.size = bo->total_size;
JB Tsai0c16a0f2015-03-19 14:30:31 +080048
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070049 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MTK_GEM_CREATE, &gem_create);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -070050 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070051 fprintf(stderr, "drv: DRM_IOCTL_MTK_GEM_CREATE failed "
Gurchetan Singh42cc6d62016-08-29 18:19:19 -070052 "(size=%llu)\n", gem_create.size);
JB Tsai0c16a0f2015-03-19 14:30:31 +080053 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -070054 }
JB Tsai0c16a0f2015-03-19 14:30:31 +080055
Gurchetan Singh42cc6d62016-08-29 18:19:19 -070056 for (plane = 0; plane < bo->num_planes; plane++)
57 bo->handles[plane].u32 = gem_create.handle;
JB Tsai0c16a0f2015-03-19 14:30:31 +080058
59 return 0;
60}
61
Gurchetan Singh1a31e602016-10-06 10:58:00 -070062static void *mediatek_bo_map(struct bo *bo, struct map_info *data, size_t plane)
Gurchetan Singhef920532016-08-12 16:38:25 -070063{
64 int ret;
65 struct drm_mtk_gem_map_off gem_map;
66
67 memset(&gem_map, 0, sizeof(gem_map));
68 gem_map.handle = bo->handles[0].u32;
69
70 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MTK_GEM_MAP_OFFSET, &gem_map);
71 if (ret) {
72 fprintf(stderr,"drv: DRM_IOCTL_MTK_GEM_MAP_OFFSET failed\n");
73 return MAP_FAILED;
74 }
75
Gurchetan Singh1a31e602016-10-06 10:58:00 -070076 data->length = bo->total_size;
77
Gurchetan Singha40ca9e2016-08-29 19:51:45 -070078 return mmap(0, bo->total_size, PROT_READ | PROT_WRITE, MAP_SHARED,
Gurchetan Singhef920532016-08-12 16:38:25 -070079 bo->drv->fd, gem_map.offset);
80}
81
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080082static uint32_t mediatek_resolve_format(uint32_t format)
Gurchetan Singh42cc6d62016-08-29 18:19:19 -070083{
84 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080085 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Gurchetan Singh42cc6d62016-08-29 18:19:19 -070086 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080087 return DRM_FORMAT_XBGR8888;
88 case DRM_FORMAT_FLEX_YCbCr_420_888:
Gurchetan Singh03f13562017-02-08 15:21:14 -080089 return DRM_FORMAT_YVU420_ANDROID;
Gurchetan Singh42cc6d62016-08-29 18:19:19 -070090 default:
91 return format;
92 }
93}
94
Gurchetan Singh179687e2016-10-28 10:07:35 -070095struct backend backend_mediatek =
JB Tsai0c16a0f2015-03-19 14:30:31 +080096{
97 .name = "mediatek",
Gurchetan Singh179687e2016-10-28 10:07:35 -070098 .init = mediatek_init,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -070099 .bo_create = mediatek_bo_create,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700100 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singh71611d62017-01-03 16:49:56 -0800101 .bo_import = drv_prime_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700102 .bo_map = mediatek_bo_map,
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700103 .resolve_format = mediatek_resolve_format,
JB Tsai0c16a0f2015-03-19 14:30:31 +0800104};
105
106#endif