blob: bc04133f50f5022e57c78021c20030bf8db95c0c [file] [log] [blame]
Jakob Bornecrantz8c0571a2009-11-24 17:54:10 +01001/**************************************************************************
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
Emil Velikov8e93afc2014-07-27 14:46:45 +010029#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
Jakob Bornecrantz8c0571a2009-11-24 17:54:10 +010032
33#include <errno.h>
34#include <stdlib.h>
35#include <string.h>
36#include "internal.h"
37
Jakob Bornecrantz8c0571a2009-11-24 17:54:10 +010038#include "xf86drm.h"
Emil Velikovdadc9ef2014-09-07 19:41:37 +010039#include "libdrm.h"
Jakob Bornecrantz8c0571a2009-11-24 17:54:10 +010040#include "vmwgfx_drm.h"
41
42struct vmwgfx_bo
43{
44 struct kms_bo base;
45 uint64_t map_handle;
46 unsigned map_count;
47};
48
49static int
50vmwgfx_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
51{
52 switch (key) {
Jakob Bornecrantz44a0e0a2010-01-23 01:41:49 +000053 case KMS_BO_TYPE:
54 *out = KMS_BO_TYPE_SCANOUT_X8R8G8B8 | KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8;
Jakob Bornecrantz8c0571a2009-11-24 17:54:10 +010055 break;
56 default:
57 return -EINVAL;
58 }
59 return 0;
60}
61
62static int
63vmwgfx_destroy(struct kms_driver *kms)
64{
65 free(kms);
66 return 0;
67}
68
69static int
70vmwgfx_bo_create(struct kms_driver *kms,
71 const unsigned width, const unsigned height,
72 const enum kms_bo_type type, const unsigned *attr,
73 struct kms_bo **out)
74{
75 struct vmwgfx_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:
Jakob Bornecrantzdfa72972010-01-23 01:38:18 +000085 return -EINVAL;
Jakob Bornecrantz8c0571a2009-11-24 17:54:10 +010086 }
87 }
88
89 bo = calloc(1, sizeof(*bo));
90 if (!bo)
91 return -EINVAL;
92
93 {
94 union drm_vmw_alloc_dmabuf_arg arg;
95 struct drm_vmw_alloc_dmabuf_req *req = &arg.req;
96 struct drm_vmw_dmabuf_rep *rep = &arg.rep;
97
98 memset(&arg, 0, sizeof(arg));
99 req->size = width * height * 4;
100 bo->base.size = req->size;
101 bo->base.pitch = width * 4;
102 bo->base.kms = kms;
103
104 do {
105 ret = drmCommandWriteRead(bo->base.kms->fd,
106 DRM_VMW_ALLOC_DMABUF,
107 &arg, sizeof(arg));
108 } while (ret == -ERESTART);
109
110 if (ret)
111 goto err_free;
112
113 bo->base.handle = rep->handle;
114 bo->map_handle = rep->map_handle;
115 bo->base.handle = rep->cur_gmr_id;
116 bo->base.offset = rep->cur_gmr_offset;
117 }
118
119 *out = &bo->base;
120
121 return 0;
122
123err_free:
124 free(bo);
125 return ret;
126}
127
128static int
129vmwgfx_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
130{
131 switch (key) {
132 default:
133 return -EINVAL;
134 }
135}
136
137static int
138vmwgfx_bo_map(struct kms_bo *_bo, void **out)
139{
140 struct vmwgfx_bo *bo = (struct vmwgfx_bo *)_bo;
141 void *map;
142
Jakob Bornecrantz29592662010-01-15 20:13:20 +0000143 if (bo->base.ptr) {
144 bo->map_count++;
145 *out = bo->base.ptr;
146 return 0;
Jakob Bornecrantz8c0571a2009-11-24 17:54:10 +0100147 }
148
Emil Velikovdadc9ef2014-09-07 19:41:37 +0100149 map = drm_mmap(NULL, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, bo->map_handle);
Jakob Bornecrantz29592662010-01-15 20:13:20 +0000150 if (map == MAP_FAILED)
151 return -errno;
152
153 bo->base.ptr = map;
Jakob Bornecrantz8c0571a2009-11-24 17:54:10 +0100154 bo->map_count++;
155 *out = bo->base.ptr;
156
157 return 0;
158}
159
160static int
161vmwgfx_bo_unmap(struct kms_bo *_bo)
162{
163 struct vmwgfx_bo *bo = (struct vmwgfx_bo *)_bo;
164 bo->map_count--;
165 return 0;
166}
167
168static int
169vmwgfx_bo_destroy(struct kms_bo *_bo)
170{
171 struct vmwgfx_bo *bo = (struct vmwgfx_bo *)_bo;
172 struct drm_vmw_unref_dmabuf_arg arg;
173
Jakob Bornecrantz29592662010-01-15 20:13:20 +0000174 if (bo->base.ptr) {
175 /* XXX Sanity check map_count */
Emil Velikovdadc9ef2014-09-07 19:41:37 +0100176 drm_munmap(bo->base.ptr, bo->base.size);
Jakob Bornecrantz8c0571a2009-11-24 17:54:10 +0100177 bo->base.ptr = NULL;
178 }
179
180 memset(&arg, 0, sizeof(arg));
181 arg.handle = bo->base.handle;
182 drmCommandWrite(bo->base.kms->fd, DRM_VMW_UNREF_DMABUF, &arg, sizeof(arg));
183
184 free(bo);
185 return 0;
186}
187
188int
189vmwgfx_create(int fd, struct kms_driver **out)
190{
191 struct kms_driver *kms;
192
193 kms = calloc(1, sizeof(*kms));
194 if (!kms)
195 return -ENOMEM;
196
197 kms->fd = fd;
198
199 kms->bo_create = vmwgfx_bo_create;
200 kms->bo_map = vmwgfx_bo_map;
201 kms->bo_unmap = vmwgfx_bo_unmap;
202 kms->bo_get_prop = vmwgfx_bo_get_prop;
203 kms->bo_destroy = vmwgfx_bo_destroy;
204 kms->get_prop = vmwgfx_get_prop;
205 kms->destroy = vmwgfx_destroy;
206 *out = kms;
207 return 0;
208}