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 "radeon_priv.h"
34 #include "radeon_drm.h"
36 struct radeon_bo
*radeon_bo(struct radeon
*radeon
, unsigned handle
,
37 unsigned size
, unsigned alignment
, void *ptr
)
42 bo
= calloc(1, sizeof(*bo
));
48 pipe_reference_init(&bo
->reference
, 1);
49 bo
->alignment
= alignment
;
52 struct drm_gem_open open_arg
;
54 memset(&open_arg
, 0, sizeof(open_arg
));
55 open_arg
.name
= handle
;
56 r
= drmIoctl(radeon
->fd
, DRM_IOCTL_GEM_OPEN
, &open_arg
);
61 bo
->handle
= open_arg
.handle
;
62 bo
->size
= open_arg
.size
;
64 struct drm_radeon_gem_create args
;
67 args
.alignment
= alignment
;
68 args
.initial_domain
= RADEON_GEM_DOMAIN_CPU
;
71 r
= drmCommandWriteRead(radeon
->fd
, DRM_RADEON_GEM_CREATE
,
73 bo
->handle
= args
.handle
;
75 fprintf(stderr
, "Failed to allocate :\n");
76 fprintf(stderr
, " size : %d bytes\n", size
);
77 fprintf(stderr
, " alignment : %d bytes\n", alignment
);
83 if (radeon_bo_map(radeon
, bo
)) {
84 fprintf(stderr
, "%s failed to copy data into bo\n", __func__
);
85 radeon_bo_reference(radeon
, &bo
, NULL
);
88 memcpy(bo
->data
, ptr
, size
);
89 radeon_bo_unmap(radeon
, bo
);
94 int radeon_bo_map(struct radeon
*radeon
, struct radeon_bo
*bo
)
96 struct drm_radeon_gem_mmap args
;
100 if (bo
->map_count
!= 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(radeon
->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
, radeon
->fd
, args
.addr_ptr
);
116 if (ptr
== MAP_FAILED
) {
117 fprintf(stderr
, "%s failed to map bo\n", __func__
);
128 void radeon_bo_unmap(struct radeon
*radeon
, struct radeon_bo
*bo
)
130 if (--bo
->map_count
> 0) {
133 munmap(bo
->data
, bo
->size
);
137 static void radeon_bo_destroy(struct radeon
*radeon
, struct radeon_bo
*bo
)
139 struct drm_gem_close args
;
141 memset(&args
, 0, sizeof(args
));
142 args
.handle
= bo
->handle
;
143 drmIoctl(radeon
->fd
, DRM_IOCTL_GEM_CLOSE
, &args
);
144 memset(bo
, 0, sizeof(struct radeon_bo
));
148 void radeon_bo_reference(struct radeon
*radeon
,
149 struct radeon_bo
**dst
,
150 struct radeon_bo
*src
)
152 struct radeon_bo
*old
= *dst
;
153 if (pipe_reference(&(*dst
)->reference
, &src
->reference
)) {
154 radeon_bo_destroy(radeon
, old
);
159 int radeon_bo_wait(struct radeon
*radeon
, struct radeon_bo
*bo
)
161 struct drm_radeon_gem_wait_idle args
;
164 /* Zero out args to make valgrind happy */
165 memset(&args
, 0, sizeof(args
));
166 args
.handle
= bo
->handle
;
168 ret
= drmCommandWriteRead(radeon
->fd
, DRM_RADEON_GEM_WAIT_IDLE
,
169 &args
, sizeof(args
));
170 } while (ret
== -EBUSY
);