kfd_doorbell.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /*
  3. * Copyright 2014-2022 Advanced Micro Devices, Inc.
  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. #include "kfd_priv.h"
  24. #include <linux/mm.h>
  25. #include <linux/mman.h>
  26. #include <linux/slab.h>
  27. #include <linux/io.h>
  28. #include <linux/idr.h>
  29. /*
  30. * This extension supports a kernel level doorbells management for the
  31. * kernel queues using the first doorbell page reserved for the kernel.
  32. */
  33. /*
  34. * Each device exposes a doorbell aperture, a PCI MMIO aperture that
  35. * receives 32-bit writes that are passed to queues as wptr values.
  36. * The doorbells are intended to be written by applications as part
  37. * of queueing work on user-mode queues.
  38. * We assign doorbells to applications in PAGE_SIZE-sized and aligned chunks.
  39. * We map the doorbell address space into user-mode when a process creates
  40. * its first queue on each device.
  41. * Although the mapping is done by KFD, it is equivalent to an mmap of
  42. * the /dev/kfd with the particular device encoded in the mmap offset.
  43. * There will be other uses for mmap of /dev/kfd, so only a range of
  44. * offsets (KFD_MMAP_DOORBELL_START-END) is used for doorbells.
  45. */
  46. /* # of doorbell bytes allocated for each process. */
  47. size_t kfd_doorbell_process_slice(struct kfd_dev *kfd)
  48. {
  49. if (!kfd->shared_resources.enable_mes)
  50. return roundup(kfd->device_info.doorbell_size *
  51. KFD_MAX_NUM_OF_QUEUES_PER_PROCESS,
  52. PAGE_SIZE);
  53. else
  54. return amdgpu_mes_doorbell_process_slice(
  55. (struct amdgpu_device *)kfd->adev);
  56. }
  57. /* Doorbell calculations for device init. */
  58. int kfd_doorbell_init(struct kfd_dev *kfd)
  59. {
  60. int size = PAGE_SIZE;
  61. int r;
  62. /*
  63. * Todo: KFD kernel level operations need only one doorbell for
  64. * ring test/HWS. So instead of reserving a whole page here for
  65. * kernel, reserve and consume a doorbell from existing KGD kernel
  66. * doorbell page.
  67. */
  68. /* Bitmap to dynamically allocate doorbells from kernel page */
  69. kfd->doorbell_bitmap = bitmap_zalloc(size / sizeof(u32), GFP_KERNEL);
  70. if (!kfd->doorbell_bitmap) {
  71. DRM_ERROR("Failed to allocate kernel doorbell bitmap\n");
  72. return -ENOMEM;
  73. }
  74. /* Alloc a doorbell page for KFD kernel usages */
  75. r = amdgpu_bo_create_kernel(kfd->adev,
  76. size,
  77. PAGE_SIZE,
  78. AMDGPU_GEM_DOMAIN_DOORBELL,
  79. &kfd->doorbells,
  80. NULL,
  81. (void **)&kfd->doorbell_kernel_ptr);
  82. if (r) {
  83. pr_err("failed to allocate kernel doorbells\n");
  84. bitmap_free(kfd->doorbell_bitmap);
  85. return r;
  86. }
  87. pr_debug("Doorbell kernel address == %p\n", kfd->doorbell_kernel_ptr);
  88. return 0;
  89. }
  90. void kfd_doorbell_fini(struct kfd_dev *kfd)
  91. {
  92. bitmap_free(kfd->doorbell_bitmap);
  93. amdgpu_bo_free_kernel(&kfd->doorbells, NULL,
  94. (void **)&kfd->doorbell_kernel_ptr);
  95. }
  96. int kfd_doorbell_mmap(struct kfd_node *dev, struct kfd_process *process,
  97. struct vm_area_struct *vma)
  98. {
  99. phys_addr_t address;
  100. struct kfd_process_device *pdd;
  101. /*
  102. * For simplicitly we only allow mapping of the entire doorbell
  103. * allocation of a single device & process.
  104. */
  105. if (vma->vm_end - vma->vm_start != kfd_doorbell_process_slice(dev->kfd))
  106. return -EINVAL;
  107. pdd = kfd_get_process_device_data(dev, process);
  108. if (!pdd)
  109. return -EINVAL;
  110. /* Calculate physical address of doorbell */
  111. address = kfd_get_process_doorbells(pdd);
  112. if (!address)
  113. return -ENOMEM;
  114. vm_flags_set(vma, VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_NORESERVE |
  115. VM_DONTDUMP | VM_PFNMAP);
  116. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  117. pr_debug("Mapping doorbell page\n"
  118. " target user address == 0x%08llX\n"
  119. " physical address == 0x%08llX\n"
  120. " vm_flags == 0x%04lX\n"
  121. " size == 0x%04lX\n",
  122. (unsigned long long) vma->vm_start, address, vma->vm_flags,
  123. kfd_doorbell_process_slice(dev->kfd));
  124. return io_remap_pfn_range(vma,
  125. vma->vm_start,
  126. address >> PAGE_SHIFT,
  127. kfd_doorbell_process_slice(dev->kfd),
  128. vma->vm_page_prot);
  129. }
  130. /* get kernel iomem pointer for a doorbell */
  131. void __iomem *kfd_get_kernel_doorbell(struct kfd_dev *kfd,
  132. unsigned int *doorbell_off)
  133. {
  134. u32 inx;
  135. mutex_lock(&kfd->doorbell_mutex);
  136. inx = find_first_zero_bit(kfd->doorbell_bitmap, PAGE_SIZE / sizeof(u32));
  137. __set_bit(inx, kfd->doorbell_bitmap);
  138. mutex_unlock(&kfd->doorbell_mutex);
  139. if (inx >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS)
  140. return NULL;
  141. *doorbell_off = amdgpu_doorbell_index_on_bar(kfd->adev,
  142. kfd->doorbells,
  143. inx,
  144. kfd->device_info.doorbell_size);
  145. inx *= 2;
  146. pr_debug("Get kernel queue doorbell\n"
  147. " doorbell offset == 0x%08X\n"
  148. " doorbell index == 0x%x\n",
  149. *doorbell_off, inx);
  150. return kfd->doorbell_kernel_ptr + inx;
  151. }
  152. void kfd_release_kernel_doorbell(struct kfd_dev *kfd, u32 __iomem *db_addr)
  153. {
  154. unsigned int inx;
  155. inx = (unsigned int)(db_addr - kfd->doorbell_kernel_ptr);
  156. inx /= 2;
  157. mutex_lock(&kfd->doorbell_mutex);
  158. __clear_bit(inx, kfd->doorbell_bitmap);
  159. mutex_unlock(&kfd->doorbell_mutex);
  160. }
  161. void write_kernel_doorbell(void __iomem *db, u32 value)
  162. {
  163. if (db) {
  164. writel(value, db);
  165. pr_debug("Writing %d to doorbell address %p\n", value, db);
  166. }
  167. }
  168. void write_kernel_doorbell64(void __iomem *db, u64 value)
  169. {
  170. if (db) {
  171. WARN(((unsigned long)db & 7) != 0,
  172. "Unaligned 64-bit doorbell");
  173. writeq(value, (u64 __iomem *)db);
  174. pr_debug("writing %llu to doorbell address %p\n", value, db);
  175. }
  176. }
  177. static int init_doorbell_bitmap(struct qcm_process_device *qpd,
  178. struct kfd_dev *dev)
  179. {
  180. unsigned int i;
  181. int range_start = dev->shared_resources.non_cp_doorbells_start;
  182. int range_end = dev->shared_resources.non_cp_doorbells_end;
  183. if (!KFD_IS_SOC15(dev))
  184. return 0;
  185. /* Mask out doorbells reserved for SDMA, IH, and VCN on SOC15. */
  186. pr_debug("reserved doorbell 0x%03x - 0x%03x\n", range_start, range_end);
  187. pr_debug("reserved doorbell 0x%03x - 0x%03x\n",
  188. range_start + KFD_QUEUE_DOORBELL_MIRROR_OFFSET,
  189. range_end + KFD_QUEUE_DOORBELL_MIRROR_OFFSET);
  190. for (i = 0; i < KFD_MAX_NUM_OF_QUEUES_PER_PROCESS / 2; i++) {
  191. if (i >= range_start && i <= range_end) {
  192. __set_bit(i, qpd->doorbell_bitmap);
  193. __set_bit(i + KFD_QUEUE_DOORBELL_MIRROR_OFFSET,
  194. qpd->doorbell_bitmap);
  195. }
  196. }
  197. return 0;
  198. }
  199. phys_addr_t kfd_get_process_doorbells(struct kfd_process_device *pdd)
  200. {
  201. struct amdgpu_device *adev = pdd->dev->adev;
  202. uint32_t first_db_index;
  203. if (!pdd->qpd.proc_doorbells) {
  204. if (kfd_alloc_process_doorbells(pdd->dev->kfd, pdd))
  205. /* phys_addr_t 0 is error */
  206. return 0;
  207. }
  208. first_db_index = amdgpu_doorbell_index_on_bar(adev,
  209. pdd->qpd.proc_doorbells,
  210. 0,
  211. pdd->dev->kfd->device_info.doorbell_size);
  212. return adev->doorbell.base + first_db_index * sizeof(uint32_t);
  213. }
  214. int kfd_alloc_process_doorbells(struct kfd_dev *kfd, struct kfd_process_device *pdd)
  215. {
  216. int r;
  217. struct qcm_process_device *qpd = &pdd->qpd;
  218. /* Allocate bitmap for dynamic doorbell allocation */
  219. qpd->doorbell_bitmap = bitmap_zalloc(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS,
  220. GFP_KERNEL);
  221. if (!qpd->doorbell_bitmap) {
  222. DRM_ERROR("Failed to allocate process doorbell bitmap\n");
  223. return -ENOMEM;
  224. }
  225. r = init_doorbell_bitmap(&pdd->qpd, kfd);
  226. if (r) {
  227. DRM_ERROR("Failed to initialize process doorbells\n");
  228. r = -ENOMEM;
  229. goto err;
  230. }
  231. /* Allocate doorbells for this process */
  232. r = amdgpu_bo_create_kernel(kfd->adev,
  233. kfd_doorbell_process_slice(kfd),
  234. PAGE_SIZE,
  235. AMDGPU_GEM_DOMAIN_DOORBELL,
  236. &qpd->proc_doorbells,
  237. NULL,
  238. NULL);
  239. if (r) {
  240. DRM_ERROR("Failed to allocate process doorbells\n");
  241. goto err;
  242. }
  243. return 0;
  244. err:
  245. bitmap_free(qpd->doorbell_bitmap);
  246. qpd->doorbell_bitmap = NULL;
  247. return r;
  248. }
  249. void kfd_free_process_doorbells(struct kfd_dev *kfd, struct kfd_process_device *pdd)
  250. {
  251. struct qcm_process_device *qpd = &pdd->qpd;
  252. if (qpd->doorbell_bitmap) {
  253. bitmap_free(qpd->doorbell_bitmap);
  254. qpd->doorbell_bitmap = NULL;
  255. }
  256. amdgpu_bo_free_kernel(&qpd->proc_doorbells, NULL, NULL);
  257. }