amdgpu: add AMDGPU_VA_RANGE_HIGH
[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
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>
34
35 #include "libdrm_macros.h"
36 #include "xf86atomic.h"
37 #include "amdgpu.h"
38 #include "util_double_list.h"
39
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))
45
46 #define AMDGPU_INVALID_VA_ADDRESS       0xffffffffffffffff
47 #define AMDGPU_NULL_SUBMIT_SEQ          0
48
49 struct amdgpu_bo_va_hole {
50         struct list_head list;
51         uint64_t offset;
52         uint64_t size;
53 };
54
55 struct amdgpu_bo_va_mgr {
56         uint64_t va_max;
57         struct list_head va_holes;
58         pthread_mutex_t bo_va_mutex;
59         uint32_t va_alignment;
60 };
61
62 struct amdgpu_va {
63         amdgpu_device_handle dev;
64         uint64_t address;
65         uint64_t size;
66         enum amdgpu_gpu_va_range range;
67         struct amdgpu_bo_va_mgr *vamgr;
68 };
69
70 struct amdgpu_device {
71         atomic_t refcount;
72         int fd;
73         int flink_fd;
74         unsigned major_version;
75         unsigned minor_version;
76
77         char *marketing_name;
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         /** The VA manager for the lower virtual address space */
87         struct amdgpu_bo_va_mgr vamgr;
88         /** The VA manager for the 32bit address space */
89         struct amdgpu_bo_va_mgr vamgr_32;
90         /** The VA manager for the high virtual address space */
91         struct amdgpu_bo_va_mgr vamgr_high;
92         /** The VA manager for the 32bit high address space */
93         struct amdgpu_bo_va_mgr vamgr_high_32;
94 };
95
96 struct amdgpu_bo {
97         atomic_t refcount;
98         struct amdgpu_device *dev;
99
100         uint64_t alloc_size;
101
102         uint32_t handle;
103         uint32_t flink_name;
104
105         pthread_mutex_t cpu_access_mutex;
106         void *cpu_ptr;
107         int cpu_map_count;
108 };
109
110 struct amdgpu_bo_list {
111         struct amdgpu_device *dev;
112
113         uint32_t handle;
114 };
115
116 struct amdgpu_context {
117         struct amdgpu_device *dev;
118         /** Mutex for accessing fences and to maintain command submissions
119             in good sequence. */
120         pthread_mutex_t sequence_mutex;
121         /* context id*/
122         uint32_t id;
123         uint64_t last_seq[AMDGPU_HW_IP_NUM][AMDGPU_HW_IP_INSTANCE_MAX_COUNT][AMDGPU_CS_MAX_RINGS];
124         struct list_head sem_list[AMDGPU_HW_IP_NUM][AMDGPU_HW_IP_INSTANCE_MAX_COUNT][AMDGPU_CS_MAX_RINGS];
125 };
126
127 /**
128  * Structure describing sw semaphore based on scheduler
129  *
130  */
131 struct amdgpu_semaphore {
132         atomic_t refcount;
133         struct list_head list;
134         struct amdgpu_cs_fence signal_fence;
135 };
136
137 /**
138  * Functions.
139  */
140
141 drm_private void amdgpu_vamgr_init(struct amdgpu_bo_va_mgr *mgr, uint64_t start,
142                        uint64_t max, uint64_t alignment);
143
144 drm_private void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr);
145
146 drm_private void amdgpu_parse_asic_ids(struct amdgpu_device *dev);
147
148 drm_private int amdgpu_query_gpu_info_init(amdgpu_device_handle dev);
149
150 drm_private uint64_t amdgpu_cs_calculate_timeout(uint64_t timeout);
151
152 /**
153  * Inline functions.
154  */
155
156 /**
157  * Increment src and decrement dst as if we were updating references
158  * for an assignment between 2 pointers of some objects.
159  *
160  * \return  true if dst is 0
161  */
162 static inline bool update_references(atomic_t *dst, atomic_t *src)
163 {
164         if (dst != src) {
165                 /* bump src first */
166                 if (src) {
167                         assert(atomic_read(src) > 0);
168                         atomic_inc(src);
169                 }
170                 if (dst) {
171                         assert(atomic_read(dst) > 0);
172                         return atomic_dec_and_test(dst);
173                 }
174         }
175         return false;
176 }
177
178 #endif