kfd_doorbell.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. #include "kfd_priv.h"
  23. #include <linux/mm.h>
  24. #include <linux/mman.h>
  25. #include <linux/slab.h>
  26. #include <linux/io.h>
  27. #include <linux/idr.h>
  28. /*
  29. * This extension supports a kernel level doorbells management for the
  30. * kernel queues using the first doorbell page reserved for the kernel.
  31. */
  32. static DEFINE_IDA(doorbell_ida);
  33. static unsigned int max_doorbell_slices;
  34. /*
  35. * Each device exposes a doorbell aperture, a PCI MMIO aperture that
  36. * receives 32-bit writes that are passed to queues as wptr values.
  37. * The doorbells are intended to be written by applications as part
  38. * of queueing work on user-mode queues.
  39. * We assign doorbells to applications in PAGE_SIZE-sized and aligned chunks.
  40. * We map the doorbell address space into user-mode when a process creates
  41. * its first queue on each device.
  42. * Although the mapping is done by KFD, it is equivalent to an mmap of
  43. * the /dev/kfd with the particular device encoded in the mmap offset.
  44. * There will be other uses for mmap of /dev/kfd, so only a range of
  45. * offsets (KFD_MMAP_DOORBELL_START-END) is used for doorbells.
  46. */
  47. /* # of doorbell bytes allocated for each process. */
  48. size_t kfd_doorbell_process_slice(struct kfd_dev *kfd)
  49. {
  50. return roundup(kfd->device_info->doorbell_size *
  51. KFD_MAX_NUM_OF_QUEUES_PER_PROCESS,
  52. PAGE_SIZE);
  53. }
  54. /* Doorbell calculations for device init. */
  55. int kfd_doorbell_init(struct kfd_dev *kfd)
  56. {
  57. size_t doorbell_start_offset;
  58. size_t doorbell_aperture_size;
  59. size_t doorbell_process_limit;
  60. /*
  61. * We start with calculations in bytes because the input data might
  62. * only be byte-aligned.
  63. * Only after we have done the rounding can we assume any alignment.
  64. */
  65. doorbell_start_offset =
  66. roundup(kfd->shared_resources.doorbell_start_offset,
  67. kfd_doorbell_process_slice(kfd));
  68. doorbell_aperture_size =
  69. rounddown(kfd->shared_resources.doorbell_aperture_size,
  70. kfd_doorbell_process_slice(kfd));
  71. if (doorbell_aperture_size > doorbell_start_offset)
  72. doorbell_process_limit =
  73. (doorbell_aperture_size - doorbell_start_offset) /
  74. kfd_doorbell_process_slice(kfd);
  75. else
  76. return -ENOSPC;
  77. if (!max_doorbell_slices ||
  78. doorbell_process_limit < max_doorbell_slices)
  79. max_doorbell_slices = doorbell_process_limit;
  80. kfd->doorbell_base = kfd->shared_resources.doorbell_physical_address +
  81. doorbell_start_offset;
  82. kfd->doorbell_id_offset = doorbell_start_offset / sizeof(u32);
  83. kfd->doorbell_kernel_ptr = ioremap(kfd->doorbell_base,
  84. kfd_doorbell_process_slice(kfd));
  85. if (!kfd->doorbell_kernel_ptr)
  86. return -ENOMEM;
  87. pr_debug("Doorbell initialization:\n");
  88. pr_debug("doorbell base == 0x%08lX\n",
  89. (uintptr_t)kfd->doorbell_base);
  90. pr_debug("doorbell_id_offset == 0x%08lX\n",
  91. kfd->doorbell_id_offset);
  92. pr_debug("doorbell_process_limit == 0x%08lX\n",
  93. doorbell_process_limit);
  94. pr_debug("doorbell_kernel_offset == 0x%08lX\n",
  95. (uintptr_t)kfd->doorbell_base);
  96. pr_debug("doorbell aperture size == 0x%08lX\n",
  97. kfd->shared_resources.doorbell_aperture_size);
  98. pr_debug("doorbell kernel address == %p\n", kfd->doorbell_kernel_ptr);
  99. return 0;
  100. }
  101. void kfd_doorbell_fini(struct kfd_dev *kfd)
  102. {
  103. if (kfd->doorbell_kernel_ptr)
  104. iounmap(kfd->doorbell_kernel_ptr);
  105. }
  106. int kfd_doorbell_mmap(struct kfd_dev *dev, struct kfd_process *process,
  107. struct vm_area_struct *vma)
  108. {
  109. phys_addr_t address;
  110. /*
  111. * For simplicitly we only allow mapping of the entire doorbell
  112. * allocation of a single device & process.
  113. */
  114. if (vma->vm_end - vma->vm_start != kfd_doorbell_process_slice(dev))
  115. return -EINVAL;
  116. /* Calculate physical address of doorbell */
  117. address = kfd_get_process_doorbells(dev, process);
  118. vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_NORESERVE |
  119. VM_DONTDUMP | VM_PFNMAP;
  120. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  121. pr_debug("Mapping doorbell page\n"
  122. " target user address == 0x%08llX\n"
  123. " physical address == 0x%08llX\n"
  124. " vm_flags == 0x%04lX\n"
  125. " size == 0x%04lX\n",
  126. (unsigned long long) vma->vm_start, address, vma->vm_flags,
  127. kfd_doorbell_process_slice(dev));
  128. return io_remap_pfn_range(vma,
  129. vma->vm_start,
  130. address >> PAGE_SHIFT,
  131. kfd_doorbell_process_slice(dev),
  132. vma->vm_page_prot);
  133. }
  134. /* get kernel iomem pointer for a doorbell */
  135. void __iomem *kfd_get_kernel_doorbell(struct kfd_dev *kfd,
  136. unsigned int *doorbell_off)
  137. {
  138. u32 inx;
  139. mutex_lock(&kfd->doorbell_mutex);
  140. inx = find_first_zero_bit(kfd->doorbell_available_index,
  141. KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
  142. __set_bit(inx, kfd->doorbell_available_index);
  143. mutex_unlock(&kfd->doorbell_mutex);
  144. if (inx >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS)
  145. return NULL;
  146. inx *= kfd->device_info->doorbell_size / sizeof(u32);
  147. /*
  148. * Calculating the kernel doorbell offset using the first
  149. * doorbell page.
  150. */
  151. *doorbell_off = kfd->doorbell_id_offset + inx;
  152. pr_debug("Get kernel queue doorbell\n"
  153. " doorbell offset == 0x%08X\n"
  154. " doorbell index == 0x%x\n",
  155. *doorbell_off, inx);
  156. return kfd->doorbell_kernel_ptr + inx;
  157. }
  158. void kfd_release_kernel_doorbell(struct kfd_dev *kfd, u32 __iomem *db_addr)
  159. {
  160. unsigned int inx;
  161. inx = (unsigned int)(db_addr - kfd->doorbell_kernel_ptr)
  162. * sizeof(u32) / kfd->device_info->doorbell_size;
  163. mutex_lock(&kfd->doorbell_mutex);
  164. __clear_bit(inx, kfd->doorbell_available_index);
  165. mutex_unlock(&kfd->doorbell_mutex);
  166. }
  167. void write_kernel_doorbell(void __iomem *db, u32 value)
  168. {
  169. if (db) {
  170. writel(value, db);
  171. pr_debug("Writing %d to doorbell address %p\n", value, db);
  172. }
  173. }
  174. void write_kernel_doorbell64(void __iomem *db, u64 value)
  175. {
  176. if (db) {
  177. WARN(((unsigned long)db & 7) != 0,
  178. "Unaligned 64-bit doorbell");
  179. writeq(value, (u64 __iomem *)db);
  180. pr_debug("writing %llu to doorbell address %p\n", value, db);
  181. }
  182. }
  183. unsigned int kfd_doorbell_id_to_offset(struct kfd_dev *kfd,
  184. struct kfd_process *process,
  185. unsigned int doorbell_id)
  186. {
  187. /*
  188. * doorbell_id_offset accounts for doorbells taken by KGD.
  189. * index * kfd_doorbell_process_slice/sizeof(u32) adjusts to
  190. * the process's doorbells. The offset returned is in dword
  191. * units regardless of the ASIC-dependent doorbell size.
  192. */
  193. return kfd->doorbell_id_offset +
  194. process->doorbell_index
  195. * kfd_doorbell_process_slice(kfd) / sizeof(u32) +
  196. doorbell_id * kfd->device_info->doorbell_size / sizeof(u32);
  197. }
  198. uint64_t kfd_get_number_elems(struct kfd_dev *kfd)
  199. {
  200. uint64_t num_of_elems = (kfd->shared_resources.doorbell_aperture_size -
  201. kfd->shared_resources.doorbell_start_offset) /
  202. kfd_doorbell_process_slice(kfd) + 1;
  203. return num_of_elems;
  204. }
  205. phys_addr_t kfd_get_process_doorbells(struct kfd_dev *dev,
  206. struct kfd_process *process)
  207. {
  208. return dev->doorbell_base +
  209. process->doorbell_index * kfd_doorbell_process_slice(dev);
  210. }
  211. int kfd_alloc_process_doorbells(struct kfd_process *process)
  212. {
  213. int r = ida_simple_get(&doorbell_ida, 1, max_doorbell_slices,
  214. GFP_KERNEL);
  215. if (r > 0)
  216. process->doorbell_index = r;
  217. return r;
  218. }
  219. void kfd_free_process_doorbells(struct kfd_process *process)
  220. {
  221. if (process->doorbell_index)
  222. ida_simple_remove(&doorbell_ida, process->doorbell_index);
  223. }