aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/winsys/r600/drm/radeon_bo_pb.c
blob: 312552f07589717dedda92406043fe1dc14edef3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * Copyright 2010 Dave Airlie
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *      Dave Airlie
 */
#include <util/u_inlines.h>
#include <util/u_memory.h>
#include <util/u_double_list.h>
#include <pipebuffer/pb_buffer.h>
#include <pipebuffer/pb_bufmgr.h>
#include "r600_priv.h"

struct radeon_bo_pb {
	struct pb_buffer b;
	struct radeon_bo *bo;

	struct radeon_bo_pbmgr *mgr;
	struct list_head maplist;
};

extern const struct pb_vtbl radeon_bo_pb_vtbl;

static INLINE struct radeon_bo_pb *radeon_bo_pb(struct pb_buffer *buf)
{
	assert(buf);
	assert(buf->vtbl == &radeon_bo_pb_vtbl);
	return (struct radeon_bo_pb *)buf;
}

struct radeon_bo_pbmgr {
	struct pb_manager b;
	struct radeon *radeon;
	struct list_head buffer_map_list;
};

static INLINE struct radeon_bo_pbmgr *radeon_bo_pbmgr(struct pb_manager *mgr)
{
	assert(mgr);
	return (struct radeon_bo_pbmgr *)mgr;
}

static void radeon_bo_pb_destroy(struct pb_buffer *_buf)
{
	struct radeon_bo_pb *buf = radeon_bo_pb(_buf);

	/* If this buffer is on the list of buffers to unmap,
	 * do the unmapping now.
	 */
	if (!LIST_IS_EMPTY(&buf->maplist))
		radeon_bo_unmap(buf->mgr->radeon, buf->bo);

	LIST_DEL(&buf->maplist);
	radeon_bo_reference(buf->mgr->radeon, &buf->bo, NULL);
	FREE(buf);
}

static void *
radeon_bo_pb_map_internal(struct pb_buffer *_buf,
			  unsigned flags, void *ctx)
{
	struct radeon_bo_pb *buf = radeon_bo_pb(_buf);
	struct pipe_context *pctx = ctx;

	if (flags & PB_USAGE_UNSYNCHRONIZED) {
		if (radeon_bo_map(buf->mgr->radeon, buf->bo)) {
			return NULL;
		}
		LIST_DELINIT(&buf->maplist);
		return buf->bo->data;
	}

	if (p_atomic_read(&buf->bo->reference.count) > 1) {
		if (flags & PB_USAGE_DONTBLOCK) {
			return NULL;
		}
		if (ctx) {
			pctx->flush(pctx, 0, NULL);
		}
	}

	if (flags & PB_USAGE_DONTBLOCK) {
		uint32_t domain;
		if (radeon_bo_busy(buf->mgr->radeon, buf->bo, &domain))
			return NULL;
		if (radeon_bo_map(buf->mgr->radeon, buf->bo)) {
			return NULL;
		}
		goto out;
	}

	if (radeon_bo_map(buf->mgr->radeon, buf->bo)) {
		return NULL;
	}
	if (radeon_bo_wait(buf->mgr->radeon, buf->bo)) {
		radeon_bo_unmap(buf->mgr->radeon, buf->bo);
		return NULL;
	}
out:
	LIST_DELINIT(&buf->maplist);
	return buf->bo->data;
}

static void radeon_bo_pb_unmap_internal(struct pb_buffer *_buf)
{
	struct radeon_bo_pb *buf = radeon_bo_pb(_buf);
	LIST_ADDTAIL(&buf->maplist, &buf->mgr->buffer_map_list);
}

static void
radeon_bo_pb_get_base_buffer(struct pb_buffer *buf,
			     struct pb_buffer **base_buf,
			     unsigned *offset)
{
	*base_buf = buf;
	*offset = 0;
}

static enum pipe_error
radeon_bo_pb_validate(struct pb_buffer *_buf, 
		      struct pb_validate *vl,
		      unsigned flags)
{
	/* Always pinned */
	return PIPE_OK;
}

static void
radeon_bo_pb_fence(struct pb_buffer *buf,
		   struct pipe_fence_handle *fence)
{
}

const struct pb_vtbl radeon_bo_pb_vtbl = {
    radeon_bo_pb_destroy,
    radeon_bo_pb_map_internal,
    radeon_bo_pb_unmap_internal,
    radeon_bo_pb_validate,
    radeon_bo_pb_fence,
    radeon_bo_pb_get_base_buffer,
};

