1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3  * Copyright 2014-2022 Advanced Micro Devices, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #include "kfd_priv.h"
24 #include <linux/mm.h>
25 #include <linux/mman.h>
26 #include <linux/slab.h>
27 #include <linux/io.h>
28 #include <linux/idr.h>
29 
30 /*
31  * This extension supports a kernel level doorbells management for the
32  * kernel queues using the first doorbell page reserved for the kernel.
33  */
34 
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 
49 /* # of doorbell bytes allocated for each process. */
50 size_t kfd_doorbell_process_slice(struct kfd_dev *kfd)
51 {
52 	return roundup(kfd->device_info.doorbell_size *
53 			KFD_MAX_NUM_OF_QUEUES_PER_PROCESS,
54 			PAGE_SIZE);
55 }
56 
57 /* Doorbell calculations for device init. */
58 int kfd_doorbell_init(struct kfd_dev *kfd)
59 {
60 	size_t doorbell_start_offset;
61 	size_t doorbell_aperture_size;
62 	size_t doorbell_process_limit;
63 
64 	/*
65 	 * We start with calculations in bytes because the input data might
66 	 * only be byte-aligned.
67 	 * Only after we have done the rounding can we assume any alignment.
68 	 */
69 
70 	doorbell_start_offset =
71 			roundup(kfd->shared_resources.doorbell_start_offset,
72 					kfd_doorbell_process_slice(kfd));
73 
74 	doorbell_aperture_size =
75 			rounddown(kfd->shared_resources.doorbell_aperture_size,
76 					kfd_doorbell_process_slice(kfd));
77 
78 	if (doorbell_aperture_size > doorbell_start_offset)
79 		doorbell_process_limit =
80 			(doorbell_aperture_size - doorbell_start_offset) /
81 						kfd_doorbell_process_slice(kfd);
82 	else
83 		return -ENOSPC;
84 
85 	if (!kfd->max_doorbell_slices ||
86 	    doorbell_process_limit < kfd->max_doorbell_slices)
87 		kfd->max_doorbell_slices = doorbell_process_limit;
88 
89 	kfd->doorbell_base = kfd->shared_resources.doorbell_physical_address +
90 				doorbell_start_offset;
91 
92 	kfd->doorbell_base_dw_offset = doorbell_start_offset / sizeof(u32);
93 
94 	kfd->doorbell_kernel_ptr = ioremap(kfd->doorbell_base,
95 					   kfd_doorbell_process_slice(kfd));
96 
97 	if (!kfd->doorbell_kernel_ptr)
98 		return -ENOMEM;
99 
100 	pr_debug("Doorbell initialization:\n");
101 	pr_debug("doorbell base           == 0x%08lX\n",
102 			(uintptr_t)kfd->doorbell_base);
103 
104 	pr_debug("doorbell_base_dw_offset      == 0x%08lX\n",
105 			kfd->doorbell_base_dw_offset);
106 
107 	pr_debug("doorbell_process_limit  == 0x%08lX\n",
108 			doorbell_process_limit);
109 
110 	pr_debug("doorbell_kernel_offset  == 0x%08lX\n",
111 			(uintptr_t)kfd->doorbell_base);
112 
113 	pr_debug("doorbell aperture size  == 0x%08lX\n",
114 			kfd->shared_resources.doorbell_aperture_size);
115 
116 	pr_debug("doorbell kernel address == %p\n", kfd->doorbell_kernel_ptr);
117 
118 	return 0;
119 }
120 
121 void kfd_doorbell_fini(struct kfd_dev *kfd)
122 {
123 	if (kfd->doorbell_kernel_ptr)
124 		iounmap(kfd->doorbell_kernel_ptr);
125 }
126 
127 int kfd_doorbell_mmap(struct kfd_dev *dev, struct kfd_process *process,
128 		      struct vm_area_struct *vma)
129 {
130 	phys_addr_t address;
131 	struct kfd_process_device *pdd;
132 
133 	/*
134 	 * For simplicitly we only allow mapping of the entire doorbell
135 	 * allocation of a single device & process.
136 	 */
137 	if (vma->vm_end - vma->vm_start != kfd_doorbell_process_slice(dev))
138 		return -EINVAL;
139 
140 	pdd = kfd_get_process_device_data(dev, process);
141 	if (!pdd)
142 		return -EINVAL;
143 
144 	/* Calculate physical address of doorbell */
145 	address = kfd_get_process_doorbells(pdd);
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("Mapping doorbell page\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 		 kfd_doorbell_process_slice(dev));
158 
159 
160 	return io_remap_pfn_range(vma,
161 				vma->vm_start,
162 				address >> PAGE_SHIFT,
163 				kfd_doorbell_process_slice(dev),
164 				vma->vm_page_prot);
165 }
166 
167 
168 /* get kernel iomem pointer for a doorbell */
169 void __iomem *kfd_get_kernel_doorbell(struct kfd_dev *kfd,
170 					unsigned int *doorbell_off)
171 {
172 	u32 inx;
173 
174 	mutex_lock(&kfd->doorbell_mutex);
175 	inx = find_first_zero_bit(kfd->doorbell_available_index,
176 					KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
177 
178 	__set_bit(inx, kfd->doorbell_available_index);
179 	mutex_unlock(&kfd->doorbell_mutex);
180 
181 	if (inx >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS)
182 		return NULL;
183 
184 	inx *= kfd->device_info.doorbell_size / sizeof(u32);
185 
186 	/*
187 	 * Calculating the kernel doorbell offset using the first
188 	 * doorbell page.
189 	 */
190 	*doorbell_off = kfd->doorbell_base_dw_offset + inx;
191 
192 	pr_debug("Get kernel queue doorbell\n"
193 			"     doorbell offset   == 0x%08X\n"
194 			"     doorbell index    == 0x%x\n",
195 		*doorbell_off, inx);
196 
197 	return kfd->doorbell_kernel_ptr + inx;
198 }
199 
200 void kfd_release_kernel_doorbell(struct kfd_dev *kfd, u32 __iomem *db_addr)
201 {
202 	unsigned int inx;
203 
204 	inx = (unsigned int)(db_addr - kfd->doorbell_kernel_ptr)
205 		* sizeof(u32) / kfd->device_info.doorbell_size;
206 
207 	mutex_lock(&kfd->doorbell_mutex);
208 	__clear_bit(inx, kfd->doorbell_available_index);
209 	mutex_unlock(&kfd->doorbell_mutex);
210 }
211 
212 void write_kernel_doorbell(void __iomem *db, u32 value)
213 {
214 	if (db) {
215 		writel(value, db);
216 		pr_debug("Writing %d to doorbell address %p\n", value, db);
217 	}
218 }
219 
220 void write_kernel_doorbell64(void __iomem *db, u64 value)
221 {
222 	if (db) {
223 		WARN(((unsigned long)db & 7) != 0,
224 		     "Unaligned 64-bit doorbell");
225 		writeq(value, (u64 __iomem *)db);
226 		pr_debug("writing %llu to doorbell address %p\n", value, db);
227 	}
228 }
229 
230 unsigned int kfd_get_doorbell_dw_offset_in_bar(struct kfd_dev *kfd,
231 					struct kfd_process_device *pdd,
232 					unsigned int doorbell_id)
233 {
234 	/*
235 	 * doorbell_base_dw_offset accounts for doorbells taken by KGD.
236 	 * index * kfd_doorbell_process_slice/sizeof(u32) adjusts to
237 	 * the process's doorbells. The offset returned is in dword
238 	 * units regardless of the ASIC-dependent doorbell size.
239 	 */
240 	return kfd->doorbell_base_dw_offset +
241 		pdd->doorbell_index
242 		* kfd_doorbell_process_slice(kfd) / sizeof(u32) +
243 		doorbell_id * kfd->device_info.doorbell_size / sizeof(u32);
244 }
245 
246 uint64_t kfd_get_number_elems(struct kfd_dev *kfd)
247 {
248 	uint64_t num_of_elems = (kfd->shared_resources.doorbell_aperture_size -
249 				kfd->shared_resources.doorbell_start_offset) /
250 					kfd_doorbell_process_slice(kfd) + 1;
251 
252 	return num_of_elems;
253 
254 }
255 
256 phys_addr_t kfd_get_process_doorbells(struct kfd_process_device *pdd)
257 {
258 	return pdd->dev->doorbell_base +
259 		pdd->doorbell_index * kfd_doorbell_process_slice(pdd->dev);
260 }
261 
262 int kfd_alloc_process_doorbells(struct kfd_dev *kfd, unsigned int *doorbell_index)
263 {
264 	int r = ida_simple_get(&kfd->doorbell_ida, 1, kfd->max_doorbell_slices,
265 				GFP_KERNEL);
266 	if (r > 0)
267 		*doorbell_index = r;
268 
269 	return r;
270 }
271 
272 void kfd_free_process_doorbells(struct kfd_dev *kfd, unsigned int doorbell_index)
273 {
274 	if (doorbell_index)
275 		ida_simple_remove(&kfd->doorbell_ida, doorbell_index);
276 }
277