Back to index

libdrm  2.4.37
nouveau.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 "nouveau_drm.h"
00043 
00044 struct nouveau_bo
00045 {
00046        struct kms_bo base;
00047        uint64_t map_handle;
00048        unsigned map_count;
00049 };
00050 
00051 static int
00052 nouveau_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
00053 {
00054        switch (key) {
00055        case KMS_BO_TYPE:
00056               *out = KMS_BO_TYPE_SCANOUT_X8R8G8B8 | KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8;
00057               break;
00058        default:
00059               return -EINVAL;
00060        }
00061        return 0;
00062 }
00063 
00064 static int
00065 nouveau_destroy(struct kms_driver *kms)
00066 {
00067        free(kms);
00068        return 0;
00069 }
00070 
00071 static int
00072 nouveau_bo_create(struct kms_driver *kms,
00073                const unsigned width, const unsigned height,
00074                const enum kms_bo_type type, const unsigned *attr,
00075                struct kms_bo **out)
00076 {
00077        struct drm_nouveau_gem_new arg;
00078        unsigned size, pitch;
00079        struct nouveau_bo *bo;
00080        int i, ret;
00081 
00082        for (i = 0; attr[i]; i += 2) {
00083               switch (attr[i]) {
00084               case KMS_WIDTH:
00085               case KMS_HEIGHT:
00086               case KMS_BO_TYPE:
00087                      break;
00088               default:
00089                      return -EINVAL;
00090               }
00091        }
00092 
00093        bo = calloc(1, sizeof(*bo));
00094        if (!bo)
00095               return -ENOMEM;
00096 
00097        if (type == KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8) {
00098               pitch = 64 * 4;
00099               size = 64 * 64 * 4;
00100        } else if (type == KMS_BO_TYPE_SCANOUT_X8R8G8B8) {
00101               pitch = width * 4;
00102               pitch = (pitch + 512 - 1) & ~(512 - 1);
00103               size = pitch * height;
00104        } else {
00105               return -EINVAL;
00106        }
00107 
00108        memset(&arg, 0, sizeof(arg));
00109        arg.info.size = size;
00110        arg.info.domain = NOUVEAU_GEM_DOMAIN_MAPPABLE | NOUVEAU_GEM_DOMAIN_VRAM;
00111        arg.info.tile_mode = 0;
00112        arg.info.tile_flags = 0;
00113        arg.align = 512;
00114        arg.channel_hint = 0;
00115 
00116        ret = drmCommandWriteRead(kms->fd, DRM_NOUVEAU_GEM_NEW, &arg, sizeof(arg));
00117        if (ret)
00118               goto err_free;
00119 
00120        bo->base.kms = kms;
00121        bo->base.handle = arg.info.handle;
00122        bo->base.size = size;
00123        bo->base.pitch = pitch;
00124        bo->map_handle = arg.info.map_handle;
00125 
00126        *out = &bo->base;
00127 
00128        return 0;
00129 
00130 err_free:
00131        free(bo);
00132        return ret;
00133 }
00134 
00135 static int
00136 nouveau_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
00137 {
00138        switch (key) {
00139        default:
00140               return -EINVAL;
00141        }
00142 }
00143 
00144 static int
00145 nouveau_bo_map(struct kms_bo *_bo, void **out)
00146 {
00147        struct nouveau_bo *bo = (struct nouveau_bo *)_bo;
00148        void *map = NULL;
00149 
00150        if (bo->base.ptr) {
00151               bo->map_count++;
00152               *out = bo->base.ptr;
00153               return 0;
00154        }
00155 
00156        map = mmap(0, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, bo->map_handle);
00157        if (map == MAP_FAILED)
00158               return -errno;
00159 
00160        bo->base.ptr = map;
00161        bo->map_count++;
00162        *out = bo->base.ptr;
00163 
00164        return 0;
00165 }
00166 
00167 static int
00168 nouveau_bo_unmap(struct kms_bo *_bo)
00169 {
00170        struct nouveau_bo *bo = (struct nouveau_bo *)_bo;
00171        bo->map_count--;
00172        return 0;
00173 }
00174 
00175 static int
00176 nouveau_bo_destroy(struct kms_bo *_bo)
00177 {
00178        struct nouveau_bo *bo = (struct nouveau_bo *)_bo;
00179        struct drm_gem_close arg;
00180        int ret;
00181 
00182        if (bo->base.ptr) {
00183               /* XXX Sanity check map_count */
00184               munmap(bo->base.ptr, bo->base.size);
00185               bo->base.ptr = NULL;
00186        }
00187 
00188        memset(&arg, 0, sizeof(arg));
00189        arg.handle = bo->base.handle;
00190 
00191        ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_GEM_CLOSE, &arg);
00192        if (ret)
00193               return -errno;
00194 
00195        free(bo);
00196        return 0;
00197 }
00198 
00199 int
00200 nouveau_create(int fd, struct kms_driver **out)
00201 {
00202        struct kms_driver *kms;
00203 
00204        kms = calloc(1, sizeof(*kms));
00205        if (!kms)
00206               return -ENOMEM;
00207 
00208        kms->fd = fd;
00209 
00210        kms->bo_create = nouveau_bo_create;
00211        kms->bo_map = nouveau_bo_map;
00212        kms->bo_unmap = nouveau_bo_unmap;
00213        kms->bo_get_prop = nouveau_bo_get_prop;
00214        kms->bo_destroy = nouveau_bo_destroy;
00215        kms->get_prop = nouveau_get_prop;
00216        kms->destroy = nouveau_destroy;
00217        *out = kms;
00218 
00219        return 0;
00220 }