Back to index

libdrm  2.4.37
intel.c
Go to the documentation of this file.
00001 /**************************************************************************
00002  *
00003  * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
00004  * All Rights Reserved.
00005  *
00006  * Permission is hereby granted, free of charge, to any person obtaining a
00007  * copy of this software and associated documentation files (the
00008  * "Software"), to deal in the Software without restriction, including
00009  * without limitation the rights to use, copy, modify, merge, publish,
00010  * distribute, sub license, and/or sell copies of the Software, and to
00011  * permit persons to whom the Software is furnished to do so, subject to
00012  * the following conditions:
00013  *
00014  * The above copyright notice and this permission notice (including the
00015  * next paragraph) shall be included in all copies or substantial portions
00016  * of the Software.
00017  *
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
00021  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
00022  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
00023  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
00024  * USE OR OTHER DEALINGS IN THE SOFTWARE.
00025  *
00026  **************************************************************************/
00027 
00028 
00029 #define HAVE_STDINT_H
00030 #define _FILE_OFFSET_BITS 64
00031 
00032 #include <errno.h>
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include "internal.h"
00037 
00038 #include <sys/mman.h>
00039 #include <sys/ioctl.h>
00040 #include "xf86drm.h"
00041 
00042 #include "i915_drm.h"
00043 
00044 struct intel_bo
00045 {
00046        struct kms_bo base;
00047        unsigned map_count;
00048 };
00049 
00050 static int
00051 intel_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
00052 {
00053        switch (key) {
00054        case KMS_BO_TYPE:
00055               *out = KMS_BO_TYPE_SCANOUT_X8R8G8B8 | KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8;
00056               break;
00057        default:
00058               return -EINVAL;
00059        }
00060        return 0;
00061 }
00062 
00063 static int
00064 intel_destroy(struct kms_driver *kms)
00065 {
00066        free(kms);
00067        return 0;
00068 }
00069 
00070 static int
00071 intel_bo_create(struct kms_driver *kms,
00072                const unsigned width, const unsigned height,
00073                const enum kms_bo_type type, const unsigned *attr,
00074                struct kms_bo **out)
00075 {
00076        struct drm_i915_gem_create arg;
00077        unsigned size, pitch;
00078        struct intel_bo *bo;
00079        int i, ret;
00080 
00081        for (i = 0; attr[i]; i += 2) {
00082               switch (attr[i]) {
00083               case KMS_WIDTH:
00084               case KMS_HEIGHT:
00085               case KMS_BO_TYPE:
00086                      break;
00087               default:
00088                      return -EINVAL;
00089               }
00090        }
00091 
00092        bo = calloc(1, sizeof(*bo));
00093        if (!bo)
00094               return -ENOMEM;
00095 
00096        if (type == KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8) {
00097               pitch = 64 * 4;
00098               size = 64 * 64 * 4;
00099        } else if (type == KMS_BO_TYPE_SCANOUT_X8R8G8B8) {
00100               pitch = width * 4;
00101               pitch = (pitch + 512 - 1) & ~(512 - 1);
00102               size = pitch * ((height + 4 - 1) & ~(4 - 1));
00103        } else {
00104               return -EINVAL;
00105        }
00106 
00107        memset(&arg, 0, sizeof(arg));
00108        arg.size = size;
00109 
00110        ret = drmCommandWriteRead(kms->fd, DRM_I915_GEM_CREATE, &arg, sizeof(arg));
00111        if (ret)
00112               goto err_free;
00113 
00114        bo->base.kms = kms;
00115        bo->base.handle = arg.handle;
00116        bo->base.size = size;
00117        bo->base.pitch = pitch;
00118 
00119        *out = &bo->base;
00120        if (type == KMS_BO_TYPE_SCANOUT_X8R8G8B8 && pitch > 512) {
00121               struct drm_i915_gem_set_tiling tile;
00122 
00123               memset(&tile, 0, sizeof(tile));
00124               tile.handle = bo->base.handle;
00125               tile.tiling_mode = I915_TILING_X;
00126               tile.stride = bo->base.pitch;
00127 
00128               ret = drmCommandWriteRead(kms->fd, DRM_I915_GEM_SET_TILING, &tile, sizeof(tile));
00129 #if 0
00130               if (ret) {
00131                      kms_bo_destroy(out);
00132                      return ret;
00133               }
00134 #endif
00135        }
00136 
00137        return 0;
00138 
00139 err_free:
00140        free(bo);
00141        return ret;
00142 }
00143 
00144 static int
00145 intel_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
00146 {
00147        switch (key) {
00148        default:
00149               return -EINVAL;
00150        }
00151 }
00152 
00153 static int
00154 intel_bo_map(struct kms_bo *_bo, void **out)
00155 {
00156        struct intel_bo *bo = (struct intel_bo *)_bo;
00157        struct drm_i915_gem_mmap_gtt arg;
00158        void *map = NULL;
00159        int ret;
00160 
00161        if (bo->base.ptr) {
00162               bo->map_count++;
00163               *out = bo->base.ptr;
00164               return 0;
00165        }
00166 
00167        memset(&arg, 0, sizeof(arg));
00168        arg.handle = bo->base.handle;
00169 
00170        ret = drmCommandWriteRead(bo->base.kms->fd, DRM_I915_GEM_MMAP_GTT, &arg, sizeof(arg));
00171        if (ret)
00172               return ret;
00173 
00174        map = mmap(0, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, arg.offset);
00175        if (map == MAP_FAILED)
00176               return -errno;
00177 
00178        bo->base.ptr = map;
00179        bo->map_count++;
00180        *out = bo->base.ptr;
00181 
00182        return 0;
00183 }
00184 
00185 static int
00186 intel_bo_unmap(struct kms_bo *_bo)
00187 {
00188        struct intel_bo *bo = (struct intel_bo *)_bo;
00189        bo->map_count--;
00190        return 0;
00191 }
00192 
00193 static int
00194 intel_bo_destroy(struct kms_bo *_bo)
00195 {
00196        struct intel_bo *bo = (struct intel_bo *)_bo;
00197        struct drm_gem_close arg;
00198        int ret;
00199 
00200        if (bo->base.ptr) {
00201               /* XXX Sanity check map_count */
00202               munmap(bo->base.ptr, bo->base.size);
00203               bo->base.ptr = NULL;
00204        }
00205 
00206        memset(&arg, 0, sizeof(arg));
00207        arg.handle = bo->base.handle;
00208 
00209        ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_GEM_CLOSE, &arg);
00210        if (ret)
00211               return -errno;
00212 
00213        free(bo);
00214        return 0;
00215 }
00216 
00217 int
00218 intel_create(int fd, struct kms_driver **out)
00219 {
00220        struct kms_driver *kms;
00221 
00222        kms = calloc(1, sizeof(*kms));
00223        if (!kms)
00224               return -ENOMEM;
00225 
00226        kms->fd = fd;
00227 
00228        kms->bo_create = intel_bo_create;
00229        kms->bo_map = intel_bo_map;
00230        kms->bo_unmap = intel_bo_unmap;
00231        kms->bo_get_prop = intel_bo_get_prop;
00232        kms->bo_destroy = intel_bo_destroy;
00233        kms->get_prop = intel_get_prop;
00234        kms->destroy = intel_destroy;
00235        *out = kms;
00236 
00237        return 0;
00238 }