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