2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #define _FILE_OFFSET_BITS 64
32 #include "r600_priv.h"
34 #include "radeon_drm.h"
36 static int radeon_bo_fixed_map(struct radeon
*radeon
, struct radeon_bo
*bo
)
38 struct drm_radeon_gem_mmap args
;
42 /* Zero out args to make valgrind happy */
43 memset(&args
, 0, sizeof(args
));
44 args
.handle
= bo
->handle
;
46 args
.size
= (uint64_t)bo
->size
;
47 r
= drmCommandWriteRead(radeon
->fd
, DRM_RADEON_GEM_MMAP
,
50 fprintf(stderr
, "error mapping %p 0x%08X (error = %d)\n",
54 ptr
= mmap(0, args
.size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, radeon
->fd
, args
.addr_ptr
);
55 if (ptr
== MAP_FAILED
) {
56 fprintf(stderr
, "%s failed to map bo\n", __func__
);
65 static void radeon_bo_fixed_unmap(struct radeon
*radeon
, struct radeon_bo
*bo
)
67 munmap(bo
->data
, bo
->size
);
71 struct radeon_bo
*radeon_bo(struct radeon
*radeon
, unsigned handle
,
72 unsigned size
, unsigned alignment
)
78 pipe_mutex_lock(radeon
->bo_handles_mutex
);
79 bo
= util_hash_table_get(radeon
->bo_handles
,
80 (void *)(uintptr_t)handle
);
82 struct radeon_bo
*b
= NULL
;
83 radeon_bo_reference(radeon
, &b
, bo
);
87 bo
= calloc(1, sizeof(*bo
));
93 pipe_reference_init(&bo
->reference
, 1);
94 bo
->alignment
= alignment
;
95 LIST_INITHEAD(&bo
->fencedlist
);
98 struct drm_gem_open open_arg
;
100 memset(&open_arg
, 0, sizeof(open_arg
));
101 open_arg
.name
= handle
;
102 r
= drmIoctl(radeon
->fd
, DRM_IOCTL_GEM_OPEN
, &open_arg
);
108 bo
->handle
= open_arg
.handle
;
109 bo
->size
= open_arg
.size
;
112 struct drm_radeon_gem_create args
= {};
115 args
.alignment
= alignment
;
116 args
.initial_domain
= RADEON_GEM_DOMAIN_CPU
;
119 r
= drmCommandWriteRead(radeon
->fd
, DRM_RADEON_GEM_CREATE
,
120 &args
, sizeof(args
));
121 bo
->handle
= args
.handle
;
123 fprintf(stderr
, "Failed to allocate :\n");
124 fprintf(stderr
, " size : %d bytes\n", size
);
125 fprintf(stderr
, " alignment : %d bytes\n", alignment
);
130 if (radeon_bo_fixed_map(radeon
, bo
)) {
131 R600_ERR("failed to map bo\n");
132 radeon_bo_reference(radeon
, &bo
, NULL
);
137 util_hash_table_set(radeon
->bo_handles
, (void *)(uintptr_t)handle
, bo
);
140 pipe_mutex_unlock(radeon
->bo_handles_mutex
);
145 static void radeon_bo_destroy(struct radeon
*radeon
, struct radeon_bo
*bo
)
147 struct drm_gem_close args
;
150 pipe_mutex_lock(radeon
->bo_handles_mutex
);
151 util_hash_table_remove(radeon
->bo_handles
,
152 (void *)(uintptr_t)bo
->name
);
153 pipe_mutex_unlock(radeon
->bo_handles_mutex
);
155 LIST_DEL(&bo
->fencedlist
);
156 radeon_bo_fixed_unmap(radeon
, bo
);
157 memset(&args
, 0, sizeof(args
));
158 args
.handle
= bo
->handle
;
159 drmIoctl(radeon
->fd
, DRM_IOCTL_GEM_CLOSE
, &args
);
160 memset(bo
, 0, sizeof(struct radeon_bo
));
164 void radeon_bo_reference(struct radeon
*radeon
,
165 struct radeon_bo
**dst
,
166 struct radeon_bo
*src
)
168 struct radeon_bo
*old
= *dst
;
169 if (pipe_reference(&(*dst
)->reference
, &src
->reference
)) {
170 radeon_bo_destroy(radeon
, old
);
175 int radeon_bo_wait(struct radeon
*radeon
, struct radeon_bo
*bo
)
177 struct drm_radeon_gem_wait_idle args
;
183 if (bo
->fence
<= *radeon
->cfence
) {
184 LIST_DELINIT(&bo
->fencedlist
);
190 /* Zero out args to make valgrind happy */
191 memset(&args
, 0, sizeof(args
));
192 args
.handle
= bo
->handle
;
194 ret
= drmCommandWriteRead(radeon
->fd
, DRM_RADEON_GEM_WAIT_IDLE
,
195 &args
, sizeof(args
));
196 } while (ret
== -EBUSY
);
200 int radeon_bo_busy(struct radeon
*radeon
, struct radeon_bo
*bo
, uint32_t *domain
)
202 struct drm_radeon_gem_busy args
;
208 if (bo
->fence
<= *radeon
->cfence
) {
209 LIST_DELINIT(&bo
->fencedlist
);
215 memset(&args
, 0, sizeof(args
));
216 args
.handle
= bo
->handle
;
219 ret
= drmCommandWriteRead(radeon
->fd
, DRM_RADEON_GEM_BUSY
,
220 &args
, sizeof(args
));
222 *domain
= args
.domain
;
226 int radeon_bo_get_tiling_flags(struct radeon
*radeon
,
227 struct radeon_bo
*bo
,
228 uint32_t *tiling_flags
,
231 struct drm_radeon_gem_get_tiling args
= {};
234 args
.handle
= bo
->handle
;
235 ret
= drmCommandWriteRead(radeon
->fd
, DRM_RADEON_GEM_GET_TILING
,
236 &args
, sizeof(args
));
240 *tiling_flags
= args
.tiling_flags
;
245 int radeon_bo_get_name(struct radeon
*radeon
,
246 struct radeon_bo
*bo
,
249 struct drm_gem_flink flink
;
252 flink
.handle
= bo
->handle
;
253 ret
= drmIoctl(radeon
->fd
, DRM_IOCTL_GEM_FLINK
, &flink
);