blob: 7fe23db3334d6a70812170c5dbf4cb4de6722318 [file] [log] [blame]
Marcin Kościelnickid5a2e772010-02-27 16:02:25 +00001/**************************************************************************
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
Marcin Kościelnickid5a2e772010-02-27 16:02:25 +000029#include <errno.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include "internal.h"
34
Marcin Kościelnickid5a2e772010-02-27 16:02:25 +000035#include <sys/ioctl.h>
36#include "xf86drm.h"
Emil Velikov42465fe2015-04-05 15:51:59 +010037#include "libdrm_macros.h"
Marcin Kościelnickid5a2e772010-02-27 16:02:25 +000038
39#include "nouveau_drm.h"
40
41struct nouveau_bo
42{
43 struct kms_bo base;
44 uint64_t map_handle;
45 unsigned map_count;
46};
47
48static int
49nouveau_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
50{
51 switch (key) {
52 case KMS_BO_TYPE:
53 *out = KMS_BO_TYPE_SCANOUT_X8R8G8B8 | KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8;
54 break;
55 default:
56 return -EINVAL;
57 }
58 return 0;
59}
60
61static int
62nouveau_destroy(struct kms_driver *kms)
63{
64 free(kms);
65 return 0;
66}
67
68static int
69nouveau_bo_create(struct kms_driver *kms,
70 const unsigned width, const unsigned height,
71 const enum kms_bo_type type, const unsigned *attr,
72 struct kms_bo **out)
73{
74 struct drm_nouveau_gem_new arg;
75 unsigned size, pitch;
76 struct nouveau_bo *bo;
77 int i, ret;
78
79 for (i = 0; attr[i]; i += 2) {
80 switch (attr[i]) {
81 case KMS_WIDTH:
82 case KMS_HEIGHT:
83 case KMS_BO_TYPE:
84 break;
85 default:
86 return -EINVAL;
87 }
88 }
89
90 bo = calloc(1, sizeof(*bo));
91 if (!bo)
92 return -ENOMEM;
93
94 if (type == KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8) {
95 pitch = 64 * 4;
96 size = 64 * 64 * 4;
97 } else if (type == KMS_BO_TYPE_SCANOUT_X8R8G8B8) {
98 pitch = width * 4;
99 pitch = (pitch + 512 - 1) & ~(512 - 1);
100 size = pitch * height;
101 } else {
Maxime Villard1f4b5e12013-01-02 10:55:50 -0500102 free(bo);
Marcin Kościelnickid5a2e772010-02-27 16:02:25 +0000103 return -EINVAL;
104 }
105
106 memset(&arg, 0, sizeof(arg));
107 arg.info.size = size;
108 arg.info.domain = NOUVEAU_GEM_DOMAIN_MAPPABLE | NOUVEAU_GEM_DOMAIN_VRAM;
109 arg.info.tile_mode = 0;
110 arg.info.tile_flags = 0;
111 arg.align = 512;
112 arg.channel_hint = 0;
113
114 ret = drmCommandWriteRead(kms->fd, DRM_NOUVEAU_GEM_NEW, &arg, sizeof(arg));
115 if (ret)
116 goto err_free;
117
118 bo->base.kms = kms;
119 bo->base.handle = arg.info.handle;
120 bo->base.size = size;
121 bo->base.pitch = pitch;
122 bo->map_handle = arg.info.map_handle;
123
124 *out = &bo->base;
125
126 return 0;
127
128err_free:
129 free(bo);
130 return ret;
131}
132
133static int
134nouveau_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
135{
136 switch (key) {
137 default:
138 return -EINVAL;
139 }
140}
141
142static int
143nouveau_bo_map(struct kms_bo *_bo, void **out)
144{
145 struct nouveau_bo *bo = (struct nouveau_bo *)_bo;
146 void *map = NULL;
147
148 if (bo->base.ptr) {
149 bo->map_count++;
150 *out = bo->base.ptr;
151 return 0;
152 }
153
Emil Velikovdadc9ef2014-09-07 19:41:37 +0100154 map = drm_mmap(0, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, bo->map_handle);
Marcin Kościelnickid5a2e772010-02-27 16:02:25 +0000155 if (map == MAP_FAILED)
156 return -errno;
157
158 bo->base.ptr = map;
159 bo->map_count++;
160 *out = bo->base.ptr;
161
162 return 0;
163}
164
165static int
166nouveau_bo_unmap(struct kms_bo *_bo)
167{
168 struct nouveau_bo *bo = (struct nouveau_bo *)_bo;
169 bo->map_count--;
170 return 0;
171}
172
173static int
174nouveau_bo_destroy(struct kms_bo *_bo)
175{
176 struct nouveau_bo *bo = (struct nouveau_bo *)_bo;
177 struct drm_gem_close arg;
178 int ret;
179
180 if (bo->base.ptr) {
181 /* XXX Sanity check map_count */
Emil Velikovdadc9ef2014-09-07 19:41:37 +0100182 drm_munmap(bo->base.ptr, bo->base.size);
Marcin Kościelnickid5a2e772010-02-27 16:02:25 +0000183 bo->base.ptr = NULL;
184 }
185
186 memset(&arg, 0, sizeof(arg));
187 arg.handle = bo->base.handle;
188
189 ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_GEM_CLOSE, &arg);
190 if (ret)
191 return -errno;
192
193 free(bo);
194 return 0;
195}
196
Emil Velikov983892d2015-03-23 23:24:48 +0000197drm_private int
Marcin Kościelnickid5a2e772010-02-27 16:02:25 +0000198nouveau_create(int fd, struct kms_driver **out)
199{
200 struct kms_driver *kms;
201
202 kms = calloc(1, sizeof(*kms));
203 if (!kms)
204 return -ENOMEM;
205
206 kms->fd = fd;
207
208 kms->bo_create = nouveau_bo_create;
209 kms->bo_map = nouveau_bo_map;
210 kms->bo_unmap = nouveau_bo_unmap;
211 kms->bo_get_prop = nouveau_bo_get_prop;
212 kms->bo_destroy = nouveau_bo_destroy;
213 kms->get_prop = nouveau_get_prop;
214 kms->destroy = nouveau_destroy;
215 *out = kms;
216
217 return 0;
218}