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
27 #include "r600_priv.h"
28 #include "util/u_hash_table.h"
29 #include "util/u_memory.h"
30 #include "radeon_drm.h"
35 int radeon_bo_fixed_map(struct radeon
*radeon
, struct radeon_bo
*bo
)
37 struct drm_radeon_gem_mmap args
;
41 /* Zero out args to make valgrind happy */
42 memset(&args
, 0, sizeof(args
));
43 args
.handle
= bo
->handle
;
45 args
.size
= (uint64_t)bo
->size
;
46 r
= drmCommandWriteRead(radeon
->info
.fd
, DRM_RADEON_GEM_MMAP
,
49 fprintf(stderr
, "error mapping %p 0x%08X (error = %d)\n",
53 ptr
= mmap(0, args
.size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, radeon
->info
.fd
, args
.addr_ptr
);
54 if (ptr
== MAP_FAILED
) {
55 fprintf(stderr
, "%s failed to map bo\n", __func__
);
64 static void radeon_bo_fixed_unmap(struct radeon
*radeon
, struct radeon_bo
*bo
)
67 munmap(bo
->data
, bo
->size
);
72 #include "state_tracker/drm_driver.h"
74 struct radeon_bo
*radeon_bo(struct radeon
*radeon
, unsigned handle
,
75 unsigned size
, unsigned alignment
, unsigned bind
,
76 unsigned initial_domain
)
79 struct winsys_handle whandle
= {};
80 whandle
.handle
= handle
;
82 bo
= calloc(1, sizeof(*bo
));
88 pipe_reference_init(&bo
->reference
, 1);
89 LIST_INITHEAD(&bo
->fencedlist
);
93 bo
->buf
= radeon
->ws
->buffer_from_handle(radeon
->ws
, &whandle
, NULL
, &size
);
98 bo
->handle
= radeon
->ws
->trans_get_buffer_handle(bo
->buf
);
102 bo
->buf
= radeon
->ws
->buffer_create(radeon
->ws
, size
, alignment
, bind
, initial_domain
);
107 bo
->handle
= radeon
->ws
->trans_get_buffer_handle(bo
->buf
);
112 static void radeon_bo_destroy(struct radeon
*radeon
, struct radeon_bo
*bo
)
114 LIST_DEL(&bo
->fencedlist
);
115 radeon_bo_fixed_unmap(radeon
, bo
);
116 pb_reference(&bo
->buf
, NULL
);
120 void radeon_bo_reference(struct radeon
*radeon
,
121 struct radeon_bo
**dst
,
122 struct radeon_bo
*src
)
124 struct radeon_bo
*old
= *dst
;
125 if (pipe_reference(&(*dst
)->reference
, &src
->reference
)) {
126 radeon_bo_destroy(radeon
, old
);
131 int radeon_bo_wait(struct radeon
*radeon
, struct radeon_bo
*bo
)
133 struct drm_radeon_gem_wait_idle args
;
139 if (bo
->fence
<= *radeon
->cfence
) {
140 LIST_DELINIT(&bo
->fencedlist
);
146 /* Zero out args to make valgrind happy */
147 memset(&args
, 0, sizeof(args
));
148 args
.handle
= bo
->handle
;
150 ret
= drmCommandWriteRead(radeon
->info
.fd
, DRM_RADEON_GEM_WAIT_IDLE
,
151 &args
, sizeof(args
));
152 } while (ret
== -EBUSY
);
156 int radeon_bo_busy(struct radeon
*radeon
, struct radeon_bo
*bo
, uint32_t *domain
)
158 struct drm_radeon_gem_busy args
;
164 if (bo
->fence
<= *radeon
->cfence
) {
165 LIST_DELINIT(&bo
->fencedlist
);
171 memset(&args
, 0, sizeof(args
));
172 args
.handle
= bo
->handle
;
175 ret
= drmCommandWriteRead(radeon
->info
.fd
, DRM_RADEON_GEM_BUSY
,
176 &args
, sizeof(args
));
178 *domain
= args
.domain
;
182 int radeon_bo_get_tiling_flags(struct radeon
*radeon
,
183 struct radeon_bo
*bo
,
184 uint32_t *tiling_flags
)
186 struct drm_radeon_gem_get_tiling args
= {};
189 args
.handle
= bo
->handle
;
190 ret
= drmCommandWriteRead(radeon
->info
.fd
, DRM_RADEON_GEM_GET_TILING
,
191 &args
, sizeof(args
));
195 *tiling_flags
= args
.tiling_flags
;
199 int radeon_bo_get_name(struct radeon
*radeon
,
200 struct radeon_bo
*bo
,
203 struct drm_gem_flink flink
;
206 flink
.handle
= bo
->handle
;
207 ret
= drmIoctl(radeon
->info
.fd
, DRM_IOCTL_GEM_FLINK
, &flink
);