Linux Audio

Check our new training course

Loading...
v4.6
 
  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
 36#define KERNEL_DOORBELL_PASID 1
 37#define KFD_SIZE_OF_DOORBELL_IN_BYTES 4
 38
 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
 53/* # of doorbell bytes allocated for each process. */
 54static inline size_t doorbell_process_allocation(void)
 55{
 56	return roundup(KFD_SIZE_OF_DOORBELL_IN_BYTES *
 57			KFD_MAX_NUM_OF_QUEUES_PER_PROCESS,
 58			PAGE_SIZE);
 
 
 
 
 59}
 60
 61/* Doorbell calculations for device init. */
 62void kfd_doorbell_init(struct kfd_dev *kfd)
 63{
 64	size_t doorbell_start_offset;
 65	size_t doorbell_aperture_size;
 66	size_t doorbell_process_limit;
 67
 68	/*
 
 
 
 
 
 
 
 
 
 
 69	 * We start with calculations in bytes because the input data might
 70	 * only be byte-aligned.
 71	 * Only after we have done the rounding can we assume any alignment.
 72	 */
 73
 74	doorbell_start_offset =
 75			roundup(kfd->shared_resources.doorbell_start_offset,
 76					doorbell_process_allocation());
 77
 78	doorbell_aperture_size =
 79			rounddown(kfd->shared_resources.doorbell_aperture_size,
 80					doorbell_process_allocation());
 81
 82	if (doorbell_aperture_size > doorbell_start_offset)
 83		doorbell_process_limit =
 84			(doorbell_aperture_size - doorbell_start_offset) /
 85						doorbell_process_allocation();
 86	else
 87		doorbell_process_limit = 0;
 
 
 
 
 88
 89	kfd->doorbell_base = kfd->shared_resources.doorbell_physical_address +
 90				doorbell_start_offset;
 91
 92	kfd->doorbell_id_offset = doorbell_start_offset / sizeof(u32);
 93	kfd->doorbell_process_limit = doorbell_process_limit - 1;
 94
 95	kfd->doorbell_kernel_ptr = ioremap(kfd->doorbell_base,
 96						doorbell_process_allocation());
 97
 98	BUG_ON(!kfd->doorbell_kernel_ptr);
 
 99
100	pr_debug("kfd: doorbell initialization:\n");
101	pr_debug("kfd: doorbell base           == 0x%08lX\n",
102			(uintptr_t)kfd->doorbell_base);
103
104	pr_debug("kfd: doorbell_id_offset      == 0x%08lX\n",
105			kfd->doorbell_id_offset);
106
107	pr_debug("kfd: doorbell_process_limit  == 0x%08lX\n",
108			doorbell_process_limit);
109
110	pr_debug("kfd: doorbell_kernel_offset  == 0x%08lX\n",
111			(uintptr_t)kfd->doorbell_base);
112
113	pr_debug("kfd: doorbell aperture size  == 0x%08lX\n",
114			kfd->shared_resources.doorbell_aperture_size);
115
116	pr_debug("kfd: doorbell kernel address == 0x%08lX\n",
117			(uintptr_t)kfd->doorbell_kernel_ptr);
 
118}
119
120int kfd_doorbell_mmap(struct kfd_process *process, struct vm_area_struct *vma)
 
 
 
 
 
 
 
