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