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