2 * Copyright © 2014 Advanced Micro Devices, Inc.
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
25 #ifndef _AMDGPU_INTERNAL_H_
26 #define _AMDGPU_INTERNAL_H_
35 #include "libdrm_macros.h"
36 #include "xf86atomic.h"
38 #include "util_double_list.h"
40 #define AMDGPU_CS_MAX_RINGS 8
41 /* do not use below macro if b is not power of 2 aligned value */
42 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
43 #define ROUND_UP(x, y) ((((x)-1) | __round_mask(x, y))+1)
44 #define ROUND_DOWN(x, y) ((x) & ~__round_mask(x, y))
46 #define AMDGPU_INVALID_VA_ADDRESS 0xffffffffffffffff
48 struct amdgpu_bo_va_hole {
49 struct list_head list;
54 struct amdgpu_bo_va_mgr {
56 /* the start virtual address */
59 struct list_head va_holes;
60 pthread_mutex_t bo_va_mutex;
61 uint32_t va_alignment;
65 amdgpu_device_handle dev;
68 enum amdgpu_gpu_va_range range;
71 struct amdgpu_device {
75 unsigned major_version;
76 unsigned minor_version;
78 /** List of buffer handles. Protected by bo_table_mutex. */
79 struct util_hash_table *bo_handles;
80 /** List of buffer GEM flink names. Protected by bo_table_mutex. */
81 struct util_hash_table *bo_flink_names;
82 /** This protects all hash tables. */
83 pthread_mutex_t bo_table_mutex;
84 struct drm_amdgpu_info_device dev_info;
85 struct amdgpu_gpu_info info;
86 struct amdgpu_bo_va_mgr *vamgr;
91 struct amdgpu_device *dev;
98 pthread_mutex_t cpu_access_mutex;
103 struct amdgpu_bo_list {
104 struct amdgpu_device *dev;
109 struct amdgpu_context {
110 struct amdgpu_device *dev;
111 /** Mutex for accessing fences and to maintain command submissions
113 pthread_mutex_t sequence_mutex;
122 drm_private void amdgpu_bo_free_internal(amdgpu_bo_handle bo);
124 drm_private struct amdgpu_bo_va_mgr*
125 amdgpu_vamgr_get_global(struct amdgpu_device *dev);
128 amdgpu_vamgr_reference(struct amdgpu_bo_va_mgr **dst,
129 struct amdgpu_bo_va_mgr *src);
132 amdgpu_vamgr_find_va(struct amdgpu_bo_va_mgr *mgr, uint64_t size,
133 uint64_t alignment, uint64_t base_required);
136 amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va, uint64_t size);
138 drm_private int amdgpu_query_gpu_info_init(amdgpu_device_handle dev);
140 drm_private uint64_t amdgpu_cs_calculate_timeout(uint64_t timeout);
147 * Increment src and decrement dst as if we were updating references
148 * for an assignment between 2 pointers of some objects.
150 * \return true if dst is 0
152 static inline bool update_references(atomic_t *dst, atomic_t *src)
157 assert(atomic_read(src) > 0);
161 assert(atomic_read(dst) > 0);
162 return atomic_dec_and_test(dst);
169 * Assignment between two amdgpu_bo pointers with reference counting.
172 * struct amdgpu_bo *dst = ... , *src = ...;
175 * // No reference counting. Only use this when you need to move
176 * // a reference from one pointer to another.
178 * amdgpu_bo_reference(&dst, src);
179 * // Reference counters are updated. dst is decremented and src is
180 * // incremented. dst is freed if its reference counter is 0.
182 static inline void amdgpu_bo_reference(struct amdgpu_bo **dst,
183 struct amdgpu_bo *src)
185 if (update_references(&(*dst)->refcount, &src->refcount))
186 amdgpu_bo_free_internal(*dst);