blob: e68246bf97b6a11ee80f6e523d7317efa5c91574 [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.
Emil Velikova30da8e2015-08-07 17:20:51 +010022 *
Alex Deucher09361392015-04-20 12:04:22 -040023 */
24
25#ifndef _AMDGPU_INTERNAL_H_
26#define _AMDGPU_INTERNAL_H_
27
28#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
31
32#include <assert.h>
33#include <pthread.h>
Emil Velikovb4718182015-08-07 16:54:29 +010034
35#include "libdrm_macros.h"
Alex Deucher09361392015-04-20 12:04:22 -040036#include "xf86atomic.h"
37#include "amdgpu.h"
38#include "util_double_list.h"
39
40#define AMDGPU_CS_MAX_RINGS 8
monk.liu2f2c8ac2015-04-23 13:18:59 +080041/* do not use below macro if b is not power of 2 aligned value */
Jack Xiao74547792015-05-07 16:07:03 +080042#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))
Alex Deucher09361392015-04-20 12:04:22 -040045
Jammy Zhou241cf6d2015-05-13 01:14:11 +080046#define AMDGPU_INVALID_VA_ADDRESS 0xffffffffffffffff
Ken Wangf884af92016-02-04 13:52:22 +080047#define AMDGPU_NULL_SUBMIT_SEQ 0
Jammy Zhou241cf6d2015-05-13 01:14:11 +080048
Alex Deucher09361392015-04-20 12:04:22 -040049struct amdgpu_bo_va_hole {
50 struct list_head list;
51 uint64_t offset;
52 uint64_t size;
53};
54
55struct amdgpu_bo_va_mgr {
56 /* the start virtual address */
57 uint64_t va_offset;
Jammy Zhou241cf6d2015-05-13 01:14:11 +080058 uint64_t va_max;
Alex Deucher09361392015-04-20 12:04:22 -040059 struct list_head va_holes;
60 pthread_mutex_t bo_va_mutex;
61 uint32_t va_alignment;
62};
63
Sabre Shao23fab592015-07-09 13:50:36 +080064struct amdgpu_va {
65 amdgpu_device_handle dev;
66 uint64_t address;
67 uint64_t size;
68 enum amdgpu_gpu_va_range range;
Jammy Zhouffa305d2015-08-17 11:09:08 +080069 struct amdgpu_bo_va_mgr *vamgr;
Sabre Shao23fab592015-07-09 13:50:36 +080070};
71
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040072struct amdgpu_asic_id {
73 uint32_t did;
74 uint32_t rid;
75 char *marketing_name;
76};
77
Alex Deucher09361392015-04-20 12:04:22 -040078struct amdgpu_device {
79 atomic_t refcount;
80 int fd;
81 int flink_fd;
82 unsigned major_version;
83 unsigned minor_version;
84
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040085 /** Lookup table of asic device id, revision id and marketing name */
86 struct amdgpu_asic_id *asic_ids;
Alex Deucher09361392015-04-20 12:04:22 -040087 /** List of buffer handles. Protected by bo_table_mutex. */
88 struct util_hash_table *bo_handles;
89 /** List of buffer GEM flink names. Protected by bo_table_mutex. */
90 struct util_hash_table *bo_flink_names;
Alex Deucher09361392015-04-20 12:04:22 -040091 /** This protects all hash tables. */
92 pthread_mutex_t bo_table_mutex;
Alex Deucher09361392015-04-20 12:04:22 -040093 struct drm_amdgpu_info_device dev_info;
94 struct amdgpu_gpu_info info;
Jammy Zhouffa305d2015-08-17 11:09:08 +080095 /** The global VA manager for the whole virtual address space */
Alex Xiefe7cb342017-01-28 21:50:44 +020096 struct amdgpu_bo_va_mgr vamgr;
Jammy Zhouffa305d2015-08-17 11:09:08 +080097 /** The VA manager for the 32bit address space */
Alex Xie067e9a12017-01-28 21:50:36 +020098 struct amdgpu_bo_va_mgr vamgr_32;
Alex Deucher09361392015-04-20 12:04:22 -040099};
100
101struct amdgpu_bo {
102 atomic_t refcount;
103 struct amdgpu_device *dev;
104
105 uint64_t alloc_size;
Alex Deucher09361392015-04-20 12:04:22 -0400106
107 uint32_t handle;
108 uint32_t flink_name;
109
110 pthread_mutex_t cpu_access_mutex;
111 void *cpu_ptr;
112 int cpu_map_count;
113};
114
Christian König6dc2eaf2015-04-22 14:52:34 +0200115struct amdgpu_bo_list {
116 struct amdgpu_device *dev;
117
118 uint32_t handle;
119};
120
Alex Deucher09361392015-04-20 12:04:22 -0400121struct amdgpu_context {
Christian König9c2afff2015-04-22 12:21:13 +0200122 struct amdgpu_device *dev;
Marek Olšák6afadea2016-01-12 22:13:07 +0100123 /** Mutex for accessing fences and to maintain command submissions
124 in good sequence. */
125 pthread_mutex_t sequence_mutex;
Alex Deucher09361392015-04-20 12:04:22 -0400126 /* context id*/
127 uint32_t id;
Marek Olšák6afadea2016-01-12 22:13:07 +0100128 uint64_t last_seq[AMDGPU_HW_IP_NUM][AMDGPU_HW_IP_INSTANCE_MAX_COUNT][AMDGPU_CS_MAX_RINGS];
129 struct list_head sem_list[AMDGPU_HW_IP_NUM][AMDGPU_HW_IP_INSTANCE_MAX_COUNT][AMDGPU_CS_MAX_RINGS];
130};
131
132/**
133 * Structure describing sw semaphore based on scheduler
134 *
135 */
136struct amdgpu_semaphore {
137 atomic_t refcount;
138 struct list_head list;
139 struct amdgpu_cs_fence signal_fence;
Alex Deucher09361392015-04-20 12:04:22 -0400140};
141
Alex Deucher09361392015-04-20 12:04:22 -0400142/**
143 * Functions.
144 */
145
Emil Velikovbddf4df2015-08-07 17:09:35 +0100146drm_private void amdgpu_bo_free_internal(amdgpu_bo_handle bo);
Alex Deucher09361392015-04-20 12:04:22 -0400147
Jammy Zhouffa305d2015-08-17 11:09:08 +0800148drm_private void amdgpu_vamgr_init(struct amdgpu_bo_va_mgr *mgr, uint64_t start,
149 uint64_t max, uint64_t alignment);
150
151drm_private void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr);
152
Emil Velikovb4718182015-08-07 16:54:29 +0100153drm_private uint64_t
154amdgpu_vamgr_find_va(struct amdgpu_bo_va_mgr *mgr, uint64_t size,
155 uint64_t alignment, uint64_t base_required);
Alex Deucher09361392015-04-20 12:04:22 -0400156
Emil Velikovb4718182015-08-07 16:54:29 +0100157drm_private void
158amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va, uint64_t size);
Alex Deucher09361392015-04-20 12:04:22 -0400159
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400160drm_private int amdgpu_parse_asic_ids(struct amdgpu_asic_id **asic_ids);
161
Emil Velikovbddf4df2015-08-07 17:09:35 +0100162drm_private int amdgpu_query_gpu_info_init(amdgpu_device_handle dev);
Alex Deucher09361392015-04-20 12:04:22 -0400163
Emil Velikovbddf4df2015-08-07 17:09:35 +0100164drm_private uint64_t amdgpu_cs_calculate_timeout(uint64_t timeout);
Alex Deucher09361392015-04-20 12:04:22 -0400165
166/**
167 * Inline functions.
168 */
169
170/**
171 * Increment src and decrement dst as if we were updating references
172 * for an assignment between 2 pointers of some objects.
173 *
174 * \return true if dst is 0
175 */
176static inline bool update_references(atomic_t *dst, atomic_t *src)
177{
178 if (dst != src) {
179 /* bump src first */
180 if (src) {
181 assert(atomic_read(src) > 0);
182 atomic_inc(src);
183 }
184 if (dst) {
185 assert(atomic_read(dst) > 0);
186 return atomic_dec_and_test(dst);
187 }
188 }
189 return false;
190}
191
192/**
193 * Assignment between two amdgpu_bo pointers with reference counting.
194 *
195 * Usage:
196 * struct amdgpu_bo *dst = ... , *src = ...;
197 *
198 * dst = src;
199 * // No reference counting. Only use this when you need to move
200 * // a reference from one pointer to another.
201 *
202 * amdgpu_bo_reference(&dst, src);
203 * // Reference counters are updated. dst is decremented and src is
204 * // incremented. dst is freed if its reference counter is 0.
205 */
206static inline void amdgpu_bo_reference(struct amdgpu_bo **dst,
207 struct amdgpu_bo *src)
208{
209 if (update_references(&(*dst)->refcount, &src->refcount))
210 amdgpu_bo_free_internal(*dst);
211 *dst = src;
212}
213
Alex Deucher09361392015-04-20 12:04:22 -0400214#endif