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 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 19 | static const uint32_t supported_formats[] = { |
| 20 | DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888, DRM_FORMAT_RGB565, |
Gurchetan Singh | 03f1356 | 2017-02-08 15:21:14 -0800 | [diff] [blame] | 21 | DRM_FORMAT_XBGR8888, DRM_FORMAT_XRGB8888, DRM_FORMAT_YVU420, |
| 22 | DRM_FORMAT_YVU420_ANDROID |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | static int mediatek_init(struct driver *drv) |
| 26 | { |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 27 | return drv_add_linear_combinations(drv, supported_formats, |
| 28 | ARRAY_SIZE(supported_formats)); |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 29 | } |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 30 | |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame] | 31 | static int mediatek_bo_create(struct bo *bo, uint32_t width, uint32_t height, |
| 32 | uint32_t format, uint32_t flags) |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 33 | { |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 34 | int ret; |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 35 | size_t plane; |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame^] | 36 | uint32_t stride; |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 37 | struct drm_mtk_gem_create gem_create; |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 38 | |
Gurchetan Singh | 6ea14ba | 2017-02-08 15:09:13 -0800 | [diff] [blame] | 39 | /* |
| 40 | * Since the ARM L1 cache line size is 64 bytes, align to that as a |
| 41 | * performance optimization. |
| 42 | */ |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame^] | 43 | stride = drv_stride_from_format(format, width, 0); |
| 44 | stride = ALIGN(stride, 64); |
| 45 | drv_bo_from_format(bo, stride, height, format); |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 46 | |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 47 | memset(&gem_create, 0, sizeof(gem_create)); |
Gurchetan Singh | a40ca9e | 2016-08-29 19:51:45 -0700 | [diff] [blame] | 48 | gem_create.size = bo->total_size; |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 49 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 50 | 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] | 51 | if (ret) { |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 52 | fprintf(stderr, "drv: DRM_IOCTL_MTK_GEM_CREATE failed " |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 53 | "(size=%llu)\n", gem_create.size); |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 54 | return ret; |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 55 | } |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 56 | |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 57 | for (plane = 0; plane < bo->num_planes; plane++) |
| 58 | bo->handles[plane].u32 = gem_create.handle; |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 63 | 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] | 64 | { |
| 65 | int ret; |
| 66 | struct drm_mtk_gem_map_off gem_map; |
| 67 | |
| 68 | memset(&gem_map, 0, sizeof(gem_map)); |
| 69 | gem_map.handle = bo->handles[0].u32; |
| 70 | |
| 71 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MTK_GEM_MAP_OFFSET, &gem_map); |
| 72 | if (ret) { |
| 73 | fprintf(stderr,"drv: DRM_IOCTL_MTK_GEM_MAP_OFFSET failed\n"); |
| 74 | return MAP_FAILED; |
| 75 | } |
| 76 | |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 77 | data->length = bo->total_size; |
| 78 | |
Gurchetan Singh | a40ca9e | 2016-08-29 19:51:45 -0700 | [diff] [blame] | 79 | return mmap(0, bo->total_size, PROT_READ | PROT_WRITE, MAP_SHARED, |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 80 | bo->drv->fd, gem_map.offset); |
| 81 | } |
| 82 | |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 83 | static uint32_t mediatek_resolve_format(uint32_t format) |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 84 | { |
| 85 | switch (format) { |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 86 | case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED: |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 87 | /*HACK: See b/28671744 */ |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 88 | return DRM_FORMAT_XBGR8888; |
| 89 | case DRM_FORMAT_FLEX_YCbCr_420_888: |
Gurchetan Singh | 03f1356 | 2017-02-08 15:21:14 -0800 | [diff] [blame] | 90 | return DRM_FORMAT_YVU420_ANDROID; |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 91 | default: |
| 92 | return format; |
| 93 | } |
| 94 | } |
| 95 | |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 96 | struct backend backend_mediatek = |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 97 | { |
| 98 | .name = "mediatek", |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 99 | .init = mediatek_init, |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame] | 100 | .bo_create = mediatek_bo_create, |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 101 | .bo_destroy = drv_gem_bo_destroy, |
Gurchetan Singh | 71611d6 | 2017-01-03 16:49:56 -0800 | [diff] [blame] | 102 | .bo_import = drv_prime_bo_import, |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame] | 103 | .bo_map = mediatek_bo_map, |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 104 | .resolve_format = mediatek_resolve_format, |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | #endif |