amdgpu_internal.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #include <assert.h>
  27. #include <pthread.h>
  28. #include "libdrm_macros.h"
  29. #include "xf86atomic.h"
  30. #include "amdgpu.h"
  31. #include "util_double_list.h"
  32. #include "handle_table.h"
  33. #define AMDGPU_CS_MAX_RINGS 8
  34. /* do not use below macro if b is not power of 2 aligned value */
  35. #define __round_mask(x, y) ((__typeof__(x))((y)-1))
  36. #define ROUND_UP(x, y) ((((x)-1) | __round_mask(x, y))+1)
  37. #define ROUND_DOWN(x, y) ((x) & ~__round_mask(x, y))
  38. #define AMDGPU_INVALID_VA_ADDRESS 0xffffffffffffffff
  39. #define AMDGPU_NULL_SUBMIT_SEQ 0
  40. struct amdgpu_bo_va_hole {
  41. struct list_head list;
  42. uint64_t offset;
  43. uint64_t size;
  44. };
  45. struct amdgpu_bo_va_mgr {
  46. uint64_t va_max;
  47. struct list_head va_holes;
  48. pthread_mutex_t bo_va_mutex;
  49. uint32_t va_alignment;
  50. };
  51. struct amdgpu_va {
  52. uint64_t address;
  53. uint64_t size;
  54. enum amdgpu_gpu_va_range range;
  55. struct amdgpu_bo_va_mgr *vamgr;
  56. };
  57. struct amdgpu_va_manager {
  58. /** The VA manager for the lower virtual address space */
  59. struct amdgpu_bo_va_mgr vamgr_low;
  60. /** The VA manager for the 32bit address space */
  61. struct amdgpu_bo_va_mgr vamgr_32;
  62. /** The VA manager for the high virtual address space */
  63. struct amdgpu_bo_va_mgr vamgr_high;
  64. /** The VA manager for the 32bit high address space */
  65. struct amdgpu_bo_va_mgr vamgr_high_32;
  66. /** The bit to control whether it's the "LOW" or "HIGH" halves, when
  67. * half of the address space is reserved for PRT to implement a SW
  68. * workaround. */
  69. unsigned address_prt_wa_control_bit;
  70. };
  71. struct amdgpu_device {
  72. atomic_t refcount;
  73. struct amdgpu_device *next;
  74. int fd;
  75. int flink_fd;
  76. unsigned major_version;
  77. unsigned minor_version;
  78. char *marketing_name;
  79. /** List of buffer handles. Protected by bo_table_mutex. */
  80. struct handle_table bo_handles;
  81. /** List of buffer GEM flink names. Protected by bo_table_mutex. */
  82. struct handle_table bo_flink_names;
  83. /** This protects all hash tables. */
  84. pthread_mutex_t bo_table_mutex;
  85. struct drm_amdgpu_info_device dev_info;
  86. struct amdgpu_gpu_info info;
  87. struct amdgpu_va_manager va_mgr;
  88. };
  89. struct amdgpu_bo {
  90. atomic_t refcount;
  91. struct amdgpu_device *dev;
  92. uint64_t alloc_size;
  93. uint32_t handle;
  94. uint32_t flink_name;
  95. pthread_mutex_t cpu_access_mutex;
  96. void *cpu_ptr;
  97. int64_t cpu_map_count;
  98. };
  99. struct amdgpu_bo_list {
  100. struct amdgpu_device *dev;
  101. uint32_t handle;
  102. };
  103. struct amdgpu_context {
  104. struct amdgpu_device *dev;
  105. /** Mutex for accessing fences and to maintain command submissions
  106. in good sequence. */
  107. pthread_mutex_t sequence_mutex;
  108. /* context id*/
  109. uint32_t id;
  110. uint64_t last_seq[AMDGPU_HW_IP_NUM][AMDGPU_HW_IP_INSTANCE_MAX_COUNT][AMDGPU_CS_MAX_RINGS];
  111. struct list_head sem_list[AMDGPU_HW_IP_NUM][AMDGPU_HW_IP_INSTANCE_MAX_COUNT][AMDGPU_CS_MAX_RINGS];
  112. };
  113. /**
  114. * Structure describing sw semaphore based on scheduler
  115. *
  116. */
  117. struct amdgpu_semaphore {
  118. atomic_t refcount;
  119. struct list_head list;
  120. struct amdgpu_cs_fence signal_fence;
  121. };
  122. /**
  123. * Functions.
  124. */
  125. drm_private void amdgpu_vamgr_init(struct amdgpu_bo_va_mgr *mgr, uint64_t start,
  126. uint64_t max, uint64_t alignment);
  127. drm_private void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr);
  128. drm_private void amdgpu_parse_asic_ids(struct amdgpu_device *dev);
  129. drm_private int amdgpu_query_gpu_info_init(amdgpu_device_handle dev);
  130. drm_private uint64_t amdgpu_cs_calculate_timeout(uint64_t timeout);
  131. /**
  132. * Inline functions.
  133. */
  134. /**
  135. * Increment src and decrement dst as if we were updating references
  136. * for an assignment between 2 pointers of some objects.
  137. *
  138. * \return true if dst is 0
  139. */
  140. static inline bool update_references(atomic_t *dst, atomic_t *src)
  141. {
  142. if (dst != src) {
  143. /* bump src first */
  144. if (src) {
  145. assert(atomic_read(src) > 0);
  146. atomic_inc(src);
  147. }
  148. if (dst) {
  149. assert(atomic_read(dst) > 0);
  150. return atomic_dec_and_test(dst);
  151. }
  152. }
  153. return false;
  154. }
  155. #endif