2 * Permission is hereby granted, free of charge, to any person obtaining a
3 * copy of this software and associated documentation files (the "Software"),
4 * to deal in the Software without restriction, including without limitation
5 * on the rights to use, copy, modify, merge, publish, distribute, sub
6 * license, and/or sell copies of the Software, and to permit persons to whom
7 * the Software is furnished to do so, subject to the following conditions:
9 * The above copyright notice and this permission notice (including the next
10 * paragraph) shall be included in all copies or substantial portions of the
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * Adam Rak <adam.rak@streamnovation.com>
25 #include "pipe/p_defines.h"
26 #include "pipe/p_state.h"
27 #include "pipe/p_context.h"
28 #include "util/u_blitter.h"
29 #include "util/u_double_list.h"
30 #include "util/u_transfer.h"
31 #include "util/u_surface.h"
32 #include "util/u_pack_color.h"
33 #include "util/u_math.h"
34 #include "util/u_memory.h"
35 #include "util/u_inlines.h"
36 #include "util/u_framebuffer.h"
37 #include "r600_shader.h"
38 #include "r600_pipe.h"
39 #include "r600_formats.h"
40 #include "compute_memory_pool.h"
41 #include "evergreen_compute.h"
42 #include "evergreen_compute_internal.h"
45 #define ITEM_ALIGNMENT 1024
49 struct compute_memory_pool
* compute_memory_pool_new(
50 struct r600_screen
* rscreen
)
52 struct compute_memory_pool
* pool
= (struct compute_memory_pool
*)
53 CALLOC(sizeof(struct compute_memory_pool
), 1);
57 COMPUTE_DBG(rscreen
, "* compute_memory_pool_new()\n");
59 pool
->screen
= rscreen
;
60 pool
->item_list
= (struct list_head
*)
61 CALLOC(sizeof(struct list_head
), 1);
62 pool
->unallocated_list
= (struct list_head
*)
63 CALLOC(sizeof(struct list_head
), 1);
64 list_inithead(pool
->item_list
);
65 list_inithead(pool
->unallocated_list
);
69 static void compute_memory_pool_init(struct compute_memory_pool
* pool
,
70 unsigned initial_size_in_dw
)
73 COMPUTE_DBG(pool
->screen
, "* compute_memory_pool_init() initial_size_in_dw = %ld\n",
76 pool
->shadow
= (uint32_t*)CALLOC(initial_size_in_dw
, 4);
77 if (pool
->shadow
== NULL
)
80 pool
->size_in_dw
= initial_size_in_dw
;
81 pool
->bo
= (struct r600_resource
*)r600_compute_buffer_alloc_vram(pool
->screen
,
82 pool
->size_in_dw
* 4);
86 * Frees all stuff in the pool and the pool struct itself too
88 void compute_memory_pool_delete(struct compute_memory_pool
* pool
)
90 COMPUTE_DBG(pool
->screen
, "* compute_memory_pool_delete()\n");
93 pool
->screen
->b
.b
.resource_destroy((struct pipe_screen
*)
94 pool
->screen
, (struct pipe_resource
*)pool
->bo
);
100 * Searches for an empty space in the pool, return with the pointer to the
101 * allocatable space in the pool, returns -1 on failure.
103 int64_t compute_memory_prealloc_chunk(
104 struct compute_memory_pool
* pool
,
107 struct compute_memory_item
*item
;
111 assert(size_in_dw
<= pool
->size_in_dw
);
113 COMPUTE_DBG(pool
->screen
, "* compute_memory_prealloc_chunk() size_in_dw = %ld\n",
116 LIST_FOR_EACH_ENTRY(item
, pool
->item_list
, link
) {
117 if (last_end
+ size_in_dw
<= item
->start_in_dw
) {
121 last_end
= item
->start_in_dw
+ align(item
->size_in_dw
, ITEM_ALIGNMENT
);
124 if (pool
->size_in_dw
- last_end
< size_in_dw
) {
132 * Search for the chunk where we can link our new chunk after it.
134 struct list_head
*compute_memory_postalloc_chunk(
135 struct compute_memory_pool
* pool
,
138 struct compute_memory_item
*item
;
139 struct compute_memory_item
*next
;
140 struct list_head
*next_link
;
142 COMPUTE_DBG(pool
->screen
, "* compute_memory_postalloc_chunck() start_in_dw = %ld\n",
145 /* Check if we can insert it in the front of the list */
146 item
= LIST_ENTRY(struct compute_memory_item
, pool
->item_list
->next
, link
);
147 if (LIST_IS_EMPTY(pool
->item_list
) || item
->start_in_dw
> start_in_dw
) {
148 return pool
->item_list
;
151 LIST_FOR_EACH_ENTRY(item
, pool
->item_list
, link
) {
152 next_link
= item
->link
.next
;
154 if (next_link
!= pool
->item_list
) {
155 next
= container_of(next_link
, item
, link
);
156 if (item
->start_in_dw
< start_in_dw
157 && next
->start_in_dw
> start_in_dw
) {
163 assert(item
->start_in_dw
< start_in_dw
);
168 assert(0 && "unreachable");
173 * Reallocates pool, conserves data.
174 * @returns -1 if it fails, 0 otherwise
176 int compute_memory_grow_pool(struct compute_memory_pool
* pool
,
177 struct pipe_context
* pipe
, int new_size_in_dw
)
179 COMPUTE_DBG(pool
->screen
, "* compute_memory_grow_pool() "
180 "new_size_in_dw = %d (%d bytes)\n",
181 new_size_in_dw
, new_size_in_dw
* 4);
183 assert(new_size_in_dw
>= pool
->size_in_dw
);
186 compute_memory_pool_init(pool
, MAX2(new_size_in_dw
, 1024 * 16));
187 if (pool
->shadow
== NULL
)
190 new_size_in_dw
= align(new_size_in_dw
, ITEM_ALIGNMENT
);
192 COMPUTE_DBG(pool
->screen
, " Aligned size = %d (%d bytes)\n",
193 new_size_in_dw
, new_size_in_dw
* 4);
195 compute_memory_shadow(pool
, pipe
, 1);
196 pool
->shadow
= realloc(pool
->shadow
, new_size_in_dw
*4);
197 if (pool
->shadow
== NULL
)
200 pool
->size_in_dw
= new_size_in_dw
;
201 pool
->screen
->b
.b
.resource_destroy(
202 (struct pipe_screen
*)pool
->screen
,
203 (struct pipe_resource
*)pool
->bo
);
204 pool
->bo
= (struct r600_resource
*)r600_compute_buffer_alloc_vram(
206 pool
->size_in_dw
* 4);
207 compute_memory_shadow(pool
, pipe
, 0);
214 * Copy pool from device to host, or host to device.
216 void compute_memory_shadow(struct compute_memory_pool
* pool
,
217 struct pipe_context
* pipe
, int device_to_host
)
219 struct compute_memory_item chunk
;
221 COMPUTE_DBG(pool
->screen
, "* compute_memory_shadow() device_to_host = %d\n",
225 chunk
.start_in_dw
= 0;
226 chunk
.size_in_dw
= pool
->size_in_dw
;
227 compute_memory_transfer(pool
, pipe
, device_to_host
, &chunk
,
228 pool
->shadow
, 0, pool
->size_in_dw
*4);
232 * Allocates pending allocations in the pool
233 * @returns -1 if it fails, 0 otherwise
235 int compute_memory_finalize_pending(struct compute_memory_pool
* pool
,
236 struct pipe_context
* pipe
)
238 struct compute_memory_item
*item
, *next
;
240 int64_t allocated
= 0;
241 int64_t unallocated
= 0;
245 COMPUTE_DBG(pool
->screen
, "* compute_memory_finalize_pending()\n");
247 LIST_FOR_EACH_ENTRY(item
, pool
->item_list
, link
) {
248 COMPUTE_DBG(pool
->screen
, " + list: offset = %i id = %i size = %i "
249 "(%i bytes)\n",item
->start_in_dw
, item
->id
,
250 item
->size_in_dw
, item
->size_in_dw
* 4);
253 /* Calculate the total allocated size */
254 LIST_FOR_EACH_ENTRY(item
, pool
->item_list
, link
) {
255 allocated
+= align(item
->size_in_dw
, ITEM_ALIGNMENT
);
258 /* Calculate the total unallocated size of the items that
259 * will be promoted to the pool */
260 LIST_FOR_EACH_ENTRY(item
, pool
->unallocated_list
, link
) {
261 if (item
->status
& ITEM_FOR_PROMOTING
)
262 unallocated
+= align(item
->size_in_dw
, ITEM_ALIGNMENT
);
265 if (unallocated
== 0) {
269 if (pool
->status
& POOL_FRAGMENTED
) {
270 compute_memory_defrag(pool
, pipe
);
273 if (pool
->size_in_dw
< allocated
+ unallocated
) {
274 err
= compute_memory_grow_pool(pool
, pipe
, allocated
+ unallocated
);
279 /* Loop through all the unallocated items, check if they are marked
280 * for promoting, allocate space for them and add them to the item_list. */
281 LIST_FOR_EACH_ENTRY_SAFE(item
, next
, pool
->unallocated_list
, link
) {
282 if (item
->status
& ITEM_FOR_PROMOTING
) {
283 err
= compute_memory_promote_item(pool
, item
, pipe
, allocated
);
284 item
->status
^= ITEM_FOR_PROMOTING
;
286 allocated
+= align(item
->size_in_dw
, ITEM_ALIGNMENT
);
297 * Defragments the pool, so that there's no gap between items.
298 * \param pool The pool to be defragmented
300 void compute_memory_defrag(struct compute_memory_pool
*pool
,
301 struct pipe_context
*pipe
)
303 struct compute_memory_item
*item
;
306 COMPUTE_DBG(pool
->screen
, "* compute_memory_defrag()\n");
309 LIST_FOR_EACH_ENTRY(item
, pool
->item_list
, link
) {
310 if (item
->start_in_dw
!= last_pos
) {
311 assert(last_pos
< item
->start_in_dw
);
313 compute_memory_move_item(pool
, item
, last_pos
, pipe
);
316 last_pos
+= align(item
->size_in_dw
, ITEM_ALIGNMENT
);
319 pool
->status
&= ~POOL_FRAGMENTED
;
322 int compute_memory_promote_item(struct compute_memory_pool
*pool
,
323 struct compute_memory_item
*item
, struct pipe_context
*pipe
,
326 struct pipe_screen
*screen
= (struct pipe_screen
*)pool
->screen
;
327 struct r600_context
*rctx
= (struct r600_context
*)pipe
;
328 struct pipe_resource
*src
= (struct pipe_resource
*)item
->real_buffer
;
329 struct pipe_resource
*dst
= NULL
;
332 struct list_head
*pos
;
337 /* Search for free space in the pool for this item. */
338 while ((start_in_dw
=compute_memory_prealloc_chunk(pool
,
339 item
->size_in_dw
)) == -1) {
340 int64_t need
= item
->size_in_dw
+ 2048 -
341 (pool
->size_in_dw
- allocated
);
344 /* There's enough free space, but it's too
345 * fragmented. Assume half of the item can fit
346 * int the last chunk */
347 need
= (item
->size_in_dw
/ 2) + ITEM_ALIGNMENT
;
350 need
= align(need
, ITEM_ALIGNMENT
);
352 err
= compute_memory_grow_pool(pool
,
354 pool
->size_in_dw
+ need
);
359 dst
= (struct pipe_resource
*)pool
->bo
;
360 COMPUTE_DBG(pool
->screen
, " + Found space for Item %p id = %u "
361 "start_in_dw = %u (%u bytes) size_in_dw = %u (%u bytes)\n",
362 item
, item
->id
, start_in_dw
, start_in_dw
* 4,
363 item
->size_in_dw
, item
->size_in_dw
* 4);
365 /* Remove the item from the unallocated list */
366 list_del(&item
->link
);
368 /* Add it back to the item_list */
369 pos
= compute_memory_postalloc_chunk(pool
, start_in_dw
);
370 list_add(&item
->link
, pos
);
371 item
->start_in_dw
= start_in_dw
;
374 u_box_1d(0, item
->size_in_dw
* 4, &box
);
376 rctx
->b
.b
.resource_copy_region(pipe
,
377 dst
, 0, item
->start_in_dw
* 4, 0 ,0,
380 /* We check if the item is mapped for reading.
381 * In this case, we need to keep the temporary buffer 'alive'
382 * because it is possible to keep a map active for reading
383 * while a kernel (that reads from it) executes */
384 if (!(item
->status
& ITEM_MAPPED_FOR_READING
)) {
385 pool
->screen
->b
.b
.resource_destroy(screen
, src
);
386 item
->real_buffer
= NULL
;
393 void compute_memory_demote_item(struct compute_memory_pool
*pool
,
394 struct compute_memory_item
*item
, struct pipe_context
*pipe
)
396 struct r600_context
*rctx
= (struct r600_context
*)pipe
;
397 struct pipe_resource
*src
= (struct pipe_resource
*)pool
->bo
;
398 struct pipe_resource
*dst
;
401 /* First, we remove the item from the item_list */
402 list_del(&item
->link
);
404 /* Now we add it to the unallocated list */
405 list_addtail(&item
->link
, pool
->unallocated_list
);
407 /* We check if the intermediate buffer exists, and if it
408 * doesn't, we create it again */
409 if (item
->real_buffer
== NULL
) {
410 item
->real_buffer
= (struct r600_resource
*)r600_compute_buffer_alloc_vram(
411 pool
->screen
, item
->size_in_dw
* 4);
414 dst
= (struct pipe_resource
*)item
->real_buffer
;
416 /* We transfer the memory from the item in the pool to the
417 * temporary buffer */
418 u_box_1d(item
->start_in_dw
* 4, item
->size_in_dw
* 4, &box
);
420 rctx
->b
.b
.resource_copy_region(pipe
,
424 /* Remember to mark the buffer as 'pending' by setting start_in_dw to -1 */
425 item
->start_in_dw
= -1;
427 if (item
->link
.next
!= pool
->item_list
) {
428 pool
->status
|= POOL_FRAGMENTED
;
433 * Moves the item \a item forward in the pool to \a new_start_in_dw
435 * This function assumes two things:
436 * 1) The item is \b only moved forward
437 * 2) The item \b won't change it's position inside the \a item_list
439 * \param item The item that will be moved
440 * \param new_start_in_dw The new position of the item in \a item_list
441 * \see compute_memory_defrag
443 void compute_memory_move_item(struct compute_memory_pool
*pool
,
444 struct compute_memory_item
*item
, uint64_t new_start_in_dw
,
445 struct pipe_context
*pipe
)
447 struct pipe_screen
*screen
= (struct pipe_screen
*)pool
->screen
;
448 struct r600_context
*rctx
= (struct r600_context
*)pipe
;
449 struct pipe_resource
*src
= (struct pipe_resource
*)pool
->bo
;
450 struct pipe_resource
*dst
;
453 struct compute_memory_item
*prev
;
455 COMPUTE_DBG(pool
->screen
, "* compute_memory_move_item()\n"
456 " + Moving item %i from %u (%u bytes) to %u (%u bytes)\n",
457 item
->id
, item
->start_in_dw
, item
->start_in_dw
* 4,
458 new_start_in_dw
, new_start_in_dw
* 4);
460 if (pool
->item_list
!= item
->link
.prev
) {
461 prev
= container_of(item
->link
.prev
, item
, link
);
462 assert(prev
->start_in_dw
+ prev
->size_in_dw
<= new_start_in_dw
);
465 u_box_1d(item
->start_in_dw
* 4, item
->size_in_dw
* 4, &box
);
467 /* If the ranges don't overlap, we can just copy the item directly */
468 if (new_start_in_dw
+ item
->size_in_dw
<= item
->start_in_dw
) {
469 dst
= (struct pipe_resource
*)pool
->bo
;
471 rctx
->b
.b
.resource_copy_region(pipe
,
472 dst
, 0, new_start_in_dw
* 4, 0, 0,
475 /* The ranges overlap, we will try first to use an intermediate
476 * resource to move the item */
477 dst
= (struct pipe_resource
*)r600_compute_buffer_alloc_vram(
478 pool
->screen
, item
->size_in_dw
* 4);
481 rctx
->b
.b
.resource_copy_region(pipe
,
486 dst
= (struct pipe_resource
*)pool
->bo
;
490 rctx
->b
.b
.resource_copy_region(pipe
,
491 dst
, 0, new_start_in_dw
* 4, 0, 0,
494 pool
->screen
->b
.b
.resource_destroy(screen
, src
);
497 /* The allocation of the temporary resource failed,
498 * falling back to use mappings */
501 struct pipe_transfer
*trans
;
503 offset
= item
->start_in_dw
- new_start_in_dw
;
505 u_box_1d(new_start_in_dw
* 4, (offset
+ item
->size_in_dw
) * 4, &box
);
507 map
= pipe
->transfer_map(pipe
, src
, 0, PIPE_TRANSFER_READ_WRITE
,
513 memmove(map
, map
+ offset
, item
->size_in_dw
* 4);
515 pipe
->transfer_unmap(pipe
, trans
);
519 item
->start_in_dw
= new_start_in_dw
;
522 void compute_memory_free(struct compute_memory_pool
* pool
, int64_t id
)
524 struct compute_memory_item
*item
, *next
;
525 struct pipe_screen
*screen
= (struct pipe_screen
*)pool
->screen
;
526 struct pipe_resource
*res
;
528 COMPUTE_DBG(pool
->screen
, "* compute_memory_free() id + %ld \n", id
);
530 LIST_FOR_EACH_ENTRY_SAFE(item
, next
, pool
->item_list
, link
) {
532 if (item
->id
== id
) {
534 if (item
->link
.next
!= pool
->item_list
) {
535 pool
->status
|= POOL_FRAGMENTED
;
538 list_del(&item
->link
);
540 if (item
->real_buffer
) {
541 res
= (struct pipe_resource
*)item
->real_buffer
;
542 pool
->screen
->b
.b
.resource_destroy(
552 LIST_FOR_EACH_ENTRY_SAFE(item
, next
, pool
->unallocated_list
, link
) {
554 if (item
->id
== id
) {
555 list_del(&item
->link
);
557 if (item
->real_buffer
) {
558 res
= (struct pipe_resource
*)item
->real_buffer
;
559 pool
->screen
->b
.b
.resource_destroy(
569 fprintf(stderr
, "Internal error, invalid id %"PRIi64
" "
570 "for compute_memory_free\n", id
);
572 assert(0 && "error");
576 * Creates pending allocations
578 struct compute_memory_item
* compute_memory_alloc(
579 struct compute_memory_pool
* pool
,
582 struct compute_memory_item
*new_item
= NULL
;
584 COMPUTE_DBG(pool
->screen
, "* compute_memory_alloc() size_in_dw = %ld (%ld bytes)\n",
585 size_in_dw
, 4 * size_in_dw
);
587 new_item
= (struct compute_memory_item
*)
588 CALLOC(sizeof(struct compute_memory_item
), 1);
589 if (new_item
== NULL
)
592 new_item
->size_in_dw
= size_in_dw
;
593 new_item
->start_in_dw
= -1; /* mark pending */
594 new_item
->id
= pool
->next_id
++;
595 new_item
->pool
= pool
;
596 new_item
->real_buffer
= NULL
;
598 list_addtail(&new_item
->link
, pool
->unallocated_list
);
600 COMPUTE_DBG(pool
->screen
, " + Adding item %p id = %u size = %u (%u bytes)\n",
601 new_item
, new_item
->id
, new_item
->size_in_dw
,
602 new_item
->size_in_dw
* 4);
607 * Transfer data host<->device, offset and size is in bytes
609 void compute_memory_transfer(
610 struct compute_memory_pool
* pool
,
611 struct pipe_context
* pipe
,
613 struct compute_memory_item
* chunk
,
618 int64_t aligned_size
= pool
->size_in_dw
;
619 struct pipe_resource
* gart
= (struct pipe_resource
*)pool
->bo
;
620 int64_t internal_offset
= chunk
->start_in_dw
*4 + offset_in_chunk
;
622 struct pipe_transfer
*xfer
;
627 COMPUTE_DBG(pool
->screen
, "* compute_memory_transfer() device_to_host = %d, "
628 "offset_in_chunk = %d, size = %d\n", device_to_host
,
629 offset_in_chunk
, size
);
631 if (device_to_host
) {
632 map
= pipe
->transfer_map(pipe
, gart
, 0, PIPE_TRANSFER_READ
,
633 &(struct pipe_box
) { .width
= aligned_size
* 4,
634 .height
= 1, .depth
= 1 }, &xfer
);
637 memcpy(data
, map
+ internal_offset
, size
);
638 pipe
->transfer_unmap(pipe
, xfer
);
640 map
= pipe
->transfer_map(pipe
, gart
, 0, PIPE_TRANSFER_WRITE
,
641 &(struct pipe_box
) { .width
= aligned_size
* 4,
642 .height
= 1, .depth
= 1 }, &xfer
);
645 memcpy(map
+ internal_offset
, data
, size
);
646 pipe
->transfer_unmap(pipe
, xfer
);
651 * Transfer data between chunk<->data, it is for VRAM<->GART transfers
653 void compute_memory_transfer_direct(
654 struct compute_memory_pool
* pool
,
656 struct compute_memory_item
* chunk
,
657 struct r600_resource
* data
,