r600g: allocate/destroy buffers using radeon_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 "util/u_memory.h"
30 #include "radeon_drm.h"
31 #include "xf86drm.h"
32 #include <sys/mman.h>
33 #include <errno.h>
34
35 int radeon_bo_fixed_map(struct radeon *radeon, struct radeon_bo *bo)
36 {
37 struct drm_radeon_gem_mmap args;
38 void *ptr;
39 int r;
40
41 /* Zero out args to make valgrind happy */
42 memset(&args, 0, sizeof(args));
43 args.handle = bo->handle;
44 args.offset = 0;
45 args.size = (uint64_t)bo->size;
46 r = drmCommandWriteRead(radeon->info.fd, DRM_RADEON_GEM_MMAP,
47 &args, sizeof(args));
48 if (r) {
49 fprintf(stderr, "error mapping %p 0x%08X (error = %d)\n",
50 bo, bo->handle, r);
51 return r;
52 }
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__);
56 return -errno;
57 }
58 bo->data = ptr;
59
60 bo->map_count++;
61 return 0;
62 }
63
64 static void radeon_bo_fixed_unmap(struct radeon *radeon, struct radeon_bo *bo)
65 {
66 if (bo->data) {
67 munmap(bo->data, bo->size);
68 bo->data = NULL;
69 }
70 }
71
72 #include "state_tracker/drm_driver.h"
73
74 struct radeon_bo *radeon_bo(struct radeon *radeon, unsigned handle,
75 unsigned size, unsigned alignment, unsigned bind,
76 unsigned initial_domain)
77 {
78 struct radeon_bo *bo;
79 struct winsys_handle whandle = {};
80 whandle.handle = handle;
81
82 bo = calloc(1, sizeof(*bo));
83 if (bo == NULL) {
84 return NULL;
85 }
86 bo->size = size;
87 bo->handle = handle;
88 pipe_reference_init(&bo->reference, 1);
89 LIST_INITHEAD(&bo->fencedlist);
90
91 if (handle) {
92 unsigned size;
93 bo->buf = radeon->ws->buffer_from_handle(radeon->ws, &whandle, NULL, &size);
94 if (!bo->buf) {
95 FREE(bo);
96 return NULL;
97 }
98 bo->handle = radeon->ws->trans_get_buffer_handle(bo->buf);
99 bo->size = size;
100 bo->shared = TRUE;
101 } else {
102 bo->buf = radeon->ws->buffer_create(radeon->ws, size, alignment, bind, initial_domain);
103 if (!bo->buf) {
104 FREE(bo);
105 return NULL;
106 }
107 bo->handle = radeon->ws->trans_get_buffer_handle(bo->buf);
108 }
109 return bo;
110 }
111
112 static void radeon_bo_destroy(struct radeon *radeon, struct radeon_bo *bo)
113 {
114 LIST_DEL(&bo->fencedlist);
115 radeon_bo_fixed_unmap(radeon, bo);
116 pb_reference(&bo->buf, NULL);
117 FREE(bo);
118 }
119
120 void radeon_bo_reference(struct radeon *radeon,
121 struct radeon_bo **dst,
122 struct radeon_bo *src)
123 {
124 struct radeon_bo *old = *dst;
125 if (pipe_reference(&(*dst)->reference, &src->reference)) {
126 radeon_bo_destroy(radeon, old);
127 }
128 *dst = src;
129 }
130
131 int radeon_bo_wait(struct radeon *radeon, struct radeon_bo *bo)
132 {
133 struct drm_radeon_gem_wait_idle args;
134 int ret;
135
136 if (!bo->shared) {
137 if (!bo->fence)
138 return 0;
139 if (bo->fence <= *radeon->cfence) {
140 LIST_DELINIT(&bo->fencedlist);
141 bo->fence = 0;
142 return 0;
143 }
144 }
145
146 /* Zero out args to make valgrind happy */
147 memset(&args, 0, sizeof(args));
148 args.handle = bo->handle;
149 do {
150 ret = drmCommandWriteRead(radeon->info.fd, DRM_RADEON_GEM_WAIT_IDLE,
151 &args, sizeof(args));
152 } while (ret == -EBUSY);
153 return ret;
154 }
155
156 int radeon_bo_busy(struct radeon *radeon, struct radeon_bo *bo, uint32_t *domain)
157 {
158 struct drm_radeon_gem_busy args;
159 int ret;
160
161 if (!bo->shared) {
162 if (!bo->fence)
163 return 0;
164 if (bo->fence <= *radeon->cfence) {
165 LIST_DELINIT(&bo->fencedlist);
166 bo->fence = 0;
167 return 0;
168 }
169 }
170
171 memset(&args, 0, sizeof(args));
172 args.handle = bo->handle;
173 args.domain = 0;
174
175 ret = drmCommandWriteRead(radeon->info.fd, DRM_RADEON_GEM_BUSY,
176 &args, sizeof(args));
177
178 *domain = args.domain;
179 return ret;
180 }
181
182 int radeon_bo_get_tiling_flags(struct radeon *radeon,
183 struct radeon_bo *bo,
184 uint32_t *tiling_flags)
185 {
186 struct drm_radeon_gem_get_tiling args = {};
187 int ret;
188
189 args.handle = bo->handle;
190 ret = drmCommandWriteRead(radeon->info.fd, DRM_RADEON_GEM_GET_TILING,
191 &args, sizeof(args));
192 if (ret)
193 return ret;
194
195 *tiling_flags = args.tiling_flags;
196 return ret;
197 }
198
199 int radeon_bo_get_name(struct radeon *radeon,
200 struct radeon_bo *bo,
201 uint32_t *name)
202 {
203 struct drm_gem_flink flink;
204 int ret;
205
206 flink.handle = bo->handle;
207 ret = drmIoctl(radeon->info.fd, DRM_IOCTL_GEM_FLINK, &flink);
208 if (ret)
209 return ret;
210
211 *name = flink.name;
212 return ret;
213 }