121{
122	phys_addr_t address;
123	struct kfd_dev *dev;
124
125	/*
126	 * For simplicitly we only allow mapping of the entire doorbell
127	 * allocation of a single device & process.
128	 */
129	if (vma->vm_end - vma->vm_start != doorbell_process_allocation())
130		return -EINVAL;
131
132	/* Find kfd device according to gpu id */
133	dev = kfd_device_by_id(vma->vm_pgoff);
134	if (dev == NULL)
135		return -EINVAL;
136
137	/* Calculate physical address of doorbell */
138	address = kfd_get_process_doorbells(dev, process);
139
 
140	vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_NORESERVE |
141				VM_DONTDUMP | VM_PFNMAP;
142
143	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
144
145	pr_debug("mapping doorbell page:\n");
146	pr_debug("     target user address == 0x%08llX\n",
147			(unsigned long long) vma->vm_start);
148	pr_debug("     physical address    == 0x%08llX\n", address);
149	pr_debug("     vm_flags            == 0x%04lX\n", vma->vm_flags);
150	pr_debug("     size                == 0x%04lX\n",
151			 doorbell_process_allocation());
 
152
153	return io_remap_pfn_range(vma,
154				vma->vm_start,
155				address >> PAGE_SHIFT,
156				doorbell_process_allocation(),
157				vma->vm_page_prot);
158}
159
160
161/* get kernel iomem pointer for a doorbell */
162u32 __iomem *kfd_get_kernel_doorbell(struct kfd_dev *kfd,
163					unsigned int *doorbell_off)
164{
165	u32 inx;
166
167	BUG_ON(!kfd || !doorbell_off);
168
169	mutex_lock(&kfd->doorbell_mutex);
170	inx = find_first_zero_bit(kfd->doorbell_available_index,
171					KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
172
173	__set_bit(inx, kfd->doorbell_available_index);
174	mutex_unlock(&kfd->doorbell_mutex);
175
176	if (inx >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS)
177		return NULL;
178
 
 
179	/*
180	 * Calculating the kernel doorbell offset using "faked" kernel
181	 * pasid that allocated for kernel queues only
182	 */
183	*doorbell_off = KERNEL_DOORBELL_PASID * (doorbell_process_allocation() /
184							sizeof(u32)) + inx;
185
186	pr_debug("kfd: get kernel queue doorbell\n"
187			 "     doorbell offset   == 0x%08d\n"
188			 "     kernel address    == 0x%08lX\n",
189		*doorbell_off, (uintptr_t)(kfd->doorbell_kernel_ptr + inx));
190
191	return kfd->doorbell_kernel_ptr + inx;
192}
193
194void kfd_release_kernel_doorbell(struct kfd_dev *kfd, u32 __iomem *db_addr)
195{
196	unsigned int inx;
197
198	BUG_ON(!kfd || !db_addr);
199
200	inx = (unsigned int)(db_addr - kfd->doorbell_kernel_ptr);
201
202	mutex_lock(&kfd->doorbell_mutex);
203	__clear_bit(inx, kfd->doorbell_available_index);
204	mutex_unlock(&kfd->doorbell_mutex);
205}
206
207inline void write_kernel_doorbell(u32 __iomem *db, u32 value)
208{
209	if (db) {
210		writel(value, db);
211		pr_debug("writing %d to doorbell address 0x%p\n", value, db);
212	}
213}
214
215/*
216 * queue_ids are in the range [0,MAX_PROCESS_QUEUES) and are mapped 1:1
217 * to doorbells with the process's doorbell page
218 */
219unsigned int kfd_queue_id_to_doorbell(struct kfd_dev *kfd,
220					struct kfd_process *process,
221					unsigned int queue_id)
 
 
 
 
 
 
222{
223	/*
224	 * doorbell_id_offset accounts for doorbells taken by KGD.
225	 * pasid * doorbell_process_allocation/sizeof(u32) adjusts
226	 * to the process's doorbells
 
227	 */
228	return kfd->doorbell_id_offset +
229		process->pasid * (doorbell_process_allocation()/sizeof(u32)) +
230		queue_id;
 
 
 
 
 
 
 
231}
232
233uint64_t kfd_get_number_elems(struct kfd_dev *kfd)
234{
235	uint64_t num_of_elems = (kfd->shared_resources.doorbell_aperture_size -
236				kfd->shared_resources.doorbell_start_offset) /
237					doorbell_process_allocation() + 1;
238
239	return num_of_elems;
240
241}
242
243phys_addr_t kfd_get_process_doorbells(struct kfd_dev *dev,
244					struct kfd_process *process)
 
 
 
 
 
 
 
 
 
 
 
 
245{
246	return dev->doorbell_base +
247		process->pasid * doorbell_process_allocation();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248}
v6.2
  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. */
 50size_t kfd_doorbell_process_slice(struct kfd_dev *kfd)
 51{
 52	if (!kfd->shared_resources.enable_mes)
 53		return roundup(kfd->device_info.doorbell_size *
 54				KFD_MAX_NUM_OF_QUEUES_PER_PROCESS,
 55				PAGE_SIZE);
 56	else
 57		return amdgpu_mes_doorbell_process_slice(
 58					(struct amdgpu_device *)kfd->adev);
 59}
 60
 61/* Doorbell calculations for device init. */
 62int kfd_doorbell_init(struct kfd_dev *kfd)
 63{
 64	size_t doorbell_start_offset;
 65	size_t doorbell_aperture_size;
 66	size_t doorbell_process_limit;
 67
 68	/*
 69	 * With MES enabled, just set the doorbell base as it is needed
 70	 * to calculate doorbell physical address.
 71	 */
 72	if (kfd->shared_resources.enable_mes) {
 73		kfd->doorbell_base =
 74			kfd->shared_resources.doorbell_physical_address;
 75		return 0;
 76	}
 77
 78	/*
 79	 * We start with calculations in bytes because the input data might
 80	 * only be byte-aligned.
 81	 * Only after we have done the rounding can we assume any alignment.
 82	 */
 83
 84	doorbell_start_offset =
 85			roundup(kfd->shared_resources.doorbell_start_offset,
 86					kfd_doorbell_process_slice(kfd));
 87
 88	doorbell_aperture_size =
 89			rounddown(kfd->shared_resources.doorbell_aperture_size,
 90					kfd_doorbell_process_slice(kfd));
 91
 92	if (doorbell_aperture_size > doorbell_start_offset)
 93		doorbell_process_limit =
 94			(doorbell_aperture_size - doorbell_start_offset) /
 95						kfd_doorbell_process_slice(kfd);
 96	else
 97		return -ENOSPC;
 98
 99	if (!kfd->max_doorbell_slices ||
100	    doorbell_process_limit < kfd->max_doorbell_slices)
101		kfd->max_doorbell_slices = doorbell_process_limit;
102
103	kfd->doorbell_base = kfd->shared_resources.doorbell_physical_address +
104				doorbell_start_offset;
105
106	kfd->doorbell_base_dw_offset = doorbell_start_offset / sizeof(u32);
 
107
108	kfd->doorbell_kernel_ptr = ioremap(kfd->doorbell_base,
109					   kfd_doorbell_process_slice(kfd));
110
111	if (!kfd->doorbell_kernel_ptr)
112		return -ENOMEM;
113
114	pr_debug("Doorbell initialization:\n");
115	pr_debug("doorbell base           == 0x%08lX\n",
116			(uintptr_t)kfd->doorbell_base);
117
118	pr_debug("doorbell_base_dw_offset      == 0x%08lX\n",
119			kfd->doorbell_base_dw_offset);
120
121	pr_debug("doorbell_process_limit  == 0x%08lX\n",
122			doorbell_process_limit);
123
124	pr_debug("doorbell_kernel_offset  == 0x%08lX\n",
125			(uintptr_t)kfd->doorbell_base);
126
127	pr_debug("doorbell aperture size  == 0x%08lX\n",
128			kfd->shared_resources.doorbell_aperture_size);
129
130	pr_debug("doorbell kernel address == %p\n", kfd->doorbell_kernel_ptr);
131
132	return 0;
133}
134
135void kfd_doorbell_fini(struct kfd_dev *kfd)
136{
137	if (kfd->doorbell_kernel_ptr)
138		iounmap(kfd->doorbell_kernel_ptr);
139}
140
141int kfd_doorbell_mmap(struct kfd_dev *dev, struct kfd_process *process,
142		      struct vm_area_struct *vma)
143{
144	phys_addr_t address;
145	struct kfd_process_device *pdd;
146
147	/*
148	 * For simplicitly we only allow mapping of the entire doorbell
149	 * allocation of a single device & process.
150	 */
151	if (vma->vm_end - vma->vm_start != kfd_doorbell_process_slice(dev))
152		return -EINVAL;
153
154	pdd = kfd_get_process_device_data(dev, process);
155	if (!pdd)
 
156		return -EINVAL;
157
158	/* Calculate physical address of doorbell */
159	address = kfd_get_process_doorbells(pdd);
160	if (!address)
161		return -ENOMEM;
162	vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_NORESERVE |
163				VM_DONTDUMP | VM_PFNMAP;
164
165	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
166
167	pr_debug("Mapping doorbell page\n"
168		 "     target user address == 0x%08llX\n"
169		 "     physical address    == 0x%08llX\n"
170		 "     vm_flags            == 0x%04lX\n"
171		 "     size                == 0x%04lX\n",
172		 (unsigned long long) vma->vm_start, address, vma->vm_flags,
173		 kfd_doorbell_process_slice(dev));
174
175
176	return io_remap_pfn_range(vma,
177				vma->vm_start,
178				address >> PAGE_SHIFT,
179				kfd_doorbell_process_slice(dev),
180				vma->vm_page_prot);
181}
182
183
184/* get kernel iomem pointer for a doorbell */
185void __iomem *kfd_get_kernel_doorbell(struct kfd_dev *kfd,
186					unsigned int *doorbell_off)
187{
188	u32 inx;
189
 
 
190	mutex_lock(&kfd->doorbell_mutex);
191	inx = find_first_zero_bit(kfd->doorbell_available_index,
192					KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
193
194	__set_bit(inx, kfd->doorbell_available_index);
195	mutex_unlock(&kfd->doorbell_mutex);
196
197	if (inx >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS)
198		return NULL;
199
200	inx *= kfd->device_info.doorbell_size / sizeof(u32);
201
202	/*
203	 * Calculating the kernel doorbell offset using the first
204	 * doorbell page.
205	 */
206	*doorbell_off = kfd->doorbell_base_dw_offset + inx;
 
207
208	pr_debug("Get kernel queue doorbell\n"
209			"     doorbell offset   == 0x%08X\n"
210			"     doorbell index    == 0x%x\n",
211		*doorbell_off, inx);
212
213	return kfd->doorbell_kernel_ptr + inx;
214}
215
216void kfd_release_kernel_doorbell(struct kfd_dev *kfd, u32 __iomem *db_addr)
217{
218	unsigned int inx;
219
220	inx = (unsigned int)(db_addr - kfd->doorbell_kernel_ptr)
221		* sizeof(u32) / kfd->device_info.doorbell_size;
 
222
223	mutex_lock(&kfd->doorbell_mutex);
224	__clear_bit(inx, kfd->doorbell_available_index);
225	mutex_unlock(&kfd->doorbell_mutex);
226}
227
228void write_kernel_doorbell(void __iomem *db, u32 value)
229{
230	if (db) {
231		writel(value, db);
232		pr_debug("Writing %d to doorbell address %p\n", value, db);
233	}
234}
235
236void write_kernel_doorbell64(void __iomem *db, u64 value)
237{
238	if (db) {
239		WARN(((unsigned long)db & 7) != 0,
240		     "Unaligned 64-bit doorbell");
241		writeq(value, (u64 __iomem *)db);
242		pr_debug("writing %llu to doorbell address %p\n", value, db);
243	}
244}
245
246unsigned int kfd_get_doorbell_dw_offset_in_bar(struct kfd_dev *kfd,
247					struct kfd_process_device *pdd,
248					unsigned int doorbell_id)
249{
250	/*
251	 * doorbell_base_dw_offset accounts for doorbells taken by KGD.
252	 * index * kfd_doorbell_process_slice/sizeof(u32) adjusts to
253	 * the process's doorbells. The offset returned is in dword
254	 * units regardless of the ASIC-dependent doorbell size.
255	 */
256	if (!kfd->shared_resources.enable_mes)
257		return kfd->doorbell_base_dw_offset +
258			pdd->doorbell_index
259			* kfd_doorbell_process_slice(kfd) / sizeof(u32) +
260			doorbell_id *
261			kfd->device_info.doorbell_size / sizeof(u32);
262	else
263		return amdgpu_mes_get_doorbell_dw_offset_in_bar(
264				(struct amdgpu_device *)kfd->adev,
265				pdd->doorbell_index, doorbell_id);
266}
267
268uint64_t kfd_get_number_elems(struct kfd_dev *kfd)
269{
270	uint64_t num_of_elems = (kfd->shared_resources.doorbell_aperture_size -
271				kfd->shared_resources.doorbell_start_offset) /
272					kfd_doorbell_process_slice(kfd) + 1;
273
274	return num_of_elems;
275
276}
277
278phys_addr_t kfd_get_process_doorbells(struct kfd_process_device *pdd)
279{
280	if (!pdd->doorbell_index) {
281		int r = kfd_alloc_process_doorbells(pdd->dev,
282						    &pdd->doorbell_index);
283		if (r)
284			return 0;
285	}
286
287	return pdd->dev->doorbell_base +
288		pdd->doorbell_index * kfd_doorbell_process_slice(pdd->dev);
289}
290
291int kfd_alloc_process_doorbells(struct kfd_dev *kfd, unsigned int *doorbell_index)
292{
293	int r = 0;
294
295	if (!kfd->shared_resources.enable_mes)
296		r = ida_simple_get(&kfd->doorbell_ida, 1,
297				   kfd->max_doorbell_slices, GFP_KERNEL);
298	else
299		r = amdgpu_mes_alloc_process_doorbells(
300				(struct amdgpu_device *)kfd->adev,
301				doorbell_index);
302
303	if (r > 0)
304		*doorbell_index = r;
305
306	if (r < 0)
307		pr_err("Failed to allocate process doorbells\n");
308
309	return r;
310}
311
312void kfd_free_process_doorbells(struct kfd_dev *kfd, unsigned int doorbell_index)
313{
314	if (doorbell_index) {
315		if (!kfd->shared_resources.enable_mes)
316			ida_simple_remove(&kfd->doorbell_ida, doorbell_index);
317		else
318			amdgpu_mes_free_process_doorbells(
319					(struct amdgpu_device *)kfd->adev,
320					doorbell_index);
321	}
322}