all: include config.h only when available and use its defines
[platform/upstream/libdrm.git] / libkms / intel.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 "i915_drm.h"
45
46 struct intel_bo
47 {
48         struct kms_bo base;
49         unsigned map_count;
50 };
51
52 static int
53 intel_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
54 {
55         switch (key) {
56         case KMS_BO_TYPE:
57                 *out = KMS_BO_TYPE_SCANOUT_X8R8G8B8 | KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8;
58                 break;
59         default:
60                 return -EINVAL;
61         }
62         return 0;
63 }
64
65 static int
66 intel_destroy(struct kms_driver *kms)
67 {
68         free(kms);
69         return 0;
70 }
71
72 static int
73 intel_bo_create(struct kms_driver *kms,
74                  const unsigned width, const unsigned height,
75                  const enum kms_bo_type type, const unsigned *attr,
76                  struct kms_bo **out)
77 {
78         struct drm_i915_gem_create arg;
79         unsigned size, pitch;
80         struct intel_bo *bo;
81         int i, ret;
82
83         for (i = 0; attr[i]; i += 2) {
84                 switch (attr[i]) {
85                 case KMS_WIDTH:
86                 case KMS_HEIGHT:
87                 case KMS_BO_TYPE:
88                         break;
89                 default:
90                         return -EINVAL;
91                 }
92         }
93
94         bo = calloc(1, sizeof(*bo));
95         if (!bo)
96                 return -ENOMEM;
97
98         if (type == KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8) {
99                 pitch = 64 * 4;
100                 size = 64 * 64 * 4;
101         } else if (type == KMS_BO_TYPE_SCANOUT_X8R8G8B8) {
102                 pitch = width * 4;
103                 pitch = (pitch + 512 - 1) & ~(512 - 1);
104                 size = pitch * ((height + 4 - 1) & ~(4 - 1));
105         } else {
106                 free(bo);
107                 return -EINVAL;
108         }
109
110         memset(&arg, 0, sizeof(arg));
111         arg.size = size;
112
113         ret = drmCommandWriteRead(kms->fd, DRM_I915_GEM_CREATE, &arg, sizeof(arg));
114         if (ret)
115                 goto err_free;
116
117         bo->base.kms = kms;
118         bo->base.handle = arg.handle;
119         bo->base.size = size;
120         bo->base.pitch = pitch;
121
122         *out = &bo->base;
123         if (type == KMS_BO_TYPE_SCANOUT_X8R8G8B8 && pitch > 512) {
124                 struct drm_i915_gem_set_tiling tile;
125
126                 memset(&tile, 0, sizeof(tile));
127                 tile.handle = bo->base.handle;
128                 tile.tiling_mode = I915_TILING_X;
129                 tile.stride = bo->base.pitch;
130
131                 ret = drmCommandWriteRead(kms->fd, DRM_I915_GEM_SET_TILING, &tile, sizeof(tile));
132 #if 0
133                 if (ret) {
134                         kms_bo_destroy(out);
135                         return ret;
136                 }
137 #endif
138         }
139
140         return 0;
141
142 err_free:
143         free(bo);
144         return ret;
145 }
146
147 static int
148 intel_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
149 {
150         switch (key) {
151         default:
152                 return -EINVAL;
153         }
154 }
155
156 static int
157 intel_bo_map(struct kms_bo *_bo, void **out)
158 {
159         struct intel_bo *bo = (struct intel_bo *)_bo;
160         struct drm_i915_gem_mmap_gtt arg;
161         void *map = NULL;
162         int ret;
163
164         if (bo->base.ptr) {
165                 bo->map_count++;
166                 *out = bo->base.ptr;
167                 return 0;
168         }
169
170         memset(&arg, 0, sizeof(arg));
171         arg.handle = bo->base.handle;
172
173         ret = drmCommandWriteRead(bo->base.kms->fd, DRM_I915_GEM_MMAP_GTT, &arg, sizeof(arg));
174         if (ret)
175                 return ret;
176
177         map = mmap(0, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, arg.offset);
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 intel_bo_unmap(struct kms_bo *_bo)
190 {
191         struct intel_bo *bo = (struct intel_bo *)_bo;
192         bo->map_count--;
193         return 0;
194 }
195
196 static int
197 intel_bo_destroy(struct kms_bo *_bo)
198 {
199         struct intel_bo *bo = (struct intel_bo *)_bo;
200         struct drm_gem_close arg;
201         int ret;
202
203         if (bo->base.ptr) {
204                 /* XXX Sanity check map_count */
205                 munmap(bo->base.ptr, bo->base.size);
206                 bo->base.ptr = NULL;
207         }
208
209         memset(&arg, 0, sizeof(arg));
210         arg.handle = bo->base.handle;
211
212         ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_GEM_CLOSE, &arg);
213         if (ret)
214                 return -errno;
215
216         free(bo);
217         return 0;
218 }
219
220 int
221 intel_create(int fd, struct kms_driver **out)
222 {
223         struct kms_driver *kms;
224
225         kms = calloc(1, sizeof(*kms));
226         if (!kms)
227                 return -ENOMEM;
228
229         kms->fd = fd;
230
231         kms->bo_create = intel_bo_create;
232         kms->bo_map = intel_bo_map;
233         kms->bo_unmap = intel_bo_unmap;
234         kms->bo_get_prop = intel_bo_get_prop;
235         kms->bo_destroy = intel_bo_destroy;
236         kms->get_prop = intel_get_prop;
237         kms->destroy = intel_destroy;
238         *out = kms;
239
240         return 0;
241 }