1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3 * Copyright 2014-2022 Advanced Micro Devices, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <linux/capability.h>
25 #include <linux/device.h>
26 #include <linux/err.h>
27 #include <linux/fs.h>
28 #include <linux/file.h>
29 #include <linux/overflow.h>
30 #include <linux/sched.h>
31 #include <linux/slab.h>
32 #include <linux/uaccess.h>
33 #include <linux/compat.h>
34 #include <uapi/linux/kfd_ioctl.h>
35 #include <linux/time.h>
36 #include <linux/mm.h>
37 #include <linux/mman.h>
38 #include <linux/ptrace.h>
39 #include <linux/dma-buf.h>
40 #include <linux/processor.h>
41 #include "kfd_priv.h"
42 #include "kfd_device_queue_manager.h"
43 #include "kfd_svm.h"
44 #include "amdgpu_amdkfd.h"
45 #include "kfd_smi_events.h"
46 #include "amdgpu_dma_buf.h"
47 #include "kfd_debug.h"
48 #include "amdgpu_ptl.h"
49
50 static long kfd_ioctl(struct file *, unsigned int, unsigned long);
51 static int kfd_open(struct inode *, struct file *);
52 static int kfd_release(struct inode *, struct file *);
53 static int kfd_mmap(struct file *, struct vm_area_struct *);
54
55 static const char kfd_dev_name[] = "kfd";
56
57 static const struct file_operations kfd_fops = {
58 .owner = THIS_MODULE,
59 .unlocked_ioctl = kfd_ioctl,
60 .compat_ioctl = compat_ptr_ioctl,
61 .open = kfd_open,
62 .release = kfd_release,
63 .mmap = kfd_mmap,
64 };
65
66 static int kfd_char_dev_major = -1;
67 struct device *kfd_device;
68 static const struct class kfd_class = {
69 .name = kfd_dev_name,
70 };
71
72 /*
73 * Cache the address space of the chardev on first open so that the reset
74 * path can drop all userspace mappings of doorbell and MMIO ranges via
75 * unmap_mapping_range().
76 */
77 static struct address_space *kfd_dev_mapping;
78
kfd_dev_unmap_mapping_range(loff_t const holebegin,loff_t const holelen)79 void kfd_dev_unmap_mapping_range(loff_t const holebegin, loff_t const holelen)
80 {
81 struct address_space *mapping = READ_ONCE(kfd_dev_mapping);
82
83 if (mapping)
84 unmap_mapping_range(mapping, holebegin, holelen, 1);
85 }
86
kfd_lock_pdd_by_id(struct kfd_process * p,__u32 gpu_id)87 static inline struct kfd_process_device *kfd_lock_pdd_by_id(struct kfd_process *p, __u32 gpu_id)
88 {
89 struct kfd_process_device *pdd;
90
91 mutex_lock(&p->mutex);
92 pdd = kfd_process_device_data_by_id(p, gpu_id);
93
94 if (pdd)
95 return pdd;
96
97 mutex_unlock(&p->mutex);
98 return NULL;
99 }
100
kfd_unlock_pdd(struct kfd_process_device * pdd)101 static inline void kfd_unlock_pdd(struct kfd_process_device *pdd)
102 {
103 mutex_unlock(&pdd->process->mutex);
104 }
105
kfd_chardev_init(void)106 int kfd_chardev_init(void)
107 {
108 int err = 0;
109
110 kfd_char_dev_major = register_chrdev(0, kfd_dev_name, &kfd_fops);
111 err = kfd_char_dev_major;
112 if (err < 0)
113 goto err_register_chrdev;
114
115 err = class_register(&kfd_class);
116 if (err)
117 goto err_class_create;
118
119 kfd_device = device_create(&kfd_class, NULL,
120 MKDEV(kfd_char_dev_major, 0),
121 NULL, kfd_dev_name);
122 err = PTR_ERR(kfd_device);
123 if (IS_ERR(kfd_device))
124 goto err_device_create;
125
126 return 0;
127
128 err_device_create:
129 class_unregister(&kfd_class);
130 err_class_create:
131 unregister_chrdev(kfd_char_dev_major, kfd_dev_name);
132 err_register_chrdev:
133 return err;
134 }
135
kfd_chardev_exit(void)136 void kfd_chardev_exit(void)
137 {
138 device_destroy(&kfd_class, MKDEV(kfd_char_dev_major, 0));
139 class_unregister(&kfd_class);
140 unregister_chrdev(kfd_char_dev_major, kfd_dev_name);
141 kfd_device = NULL;
142 }
143
144
kfd_open(struct inode * inode,struct file * filep)145 static int kfd_open(struct inode *inode, struct file *filep)
146 {
147 struct kfd_process *process;
148 bool is_32bit_user_mode;
149
150 if (iminor(inode) != 0)
151 return -ENODEV;
152
153 /*
154 * /dev/kfd is a single chardev so all opens share one inode. Cache
155 * its address_space on the first open for use by the reset path.
156 */
157 if (!READ_ONCE(kfd_dev_mapping))
158 cmpxchg(&kfd_dev_mapping, NULL, inode->i_mapping);
159
160 is_32bit_user_mode = in_compat_syscall();
161
162 if (is_32bit_user_mode) {
163 dev_warn(kfd_device,
164 "Process %d (32-bit) failed to open /dev/kfd\n"
165 "32-bit processes are not supported by amdkfd\n",
166 current->pid);
167 return -EPERM;
168 }
169
170 process = kfd_create_process(current);
171 if (IS_ERR(process))
172 return PTR_ERR(process);
173
174 /* filep now owns the reference returned by kfd_create_process */
175 filep->private_data = process;
176
177 dev_dbg(kfd_device, "process pid %d opened kfd node, compat mode (32 bit) - %d\n",
178 process->lead_thread->pid, process->is_32bit_user_mode);
179
180 return 0;
181 }
182
kfd_release(struct inode * inode,struct file * filep)183 static int kfd_release(struct inode *inode, struct file *filep)
184 {
185 struct kfd_process *process = filep->private_data;
186
187 if (!process)
188 return 0;
189
190 if (process->context_id != KFD_CONTEXT_ID_PRIMARY)
191 kfd_process_notifier_release_internal(process);
192
193 kfd_unref_process(process);
194
195 return 0;
196 }
197
kfd_ioctl_get_version(struct file * filep,struct kfd_process * p,void * data)198 static int kfd_ioctl_get_version(struct file *filep, struct kfd_process *p,
199 void *data)
200 {
201 struct kfd_ioctl_get_version_args *args = data;
202
203 args->major_version = KFD_IOCTL_MAJOR_VERSION;
204 args->minor_version = KFD_IOCTL_MINOR_VERSION;
205
206 return 0;
207 }
208
set_queue_properties_from_user(struct queue_properties * q_properties,struct kfd_ioctl_create_queue_args * args)209 static int set_queue_properties_from_user(struct queue_properties *q_properties,
210 struct kfd_ioctl_create_queue_args *args)
211 {
212 /*
213 * Repurpose queue percentage to accommodate new features:
214 * bit 0-7: queue percentage
215 * bit 8-15: pm4_target_xcc
216 */
217 if ((args->queue_percentage & 0xFF) > KFD_MAX_QUEUE_PERCENTAGE) {
218 pr_err("Queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
219 return -EINVAL;
220 }
221
222 if (args->queue_priority > KFD_MAX_QUEUE_PRIORITY) {
223 pr_err("Queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
224 return -EINVAL;
225 }
226
227 if ((args->ring_base_address) &&
228 (!access_ok((const void __user *) args->ring_base_address,
229 sizeof(uint64_t)))) {
230 pr_err("Can't access ring base address\n");
231 return -EFAULT;
232 }
233
234 if (!is_power_of_2(args->ring_size) && (args->ring_size != 0)) {
235 pr_err("Ring size must be a power of 2 or 0\n");
236 return -EINVAL;
237 }
238
239 if (args->ring_size < KFD_MIN_QUEUE_RING_SIZE) {
240 args->ring_size = KFD_MIN_QUEUE_RING_SIZE;
241 pr_debug("Size lower. clamped to KFD_MIN_QUEUE_RING_SIZE");
242 }
243
244 if ((args->metadata_ring_size != 0) && !is_power_of_2(args->metadata_ring_size)) {
245 pr_err("Metadata ring size must be a power of 2 or 0\n");
246 return -EINVAL;
247 }
248
249 if (!access_ok((const void __user *) args->read_pointer_address,
250 sizeof(uint32_t))) {
251 pr_err("Can't access read pointer\n");
252 return -EFAULT;
253 }
254
255 if (!access_ok((const void __user *) args->write_pointer_address,
256 sizeof(uint32_t))) {
257 pr_err("Can't access write pointer\n");
258 return -EFAULT;
259 }
260
261 if (args->eop_buffer_address &&
262 !access_ok((const void __user *) args->eop_buffer_address,
263 sizeof(uint32_t))) {
264 pr_debug("Can't access eop buffer");
265 return -EFAULT;
266 }
267
268 if (args->ctx_save_restore_address &&
269 !access_ok((const void __user *) args->ctx_save_restore_address,
270 sizeof(uint32_t))) {
271 pr_debug("Can't access ctx save restore buffer");
272 return -EFAULT;
273 }
274
275 q_properties->is_interop = false;
276 q_properties->is_gws = false;
277 q_properties->queue_percent = args->queue_percentage & 0xFF;
278 /* bit 8-15 are repurposed to be PM4 target XCC */
279 q_properties->pm4_target_xcc = (args->queue_percentage >> 8) & 0xFF;
280 q_properties->priority = args->queue_priority;
281 q_properties->queue_address = args->ring_base_address;
282 q_properties->queue_size = args->ring_size;
283 if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL)
284 q_properties->metadata_queue_size = args->metadata_ring_size;
285
286 q_properties->read_ptr = (void __user *)args->read_pointer_address;
287 q_properties->write_ptr = (void __user *)args->write_pointer_address;
288 q_properties->eop_ring_buffer_address = args->eop_buffer_address;
289 q_properties->eop_ring_buffer_size = args->eop_buffer_size;
290 q_properties->ctx_save_restore_area_address =
291 args->ctx_save_restore_address;
292 q_properties->ctx_save_restore_area_size = args->ctx_save_restore_size;
293 q_properties->ctl_stack_size = args->ctl_stack_size;
294 q_properties->sdma_engine_id = args->sdma_engine_id;
295 if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE ||
296 args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL)
297 q_properties->type = KFD_QUEUE_TYPE_COMPUTE;
298 else if (args->queue_type == KFD_IOC_QUEUE_TYPE_SDMA)
299 q_properties->type = KFD_QUEUE_TYPE_SDMA;
300 else if (args->queue_type == KFD_IOC_QUEUE_TYPE_SDMA_XGMI)
301 q_properties->type = KFD_QUEUE_TYPE_SDMA_XGMI;
302 else if (args->queue_type == KFD_IOC_QUEUE_TYPE_SDMA_BY_ENG_ID)
303 q_properties->type = KFD_QUEUE_TYPE_SDMA_BY_ENG_ID;
304 else
305 return -ENOTSUPP;
306
307 if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL)
308 q_properties->format = KFD_QUEUE_FORMAT_AQL;
309 else
310 q_properties->format = KFD_QUEUE_FORMAT_PM4;
311
312 pr_debug("Queue Percentage: %d, %d\n",
313 q_properties->queue_percent, args->queue_percentage);
314
315 pr_debug("Queue Priority: %d, %d\n",
316 q_properties->priority, args->queue_priority);
317
318 pr_debug("Queue Address: 0x%llX, 0x%llX\n",
319 q_properties->queue_address, args->ring_base_address);
320
321 pr_debug("Queue Size: 0x%llX, %u\n",
322 q_properties->queue_size, args->ring_size);
323
324 pr_debug("Queue r/w Pointers: %px, %px\n",
325 q_properties->read_ptr,
326 q_properties->write_ptr);
327
328 pr_debug("Queue Format: %d\n", q_properties->format);
329
330 pr_debug("Queue EOP: 0x%llX\n", q_properties->eop_ring_buffer_address);
331
332 pr_debug("Queue CTX save area: 0x%llX\n",
333 q_properties->ctx_save_restore_area_address);
334
335 return 0;
336 }
337
kfd_ioctl_create_queue(struct file * filep,struct kfd_process * p,void * data)338 static int kfd_ioctl_create_queue(struct file *filep, struct kfd_process *p,
339 void *data)
340 {
341 struct kfd_ioctl_create_queue_args *args = data;
342 struct kfd_node *dev;
343 int err = 0;
344 unsigned int queue_id;
345 struct kfd_process_device *pdd;
346 struct queue_properties q_properties;
347 uint32_t doorbell_offset_in_process = 0;
348
349 memset(&q_properties, 0, sizeof(struct queue_properties));
350
351 pr_debug("Creating queue ioctl\n");
352
353 err = set_queue_properties_from_user(&q_properties, args);
354 if (err)
355 return err;
356
357 pr_debug("Looking for gpu id 0x%x\n", args->gpu_id);
358
359 mutex_lock(&p->mutex);
360
361 pdd = kfd_process_device_data_by_id(p, args->gpu_id);
362 if (!pdd) {
363 pr_debug("Could not find gpu id 0x%x\n", args->gpu_id);
364 err = -EINVAL;
365 goto err_pdd;
366 }
367 dev = pdd->dev;
368
369 pdd = kfd_bind_process_to_device(dev, p);
370 if (IS_ERR(pdd)) {
371 err = -ESRCH;
372 goto err_bind_process;
373 }
374
375 if (q_properties.type == KFD_QUEUE_TYPE_SDMA_BY_ENG_ID) {
376 int max_sdma_eng_id = kfd_get_num_sdma_engines(dev) +
377 kfd_get_num_xgmi_sdma_engines(dev) - 1;
378
379 if (q_properties.sdma_engine_id > max_sdma_eng_id) {
380 err = -EINVAL;
381 pr_err("sdma_engine_id %i exceeds maximum id of %i\n",
382 q_properties.sdma_engine_id, max_sdma_eng_id);
383 goto err_sdma_engine_id;
384 }
385 }
386
387 if (!pdd->qpd.proc_doorbells) {
388 err = kfd_alloc_process_doorbells(dev->kfd, pdd);
389 if (err) {
390 pr_debug("failed to allocate process doorbells\n");
391 goto err_bind_process;
392 }
393 }
394
395 err = kfd_queue_acquire_buffers(pdd, &q_properties);
396 if (err) {
397 pr_debug("failed to acquire user queue buffers\n");
398 goto err_acquire_queue_buf;
399 }
400
401 pr_debug("Creating queue for process pid %d on gpu 0x%x\n",
402 p->lead_thread->pid,
403 dev->id);
404
405 err = pqm_create_queue(&p->pqm, dev, &q_properties, &queue_id,
406 NULL, NULL, NULL, &doorbell_offset_in_process);
407 if (err != 0)
408 goto err_create_queue;
409
410 args->queue_id = queue_id;
411
412
413 /* Return gpu_id as doorbell offset for mmap usage */
414 args->doorbell_offset = KFD_MMAP_TYPE_DOORBELL;
415 args->doorbell_offset |= KFD_MMAP_GPU_ID(args->gpu_id);
416 if (KFD_IS_SOC15(dev))
417 /* On SOC15 ASICs, include the doorbell offset within the
418 * process doorbell frame, which is 2 pages.
419 */
420 args->doorbell_offset |= doorbell_offset_in_process;
421
422 mutex_unlock(&p->mutex);
423
424 pr_debug("Queue id %d was created successfully\n", args->queue_id);
425
426 pr_debug("Ring buffer address == 0x%016llX\n",
427 args->ring_base_address);
428
429 pr_debug("Read ptr address == 0x%016llX\n",
430 args->read_pointer_address);
431
432 pr_debug("Write ptr address == 0x%016llX\n",
433 args->write_pointer_address);
434
435 kfd_dbg_ev_raise(KFD_EC_MASK(EC_QUEUE_NEW), p, dev, queue_id, false, NULL, 0);
436 return 0;
437
438 err_create_queue:
439 kfd_queue_unref_bo_vas(pdd, &q_properties);
440 kfd_queue_release_buffers(pdd, &q_properties);
441 err_acquire_queue_buf:
442 err_sdma_engine_id:
443 err_bind_process:
444 err_pdd:
445 mutex_unlock(&p->mutex);
446 return err;
447 }
448
kfd_ioctl_destroy_queue(struct file * filp,struct kfd_process * p,void * data)449 static int kfd_ioctl_destroy_queue(struct file *filp, struct kfd_process *p,
450 void *data)
451 {
452 int retval;
453 struct kfd_ioctl_destroy_queue_args *args = data;
454
455 pr_debug("Destroying queue id %d for process pid %d\n",
456 args->queue_id,
457 p->lead_thread->pid);
458
459 mutex_lock(&p->mutex);
460
461 retval = pqm_destroy_queue(&p->pqm, args->queue_id);
462
463 mutex_unlock(&p->mutex);
464 return retval;
465 }
466
kfd_ioctl_update_queue(struct file * filp,struct kfd_process * p,void * data)467 static int kfd_ioctl_update_queue(struct file *filp, struct kfd_process *p,
468 void *data)
469 {
470 int retval;
471 struct kfd_ioctl_update_queue_args *args = data;
472 struct queue_properties properties;
473
474 /*
475 * Repurpose queue percentage to accommodate new features:
476 * bit 0-7: queue percentage
477 * bit 8-15: pm4_target_xcc
478 */
479 if ((args->queue_percentage & 0xFF) > KFD_MAX_QUEUE_PERCENTAGE) {
480 pr_err("Queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
481 return -EINVAL;
482 }
483
484 if (args->queue_priority > KFD_MAX_QUEUE_PRIORITY) {
485 pr_err("Queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
486 return -EINVAL;
487 }
488
489 if ((args->ring_base_address) &&
490 (!access_ok((const void __user *) args->ring_base_address,
491 sizeof(uint64_t)))) {
492 pr_err("Can't access ring base address\n");
493 return -EFAULT;
494 }
495
496 if (!is_power_of_2(args->ring_size) && (args->ring_size != 0)) {
497 pr_err("Ring size must be a power of 2 or 0\n");
498 return -EINVAL;
499 }
500
501 if (args->ring_size < KFD_MIN_QUEUE_RING_SIZE) {
502 args->ring_size = KFD_MIN_QUEUE_RING_SIZE;
503 pr_debug("Size lower. clamped to KFD_MIN_QUEUE_RING_SIZE");
504 }
505
506 properties.queue_address = args->ring_base_address;
507 properties.queue_size = args->ring_size;
508 properties.queue_percent = args->queue_percentage & 0xFF;
509 /* bit 8-15 are repurposed to be PM4 target XCC */
510 properties.pm4_target_xcc = (args->queue_percentage >> 8) & 0xFF;
511 properties.priority = args->queue_priority;
512
513 pr_debug("Updating queue id %d for process pid %d\n",
514 args->queue_id, p->lead_thread->pid);
515
516 mutex_lock(&p->mutex);
517
518 retval = pqm_update_queue_properties(&p->pqm, args->queue_id, &properties);
519
520 mutex_unlock(&p->mutex);
521
522 return retval;
523 }
524
kfd_ioctl_set_cu_mask(struct file * filp,struct kfd_process * p,void * data)525 static int kfd_ioctl_set_cu_mask(struct file *filp, struct kfd_process *p,
526 void *data)
527 {
528 int retval;
529 const int max_num_cus = 1024;
530 struct kfd_ioctl_set_cu_mask_args *args = data;
531 struct mqd_update_info minfo = {0};
532 uint32_t __user *cu_mask_ptr = (uint32_t __user *)args->cu_mask_ptr;
533 size_t cu_mask_size = sizeof(uint32_t) * (args->num_cu_mask / 32);
534
535 if ((args->num_cu_mask % 32) != 0) {
536 pr_debug("num_cu_mask 0x%x must be a multiple of 32",
537 args->num_cu_mask);
538 return -EINVAL;
539 }
540
541 minfo.cu_mask.count = args->num_cu_mask;
542 if (minfo.cu_mask.count == 0) {
543 pr_debug("CU mask cannot be 0");
544 return -EINVAL;
545 }
546
547 /* To prevent an unreasonably large CU mask size, set an arbitrary
548 * limit of max_num_cus bits. We can then just drop any CU mask bits
549 * past max_num_cus bits and just use the first max_num_cus bits.
550 */
551 if (minfo.cu_mask.count > max_num_cus) {
552 pr_debug("CU mask cannot be greater than 1024 bits");
553 minfo.cu_mask.count = max_num_cus;
554 cu_mask_size = sizeof(uint32_t) * (max_num_cus/32);
555 }
556
557 minfo.cu_mask.ptr = memdup_user(cu_mask_ptr, cu_mask_size);
558 if (IS_ERR(minfo.cu_mask.ptr)) {
559 pr_debug("Could not copy CU mask from userspace");
560 return PTR_ERR(minfo.cu_mask.ptr);
561 }
562
563 mutex_lock(&p->mutex);
564
565 retval = pqm_update_mqd(&p->pqm, args->queue_id, &minfo);
566
567 mutex_unlock(&p->mutex);
568
569 kfree(minfo.cu_mask.ptr);
570 return retval;
571 }
572
kfd_ioctl_get_queue_wave_state(struct file * filep,struct kfd_process * p,void * data)573 static int kfd_ioctl_get_queue_wave_state(struct file *filep,
574 struct kfd_process *p, void *data)
575 {
576 struct kfd_ioctl_get_queue_wave_state_args *args = data;
577 int r;
578
579 mutex_lock(&p->mutex);
580
581 r = pqm_get_wave_state(&p->pqm, args->queue_id,
582 (void __user *)args->ctl_stack_address,
583 &args->ctl_stack_used_size,
584 &args->save_area_used_size);
585
586 mutex_unlock(&p->mutex);
587
588 return r;
589 }
590
kfd_ioctl_set_memory_policy(struct file * filep,struct kfd_process * p,void * data)591 static int kfd_ioctl_set_memory_policy(struct file *filep,
592 struct kfd_process *p, void *data)
593 {
594 struct kfd_ioctl_set_memory_policy_args *args = data;
595 int err = 0;
596 struct kfd_process_device *pdd;
597 enum cache_policy default_policy, alternate_policy;
598
599 if (args->default_policy != KFD_IOC_CACHE_POLICY_COHERENT
600 && args->default_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) {
601 return -EINVAL;
602 }
603
604 if (args->alternate_policy != KFD_IOC_CACHE_POLICY_COHERENT
605 && args->alternate_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) {
606 return -EINVAL;
607 }
608
609 mutex_lock(&p->mutex);
610 pdd = kfd_process_device_data_by_id(p, args->gpu_id);
611 if (!pdd) {
612 pr_debug("Could not find gpu id 0x%x\n", args->gpu_id);
613 err = -EINVAL;
614 goto err_pdd;
615 }
616
617 pdd = kfd_bind_process_to_device(pdd->dev, p);
618 if (IS_ERR(pdd)) {
619 err = -ESRCH;
620 goto out;
621 }
622
623 default_policy = (args->default_policy == KFD_IOC_CACHE_POLICY_COHERENT)
624 ? cache_policy_coherent : cache_policy_noncoherent;
625
626 alternate_policy =
627 (args->alternate_policy == KFD_IOC_CACHE_POLICY_COHERENT)
628 ? cache_policy_coherent : cache_policy_noncoherent;
629
630 if (!pdd->dev->dqm->ops.set_cache_memory_policy(pdd->dev->dqm,
631 &pdd->qpd,
632 default_policy,
633 alternate_policy,
634 (void __user *)args->alternate_aperture_base,
635 args->alternate_aperture_size,
636 args->misc_process_flag))
637 err = -EINVAL;
638
639 out:
640 err_pdd:
641 mutex_unlock(&p->mutex);
642
643 return err;
644 }
645
kfd_ioctl_set_trap_handler(struct file * filep,struct kfd_process * p,void * data)646 static int kfd_ioctl_set_trap_handler(struct file *filep,
647 struct kfd_process *p, void *data)
648 {
649 struct kfd_ioctl_set_trap_handler_args *args = data;
650 int err = 0;
651 struct kfd_process_device *pdd;
652
653 mutex_lock(&p->mutex);
654
655 pdd = kfd_process_device_data_by_id(p, args->gpu_id);
656 if (!pdd) {
657 err = -EINVAL;
658 goto err_pdd;
659 }
660
661 pdd = kfd_bind_process_to_device(pdd->dev, p);
662 if (IS_ERR(pdd)) {
663 err = -ESRCH;
664 goto out;
665 }
666
667 kfd_process_set_trap_handler(&pdd->qpd, args->tba_addr, args->tma_addr);
668
669 out:
670 err_pdd:
671 mutex_unlock(&p->mutex);
672
673 return err;
674 }
675
kfd_ioctl_dbg_register(struct file * filep,struct kfd_process * p,void * data)676 static int kfd_ioctl_dbg_register(struct file *filep,
677 struct kfd_process *p, void *data)
678 {
679 return -EPERM;
680 }
681
kfd_ioctl_dbg_unregister(struct file * filep,struct kfd_process * p,void * data)682 static int kfd_ioctl_dbg_unregister(struct file *filep,
683 struct kfd_process *p, void *data)
684 {
685 return -EPERM;
686 }
687
kfd_ioctl_dbg_address_watch(struct file * filep,struct kfd_process * p,void * data)688 static int kfd_ioctl_dbg_address_watch(struct file *filep,
689 struct kfd_process *p, void *data)
690 {
691 return -EPERM;
692 }
693
694 /* Parse and generate fixed size data structure for wave control */
kfd_ioctl_dbg_wave_control(struct file * filep,struct kfd_process * p,void * data)695 static int kfd_ioctl_dbg_wave_control(struct file *filep,
696 struct kfd_process *p, void *data)
697 {
698 return -EPERM;
699 }
700
kfd_ioctl_get_clock_counters(struct file * filep,struct kfd_process * p,void * data)701 static int kfd_ioctl_get_clock_counters(struct file *filep,
702 struct kfd_process *p, void *data)
703 {
704 struct kfd_ioctl_get_clock_counters_args *args = data;
705 struct kfd_process_device *pdd;
706
707 mutex_lock(&p->mutex);
708 pdd = kfd_process_device_data_by_id(p, args->gpu_id);
709 mutex_unlock(&p->mutex);
710 if (pdd)
711 /* Reading GPU clock counter from KGD */
712 args->gpu_clock_counter = amdgpu_amdkfd_get_gpu_clock_counter(pdd->dev->adev);
713 else
714 /* Node without GPU resource */
715 args->gpu_clock_counter = 0;
716
717 /* No access to rdtsc. Using raw monotonic time */
718 args->cpu_clock_counter = ktime_get_raw_ns();
719 args->system_clock_counter = ktime_get_boottime_ns();
720
721 /* Since the counter is in nano-seconds we use 1GHz frequency */
722 args->system_clock_freq = 1000000000;
723
724 return 0;
725 }
726
727
kfd_ioctl_get_process_apertures(struct file * filp,struct kfd_process * p,void * data)728 static int kfd_ioctl_get_process_apertures(struct file *filp,
729 struct kfd_process *p, void *data)
730 {
731 struct kfd_ioctl_get_process_apertures_args *args = data;
732 struct kfd_process_device_apertures *pAperture;
733 int i;
734
735 dev_dbg(kfd_device, "get apertures for process pid %d", p->lead_thread->pid);
736
737 args->num_of_nodes = 0;
738
739 mutex_lock(&p->mutex);
740 /* Run over all pdd of the process */
741 for (i = 0; i < p->n_pdds; i++) {
742 struct kfd_process_device *pdd = p->pdds[i];
743
744 pAperture =
745 &args->process_apertures[args->num_of_nodes];
746 pAperture->gpu_id = pdd->dev->id;
747 pAperture->lds_base = pdd->lds_base;
748 pAperture->lds_limit = pdd->lds_limit;
749 pAperture->gpuvm_base = pdd->gpuvm_base;
750 pAperture->gpuvm_limit = pdd->gpuvm_limit;
751 pAperture->scratch_base = pdd->scratch_base;
752 pAperture->scratch_limit = pdd->scratch_limit;
753
754 dev_dbg(kfd_device,
755 "node id %u\n", args->num_of_nodes);
756 dev_dbg(kfd_device,
757 "gpu id %u\n", pdd->dev->id);
758 dev_dbg(kfd_device,
759 "lds_base %llX\n", pdd->lds_base);
760 dev_dbg(kfd_device,
761 "lds_limit %llX\n", pdd->lds_limit);
762 dev_dbg(kfd_device,
763 "gpuvm_base %llX\n", pdd->gpuvm_base);
764 dev_dbg(kfd_device,
765 "gpuvm_limit %llX\n", pdd->gpuvm_limit);
766 dev_dbg(kfd_device,
767 "scratch_base %llX\n", pdd->scratch_base);
768 dev_dbg(kfd_device,
769 "scratch_limit %llX\n", pdd->scratch_limit);
770
771 if (++args->num_of_nodes >= NUM_OF_SUPPORTED_GPUS)
772 break;
773 }
774 mutex_unlock(&p->mutex);
775
776 return 0;
777 }
778
kfd_ioctl_get_process_apertures_new(struct file * filp,struct kfd_process * p,void * data)779 static int kfd_ioctl_get_process_apertures_new(struct file *filp,
780 struct kfd_process *p, void *data)
781 {
782 struct kfd_ioctl_get_process_apertures_new_args *args = data;
783 struct kfd_process_device_apertures *pa;
784 int ret;
785 int i;
786
787 dev_dbg(kfd_device, "get apertures for process pid %d",
788 p->lead_thread->pid);
789
790 if (args->num_of_nodes == 0) {
791 /* Return number of nodes, so that user space can alloacate
792 * sufficient memory
793 */
794 mutex_lock(&p->mutex);
795 args->num_of_nodes = p->n_pdds;
796 goto out_unlock;
797 }
798
799 if (args->num_of_nodes > kfd_topology_get_num_devices())
800 return -EINVAL;
801
802 /* Fill in process-aperture information for all available
803 * nodes, but not more than args->num_of_nodes as that is
804 * the amount of memory allocated by user
805 */
806 pa = kzalloc_objs(struct kfd_process_device_apertures,
807 args->num_of_nodes);
808 if (!pa)
809 return -ENOMEM;
810
811 mutex_lock(&p->mutex);
812
813 if (!p->n_pdds) {
814 args->num_of_nodes = 0;
815 kfree(pa);
816 goto out_unlock;
817 }
818
819 /* Run over all pdd of the process */
820 for (i = 0; i < min(p->n_pdds, args->num_of_nodes); i++) {
821 struct kfd_process_device *pdd = p->pdds[i];
822
823 pa[i].gpu_id = pdd->dev->id;
824 pa[i].lds_base = pdd->lds_base;
825 pa[i].lds_limit = pdd->lds_limit;
826 pa[i].gpuvm_base = pdd->gpuvm_base;
827 pa[i].gpuvm_limit = pdd->gpuvm_limit;
828 pa[i].scratch_base = pdd->scratch_base;
829 pa[i].scratch_limit = pdd->scratch_limit;
830
831 dev_dbg(kfd_device,
832 "gpu id %u\n", pdd->dev->id);
833 dev_dbg(kfd_device,
834 "lds_base %llX\n", pdd->lds_base);
835 dev_dbg(kfd_device,
836 "lds_limit %llX\n", pdd->lds_limit);
837 dev_dbg(kfd_device,
838 "gpuvm_base %llX\n", pdd->gpuvm_base);
839 dev_dbg(kfd_device,
840 "gpuvm_limit %llX\n", pdd->gpuvm_limit);
841 dev_dbg(kfd_device,
842 "scratch_base %llX\n", pdd->scratch_base);
843 dev_dbg(kfd_device,
844 "scratch_limit %llX\n", pdd->scratch_limit);
845 }
846 mutex_unlock(&p->mutex);
847
848 args->num_of_nodes = i;
849 ret = copy_to_user(
850 (void __user *)args->kfd_process_device_apertures_ptr,
851 pa,
852 (i * sizeof(struct kfd_process_device_apertures)));
853 kfree(pa);
854 return ret ? -EFAULT : 0;
855
856 out_unlock:
857 mutex_unlock(&p->mutex);
858 return 0;
859 }
860
kfd_ioctl_create_event(struct file * filp,struct kfd_process * p,void * data)861 static int kfd_ioctl_create_event(struct file *filp, struct kfd_process *p,
862 void *data)
863 {
864 struct kfd_ioctl_create_event_args *args = data;
865 int err;
866
867 /* For dGPUs the event page is allocated in user mode. The
868 * handle is passed to KFD with the first call to this IOCTL
869 * through the event_page_offset field.
870 */
871 if (args->event_page_offset) {
872 mutex_lock(&p->mutex);
873 err = kfd_kmap_event_page(p, args->event_page_offset);
874 mutex_unlock(&p->mutex);
875 if (err)
876 return err;
877 }
878
879 err = kfd_event_create(filp, p, args->event_type,
880 args->auto_reset != 0, args->node_id,
881 &args->event_id, &args->event_trigger_data,
882 &args->event_page_offset,
883 &args->event_slot_index);
884
885 pr_debug("Created event (id:0x%08x) (%s)\n", args->event_id, __func__);
886 return err;
887 }
888
kfd_ioctl_destroy_event(struct file * filp,struct kfd_process * p,void * data)889 static int kfd_ioctl_destroy_event(struct file *filp, struct kfd_process *p,
890 void *data)
891 {
892 struct kfd_ioctl_destroy_event_args *args = data;
893
894 return kfd_event_destroy(p, args->event_id);
895 }
896
kfd_ioctl_set_event(struct file * filp,struct kfd_process * p,void * data)897 static int kfd_ioctl_set_event(struct file *filp, struct kfd_process *p,
898 void *data)
899 {
900 struct kfd_ioctl_set_event_args *args = data;
901
902 return kfd_set_event(p, args->event_id);
903 }
904
kfd_ioctl_reset_event(struct file * filp,struct kfd_process * p,void * data)905 static int kfd_ioctl_reset_event(struct file *filp, struct kfd_process *p,
906 void *data)
907 {
908 struct kfd_ioctl_reset_event_args *args = data;
909
910 return kfd_reset_event(p, args->event_id);
911 }
912
kfd_ioctl_wait_events(struct file * filp,struct kfd_process * p,void * data)913 static int kfd_ioctl_wait_events(struct file *filp, struct kfd_process *p,
914 void *data)
915 {
916 struct kfd_ioctl_wait_events_args *args = data;
917
918 return kfd_wait_on_events(p, args->num_events,
919 (void __user *)args->events_ptr,
920 (args->wait_for_all != 0),
921 &args->timeout, &args->wait_result);
922 }
kfd_ioctl_set_scratch_backing_va(struct file * filep,struct kfd_process * p,void * data)923 static int kfd_ioctl_set_scratch_backing_va(struct file *filep,
924 struct kfd_process *p, void *data)
925 {
926 struct kfd_ioctl_set_scratch_backing_va_args *args = data;
927 struct kfd_process_device *pdd;
928 struct kfd_node *dev;
929 long err;
930
931 mutex_lock(&p->mutex);
932 pdd = kfd_process_device_data_by_id(p, args->gpu_id);
933 if (!pdd) {
934 err = -EINVAL;
935 goto err_pdd;
936 }
937 dev = pdd->dev;
938
939 pdd = kfd_bind_process_to_device(dev, p);
940 if (IS_ERR(pdd)) {
941 err = PTR_ERR(pdd);
942 goto bind_process_to_device_fail;
943 }
944
945 pdd->qpd.sh_hidden_private_base = args->va_addr;
946
947 mutex_unlock(&p->mutex);
948
949 if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS &&
950 pdd->qpd.vmid != 0 && dev->kfd2kgd->set_scratch_backing_va)
951 dev->kfd2kgd->set_scratch_backing_va(
952 dev->adev, args->va_addr, pdd->qpd.vmid);
953
954 return 0;
955
956 bind_process_to_device_fail:
957 err_pdd:
958 mutex_unlock(&p->mutex);
959 return err;
960 }
961
kfd_ioctl_get_tile_config(struct file * filep,struct kfd_process * p,void * data)962 static int kfd_ioctl_get_tile_config(struct file *filep,
963 struct kfd_process *p, void *data)
964 {
965 struct kfd_ioctl_get_tile_config_args *args = data;
966 struct kfd_process_device *pdd;
967 struct tile_config config;
968 int err = 0;
969
970 mutex_lock(&p->mutex);
971 pdd = kfd_process_device_data_by_id(p, args->gpu_id);
972 mutex_unlock(&p->mutex);
973 if (!pdd)
974 return -EINVAL;
975
976 amdgpu_amdkfd_get_tile_config(pdd->dev->adev, &config);
977
978 args->gb_addr_config = config.gb_addr_config;
979 args->num_banks = config.num_banks;
980 args->num_ranks = config.num_ranks;
981
982 if (args->num_tile_configs > config.num_tile_configs)
983 args->num_tile_configs = config.num_tile_configs;
984 err = copy_to_user((void __user *)args->tile_config_ptr,
985 config.tile_config_ptr,
986 args->num_tile_configs * sizeof(uint32_t));
987 if (err) {
988 args->num_tile_configs = 0;
989 return -EFAULT;
990 }
991
992 if (args->num_macro_tile_configs > config.num_macro_tile_configs)
993 args->num_macro_tile_configs =
994 config.num_macro_tile_configs;
995 err = copy_to_user((void __user *)args->macro_tile_config_ptr,
996 config.macro_tile_config_ptr,
997 args->num_macro_tile_configs * sizeof(uint32_t));
998 if (err) {
999 args->num_macro_tile_configs = 0;
1000 return -EFAULT;
1001 }
1002
1003 return 0;
1004 }
1005
kfd_ioctl_acquire_vm(struct file * filep,struct kfd_process * p,void * data)1006 static int kfd_ioctl_acquire_vm(struct file *filep, struct kfd_process *p,
1007 void *data)
1008 {
1009 struct kfd_ioctl_acquire_vm_args *args = data;
1010 struct kfd_process_device *pdd;
1011 struct file *drm_file;
1012 int ret;
1013
1014 drm_file = fget(args->drm_fd);
1015 if (!drm_file)
1016 return -EINVAL;
1017
1018 mutex_lock(&p->mutex);
1019 pdd = kfd_process_device_data_by_id(p, args->gpu_id);
1020 if (!pdd) {
1021 ret = -EINVAL;
1022 goto err_pdd;
1023 }
1024
1025 if (pdd->drm_file) {
1026 ret = pdd->drm_file == drm_file ? 0 : -EBUSY;
1027 goto err_drm_file;
1028 }
1029
1030 ret = kfd_process_device_init_vm(pdd, drm_file);
1031 if (ret)
1032 goto err_unlock;
1033
1034 /* On success, the PDD keeps the drm_file reference */
1035 mutex_unlock(&p->mutex);
1036
1037 return 0;
1038
1039 err_unlock:
1040 err_pdd:
1041 err_drm_file:
1042 mutex_unlock(&p->mutex);
1043 fput(drm_file);
1044 return ret;
1045 }
1046
kfd_dev_is_large_bar(struct kfd_node * dev)1047 bool kfd_dev_is_large_bar(struct kfd_node *dev)
1048 {
1049 if (dev->kfd->adev->debug_largebar) {
1050 pr_debug("Simulate large-bar allocation on non large-bar machine\n");
1051 return true;
1052 }
1053
1054 if (dev->local_mem_info.local_mem_size_private == 0 &&
1055 dev->local_mem_info.local_mem_size_public > 0)
1056 return true;
1057
1058 if (dev->local_mem_info.local_mem_size_public == 0 &&
1059 dev->kfd->adev->gmc.is_app_apu) {
1060 pr_debug("APP APU, Consider like a large bar system\n");
1061 return true;
1062 }
1063
1064 return false;
1065 }
1066
kfd_ioctl_get_available_memory(struct file * filep,struct kfd_process * p,void * data)1067 static int kfd_ioctl_get_available_memory(struct file *filep,
1068 struct kfd_process *p, void *data)
1069 {
1070 struct kfd_ioctl_get_available_memory_args *args = data;
1071 struct kfd_process_device *pdd = kfd_lock_pdd_by_id(p, args->gpu_id);
1072
1073 if (!pdd)
1074 return -EINVAL;
1075 args->available = amdgpu_amdkfd_get_available_memory(pdd->dev->adev,
1076 pdd->dev->node_id);
1077 kfd_unlock_pdd(pdd);
1078 return 0;
1079 }
1080
kfd_ioctl_alloc_memory_of_gpu(struct file * filep,struct kfd_process * p,void * data)1081 static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep,
1082 struct kfd_process *p, void *data)
1083 {
1084 struct kfd_ioctl_alloc_memory_of_gpu_args *args = data;
1085 struct kfd_process_device *pdd;
1086 void *mem;
1087 struct kfd_node *dev;
1088 int idr_handle;
1089 long err;
1090 uint64_t offset = args->mmap_offset;
1091 uint32_t flags = args->flags;
1092
1093 if (args->size == 0)
1094 return -EINVAL;
1095
1096 if (p->context_id != KFD_CONTEXT_ID_PRIMARY && (flags & KFD_IOC_ALLOC_MEM_FLAGS_USERPTR)) {
1097 pr_debug("USERPTR is not supported on non-primary kfd_process\n");
1098
1099 return -EOPNOTSUPP;
1100 }
1101
1102 #if IS_ENABLED(CONFIG_HSA_AMD_SVM)
1103 /* Flush pending deferred work to avoid racing with deferred actions
1104 * from previous memory map changes (e.g. munmap).
1105 */
1106 svm_range_list_lock_and_flush_work(&p->svms, current->mm);
1107 mutex_lock(&p->svms.lock);
1108 mmap_write_unlock(current->mm);
1109
1110 /* Skip a special case that allocates VRAM without VA,
1111 * VA will be invalid of 0.
1112 */
1113 if (!(!args->va_addr && (flags & KFD_IOC_ALLOC_MEM_FLAGS_VRAM)) &&
1114 interval_tree_iter_first(&p->svms.objects,
1115 args->va_addr >> PAGE_SHIFT,
1116 (args->va_addr + args->size - 1) >> PAGE_SHIFT)) {
1117 pr_err("Address: 0x%llx already allocated by SVM\n",
1118 args->va_addr);
1119 mutex_unlock(&p->svms.lock);
1120 return -EADDRINUSE;
1121 }
1122
1123 /* When register user buffer check if it has been registered by svm by
1124 * buffer cpu virtual address.
1125 */
1126 if ((flags & KFD_IOC_ALLOC_MEM_FLAGS_USERPTR) &&
1127 interval_tree_iter_first(&p->svms.objects,
1128 args->mmap_offset >> PAGE_SHIFT,
1129 (args->mmap_offset + args->size - 1) >> PAGE_SHIFT)) {
1130 pr_err("User Buffer Address: 0x%llx already allocated by SVM\n",
1131 args->mmap_offset);
1132 mutex_unlock(&p->svms.lock);
1133 return -EADDRINUSE;
1134 }
1135
1136 mutex_unlock(&p->svms.lock);
1137 #endif
1138 mutex_lock(&p->mutex);
1139 pdd = kfd_process_device_data_by_id(p, args->gpu_id);
1140 if (!pdd) {
1141 err = -EINVAL;
1142 goto err_pdd;
1143 }
1144
1145 dev = pdd->dev;
1146
1147 if ((flags & KFD_IOC_ALLOC_MEM_FLAGS_PUBLIC) &&
1148 (flags & KFD_IOC_ALLOC_MEM_FLAGS_VRAM) &&
1149 !kfd_dev_is_large_bar(dev)) {
1150 pr_err("Alloc host visible vram on small bar is not allowed\n");
1151 err = -EINVAL;
1152 goto err_large_bar;
1153 }
1154
1155 pdd = kfd_bind_process_to_device(dev, p);
1156 if (IS_ERR(pdd)) {
1157 err = PTR_ERR(pdd);
1158 goto err_unlock;
1159 }
1160
1161 if (flags & KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL) {
1162 if (args->size != kfd_doorbell_process_slice(dev->kfd)) {
1163 err = -EINVAL;
1164 goto err_unlock;
1165 }
1166 offset = kfd_get_process_doorbells(pdd);
1167 if (!offset) {
1168 err = -ENOMEM;
1169 goto err_unlock;
1170 }
1171 } else if (flags & KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP) {
1172 if (args->size != PAGE_SIZE) {
1173 err = -EINVAL;
1174 goto err_unlock;
1175 }
1176 offset = dev->adev->rmmio_remap.bus_addr;
1177 if (!offset || (PAGE_SIZE > 4096)) {
1178 err = -ENOMEM;
1179 goto err_unlock;
1180 }
1181 }
1182
1183 err = amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
1184 dev->adev, args->va_addr, args->size,
1185 pdd->drm_priv, (struct kgd_mem **) &mem, &offset,
1186 flags, false);
1187
1188 if (err)
1189 goto err_unlock;
1190
1191 idr_handle = kfd_process_device_create_obj_handle(pdd, mem);
1192 if (idr_handle < 0) {
1193 err = -EFAULT;
1194 goto err_free;
1195 }
1196
1197 /* Update the VRAM usage count */
1198 if (flags & KFD_IOC_ALLOC_MEM_FLAGS_VRAM) {
1199 uint64_t size = args->size;
1200
1201 if (flags & KFD_IOC_ALLOC_MEM_FLAGS_AQL_QUEUE_MEM)
1202 size >>= 1;
1203 atomic64_add(PAGE_ALIGN(size), &pdd->vram_usage);
1204 }
1205
1206 mutex_unlock(&p->mutex);
1207
1208 args->handle = MAKE_HANDLE(args->gpu_id, idr_handle);
1209 args->mmap_offset = offset;
1210
1211 /* MMIO is mapped through kfd device
1212 * Generate a kfd mmap offset
1213 */
1214 if (flags & KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP)
1215 args->mmap_offset = KFD_MMAP_TYPE_MMIO
1216 | KFD_MMAP_GPU_ID(args->gpu_id);
1217
1218 return 0;
1219
1220 err_free:
1221 amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->adev, (struct kgd_mem *)mem,
1222 pdd->drm_priv, NULL);
1223 err_unlock:
1224 err_pdd:
1225 err_large_bar:
1226 mutex_unlock(&p->mutex);
1227 return err;
1228 }
1229
kfd_ioctl_free_memory_of_gpu(struct file * filep,struct kfd_process * p,void * data)1230 static int kfd_ioctl_free_memory_of_gpu(struct file *filep,
1231 struct kfd_process *p, void *data)
1232 {
1233 struct kfd_ioctl_free_memory_of_gpu_args *args = data;
1234 struct kfd_process_device *pdd;
1235 void *mem;
1236 int ret;
1237 uint64_t size = 0;
1238
1239 mutex_lock(&p->mutex);
1240 /*
1241 * Safeguard to prevent user space from freeing signal BO.
1242 * It will be freed at process termination.
1243 */
1244 if (p->signal_handle && (p->signal_handle == args->handle)) {
1245 pr_err("Free signal BO is not allowed\n");
1246 ret = -EPERM;
1247 goto err_unlock;
1248 }
1249
1250 pdd = kfd_process_device_data_by_id(p, GET_GPU_ID(args->handle));
1251 if (!pdd) {
1252 pr_err("Process device data doesn't exist\n");
1253 ret = -EINVAL;
1254 goto err_pdd;
1255 }
1256
1257 mem = kfd_process_device_translate_handle(
1258 pdd, GET_IDR_HANDLE(args->handle));
1259 if (!mem) {
1260 ret = -EINVAL;
1261 goto err_unlock;
1262 }
1263
1264 ret = amdgpu_amdkfd_gpuvm_free_memory_of_gpu(pdd->dev->adev,
1265 (struct kgd_mem *)mem, pdd->drm_priv, &size);
1266
1267 /* If freeing the buffer failed, leave the handle in place for
1268 * clean-up during process tear-down.
1269 */
1270 if (!ret)
1271 kfd_process_device_remove_obj_handle(
1272 pdd, GET_IDR_HANDLE(args->handle));
1273
1274 atomic64_sub(size, &pdd->vram_usage);
1275
1276 err_unlock:
1277 err_pdd:
1278 mutex_unlock(&p->mutex);
1279 return ret;
1280 }
1281
kfd_ioctl_map_memory_to_gpu(struct file * filep,struct kfd_process * p,void * data)1282 static int kfd_ioctl_map_memory_to_gpu(struct file *filep,
1283 struct kfd_process *p, void *data)
1284 {
1285 struct kfd_ioctl_map_memory_to_gpu_args *args = data;
1286 struct kfd_process_device *pdd, *peer_pdd;
1287 void *mem;
1288 struct kfd_node *dev;
1289 long err = 0;
1290 int i;
1291 uint32_t *devices_arr = NULL;
1292
1293 if (!args->n_devices) {
1294 pr_debug("Device IDs array empty\n");
1295 return -EINVAL;
1296 }
1297 if (args->n_success > args->n_devices) {
1298 pr_debug("n_success exceeds n_devices\n");
1299 return -EINVAL;
1300 }
1301
1302 devices_arr = memdup_array_user((void *)args->device_ids_array_ptr,
1303 args->n_devices, sizeof(*devices_arr));
1304
1305 if (IS_ERR(devices_arr))
1306 return PTR_ERR(devices_arr);
1307
1308 mutex_lock(&p->mutex);
1309 pdd = kfd_process_device_data_by_id(p, GET_GPU_ID(args->handle));
1310 if (!pdd) {
1311 err = -EINVAL;
1312 goto get_process_device_data_failed;
1313 }
1314 dev = pdd->dev;
1315
1316 pdd = kfd_bind_process_to_device(dev, p);
1317 if (IS_ERR(pdd)) {
1318 err = PTR_ERR(pdd);
1319 goto bind_process_to_device_failed;
1320 }
1321
1322 mem = kfd_process_device_translate_handle(pdd,
1323 GET_IDR_HANDLE(args->handle));
1324 if (!mem) {
1325 err = -ENOMEM;
1326 goto get_mem_obj_from_handle_failed;
1327 }
1328
1329 for (i = args->n_success; i < args->n_devices; i++) {
1330 peer_pdd = kfd_process_device_data_by_id(p, devices_arr[i]);
1331 if (!peer_pdd) {
1332 pr_debug("Getting device by id failed for 0x%x\n",
1333 devices_arr[i]);
1334 err = -EINVAL;
1335 goto get_mem_obj_from_handle_failed;
1336 }
1337
1338 peer_pdd = kfd_bind_process_to_device(peer_pdd->dev, p);
1339 if (IS_ERR(peer_pdd)) {
1340 err = PTR_ERR(peer_pdd);
1341 goto get_mem_obj_from_handle_failed;
1342 }
1343
1344 err = amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
1345 peer_pdd->dev->adev, (struct kgd_mem *)mem,
1346 peer_pdd->drm_priv);
1347 if (err) {
1348 struct pci_dev *pdev = peer_pdd->dev->adev->pdev;
1349
1350 dev_err(dev->adev->dev,
1351 "Failed to map peer:%04x:%02x:%02x.%d mem_domain:%d\n",
1352 pci_domain_nr(pdev->bus),
1353 pdev->bus->number,
1354 PCI_SLOT(pdev->devfn),
1355 PCI_FUNC(pdev->devfn),
1356 ((struct kgd_mem *)mem)->domain);
1357 goto map_memory_to_gpu_failed;
1358 }
1359 args->n_success = i+1;
1360 }
1361
1362 err = amdgpu_amdkfd_gpuvm_sync_memory(dev->adev, (struct kgd_mem *) mem, true);
1363 if (err) {
1364 pr_debug("Sync memory failed, wait interrupted by user signal\n");
1365 goto sync_memory_failed;
1366 }
1367
1368 mutex_unlock(&p->mutex);
1369
1370 /* Flush TLBs after waiting for the page table updates to complete */
1371 for (i = 0; i < args->n_devices; i++) {
1372 peer_pdd = kfd_process_device_data_by_id(p, devices_arr[i]);
1373 if (WARN_ON_ONCE(!peer_pdd))
1374 continue;
1375 kfd_flush_tlb(peer_pdd);
1376 }
1377 kfree(devices_arr);
1378
1379 return err;
1380
1381 get_process_device_data_failed:
1382 bind_process_to_device_failed:
1383 get_mem_obj_from_handle_failed:
1384 map_memory_to_gpu_failed:
1385 sync_memory_failed:
1386 mutex_unlock(&p->mutex);
1387 kfree(devices_arr);
1388
1389 return err;
1390 }
1391
kfd_ioctl_unmap_memory_from_gpu(struct file * filep,struct kfd_process * p,void * data)1392 static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
1393 struct kfd_process *p, void *data)
1394 {
1395 struct kfd_ioctl_unmap_memory_from_gpu_args *args = data;
1396 struct kfd_process_device *pdd, *peer_pdd;
1397 void *mem;
1398 long err = 0;
1399 uint32_t *devices_arr = NULL, i;
1400 bool flush_tlb;
1401
1402 if (!args->n_devices) {
1403 pr_debug("Device IDs array empty\n");
1404 return -EINVAL;
1405 }
1406 if (args->n_success > args->n_devices) {
1407 pr_debug("n_success exceeds n_devices\n");
1408 return -EINVAL;
1409 }
1410
1411 devices_arr = memdup_array_user((void *)args->device_ids_array_ptr,
1412 args->n_devices, sizeof(*devices_arr));
1413
1414 if (IS_ERR(devices_arr))
1415 return PTR_ERR(devices_arr);
1416
1417 mutex_lock(&p->mutex);
1418 pdd = kfd_process_device_data_by_id(p, GET_GPU_ID(args->handle));
1419 if (!pdd) {
1420 err = -EINVAL;
1421 goto bind_process_to_device_failed;
1422 }
1423
1424 mem = kfd_process_device_translate_handle(pdd,
1425 GET_IDR_HANDLE(args->handle));
1426 if (!mem) {
1427 err = -ENOMEM;
1428 goto get_mem_obj_from_handle_failed;
1429 }
1430
1431 for (i = args->n_success; i < args->n_devices; i++) {
1432 peer_pdd = kfd_process_device_data_by_id(p, devices_arr[i]);
1433 if (!peer_pdd) {
1434 err = -EINVAL;
1435 goto get_mem_obj_from_handle_failed;
1436 }
1437 err = amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
1438 peer_pdd->dev->adev, (struct kgd_mem *)mem, peer_pdd->drm_priv);
1439 if (err) {
1440 pr_debug("Failed to unmap from gpu %d/%d\n", i, args->n_devices);
1441 goto unmap_memory_from_gpu_failed;
1442 }
1443 args->n_success = i+1;
1444 }
1445
1446 flush_tlb = kfd_flush_tlb_after_unmap(pdd->dev->kfd);
1447 if (flush_tlb) {
1448 err = amdgpu_amdkfd_gpuvm_sync_memory(pdd->dev->adev,
1449 (struct kgd_mem *) mem, true);
1450 if (err) {
1451 pr_debug("Sync memory failed, wait interrupted by user signal\n");
1452 goto sync_memory_failed;
1453 }
1454 }
1455
1456 /* Flush TLBs after waiting for the page table updates to complete */
1457 for (i = 0; i < args->n_devices; i++) {
1458 peer_pdd = kfd_process_device_data_by_id(p, devices_arr[i]);
1459 if (WARN_ON_ONCE(!peer_pdd))
1460 continue;
1461 if (flush_tlb)
1462 kfd_flush_tlb(peer_pdd);
1463
1464 /* Remove dma mapping after tlb flush to avoid IO_PAGE_FAULT */
1465 err = amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem, peer_pdd->drm_priv);
1466 if (err)
1467 goto sync_memory_failed;
1468 }
1469
1470 mutex_unlock(&p->mutex);
1471
1472 kfree(devices_arr);
1473
1474 return 0;
1475
1476 bind_process_to_device_failed:
1477 get_mem_obj_from_handle_failed:
1478 unmap_memory_from_gpu_failed:
1479 sync_memory_failed:
1480 mutex_unlock(&p->mutex);
1481 kfree(devices_arr);
1482 return err;
1483 }
1484
kfd_ioctl_alloc_queue_gws(struct file * filep,struct kfd_process * p,void * data)1485 static int kfd_ioctl_alloc_queue_gws(struct file *filep,
1486 struct kfd_process *p, void *data)
1487 {
1488 int retval;
1489 struct kfd_ioctl_alloc_queue_gws_args *args = data;
1490 struct queue *q;
1491 struct kfd_node *dev;
1492
1493 mutex_lock(&p->mutex);
1494 q = pqm_get_user_queue(&p->pqm, args->queue_id);
1495
1496 if (q) {
1497 dev = q->device;
1498 } else {
1499 retval = -EINVAL;
1500 goto out_unlock;
1501 }
1502
1503 if (!dev->gws) {
1504 retval = -ENODEV;
1505 goto out_unlock;
1506 }
1507
1508 if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
1509 retval = -ENODEV;
1510 goto out_unlock;
1511 }
1512
1513 if (p->debug_trap_enabled && (!kfd_dbg_has_gws_support(dev) ||
1514 kfd_dbg_has_cwsr_workaround(dev))) {
1515 retval = -EBUSY;
1516 goto out_unlock;
1517 }
1518
1519 retval = pqm_set_gws(&p->pqm, args->queue_id, args->num_gws ? dev->gws : NULL);
1520 mutex_unlock(&p->mutex);
1521
1522 args->first_gws = 0;
1523 return retval;
1524
1525 out_unlock:
1526 mutex_unlock(&p->mutex);
1527 return retval;
1528 }
1529
kfd_ioctl_get_dmabuf_info(struct file * filep,struct kfd_process * p,void * data)1530 static int kfd_ioctl_get_dmabuf_info(struct file *filep,
1531 struct kfd_process *p, void *data)
1532 {
1533 struct kfd_ioctl_get_dmabuf_info_args *args = data;
1534 struct kfd_node *dev = NULL;
1535 struct amdgpu_device *dmabuf_adev;
1536 void *metadata_buffer = NULL;
1537 uint32_t flags;
1538 int8_t xcp_id;
1539 unsigned int i;
1540 int r;
1541
1542 /* Find a KFD GPU device that supports the get_dmabuf_info query */
1543 for (i = 0; kfd_topology_enum_kfd_devices(i, &dev) == 0; i++)
1544 if (dev && !kfd_devcgroup_check_permission(dev))
1545 break;
1546 if (!dev)
1547 return -EINVAL;
1548
1549 /* Get dmabuf info from KGD */
1550 r = amdgpu_amdkfd_get_dmabuf_info(dev->adev, args->dmabuf_fd,
1551 &dmabuf_adev, &args->size,
1552 &metadata_buffer, args->metadata_size,
1553 &args->metadata_size, &flags, &xcp_id);
1554 if (r)
1555 goto exit;
1556
1557 if (xcp_id >= 0)
1558 args->gpu_id = dmabuf_adev->kfd.dev->nodes[xcp_id]->id;
1559 else
1560 args->gpu_id = dev->id;
1561 args->flags = flags;
1562
1563 /* Copy metadata buffer to user mode */
1564 if (metadata_buffer && args->metadata_ptr) {
1565 r = copy_to_user((void __user *)args->metadata_ptr,
1566 metadata_buffer, args->metadata_size);
1567 if (r != 0)
1568 r = -EFAULT;
1569 }
1570
1571 exit:
1572 kfree(metadata_buffer);
1573
1574 return r;
1575 }
1576
kfd_ioctl_import_dmabuf(struct file * filep,struct kfd_process * p,void * data)1577 static int kfd_ioctl_import_dmabuf(struct file *filep,
1578 struct kfd_process *p, void *data)
1579 {
1580 struct kfd_ioctl_import_dmabuf_args *args = data;
1581 struct kfd_process_device *pdd;
1582 int idr_handle;
1583 uint64_t size;
1584 void *mem;
1585 int r;
1586
1587 mutex_lock(&p->mutex);
1588 pdd = kfd_process_device_data_by_id(p, args->gpu_id);
1589 if (!pdd) {
1590 r = -EINVAL;
1591 goto err_unlock;
1592 }
1593
1594 pdd = kfd_bind_process_to_device(pdd->dev, p);
1595 if (IS_ERR(pdd)) {
1596 r = PTR_ERR(pdd);
1597 goto err_unlock;
1598 }
1599
1600 r = amdgpu_amdkfd_gpuvm_import_dmabuf_fd(pdd->dev->adev, args->dmabuf_fd,
1601 args->va_addr, pdd->drm_priv,
1602 (struct kgd_mem **)&mem, &size,
1603 NULL);
1604 if (r)
1605 goto err_unlock;
1606
1607 idr_handle = kfd_process_device_create_obj_handle(pdd, mem);
1608 if (idr_handle < 0) {
1609 r = -EFAULT;
1610 goto err_free;
1611 }
1612
1613 mutex_unlock(&p->mutex);
1614
1615 args->handle = MAKE_HANDLE(args->gpu_id, idr_handle);
1616
1617 return 0;
1618
1619 err_free:
1620 amdgpu_amdkfd_gpuvm_free_memory_of_gpu(pdd->dev->adev, (struct kgd_mem *)mem,
1621 pdd->drm_priv, NULL);
1622 err_unlock:
1623 mutex_unlock(&p->mutex);
1624 return r;
1625 }
1626
kfd_ioctl_export_dmabuf(struct file * filep,struct kfd_process * p,void * data)1627 static int kfd_ioctl_export_dmabuf(struct file *filep,
1628 struct kfd_process *p, void *data)
1629 {
1630 struct kfd_ioctl_export_dmabuf_args *args = data;
1631 struct kfd_process_device *pdd;
1632 struct dma_buf *dmabuf;
1633 struct kfd_node *dev;
1634 void *mem;
1635 int ret = 0;
1636
1637 dev = kfd_device_by_id(GET_GPU_ID(args->handle));
1638 if (!dev)
1639 return -EINVAL;
1640
1641 mutex_lock(&p->mutex);
1642
1643 pdd = kfd_get_process_device_data(dev, p);
1644 if (!pdd) {
1645 ret = -EINVAL;
1646 goto err_unlock;
1647 }
1648
1649 mem = kfd_process_device_translate_handle(pdd,
1650 GET_IDR_HANDLE(args->handle));
1651 if (!mem) {
1652 ret = -EINVAL;
1653 goto err_unlock;
1654 }
1655
1656 ret = amdgpu_amdkfd_gpuvm_export_dmabuf(mem, &dmabuf);
1657 mutex_unlock(&p->mutex);
1658 if (ret)
1659 goto err_out;
1660
1661 ret = dma_buf_fd(dmabuf, args->flags);
1662 if (ret < 0) {
1663 dma_buf_put(dmabuf);
1664 goto err_out;
1665 }
1666 /* dma_buf_fd assigns the reference count to the fd, no need to
1667 * put the reference here.
1668 */
1669 args->dmabuf_fd = ret;
1670
1671 return 0;
1672
1673 err_unlock:
1674 mutex_unlock(&p->mutex);
1675 err_out:
1676 return ret;
1677 }
1678
1679 /* Handle requests for watching SMI events */
kfd_ioctl_smi_events(struct file * filep,struct kfd_process * p,void * data)1680 static int kfd_ioctl_smi_events(struct file *filep,
1681 struct kfd_process *p, void *data)
1682 {
1683 struct kfd_ioctl_smi_events_args *args = data;
1684 struct kfd_process_device *pdd;
1685
1686 mutex_lock(&p->mutex);
1687
1688 pdd = kfd_process_device_data_by_id(p, args->gpuid);
1689 mutex_unlock(&p->mutex);
1690 if (!pdd)
1691 return -EINVAL;
1692
1693 return kfd_smi_event_open(pdd->dev, &args->anon_fd);
1694 }
1695
kfd_ioctl_svm_validate(void * kdata,unsigned int usize)1696 static int kfd_ioctl_svm_validate(void *kdata, unsigned int usize)
1697 {
1698 struct kfd_ioctl_svm_args *args = kdata;
1699 size_t expected = struct_size(args, attrs, args->nattr);
1700
1701 if (expected == SIZE_MAX || usize < expected)
1702 return -EINVAL;
1703 return 0;
1704 }
1705
1706 #if IS_ENABLED(CONFIG_HSA_AMD_SVM)
1707
kfd_ioctl_set_xnack_mode(struct file * filep,struct kfd_process * p,void * data)1708 static int kfd_ioctl_set_xnack_mode(struct file *filep,
1709 struct kfd_process *p, void *data)
1710 {
1711 struct kfd_ioctl_set_xnack_mode_args *args = data;
1712 int r = 0;
1713
1714 mutex_lock(&p->mutex);
1715 if (args->xnack_enabled >= 0) {
1716 if (!list_empty(&p->pqm.queues)) {
1717 pr_debug("Process has user queues running\n");
1718 r = -EBUSY;
1719 goto out_unlock;
1720 }
1721
1722 if (p->xnack_enabled == args->xnack_enabled)
1723 goto out_unlock;
1724
1725 if (args->xnack_enabled && !kfd_process_xnack_mode(p, true)) {
1726 r = -EPERM;
1727 goto out_unlock;
1728 }
1729
1730 r = svm_range_switch_xnack_reserve_mem(p, args->xnack_enabled);
1731 } else {
1732 args->xnack_enabled = p->xnack_enabled;
1733 }
1734
1735 out_unlock:
1736 mutex_unlock(&p->mutex);
1737
1738 return r;
1739 }
1740
kfd_ioctl_svm(struct file * filep,struct kfd_process * p,void * data)1741 static int kfd_ioctl_svm(struct file *filep, struct kfd_process *p, void *data)
1742 {
1743 struct kfd_ioctl_svm_args *args = data;
1744 int r = 0;
1745
1746 if (p->context_id != KFD_CONTEXT_ID_PRIMARY) {
1747 pr_debug("SVM ioctl not supported on non-primary kfd process\n");
1748
1749 return -EOPNOTSUPP;
1750 }
1751
1752 pr_debug("start 0x%llx size 0x%llx op 0x%x nattr 0x%x\n",
1753 args->start_addr, args->size, args->op, args->nattr);
1754
1755 if ((args->start_addr & ~PAGE_MASK) || (args->size & ~PAGE_MASK))
1756 return -EINVAL;
1757 if (!args->start_addr || !args->size)
1758 return -EINVAL;
1759
1760 r = svm_ioctl(p, args->op, args->start_addr, args->size, args->nattr,
1761 args->attrs);
1762
1763 return r;
1764 }
1765 #else
kfd_ioctl_set_xnack_mode(struct file * filep,struct kfd_process * p,void * data)1766 static int kfd_ioctl_set_xnack_mode(struct file *filep,
1767 struct kfd_process *p, void *data)
1768 {
1769 return -EPERM;
1770 }
kfd_ioctl_svm(struct file * filep,struct kfd_process * p,void * data)1771 static int kfd_ioctl_svm(struct file *filep, struct kfd_process *p, void *data)
1772 {
1773 return -EPERM;
1774 }
1775 #endif
1776
kfd_ptl_control(struct kfd_process_device * pdd,bool enable)1777 static int kfd_ptl_control(struct kfd_process_device *pdd, bool enable)
1778 {
1779 struct amdgpu_device *adev = pdd->dev->adev;
1780 struct amdgpu_ptl *ptl = &adev->psp.ptl;
1781 enum amdgpu_ptl_fmt pref_format1 = ptl->fmt1;
1782 enum amdgpu_ptl_fmt pref_format2 = ptl->fmt2;
1783 uint32_t ptl_state = enable ? 1 : 0;
1784 int ret;
1785
1786 if (!ptl->hw_supported)
1787 return -EOPNOTSUPP;
1788
1789 if (!pdd->dev->kfd2kgd || !pdd->dev->kfd2kgd->ptl_ctrl)
1790 return -EOPNOTSUPP;
1791
1792 ret = pdd->dev->kfd2kgd->ptl_ctrl(adev, PSP_PTL_PERF_MON_SET,
1793 &ptl_state,
1794 &pref_format1,
1795 &pref_format2);
1796
1797 return ret;
1798 }
1799
kfd_ptl_disable_request(struct kfd_process_device * pdd,struct kfd_process * p)1800 int kfd_ptl_disable_request(struct kfd_process_device *pdd,
1801 struct kfd_process *p)
1802 {
1803 struct amdgpu_device *adev = pdd->dev->adev;
1804 struct amdgpu_ptl *ptl = &adev->psp.ptl;
1805 int ret = 0;
1806
1807 mutex_lock(&ptl->mutex);
1808
1809 if (pdd->ptl_disable_req)
1810 goto out;
1811
1812 if (atomic_inc_return(&ptl->disable_ref) == 1) {
1813 ret = kfd_ptl_control(pdd, false);
1814 if (ret) {
1815 atomic_dec(&ptl->disable_ref);
1816 dev_warn(pdd->dev->adev->dev,
1817 "failed to disable PTL\n");
1818 goto out;
1819 }
1820 }
1821 set_bit(AMDGPU_PTL_DISABLE_PROFILER, ptl->disable_bitmap);
1822 pdd->ptl_disable_req = true;
1823
1824 out:
1825 mutex_unlock(&ptl->mutex);
1826 return ret;
1827 }
1828
kfd_ptl_disable_release(struct kfd_process_device * pdd,struct kfd_process * p)1829 int kfd_ptl_disable_release(struct kfd_process_device *pdd,
1830 struct kfd_process *p)
1831 {
1832 struct amdgpu_device *adev = pdd->dev->adev;
1833 struct amdgpu_ptl *ptl = &adev->psp.ptl;
1834 int ret = 0;
1835
1836 mutex_lock(&ptl->mutex);
1837
1838 if (!pdd->ptl_disable_req)
1839 goto out;
1840
1841 if (atomic_dec_return(&ptl->disable_ref) == 0) {
1842 clear_bit(AMDGPU_PTL_DISABLE_PROFILER, ptl->disable_bitmap);
1843 ret = kfd_ptl_control(pdd, true);
1844 if (ret) {
1845 atomic_inc(&ptl->disable_ref);
1846 set_bit(AMDGPU_PTL_DISABLE_PROFILER, ptl->disable_bitmap);
1847 dev_warn(adev->dev, "Failed to enable PTL on release: %d\n", ret);
1848 goto out;
1849 }
1850 }
1851 pdd->ptl_disable_req = false;
1852
1853 out:
1854 mutex_unlock(&ptl->mutex);
1855 return ret;
1856 }
1857
kfd_profiler_ptl_control(struct kfd_process * p,struct kfd_ioctl_ptl_control * args)1858 static int kfd_profiler_ptl_control(struct kfd_process *p,
1859 struct kfd_ioctl_ptl_control *args)
1860 {
1861 struct kfd_process_device *pdd;
1862 int ret;
1863
1864 mutex_lock(&p->mutex);
1865 pdd = kfd_process_device_data_by_id(p, args->gpu_id);
1866 mutex_unlock(&p->mutex);
1867
1868 if (!pdd || !pdd->dev || !pdd->dev->kfd)
1869 return -EINVAL;
1870
1871 if (args->enable == 0)
1872 ret = kfd_ptl_disable_request(pdd, p);
1873 else
1874 ret = kfd_ptl_disable_release(pdd, p);
1875
1876 return ret;
1877 }
1878
criu_checkpoint_process(struct kfd_process * p,uint8_t __user * user_priv_data,uint64_t * priv_offset)1879 static int criu_checkpoint_process(struct kfd_process *p,
1880 uint8_t __user *user_priv_data,
1881 uint64_t *priv_offset)
1882 {
1883 struct kfd_criu_process_priv_data process_priv;
1884 int ret;
1885
1886 memset(&process_priv, 0, sizeof(process_priv));
1887
1888 process_priv.version = KFD_CRIU_PRIV_VERSION;
1889 /* For CR, we don't consider negative xnack mode which is used for
1890 * querying without changing it, here 0 simply means disabled and 1
1891 * means enabled so retry for finding a valid PTE.
1892 */
1893 process_priv.xnack_mode = p->xnack_enabled ? 1 : 0;
1894
1895 ret = copy_to_user(user_priv_data + *priv_offset,
1896 &process_priv, sizeof(process_priv));
1897
1898 if (ret) {
1899 pr_err("Failed to copy process information to user\n");
1900 ret = -EFAULT;
1901 }
1902
1903 *priv_offset += sizeof(process_priv);
1904 return ret;
1905 }
1906
criu_checkpoint_devices(struct kfd_process * p,uint32_t num_devices,uint8_t __user * user_addr,uint8_t __user * user_priv_data,uint64_t * priv_offset)1907 static int criu_checkpoint_devices(struct kfd_process *p,
1908 uint32_t num_devices,
1909 uint8_t __user *user_addr,
1910 uint8_t __user *user_priv_data,
1911 uint64_t *priv_offset)
1912 {
1913 struct kfd_criu_device_priv_data *device_priv = NULL;
1914 struct kfd_criu_device_bucket *device_buckets = NULL;
1915 int ret = 0, i;
1916
1917 device_buckets = kvzalloc(num_devices * sizeof(*device_buckets), GFP_KERNEL);
1918 if (!device_buckets) {
1919 ret = -ENOMEM;
1920 goto exit;
1921 }
1922
1923 device_priv = kvzalloc(num_devices * sizeof(*device_priv), GFP_KERNEL);
1924 if (!device_priv) {
1925 ret = -ENOMEM;
1926 goto exit;
1927 }
1928
1929 for (i = 0; i < num_devices; i++) {
1930 struct kfd_process_device *pdd = p->pdds[i];
1931
1932 device_buckets[i].user_gpu_id = pdd->user_gpu_id;
1933 device_buckets[i].actual_gpu_id = pdd->dev->id;
1934
1935 /*
1936 * priv_data does not contain useful information for now and is reserved for
1937 * future use, so we do not set its contents.
1938 */
1939 }
1940
1941 ret = copy_to_user(user_addr, device_buckets, num_devices * sizeof(*device_buckets));
1942 if (ret) {
1943 pr_err("Failed to copy device information to user\n");
1944 ret = -EFAULT;
1945 goto exit;
1946 }
1947
1948 ret = copy_to_user(user_priv_data + *priv_offset,
1949 device_priv,
1950 num_devices * sizeof(*device_priv));
1951 if (ret) {
1952 pr_err("Failed to copy device information to user\n");
1953 ret = -EFAULT;
1954 }
1955 *priv_offset += num_devices * sizeof(*device_priv);
1956
1957 exit:
1958 kvfree(device_buckets);
1959 kvfree(device_priv);
1960 return ret;
1961 }
1962
get_process_num_bos(struct kfd_process * p)1963 static uint32_t get_process_num_bos(struct kfd_process *p)
1964 {
1965 uint32_t num_of_bos = 0;
1966 int i;
1967
1968 /* Run over all PDDs of the process */
1969 for (i = 0; i < p->n_pdds; i++) {
1970 struct kfd_process_device *pdd = p->pdds[i];
1971 void *mem;
1972 int id;
1973
1974 idr_for_each_entry(&pdd->alloc_idr, mem, id) {
1975 struct kgd_mem *kgd_mem = (struct kgd_mem *)mem;
1976
1977 if (!kgd_mem->va || kgd_mem->va > pdd->gpuvm_base)
1978 num_of_bos++;
1979 }
1980 }
1981 return num_of_bos;
1982 }
1983
criu_get_prime_handle(struct kgd_mem * mem,int flags,u32 * shared_fd,struct file ** file)1984 static int criu_get_prime_handle(struct kgd_mem *mem,
1985 int flags, u32 *shared_fd,
1986 struct file **file)
1987 {
1988 struct dma_buf *dmabuf;
1989 int ret;
1990
1991 ret = amdgpu_amdkfd_gpuvm_export_dmabuf(mem, &dmabuf);
1992 if (ret) {
1993 pr_err("dmabuf export failed for the BO\n");
1994 return ret;
1995 }
1996
1997 ret = get_unused_fd_flags(flags);
1998 if (ret < 0) {
1999 pr_err("dmabuf create fd failed, ret:%d\n", ret);
2000 goto out_free_dmabuf;
2001 }
2002
2003 *shared_fd = ret;
2004 *file = dmabuf->file;
2005 return 0;
2006
2007 out_free_dmabuf:
2008 dma_buf_put(dmabuf);
2009 return ret;
2010 }
2011
commit_files(struct file ** files,struct kfd_criu_bo_bucket * bo_buckets,unsigned int count,int err)2012 static void commit_files(struct file **files,
2013 struct kfd_criu_bo_bucket *bo_buckets,
2014 unsigned int count,
2015 int err)
2016 {
2017 while (count--) {
2018 struct file *file = files[count];
2019
2020 if (!file)
2021 continue;
2022 if (err) {
2023 fput(file);
2024 put_unused_fd(bo_buckets[count].dmabuf_fd);
2025 } else {
2026 fd_install(bo_buckets[count].dmabuf_fd, file);
2027 }
2028 }
2029 }
2030
criu_checkpoint_bos(struct kfd_process * p,uint32_t num_bos,uint8_t __user * user_bos,uint8_t __user * user_priv_data,uint64_t * priv_offset)2031 static int criu_checkpoint_bos(struct kfd_process *p,
2032 uint32_t num_bos,
2033 uint8_t __user *user_bos,
2034 uint8_t __user *user_priv_data,
2035 uint64_t *priv_offset)
2036 {
2037 struct kfd_criu_bo_bucket *bo_buckets;
2038 struct kfd_criu_bo_priv_data *bo_privs;
2039 struct file **files = NULL;
2040 int ret = 0, pdd_index, bo_index = 0, id;
2041 void *mem;
2042
2043 bo_buckets = kvzalloc(num_bos * sizeof(*bo_buckets), GFP_KERNEL);
2044 if (!bo_buckets)
2045 return -ENOMEM;
2046
2047 bo_privs = kvzalloc(num_bos * sizeof(*bo_privs), GFP_KERNEL);
2048 if (!bo_privs) {
2049 ret = -ENOMEM;
2050 goto exit;
2051 }
2052
2053 files = kvzalloc(num_bos * sizeof(struct file *), GFP_KERNEL);
2054 if (!files) {
2055 ret = -ENOMEM;
2056 goto exit;
2057 }
2058
2059 for (pdd_index = 0; pdd_index < p->n_pdds; pdd_index++) {
2060 struct kfd_process_device *pdd = p->pdds[pdd_index];
2061 struct amdgpu_bo *dumper_bo;
2062 struct kgd_mem *kgd_mem;
2063
2064 idr_for_each_entry(&pdd->alloc_idr, mem, id) {
2065 struct kfd_criu_bo_bucket *bo_bucket;
2066 struct kfd_criu_bo_priv_data *bo_priv;
2067 int i, dev_idx = 0;
2068
2069 kgd_mem = (struct kgd_mem *)mem;
2070 dumper_bo = kgd_mem->bo;
2071
2072 /* Skip checkpointing BOs that are used for Trap handler
2073 * code and state. Currently, these BOs have a VA that
2074 * is less GPUVM Base
2075 */
2076 if (kgd_mem->va && kgd_mem->va <= pdd->gpuvm_base)
2077 continue;
2078
2079 bo_bucket = &bo_buckets[bo_index];
2080 bo_priv = &bo_privs[bo_index];
2081
2082 bo_bucket->gpu_id = pdd->user_gpu_id;
2083 bo_bucket->addr = (uint64_t)kgd_mem->va;
2084 bo_bucket->size = amdgpu_bo_size(dumper_bo);
2085 bo_bucket->alloc_flags = (uint32_t)kgd_mem->alloc_flags;
2086 bo_priv->idr_handle = id;
2087
2088 if (bo_bucket->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_USERPTR) {
2089 ret = amdgpu_ttm_tt_get_userptr(&dumper_bo->tbo,
2090 &bo_priv->user_addr);
2091 if (ret) {
2092 pr_err("Failed to obtain user address for user-pointer bo\n");
2093 goto exit;
2094 }
2095 }
2096 if (bo_bucket->alloc_flags
2097 & (KFD_IOC_ALLOC_MEM_FLAGS_VRAM | KFD_IOC_ALLOC_MEM_FLAGS_GTT)) {
2098 ret = criu_get_prime_handle(kgd_mem,
2099 bo_bucket->alloc_flags &
2100 KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE ? DRM_RDWR : 0,
2101 &bo_bucket->dmabuf_fd, &files[bo_index]);
2102 if (ret)
2103 goto exit;
2104 } else {
2105 bo_bucket->dmabuf_fd = KFD_INVALID_FD;
2106 }
2107
2108 if (bo_bucket->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL)
2109 bo_bucket->offset = KFD_MMAP_TYPE_DOORBELL |
2110 KFD_MMAP_GPU_ID(pdd->dev->id);
2111 else if (bo_bucket->alloc_flags &
2112 KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP)
2113 bo_bucket->offset = KFD_MMAP_TYPE_MMIO |
2114 KFD_MMAP_GPU_ID(pdd->dev->id);
2115 else
2116 bo_bucket->offset = amdgpu_bo_mmap_offset(dumper_bo);
2117
2118 for (i = 0; i < p->n_pdds; i++) {
2119 if (amdgpu_amdkfd_bo_mapped_to_dev(p->pdds[i]->drm_priv, kgd_mem))
2120 bo_priv->mapped_gpuids[dev_idx++] = p->pdds[i]->user_gpu_id;
2121 }
2122
2123 pr_debug("bo_size = 0x%llx, bo_addr = 0x%llx bo_offset = 0x%llx\n"
2124 "gpu_id = 0x%x alloc_flags = 0x%x idr_handle = 0x%x",
2125 bo_bucket->size,
2126 bo_bucket->addr,
2127 bo_bucket->offset,
2128 bo_bucket->gpu_id,
2129 bo_bucket->alloc_flags,
2130 bo_priv->idr_handle);
2131 bo_index++;
2132 }
2133 }
2134
2135 ret = copy_to_user(user_bos, bo_buckets, num_bos * sizeof(*bo_buckets));
2136 if (ret) {
2137 pr_err("Failed to copy BO information to user\n");
2138 ret = -EFAULT;
2139 goto exit;
2140 }
2141
2142 ret = copy_to_user(user_priv_data + *priv_offset, bo_privs, num_bos * sizeof(*bo_privs));
2143 if (ret) {
2144 pr_err("Failed to copy BO priv information to user\n");
2145 ret = -EFAULT;
2146 goto exit;
2147 }
2148
2149 *priv_offset += num_bos * sizeof(*bo_privs);
2150
2151 exit:
2152 commit_files(files, bo_buckets, bo_index, ret);
2153 kvfree(files);
2154 kvfree(bo_buckets);
2155 kvfree(bo_privs);
2156 return ret;
2157 }
2158
criu_get_process_object_info(struct kfd_process * p,uint32_t * num_devices,uint32_t * num_bos,uint32_t * num_objects,uint64_t * objs_priv_size)2159 static int criu_get_process_object_info(struct kfd_process *p,
2160 uint32_t *num_devices,
2161 uint32_t *num_bos,
2162 uint32_t *num_objects,
2163 uint64_t *objs_priv_size)
2164 {
2165 uint64_t queues_priv_data_size, svm_priv_data_size, priv_size;
2166 uint32_t num_queues, num_events, num_svm_ranges;
2167 int ret;
2168
2169 *num_devices = p->n_pdds;
2170 *num_bos = get_process_num_bos(p);
2171
2172 ret = kfd_process_get_queue_info(p, &num_queues, &queues_priv_data_size);
2173 if (ret)
2174 return ret;
2175
2176 num_events = kfd_get_num_events(p);
2177
2178 svm_range_get_info(p, &num_svm_ranges, &svm_priv_data_size);
2179
2180 *num_objects = num_queues + num_events + num_svm_ranges;
2181
2182 if (objs_priv_size) {
2183 priv_size = sizeof(struct kfd_criu_process_priv_data);
2184 priv_size += *num_devices * sizeof(struct kfd_criu_device_priv_data);
2185 priv_size += *num_bos * sizeof(struct kfd_criu_bo_priv_data);
2186 priv_size += queues_priv_data_size;
2187 priv_size += num_events * sizeof(struct kfd_criu_event_priv_data);
2188 priv_size += svm_priv_data_size;
2189 *objs_priv_size = priv_size;
2190 }
2191 return 0;
2192 }
2193
criu_checkpoint(struct file * filep,struct kfd_process * p,struct kfd_ioctl_criu_args * args)2194 static int criu_checkpoint(struct file *filep,
2195 struct kfd_process *p,
2196 struct kfd_ioctl_criu_args *args)
2197 {
2198 int ret;
2199 uint32_t num_devices, num_bos, num_objects;
2200 uint64_t priv_size, priv_offset = 0, bo_priv_offset;
2201
2202 if (!args->devices || !args->bos || !args->priv_data)
2203 return -EINVAL;
2204
2205 mutex_lock(&p->mutex);
2206
2207 if (!p->n_pdds) {
2208 pr_err("No pdd for given process\n");
2209 ret = -ENODEV;
2210 goto exit_unlock;
2211 }
2212
2213 /* Confirm all process queues are evicted */
2214 if (!p->queues_paused) {
2215 pr_err("Cannot dump process when queues are not in evicted state\n");
2216 /* CRIU plugin did not call op PROCESS_INFO before checkpointing */
2217 ret = -EINVAL;
2218 goto exit_unlock;
2219 }
2220
2221 ret = criu_get_process_object_info(p, &num_devices, &num_bos, &num_objects, &priv_size);
2222 if (ret)
2223 goto exit_unlock;
2224
2225 if (num_devices != args->num_devices ||
2226 num_bos != args->num_bos ||
2227 num_objects != args->num_objects ||
2228 priv_size != args->priv_data_size) {
2229
2230 ret = -EINVAL;
2231 goto exit_unlock;
2232 }
2233
2234 /* each function will store private data inside priv_data and adjust priv_offset */
2235 ret = criu_checkpoint_process(p, (uint8_t __user *)args->priv_data, &priv_offset);
2236 if (ret)
2237 goto exit_unlock;
2238
2239 ret = criu_checkpoint_devices(p, num_devices, (uint8_t __user *)args->devices,
2240 (uint8_t __user *)args->priv_data, &priv_offset);
2241 if (ret)
2242 goto exit_unlock;
2243
2244 /* Leave room for BOs in the private data. They need to be restored
2245 * before events, but we checkpoint them last to simplify the error
2246 * handling.
2247 */
2248 bo_priv_offset = priv_offset;
2249 priv_offset += num_bos * sizeof(struct kfd_criu_bo_priv_data);
2250
2251 if (num_objects) {
2252 ret = kfd_criu_checkpoint_queues(p, (uint8_t __user *)args->priv_data,
2253 &priv_offset);
2254 if (ret)
2255 goto exit_unlock;
2256
2257 ret = kfd_criu_checkpoint_events(p, (uint8_t __user *)args->priv_data,
2258 &priv_offset);
2259 if (ret)
2260 goto exit_unlock;
2261
2262 ret = kfd_criu_checkpoint_svm(p, (uint8_t __user *)args->priv_data, &priv_offset);
2263 if (ret)
2264 goto exit_unlock;
2265 }
2266
2267 /* This must be the last thing in this function that can fail.
2268 * Otherwise we leak dmabuf file descriptors.
2269 */
2270 ret = criu_checkpoint_bos(p, num_bos, (uint8_t __user *)args->bos,
2271 (uint8_t __user *)args->priv_data, &bo_priv_offset);
2272
2273 exit_unlock:
2274 mutex_unlock(&p->mutex);
2275 if (ret)
2276 pr_err("Failed to dump CRIU ret:%d\n", ret);
2277 else
2278 pr_debug("CRIU dump ret:%d\n", ret);
2279
2280 return ret;
2281 }
2282
criu_restore_process(struct kfd_process * p,struct kfd_ioctl_criu_args * args,uint64_t * priv_offset,uint64_t max_priv_data_size)2283 static int criu_restore_process(struct kfd_process *p,
2284 struct kfd_ioctl_criu_args *args,
2285 uint64_t *priv_offset,
2286 uint64_t max_priv_data_size)
2287 {
2288 int ret = 0;
2289 struct kfd_criu_process_priv_data process_priv;
2290
2291 if (*priv_offset + sizeof(process_priv) > max_priv_data_size)
2292 return -EINVAL;
2293
2294 ret = copy_from_user(&process_priv,
2295 (void __user *)(args->priv_data + *priv_offset),
2296 sizeof(process_priv));
2297 if (ret) {
2298 pr_err("Failed to copy process private information from user\n");
2299 ret = -EFAULT;
2300 goto exit;
2301 }
2302 *priv_offset += sizeof(process_priv);
2303
2304 if (process_priv.version != KFD_CRIU_PRIV_VERSION) {
2305 pr_err("Invalid CRIU API version (checkpointed:%d current:%d)\n",
2306 process_priv.version, KFD_CRIU_PRIV_VERSION);
2307 return -EINVAL;
2308 }
2309
2310 pr_debug("Setting XNACK mode\n");
2311 if (process_priv.xnack_mode && !kfd_process_xnack_mode(p, true)) {
2312 pr_err("xnack mode cannot be set\n");
2313 ret = -EPERM;
2314 goto exit;
2315 } else {
2316 pr_debug("set xnack mode: %d\n", process_priv.xnack_mode);
2317 p->xnack_enabled = process_priv.xnack_mode;
2318 }
2319
2320 exit:
2321 return ret;
2322 }
2323
criu_restore_devices(struct kfd_process * p,struct kfd_ioctl_criu_args * args,uint64_t * priv_offset,uint64_t max_priv_data_size)2324 static int criu_restore_devices(struct kfd_process *p,
2325 struct kfd_ioctl_criu_args *args,
2326 uint64_t *priv_offset,
2327 uint64_t max_priv_data_size)
2328 {
2329 struct kfd_criu_device_bucket *device_buckets;
2330 struct kfd_criu_device_priv_data *device_privs;
2331 int ret = 0;
2332 uint32_t i;
2333
2334 if (args->num_devices != p->n_pdds)
2335 return -EINVAL;
2336
2337 if (*priv_offset + (args->num_devices * sizeof(*device_privs)) > max_priv_data_size)
2338 return -EINVAL;
2339
2340 device_buckets = memdup_array_user((void *)args->devices,
2341 args->num_devices, sizeof(*device_buckets));
2342
2343 if (IS_ERR(device_buckets))
2344 return PTR_ERR(device_buckets);
2345
2346 for (i = 0; i < args->num_devices; i++) {
2347 struct kfd_node *dev;
2348 struct kfd_process_device *pdd;
2349 struct file *drm_file;
2350
2351 /* device private data is not currently used */
2352
2353 if (!device_buckets[i].user_gpu_id) {
2354 pr_err("Invalid user gpu_id\n");
2355 ret = -EINVAL;
2356 goto exit;
2357 }
2358
2359 dev = kfd_device_by_id(device_buckets[i].actual_gpu_id);
2360 if (!dev) {
2361 pr_err("Failed to find device with gpu_id = %x\n",
2362 device_buckets[i].actual_gpu_id);
2363 ret = -EINVAL;
2364 goto exit;
2365 }
2366
2367 pdd = kfd_get_process_device_data(dev, p);
2368 if (!pdd) {
2369 pr_err("Failed to get pdd for gpu_id = %x\n",
2370 device_buckets[i].actual_gpu_id);
2371 ret = -EINVAL;
2372 goto exit;
2373 }
2374
2375 if (pdd->drm_file) {
2376 ret = -EINVAL;
2377 goto exit;
2378 }
2379 pdd->user_gpu_id = device_buckets[i].user_gpu_id;
2380
2381 drm_file = fget(device_buckets[i].drm_fd);
2382 if (!drm_file) {
2383 pr_err("Invalid render node file descriptor sent from plugin (%d)\n",
2384 device_buckets[i].drm_fd);
2385 ret = -EINVAL;
2386 goto exit;
2387 }
2388
2389 /* create the vm using render nodes for kfd pdd */
2390 if (kfd_process_device_init_vm(pdd, drm_file)) {
2391 pr_err("could not init vm for given pdd\n");
2392 /* On success, the PDD keeps the drm_file reference */
2393 fput(drm_file);
2394 ret = -EINVAL;
2395 goto exit;
2396 }
2397 /*
2398 * pdd now already has the vm bound to render node so below api won't create a new
2399 * exclusive kfd mapping but use existing one with renderDXXX but is still needed
2400 * for iommu v2 binding and runtime pm.
2401 */
2402 pdd = kfd_bind_process_to_device(dev, p);
2403 if (IS_ERR(pdd)) {
2404 ret = PTR_ERR(pdd);
2405 goto exit;
2406 }
2407
2408 if (!pdd->qpd.proc_doorbells) {
2409 ret = kfd_alloc_process_doorbells(dev->kfd, pdd);
2410 if (ret)
2411 goto exit;
2412 }
2413 }
2414
2415 /*
2416 * We are not copying device private data from user as we are not using the data for now,
2417 * but we still adjust for its private data.
2418 */
2419 *priv_offset += args->num_devices * sizeof(*device_privs);
2420
2421 exit:
2422 kfree(device_buckets);
2423 return ret;
2424 }
2425
criu_restore_memory_of_gpu(struct kfd_process_device * pdd,struct kfd_criu_bo_bucket * bo_bucket,struct kfd_criu_bo_priv_data * bo_priv,struct kgd_mem ** kgd_mem)2426 static int criu_restore_memory_of_gpu(struct kfd_process_device *pdd,
2427 struct kfd_criu_bo_bucket *bo_bucket,
2428 struct kfd_criu_bo_priv_data *bo_priv,
2429 struct kgd_mem **kgd_mem)
2430 {
2431 int idr_handle;
2432 int ret;
2433 const bool criu_resume = true;
2434 u64 offset;
2435
2436 if (bo_priv->idr_handle > INT_MAX)
2437 return -EINVAL;
2438
2439 if (bo_bucket->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL) {
2440 if (bo_bucket->size !=
2441 kfd_doorbell_process_slice(pdd->dev->kfd))
2442 return -EINVAL;
2443
2444 offset = kfd_get_process_doorbells(pdd);
2445 if (!offset)
2446 return -ENOMEM;
2447 } else if (bo_bucket->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP) {
2448 /* MMIO BOs need remapped bus address */
2449 if (bo_bucket->size != PAGE_SIZE) {
2450 pr_err("Invalid page size\n");
2451 return -EINVAL;
2452 }
2453 offset = pdd->dev->adev->rmmio_remap.bus_addr;
2454 if (!offset || (PAGE_SIZE > 4096)) {
2455 pr_err("amdgpu_amdkfd_get_mmio_remap_phys_addr failed\n");
2456 return -ENOMEM;
2457 }
2458 } else if (bo_bucket->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_USERPTR) {
2459 offset = bo_priv->user_addr;
2460 }
2461 /* Create the BO */
2462 ret = amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(pdd->dev->adev, bo_bucket->addr,
2463 bo_bucket->size, pdd->drm_priv, kgd_mem,
2464 &offset, bo_bucket->alloc_flags, criu_resume);
2465 if (ret) {
2466 pr_err("Could not create the BO\n");
2467 return ret;
2468 }
2469 pr_debug("New BO created: size:0x%llx addr:0x%llx offset:0x%llx\n",
2470 bo_bucket->size, bo_bucket->addr, offset);
2471
2472 /* Restore previous IDR handle */
2473 pr_debug("Restoring old IDR handle for the BO");
2474 idr_handle = idr_alloc(&pdd->alloc_idr, *kgd_mem, bo_priv->idr_handle,
2475 bo_priv->idr_handle + 1, GFP_KERNEL);
2476
2477 if (idr_handle < 0) {
2478 pr_err("Could not allocate idr\n");
2479 amdgpu_amdkfd_gpuvm_free_memory_of_gpu(pdd->dev->adev, *kgd_mem, pdd->drm_priv,
2480 NULL);
2481 return -ENOMEM;
2482 }
2483
2484 if (bo_bucket->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL)
2485 bo_bucket->restored_offset = KFD_MMAP_TYPE_DOORBELL | KFD_MMAP_GPU_ID(pdd->dev->id);
2486 if (bo_bucket->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP) {
2487 bo_bucket->restored_offset = KFD_MMAP_TYPE_MMIO | KFD_MMAP_GPU_ID(pdd->dev->id);
2488 } else if (bo_bucket->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_GTT) {
2489 bo_bucket->restored_offset = offset;
2490 } else if (bo_bucket->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_VRAM) {
2491 bo_bucket->restored_offset = offset;
2492 /* Update the VRAM usage count */
2493 atomic64_add(bo_bucket->size, &pdd->vram_usage);
2494 }
2495 return 0;
2496 }
2497
criu_restore_bo(struct kfd_process * p,struct kfd_criu_bo_bucket * bo_bucket,struct kfd_criu_bo_priv_data * bo_priv,struct file ** file)2498 static int criu_restore_bo(struct kfd_process *p,
2499 struct kfd_criu_bo_bucket *bo_bucket,
2500 struct kfd_criu_bo_priv_data *bo_priv,
2501 struct file **file)
2502 {
2503 struct kfd_process_device *pdd;
2504 struct kgd_mem *kgd_mem;
2505 int ret;
2506 int j;
2507
2508 pr_debug("Restoring BO size:0x%llx addr:0x%llx gpu_id:0x%x flags:0x%x idr_handle:0x%x\n",
2509 bo_bucket->size, bo_bucket->addr, bo_bucket->gpu_id, bo_bucket->alloc_flags,
2510 bo_priv->idr_handle);
2511
2512 pdd = kfd_process_device_data_by_id(p, bo_bucket->gpu_id);
2513 if (!pdd) {
2514 pr_err("Failed to get pdd\n");
2515 return -ENODEV;
2516 }
2517
2518 ret = criu_restore_memory_of_gpu(pdd, bo_bucket, bo_priv, &kgd_mem);
2519 if (ret)
2520 return ret;
2521
2522 /* now map these BOs to GPU/s */
2523 for (j = 0; j < p->n_pdds; j++) {
2524 struct kfd_node *peer;
2525 struct kfd_process_device *peer_pdd;
2526
2527 if (!bo_priv->mapped_gpuids[j])
2528 break;
2529
2530 peer_pdd = kfd_process_device_data_by_id(p, bo_priv->mapped_gpuids[j]);
2531 if (!peer_pdd)
2532 return -EINVAL;
2533
2534 peer = peer_pdd->dev;
2535
2536 peer_pdd = kfd_bind_process_to_device(peer, p);
2537 if (IS_ERR(peer_pdd))
2538 return PTR_ERR(peer_pdd);
2539
2540 ret = amdgpu_amdkfd_gpuvm_map_memory_to_gpu(peer->adev, kgd_mem,
2541 peer_pdd->drm_priv);
2542 if (ret) {
2543 pr_err("Failed to map to gpu %d/%d\n", j, p->n_pdds);
2544 return ret;
2545 }
2546 }
2547
2548 pr_debug("map memory was successful for the BO\n");
2549 /* create the dmabuf object and export the bo */
2550 if (bo_bucket->alloc_flags
2551 & (KFD_IOC_ALLOC_MEM_FLAGS_VRAM | KFD_IOC_ALLOC_MEM_FLAGS_GTT)) {
2552 ret = criu_get_prime_handle(kgd_mem, DRM_RDWR,
2553 &bo_bucket->dmabuf_fd, file);
2554 if (ret)
2555 return ret;
2556 } else {
2557 bo_bucket->dmabuf_fd = KFD_INVALID_FD;
2558 }
2559
2560 return 0;
2561 }
2562
criu_restore_bos(struct kfd_process * p,struct kfd_ioctl_criu_args * args,uint64_t * priv_offset,uint64_t max_priv_data_size)2563 static int criu_restore_bos(struct kfd_process *p,
2564 struct kfd_ioctl_criu_args *args,
2565 uint64_t *priv_offset,
2566 uint64_t max_priv_data_size)
2567 {
2568 struct kfd_criu_bo_bucket *bo_buckets = NULL;
2569 struct kfd_criu_bo_priv_data *bo_privs = NULL;
2570 struct file **files = NULL;
2571 int ret = 0;
2572 uint32_t i = 0;
2573
2574 if (*priv_offset + (args->num_bos * sizeof(*bo_privs)) > max_priv_data_size)
2575 return -EINVAL;
2576
2577 /* Prevent MMU notifications until stage-4 IOCTL (CRIU_RESUME) is received */
2578 amdgpu_amdkfd_block_mmu_notifications(p->kgd_process_info);
2579
2580 bo_buckets = kvmalloc_objs(*bo_buckets, args->num_bos);
2581 if (!bo_buckets)
2582 return -ENOMEM;
2583
2584 files = kvzalloc(args->num_bos * sizeof(struct file *), GFP_KERNEL);
2585 if (!files) {
2586 ret = -ENOMEM;
2587 goto exit;
2588 }
2589
2590 ret = copy_from_user(bo_buckets, (void __user *)args->bos,
2591 args->num_bos * sizeof(*bo_buckets));
2592 if (ret) {
2593 pr_err("Failed to copy BOs information from user\n");
2594 ret = -EFAULT;
2595 goto exit;
2596 }
2597
2598 bo_privs = kvmalloc_objs(*bo_privs, args->num_bos);
2599 if (!bo_privs) {
2600 ret = -ENOMEM;
2601 goto exit;
2602 }
2603
2604 ret = copy_from_user(bo_privs, (void __user *)args->priv_data + *priv_offset,
2605 args->num_bos * sizeof(*bo_privs));
2606 if (ret) {
2607 pr_err("Failed to copy BOs information from user\n");
2608 ret = -EFAULT;
2609 goto exit;
2610 }
2611 *priv_offset += args->num_bos * sizeof(*bo_privs);
2612
2613 /* Create and map new BOs */
2614 for (; i < args->num_bos; i++) {
2615 ret = criu_restore_bo(p, &bo_buckets[i], &bo_privs[i], &files[i]);
2616 if (ret) {
2617 pr_debug("Failed to restore BO[%d] ret%d\n", i, ret);
2618 goto exit;
2619 }
2620 } /* done */
2621
2622 /* Copy only the buckets back so user can read bo_buckets[N].restored_offset */
2623 ret = copy_to_user((void __user *)args->bos,
2624 bo_buckets,
2625 (args->num_bos * sizeof(*bo_buckets)));
2626 if (ret)
2627 ret = -EFAULT;
2628
2629 exit:
2630 commit_files(files, bo_buckets, i, ret);
2631 kvfree(files);
2632 kvfree(bo_buckets);
2633 kvfree(bo_privs);
2634 return ret;
2635 }
2636
criu_restore_objects(struct file * filep,struct kfd_process * p,struct kfd_ioctl_criu_args * args,uint64_t * priv_offset,uint64_t max_priv_data_size)2637 static int criu_restore_objects(struct file *filep,
2638 struct kfd_process *p,
2639 struct kfd_ioctl_criu_args *args,
2640 uint64_t *priv_offset,
2641 uint64_t max_priv_data_size)
2642 {
2643 int ret = 0;
2644 uint32_t i;
2645
2646 BUILD_BUG_ON(offsetof(struct kfd_criu_queue_priv_data, object_type));
2647 BUILD_BUG_ON(offsetof(struct kfd_criu_event_priv_data, object_type));
2648 BUILD_BUG_ON(offsetof(struct kfd_criu_svm_range_priv_data, object_type));
2649
2650 for (i = 0; i < args->num_objects; i++) {
2651 uint32_t object_type;
2652
2653 if (*priv_offset + sizeof(object_type) > max_priv_data_size) {
2654 pr_err("Invalid private data size\n");
2655 return -EINVAL;
2656 }
2657
2658 ret = get_user(object_type, (uint32_t __user *)(args->priv_data + *priv_offset));
2659 if (ret) {
2660 pr_err("Failed to copy private information from user\n");
2661 goto exit;
2662 }
2663
2664 switch (object_type) {
2665 case KFD_CRIU_OBJECT_TYPE_QUEUE:
2666 ret = kfd_criu_restore_queue(p, (uint8_t __user *)args->priv_data,
2667 priv_offset, max_priv_data_size);
2668 if (ret)
2669 goto exit;
2670 break;
2671 case KFD_CRIU_OBJECT_TYPE_EVENT:
2672 ret = kfd_criu_restore_event(filep, p, (uint8_t __user *)args->priv_data,
2673 priv_offset, max_priv_data_size);
2674 if (ret)
2675 goto exit;
2676 break;
2677 case KFD_CRIU_OBJECT_TYPE_SVM_RANGE:
2678 ret = kfd_criu_restore_svm(p, (uint8_t __user *)args->priv_data,
2679 priv_offset, max_priv_data_size);
2680 if (ret)
2681 goto exit;
2682 break;
2683 default:
2684 pr_err("Invalid object type:%u at index:%d\n", object_type, i);
2685 ret = -EINVAL;
2686 goto exit;
2687 }
2688 }
2689 exit:
2690 return ret;
2691 }
2692
criu_restore(struct file * filep,struct kfd_process * p,struct kfd_ioctl_criu_args * args)2693 static int criu_restore(struct file *filep,
2694 struct kfd_process *p,
2695 struct kfd_ioctl_criu_args *args)
2696 {
2697 uint64_t priv_offset = 0;
2698 int ret = 0;
2699
2700 pr_debug("CRIU restore (num_devices:%u num_bos:%u num_objects:%u priv_data_size:%llu)\n",
2701 args->num_devices, args->num_bos, args->num_objects, args->priv_data_size);
2702
2703 if ((args->num_bos > 0 && !args->bos) || !args->devices || !args->priv_data ||
2704 !args->priv_data_size || !args->num_devices)
2705 return -EINVAL;
2706
2707 mutex_lock(&p->mutex);
2708
2709 /*
2710 * Set the process to evicted state to avoid running any new queues before all the memory
2711 * mappings are ready.
2712 */
2713 ret = kfd_process_evict_queues(p, KFD_QUEUE_EVICTION_CRIU_RESTORE);
2714 if (ret)
2715 goto exit_unlock;
2716
2717 /* Each function will adjust priv_offset based on how many bytes they consumed */
2718 ret = criu_restore_process(p, args, &priv_offset, args->priv_data_size);
2719 if (ret)
2720 goto exit_unlock;
2721
2722 ret = criu_restore_devices(p, args, &priv_offset, args->priv_data_size);
2723 if (ret)
2724 goto exit_unlock;
2725
2726 ret = criu_restore_bos(p, args, &priv_offset, args->priv_data_size);
2727 if (ret)
2728 goto exit_unlock;
2729
2730 ret = criu_restore_objects(filep, p, args, &priv_offset, args->priv_data_size);
2731 if (ret)
2732 goto exit_unlock;
2733
2734 if (priv_offset != args->priv_data_size) {
2735 pr_err("Invalid private data size\n");
2736 ret = -EINVAL;
2737 }
2738
2739 exit_unlock:
2740 mutex_unlock(&p->mutex);
2741 if (ret)
2742 pr_err("Failed to restore CRIU ret:%d\n", ret);
2743 else
2744 pr_debug("CRIU restore successful\n");
2745
2746 return ret;
2747 }
2748
criu_unpause(struct file * filep,struct kfd_process * p,struct kfd_ioctl_criu_args * args)2749 static int criu_unpause(struct file *filep,
2750 struct kfd_process *p,
2751 struct kfd_ioctl_criu_args *args)
2752 {
2753 int ret;
2754
2755 mutex_lock(&p->mutex);
2756
2757 if (!p->queues_paused) {
2758 mutex_unlock(&p->mutex);
2759 return -EINVAL;
2760 }
2761
2762 ret = kfd_process_restore_queues(p);
2763 if (ret)
2764 pr_err("Failed to unpause queues ret:%d\n", ret);
2765 else
2766 p->queues_paused = false;
2767
2768 mutex_unlock(&p->mutex);
2769
2770 return ret;
2771 }
2772
criu_resume(struct file * filep,struct kfd_process * p,struct kfd_ioctl_criu_args * args)2773 static int criu_resume(struct file *filep,
2774 struct kfd_process *p,
2775 struct kfd_ioctl_criu_args *args)
2776 {
2777 struct kfd_process *target = NULL;
2778 struct pid *pid = NULL;
2779 int ret = 0;
2780
2781 pr_debug("Inside %s, target pid for criu restore: %d\n", __func__,
2782 args->pid);
2783
2784 pid = find_get_pid(args->pid);
2785 if (!pid) {
2786 pr_err("Cannot find pid info for %i\n", args->pid);
2787 return -ESRCH;
2788 }
2789
2790 pr_debug("calling kfd_lookup_process_by_pid\n");
2791 target = kfd_lookup_process_by_pid(pid);
2792
2793 put_pid(pid);
2794
2795 if (!target) {
2796 pr_debug("Cannot find process info for %i\n", args->pid);
2797 return -ESRCH;
2798 }
2799
2800 mutex_lock(&target->mutex);
2801 ret = kfd_criu_resume_svm(target);
2802 if (ret) {
2803 pr_err("kfd_criu_resume_svm failed for %i\n", args->pid);
2804 goto exit;
2805 }
2806
2807 ret = amdgpu_amdkfd_criu_resume(target->kgd_process_info);
2808 if (ret)
2809 pr_err("amdgpu_amdkfd_criu_resume failed for %i\n", args->pid);
2810
2811 exit:
2812 mutex_unlock(&target->mutex);
2813
2814 kfd_unref_process(target);
2815 return ret;
2816 }
2817
criu_process_info(struct file * filep,struct kfd_process * p,struct kfd_ioctl_criu_args * args)2818 static int criu_process_info(struct file *filep,
2819 struct kfd_process *p,
2820 struct kfd_ioctl_criu_args *args)
2821 {
2822 int ret = 0;
2823
2824 mutex_lock(&p->mutex);
2825
2826 if (!p->n_pdds) {
2827 pr_err("No pdd for given process\n");
2828 ret = -ENODEV;
2829 goto err_unlock;
2830 }
2831
2832 ret = kfd_process_evict_queues(p, KFD_QUEUE_EVICTION_CRIU_CHECKPOINT);
2833 if (ret)
2834 goto err_unlock;
2835
2836 p->queues_paused = true;
2837
2838 args->pid = task_pid_nr_ns(p->lead_thread,
2839 task_active_pid_ns(p->lead_thread));
2840
2841 ret = criu_get_process_object_info(p, &args->num_devices, &args->num_bos,
2842 &args->num_objects, &args->priv_data_size);
2843 if (ret)
2844 goto err_unlock;
2845
2846 dev_dbg(kfd_device, "Num of devices:%u bos:%u objects:%u priv_data_size:%lld\n",
2847 args->num_devices, args->num_bos, args->num_objects,
2848 args->priv_data_size);
2849
2850 err_unlock:
2851 if (ret) {
2852 kfd_process_restore_queues(p);
2853 p->queues_paused = false;
2854 }
2855 mutex_unlock(&p->mutex);
2856 return ret;
2857 }
2858
kfd_ioctl_criu(struct file * filep,struct kfd_process * p,void * data)2859 static int kfd_ioctl_criu(struct file *filep, struct kfd_process *p, void *data)
2860 {
2861 struct kfd_ioctl_criu_args *args = data;
2862 int ret;
2863
2864 dev_dbg(kfd_device, "CRIU operation: %d\n", args->op);
2865 switch (args->op) {
2866 case KFD_CRIU_OP_PROCESS_INFO:
2867 ret = criu_process_info(filep, p, args);
2868 break;
2869 case KFD_CRIU_OP_CHECKPOINT:
2870 ret = criu_checkpoint(filep, p, args);
2871 break;
2872 case KFD_CRIU_OP_UNPAUSE:
2873 ret = criu_unpause(filep, p, args);
2874 break;
2875 case KFD_CRIU_OP_RESTORE:
2876 ret = criu_restore(filep, p, args);
2877 break;
2878 case KFD_CRIU_OP_RESUME:
2879 ret = criu_resume(filep, p, args);
2880 break;
2881 default:
2882 dev_dbg(kfd_device, "Unsupported CRIU operation:%d\n", args->op);
2883 ret = -EINVAL;
2884 break;
2885 }
2886
2887 if (ret)
2888 dev_dbg(kfd_device, "CRIU operation:%d err:%d\n", args->op, ret);
2889
2890 return ret;
2891 }
2892
runtime_enable(struct kfd_process * p,uint64_t r_debug,bool enable_ttmp_setup)2893 static int runtime_enable(struct kfd_process *p, uint64_t r_debug,
2894 bool enable_ttmp_setup)
2895 {
2896 int i = 0, ret = 0;
2897
2898 if (p->is_runtime_retry)
2899 goto retry;
2900
2901 if (p->runtime_info.runtime_state != DEBUG_RUNTIME_STATE_DISABLED)
2902 return -EBUSY;
2903
2904 for (i = 0; i < p->n_pdds; i++) {
2905 struct kfd_process_device *pdd = p->pdds[i];
2906
2907 if (pdd->qpd.queue_count)
2908 return -EEXIST;
2909
2910 /*
2911 * Setup TTMPs by default.
2912 * Note that this call must remain here for MES ADD QUEUE to
2913 * skip_process_ctx_clear unconditionally as the first call to
2914 * SET_SHADER_DEBUGGER clears any stale process context data
2915 * saved in MES.
2916 */
2917 if (pdd->dev->kfd->shared_resources.enable_mes) {
2918 ret = kfd_dbg_set_mes_debug_mode(
2919 pdd, !kfd_dbg_has_cwsr_workaround(pdd->dev));
2920 if (ret)
2921 return ret;
2922 }
2923 }
2924
2925 p->runtime_info.runtime_state = DEBUG_RUNTIME_STATE_ENABLED;
2926 p->runtime_info.r_debug = r_debug;
2927 p->runtime_info.ttmp_setup = enable_ttmp_setup;
2928
2929 if (p->runtime_info.ttmp_setup) {
2930 for (i = 0; i < p->n_pdds; i++) {
2931 struct kfd_process_device *pdd = p->pdds[i];
2932
2933 if (!kfd_dbg_is_rlc_restore_supported(pdd->dev)) {
2934 amdgpu_gfx_off_ctrl(pdd->dev->adev, false);
2935 pdd->dev->kfd2kgd->enable_debug_trap(
2936 pdd->dev->adev,
2937 true,
2938 pdd->dev->vm_info.last_vmid_kfd);
2939 } else if (kfd_dbg_is_per_vmid_supported(pdd->dev)) {
2940 pdd->spi_dbg_override = pdd->dev->kfd2kgd->enable_debug_trap(
2941 pdd->dev->adev,
2942 false,
2943 0);
2944 }
2945 }
2946 }
2947
2948 retry:
2949 if (p->debug_trap_enabled) {
2950 if (!p->is_runtime_retry) {
2951 kfd_dbg_trap_activate(p);
2952 kfd_dbg_ev_raise(KFD_EC_MASK(EC_PROCESS_RUNTIME),
2953 p, NULL, 0, false, NULL, 0);
2954 }
2955
2956 mutex_unlock(&p->mutex);
2957 ret = down_interruptible(&p->runtime_enable_sema);
2958 mutex_lock(&p->mutex);
2959
2960 p->is_runtime_retry = !!ret;
2961 }
2962
2963 return ret;
2964 }
2965
runtime_disable(struct kfd_process * p)2966 static int runtime_disable(struct kfd_process *p)
2967 {
2968 int i = 0, ret = 0;
2969 bool was_enabled = p->runtime_info.runtime_state == DEBUG_RUNTIME_STATE_ENABLED;
2970
2971 p->runtime_info.runtime_state = DEBUG_RUNTIME_STATE_DISABLED;
2972 p->runtime_info.r_debug = 0;
2973
2974 if (p->debug_trap_enabled) {
2975 if (was_enabled)
2976 kfd_dbg_trap_deactivate(p, false, 0);
2977
2978 if (!p->is_runtime_retry)
2979 kfd_dbg_ev_raise(KFD_EC_MASK(EC_PROCESS_RUNTIME),
2980 p, NULL, 0, false, NULL, 0);
2981
2982 mutex_unlock(&p->mutex);
2983 ret = down_interruptible(&p->runtime_enable_sema);
2984 mutex_lock(&p->mutex);
2985
2986 p->is_runtime_retry = !!ret;
2987 if (ret)
2988 return ret;
2989 }
2990
2991 if (was_enabled && p->runtime_info.ttmp_setup) {
2992 for (i = 0; i < p->n_pdds; i++) {
2993 struct kfd_process_device *pdd = p->pdds[i];
2994
2995 if (!kfd_dbg_is_rlc_restore_supported(pdd->dev))
2996 amdgpu_gfx_off_ctrl(pdd->dev->adev, true);
2997 }
2998 }
2999
3000 p->runtime_info.ttmp_setup = false;
3001
3002 /* disable ttmp setup */
3003 for (i = 0; i < p->n_pdds; i++) {
3004 struct kfd_process_device *pdd = p->pdds[i];
3005 int last_err = 0;
3006
3007 if (kfd_dbg_is_per_vmid_supported(pdd->dev)) {
3008 pdd->spi_dbg_override =
3009 pdd->dev->kfd2kgd->disable_debug_trap(
3010 pdd->dev->adev,
3011 false,
3012 pdd->dev->vm_info.last_vmid_kfd);
3013
3014 if (!pdd->dev->kfd->shared_resources.enable_mes)
3015 last_err = debug_refresh_runlist(pdd->dev->dqm);
3016 else
3017 last_err = kfd_dbg_set_mes_debug_mode(pdd,
3018 !kfd_dbg_has_cwsr_workaround(pdd->dev));
3019
3020 if (last_err)
3021 ret = last_err;
3022 }
3023 }
3024
3025 return ret;
3026 }
3027
kfd_ioctl_runtime_enable(struct file * filep,struct kfd_process * p,void * data)3028 static int kfd_ioctl_runtime_enable(struct file *filep, struct kfd_process *p, void *data)
3029 {
3030 struct kfd_ioctl_runtime_enable_args *args = data;
3031 int r;
3032
3033 mutex_lock(&p->mutex);
3034
3035 if (args->mode_mask & KFD_RUNTIME_ENABLE_MODE_ENABLE_MASK)
3036 r = runtime_enable(p, args->r_debug,
3037 !!(args->mode_mask & KFD_RUNTIME_ENABLE_MODE_TTMP_SAVE_MASK));
3038 else
3039 r = runtime_disable(p);
3040
3041 mutex_unlock(&p->mutex);
3042
3043 return r;
3044 }
3045
kfd_ioctl_set_debug_trap(struct file * filep,struct kfd_process * p,void * data)3046 static int kfd_ioctl_set_debug_trap(struct file *filep, struct kfd_process *p, void *data)
3047 {
3048 struct kfd_ioctl_dbg_trap_args *args = data;
3049 struct task_struct *thread = NULL;
3050 struct mm_struct *mm = NULL;
3051 struct pid *pid = NULL;
3052 struct kfd_process *target = NULL;
3053 struct kfd_process_device *pdd = NULL;
3054 int r = 0;
3055
3056 if (p->context_id != KFD_CONTEXT_ID_PRIMARY) {
3057 pr_debug("Set debug trap ioctl can not be invoked on non-primary kfd process\n");
3058
3059 return -EOPNOTSUPP;
3060 }
3061
3062 if (sched_policy == KFD_SCHED_POLICY_NO_HWS) {
3063 pr_err("Debugging does not support sched_policy %i", sched_policy);
3064 return -EINVAL;
3065 }
3066
3067 pid = find_get_pid(args->pid);
3068 if (!pid) {
3069 pr_debug("Cannot find pid info for %i\n", args->pid);
3070 r = -ESRCH;
3071 goto out;
3072 }
3073
3074 thread = get_pid_task(pid, PIDTYPE_PID);
3075 if (!thread) {
3076 r = -ESRCH;
3077 goto out;
3078 }
3079
3080 mm = get_task_mm(thread);
3081 if (!mm) {
3082 r = -ESRCH;
3083 goto out;
3084 }
3085
3086 if (args->op == KFD_IOC_DBG_TRAP_ENABLE) {
3087 bool create_process;
3088
3089 rcu_read_lock();
3090 create_process = thread && thread != current && ptrace_parent(thread) == current;
3091 rcu_read_unlock();
3092
3093 target = create_process ? kfd_create_process(thread) :
3094 kfd_lookup_process_by_pid(pid);
3095 } else {
3096 target = kfd_lookup_process_by_pid(pid);
3097 }
3098
3099 if (IS_ERR_OR_NULL(target)) {
3100 pr_debug("Cannot find process PID %i to debug\n", args->pid);
3101 r = target ? PTR_ERR(target) : -ESRCH;
3102 target = NULL;
3103 goto out;
3104 }
3105
3106 if (target->context_id != KFD_CONTEXT_ID_PRIMARY) {
3107 pr_debug("Set debug trap ioctl not supported on non-primary kfd process\n");
3108 r = -EOPNOTSUPP;
3109 goto out;
3110 }
3111
3112 /* Check if target is still PTRACED. */
3113 rcu_read_lock();
3114 if (target != p && args->op != KFD_IOC_DBG_TRAP_DISABLE
3115 && ptrace_parent(target->lead_thread) != current) {
3116 pr_err("PID %i is not PTRACED and cannot be debugged\n", args->pid);
3117 r = -EPERM;
3118 }
3119 rcu_read_unlock();
3120
3121 if (r)
3122 goto out;
3123
3124 mutex_lock(&target->mutex);
3125
3126 if (args->op != KFD_IOC_DBG_TRAP_ENABLE && !target->debug_trap_enabled) {
3127 pr_err("PID %i not debug enabled for op %i\n", args->pid, args->op);
3128 r = -EINVAL;
3129 goto unlock_out;
3130 }
3131
3132 if (target->runtime_info.runtime_state != DEBUG_RUNTIME_STATE_ENABLED &&
3133 (args->op == KFD_IOC_DBG_TRAP_SET_WAVE_LAUNCH_OVERRIDE ||
3134 args->op == KFD_IOC_DBG_TRAP_SET_WAVE_LAUNCH_MODE ||
3135 args->op == KFD_IOC_DBG_TRAP_SUSPEND_QUEUES ||
3136 args->op == KFD_IOC_DBG_TRAP_RESUME_QUEUES ||
3137 args->op == KFD_IOC_DBG_TRAP_SET_NODE_ADDRESS_WATCH ||
3138 args->op == KFD_IOC_DBG_TRAP_CLEAR_NODE_ADDRESS_WATCH ||
3139 args->op == KFD_IOC_DBG_TRAP_SET_FLAGS)) {
3140 r = -EPERM;
3141 goto unlock_out;
3142 }
3143
3144 if (args->op == KFD_IOC_DBG_TRAP_SET_NODE_ADDRESS_WATCH ||
3145 args->op == KFD_IOC_DBG_TRAP_CLEAR_NODE_ADDRESS_WATCH) {
3146 int user_gpu_id = kfd_process_get_user_gpu_id(target,
3147 args->op == KFD_IOC_DBG_TRAP_SET_NODE_ADDRESS_WATCH ?
3148 args->set_node_address_watch.gpu_id :
3149 args->clear_node_address_watch.gpu_id);
3150
3151 pdd = kfd_process_device_data_by_id(target, user_gpu_id);
3152 if (user_gpu_id == -EINVAL || !pdd) {
3153 r = -ENODEV;
3154 goto unlock_out;
3155 }
3156 }
3157
3158 switch (args->op) {
3159 case KFD_IOC_DBG_TRAP_ENABLE:
3160 if (target != p)
3161 target->debugger_process = p;
3162
3163 r = kfd_dbg_trap_enable(target,
3164 args->enable.dbg_fd,
3165 (void __user *)args->enable.rinfo_ptr,
3166 &args->enable.rinfo_size);
3167 if (!r)
3168 target->exception_enable_mask = args->enable.exception_mask;
3169
3170 break;
3171 case KFD_IOC_DBG_TRAP_DISABLE:
3172 r = kfd_dbg_trap_disable(target);
3173 break;
3174 case KFD_IOC_DBG_TRAP_SEND_RUNTIME_EVENT:
3175 r = kfd_dbg_send_exception_to_runtime(target,
3176 args->send_runtime_event.gpu_id,
3177 args->send_runtime_event.queue_id,
3178 args->send_runtime_event.exception_mask);
3179 break;
3180 case KFD_IOC_DBG_TRAP_SET_EXCEPTIONS_ENABLED:
3181 kfd_dbg_set_enabled_debug_exception_mask(target,
3182 args->set_exceptions_enabled.exception_mask);
3183 break;
3184 case KFD_IOC_DBG_TRAP_SET_WAVE_LAUNCH_OVERRIDE:
3185 r = kfd_dbg_trap_set_wave_launch_override(target,
3186 args->launch_override.override_mode,
3187 args->launch_override.enable_mask,
3188 args->launch_override.support_request_mask,
3189 &args->launch_override.enable_mask,
3190 &args->launch_override.support_request_mask);
3191 break;
3192 case KFD_IOC_DBG_TRAP_SET_WAVE_LAUNCH_MODE:
3193 r = kfd_dbg_trap_set_wave_launch_mode(target,
3194 args->launch_mode.launch_mode);
3195 break;
3196 case KFD_IOC_DBG_TRAP_SUSPEND_QUEUES:
3197 r = suspend_queues(target,
3198 args->suspend_queues.num_queues,
3199 args->suspend_queues.grace_period,
3200 args->suspend_queues.exception_mask,
3201 (uint32_t *)args->suspend_queues.queue_array_ptr);
3202
3203 break;
3204 case KFD_IOC_DBG_TRAP_RESUME_QUEUES:
3205 r = resume_queues(target, args->resume_queues.num_queues,
3206 (uint32_t *)args->resume_queues.queue_array_ptr);
3207 break;
3208 case KFD_IOC_DBG_TRAP_SET_NODE_ADDRESS_WATCH:
3209 r = kfd_dbg_trap_set_dev_address_watch(pdd,
3210 args->set_node_address_watch.address,
3211 args->set_node_address_watch.mask,
3212 &args->set_node_address_watch.id,
3213 args->set_node_address_watch.mode);
3214 break;
3215 case KFD_IOC_DBG_TRAP_CLEAR_NODE_ADDRESS_WATCH:
3216 r = kfd_dbg_trap_clear_dev_address_watch(pdd,
3217 args->clear_node_address_watch.id);
3218 break;
3219 case KFD_IOC_DBG_TRAP_SET_FLAGS:
3220 r = kfd_dbg_trap_set_flags(target, &args->set_flags.flags);
3221 break;
3222 case KFD_IOC_DBG_TRAP_QUERY_DEBUG_EVENT:
3223 r = kfd_dbg_ev_query_debug_event(target,
3224 &args->query_debug_event.queue_id,
3225 &args->query_debug_event.gpu_id,
3226 args->query_debug_event.exception_mask,
3227 &args->query_debug_event.exception_mask);
3228 break;
3229 case KFD_IOC_DBG_TRAP_QUERY_EXCEPTION_INFO:
3230 r = kfd_dbg_trap_query_exception_info(target,
3231 args->query_exception_info.source_id,
3232 args->query_exception_info.exception_code,
3233 args->query_exception_info.clear_exception,
3234 (void __user *)args->query_exception_info.info_ptr,
3235 &args->query_exception_info.info_size);
3236 break;
3237 case KFD_IOC_DBG_TRAP_GET_QUEUE_SNAPSHOT:
3238 r = pqm_get_queue_snapshot(&target->pqm,
3239 args->queue_snapshot.exception_mask,
3240 (void __user *)args->queue_snapshot.snapshot_buf_ptr,
3241 &args->queue_snapshot.num_queues,
3242 &args->queue_snapshot.entry_size);
3243 break;
3244 case KFD_IOC_DBG_TRAP_GET_DEVICE_SNAPSHOT:
3245 r = kfd_dbg_trap_device_snapshot(target,
3246 args->device_snapshot.exception_mask,
3247 (void __user *)args->device_snapshot.snapshot_buf_ptr,
3248 &args->device_snapshot.num_devices,
3249 &args->device_snapshot.entry_size);
3250 break;
3251 default:
3252 pr_err("Invalid option: %i\n", args->op);
3253 r = -EINVAL;
3254 }
3255
3256 unlock_out:
3257 mutex_unlock(&target->mutex);
3258
3259 out:
3260 if (thread)
3261 put_task_struct(thread);
3262
3263 if (mm)
3264 mmput(mm);
3265
3266 if (pid)
3267 put_pid(pid);
3268
3269 if (target)
3270 kfd_unref_process(target);
3271
3272 return r;
3273 }
3274
3275 /* userspace programs need to invoke this ioctl explicitly on a FD to
3276 * create a secondary kfd_process which replacing its primary kfd_process
3277 */
kfd_ioctl_create_process(struct file * filep,struct kfd_process * p,void * data)3278 static int kfd_ioctl_create_process(struct file *filep, struct kfd_process *p, void *data)
3279 {
3280 struct kfd_process *process;
3281 int ret;
3282
3283 if (!filep->private_data || !p)
3284 return -EINVAL;
3285
3286 /* Each FD owns only one kfd_process */
3287 if (p->context_id != KFD_CONTEXT_ID_PRIMARY)
3288 return -EINVAL;
3289
3290 mutex_lock(&kfd_processes_mutex);
3291 if (p != filep->private_data) {
3292 mutex_unlock(&kfd_processes_mutex);
3293 return -EINVAL;
3294 }
3295
3296 process = create_process(current, false);
3297 if (IS_ERR(process)) {
3298 mutex_unlock(&kfd_processes_mutex);
3299 return PTR_ERR(process);
3300 }
3301
3302 filep->private_data = process;
3303 mutex_unlock(&kfd_processes_mutex);
3304
3305 ret = kfd_create_process_sysfs(process);
3306 if (ret)
3307 pr_warn("Failed to create sysfs entry for the kfd_process");
3308
3309 /* Each open() increases kref of the primary kfd_process,
3310 * so we need to reduce it here when we create a new secondary process replacing it
3311 */
3312 kfd_unref_process(p);
3313
3314 return 0;
3315 }
3316
profile_lock_device(struct kfd_process * p,uint32_t gpu_id,uint32_t op)3317 static inline uint32_t profile_lock_device(struct kfd_process *p,
3318 uint32_t gpu_id, uint32_t op)
3319 {
3320 struct kfd_process_device *pdd;
3321 struct kfd_dev *kfd;
3322 int status = -EINVAL;
3323 struct amdgpu_ptl *ptl;
3324
3325 if (!p)
3326 return -EINVAL;
3327
3328 mutex_lock(&p->mutex);
3329 pdd = kfd_process_device_data_by_id(p, gpu_id);
3330 mutex_unlock(&p->mutex);
3331
3332 if (!pdd || !pdd->dev || !pdd->dev->kfd)
3333 return -EINVAL;
3334
3335 kfd = pdd->dev->kfd;
3336 ptl = &pdd->dev->adev->psp.ptl;
3337
3338 mutex_lock(&kfd->profiler_lock);
3339 if (op == 1) {
3340 if (!kfd->profiler_process) {
3341 kfd->profiler_process = p;
3342 status = 0;
3343 mutex_unlock(&kfd->profiler_lock);
3344 if (ptl->hw_supported) {
3345 status = kfd_ptl_disable_request(pdd, p);
3346 if (status != 0)
3347 dev_err(kfd_device,
3348 "Failed to lock device %d for profiling, error %d\n",
3349 gpu_id, status);
3350 }
3351 return status;
3352 } else if (kfd->profiler_process == p) {
3353 status = -EALREADY;
3354 } else {
3355 status = -EBUSY;
3356 }
3357 } else if (op == 0 && kfd->profiler_process == p) {
3358 kfd->profiler_process = NULL;
3359 status = 0;
3360 mutex_unlock(&kfd->profiler_lock);
3361
3362 if (ptl->hw_supported) {
3363 status = kfd_ptl_disable_release(pdd, p);
3364 if (status)
3365 dev_err(kfd_device,
3366 "Failed to unlock device %d for profiling, error %d\n",
3367 gpu_id, status);
3368 }
3369 return status;
3370 }
3371 mutex_unlock(&kfd->profiler_lock);
3372
3373 return status;
3374 }
3375
kfd_profiler_pmc(struct kfd_process * p,struct kfd_ioctl_pmc_settings * args)3376 static inline int kfd_profiler_pmc(struct kfd_process *p,
3377 struct kfd_ioctl_pmc_settings *args)
3378 {
3379 struct kfd_process_device *pdd;
3380 struct device_queue_manager *dqm;
3381 int status;
3382
3383 /* Check if we have the correct permissions. */
3384 if (!perfmon_capable())
3385 return -EPERM;
3386
3387 /* Lock/Unlock the device based on the parameter given in OP */
3388 status = profile_lock_device(p, args->gpu_id, args->lock);
3389 if (status != 0)
3390 return status;
3391
3392 /* Enable/disable perfcount if requested */
3393 mutex_lock(&p->mutex);
3394 pdd = kfd_process_device_data_by_id(p, args->gpu_id);
3395 dqm = pdd->dev->dqm;
3396 mutex_unlock(&p->mutex);
3397
3398 dqm->ops.set_perfcount(dqm, args->perfcount_enable);
3399 return status;
3400 }
3401
kfd_ioctl_profiler(struct file * filep,struct kfd_process * p,void * data)3402 static int kfd_ioctl_profiler(struct file *filep, struct kfd_process *p, void *data)
3403 {
3404 struct kfd_ioctl_profiler_args *args = data;
3405
3406 switch (args->op) {
3407 case KFD_IOC_PROFILER_VERSION:
3408 args->version = KFD_IOC_PROFILER_VERSION_NUM;
3409 return 0;
3410 case KFD_IOC_PROFILER_PMC:
3411 return kfd_profiler_pmc(p, &args->pmc);
3412 case KFD_IOC_PROFILER_PTL_CONTROL:
3413 return kfd_profiler_ptl_control(p, &args->ptl);
3414 }
3415 return -EINVAL;
3416 }
3417
3418 #define AMDKFD_IOCTL_DEF(ioctl, _func, _flags) \
3419 [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func, .flags = _flags, \
3420 .validate = NULL, .cmd_drv = 0, .name = #ioctl}
3421
3422 #define AMDKFD_IOCTL_DEF_V(ioctl, _func, _validate, _flags) \
3423 [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func, .flags = _flags, \
3424 .validate = _validate, .cmd_drv = 0, .name = #ioctl}
3425
3426 /** Ioctl table */
3427 static const struct amdkfd_ioctl_desc amdkfd_ioctls[] = {
3428 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_VERSION,
3429 kfd_ioctl_get_version, 0),
3430
3431 AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_QUEUE,
3432 kfd_ioctl_create_queue, 0),
3433
3434 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_QUEUE,
3435 kfd_ioctl_destroy_queue, 0),
3436
3437 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_MEMORY_POLICY,
3438 kfd_ioctl_set_memory_policy, 0),
3439
3440 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_CLOCK_COUNTERS,
3441 kfd_ioctl_get_clock_counters, 0),
3442
3443 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_PROCESS_APERTURES,
3444 kfd_ioctl_get_process_apertures, 0),
3445
3446 AMDKFD_IOCTL_DEF(AMDKFD_IOC_UPDATE_QUEUE,
3447 kfd_ioctl_update_queue, 0),
3448
3449 AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_EVENT,
3450 kfd_ioctl_create_event, 0),
3451
3452 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_EVENT,
3453 kfd_ioctl_destroy_event, 0),
3454
3455 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_EVENT,
3456 kfd_ioctl_set_event, 0),
3457
3458 AMDKFD_IOCTL_DEF(AMDKFD_IOC_RESET_EVENT,
3459 kfd_ioctl_reset_event, 0),
3460
3461 AMDKFD_IOCTL_DEF(AMDKFD_IOC_WAIT_EVENTS,
3462 kfd_ioctl_wait_events, 0),
3463
3464 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_REGISTER_DEPRECATED,
3465 kfd_ioctl_dbg_register, 0),
3466
3467 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_UNREGISTER_DEPRECATED,
3468 kfd_ioctl_dbg_unregister, 0),
3469
3470 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_ADDRESS_WATCH_DEPRECATED,
3471 kfd_ioctl_dbg_address_watch, 0),
3472
3473 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_WAVE_CONTROL_DEPRECATED,
3474 kfd_ioctl_dbg_wave_control, 0),
3475
3476 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_SCRATCH_BACKING_VA,
3477 kfd_ioctl_set_scratch_backing_va, 0),
3478
3479 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_TILE_CONFIG,
3480 kfd_ioctl_get_tile_config, 0),
3481
3482 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_TRAP_HANDLER,
3483 kfd_ioctl_set_trap_handler, 0),
3484
3485 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_PROCESS_APERTURES_NEW,
3486 kfd_ioctl_get_process_apertures_new, 0),
3487
3488 AMDKFD_IOCTL_DEF(AMDKFD_IOC_ACQUIRE_VM,
3489 kfd_ioctl_acquire_vm, 0),
3490
3491 AMDKFD_IOCTL_DEF(AMDKFD_IOC_ALLOC_MEMORY_OF_GPU,
3492 kfd_ioctl_alloc_memory_of_gpu, 0),
3493
3494 AMDKFD_IOCTL_DEF(AMDKFD_IOC_FREE_MEMORY_OF_GPU,
3495 kfd_ioctl_free_memory_of_gpu, 0),
3496
3497 AMDKFD_IOCTL_DEF(AMDKFD_IOC_MAP_MEMORY_TO_GPU,
3498 kfd_ioctl_map_memory_to_gpu, 0),
3499
3500 AMDKFD_IOCTL_DEF(AMDKFD_IOC_UNMAP_MEMORY_FROM_GPU,
3501 kfd_ioctl_unmap_memory_from_gpu, 0),
3502
3503 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_CU_MASK,
3504 kfd_ioctl_set_cu_mask, 0),
3505
3506 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_QUEUE_WAVE_STATE,
3507 kfd_ioctl_get_queue_wave_state, 0),
3508
3509 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_DMABUF_INFO,
3510 kfd_ioctl_get_dmabuf_info, 0),
3511
3512 AMDKFD_IOCTL_DEF(AMDKFD_IOC_IMPORT_DMABUF,
3513 kfd_ioctl_import_dmabuf, 0),
3514
3515 AMDKFD_IOCTL_DEF(AMDKFD_IOC_ALLOC_QUEUE_GWS,
3516 kfd_ioctl_alloc_queue_gws, 0),
3517
3518 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SMI_EVENTS,
3519 kfd_ioctl_smi_events, 0),
3520
3521 AMDKFD_IOCTL_DEF_V(AMDKFD_IOC_SVM, kfd_ioctl_svm,
3522 kfd_ioctl_svm_validate, 0),
3523
3524 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_XNACK_MODE,
3525 kfd_ioctl_set_xnack_mode, 0),
3526
3527 AMDKFD_IOCTL_DEF(AMDKFD_IOC_CRIU_OP,
3528 kfd_ioctl_criu, KFD_IOC_FLAG_CHECKPOINT_RESTORE),
3529
3530 AMDKFD_IOCTL_DEF(AMDKFD_IOC_AVAILABLE_MEMORY,
3531 kfd_ioctl_get_available_memory, 0),
3532
3533 AMDKFD_IOCTL_DEF(AMDKFD_IOC_EXPORT_DMABUF,
3534 kfd_ioctl_export_dmabuf, 0),
3535
3536 AMDKFD_IOCTL_DEF(AMDKFD_IOC_RUNTIME_ENABLE,
3537 kfd_ioctl_runtime_enable, 0),
3538
3539 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_TRAP,
3540 kfd_ioctl_set_debug_trap, 0),
3541
3542 AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_PROCESS,
3543 kfd_ioctl_create_process, 0),
3544
3545 AMDKFD_IOCTL_DEF(AMDKFD_IOC_PROFILER,
3546 kfd_ioctl_profiler, 0),
3547 };
3548
3549 #define AMDKFD_CORE_IOCTL_COUNT ARRAY_SIZE(amdkfd_ioctls)
3550
kfd_ioctl(struct file * filep,unsigned int cmd,unsigned long arg)3551 static long kfd_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
3552 {
3553 struct kfd_process *process;
3554 amdkfd_ioctl_t *func;
3555 const struct amdkfd_ioctl_desc *ioctl = NULL;
3556 unsigned int nr = _IOC_NR(cmd);
3557 char stack_kdata[128];
3558 char *kdata = NULL;
3559 unsigned int usize, asize;
3560 int retcode = -EINVAL;
3561 bool ptrace_attached = false;
3562
3563 if (nr >= AMDKFD_CORE_IOCTL_COUNT) {
3564 retcode = -ENOTTY;
3565 goto err_i1;
3566 }
3567
3568 if ((nr >= AMDKFD_COMMAND_START) && (nr < AMDKFD_COMMAND_END)) {
3569 u32 amdkfd_size;
3570
3571 ioctl = &amdkfd_ioctls[nr];
3572
3573 amdkfd_size = _IOC_SIZE(ioctl->cmd);
3574 usize = asize = _IOC_SIZE(cmd);
3575 if (amdkfd_size > asize)
3576 asize = amdkfd_size;
3577
3578 cmd = ioctl->cmd;
3579 } else {
3580 retcode = -ENOTTY;
3581 goto err_i1;
3582 }
3583
3584 dev_dbg(kfd_device, "ioctl cmd 0x%x (#0x%x), arg 0x%lx\n", cmd, nr, arg);
3585
3586 /* Get the process struct from the filep. Only the process
3587 * that opened /dev/kfd can use the file descriptor. Child
3588 * processes need to create their own KFD device context.
3589 */
3590 process = filep->private_data;
3591
3592 rcu_read_lock();
3593 if ((ioctl->flags & KFD_IOC_FLAG_CHECKPOINT_RESTORE) &&
3594 ptrace_parent(process->lead_thread) == current)
3595 ptrace_attached = true;
3596 rcu_read_unlock();
3597
3598 if (process->lead_thread != current->group_leader
3599 && !ptrace_attached) {
3600 dev_dbg(kfd_device, "Using KFD FD in wrong process\n");
3601 retcode = -EBADF;
3602 goto err_i1;
3603 }
3604
3605 /* Do not trust userspace, use our own definition */
3606 func = ioctl->func;
3607
3608 if (unlikely(!func)) {
3609 dev_dbg(kfd_device, "no function\n");
3610 retcode = -EINVAL;
3611 goto err_i1;
3612 }
3613
3614 /*
3615 * Versions of docker shipped in Ubuntu 18.xx and 20.xx do not support
3616 * CAP_CHECKPOINT_RESTORE, so we also allow access if CAP_SYS_ADMIN as CAP_SYS_ADMIN is a
3617 * more priviledged access.
3618 */
3619 if (unlikely(ioctl->flags & KFD_IOC_FLAG_CHECKPOINT_RESTORE)) {
3620 if (!capable(CAP_CHECKPOINT_RESTORE) &&
3621 !capable(CAP_SYS_ADMIN)) {
3622 retcode = -EACCES;
3623 goto err_i1;
3624 }
3625 }
3626
3627 if (cmd & (IOC_IN | IOC_OUT)) {
3628 if (asize <= sizeof(stack_kdata)) {
3629 kdata = stack_kdata;
3630 } else {
3631 kdata = kmalloc(asize, GFP_KERNEL);
3632 if (!kdata) {
3633 retcode = -ENOMEM;
3634 goto err_i1;
3635 }
3636 }
3637 if (asize > usize)
3638 memset(kdata + usize, 0, asize - usize);
3639 }
3640
3641 if (cmd & IOC_IN) {
3642 if (copy_from_user(kdata, (void __user *)arg, usize) != 0) {
3643 retcode = -EFAULT;
3644 goto err_i1;
3645 }
3646 } else if (cmd & IOC_OUT) {
3647 memset(kdata, 0, usize);
3648 }
3649
3650 if (ioctl->validate) {
3651 retcode = ioctl->validate(kdata, usize);
3652 if (retcode)
3653 goto err_i1;
3654 }
3655
3656 retcode = func(filep, process, kdata);
3657
3658 if (cmd & IOC_OUT)
3659 if (copy_to_user((void __user *)arg, kdata, usize) != 0)
3660 retcode = -EFAULT;
3661
3662 err_i1:
3663 if (!ioctl)
3664 dev_dbg(kfd_device, "invalid ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n",
3665 task_pid_nr(current), cmd, nr);
3666
3667 if (kdata != stack_kdata)
3668 kfree(kdata);
3669
3670 if (retcode)
3671 dev_dbg(kfd_device, "ioctl cmd (#0x%x), arg 0x%lx, ret = %d\n",
3672 nr, arg, retcode);
3673
3674 return retcode;
3675 }
3676
kfd_mmio_mmap(struct kfd_node * dev,struct kfd_process * process,struct vm_area_struct * vma)3677 static int kfd_mmio_mmap(struct kfd_node *dev, struct kfd_process *process,
3678 struct vm_area_struct *vma)
3679 {
3680 phys_addr_t address;
3681
3682 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
3683 return -EINVAL;
3684
3685 if (PAGE_SIZE > 4096)
3686 return -EINVAL;
3687
3688 address = dev->adev->rmmio_remap.bus_addr;
3689
3690 vm_flags_set(vma, VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_NORESERVE |
3691 VM_DONTDUMP | VM_PFNMAP);
3692
3693 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
3694
3695 pr_debug("process pid %d mapping mmio page\n"
3696 " target user address == 0x%08llX\n"
3697 " physical address == 0x%08llX\n"
3698 " vm_flags == 0x%04lX\n"
3699 " size == 0x%04lX\n",
3700 process->lead_thread->pid, (unsigned long long) vma->vm_start,
3701 address, vma->vm_flags, PAGE_SIZE);
3702
3703 return io_remap_pfn_range(vma,
3704 vma->vm_start,
3705 address >> PAGE_SHIFT,
3706 PAGE_SIZE,
3707 vma->vm_page_prot);
3708 }
3709
3710
kfd_mmap(struct file * filep,struct vm_area_struct * vma)3711 static int kfd_mmap(struct file *filep, struct vm_area_struct *vma)
3712 {
3713 struct kfd_process *process;
3714 struct kfd_node *dev = NULL;
3715 unsigned long mmap_offset;
3716 unsigned int gpu_id;
3717
3718 process = filep->private_data;
3719 if (!process)
3720 return -ESRCH;
3721
3722 if (process->lead_thread != current->group_leader)
3723 return -EBADF;
3724
3725 mmap_offset = vma->vm_pgoff << PAGE_SHIFT;
3726 gpu_id = KFD_MMAP_GET_GPU_ID(mmap_offset);
3727 if (gpu_id)
3728 dev = kfd_device_by_id(gpu_id);
3729
3730 switch (mmap_offset & KFD_MMAP_TYPE_MASK) {
3731 case KFD_MMAP_TYPE_DOORBELL:
3732 if (!dev)
3733 return -ENODEV;
3734 return kfd_doorbell_mmap(dev, process, vma);
3735
3736 case KFD_MMAP_TYPE_EVENTS:
3737 return kfd_event_mmap(process, vma);
3738
3739 case KFD_MMAP_TYPE_RESERVED_MEM:
3740 pr_warn("KFD_MMAP_TYPE_RESERVED_MEM is no longer supported\n");
3741 return -EINVAL;
3742 case KFD_MMAP_TYPE_MMIO:
3743 if (!dev)
3744 return -ENODEV;
3745 return kfd_mmio_mmap(dev, process, vma);
3746 }
3747
3748 return -EFAULT;
3749 }
3750