aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'libkms/nouveau.c')
-rw-r--r--libkms/nouveau.c220
1 files changed, 220 insertions, 0 deletions
diff --git a/libkms/nouveau.c b/libkms/nouveau.c
new file mode 100644
index 00000000..0e24a155
--- /dev/null
+++ b/libkms/nouveau.c
@@ -0,0 +1,220 @@
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#define HAVE_STDINT_H
30#define _FILE_OFFSET_BITS 64
31
32#include <errno.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include "internal.h"
37
38#include <sys/mman.h>
39#include <sys/ioctl.h>
40#include "xf86drm.h"
41
42#include "nouveau_drm.h"
43
44struct nouveau_bo
45{
46 struct kms_bo base;
47 uint64_t map_handle;
48 unsigned map_count;
49};
50
51static int
52nouveau_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}
63
64static int
65nouveau_destroy(struct kms_driver *kms)
66{
67 free(kms);
68 return 0;
69}
70
71static int
72nouveau_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;
81
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 }
92
93 bo = calloc(1, sizeof(*bo));
94 if (!bo)
95 return -ENOMEM;
96
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 return -EINVAL;
106 }
107
108 memset(&arg, 0, sizeof(arg));
109 arg.info.size = size;
110 arg.info.domain = NOUVEAU_GEM_DOMAIN_MAPPABLE | NOUVEAU_GEM_DOMAIN_VRAM;
111 arg.info.tile_mode = 0;
112 arg.info.tile_flags = 0;
113 arg.align = 512;
114 arg.channel_hint = 0;
115
116 ret = drmCommandWriteRead(kms->fd, DRM_NOUVEAU_GEM_NEW, &arg, sizeof(arg));
117 if (ret)
118 goto err_free;
119
120 bo->base.kms = kms;
121 bo->base.handle = arg.info.handle;
122 bo->base.size = size;
123 bo->base.pitch = pitch;
124 bo->map_handle = arg.info.map_handle;
125
126 *out = &bo->base;
127
128 return 0;
129
130err_free:
131 free(bo);
132 return ret;
133}
134
135static int
136nouveau_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
137{
138 switch (key) {
139 default:
140 return -EINVAL;
141 }
142}
143
144static int
145nouveau_bo_map(struct kms_bo *_bo, void **out)
146{
147 struct nouveau_bo *bo = (struct nouveau_bo *)_bo;
148 void *map = NULL;
149
150 if (bo->base.ptr) {
151 bo->map_count++;
152 *out = bo->base.ptr;
153 return 0;
154 }
155
156 map = mmap(0, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, bo->map_handle);
157 if (map == MAP_FAILED)
158 return -errno;
159
160 bo->base.ptr = map;
161 bo->map_count++;
162 *out = bo->base.ptr;
163
164 return 0;
165}
166
167static int
168nouveau_bo_unmap(struct kms_bo *_bo)
169{
170 struct nouveau_bo *bo = (struct nouveau_bo *)_bo;
171 bo->map_count--;
172 return 0;
173}
174
175static int
176nouveau_bo_destroy(struct kms_bo *_bo)
177{
178 struct nouveau_bo *bo = (struct nouveau_bo *)_bo;
179 struct drm_gem_close arg;
180 int ret;
181
182 if (bo->base.ptr) {
183 /* XXX Sanity check map_count */
184 munmap(bo->base.ptr, bo->base.size);
185 bo->base.ptr = NULL;
186 }
187
188 memset(&arg, 0, sizeof(arg));
189 arg.handle = bo->base.handle;
190
191 ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_GEM_CLOSE, &arg);
192 if (ret)
193 return -errno;
194
195 free(bo);
196 return 0;
197}
198
199int
200nouveau_create(int fd, struct kms_driver **out)
201{
202 struct kms_driver *kms;
203
204 kms = calloc(1, sizeof(*kms));
205 if (!kms)
206 return -ENOMEM;
207
208 kms->fd = fd;
209
210 kms->bo_create = nouveau_bo_create;
211 kms->bo_map = nouveau_bo_map;
212 kms->bo_unmap = nouveau_bo_unmap;
213 kms->bo_get_prop = nouveau_bo_get_prop;
214 kms->bo_destroy = nouveau_bo_destroy;
215 kms->get_prop = nouveau_get_prop;
216 kms->destroy = nouveau_destroy;
217 *out = kms;
218
219 return 0;
220}