all: include config.h only when available and use its defines
[platform/upstream/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  **************************************************************************/
27
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 #define _FILE_OFFSET_BITS 64
33
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include "internal.h"
39
40 #include <sys/mman.h>
41 #include <sys/ioctl.h>
42 #include "xf86drm.h"
43
44 #include "nouveau_drm.h"
45
46 struct nouveau_bo
47 {
48         struct kms_bo base;
49         uint64_t map_handle;
50         unsigned map_count;
51 };
52
53 static int
54 nouveau_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
55 {
56         switch (key) {
57         case KMS_BO_TYPE:
58                 *out = KMS_BO_TYPE_SCANOUT_X8R8G8B8 | KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8;
59                 break;
60         default:
61                 return -EINVAL;
62         }
63         return 0;
64 }
65
66 static int
67 nouveau_destroy(struct kms_driver *kms)
68 {
69         free(kms);
70         return 0;
71 }
72
73 static int
74 nouveau_bo_create(struct kms_driver *kms,
75                  const unsigned width, const unsigned height,
76                  const enum kms_bo_type type, const unsigned *attr,
77                  struct kms_bo **out)
78 {
79         struct drm_nouveau_gem_new arg;
80         unsigned size, pitch;
81         struct nouveau_bo *bo;
82         int i, ret;
83
84         for (i = 0; attr[i]; i += 2) {
85                 switch (attr[i]) {
86                 case KMS_WIDTH:
87                 case KMS_HEIGHT:
88                 case KMS_BO_TYPE:
89                         break;
90                 default:
91                         return -EINVAL;
92                 }
93         }
94
95         bo = calloc(1, sizeof(*bo));
96         if (!bo)
97                 return -ENOMEM;
98
99         if (type == KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8) {
100                 pitch = 64 * 4;
101                 size = 64 * 64 * 4;
102         } else if (type == KMS_BO_TYPE_SCANOUT_X8R8G8B8) {
103                 pitch = width * 4;
104                 pitch = (pitch + 512 - 1) & ~(512 - 1);
105                 size = pitch * height;
106         } else {
107                 free(bo);
108                 return -EINVAL;
109         }
110
111         memset(&arg, 0, sizeof(arg));
112         arg.info.size = size;
113         arg.info.domain = NOUVEAU_GEM_DOMAIN_MAPPABLE | NOUVEAU_GEM_DOMAIN_VRAM;
114         arg.info.tile_mode = 0;
115         arg.info.tile_flags = 0;
116         arg.align = 512;
117         arg.channel_hint = 0;
118
119         ret = drmCommandWriteRead(kms->fd, DRM_NOUVEAU_GEM_NEW, &arg, sizeof(arg));
120         if (ret)
121                 goto err_free;
122
123         bo->base.kms = kms;
124         bo->base.handle = arg.info.handle;
125         bo->base.size = size;
126         bo->base.pitch = pitch;
127         bo->map_handle = arg.info.map_handle;
128
129         *out = &bo->base;
130
131         return 0;
132
133 err_free:
134         free(bo);
135         return ret;
136 }
137
138 static int
139 nouveau_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
140 {
141         switch (key) {
142         default:
143                 return -EINVAL;
144         }
145 }
146
147 static int
148 nouveau_bo_map(struct kms_bo *_bo, void **out)
149 {
150         struct nouveau_bo *bo = (struct nouveau_bo *)_bo;
151         void *map = NULL;
152
153         if (bo->base.ptr) {
154                 bo->map_count++;
155                 *out = bo->base.ptr;
156                 return 0;
157         }
158
159         map = mmap(0, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, bo->map_handle);
160         if (map == MAP_FAILED)
161                 return -errno;
162
163         bo->base.ptr = map;
164         bo->map_count++;
165         *out = bo->base.ptr;
166
167         return 0;
168 }
169
170 static int
171 nouveau_bo_unmap(struct kms_bo *_bo)
172 {
173         struct nouveau_bo *bo = (struct nouveau_bo *)_bo;
174         bo->map_count--;
175         return 0;
176 }
177
178 static int
179 nouveau_bo_destroy(struct kms_bo *_bo)
180 {
181         struct nouveau_bo *bo = (struct nouveau_bo *)_bo;
182         struct drm_gem_close arg;
183         int ret;
184
185         if (bo->base.ptr) {
186                 /* XXX Sanity check map_count */
187                 munmap(bo->base.ptr, bo->base.size);
188                 bo->base.ptr = NULL;
189         }
190
191         memset(&arg, 0, sizeof(arg));
192         arg.handle = bo->base.handle;
193
194         ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_GEM_CLOSE, &arg);
195         if (ret)
196                 return -errno;
197
198         free(bo);
199         return 0;
200 }
201
202 int
203 nouveau_create(int fd, struct kms_driver **out)
204 {
205         struct kms_driver *kms;
206
207         kms = calloc(1, sizeof(*kms));
208         if (!kms)
209                 return -ENOMEM;
210
211         kms->fd = fd;
212
213         kms->bo_create = nouveau_bo_create;
214         kms->bo_map = nouveau_bo_map;
215         kms->bo_unmap = nouveau_bo_unmap;
216         kms->bo_get_prop = nouveau_bo_get_prop;
217         kms->bo_destroy = nouveau_bo_destroy;
218         kms->get_prop = nouveau_get_prop;
219         kms->destroy = nouveau_destroy;
220         *out = kms;
221
222         return 0;
223 }