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 #ifdef __FreeBSD__ 29 #define _WANT_KERNEL_ERRNO 30 #endif 31 32 #include <errno.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include "internal.h" 36 37 #include "xf86drm.h" 38 #include "libdrm_macros.h" 39 #include "vmwgfx_drm.h" 40 41 struct vmwgfx_bo 42 { 43 struct kms_bo base; 44 uint64_t map_handle; 45 unsigned map_count; 46 }; 47 48 static int 49 vmwgfx_get_prop(struct kms_driver *kms, unsigned key, unsigned *out) 50 { 51 switch (key) { 52 case KMS_BO_TYPE: 53 *out = KMS_BO_TYPE_SCANOUT_X8R8G8B8 | KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8; 54 break; 55 default: 56 return -EINVAL; 57 } 58 return 0; 59 } 60 61 static int 62 vmwgfx_destroy(struct kms_driver *kms) 63 { 64 free(kms); 65 return 0; 66 } 67 68 static int 69 vmwgfx_bo_create(struct kms_driver *kms, 70 const unsigned width, const unsigned height, 71 const enum kms_bo_type type, const unsigned *attr, 72 struct kms_bo **out) 73 { 74 struct vmwgfx_bo *bo; 75 int i, ret; 76 77 for (i = 0; attr[i]; i += 2) { 78 switch (attr[i]) { 79 case KMS_WIDTH: 80 case KMS_HEIGHT: 81 case KMS_BO_TYPE: 82 break; 83 default: 84 return -EINVAL; 85 } 86 } 87 88 bo = calloc(1, sizeof(*bo)); 89 if (!bo) 90 return -EINVAL; 91 92 { 93 union drm_vmw_alloc_dmabuf_arg arg; 94 struct drm_vmw_alloc_dmabuf_req *req = &arg.req; 95 struct drm_vmw_dmabuf_rep *rep = &arg.rep; 96 97 memset(&arg, 0, sizeof(arg)); 98 req->size = width * height * 4; 99 bo->base.size = req->size; 100 bo->base.pitch = width * 4; 101 bo->base.kms = kms; 102 103 do { 104 ret = drmCommandWriteRead(bo->base.kms->fd, 105 DRM_VMW_ALLOC_DMABUF, 106 &arg, sizeof(arg)); 107 } while (ret == -ERESTART); 108 109 if (ret) 110 goto err_free; 111 112 bo->base.handle = rep->handle; 113 bo->map_handle = rep->map_handle; 114 bo->base.handle = rep->cur_gmr_id; 115 bo->base.offset = rep->cur_gmr_offset; 116 } 117 118 *out = &bo->base; 119 120 return 0; 121 122 err_free: 123 free(bo); 124 return ret; 125 } 126 127 static int 128 vmwgfx_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out) 129 { 130 switch (key) { 131 default: 132 return -EINVAL; 133 } 134 } 135 136 static int 137 vmwgfx_bo_map(struct kms_bo *_bo, void **out) 138 { 139 struct vmwgfx_bo *bo = (struct vmwgfx_bo *)_bo; 140 void *map; 141 142 if (bo->base.ptr) { 143 bo->map_count++; 144 *out = bo->base.ptr; 145 return 0; 146 } 147 148 map = drm_mmap(NULL, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, bo->map_handle); 149 if (map == MAP_FAILED) 150 return -errno; 151 152 bo->base.ptr = map; 153 bo->map_count++; 154 *out = bo->base.ptr; 155 156 return 0; 157 } 158 159 static int 160 vmwgfx_bo_unmap(struct kms_bo *_bo) 161 { 162 struct vmwgfx_bo *bo = (struct vmwgfx_bo *)_bo; 163 bo->map_count--; 164 return 0; 165 } 166 167 static int 168 vmwgfx_bo_destroy(struct kms_bo *_bo) 169 { 170 struct vmwgfx_bo *bo = (struct vmwgfx_bo *)_bo; 171 struct drm_vmw_unref_dmabuf_arg arg; 172 173 if (bo->base.ptr) { 174 /* XXX Sanity check map_count */ 175 drm_munmap(bo->base.ptr, bo->base.size); 176 bo->base.ptr = NULL; 177 } 178 179 memset(&arg, 0, sizeof(arg)); 180 arg.handle = bo->base.handle; 181 drmCommandWrite(bo->base.kms->fd, DRM_VMW_UNREF_DMABUF, &arg, sizeof(arg)); 182 183 free(bo); 184 return 0; 185 } 186 187 drm_private int 188 vmwgfx_create(int fd, struct kms_driver **out) 189 { 190 struct kms_driver *kms; 191 192 kms = calloc(1, sizeof(*kms)); 193 if (!kms) 194 return -ENOMEM; 195 196 kms->fd = fd; 197 198 kms->bo_create = vmwgfx_bo_create; 199 kms->bo_map = vmwgfx_bo_map; 200 kms->bo_unmap = vmwgfx_bo_unmap; 201 kms->bo_get_prop = vmwgfx_bo_get_prop; 202 kms->bo_destroy = vmwgfx_bo_destroy; 203 kms->get_prop = vmwgfx_get_prop; 204 kms->destroy = vmwgfx_destroy; 205 *out = kms; 206 return 0; 207 } 208