r600g: cleanup includes in winsys
[mesa.git] / src / gallium / winsys / r600 / drm / radeon_bo.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 *
23 * Authors:
24 * Jerome Glisse
25 */
26 #define _FILE_OFFSET_BITS 64
27 #include "r600_priv.h"
28 #include "util/u_hash_table.h"
29 #include "radeon_drm.h"
30 #include "xf86drm.h"
31 #include <sys/mman.h>
32 #include <errno.h>
33
34 int radeon_bo_fixed_map(struct radeon *radeon, struct radeon_bo *bo)
35 {
36 struct drm_radeon_gem_mmap args;
37 void *ptr;
38 int r;
39
40 /* Zero out args to make valgrind happy */
41 memset(&args, 0, sizeof(args));
42 args.handle = bo->handle;
43 args.offset = 0;
44 args.size = (uint64_t)bo->size;
45 r = drmCommandWriteRead(radeon->info.fd, DRM_RADEON_GEM_MMAP,
46 &args, sizeof(args));
47 if (r) {
48 fprintf(stderr, "error mapping %p 0x%08X (error = %d)\n",
49 bo, bo->handle, r);
50 return r;
51 }
52 ptr = mmap(0, args.size, PROT_READ|PROT_WRITE, MAP_SHARED, radeon->info.fd, args.addr_ptr);
53 if (ptr == MAP_FAILED) {
54 fprintf(stderr, "%s failed to map bo\n", __func__);
55 return -errno;
56 }
57 bo->data = ptr;
58
59 bo->map_count++;
60 return 0;
61 }
62
63 static void radeon_bo_fixed_unmap(struct radeon *radeon, struct radeon_bo *bo)
64 {
65 if (bo->data) {
66 munmap(bo->data, bo->size);
67 bo->data = NULL;
68 }
69 }
70
71 struct radeon_bo *radeon_bo(struct radeon *radeon, unsigned handle,
72 unsigned size, unsigned alignment, unsigned initial_domain)
73 {
74 struct radeon_bo *bo;
75 int r;
76
77 if (handle) {
78 pipe_mutex_lock(radeon->bo_handles_mutex);
79 bo = util_hash_table_get(radeon->bo_handles,
80 (void *)(uintptr_t)handle);
81 if (bo) {
82 struct radeon_bo *b = NULL;
83 radeon_bo_reference(radeon, &b, bo);
84 goto done;
85 }
86 }
87 bo = calloc(1, sizeof(*bo));
88 if (bo == NULL) {
89 return NULL;
90 }
91 bo->size = size;
92 bo->handle = handle;
93 pipe_reference_init(&bo->reference, 1);
94 bo->alignment = alignment;
95 LIST_INITHEAD(&bo->fencedlist);
96
97 if (handle) {
98 struct drm_gem_open open_arg;
99
100 memset(&open_arg, 0, sizeof(open_arg));
101 open_arg.name = handle;
102 r = drmIoctl(radeon->info.fd, DRM_IOCTL_GEM_OPEN, &open_arg);
103 if (r != 0) {
104 free(bo);
105 return NULL;
106 }
107 bo->name = handle;
108 bo->handle = open_arg.handle;
109 bo->size = open_arg.size;
110 bo->shared = TRUE;
111 } else {
112 struct drm_radeon_gem_create args = {};
113
114 args.size = size;
115 args.alignment = alignment;
116 args.initial_domain = initial_domain;
117 args.flags = 0;
118 args.handle = 0;
119 r = drmCommandWriteRead(radeon->info.fd, DRM_RADEON_GEM_CREATE,
120 &args, sizeof(args));
121 bo->handle = args.handle;
122 if (r) {
123 fprintf(stderr, "Failed to allocate :\n");
124 fprintf(stderr, " size : %d bytes\n", size);
125 fprintf(stderr, " alignment : %d bytes\n", alignment);
126 free(bo);
127 return NULL;
128 }
129 }
130
131 if (handle)
132 util_hash_table_set(radeon->bo_handles, (void *)(uintptr_t)handle, bo);
133 done:
134 if (handle)
135 pipe_mutex_unlock(radeon->bo_handles_mutex);
136
137 return bo;
138 }
139
140 static void radeon_bo_destroy(struct radeon *radeon, struct radeon_bo *bo)
141 {
142 struct drm_gem_close args;
143
144 if (bo->name) {
145 pipe_mutex_lock(radeon->bo_handles_mutex);
146 util_hash_table_remove(radeon->bo_handles,
147 (void *)(uintptr_t)bo->name);
148 pipe_mutex_unlock(radeon->bo_handles_mutex);
149 }
150 LIST_DEL(&bo->fencedlist);
151 radeon_bo_fixed_unmap(radeon, bo);
152 memset(&args, 0, sizeof(args));
153 args.handle = bo->handle;
154 drmIoctl(radeon->info.fd, DRM_IOCTL_GEM_CLOSE, &args);
155 memset(bo, 0, sizeof(struct radeon_bo));
156 free(bo);
157 }
158
159 void radeon_bo_reference(struct radeon *radeon,
160 struct radeon_bo **dst,
161 struct radeon_bo *src)
162 {
163 struct radeon_bo *old = *dst;
164 if (pipe_reference(&(*dst)->reference, &src->reference)) {
165 radeon_bo_destroy(radeon, old);
166 }
167 *dst = src;
168 }
169
170 int radeon_bo_wait(struct radeon *radeon, struct radeon_bo *bo)
171 {
172 struct drm_radeon_gem_wait_idle args;
173 int ret;
174
175 if (!bo->shared) {
176 if (!bo->fence)
177 return 0;
178 if (bo->fence <= *radeon->cfence) {
179 LIST_DELINIT(&bo->fencedlist);
180 bo->fence = 0;
181 return 0;
182 }
183 }
184
185 /* Zero out args to make valgrind happy */
186 memset(&args, 0, sizeof(args));
187 args.handle = bo->handle;
188 do {
189 ret = drmCommandWriteRead(radeon->info.fd, DRM_RADEON_GEM_WAIT_IDLE,
190 &args, sizeof(args));
191 } while (ret == -EBUSY);
192 return ret;
193 }
194
195 int radeon_bo_busy(struct radeon *radeon, struct radeon_bo *bo, uint32_t *domain)
196 {
197 struct drm_radeon_gem_busy args;
198 int ret;
199
200 if (!bo->shared) {
201 if (!bo->fence)
202 return 0;
203 if (bo->fence <= *radeon->cfence) {
204 LIST_DELINIT(&bo->fencedlist);
205 bo->fence = 0;
206 return 0;
207 }
208 }
209
210 memset(&args, 0, sizeof(args));
211 args.handle = bo->handle;
212 args.domain = 0;
213
214 ret = drmCommandWriteRead(radeon->info.fd, DRM_RADEON_GEM_BUSY,
215 &args, sizeof(args));
216
217 *domain = args.domain;
218 return ret;
219 }
220
221 int radeon_bo_get_tiling_flags(struct radeon *radeon,
222 struct radeon_bo *bo,
223 uint32_t *tiling_flags,
224 uint32_t *pitch)
225 {
226 struct drm_radeon_gem_get_tiling args = {};
227 int ret;
228
229 args.handle = bo->handle;
230 ret = drmCommandWriteRead(radeon->info.fd, DRM_RADEON_GEM_GET_TILING,
231 &args, sizeof(args));
232 if (ret)
233 return ret;
234
235 *tiling_flags = args.tiling_flags;
236 *pitch = args.pitch;
237 return ret;
238 }
239
240 int radeon_bo_get_name(struct radeon *radeon,
241 struct radeon_bo *bo,
242 uint32_t *name)
243 {
244 struct drm_gem_flink flink;
245 int ret;
246
247 flink.handle = bo->handle;
248 ret = drmIoctl(radeon->info.fd, DRM_IOCTL_GEM_FLINK, &flink);
249 if (ret)
250 return ret;
251
252 *name = flink.name;
253 return ret;
254 }