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
));
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 return radeon_bo_decref(radeon
, bo
);
87 memcpy(bo
->data
, ptr
, size
);
88 radeon_bo_unmap(radeon
, bo
);
93 int radeon_bo_map(struct radeon
*radeon
, struct radeon_bo
*bo
)
95 struct drm_radeon_gem_mmap args
;
99 if (bo
->map_count
++ != 0) {
102 /* Zero out args to make valgrind happy */
103 memset(&args
, 0, sizeof(args
));
104 args
.handle
= bo
->handle
;
106 args
.size
= (uint64_t)bo
->size
;
107 r
= drmCommandWriteRead(radeon
->fd
, DRM_RADEON_GEM_MMAP
,
108 &args
, sizeof(args
));
110 fprintf(stderr
, "error mapping %p 0x%08X (error = %d)\n",
114 ptr
= mmap(0, args
.size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, radeon
->fd
, args
.addr_ptr
);
115 if (ptr
== MAP_FAILED
) {
116 fprintf(stderr
, "%s failed to map bo\n", __func__
);
123 void radeon_bo_unmap(struct radeon
*radeon
, struct radeon_bo
*bo
)
125 if (--bo
->map_count
> 0) {
128 munmap(bo
->data
, bo
->size
);
132 struct radeon_bo
*radeon_bo_incref(struct radeon
*radeon
, struct radeon_bo
*bo
)
138 struct radeon_bo
*radeon_bo_decref(struct radeon
*radeon
, struct radeon_bo
*bo
)
140 struct drm_gem_close args
;
144 if (--bo
->refcount
> 0) {
149 munmap(bo
->data
, bo
->size
);
151 memset(&args
, 0, sizeof(args
));
152 args
.handle
= bo
->handle
;
153 drmIoctl(radeon
->fd
, DRM_IOCTL_GEM_CLOSE
, &args
);
154 memset(bo
, 0, sizeof(struct radeon_bo
));
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
);