kfd_device_queue_manager.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright 2014 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. */
  23. #ifndef KFD_DEVICE_QUEUE_MANAGER_H_
  24. #define KFD_DEVICE_QUEUE_MANAGER_H_
  25. #include <linux/rwsem.h>
  26. #include <linux/list.h>
  27. #include <linux/mutex.h>
  28. #include <linux/sched/mm.h>
  29. #include "kfd_priv.h"
  30. #include "kfd_mqd_manager.h"
  31. #define KFD_UNMAP_LATENCY_MS (4000)
  32. #define QUEUE_PREEMPT_DEFAULT_TIMEOUT_MS (2 * KFD_UNMAP_LATENCY_MS + 1000)
  33. struct device_process_node {
  34. struct qcm_process_device *qpd;
  35. struct list_head list;
  36. };
  37. /**
  38. * struct device_queue_manager_ops
  39. *
  40. * @create_queue: Queue creation routine.
  41. *
  42. * @destroy_queue: Queue destruction routine.
  43. *
  44. * @update_queue: Queue update routine.
  45. *
  46. * @get_mqd_manager: Returns the mqd manager according to the mqd type.
  47. *
  48. * @exeute_queues: Dispatches the queues list to the H/W.
  49. *
  50. * @register_process: This routine associates a specific process with device.
  51. *
  52. * @unregister_process: destroys the associations between process to device.
  53. *
  54. * @initialize: Initializes the pipelines and memory module for that device.
  55. *
  56. * @start: Initializes the resources/modules the the device needs for queues
  57. * execution. This function is called on device initialization and after the
  58. * system woke up after suspension.
  59. *
  60. * @stop: This routine stops execution of all the active queue running on the
  61. * H/W and basically this function called on system suspend.
  62. *
  63. * @uninitialize: Destroys all the device queue manager resources allocated in
  64. * initialize routine.
  65. *
  66. * @create_kernel_queue: Creates kernel queue. Used for debug queue.
  67. *
  68. * @destroy_kernel_queue: Destroys kernel queue. Used for debug queue.
  69. *
  70. * @set_cache_memory_policy: Sets memory policy (cached/ non cached) for the
  71. * memory apertures.
  72. *
  73. * @process_termination: Clears all process queues belongs to that device.
  74. *
  75. * @evict_process_queues: Evict all active queues of a process
  76. *
  77. * @restore_process_queues: Restore all evicted queues queues of a process
  78. *
  79. * @get_wave_state: Retrieves context save state and optionally copies the
  80. * control stack, if kept in the MQD, to the given userspace address.
  81. */
  82. struct device_queue_manager_ops {
  83. int (*create_queue)(struct device_queue_manager *dqm,
  84. struct queue *q,
  85. struct qcm_process_device *qpd);
  86. int (*destroy_queue)(struct device_queue_manager *dqm,
  87. struct qcm_process_device *qpd,
  88. struct queue *q);
  89. int (*update_queue)(struct device_queue_manager *dqm,
  90. struct queue *q);
  91. struct mqd_manager * (*get_mqd_manager)
  92. (struct device_queue_manager *dqm,
  93. enum KFD_MQD_TYPE type);
  94. int (*register_process)(struct device_queue_manager *dqm,
  95. struct qcm_process_device *qpd);
  96. int (*unregister_process)(struct device_queue_manager *dqm,
  97. struct qcm_process_device *qpd);
  98. int (*initialize)(struct device_queue_manager *dqm);
  99. int (*start)(struct device_queue_manager *dqm);
  100. int (*stop)(struct device_queue_manager *dqm);
  101. void (*uninitialize)(struct device_queue_manager *dqm);
  102. int (*create_kernel_queue)(struct device_queue_manager *dqm,
  103. struct kernel_queue *kq,
  104. struct qcm_process_device *qpd);
  105. void (*destroy_kernel_queue)(struct device_queue_manager *dqm,
  106. struct kernel_queue *kq,
  107. struct qcm_process_device *qpd);
  108. bool (*set_cache_memory_policy)(struct device_queue_manager *dqm,
  109. struct qcm_process_device *qpd,
  110. enum cache_policy default_policy,
  111. enum cache_policy alternate_policy,
  112. void __user *alternate_aperture_base,
  113. uint64_t alternate_aperture_size);
  114. int (*set_trap_handler)(struct device_queue_manager *dqm,
  115. struct qcm_process_device *qpd,
  116. uint64_t tba_addr,
  117. uint64_t tma_addr);
  118. int (*process_termination)(struct device_queue_manager *dqm,
  119. struct qcm_process_device *qpd);
  120. int (*evict_process_queues)(struct device_queue_manager *dqm,
  121. struct qcm_process_device *qpd);
  122. int (*restore_process_queues)(struct device_queue_manager *dqm,
  123. struct qcm_process_device *qpd);
  124. int (*get_wave_state)(struct device_queue_manager *dqm,
  125. struct queue *q,
  126. void __user *ctl_stack,
  127. u32 *ctl_stack_used_size,
  128. u32 *save_area_used_size);
  129. };
  130. struct device_queue_manager_asic_ops {
  131. int (*update_qpd)(struct device_queue_manager *dqm,
  132. struct qcm_process_device *qpd);
  133. bool (*set_cache_memory_policy)(struct device_queue_manager *dqm,
  134. struct qcm_process_device *qpd,
  135. enum cache_policy default_policy,
  136. enum cache_policy alternate_policy,
  137. void __user *alternate_aperture_base,
  138. uint64_t alternate_aperture_size);
  139. void (*init_sdma_vm)(struct device_queue_manager *dqm,
  140. struct queue *q,
  141. struct qcm_process_device *qpd);
  142. };
  143. /**
  144. * struct device_queue_manager
  145. *
  146. * This struct is a base class for the kfd queues scheduler in the
  147. * device level. The device base class should expose the basic operations
  148. * for queue creation and queue destruction. This base class hides the
  149. * scheduling mode of the driver and the specific implementation of the
  150. * concrete device. This class is the only class in the queues scheduler
  151. * that configures the H/W.
  152. *
  153. */
  154. struct device_queue_manager {
  155. struct device_queue_manager_ops ops;
  156. struct device_queue_manager_asic_ops asic_ops;
  157. struct mqd_manager *mqd_mgrs[KFD_MQD_TYPE_MAX];
  158. struct packet_manager packets;
  159. struct kfd_dev *dev;
  160. struct mutex lock_hidden; /* use dqm_lock/unlock(dqm) */
  161. struct list_head queues;
  162. unsigned int saved_flags;
  163. unsigned int processes_count;
  164. unsigned int queue_count;
  165. unsigned int sdma_queue_count;
  166. unsigned int total_queue_count;
  167. unsigned int next_pipe_to_allocate;
  168. unsigned int *allocated_queues;
  169. unsigned int sdma_bitmap;
  170. unsigned int vmid_bitmap;
  171. uint64_t pipelines_addr;
  172. struct kfd_mem_obj *pipeline_mem;
  173. uint64_t fence_gpu_addr;
  174. unsigned int *fence_addr;
  175. struct kfd_mem_obj *fence_mem;
  176. bool active_runlist;
  177. int sched_policy;
  178. /* hw exception */
  179. bool is_hws_hang;
  180. struct work_struct hw_exception_work;
  181. };
  182. void device_queue_manager_init_cik(
  183. struct device_queue_manager_asic_ops *asic_ops);
  184. void device_queue_manager_init_cik_hawaii(
  185. struct device_queue_manager_asic_ops *asic_ops);
  186. void device_queue_manager_init_vi(
  187. struct device_queue_manager_asic_ops *asic_ops);
  188. void device_queue_manager_init_vi_tonga(
  189. struct device_queue_manager_asic_ops *asic_ops);
  190. void device_queue_manager_init_v9(
  191. struct device_queue_manager_asic_ops *asic_ops);
  192. void program_sh_mem_settings(struct device_queue_manager *dqm,
  193. struct qcm_process_device *qpd);
  194. unsigned int get_queues_num(struct device_queue_manager *dqm);
  195. unsigned int get_queues_per_pipe(struct device_queue_manager *dqm);
  196. unsigned int get_pipes_per_mec(struct device_queue_manager *dqm);
  197. unsigned int get_num_sdma_queues(struct device_queue_manager *dqm);
  198. static inline unsigned int get_sh_mem_bases_32(struct kfd_process_device *pdd)
  199. {
  200. return (pdd->lds_base >> 16) & 0xFF;
  201. }
  202. static inline unsigned int
  203. get_sh_mem_bases_nybble_64(struct kfd_process_device *pdd)
  204. {
  205. return (pdd->lds_base >> 60) & 0x0E;
  206. }
  207. /* The DQM lock can be taken in MMU notifiers. Make sure no reclaim-FS
  208. * happens while holding this lock anywhere to prevent deadlocks when
  209. * an MMU notifier runs in reclaim-FS context.
  210. */
  211. static inline void dqm_lock(struct device_queue_manager *dqm)
  212. {
  213. mutex_lock(&dqm->lock_hidden);
  214. dqm->saved_flags = memalloc_nofs_save();
  215. }
  216. static inline void dqm_unlock(struct device_queue_manager *dqm)
  217. {
  218. memalloc_nofs_restore(dqm->saved_flags);
  219. mutex_unlock(&dqm->lock_hidden);
  220. }
  221. #endif /* KFD_DEVICE_QUEUE_MANAGER_H_ */