blob: 23b1dd216321639ed8d8c6f0cd6889aba1aafaab [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
19static struct supported_combination combos[8] = {
20 {DRM_FORMAT_ABGR8888, DRM_FORMAT_MOD_NONE,
21 DRV_BO_USE_RENDERING | DRV_BO_USE_SW_READ_OFTEN | DRV_BO_USE_SW_WRITE_OFTEN |
22 DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
23 {DRM_FORMAT_ARGB8888, DRM_FORMAT_MOD_NONE,
24 DRV_BO_USE_CURSOR | DRV_BO_USE_LINEAR | DRV_BO_USE_SW_READ_OFTEN | DRV_BO_USE_SW_WRITE_OFTEN},
25 {DRM_FORMAT_ARGB8888, DRM_FORMAT_MOD_NONE,
26 DRV_BO_USE_RENDERING | DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
27 {DRM_FORMAT_RGB565, DRM_FORMAT_MOD_NONE,
28 DRV_BO_USE_RENDERING | DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
29 {DRM_FORMAT_XRGB8888, DRM_FORMAT_MOD_NONE,
30 DRV_BO_USE_CURSOR | DRV_BO_USE_LINEAR | DRV_BO_USE_SW_READ_OFTEN | DRV_BO_USE_SW_WRITE_OFTEN},
31 {DRM_FORMAT_XBGR8888, DRM_FORMAT_MOD_NONE,
32 DRV_BO_USE_RENDERING | DRV_BO_USE_SW_READ_OFTEN | DRV_BO_USE_SW_WRITE_OFTEN |
33 DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
34 {DRM_FORMAT_XRGB8888, DRM_FORMAT_MOD_NONE,
35 DRV_BO_USE_RENDERING | DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
36 {DRM_FORMAT_YVU420, DRM_FORMAT_MOD_NONE,
37 DRV_BO_USE_RENDERING | DRV_BO_USE_SW_READ_RARELY | DRV_BO_USE_SW_WRITE_RARELY},
38};
39
40static int mediatek_init(struct driver *drv)
41{
42 drv_insert_combinations(drv, combos, ARRAY_SIZE(combos));
43 return drv_add_kms_flags(drv);
44}
JB Tsai0c16a0f2015-03-19 14:30:31 +080045
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -070046static int mediatek_bo_create(struct bo *bo, uint32_t width, uint32_t height,
47 uint32_t format, uint32_t flags)
JB Tsai0c16a0f2015-03-19 14:30:31 +080048{
JB Tsai0c16a0f2015-03-19 14:30:31 +080049 int ret;
Gurchetan Singh42cc6d62016-08-29 18:19:19 -070050 size_t plane;
51 struct drm_mtk_gem_create gem_create;
JB Tsai0c16a0f2015-03-19 14:30:31 +080052
Gurchetan Singh42cc6d62016-08-29 18:19:19 -070053 drv_bo_from_format(bo, width, height, format);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050054
JB Tsai0c16a0f2015-03-19 14:30:31 +080055 memset(&gem_create, 0, sizeof(gem_create));
Gurchetan Singha40ca9e2016-08-29 19:51:45 -070056 gem_create.size = bo->total_size;
JB Tsai0c16a0f2015-03-19 14:30:31 +080057
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070058 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MTK_GEM_CREATE, &gem_create);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -070059 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070060 fprintf(stderr, "drv: DRM_IOCTL_MTK_GEM_CREATE failed "
Gurchetan Singh42cc6d62016-08-29 18:19:19 -070061 "(size=%llu)\n", gem_create.size);
JB Tsai0c16a0f2015-03-19 14:30:31 +080062 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -070063 }
JB Tsai0c16a0f2015-03-19 14:30:31 +080064
Gurchetan Singh42cc6d62016-08-29 18:19:19 -070065 for (plane = 0; plane < bo->num_planes; plane++)
66 bo->handles[plane].u32 = gem_create.handle;
JB Tsai0c16a0f2015-03-19 14:30:31 +080067
68 return 0;
69}
70
Gurchetan Singh1a31e602016-10-06 10:58:00 -070071static void *mediatek_bo_map(struct bo *bo, struct map_info *data, size_t plane)
Gurchetan Singhef920532016-08-12 16:38:25 -070072{
73 int ret;
74 struct drm_mtk_gem_map_off gem_map;
75
76 memset(&gem_map, 0, sizeof(gem_map));
77 gem_map.handle = bo->handles[0].u32;
78
79 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MTK_GEM_MAP_OFFSET, &gem_map);
80 if (ret) {
81 fprintf(stderr,"drv: DRM_IOCTL_MTK_GEM_MAP_OFFSET failed\n");
82 return MAP_FAILED;
83 }
84
Gurchetan Singh1a31e602016-10-06 10:58:00 -070085 data->length = bo->total_size;
86
Gurchetan Singha40ca9e2016-08-29 19:51:45 -070087 return mmap(0, bo->total_size, PROT_READ | PROT_WRITE, MAP_SHARED,
Gurchetan Singhef920532016-08-12 16:38:25 -070088 bo->drv->fd, gem_map.offset);
89}
90
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080091static uint32_t mediatek_resolve_format(uint32_t format)
Gurchetan Singh42cc6d62016-08-29 18:19:19 -070092{
93 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080094 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Gurchetan Singh42cc6d62016-08-29 18:19:19 -070095 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080096 return DRM_FORMAT_XBGR8888;
97 case DRM_FORMAT_FLEX_YCbCr_420_888:
98 return DRM_FORMAT_YVU420;
Gurchetan Singh42cc6d62016-08-29 18:19:19 -070099 default:
100 return format;
101 }
102}
103
Gurchetan Singh179687e2016-10-28 10:07:35 -0700104struct backend backend_mediatek =
JB Tsai0c16a0f2015-03-19 14:30:31 +0800105{
106 .name = "mediatek",
Gurchetan Singh179687e2016-10-28 10:07:35 -0700107 .init = mediatek_init,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700108 .bo_create = mediatek_bo_create,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700109 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700110 .bo_map = mediatek_bo_map,
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700111 .resolve_format = mediatek_resolve_format,
JB Tsai0c16a0f2015-03-19 14:30:31 +0800112};
113
114#endif