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