JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 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 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 7 | #ifdef DRV_MEDIATEK |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 8 | |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 9 | #include <stdio.h> |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 10 | #include <string.h> |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 11 | #include <sys/mman.h> |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 12 | #include <xf86drm.h> |
| 13 | #include <mediatek_drm.h> |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 14 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 15 | #include "drv_priv.h" |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 16 | #include "helpers.h" |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame^] | 17 | #include "util.h" |
| 18 | |
| 19 | static 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 | |
| 40 | static 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 Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 45 | |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame] | 46 | static int mediatek_bo_create(struct bo *bo, uint32_t width, uint32_t height, |
| 47 | uint32_t format, uint32_t flags) |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 48 | { |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 49 | int ret; |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 50 | size_t plane; |
| 51 | struct drm_mtk_gem_create gem_create; |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 52 | |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 53 | drv_bo_from_format(bo, width, height, format); |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 54 | |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 55 | memset(&gem_create, 0, sizeof(gem_create)); |
Gurchetan Singh | a40ca9e | 2016-08-29 19:51:45 -0700 | [diff] [blame] | 56 | gem_create.size = bo->total_size; |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 57 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 58 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MTK_GEM_CREATE, &gem_create); |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 59 | if (ret) { |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 60 | fprintf(stderr, "drv: DRM_IOCTL_MTK_GEM_CREATE failed " |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 61 | "(size=%llu)\n", gem_create.size); |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 62 | return ret; |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 63 | } |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 64 | |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 65 | for (plane = 0; plane < bo->num_planes; plane++) |
| 66 | bo->handles[plane].u32 = gem_create.handle; |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 71 | static void *mediatek_bo_map(struct bo *bo, struct map_info *data, size_t plane) |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 72 | { |
| 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 Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 85 | data->length = bo->total_size; |
| 86 | |
Gurchetan Singh | a40ca9e | 2016-08-29 19:51:45 -0700 | [diff] [blame] | 87 | return mmap(0, bo->total_size, PROT_READ | PROT_WRITE, MAP_SHARED, |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 88 | bo->drv->fd, gem_map.offset); |
| 89 | } |
| 90 | |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 91 | static uint32_t mediatek_resolve_format(uint32_t format) |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 92 | { |
| 93 | switch (format) { |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 94 | case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED: |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 95 | /*HACK: See b/28671744 */ |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 96 | return DRM_FORMAT_XBGR8888; |
| 97 | case DRM_FORMAT_FLEX_YCbCr_420_888: |
| 98 | return DRM_FORMAT_YVU420; |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 99 | default: |
| 100 | return format; |
| 101 | } |
| 102 | } |
| 103 | |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame^] | 104 | struct backend backend_mediatek = |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 105 | { |
| 106 | .name = "mediatek", |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame^] | 107 | .init = mediatek_init, |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame] | 108 | .bo_create = mediatek_bo_create, |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 109 | .bo_destroy = drv_gem_bo_destroy, |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame] | 110 | .bo_map = mediatek_bo_map, |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 111 | .resolve_format = mediatek_resolve_format, |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | #endif |