440efb31051dc0a4bdef9081d76d7538b1ad92ca
[platform/upstream/libdrm.git] / libkms / dumb.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 #define HAVE_STDINT_H
30 #define _FILE_OFFSET_BITS 64
31
32 #include <errno.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include "internal.h"
37
38 #include <sys/mman.h>
39 #include <sys/ioctl.h>
40 #include "xf86drm.h"
41
42 #include "i915_drm.h"
43
44 struct dumb_bo
45 {
46         struct kms_bo base;
47         unsigned map_count;
48 };
49
50 static int
51 dumb_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 dumb_destroy(struct kms_driver *kms)
65 {
66         free(kms);
67         return 0;
68 }
69
70 static int
71 dumb_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 drm_mode_create_dumb arg;
77         struct dumb_bo *bo;
78         int i, ret;
79
80         for (i = 0; attr[i]; i += 2) {
81                 switch (attr[i]) {
82                 case KMS_WIDTH:
83                 case KMS_HEIGHT:
84                         break;
85                 case KMS_BO_TYPE:
86                         break;
87                 default:
88                         return -EINVAL;
89                 }
90         }
91
92         bo = calloc(1, sizeof(*bo));
93         if (!bo)
94                 return -ENOMEM;
95
96         memset(&arg, 0, sizeof(arg));
97
98         /* All BO_TYPE currently are 32bpp formats */
99         arg.bpp = 32;
100         arg.width = width;
101         arg.height = height;
102
103         ret = drmIoctl(kms->fd, DRM_IOCTL_MODE_CREATE_DUMB, &arg);
104         if (ret)
105                 goto err_free;
106
107         bo->base.kms = kms;
108         bo->base.handle = arg.handle;
109         bo->base.size = arg.size;
110         bo->base.pitch = arg.pitch;
111
112         *out = &bo->base;
113
114         return 0;
115
116 err_free:
117         free(bo);
118         return ret;
119 }
120
121 static int
122 dumb_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
123 {
124         switch (key) {
125         default:
126                 return -EINVAL;
127         }
128 }
129
130 static int
131 dumb_bo_map(struct kms_bo *_bo, void **out)
132 {
133         struct dumb_bo *bo = (struct dumb_bo *)_bo;
134         struct drm_mode_map_dumb arg;
135         void *map = NULL;
136         int ret;
137
138         if (bo->base.ptr) {
139                 bo->map_count++;
140                 *out = bo->base.ptr;
141                 return 0;
142         }
143
144         memset(&arg, 0, sizeof(arg));
145         arg.handle = bo->base.handle;
146
147         ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_MODE_MAP_DUMB, &arg);
148         if (ret)
149                 return ret;
150
151         map = mmap(0, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, arg.offset);
152         if (map == MAP_FAILED)
153                 return -errno;
154
155         bo->base.ptr = map;
156         bo->map_count++;
157         *out = bo->base.ptr;
158
159         return 0;
160 }
161
162 static int
163 dumb_bo_unmap(struct kms_bo *_bo)
164 {
165         struct dumb_bo *bo = (struct dumb_bo *)_bo;
166         bo->map_count--;
167         return 0;
168 }
169
170 static int
171 dumb_bo_destroy(struct kms_bo *_bo)
172 {
173         struct dumb_bo *bo = (struct dumb_bo *)_bo;
174         struct drm_mode_destroy_dumb arg;
175         int ret;
176
177         if (bo->base.ptr) {
178                 /* XXX Sanity check map_count */
179                 munmap(bo->base.ptr, bo->base.size);
180                 bo->base.ptr = NULL;
181         }
182
183         memset(&arg, 0, sizeof(arg));
184         arg.handle = bo->base.handle;
185
186         ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
187         if (ret)
188                 return -errno;
189
190         free(bo);
191         return 0;
192 }
193
194 int
195 dumb_create(int fd, struct kms_driver **out)
196 {
197         struct kms_driver *kms;
198         int ret;
199         uint64_t cap = 0;
200
201         ret = drmGetCap(fd, DRM_CAP_DUMB_BUFFER, &cap);
202         if (ret || cap == 0)
203                 return -EINVAL;
204
205         kms = calloc(1, sizeof(*kms));
206         if (!kms)
207                 return -ENOMEM;
208
209         kms->fd = fd;
210
211         kms->bo_create = dumb_bo_create;
212         kms->bo_map = dumb_bo_map;
213         kms->bo_unmap = dumb_bo_unmap;
214         kms->bo_get_prop = dumb_bo_get_prop;
215         kms->bo_destroy = dumb_bo_destroy;
216         kms->get_prop = dumb_get_prop;
217         kms->destroy = dumb_destroy;
218         *out = kms;
219
220         return 0;
221 }