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