2 * Copyright © 2011 Red Hat
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Jerome Glisse <j.glisse@gmail.com>
26 #define _FILE_OFFSET_BITS 64
33 #include "radeon_drm.h"
36 struct rbo
*rbo(int fd
, unsigned handle
, unsigned size
,
37 unsigned alignment
, void *ptr
)
42 bo
= calloc(1, sizeof(*bo
));
46 list_inithead(&bo
->list
);
51 bo
->alignment
= alignment
;
54 struct drm_gem_open open_arg
;
56 memset(&open_arg
, 0, sizeof(open_arg
));
57 open_arg
.name
= handle
;
58 r
= drmIoctl(fd
, DRM_IOCTL_GEM_OPEN
, &open_arg
);
63 bo
->handle
= open_arg
.handle
;
65 struct drm_radeon_gem_create args
;
68 args
.alignment
= alignment
;
69 args
.initial_domain
= RADEON_GEM_DOMAIN_CPU
;
72 r
= drmCommandWriteRead(fd
, DRM_RADEON_GEM_CREATE
,
74 bo
->handle
= args
.handle
;
76 fprintf(stderr
, "Failed to allocate :\n");
77 fprintf(stderr
, " size : %d bytes\n", size
);
78 fprintf(stderr
, " alignment : %d bytes\n", alignment
);
85 fprintf(stderr
, "%s failed to copy data into bo\n", __func__
);
86 return rbo_decref(bo
);
88 memcpy(bo
->data
, ptr
, size
);
94 int rbo_map(struct rbo
*bo
)
96 struct drm_radeon_gem_mmap args
;
100 if (bo
->mapcount
++ != 0) {
103 /* Zero out args to make valgrind happy */
104 memset(&args
, 0, sizeof(args
));
105 args
.handle
= bo
->handle
;
107 args
.size
= (uint64_t)bo
->size
;
108 r
= drmCommandWriteRead(bo
->fd
, DRM_RADEON_GEM_MMAP
,
109 &args
, sizeof(args
));
111 fprintf(stderr
, "error mapping %p 0x%08X (error = %d)\n",
115 ptr
= mmap(0, args
.size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, bo
->fd
, args
.addr_ptr
);
116 if (ptr
== MAP_FAILED
) {
117 fprintf(stderr
, "%s failed to map bo\n", __func__
);
124 void rbo_unmap(struct rbo
*bo
)
126 if (--bo
->mapcount
> 0) {
129 munmap(bo
->data
, bo
->size
);
133 struct rbo
*rbo_incref(struct rbo
*bo
)
139 struct rbo
*rbo_decref(struct rbo
*bo
)
141 struct drm_gem_close args
;
145 if (--bo
->refcount
> 0) {
149 munmap(bo
->data
, bo
->size
);
150 memset(&args
, 0, sizeof(args
));
151 args
.handle
= bo
->handle
;
152 drmIoctl(bo
->fd
, DRM_IOCTL_GEM_CLOSE
, &args
);
153 memset(bo
, 0, sizeof(struct rbo
));
158 int rbo_wait(struct rbo
*bo
)
160 struct drm_radeon_gem_wait_idle args
;
163 /* Zero out args to make valgrind happy */
164 memset(&args
, 0, sizeof(args
));
165 args
.handle
= bo
->handle
;
167 ret
= drmCommandWriteRead(bo
->fd
, DRM_RADEON_GEM_WAIT_IDLE
,
168 &args
, sizeof(args
));
169 } while (ret
== -EBUSY
);