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_memory.h"
34 #include "util/u_inlines.h"
35 #include "util/u_framebuffer.h"
36 #include "r600_shader.h"
37 #include "r600_pipe.h"
38 #include "r600_formats.h"
39 #include "compute_memory_pool.h"
40 #include "evergreen_compute.h"
41 #include "evergreen_compute_internal.h"
47 struct compute_memory_pool
* compute_memory_pool_new(
48 struct r600_screen
* rscreen
)
50 struct compute_memory_pool
* pool
= (struct compute_memory_pool
*)
51 CALLOC(sizeof(struct compute_memory_pool
), 1);
55 COMPUTE_DBG(rscreen
, "* compute_memory_pool_new()\n");
57 pool
->screen
= rscreen
;
61 static void compute_memory_pool_init(struct compute_memory_pool
* pool
,
62 unsigned initial_size_in_dw
)
65 COMPUTE_DBG(pool
->screen
, "* compute_memory_pool_init() initial_size_in_dw = %ld\n",
68 pool
->shadow
= (uint32_t*)CALLOC(initial_size_in_dw
, 4);
69 if (pool
->shadow
== NULL
)
73 pool
->size_in_dw
= initial_size_in_dw
;
74 pool
->bo
= (struct r600_resource
*)r600_compute_buffer_alloc_vram(pool
->screen
,
75 pool
->size_in_dw
* 4);
79 * Frees all stuff in the pool and the pool struct itself too
81 void compute_memory_pool_delete(struct compute_memory_pool
* pool
)
83 COMPUTE_DBG(pool
->screen
, "* compute_memory_pool_delete()\n");
86 pool
->screen
->b
.b
.resource_destroy((struct pipe_screen
*)
87 pool
->screen
, (struct pipe_resource
*)pool
->bo
);
93 * Searches for an empty space in the pool, return with the pointer to the
94 * allocatable space in the pool, returns -1 on failure.
96 int64_t compute_memory_prealloc_chunk(
97 struct compute_memory_pool
* pool
,
100 struct compute_memory_item
*item
;
104 assert(size_in_dw
<= pool
->size_in_dw
);
106 COMPUTE_DBG(pool
->screen
, "* compute_memory_prealloc_chunk() size_in_dw = %ld\n",
109 for (item
= pool
->item_list
; item
; item
= item
->next
) {
110 if (item
->start_in_dw
> -1) {
111 if (item
->start_in_dw
-last_end
> size_in_dw
) {
115 last_end
= item
->start_in_dw
+ item
->size_in_dw
;
116 last_end
+= (1024 - last_end
% 1024);
120 if (pool
->size_in_dw
- last_end
< size_in_dw
) {
128 * Search for the chunk where we can link our new chunk after it.
130 struct compute_memory_item
* compute_memory_postalloc_chunk(
131 struct compute_memory_pool
* pool
,
134 struct compute_memory_item
* item
;
136 COMPUTE_DBG(pool
->screen
, "* compute_memory_postalloc_chunck() start_in_dw = %ld\n",
139 /* Check if we can insert it in the front of the list */
140 if (pool
->item_list
&& pool
->item_list
->start_in_dw
> start_in_dw
) {
144 for (item
= pool
->item_list
; item
; item
= item
->next
) {
146 if (item
->start_in_dw
< start_in_dw
147 && item
->next
->start_in_dw
> start_in_dw
) {
153 assert(item
->start_in_dw
< start_in_dw
);
158 assert(0 && "unreachable");
163 * Reallocates pool, conserves data.
164 * @returns -1 if it fails, 0 otherwise
166 int compute_memory_grow_pool(struct compute_memory_pool
* pool
,
167 struct pipe_context
* pipe
, int new_size_in_dw
)
169 COMPUTE_DBG(pool
->screen
, "* compute_memory_grow_pool() "
170 "new_size_in_dw = %d (%d bytes)\n",
171 new_size_in_dw
, new_size_in_dw
* 4);
173 assert(new_size_in_dw
>= pool
->size_in_dw
);
176 compute_memory_pool_init(pool
, MAX2(new_size_in_dw
, 1024 * 16));
177 if (pool
->shadow
== NULL
)
180 new_size_in_dw
+= 1024 - (new_size_in_dw
% 1024);
182 COMPUTE_DBG(pool
->screen
, " Aligned size = %d (%d bytes)\n",
183 new_size_in_dw
, new_size_in_dw
* 4);
185 compute_memory_shadow(pool
, pipe
, 1);
186 pool
->shadow
= realloc(pool
->shadow
, new_size_in_dw
*4);
187 if (pool
->shadow
== NULL
)
190 pool
->size_in_dw
= new_size_in_dw
;
191 pool
->screen
->b
.b
.resource_destroy(
192 (struct pipe_screen
*)pool
->screen
,
193 (struct pipe_resource
*)pool
->bo
);
194 pool
->bo
= (struct r600_resource
*)r600_compute_buffer_alloc_vram(
196 pool
->size_in_dw
* 4);
197 compute_memory_shadow(pool
, pipe
, 0);
204 * Copy pool from device to host, or host to device.
206 void compute_memory_shadow(struct compute_memory_pool
* pool
,
207 struct pipe_context
* pipe
, int device_to_host
)
209 struct compute_memory_item chunk
;
211 COMPUTE_DBG(pool
->screen
, "* compute_memory_shadow() device_to_host = %d\n",
215 chunk
.start_in_dw
= 0;
216 chunk
.size_in_dw
= pool
->size_in_dw
;
217 chunk
.prev
= chunk
.next
= NULL
;
218 compute_memory_transfer(pool
, pipe
, device_to_host
, &chunk
,
219 pool
->shadow
, 0, pool
->size_in_dw
*4);
223 * Allocates pending allocations in the pool
224 * @returns -1 if it fails, 0 otherwise
226 int compute_memory_finalize_pending(struct compute_memory_pool
* pool
,
227 struct pipe_context
* pipe
)
229 struct compute_memory_item
*pending_list
= NULL
, *end_p
= NULL
;
230 struct compute_memory_item
*item
, *next
;
232 int64_t allocated
= 0;
233 int64_t unallocated
= 0;
235 int64_t start_in_dw
= 0;
239 COMPUTE_DBG(pool
->screen
, "* compute_memory_finalize_pending()\n");
241 for (item
= pool
->item_list
; item
; item
= item
->next
) {
242 COMPUTE_DBG(pool
->screen
, " + list: offset = %i id = %i size = %i "
243 "(%i bytes)\n",item
->start_in_dw
, item
->id
,
244 item
->size_in_dw
, item
->size_in_dw
* 4);
247 /* Search through the list of memory items in the pool */
248 for (item
= pool
->item_list
; item
; item
= next
) {
251 /* Check if the item is pending. */
252 if (item
->start_in_dw
== -1) {
253 /* It is pending, so add it to the pending_list... */
261 /* ... and then remove it from the item list. */
263 item
->prev
->next
= next
;
266 pool
->item_list
= next
;
270 next
->prev
= item
->prev
;
273 /* This sequence makes the item be at the end of the list */
278 /* Update the amount of space we will need to allocate. */
279 unallocated
+= item
->size_in_dw
+1024;
282 /* The item is not pending, so update the amount of space
283 * that has already been allocated. */
284 allocated
+= item
->size_in_dw
;
288 /* If we require more space than the size of the pool, then grow the
291 * XXX: I'm pretty sure this won't work. Imagine this scenario:
299 * Allocated size = 150
300 * Pending Item D Size = 200
302 * In this case, there are 300 units of free space in the pool, but
303 * they aren't contiguous, so it will be impossible to allocate Item D.
305 if (pool
->size_in_dw
< allocated
+unallocated
) {
306 err
= compute_memory_grow_pool(pool
, pipe
, allocated
+unallocated
);
311 /* Loop through all the pending items, allocate space for them and
312 * add them back to the item_list. */
313 for (item
= pending_list
; item
; item
= next
) {
316 /* Search for free space in the pool for this item. */
317 while ((start_in_dw
=compute_memory_prealloc_chunk(pool
,
318 item
->size_in_dw
)) == -1) {
319 int64_t need
= item
->size_in_dw
+2048 -
320 (pool
->size_in_dw
- allocated
);
322 need
+= 1024 - (need
% 1024);
325 err
= compute_memory_grow_pool(pool
,
327 pool
->size_in_dw
+ need
);
330 need
= pool
->size_in_dw
/ 10;
331 need
+= 1024 - (need
% 1024);
332 err
= compute_memory_grow_pool(pool
,
334 pool
->size_in_dw
+ need
);
340 COMPUTE_DBG(pool
->screen
, " + Found space for Item %p id = %u "
341 "start_in_dw = %u (%u bytes) size_in_dw = %u (%u bytes)\n",
342 item
, item
->id
, start_in_dw
, start_in_dw
* 4,
343 item
->size_in_dw
, item
->size_in_dw
* 4);
345 item
->start_in_dw
= start_in_dw
;
349 if (pool
->item_list
) {
350 struct compute_memory_item
*pos
;
352 pos
= compute_memory_postalloc_chunk(pool
, start_in_dw
);
355 item
->next
= pos
->next
;
358 item
->next
->prev
= item
;
361 /* Add item to the front of the list */
362 item
->next
= pool
->item_list
;
363 item
->prev
= pool
->item_list
->prev
;
364 pool
->item_list
->prev
= item
;
365 pool
->item_list
= item
;
369 pool
->item_list
= item
;
372 allocated
+= item
->size_in_dw
;
379 void compute_memory_free(struct compute_memory_pool
* pool
, int64_t id
)
381 struct compute_memory_item
*item
, *next
;
383 COMPUTE_DBG(pool
->screen
, "* compute_memory_free() id + %ld \n", id
);
385 for (item
= pool
->item_list
; item
; item
= next
) {
388 if (item
->id
== id
) {
390 item
->prev
->next
= item
->next
;
393 pool
->item_list
= item
->next
;
397 item
->next
->prev
= item
->prev
;
406 fprintf(stderr
, "Internal error, invalid id %"PRIi64
" "
407 "for compute_memory_free\n", id
);
409 assert(0 && "error");
413 * Creates pending allocations
415 struct compute_memory_item
* compute_memory_alloc(
416 struct compute_memory_pool
* pool
,
419 struct compute_memory_item
*new_item
= NULL
, *last_item
= NULL
;
421 COMPUTE_DBG(pool
->screen
, "* compute_memory_alloc() size_in_dw = %ld (%ld bytes)\n",
422 size_in_dw
, 4 * size_in_dw
);
424 new_item
= (struct compute_memory_item
*)
425 CALLOC(sizeof(struct compute_memory_item
), 1);
426 if (new_item
== NULL
)
429 new_item
->size_in_dw
= size_in_dw
;
430 new_item
->start_in_dw
= -1; /* mark pending */
431 new_item
->id
= pool
->next_id
++;
432 new_item
->pool
= pool
;
434 if (pool
->item_list
) {
435 for (last_item
= pool
->item_list
; last_item
->next
;
436 last_item
= last_item
->next
);
438 last_item
->next
= new_item
;
439 new_item
->prev
= last_item
;
442 pool
->item_list
= new_item
;
445 COMPUTE_DBG(pool
->screen
, " + Adding item %p id = %u size = %u (%u bytes)\n",
446 new_item
, new_item
->id
, new_item
->size_in_dw
,
447 new_item
->size_in_dw
* 4);
452 * Transfer data host<->device, offset and size is in bytes
454 void compute_memory_transfer(
455 struct compute_memory_pool
* pool
,
456 struct pipe_context
* pipe
,
458 struct compute_memory_item
* chunk
,
463 int64_t aligned_size
= pool
->size_in_dw
;
464 struct pipe_resource
* gart
= (struct pipe_resource
*)pool
->bo
;
465 int64_t internal_offset
= chunk
->start_in_dw
*4 + offset_in_chunk
;
467 struct pipe_transfer
*xfer
;
472 COMPUTE_DBG(pool
->screen
, "* compute_memory_transfer() device_to_host = %d, "
473 "offset_in_chunk = %d, size = %d\n", device_to_host
,
474 offset_in_chunk
, size
);
476 if (device_to_host
) {
477 map
= pipe
->transfer_map(pipe
, gart
, 0, PIPE_TRANSFER_READ
,
478 &(struct pipe_box
) { .width
= aligned_size
* 4,
479 .height
= 1, .depth
= 1 }, &xfer
);
482 memcpy(data
, map
+ internal_offset
, size
);
483 pipe
->transfer_unmap(pipe
, xfer
);
485 map
= pipe
->transfer_map(pipe
, gart
, 0, PIPE_TRANSFER_WRITE
,
486 &(struct pipe_box
) { .width
= aligned_size
* 4,
487 .height
= 1, .depth
= 1 }, &xfer
);
490 memcpy(map
+ internal_offset
, data
, size
);
491 pipe
->transfer_unmap(pipe
, xfer
);
496 * Transfer data between chunk<->data, it is for VRAM<->GART transfers
498 void compute_memory_transfer_direct(
499 struct compute_memory_pool
* pool
,
501 struct compute_memory_item
* chunk
,
502 struct r600_resource
* data
,