1 /*
2 * Copyright © 2008 Dave Airlie
3 * Copyright © 2008 Jérôme Glisse
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a 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 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
18 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
25 * of the Software.
26 */
27 /*
28 * Authors:
29 * Dave Airlie
30 * Jérôme Glisse <glisse@freedesktop.org>
31 */
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35 #include <stdio.h>
36 #include <stdint.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/mman.h>
40 #include <errno.h>
41 #include "xf86drm.h"
42 #include "drm.h"
43 #include "radeon_drm.h"
44 #include "radeon_bo.h"
45 #include "radeon_bo_int.h"
46 #include "radeon_bo_gem.h"
48 struct radeon_bo_gem {
49 struct radeon_bo_int base;
50 uint32_t name;
51 int map_count;
52 void *priv_ptr;
53 };
55 struct bo_manager_gem {
56 struct radeon_bo_manager base;
57 };
59 static int bo_wait(struct radeon_bo_int *boi);
61 static struct radeon_bo *bo_open(struct radeon_bo_manager *bom,
62 uint32_t handle,
63 uint32_t size,
64 uint32_t alignment,
65 uint32_t domains,
66 uint32_t flags)
67 {
68 struct radeon_bo_gem *bo;
69 int r;
71 bo = (struct radeon_bo_gem*)calloc(1, sizeof(struct radeon_bo_gem));
72 if (bo == NULL) {
73 return NULL;
74 }
76 bo->base.bom = bom;
77 bo->base.handle = 0;
78 bo->base.size = size;
79 bo->base.alignment = alignment;
80 bo->base.domains = domains;
81 bo->base.flags = flags;
82 bo->base.ptr = NULL;
83 bo->map_count = 0;
84 if (handle) {
85 struct drm_gem_open open_arg;
87 memset(&open_arg, 0, sizeof(open_arg));
88 open_arg.name = handle;
89 r = drmIoctl(bom->fd, DRM_IOCTL_GEM_OPEN, &open_arg);
90 if (r != 0) {
91 free(bo);
92 return NULL;
93 }
94 bo->base.handle = open_arg.handle;
95 bo->base.size = open_arg.size;
96 bo->name = handle;
97 } else {
98 struct drm_radeon_gem_create args;
100 args.size = size;
101 args.alignment = alignment;
102 args.initial_domain = bo->base.domains;
103 args.flags = 0;
104 args.handle = 0;
105 r = drmCommandWriteRead(bom->fd, DRM_RADEON_GEM_CREATE,
106 &args, sizeof(args));
107 bo->base.handle = args.handle;
108 if (r) {
109 fprintf(stderr, "Failed to allocate :\n");
110 fprintf(stderr, " size : %d bytes\n", size);
111 fprintf(stderr, " alignment : %d bytes\n", alignment);
112 fprintf(stderr, " domains : %d\n", bo->base.domains);
113 free(bo);
114 return NULL;
115 }
116 }
117 radeon_bo_ref((struct radeon_bo*)bo);
118 return (struct radeon_bo*)bo;
119 }
121 static void bo_ref(struct radeon_bo_int *boi)
122 {
123 }
125 static struct radeon_bo *bo_unref(struct radeon_bo_int *boi)
126 {
127 struct radeon_bo_gem *bo_gem = (struct radeon_bo_gem*)boi;
128 struct drm_gem_close args;
130 if (boi->cref) {
131 return (struct radeon_bo *)boi;
132 }
133 if (bo_gem->priv_ptr) {
134 munmap(bo_gem->priv_ptr, boi->size);
135 }
137 /* Zero out args to make valgrind happy */
138 memset(&args, 0, sizeof(args));
140 /* close object */
141 args.handle = boi->handle;
142 drmIoctl(boi->bom->fd, DRM_IOCTL_GEM_CLOSE, &args);
143 memset(bo_gem, 0, sizeof(struct radeon_bo_gem));
144 free(bo_gem);
145 return NULL;
146 }
148 static int bo_map(struct radeon_bo_int *boi, int write)
149 {
150 struct radeon_bo_gem *bo_gem = (struct radeon_bo_gem*)boi;
151 struct drm_radeon_gem_mmap args;
152 int r;
153 void *ptr;
155 if (bo_gem->map_count++ != 0) {
156 return 0;
157 }
158 if (bo_gem->priv_ptr) {
159 goto wait;
160 }
162 boi->ptr = NULL;
164 /* Zero out args to make valgrind happy */
165 memset(&args, 0, sizeof(args));
166 args.handle = boi->handle;
167 args.offset = 0;
168 args.size = (uint64_t)boi->size;
169 r = drmCommandWriteRead(boi->bom->fd,
170 DRM_RADEON_GEM_MMAP,
171 &args,
172 sizeof(args));
173 if (r) {
174 fprintf(stderr, "error mapping %p 0x%08X (error = %d)\n",
175 boi, boi->handle, r);
176 return r;
177 }
178 ptr = mmap(0, args.size, PROT_READ|PROT_WRITE, MAP_SHARED, boi->bom->fd, args.addr_ptr);
179 if (ptr == MAP_FAILED)
180 return -errno;
181 bo_gem->priv_ptr = ptr;
182 wait:
183 boi->ptr = bo_gem->priv_ptr;
184 r = bo_wait(boi);
185 if (r)
186 return r;
187 return 0;
188 }
190 static int bo_unmap(struct radeon_bo_int *boi)
191 {
192 struct radeon_bo_gem *bo_gem = (struct radeon_bo_gem*)boi;
194 if (--bo_gem->map_count > 0) {
195 return 0;
196 }
197 //munmap(bo->ptr, bo->size);
198 boi->ptr = NULL;
199 return 0;
200 }
202 static int bo_wait(struct radeon_bo_int *boi)
203 {
204 struct drm_radeon_gem_wait_idle args;
205 int ret;
207 /* Zero out args to make valgrind happy */
208 memset(&args, 0, sizeof(args));
209 args.handle = boi->handle;
210 do {
211 ret = drmCommandWriteRead(boi->bom->fd, DRM_RADEON_GEM_WAIT_IDLE,
212 &args, sizeof(args));
213 } while (ret == -EBUSY);
214 return ret;
215 }
217 static int bo_is_busy(struct radeon_bo_int *boi, uint32_t *domain)
218 {
219 struct drm_radeon_gem_busy args;
220 int ret;
222 args.handle = boi->handle;
223 args.domain = 0;
225 ret = drmCommandWriteRead(boi->bom->fd, DRM_RADEON_GEM_BUSY,
226 &args, sizeof(args));
228 *domain = args.domain;
229 return ret;
230 }
232 static int bo_set_tiling(struct radeon_bo_int *boi, uint32_t tiling_flags,
233 uint32_t pitch)
234 {
235 struct drm_radeon_gem_set_tiling args;
236 int r;
238 args.handle = boi->handle;
239 args.tiling_flags = tiling_flags;
240 args.pitch = pitch;
242 r = drmCommandWriteRead(boi->bom->fd,
243 DRM_RADEON_GEM_SET_TILING,
244 &args,
245 sizeof(args));
246 return r;
247 }
249 static int bo_get_tiling(struct radeon_bo_int *boi, uint32_t *tiling_flags,
250 uint32_t *pitch)
251 {
252 struct drm_radeon_gem_set_tiling args;
253 int r;
255 args.handle = boi->handle;
257 r = drmCommandWriteRead(boi->bom->fd,
258 DRM_RADEON_GEM_GET_TILING,
259 &args,
260 sizeof(args));
262 if (r)
263 return r;
265 *tiling_flags = args.tiling_flags;
266 *pitch = args.pitch;
267 return r;
268 }
270 static struct radeon_bo_funcs bo_gem_funcs = {
271 bo_open,
272 bo_ref,
273 bo_unref,
274 bo_map,
275 bo_unmap,
276 bo_wait,
277 NULL,
278 bo_set_tiling,
279 bo_get_tiling,
280 bo_is_busy,
281 };
283 struct radeon_bo_manager *radeon_bo_manager_gem_ctor(int fd)
284 {
285 struct bo_manager_gem *bomg;
287 bomg = (struct bo_manager_gem*)calloc(1, sizeof(struct bo_manager_gem));
288 if (bomg == NULL) {
289 return NULL;
290 }
291 bomg->base.funcs = &bo_gem_funcs;
292 bomg->base.fd = fd;
293 return (struct radeon_bo_manager*)bomg;
294 }
296 void radeon_bo_manager_gem_dtor(struct radeon_bo_manager *bom)
297 {
298 struct bo_manager_gem *bomg = (struct bo_manager_gem*)bom;
300 if (bom == NULL) {
301 return;
302 }
303 free(bomg);
304 }
306 uint32_t radeon_gem_name_bo(struct radeon_bo *bo)
307 {
308 struct radeon_bo_gem *bo_gem = (struct radeon_bo_gem*)bo;
309 return bo_gem->name;
310 }
312 int radeon_gem_get_kernel_name(struct radeon_bo *bo, uint32_t *name)
313 {
314 struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
315 struct drm_gem_flink flink;
316 int r;
318 flink.handle = bo->handle;
319 r = drmIoctl(boi->bom->fd, DRM_IOCTL_GEM_FLINK, &flink);
320 if (r) {
321 return r;
322 }
323 *name = flink.name;
324 return 0;
325 }
327 int radeon_gem_set_domain(struct radeon_bo *bo, uint32_t read_domains, uint32_t write_domain)
328 {
329 struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
330 struct drm_radeon_gem_set_domain args;
331 int r;
333 args.handle = bo->handle;
334 args.read_domains = read_domains;
335 args.write_domain = write_domain;
337 r = drmCommandWriteRead(boi->bom->fd,
338 DRM_RADEON_GEM_SET_DOMAIN,
339 &args,
340 sizeof(args));
341 return r;
342 }