blob: 37a7c9d56dfdf2a2bcd62bd539f476a60ce9e500 [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
Alex Deucher09361392015-04-20 12:04:22 -040028#include <assert.h>
29#include <pthread.h>
Emil Velikovb4718182015-08-07 16:54:29 +010030
31#include "libdrm_macros.h"
Alex Deucher09361392015-04-20 12:04:22 -040032#include "xf86atomic.h"
33#include "amdgpu.h"
34#include "util_double_list.h"
Christian Königbde850b2018-08-02 10:47:02 +020035#include "handle_table.h"
Alex Deucher09361392015-04-20 12:04:22 -040036
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 */
Jack Xiao74547792015-05-07 16:07:03 +080039#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))
Alex Deucher09361392015-04-20 12:04:22 -040042
Jammy Zhou241cf6d2015-05-13 01:14:11 +080043#define AMDGPU_INVALID_VA_ADDRESS 0xffffffffffffffff
Ken Wangf884af92016-02-04 13:52:22 +080044#define AMDGPU_NULL_SUBMIT_SEQ 0
Jammy Zhou241cf6d2015-05-13 01:14:11 +080045
Alex Deucher09361392015-04-20 12:04:22 -040046struct amdgpu_bo_va_hole {
47 struct list_head list;
48 uint64_t offset;
49 uint64_t size;
50};
51
52struct amdgpu_bo_va_mgr {
Jammy Zhou241cf6d2015-05-13 01:14:11 +080053 uint64_t va_max;
Alex Deucher09361392015-04-20 12:04:22 -040054 struct list_head va_holes;
55 pthread_mutex_t bo_va_mutex;
56 uint32_t va_alignment;
57};
58
Sabre Shao23fab592015-07-09 13:50:36 +080059struct amdgpu_va {
60 amdgpu_device_handle dev;
61 uint64_t address;
62 uint64_t size;
63 enum amdgpu_gpu_va_range range;
Jammy Zhouffa305d2015-08-17 11:09:08 +080064 struct amdgpu_bo_va_mgr *vamgr;
Sabre Shao23fab592015-07-09 13:50:36 +080065};
66
Alex Deucher09361392015-04-20 12:04:22 -040067struct amdgpu_device {
68 atomic_t refcount;
Christian König7aa1a512018-08-01 20:44:44 +020069 struct amdgpu_device *next;
Alex Deucher09361392015-04-20 12:04:22 -040070 int fd;
71 int flink_fd;
72 unsigned major_version;
73 unsigned minor_version;
74
Michel Dänzerf05a2b42017-11-30 18:52:06 +010075 char *marketing_name;
Alex Deucher09361392015-04-20 12:04:22 -040076 /** List of buffer handles. Protected by bo_table_mutex. */
Christian Königbde850b2018-08-02 10:47:02 +020077 struct handle_table bo_handles;
Alex Deucher09361392015-04-20 12:04:22 -040078 /** List of buffer GEM flink names. Protected by bo_table_mutex. */
Christian König9a38e852018-08-02 10:56:11 +020079 struct handle_table bo_flink_names;
Alex Deucher09361392015-04-20 12:04:22 -040080 /** This protects all hash tables. */
81 pthread_mutex_t bo_table_mutex;
Alex Deucher09361392015-04-20 12:04:22 -040082 struct drm_amdgpu_info_device dev_info;
83 struct amdgpu_gpu_info info;
Christian Königcd8a8042018-02-26 12:30:36 +010084 /** The VA manager for the lower virtual address space */
Alex Xiefe7cb342017-01-28 21:50:44 +020085 struct amdgpu_bo_va_mgr vamgr;
Jammy Zhouffa305d2015-08-17 11:09:08 +080086 /** The VA manager for the 32bit address space */
Alex Xie067e9a12017-01-28 21:50:36 +020087 struct amdgpu_bo_va_mgr vamgr_32;
Christian Königcd8a8042018-02-26 12:30:36 +010088 /** The VA manager for the high virtual address space */
89 struct amdgpu_bo_va_mgr vamgr_high;
90 /** The VA manager for the 32bit high address space */
91 struct amdgpu_bo_va_mgr vamgr_high_32;
Alex Deucher09361392015-04-20 12:04:22 -040092};
93
94struct amdgpu_bo {
95 atomic_t refcount;
96 struct amdgpu_device *dev;
97
98 uint64_t alloc_size;
Alex Deucher09361392015-04-20 12:04:22 -040099
100 uint32_t handle;
101 uint32_t flink_name;
102
103 pthread_mutex_t cpu_access_mutex;
104 void *cpu_ptr;
Pierre-Eric Pelloux-Prayerb9bf42d2020-03-17 16:50:56 +0100105 int64_t cpu_map_count;
Alex Deucher09361392015-04-20 12:04:22 -0400106};
107
Christian König6dc2eaf2015-04-22 14:52:34 +0200108struct amdgpu_bo_list {
109 struct amdgpu_device *dev;
110
111 uint32_t handle;
112};
113
Alex Deucher09361392015-04-20 12:04:22 -0400114struct amdgpu_context {
Christian König9c2afff2015-04-22 12:21:13 +0200115 struct amdgpu_device *dev;
Marek Olšák6afadea2016-01-12 22:13:07 +0100116 /** Mutex for accessing fences and to maintain command submissions
117 in good sequence. */
118 pthread_mutex_t sequence_mutex;
Alex Deucher09361392015-04-20 12:04:22 -0400119 /* context id*/
120 uint32_t id;
Marek Olšák6afadea2016-01-12 22:13:07 +0100121 uint64_t last_seq[AMDGPU_HW_IP_NUM][AMDGPU_HW_IP_INSTANCE_MAX_COUNT][AMDGPU_CS_MAX_RINGS];
122 struct list_head sem_list[AMDGPU_HW_IP_NUM][AMDGPU_HW_IP_INSTANCE_MAX_COUNT][AMDGPU_CS_MAX_RINGS];
123};
124
125/**
126 * Structure describing sw semaphore based on scheduler
127 *
128 */
129struct amdgpu_semaphore {
130 atomic_t refcount;
131 struct list_head list;
132 struct amdgpu_cs_fence signal_fence;
Alex Deucher09361392015-04-20 12:04:22 -0400133};
134
Alex Deucher09361392015-04-20 12:04:22 -0400135/**
136 * Functions.
137 */
138
Jammy Zhouffa305d2015-08-17 11:09:08 +0800139drm_private void amdgpu_vamgr_init(struct amdgpu_bo_va_mgr *mgr, uint64_t start,
140 uint64_t max, uint64_t alignment);
141
142drm_private void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr);
143
Michel Dänzerf05a2b42017-11-30 18:52:06 +0100144drm_private void amdgpu_parse_asic_ids(struct amdgpu_device *dev);
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400145
Emil Velikovbddf4df2015-08-07 17:09:35 +0100146drm_private int amdgpu_query_gpu_info_init(amdgpu_device_handle dev);
Alex Deucher09361392015-04-20 12:04:22 -0400147
Emil Velikovbddf4df2015-08-07 17:09:35 +0100148drm_private uint64_t amdgpu_cs_calculate_timeout(uint64_t timeout);
Alex Deucher09361392015-04-20 12:04:22 -0400149
150/**
151 * Inline functions.
152 */
153
154/**
155 * Increment src and decrement dst as if we were updating references
156 * for an assignment between 2 pointers of some objects.
157 *
158 * \return true if dst is 0
159 */
160static inline bool update_references(atomic_t *dst, atomic_t *src)
161{
162 if (dst != src) {
163 /* bump src first */
164 if (src) {
165 assert(atomic_read(src) > 0);
166 atomic_inc(src);
167 }
168 if (dst) {
169 assert(atomic_read(dst) > 0);
170 return atomic_dec_and_test(dst);
171 }
172 }
173 return false;
174}
175
Alex Deucher09361392015-04-20 12:04:22 -0400176#endif