all: include config.h only when available and use its defines
[platform/upstream/libdrm.git] / libkms / radeon.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 <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include "internal.h"
39
40 #include <sys/mman.h>
41 #include <sys/ioctl.h>
42 #include "xf86drm.h"
43
44 #include "radeon_drm.h"
45
46
47 #define ALIGNMENT 512
48
49 struct radeon_bo
50 {
51         struct kms_bo base;
52         unsigned map_count;
53 };
54
55 static int
56 radeon_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
57 {
58         switch (key) {
59         case KMS_BO_TYPE:
60                 *out = KMS_BO_TYPE_SCANOUT_X8R8G8B8 | KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8;
61                 break;
62         default:
63                 return -EINVAL;
64         }
65         return 0;
66 }
67
68 static int
69 radeon_destroy(struct kms_driver *kms)
70 {
71         free(kms);
72         return 0;
73 }
74
75 static int
76 radeon_bo_create(struct kms_driver *kms,
77                  const unsigned width, const unsigned height,
78                  const enum kms_bo_type type, const unsigned *attr,
79                  struct kms_bo **out)
80 {
81         struct drm_radeon_gem_create arg;
82         unsigned size, pitch;
83         struct radeon_bo *bo;
84         int i, ret;
85
86         for (i = 0; attr[i]; i += 2) {
87                 switch (attr[i]) {
88                 case KMS_WIDTH:
89                 case KMS_HEIGHT:
90                 case KMS_BO_TYPE:
91                         break;
92                 default:
93                         return -EINVAL;
94                 }
95         }
96
97         switch (type) {
98         case KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8:
99                 pitch = 4 * 64;
100                 size  = 4 * 64 * 64;
101                 break;
102         case KMS_BO_TYPE_SCANOUT_X8R8G8B8:
103                 pitch = width * 4;
104                 pitch = (pitch + ALIGNMENT - 1) & ~(ALIGNMENT - 1);
105                 size  = pitch * height;
106                 break;
107         default:
108                 return -EINVAL;
109         }
110
111         bo = calloc(1, sizeof(*bo));
112         if (!bo)
113                 return -ENOMEM;
114
115         memset(&arg, 0, sizeof(arg));
116         arg.size = size;
117         arg.alignment = ALIGNMENT;
118         arg.initial_domain = RADEON_GEM_DOMAIN_CPU;
119         arg.flags = 0;
120         arg.handle = 0;
121
122         ret = drmCommandWriteRead(kms->fd, DRM_RADEON_GEM_CREATE,
123                                   &arg, sizeof(arg));
124         if (ret)
125                 goto err_free;
126
127         bo->base.kms = kms;
128         bo->base.handle = arg.handle;
129         bo->base.size = size;
130         bo->base.pitch = pitch;
131         bo->base.offset = 0;
132         bo->map_count = 0;
133
134         *out = &bo->base;
135
136         return 0;
137
138 err_free:
139         free(bo);
140         return ret;
141 }
142
143 static int
144 radeon_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
145 {
146         switch (key) {
147         default:
148                 return -EINVAL;
149         }
150 }
151
152 static int
153 radeon_bo_map(struct kms_bo *_bo, void **out)
154 {
155         struct radeon_bo *bo = (struct radeon_bo *)_bo;
156         struct drm_radeon_gem_mmap arg;
157         void *map = NULL;
158         int ret;
159
160         if (bo->base.ptr) {
161                 bo->map_count++;
162                 *out = bo->base.ptr;
163                 return 0;
164         }
165
166         memset(&arg, 0, sizeof(arg));
167         arg.handle = bo->base.handle;
168         arg.offset = bo->base.offset;
169         arg.size = (uint64_t)bo->base.size;
170
171         ret = drmCommandWriteRead(bo->base.kms->fd, DRM_RADEON_GEM_MMAP,
172                                 &arg, sizeof(arg));
173         if (ret)
174                 return -errno;
175
176         map = mmap(0, arg.size, PROT_READ | PROT_WRITE, MAP_SHARED,
177                    bo->base.kms->fd, arg.addr_ptr);
178         if (map == MAP_FAILED)
179                 return -errno;
180
181         bo->base.ptr = map;
182         bo->map_count++;
183         *out = bo->base.ptr;
184
185         return 0;
186 }
187
188 static int
189 radeon_bo_unmap(struct kms_bo *_bo)
190 {
191         struct radeon_bo *bo = (struct radeon_bo *)_bo;
192         if (--bo->map_count == 0) {
193                 munmap(bo->base.ptr, bo->base.size);
194                 bo->base.ptr = NULL;
195         }
196         return 0;
197 }
198
199 static int
200 radeon_bo_destroy(struct kms_bo *_bo)
201 {
202         struct radeon_bo *bo = (struct radeon_bo *)_bo;
203         struct drm_gem_close arg;
204         int ret;
205
206         if (bo->base.ptr) {
207                 /* XXX Sanity check map_count */
208                 munmap(bo->base.ptr, bo->base.size);
209                 bo->base.ptr = NULL;
210         }
211
212         memset(&arg, 0, sizeof(arg));
213         arg.handle = bo->base.handle;
214
215         ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_GEM_CLOSE, &arg);
216         if (ret)
217                 return -errno;
218
219         free(bo);
220         return 0;
221 }
222
223 int
224 radeon_create(int fd, struct kms_driver **out)
225 {
226         struct kms_driver *kms;
227
228         kms = calloc(1, sizeof(*kms));
229         if (!kms)
230                 return -ENOMEM;
231
232         kms->fd = fd;
233
234         kms->bo_create = radeon_bo_create;
235         kms->bo_map = radeon_bo_map;
236         kms->bo_unmap = radeon_bo_unmap;
237         kms->bo_get_prop = radeon_bo_get_prop;
238         kms->bo_destroy = radeon_bo_destroy;
239         kms->get_prop = radeon_get_prop;
240         kms->destroy = radeon_destroy;
241         *out = kms;
242
243         return 0;
244 }