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);
53 COMPUTE_DBG(rscreen
, "* compute_memory_pool_new()\n");
55 pool
->screen
= rscreen
;
59 static void compute_memory_pool_init(struct compute_memory_pool
* pool
,
60 unsigned initial_size_in_dw
)
63 COMPUTE_DBG(pool
->screen
, "* compute_memory_pool_init() initial_size_in_dw = %ld\n",
66 pool
->shadow
= (uint32_t*)CALLOC(initial_size_in_dw
, 4);
68 pool
->size_in_dw
= initial_size_in_dw
;
69 pool
->bo
= (struct r600_resource
*)r600_compute_buffer_alloc_vram(pool
->screen
,
70 pool
->size_in_dw
* 4);
74 * Frees all stuff in the pool and the pool struct itself too
76 void compute_memory_pool_delete(struct compute_memory_pool
* pool
)
78 COMPUTE_DBG(pool
->screen
, "* compute_memory_pool_delete()\n");
81 pool
->screen
->b
.b
.resource_destroy((struct pipe_screen
*)
82 pool
->screen
, (struct pipe_resource
*)pool
->bo
);
88 * Searches for an empty space in the pool, return with the pointer to the
89 * allocatable space in the pool, returns -1 on failure.
91 int64_t compute_memory_prealloc_chunk(
92 struct compute_memory_pool
* pool
,
95 struct compute_memory_item
*item
;
99 assert(size_in_dw
<= pool
->size_in_dw
);
101 COMPUTE_DBG(pool
->screen
, "* compute_memory_prealloc_chunk() size_in_dw = %ld\n",
104 for (item
= pool
->item_list
; item
; item
= item
->next
) {
105 if (item
->start_in_dw
> -1) {
106 if (item
->start_in_dw
-last_end
> size_in_dw
) {
110 last_end
= item
->start_in_dw
+ item
->size_in_dw
;
111 last_end
+= (1024 - last_end
% 1024);
115 if (pool
->size_in_dw
- last_end
< size_in_dw
) {
123 * Search for the chunk where we can link our new chunk after it.
125 struct compute_memory_item
* compute_memory_postalloc_chunk(
126 struct compute_memory_pool
* pool
,
129 struct compute_memory_item
* item
;
131 COMPUTE_DBG(pool
->screen
, "* compute_memory_postalloc_chunck() start_in_dw = %ld\n",
134 /* Check if we can insert it in the front of the list */
135 if (pool
->item_list
&& pool
->item_list
->start_in_dw
> start_in_dw
) {
139 for (item
= pool
->item_list
; item
; item
= item
->next
) {
141 if (item
->start_in_dw
< start_in_dw
142 && item
->next
->start_in_dw
> start_in_dw
) {
148 assert(item
->start_in_dw
< start_in_dw
);
153 assert(0 && "unreachable");
158 * Reallocates pool, conserves data
160 void compute_memory_grow_pool(struct compute_memory_pool
* pool
,
161 struct pipe_context
* pipe
, int new_size_in_dw
)
163 COMPUTE_DBG(pool
->screen
, "* compute_memory_grow_pool() "
164 "new_size_in_dw = %d (%d bytes)\n",
165 new_size_in_dw
, new_size_in_dw
* 4);
167 assert(new_size_in_dw
>= pool
->size_in_dw
);
170 compute_memory_pool_init(pool
, MAX2(new_size_in_dw
, 1024 * 16));
172 new_size_in_dw
+= 1024 - (new_size_in_dw
% 1024);
174 COMPUTE_DBG(pool
->screen
, " Aligned size = %d (%d bytes)\n",
175 new_size_in_dw
, new_size_in_dw
* 4);
177 compute_memory_shadow(pool
, pipe
, 1);
178 pool
->shadow
= realloc(pool
->shadow
, new_size_in_dw
*4);
179 pool
->size_in_dw
= new_size_in_dw
;
180 pool
->screen
->b
.b
.resource_destroy(
181 (struct pipe_screen
*)pool
->screen
,
182 (struct pipe_resource
*)pool
->bo
);
183 pool
->bo
= (struct r600_resource
*)r600_compute_buffer_alloc_vram(
185 pool
->size_in_dw
* 4);
186 compute_memory_shadow(pool
, pipe
, 0);
191 * Copy pool from device to host, or host to device.
193 void compute_memory_shadow(struct compute_memory_pool
* pool
,
194 struct pipe_context
* pipe
, int device_to_host
)
196 struct compute_memory_item chunk
;
198 COMPUTE_DBG(pool
->screen
, "* compute_memory_shadow() device_to_host = %d\n",
202 chunk
.start_in_dw
= 0;
203 chunk
.size_in_dw
= pool
->size_in_dw
;
204 chunk
.prev
= chunk
.next
= NULL
;
205 compute_memory_transfer(pool
, pipe
, device_to_host
, &chunk
,
206 pool
->shadow
, 0, pool
->size_in_dw
*4);
210 * Allocates pending allocations in the pool
212 void compute_memory_finalize_pending(struct compute_memory_pool
* pool
,
213 struct pipe_context
* pipe
)
215 struct compute_memory_item
*pending_list
= NULL
, *end_p
= NULL
;
216 struct compute_memory_item
*item
, *next
;
218 int64_t allocated
= 0;
219 int64_t unallocated
= 0;
221 int64_t start_in_dw
= 0;
223 COMPUTE_DBG(pool
->screen
, "* compute_memory_finalize_pending()\n");
225 for (item
= pool
->item_list
; item
; item
= item
->next
) {
226 COMPUTE_DBG(pool
->screen
, " + list: offset = %i id = %i size = %i "
227 "(%i bytes)\n",item
->start_in_dw
, item
->id
,
228 item
->size_in_dw
, item
->size_in_dw
* 4);
231 /* Search through the list of memory items in the pool */
232 for (item
= pool
->item_list
; item
; item
= next
) {
235 /* Check if the item is pending. */
236 if (item
->start_in_dw
== -1) {
237 /* It is pending, so add it to the pending_list... */
245 /* ... and then remove it from the item list. */
247 item
->prev
->next
= next
;
250 pool
->item_list
= next
;
254 next
->prev
= item
->prev
;
257 /* This sequence makes the item be at the end of the list */
262 /* Update the amount of space we will need to allocate. */
263 unallocated
+= item
->size_in_dw
+1024;
266 /* The item is not pendng, so update the amount of space
267 * that has already been allocated. */
268 allocated
+= item
->size_in_dw
;
272 /* If we require more space than the size of the pool, then grow the
275 * XXX: I'm pretty sure this won't work. Imagine this scenario:
283 * Allocated size = 150
284 * Pending Item D Size = 200
286 * In this case, there are 300 units of free space in the pool, but
287 * they aren't contiguous, so it will be impossible to allocate Item D.
289 if (pool
->size_in_dw
< allocated
+unallocated
) {
290 compute_memory_grow_pool(pool
, pipe
, allocated
+unallocated
);
293 /* Loop through all the pending items, allocate space for them and
294 * add them back to the item_list. */
295 for (item
= pending_list
; item
; item
= next
) {
298 /* Search for free space in the pool for this item. */
299 while ((start_in_dw
=compute_memory_prealloc_chunk(pool
,
300 item
->size_in_dw
)) == -1) {
301 int64_t need
= item
->size_in_dw
+2048 -
302 (pool
->size_in_dw
- allocated
);
304 need
+= 1024 - (need
% 1024);
307 compute_memory_grow_pool(pool
,
309 pool
->size_in_dw
+ need
);
312 need
= pool
->size_in_dw
/ 10;
313 need
+= 1024 - (need
% 1024);
314 compute_memory_grow_pool(pool
,
316 pool
->size_in_dw
+ need
);
319 COMPUTE_DBG(pool
->screen
, " + Found space for Item %p id = %u "
320 "start_in_dw = %u (%u bytes) size_in_dw = %u (%u bytes)\n",
321 item
, item
->id
, start_in_dw
, start_in_dw
* 4,
322 item
->size_in_dw
, item
->size_in_dw
* 4);
324 item
->start_in_dw
= start_in_dw
;
328 if (pool
->item_list
) {
329 struct compute_memory_item
*pos
;
331 pos
= compute_memory_postalloc_chunk(pool
, start_in_dw
);
334 item
->next
= pos
->next
;
337 item
->next
->prev
= item
;
340 /* Add item to the front of the list */
341 item
->next
= pool
->item_list
;
342 item
->prev
= pool
->item_list
->prev
;
343 pool
->item_list
->prev
= item
;
344 pool
->item_list
= item
;
348 pool
->item_list
= item
;
351 allocated
+= item
->size_in_dw
;
356 void compute_memory_free(struct compute_memory_pool
* pool
, int64_t id
)
358 struct compute_memory_item
*item
, *next
;
360 COMPUTE_DBG(pool
->screen
, "* compute_memory_free() id + %ld \n", id
);
362 for (item
= pool
->item_list
; item
; item
= next
) {
365 if (item
->id
== id
) {
367 item
->prev
->next
= item
->next
;
370 pool
->item_list
= item
->next
;
374 item
->next
->prev
= item
->prev
;
383 fprintf(stderr
, "Internal error, invalid id %"PRIi64
" "
384 "for compute_memory_free\n", id
);
386 assert(0 && "error");
390 * Creates pending allocations
392 struct compute_memory_item
* compute_memory_alloc(
393 struct compute_memory_pool
* pool
,
396 struct compute_memory_item
*new_item
= NULL
, *last_item
= NULL
;
398 COMPUTE_DBG(pool
->screen
, "* compute_memory_alloc() size_in_dw = %ld (%ld bytes)\n",
399 size_in_dw
, 4 * size_in_dw
);
401 new_item
= (struct compute_memory_item
*)
402 CALLOC(sizeof(struct compute_memory_item
), 1);
403 new_item
->size_in_dw
= size_in_dw
;
404 new_item
->start_in_dw
= -1; /* mark pending */
405 new_item
->id
= pool
->next_id
++;
406 new_item
->pool
= pool
;
408 if (pool
->item_list
) {
409 for (last_item
= pool
->item_list
; last_item
->next
;
410 last_item
= last_item
->next
);
412 last_item
->next
= new_item
;
413 new_item
->prev
= last_item
;
416 pool
->item_list
= new_item
;
419 COMPUTE_DBG(pool
->screen
, " + Adding item %p id = %u size = %u (%u bytes)\n",
420 new_item
, new_item
->id
, new_item
->size_in_dw
,
421 new_item
->size_in_dw
* 4);
426 * Transfer data host<->device, offset and size is in bytes
428 void compute_memory_transfer(
429 struct compute_memory_pool
* pool
,
430 struct pipe_context
* pipe
,
432 struct compute_memory_item
* chunk
,
437 int64_t aligned_size
= pool
->size_in_dw
;
438 struct pipe_resource
* gart
= (struct pipe_resource
*)pool
->bo
;
439 int64_t internal_offset
= chunk
->start_in_dw
*4 + offset_in_chunk
;
441 struct pipe_transfer
*xfer
;
446 COMPUTE_DBG(pool
->screen
, "* compute_memory_transfer() device_to_host = %d, "
447 "offset_in_chunk = %d, size = %d\n", device_to_host
,
448 offset_in_chunk
, size
);
450 if (device_to_host
) {
451 map
= pipe
->transfer_map(pipe
, gart
, 0, PIPE_TRANSFER_READ
,
452 &(struct pipe_box
) { .width
= aligned_size
* 4,
453 .height
= 1, .depth
= 1 }, &xfer
);
456 memcpy(data
, map
+ internal_offset
, size
);
457 pipe
->transfer_unmap(pipe
, xfer
);
459 map
= pipe
->transfer_map(pipe
, gart
, 0, PIPE_TRANSFER_WRITE
,
460 &(struct pipe_box
) { .width
= aligned_size
* 4,
461 .height
= 1, .depth
= 1 }, &xfer
);
464 memcpy(map
+ internal_offset
, data
, size
);
465 pipe
->transfer_unmap(pipe
, xfer
);
470 * Transfer data between chunk<->data, it is for VRAM<->GART transfers
472 void compute_memory_transfer_direct(
473 struct compute_memory_pool
* pool
,
475 struct compute_memory_item
* chunk
,
476 struct r600_resource
* data
,