| Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 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 | #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.liu | 2f2c8ac | 2015-04-23 13:18:59 +0800 | [diff] [blame] | 38 | /* do not use below macro if b is not power of 2 aligned value */ | 
| Jack Xiao | 7454779 | 2015-05-07 16:07:03 +0800 | [diff] [blame] | 39 | #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 Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 42 |  | 
| Jammy Zhou | 241cf6d | 2015-05-13 01:14:11 +0800 | [diff] [blame] | 43 | #define AMDGPU_INVALID_VA_ADDRESS	0xffffffffffffffff | 
|  | 44 |  | 
| Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 45 | struct amdgpu_bo_va_hole { | 
|  | 46 | struct list_head list; | 
|  | 47 | uint64_t offset; | 
|  | 48 | uint64_t size; | 
|  | 49 | }; | 
|  | 50 |  | 
|  | 51 | struct amdgpu_bo_va_mgr { | 
| Ken Wang | 322d02d | 2015-05-21 17:21:21 +0800 | [diff] [blame] | 52 | atomic_t refcount; | 
| Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 53 | /* the start virtual address */ | 
|  | 54 | uint64_t va_offset; | 
| Jammy Zhou | 241cf6d | 2015-05-13 01:14:11 +0800 | [diff] [blame] | 55 | uint64_t va_max; | 
| Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 56 | struct list_head va_holes; | 
|  | 57 | pthread_mutex_t bo_va_mutex; | 
|  | 58 | uint32_t va_alignment; | 
|  | 59 | }; | 
|  | 60 |  | 
|  | 61 | struct amdgpu_device { | 
|  | 62 | atomic_t refcount; | 
|  | 63 | int fd; | 
|  | 64 | int flink_fd; | 
|  | 65 | unsigned major_version; | 
|  | 66 | unsigned minor_version; | 
|  | 67 |  | 
|  | 68 | /** List of buffer handles. Protected by bo_table_mutex. */ | 
|  | 69 | struct util_hash_table *bo_handles; | 
|  | 70 | /** List of buffer GEM flink names. Protected by bo_table_mutex. */ | 
|  | 71 | struct util_hash_table *bo_flink_names; | 
| Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 72 | /** This protects all hash tables. */ | 
|  | 73 | pthread_mutex_t bo_table_mutex; | 
| Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 74 | struct drm_amdgpu_info_device dev_info; | 
|  | 75 | struct amdgpu_gpu_info info; | 
| Ken Wang | 322d02d | 2015-05-21 17:21:21 +0800 | [diff] [blame] | 76 | struct amdgpu_bo_va_mgr *vamgr; | 
| Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 77 | }; | 
|  | 78 |  | 
|  | 79 | struct amdgpu_bo { | 
|  | 80 | atomic_t refcount; | 
|  | 81 | struct amdgpu_device *dev; | 
|  | 82 |  | 
|  | 83 | uint64_t alloc_size; | 
|  | 84 | uint64_t virtual_mc_base_address; | 
|  | 85 |  | 
|  | 86 | uint32_t handle; | 
|  | 87 | uint32_t flink_name; | 
|  | 88 |  | 
|  | 89 | pthread_mutex_t cpu_access_mutex; | 
|  | 90 | void *cpu_ptr; | 
|  | 91 | int cpu_map_count; | 
|  | 92 | }; | 
|  | 93 |  | 
| Christian König | 6dc2eaf | 2015-04-22 14:52:34 +0200 | [diff] [blame] | 94 | struct amdgpu_bo_list { | 
|  | 95 | struct amdgpu_device *dev; | 
|  | 96 |  | 
|  | 97 | uint32_t handle; | 
|  | 98 | }; | 
|  | 99 |  | 
| Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 100 | struct amdgpu_context { | 
| Christian König | 9c2afff | 2015-04-22 12:21:13 +0200 | [diff] [blame] | 101 | struct amdgpu_device *dev; | 
| Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 102 | /** Mutex for accessing fences and to maintain command submissions | 
| Jammy Zhou | 40c5336 | 2015-05-29 12:59:59 +0200 | [diff] [blame^] | 103 | in good sequence. */ | 
| Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 104 | pthread_mutex_t sequence_mutex; | 
|  | 105 | /** Buffer for user fences */ | 
|  | 106 | struct amdgpu_ib *fence_ib; | 
|  | 107 | /** The newest expired fence for the ring of the ip blocks. */ | 
|  | 108 | uint64_t expired_fences[AMDGPU_HW_IP_NUM][AMDGPU_HW_IP_INSTANCE_MAX_COUNT][AMDGPU_CS_MAX_RINGS]; | 
| Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 109 | /* context id*/ | 
|  | 110 | uint32_t id; | 
|  | 111 | }; | 
|  | 112 |  | 
|  | 113 | struct amdgpu_ib { | 
| Christian König | 9c2afff | 2015-04-22 12:21:13 +0200 | [diff] [blame] | 114 | amdgpu_context_handle context; | 
| Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 115 | amdgpu_bo_handle buf_handle; | 
|  | 116 | void *cpu; | 
|  | 117 | uint64_t virtual_mc_base_address; | 
| Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 118 | }; | 
|  | 119 |  | 
|  | 120 | /** | 
|  | 121 | * Functions. | 
|  | 122 | */ | 
|  | 123 |  | 
|  | 124 | void amdgpu_device_free_internal(amdgpu_device_handle dev); | 
|  | 125 |  | 
|  | 126 | void amdgpu_bo_free_internal(amdgpu_bo_handle bo); | 
|  | 127 |  | 
| Ken Wang | 322d02d | 2015-05-21 17:21:21 +0800 | [diff] [blame] | 128 | struct amdgpu_bo_va_mgr* amdgpu_vamgr_get_global(struct amdgpu_device *dev); | 
|  | 129 |  | 
|  | 130 | void amdgpu_vamgr_reference(struct amdgpu_bo_va_mgr **dst, struct amdgpu_bo_va_mgr *src); | 
| Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 131 |  | 
|  | 132 | uint64_t amdgpu_vamgr_find_va(struct amdgpu_bo_va_mgr *mgr, | 
| Ken Wang | 322d02d | 2015-05-21 17:21:21 +0800 | [diff] [blame] | 133 | uint64_t size, uint64_t alignment); | 
| Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 134 |  | 
| Ken Wang | 322d02d | 2015-05-21 17:21:21 +0800 | [diff] [blame] | 135 | void amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va, | 
|  | 136 | uint64_t size); | 
| Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 137 |  | 
|  | 138 | int amdgpu_query_gpu_info_init(amdgpu_device_handle dev); | 
|  | 139 |  | 
|  | 140 | uint64_t amdgpu_cs_calculate_timeout(uint64_t timeout); | 
|  | 141 |  | 
|  | 142 | /** | 
|  | 143 | * Inline functions. | 
|  | 144 | */ | 
|  | 145 |  | 
|  | 146 | /** | 
|  | 147 | * Increment src and decrement dst as if we were updating references | 
|  | 148 | * for an assignment between 2 pointers of some objects. | 
|  | 149 | * | 
|  | 150 | * \return  true if dst is 0 | 
|  | 151 | */ | 
|  | 152 | static inline bool update_references(atomic_t *dst, atomic_t *src) | 
|  | 153 | { | 
|  | 154 | if (dst != src) { | 
|  | 155 | /* bump src first */ | 
|  | 156 | if (src) { | 
|  | 157 | assert(atomic_read(src) > 0); | 
|  | 158 | atomic_inc(src); | 
|  | 159 | } | 
|  | 160 | if (dst) { | 
|  | 161 | assert(atomic_read(dst) > 0); | 
|  | 162 | return atomic_dec_and_test(dst); | 
|  | 163 | } | 
|  | 164 | } | 
|  | 165 | return false; | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | /** | 
|  | 169 | * Assignment between two amdgpu_bo pointers with reference counting. | 
|  | 170 | * | 
|  | 171 | * Usage: | 
|  | 172 | *    struct amdgpu_bo *dst = ... , *src = ...; | 
|  | 173 | * | 
|  | 174 | *    dst = src; | 
|  | 175 | *    // No reference counting. Only use this when you need to move | 
|  | 176 | *    // a reference from one pointer to another. | 
|  | 177 | * | 
|  | 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. | 
|  | 181 | */ | 
|  | 182 | static inline void amdgpu_bo_reference(struct amdgpu_bo **dst, | 
|  | 183 | struct amdgpu_bo *src) | 
|  | 184 | { | 
|  | 185 | if (update_references(&(*dst)->refcount, &src->refcount)) | 
|  | 186 | amdgpu_bo_free_internal(*dst); | 
|  | 187 | *dst = src; | 
|  | 188 | } | 
|  | 189 |  | 
|  | 190 | /** | 
|  | 191 | * Assignment between two amdgpu_device pointers with reference counting. | 
|  | 192 | * | 
|  | 193 | * Usage: | 
|  | 194 | *    struct amdgpu_device *dst = ... , *src = ...; | 
|  | 195 | * | 
|  | 196 | *    dst = src; | 
|  | 197 | *    // No reference counting. Only use this when you need to move | 
|  | 198 | *    // a reference from one pointer to another. | 
|  | 199 | * | 
|  | 200 | *    amdgpu_device_reference(&dst, src); | 
|  | 201 | *    // Reference counters are updated. dst is decremented and src is | 
|  | 202 | *    // incremented. dst is freed if its reference counter is 0. | 
|  | 203 | */ | 
|  | 204 | void amdgpu_device_reference(struct amdgpu_device **dst, | 
|  | 205 | struct amdgpu_device *src); | 
|  | 206 | #endif |