kfd_doorbell.c 7.9 KB

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