1 /* exynos.c 2 * 3 * Copyright 2009 Samsung Electronics Co., Ltd. 4 * Authors: 5 * SooChan Lim <sc1.lim (at) samsung.com> 6 * Sangjin LEE <lsj119 (at) samsung.com> 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a 9 * copy of this software and associated documentation files (the "Software"), 10 * to deal in the Software without restriction, including without limitation 11 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 12 * and/or sell copies of the Software, and to permit persons to whom the 13 * Software is furnished to do so, subject to the following conditions: 14 * 15 * The above copyright notice and this permission notice (including the next 16 * paragraph) shall be included in all copies or substantial portions of the 17 * Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 * SOFTWARE. 26 */ 27 28 #include <errno.h> 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <string.h> 32 #include "internal.h" 33 34 #include <sys/mman.h> 35 #include <sys/ioctl.h> 36 #include "xf86drm.h" 37 38 #include "libdrm_macros.h" 39 #include "exynos_drm.h" 40 41 struct exynos_bo 42 { 43 struct kms_bo base; 44 unsigned map_count; 45 }; 46 47 static int 48 exynos_get_prop(struct kms_driver *kms, unsigned key, unsigned *out) 49 { 50 switch (key) { 51 case KMS_BO_TYPE: 52 *out = KMS_BO_TYPE_SCANOUT_X8R8G8B8 | KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8; 53 break; 54 default: 55 return -EINVAL; 56 } 57 return 0; 58 } 59 60 static int 61 exynos_destroy(struct kms_driver *kms) 62 { 63 free(kms); 64 return 0; 65 } 66 67 static int 68 exynos_bo_create(struct kms_driver *kms, 69 const unsigned width, const unsigned height, 70 const enum kms_bo_type type, const unsigned *attr, 71 struct kms_bo **out) 72 { 73 struct drm_exynos_gem_create arg; 74 unsigned size, pitch; 75 struct exynos_bo *bo; 76 int i, ret; 77 78 for (i = 0; attr[i]; i += 2) { 79 switch (attr[i]) { 80 case KMS_WIDTH: 81 case KMS_HEIGHT: 82 case KMS_BO_TYPE: 83 break; 84 default: 85 return -EINVAL; 86 } 87 } 88 89 bo = calloc(1, sizeof(*bo)); 90 if (!bo) 91 return -ENOMEM; 92 93 if (type == KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8) { 94 pitch = 64 * 4; 95 size = 64 * 64 * 4; 96 } else if (type == KMS_BO_TYPE_SCANOUT_X8R8G8B8) { 97 pitch = width * 4; 98 pitch = (pitch + 512 - 1) & ~(512 - 1); 99 size = pitch * ((height + 4 - 1) & ~(4 - 1)); 100 } else { 101 ret = -EINVAL; 102 goto err_free; 103 } 104 105 memset(&arg, 0, sizeof(arg)); 106 arg.size = size; 107 108 ret = drmCommandWriteRead(kms->fd, DRM_EXYNOS_GEM_CREATE, &arg, sizeof(arg)); 109 if (ret) 110 goto err_free; 111 112 bo->base.kms = kms; 113 bo->base.handle = arg.handle; 114 bo->base.size = size; 115 bo->base.pitch = pitch; 116 117 *out = &bo->base; 118 119 return 0; 120 121 err_free: 122 free(bo); 123 return ret; 124 } 125 126 static int 127 exynos_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out) 128 { 129 switch (key) { 130 default: 131 return -EINVAL; 132 } 133 } 134 135 static int 136 exynos_bo_map(struct kms_bo *_bo, void **out) 137 { 138 struct exynos_bo *bo = (struct exynos_bo *)_bo; 139 struct drm_mode_map_dumb arg; 140 void *map = NULL; 141 int ret; 142 143 if (bo->base.ptr) { 144 bo->map_count++; 145 *out = bo->base.ptr; 146 return 0; 147 } 148 149 memset(&arg, 0, sizeof(arg)); 150 arg.handle = bo->base.handle; 151 152 ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_MODE_MAP_DUMB, &arg); 153 if (ret) 154 return ret; 155 156 map = drm_mmap(0, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, arg.offset); 157 if (map == MAP_FAILED) 158 return -errno; 159 160 bo->base.ptr = map; 161 bo->map_count++; 162 *out = bo->base.ptr; 163 164 return 0; 165 } 166 167 static int 168 exynos_bo_unmap(struct kms_bo *_bo) 169 { 170 struct exynos_bo *bo = (struct exynos_bo *)_bo; 171 bo->map_count--; 172 return 0; 173 } 174 175 static int 176 exynos_bo_destroy(struct kms_bo *_bo) 177 { 178 struct exynos_bo *bo = (struct exynos_bo *)_bo; 179 struct drm_gem_close arg; 180 int ret; 181 182 if (bo->base.ptr) { 183 /* XXX Sanity check map_count */ 184 munmap(bo->base.ptr, bo->base.size); 185 bo->base.ptr = NULL; 186 } 187 188 memset(&arg, 0, sizeof(arg)); 189 arg.handle = bo->base.handle; 190 191 ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_GEM_CLOSE, &arg); 192 if (ret) 193 return -errno; 194 195 free(bo); 196 return 0; 197 } 198 199 drm_private int 200 exynos_create(int fd, struct kms_driver **out) 201 { 202 struct kms_driver *kms; 203 204 kms = calloc(1, sizeof(*kms)); 205 if (!kms) 206 return -ENOMEM; 207 208 kms->fd = fd; 209 210 kms->bo_create = exynos_bo_create; 211 kms->bo_map = exynos_bo_map; 212 kms->bo_unmap = exynos_bo_unmap; 213 kms->bo_get_prop = exynos_bo_get_prop; 214 kms->bo_destroy = exynos_bo_destroy; 215 kms->get_prop = exynos_get_prop; 216 kms->destroy = exynos_destroy; 217 *out = kms; 218 219 return 0; 220 } 221