OSDN Git Service

amdgpu: add IB sharing support v2
[android-x86/external-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         /* the start virtual address */
53         uint64_t va_offset;
54         uint64_t va_max;
55         struct list_head va_holes;
56         pthread_mutex_t bo_va_mutex;
57         uint32_t va_alignment;
58 };
59
60 struct amdgpu_device {
61         atomic_t refcount;
62         int fd;
63         int flink_fd;
64         unsigned major_version;
65         unsigned minor_version;
66
67         /** List of buffer handles. Protected by bo_table_mutex. */
68         struct util_hash_table *bo_handles;
69         /** List of buffer GEM flink names. Protected by bo_table_mutex. */
70         struct util_hash_table *bo_flink_names;
71         /** This protects all hash tables. */
72         pthread_mutex_t bo_table_mutex;
73         struct amdgpu_bo_va_mgr vamgr;
74         struct drm_amdgpu_info_device dev_info;
75         struct amdgpu_gpu_info info;
76 };
77
78 struct amdgpu_bo {
79         atomic_t refcount;
80         struct amdgpu_device *dev;
81
82         uint64_t alloc_size;
83         uint64_t virtual_mc_base_address;
84
85         uint32_t handle;
86         uint32_t flink_name;
87
88         pthread_mutex_t cpu_access_mutex;
89         void *cpu_ptr;
90         int cpu_map_count;
91 };
92
93 struct amdgpu_bo_list {
94         struct amdgpu_device *dev;
95
96         uint32_t handle;
97 };
98
99 /*
100  * There are three mutexes.
101  * To avoid deadlock, only hold the mutexes in this order:
102  * sequence_mutex -> pendings_mutex -> pool_mutex.
103 */
104 struct amdgpu_context {
105         struct amdgpu_device *dev;
106         /** Mutex for accessing fences and to maintain command submissions
107             and pending lists in good sequence. */
108         pthread_mutex_t sequence_mutex;
109         /** Buffer for user fences */
110         struct amdgpu_ib *fence_ib;
111         /** The newest expired fence for the ring of the ip blocks. */
112         uint64_t expired_fences[AMDGPU_HW_IP_NUM][AMDGPU_HW_IP_INSTANCE_MAX_COUNT][AMDGPU_CS_MAX_RINGS];
113         /** Mutex for accessing pendings list. */
114         pthread_mutex_t pendings_mutex;
115         /** Pending IBs. */
116         struct list_head pendings[AMDGPU_HW_IP_NUM][AMDGPU_HW_IP_INSTANCE_MAX_COUNT][AMDGPU_CS_MAX_RINGS];
117         /** Freed IBs not yet in pool */
118         struct list_head freed;
119         /** Mutex for accessing free ib pool. */
120         pthread_mutex_t pool_mutex;
121         /** Internal free IB pools. */
122         struct list_head ib_pools[AMDGPU_CS_IB_SIZE_NUM];
123         /* context id*/
124         uint32_t id;
125 };
126
127 struct amdgpu_ib {
128         amdgpu_context_handle context;
129         struct list_head list_node;
130         amdgpu_bo_handle buf_handle;
131         void *cpu;
132         uint64_t virtual_mc_base_address;
133         enum amdgpu_cs_ib_size ib_size;
134         uint64_t cs_handle;
135 };
136
137 /**
138  * Functions.
139  */
140
141 void amdgpu_device_free_internal(amdgpu_device_handle dev);
142
143 void amdgpu_bo_free_internal(amdgpu_bo_handle bo);
144
145 void amdgpu_vamgr_init(struct amdgpu_device *dev);
146
147 uint64_t amdgpu_vamgr_find_va(struct amdgpu_bo_va_mgr *mgr,
148                                uint64_t size, uint64_t alignment);
149
150 void amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va,
151                            uint64_t size);
152
153 int amdgpu_query_gpu_info_init(amdgpu_device_handle dev);
154
155 uint64_t amdgpu_cs_calculate_timeout(uint64_t timeout);
156
157 /**
158  * Inline functions.
159  */
160
161 /**
162  * Increment src and decrement dst as if we were updating references
163  * for an assignment between 2 pointers of some objects.
164  *
165  * \return  true if dst is 0
166  */
167 static inline bool update_references(atomic_t *dst, atomic_t *src)
168 {
169         if (dst != src) {
170                 /* bump src first */
171                 if (src) {
172                         assert(atomic_read(src) > 0);
173                         atomic_inc(src);
174                 }
175                 if (dst) {
176                         assert(atomic_read(dst) > 0);
177                         return atomic_dec_and_test(dst);
178                 }
179         }
180         return false;
181 }
182
183 /**
184  * Assignment between two amdgpu_bo pointers with reference counting.
185  *
186  * Usage:
187  *    struct amdgpu_bo *dst = ... , *src = ...;
188  *
189  *    dst = src;
190  *    // No reference counting. Only use this when you need to move
191  *    // a reference from one pointer to another.
192  *
193  *    amdgpu_bo_reference(&dst, src);
194  *    // Reference counters are updated. dst is decremented and src is
195  *    // incremented. dst is freed if its reference counter is 0.
196  */
197 static inline void amdgpu_bo_reference(struct amdgpu_bo **dst,
198                                         struct amdgpu_bo *src)
199 {
200         if (update_references(&(*dst)->refcount, &src->refcount))
201                 amdgpu_bo_free_internal(*dst);
202         *dst = src;
203 }
204
205 /**
206  * Assignment between two amdgpu_device pointers with reference counting.
207  *
208  * Usage:
209  *    struct amdgpu_device *dst = ... , *src = ...;
210  *
211  *    dst = src;
212  *    // No reference counting. Only use this when you need to move
213  *    // a reference from one pointer to another.
214  *
215  *    amdgpu_device_reference(&dst, src);
216  *    // Reference counters are updated. dst is decremented and src is
217  *    // incremented. dst is freed if its reference counter is 0.
218  */
219 void amdgpu_device_reference(struct amdgpu_device **dst,
220                              struct amdgpu_device *src);
221 #endif