Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 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_ROCKCHIP |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 8 | |
Zach Reizner | 58080df | 2016-04-27 11:14:41 -0700 | [diff] [blame] | 9 | #include <assert.h> |
| 10 | #include <errno.h> |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 11 | #include <stdio.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 12 | #include <string.h> |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 13 | #include <sys/mman.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 14 | #include <xf86drm.h> |
| 15 | #include <rockchip_drm.h> |
| 16 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 17 | #include "drv_priv.h" |
Dominik Behr | e13ac28 | 2015-01-13 00:59:21 -0800 | [diff] [blame] | 18 | #include "helpers.h" |
Zach Reizner | 58080df | 2016-04-27 11:14:41 -0700 | [diff] [blame] | 19 | #include "util.h" |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 20 | |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame^] | 21 | static int rockchip_bo_create(struct bo *bo, uint32_t width, uint32_t height, |
| 22 | uint32_t format, uint32_t flags) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 23 | { |
Zach Reizner | 58080df | 2016-04-27 11:14:41 -0700 | [diff] [blame] | 24 | size_t plane; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 25 | |
Zach Reizner | 58080df | 2016-04-27 11:14:41 -0700 | [diff] [blame] | 26 | switch (format) { |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 27 | case DRV_FORMAT_NV12: |
Gurchetan Singh | f64487b | 2016-07-14 19:54:44 -0700 | [diff] [blame] | 28 | width = ALIGN(width, 4); |
| 29 | height = ALIGN(height, 4); |
| 30 | bo->strides[0] = bo->strides[1] = width; |
| 31 | bo->sizes[0] = height * bo->strides[0]; |
| 32 | bo->sizes[1] = height * bo->strides[1] / 2; |
| 33 | bo->offsets[0] = 0; |
| 34 | bo->offsets[1] = height * bo->strides[0]; |
| 35 | break; |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 36 | case DRV_FORMAT_XRGB8888: |
| 37 | case DRV_FORMAT_ARGB8888: |
| 38 | case DRV_FORMAT_ABGR8888: |
| 39 | bo->strides[0] = drv_stride_from_format(format, width); |
Gurchetan Singh | f64487b | 2016-07-14 19:54:44 -0700 | [diff] [blame] | 40 | bo->sizes[0] = height * bo->strides[0]; |
| 41 | bo->offsets[0] = 0; |
| 42 | break; |
| 43 | default: |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 44 | fprintf(stderr, "drv: rockchip: unsupported format %4.4s\n", |
Gurchetan Singh | f64487b | 2016-07-14 19:54:44 -0700 | [diff] [blame] | 45 | (char*)&format); |
| 46 | assert(0); |
| 47 | return -EINVAL; |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 48 | } |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 49 | |
Zach Reizner | 58080df | 2016-04-27 11:14:41 -0700 | [diff] [blame] | 50 | int ret; |
Gurchetan Singh | f64487b | 2016-07-14 19:54:44 -0700 | [diff] [blame] | 51 | size_t size = 0; |
Zach Reizner | 58080df | 2016-04-27 11:14:41 -0700 | [diff] [blame] | 52 | |
Gurchetan Singh | f64487b | 2016-07-14 19:54:44 -0700 | [diff] [blame] | 53 | for (plane = 0; plane < bo->num_planes; plane++) |
| 54 | size += bo->sizes[plane]; |
Zach Reizner | 58080df | 2016-04-27 11:14:41 -0700 | [diff] [blame] | 55 | |
Gurchetan Singh | f64487b | 2016-07-14 19:54:44 -0700 | [diff] [blame] | 56 | struct drm_rockchip_gem_create gem_create; |
Zach Reizner | 58080df | 2016-04-27 11:14:41 -0700 | [diff] [blame] | 57 | |
Gurchetan Singh | f64487b | 2016-07-14 19:54:44 -0700 | [diff] [blame] | 58 | memset(&gem_create, 0, sizeof(gem_create)); |
| 59 | gem_create.size = size; |
| 60 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 61 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_CREATE, |
Gurchetan Singh | f64487b | 2016-07-14 19:54:44 -0700 | [diff] [blame] | 62 | &gem_create); |
| 63 | |
| 64 | if (ret) { |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 65 | fprintf(stderr, "drv: DRM_IOCTL_ROCKCHIP_GEM_CREATE failed " |
Gurchetan Singh | f64487b | 2016-07-14 19:54:44 -0700 | [diff] [blame] | 66 | "(size=%zu)\n", size); |
Zach Reizner | 58080df | 2016-04-27 11:14:41 -0700 | [diff] [blame] | 67 | } |
Gurchetan Singh | f64487b | 2016-07-14 19:54:44 -0700 | [diff] [blame] | 68 | else { |
| 69 | for (plane = 0; plane < bo->num_planes; plane++) |
| 70 | bo->handles[plane].u32 = gem_create.handle; |
Zach Reizner | 58080df | 2016-04-27 11:14:41 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | return ret; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame^] | 76 | static void *rockchip_bo_map(struct bo *bo) |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 77 | { |
| 78 | int ret; |
| 79 | struct drm_rockchip_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_ROCKCHIP_GEM_MAP_OFFSET, |
| 85 | &gem_map); |
| 86 | if (ret) { |
| 87 | fprintf(stderr, |
| 88 | "drv: DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET failed\n"); |
| 89 | return MAP_FAILED; |
| 90 | } |
| 91 | |
| 92 | return mmap(0, bo->sizes[0], PROT_READ | PROT_WRITE, MAP_SHARED, |
| 93 | bo->drv->fd, gem_map.offset); |
| 94 | } |
| 95 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 96 | const struct backend backend_rockchip = |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 97 | { |
| 98 | .name = "rockchip", |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame^] | 99 | .bo_create = rockchip_bo_create, |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 100 | .bo_destroy = drv_gem_bo_destroy, |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame^] | 101 | .bo_map = rockchip_bo_map, |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 102 | .format_list = { |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 103 | {DRV_FORMAT_XRGB8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_RENDERING}, |
| 104 | {DRV_FORMAT_XRGB8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_LINEAR}, |
| 105 | {DRV_FORMAT_ARGB8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_RENDERING}, |
| 106 | {DRV_FORMAT_ARGB8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_LINEAR}, |
| 107 | {DRV_FORMAT_ABGR8888, DRV_BO_USE_SCANOUT | DRV_BO_USE_CURSOR | DRV_BO_USE_RENDERING}, |
| 108 | {DRV_FORMAT_NV12, DRV_BO_USE_SCANOUT | DRV_BO_USE_RENDERING}, |
| 109 | {DRV_FORMAT_NV12, DRV_BO_USE_SCANOUT | DRV_BO_USE_LINEAR}, |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 110 | } |
| 111 | }; |
| 112 | |
| 113 | #endif |