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_release(struct inode
*, struct file
*);
46 static int kfd_mmap(struct file
*, struct vm_area_struct
*);
48 static const char kfd_dev_name
[] = "kfd";
50 static const struct file_operations kfd_fops
= {
52 .unlocked_ioctl
= kfd_ioctl
,
53 .compat_ioctl
= compat_ptr_ioctl
,
55 .release
= kfd_release
,
59 static int kfd_char_dev_major
= -1;
60 static struct class *kfd_class
;
61 struct device
*kfd_device
;
63 int kfd_chardev_init(void)
67 kfd_char_dev_major
= register_chrdev(0, kfd_dev_name
, &kfd_fops
);
68 err
= kfd_char_dev_major
;
70 goto err_register_chrdev
;
72 kfd_class
= class_create(THIS_MODULE
, kfd_dev_name
);
73 err
= PTR_ERR(kfd_class
);
74 if (IS_ERR(kfd_class
))
75 goto err_class_create
;
77 kfd_device
= device_create(kfd_class
, NULL
,
78 MKDEV(kfd_char_dev_major
, 0),
80 err
= PTR_ERR(kfd_device
);
81 if (IS_ERR(kfd_device
))
82 goto err_device_create
;
87 class_destroy(kfd_class
);
89 unregister_chrdev(kfd_char_dev_major
, kfd_dev_name
);
94 void kfd_chardev_exit(void)
96 device_destroy(kfd_class
, MKDEV(kfd_char_dev_major
, 0));
97 class_destroy(kfd_class
);
98 unregister_chrdev(kfd_char_dev_major
, kfd_dev_name
);
101 struct device
*kfd_chardev(void)
107 static int kfd_open(struct inode
*inode
, struct file
*filep
)
109 struct kfd_process
*process
;
110 bool is_32bit_user_mode
;
112 if (iminor(inode
) != 0)
115 is_32bit_user_mode
= in_compat_syscall();
117 if (is_32bit_user_mode
) {
119 "Process %d (32-bit) failed to open /dev/kfd\n"
120 "32-bit processes are not supported by amdkfd\n",
125 process
= kfd_create_process(filep
);
127 return PTR_ERR(process
);
129 if (kfd_is_locked()) {
130 kfd_unref_process(process
);
134 /* filep now owns the reference returned by kfd_create_process */
135 filep
->private_data
= process
;
137 dev_dbg(kfd_device
, "process %d opened, compat mode (32 bit) - %d\n",
138 process
->pasid
, process
->is_32bit_user_mode
);
143 static int kfd_release(struct inode
*inode
, struct file
*filep
)
145 struct kfd_process
*process
= filep
->private_data
;
148 kfd_unref_process(process
);
153 static int kfd_ioctl_get_version(struct file
*filep
, struct kfd_process
*p
,
156 struct kfd_ioctl_get_version_args
*args
= data
;
158 args
->major_version
= KFD_IOCTL_MAJOR_VERSION
;
159 args
->minor_version
= KFD_IOCTL_MINOR_VERSION
;
164 static int set_queue_properties_from_user(struct queue_properties
*q_properties
,
165 struct kfd_ioctl_create_queue_args
*args
)
167 if (args
->queue_percentage
> KFD_MAX_QUEUE_PERCENTAGE
) {
168 pr_err("Queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
172 if (args
->queue_priority
> KFD_MAX_QUEUE_PRIORITY
) {
173 pr_err("Queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
177 if ((args
->ring_base_address
) &&
178 (!access_ok((const void __user
*) args
->ring_base_address
,
179 sizeof(uint64_t)))) {
180 pr_err("Can't access ring base address\n");
184 if (!is_power_of_2(args
->ring_size
) && (args
->ring_size
!= 0)) {
185 pr_err("Ring size must be a power of 2 or 0\n");
189 if (!access_ok((const void __user
*) args
->read_pointer_address
,
191 pr_err("Can't access read pointer\n");
195 if (!access_ok((const void __user
*) args
->write_pointer_address
,
197 pr_err("Can't access write pointer\n");
201 if (args
->eop_buffer_address
&&
202 !access_ok((const void __user
*) args
->eop_buffer_address
,
204 pr_debug("Can't access eop buffer");
208 if (args
->ctx_save_restore_address
&&
209 !access_ok((const void __user
*) args
->ctx_save_restore_address
,
211 pr_debug("Can't access ctx save restore buffer");
215 q_properties
->is_interop
= false;
216 q_properties
->queue_percent
= args
->queue_percentage
;
217 q_properties
->priority
= args
->queue_priority
;
218 q_properties
->queue_address
= args
->ring_base_address
;
219 q_properties
->queue_size
= args
->ring_size
;
220 q_properties
->read_ptr
= (uint32_t *) args
->read_pointer_address
;
221 q_properties
->write_ptr
= (uint32_t *) args
->write_pointer_address
;
222 q_properties
->eop_ring_buffer_address
= args
->eop_buffer_address
;
223 q_properties
->eop_ring_buffer_size
= args
->eop_buffer_size
;
224 q_properties
->ctx_save_restore_area_address
=
225 args
->ctx_save_restore_address
;
226 q_properties
->ctx_save_restore_area_size
= args
->ctx_save_restore_size
;
227 q_properties
->ctl_stack_size
= args
->ctl_stack_size
;
228 if (args
->queue_type
== KFD_IOC_QUEUE_TYPE_COMPUTE
||
229 args
->queue_type
== KFD_IOC_QUEUE_TYPE_COMPUTE_AQL
)
230 q_properties
->type
= KFD_QUEUE_TYPE_COMPUTE
;
231 else if (args
->queue_type
== KFD_IOC_QUEUE_TYPE_SDMA
)
232 q_properties
->type
= KFD_QUEUE_TYPE_SDMA
;
233 else if (args
->queue_type
== KFD_IOC_QUEUE_TYPE_SDMA_XGMI
)
234 q_properties
->type
= KFD_QUEUE_TYPE_SDMA_XGMI
;
238 if (args
->queue_type
== KFD_IOC_QUEUE_TYPE_COMPUTE_AQL
)
239 q_properties
->format
= KFD_QUEUE_FORMAT_AQL
;
241 q_properties
->format
= KFD_QUEUE_FORMAT_PM4
;
243 pr_debug("Queue Percentage: %d, %d\n",
244 q_properties
->queue_percent
, args
->queue_percentage
);
246 pr_debug("Queue Priority: %d, %d\n",
247 q_properties
->priority
, args
->queue_priority
);
249 pr_debug("Queue Address: 0x%llX, 0x%llX\n",
250 q_properties
->queue_address
, args
->ring_base_address
);
252 pr_debug("Queue Size: 0x%llX, %u\n",
253 q_properties
->queue_size
, args
->ring_size
);
255 pr_debug("Queue r/w Pointers: %px, %px\n",
256 q_properties
->read_ptr
,
257 q_properties
->write_ptr
);
259 pr_debug("Queue Format: %d\n", q_properties
->format
);
261 pr_debug("Queue EOP: 0x%llX\n", q_properties
->eop_ring_buffer_address
);
263 pr_debug("Queue CTX save area: 0x%llX\n",
264 q_properties
->ctx_save_restore_area_address
);
269 static int kfd_ioctl_create_queue(struct file
*filep
, struct kfd_process
*p
,
272 struct kfd_ioctl_create_queue_args
*args
= data
;
275 unsigned int queue_id
;
276 struct kfd_process_device
*pdd
;
277 struct queue_properties q_properties
;
278 uint32_t doorbell_offset_in_process
= 0;
280 memset(&q_properties
, 0, sizeof(struct queue_properties
));
282 pr_debug("Creating queue ioctl\n");
284 err
= set_queue_properties_from_user(&q_properties
, args
);
288 pr_debug("Looking for gpu id 0x%x\n", args
->gpu_id
);
289 dev
= kfd_device_by_id(args
->gpu_id
);
291 pr_debug("Could not find gpu id 0x%x\n", args
->gpu_id
);
295 mutex_lock(&p
->mutex
);
297 pdd
= kfd_bind_process_to_device(dev
, p
);
300 goto err_bind_process
;
303 pr_debug("Creating queue for PASID 0x%x on gpu 0x%x\n",
307 err
= pqm_create_queue(&p
->pqm
, dev
, filep
, &q_properties
, &queue_id
,
308 &doorbell_offset_in_process
);
310 goto err_create_queue
;
312 args
->queue_id
= queue_id
;
315 /* Return gpu_id as doorbell offset for mmap usage */
316 args
->doorbell_offset
= KFD_MMAP_TYPE_DOORBELL
;
317 args
->doorbell_offset
|= KFD_MMAP_GPU_ID(args
->gpu_id
);
318 if (KFD_IS_SOC15(dev
->device_info
->asic_family
))
319 /* On SOC15 ASICs, include the doorbell offset within the
320 * process doorbell frame, which is 2 pages.
322 args
->doorbell_offset
|= doorbell_offset_in_process
;
324 mutex_unlock(&p
->mutex
);
326 pr_debug("Queue id %d was created successfully\n", args
->queue_id
);
328 pr_debug("Ring buffer address == 0x%016llX\n",
329 args
->ring_base_address
);
331 pr_debug("Read ptr address == 0x%016llX\n",
332 args
->read_pointer_address
);
334 pr_debug("Write ptr address == 0x%016llX\n",
335 args
->write_pointer_address
);
341 mutex_unlock(&p
->mutex
);
345 static int kfd_ioctl_destroy_queue(struct file
*filp
, struct kfd_process
*p
,
349 struct kfd_ioctl_destroy_queue_args
*args
= data
;
351 pr_debug("Destroying queue id %d for pasid 0x%x\n",
355 mutex_lock(&p
->mutex
);
357 retval
= pqm_destroy_queue(&p
->pqm
, args
->queue_id
);
359 mutex_unlock(&p
->mutex
);
363 static int kfd_ioctl_update_queue(struct file
*filp
, struct kfd_process
*p
,
367 struct kfd_ioctl_update_queue_args
*args
= data
;
368 struct queue_properties properties
;
370 if (args
->queue_percentage
> KFD_MAX_QUEUE_PERCENTAGE
) {
371 pr_err("Queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
375 if (args
->queue_priority
> KFD_MAX_QUEUE_PRIORITY
) {
376 pr_err("Queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
380 if ((args
->ring_base_address
) &&
381 (!access_ok((const void __user
*) args
->ring_base_address
,
382 sizeof(uint64_t)))) {
383 pr_err("Can't access ring base address\n");
387 if (!is_power_of_2(args
->ring_size
) && (args
->ring_size
!= 0)) {
388 pr_err("Ring size must be a power of 2 or 0\n");
392 properties
.queue_address
= args
->ring_base_address
;
393 properties
.queue_size
= args
->ring_size
;
394 properties
.queue_percent
= args
->queue_percentage
;
395 properties
.priority
= args
->queue_priority
;
397 pr_debug("Updating queue id %d for pasid 0x%x\n",
398 args
->queue_id
, p
->pasid
);
400 mutex_lock(&p
->mutex
);
402 retval
= pqm_update_queue(&p
->pqm
, args
->queue_id
, &properties
);
404 mutex_unlock(&p
->mutex
);
409 static int kfd_ioctl_set_cu_mask(struct file
*filp
, struct kfd_process
*p
,
413 const int max_num_cus
= 1024;
414 struct kfd_ioctl_set_cu_mask_args
*args
= data
;
415 struct queue_properties properties
;
416 uint32_t __user
*cu_mask_ptr
= (uint32_t __user
*)args
->cu_mask_ptr
;
417 size_t cu_mask_size
= sizeof(uint32_t) * (args
->num_cu_mask
/ 32);
419 if ((args
->num_cu_mask
% 32) != 0) {
420 pr_debug("num_cu_mask 0x%x must be a multiple of 32",
425 properties
.cu_mask_count
= args
->num_cu_mask
;
426 if (properties
.cu_mask_count
== 0) {
427 pr_debug("CU mask cannot be 0");
431 /* To prevent an unreasonably large CU mask size, set an arbitrary
432 * limit of max_num_cus bits. We can then just drop any CU mask bits
433 * past max_num_cus bits and just use the first max_num_cus bits.
435 if (properties
.cu_mask_count
> max_num_cus
) {
436 pr_debug("CU mask cannot be greater than 1024 bits");
437 properties
.cu_mask_count
= max_num_cus
;
438 cu_mask_size
= sizeof(uint32_t) * (max_num_cus
/32);
441 properties
.cu_mask
= kzalloc(cu_mask_size
, GFP_KERNEL
);
442 if (!properties
.cu_mask
)
445 retval
= copy_from_user(properties
.cu_mask
, cu_mask_ptr
, cu_mask_size
);
447 pr_debug("Could not copy CU mask from userspace");
448 kfree(properties
.cu_mask
);
452 mutex_lock(&p
->mutex
);
454 retval
= pqm_set_cu_mask(&p
->pqm
, args
->queue_id
, &properties
);
456 mutex_unlock(&p
->mutex
);
459 kfree(properties
.cu_mask
);
464 static int kfd_ioctl_get_queue_wave_state(struct file
*filep
,
465 struct kfd_process
*p
, void *data
)
467 struct kfd_ioctl_get_queue_wave_state_args
*args
= data
;
470 mutex_lock(&p
->mutex
);
472 r
= pqm_get_wave_state(&p
->pqm
, args
->queue_id
,
473 (void __user
*)args
->ctl_stack_address
,
474 &args
->ctl_stack_used_size
,
475 &args
->save_area_used_size
);
477 mutex_unlock(&p
->mutex
);
482 static int kfd_ioctl_set_memory_policy(struct file
*filep
,
483 struct kfd_process
*p
, void *data
)
485 struct kfd_ioctl_set_memory_policy_args
*args
= data
;
488 struct kfd_process_device
*pdd
;
489 enum cache_policy default_policy
, alternate_policy
;
491 if (args
->default_policy
!= KFD_IOC_CACHE_POLICY_COHERENT
492 && args
->default_policy
!= KFD_IOC_CACHE_POLICY_NONCOHERENT
) {
496 if (args
->alternate_policy
!= KFD_IOC_CACHE_POLICY_COHERENT
497 && args
->alternate_policy
!= KFD_IOC_CACHE_POLICY_NONCOHERENT
) {
501 dev
= kfd_device_by_id(args
->gpu_id
);
505 mutex_lock(&p
->mutex
);
507 pdd
= kfd_bind_process_to_device(dev
, p
);
513 default_policy
= (args
->default_policy
== KFD_IOC_CACHE_POLICY_COHERENT
)
514 ? cache_policy_coherent
: cache_policy_noncoherent
;
517 (args
->alternate_policy
== KFD_IOC_CACHE_POLICY_COHERENT
)
518 ? cache_policy_coherent
: cache_policy_noncoherent
;
520 if (!dev
->dqm
->ops
.set_cache_memory_policy(dev
->dqm
,
524 (void __user
*)args
->alternate_aperture_base
,
525 args
->alternate_aperture_size
))
529 mutex_unlock(&p
->mutex
);
534 static int kfd_ioctl_set_trap_handler(struct file
*filep
,
535 struct kfd_process
*p
, void *data
)
537 struct kfd_ioctl_set_trap_handler_args
*args
= data
;
540 struct kfd_process_device
*pdd
;
542 dev
= kfd_device_by_id(args
->gpu_id
);
546 mutex_lock(&p
->mutex
);
548 pdd
= kfd_bind_process_to_device(dev
, p
);
554 if (dev
->dqm
->ops
.set_trap_handler(dev
->dqm
,
561 mutex_unlock(&p
->mutex
);
566 static int kfd_ioctl_dbg_register(struct file
*filep
,
567 struct kfd_process
*p
, void *data
)
569 struct kfd_ioctl_dbg_register_args
*args
= data
;
571 struct kfd_dbgmgr
*dbgmgr_ptr
;
572 struct kfd_process_device
*pdd
;
576 dev
= kfd_device_by_id(args
->gpu_id
);
580 if (dev
->device_info
->asic_family
== CHIP_CARRIZO
) {
581 pr_debug("kfd_ioctl_dbg_register not supported on CZ\n");
585 mutex_lock(&p
->mutex
);
586 mutex_lock(kfd_get_dbgmgr_mutex());
589 * make sure that we have pdd, if this the first queue created for
592 pdd
= kfd_bind_process_to_device(dev
, p
);
594 status
= PTR_ERR(pdd
);
599 /* In case of a legal call, we have no dbgmgr yet */
600 create_ok
= kfd_dbgmgr_create(&dbgmgr_ptr
, dev
);
602 status
= kfd_dbgmgr_register(dbgmgr_ptr
, p
);
604 kfd_dbgmgr_destroy(dbgmgr_ptr
);
606 dev
->dbgmgr
= dbgmgr_ptr
;
609 pr_debug("debugger already registered\n");
614 mutex_unlock(kfd_get_dbgmgr_mutex());
615 mutex_unlock(&p
->mutex
);
620 static int kfd_ioctl_dbg_unregister(struct file
*filep
,
621 struct kfd_process
*p
, void *data
)
623 struct kfd_ioctl_dbg_unregister_args
*args
= data
;
627 dev
= kfd_device_by_id(args
->gpu_id
);
628 if (!dev
|| !dev
->dbgmgr
)
631 if (dev
->device_info
->asic_family
== CHIP_CARRIZO
) {
632 pr_debug("kfd_ioctl_dbg_unregister not supported on CZ\n");
636 mutex_lock(kfd_get_dbgmgr_mutex());
638 status
= kfd_dbgmgr_unregister(dev
->dbgmgr
, p
);
640 kfd_dbgmgr_destroy(dev
->dbgmgr
);
644 mutex_unlock(kfd_get_dbgmgr_mutex());
650 * Parse and generate variable size data structure for address watch.
651 * Total size of the buffer and # watch points is limited in order
652 * to prevent kernel abuse. (no bearing to the much smaller HW limitation
653 * which is enforced by dbgdev module)
654 * please also note that the watch address itself are not "copied from user",
655 * since it be set into the HW in user mode values.
658 static int kfd_ioctl_dbg_address_watch(struct file
*filep
,
659 struct kfd_process
*p
, void *data
)
661 struct kfd_ioctl_dbg_address_watch_args
*args
= data
;
663 struct dbg_address_watch_info aw_info
;
664 unsigned char *args_buff
;
666 void __user
*cmd_from_user
;
667 uint64_t watch_mask_value
= 0;
668 unsigned int args_idx
= 0;
670 memset((void *) &aw_info
, 0, sizeof(struct dbg_address_watch_info
));
672 dev
= kfd_device_by_id(args
->gpu_id
);
676 if (dev
->device_info
->asic_family
== CHIP_CARRIZO
) {
677 pr_debug("kfd_ioctl_dbg_wave_control not supported on CZ\n");
681 cmd_from_user
= (void __user
*) args
->content_ptr
;
683 /* Validate arguments */
685 if ((args
->buf_size_in_bytes
> MAX_ALLOWED_AW_BUFF_SIZE
) ||
686 (args
->buf_size_in_bytes
<= sizeof(*args
) + sizeof(int) * 2) ||
687 (cmd_from_user
== NULL
))
690 /* this is the actual buffer to work with */
691 args_buff
= memdup_user(cmd_from_user
,
692 args
->buf_size_in_bytes
- sizeof(*args
));
693 if (IS_ERR(args_buff
))
694 return PTR_ERR(args_buff
);
698 aw_info
.num_watch_points
= *((uint32_t *)(&args_buff
[args_idx
]));
699 args_idx
+= sizeof(aw_info
.num_watch_points
);
701 aw_info
.watch_mode
= (enum HSA_DBG_WATCH_MODE
*) &args_buff
[args_idx
];
702 args_idx
+= sizeof(enum HSA_DBG_WATCH_MODE
) * aw_info
.num_watch_points
;
705 * set watch address base pointer to point on the array base
708 aw_info
.watch_address
= (uint64_t *) &args_buff
[args_idx
];
710 /* skip over the addresses buffer */
711 args_idx
+= sizeof(aw_info
.watch_address
) * aw_info
.num_watch_points
;
713 if (args_idx
>= args
->buf_size_in_bytes
- sizeof(*args
)) {
718 watch_mask_value
= (uint64_t) args_buff
[args_idx
];
720 if (watch_mask_value
> 0) {
722 * There is an array of masks.
723 * set watch mask base pointer to point on the array base
726 aw_info
.watch_mask
= (uint64_t *) &args_buff
[args_idx
];
728 /* skip over the masks buffer */
729 args_idx
+= sizeof(aw_info
.watch_mask
) *
730 aw_info
.num_watch_points
;
732 /* just the NULL mask, set to NULL and skip over it */
733 aw_info
.watch_mask
= NULL
;
734 args_idx
+= sizeof(aw_info
.watch_mask
);
737 if (args_idx
>= args
->buf_size_in_bytes
- sizeof(args
)) {
742 /* Currently HSA Event is not supported for DBG */
743 aw_info
.watch_event
= NULL
;
745 mutex_lock(kfd_get_dbgmgr_mutex());
747 status
= kfd_dbgmgr_address_watch(dev
->dbgmgr
, &aw_info
);
749 mutex_unlock(kfd_get_dbgmgr_mutex());
757 /* Parse and generate fixed size data structure for wave control */
758 static int kfd_ioctl_dbg_wave_control(struct file
*filep
,
759 struct kfd_process
*p
, void *data
)
761 struct kfd_ioctl_dbg_wave_control_args
*args
= data
;
763 struct dbg_wave_control_info wac_info
;
764 unsigned char *args_buff
;
765 uint32_t computed_buff_size
;
767 void __user
*cmd_from_user
;
768 unsigned int args_idx
= 0;
770 memset((void *) &wac_info
, 0, sizeof(struct dbg_wave_control_info
));
772 /* we use compact form, independent of the packing attribute value */
773 computed_buff_size
= sizeof(*args
) +
774 sizeof(wac_info
.mode
) +
775 sizeof(wac_info
.operand
) +
776 sizeof(wac_info
.dbgWave_msg
.DbgWaveMsg
) +
777 sizeof(wac_info
.dbgWave_msg
.MemoryVA
) +
778 sizeof(wac_info
.trapId
);
780 dev
= kfd_device_by_id(args
->gpu_id
);
784 if (dev
->device_info
->asic_family
== CHIP_CARRIZO
) {
785 pr_debug("kfd_ioctl_dbg_wave_control not supported on CZ\n");
789 /* input size must match the computed "compact" size */
790 if (args
->buf_size_in_bytes
!= computed_buff_size
) {
791 pr_debug("size mismatch, computed : actual %u : %u\n",
792 args
->buf_size_in_bytes
, computed_buff_size
);
796 cmd_from_user
= (void __user
*) args
->content_ptr
;
798 if (cmd_from_user
== NULL
)
801 /* copy the entire buffer from user */
803 args_buff
= memdup_user(cmd_from_user
,
804 args
->buf_size_in_bytes
- sizeof(*args
));
805 if (IS_ERR(args_buff
))
806 return PTR_ERR(args_buff
);
808 /* move ptr to the start of the "pay-load" area */
809 wac_info
.process
= p
;
811 wac_info
.operand
= *((enum HSA_DBG_WAVEOP
*)(&args_buff
[args_idx
]));
812 args_idx
+= sizeof(wac_info
.operand
);
814 wac_info
.mode
= *((enum HSA_DBG_WAVEMODE
*)(&args_buff
[args_idx
]));
815 args_idx
+= sizeof(wac_info
.mode
);
817 wac_info
.trapId
= *((uint32_t *)(&args_buff
[args_idx
]));
818 args_idx
+= sizeof(wac_info
.trapId
);
820 wac_info
.dbgWave_msg
.DbgWaveMsg
.WaveMsgInfoGen2
.Value
=
821 *((uint32_t *)(&args_buff
[args_idx
]));
822 wac_info
.dbgWave_msg
.MemoryVA
= NULL
;
824 mutex_lock(kfd_get_dbgmgr_mutex());
826 pr_debug("Calling dbg manager process %p, operand %u, mode %u, trapId %u, message %u\n",
827 wac_info
.process
, wac_info
.operand
,
828 wac_info
.mode
, wac_info
.trapId
,
829 wac_info
.dbgWave_msg
.DbgWaveMsg
.WaveMsgInfoGen2
.Value
);
831 status
= kfd_dbgmgr_wave_control(dev
->dbgmgr
, &wac_info
);
833 pr_debug("Returned status of dbg manager is %ld\n", status
);
835 mutex_unlock(kfd_get_dbgmgr_mutex());
842 static int kfd_ioctl_get_clock_counters(struct file
*filep
,
843 struct kfd_process
*p
, void *data
)
845 struct kfd_ioctl_get_clock_counters_args
*args
= data
;
848 dev
= kfd_device_by_id(args
->gpu_id
);
850 /* Reading GPU clock counter from KGD */
851 args
->gpu_clock_counter
= amdgpu_amdkfd_get_gpu_clock_counter(dev
->kgd
);
853 /* Node without GPU resource */
854 args
->gpu_clock_counter
= 0;
856 /* No access to rdtsc. Using raw monotonic time */
857 args
->cpu_clock_counter
= ktime_get_raw_ns();
858 args
->system_clock_counter
= ktime_get_boottime_ns();
860 /* Since the counter is in nano-seconds we use 1GHz frequency */
861 args
->system_clock_freq
= 1000000000;
867 static int kfd_ioctl_get_process_apertures(struct file
*filp
,
868 struct kfd_process
*p
, void *data
)
870 struct kfd_ioctl_get_process_apertures_args
*args
= data
;
871 struct kfd_process_device_apertures
*pAperture
;
872 struct kfd_process_device
*pdd
;
874 dev_dbg(kfd_device
, "get apertures for PASID 0x%x", p
->pasid
);
876 args
->num_of_nodes
= 0;
878 mutex_lock(&p
->mutex
);
880 /*if the process-device list isn't empty*/
881 if (kfd_has_process_device_data(p
)) {
882 /* Run over all pdd of the process */
883 pdd
= kfd_get_first_process_device_data(p
);
886 &args
->process_apertures
[args
->num_of_nodes
];
887 pAperture
->gpu_id
= pdd
->dev
->id
;
888 pAperture
->lds_base
= pdd
->lds_base
;
889 pAperture
->lds_limit
= pdd
->lds_limit
;
890 pAperture
->gpuvm_base
= pdd
->gpuvm_base
;
891 pAperture
->gpuvm_limit
= pdd
->gpuvm_limit
;
892 pAperture
->scratch_base
= pdd
->scratch_base
;
893 pAperture
->scratch_limit
= pdd
->scratch_limit
;
896 "node id %u\n", args
->num_of_nodes
);
898 "gpu id %u\n", pdd
->dev
->id
);
900 "lds_base %llX\n", pdd
->lds_base
);
902 "lds_limit %llX\n", pdd
->lds_limit
);
904 "gpuvm_base %llX\n", pdd
->gpuvm_base
);
906 "gpuvm_limit %llX\n", pdd
->gpuvm_limit
);
908 "scratch_base %llX\n", pdd
->scratch_base
);
910 "scratch_limit %llX\n", pdd
->scratch_limit
);
912 args
->num_of_nodes
++;
914 pdd
= kfd_get_next_process_device_data(p
, pdd
);
915 } while (pdd
&& (args
->num_of_nodes
< NUM_OF_SUPPORTED_GPUS
));
918 mutex_unlock(&p
->mutex
);
923 static int kfd_ioctl_get_process_apertures_new(struct file
*filp
,
924 struct kfd_process
*p
, void *data
)
926 struct kfd_ioctl_get_process_apertures_new_args
*args
= data
;
927 struct kfd_process_device_apertures
*pa
;
928 struct kfd_process_device
*pdd
;
932 dev_dbg(kfd_device
, "get apertures for PASID 0x%x", p
->pasid
);
934 if (args
->num_of_nodes
== 0) {
935 /* Return number of nodes, so that user space can alloacate
938 mutex_lock(&p
->mutex
);
940 if (!kfd_has_process_device_data(p
))
943 /* Run over all pdd of the process */
944 pdd
= kfd_get_first_process_device_data(p
);
946 args
->num_of_nodes
++;
947 pdd
= kfd_get_next_process_device_data(p
, pdd
);
953 /* Fill in process-aperture information for all available
954 * nodes, but not more than args->num_of_nodes as that is
955 * the amount of memory allocated by user
957 pa
= kzalloc((sizeof(struct kfd_process_device_apertures
) *
958 args
->num_of_nodes
), GFP_KERNEL
);
962 mutex_lock(&p
->mutex
);
964 if (!kfd_has_process_device_data(p
)) {
965 args
->num_of_nodes
= 0;
970 /* Run over all pdd of the process */
971 pdd
= kfd_get_first_process_device_data(p
);
973 pa
[nodes
].gpu_id
= pdd
->dev
->id
;
974 pa
[nodes
].lds_base
= pdd
->lds_base
;
975 pa
[nodes
].lds_limit
= pdd
->lds_limit
;
976 pa
[nodes
].gpuvm_base
= pdd
->gpuvm_base
;
977 pa
[nodes
].gpuvm_limit
= pdd
->gpuvm_limit
;
978 pa
[nodes
].scratch_base
= pdd
->scratch_base
;
979 pa
[nodes
].scratch_limit
= pdd
->scratch_limit
;
982 "gpu id %u\n", pdd
->dev
->id
);
984 "lds_base %llX\n", pdd
->lds_base
);
986 "lds_limit %llX\n", pdd
->lds_limit
);
988 "gpuvm_base %llX\n", pdd
->gpuvm_base
);
990 "gpuvm_limit %llX\n", pdd
->gpuvm_limit
);
992 "scratch_base %llX\n", pdd
->scratch_base
);
994 "scratch_limit %llX\n", pdd
->scratch_limit
);
997 pdd
= kfd_get_next_process_device_data(p
, pdd
);
998 } while (pdd
&& (nodes
< args
->num_of_nodes
));
999 mutex_unlock(&p
->mutex
);
1001 args
->num_of_nodes
= nodes
;
1003 (void __user
*)args
->kfd_process_device_apertures_ptr
,
1005 (nodes
* sizeof(struct kfd_process_device_apertures
)));
1007 return ret
? -EFAULT
: 0;
1010 mutex_unlock(&p
->mutex
);
1014 static int kfd_ioctl_create_event(struct file
*filp
, struct kfd_process
*p
,
1017 struct kfd_ioctl_create_event_args
*args
= data
;
1020 /* For dGPUs the event page is allocated in user mode. The
1021 * handle is passed to KFD with the first call to this IOCTL
1022 * through the event_page_offset field.
1024 if (args
->event_page_offset
) {
1025 struct kfd_dev
*kfd
;
1026 struct kfd_process_device
*pdd
;
1027 void *mem
, *kern_addr
;
1030 if (p
->signal_page
) {
1031 pr_err("Event page is already set\n");
1035 kfd
= kfd_device_by_id(GET_GPU_ID(args
->event_page_offset
));
1037 pr_err("Getting device by id failed in %s\n", __func__
);
1041 mutex_lock(&p
->mutex
);
1042 pdd
= kfd_bind_process_to_device(kfd
, p
);
1048 mem
= kfd_process_device_translate_handle(pdd
,
1049 GET_IDR_HANDLE(args
->event_page_offset
));
1051 pr_err("Can't find BO, offset is 0x%llx\n",
1052 args
->event_page_offset
);
1056 mutex_unlock(&p
->mutex
);
1058 err
= amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(kfd
->kgd
,
1059 mem
, &kern_addr
, &size
);
1061 pr_err("Failed to map event page to kernel\n");
1065 err
= kfd_event_page_set(p
, kern_addr
, size
);
1067 pr_err("Failed to set event page\n");
1072 err
= kfd_event_create(filp
, p
, args
->event_type
,
1073 args
->auto_reset
!= 0, args
->node_id
,
1074 &args
->event_id
, &args
->event_trigger_data
,
1075 &args
->event_page_offset
,
1076 &args
->event_slot_index
);
1081 mutex_unlock(&p
->mutex
);
1085 static int kfd_ioctl_destroy_event(struct file
*filp
, struct kfd_process
*p
,
1088 struct kfd_ioctl_destroy_event_args
*args
= data
;
1090 return kfd_event_destroy(p
, args
->event_id
);
1093 static int kfd_ioctl_set_event(struct file
*filp
, struct kfd_process
*p
,
1096 struct kfd_ioctl_set_event_args
*args
= data
;
1098 return kfd_set_event(p
, args
->event_id
);
1101 static int kfd_ioctl_reset_event(struct file
*filp
, struct kfd_process
*p
,
1104 struct kfd_ioctl_reset_event_args
*args
= data
;
1106 return kfd_reset_event(p
, args
->event_id
);
1109 static int kfd_ioctl_wait_events(struct file
*filp
, struct kfd_process
*p
,
1112 struct kfd_ioctl_wait_events_args
*args
= data
;
1115 err
= kfd_wait_on_events(p
, args
->num_events
,
1116 (void __user
*)args
->events_ptr
,
1117 (args
->wait_for_all
!= 0),
1118 args
->timeout
, &args
->wait_result
);
1122 static int kfd_ioctl_set_scratch_backing_va(struct file
*filep
,
1123 struct kfd_process
*p
, void *data
)
1125 struct kfd_ioctl_set_scratch_backing_va_args
*args
= data
;
1126 struct kfd_process_device
*pdd
;
1127 struct kfd_dev
*dev
;
1130 dev
= kfd_device_by_id(args
->gpu_id
);
1134 mutex_lock(&p
->mutex
);
1136 pdd
= kfd_bind_process_to_device(dev
, p
);
1139 goto bind_process_to_device_fail
;
1142 pdd
->qpd
.sh_hidden_private_base
= args
->va_addr
;
1144 mutex_unlock(&p
->mutex
);
1146 if (dev
->dqm
->sched_policy
== KFD_SCHED_POLICY_NO_HWS
&&
1147 pdd
->qpd
.vmid
!= 0 && dev
->kfd2kgd
->set_scratch_backing_va
)
1148 dev
->kfd2kgd
->set_scratch_backing_va(
1149 dev
->kgd
, args
->va_addr
, pdd
->qpd
.vmid
);
1153 bind_process_to_device_fail
:
1154 mutex_unlock(&p
->mutex
);
1158 static int kfd_ioctl_get_tile_config(struct file
*filep
,
1159 struct kfd_process
*p
, void *data
)
1161 struct kfd_ioctl_get_tile_config_args
*args
= data
;
1162 struct kfd_dev
*dev
;
1163 struct tile_config config
;
1166 dev
= kfd_device_by_id(args
->gpu_id
);
1170 dev
->kfd2kgd
->get_tile_config(dev
->kgd
, &config
);
1172 args
->gb_addr_config
= config
.gb_addr_config
;
1173 args
->num_banks
= config
.num_banks
;
1174 args
->num_ranks
= config
.num_ranks
;
1176 if (args
->num_tile_configs
> config
.num_tile_configs
)
1177 args
->num_tile_configs
= config
.num_tile_configs
;
1178 err
= copy_to_user((void __user
*)args
->tile_config_ptr
,
1179 config
.tile_config_ptr
,
1180 args
->num_tile_configs
* sizeof(uint32_t));
1182 args
->num_tile_configs
= 0;
1186 if (args
->num_macro_tile_configs
> config
.num_macro_tile_configs
)
1187 args
->num_macro_tile_configs
=
1188 config
.num_macro_tile_configs
;
1189 err
= copy_to_user((void __user
*)args
->macro_tile_config_ptr
,
1190 config
.macro_tile_config_ptr
,
1191 args
->num_macro_tile_configs
* sizeof(uint32_t));
1193 args
->num_macro_tile_configs
= 0;
1200 static int kfd_ioctl_acquire_vm(struct file
*filep
, struct kfd_process
*p
,
1203 struct kfd_ioctl_acquire_vm_args
*args
= data
;
1204 struct kfd_process_device
*pdd
;
1205 struct kfd_dev
*dev
;
1206 struct file
*drm_file
;
1209 dev
= kfd_device_by_id(args
->gpu_id
);
1213 drm_file
= fget(args
->drm_fd
);
1217 mutex_lock(&p
->mutex
);
1219 pdd
= kfd_get_process_device_data(dev
, p
);
1225 if (pdd
->drm_file
) {
1226 ret
= pdd
->drm_file
== drm_file
? 0 : -EBUSY
;
1230 ret
= kfd_process_device_init_vm(pdd
, drm_file
);
1233 /* On success, the PDD keeps the drm_file reference */
1234 mutex_unlock(&p
->mutex
);
1239 mutex_unlock(&p
->mutex
);
1244 bool kfd_dev_is_large_bar(struct kfd_dev
*dev
)
1246 struct kfd_local_mem_info mem_info
;
1248 if (debug_largebar
) {
1249 pr_debug("Simulate large-bar allocation on non large-bar machine\n");
1253 if (dev
->device_info
->needs_iommu_device
)
1256 amdgpu_amdkfd_get_local_mem_info(dev
->kgd
, &mem_info
);
1257 if (mem_info
.local_mem_size_private
== 0 &&
1258 mem_info
.local_mem_size_public
> 0)
1263 static int kfd_ioctl_alloc_memory_of_gpu(struct file
*filep
,
1264 struct kfd_process
*p
, void *data
)
1266 struct kfd_ioctl_alloc_memory_of_gpu_args
*args
= data
;
1267 struct kfd_process_device
*pdd
;
1269 struct kfd_dev
*dev
;
1272 uint64_t offset
= args
->mmap_offset
;
1273 uint32_t flags
= args
->flags
;
1275 if (args
->size
== 0)
1278 dev
= kfd_device_by_id(args
->gpu_id
);
1282 if ((flags
& KFD_IOC_ALLOC_MEM_FLAGS_PUBLIC
) &&
1283 (flags
& KFD_IOC_ALLOC_MEM_FLAGS_VRAM
) &&
1284 !kfd_dev_is_large_bar(dev
)) {
1285 pr_err("Alloc host visible vram on small bar is not allowed\n");
1289 if (flags
& KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL
) {
1290 if (args
->size
!= kfd_doorbell_process_slice(dev
))
1292 offset
= kfd_get_process_doorbells(dev
, p
);
1293 } else if (flags
& KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP
) {
1294 if (args
->size
!= PAGE_SIZE
)
1296 offset
= amdgpu_amdkfd_get_mmio_remap_phys_addr(dev
->kgd
);
1301 mutex_lock(&p
->mutex
);
1303 pdd
= kfd_bind_process_to_device(dev
, p
);
1309 err
= amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
1310 dev
->kgd
, args
->va_addr
, args
->size
,
1311 pdd
->vm
, (struct kgd_mem
**) &mem
, &offset
,
1317 idr_handle
= kfd_process_device_create_obj_handle(pdd
, mem
);
1318 if (idr_handle
< 0) {
1323 mutex_unlock(&p
->mutex
);
1325 args
->handle
= MAKE_HANDLE(args
->gpu_id
, idr_handle
);
1326 args
->mmap_offset
= offset
;
1328 /* MMIO is mapped through kfd device
1329 * Generate a kfd mmap offset
1331 if (flags
& KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP
)
1332 args
->mmap_offset
= KFD_MMAP_TYPE_MMIO
1333 | KFD_MMAP_GPU_ID(args
->gpu_id
);
1338 amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev
->kgd
, (struct kgd_mem
*)mem
);
1340 mutex_unlock(&p
->mutex
);
1344 static int kfd_ioctl_free_memory_of_gpu(struct file
*filep
,
1345 struct kfd_process
*p
, void *data
)
1347 struct kfd_ioctl_free_memory_of_gpu_args
*args
= data
;
1348 struct kfd_process_device
*pdd
;
1350 struct kfd_dev
*dev
;
1353 dev
= kfd_device_by_id(GET_GPU_ID(args
->handle
));
1357 mutex_lock(&p
->mutex
);
1359 pdd
= kfd_get_process_device_data(dev
, p
);
1361 pr_err("Process device data doesn't exist\n");
1366 mem
= kfd_process_device_translate_handle(
1367 pdd
, GET_IDR_HANDLE(args
->handle
));
1373 ret
= amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev
->kgd
,
1374 (struct kgd_mem
*)mem
);
1376 /* If freeing the buffer failed, leave the handle in place for
1377 * clean-up during process tear-down.
1380 kfd_process_device_remove_obj_handle(
1381 pdd
, GET_IDR_HANDLE(args
->handle
));
1384 mutex_unlock(&p
->mutex
);
1388 static int kfd_ioctl_map_memory_to_gpu(struct file
*filep
,
1389 struct kfd_process
*p
, void *data
)
1391 struct kfd_ioctl_map_memory_to_gpu_args
*args
= data
;
1392 struct kfd_process_device
*pdd
, *peer_pdd
;
1394 struct kfd_dev
*dev
, *peer
;
1397 uint32_t *devices_arr
= NULL
;
1399 dev
= kfd_device_by_id(GET_GPU_ID(args
->handle
));
1403 if (!args
->n_devices
) {
1404 pr_debug("Device IDs array empty\n");
1407 if (args
->n_success
> args
->n_devices
) {
1408 pr_debug("n_success exceeds n_devices\n");
1412 devices_arr
= kmalloc_array(args
->n_devices
, sizeof(*devices_arr
),
1417 err
= copy_from_user(devices_arr
,
1418 (void __user
*)args
->device_ids_array_ptr
,
1419 args
->n_devices
* sizeof(*devices_arr
));
1422 goto copy_from_user_failed
;
1425 mutex_lock(&p
->mutex
);
1427 pdd
= kfd_bind_process_to_device(dev
, p
);
1430 goto bind_process_to_device_failed
;
1433 mem
= kfd_process_device_translate_handle(pdd
,
1434 GET_IDR_HANDLE(args
->handle
));
1437 goto get_mem_obj_from_handle_failed
;
1440 for (i
= args
->n_success
; i
< args
->n_devices
; i
++) {
1441 peer
= kfd_device_by_id(devices_arr
[i
]);
1443 pr_debug("Getting device by id failed for 0x%x\n",
1446 goto get_mem_obj_from_handle_failed
;
1449 peer_pdd
= kfd_bind_process_to_device(peer
, p
);
1450 if (IS_ERR(peer_pdd
)) {
1451 err
= PTR_ERR(peer_pdd
);
1452 goto get_mem_obj_from_handle_failed
;
1454 err
= amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
1455 peer
->kgd
, (struct kgd_mem
*)mem
, peer_pdd
->vm
);
1457 pr_err("Failed to map to gpu %d/%d\n",
1458 i
, args
->n_devices
);
1459 goto map_memory_to_gpu_failed
;
1461 args
->n_success
= i
+1;
1464 mutex_unlock(&p
->mutex
);
1466 err
= amdgpu_amdkfd_gpuvm_sync_memory(dev
->kgd
, (struct kgd_mem
*) mem
, true);
1468 pr_debug("Sync memory failed, wait interrupted by user signal\n");
1469 goto sync_memory_failed
;
1472 /* Flush TLBs after waiting for the page table updates to complete */
1473 for (i
= 0; i
< args
->n_devices
; i
++) {
1474 peer
= kfd_device_by_id(devices_arr
[i
]);
1475 if (WARN_ON_ONCE(!peer
))
1477 peer_pdd
= kfd_get_process_device_data(peer
, p
);
1478 if (WARN_ON_ONCE(!peer_pdd
))
1480 kfd_flush_tlb(peer_pdd
);
1487 bind_process_to_device_failed
:
1488 get_mem_obj_from_handle_failed
:
1489 map_memory_to_gpu_failed
:
1490 mutex_unlock(&p
->mutex
);
1491 copy_from_user_failed
:
1498 static int kfd_ioctl_unmap_memory_from_gpu(struct file
*filep
,
1499 struct kfd_process
*p
, void *data
)
1501 struct kfd_ioctl_unmap_memory_from_gpu_args
*args
= data
;
1502 struct kfd_process_device
*pdd
, *peer_pdd
;
1504 struct kfd_dev
*dev
, *peer
;
1506 uint32_t *devices_arr
= NULL
, i
;
1508 dev
= kfd_device_by_id(GET_GPU_ID(args
->handle
));
1512 if (!args
->n_devices
) {
1513 pr_debug("Device IDs array empty\n");
1516 if (args
->n_success
> args
->n_devices
) {
1517 pr_debug("n_success exceeds n_devices\n");
1521 devices_arr
= kmalloc_array(args
->n_devices
, sizeof(*devices_arr
),
1526 err
= copy_from_user(devices_arr
,
1527 (void __user
*)args
->device_ids_array_ptr
,
1528 args
->n_devices
* sizeof(*devices_arr
));
1531 goto copy_from_user_failed
;
1534 mutex_lock(&p
->mutex
);
1536 pdd
= kfd_get_process_device_data(dev
, p
);
1539 goto bind_process_to_device_failed
;
1542 mem
= kfd_process_device_translate_handle(pdd
,
1543 GET_IDR_HANDLE(args
->handle
));
1546 goto get_mem_obj_from_handle_failed
;
1549 for (i
= args
->n_success
; i
< args
->n_devices
; i
++) {
1550 peer
= kfd_device_by_id(devices_arr
[i
]);
1553 goto get_mem_obj_from_handle_failed
;
1556 peer_pdd
= kfd_get_process_device_data(peer
, p
);
1559 goto get_mem_obj_from_handle_failed
;
1561 err
= amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
1562 peer
->kgd
, (struct kgd_mem
*)mem
, peer_pdd
->vm
);
1564 pr_err("Failed to unmap from gpu %d/%d\n",
1565 i
, args
->n_devices
);
1566 goto unmap_memory_from_gpu_failed
;
1568 args
->n_success
= i
+1;
1572 mutex_unlock(&p
->mutex
);
1576 bind_process_to_device_failed
:
1577 get_mem_obj_from_handle_failed
:
1578 unmap_memory_from_gpu_failed
:
1579 mutex_unlock(&p
->mutex
);
1580 copy_from_user_failed
:
1585 static int kfd_ioctl_get_dmabuf_info(struct file
*filep
,
1586 struct kfd_process
*p
, void *data
)
1588 struct kfd_ioctl_get_dmabuf_info_args
*args
= data
;
1589 struct kfd_dev
*dev
= NULL
;
1590 struct kgd_dev
*dma_buf_kgd
;
1591 void *metadata_buffer
= NULL
;
1596 /* Find a KFD GPU device that supports the get_dmabuf_info query */
1597 for (i
= 0; kfd_topology_enum_kfd_devices(i
, &dev
) == 0; i
++)
1603 if (args
->metadata_ptr
) {
1604 metadata_buffer
= kzalloc(args
->metadata_size
, GFP_KERNEL
);
1605 if (!metadata_buffer
)
1609 /* Get dmabuf info from KGD */
1610 r
= amdgpu_amdkfd_get_dmabuf_info(dev
->kgd
, args
->dmabuf_fd
,
1611 &dma_buf_kgd
, &args
->size
,
1612 metadata_buffer
, args
->metadata_size
,
1613 &args
->metadata_size
, &flags
);
1617 /* Reverse-lookup gpu_id from kgd pointer */
1618 dev
= kfd_device_by_kgd(dma_buf_kgd
);
1623 args
->gpu_id
= dev
->id
;
1624 args
->flags
= flags
;
1626 /* Copy metadata buffer to user mode */
1627 if (metadata_buffer
) {
1628 r
= copy_to_user((void __user
*)args
->metadata_ptr
,
1629 metadata_buffer
, args
->metadata_size
);
1635 kfree(metadata_buffer
);
1640 static int kfd_ioctl_import_dmabuf(struct file
*filep
,
1641 struct kfd_process
*p
, void *data
)
1643 struct kfd_ioctl_import_dmabuf_args
*args
= data
;
1644 struct kfd_process_device
*pdd
;
1645 struct dma_buf
*dmabuf
;
1646 struct kfd_dev
*dev
;
1652 dev
= kfd_device_by_id(args
->gpu_id
);
1656 dmabuf
= dma_buf_get(args
->dmabuf_fd
);
1658 return PTR_ERR(dmabuf
);
1660 mutex_lock(&p
->mutex
);
1662 pdd
= kfd_bind_process_to_device(dev
, p
);
1668 r
= amdgpu_amdkfd_gpuvm_import_dmabuf(dev
->kgd
, dmabuf
,
1669 args
->va_addr
, pdd
->vm
,
1670 (struct kgd_mem
**)&mem
, &size
,
1675 idr_handle
= kfd_process_device_create_obj_handle(pdd
, mem
);
1676 if (idr_handle
< 0) {
1681 mutex_unlock(&p
->mutex
);
1683 args
->handle
= MAKE_HANDLE(args
->gpu_id
, idr_handle
);
1688 amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev
->kgd
, (struct kgd_mem
*)mem
);
1690 mutex_unlock(&p
->mutex
);
1694 #define AMDKFD_IOCTL_DEF(ioctl, _func, _flags) \
1695 [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func, .flags = _flags, \
1696 .cmd_drv = 0, .name = #ioctl}
1699 static const struct amdkfd_ioctl_desc amdkfd_ioctls
[] = {
1700 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_VERSION
,
1701 kfd_ioctl_get_version
, 0),
1703 AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_QUEUE
,
1704 kfd_ioctl_create_queue
, 0),
1706 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_QUEUE
,
1707 kfd_ioctl_destroy_queue
, 0),
1709 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_MEMORY_POLICY
,
1710 kfd_ioctl_set_memory_policy
, 0),
1712 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_CLOCK_COUNTERS
,
1713 kfd_ioctl_get_clock_counters
, 0),
1715 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_PROCESS_APERTURES
,
1716 kfd_ioctl_get_process_apertures
, 0),
1718 AMDKFD_IOCTL_DEF(AMDKFD_IOC_UPDATE_QUEUE
,
1719 kfd_ioctl_update_queue
, 0),
1721 AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_EVENT
,
1722 kfd_ioctl_create_event
, 0),
1724 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_EVENT
,
1725 kfd_ioctl_destroy_event
, 0),
1727 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_EVENT
,
1728 kfd_ioctl_set_event
, 0),
1730 AMDKFD_IOCTL_DEF(AMDKFD_IOC_RESET_EVENT
,
1731 kfd_ioctl_reset_event
, 0),
1733 AMDKFD_IOCTL_DEF(AMDKFD_IOC_WAIT_EVENTS
,
1734 kfd_ioctl_wait_events
, 0),
1736 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_REGISTER
,
1737 kfd_ioctl_dbg_register
, 0),
1739 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_UNREGISTER
,
1740 kfd_ioctl_dbg_unregister
, 0),
1742 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_ADDRESS_WATCH
,
1743 kfd_ioctl_dbg_address_watch
, 0),
1745 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_WAVE_CONTROL
,
1746 kfd_ioctl_dbg_wave_control
, 0),
1748 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_SCRATCH_BACKING_VA
,
1749 kfd_ioctl_set_scratch_backing_va
, 0),
1751 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_TILE_CONFIG
,
1752 kfd_ioctl_get_tile_config
, 0),
1754 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_TRAP_HANDLER
,
1755 kfd_ioctl_set_trap_handler
, 0),
1757 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_PROCESS_APERTURES_NEW
,
1758 kfd_ioctl_get_process_apertures_new
, 0),
1760 AMDKFD_IOCTL_DEF(AMDKFD_IOC_ACQUIRE_VM
,
1761 kfd_ioctl_acquire_vm
, 0),
1763 AMDKFD_IOCTL_DEF(AMDKFD_IOC_ALLOC_MEMORY_OF_GPU
,
1764 kfd_ioctl_alloc_memory_of_gpu
, 0),
1766 AMDKFD_IOCTL_DEF(AMDKFD_IOC_FREE_MEMORY_OF_GPU
,
1767 kfd_ioctl_free_memory_of_gpu
, 0),
1769 AMDKFD_IOCTL_DEF(AMDKFD_IOC_MAP_MEMORY_TO_GPU
,
1770 kfd_ioctl_map_memory_to_gpu
, 0),
1772 AMDKFD_IOCTL_DEF(AMDKFD_IOC_UNMAP_MEMORY_FROM_GPU
,
1773 kfd_ioctl_unmap_memory_from_gpu
, 0),
1775 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_CU_MASK
,
1776 kfd_ioctl_set_cu_mask
, 0),
1778 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_QUEUE_WAVE_STATE
,
1779 kfd_ioctl_get_queue_wave_state
, 0),
1781 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_DMABUF_INFO
,
1782 kfd_ioctl_get_dmabuf_info
, 0),
1784 AMDKFD_IOCTL_DEF(AMDKFD_IOC_IMPORT_DMABUF
,
1785 kfd_ioctl_import_dmabuf
, 0),
1789 #define AMDKFD_CORE_IOCTL_COUNT ARRAY_SIZE(amdkfd_ioctls)
1791 static long kfd_ioctl(struct file
*filep
, unsigned int cmd
, unsigned long arg
)
1793 struct kfd_process
*process
;
1794 amdkfd_ioctl_t
*func
;
1795 const struct amdkfd_ioctl_desc
*ioctl
= NULL
;
1796 unsigned int nr
= _IOC_NR(cmd
);
1797 char stack_kdata
[128];
1799 unsigned int usize
, asize
;
1800 int retcode
= -EINVAL
;
1802 if (nr
>= AMDKFD_CORE_IOCTL_COUNT
)
1805 if ((nr
>= AMDKFD_COMMAND_START
) && (nr
< AMDKFD_COMMAND_END
)) {
1808 ioctl
= &amdkfd_ioctls
[nr
];
1810 amdkfd_size
= _IOC_SIZE(ioctl
->cmd
);
1811 usize
= asize
= _IOC_SIZE(cmd
);
1812 if (amdkfd_size
> asize
)
1813 asize
= amdkfd_size
;
1819 dev_dbg(kfd_device
, "ioctl cmd 0x%x (#0x%x), arg 0x%lx\n", cmd
, nr
, arg
);
1821 /* Get the process struct from the filep. Only the process
1822 * that opened /dev/kfd can use the file descriptor. Child
1823 * processes need to create their own KFD device context.
1825 process
= filep
->private_data
;
1826 if (process
->lead_thread
!= current
->group_leader
) {
1827 dev_dbg(kfd_device
, "Using KFD FD in wrong process\n");
1832 /* Do not trust userspace, use our own definition */
1835 if (unlikely(!func
)) {
1836 dev_dbg(kfd_device
, "no function\n");
1841 if (cmd
& (IOC_IN
| IOC_OUT
)) {
1842 if (asize
<= sizeof(stack_kdata
)) {
1843 kdata
= stack_kdata
;
1845 kdata
= kmalloc(asize
, GFP_KERNEL
);
1852 memset(kdata
+ usize
, 0, asize
- usize
);
1856 if (copy_from_user(kdata
, (void __user
*)arg
, usize
) != 0) {
1860 } else if (cmd
& IOC_OUT
) {
1861 memset(kdata
, 0, usize
);
1864 retcode
= func(filep
, process
, kdata
);
1867 if (copy_to_user((void __user
*)arg
, kdata
, usize
) != 0)
1872 dev_dbg(kfd_device
, "invalid ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n",
1873 task_pid_nr(current
), cmd
, nr
);
1875 if (kdata
!= stack_kdata
)
1879 dev_dbg(kfd_device
, "ioctl cmd (#0x%x), arg 0x%lx, ret = %d\n",
1885 static int kfd_mmio_mmap(struct kfd_dev
*dev
, struct kfd_process
*process
,
1886 struct vm_area_struct
*vma
)
1888 phys_addr_t address
;
1891 if (vma
->vm_end
- vma
->vm_start
!= PAGE_SIZE
)
1894 address
= amdgpu_amdkfd_get_mmio_remap_phys_addr(dev
->kgd
);
1896 vma
->vm_flags
|= VM_IO
| VM_DONTCOPY
| VM_DONTEXPAND
| VM_NORESERVE
|
1897 VM_DONTDUMP
| VM_PFNMAP
;
1899 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1901 pr_debug("pasid 0x%x mapping mmio page\n"
1902 " target user address == 0x%08llX\n"
1903 " physical address == 0x%08llX\n"
1904 " vm_flags == 0x%04lX\n"
1905 " size == 0x%04lX\n",
1906 process
->pasid
, (unsigned long long) vma
->vm_start
,
1907 address
, vma
->vm_flags
, PAGE_SIZE
);
1909 ret
= io_remap_pfn_range(vma
,
1911 address
>> PAGE_SHIFT
,
1918 static int kfd_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1920 struct kfd_process
*process
;
1921 struct kfd_dev
*dev
= NULL
;
1922 unsigned long mmap_offset
;
1923 unsigned int gpu_id
;
1925 process
= kfd_get_process(current
);
1926 if (IS_ERR(process
))
1927 return PTR_ERR(process
);
1929 mmap_offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1930 gpu_id
= KFD_MMAP_GET_GPU_ID(mmap_offset
);
1932 dev
= kfd_device_by_id(gpu_id
);
1934 switch (mmap_offset
& KFD_MMAP_TYPE_MASK
) {
1935 case KFD_MMAP_TYPE_DOORBELL
:
1938 return kfd_doorbell_mmap(dev
, process
, vma
);
1940 case KFD_MMAP_TYPE_EVENTS
:
1941 return kfd_event_mmap(process
, vma
);
1943 case KFD_MMAP_TYPE_RESERVED_MEM
:
1946 return kfd_reserved_mem_mmap(dev
, process
, vma
);
1947 case KFD_MMAP_TYPE_MMIO
:
1950 return kfd_mmio_mmap(dev
, process
, vma
);