Linux Audio

Check our new training course

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