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