1 /**************************************************************************
3 * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 #define _FILE_OFFSET_BITS 64
39 #include <sys/ioctl.h>
53 intel_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
56 case KMS_MAX_SCANOUT_WIDTH:
59 case KMS_MAX_SCANOUT_HEIGHT:
62 case KMS_MIN_SCANOUT_WIDTH:
65 case KMS_MIN_SCANOUT_HEIGHT:
68 case KMS_MAX_CURSOR_WIDTH:
71 case KMS_MAX_CURSOR_HEIGHT:
74 case KMS_MIN_CURSOR_WIDTH:
77 case KMS_MIN_CURSOR_HEIGHT:
87 intel_destroy(struct kms_driver *kms)
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,
99 struct drm_i915_gem_create arg;
100 unsigned size, pitch;
104 for (i = 0; attr[i]; i += 2) {
115 bo = calloc(1, sizeof(*bo));
119 if (type == KMS_BO_TYPE_CURSOR) {
122 } else if (type == KMS_BO_TYPE_SCANOUT) {
124 pitch = (pitch + 512 - 1) & ~(512 - 1);
125 size = pitch * ((height + 4 - 1) & ~(4 - 1));
130 memset(&arg, 0, sizeof(arg));
133 ret = drmCommandWriteRead(kms->fd, DRM_I915_GEM_CREATE, &arg, sizeof(arg));
138 bo->base.handle = arg.handle;
139 bo->base.size = size;
140 bo->base.pitch = pitch;
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));
168 intel_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
177 intel_bo_map(struct kms_bo *_bo, void **out)
179 struct intel_bo *bo = (struct intel_bo *)_bo;
180 struct drm_i915_gem_mmap_gtt arg;
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));
197 map = mmap(0, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, arg.offset);
198 if (map == MAP_FAILED)
209 intel_bo_unmap(struct kms_bo *_bo)
211 struct intel_bo *bo = (struct intel_bo *)_bo;
217 intel_bo_destroy(struct kms_bo *_bo)
219 struct intel_bo *bo = (struct intel_bo *)_bo;
220 struct drm_gem_close arg;
224 /* XXX Sanity check map_count */
225 munmap(bo->base.ptr, bo->base.size);
229 memset(&arg, 0, sizeof(arg));
230 arg.handle = bo->base.handle;
232 ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_GEM_CLOSE, &arg);
241 intel_create(int fd, struct kms_driver **out)
243 struct kms_driver *kms;
245 kms = calloc(1, sizeof(*kms));
251 kms->bo_create = intel_bo_create;
252 kms->bo_map = intel_bo_map;
253 kms->bo_unmap = intel_bo_unmap;
254 kms->bo_get_prop = intel_bo_get_prop;
255 kms->bo_destroy = intel_bo_destroy;
256 kms->get_prop = intel_get_prop;
257 kms->destroy = intel_destroy;