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 **************************************************************************/
29 #define HAVE_STDINT_H
30 #define _FILE_OFFSET_BITS 64
32 #include <errno.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include "internal.h"
38 #include <sys/mman.h>
39 #include <sys/ioctl.h>
40 #include "xf86drm.h"
42 #include "i915_drm.h"
44 struct intel_bo
45 {
46 struct kms_bo base;
47 unsigned handle;
48 unsigned map_count;
49 int mapped;
50 };
52 static int
53 intel_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
54 {
55 switch (key) {
56 case KMS_MAX_SCANOUT_WIDTH:
57 *out = 4096;
58 break;
59 case KMS_MAX_SCANOUT_HEIGHT:
60 *out = 4096;
61 break;
62 case KMS_MIN_SCANOUT_WIDTH:
63 *out = 1;
64 break;
65 case KMS_MIN_SCANOUT_HEIGHT:
66 *out = 1;
67 break;
68 case KMS_MAX_CURSOR_WIDTH:
69 *out = 64;
70 break;
71 case KMS_MAX_CURSOR_HEIGHT:
72 *out = 64;
73 break;
74 case KMS_MIN_CURSOR_WIDTH:
75 *out = 64;
76 break;
77 case KMS_MIN_CURSOR_HEIGHT:
78 *out = 64;
79 break;
80 default:
81 return -EINVAL;
82 }
83 return 0;
84 }
86 static int
87 intel_destroy(struct kms_driver *kms)
88 {
89 free(kms);
90 return 0;
91 }
93 static int
94 intel_bo_create(struct kms_driver *kms,
95 const unsigned width, const unsigned height,
96 const enum kms_bo_type type, const unsigned *attr,
97 struct kms_bo **out)
98 {
99 struct drm_i915_gem_create arg;
100 unsigned size, pitch;
101 struct intel_bo *bo;
102 int i, ret;
104 for (i = 0; attr[i]; i += 2) {
105 switch (attr[i]) {
106 case KMS_WIDTH:
107 case KMS_HEIGHT:
108 case KMS_BO_TYPE:
109 break;
110 default:
111 return -EINVAL;
112 }
113 }
115 bo = calloc(1, sizeof(*bo));
116 if (!bo)
117 return -ENOMEM;
119 if (type == KMS_BO_TYPE_CURSOR) {
120 pitch = 64 * 4;
121 size = 64 * 64 * 4;
122 } else if (type == KMS_BO_TYPE_SCANOUT) {
123 pitch = width * 4;
124 pitch = (pitch + 512 - 1) & ~(512 - 1);
125 size = pitch * ((height + 4 - 1) & ~(4 - 1));
126 } else {
127 return -EINVAL;
128 }
130 memset(&arg, 0, sizeof(arg));
131 arg.size = size;
133 ret = drmCommandWriteRead(kms->fd, DRM_I915_GEM_CREATE, &arg, sizeof(arg));
134 if (ret)
135 goto err_free;
137 bo->base.kms = kms;
138 bo->base.handle = arg.handle;
139 bo->base.size = size;
140 bo->base.pitch = pitch;
142 *out = &bo->base;
143 if (type == KMS_BO_TYPE_SCANOUT && pitch > 512) {
144 struct drm_i915_gem_set_tiling tile;
146 memset(&tile, 0, sizeof(tile));
147 tile.handle = bo->base.handle;
148 tile.tiling_mode = I915_TILING_X;
149 tile.stride = bo->base.pitch;
151 ret = drmCommandWriteRead(kms->fd, DRM_I915_GEM_SET_TILING, &tile, sizeof(tile));
152 #if 0
153 if (ret) {
154 kms_bo_destroy(out);
155 return ret;
156 }
157 #endif
158 }
160 return 0;
162 err_free:
163 free(bo);
164 return ret;
165 }
167 static int
168 intel_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
169 {
170 switch (key) {
171 default:
172 return -EINVAL;
173 }
174 }
176 static int
177 intel_bo_map(struct kms_bo *_bo, void **out)
178 {
179 struct intel_bo *bo = (struct intel_bo *)_bo;
180 struct drm_i915_gem_mmap_gtt arg;
181 void *map = NULL;
182 int ret;
184 if (bo->base.ptr) {
185 bo->map_count++;
186 *out = bo->base.ptr;
187 return 0;
188 }
190 memset(&arg, 0, sizeof(arg));
191 arg.handle = bo->base.handle;
193 ret = drmCommandWriteRead(bo->base.kms->fd, DRM_I915_GEM_MMAP_GTT, &arg, sizeof(arg));
194 if (ret)
195 return ret;
197 map = mmap(0, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, arg.offset);
198 if (map == MAP_FAILED)
199 return -errno;
201 bo->base.ptr = map;
202 bo->map_count++;
203 *out = bo->base.ptr;
205 return 0;
206 }
208 static int
209 intel_bo_unmap(struct kms_bo *_bo)
210 {
211 struct intel_bo *bo = (struct intel_bo *)_bo;
212 bo->map_count--;
213 return 0;
214 }
216 static int
217 intel_bo_destroy(struct kms_bo *_bo)
218 {
219 struct intel_bo *bo = (struct intel_bo *)_bo;
220 struct drm_gem_close arg;
221 int ret;
223 if (bo->base.ptr)
224 munmap(bo->base.ptr, bo->base.size);
226 memset(&arg, 0, sizeof(arg));
227 arg.handle = bo->base.handle;
229 ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_GEM_CLOSE, &arg);
230 if (ret)
231 return -errno;
233 free(bo);
234 return 0;
235 }
237 int
238 intel_create(int fd, struct kms_driver **out)
239 {
240 struct kms_driver *kms;
242 kms = calloc(1, sizeof(*kms));
243 if (!kms)
244 return -ENOMEM;
246 kms->fd = fd;
248 kms->bo_create = intel_bo_create;
249 kms->bo_map = intel_bo_map;
250 kms->bo_unmap = intel_bo_unmap;
251 kms->bo_get_prop = intel_bo_get_prop;
252 kms->bo_destroy = intel_bo_destroy;
253 kms->get_prop = intel_get_prop;
254 kms->destroy = intel_destroy;
255 *out = kms;
257 return 0;
258 }