OSDN Git Service

android: set LOCAL_MODULE_TAGS to optional
[android-x86/external-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 #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 intel_bo
45 {
46         struct kms_bo base;
47         unsigned map_count;
48 };
49
50 static int
51 intel_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 intel_destroy(struct kms_driver *kms)
65 {
66         free(kms);
67         return 0;
68 }
69
70 static int
71 intel_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_i915_gem_create arg;
77         unsigned size, pitch;
78         struct intel_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                 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         if (type == KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8) {
97                 pitch = 64 * 4;
98                 size = 64 * 64 * 4;
99         } else if (type == KMS_BO_TYPE_SCANOUT_X8R8G8B8) {
100                 pitch = width * 4;
101                 pitch = (pitch + 512 - 1) & ~(512 - 1);
102                 size = pitch * ((height + 4 - 1) & ~(4 - 1));
103         } else {
104                 return -EINVAL;
105         }
106
107         memset(&arg, 0, sizeof(arg));
108         arg.size = size;
109
110         ret = drmCommandWriteRead(kms->fd, DRM_I915_GEM_CREATE, &arg, sizeof(arg));
111         if (ret)
112                 goto err_free;
113
114         bo->base.kms = kms;
115         bo->base.handle = arg.handle;
116         bo->base.size = size;
117         bo->base.pitch = pitch;
118
119         *out = &bo->base;
120         if (type == KMS_BO_TYPE_SCANOUT_X8R8G8B8 && pitch > 512) {
121                 struct drm_i915_gem_set_tiling tile;
122
123                 memset(&tile, 0, sizeof(tile));
124                 tile.handle = bo->base.handle;
125                 tile.tiling_mode = I915_TILING_X;
126                 tile.stride = bo->base.pitch;
127
128                 ret = drmCommandWriteRead(kms->fd, DRM_I915_GEM_SET_TILING, &tile, sizeof(tile));
129 #if 0
130                 if (ret) {
131                         kms_bo_destroy(out);
132                         return ret;
133                 }
134 #endif
135         }
136
137         return 0;
138
139 err_free:
140         free(bo);
141         return ret;
142 }
143
144 static int
145 intel_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
146 {
147         switch (key) {
148         default:
149                 return -EINVAL;
150         }
151 }
152
153 static int
154 intel_bo_map(struct kms_bo *_bo, void **out)
155 {
156         struct intel_bo *bo = (struct intel_bo *)_bo;
157         struct drm_i915_gem_mmap_gtt arg;
158         void *map = NULL;
159         int ret;
160
161         if (bo->base.ptr) {
162                 bo->map_count++;
163                 *out = bo->base.ptr;
164                 return 0;
165         }
166
167         memset(&arg, 0, sizeof(arg));
168         arg.handle = bo->base.handle;
169
170         ret = drmCommandWriteRead(bo->base.kms->fd, DRM_I915_GEM_MMAP_GTT, &arg, sizeof(arg));
171         if (ret)
172                 return ret;
173
174         map = mmap(0, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, arg.offset);
175         if (map == MAP_FAILED)
176                 return -errno;
177
178         bo->base.ptr = map;
179         bo->map_count++;
180         *out = bo->base.ptr;
181
182         return 0;
183 }
184
185 static int
186 intel_bo_unmap(struct kms_bo *_bo)
187 {
188         struct intel_bo *bo = (struct intel_bo *)_bo;
189         bo->map_count--;
190         return 0;
191 }
192
193 static int
194 intel_bo_destroy(struct kms_bo *_bo)
195 {
196         struct intel_bo *bo = (struct intel_bo *)_bo;
197         struct drm_gem_close arg;
198         int ret;
199
200         if (bo->base.ptr) {
201                 /* XXX Sanity check map_count */
202                 munmap(bo->base.ptr, bo->base.size);
203                 bo->base.ptr = NULL;
204         }
205
206         memset(&arg, 0, sizeof(arg));
207         arg.handle = bo->base.handle;
208
209         ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_GEM_CLOSE, &arg);
210         if (ret)
211                 return -errno;
212
213         free(bo);
214         return 0;
215 }
216
217 int
218 intel_create(int fd, struct kms_driver **out)
219 {
220         struct kms_driver *kms;
221
222         kms = calloc(1, sizeof(*kms));
223         if (!kms)
224                 return -ENOMEM;
225
226         kms->fd = fd;
227
228         kms->bo_create = intel_bo_create;
229         kms->bo_map = intel_bo_map;
230         kms->bo_unmap = intel_bo_unmap;
231         kms->bo_get_prop = intel_bo_get_prop;
232         kms->bo_destroy = intel_bo_destroy;
233         kms->get_prop = intel_get_prop;
234         kms->destroy = intel_destroy;
235         *out = kms;
236
237         return 0;
238 }