kfd_mqd_manager_vi.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. #include <linux/printk.h>
  24. #include <linux/slab.h>
  25. #include "kfd_priv.h"
  26. #include "kfd_mqd_manager.h"
  27. #include "vi_structs.h"
  28. #include "gca/gfx_8_0_sh_mask.h"
  29. #include "gca/gfx_8_0_enum.h"
  30. #define CP_MQD_CONTROL__PRIV_STATE__SHIFT 0x8
  31. static inline struct vi_mqd *get_mqd(void *mqd)
  32. {
  33. return (struct vi_mqd *)mqd;
  34. }
  35. static int init_mqd(struct mqd_manager *mm, void **mqd,
  36. struct kfd_mem_obj **mqd_mem_obj, uint64_t *gart_addr,
  37. struct queue_properties *q)
  38. {
  39. int retval;
  40. uint64_t addr;
  41. struct vi_mqd *m;
  42. retval = kfd_gtt_sa_allocate(mm->dev, sizeof(struct vi_mqd),
  43. mqd_mem_obj);
  44. if (retval != 0)
  45. return -ENOMEM;
  46. m = (struct vi_mqd *) (*mqd_mem_obj)->cpu_ptr;
  47. addr = (*mqd_mem_obj)->gpu_addr;
  48. memset(m, 0, sizeof(struct vi_mqd));
  49. m->header = 0xC0310800;
  50. m->compute_pipelinestat_enable = 1;
  51. m->compute_static_thread_mgmt_se0 = 0xFFFFFFFF;
  52. m->compute_static_thread_mgmt_se1 = 0xFFFFFFFF;
  53. m->compute_static_thread_mgmt_se2 = 0xFFFFFFFF;
  54. m->compute_static_thread_mgmt_se3 = 0xFFFFFFFF;
  55. m->cp_hqd_persistent_state = CP_HQD_PERSISTENT_STATE__PRELOAD_REQ_MASK |
  56. 0x53 << CP_HQD_PERSISTENT_STATE__PRELOAD_SIZE__SHIFT;
  57. m->cp_mqd_control = 1 << CP_MQD_CONTROL__PRIV_STATE__SHIFT |
  58. MTYPE_UC << CP_MQD_CONTROL__MTYPE__SHIFT;
  59. m->cp_mqd_base_addr_lo = lower_32_bits(addr);
  60. m->cp_mqd_base_addr_hi = upper_32_bits(addr);
  61. m->cp_hqd_quantum = 1 << CP_HQD_QUANTUM__QUANTUM_EN__SHIFT |
  62. 1 << CP_HQD_QUANTUM__QUANTUM_SCALE__SHIFT |
  63. 10 << CP_HQD_QUANTUM__QUANTUM_DURATION__SHIFT;
  64. m->cp_hqd_pipe_priority = 1;
  65. m->cp_hqd_queue_priority = 15;
  66. m->cp_hqd_eop_rptr = 1 << CP_HQD_EOP_RPTR__INIT_FETCHER__SHIFT;
  67. if (q->format == KFD_QUEUE_FORMAT_AQL)
  68. m->cp_hqd_iq_rptr = 1;
  69. *mqd = m;
  70. if (gart_addr != NULL)
  71. *gart_addr = addr;
  72. retval = mm->update_mqd(mm, m, q);
  73. return retval;
  74. }
  75. static int load_mqd(struct mqd_manager *mm, void *mqd,
  76. uint32_t pipe_id, uint32_t queue_id,
  77. uint32_t __user *wptr)
  78. {
  79. return mm->dev->kfd2kgd->hqd_load
  80. (mm->dev->kgd, mqd, pipe_id, queue_id, wptr);
  81. }
  82. static int __update_mqd(struct mqd_manager *mm, void *mqd,
  83. struct queue_properties *q, unsigned int mtype,
  84. unsigned int atc_bit)
  85. {
  86. struct vi_mqd *m;
  87. BUG_ON(!mm || !q || !mqd);
  88. pr_debug("kfd: In func %s\n", __func__);
  89. m = get_mqd(mqd);
  90. m->cp_hqd_pq_control = 5 << CP_HQD_PQ_CONTROL__RPTR_BLOCK_SIZE__SHIFT |
  91. atc_bit << CP_HQD_PQ_CONTROL__PQ_ATC__SHIFT |
  92. mtype << CP_HQD_PQ_CONTROL__MTYPE__SHIFT;
  93. m->cp_hqd_pq_control |=
  94. ffs(q->queue_size / sizeof(unsigned int)) - 1 - 1;
  95. pr_debug("kfd: cp_hqd_pq_control 0x%x\n", m->cp_hqd_pq_control);
  96. m->cp_hqd_pq_base_lo = lower_32_bits((uint64_t)q->queue_address >> 8);
  97. m->cp_hqd_pq_base_hi = upper_32_bits((uint64_t)q->queue_address >> 8);
  98. m->cp_hqd_pq_rptr_report_addr_lo = lower_32_bits((uint64_t)q->read_ptr);
  99. m->cp_hqd_pq_rptr_report_addr_hi = upper_32_bits((uint64_t)q->read_ptr);
  100. m->cp_hqd_pq_doorbell_control =
  101. 1 << CP_HQD_PQ_DOORBELL_CONTROL__DOORBELL_EN__SHIFT |
  102. q->doorbell_off <<
  103. CP_HQD_PQ_DOORBELL_CONTROL__DOORBELL_OFFSET__SHIFT;
  104. pr_debug("kfd: cp_hqd_pq_doorbell_control 0x%x\n",
  105. m->cp_hqd_pq_doorbell_control);
  106. m->cp_hqd_eop_control = atc_bit << CP_HQD_EOP_CONTROL__EOP_ATC__SHIFT |
  107. mtype << CP_HQD_EOP_CONTROL__MTYPE__SHIFT;
  108. m->cp_hqd_ib_control = atc_bit << CP_HQD_IB_CONTROL__IB_ATC__SHIFT |
  109. 3 << CP_HQD_IB_CONTROL__MIN_IB_AVAIL_SIZE__SHIFT |
  110. mtype << CP_HQD_IB_CONTROL__MTYPE__SHIFT;
  111. m->cp_hqd_eop_control |=
  112. ffs(q->eop_ring_buffer_size / sizeof(unsigned int)) - 1 - 1;
  113. m->cp_hqd_eop_base_addr_lo =
  114. lower_32_bits(q->eop_ring_buffer_address >> 8);
  115. m->cp_hqd_eop_base_addr_hi =
  116. upper_32_bits(q->eop_ring_buffer_address >> 8);
  117. m->cp_hqd_iq_timer = atc_bit << CP_HQD_IQ_TIMER__IQ_ATC__SHIFT |
  118. mtype << CP_HQD_IQ_TIMER__MTYPE__SHIFT;
  119. m->cp_hqd_vmid = q->vmid;
  120. if (q->format == KFD_QUEUE_FORMAT_AQL) {
  121. m->cp_hqd_pq_control |= CP_HQD_PQ_CONTROL__NO_UPDATE_RPTR_MASK |
  122. 2 << CP_HQD_PQ_CONTROL__SLOT_BASED_WPTR__SHIFT;
  123. }
  124. m->cp_hqd_active = 0;
  125. q->is_active = false;
  126. if (q->queue_size > 0 &&
  127. q->queue_address != 0 &&
  128. q->queue_percent > 0) {
  129. m->cp_hqd_active = 1;
  130. q->is_active = true;
  131. }
  132. return 0;
  133. }
  134. static int update_mqd(struct mqd_manager *mm, void *mqd,
  135. struct queue_properties *q)
  136. {
  137. return __update_mqd(mm, mqd, q, MTYPE_CC, 1);
  138. }
  139. static int destroy_mqd(struct mqd_manager *mm, void *mqd,
  140. enum kfd_preempt_type type,
  141. unsigned int timeout, uint32_t pipe_id,
  142. uint32_t queue_id)
  143. {
  144. return mm->dev->kfd2kgd->hqd_destroy
  145. (mm->dev->kgd, type, timeout,
  146. pipe_id, queue_id);
  147. }
  148. static void uninit_mqd(struct mqd_manager *mm, void *mqd,
  149. struct kfd_mem_obj *mqd_mem_obj)
  150. {
  151. BUG_ON(!mm || !mqd);
  152. kfd_gtt_sa_free(mm->dev, mqd_mem_obj);
  153. }
  154. static bool is_occupied(struct mqd_manager *mm, void *mqd,
  155. uint64_t queue_address, uint32_t pipe_id,
  156. uint32_t queue_id)
  157. {
  158. return mm->dev->kfd2kgd->hqd_is_occupied(
  159. mm->dev->kgd, queue_address,
  160. pipe_id, queue_id);
  161. }
  162. static int init_mqd_hiq(struct mqd_manager *mm, void **mqd,
  163. struct kfd_mem_obj **mqd_mem_obj, uint64_t *gart_addr,
  164. struct queue_properties *q)
  165. {
  166. struct vi_mqd *m;
  167. int retval = init_mqd(mm, mqd, mqd_mem_obj, gart_addr, q);
  168. if (retval != 0)
  169. return retval;
  170. m = get_mqd(*mqd);
  171. m->cp_hqd_pq_control |= 1 << CP_HQD_PQ_CONTROL__PRIV_STATE__SHIFT |
  172. 1 << CP_HQD_PQ_CONTROL__KMD_QUEUE__SHIFT;
  173. return retval;
  174. }
  175. static int update_mqd_hiq(struct mqd_manager *mm, void *mqd,
  176. struct queue_properties *q)
  177. {
  178. struct vi_mqd *m;
  179. int retval = __update_mqd(mm, mqd, q, MTYPE_UC, 0);
  180. if (retval != 0)
  181. return retval;
  182. m = get_mqd(mqd);
  183. m->cp_hqd_vmid = q->vmid;
  184. return retval;
  185. }
  186. struct mqd_manager *mqd_manager_init_vi(enum KFD_MQD_TYPE type,
  187. struct kfd_dev *dev)
  188. {
  189. struct mqd_manager *mqd;
  190. BUG_ON(!dev);
  191. BUG_ON(type >= KFD_MQD_TYPE_MAX);
  192. pr_debug("kfd: In func %s\n", __func__);
  193. mqd = kzalloc(sizeof(struct mqd_manager), GFP_KERNEL);
  194. if (!mqd)
  195. return NULL;
  196. mqd->dev = dev;
  197. switch (type) {
  198. case KFD_MQD_TYPE_CP:
  199. case KFD_MQD_TYPE_COMPUTE:
  200. mqd->init_mqd = init_mqd;
  201. mqd->uninit_mqd = uninit_mqd;
  202. mqd->load_mqd = load_mqd;
  203. mqd->update_mqd = update_mqd;
  204. mqd->destroy_mqd = destroy_mqd;
  205. mqd->is_occupied = is_occupied;
  206. break;
  207. case KFD_MQD_TYPE_HIQ:
  208. mqd->init_mqd = init_mqd_hiq;
  209. mqd->uninit_mqd = uninit_mqd;
  210. mqd->load_mqd = load_mqd;
  211. mqd->update_mqd = update_mqd_hiq;
  212. mqd->destroy_mqd = destroy_mqd;
  213. mqd->is_occupied = is_occupied;
  214. break;
  215. case KFD_MQD_TYPE_SDMA:
  216. break;
  217. default:
  218. kfree(mqd);
  219. return NULL;
  220. }
  221. return mqd;
  222. }