2 * Copyright 2014 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
23 #include <linux/device.h>
24 #include <linux/export.h>
25 #include <linux/err.h>
27 #include <linux/file.h>
28 #include <linux/sched.h>
29 #include <linux/slab.h>
30 #include <linux/uaccess.h>
31 #include <linux/compat.h>
32 #include <uapi/linux/kfd_ioctl.h>
33 #include <linux/time.h>
35 #include <linux/mman.h>
36 #include <linux/dma-buf.h>
37 #include <asm/processor.h>
39 #include "kfd_device_queue_manager.h"
40 #include "kfd_dbgmgr.h"
41 #include "amdgpu_amdkfd.h"
42 #include "kfd_smi_events.h"
44 static long kfd_ioctl(struct file *, unsigned int, unsigned long);
45 static int kfd_open(struct inode *, struct file *);
46 static int kfd_release(struct inode *, struct file *);
47 static int kfd_mmap(struct file *, struct vm_area_struct *);
49 static const char kfd_dev_name[] = "kfd";
51 static const struct file_operations kfd_fops = {
53 .unlocked_ioctl = kfd_ioctl,
54 .compat_ioctl = compat_ptr_ioctl,
56 .release = kfd_release,
60 static int kfd_char_dev_major = -1;
61 static struct class *kfd_class;
62 struct device *kfd_device;
64 int kfd_chardev_init(void)
68 kfd_char_dev_major = register_chrdev(0, kfd_dev_name, &kfd_fops);
69 err = kfd_char_dev_major;
71 goto err_register_chrdev;
73 kfd_class = class_create(THIS_MODULE, kfd_dev_name);
74 err = PTR_ERR(kfd_class);
75 if (IS_ERR(kfd_class))
76 goto err_class_create;
78 kfd_device = device_create(kfd_class, NULL,
79 MKDEV(kfd_char_dev_major, 0),
81 err = PTR_ERR(kfd_device);
82 if (IS_ERR(kfd_device))
83 goto err_device_create;
88 class_destroy(kfd_class);
90 unregister_chrdev(kfd_char_dev_major, kfd_dev_name);
95 void kfd_chardev_exit(void)
97 device_destroy(kfd_class, MKDEV(kfd_char_dev_major, 0));
98 class_destroy(kfd_class);
99 unregister_chrdev(kfd_char_dev_major, kfd_dev_name);
103 struct device *kfd_chardev(void)
109 static int kfd_open(struct inode *inode, struct file *filep)
111 struct kfd_process *process;
112 bool is_32bit_user_mode;
114 if (iminor(inode) != 0)
117 is_32bit_user_mode = in_compat_syscall();
119 if (is_32bit_user_mode) {
121 "Process %d (32-bit) failed to open /dev/kfd\n"
122 "32-bit processes are not supported by amdkfd\n",
127 process = kfd_create_process(filep);
129 return PTR_ERR(process);
131 if (kfd_is_locked()) {
132 dev_dbg(kfd_device, "kfd is locked!\n"
133 "process %d unreferenced", process->pasid);
134 kfd_unref_process(process);
138 /* filep now owns the reference returned by kfd_create_process */
139 filep->private_data = process;
141 dev_dbg(kfd_device, "process %d opened, compat mode (32 bit) - %d\n",
142 process->pasid, process->is_32bit_user_mode);
147 static int kfd_release(struct inode *inode, struct file *filep)
149 struct kfd_process *process = filep->private_data;
152 kfd_unref_process(process);
157 static int kfd_ioctl_get_version(struct file *filep, struct kfd_process *p,
160 struct kfd_ioctl_get_version_args *args = data;
162 args->major_version = KFD_IOCTL_MAJOR_VERSION;
163 args->minor_version = KFD_IOCTL_MINOR_VERSION;
168 static int set_queue_properties_from_user(struct queue_properties *q_properties,
169 struct kfd_ioctl_create_queue_args *args)
171 if (args->queue_percentage > KFD_MAX_QUEUE_PERCENTAGE) {
172 pr_err("Queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
176 if (args->queue_priority > KFD_MAX_QUEUE_PRIORITY) {
177 pr_err("Queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
181 if ((args->ring_base_address) &&
182 (!access_ok((const void __user *) args->ring_base_address,
183 sizeof(uint64_t)))) {
184 pr_err("Can't access ring base address\n");
188 if (!is_power_of_2(args->ring_size) && (args->ring_size != 0)) {
189 pr_err("Ring size must be a power of 2 or 0\n");
193 if (!access_ok((const void __user *) args->read_pointer_address,
195 pr_err("Can't access read pointer\n");
199 if (!access_ok((const void __user *) args->write_pointer_address,
201 pr_err("Can't access write pointer\n");
205 if (args->eop_buffer_address &&
206 !access_ok((const void __user *) args->eop_buffer_address,
208 pr_debug("Can't access eop buffer");
212 if (args->ctx_save_restore_address &&
213 !access_ok((const void __user *) args->ctx_save_restore_address,
215 pr_debug("Can't access ctx save restore buffer");
219 q_properties->is_interop = false;
220 q_properties->is_gws = false;
221 q_properties->queue_percent = args->queue_percentage;
222 q_properties->priority = args->queue_priority;
223 q_properties->queue_address = args->ring_base_address;
224 q_properties->queue_size = args->ring_size;
225 q_properties->read_ptr = (uint32_t *) args->read_pointer_address;
226 q_properties->write_ptr = (uint32_t *) args->write_pointer_address;
227 q_properties->eop_ring_buffer_address = args->eop_buffer_address;
228 q_properties->eop_ring_buffer_size = args->eop_buffer_size;
229 q_properties->ctx_save_restore_area_address =
230 args->ctx_save_restore_address;
231 q_properties->ctx_save_restore_area_size = args->ctx_save_restore_size;
232 q_properties->ctl_stack_size = args->ctl_stack_size;
233 if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE ||
234 args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL)
235 q_properties->type = KFD_QUEUE_TYPE_COMPUTE;
236 else if (args->queue_type == KFD_IOC_QUEUE_TYPE_SDMA)
237 q_properties->type = KFD_QUEUE_TYPE_SDMA;
238 else if (args->queue_type == KFD_IOC_QUEUE_TYPE_SDMA_XGMI)
239 q_properties->type = KFD_QUEUE_TYPE_SDMA_XGMI;
243 if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL)
244 q_properties->format = KFD_QUEUE_FORMAT_AQL;
246 q_properties->format = KFD_QUEUE_FORMAT_PM4;
248 pr_debug("Queue Percentage: %d, %d\n",
249 q_properties->queue_percent, args->queue_percentage);
251 pr_debug("Queue Priority: %d, %d\n",
252 q_properties->priority, args->queue_priority);
254 pr_debug("Queue Address: 0x%llX, 0x%llX\n",
255 q_properties->queue_address, args->ring_base_address);
257 pr_debug("Queue Size: 0x%llX, %u\n",
258 q_properties->queue_size, args->ring_size);
260 pr_debug("Queue r/w Pointers: %px, %px\n",
261 q_properties->read_ptr,
262 q_properties->write_ptr);
264 pr_debug("Queue Format: %d\n", q_properties->format);
266 pr_debug("Queue EOP: 0x%llX\n", q_properties->eop_ring_buffer_address);
268 pr_debug("Queue CTX save area: 0x%llX\n",
269 q_properties->ctx_save_restore_area_address);
274 static int kfd_ioctl_create_queue(struct file *filep, struct kfd_process *p,
277 struct kfd_ioctl_create_queue_args *args = data;
280 unsigned int queue_id;
281 struct kfd_process_device *pdd;
282 struct queue_properties q_properties;
283 uint32_t doorbell_offset_in_process = 0;
285 memset(&q_properties, 0, sizeof(struct queue_properties));
287 pr_debug("Creating queue ioctl\n");
289 err = set_queue_properties_from_user(&q_properties, args);
293 pr_debug("Looking for gpu id 0x%x\n", args->gpu_id);
294 dev = kfd_device_by_id(args->gpu_id);
296 pr_debug("Could not find gpu id 0x%x\n", args->gpu_id);
300 mutex_lock(&p->mutex);
302 pdd = kfd_bind_process_to_device(dev, p);
305 goto err_bind_process;
308 pr_debug("Creating queue for PASID 0x%x on gpu 0x%x\n",
312 err = pqm_create_queue(&p->pqm, dev, filep, &q_properties, &queue_id,
313 &doorbell_offset_in_process);
315 goto err_create_queue;
317 args->queue_id = queue_id;
320 /* Return gpu_id as doorbell offset for mmap usage */
321 args->doorbell_offset = KFD_MMAP_TYPE_DOORBELL;
322 args->doorbell_offset |= KFD_MMAP_GPU_ID(args->gpu_id);
323 if (KFD_IS_SOC15(dev->device_info->asic_family))
324 /* On SOC15 ASICs, include the doorbell offset within the
325 * process doorbell frame, which is 2 pages.
327 args->doorbell_offset |= doorbell_offset_in_process;
329 mutex_unlock(&p->mutex);
331 pr_debug("Queue id %d was created successfully\n", args->queue_id);
333 pr_debug("Ring buffer address == 0x%016llX\n",
334 args->ring_base_address);
336 pr_debug("Read ptr address == 0x%016llX\n",
337 args->read_pointer_address);
339 pr_debug("Write ptr address == 0x%016llX\n",
340 args->write_pointer_address);
346 mutex_unlock(&p->mutex);
350 static int kfd_ioctl_destroy_queue(struct file *filp, struct kfd_process *p,
354 struct kfd_ioctl_destroy_queue_args *args = data;
356 pr_debug("Destroying queue id %d for pasid 0x%x\n",
360 mutex_lock(&p->mutex);
362 retval = pqm_destroy_queue(&p->pqm, args->queue_id);
364 mutex_unlock(&p->mutex);
368 static int kfd_ioctl_update_queue(struct file *filp, struct kfd_process *p,
372 struct kfd_ioctl_update_queue_args *args = data;
373 struct queue_properties properties;
375 if (args->queue_percentage > KFD_MAX_QUEUE_PERCENTAGE) {
376 pr_err("Queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
380 if (args->queue_priority > KFD_MAX_QUEUE_PRIORITY) {
381 pr_err("Queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
385 if ((args->ring_base_address) &&
386 (!access_ok((const void __user *) args->ring_base_address,
387 sizeof(uint64_t)))) {
388 pr_err("Can't access ring base address\n");
392 if (!is_power_of_2(args->ring_size) && (args->ring_size != 0)) {
393 pr_err("Ring size must be a power of 2 or 0\n");
397 properties.queue_address = args->ring_base_address;
398 properties.queue_size = args->ring_size;
399 properties.queue_percent = args->queue_percentage;
400 properties.priority = args->queue_priority;
402 pr_debug("Updating queue id %d for pasid 0x%x\n",
403 args->queue_id, p->pasid);
405 mutex_lock(&p->mutex);
407 retval = pqm_update_queue(&p->pqm, args->queue_id, &properties);
409 mutex_unlock(&p->mutex);
414 static int kfd_ioctl_set_cu_mask(struct file *filp, struct kfd_process *p,
418 const int max_num_cus = 1024;
419 struct kfd_ioctl_set_cu_mask_args *args = data;
420 struct queue_properties properties;
421 uint32_t __user *cu_mask_ptr = (uint32_t __user *)args->cu_mask_ptr;
422 size_t cu_mask_size = sizeof(uint32_t) * (args->num_cu_mask / 32);
424 if ((args->num_cu_mask % 32) != 0) {
425 pr_debug("num_cu_mask 0x%x must be a multiple of 32",
430 properties.cu_mask_count = args->num_cu_mask;
431 if (properties.cu_mask_count == 0) {
432 pr_debug("CU mask cannot be 0");
436 /* To prevent an unreasonably large CU mask size, set an arbitrary
437 * limit of max_num_cus bits. We can then just drop any CU mask bits
438 * past max_num_cus bits and just use the first max_num_cus bits.
440 if (properties.cu_mask_count > max_num_cus) {
441 pr_debug("CU mask cannot be greater than 1024 bits");
442 properties.cu_mask_count = max_num_cus;
443 cu_mask_size = sizeof(uint32_t) * (max_num_cus/32);
446 properties.cu_mask = kzalloc(cu_mask_size, GFP_KERNEL);
447 if (!properties.cu_mask)
450 retval = copy_from_user(properties.cu_mask, cu_mask_ptr, cu_mask_size);
452 pr_debug("Could not copy CU mask from userspace");
453 kfree(properties.cu_mask);
457 mutex_lock(&p->mutex);
459 retval = pqm_set_cu_mask(&p->pqm, args->queue_id, &properties);
461 mutex_unlock(&p->mutex);
464 kfree(properties.cu_mask);
469 static int kfd_ioctl_get_queue_wave_state(struct file *filep,
470 struct kfd_process *p, void *data)
472 struct kfd_ioctl_get_queue_wave_state_args *args = data;
475 mutex_lock(&p->mutex);
477 r = pqm_get_wave_state(&p->pqm, args->queue_id,
478 (void __user *)args->ctl_stack_address,
479 &args->ctl_stack_used_size,
480 &args->save_area_used_size);
482 mutex_unlock(&p->mutex);
487 static int kfd_ioctl_set_memory_policy(struct file *filep,
488 struct kfd_process *p, void *data)
490 struct kfd_ioctl_set_memory_policy_args *args = data;
493 struct kfd_process_device *pdd;
494 enum cache_policy default_policy, alternate_policy;
496 if (args->default_policy != KFD_IOC_CACHE_POLICY_COHERENT
497 && args->default_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) {
501 if (args->alternate_policy != KFD_IOC_CACHE_POLICY_COHERENT
502 && args->alternate_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) {
506 dev = kfd_device_by_id(args->gpu_id);
510 mutex_lock(&p->mutex);
512 pdd = kfd_bind_process_to_device(dev, p);
518 default_policy = (args->default_policy == KFD_IOC_CACHE_POLICY_COHERENT)
519 ? cache_policy_coherent : cache_policy_noncoherent;
522 (args->alternate_policy == KFD_IOC_CACHE_POLICY_COHERENT)
523 ? cache_policy_coherent : cache_policy_noncoherent;
525 if (!dev->dqm->ops.set_cache_memory_policy(dev->dqm,
529 (void __user *)args->alternate_aperture_base,
530 args->alternate_aperture_size))
534 mutex_unlock(&p->mutex);
539 static int kfd_ioctl_set_trap_handler(struct file *filep,
540 struct kfd_process *p, void *data)
542 struct kfd_ioctl_set_trap_handler_args *args = data;
545 struct kfd_process_device *pdd;
547 dev = kfd_device_by_id(args->gpu_id);
551 mutex_lock(&p->mutex);
553 pdd = kfd_bind_process_to_device(dev, p);
559 if (dev->dqm->ops.set_trap_handler(dev->dqm,
566 mutex_unlock(&p->mutex);
571 static int kfd_ioctl_dbg_register(struct file *filep,
572 struct kfd_process *p, void *data)
574 struct kfd_ioctl_dbg_register_args *args = data;
576 struct kfd_dbgmgr *dbgmgr_ptr;
577 struct kfd_process_device *pdd;
581 dev = kfd_device_by_id(args->gpu_id);
585 if (dev->device_info->asic_family == CHIP_CARRIZO) {
586 pr_debug("kfd_ioctl_dbg_register not supported on CZ\n");
590 mutex_lock(&p->mutex);
591 mutex_lock(kfd_get_dbgmgr_mutex());
594 * make sure that we have pdd, if this the first queue created for
597 pdd = kfd_bind_process_to_device(dev, p);
599 status = PTR_ERR(pdd);
604 /* In case of a legal call, we have no dbgmgr yet */
605 create_ok = kfd_dbgmgr_create(&dbgmgr_ptr, dev);
607 status = kfd_dbgmgr_register(dbgmgr_ptr, p);
609 kfd_dbgmgr_destroy(dbgmgr_ptr);
611 dev->dbgmgr = dbgmgr_ptr;
614 pr_debug("debugger already registered\n");
619 mutex_unlock(kfd_get_dbgmgr_mutex());
620 mutex_unlock(&p->mutex);
625 static int kfd_ioctl_dbg_unregister(struct file *filep,
626 struct kfd_process *p, void *data)
628 struct kfd_ioctl_dbg_unregister_args *args = data;
632 dev = kfd_device_by_id(args->gpu_id);
633 if (!dev || !dev->dbgmgr)
636 if (dev->device_info->asic_family == CHIP_CARRIZO) {
637 pr_debug("kfd_ioctl_dbg_unregister not supported on CZ\n");
641 mutex_lock(kfd_get_dbgmgr_mutex());
643 status = kfd_dbgmgr_unregister(dev->dbgmgr, p);
645 kfd_dbgmgr_destroy(dev->dbgmgr);
649 mutex_unlock(kfd_get_dbgmgr_mutex());
655 * Parse and generate variable size data structure for address watch.
656 * Total size of the buffer and # watch points is limited in order
657 * to prevent kernel abuse. (no bearing to the much smaller HW limitation
658 * which is enforced by dbgdev module)
659 * please also note that the watch address itself are not "copied from user",
660 * since it be set into the HW in user mode values.
663 static int kfd_ioctl_dbg_address_watch(struct file *filep,
664 struct kfd_process *p, void *data)
666 struct kfd_ioctl_dbg_address_watch_args *args = data;
668 struct dbg_address_watch_info aw_info;
669 unsigned char *args_buff;
671 void __user *cmd_from_user;
672 uint64_t watch_mask_value = 0;
673 unsigned int args_idx = 0;
675 memset((void *) &aw_info, 0, sizeof(struct dbg_address_watch_info));
677 dev = kfd_device_by_id(args->gpu_id);
681 if (dev->device_info->asic_family == CHIP_CARRIZO) {
682 pr_debug("kfd_ioctl_dbg_wave_control not supported on CZ\n");
686 cmd_from_user = (void __user *) args->content_ptr;
688 /* Validate arguments */
690 if ((args->buf_size_in_bytes > MAX_ALLOWED_AW_BUFF_SIZE) ||
691 (args->buf_size_in_bytes <= sizeof(*args) + sizeof(int) * 2) ||
692 (cmd_from_user == NULL))
695 /* this is the actual buffer to work with */
696 args_buff = memdup_user(cmd_from_user,
697 args->buf_size_in_bytes - sizeof(*args));
698 if (IS_ERR(args_buff))
699 return PTR_ERR(args_buff);
703 aw_info.num_watch_points = *((uint32_t *)(&args_buff[args_idx]));
704 args_idx += sizeof(aw_info.num_watch_points);
706 aw_info.watch_mode = (enum HSA_DBG_WATCH_MODE *) &args_buff[args_idx];
707 args_idx += sizeof(enum HSA_DBG_WATCH_MODE) * aw_info.num_watch_points;
710 * set watch address base pointer to point on the array base
713 aw_info.watch_address = (uint64_t *) &args_buff[args_idx];
715 /* skip over the addresses buffer */
716 args_idx += sizeof(aw_info.watch_address) * aw_info.num_watch_points;
718 if (args_idx >= args->buf_size_in_bytes - sizeof(*args)) {
723 watch_mask_value = (uint64_t) args_buff[args_idx];
725 if (watch_mask_value > 0) {
727 * There is an array of masks.
728 * set watch mask base pointer to point on the array base
731 aw_info.watch_mask = (uint64_t *) &args_buff[args_idx];
733 /* skip over the masks buffer */
734 args_idx += sizeof(aw_info.watch_mask) *
735 aw_info.num_watch_points;
737 /* just the NULL mask, set to NULL and skip over it */
738 aw_info.watch_mask = NULL;
739 args_idx += sizeof(aw_info.watch_mask);
742 if (args_idx >= args->buf_size_in_bytes - sizeof(args)) {
747 /* Currently HSA Event is not supported for DBG */
748 aw_info.watch_event = NULL;
750 mutex_lock(kfd_get_dbgmgr_mutex());
752 status = kfd_dbgmgr_address_watch(dev->dbgmgr, &aw_info);
754 mutex_unlock(kfd_get_dbgmgr_mutex());
762 /* Parse and generate fixed size data structure for wave control */
763 static int kfd_ioctl_dbg_wave_control(struct file *filep,
764 struct kfd_process *p, void *data)
766 struct kfd_ioctl_dbg_wave_control_args *args = data;
768 struct dbg_wave_control_info wac_info;
769 unsigned char *args_buff;
770 uint32_t computed_buff_size;
772 void __user *cmd_from_user;
773 unsigned int args_idx = 0;
775 memset((void *) &wac_info, 0, sizeof(struct dbg_wave_control_info));
777 /* we use compact form, independent of the packing attribute value */
778 computed_buff_size = sizeof(*args) +
779 sizeof(wac_info.mode) +
780 sizeof(wac_info.operand) +
781 sizeof(wac_info.dbgWave_msg.DbgWaveMsg) +
782 sizeof(wac_info.dbgWave_msg.MemoryVA) +
783 sizeof(wac_info.trapId);
785 dev = kfd_device_by_id(args->gpu_id);
789 if (dev->device_info->asic_family == CHIP_CARRIZO) {
790 pr_debug("kfd_ioctl_dbg_wave_control not supported on CZ\n");
794 /* input size must match the computed "compact" size */
795 if (args->buf_size_in_bytes != computed_buff_size) {
796 pr_debug("size mismatch, computed : actual %u : %u\n",
797 args->buf_size_in_bytes, computed_buff_size);
801 cmd_from_user = (void __user *) args->content_ptr;
803 if (cmd_from_user == NULL)
806 /* copy the entire buffer from user */
808 args_buff = memdup_user(cmd_from_user,
809 args->buf_size_in_bytes - sizeof(*args));
810 if (IS_ERR(args_buff))
811 return PTR_ERR(args_buff);
813 /* move ptr to the start of the "pay-load" area */
814 wac_info.process = p;
816 wac_info.operand = *((enum HSA_DBG_WAVEOP *)(&args_buff[args_idx]));
817 args_idx += sizeof(wac_info.operand);
819 wac_info.mode = *((enum HSA_DBG_WAVEMODE *)(&args_buff[args_idx]));
820 args_idx += sizeof(wac_info.mode);
822 wac_info.trapId = *((uint32_t *)(&args_buff[args_idx]));
823 args_idx += sizeof(wac_info.trapId);
825 wac_info.dbgWave_msg.DbgWaveMsg.WaveMsgInfoGen2.Value =
826 *((uint32_t *)(&args_buff[args_idx]));
827 wac_info.dbgWave_msg.MemoryVA = NULL;
829 mutex_lock(kfd_get_dbgmgr_mutex());
831 pr_debug("Calling dbg manager process %p, operand %u, mode %u, trapId %u, message %u\n",
832 wac_info.process, wac_info.operand,
833 wac_info.mode, wac_info.trapId,
834 wac_info.dbgWave_msg.DbgWaveMsg.WaveMsgInfoGen2.Value);
836 status = kfd_dbgmgr_wave_control(dev->dbgmgr, &wac_info);
838 pr_debug("Returned status of dbg manager is %ld\n", status);
840 mutex_unlock(kfd_get_dbgmgr_mutex());
847 static int kfd_ioctl_get_clock_counters(struct file *filep,
848 struct kfd_process *p, void *data)
850 struct kfd_ioctl_get_clock_counters_args *args = data;
853 dev = kfd_device_by_id(args->gpu_id);
855 /* Reading GPU clock counter from KGD */
856 args->gpu_clock_counter = amdgpu_amdkfd_get_gpu_clock_counter(dev->kgd);
858 /* Node without GPU resource */
859 args->gpu_clock_counter = 0;
861 /* No access to rdtsc. Using raw monotonic time */
862 args->cpu_clock_counter = ktime_get_raw_ns();
863 args->system_clock_counter = ktime_get_boottime_ns();
865 /* Since the counter is in nano-seconds we use 1GHz frequency */
866 args->system_clock_freq = 1000000000;
872 static int kfd_ioctl_get_process_apertures(struct file *filp,
873 struct kfd_process *p, void *data)
875 struct kfd_ioctl_get_process_apertures_args *args = data;
876 struct kfd_process_device_apertures *pAperture;
877 struct kfd_process_device *pdd;
879 dev_dbg(kfd_device, "get apertures for PASID 0x%x", p->pasid);
881 args->num_of_nodes = 0;
883 mutex_lock(&p->mutex);
885 /*if the process-device list isn't empty*/
886 if (kfd_has_process_device_data(p)) {
887 /* Run over all pdd of the process */
888 pdd = kfd_get_first_process_device_data(p);
891 &args->process_apertures[args->num_of_nodes];
892 pAperture->gpu_id = pdd->dev->id;
893 pAperture->lds_base = pdd->lds_base;
894 pAperture->lds_limit = pdd->lds_limit;
895 pAperture->gpuvm_base = pdd->gpuvm_base;
896 pAperture->gpuvm_limit = pdd->gpuvm_limit;
897 pAperture->scratch_base = pdd->scratch_base;
898 pAperture->scratch_limit = pdd->scratch_limit;
901 "node id %u\n", args->num_of_nodes);
903 "gpu id %u\n", pdd->dev->id);
905 "lds_base %llX\n", pdd->lds_base);
907 "lds_limit %llX\n", pdd->lds_limit);
909 "gpuvm_base %llX\n", pdd->gpuvm_base);
911 "gpuvm_limit %llX\n", pdd->gpuvm_limit);
913 "scratch_base %llX\n", pdd->scratch_base);
915 "scratch_limit %llX\n", pdd->scratch_limit);
917 args->num_of_nodes++;
919 pdd = kfd_get_next_process_device_data(p, pdd);
920 } while (pdd && (args->num_of_nodes < NUM_OF_SUPPORTED_GPUS));
923 mutex_unlock(&p->mutex);
928 static int kfd_ioctl_get_process_apertures_new(struct file *filp,
929 struct kfd_process *p, void *data)
931 struct kfd_ioctl_get_process_apertures_new_args *args = data;
932 struct kfd_process_device_apertures *pa;
933 struct kfd_process_device *pdd;
937 dev_dbg(kfd_device, "get apertures for PASID 0x%x", p->pasid);
939 if (args->num_of_nodes == 0) {
940 /* Return number of nodes, so that user space can alloacate
943 mutex_lock(&p->mutex);
945 if (!kfd_has_process_device_data(p))
948 /* Run over all pdd of the process */
949 pdd = kfd_get_first_process_device_data(p);
951 args->num_of_nodes++;
952 pdd = kfd_get_next_process_device_data(p, pdd);
958 /* Fill in process-aperture information for all available
959 * nodes, but not more than args->num_of_nodes as that is
960 * the amount of memory allocated by user
962 pa = kzalloc((sizeof(struct kfd_process_device_apertures) *
963 args->num_of_nodes), GFP_KERNEL);
967 mutex_lock(&p->mutex);
969 if (!kfd_has_process_device_data(p)) {
970 args->num_of_nodes = 0;
975 /* Run over all pdd of the process */
976 pdd = kfd_get_first_process_device_data(p);
978 pa[nodes].gpu_id = pdd->dev->id;
979 pa[nodes].lds_base = pdd->lds_base;
980 pa[nodes].lds_limit = pdd->lds_limit;
981 pa[nodes].gpuvm_base = pdd->gpuvm_base;
982 pa[nodes].gpuvm_limit = pdd->gpuvm_limit;
983 pa[nodes].scratch_base = pdd->scratch_base;
984 pa[nodes].scratch_limit = pdd->scratch_limit;
987 "gpu id %u\n", pdd->dev->id);
989 "lds_base %llX\n", pdd->lds_base);
991 "lds_limit %llX\n", pdd->lds_limit);
993 "gpuvm_base %llX\n", pdd->gpuvm_base);
995 "gpuvm_limit %llX\n", pdd->gpuvm_limit);
997 "scratch_base %llX\n", pdd->scratch_base);
999 "scratch_limit %llX\n", pdd->scratch_limit);
1002 pdd = kfd_get_next_process_device_data(p, pdd);
1003 } while (pdd && (nodes < args->num_of_nodes));
1004 mutex_unlock(&p->mutex);
1006 args->num_of_nodes = nodes;
1008 (void __user *)args->kfd_process_device_apertures_ptr,
1010 (nodes * sizeof(struct kfd_process_device_apertures)));
1012 return ret ? -EFAULT : 0;
1015 mutex_unlock(&p->mutex);
1019 static int kfd_ioctl_create_event(struct file *filp, struct kfd_process *p,
1022 struct kfd_ioctl_create_event_args *args = data;
1025 /* For dGPUs the event page is allocated in user mode. The
1026 * handle is passed to KFD with the first call to this IOCTL
1027 * through the event_page_offset field.
1029 if (args->event_page_offset) {
1030 struct kfd_dev *kfd;
1031 struct kfd_process_device *pdd;
1032 void *mem, *kern_addr;
1035 if (p->signal_page) {
1036 pr_err("Event page is already set\n");
1040 kfd = kfd_device_by_id(GET_GPU_ID(args->event_page_offset));
1042 pr_err("Getting device by id failed in %s\n", __func__);
1046 mutex_lock(&p->mutex);
1047 pdd = kfd_bind_process_to_device(kfd, p);
1053 mem = kfd_process_device_translate_handle(pdd,
1054 GET_IDR_HANDLE(args->event_page_offset));
1056 pr_err("Can't find BO, offset is 0x%llx\n",
1057 args->event_page_offset);
1061 mutex_unlock(&p->mutex);
1063 err = amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(kfd->kgd,
1064 mem, &kern_addr, &size);
1066 pr_err("Failed to map event page to kernel\n");
1070 err = kfd_event_page_set(p, kern_addr, size);
1072 pr_err("Failed to set event page\n");
1077 err = kfd_event_create(filp, p, args->event_type,
1078 args->auto_reset != 0, args->node_id,
1079 &args->event_id, &args->event_trigger_data,
1080 &args->event_page_offset,
1081 &args->event_slot_index);
1086 mutex_unlock(&p->mutex);
1090 static int kfd_ioctl_destroy_event(struct file *filp, struct kfd_process *p,
1093 struct kfd_ioctl_destroy_event_args *args = data;
1095 return kfd_event_destroy(p, args->event_id);
1098 static int kfd_ioctl_set_event(struct file *filp, struct kfd_process *p,
1101 struct kfd_ioctl_set_event_args *args = data;
1103 return kfd_set_event(p, args->event_id);
1106 static int kfd_ioctl_reset_event(struct file *filp, struct kfd_process *p,
1109 struct kfd_ioctl_reset_event_args *args = data;
1111 return kfd_reset_event(p, args->event_id);
1114 static int kfd_ioctl_wait_events(struct file *filp, struct kfd_process *p,
1117 struct kfd_ioctl_wait_events_args *args = data;
1120 err = kfd_wait_on_events(p, args->num_events,
1121 (void __user *)args->events_ptr,
1122 (args->wait_for_all != 0),
1123 args->timeout, &args->wait_result);
1127 static int kfd_ioctl_set_scratch_backing_va(struct file *filep,
1128 struct kfd_process *p, void *data)
1130 struct kfd_ioctl_set_scratch_backing_va_args *args = data;
1131 struct kfd_process_device *pdd;
1132 struct kfd_dev *dev;
1135 dev = kfd_device_by_id(args->gpu_id);
1139 mutex_lock(&p->mutex);
1141 pdd = kfd_bind_process_to_device(dev, p);
1144 goto bind_process_to_device_fail;
1147 pdd->qpd.sh_hidden_private_base = args->va_addr;
1149 mutex_unlock(&p->mutex);
1151 if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS &&
1152 pdd->qpd.vmid != 0 && dev->kfd2kgd->set_scratch_backing_va)
1153 dev->kfd2kgd->set_scratch_backing_va(
1154 dev->kgd, args->va_addr, pdd->qpd.vmid);
1158 bind_process_to_device_fail:
1159 mutex_unlock(&p->mutex);
1163 static int kfd_ioctl_get_tile_config(struct file *filep,
1164 struct kfd_process *p, void *data)
1166 struct kfd_ioctl_get_tile_config_args *args = data;
1167 struct kfd_dev *dev;
1168 struct tile_config config;
1171 dev = kfd_device_by_id(args->gpu_id);
1175 amdgpu_amdkfd_get_tile_config(dev->kgd, &config);
1177 args->gb_addr_config = config.gb_addr_config;
1178 args->num_banks = config.num_banks;
1179 args->num_ranks = config.num_ranks;
1181 if (args->num_tile_configs > config.num_tile_configs)
1182 args->num_tile_configs = config.num_tile_configs;
1183 err = copy_to_user((void __user *)args->tile_config_ptr,
1184 config.tile_config_ptr,
1185 args->num_tile_configs * sizeof(uint32_t));
1187 args->num_tile_configs = 0;
1191 if (args->num_macro_tile_configs > config.num_macro_tile_configs)
1192 args->num_macro_tile_configs =
1193 config.num_macro_tile_configs;
1194 err = copy_to_user((void __user *)args->macro_tile_config_ptr,
1195 config.macro_tile_config_ptr,
1196 args->num_macro_tile_configs * sizeof(uint32_t));
1198 args->num_macro_tile_configs = 0;
1205 static int kfd_ioctl_acquire_vm(struct file *filep, struct kfd_process *p,
1208 struct kfd_ioctl_acquire_vm_args *args = data;
1209 struct kfd_process_device *pdd;
1210 struct kfd_dev *dev;
1211 struct file *drm_file;
1214 dev = kfd_device_by_id(args->gpu_id);
1218 drm_file = fget(args->drm_fd);
1222 mutex_lock(&p->mutex);
1224 pdd = kfd_get_process_device_data(dev, p);
1230 if (pdd->drm_file) {
1231 ret = pdd->drm_file == drm_file ? 0 : -EBUSY;
1235 ret = kfd_process_device_init_vm(pdd, drm_file);
1238 /* On success, the PDD keeps the drm_file reference */
1239 mutex_unlock(&p->mutex);
1244 mutex_unlock(&p->mutex);
1249 bool kfd_dev_is_large_bar(struct kfd_dev *dev)
1251 struct kfd_local_mem_info mem_info;
1253 if (debug_largebar) {
1254 pr_debug("Simulate large-bar allocation on non large-bar machine\n");
1258 if (dev->use_iommu_v2)
1261 amdgpu_amdkfd_get_local_mem_info(dev->kgd, &mem_info);
1262 if (mem_info.local_mem_size_private == 0 &&
1263 mem_info.local_mem_size_public > 0)
1268 static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep,
1269 struct kfd_process *p, void *data)
1271 struct kfd_ioctl_alloc_memory_of_gpu_args *args = data;
1272 struct kfd_process_device *pdd;
1274 struct kfd_dev *dev;
1277 uint64_t offset = args->mmap_offset;
1278 uint32_t flags = args->flags;
1280 if (args->size == 0)
1283 dev = kfd_device_by_id(args->gpu_id);
1287 if ((flags & KFD_IOC_ALLOC_MEM_FLAGS_PUBLIC) &&
1288 (flags & KFD_IOC_ALLOC_MEM_FLAGS_VRAM) &&
1289 !kfd_dev_is_large_bar(dev)) {
1290 pr_err("Alloc host visible vram on small bar is not allowed\n");
1294 mutex_lock(&p->mutex);
1296 pdd = kfd_bind_process_to_device(dev, p);
1302 if (flags & KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL) {
1303 if (args->size != kfd_doorbell_process_slice(dev)) {
1307 offset = kfd_get_process_doorbells(pdd);
1308 } else if (flags & KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP) {
1309 if (args->size != PAGE_SIZE) {
1313 offset = amdgpu_amdkfd_get_mmio_remap_phys_addr(dev->kgd);
1320 err = amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
1321 dev->kgd, args->va_addr, args->size,
1322 pdd->vm, (struct kgd_mem **) &mem, &offset,
1328 idr_handle = kfd_process_device_create_obj_handle(pdd, mem);
1329 if (idr_handle < 0) {
1334 /* Update the VRAM usage count */
1335 if (flags & KFD_IOC_ALLOC_MEM_FLAGS_VRAM)
1336 WRITE_ONCE(pdd->vram_usage, pdd->vram_usage + args->size);
1338 mutex_unlock(&p->mutex);
1340 args->handle = MAKE_HANDLE(args->gpu_id, idr_handle);
1341 args->mmap_offset = offset;
1343 /* MMIO is mapped through kfd device
1344 * Generate a kfd mmap offset
1346 if (flags & KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP)
1347 args->mmap_offset = KFD_MMAP_TYPE_MMIO
1348 | KFD_MMAP_GPU_ID(args->gpu_id);
1353 amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->kgd, (struct kgd_mem *)mem, NULL);
1355 mutex_unlock(&p->mutex);
1359 static int kfd_ioctl_free_memory_of_gpu(struct file *filep,
1360 struct kfd_process *p, void *data)
1362 struct kfd_ioctl_free_memory_of_gpu_args *args = data;
1363 struct kfd_process_device *pdd;
1365 struct kfd_dev *dev;
1369 dev = kfd_device_by_id(GET_GPU_ID(args->handle));
1373 mutex_lock(&p->mutex);
1375 pdd = kfd_get_process_device_data(dev, p);
1377 pr_err("Process device data doesn't exist\n");
1382 mem = kfd_process_device_translate_handle(
1383 pdd, GET_IDR_HANDLE(args->handle));
1389 ret = amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->kgd,
1390 (struct kgd_mem *)mem, &size);
1392 /* If freeing the buffer failed, leave the handle in place for
1393 * clean-up during process tear-down.
1396 kfd_process_device_remove_obj_handle(
1397 pdd, GET_IDR_HANDLE(args->handle));
1399 WRITE_ONCE(pdd->vram_usage, pdd->vram_usage - size);
1402 mutex_unlock(&p->mutex);
1406 static int kfd_ioctl_map_memory_to_gpu(struct file *filep,
1407 struct kfd_process *p, void *data)
1409 struct kfd_ioctl_map_memory_to_gpu_args *args = data;
1410 struct kfd_process_device *pdd, *peer_pdd;
1412 struct kfd_dev *dev, *peer;
1415 uint32_t *devices_arr = NULL;
1417 dev = kfd_device_by_id(GET_GPU_ID(args->handle));
1421 if (!args->n_devices) {
1422 pr_debug("Device IDs array empty\n");
1425 if (args->n_success > args->n_devices) {
1426 pr_debug("n_success exceeds n_devices\n");
1430 devices_arr = kmalloc_array(args->n_devices, sizeof(*devices_arr),
1435 err = copy_from_user(devices_arr,
1436 (void __user *)args->device_ids_array_ptr,
1437 args->n_devices * sizeof(*devices_arr));
1440 goto copy_from_user_failed;
1443 mutex_lock(&p->mutex);
1445 pdd = kfd_bind_process_to_device(dev, p);
1448 goto bind_process_to_device_failed;
1451 mem = kfd_process_device_translate_handle(pdd,
1452 GET_IDR_HANDLE(args->handle));
1455 goto get_mem_obj_from_handle_failed;
1458 for (i = args->n_success; i < args->n_devices; i++) {
1459 peer = kfd_device_by_id(devices_arr[i]);
1461 pr_debug("Getting device by id failed for 0x%x\n",
1464 goto get_mem_obj_from_handle_failed;
1467 peer_pdd = kfd_bind_process_to_device(peer, p);
1468 if (IS_ERR(peer_pdd)) {
1469 err = PTR_ERR(peer_pdd);
1470 goto get_mem_obj_from_handle_failed;
1472 err = amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
1473 peer->kgd, (struct kgd_mem *)mem, peer_pdd->vm);
1475 pr_err("Failed to map to gpu %d/%d\n",
1476 i, args->n_devices);
1477 goto map_memory_to_gpu_failed;
1479 args->n_success = i+1;
1482 mutex_unlock(&p->mutex);
1484 err = amdgpu_amdkfd_gpuvm_sync_memory(dev->kgd, (struct kgd_mem *) mem, true);
1486 pr_debug("Sync memory failed, wait interrupted by user signal\n");
1487 goto sync_memory_failed;
1490 /* Flush TLBs after waiting for the page table updates to complete */
1491 for (i = 0; i < args->n_devices; i++) {
1492 peer = kfd_device_by_id(devices_arr[i]);
1493 if (WARN_ON_ONCE(!peer))
1495 peer_pdd = kfd_get_process_device_data(peer, p);
1496 if (WARN_ON_ONCE(!peer_pdd))
1498 kfd_flush_tlb(peer_pdd);
1505 bind_process_to_device_failed:
1506 get_mem_obj_from_handle_failed:
1507 map_memory_to_gpu_failed:
1508 mutex_unlock(&p->mutex);
1509 copy_from_user_failed:
1516 static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
1517 struct kfd_process *p, void *data)
1519 struct kfd_ioctl_unmap_memory_from_gpu_args *args = data;
1520 struct kfd_process_device *pdd, *peer_pdd;
1522 struct kfd_dev *dev, *peer;
1524 uint32_t *devices_arr = NULL, i;
1526 dev = kfd_device_by_id(GET_GPU_ID(args->handle));
1530 if (!args->n_devices) {
1531 pr_debug("Device IDs array empty\n");
1534 if (args->n_success > args->n_devices) {
1535 pr_debug("n_success exceeds n_devices\n");
1539 devices_arr = kmalloc_array(args->n_devices, sizeof(*devices_arr),
1544 err = copy_from_user(devices_arr,
1545 (void __user *)args->device_ids_array_ptr,
1546 args->n_devices * sizeof(*devices_arr));
1549 goto copy_from_user_failed;
1552 mutex_lock(&p->mutex);
1554 pdd = kfd_get_process_device_data(dev, p);
1557 goto bind_process_to_device_failed;
1560 mem = kfd_process_device_translate_handle(pdd,
1561 GET_IDR_HANDLE(args->handle));
1564 goto get_mem_obj_from_handle_failed;
1567 for (i = args->n_success; i < args->n_devices; i++) {
1568 peer = kfd_device_by_id(devices_arr[i]);
1571 goto get_mem_obj_from_handle_failed;
1574 peer_pdd = kfd_get_process_device_data(peer, p);
1577 goto get_mem_obj_from_handle_failed;
1579 err = amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
1580 peer->kgd, (struct kgd_mem *)mem, peer_pdd->vm);
1582 pr_err("Failed to unmap from gpu %d/%d\n",
1583 i, args->n_devices);
1584 goto unmap_memory_from_gpu_failed;
1586 args->n_success = i+1;
1590 mutex_unlock(&p->mutex);
1594 bind_process_to_device_failed:
1595 get_mem_obj_from_handle_failed:
1596 unmap_memory_from_gpu_failed:
1597 mutex_unlock(&p->mutex);
1598 copy_from_user_failed:
1603 static int kfd_ioctl_alloc_queue_gws(struct file *filep,
1604 struct kfd_process *p, void *data)
1607 struct kfd_ioctl_alloc_queue_gws_args *args = data;
1609 struct kfd_dev *dev;
1611 mutex_lock(&p->mutex);
1612 q = pqm_get_user_queue(&p->pqm, args->queue_id);
1626 if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
1631 retval = pqm_set_gws(&p->pqm, args->queue_id, args->num_gws ? dev->gws : NULL);
1632 mutex_unlock(&p->mutex);
1634 args->first_gws = 0;
1638 mutex_unlock(&p->mutex);
1642 static int kfd_ioctl_get_dmabuf_info(struct file *filep,
1643 struct kfd_process *p, void *data)
1645 struct kfd_ioctl_get_dmabuf_info_args *args = data;
1646 struct kfd_dev *dev = NULL;
1647 struct kgd_dev *dma_buf_kgd;
1648 void *metadata_buffer = NULL;
1653 /* Find a KFD GPU device that supports the get_dmabuf_info query */
1654 for (i = 0; kfd_topology_enum_kfd_devices(i, &dev) == 0; i++)
1660 if (args->metadata_ptr) {
1661 metadata_buffer = kzalloc(args->metadata_size, GFP_KERNEL);
1662 if (!metadata_buffer)
1666 /* Get dmabuf info from KGD */
1667 r = amdgpu_amdkfd_get_dmabuf_info(dev->kgd, args->dmabuf_fd,
1668 &dma_buf_kgd, &args->size,
1669 metadata_buffer, args->metadata_size,
1670 &args->metadata_size, &flags);
1674 /* Reverse-lookup gpu_id from kgd pointer */
1675 dev = kfd_device_by_kgd(dma_buf_kgd);
1680 args->gpu_id = dev->id;
1681 args->flags = flags;
1683 /* Copy metadata buffer to user mode */
1684 if (metadata_buffer) {
1685 r = copy_to_user((void __user *)args->metadata_ptr,
1686 metadata_buffer, args->metadata_size);
1692 kfree(metadata_buffer);
1697 static int kfd_ioctl_import_dmabuf(struct file *filep,
1698 struct kfd_process *p, void *data)
1700 struct kfd_ioctl_import_dmabuf_args *args = data;
1701 struct kfd_process_device *pdd;
1702 struct dma_buf *dmabuf;
1703 struct kfd_dev *dev;
1709 dev = kfd_device_by_id(args->gpu_id);
1713 dmabuf = dma_buf_get(args->dmabuf_fd);
1715 return PTR_ERR(dmabuf);
1717 mutex_lock(&p->mutex);
1719 pdd = kfd_bind_process_to_device(dev, p);
1725 r = amdgpu_amdkfd_gpuvm_import_dmabuf(dev->kgd, dmabuf,
1726 args->va_addr, pdd->vm,
1727 (struct kgd_mem **)&mem, &size,
1732 idr_handle = kfd_process_device_create_obj_handle(pdd, mem);
1733 if (idr_handle < 0) {
1738 mutex_unlock(&p->mutex);
1739 dma_buf_put(dmabuf);
1741 args->handle = MAKE_HANDLE(args->gpu_id, idr_handle);
1746 amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->kgd, (struct kgd_mem *)mem, NULL);
1748 mutex_unlock(&p->mutex);
1749 dma_buf_put(dmabuf);
1753 /* Handle requests for watching SMI events */
1754 static int kfd_ioctl_smi_events(struct file *filep,
1755 struct kfd_process *p, void *data)
1757 struct kfd_ioctl_smi_events_args *args = data;
1758 struct kfd_dev *dev;
1760 dev = kfd_device_by_id(args->gpuid);
1764 return kfd_smi_event_open(dev, &args->anon_fd);
1767 #define AMDKFD_IOCTL_DEF(ioctl, _func, _flags) \
1768 [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func, .flags = _flags, \
1769 .cmd_drv = 0, .name = #ioctl}
1772 static const struct amdkfd_ioctl_desc amdkfd_ioctls[] = {
1773 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_VERSION,
1774 kfd_ioctl_get_version, 0),
1776 AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_QUEUE,
1777 kfd_ioctl_create_queue, 0),
1779 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_QUEUE,
1780 kfd_ioctl_destroy_queue, 0),
1782 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_MEMORY_POLICY,
1783 kfd_ioctl_set_memory_policy, 0),
1785 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_CLOCK_COUNTERS,
1786 kfd_ioctl_get_clock_counters, 0),
1788 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_PROCESS_APERTURES,
1789 kfd_ioctl_get_process_apertures, 0),
1791 AMDKFD_IOCTL_DEF(AMDKFD_IOC_UPDATE_QUEUE,
1792 kfd_ioctl_update_queue, 0),
1794 AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_EVENT,
1795 kfd_ioctl_create_event, 0),
1797 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_EVENT,
1798 kfd_ioctl_destroy_event, 0),
1800 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_EVENT,
1801 kfd_ioctl_set_event, 0),
1803 AMDKFD_IOCTL_DEF(AMDKFD_IOC_RESET_EVENT,
1804 kfd_ioctl_reset_event, 0),
1806 AMDKFD_IOCTL_DEF(AMDKFD_IOC_WAIT_EVENTS,
1807 kfd_ioctl_wait_events, 0),
1809 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_REGISTER,
1810 kfd_ioctl_dbg_register, 0),
1812 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_UNREGISTER,
1813 kfd_ioctl_dbg_unregister, 0),
1815 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_ADDRESS_WATCH,
1816 kfd_ioctl_dbg_address_watch, 0),
1818 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_WAVE_CONTROL,
1819 kfd_ioctl_dbg_wave_control, 0),
1821 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_SCRATCH_BACKING_VA,
1822 kfd_ioctl_set_scratch_backing_va, 0),
1824 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_TILE_CONFIG,
1825 kfd_ioctl_get_tile_config, 0),
1827 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_TRAP_HANDLER,
1828 kfd_ioctl_set_trap_handler, 0),
1830 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_PROCESS_APERTURES_NEW,
1831 kfd_ioctl_get_process_apertures_new, 0),
1833 AMDKFD_IOCTL_DEF(AMDKFD_IOC_ACQUIRE_VM,
1834 kfd_ioctl_acquire_vm, 0),
1836 AMDKFD_IOCTL_DEF(AMDKFD_IOC_ALLOC_MEMORY_OF_GPU,
1837 kfd_ioctl_alloc_memory_of_gpu, 0),
1839 AMDKFD_IOCTL_DEF(AMDKFD_IOC_FREE_MEMORY_OF_GPU,
1840 kfd_ioctl_free_memory_of_gpu, 0),
1842 AMDKFD_IOCTL_DEF(AMDKFD_IOC_MAP_MEMORY_TO_GPU,
1843 kfd_ioctl_map_memory_to_gpu, 0),
1845 AMDKFD_IOCTL_DEF(AMDKFD_IOC_UNMAP_MEMORY_FROM_GPU,
1846 kfd_ioctl_unmap_memory_from_gpu, 0),
1848 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_CU_MASK,
1849 kfd_ioctl_set_cu_mask, 0),
1851 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_QUEUE_WAVE_STATE,
1852 kfd_ioctl_get_queue_wave_state, 0),
1854 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_DMABUF_INFO,
1855 kfd_ioctl_get_dmabuf_info, 0),
1857 AMDKFD_IOCTL_DEF(AMDKFD_IOC_IMPORT_DMABUF,
1858 kfd_ioctl_import_dmabuf, 0),
1860 AMDKFD_IOCTL_DEF(AMDKFD_IOC_ALLOC_QUEUE_GWS,
1861 kfd_ioctl_alloc_queue_gws, 0),
1863 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SMI_EVENTS,
1864 kfd_ioctl_smi_events, 0),
1867 #define AMDKFD_CORE_IOCTL_COUNT ARRAY_SIZE(amdkfd_ioctls)
1869 static long kfd_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1871 struct kfd_process *process;
1872 amdkfd_ioctl_t *func;
1873 const struct amdkfd_ioctl_desc *ioctl = NULL;
1874 unsigned int nr = _IOC_NR(cmd);
1875 char stack_kdata[128];
1877 unsigned int usize, asize;
1878 int retcode = -EINVAL;
1880 if (nr >= AMDKFD_CORE_IOCTL_COUNT)
1883 if ((nr >= AMDKFD_COMMAND_START) && (nr < AMDKFD_COMMAND_END)) {
1886 ioctl = &amdkfd_ioctls[nr];
1888 amdkfd_size = _IOC_SIZE(ioctl->cmd);
1889 usize = asize = _IOC_SIZE(cmd);
1890 if (amdkfd_size > asize)
1891 asize = amdkfd_size;
1897 dev_dbg(kfd_device, "ioctl cmd 0x%x (#0x%x), arg 0x%lx\n", cmd, nr, arg);
1899 /* Get the process struct from the filep. Only the process
1900 * that opened /dev/kfd can use the file descriptor. Child
1901 * processes need to create their own KFD device context.
1903 process = filep->private_data;
1904 if (process->lead_thread != current->group_leader) {
1905 dev_dbg(kfd_device, "Using KFD FD in wrong process\n");
1910 /* Do not trust userspace, use our own definition */
1913 if (unlikely(!func)) {
1914 dev_dbg(kfd_device, "no function\n");
1919 if (cmd & (IOC_IN | IOC_OUT)) {
1920 if (asize <= sizeof(stack_kdata)) {
1921 kdata = stack_kdata;
1923 kdata = kmalloc(asize, GFP_KERNEL);
1930 memset(kdata + usize, 0, asize - usize);
1934 if (copy_from_user(kdata, (void __user *)arg, usize) != 0) {
1938 } else if (cmd & IOC_OUT) {
1939 memset(kdata, 0, usize);
1942 retcode = func(filep, process, kdata);
1945 if (copy_to_user((void __user *)arg, kdata, usize) != 0)
1950 dev_dbg(kfd_device, "invalid ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n",
1951 task_pid_nr(current), cmd, nr);
1953 if (kdata != stack_kdata)
1957 dev_dbg(kfd_device, "ioctl cmd (#0x%x), arg 0x%lx, ret = %d\n",
1963 static int kfd_mmio_mmap(struct kfd_dev *dev, struct kfd_process *process,
1964 struct vm_area_struct *vma)
1966 phys_addr_t address;
1969 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1972 address = amdgpu_amdkfd_get_mmio_remap_phys_addr(dev->kgd);
1974 vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_NORESERVE |
1975 VM_DONTDUMP | VM_PFNMAP;
1977 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1979 pr_debug("pasid 0x%x mapping mmio page\n"
1980 " target user address == 0x%08llX\n"
1981 " physical address == 0x%08llX\n"
1982 " vm_flags == 0x%04lX\n"
1983 " size == 0x%04lX\n",
1984 process->pasid, (unsigned long long) vma->vm_start,
1985 address, vma->vm_flags, PAGE_SIZE);
1987 ret = io_remap_pfn_range(vma,
1989 address >> PAGE_SHIFT,
1996 static int kfd_mmap(struct file *filp, struct vm_area_struct *vma)
1998 struct kfd_process *process;
1999 struct kfd_dev *dev = NULL;
2000 unsigned long mmap_offset;
2001 unsigned int gpu_id;
2003 process = kfd_get_process(current);
2004 if (IS_ERR(process))
2005 return PTR_ERR(process);
2007 mmap_offset = vma->vm_pgoff << PAGE_SHIFT;
2008 gpu_id = KFD_MMAP_GET_GPU_ID(mmap_offset);
2010 dev = kfd_device_by_id(gpu_id);
2012 switch (mmap_offset & KFD_MMAP_TYPE_MASK) {
2013 case KFD_MMAP_TYPE_DOORBELL:
2016 return kfd_doorbell_mmap(dev, process, vma);
2018 case KFD_MMAP_TYPE_EVENTS:
2019 return kfd_event_mmap(process, vma);
2021 case KFD_MMAP_TYPE_RESERVED_MEM:
2024 return kfd_reserved_mem_mmap(dev, process, vma);
2025 case KFD_MMAP_TYPE_MMIO:
2028 return kfd_mmio_mmap(dev, process, vma);