kfd_mqd_manager.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 "cik_regs.h"
  28. #include "../../radeon/cik_reg.h"
  29. inline void busy_wait(unsigned long ms)
  30. {
  31. while (time_before(jiffies, ms))
  32. cpu_relax();
  33. }
  34. static inline struct cik_mqd *get_mqd(void *mqd)
  35. {
  36. return (struct cik_mqd *)mqd;
  37. }
  38. static int init_mqd(struct mqd_manager *mm, void **mqd,
  39. struct kfd_mem_obj **mqd_mem_obj, uint64_t *gart_addr,
  40. struct queue_properties *q)
  41. {
  42. uint64_t addr;
  43. struct cik_mqd *m;
  44. int retval;
  45. BUG_ON(!mm || !q || !mqd);
  46. pr_debug("kfd: In func %s\n", __func__);
  47. retval = kfd2kgd->allocate_mem(mm->dev->kgd,
  48. sizeof(struct cik_mqd),
  49. 256,
  50. KFD_MEMPOOL_SYSTEM_WRITECOMBINE,
  51. (struct kgd_mem **) mqd_mem_obj);
  52. if (retval != 0)
  53. return -ENOMEM;
  54. m = (struct cik_mqd *) (*mqd_mem_obj)->cpu_ptr;
  55. addr = (*mqd_mem_obj)->gpu_addr;
  56. memset(m, 0, ALIGN(sizeof(struct cik_mqd), 256));
  57. m->header = 0xC0310800;
  58. m->compute_pipelinestat_enable = 1;
  59. m->compute_static_thread_mgmt_se0 = 0xFFFFFFFF;
  60. m->compute_static_thread_mgmt_se1 = 0xFFFFFFFF;
  61. m->compute_static_thread_mgmt_se2 = 0xFFFFFFFF;
  62. m->compute_static_thread_mgmt_se3 = 0xFFFFFFFF;
  63. /*
  64. * Make sure to use the last queue state saved on mqd when the cp
  65. * reassigns the queue, so when queue is switched on/off (e.g over
  66. * subscription or quantum timeout) the context will be consistent
  67. */
  68. m->cp_hqd_persistent_state =
  69. DEFAULT_CP_HQD_PERSISTENT_STATE | PRELOAD_REQ;
  70. m->cp_mqd_control = MQD_CONTROL_PRIV_STATE_EN;
  71. m->cp_mqd_base_addr_lo = lower_32_bits(addr);
  72. m->cp_mqd_base_addr_hi = upper_32_bits(addr);
  73. m->cp_hqd_ib_control = DEFAULT_MIN_IB_AVAIL_SIZE | IB_ATC_EN;
  74. /* Although WinKFD writes this, I suspect it should not be necessary */
  75. m->cp_hqd_ib_control = IB_ATC_EN | DEFAULT_MIN_IB_AVAIL_SIZE;
  76. m->cp_hqd_quantum = QUANTUM_EN | QUANTUM_SCALE_1MS |
  77. QUANTUM_DURATION(10);
  78. /*
  79. * Pipe Priority
  80. * Identifies the pipe relative priority when this queue is connected
  81. * to the pipeline. The pipe priority is against the GFX pipe and HP3D.
  82. * In KFD we are using a fixed pipe priority set to CS_MEDIUM.
  83. * 0 = CS_LOW (typically below GFX)
  84. * 1 = CS_MEDIUM (typically between HP3D and GFX
  85. * 2 = CS_HIGH (typically above HP3D)
  86. */
  87. m->cp_hqd_pipe_priority = 1;
  88. m->cp_hqd_queue_priority = 15;
  89. *mqd = m;
  90. if (gart_addr != NULL)
  91. *gart_addr = addr;
  92. retval = mm->update_mqd(mm, m, q);
  93. return retval;
  94. }
  95. static void uninit_mqd(struct mqd_manager *mm, void *mqd,
  96. struct kfd_mem_obj *mqd_mem_obj)
  97. {
  98. BUG_ON(!mm || !mqd);
  99. kfd2kgd->free_mem(mm->dev->kgd, (struct kgd_mem *) mqd_mem_obj);
  100. }
  101. static int load_mqd(struct mqd_manager *mm, void *mqd, uint32_t pipe_id,
  102. uint32_t queue_id, uint32_t __user *wptr)
  103. {
  104. return kfd2kgd->hqd_load(mm->dev->kgd, mqd, pipe_id, queue_id, wptr);
  105. }
  106. static int update_mqd(struct mqd_manager *mm, void *mqd,
  107. struct queue_properties *q)
  108. {
  109. struct cik_mqd *m;
  110. BUG_ON(!mm || !q || !mqd);
  111. pr_debug("kfd: In func %s\n", __func__);
  112. m = get_mqd(mqd);
  113. m->cp_hqd_pq_control = DEFAULT_RPTR_BLOCK_SIZE |
  114. DEFAULT_MIN_AVAIL_SIZE | PQ_ATC_EN;
  115. /*
  116. * Calculating queue size which is log base 2 of actual queue size -1
  117. * dwords and another -1 for ffs
  118. */
  119. m->cp_hqd_pq_control |= ffs(q->queue_size / sizeof(unsigned int))
  120. - 1 - 1;
  121. m->cp_hqd_pq_base_lo = lower_32_bits((uint64_t)q->queue_address >> 8);
  122. m->cp_hqd_pq_base_hi = upper_32_bits((uint64_t)q->queue_address >> 8);
  123. m->cp_hqd_pq_rptr_report_addr_lo = lower_32_bits((uint64_t)q->read_ptr);
  124. m->cp_hqd_pq_rptr_report_addr_hi = upper_32_bits((uint64_t)q->read_ptr);
  125. m->cp_hqd_pq_doorbell_control = DOORBELL_EN |
  126. DOORBELL_OFFSET(q->doorbell_off);
  127. m->cp_hqd_vmid = q->vmid;
  128. if (q->format == KFD_QUEUE_FORMAT_AQL) {
  129. m->cp_hqd_iq_rptr = AQL_ENABLE;
  130. m->cp_hqd_pq_control |= NO_UPDATE_RPTR;
  131. }
  132. m->cp_hqd_active = 0;
  133. q->is_active = false;
  134. if (q->queue_size > 0 &&
  135. q->queue_address != 0 &&
  136. q->queue_percent > 0) {
  137. m->cp_hqd_active = 1;
  138. q->is_active = true;
  139. }
  140. return 0;
  141. }
  142. static int destroy_mqd(struct mqd_manager *mm, void *mqd,
  143. enum kfd_preempt_type type,
  144. unsigned int timeout, uint32_t pipe_id,
  145. uint32_t queue_id)
  146. {
  147. return kfd2kgd->hqd_destroy(mm->dev->kgd, type, timeout,
  148. pipe_id, queue_id);
  149. }
  150. static bool is_occupied(struct mqd_manager *mm, void *mqd,
  151. uint64_t queue_address, uint32_t pipe_id,
  152. uint32_t queue_id)
  153. {
  154. return kfd2kgd->hqd_is_occupied(mm->dev->kgd, queue_address,
  155. pipe_id, queue_id);
  156. }
  157. /*
  158. * HIQ MQD Implementation, concrete implementation for HIQ MQD implementation.
  159. * The HIQ queue in Kaveri is using the same MQD structure as all the user mode
  160. * queues but with different initial values.
  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. uint64_t addr;
  167. struct cik_mqd *m;
  168. int retval;
  169. BUG_ON(!mm || !q || !mqd || !mqd_mem_obj);
  170. pr_debug("kfd: In func %s\n", __func__);
  171. retval = kfd2kgd->allocate_mem(mm->dev->kgd,
  172. sizeof(struct cik_mqd),
  173. 256,
  174. KFD_MEMPOOL_SYSTEM_WRITECOMBINE,
  175. (struct kgd_mem **) mqd_mem_obj);
  176. if (retval != 0)
  177. return -ENOMEM;
  178. m = (struct cik_mqd *) (*mqd_mem_obj)->cpu_ptr;
  179. addr = (*mqd_mem_obj)->gpu_addr;
  180. memset(m, 0, ALIGN(sizeof(struct cik_mqd), 256));
  181. m->header = 0xC0310800;
  182. m->compute_pipelinestat_enable = 1;
  183. m->compute_static_thread_mgmt_se0 = 0xFFFFFFFF;
  184. m->compute_static_thread_mgmt_se1 = 0xFFFFFFFF;
  185. m->compute_static_thread_mgmt_se2 = 0xFFFFFFFF;
  186. m->compute_static_thread_mgmt_se3 = 0xFFFFFFFF;
  187. m->cp_hqd_persistent_state = DEFAULT_CP_HQD_PERSISTENT_STATE |
  188. PRELOAD_REQ;
  189. m->cp_hqd_quantum = QUANTUM_EN | QUANTUM_SCALE_1MS |
  190. QUANTUM_DURATION(10);
  191. m->cp_mqd_control = MQD_CONTROL_PRIV_STATE_EN;
  192. m->cp_mqd_base_addr_lo = lower_32_bits(addr);
  193. m->cp_mqd_base_addr_hi = upper_32_bits(addr);
  194. m->cp_hqd_ib_control = DEFAULT_MIN_IB_AVAIL_SIZE;
  195. /*
  196. * Pipe Priority
  197. * Identifies the pipe relative priority when this queue is connected
  198. * to the pipeline. The pipe priority is against the GFX pipe and HP3D.
  199. * In KFD we are using a fixed pipe priority set to CS_MEDIUM.
  200. * 0 = CS_LOW (typically below GFX)
  201. * 1 = CS_MEDIUM (typically between HP3D and GFX
  202. * 2 = CS_HIGH (typically above HP3D)
  203. */
  204. m->cp_hqd_pipe_priority = 1;
  205. m->cp_hqd_queue_priority = 15;
  206. *mqd = m;
  207. if (gart_addr)
  208. *gart_addr = addr;
  209. retval = mm->update_mqd(mm, m, q);
  210. return retval;
  211. }
  212. static int update_mqd_hiq(struct mqd_manager *mm, void *mqd,
  213. struct queue_properties *q)
  214. {
  215. struct cik_mqd *m;
  216. BUG_ON(!mm || !q || !mqd);
  217. pr_debug("kfd: In func %s\n", __func__);
  218. m = get_mqd(mqd);
  219. m->cp_hqd_pq_control = DEFAULT_RPTR_BLOCK_SIZE |
  220. DEFAULT_MIN_AVAIL_SIZE |
  221. PRIV_STATE |
  222. KMD_QUEUE;
  223. /*
  224. * Calculating queue size which is log base 2 of actual queue
  225. * size -1 dwords
  226. */
  227. m->cp_hqd_pq_control |= ffs(q->queue_size / sizeof(unsigned int))
  228. - 1 - 1;
  229. m->cp_hqd_pq_base_lo = lower_32_bits((uint64_t)q->queue_address >> 8);
  230. m->cp_hqd_pq_base_hi = upper_32_bits((uint64_t)q->queue_address >> 8);
  231. m->cp_hqd_pq_rptr_report_addr_lo = lower_32_bits((uint64_t)q->read_ptr);
  232. m->cp_hqd_pq_rptr_report_addr_hi = upper_32_bits((uint64_t)q->read_ptr);
  233. m->cp_hqd_pq_doorbell_control = DOORBELL_EN |
  234. DOORBELL_OFFSET(q->doorbell_off);
  235. m->cp_hqd_vmid = q->vmid;
  236. m->cp_hqd_active = 0;
  237. q->is_active = false;
  238. if (q->queue_size > 0 &&
  239. q->queue_address != 0 &&
  240. q->queue_percent > 0) {
  241. m->cp_hqd_active = 1;
  242. q->is_active = true;
  243. }
  244. return 0;
  245. }
  246. struct mqd_manager *mqd_manager_init(enum KFD_MQD_TYPE type,
  247. struct kfd_dev *dev)
  248. {
  249. struct mqd_manager *mqd;
  250. BUG_ON(!dev);
  251. BUG_ON(type >= KFD_MQD_TYPE_MAX);
  252. pr_debug("kfd: In func %s\n", __func__);
  253. mqd = kzalloc(sizeof(struct mqd_manager), GFP_KERNEL);
  254. if (!mqd)
  255. return NULL;
  256. mqd->dev = dev;
  257. switch (type) {
  258. case KFD_MQD_TYPE_CIK_CP:
  259. case KFD_MQD_TYPE_CIK_COMPUTE:
  260. mqd->init_mqd = init_mqd;
  261. mqd->uninit_mqd = uninit_mqd;
  262. mqd->load_mqd = load_mqd;
  263. mqd->update_mqd = update_mqd;
  264. mqd->destroy_mqd = destroy_mqd;
  265. mqd->is_occupied = is_occupied;
  266. break;
  267. case KFD_MQD_TYPE_CIK_HIQ:
  268. mqd->init_mqd = init_mqd_hiq;
  269. mqd->uninit_mqd = uninit_mqd;
  270. mqd->load_mqd = load_mqd;
  271. mqd->update_mqd = update_mqd_hiq;
  272. mqd->destroy_mqd = destroy_mqd;
  273. mqd->is_occupied = is_occupied;
  274. break;
  275. default:
  276. kfree(mqd);
  277. return NULL;
  278. }
  279. return mqd;
  280. }
  281. /* SDMA queues should be implemented here when the cp will supports them */