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#ifndef COMPUTE_MEMORY_POOL
26#define COMPUTE_MEMORY_POOL
27
28#include <stdlib.h>
29
30struct compute_memory_pool;
31
32struct compute_memory_item
33{
34	int64_t id; ///ID of the memory chunk
35
36	int untouched; ///True if the memory contains only junk, no need to save it for defrag
37
38	int64_t start_in_dw; ///Start pointer in dwords relative in the pool bo
39	int64_t size_in_dw; ///Size of the chunk in dwords
40
41	struct compute_memory_pool* pool;
42
43	struct compute_memory_item* prev;
44	struct compute_memory_item* next;
45};
46
47struct compute_memory_pool
48{
49	int64_t next_id; ///For generating unique IDs for memory chunks
50	int64_t size_in_dw; ///Size of the pool in dwords
51
52	struct r600_resource *bo; ///The pool buffer object resource
53	struct compute_memory_item* item_list; ///Allocated memory chunks in the buffer,they must be ordered by "start_in_dw"
54	struct r600_screen *screen;
55
56	uint32_t *shadow; ///host copy of the pool, used for defragmentation
57};
58
59
60struct compute_memory_pool* compute_memory_pool_new(struct r600_screen *rscreen); ///Creates a new pool
61void compute_memory_pool_delete(struct compute_memory_pool* pool); ///Frees all stuff in the pool and the pool struct itself too
62
63int64_t compute_memory_prealloc_chunk(struct compute_memory_pool* pool, int64_t size_in_dw); ///searches for an empty space in the pool, return with the pointer to the allocatable space in the pool, returns -1 on failure
64
65struct compute_memory_item* compute_memory_postalloc_chunk(struct compute_memory_pool* pool, int64_t start_in_dw); ///search for the chunk where we can link our new chunk after it
66
67/**
68 * reallocates pool, conserves data
69 */
70void compute_memory_grow_pool(struct compute_memory_pool* pool, struct pipe_context * pipe,
71	int new_size_in_dw);
72
73/**
74 * Copy pool from device to host, or host to device
75 */
76void compute_memory_shadow(struct compute_memory_pool* pool,
77	struct pipe_context * pipe, int device_to_host);
78
79/**
80 * Allocates pending allocations in the pool
81 */
82void compute_memory_finalize_pending(struct compute_memory_pool* pool,
83	struct pipe_context * pipe);
84void compute_memory_defrag(struct compute_memory_pool* pool); ///Defragment the memory pool, always heavy memory usage
85void compute_memory_free(struct compute_memory_pool* pool, int64_t id);
86struct compute_memory_item* compute_memory_alloc(struct compute_memory_pool* pool, int64_t size_in_dw); ///Creates pending allocations
87
88/**
89 * Transfer data host<->device, offset and size is in bytes
90 */
91void compute_memory_transfer(struct compute_memory_pool* pool,
92	struct pipe_context * pipe, int device_to_host,
93	struct compute_memory_item* chunk, void* data,
94	int offset_in_chunk, int size);
95
96void compute_memory_transfer_direct(struct compute_memory_pool* pool, int chunk_to_data, struct compute_memory_item* chunk, struct r600_resource* data, int offset_in_chunk, int offset_in_data, int size); ///Transfer data between chunk<->data, it is for VRAM<->GART transfers
97
98#endif
99