]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/libdrm.git/blob - libkms/nouveau.c
DEBIAN: debianization
[glsdk/libdrm.git] / libkms / nouveau.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  **************************************************************************/
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 "nouveau_drm.h"
44 struct nouveau_bo
45 {
46         struct kms_bo base;
47         uint64_t map_handle;
48         unsigned map_count;
49 };
51 static int
52 nouveau_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
53 {
54         switch (key) {
55         case KMS_BO_TYPE:
56                 *out = KMS_BO_TYPE_SCANOUT_X8R8G8B8 | KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8;
57                 break;
58         default:
59                 return -EINVAL;
60         }
61         return 0;
62 }
64 static int
65 nouveau_destroy(struct kms_driver *kms)
66 {
67         free(kms);
68         return 0;
69 }
71 static int
72 nouveau_bo_create(struct kms_driver *kms,
73                  const unsigned width, const unsigned height,
74                  const enum kms_bo_type type, const unsigned *attr,
75                  struct kms_bo **out)
76 {
77         struct drm_nouveau_gem_new arg;
78         unsigned size, pitch;
79         struct nouveau_bo *bo;
80         int i, ret;
82         for (i = 0; attr[i]; i += 2) {
83                 switch (attr[i]) {
84                 case KMS_WIDTH:
85                 case KMS_HEIGHT:
86                 case KMS_BO_TYPE:
87                         break;
88                 default:
89                         return -EINVAL;
90                 }
91         }
93         bo = calloc(1, sizeof(*bo));
94         if (!bo)
95                 return -ENOMEM;
97         if (type == KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8) {
98                 pitch = 64 * 4;
99                 size = 64 * 64 * 4;
100         } else if (type == KMS_BO_TYPE_SCANOUT_X8R8G8B8) {
101                 pitch = width * 4;
102                 pitch = (pitch + 512 - 1) & ~(512 - 1);
103                 size = pitch * height;
104         } else {
105                 free(bo);
106                 return -EINVAL;
107         }
109         memset(&arg, 0, sizeof(arg));
110         arg.info.size = size;
111         arg.info.domain = NOUVEAU_GEM_DOMAIN_MAPPABLE | NOUVEAU_GEM_DOMAIN_VRAM;
112         arg.info.tile_mode = 0;
113         arg.info.tile_flags = 0;
114         arg.align = 512;
115         arg.channel_hint = 0;
117         ret = drmCommandWriteRead(kms->fd, DRM_NOUVEAU_GEM_NEW, &arg, sizeof(arg));
118         if (ret)
119                 goto err_free;
121         bo->base.kms = kms;
122         bo->base.handle = arg.info.handle;
123         bo->base.size = size;
124         bo->base.pitch = pitch;
125         bo->map_handle = arg.info.map_handle;
127         *out = &bo->base;
129         return 0;
131 err_free:
132         free(bo);
133         return ret;
136 static int
137 nouveau_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
139         switch (key) {
140         default:
141                 return -EINVAL;
142         }
145 static int
146 nouveau_bo_map(struct kms_bo *_bo, void **out)
148         struct nouveau_bo *bo = (struct nouveau_bo *)_bo;
149         void *map = NULL;
151         if (bo->base.ptr) {
152                 bo->map_count++;
153                 *out = bo->base.ptr;
154                 return 0;
155         }
157         map = mmap(0, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, bo->map_handle);
158         if (map == MAP_FAILED)
159                 return -errno;
161         bo->base.ptr = map;
162         bo->map_count++;
163         *out = bo->base.ptr;
165         return 0;
168 static int
169 nouveau_bo_unmap(struct kms_bo *_bo)
171         struct nouveau_bo *bo = (struct nouveau_bo *)_bo;
172         bo->map_count--;
173         return 0;
176 static int
177 nouveau_bo_destroy(struct kms_bo *_bo)
179         struct nouveau_bo *bo = (struct nouveau_bo *)_bo;
180         struct drm_gem_close arg;
181         int ret;
183         if (bo->base.ptr) {
184                 /* XXX Sanity check map_count */
185                 munmap(bo->base.ptr, bo->base.size);
186                 bo->base.ptr = NULL;
187         }
189         memset(&arg, 0, sizeof(arg));
190         arg.handle = bo->base.handle;
192         ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_GEM_CLOSE, &arg);
193         if (ret)
194                 return -errno;
196         free(bo);
197         return 0;
200 int
201 nouveau_create(int fd, struct kms_driver **out)
203         struct kms_driver *kms;
205         kms = calloc(1, sizeof(*kms));
206         if (!kms)
207                 return -ENOMEM;
209         kms->fd = fd;
211         kms->bo_create = nouveau_bo_create;
212         kms->bo_map = nouveau_bo_map;
213         kms->bo_unmap = nouveau_bo_unmap;
214         kms->bo_get_prop = nouveau_bo_get_prop;
215         kms->bo_destroy = nouveau_bo_destroy;
216         kms->get_prop = nouveau_get_prop;
217         kms->destroy = nouveau_destroy;
218         *out = kms;
220         return 0;