amdgpu: add amdgpu_bo_va_op for va map/unmap support v3
[platform/upstream/libdrm.git] / amdgpu / amdgpu_internal.h
1 /*
2  * Copyright © 2014 Advanced Micro Devices, Inc.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  */
23
24 #ifndef _AMDGPU_INTERNAL_H_
25 #define _AMDGPU_INTERNAL_H_
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <assert.h>
32 #include <pthread.h>
33 #include "xf86atomic.h"
34 #include "amdgpu.h"
35 #include "util_double_list.h"
36
37 #define AMDGPU_CS_MAX_RINGS 8
38 /* do not use below macro if b is not power of 2 aligned value */
39 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
40 #define ROUND_UP(x, y) ((((x)-1) | __round_mask(x, y))+1)
41 #define ROUND_DOWN(x, y) ((x) & ~__round_mask(x, y))
42
43 #define AMDGPU_INVALID_VA_ADDRESS       0xffffffffffffffff
44
45 struct amdgpu_bo_va_hole {
46         struct list_head list;
47         uint64_t offset;
48         uint64_t size;
49 };
50
51 struct amdgpu_bo_va_mgr {
52         atomic_t refcount;
53         /* the start virtual address */
54         uint64_t va_offset;
55         uint64_t va_max;
56         struct list_head va_holes;
57         pthread_mutex_t bo_va_mutex;
58         uint32_t va_alignment;
59 };
60
61 struct amdgpu_va {
62         amdgpu_device_handle dev;
63         uint64_t address;
64         uint64_t size;
65         enum amdgpu_gpu_va_range range;
66 };
67
68 struct amdgpu_device {
69         atomic_t refcount;
70         int fd;
71         int flink_fd;
72         unsigned major_version;
73         unsigned minor_version;
74
75         /** List of buffer handles. Protected by bo_table_mutex. */
76         struct util_hash_table *bo_handles;
77         /** List of buffer GEM flink names. Protected by bo_table_mutex. */
78         struct util_hash_table *bo_flink_names;
79         /** This protects all hash tables. */
80         pthread_mutex_t bo_table_mutex;
81         struct drm_amdgpu_info_device dev_info;
82         struct amdgpu_gpu_info info;
83         struct amdgpu_bo_va_mgr *vamgr;
84 };
85
86 struct amdgpu_bo {
87         atomic_t refcount;
88         struct amdgpu_device *dev;
89
90         uint64_t alloc_size;
91
92         uint32_t handle;
93         uint32_t flink_name;
94
95         pthread_mutex_t cpu_access_mutex;
96         void *cpu_ptr;
97         int cpu_map_count;
98 };
99
100 struct amdgpu_bo_list {
101         struct amdgpu_device *dev;
102
103         uint32_t handle;
104 };
105
106 struct amdgpu_context {
107         struct amdgpu_device *dev;
108         /** Mutex for accessing fences and to maintain command submissions
109             in good sequence. */
110         pthread_mutex_t sequence_mutex;
111         /* context id*/
112         uint32_t id;
113 };
114
115 /**
116  * Functions.
117  */
118
119 void amdgpu_device_free_internal(amdgpu_device_handle dev);
120
121 void amdgpu_bo_free_internal(amdgpu_bo_handle bo);
122
123 struct amdgpu_bo_va_mgr* amdgpu_vamgr_get_global(struct amdgpu_device *dev);
124
125 void amdgpu_vamgr_reference(struct amdgpu_bo_va_mgr **dst, struct amdgpu_bo_va_mgr *src);
126
127 uint64_t amdgpu_vamgr_find_va(struct amdgpu_bo_va_mgr *mgr, uint64_t size,
128                                 uint64_t alignment, uint64_t base_required);
129
130 void amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va, 
131                                 uint64_t size);
132
133 int amdgpu_query_gpu_info_init(amdgpu_device_handle dev);
134
135 uint64_t amdgpu_cs_calculate_timeout(uint64_t timeout);
136
137 /**
138  * Inline functions.
139  */
140
141 /**
142  * Increment src and decrement dst as if we were updating references
143  * for an assignment between 2 pointers of some objects.
144  *
145  * \return  true if dst is 0
146  */
147 static inline bool update_references(atomic_t *dst, atomic_t *src)
148 {
149         if (dst != src) {
150                 /* bump src first */
151                 if (src) {
152                         assert(atomic_read(src) > 0);
153                         atomic_inc(src);
154                 }
155                 if (dst) {
156                         assert(atomic_read(dst) > 0);
157                         return atomic_dec_and_test(dst);
158                 }
159         }
160         return false;
161 }
162
163 /**
164  * Assignment between two amdgpu_bo pointers with reference counting.
165  *
166  * Usage:
167  *    struct amdgpu_bo *dst = ... , *src = ...;
168  *
169  *    dst = src;
170  *    // No reference counting. Only use this when you need to move
171  *    // a reference from one pointer to another.
172  *
173  *    amdgpu_bo_reference(&dst, src);
174  *    // Reference counters are updated. dst is decremented and src is
175  *    // incremented. dst is freed if its reference counter is 0.
176  */
177 static inline void amdgpu_bo_reference(struct amdgpu_bo **dst,
178                                         struct amdgpu_bo *src)
179 {
180         if (update_references(&(*dst)->refcount, &src->refcount))
181                 amdgpu_bo_free_internal(*dst);
182         *dst = src;
183 }
184
185 /**
186  * Assignment between two amdgpu_device pointers with reference counting.
187  *
188  * Usage:
189  *    struct amdgpu_device *dst = ... , *src = ...;
190  *
191  *    dst = src;
192  *    // No reference counting. Only use this when you need to move
193  *    // a reference from one pointer to another.
194  *
195  *    amdgpu_device_reference(&dst, src);
196  *    // Reference counters are updated. dst is decremented and src is
197  *    // incremented. dst is freed if its reference counter is 0.
198  */
199 void amdgpu_device_reference(struct amdgpu_device **dst,
200                              struct amdgpu_device *src);
201 #endif