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.h"
42
43 struct dumb_bo
44 {
45 struct kms_bo base;
46 unsigned map_count;
47 };
48
49 static int
dumb_get_prop(struct kms_driver * kms,unsigned key,unsigned * out)50 dumb_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
51 {
52 switch (key) {
53 case KMS_BO_TYPE:
54 *out = KMS_BO_TYPE_SCANOUT_X8R8G8B8 | KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8;
55 break;
56 default:
57 return -EINVAL;
58 }
59 return 0;
60 }
61
62 static int
dumb_destroy(struct kms_driver * kms)63 dumb_destroy(struct kms_driver *kms)
64 {
65 free(kms);
66 return 0;
67 }
68
69 static int
dumb_bo_create(struct kms_driver * kms,const unsigned width,const unsigned height,const enum kms_bo_type type,const unsigned * attr,struct kms_bo ** out)70 dumb_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 drm_mode_create_dumb arg;
76 struct dumb_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 break;
84 case KMS_BO_TYPE:
85 break;
86 default:
87 return -EINVAL;
88 }
89 }
90
91 bo = calloc(1, sizeof(*bo));
92 if (!bo)
93 return -ENOMEM;
94
95 memset(&arg, 0, sizeof(arg));
96
97 /* All BO_TYPE currently are 32bpp formats */
98 arg.bpp = 32;
99 arg.width = width;
100 arg.height = height;
101
102 ret = drmIoctl(kms->fd, DRM_IOCTL_MODE_CREATE_DUMB, &arg);
103 if (ret)
104 goto err_free;
105
106 bo->base.kms = kms;
107 bo->base.handle = arg.handle;
108 bo->base.size = arg.size;
109 bo->base.pitch = arg.pitch;
110
111 *out = &bo->base;
112
113 return 0;
114
115 err_free:
116 free(bo);
117 return ret;
118 }
119
120 static int
dumb_bo_get_prop(struct kms_bo * bo,unsigned key,unsigned * out)121 dumb_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
122 {
123 switch (key) {
124 default:
125 return -EINVAL;
126 }
127 }
128
129 static int
dumb_bo_map(struct kms_bo * _bo,void ** out)130 dumb_bo_map(struct kms_bo *_bo, void **out)
131 {
132 struct dumb_bo *bo = (struct dumb_bo *)_bo;
133 struct drm_mode_map_dumb arg;
134 void *map = NULL;
135 int ret;
136
137 if (bo->base.ptr) {
138 bo->map_count++;
139 *out = bo->base.ptr;
140 return 0;
141 }
142
143 memset(&arg, 0, sizeof(arg));
144 arg.handle = bo->base.handle;
145
146 ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_MODE_MAP_DUMB, &arg);
147 if (ret)
148 return ret;
149
150 map = drm_mmap(0, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, arg.offset);
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
dumb_bo_unmap(struct kms_bo * _bo)162 dumb_bo_unmap(struct kms_bo *_bo)
163 {
164 struct dumb_bo *bo = (struct dumb_bo *)_bo;
165 bo->map_count--;
166 return 0;
167 }
168
169 static int
dumb_bo_destroy(struct kms_bo * _bo)170 dumb_bo_destroy(struct kms_bo *_bo)
171 {
172 struct dumb_bo *bo = (struct dumb_bo *)_bo;
173 struct drm_mode_destroy_dumb arg;
174 int ret;
175
176 if (bo->base.ptr) {
177 /* XXX Sanity check map_count */
178 drm_munmap(bo->base.ptr, bo->base.size);
179 bo->base.ptr = NULL;
180 }
181
182 memset(&arg, 0, sizeof(arg));
183 arg.handle = bo->base.handle;
184
185 ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
186 if (ret)
187 return -errno;
188
189 free(bo);
190 return 0;
191 }
192
193 int
dumb_create(int fd,struct kms_driver ** out)194 dumb_create(int fd, struct kms_driver **out)
195 {
196 struct kms_driver *kms;
197 int ret;
198 uint64_t cap = 0;
199
200 ret = drmGetCap(fd, DRM_CAP_DUMB_BUFFER, &cap);
201 if (ret || cap == 0)
202 return -EINVAL;
203
204 kms = calloc(1, sizeof(*kms));
205 if (!kms)
206 return -ENOMEM;
207
208 kms->fd = fd;
209
210 kms->bo_create = dumb_bo_create;
211 kms->bo_map = dumb_bo_map;
212 kms->bo_unmap = dumb_bo_unmap;
213 kms->bo_get_prop = dumb_bo_get_prop;
214 kms->bo_destroy = dumb_bo_destroy;
215 kms->get_prop = dumb_get_prop;
216 kms->destroy = dumb_destroy;
217 *out = kms;
218
219 return 0;
220 }
221