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"
37 #include "r600_resource.h"
38 #include "r600_shader.h"
39 #include "r600_pipe.h"
40 #include "r600_formats.h"
41 #include "compute_memory_pool.h"
42 #include "evergreen_compute_internal.h"
44 static struct r600_resource_texture
* create_pool_texture(struct r600_screen
* screen
,
48 struct pipe_resource templ
;
49 struct r600_resource_texture
* tex
;
51 memset(&templ
, 0, sizeof(templ
));
52 templ
.target
= PIPE_TEXTURE_1D
;
53 templ
.format
= PIPE_FORMAT_R32_UINT
;
54 templ
.bind
= PIPE_BIND_CUSTOM
;
55 templ
.usage
= PIPE_USAGE_IMMUTABLE
;
57 templ
.width0
= size_in_dw
;
62 tex
= (struct r600_resource_texture
*)r600_texture_create(
63 &screen
->screen
, &templ
);
64 /* XXX: Propagate this error */
65 assert(tex
&& "Out of memory");
73 struct compute_memory_pool
* compute_memory_pool_new(
74 int64_t initial_size_in_dw
,
75 struct r600_screen
* rscreen
)
77 struct compute_memory_pool
* pool
= (struct compute_memory_pool
*)
78 CALLOC(sizeof(struct compute_memory_pool
), 1);
81 pool
->size_in_dw
= initial_size_in_dw
;
82 pool
->screen
= rscreen
;
83 pool
->bo
= (struct r600_resource
*)create_pool_texture(pool
->screen
,
85 pool
->shadow
= (uint32_t*)CALLOC(4, pool
->size_in_dw
);
91 * Frees all stuff in the pool and the pool struct itself too
93 void compute_memory_pool_delete(struct compute_memory_pool
* pool
)
96 pool
->screen
->screen
.resource_destroy((struct pipe_screen
*)
97 pool
->screen
, (struct pipe_resource
*)pool
->bo
);
102 * Searches for an empty space in the pool, return with the pointer to the
103 * allocatable space in the pool, returns -1 on failure.
105 int64_t compute_memory_prealloc_chunk(
106 struct compute_memory_pool
* pool
,
109 assert(size_in_dw
<= pool
->size_in_dw
);
111 struct compute_memory_item
*item
;
115 for (item
= pool
->item_list
; item
; item
= item
->next
) {
116 if (item
->start_in_dw
> -1) {
117 if (item
->start_in_dw
-last_end
> size_in_dw
) {
121 last_end
= item
->start_in_dw
+ item
->size_in_dw
;
122 last_end
+= (1024 - last_end
% 1024);
126 if (pool
->size_in_dw
- last_end
< size_in_dw
) {
134 * Search for the chunk where we can link our new chunk after it.
136 struct compute_memory_item
* compute_memory_postalloc_chunk(
137 struct compute_memory_pool
* pool
,
140 struct compute_memory_item
* item
;
142 for (item
= pool
->item_list
; item
; item
= item
->next
) {
144 if (item
->start_in_dw
< start_in_dw
145 && item
->next
->start_in_dw
> start_in_dw
) {
151 assert(item
->start_in_dw
< start_in_dw
);
156 assert(0 && "unreachable");
161 * Reallocates pool, conserves data
163 void compute_memory_grow_pool(struct compute_memory_pool
* pool
,
164 struct pipe_context
* pipe
, int new_size_in_dw
)
166 assert(new_size_in_dw
>= pool
->size_in_dw
);
168 new_size_in_dw
+= 1024 - (new_size_in_dw
% 1024);
170 compute_memory_shadow(pool
, pipe
, 1);
171 pool
->shadow
= (uint32_t*)realloc(pool
->shadow
, new_size_in_dw
*4);
172 pool
->size_in_dw
= new_size_in_dw
;
173 pool
->screen
->screen
.resource_destroy(
174 (struct pipe_screen
*)pool
->screen
,
175 (struct pipe_resource
*)pool
->bo
);
176 pool
->bo
= (struct r600_resource
*)create_pool_texture(pool
->screen
,
178 compute_memory_shadow(pool
, pipe
, 0);
182 * Copy pool from device to host, or host to device.
184 void compute_memory_shadow(struct compute_memory_pool
* pool
,
185 struct pipe_context
* pipe
, int device_to_host
)
187 struct compute_memory_item chunk
;
190 chunk
.start_in_dw
= 0;
191 chunk
.size_in_dw
= pool
->size_in_dw
;
192 chunk
.prev
= chunk
.next
= NULL
;
193 compute_memory_transfer(pool
, pipe
, device_to_host
, &chunk
,
194 pool
->shadow
, 0, pool
->size_in_dw
*4);
198 * Allocates pending allocations in the pool
200 void compute_memory_finalize_pending(struct compute_memory_pool
* pool
,
201 struct pipe_context
* pipe
)
203 struct compute_memory_item
*pending_list
= NULL
, *end_p
= NULL
;
204 struct compute_memory_item
*item
, *next
;
206 int64_t allocated
= 0;
207 int64_t unallocated
= 0;
209 for (item
= pool
->item_list
; item
; item
= item
->next
) {
210 COMPUTE_DBG("list: %i %p\n", item
->start_in_dw
, item
->next
);
213 for (item
= pool
->item_list
; item
; item
= next
) {
217 if (item
->start_in_dw
== -1) {
226 item
->prev
->next
= next
;
229 pool
->item_list
= next
;
233 next
->prev
= item
->prev
;
240 unallocated
+= item
->size_in_dw
+1024;
243 allocated
+= item
->size_in_dw
;
247 if (pool
->size_in_dw
< allocated
+unallocated
) {
248 compute_memory_grow_pool(pool
, pipe
, allocated
+unallocated
);
251 for (item
= pending_list
; item
; item
= next
) {
256 while ((start_in_dw
=compute_memory_prealloc_chunk(pool
,
257 item
->size_in_dw
)) == -1) {
258 int64_t need
= item
->size_in_dw
+2048 -
259 (pool
->size_in_dw
- allocated
);
261 need
+= 1024 - (need
% 1024);
264 compute_memory_grow_pool(pool
,
266 pool
->size_in_dw
+ need
);
269 need
= pool
->size_in_dw
/ 10;
270 need
+= 1024 - (need
% 1024);
271 compute_memory_grow_pool(pool
,
273 pool
->size_in_dw
+ need
);
277 item
->start_in_dw
= start_in_dw
;
281 if (pool
->item_list
) {
282 struct compute_memory_item
*pos
;
284 pos
= compute_memory_postalloc_chunk(pool
, start_in_dw
);
286 item
->next
= pos
->next
;
290 item
->next
->prev
= item
;
294 pool
->item_list
= item
;
297 allocated
+= item
->size_in_dw
;
302 void compute_memory_free(struct compute_memory_pool
* pool
, int64_t id
)
304 struct compute_memory_item
*item
, *next
;
306 for (item
= pool
->item_list
; item
; item
= next
) {
309 if (item
->id
== id
) {
311 item
->prev
->next
= item
->next
;
314 pool
->item_list
= item
->next
;
318 item
->next
->prev
= item
->prev
;
327 fprintf(stderr
, "Internal error, invalid id %ld "
328 "for compute_memory_free\n", id
);
330 assert(0 && "error");
334 * Creates pending allocations
336 struct compute_memory_item
* compute_memory_alloc(
337 struct compute_memory_pool
* pool
,
340 struct compute_memory_item
*new_item
;
342 COMPUTE_DBG("Alloc: %i\n", size_in_dw
);
344 new_item
= (struct compute_memory_item
*)
345 CALLOC(sizeof(struct compute_memory_item
), 1);
346 new_item
->size_in_dw
= size_in_dw
;
347 new_item
->start_in_dw
= -1; /* mark pending */
348 new_item
->id
= pool
->next_id
++;
349 new_item
->pool
= pool
;
351 struct compute_memory_item
*last_item
;
353 if (pool
->item_list
) {
354 for (last_item
= pool
->item_list
; last_item
->next
;
355 last_item
= last_item
->next
);
357 last_item
->next
= new_item
;
358 new_item
->prev
= last_item
;
361 pool
->item_list
= new_item
;
368 * Transfer data host<->device, offset and size is in bytes
370 void compute_memory_transfer(
371 struct compute_memory_pool
* pool
,
372 struct pipe_context
* pipe
,
374 struct compute_memory_item
* chunk
,
379 int64_t aligned_size
= pool
->size_in_dw
;
380 struct pipe_resource
* gart
= (struct pipe_resource
*)pool
->bo
;
381 int64_t internal_offset
= chunk
->start_in_dw
*4 + offset_in_chunk
;
383 struct pipe_transfer
*xfer
;
388 xfer
= pipe
->get_transfer(pipe
, gart
, 0, PIPE_TRANSFER_READ
,
389 &(struct pipe_box
) { .width
= aligned_size
,
390 .height
= 1, .depth
= 1 });
392 map
= pipe
->transfer_map(pipe
, xfer
);
394 memcpy(data
, map
+ internal_offset
, size
);
395 pipe
->transfer_unmap(pipe
, xfer
);
396 pipe
->transfer_destroy(pipe
, xfer
);
398 xfer
= pipe
->get_transfer(pipe
, gart
, 0, PIPE_TRANSFER_WRITE
,
399 &(struct pipe_box
) { .width
= aligned_size
,
400 .height
= 1, .depth
= 1 });
402 map
= pipe
->transfer_map(pipe
, xfer
);
404 memcpy(map
+ internal_offset
, data
, size
);
405 pipe
->transfer_unmap(pipe
, xfer
);
406 pipe
->transfer_destroy(pipe
, xfer
);
411 * Transfer data between chunk<->data, it is for VRAM<->GART transfers
413 void compute_memory_transfer_direct(
414 struct compute_memory_pool
* pool
,
416 struct compute_memory_item
* chunk
,
417 struct r600_resource
* data
,