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