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