kfd_doorbell.c 7.9 KB

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