kfd_kernel_queue.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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/types.h>
  24. #include <linux/mutex.h>
  25. #include <linux/slab.h>
  26. #include <linux/printk.h>
  27. #include <linux/sched.h>
  28. #include "kfd_kernel_queue.h"
  29. #include "kfd_priv.h"
  30. #include "kfd_device_queue_manager.h"
  31. #include "kfd_pm4_headers.h"
  32. #include "kfd_pm4_opcodes.h"
  33. #define PM4_COUNT_ZERO (((1 << 15) - 1) << 16)
  34. static bool initialize(struct kernel_queue *kq, struct kfd_dev *dev,
  35. enum kfd_queue_type type, unsigned int queue_size)
  36. {
  37. struct queue_properties prop;
  38. int retval;
  39. union PM4_MES_TYPE_3_HEADER nop;
  40. BUG_ON(!kq || !dev);
  41. BUG_ON(type != KFD_QUEUE_TYPE_DIQ && type != KFD_QUEUE_TYPE_HIQ);
  42. pr_debug("amdkfd: In func %s initializing queue type %d size %d\n",
  43. __func__, KFD_QUEUE_TYPE_HIQ, queue_size);
  44. nop.opcode = IT_NOP;
  45. nop.type = PM4_TYPE_3;
  46. nop.u32all |= PM4_COUNT_ZERO;
  47. kq->dev = dev;
  48. kq->nop_packet = nop.u32all;
  49. switch (type) {
  50. case KFD_QUEUE_TYPE_DIQ:
  51. case KFD_QUEUE_TYPE_HIQ:
  52. kq->mqd = dev->dqm->ops.get_mqd_manager(dev->dqm,
  53. KFD_MQD_TYPE_HIQ);
  54. break;
  55. default:
  56. BUG();
  57. break;
  58. }
  59. if (kq->mqd == NULL)
  60. return false;
  61. prop.doorbell_ptr = kfd_get_kernel_doorbell(dev, &prop.doorbell_off);
  62. if (prop.doorbell_ptr == NULL) {
  63. pr_err("amdkfd: error init doorbell");
  64. goto err_get_kernel_doorbell;
  65. }
  66. retval = kfd_gtt_sa_allocate(dev, queue_size, &kq->pq);
  67. if (retval != 0) {
  68. pr_err("amdkfd: error init pq queues size (%d)\n", queue_size);
  69. goto err_pq_allocate_vidmem;
  70. }
  71. kq->pq_kernel_addr = kq->pq->cpu_ptr;
  72. kq->pq_gpu_addr = kq->pq->gpu_addr;
  73. retval = kq->ops_asic_specific.initialize(kq, dev, type, queue_size);
  74. if (retval == false)
  75. goto err_eop_allocate_vidmem;
  76. retval = kfd_gtt_sa_allocate(dev, sizeof(*kq->rptr_kernel),
  77. &kq->rptr_mem);
  78. if (retval != 0)
  79. goto err_rptr_allocate_vidmem;
  80. kq->rptr_kernel = kq->rptr_mem->cpu_ptr;
  81. kq->rptr_gpu_addr = kq->rptr_mem->gpu_addr;
  82. retval = kfd_gtt_sa_allocate(dev, sizeof(*kq->wptr_kernel),
  83. &kq->wptr_mem);
  84. if (retval != 0)
  85. goto err_wptr_allocate_vidmem;
  86. kq->wptr_kernel = kq->wptr_mem->cpu_ptr;
  87. kq->wptr_gpu_addr = kq->wptr_mem->gpu_addr;
  88. memset(kq->pq_kernel_addr, 0, queue_size);
  89. memset(kq->rptr_kernel, 0, sizeof(*kq->rptr_kernel));
  90. memset(kq->wptr_kernel, 0, sizeof(*kq->wptr_kernel));
  91. prop.queue_size = queue_size;
  92. prop.is_interop = false;
  93. prop.priority = 1;
  94. prop.queue_percent = 100;
  95. prop.type = type;
  96. prop.vmid = 0;
  97. prop.queue_address = kq->pq_gpu_addr;
  98. prop.read_ptr = (uint32_t *) kq->rptr_gpu_addr;
  99. prop.write_ptr = (uint32_t *) kq->wptr_gpu_addr;
  100. prop.eop_ring_buffer_address = kq->eop_gpu_addr;
  101. prop.eop_ring_buffer_size = PAGE_SIZE;
  102. if (init_queue(&kq->queue, prop) != 0)
  103. goto err_init_queue;
  104. kq->queue->device = dev;
  105. kq->queue->process = kfd_get_process(current);
  106. retval = kq->mqd->init_mqd(kq->mqd, &kq->queue->mqd,
  107. &kq->queue->mqd_mem_obj,
  108. &kq->queue->gart_mqd_addr,
  109. &kq->queue->properties);
  110. if (retval != 0)
  111. goto err_init_mqd;
  112. /* assign HIQ to HQD */
  113. if (type == KFD_QUEUE_TYPE_HIQ) {
  114. pr_debug("assigning hiq to hqd\n");
  115. kq->queue->pipe = KFD_CIK_HIQ_PIPE;
  116. kq->queue->queue = KFD_CIK_HIQ_QUEUE;
  117. kq->mqd->load_mqd(kq->mqd, kq->queue->mqd, kq->queue->pipe,
  118. kq->queue->queue, NULL);
  119. } else {
  120. /* allocate fence for DIQ */
  121. retval = kfd_gtt_sa_allocate(dev, sizeof(uint32_t),
  122. &kq->fence_mem_obj);
  123. if (retval != 0)
  124. goto err_alloc_fence;
  125. kq->fence_kernel_address = kq->fence_mem_obj->cpu_ptr;
  126. kq->fence_gpu_addr = kq->fence_mem_obj->gpu_addr;
  127. }
  128. print_queue(kq->queue);
  129. return true;
  130. err_alloc_fence:
  131. err_init_mqd:
  132. uninit_queue(kq->queue);
  133. err_init_queue:
  134. kfd_gtt_sa_free(dev, kq->wptr_mem);
  135. err_wptr_allocate_vidmem:
  136. kfd_gtt_sa_free(dev, kq->rptr_mem);
  137. err_rptr_allocate_vidmem:
  138. kfd_gtt_sa_free(dev, kq->eop_mem);
  139. err_eop_allocate_vidmem:
  140. kfd_gtt_sa_free(dev, kq->pq);
  141. err_pq_allocate_vidmem:
  142. kfd_release_kernel_doorbell(dev, prop.doorbell_ptr);
  143. err_get_kernel_doorbell:
  144. return false;
  145. }
  146. static void uninitialize(struct kernel_queue *kq)
  147. {
  148. BUG_ON(!kq);
  149. if (kq->queue->properties.type == KFD_QUEUE_TYPE_HIQ)
  150. kq->mqd->destroy_mqd(kq->mqd,
  151. NULL,
  152. false,
  153. QUEUE_PREEMPT_DEFAULT_TIMEOUT_MS,
  154. kq->queue->pipe,
  155. kq->queue->queue);
  156. else if (kq->queue->properties.type == KFD_QUEUE_TYPE_DIQ)
  157. kfd_gtt_sa_free(kq->dev, kq->fence_mem_obj);
  158. kq->mqd->uninit_mqd(kq->mqd, kq->queue->mqd, kq->queue->mqd_mem_obj);
  159. kfd_gtt_sa_free(kq->dev, kq->rptr_mem);
  160. kfd_gtt_sa_free(kq->dev, kq->wptr_mem);
  161. kq->ops_asic_specific.uninitialize(kq);
  162. kfd_gtt_sa_free(kq->dev, kq->pq);
  163. kfd_release_kernel_doorbell(kq->dev,
  164. kq->queue->properties.doorbell_ptr);
  165. uninit_queue(kq->queue);
  166. }
  167. static int acquire_packet_buffer(struct kernel_queue *kq,
  168. size_t packet_size_in_dwords, unsigned int **buffer_ptr)
  169. {
  170. size_t available_size;
  171. size_t queue_size_dwords;
  172. uint32_t wptr, rptr;
  173. unsigned int *queue_address;
  174. BUG_ON(!kq || !buffer_ptr);
  175. rptr = *kq->rptr_kernel;
  176. wptr = *kq->wptr_kernel;
  177. queue_address = (unsigned int *)kq->pq_kernel_addr;
  178. queue_size_dwords = kq->queue->properties.queue_size / sizeof(uint32_t);
  179. pr_debug("rptr: %d\n", rptr);
  180. pr_debug("wptr: %d\n", wptr);
  181. pr_debug("queue_address 0x%p\n", queue_address);
  182. available_size = (rptr - 1 - wptr + queue_size_dwords) %
  183. queue_size_dwords;
  184. if (packet_size_in_dwords >= queue_size_dwords ||
  185. packet_size_in_dwords >= available_size) {
  186. /*
  187. * make sure calling functions know
  188. * acquire_packet_buffer() failed
  189. */
  190. *buffer_ptr = NULL;
  191. return -ENOMEM;
  192. }
  193. if (wptr + packet_size_in_dwords >= queue_size_dwords) {
  194. while (wptr > 0) {
  195. queue_address[wptr] = kq->nop_packet;
  196. wptr = (wptr + 1) % queue_size_dwords;
  197. }
  198. }
  199. *buffer_ptr = &queue_address[wptr];
  200. kq->pending_wptr = wptr + packet_size_in_dwords;
  201. return 0;
  202. }
  203. static void submit_packet(struct kernel_queue *kq)
  204. {
  205. #ifdef DEBUG
  206. int i;
  207. #endif
  208. BUG_ON(!kq);
  209. #ifdef DEBUG
  210. for (i = *kq->wptr_kernel; i < kq->pending_wptr; i++) {
  211. pr_debug("0x%2X ", kq->pq_kernel_addr[i]);
  212. if (i % 15 == 0)
  213. pr_debug("\n");
  214. }
  215. pr_debug("\n");
  216. #endif
  217. *kq->wptr_kernel = kq->pending_wptr;
  218. write_kernel_doorbell(kq->queue->properties.doorbell_ptr,
  219. kq->pending_wptr);
  220. }
  221. static void rollback_packet(struct kernel_queue *kq)
  222. {
  223. BUG_ON(!kq);
  224. kq->pending_wptr = *kq->queue->properties.write_ptr;
  225. }
  226. struct kernel_queue *kernel_queue_init(struct kfd_dev *dev,
  227. enum kfd_queue_type type)
  228. {
  229. struct kernel_queue *kq;
  230. BUG_ON(!dev);
  231. kq = kzalloc(sizeof(struct kernel_queue), GFP_KERNEL);
  232. if (!kq)
  233. return NULL;
  234. kq->ops.initialize = initialize;
  235. kq->ops.uninitialize = uninitialize;
  236. kq->ops.acquire_packet_buffer = acquire_packet_buffer;
  237. kq->ops.submit_packet = submit_packet;
  238. kq->ops.rollback_packet = rollback_packet;
  239. switch (dev->device_info->asic_family) {
  240. case CHIP_CARRIZO:
  241. kernel_queue_init_vi(&kq->ops_asic_specific);
  242. break;
  243. case CHIP_KAVERI:
  244. kernel_queue_init_cik(&kq->ops_asic_specific);
  245. break;
  246. }
  247. if (kq->ops.initialize(kq, dev, type, KFD_KERNEL_QUEUE_SIZE) == false) {
  248. pr_err("amdkfd: failed to init kernel queue\n");
  249. kfree(kq);
  250. return NULL;
  251. }
  252. return kq;
  253. }
  254. void kernel_queue_uninit(struct kernel_queue *kq)
  255. {
  256. BUG_ON(!kq);
  257. kq->ops.uninitialize(kq);
  258. kfree(kq);
  259. }
  260. static __attribute__((unused)) void test_kq(struct kfd_dev *dev)
  261. {
  262. struct kernel_queue *kq;
  263. uint32_t *buffer, i;
  264. int retval;
  265. BUG_ON(!dev);
  266. pr_err("amdkfd: starting kernel queue test\n");
  267. kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_HIQ);
  268. BUG_ON(!kq);
  269. retval = kq->ops.acquire_packet_buffer(kq, 5, &buffer);
  270. BUG_ON(retval != 0);
  271. for (i = 0; i < 5; i++)
  272. buffer[i] = kq->nop_packet;
  273. kq->ops.submit_packet(kq);
  274. pr_err("amdkfd: ending kernel queue test\n");
  275. }