OSDN Git Service

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