struct pb_buffer *
radeon_bo_pb_create_buffer_from_handle(struct pb_manager *_mgr,
				       uint32_t handle)
{
	struct radeon_bo_pbmgr *mgr = radeon_bo_pbmgr(_mgr);
	struct radeon *radeon = mgr->radeon;
	struct radeon_bo_pb *bo;
	struct radeon_bo *hw_bo;

	hw_bo = radeon_bo(radeon, handle, 0, 0);
	if (hw_bo == NULL)
		return NULL;

	bo = CALLOC_STRUCT(radeon_bo_pb);
	if (!bo) {
		radeon_bo_reference(radeon, &hw_bo, NULL);
		return NULL;
	}

	LIST_INITHEAD(&bo->maplist);
	pipe_reference_init(&bo->b.base.reference, 1);
	bo->b.base.alignment = 0;
	bo->b.base.usage = PB_USAGE_GPU_WRITE | PB_USAGE_GPU_READ;
	bo->b.base.size = hw_bo->size;
	bo->b.vtbl = &radeon_bo_pb_vtbl;
	bo->mgr = mgr;

	bo->bo = hw_bo;

	return &bo->b;
}

static struct pb_buffer *
radeon_bo_pb_create_buffer(struct pb_manager *_mgr,
			   pb_size size,
			   const struct pb_desc *desc)
{
	struct radeon_bo_pbmgr *mgr = radeon_bo_pbmgr(_mgr);
	struct radeon *radeon = mgr->radeon;
	struct radeon_bo_pb *bo;

	bo = CALLOC_STRUCT(radeon_bo_pb);
	if (!bo)
		goto error1;

	pipe_reference_init(&bo->b.base.reference, 1);
	bo->b.base.alignment = desc->alignment;
	bo->b.base.usage = desc->usage;
	bo->b.base.size = size;
	bo->b.vtbl = &radeon_bo_pb_vtbl;
	bo->mgr = mgr;

	LIST_INITHEAD(&bo->maplist);

	bo->bo = radeon_bo(radeon, 0, size, desc->alignment);
	if (bo->bo == NULL)
		goto error2;
	return &bo->b;

error2:
	FREE(bo);
error1:
	return NULL;
}

static void
radeon_bo_pbmgr_flush(struct pb_manager *mgr)
{
    /* NOP */
}

static void
radeon_bo_pbmgr_destroy(struct pb_manager *_mgr)
{
	struct radeon_bo_pbmgr *mgr = radeon_bo_pbmgr(_mgr);
	FREE(mgr);
}

struct pb_manager *radeon_bo_pbmgr_create(struct radeon *radeon)
{
	struct radeon_bo_pbmgr *mgr;

	mgr = CALLOC_STRUCT(radeon_bo_pbmgr);
	if (!mgr)
		return NULL;

	mgr->b.destroy = radeon_bo_pbmgr_destroy;
	mgr->b.create_buffer = radeon_bo_pb_create_buffer;
	mgr->b.flush = radeon_bo_pbmgr_flush;

	mgr->radeon = radeon;
	LIST_INITHEAD(&mgr->buffer_map_list);
	return &mgr->b;
}

void radeon_bo_pbmgr_flush_maps(struct pb_manager *_mgr)
{
	struct radeon_bo_pbmgr *mgr = radeon_bo_pbmgr(_mgr);
	struct radeon_bo_pb *rpb = NULL;
	struct radeon_bo_pb *t_rpb;

	LIST_FOR_EACH_ENTRY_SAFE(rpb, t_rpb, &mgr->buffer_map_list, maplist) {
		radeon_bo_unmap(mgr->radeon, rpb->bo);
		LIST_DELINIT(&rpb->maplist);
	}

	LIST_INITHEAD(&mgr->buffer_map_list);
}

struct radeon_bo *radeon_bo_pb_get_bo(struct pb_buffer *_buf)
{
	struct radeon_bo_pb *buf;
	if (_buf->vtbl == &radeon_bo_pb_vtbl) {
		buf = radeon_bo_pb(_buf);
		return buf->bo;
	} else {
		struct pb_buffer *base_buf;
		pb_size offset;
		pb_get_base_buffer(_buf, &base_buf, &offset);
		if (base_buf->vtbl == &radeon_bo_pb_vtbl) {
			buf = radeon_bo_pb(base_buf);
			return buf->bo;
		}
	}
	return NULL;
}