• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 Advanced Micro Devices, Inc.
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  *      Marek Olšák
25  */
26 
27 #include "r600_cs.h"
28 #include "evergreen_compute.h"
29 #include "compute_memory_pool.h"
30 #include "util/macros.h"
31 #include "util/u_memory.h"
32 #include "util/u_upload_mgr.h"
33 #include <inttypes.h>
34 #include <stdio.h>
35 
r600_rings_is_buffer_referenced(struct r600_common_context * ctx,struct pb_buffer_lean * buf,unsigned usage)36 bool r600_rings_is_buffer_referenced(struct r600_common_context *ctx,
37 				     struct pb_buffer_lean *buf,
38 				     unsigned usage)
39 {
40 	if (ctx->ws->cs_is_buffer_referenced(&ctx->gfx.cs, buf, usage)) {
41 		return true;
42 	}
43 	if (radeon_emitted(&ctx->dma.cs, 0) &&
44 	    ctx->ws->cs_is_buffer_referenced(&ctx->dma.cs, buf, usage)) {
45 		return true;
46 	}
47 	return false;
48 }
49 
r600_buffer_map_sync_with_rings(struct r600_common_context * ctx,struct r600_resource * resource,unsigned usage)50 void *r600_buffer_map_sync_with_rings(struct r600_common_context *ctx,
51                                       struct r600_resource *resource,
52                                       unsigned usage)
53 {
54 	unsigned rusage = RADEON_USAGE_READWRITE;
55 	bool busy = false;
56 
57 	assert(!(resource->flags & RADEON_FLAG_SPARSE));
58 
59 	if (usage & PIPE_MAP_UNSYNCHRONIZED) {
60 		return ctx->ws->buffer_map(ctx->ws, resource->buf, NULL, usage);
61 	}
62 
63 	if (!(usage & PIPE_MAP_WRITE)) {
64 		/* have to wait for the last write */
65 		rusage = RADEON_USAGE_WRITE;
66 	}
67 
68 	if (radeon_emitted(&ctx->gfx.cs, ctx->initial_gfx_cs_size) &&
69 	    ctx->ws->cs_is_buffer_referenced(&ctx->gfx.cs,
70 					     resource->buf, rusage)) {
71 		if (usage & PIPE_MAP_DONTBLOCK) {
72 			ctx->gfx.flush(ctx, PIPE_FLUSH_ASYNC, NULL);
73 			return NULL;
74 		} else {
75 			ctx->gfx.flush(ctx, 0, NULL);
76 			busy = true;
77 		}
78 	}
79 	if (radeon_emitted(&ctx->dma.cs, 0) &&
80 	    ctx->ws->cs_is_buffer_referenced(&ctx->dma.cs,
81 					     resource->buf, rusage)) {
82 		if (usage & PIPE_MAP_DONTBLOCK) {
83 			ctx->dma.flush(ctx, PIPE_FLUSH_ASYNC, NULL);
84 			return NULL;
85 		} else {
86 			ctx->dma.flush(ctx, 0, NULL);
87 			busy = true;
88 		}
89 	}
90 
91 	if (busy || !ctx->ws->buffer_wait(ctx->ws, resource->buf, 0, rusage)) {
92 		if (usage & PIPE_MAP_DONTBLOCK) {
93 			return NULL;
94 		} else {
95 			/* We will be wait for the GPU. Wait for any offloaded
96 			 * CS flush to complete to avoid busy-waiting in the winsys. */
97 			ctx->ws->cs_sync_flush(&ctx->gfx.cs);
98 			if (ctx->dma.cs.priv)
99 				ctx->ws->cs_sync_flush(&ctx->dma.cs);
100 		}
101 	}
102 
103 	/* Setting the CS to NULL will prevent doing checks we have done already. */
104 	return ctx->ws->buffer_map(ctx->ws, resource->buf, NULL, usage);
105 }
106 
r600_init_resource_fields(struct r600_common_screen * rscreen,struct r600_resource * res,uint64_t size,unsigned alignment)107 void r600_init_resource_fields(struct r600_common_screen *rscreen,
108 			       struct r600_resource *res,
109 			       uint64_t size, unsigned alignment)
110 {
111 	struct r600_texture *rtex = container_of(res, struct r600_texture, resource);
112 
113 	res->bo_size = size;
114 	res->bo_alignment = alignment;
115 	res->flags = 0;
116 	res->texture_handle_allocated = false;
117 	res->image_handle_allocated = false;
118 
119 	switch (res->b.b.usage) {
120 	case PIPE_USAGE_STREAM:
121 		res->flags = RADEON_FLAG_GTT_WC;
122 		FALLTHROUGH;
123 	case PIPE_USAGE_STAGING:
124 		/* Transfers are likely to occur more often with these
125 		 * resources. */
126 		res->domains = RADEON_DOMAIN_GTT;
127 		break;
128 	case PIPE_USAGE_DYNAMIC:
129 	case PIPE_USAGE_DEFAULT:
130 	case PIPE_USAGE_IMMUTABLE:
131 	default:
132 		/* Not listing GTT here improves performance in some
133 		 * apps. */
134 		res->domains = RADEON_DOMAIN_VRAM;
135 		res->flags |= RADEON_FLAG_GTT_WC;
136 		break;
137 	}
138 
139 	/* Tiled textures are unmappable. Always put them in VRAM. */
140 	if ((res->b.b.target != PIPE_BUFFER && !rtex->surface.is_linear) ||
141 	    res->flags & R600_RESOURCE_FLAG_UNMAPPABLE) {
142 		res->domains = RADEON_DOMAIN_VRAM;
143 		res->flags |= RADEON_FLAG_NO_CPU_ACCESS |
144 			 RADEON_FLAG_GTT_WC;
145 	}
146 
147 	/* Displayable and shareable surfaces are not suballocated. */
148 	if (res->b.b.bind & (PIPE_BIND_SHARED | PIPE_BIND_SCANOUT))
149 		res->flags |= RADEON_FLAG_NO_SUBALLOC; /* shareable */
150 	else
151 		res->flags |= RADEON_FLAG_NO_INTERPROCESS_SHARING;
152 
153 	if (rscreen->debug_flags & DBG_NO_WC)
154 		res->flags &= ~RADEON_FLAG_GTT_WC;
155 
156 	/* Set expected VRAM and GART usage for the buffer. */
157 	res->vram_usage = 0;
158 	res->gart_usage = 0;
159 
160 	if (res->domains & RADEON_DOMAIN_VRAM)
161 		res->vram_usage = size;
162 	else if (res->domains & RADEON_DOMAIN_GTT)
163 		res->gart_usage = size;
164 }
165 
r600_alloc_resource(struct r600_common_screen * rscreen,struct r600_resource * res)166 bool r600_alloc_resource(struct r600_common_screen *rscreen,
167 			 struct r600_resource *res)
168 {
169 	struct pb_buffer_lean *old_buf, *new_buf;
170 
171 	/* Allocate a new resource. */
172 	new_buf = rscreen->ws->buffer_create(rscreen->ws, res->bo_size,
173 					     res->bo_alignment,
174 					     res->domains, res->flags);
175 	if (!new_buf) {
176 		return false;
177 	}
178 
179 	/* Replace the pointer such that if res->buf wasn't NULL, it won't be
180 	 * NULL. This should prevent crashes with multiple contexts using
181 	 * the same buffer where one of the contexts invalidates it while
182 	 * the others are using it. */
183 	old_buf = res->buf;
184 	res->buf = new_buf; /* should be atomic */
185 
186 	if (rscreen->info.r600_has_virtual_memory)
187 		res->gpu_address = rscreen->ws->buffer_get_virtual_address(res->buf);
188 	else
189 		res->gpu_address = 0;
190 
191 	radeon_bo_reference(rscreen->ws, &old_buf, NULL);
192 
193 	util_range_set_empty(&res->valid_buffer_range);
194 
195 	/* Print debug information. */
196 	if (rscreen->debug_flags & DBG_VM && res->b.b.target == PIPE_BUFFER) {
197 		fprintf(stderr, "VM start=0x%"PRIX64"  end=0x%"PRIX64" | Buffer %"PRIu64" bytes\n",
198 			res->gpu_address, res->gpu_address + res->buf->size,
199 			res->buf->size);
200 	}
201 	return true;
202 }
203 
r600_buffer_destroy(struct pipe_screen * screen,struct pipe_resource * buf)204 void r600_buffer_destroy(struct pipe_screen *screen, struct pipe_resource *buf)
205 {
206 	struct r600_screen *rscreen = (struct r600_screen*)screen;
207 	struct r600_resource *rbuffer = r600_resource(buf);
208 
209 	threaded_resource_deinit(buf);
210 	util_range_destroy(&rbuffer->valid_buffer_range);
211 	pipe_resource_reference((struct pipe_resource**)&rbuffer->immed_buffer, NULL);
212 	radeon_bo_reference(rscreen->b.ws, &rbuffer->buf, NULL);
213 	FREE(rbuffer);
214 }
215 
216 static bool
r600_invalidate_buffer(struct r600_common_context * rctx,struct r600_resource * rbuffer)217 r600_invalidate_buffer(struct r600_common_context *rctx,
218 		       struct r600_resource *rbuffer)
219 {
220 	/* Shared buffers can't be reallocated. */
221 	if (rbuffer->b.is_shared)
222 		return false;
223 
224 	/* Sparse buffers can't be reallocated. */
225 	if (rbuffer->flags & RADEON_FLAG_SPARSE)
226 		return false;
227 
228 	/* In AMD_pinned_memory, the user pointer association only gets
229 	 * broken when the buffer is explicitly re-allocated.
230 	 */
231 	if (rbuffer->b.is_user_ptr)
232 		return false;
233 
234 	/* Check if mapping this buffer would cause waiting for the GPU. */
235 	if (r600_rings_is_buffer_referenced(rctx, rbuffer->buf, RADEON_USAGE_READWRITE) ||
236 	    !rctx->ws->buffer_wait(rctx->ws, rbuffer->buf, 0, RADEON_USAGE_READWRITE)) {
237 		rctx->invalidate_buffer(&rctx->b, &rbuffer->b.b);
238 	} else {
239 		util_range_set_empty(&rbuffer->valid_buffer_range);
240 	}
241 
242 	return true;
243 }
244 
245 /* Replace the storage of dst with src. */
r600_replace_buffer_storage(struct pipe_context * ctx,struct pipe_resource * dst,struct pipe_resource * src)246 void r600_replace_buffer_storage(struct pipe_context *ctx,
247 				 struct pipe_resource *dst,
248 				 struct pipe_resource *src)
249 {
250 	struct r600_common_context *rctx = (struct r600_common_context *)ctx;
251 	struct r600_resource *rdst = r600_resource(dst);
252 	struct r600_resource *rsrc = r600_resource(src);
253 	uint64_t old_gpu_address = rdst->gpu_address;
254 
255 	radeon_bo_reference(rctx->ws, &rdst->buf, rsrc->buf);
256 	rdst->gpu_address = rsrc->gpu_address;
257 	rdst->b.b.bind = rsrc->b.b.bind;
258 	rdst->flags = rsrc->flags;
259 
260 	assert(rdst->vram_usage == rsrc->vram_usage);
261 	assert(rdst->gart_usage == rsrc->gart_usage);
262 	assert(rdst->bo_size == rsrc->bo_size);
263 	assert(rdst->bo_alignment == rsrc->bo_alignment);
264 	assert(rdst->domains == rsrc->domains);
265 
266 	rctx->rebind_buffer(ctx, dst, old_gpu_address);
267 }
268 
r600_invalidate_resource(struct pipe_context * ctx,struct pipe_resource * resource)269 void r600_invalidate_resource(struct pipe_context *ctx,
270 			      struct pipe_resource *resource)
271 {
272 	struct r600_common_context *rctx = (struct r600_common_context*)ctx;
273 	struct r600_resource *rbuffer = r600_resource(resource);
274 
275 	/* We currently only do anything here for buffers */
276 	if (resource->target == PIPE_BUFFER)
277 		(void)r600_invalidate_buffer(rctx, rbuffer);
278 }
279 
r600_buffer_get_transfer(struct pipe_context * ctx,struct pipe_resource * resource,unsigned usage,const struct pipe_box * box,struct pipe_transfer ** ptransfer,void * data,struct r600_resource * staging,unsigned offset)280 static void *r600_buffer_get_transfer(struct pipe_context *ctx,
281 				      struct pipe_resource *resource,
282                                       unsigned usage,
283                                       const struct pipe_box *box,
284 				      struct pipe_transfer **ptransfer,
285 				      void *data, struct r600_resource *staging,
286 				      unsigned offset)
287 {
288 	struct r600_common_context *rctx = (struct r600_common_context*)ctx;
289 	struct r600_transfer *transfer;
290 
291 	if (usage & TC_TRANSFER_MAP_THREADED_UNSYNC)
292 		transfer = slab_zalloc(&rctx->pool_transfers_unsync);
293 	else
294 		transfer = slab_zalloc(&rctx->pool_transfers);
295 
296 	pipe_resource_reference(&transfer->b.b.resource, resource);
297 	transfer->b.b.usage = usage;
298 	transfer->b.b.box = *box;
299 	transfer->b.b.offset = offset;
300 	transfer->staging = staging;
301 	*ptransfer = &transfer->b.b;
302 	return data;
303 }
304 
r600_can_dma_copy_buffer(struct r600_common_context * rctx,unsigned dstx,unsigned srcx,unsigned size)305 static bool r600_can_dma_copy_buffer(struct r600_common_context *rctx,
306 				     unsigned dstx, unsigned srcx, unsigned size)
307 {
308 	bool dword_aligned = !(dstx % 4) && !(srcx % 4) && !(size % 4);
309 
310 	return rctx->screen->has_cp_dma ||
311 	       (dword_aligned && (rctx->dma.cs.priv ||
312 				  rctx->screen->has_streamout));
313 
314 }
315 
r600_buffer_transfer_map(struct pipe_context * ctx,struct pipe_resource * resource,unsigned level,unsigned usage,const struct pipe_box * box,struct pipe_transfer ** ptransfer)316 void *r600_buffer_transfer_map(struct pipe_context *ctx,
317                                struct pipe_resource *resource,
318                                unsigned level,
319                                unsigned usage,
320                                const struct pipe_box *box,
321                                struct pipe_transfer **ptransfer)
322 {
323 	struct r600_common_context *rctx = (struct r600_common_context*)ctx;
324 	struct r600_common_screen *rscreen = (struct r600_common_screen*)ctx->screen;
325 	struct r600_resource *rbuffer = r600_resource(resource);
326 	uint8_t *data;
327 
328 	if (r600_resource(resource)->compute_global_bo) {
329 		if ((data = r600_compute_global_transfer_map(ctx, resource, level, usage, box, ptransfer)))
330 			return data;
331 	}
332 
333 	assert(box->x + box->width <= resource->width0);
334 
335 	/* From GL_AMD_pinned_memory issues:
336 	 *
337 	 *     4) Is glMapBuffer on a shared buffer guaranteed to return the
338 	 *        same system address which was specified at creation time?
339 	 *
340 	 *        RESOLVED: NO. The GL implementation might return a different
341 	 *        virtual mapping of that memory, although the same physical
342 	 *        page will be used.
343 	 *
344 	 * So don't ever use staging buffers.
345 	 */
346 	if (rbuffer->b.is_user_ptr)
347 		usage |= PIPE_MAP_PERSISTENT;
348 
349 	/* See if the buffer range being mapped has never been initialized,
350 	 * in which case it can be mapped unsynchronized. */
351 	if (!(usage & (PIPE_MAP_UNSYNCHRONIZED |
352 		       TC_TRANSFER_MAP_NO_INFER_UNSYNCHRONIZED)) &&
353 	    usage & PIPE_MAP_WRITE &&
354 	    !rbuffer->b.is_shared &&
355 	    !util_ranges_intersect(&rbuffer->valid_buffer_range, box->x, box->x + box->width)) {
356 		usage |= PIPE_MAP_UNSYNCHRONIZED;
357 	}
358 
359 	/* If discarding the entire range, discard the whole resource instead. */
360 	if (usage & PIPE_MAP_DISCARD_RANGE &&
361 	    box->x == 0 && box->width == resource->width0) {
362 		usage |= PIPE_MAP_DISCARD_WHOLE_RESOURCE;
363 	}
364 
365 	if (usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE &&
366 	    !(usage & (PIPE_MAP_UNSYNCHRONIZED |
367 		       TC_TRANSFER_MAP_NO_INVALIDATE))) {
368 		assert(usage & PIPE_MAP_WRITE);
369 
370 		if (r600_invalidate_buffer(rctx, rbuffer)) {
371 			/* At this point, the buffer is always idle. */
372 			usage |= PIPE_MAP_UNSYNCHRONIZED;
373 		} else {
374 			/* Fall back to a temporary buffer. */
375 			usage |= PIPE_MAP_DISCARD_RANGE;
376 		}
377 	}
378 
379 	if ((usage & PIPE_MAP_DISCARD_RANGE) &&
380 	    !(rscreen->debug_flags & DBG_NO_DISCARD_RANGE) &&
381 	    ((!(usage & (PIPE_MAP_UNSYNCHRONIZED |
382 			 PIPE_MAP_PERSISTENT)) &&
383 	      r600_can_dma_copy_buffer(rctx, box->x, 0, box->width)) ||
384 	     (rbuffer->flags & RADEON_FLAG_SPARSE))) {
385 		assert(usage & PIPE_MAP_WRITE);
386 
387 		/* Check if mapping this buffer would cause waiting for the GPU.
388 		 */
389 		if (rbuffer->flags & RADEON_FLAG_SPARSE ||
390 		    r600_rings_is_buffer_referenced(rctx, rbuffer->buf, RADEON_USAGE_READWRITE) ||
391 		    !rctx->ws->buffer_wait(rctx->ws, rbuffer->buf, 0, RADEON_USAGE_READWRITE)) {
392 			/* Do a wait-free write-only transfer using a temporary buffer. */
393 			unsigned offset;
394 			struct r600_resource *staging = NULL;
395 
396 			u_upload_alloc(ctx->stream_uploader, 0,
397                                        box->width + (box->x % R600_MAP_BUFFER_ALIGNMENT),
398 				       rctx->screen->info.tcc_cache_line_size,
399 				       &offset, (struct pipe_resource**)&staging,
400                                        (void**)&data);
401 
402 			if (staging) {
403 				data += box->x % R600_MAP_BUFFER_ALIGNMENT;
404 				return r600_buffer_get_transfer(ctx, resource, usage, box,
405 								ptransfer, data, staging, offset);
406 			} else if (rbuffer->flags & RADEON_FLAG_SPARSE) {
407 				return NULL;
408 			}
409 		} else {
410 			/* At this point, the buffer is always idle (we checked it above). */
411 			usage |= PIPE_MAP_UNSYNCHRONIZED;
412 		}
413 	}
414 	/* Use a staging buffer in cached GTT for reads. */
415 	else if (((usage & PIPE_MAP_READ) &&
416 		  !(usage & PIPE_MAP_PERSISTENT) &&
417 		  (rbuffer->domains & RADEON_DOMAIN_VRAM ||
418 		   rbuffer->flags & RADEON_FLAG_GTT_WC) &&
419 		  r600_can_dma_copy_buffer(rctx, 0, box->x, box->width)) ||
420 		 (rbuffer->flags & RADEON_FLAG_SPARSE)) {
421 		struct r600_resource *staging;
422 
423 		assert(!(usage & TC_TRANSFER_MAP_THREADED_UNSYNC));
424 		staging = (struct r600_resource*) pipe_buffer_create(
425 				ctx->screen, 0, PIPE_USAGE_STAGING,
426 				box->width + (box->x % R600_MAP_BUFFER_ALIGNMENT));
427 		if (staging) {
428 			/* Copy the VRAM buffer to the staging buffer. */
429 			rctx->dma_copy(ctx, &staging->b.b, 0,
430 				       box->x % R600_MAP_BUFFER_ALIGNMENT,
431 				       0, 0, resource, 0, box);
432 
433 			data = r600_buffer_map_sync_with_rings(rctx, staging,
434 							       usage & ~PIPE_MAP_UNSYNCHRONIZED);
435 			if (!data) {
436 				r600_resource_reference(&staging, NULL);
437 				return NULL;
438 			}
439 			data += box->x % R600_MAP_BUFFER_ALIGNMENT;
440 
441 			return r600_buffer_get_transfer(ctx, resource, usage, box,
442 							ptransfer, data, staging, 0);
443 		} else if (rbuffer->flags & RADEON_FLAG_SPARSE) {
444 			return NULL;
445 		}
446 	}
447 
448 	data = r600_buffer_map_sync_with_rings(rctx, rbuffer, usage);
449 	if (!data) {
450 		return NULL;
451 	}
452 	data += box->x;
453 
454 	return r600_buffer_get_transfer(ctx, resource, usage, box,
455 					ptransfer, data, NULL, 0);
456 }
457 
r600_buffer_do_flush_region(struct pipe_context * ctx,struct pipe_transfer * transfer,const struct pipe_box * box)458 static void r600_buffer_do_flush_region(struct pipe_context *ctx,
459 					struct pipe_transfer *transfer,
460 				        const struct pipe_box *box)
461 {
462 	struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
463 	struct r600_resource *rbuffer = r600_resource(transfer->resource);
464 
465 	if (rtransfer->staging) {
466 		struct pipe_resource *dst, *src;
467 		unsigned soffset;
468 		struct pipe_box dma_box;
469 
470 		dst = transfer->resource;
471 		src = &rtransfer->staging->b.b;
472 		soffset = rtransfer->b.b.offset + box->x % R600_MAP_BUFFER_ALIGNMENT;
473 
474 		u_box_1d(soffset, box->width, &dma_box);
475 
476 		/* Copy the staging buffer into the original one. */
477 		ctx->resource_copy_region(ctx, dst, 0, box->x, 0, 0, src, 0, &dma_box);
478 	}
479 
480 	util_range_add(&rbuffer->b.b, &rbuffer->valid_buffer_range, box->x,
481 		       box->x + box->width);
482 }
483 
r600_buffer_flush_region(struct pipe_context * ctx,struct pipe_transfer * transfer,const struct pipe_box * rel_box)484 void r600_buffer_flush_region(struct pipe_context *ctx,
485 			      struct pipe_transfer *transfer,
486 			      const struct pipe_box *rel_box)
487 {
488 	unsigned required_usage = PIPE_MAP_WRITE |
489 				  PIPE_MAP_FLUSH_EXPLICIT;
490 
491 	if (r600_resource(transfer->resource)->compute_global_bo)
492 		return;
493 
494 	if ((transfer->usage & required_usage) == required_usage) {
495 		struct pipe_box box;
496 
497 		u_box_1d(transfer->box.x + rel_box->x, rel_box->width, &box);
498 		r600_buffer_do_flush_region(ctx, transfer, &box);
499 	}
500 }
501 
r600_buffer_transfer_unmap(struct pipe_context * ctx,struct pipe_transfer * transfer)502 void r600_buffer_transfer_unmap(struct pipe_context *ctx,
503 				struct pipe_transfer *transfer)
504 {
505 	struct r600_common_context *rctx = (struct r600_common_context*)ctx;
506 	struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
507 	struct r600_resource *rtransferr = r600_resource(transfer->resource);
508 
509 	if (rtransferr->compute_global_bo && !rtransferr->b.is_user_ptr) {
510 		r600_compute_global_transfer_unmap(ctx, transfer);
511 		return;
512 	}
513 
514 	if (transfer->usage & PIPE_MAP_WRITE &&
515 	    !(transfer->usage & PIPE_MAP_FLUSH_EXPLICIT))
516 		r600_buffer_do_flush_region(ctx, transfer, &transfer->box);
517 
518 	r600_resource_reference(&rtransfer->staging, NULL);
519 	assert(rtransfer->b.staging == NULL); /* for threaded context only */
520 	pipe_resource_reference(&transfer->resource, NULL);
521 
522 	/* Don't use pool_transfers_unsync. We are always in the driver
523 	 * thread. */
524 	slab_free(&rctx->pool_transfers, transfer);
525 }
526 
r600_buffer_subdata(struct pipe_context * ctx,struct pipe_resource * buffer,unsigned usage,unsigned offset,unsigned size,const void * data)527 void r600_buffer_subdata(struct pipe_context *ctx,
528 			 struct pipe_resource *buffer,
529 			 unsigned usage, unsigned offset,
530 			 unsigned size, const void *data)
531 {
532 	struct pipe_transfer *transfer = NULL;
533 	struct pipe_box box;
534 	uint8_t *map = NULL;
535 
536 	usage |= PIPE_MAP_WRITE;
537 
538 	if (!(usage & PIPE_MAP_DIRECTLY))
539 		usage |= PIPE_MAP_DISCARD_RANGE;
540 
541 	u_box_1d(offset, size, &box);
542 	map = r600_buffer_transfer_map(ctx, buffer, 0, usage, &box, &transfer);
543 	if (!map)
544 		return;
545 
546 	memcpy(map, data, size);
547 	r600_buffer_transfer_unmap(ctx, transfer);
548 }
549 
550 static struct r600_resource *
r600_alloc_buffer_struct(struct pipe_screen * screen,const struct pipe_resource * templ)551 r600_alloc_buffer_struct(struct pipe_screen *screen,
552 			 const struct pipe_resource *templ)
553 {
554 	struct r600_resource *rbuffer;
555 
556 	rbuffer = MALLOC_STRUCT(r600_resource);
557 
558 	rbuffer->b.b = *templ;
559 	rbuffer->b.b.next = NULL;
560 	pipe_reference_init(&rbuffer->b.b.reference, 1);
561 	rbuffer->b.b.screen = screen;
562 
563 	threaded_resource_init(&rbuffer->b.b, false);
564 
565 	rbuffer->buf = NULL;
566 	rbuffer->bind_history = 0;
567 	rbuffer->immed_buffer = NULL;
568 	rbuffer->compute_global_bo = false;
569 	util_range_init(&rbuffer->valid_buffer_range);
570 	return rbuffer;
571 }
572 
r600_buffer_create(struct pipe_screen * screen,const struct pipe_resource * templ,unsigned alignment)573 struct pipe_resource *r600_buffer_create(struct pipe_screen *screen,
574 					 const struct pipe_resource *templ,
575 					 unsigned alignment)
576 {
577 	struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
578 	struct r600_resource *rbuffer = r600_alloc_buffer_struct(screen, templ);
579 
580 	r600_init_resource_fields(rscreen, rbuffer, templ->width0, alignment);
581 
582 	if (templ->flags & PIPE_RESOURCE_FLAG_SPARSE)
583 		rbuffer->flags |= RADEON_FLAG_SPARSE;
584 
585 	if (!r600_alloc_resource(rscreen, rbuffer)) {
586 		FREE(rbuffer);
587 		return NULL;
588 	}
589 	return &rbuffer->b.b;
590 }
591 
r600_aligned_buffer_create(struct pipe_screen * screen,unsigned flags,unsigned usage,unsigned size,unsigned alignment)592 struct pipe_resource *r600_aligned_buffer_create(struct pipe_screen *screen,
593 						 unsigned flags,
594 						 unsigned usage,
595 						 unsigned size,
596 						 unsigned alignment)
597 {
598 	struct pipe_resource buffer;
599 
600 	memset(&buffer, 0, sizeof buffer);
601 	buffer.target = PIPE_BUFFER;
602 	buffer.format = PIPE_FORMAT_R8_UNORM;
603 	buffer.bind = 0;
604 	buffer.usage = usage;
605 	buffer.flags = flags;
606 	buffer.width0 = size;
607 	buffer.height0 = 1;
608 	buffer.depth0 = 1;
609 	buffer.array_size = 1;
610 	return r600_buffer_create(screen, &buffer, alignment);
611 }
612 
613 struct pipe_resource *
r600_buffer_from_user_memory(struct pipe_screen * screen,const struct pipe_resource * templ,void * user_memory)614 r600_buffer_from_user_memory(struct pipe_screen *screen,
615 			     const struct pipe_resource *templ,
616 			     void *user_memory)
617 {
618 	struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
619 	struct radeon_winsys *ws = rscreen->ws;
620 	struct r600_resource *rbuffer;
621 
622 	if ((templ->bind & PIPE_BIND_GLOBAL) &&
623 	    (templ->bind & PIPE_BIND_COMPUTE_RESOURCE)) {
624 		rbuffer = r600_resource(r600_compute_global_buffer_create(screen, templ));
625 		((struct r600_resource_global *)rbuffer)->chunk->real_buffer = rbuffer;
626 	} else {
627 		rbuffer = r600_alloc_buffer_struct(screen, templ);
628 	}
629 
630 	rbuffer->domains = RADEON_DOMAIN_GTT;
631 	rbuffer->flags = 0;
632 	rbuffer->b.is_user_ptr = true;
633 	util_range_add(&rbuffer->b.b, &rbuffer->valid_buffer_range, 0, templ->width0);
634 	util_range_add(&rbuffer->b.b, &rbuffer->b.valid_buffer_range, 0, templ->width0);
635 
636 	/* Convert a user pointer to a buffer. */
637 	rbuffer->buf = ws->buffer_from_ptr(ws, user_memory, templ->width0,
638 	                                   templ->usage == PIPE_USAGE_IMMUTABLE? RADEON_FLAG_READ_ONLY : 0);
639 	if (!rbuffer->buf) {
640 		FREE(rbuffer);
641 		return NULL;
642 	}
643 
644 	if (rscreen->info.r600_has_virtual_memory)
645 		rbuffer->gpu_address =
646 			ws->buffer_get_virtual_address(rbuffer->buf);
647 	else
648 		rbuffer->gpu_address = 0;
649 
650 	rbuffer->vram_usage = 0;
651 	rbuffer->gart_usage = templ->width0;
652 
653 	return &rbuffer->b.b;
654 }
655