blob: 17efc10a904c3641bfb6d1f2f01e054f739f0e08 [file] [log] [blame]
Dave Airliebb994652011-02-09 13:41:49 +10001/**************************************************************************
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
Dave Airliebb994652011-02-09 13:41:49 +100029#include <errno.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include "internal.h"
34
Dave Airliebb994652011-02-09 13:41:49 +100035#include <sys/ioctl.h>
36#include "xf86drm.h"
Emil Velikov42465fe2015-04-05 15:51:59 +010037#include "libdrm_macros.h"
Dave Airliebb994652011-02-09 13:41:49 +100038
39struct dumb_bo
40{
41 struct kms_bo base;
42 unsigned map_count;
43};
44
45static int
46dumb_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
58static int
59dumb_destroy(struct kms_driver *kms)
60{
61 free(kms);
62 return 0;
63}
64
65static int
66dumb_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 drm_mode_create_dumb arg;
72 struct dumb_bo *bo;
73 int i, ret;
74
75 for (i = 0; attr[i]; i += 2) {
76 switch (attr[i]) {
77 case KMS_WIDTH:
78 case KMS_HEIGHT:
79 break;
80 case KMS_BO_TYPE:
81 break;
82 default:
83 return -EINVAL;
84 }
85 }
86
87 bo = calloc(1, sizeof(*bo));
88 if (!bo)
89 return -ENOMEM;
90
91 memset(&arg, 0, sizeof(arg));
92
Chris Wilson61be9402011-05-14 11:15:06 +010093 /* All BO_TYPE currently are 32bpp formats */
94 arg.bpp = 32;
Dave Airliebb994652011-02-09 13:41:49 +100095 arg.width = width;
96 arg.height = height;
97
98 ret = drmIoctl(kms->fd, DRM_IOCTL_MODE_CREATE_DUMB, &arg);
99 if (ret)
100 goto err_free;
101
102 bo->base.kms = kms;
103 bo->base.handle = arg.handle;
104 bo->base.size = arg.size;
105 bo->base.pitch = arg.pitch;
106
107 *out = &bo->base;
108
109 return 0;
110
111err_free:
112 free(bo);
113 return ret;
114}
115
116static int
117dumb_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
118{
119 switch (key) {
120 default:
121 return -EINVAL;
122 }
123}
124
125static int
126dumb_bo_map(struct kms_bo *_bo, void **out)
127{
128 struct dumb_bo *bo = (struct dumb_bo *)_bo;
129 struct drm_mode_map_dumb arg;
130 void *map = NULL;
131 int ret;
132
133 if (bo->base.ptr) {
134 bo->map_count++;
135 *out = bo->base.ptr;
136 return 0;
137 }
138
139 memset(&arg, 0, sizeof(arg));
140 arg.handle = bo->base.handle;
141
142 ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_MODE_MAP_DUMB, &arg);
143 if (ret)
144 return ret;
145
Emil Velikovdadc9ef2014-09-07 19:41:37 +0100146 map = drm_mmap(0, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, arg.offset);
Dave Airliebb994652011-02-09 13:41:49 +1000147 if (map == MAP_FAILED)
148 return -errno;
149
150 bo->base.ptr = map;
151 bo->map_count++;
152 *out = bo->base.ptr;
153
154 return 0;
155}
156
157static int
158dumb_bo_unmap(struct kms_bo *_bo)
159{
160 struct dumb_bo *bo = (struct dumb_bo *)_bo;
161 bo->map_count--;
162 return 0;
163}
164
165static int
166dumb_bo_destroy(struct kms_bo *_bo)
167{
168 struct dumb_bo *bo = (struct dumb_bo *)_bo;
169 struct drm_mode_destroy_dumb arg;
170 int ret;
171
172 if (bo->base.ptr) {
173 /* XXX Sanity check map_count */
Emil Velikovdadc9ef2014-09-07 19:41:37 +0100174 drm_munmap(bo->base.ptr, bo->base.size);
Dave Airliebb994652011-02-09 13:41:49 +1000175 bo->base.ptr = NULL;
176 }
177
178 memset(&arg, 0, sizeof(arg));
179 arg.handle = bo->base.handle;
180
181 ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
182 if (ret)
183 return -errno;
184
185 free(bo);
186 return 0;
187}
188
Emil Velikov983892d2015-03-23 23:24:48 +0000189drm_private int
Dave Airliebb994652011-02-09 13:41:49 +1000190dumb_create(int fd, struct kms_driver **out)
191{
192 struct kms_driver *kms;
193 int ret;
194 uint64_t cap = 0;
195
196 ret = drmGetCap(fd, DRM_CAP_DUMB_BUFFER, &cap);
197 if (ret || cap == 0)
198 return -EINVAL;
199
200 kms = calloc(1, sizeof(*kms));
201 if (!kms)
202 return -ENOMEM;
203
204 kms->fd = fd;
205
206 kms->bo_create = dumb_bo_create;
207 kms->bo_map = dumb_bo_map;
208 kms->bo_unmap = dumb_bo_unmap;
209 kms->bo_get_prop = dumb_bo_get_prop;
210 kms->bo_destroy = dumb_bo_destroy;
211 kms->get_prop = dumb_get_prop;
212 kms->destroy = dumb_destroy;
213 *out = kms;
214
215 return 0;
216}