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