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"
43 static long kfd_ioctl(struct file
*, unsigned int, unsigned long);
44 static int kfd_open(struct inode
*, struct file
*);
45 static int kfd_mmap(struct file
*, struct vm_area_struct
*);
47 static const char kfd_dev_name
[] = "kfd";
49 static const struct file_operations kfd_fops
= {
51 .unlocked_ioctl
= kfd_ioctl
,
52 .compat_ioctl
= kfd_ioctl
,
57 static int kfd_char_dev_major
= -1;
58 static struct class *kfd_class
;
59 struct device
*kfd_device
;
61 int kfd_chardev_init(void)
65 kfd_char_dev_major
= register_chrdev(0, kfd_dev_name
, &kfd_fops
);
66 err
= kfd_char_dev_major
;
68 goto err_register_chrdev
;
70 kfd_class
= class_create(THIS_MODULE
, kfd_dev_name
);
71 err
= PTR_ERR(kfd_class
);
72 if (IS_ERR(kfd_class
))
73 goto err_class_create
;
75 kfd_device
= device_create(kfd_class
, NULL
,
76 MKDEV(kfd_char_dev_major
, 0),
78 err
= PTR_ERR(kfd_device
);
79 if (IS_ERR(kfd_device
))
80 goto err_device_create
;
85 class_destroy(kfd_class
);
87 unregister_chrdev(kfd_char_dev_major
, kfd_dev_name
);
92 void kfd_chardev_exit(void)
94 device_destroy(kfd_class
, MKDEV(kfd_char_dev_major
, 0));
95 class_destroy(kfd_class
);
96 unregister_chrdev(kfd_char_dev_major
, kfd_dev_name
);
99 struct device
*kfd_chardev(void)
105 static int kfd_open(struct inode
*inode
, struct file
*filep
)
107 struct kfd_process
*process
;
108 bool is_32bit_user_mode
;
110 if (iminor(inode
) != 0)
113 is_32bit_user_mode
= in_compat_syscall();
115 if (is_32bit_user_mode
) {
117 "Process %d (32-bit) failed to open /dev/kfd\n"
118 "32-bit processes are not supported by amdkfd\n",
123 process
= kfd_create_process(filep
);
125 return PTR_ERR(process
);
130 dev_dbg(kfd_device
, "process %d opened, compat mode (32 bit) - %d\n",
131 process
->pasid
, process
->is_32bit_user_mode
);
136 static int kfd_ioctl_get_version(struct file
*filep
, struct kfd_process
*p
,
139 struct kfd_ioctl_get_version_args
*args
= data
;
141 args
->major_version
= KFD_IOCTL_MAJOR_VERSION
;
142 args
->minor_version
= KFD_IOCTL_MINOR_VERSION
;
147 static int set_queue_properties_from_user(struct queue_properties
*q_properties
,
148 struct kfd_ioctl_create_queue_args
*args
)
150 if (args
->queue_percentage
> KFD_MAX_QUEUE_PERCENTAGE
) {
151 pr_err("Queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
155 if (args
->queue_priority
> KFD_MAX_QUEUE_PRIORITY
) {
156 pr_err("Queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
160 if ((args
->ring_base_address
) &&
161 (!access_ok((const void __user
*) args
->ring_base_address
,
162 sizeof(uint64_t)))) {
163 pr_err("Can't access ring base address\n");
167 if (!is_power_of_2(args
->ring_size
) && (args
->ring_size
!= 0)) {
168 pr_err("Ring size must be a power of 2 or 0\n");
172 if (!access_ok((const void __user
*) args
->read_pointer_address
,
174 pr_err("Can't access read pointer\n");
178 if (!access_ok((const void __user
*) args
->write_pointer_address
,
180 pr_err("Can't access write pointer\n");
184 if (args
->eop_buffer_address
&&
185 !access_ok((const void __user
*) args
->eop_buffer_address
,
187 pr_debug("Can't access eop buffer");
191 if (args
->ctx_save_restore_address
&&
192 !access_ok((const void __user
*) args
->ctx_save_restore_address
,
194 pr_debug("Can't access ctx save restore buffer");
198 q_properties
->is_interop
= false;
199 q_properties
->queue_percent
= args
->queue_percentage
;
200 q_properties
->priority
= args
->queue_priority
;
201 q_properties
->queue_address
= args
->ring_base_address
;
202 q_properties
->queue_size
= args
->ring_size
;
203 q_properties
->read_ptr
= (uint32_t *) args
->read_pointer_address
;
204 q_properties
->write_ptr
= (uint32_t *) args
->write_pointer_address
;
205 q_properties
->eop_ring_buffer_address
= args
->eop_buffer_address
;
206 q_properties
->eop_ring_buffer_size
= args
->eop_buffer_size
;
207 q_properties
->ctx_save_restore_area_address
=
208 args
->ctx_save_restore_address
;
209 q_properties
->ctx_save_restore_area_size
= args
->ctx_save_restore_size
;
210 q_properties
->ctl_stack_size
= args
->ctl_stack_size
;
211 if (args
->queue_type
== KFD_IOC_QUEUE_TYPE_COMPUTE
||
212 args
->queue_type
== KFD_IOC_QUEUE_TYPE_COMPUTE_AQL
)
213 q_properties
->type
= KFD_QUEUE_TYPE_COMPUTE
;
214 else if (args
->queue_type
== KFD_IOC_QUEUE_TYPE_SDMA
)
215 q_properties
->type
= KFD_QUEUE_TYPE_SDMA
;
219 if (args
->queue_type
== KFD_IOC_QUEUE_TYPE_COMPUTE_AQL
)
220 q_properties
->format
= KFD_QUEUE_FORMAT_AQL
;
222 q_properties
->format
= KFD_QUEUE_FORMAT_PM4
;
224 pr_debug("Queue Percentage: %d, %d\n",
225 q_properties
->queue_percent
, args
->queue_percentage
);
227 pr_debug("Queue Priority: %d, %d\n",
228 q_properties
->priority
, args
->queue_priority
);
230 pr_debug("Queue Address: 0x%llX, 0x%llX\n",
231 q_properties
->queue_address
, args
->ring_base_address
);
233 pr_debug("Queue Size: 0x%llX, %u\n",
234 q_properties
->queue_size
, args
->ring_size
);
236 pr_debug("Queue r/w Pointers: %px, %px\n",
237 q_properties
->read_ptr
,
238 q_properties
->write_ptr
);
240 pr_debug("Queue Format: %d\n", q_properties
->format
);
242 pr_debug("Queue EOP: 0x%llX\n", q_properties
->eop_ring_buffer_address
);
244 pr_debug("Queue CTX save area: 0x%llX\n",
245 q_properties
->ctx_save_restore_area_address
);
250 static int kfd_ioctl_create_queue(struct file
*filep
, struct kfd_process
*p
,
253 struct kfd_ioctl_create_queue_args
*args
= data
;
256 unsigned int queue_id
;
257 struct kfd_process_device
*pdd
;
258 struct queue_properties q_properties
;
260 memset(&q_properties
, 0, sizeof(struct queue_properties
));
262 pr_debug("Creating queue ioctl\n");
264 err
= set_queue_properties_from_user(&q_properties
, args
);
268 pr_debug("Looking for gpu id 0x%x\n", args
->gpu_id
);
269 dev
= kfd_device_by_id(args
->gpu_id
);
271 pr_debug("Could not find gpu id 0x%x\n", args
->gpu_id
);
275 mutex_lock(&p
->mutex
);
277 pdd
= kfd_bind_process_to_device(dev
, p
);
280 goto err_bind_process
;
283 pr_debug("Creating queue for PASID %d on gpu 0x%x\n",
287 err
= pqm_create_queue(&p
->pqm
, dev
, filep
, &q_properties
, &queue_id
);
289 goto err_create_queue
;
291 args
->queue_id
= queue_id
;
294 /* Return gpu_id as doorbell offset for mmap usage */
295 args
->doorbell_offset
= KFD_MMAP_TYPE_DOORBELL
;
296 args
->doorbell_offset
|= KFD_MMAP_GPU_ID(args
->gpu_id
);
297 args
->doorbell_offset
<<= PAGE_SHIFT
;
298 if (KFD_IS_SOC15(dev
->device_info
->asic_family
))
299 /* On SOC15 ASICs, doorbell allocation must be
300 * per-device, and independent from the per-process
301 * queue_id. Return the doorbell offset within the
302 * doorbell aperture to user mode.
304 args
->doorbell_offset
|= q_properties
.doorbell_off
;
306 mutex_unlock(&p
->mutex
);
308 pr_debug("Queue id %d was created successfully\n", args
->queue_id
);
310 pr_debug("Ring buffer address == 0x%016llX\n",
311 args
->ring_base_address
);
313 pr_debug("Read ptr address == 0x%016llX\n",
314 args
->read_pointer_address
);
316 pr_debug("Write ptr address == 0x%016llX\n",
317 args
->write_pointer_address
);
323 mutex_unlock(&p
->mutex
);
327 static int kfd_ioctl_destroy_queue(struct file
*filp
, struct kfd_process
*p
,
331 struct kfd_ioctl_destroy_queue_args
*args
= data
;
333 pr_debug("Destroying queue id %d for pasid %d\n",
337 mutex_lock(&p
->mutex
);
339 retval
= pqm_destroy_queue(&p
->pqm
, args
->queue_id
);
341 mutex_unlock(&p
->mutex
);
345 static int kfd_ioctl_update_queue(struct file
*filp
, struct kfd_process
*p
,
349 struct kfd_ioctl_update_queue_args
*args
= data
;
350 struct queue_properties properties
;
352 if (args
->queue_percentage
> KFD_MAX_QUEUE_PERCENTAGE
) {
353 pr_err("Queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
357 if (args
->queue_priority
> KFD_MAX_QUEUE_PRIORITY
) {
358 pr_err("Queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
362 if ((args
->ring_base_address
) &&
363 (!access_ok((const void __user
*) args
->ring_base_address
,
364 sizeof(uint64_t)))) {
365 pr_err("Can't access ring base address\n");
369 if (!is_power_of_2(args
->ring_size
) && (args
->ring_size
!= 0)) {
370 pr_err("Ring size must be a power of 2 or 0\n");
374 properties
.queue_address
= args
->ring_base_address
;
375 properties
.queue_size
= args
->ring_size
;
376 properties
.queue_percent
= args
->queue_percentage
;
377 properties
.priority
= args
->queue_priority
;
379 pr_debug("Updating queue id %d for pasid %d\n",
380 args
->queue_id
, p
->pasid
);
382 mutex_lock(&p
->mutex
);
384 retval
= pqm_update_queue(&p
->pqm
, args
->queue_id
, &properties
);
386 mutex_unlock(&p
->mutex
);
391 static int kfd_ioctl_set_cu_mask(struct file
*filp
, struct kfd_process
*p
,
395 const int max_num_cus
= 1024;
396 struct kfd_ioctl_set_cu_mask_args
*args
= data
;
397 struct queue_properties properties
;
398 uint32_t __user
*cu_mask_ptr
= (uint32_t __user
*)args
->cu_mask_ptr
;
399 size_t cu_mask_size
= sizeof(uint32_t) * (args
->num_cu_mask
/ 32);
401 if ((args
->num_cu_mask
% 32) != 0) {
402 pr_debug("num_cu_mask 0x%x must be a multiple of 32",
407 properties
.cu_mask_count
= args
->num_cu_mask
;
408 if (properties
.cu_mask_count
== 0) {
409 pr_debug("CU mask cannot be 0");
413 /* To prevent an unreasonably large CU mask size, set an arbitrary
414 * limit of max_num_cus bits. We can then just drop any CU mask bits
415 * past max_num_cus bits and just use the first max_num_cus bits.
417 if (properties
.cu_mask_count
> max_num_cus
) {
418 pr_debug("CU mask cannot be greater than 1024 bits");
419 properties
.cu_mask_count
= max_num_cus
;
420 cu_mask_size
= sizeof(uint32_t) * (max_num_cus
/32);
423 properties
.cu_mask
= kzalloc(cu_mask_size
, GFP_KERNEL
);
424 if (!properties
.cu_mask
)
427 retval
= copy_from_user(properties
.cu_mask
, cu_mask_ptr
, cu_mask_size
);
429 pr_debug("Could not copy CU mask from userspace");
430 kfree(properties
.cu_mask
);
434 mutex_lock(&p
->mutex
);
436 retval
= pqm_set_cu_mask(&p
->pqm
, args
->queue_id
, &properties
);
438 mutex_unlock(&p
->mutex
);
441 kfree(properties
.cu_mask
);
446 static int kfd_ioctl_get_queue_wave_state(struct file
*filep
,
447 struct kfd_process
*p
, void *data
)
449 struct kfd_ioctl_get_queue_wave_state_args
*args
= data
;
452 mutex_lock(&p
->mutex
);
454 r
= pqm_get_wave_state(&p
->pqm
, args
->queue_id
,
455 (void __user
*)args
->ctl_stack_address
,
456 &args
->ctl_stack_used_size
,
457 &args
->save_area_used_size
);
459 mutex_unlock(&p
->mutex
);
464 static int kfd_ioctl_set_memory_policy(struct file
*filep
,
465 struct kfd_process
*p
, void *data
)
467 struct kfd_ioctl_set_memory_policy_args
*args
= data
;
470 struct kfd_process_device
*pdd
;
471 enum cache_policy default_policy
, alternate_policy
;
473 if (args
->default_policy
!= KFD_IOC_CACHE_POLICY_COHERENT
474 && args
->default_policy
!= KFD_IOC_CACHE_POLICY_NONCOHERENT
) {
478 if (args
->alternate_policy
!= KFD_IOC_CACHE_POLICY_COHERENT
479 && args
->alternate_policy
!= KFD_IOC_CACHE_POLICY_NONCOHERENT
) {
483 dev
= kfd_device_by_id(args
->gpu_id
);
487 mutex_lock(&p
->mutex
);
489 pdd
= kfd_bind_process_to_device(dev
, p
);
495 default_policy
= (args
->default_policy
== KFD_IOC_CACHE_POLICY_COHERENT
)
496 ? cache_policy_coherent
: cache_policy_noncoherent
;
499 (args
->alternate_policy
== KFD_IOC_CACHE_POLICY_COHERENT
)
500 ? cache_policy_coherent
: cache_policy_noncoherent
;
502 if (!dev
->dqm
->ops
.set_cache_memory_policy(dev
->dqm
,
506 (void __user
*)args
->alternate_aperture_base
,
507 args
->alternate_aperture_size
))
511 mutex_unlock(&p
->mutex
);
516 static int kfd_ioctl_set_trap_handler(struct file
*filep
,
517 struct kfd_process
*p
, void *data
)
519 struct kfd_ioctl_set_trap_handler_args
*args
= data
;
522 struct kfd_process_device
*pdd
;
524 dev
= kfd_device_by_id(args
->gpu_id
);
528 mutex_lock(&p
->mutex
);
530 pdd
= kfd_bind_process_to_device(dev
, p
);
536 if (dev
->dqm
->ops
.set_trap_handler(dev
->dqm
,
543 mutex_unlock(&p
->mutex
);
548 static int kfd_ioctl_dbg_register(struct file
*filep
,
549 struct kfd_process
*p
, void *data
)
551 struct kfd_ioctl_dbg_register_args
*args
= data
;
553 struct kfd_dbgmgr
*dbgmgr_ptr
;
554 struct kfd_process_device
*pdd
;
558 dev
= kfd_device_by_id(args
->gpu_id
);
562 if (dev
->device_info
->asic_family
== CHIP_CARRIZO
) {
563 pr_debug("kfd_ioctl_dbg_register not supported on CZ\n");
567 mutex_lock(&p
->mutex
);
568 mutex_lock(kfd_get_dbgmgr_mutex());
571 * make sure that we have pdd, if this the first queue created for
574 pdd
= kfd_bind_process_to_device(dev
, p
);
576 status
= PTR_ERR(pdd
);
581 /* In case of a legal call, we have no dbgmgr yet */
582 create_ok
= kfd_dbgmgr_create(&dbgmgr_ptr
, dev
);
584 status
= kfd_dbgmgr_register(dbgmgr_ptr
, p
);
586 kfd_dbgmgr_destroy(dbgmgr_ptr
);
588 dev
->dbgmgr
= dbgmgr_ptr
;
591 pr_debug("debugger already registered\n");
596 mutex_unlock(kfd_get_dbgmgr_mutex());
597 mutex_unlock(&p
->mutex
);
602 static int kfd_ioctl_dbg_unregister(struct file
*filep
,
603 struct kfd_process
*p
, void *data
)
605 struct kfd_ioctl_dbg_unregister_args
*args
= data
;
609 dev
= kfd_device_by_id(args
->gpu_id
);
610 if (!dev
|| !dev
->dbgmgr
)
613 if (dev
->device_info
->asic_family
== CHIP_CARRIZO
) {
614 pr_debug("kfd_ioctl_dbg_unregister not supported on CZ\n");
618 mutex_lock(kfd_get_dbgmgr_mutex());
620 status
= kfd_dbgmgr_unregister(dev
->dbgmgr
, p
);
622 kfd_dbgmgr_destroy(dev
->dbgmgr
);
626 mutex_unlock(kfd_get_dbgmgr_mutex());
632 * Parse and generate variable size data structure for address watch.
633 * Total size of the buffer and # watch points is limited in order
634 * to prevent kernel abuse. (no bearing to the much smaller HW limitation
635 * which is enforced by dbgdev module)
636 * please also note that the watch address itself are not "copied from user",
637 * since it be set into the HW in user mode values.
640 static int kfd_ioctl_dbg_address_watch(struct file
*filep
,
641 struct kfd_process
*p
, void *data
)
643 struct kfd_ioctl_dbg_address_watch_args
*args
= data
;
645 struct dbg_address_watch_info aw_info
;
646 unsigned char *args_buff
;
648 void __user
*cmd_from_user
;
649 uint64_t watch_mask_value
= 0;
650 unsigned int args_idx
= 0;
652 memset((void *) &aw_info
, 0, sizeof(struct dbg_address_watch_info
));
654 dev
= kfd_device_by_id(args
->gpu_id
);
658 if (dev
->device_info
->asic_family
== CHIP_CARRIZO
) {
659 pr_debug("kfd_ioctl_dbg_wave_control not supported on CZ\n");
663 cmd_from_user
= (void __user
*) args
->content_ptr
;
665 /* Validate arguments */
667 if ((args
->buf_size_in_bytes
> MAX_ALLOWED_AW_BUFF_SIZE
) ||
668 (args
->buf_size_in_bytes
<= sizeof(*args
) + sizeof(int) * 2) ||
669 (cmd_from_user
== NULL
))
672 /* this is the actual buffer to work with */
673 args_buff
= memdup_user(cmd_from_user
,
674 args
->buf_size_in_bytes
- sizeof(*args
));
675 if (IS_ERR(args_buff
))
676 return PTR_ERR(args_buff
);
680 aw_info
.num_watch_points
= *((uint32_t *)(&args_buff
[args_idx
]));
681 args_idx
+= sizeof(aw_info
.num_watch_points
);
683 aw_info
.watch_mode
= (enum HSA_DBG_WATCH_MODE
*) &args_buff
[args_idx
];
684 args_idx
+= sizeof(enum HSA_DBG_WATCH_MODE
) * aw_info
.num_watch_points
;
687 * set watch address base pointer to point on the array base
690 aw_info
.watch_address
= (uint64_t *) &args_buff
[args_idx
];
692 /* skip over the addresses buffer */
693 args_idx
+= sizeof(aw_info
.watch_address
) * aw_info
.num_watch_points
;
695 if (args_idx
>= args
->buf_size_in_bytes
- sizeof(*args
)) {
700 watch_mask_value
= (uint64_t) args_buff
[args_idx
];
702 if (watch_mask_value
> 0) {
704 * There is an array of masks.
705 * set watch mask base pointer to point on the array base
708 aw_info
.watch_mask
= (uint64_t *) &args_buff
[args_idx
];
710 /* skip over the masks buffer */
711 args_idx
+= sizeof(aw_info
.watch_mask
) *
712 aw_info
.num_watch_points
;
714 /* just the NULL mask, set to NULL and skip over it */
715 aw_info
.watch_mask
= NULL
;
716 args_idx
+= sizeof(aw_info
.watch_mask
);
719 if (args_idx
>= args
->buf_size_in_bytes
- sizeof(args
)) {
724 /* Currently HSA Event is not supported for DBG */
725 aw_info
.watch_event
= NULL
;
727 mutex_lock(kfd_get_dbgmgr_mutex());
729 status
= kfd_dbgmgr_address_watch(dev
->dbgmgr
, &aw_info
);
731 mutex_unlock(kfd_get_dbgmgr_mutex());
739 /* Parse and generate fixed size data structure for wave control */
740 static int kfd_ioctl_dbg_wave_control(struct file
*filep
,
741 struct kfd_process
*p
, void *data
)
743 struct kfd_ioctl_dbg_wave_control_args
*args
= data
;
745 struct dbg_wave_control_info wac_info
;
746 unsigned char *args_buff
;
747 uint32_t computed_buff_size
;
749 void __user
*cmd_from_user
;
750 unsigned int args_idx
= 0;
752 memset((void *) &wac_info
, 0, sizeof(struct dbg_wave_control_info
));
754 /* we use compact form, independent of the packing attribute value */
755 computed_buff_size
= sizeof(*args
) +
756 sizeof(wac_info
.mode
) +
757 sizeof(wac_info
.operand
) +
758 sizeof(wac_info
.dbgWave_msg
.DbgWaveMsg
) +
759 sizeof(wac_info
.dbgWave_msg
.MemoryVA
) +
760 sizeof(wac_info
.trapId
);
762 dev
= kfd_device_by_id(args
->gpu_id
);
766 if (dev
->device_info
->asic_family
== CHIP_CARRIZO
) {
767 pr_debug("kfd_ioctl_dbg_wave_control not supported on CZ\n");
771 /* input size must match the computed "compact" size */
772 if (args
->buf_size_in_bytes
!= computed_buff_size
) {
773 pr_debug("size mismatch, computed : actual %u : %u\n",
774 args
->buf_size_in_bytes
, computed_buff_size
);
778 cmd_from_user
= (void __user
*) args
->content_ptr
;
780 if (cmd_from_user
== NULL
)
783 /* copy the entire buffer from user */
785 args_buff
= memdup_user(cmd_from_user
,
786 args
->buf_size_in_bytes
- sizeof(*args
));
787 if (IS_ERR(args_buff
))
788 return PTR_ERR(args_buff
);
790 /* move ptr to the start of the "pay-load" area */
791 wac_info
.process
= p
;
793 wac_info
.operand
= *((enum HSA_DBG_WAVEOP
*)(&args_buff
[args_idx
]));
794 args_idx
+= sizeof(wac_info
.operand
);
796 wac_info
.mode
= *((enum HSA_DBG_WAVEMODE
*)(&args_buff
[args_idx
]));
797 args_idx
+= sizeof(wac_info
.mode
);
799 wac_info
.trapId
= *((uint32_t *)(&args_buff
[args_idx
]));
800 args_idx
+= sizeof(wac_info
.trapId
);
802 wac_info
.dbgWave_msg
.DbgWaveMsg
.WaveMsgInfoGen2
.Value
=
803 *((uint32_t *)(&args_buff
[args_idx
]));
804 wac_info
.dbgWave_msg
.MemoryVA
= NULL
;
806 mutex_lock(kfd_get_dbgmgr_mutex());
808 pr_debug("Calling dbg manager process %p, operand %u, mode %u, trapId %u, message %u\n",
809 wac_info
.process
, wac_info
.operand
,
810 wac_info
.mode
, wac_info
.trapId
,
811 wac_info
.dbgWave_msg
.DbgWaveMsg
.WaveMsgInfoGen2
.Value
);
813 status
= kfd_dbgmgr_wave_control(dev
->dbgmgr
, &wac_info
);
815 pr_debug("Returned status of dbg manager is %ld\n", status
);
817 mutex_unlock(kfd_get_dbgmgr_mutex());
824 static int kfd_ioctl_get_clock_counters(struct file
*filep
,
825 struct kfd_process
*p
, void *data
)
827 struct kfd_ioctl_get_clock_counters_args
*args
= data
;
830 dev
= kfd_device_by_id(args
->gpu_id
);
832 /* Reading GPU clock counter from KGD */
833 args
->gpu_clock_counter
= amdgpu_amdkfd_get_gpu_clock_counter(dev
->kgd
);
835 /* Node without GPU resource */
836 args
->gpu_clock_counter
= 0;
838 /* No access to rdtsc. Using raw monotonic time */
839 args
->cpu_clock_counter
= ktime_get_raw_ns();
840 args
->system_clock_counter
= ktime_get_boot_ns();
842 /* Since the counter is in nano-seconds we use 1GHz frequency */
843 args
->system_clock_freq
= 1000000000;
849 static int kfd_ioctl_get_process_apertures(struct file
*filp
,
850 struct kfd_process
*p
, void *data
)
852 struct kfd_ioctl_get_process_apertures_args
*args
= data
;
853 struct kfd_process_device_apertures
*pAperture
;
854 struct kfd_process_device
*pdd
;
856 dev_dbg(kfd_device
, "get apertures for PASID %d", p
->pasid
);
858 args
->num_of_nodes
= 0;
860 mutex_lock(&p
->mutex
);
862 /*if the process-device list isn't empty*/
863 if (kfd_has_process_device_data(p
)) {
864 /* Run over all pdd of the process */
865 pdd
= kfd_get_first_process_device_data(p
);
868 &args
->process_apertures
[args
->num_of_nodes
];
869 pAperture
->gpu_id
= pdd
->dev
->id
;
870 pAperture
->lds_base
= pdd
->lds_base
;
871 pAperture
->lds_limit
= pdd
->lds_limit
;
872 pAperture
->gpuvm_base
= pdd
->gpuvm_base
;
873 pAperture
->gpuvm_limit
= pdd
->gpuvm_limit
;
874 pAperture
->scratch_base
= pdd
->scratch_base
;
875 pAperture
->scratch_limit
= pdd
->scratch_limit
;
878 "node id %u\n", args
->num_of_nodes
);
880 "gpu id %u\n", pdd
->dev
->id
);
882 "lds_base %llX\n", pdd
->lds_base
);
884 "lds_limit %llX\n", pdd
->lds_limit
);
886 "gpuvm_base %llX\n", pdd
->gpuvm_base
);
888 "gpuvm_limit %llX\n", pdd
->gpuvm_limit
);
890 "scratch_base %llX\n", pdd
->scratch_base
);
892 "scratch_limit %llX\n", pdd
->scratch_limit
);
894 args
->num_of_nodes
++;
896 pdd
= kfd_get_next_process_device_data(p
, pdd
);
897 } while (pdd
&& (args
->num_of_nodes
< NUM_OF_SUPPORTED_GPUS
));
900 mutex_unlock(&p
->mutex
);
905 static int kfd_ioctl_get_process_apertures_new(struct file
*filp
,
906 struct kfd_process
*p
, void *data
)
908 struct kfd_ioctl_get_process_apertures_new_args
*args
= data
;
909 struct kfd_process_device_apertures
*pa
;
910 struct kfd_process_device
*pdd
;
914 dev_dbg(kfd_device
, "get apertures for PASID %d", p
->pasid
);
916 if (args
->num_of_nodes
== 0) {
917 /* Return number of nodes, so that user space can alloacate
920 mutex_lock(&p
->mutex
);
922 if (!kfd_has_process_device_data(p
))
925 /* Run over all pdd of the process */
926 pdd
= kfd_get_first_process_device_data(p
);
928 args
->num_of_nodes
++;
929 pdd
= kfd_get_next_process_device_data(p
, pdd
);
935 /* Fill in process-aperture information for all available
936 * nodes, but not more than args->num_of_nodes as that is
937 * the amount of memory allocated by user
939 pa
= kzalloc((sizeof(struct kfd_process_device_apertures
) *
940 args
->num_of_nodes
), GFP_KERNEL
);
944 mutex_lock(&p
->mutex
);
946 if (!kfd_has_process_device_data(p
)) {
947 args
->num_of_nodes
= 0;
952 /* Run over all pdd of the process */
953 pdd
= kfd_get_first_process_device_data(p
);
955 pa
[nodes
].gpu_id
= pdd
->dev
->id
;
956 pa
[nodes
].lds_base
= pdd
->lds_base
;
957 pa
[nodes
].lds_limit
= pdd
->lds_limit
;
958 pa
[nodes
].gpuvm_base
= pdd
->gpuvm_base
;
959 pa
[nodes
].gpuvm_limit
= pdd
->gpuvm_limit
;
960 pa
[nodes
].scratch_base
= pdd
->scratch_base
;
961 pa
[nodes
].scratch_limit
= pdd
->scratch_limit
;
964 "gpu id %u\n", pdd
->dev
->id
);
966 "lds_base %llX\n", pdd
->lds_base
);
968 "lds_limit %llX\n", pdd
->lds_limit
);
970 "gpuvm_base %llX\n", pdd
->gpuvm_base
);
972 "gpuvm_limit %llX\n", pdd
->gpuvm_limit
);
974 "scratch_base %llX\n", pdd
->scratch_base
);
976 "scratch_limit %llX\n", pdd
->scratch_limit
);
979 pdd
= kfd_get_next_process_device_data(p
, pdd
);
980 } while (pdd
&& (nodes
< args
->num_of_nodes
));
981 mutex_unlock(&p
->mutex
);
983 args
->num_of_nodes
= nodes
;
985 (void __user
*)args
->kfd_process_device_apertures_ptr
,
987 (nodes
* sizeof(struct kfd_process_device_apertures
)));
989 return ret
? -EFAULT
: 0;
992 mutex_unlock(&p
->mutex
);
996 static int kfd_ioctl_create_event(struct file
*filp
, struct kfd_process
*p
,
999 struct kfd_ioctl_create_event_args
*args
= data
;
1002 /* For dGPUs the event page is allocated in user mode. The
1003 * handle is passed to KFD with the first call to this IOCTL
1004 * through the event_page_offset field.
1006 if (args
->event_page_offset
) {
1007 struct kfd_dev
*kfd
;
1008 struct kfd_process_device
*pdd
;
1009 void *mem
, *kern_addr
;
1012 if (p
->signal_page
) {
1013 pr_err("Event page is already set\n");
1017 kfd
= kfd_device_by_id(GET_GPU_ID(args
->event_page_offset
));
1019 pr_err("Getting device by id failed in %s\n", __func__
);
1023 mutex_lock(&p
->mutex
);
1024 pdd
= kfd_bind_process_to_device(kfd
, p
);
1030 mem
= kfd_process_device_translate_handle(pdd
,
1031 GET_IDR_HANDLE(args
->event_page_offset
));
1033 pr_err("Can't find BO, offset is 0x%llx\n",
1034 args
->event_page_offset
);
1038 mutex_unlock(&p
->mutex
);
1040 err
= amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(kfd
->kgd
,
1041 mem
, &kern_addr
, &size
);
1043 pr_err("Failed to map event page to kernel\n");
1047 err
= kfd_event_page_set(p
, kern_addr
, size
);
1049 pr_err("Failed to set event page\n");
1054 err
= kfd_event_create(filp
, p
, args
->event_type
,
1055 args
->auto_reset
!= 0, args
->node_id
,
1056 &args
->event_id
, &args
->event_trigger_data
,
1057 &args
->event_page_offset
,
1058 &args
->event_slot_index
);
1063 mutex_unlock(&p
->mutex
);
1067 static int kfd_ioctl_destroy_event(struct file
*filp
, struct kfd_process
*p
,
1070 struct kfd_ioctl_destroy_event_args
*args
= data
;
1072 return kfd_event_destroy(p
, args
->event_id
);
1075 static int kfd_ioctl_set_event(struct file
*filp
, struct kfd_process
*p
,
1078 struct kfd_ioctl_set_event_args
*args
= data
;
1080 return kfd_set_event(p
, args
->event_id
);
1083 static int kfd_ioctl_reset_event(struct file
*filp
, struct kfd_process
*p
,
1086 struct kfd_ioctl_reset_event_args
*args
= data
;
1088 return kfd_reset_event(p
, args
->event_id
);
1091 static int kfd_ioctl_wait_events(struct file
*filp
, struct kfd_process
*p
,
1094 struct kfd_ioctl_wait_events_args
*args
= data
;
1097 err
= kfd_wait_on_events(p
, args
->num_events
,
1098 (void __user
*)args
->events_ptr
,
1099 (args
->wait_for_all
!= 0),
1100 args
->timeout
, &args
->wait_result
);
1104 static int kfd_ioctl_set_scratch_backing_va(struct file
*filep
,
1105 struct kfd_process
*p
, void *data
)
1107 struct kfd_ioctl_set_scratch_backing_va_args
*args
= data
;
1108 struct kfd_process_device
*pdd
;
1109 struct kfd_dev
*dev
;
1112 dev
= kfd_device_by_id(args
->gpu_id
);
1116 mutex_lock(&p
->mutex
);
1118 pdd
= kfd_bind_process_to_device(dev
, p
);
1121 goto bind_process_to_device_fail
;
1124 pdd
->qpd
.sh_hidden_private_base
= args
->va_addr
;
1126 mutex_unlock(&p
->mutex
);
1128 if (dev
->dqm
->sched_policy
== KFD_SCHED_POLICY_NO_HWS
&&
1130 dev
->kfd2kgd
->set_scratch_backing_va(
1131 dev
->kgd
, args
->va_addr
, pdd
->qpd
.vmid
);
1135 bind_process_to_device_fail
:
1136 mutex_unlock(&p
->mutex
);
1140 static int kfd_ioctl_get_tile_config(struct file
*filep
,
1141 struct kfd_process
*p
, void *data
)
1143 struct kfd_ioctl_get_tile_config_args
*args
= data
;
1144 struct kfd_dev
*dev
;
1145 struct tile_config config
;
1148 dev
= kfd_device_by_id(args
->gpu_id
);
1152 dev
->kfd2kgd
->get_tile_config(dev
->kgd
, &config
);
1154 args
->gb_addr_config
= config
.gb_addr_config
;
1155 args
->num_banks
= config
.num_banks
;
1156 args
->num_ranks
= config
.num_ranks
;
1158 if (args
->num_tile_configs
> config
.num_tile_configs
)
1159 args
->num_tile_configs
= config
.num_tile_configs
;
1160 err
= copy_to_user((void __user
*)args
->tile_config_ptr
,
1161 config
.tile_config_ptr
,
1162 args
->num_tile_configs
* sizeof(uint32_t));
1164 args
->num_tile_configs
= 0;
1168 if (args
->num_macro_tile_configs
> config
.num_macro_tile_configs
)
1169 args
->num_macro_tile_configs
=
1170 config
.num_macro_tile_configs
;
1171 err
= copy_to_user((void __user
*)args
->macro_tile_config_ptr
,
1172 config
.macro_tile_config_ptr
,
1173 args
->num_macro_tile_configs
* sizeof(uint32_t));
1175 args
->num_macro_tile_configs
= 0;
1182 static int kfd_ioctl_acquire_vm(struct file
*filep
, struct kfd_process
*p
,
1185 struct kfd_ioctl_acquire_vm_args
*args
= data
;
1186 struct kfd_process_device
*pdd
;
1187 struct kfd_dev
*dev
;
1188 struct file
*drm_file
;
1191 dev
= kfd_device_by_id(args
->gpu_id
);
1195 drm_file
= fget(args
->drm_fd
);
1199 mutex_lock(&p
->mutex
);
1201 pdd
= kfd_get_process_device_data(dev
, p
);
1207 if (pdd
->drm_file
) {
1208 ret
= pdd
->drm_file
== drm_file
? 0 : -EBUSY
;
1212 ret
= kfd_process_device_init_vm(pdd
, drm_file
);
1215 /* On success, the PDD keeps the drm_file reference */
1216 mutex_unlock(&p
->mutex
);
1221 mutex_unlock(&p
->mutex
);
1226 bool kfd_dev_is_large_bar(struct kfd_dev
*dev
)
1228 struct kfd_local_mem_info mem_info
;
1230 if (debug_largebar
) {
1231 pr_debug("Simulate large-bar allocation on non large-bar machine\n");
1235 if (dev
->device_info
->needs_iommu_device
)
1238 amdgpu_amdkfd_get_local_mem_info(dev
->kgd
, &mem_info
);
1239 if (mem_info
.local_mem_size_private
== 0 &&
1240 mem_info
.local_mem_size_public
> 0)
1245 static int kfd_ioctl_alloc_memory_of_gpu(struct file
*filep
,
1246 struct kfd_process
*p
, void *data
)
1248 struct kfd_ioctl_alloc_memory_of_gpu_args
*args
= data
;
1249 struct kfd_process_device
*pdd
;
1251 struct kfd_dev
*dev
;
1254 uint64_t offset
= args
->mmap_offset
;
1255 uint32_t flags
= args
->flags
;
1257 if (args
->size
== 0)
1260 dev
= kfd_device_by_id(args
->gpu_id
);
1264 if ((flags
& KFD_IOC_ALLOC_MEM_FLAGS_PUBLIC
) &&
1265 (flags
& KFD_IOC_ALLOC_MEM_FLAGS_VRAM
) &&
1266 !kfd_dev_is_large_bar(dev
)) {
1267 pr_err("Alloc host visible vram on small bar is not allowed\n");
1271 if (flags
& KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL
) {
1272 if (args
->size
!= kfd_doorbell_process_slice(dev
))
1274 offset
= kfd_get_process_doorbells(dev
, p
);
1277 mutex_lock(&p
->mutex
);
1279 pdd
= kfd_bind_process_to_device(dev
, p
);
1285 err
= amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
1286 dev
->kgd
, args
->va_addr
, args
->size
,
1287 pdd
->vm
, (struct kgd_mem
**) &mem
, &offset
,
1293 idr_handle
= kfd_process_device_create_obj_handle(pdd
, mem
);
1294 if (idr_handle
< 0) {
1299 mutex_unlock(&p
->mutex
);
1301 args
->handle
= MAKE_HANDLE(args
->gpu_id
, idr_handle
);
1302 args
->mmap_offset
= offset
;
1307 amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev
->kgd
, (struct kgd_mem
*)mem
);
1309 mutex_unlock(&p
->mutex
);
1313 static int kfd_ioctl_free_memory_of_gpu(struct file
*filep
,
1314 struct kfd_process
*p
, void *data
)
1316 struct kfd_ioctl_free_memory_of_gpu_args
*args
= data
;
1317 struct kfd_process_device
*pdd
;
1319 struct kfd_dev
*dev
;
1322 dev
= kfd_device_by_id(GET_GPU_ID(args
->handle
));
1326 mutex_lock(&p
->mutex
);
1328 pdd
= kfd_get_process_device_data(dev
, p
);
1330 pr_err("Process device data doesn't exist\n");
1335 mem
= kfd_process_device_translate_handle(
1336 pdd
, GET_IDR_HANDLE(args
->handle
));
1342 ret
= amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev
->kgd
,
1343 (struct kgd_mem
*)mem
);
1345 /* If freeing the buffer failed, leave the handle in place for
1346 * clean-up during process tear-down.
1349 kfd_process_device_remove_obj_handle(
1350 pdd
, GET_IDR_HANDLE(args
->handle
));
1353 mutex_unlock(&p
->mutex
);
1357 static int kfd_ioctl_map_memory_to_gpu(struct file
*filep
,
1358 struct kfd_process
*p
, void *data
)
1360 struct kfd_ioctl_map_memory_to_gpu_args
*args
= data
;
1361 struct kfd_process_device
*pdd
, *peer_pdd
;
1363 struct kfd_dev
*dev
, *peer
;
1366 uint32_t *devices_arr
= NULL
;
1368 dev
= kfd_device_by_id(GET_GPU_ID(args
->handle
));
1372 if (!args
->n_devices
) {
1373 pr_debug("Device IDs array empty\n");
1376 if (args
->n_success
> args
->n_devices
) {
1377 pr_debug("n_success exceeds n_devices\n");
1381 devices_arr
= kmalloc_array(args
->n_devices
, sizeof(*devices_arr
),
1386 err
= copy_from_user(devices_arr
,
1387 (void __user
*)args
->device_ids_array_ptr
,
1388 args
->n_devices
* sizeof(*devices_arr
));
1391 goto copy_from_user_failed
;
1394 mutex_lock(&p
->mutex
);
1396 pdd
= kfd_bind_process_to_device(dev
, p
);
1399 goto bind_process_to_device_failed
;
1402 mem
= kfd_process_device_translate_handle(pdd
,
1403 GET_IDR_HANDLE(args
->handle
));
1406 goto get_mem_obj_from_handle_failed
;
1409 for (i
= args
->n_success
; i
< args
->n_devices
; i
++) {
1410 peer
= kfd_device_by_id(devices_arr
[i
]);
1412 pr_debug("Getting device by id failed for 0x%x\n",
1415 goto get_mem_obj_from_handle_failed
;
1418 peer_pdd
= kfd_bind_process_to_device(peer
, p
);
1419 if (IS_ERR(peer_pdd
)) {
1420 err
= PTR_ERR(peer_pdd
);
1421 goto get_mem_obj_from_handle_failed
;
1423 err
= amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
1424 peer
->kgd
, (struct kgd_mem
*)mem
, peer_pdd
->vm
);
1426 pr_err("Failed to map to gpu %d/%d\n",
1427 i
, args
->n_devices
);
1428 goto map_memory_to_gpu_failed
;
1430 args
->n_success
= i
+1;
1433 mutex_unlock(&p
->mutex
);
1435 err
= amdgpu_amdkfd_gpuvm_sync_memory(dev
->kgd
, (struct kgd_mem
*) mem
, true);
1437 pr_debug("Sync memory failed, wait interrupted by user signal\n");
1438 goto sync_memory_failed
;
1441 /* Flush TLBs after waiting for the page table updates to complete */
1442 for (i
= 0; i
< args
->n_devices
; i
++) {
1443 peer
= kfd_device_by_id(devices_arr
[i
]);
1444 if (WARN_ON_ONCE(!peer
))
1446 peer_pdd
= kfd_get_process_device_data(peer
, p
);
1447 if (WARN_ON_ONCE(!peer_pdd
))
1449 kfd_flush_tlb(peer_pdd
);
1456 bind_process_to_device_failed
:
1457 get_mem_obj_from_handle_failed
:
1458 map_memory_to_gpu_failed
:
1459 mutex_unlock(&p
->mutex
);
1460 copy_from_user_failed
:
1467 static int kfd_ioctl_unmap_memory_from_gpu(struct file
*filep
,
1468 struct kfd_process
*p
, void *data
)
1470 struct kfd_ioctl_unmap_memory_from_gpu_args
*args
= data
;
1471 struct kfd_process_device
*pdd
, *peer_pdd
;
1473 struct kfd_dev
*dev
, *peer
;
1475 uint32_t *devices_arr
= NULL
, i
;
1477 dev
= kfd_device_by_id(GET_GPU_ID(args
->handle
));
1481 if (!args
->n_devices
) {
1482 pr_debug("Device IDs array empty\n");
1485 if (args
->n_success
> args
->n_devices
) {
1486 pr_debug("n_success exceeds n_devices\n");
1490 devices_arr
= kmalloc_array(args
->n_devices
, sizeof(*devices_arr
),
1495 err
= copy_from_user(devices_arr
,
1496 (void __user
*)args
->device_ids_array_ptr
,
1497 args
->n_devices
* sizeof(*devices_arr
));
1500 goto copy_from_user_failed
;
1503 mutex_lock(&p
->mutex
);
1505 pdd
= kfd_get_process_device_data(dev
, p
);
1508 goto bind_process_to_device_failed
;
1511 mem
= kfd_process_device_translate_handle(pdd
,
1512 GET_IDR_HANDLE(args
->handle
));
1515 goto get_mem_obj_from_handle_failed
;
1518 for (i
= args
->n_success
; i
< args
->n_devices
; i
++) {
1519 peer
= kfd_device_by_id(devices_arr
[i
]);
1522 goto get_mem_obj_from_handle_failed
;
1525 peer_pdd
= kfd_get_process_device_data(peer
, p
);
1528 goto get_mem_obj_from_handle_failed
;
1530 err
= amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
1531 peer
->kgd
, (struct kgd_mem
*)mem
, peer_pdd
->vm
);
1533 pr_err("Failed to unmap from gpu %d/%d\n",
1534 i
, args
->n_devices
);
1535 goto unmap_memory_from_gpu_failed
;
1537 args
->n_success
= i
+1;
1541 mutex_unlock(&p
->mutex
);
1545 bind_process_to_device_failed
:
1546 get_mem_obj_from_handle_failed
:
1547 unmap_memory_from_gpu_failed
:
1548 mutex_unlock(&p
->mutex
);
1549 copy_from_user_failed
:
1554 static int kfd_ioctl_get_dmabuf_info(struct file
*filep
,
1555 struct kfd_process
*p
, void *data
)
1557 struct kfd_ioctl_get_dmabuf_info_args
*args
= data
;
1558 struct kfd_dev
*dev
= NULL
;
1559 struct kgd_dev
*dma_buf_kgd
;
1560 void *metadata_buffer
= NULL
;
1565 /* Find a KFD GPU device that supports the get_dmabuf_info query */
1566 for (i
= 0; kfd_topology_enum_kfd_devices(i
, &dev
) == 0; i
++)
1572 if (args
->metadata_ptr
) {
1573 metadata_buffer
= kzalloc(args
->metadata_size
, GFP_KERNEL
);
1574 if (!metadata_buffer
)
1578 /* Get dmabuf info from KGD */
1579 r
= amdgpu_amdkfd_get_dmabuf_info(dev
->kgd
, args
->dmabuf_fd
,
1580 &dma_buf_kgd
, &args
->size
,
1581 metadata_buffer
, args
->metadata_size
,
1582 &args
->metadata_size
, &flags
);
1586 /* Reverse-lookup gpu_id from kgd pointer */
1587 dev
= kfd_device_by_kgd(dma_buf_kgd
);
1592 args
->gpu_id
= dev
->id
;
1593 args
->flags
= flags
;
1595 /* Copy metadata buffer to user mode */
1596 if (metadata_buffer
) {
1597 r
= copy_to_user((void __user
*)args
->metadata_ptr
,
1598 metadata_buffer
, args
->metadata_size
);
1604 kfree(metadata_buffer
);
1609 static int kfd_ioctl_import_dmabuf(struct file
*filep
,
1610 struct kfd_process
*p
, void *data
)
1612 struct kfd_ioctl_import_dmabuf_args
*args
= data
;
1613 struct kfd_process_device
*pdd
;
1614 struct dma_buf
*dmabuf
;
1615 struct kfd_dev
*dev
;
1621 dev
= kfd_device_by_id(args
->gpu_id
);
1625 dmabuf
= dma_buf_get(args
->dmabuf_fd
);
1627 return PTR_ERR(dmabuf
);
1629 mutex_lock(&p
->mutex
);
1631 pdd
= kfd_bind_process_to_device(dev
, p
);
1637 r
= amdgpu_amdkfd_gpuvm_import_dmabuf(dev
->kgd
, dmabuf
,
1638 args
->va_addr
, pdd
->vm
,
1639 (struct kgd_mem
**)&mem
, &size
,
1644 idr_handle
= kfd_process_device_create_obj_handle(pdd
, mem
);
1645 if (idr_handle
< 0) {
1650 mutex_unlock(&p
->mutex
);
1652 args
->handle
= MAKE_HANDLE(args
->gpu_id
, idr_handle
);
1657 amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev
->kgd
, (struct kgd_mem
*)mem
);
1659 mutex_unlock(&p
->mutex
);
1663 #define AMDKFD_IOCTL_DEF(ioctl, _func, _flags) \
1664 [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func, .flags = _flags, \
1665 .cmd_drv = 0, .name = #ioctl}
1668 static const struct amdkfd_ioctl_desc amdkfd_ioctls
[] = {
1669 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_VERSION
,
1670 kfd_ioctl_get_version
, 0),
1672 AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_QUEUE
,
1673 kfd_ioctl_create_queue
, 0),
1675 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_QUEUE
,
1676 kfd_ioctl_destroy_queue
, 0),
1678 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_MEMORY_POLICY
,
1679 kfd_ioctl_set_memory_policy
, 0),
1681 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_CLOCK_COUNTERS
,
1682 kfd_ioctl_get_clock_counters
, 0),
1684 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_PROCESS_APERTURES
,
1685 kfd_ioctl_get_process_apertures
, 0),
1687 AMDKFD_IOCTL_DEF(AMDKFD_IOC_UPDATE_QUEUE
,
1688 kfd_ioctl_update_queue
, 0),
1690 AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_EVENT
,
1691 kfd_ioctl_create_event
, 0),
1693 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_EVENT
,
1694 kfd_ioctl_destroy_event
, 0),
1696 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_EVENT
,
1697 kfd_ioctl_set_event
, 0),
1699 AMDKFD_IOCTL_DEF(AMDKFD_IOC_RESET_EVENT
,
1700 kfd_ioctl_reset_event
, 0),
1702 AMDKFD_IOCTL_DEF(AMDKFD_IOC_WAIT_EVENTS
,
1703 kfd_ioctl_wait_events
, 0),
1705 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_REGISTER
,
1706 kfd_ioctl_dbg_register
, 0),
1708 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_UNREGISTER
,
1709 kfd_ioctl_dbg_unregister
, 0),
1711 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_ADDRESS_WATCH
,
1712 kfd_ioctl_dbg_address_watch
, 0),
1714 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_WAVE_CONTROL
,
1715 kfd_ioctl_dbg_wave_control
, 0),
1717 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_SCRATCH_BACKING_VA
,
1718 kfd_ioctl_set_scratch_backing_va
, 0),
1720 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_TILE_CONFIG
,
1721 kfd_ioctl_get_tile_config
, 0),
1723 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_TRAP_HANDLER
,
1724 kfd_ioctl_set_trap_handler
, 0),
1726 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_PROCESS_APERTURES_NEW
,
1727 kfd_ioctl_get_process_apertures_new
, 0),
1729 AMDKFD_IOCTL_DEF(AMDKFD_IOC_ACQUIRE_VM
,
1730 kfd_ioctl_acquire_vm
, 0),
1732 AMDKFD_IOCTL_DEF(AMDKFD_IOC_ALLOC_MEMORY_OF_GPU
,
1733 kfd_ioctl_alloc_memory_of_gpu
, 0),
1735 AMDKFD_IOCTL_DEF(AMDKFD_IOC_FREE_MEMORY_OF_GPU
,
1736 kfd_ioctl_free_memory_of_gpu
, 0),
1738 AMDKFD_IOCTL_DEF(AMDKFD_IOC_MAP_MEMORY_TO_GPU
,
1739 kfd_ioctl_map_memory_to_gpu
, 0),
1741 AMDKFD_IOCTL_DEF(AMDKFD_IOC_UNMAP_MEMORY_FROM_GPU
,
1742 kfd_ioctl_unmap_memory_from_gpu
, 0),
1744 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_CU_MASK
,
1745 kfd_ioctl_set_cu_mask
, 0),
1747 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_QUEUE_WAVE_STATE
,
1748 kfd_ioctl_get_queue_wave_state
, 0),
1750 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_DMABUF_INFO
,
1751 kfd_ioctl_get_dmabuf_info
, 0),
1753 AMDKFD_IOCTL_DEF(AMDKFD_IOC_IMPORT_DMABUF
,
1754 kfd_ioctl_import_dmabuf
, 0),
1758 #define AMDKFD_CORE_IOCTL_COUNT ARRAY_SIZE(amdkfd_ioctls)
1760 static long kfd_ioctl(struct file
*filep
, unsigned int cmd
, unsigned long arg
)
1762 struct kfd_process
*process
;
1763 amdkfd_ioctl_t
*func
;
1764 const struct amdkfd_ioctl_desc
*ioctl
= NULL
;
1765 unsigned int nr
= _IOC_NR(cmd
);
1766 char stack_kdata
[128];
1768 unsigned int usize
, asize
;
1769 int retcode
= -EINVAL
;
1771 if (nr
>= AMDKFD_CORE_IOCTL_COUNT
)
1774 if ((nr
>= AMDKFD_COMMAND_START
) && (nr
< AMDKFD_COMMAND_END
)) {
1777 ioctl
= &amdkfd_ioctls
[nr
];
1779 amdkfd_size
= _IOC_SIZE(ioctl
->cmd
);
1780 usize
= asize
= _IOC_SIZE(cmd
);
1781 if (amdkfd_size
> asize
)
1782 asize
= amdkfd_size
;
1788 dev_dbg(kfd_device
, "ioctl cmd 0x%x (#%d), arg 0x%lx\n", cmd
, nr
, arg
);
1790 process
= kfd_get_process(current
);
1791 if (IS_ERR(process
)) {
1792 dev_dbg(kfd_device
, "no process\n");
1796 /* Do not trust userspace, use our own definition */
1799 if (unlikely(!func
)) {
1800 dev_dbg(kfd_device
, "no function\n");
1805 if (cmd
& (IOC_IN
| IOC_OUT
)) {
1806 if (asize
<= sizeof(stack_kdata
)) {
1807 kdata
= stack_kdata
;
1809 kdata
= kmalloc(asize
, GFP_KERNEL
);
1816 memset(kdata
+ usize
, 0, asize
- usize
);
1820 if (copy_from_user(kdata
, (void __user
*)arg
, usize
) != 0) {
1824 } else if (cmd
& IOC_OUT
) {
1825 memset(kdata
, 0, usize
);
1828 retcode
= func(filep
, process
, kdata
);
1831 if (copy_to_user((void __user
*)arg
, kdata
, usize
) != 0)
1836 dev_dbg(kfd_device
, "invalid ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n",
1837 task_pid_nr(current
), cmd
, nr
);
1839 if (kdata
!= stack_kdata
)
1843 dev_dbg(kfd_device
, "ret = %d\n", retcode
);
1848 static int kfd_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1850 struct kfd_process
*process
;
1851 struct kfd_dev
*dev
= NULL
;
1852 unsigned long vm_pgoff
;
1853 unsigned int gpu_id
;
1855 process
= kfd_get_process(current
);
1856 if (IS_ERR(process
))
1857 return PTR_ERR(process
);
1859 vm_pgoff
= vma
->vm_pgoff
;
1860 vma
->vm_pgoff
= KFD_MMAP_OFFSET_VALUE_GET(vm_pgoff
);
1861 gpu_id
= KFD_MMAP_GPU_ID_GET(vm_pgoff
);
1863 dev
= kfd_device_by_id(gpu_id
);
1865 switch (vm_pgoff
& KFD_MMAP_TYPE_MASK
) {
1866 case KFD_MMAP_TYPE_DOORBELL
:
1869 return kfd_doorbell_mmap(dev
, process
, vma
);
1871 case KFD_MMAP_TYPE_EVENTS
:
1872 return kfd_event_mmap(process
, vma
);
1874 case KFD_MMAP_TYPE_RESERVED_MEM
:
1877 return kfd_reserved_mem_mmap(dev
, process
, vma
);