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