04aaac92a4aa24c9498304f433a4e4ae41d65b91
[mesa.git] / src / gallium / drivers / r600 / compute_memory_pool.c
1 /*
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:
8 *
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
11 * Software.
12 *
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.
20 *
21 * Authors:
22 * Adam Rak <adam.rak@streamnovation.com>
23 */
24
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"
43 #include <inttypes.h>
44
45 #define ITEM_ALIGNMENT 1024
46 /**
47 * Creates a new pool
48 */
49 struct compute_memory_pool* compute_memory_pool_new(
50 struct r600_screen * rscreen)
51 {
52 struct compute_memory_pool* pool = (struct compute_memory_pool*)
53 CALLOC(sizeof(struct compute_memory_pool), 1);
54 if (pool == NULL)
55 return NULL;
56
57 COMPUTE_DBG(rscreen, "* compute_memory_pool_new()\n");
58
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);
66 return pool;
67 }
68
69 static void compute_memory_pool_init(struct compute_memory_pool * pool,
70 unsigned initial_size_in_dw)
71 {
72
73 COMPUTE_DBG(pool->screen, "* compute_memory_pool_init() initial_size_in_dw = %ld\n",
74 initial_size_in_dw);
75
76 pool->shadow = (uint32_t*)CALLOC(initial_size_in_dw, 4);
77 if (pool->shadow == NULL)
78 return;
79
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);
83 }
84
85 /**
86 * Frees all stuff in the pool and the pool struct itself too
87 */
88 void compute_memory_pool_delete(struct compute_memory_pool* pool)
89 {
90 COMPUTE_DBG(pool->screen, "* compute_memory_pool_delete()\n");
91 free(pool->shadow);
92 if (pool->bo) {
93 pool->screen->b.b.resource_destroy((struct pipe_screen *)
94 pool->screen, (struct pipe_resource *)pool->bo);
95 }
96 free(pool);
97 }
98
99 /**
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.
102 */
103 int64_t compute_memory_prealloc_chunk(
104 struct compute_memory_pool* pool,
105 int64_t size_in_dw)
106 {
107 struct compute_memory_item *item;
108
109 int last_end = 0;
110
111 assert(size_in_dw <= pool->size_in_dw);
112
113 COMPUTE_DBG(pool->screen, "* compute_memory_prealloc_chunk() size_in_dw = %ld\n",
114 size_in_dw);
115
116 LIST_FOR_EACH_ENTRY(item, pool->item_list, link) {
117 if (last_end + size_in_dw <= item->start_in_dw) {
118 return last_end;
119 }
120
121 last_end = item->start_in_dw + align(item->size_in_dw, ITEM_ALIGNMENT);
122 }
123
124 if (pool->size_in_dw - last_end < size_in_dw) {
125 return -1;
126 }
127
128 return last_end;
129 }
130
131 /**
132 * Search for the chunk where we can link our new chunk after it.
133 */
134 struct list_head *compute_memory_postalloc_chunk(
135 struct compute_memory_pool* pool,
136 int64_t start_in_dw)
137 {
138 struct compute_memory_item *item;
139 struct compute_memory_item *next;
140 struct list_head *next_link;
141
142 COMPUTE_DBG(pool->screen, "* compute_memory_postalloc_chunck() start_in_dw = %ld\n",
143 start_in_dw);
144
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;
149 }
150
151 LIST_FOR_EACH_ENTRY(item, pool->item_list, link) {
152 next_link = item->link.next;
153
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) {
158 return &item->link;
159 }
160 }
161 else {
162 /* end of chain */
163 assert(item->start_in_dw < start_in_dw);
164 return &item->link;
165 }
166 }
167
168 assert(0 && "unreachable");
169 return NULL;
170 }
171
172 /**
173 * Reallocates pool, conserves data.
174 * @returns -1 if it fails, 0 otherwise
175 */
176 int compute_memory_grow_pool(struct compute_memory_pool* pool,
177 struct pipe_context * pipe, int new_size_in_dw)
178 {
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);
182
183 assert(new_size_in_dw >= pool->size_in_dw);
184
185 if (!pool->bo) {
186 compute_memory_pool_init(pool, MAX2(new_size_in_dw, 1024 * 16));
187 if (pool->shadow == NULL)
188 return -1;
189 } else {
190 new_size_in_dw = align(new_size_in_dw, ITEM_ALIGNMENT);
191
192 COMPUTE_DBG(pool->screen, " Aligned size = %d (%d bytes)\n",
193 new_size_in_dw, new_size_in_dw * 4);
194
195 compute_memory_shadow(pool, pipe, 1);
196 pool->shadow = realloc(pool->shadow, new_size_in_dw*4);
197 if (pool->shadow == NULL)
198 return -1;
199
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(
205 pool->screen,
206 pool->size_in_dw * 4);
207 compute_memory_shadow(pool, pipe, 0);
208 }
209
210 return 0;
211 }
212
213 /**
214 * Copy pool from device to host, or host to device.
215 */
216 void compute_memory_shadow(struct compute_memory_pool* pool,
217 struct pipe_context * pipe, int device_to_host)
218 {
219 struct compute_memory_item chunk;
220
221 COMPUTE_DBG(pool->screen, "* compute_memory_shadow() device_to_host = %d\n",
222 device_to_host);
223
224 chunk.id = 0;
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);
229 }
230
231 /**
232 * Allocates pending allocations in the pool
233 * @returns -1 if it fails, 0 otherwise
234 */
235 int compute_memory_finalize_pending(struct compute_memory_pool* pool,
236 struct pipe_context * pipe)
237 {
238 struct compute_memory_item *item, *next;
239
240 int64_t allocated = 0;
241 int64_t unallocated = 0;
242 int64_t last_pos;
243
244 int err = 0;
245
246 COMPUTE_DBG(pool->screen, "* compute_memory_finalize_pending()\n");
247
248 LIST_FOR_EACH_ENTRY(item, pool->item_list, link) {
249 COMPUTE_DBG(pool->screen, " + list: offset = %i id = %i size = %i "
250 "(%i bytes)\n",item->start_in_dw, item->id,
251 item->size_in_dw, item->size_in_dw * 4);
252 }
253
254 /* Calculate the total allocated size */
255 LIST_FOR_EACH_ENTRY(item, pool->item_list, link) {
256 allocated += align(item->size_in_dw, ITEM_ALIGNMENT);
257 }
258
259 /* Calculate the total unallocated size of the items that
260 * will be promoted to the pool */
261 LIST_FOR_EACH_ENTRY(item, pool->unallocated_list, link) {
262 if (item->status & ITEM_FOR_PROMOTING)
263 unallocated += align(item->size_in_dw, ITEM_ALIGNMENT);
264 }
265
266 if (unallocated == 0) {
267 return 0;
268 }
269
270 if (pool->status & POOL_FRAGMENTED) {
271 compute_memory_defrag(pool, pipe);
272 }
273
274 if (pool->size_in_dw < allocated + unallocated) {
275 err = compute_memory_grow_pool(pool, pipe, allocated + unallocated);
276 if (err == -1)
277 return -1;
278 }
279
280 /* After defragmenting the pool, allocated is equal to the first available
281 * position for new items in the pool */
282 last_pos = allocated;
283
284 /* Loop through all the unallocated items, check if they are marked
285 * for promoting, allocate space for them and add them to the item_list. */
286 LIST_FOR_EACH_ENTRY_SAFE(item, next, pool->unallocated_list, link) {
287 if (item->status & ITEM_FOR_PROMOTING) {
288 err = compute_memory_promote_item(pool, item, pipe, last_pos);
289 item->status &= ~ITEM_FOR_PROMOTING;
290
291 last_pos += align(item->size_in_dw, ITEM_ALIGNMENT);
292
293 if (err == -1)
294 return -1;
295 }
296 }
297
298 return 0;
299 }
300
301 /**
302 * Defragments the pool, so that there's no gap between items.
303 * \param pool The pool to be defragmented
304 */
305 void compute_memory_defrag(struct compute_memory_pool *pool,
306 struct pipe_context *pipe)
307 {
308 struct compute_memory_item *item;
309 int64_t last_pos;
310
311 COMPUTE_DBG(pool->screen, "* compute_memory_defrag()\n");
312
313 last_pos = 0;
314 LIST_FOR_EACH_ENTRY(item, pool->item_list, link) {
315 if (item->start_in_dw != last_pos) {
316 assert(last_pos < item->start_in_dw);
317
318 compute_memory_move_item(pool, item, last_pos, pipe);
319 }
320
321 last_pos += align(item->size_in_dw, ITEM_ALIGNMENT);
322 }
323
324 pool->status &= ~POOL_FRAGMENTED;
325 }
326
327 int compute_memory_promote_item(struct compute_memory_pool *pool,
328 struct compute_memory_item *item, struct pipe_context *pipe,
329 int64_t start_in_dw)
330 {
331 struct pipe_screen *screen = (struct pipe_screen *)pool->screen;
332 struct r600_context *rctx = (struct r600_context *)pipe;
333 struct pipe_resource *src = (struct pipe_resource *)item->real_buffer;
334 struct pipe_resource *dst = (struct pipe_resource *)pool->bo;
335 struct pipe_box box;
336
337 COMPUTE_DBG(pool->screen, " + Found space for Item %p id = %u "
338 "start_in_dw = %u (%u bytes) size_in_dw = %u (%u bytes)\n",
339 item, item->id, start_in_dw, start_in_dw * 4,
340 item->size_in_dw, item->size_in_dw * 4);
341
342 /* Remove the item from the unallocated list */
343 list_del(&item->link);
344
345 /* Add it back to the item_list */
346 list_addtail(&item->link, pool->item_list);
347 item->start_in_dw = start_in_dw;
348
349 if (src != NULL) {
350 u_box_1d(0, item->size_in_dw * 4, &box);
351
352 rctx->b.b.resource_copy_region(pipe,
353 dst, 0, item->start_in_dw * 4, 0 ,0,
354 src, 0, &box);
355
356 /* We check if the item is mapped for reading.
357 * In this case, we need to keep the temporary buffer 'alive'
358 * because it is possible to keep a map active for reading
359 * while a kernel (that reads from it) executes */
360 if (!(item->status & ITEM_MAPPED_FOR_READING)) {
361 pool->screen->b.b.resource_destroy(screen, src);
362 item->real_buffer = NULL;
363 }
364 }
365
366 return 0;
367 }
368
369 void compute_memory_demote_item(struct compute_memory_pool *pool,
370 struct compute_memory_item *item, struct pipe_context *pipe)
371 {
372 struct r600_context *rctx = (struct r600_context *)pipe;
373 struct pipe_resource *src = (struct pipe_resource *)pool->bo;
374 struct pipe_resource *dst;
375 struct pipe_box box;
376
377 /* First, we remove the item from the item_list */
378 list_del(&item->link);
379
380 /* Now we add it to the unallocated list */
381 list_addtail(&item->link, pool->unallocated_list);
382
383 /* We check if the intermediate buffer exists, and if it
384 * doesn't, we create it again */
385 if (item->real_buffer == NULL) {
386 item->real_buffer = (struct r600_resource*)r600_compute_buffer_alloc_vram(
387 pool->screen, item->size_in_dw * 4);
388 }
389
390 dst = (struct pipe_resource *)item->real_buffer;
391
392 /* We transfer the memory from the item in the pool to the
393 * temporary buffer */
394 u_box_1d(item->start_in_dw * 4, item->size_in_dw * 4, &box);
395
396 rctx->b.b.resource_copy_region(pipe,
397 dst, 0, 0, 0, 0,
398 src, 0, &box);
399
400 /* Remember to mark the buffer as 'pending' by setting start_in_dw to -1 */
401 item->start_in_dw = -1;
402
403 if (item->link.next != pool->item_list) {
404 pool->status |= POOL_FRAGMENTED;
405 }
406 }
407
408 /**
409 * Moves the item \a item forward in the pool to \a new_start_in_dw
410 *
411 * This function assumes two things:
412 * 1) The item is \b only moved forward
413 * 2) The item \b won't change it's position inside the \a item_list
414 *
415 * \param item The item that will be moved
416 * \param new_start_in_dw The new position of the item in \a item_list
417 * \see compute_memory_defrag
418 */
419 void compute_memory_move_item(struct compute_memory_pool *pool,
420 struct compute_memory_item *item, uint64_t new_start_in_dw,
421 struct pipe_context *pipe)
422 {
423 struct pipe_screen *screen = (struct pipe_screen *)pool->screen;
424 struct r600_context *rctx = (struct r600_context *)pipe;
425 struct pipe_resource *src = (struct pipe_resource *)pool->bo;
426 struct pipe_resource *dst;
427 struct pipe_box box;
428
429 struct compute_memory_item *prev;
430
431 COMPUTE_DBG(pool->screen, "* compute_memory_move_item()\n"
432 " + Moving item %i from %u (%u bytes) to %u (%u bytes)\n",
433 item->id, item->start_in_dw, item->start_in_dw * 4,
434 new_start_in_dw, new_start_in_dw * 4);
435
436 if (pool->item_list != item->link.prev) {
437 prev = container_of(item->link.prev, item, link);
438 assert(prev->start_in_dw + prev->size_in_dw <= new_start_in_dw);
439 }
440
441 u_box_1d(item->start_in_dw * 4, item->size_in_dw * 4, &box);
442
443 /* If the ranges don't overlap, we can just copy the item directly */
444 if (new_start_in_dw + item->size_in_dw <= item->start_in_dw) {
445 dst = (struct pipe_resource *)pool->bo;
446
447 rctx->b.b.resource_copy_region(pipe,
448 dst, 0, new_start_in_dw * 4, 0, 0,
449 src, 0, &box);
450 } else {
451 /* The ranges overlap, we will try first to use an intermediate
452 * resource to move the item */
453 dst = (struct pipe_resource *)r600_compute_buffer_alloc_vram(
454 pool->screen, item->size_in_dw * 4);
455
456 if (dst != NULL) {
457 rctx->b.b.resource_copy_region(pipe,
458 dst, 0, 0, 0, 0,
459 src, 0, &box);
460
461 src = dst;
462 dst = (struct pipe_resource *)pool->bo;
463
464 box.x = 0;
465
466 rctx->b.b.resource_copy_region(pipe,
467 dst, 0, new_start_in_dw * 4, 0, 0,
468 src, 0, &box);
469
470 pool->screen->b.b.resource_destroy(screen, src);
471
472 } else {
473 /* The allocation of the temporary resource failed,
474 * falling back to use mappings */
475 uint32_t *map;
476 int64_t offset;
477 struct pipe_transfer *trans;
478
479 offset = item->start_in_dw - new_start_in_dw;
480
481 u_box_1d(new_start_in_dw * 4, (offset + item->size_in_dw) * 4, &box);
482
483 map = pipe->transfer_map(pipe, src, 0, PIPE_TRANSFER_READ_WRITE,
484 &box, &trans);
485
486 assert(map);
487 assert(trans);
488
489 memmove(map, map + offset, item->size_in_dw * 4);
490
491 pipe->transfer_unmap(pipe, trans);
492 }
493 }
494
495 item->start_in_dw = new_start_in_dw;
496 }
497
498 void compute_memory_free(struct compute_memory_pool* pool, int64_t id)
499 {
500 struct compute_memory_item *item, *next;
501 struct pipe_screen *screen = (struct pipe_screen *)pool->screen;
502 struct pipe_resource *res;
503
504 COMPUTE_DBG(pool->screen, "* compute_memory_free() id + %ld \n", id);
505
506 LIST_FOR_EACH_ENTRY_SAFE(item, next, pool->item_list, link) {
507
508 if (item->id == id) {
509
510 if (item->link.next != pool->item_list) {
511 pool->status |= POOL_FRAGMENTED;
512 }
513
514 list_del(&item->link);
515
516 if (item->real_buffer) {
517 res = (struct pipe_resource *)item->real_buffer;
518 pool->screen->b.b.resource_destroy(
519 screen, res);
520 }
521
522 free(item);
523
524 return;
525 }
526 }
527
528 LIST_FOR_EACH_ENTRY_SAFE(item, next, pool->unallocated_list, link) {
529
530 if (item->id == id) {
531 list_del(&item->link);
532
533 if (item->real_buffer) {
534 res = (struct pipe_resource *)item->real_buffer;
535 pool->screen->b.b.resource_destroy(
536 screen, res);
537 }
538
539 free(item);
540
541 return;
542 }
543 }
544
545 fprintf(stderr, "Internal error, invalid id %"PRIi64" "
546 "for compute_memory_free\n", id);
547
548 assert(0 && "error");
549 }
550
551 /**
552 * Creates pending allocations
553 */
554 struct compute_memory_item* compute_memory_alloc(
555 struct compute_memory_pool* pool,
556 int64_t size_in_dw)
557 {
558 struct compute_memory_item *new_item = NULL;
559
560 COMPUTE_DBG(pool->screen, "* compute_memory_alloc() size_in_dw = %ld (%ld bytes)\n",
561 size_in_dw, 4 * size_in_dw);
562
563 new_item = (struct compute_memory_item *)
564 CALLOC(sizeof(struct compute_memory_item), 1);
565 if (new_item == NULL)
566 return NULL;
567
568 new_item->size_in_dw = size_in_dw;
569 new_item->start_in_dw = -1; /* mark pending */
570 new_item->id = pool->next_id++;
571 new_item->pool = pool;
572 new_item->real_buffer = NULL;
573
574 list_addtail(&new_item->link, pool->unallocated_list);
575
576 COMPUTE_DBG(pool->screen, " + Adding item %p id = %u size = %u (%u bytes)\n",
577 new_item, new_item->id, new_item->size_in_dw,
578 new_item->size_in_dw * 4);
579 return new_item;
580 }
581
582 /**
583 * Transfer data host<->device, offset and size is in bytes
584 */
585 void compute_memory_transfer(
586 struct compute_memory_pool* pool,
587 struct pipe_context * pipe,
588 int device_to_host,
589 struct compute_memory_item* chunk,
590 void* data,
591 int offset_in_chunk,
592 int size)
593 {
594 int64_t aligned_size = pool->size_in_dw;
595 struct pipe_resource* gart = (struct pipe_resource*)pool->bo;
596 int64_t internal_offset = chunk->start_in_dw*4 + offset_in_chunk;
597
598 struct pipe_transfer *xfer;
599 uint32_t *map;
600
601 assert(gart);
602
603 COMPUTE_DBG(pool->screen, "* compute_memory_transfer() device_to_host = %d, "
604 "offset_in_chunk = %d, size = %d\n", device_to_host,
605 offset_in_chunk, size);
606
607 if (device_to_host) {
608 map = pipe->transfer_map(pipe, gart, 0, PIPE_TRANSFER_READ,
609 &(struct pipe_box) { .width = aligned_size * 4,
610 .height = 1, .depth = 1 }, &xfer);
611 assert(xfer);
612 assert(map);
613 memcpy(data, map + internal_offset, size);
614 pipe->transfer_unmap(pipe, xfer);
615 } else {
616 map = pipe->transfer_map(pipe, gart, 0, PIPE_TRANSFER_WRITE,
617 &(struct pipe_box) { .width = aligned_size * 4,
618 .height = 1, .depth = 1 }, &xfer);
619 assert(xfer);
620 assert(map);
621 memcpy(map + internal_offset, data, size);
622 pipe->transfer_unmap(pipe, xfer);
623 }
624 }
625
626 /**
627 * Transfer data between chunk<->data, it is for VRAM<->GART transfers
628 */
629 void compute_memory_transfer_direct(
630 struct compute_memory_pool* pool,
631 int chunk_to_data,
632 struct compute_memory_item* chunk,
633 struct r600_resource* data,
634 int offset_in_chunk,
635 int offset_in_data,
636 int size)
637 {
638 ///TODO: DMA
639 }