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 return -EINVAL;
106 }
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;
116 ret = drmCommandWriteRead(kms->fd, DRM_NOUVEAU_GEM_NEW, &arg, sizeof(arg));
117 if (ret)
118 goto err_free;
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;
126 *out = &bo->base;
128 return 0;
130 err_free:
131 free(bo);
132 return ret;
133 }
135 static int
136 nouveau_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
137 {
138 switch (key) {
139 default:
140 return -EINVAL;
141 }
142 }
144 static int
145 nouveau_bo_map(struct kms_bo *_bo, void **out)
146 {
147 struct nouveau_bo *bo = (struct nouveau_bo *)_bo;
148 void *map = NULL;
150 if (bo->base.ptr) {
151 bo->map_count++;
152 *out = bo->base.ptr;
153 return 0;
154 }
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;
160 bo->base.ptr = map;
161 bo->map_count++;
162 *out = bo->base.ptr;
164 return 0;
165 }
167 static int
168 nouveau_bo_unmap(struct kms_bo *_bo)
169 {
170 struct nouveau_bo *bo = (struct nouveau_bo *)_bo;
171 bo->map_count--;
172 return 0;
173 }
175 static int
176 nouveau_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;
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 }
188 memset(&arg, 0, sizeof(arg));
189 arg.handle = bo->base.handle;
191 ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_GEM_CLOSE, &arg);
192 if (ret)
193 return -errno;
195 free(bo);
196 return 0;
197 }
199 int
200 nouveau_create(int fd, struct kms_driver **out)
201 {
202 struct kms_driver *kms;
204 kms = calloc(1, sizeof(*kms));
205 if (!kms)
206 return -ENOMEM;
208 kms->fd = fd;
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;
219 return 0;
220 }