kfd_chardev.c 51.5 KB
Newer Older
O
Oded Gabbay 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright 2014 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include <linux/device.h>
#include <linux/export.h>
#include <linux/err.h>
#include <linux/fs.h>
27
#include <linux/file.h>
O
Oded Gabbay 已提交
28 29 30 31 32 33 34
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/compat.h>
#include <uapi/linux/kfd_ioctl.h>
#include <linux/time.h>
#include <linux/mm.h>
35
#include <linux/mman.h>
36
#include <linux/dma-buf.h>
O
Oded Gabbay 已提交
37 38
#include <asm/processor.h>
#include "kfd_priv.h"
39
#include "kfd_device_queue_manager.h"
40
#include "kfd_dbgmgr.h"
P
Philip Yang 已提交
41
#include "kfd_svm.h"
A
Amber Lin 已提交
42
#include "amdgpu_amdkfd.h"
A
Amber Lin 已提交
43
#include "kfd_smi_events.h"
O
Oded Gabbay 已提交
44 45 46

static long kfd_ioctl(struct file *, unsigned int, unsigned long);
static int kfd_open(struct inode *, struct file *);
47
static int kfd_release(struct inode *, struct file *);
48
static int kfd_mmap(struct file *, struct vm_area_struct *);
O
Oded Gabbay 已提交
49 50 51 52 53 54

static const char kfd_dev_name[] = "kfd";

static const struct file_operations kfd_fops = {
	.owner = THIS_MODULE,
	.unlocked_ioctl = kfd_ioctl,
55
	.compat_ioctl = compat_ptr_ioctl,
O
Oded Gabbay 已提交
56
	.open = kfd_open,
57
	.release = kfd_release,
58
	.mmap = kfd_mmap,
O
Oded Gabbay 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
};

static int kfd_char_dev_major = -1;
static struct class *kfd_class;
struct device *kfd_device;

int kfd_chardev_init(void)
{
	int err = 0;

	kfd_char_dev_major = register_chrdev(0, kfd_dev_name, &kfd_fops);
	err = kfd_char_dev_major;
	if (err < 0)
		goto err_register_chrdev;

	kfd_class = class_create(THIS_MODULE, kfd_dev_name);
	err = PTR_ERR(kfd_class);
	if (IS_ERR(kfd_class))
		goto err_class_create;

	kfd_device = device_create(kfd_class, NULL,
					MKDEV(kfd_char_dev_major, 0),
					NULL, kfd_dev_name);
	err = PTR_ERR(kfd_device);
	if (IS_ERR(kfd_device))
		goto err_device_create;

	return 0;

err_device_create:
	class_destroy(kfd_class);
err_class_create:
	unregister_chrdev(kfd_char_dev_major, kfd_dev_name);
err_register_chrdev:
	return err;
}

void kfd_chardev_exit(void)
{
	device_destroy(kfd_class, MKDEV(kfd_char_dev_major, 0));
	class_destroy(kfd_class);
	unregister_chrdev(kfd_char_dev_major, kfd_dev_name);
101
	kfd_device = NULL;
O
Oded Gabbay 已提交
102 103 104 105 106 107 108 109 110 111
}

struct device *kfd_chardev(void)
{
	return kfd_device;
}


static int kfd_open(struct inode *inode, struct file *filep)
{
112
	struct kfd_process *process;
113
	bool is_32bit_user_mode;
114

O
Oded Gabbay 已提交
115 116 117
	if (iminor(inode) != 0)
		return -ENODEV;

118
	is_32bit_user_mode = in_compat_syscall();
119

120
	if (is_32bit_user_mode) {
121 122 123 124 125 126 127
		dev_warn(kfd_device,
			"Process %d (32-bit) failed to open /dev/kfd\n"
			"32-bit processes are not supported by amdkfd\n",
			current->pid);
		return -EPERM;
	}

F
Felix Kuehling 已提交
128
	process = kfd_create_process(filep);
129 130 131
	if (IS_ERR(process))
		return PTR_ERR(process);

132
	if (kfd_is_locked()) {
133 134
		dev_dbg(kfd_device, "kfd is locked!\n"
				"process %d unreferenced", process->pasid);
135
		kfd_unref_process(process);
136
		return -EAGAIN;
137 138 139 140
	}

	/* filep now owns the reference returned by kfd_create_process */
	filep->private_data = process;
141

142 143 144
	dev_dbg(kfd_device, "process %d opened, compat mode (32 bit) - %d\n",
		process->pasid, process->is_32bit_user_mode);

O
Oded Gabbay 已提交
145 146 147
	return 0;
}

148 149 150 151 152 153 154 155 156 157
static int kfd_release(struct inode *inode, struct file *filep)
{
	struct kfd_process *process = filep->private_data;

	if (process)
		kfd_unref_process(process);

	return 0;
}

158 159
static int kfd_ioctl_get_version(struct file *filep, struct kfd_process *p,
					void *data)
O
Oded Gabbay 已提交
160
{
161
	struct kfd_ioctl_get_version_args *args = data;
162

163 164
	args->major_version = KFD_IOCTL_MAJOR_VERSION;
	args->minor_version = KFD_IOCTL_MINOR_VERSION;
165

166
	return 0;
O
Oded Gabbay 已提交
167 168
}

169 170 171 172
static int set_queue_properties_from_user(struct queue_properties *q_properties,
				struct kfd_ioctl_create_queue_args *args)
{
	if (args->queue_percentage > KFD_MAX_QUEUE_PERCENTAGE) {
173
		pr_err("Queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
174 175 176 177
		return -EINVAL;
	}

	if (args->queue_priority > KFD_MAX_QUEUE_PRIORITY) {
178
		pr_err("Queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
179 180 181 182
		return -EINVAL;
	}

	if ((args->ring_base_address) &&
183
		(!access_ok((const void __user *) args->ring_base_address,
184
			sizeof(uint64_t)))) {
185
		pr_err("Can't access ring base address\n");
186 187 188 189
		return -EFAULT;
	}

	if (!is_power_of_2(args->ring_size) && (args->ring_size != 0)) {
190
		pr_err("Ring size must be a power of 2 or 0\n");
191 192 193
		return -EINVAL;
	}

194
	if (!access_ok((const void __user *) args->read_pointer_address,
195
			sizeof(uint32_t))) {
196
		pr_err("Can't access read pointer\n");
197 198 199
		return -EFAULT;
	}

200
	if (!access_ok((const void __user *) args->write_pointer_address,
201
			sizeof(uint32_t))) {
202
		pr_err("Can't access write pointer\n");
203 204 205
		return -EFAULT;
	}

O
Oded Gabbay 已提交
206
	if (args->eop_buffer_address &&
207
		!access_ok((const void __user *) args->eop_buffer_address,
O
Oded Gabbay 已提交
208
			sizeof(uint32_t))) {
209
		pr_debug("Can't access eop buffer");
210 211 212
		return -EFAULT;
	}

O
Oded Gabbay 已提交
213
	if (args->ctx_save_restore_address &&
214
		!access_ok((const void __user *) args->ctx_save_restore_address,
O
Oded Gabbay 已提交
215
			sizeof(uint32_t))) {
216
		pr_debug("Can't access ctx save restore buffer");
217 218 219
		return -EFAULT;
	}

220
	q_properties->is_interop = false;
221
	q_properties->is_gws = false;
222 223 224 225 226 227
	q_properties->queue_percent = args->queue_percentage;
	q_properties->priority = args->queue_priority;
	q_properties->queue_address = args->ring_base_address;
	q_properties->queue_size = args->ring_size;
	q_properties->read_ptr = (uint32_t *) args->read_pointer_address;
	q_properties->write_ptr = (uint32_t *) args->write_pointer_address;
228 229 230 231 232
	q_properties->eop_ring_buffer_address = args->eop_buffer_address;
	q_properties->eop_ring_buffer_size = args->eop_buffer_size;
	q_properties->ctx_save_restore_area_address =
			args->ctx_save_restore_address;
	q_properties->ctx_save_restore_area_size = args->ctx_save_restore_size;
F
Felix Kuehling 已提交
233
	q_properties->ctl_stack_size = args->ctl_stack_size;
234 235 236
	if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE ||
		args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL)
		q_properties->type = KFD_QUEUE_TYPE_COMPUTE;
237 238
	else if (args->queue_type == KFD_IOC_QUEUE_TYPE_SDMA)
		q_properties->type = KFD_QUEUE_TYPE_SDMA;
239 240
	else if (args->queue_type == KFD_IOC_QUEUE_TYPE_SDMA_XGMI)
		q_properties->type = KFD_QUEUE_TYPE_SDMA_XGMI;
241 242 243 244 245 246 247 248
	else
		return -ENOTSUPP;

	if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL)
		q_properties->format = KFD_QUEUE_FORMAT_AQL;
	else
		q_properties->format = KFD_QUEUE_FORMAT_PM4;

249
	pr_debug("Queue Percentage: %d, %d\n",
250 251
			q_properties->queue_percent, args->queue_percentage);

252
	pr_debug("Queue Priority: %d, %d\n",
253 254
			q_properties->priority, args->queue_priority);

255
	pr_debug("Queue Address: 0x%llX, 0x%llX\n",
256 257
			q_properties->queue_address, args->ring_base_address);

258
	pr_debug("Queue Size: 0x%llX, %u\n",
259 260
			q_properties->queue_size, args->ring_size);

261
	pr_debug("Queue r/w Pointers: %px, %px\n",
262 263
			q_properties->read_ptr,
			q_properties->write_ptr);
264

265
	pr_debug("Queue Format: %d\n", q_properties->format);
266

267
	pr_debug("Queue EOP: 0x%llX\n", q_properties->eop_ring_buffer_address);
268

269
	pr_debug("Queue CTX save area: 0x%llX\n",
270 271
			q_properties->ctx_save_restore_area_address);

272 273 274
	return 0;
}

275 276
static int kfd_ioctl_create_queue(struct file *filep, struct kfd_process *p,
					void *data)
O
Oded Gabbay 已提交
277
{
278
	struct kfd_ioctl_create_queue_args *args = data;
279 280 281 282 283
	struct kfd_dev *dev;
	int err = 0;
	unsigned int queue_id;
	struct kfd_process_device *pdd;
	struct queue_properties q_properties;
284
	uint32_t doorbell_offset_in_process = 0;
285 286 287

	memset(&q_properties, 0, sizeof(struct queue_properties));

288
	pr_debug("Creating queue ioctl\n");
289

290
	err = set_queue_properties_from_user(&q_properties, args);
291 292 293
	if (err)
		return err;

294
	pr_debug("Looking for gpu id 0x%x\n", args->gpu_id);
295
	dev = kfd_device_by_id(args->gpu_id);
296
	if (!dev) {
297
		pr_debug("Could not find gpu id 0x%x\n", args->gpu_id);
298
		return -EINVAL;
299
	}
300 301 302 303

	mutex_lock(&p->mutex);

	pdd = kfd_bind_process_to_device(dev, p);
304
	if (IS_ERR(pdd)) {
305
		err = -ESRCH;
306 307 308
		goto err_bind_process;
	}

309
	pr_debug("Creating queue for PASID 0x%x on gpu 0x%x\n",
310 311 312
			p->pasid,
			dev->id);

313 314
	err = pqm_create_queue(&p->pqm, dev, filep, &q_properties, &queue_id,
			&doorbell_offset_in_process);
315 316 317
	if (err != 0)
		goto err_create_queue;

318
	args->queue_id = queue_id;
319

320

321
	/* Return gpu_id as doorbell offset for mmap usage */
322 323
	args->doorbell_offset = KFD_MMAP_TYPE_DOORBELL;
	args->doorbell_offset |= KFD_MMAP_GPU_ID(args->gpu_id);
324
	if (KFD_IS_SOC15(dev->device_info->asic_family))
325 326
		/* On SOC15 ASICs, include the doorbell offset within the
		 * process doorbell frame, which is 2 pages.
327
		 */
328
		args->doorbell_offset |= doorbell_offset_in_process;
329 330 331

	mutex_unlock(&p->mutex);

332
	pr_debug("Queue id %d was created successfully\n", args->queue_id);
333

334
	pr_debug("Ring buffer address == 0x%016llX\n",
335
			args->ring_base_address);
336

337
	pr_debug("Read ptr address    == 0x%016llX\n",
338
			args->read_pointer_address);
339

340
	pr_debug("Write ptr address   == 0x%016llX\n",
341
			args->write_pointer_address);
342 343 344 345 346 347 348

	return 0;

err_create_queue:
err_bind_process:
	mutex_unlock(&p->mutex);
	return err;
O
Oded Gabbay 已提交
349 350 351
}

static int kfd_ioctl_destroy_queue(struct file *filp, struct kfd_process *p,
352
					void *data)
O
Oded Gabbay 已提交
353
{
354
	int retval;
355
	struct kfd_ioctl_destroy_queue_args *args = data;
356

357
	pr_debug("Destroying queue id %d for pasid 0x%x\n",
358
				args->queue_id,
359 360 361 362
				p->pasid);

	mutex_lock(&p->mutex);

363
	retval = pqm_destroy_queue(&p->pqm, args->queue_id);
364 365 366

	mutex_unlock(&p->mutex);
	return retval;
O
Oded Gabbay 已提交
367 368 369
}

static int kfd_ioctl_update_queue(struct file *filp, struct kfd_process *p,
370
					void *data)
O
Oded Gabbay 已提交
371
{
372
	int retval;
373
	struct kfd_ioctl_update_queue_args *args = data;
374 375
	struct queue_properties properties;

376
	if (args->queue_percentage > KFD_MAX_QUEUE_PERCENTAGE) {
377
		pr_err("Queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
378 379 380
		return -EINVAL;
	}

381
	if (args->queue_priority > KFD_MAX_QUEUE_PRIORITY) {
382
		pr_err("Queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
383 384 385
		return -EINVAL;
	}

386
	if ((args->ring_base_address) &&
387
		(!access_ok((const void __user *) args->ring_base_address,
388
			sizeof(uint64_t)))) {
389
		pr_err("Can't access ring base address\n");
390 391 392
		return -EFAULT;
	}

393
	if (!is_power_of_2(args->ring_size) && (args->ring_size != 0)) {
394
		pr_err("Ring size must be a power of 2 or 0\n");
395 396 397
		return -EINVAL;
	}

398 399 400 401
	properties.queue_address = args->ring_base_address;
	properties.queue_size = args->ring_size;
	properties.queue_percent = args->queue_percentage;
	properties.priority = args->queue_priority;
402

403
	pr_debug("Updating queue id %d for pasid 0x%x\n",
404
			args->queue_id, p->pasid);
405 406 407

	mutex_lock(&p->mutex);

408
	retval = pqm_update_queue(&p->pqm, args->queue_id, &properties);
409 410 411 412

	mutex_unlock(&p->mutex);

	return retval;
O
Oded Gabbay 已提交
413 414
}

415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
static int kfd_ioctl_set_cu_mask(struct file *filp, struct kfd_process *p,
					void *data)
{
	int retval;
	const int max_num_cus = 1024;
	struct kfd_ioctl_set_cu_mask_args *args = data;
	struct queue_properties properties;
	uint32_t __user *cu_mask_ptr = (uint32_t __user *)args->cu_mask_ptr;
	size_t cu_mask_size = sizeof(uint32_t) * (args->num_cu_mask / 32);

	if ((args->num_cu_mask % 32) != 0) {
		pr_debug("num_cu_mask 0x%x must be a multiple of 32",
				args->num_cu_mask);
		return -EINVAL;
	}

	properties.cu_mask_count = args->num_cu_mask;
	if (properties.cu_mask_count == 0) {
		pr_debug("CU mask cannot be 0");
		return -EINVAL;
	}

	/* To prevent an unreasonably large CU mask size, set an arbitrary
	 * limit of max_num_cus bits.  We can then just drop any CU mask bits
	 * past max_num_cus bits and just use the first max_num_cus bits.
	 */
	if (properties.cu_mask_count > max_num_cus) {
		pr_debug("CU mask cannot be greater than 1024 bits");
		properties.cu_mask_count = max_num_cus;
		cu_mask_size = sizeof(uint32_t) * (max_num_cus/32);
	}

	properties.cu_mask = kzalloc(cu_mask_size, GFP_KERNEL);
	if (!properties.cu_mask)
		return -ENOMEM;

	retval = copy_from_user(properties.cu_mask, cu_mask_ptr, cu_mask_size);
	if (retval) {
		pr_debug("Could not copy CU mask from userspace");
		kfree(properties.cu_mask);
		return -EFAULT;
	}

	mutex_lock(&p->mutex);

	retval = pqm_set_cu_mask(&p->pqm, args->queue_id, &properties);

	mutex_unlock(&p->mutex);

	if (retval)
		kfree(properties.cu_mask);

	return retval;
}

470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
static int kfd_ioctl_get_queue_wave_state(struct file *filep,
					  struct kfd_process *p, void *data)
{
	struct kfd_ioctl_get_queue_wave_state_args *args = data;
	int r;

	mutex_lock(&p->mutex);

	r = pqm_get_wave_state(&p->pqm, args->queue_id,
			       (void __user *)args->ctl_stack_address,
			       &args->ctl_stack_used_size,
			       &args->save_area_used_size);

	mutex_unlock(&p->mutex);

	return r;
}

488 489
static int kfd_ioctl_set_memory_policy(struct file *filep,
					struct kfd_process *p, void *data)
O
Oded Gabbay 已提交
490
{
491
	struct kfd_ioctl_set_memory_policy_args *args = data;
492 493 494 495 496
	struct kfd_dev *dev;
	int err = 0;
	struct kfd_process_device *pdd;
	enum cache_policy default_policy, alternate_policy;

497 498
	if (args->default_policy != KFD_IOC_CACHE_POLICY_COHERENT
	    && args->default_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) {
499 500 501
		return -EINVAL;
	}

502 503
	if (args->alternate_policy != KFD_IOC_CACHE_POLICY_COHERENT
	    && args->alternate_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) {
504 505 506
		return -EINVAL;
	}

507
	dev = kfd_device_by_id(args->gpu_id);
508
	if (!dev)
509 510 511 512 513
		return -EINVAL;

	mutex_lock(&p->mutex);

	pdd = kfd_bind_process_to_device(dev, p);
514
	if (IS_ERR(pdd)) {
515
		err = -ESRCH;
516 517 518
		goto out;
	}

519
	default_policy = (args->default_policy == KFD_IOC_CACHE_POLICY_COHERENT)
520 521 522
			 ? cache_policy_coherent : cache_policy_noncoherent;

	alternate_policy =
523
		(args->alternate_policy == KFD_IOC_CACHE_POLICY_COHERENT)
524 525
		   ? cache_policy_coherent : cache_policy_noncoherent;

526
	if (!dev->dqm->ops.set_cache_memory_policy(dev->dqm,
527 528 529
				&pdd->qpd,
				default_policy,
				alternate_policy,
530 531
				(void __user *)args->alternate_aperture_base,
				args->alternate_aperture_size))
532 533 534 535 536 537
		err = -EINVAL;

out:
	mutex_unlock(&p->mutex);

	return err;
O
Oded Gabbay 已提交
538 539
}

540 541 542 543 544 545 546 547 548
static int kfd_ioctl_set_trap_handler(struct file *filep,
					struct kfd_process *p, void *data)
{
	struct kfd_ioctl_set_trap_handler_args *args = data;
	struct kfd_dev *dev;
	int err = 0;
	struct kfd_process_device *pdd;

	dev = kfd_device_by_id(args->gpu_id);
K
Kent Russell 已提交
549
	if (!dev)
550 551 552 553 554 555 556 557 558 559
		return -EINVAL;

	mutex_lock(&p->mutex);

	pdd = kfd_bind_process_to_device(dev, p);
	if (IS_ERR(pdd)) {
		err = -ESRCH;
		goto out;
	}

560
	kfd_process_set_trap_handler(&pdd->qpd, args->tba_addr, args->tma_addr);
561 562 563 564 565 566 567

out:
	mutex_unlock(&p->mutex);

	return err;
}

568 569 570
static int kfd_ioctl_dbg_register(struct file *filep,
				struct kfd_process *p, void *data)
{
571 572 573 574 575 576 577 578
	struct kfd_ioctl_dbg_register_args *args = data;
	struct kfd_dev *dev;
	struct kfd_dbgmgr *dbgmgr_ptr;
	struct kfd_process_device *pdd;
	bool create_ok;
	long status = 0;

	dev = kfd_device_by_id(args->gpu_id);
579
	if (!dev)
580 581 582 583 584 585 586 587
		return -EINVAL;

	if (dev->device_info->asic_family == CHIP_CARRIZO) {
		pr_debug("kfd_ioctl_dbg_register not supported on CZ\n");
		return -EINVAL;
	}

	mutex_lock(&p->mutex);
588
	mutex_lock(kfd_get_dbgmgr_mutex());
589 590 591 592 593 594 595

	/*
	 * make sure that we have pdd, if this the first queue created for
	 * this process
	 */
	pdd = kfd_bind_process_to_device(dev, p);
	if (IS_ERR(pdd)) {
K
Kent Russell 已提交
596 597
		status = PTR_ERR(pdd);
		goto out;
598 599
	}

600
	if (!dev->dbgmgr) {
601 602 603 604 605 606 607 608 609 610 611 612 613 614
		/* In case of a legal call, we have no dbgmgr yet */
		create_ok = kfd_dbgmgr_create(&dbgmgr_ptr, dev);
		if (create_ok) {
			status = kfd_dbgmgr_register(dbgmgr_ptr, p);
			if (status != 0)
				kfd_dbgmgr_destroy(dbgmgr_ptr);
			else
				dev->dbgmgr = dbgmgr_ptr;
		}
	} else {
		pr_debug("debugger already registered\n");
		status = -EINVAL;
	}

K
Kent Russell 已提交
615
out:
616
	mutex_unlock(kfd_get_dbgmgr_mutex());
617
	mutex_unlock(&p->mutex);
618 619 620 621

	return status;
}

622
static int kfd_ioctl_dbg_unregister(struct file *filep,
623 624
				struct kfd_process *p, void *data)
{
625 626 627 628 629
	struct kfd_ioctl_dbg_unregister_args *args = data;
	struct kfd_dev *dev;
	long status;

	dev = kfd_device_by_id(args->gpu_id);
630
	if (!dev || !dev->dbgmgr)
631 632 633
		return -EINVAL;

	if (dev->device_info->asic_family == CHIP_CARRIZO) {
634
		pr_debug("kfd_ioctl_dbg_unregister not supported on CZ\n");
635 636 637 638 639 640
		return -EINVAL;
	}

	mutex_lock(kfd_get_dbgmgr_mutex());

	status = kfd_dbgmgr_unregister(dev->dbgmgr, p);
641
	if (!status) {
642 643 644 645 646
		kfd_dbgmgr_destroy(dev->dbgmgr);
		dev->dbgmgr = NULL;
	}

	mutex_unlock(kfd_get_dbgmgr_mutex());
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662

	return status;
}

/*
 * Parse and generate variable size data structure for address watch.
 * Total size of the buffer and # watch points is limited in order
 * to prevent kernel abuse. (no bearing to the much smaller HW limitation
 * which is enforced by dbgdev module)
 * please also note that the watch address itself are not "copied from user",
 * since it be set into the HW in user mode values.
 *
 */
static int kfd_ioctl_dbg_address_watch(struct file *filep,
					struct kfd_process *p, void *data)
{
663 664 665 666 667 668 669 670 671 672 673 674
	struct kfd_ioctl_dbg_address_watch_args *args = data;
	struct kfd_dev *dev;
	struct dbg_address_watch_info aw_info;
	unsigned char *args_buff;
	long status;
	void __user *cmd_from_user;
	uint64_t watch_mask_value = 0;
	unsigned int args_idx = 0;

	memset((void *) &aw_info, 0, sizeof(struct dbg_address_watch_info));

	dev = kfd_device_by_id(args->gpu_id);
675
	if (!dev)
676 677 678 679 680 681 682 683 684 685 686 687
		return -EINVAL;

	if (dev->device_info->asic_family == CHIP_CARRIZO) {
		pr_debug("kfd_ioctl_dbg_wave_control not supported on CZ\n");
		return -EINVAL;
	}

	cmd_from_user = (void __user *) args->content_ptr;

	/* Validate arguments */

	if ((args->buf_size_in_bytes > MAX_ALLOWED_AW_BUFF_SIZE) ||
688
		(args->buf_size_in_bytes <= sizeof(*args) + sizeof(int) * 2) ||
689 690 691 692
		(cmd_from_user == NULL))
		return -EINVAL;

	/* this is the actual buffer to work with */
693
	args_buff = memdup_user(cmd_from_user,
694
				args->buf_size_in_bytes - sizeof(*args));
A
Al Viro 已提交
695 696
	if (IS_ERR(args_buff))
		return PTR_ERR(args_buff);
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714

	aw_info.process = p;

	aw_info.num_watch_points = *((uint32_t *)(&args_buff[args_idx]));
	args_idx += sizeof(aw_info.num_watch_points);

	aw_info.watch_mode = (enum HSA_DBG_WATCH_MODE *) &args_buff[args_idx];
	args_idx += sizeof(enum HSA_DBG_WATCH_MODE) * aw_info.num_watch_points;

	/*
	 * set watch address base pointer to point on the array base
	 * within args_buff
	 */
	aw_info.watch_address = (uint64_t *) &args_buff[args_idx];

	/* skip over the addresses buffer */
	args_idx += sizeof(aw_info.watch_address) * aw_info.num_watch_points;

715
	if (args_idx >= args->buf_size_in_bytes - sizeof(*args)) {
K
Kent Russell 已提交
716 717
		status = -EINVAL;
		goto out;
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
	}

	watch_mask_value = (uint64_t) args_buff[args_idx];

	if (watch_mask_value > 0) {
		/*
		 * There is an array of masks.
		 * set watch mask base pointer to point on the array base
		 * within args_buff
		 */
		aw_info.watch_mask = (uint64_t *) &args_buff[args_idx];

		/* skip over the masks buffer */
		args_idx += sizeof(aw_info.watch_mask) *
				aw_info.num_watch_points;
	} else {
		/* just the NULL mask, set to NULL and skip over it */
		aw_info.watch_mask = NULL;
		args_idx += sizeof(aw_info.watch_mask);
	}

739
	if (args_idx >= args->buf_size_in_bytes - sizeof(args)) {
K
Kent Russell 已提交
740 741
		status = -EINVAL;
		goto out;
742 743 744 745 746 747 748 749 750 751 752
	}

	/* Currently HSA Event is not supported for DBG */
	aw_info.watch_event = NULL;

	mutex_lock(kfd_get_dbgmgr_mutex());

	status = kfd_dbgmgr_address_watch(dev->dbgmgr, &aw_info);

	mutex_unlock(kfd_get_dbgmgr_mutex());

K
Kent Russell 已提交
753
out:
754
	kfree(args_buff);
755 756 757 758 759 760 761 762

	return status;
}

/* Parse and generate fixed size data structure for wave control */
static int kfd_ioctl_dbg_wave_control(struct file *filep,
					struct kfd_process *p, void *data)
{
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
	struct kfd_ioctl_dbg_wave_control_args *args = data;
	struct kfd_dev *dev;
	struct dbg_wave_control_info wac_info;
	unsigned char *args_buff;
	uint32_t computed_buff_size;
	long status;
	void __user *cmd_from_user;
	unsigned int args_idx = 0;

	memset((void *) &wac_info, 0, sizeof(struct dbg_wave_control_info));

	/* we use compact form, independent of the packing attribute value */
	computed_buff_size = sizeof(*args) +
				sizeof(wac_info.mode) +
				sizeof(wac_info.operand) +
				sizeof(wac_info.dbgWave_msg.DbgWaveMsg) +
				sizeof(wac_info.dbgWave_msg.MemoryVA) +
				sizeof(wac_info.trapId);

	dev = kfd_device_by_id(args->gpu_id);
783
	if (!dev)
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
		return -EINVAL;

	if (dev->device_info->asic_family == CHIP_CARRIZO) {
		pr_debug("kfd_ioctl_dbg_wave_control not supported on CZ\n");
		return -EINVAL;
	}

	/* input size must match the computed "compact" size */
	if (args->buf_size_in_bytes != computed_buff_size) {
		pr_debug("size mismatch, computed : actual %u : %u\n",
				args->buf_size_in_bytes, computed_buff_size);
		return -EINVAL;
	}

	cmd_from_user = (void __user *) args->content_ptr;

	if (cmd_from_user == NULL)
		return -EINVAL;

A
Al Viro 已提交
803
	/* copy the entire buffer from user */
804

A
Al Viro 已提交
805
	args_buff = memdup_user(cmd_from_user,
806
				args->buf_size_in_bytes - sizeof(*args));
A
Al Viro 已提交
807 808
	if (IS_ERR(args_buff))
		return PTR_ERR(args_buff);
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839

	/* move ptr to the start of the "pay-load" area */
	wac_info.process = p;

	wac_info.operand = *((enum HSA_DBG_WAVEOP *)(&args_buff[args_idx]));
	args_idx += sizeof(wac_info.operand);

	wac_info.mode = *((enum HSA_DBG_WAVEMODE *)(&args_buff[args_idx]));
	args_idx += sizeof(wac_info.mode);

	wac_info.trapId = *((uint32_t *)(&args_buff[args_idx]));
	args_idx += sizeof(wac_info.trapId);

	wac_info.dbgWave_msg.DbgWaveMsg.WaveMsgInfoGen2.Value =
					*((uint32_t *)(&args_buff[args_idx]));
	wac_info.dbgWave_msg.MemoryVA = NULL;

	mutex_lock(kfd_get_dbgmgr_mutex());

	pr_debug("Calling dbg manager process %p, operand %u, mode %u, trapId %u, message %u\n",
			wac_info.process, wac_info.operand,
			wac_info.mode, wac_info.trapId,
			wac_info.dbgWave_msg.DbgWaveMsg.WaveMsgInfoGen2.Value);

	status = kfd_dbgmgr_wave_control(dev->dbgmgr, &wac_info);

	pr_debug("Returned status of dbg manager is %ld\n", status);

	mutex_unlock(kfd_get_dbgmgr_mutex());

	kfree(args_buff);
840 841 842 843

	return status;
}

844 845
static int kfd_ioctl_get_clock_counters(struct file *filep,
				struct kfd_process *p, void *data)
O
Oded Gabbay 已提交
846
{
847
	struct kfd_ioctl_get_clock_counters_args *args = data;
848 849
	struct kfd_dev *dev;

850
	dev = kfd_device_by_id(args->gpu_id);
851 852
	if (dev)
		/* Reading GPU clock counter from KGD */
853
		args->gpu_clock_counter = amdgpu_amdkfd_get_gpu_clock_counter(dev->kgd);
854 855 856
	else
		/* Node without GPU resource */
		args->gpu_clock_counter = 0;
857 858

	/* No access to rdtsc. Using raw monotonic time */
859
	args->cpu_clock_counter = ktime_get_raw_ns();
860
	args->system_clock_counter = ktime_get_boottime_ns();
861 862

	/* Since the counter is in nano-seconds we use 1GHz frequency */
863
	args->system_clock_freq = 1000000000;
864 865

	return 0;
O
Oded Gabbay 已提交
866 867 868 869
}


static int kfd_ioctl_get_process_apertures(struct file *filp,
870
				struct kfd_process *p, void *data)
O
Oded Gabbay 已提交
871
{
872
	struct kfd_ioctl_get_process_apertures_args *args = data;
873
	struct kfd_process_device_apertures *pAperture;
874
	int i;
875

876
	dev_dbg(kfd_device, "get apertures for PASID 0x%x", p->pasid);
877

878
	args->num_of_nodes = 0;
879 880

	mutex_lock(&p->mutex);
881 882 883 884 885 886 887 888 889 890 891 892 893
	/* Run over all pdd of the process */
	for (i = 0; i < p->n_pdds; i++) {
		struct kfd_process_device *pdd = p->pdds[i];

		pAperture =
			&args->process_apertures[args->num_of_nodes];
		pAperture->gpu_id = pdd->dev->id;
		pAperture->lds_base = pdd->lds_base;
		pAperture->lds_limit = pdd->lds_limit;
		pAperture->gpuvm_base = pdd->gpuvm_base;
		pAperture->gpuvm_limit = pdd->gpuvm_limit;
		pAperture->scratch_base = pdd->scratch_base;
		pAperture->scratch_limit = pdd->scratch_limit;
894

895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
		dev_dbg(kfd_device,
			"node id %u\n", args->num_of_nodes);
		dev_dbg(kfd_device,
			"gpu id %u\n", pdd->dev->id);
		dev_dbg(kfd_device,
			"lds_base %llX\n", pdd->lds_base);
		dev_dbg(kfd_device,
			"lds_limit %llX\n", pdd->lds_limit);
		dev_dbg(kfd_device,
			"gpuvm_base %llX\n", pdd->gpuvm_base);
		dev_dbg(kfd_device,
			"gpuvm_limit %llX\n", pdd->gpuvm_limit);
		dev_dbg(kfd_device,
			"scratch_base %llX\n", pdd->scratch_base);
		dev_dbg(kfd_device,
			"scratch_limit %llX\n", pdd->scratch_limit);
911

912 913 914
		if (++args->num_of_nodes >= NUM_OF_SUPPORTED_GPUS)
			break;
	}
915 916 917
	mutex_unlock(&p->mutex);

	return 0;
O
Oded Gabbay 已提交
918 919
}

920 921 922 923 924 925
static int kfd_ioctl_get_process_apertures_new(struct file *filp,
				struct kfd_process *p, void *data)
{
	struct kfd_ioctl_get_process_apertures_new_args *args = data;
	struct kfd_process_device_apertures *pa;
	int ret;
926
	int i;
927

928
	dev_dbg(kfd_device, "get apertures for PASID 0x%x", p->pasid);
929 930 931 932 933 934

	if (args->num_of_nodes == 0) {
		/* Return number of nodes, so that user space can alloacate
		 * sufficient memory
		 */
		mutex_lock(&p->mutex);
935
		args->num_of_nodes = p->n_pdds;
936 937 938 939 940 941 942 943 944 945 946 947 948 949
		goto out_unlock;
	}

	/* Fill in process-aperture information for all available
	 * nodes, but not more than args->num_of_nodes as that is
	 * the amount of memory allocated by user
	 */
	pa = kzalloc((sizeof(struct kfd_process_device_apertures) *
				args->num_of_nodes), GFP_KERNEL);
	if (!pa)
		return -ENOMEM;

	mutex_lock(&p->mutex);

950
	if (!p->n_pdds) {
951 952 953 954 955 956
		args->num_of_nodes = 0;
		kfree(pa);
		goto out_unlock;
	}

	/* Run over all pdd of the process */
957 958 959 960 961 962 963 964 965 966
	for (i = 0; i < min(p->n_pdds, args->num_of_nodes); i++) {
		struct kfd_process_device *pdd = p->pdds[i];

		pa[i].gpu_id = pdd->dev->id;
		pa[i].lds_base = pdd->lds_base;
		pa[i].lds_limit = pdd->lds_limit;
		pa[i].gpuvm_base = pdd->gpuvm_base;
		pa[i].gpuvm_limit = pdd->gpuvm_limit;
		pa[i].scratch_base = pdd->scratch_base;
		pa[i].scratch_limit = pdd->scratch_limit;
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981

		dev_dbg(kfd_device,
			"gpu id %u\n", pdd->dev->id);
		dev_dbg(kfd_device,
			"lds_base %llX\n", pdd->lds_base);
		dev_dbg(kfd_device,
			"lds_limit %llX\n", pdd->lds_limit);
		dev_dbg(kfd_device,
			"gpuvm_base %llX\n", pdd->gpuvm_base);
		dev_dbg(kfd_device,
			"gpuvm_limit %llX\n", pdd->gpuvm_limit);
		dev_dbg(kfd_device,
			"scratch_base %llX\n", pdd->scratch_base);
		dev_dbg(kfd_device,
			"scratch_limit %llX\n", pdd->scratch_limit);
982
	}
983 984
	mutex_unlock(&p->mutex);

985
	args->num_of_nodes = i;
986 987 988
	ret = copy_to_user(
			(void __user *)args->kfd_process_device_apertures_ptr,
			pa,
989
			(i * sizeof(struct kfd_process_device_apertures)));
990 991 992 993 994 995 996 997
	kfree(pa);
	return ret ? -EFAULT : 0;

out_unlock:
	mutex_unlock(&p->mutex);
	return 0;
}

998 999 1000
static int kfd_ioctl_create_event(struct file *filp, struct kfd_process *p,
					void *data)
{
1001 1002 1003
	struct kfd_ioctl_create_event_args *args = data;
	int err;

1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
	/* For dGPUs the event page is allocated in user mode. The
	 * handle is passed to KFD with the first call to this IOCTL
	 * through the event_page_offset field.
	 */
	if (args->event_page_offset) {
		struct kfd_dev *kfd;
		struct kfd_process_device *pdd;
		void *mem, *kern_addr;
		uint64_t size;

		if (p->signal_page) {
			pr_err("Event page is already set\n");
			return -EINVAL;
		}

		kfd = kfd_device_by_id(GET_GPU_ID(args->event_page_offset));
		if (!kfd) {
			pr_err("Getting device by id failed in %s\n", __func__);
			return -EINVAL;
		}

		mutex_lock(&p->mutex);
		pdd = kfd_bind_process_to_device(kfd, p);
		if (IS_ERR(pdd)) {
			err = PTR_ERR(pdd);
			goto out_unlock;
		}

		mem = kfd_process_device_translate_handle(pdd,
				GET_IDR_HANDLE(args->event_page_offset));
		if (!mem) {
			pr_err("Can't find BO, offset is 0x%llx\n",
			       args->event_page_offset);
			err = -EINVAL;
			goto out_unlock;
		}
		mutex_unlock(&p->mutex);

A
Amber Lin 已提交
1042
		err = amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(kfd->kgd,
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
						mem, &kern_addr, &size);
		if (err) {
			pr_err("Failed to map event page to kernel\n");
			return err;
		}

		err = kfd_event_page_set(p, kern_addr, size);
		if (err) {
			pr_err("Failed to set event page\n");
			return err;
		}
	}

1056 1057 1058 1059 1060 1061 1062
	err = kfd_event_create(filp, p, args->event_type,
				args->auto_reset != 0, args->node_id,
				&args->event_id, &args->event_trigger_data,
				&args->event_page_offset,
				&args->event_slot_index);

	return err;
1063 1064 1065 1066

out_unlock:
	mutex_unlock(&p->mutex);
	return err;
1067 1068 1069 1070 1071
}

static int kfd_ioctl_destroy_event(struct file *filp, struct kfd_process *p,
					void *data)
{
1072 1073 1074
	struct kfd_ioctl_destroy_event_args *args = data;

	return kfd_event_destroy(p, args->event_id);
1075 1076 1077 1078 1079
}

static int kfd_ioctl_set_event(struct file *filp, struct kfd_process *p,
				void *data)
{
1080 1081 1082
	struct kfd_ioctl_set_event_args *args = data;

	return kfd_set_event(p, args->event_id);
1083 1084 1085 1086 1087
}

static int kfd_ioctl_reset_event(struct file *filp, struct kfd_process *p,
				void *data)
{
1088 1089 1090
	struct kfd_ioctl_reset_event_args *args = data;

	return kfd_reset_event(p, args->event_id);
1091 1092 1093 1094 1095
}

static int kfd_ioctl_wait_events(struct file *filp, struct kfd_process *p,
				void *data)
{
1096 1097 1098 1099 1100 1101
	struct kfd_ioctl_wait_events_args *args = data;
	int err;

	err = kfd_wait_on_events(p, args->num_events,
			(void __user *)args->events_ptr,
			(args->wait_for_all != 0),
1102
			args->timeout, &args->wait_result);
1103 1104

	return err;
1105
}
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
static int kfd_ioctl_set_scratch_backing_va(struct file *filep,
					struct kfd_process *p, void *data)
{
	struct kfd_ioctl_set_scratch_backing_va_args *args = data;
	struct kfd_process_device *pdd;
	struct kfd_dev *dev;
	long err;

	dev = kfd_device_by_id(args->gpu_id);
	if (!dev)
		return -EINVAL;

	mutex_lock(&p->mutex);

	pdd = kfd_bind_process_to_device(dev, p);
	if (IS_ERR(pdd)) {
		err = PTR_ERR(pdd);
		goto bind_process_to_device_fail;
	}

	pdd->qpd.sh_hidden_private_base = args->va_addr;

	mutex_unlock(&p->mutex);

1130
	if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS &&
1131
	    pdd->qpd.vmid != 0 && dev->kfd2kgd->set_scratch_backing_va)
1132 1133 1134 1135 1136 1137 1138 1139 1140
		dev->kfd2kgd->set_scratch_backing_va(
			dev->kgd, args->va_addr, pdd->qpd.vmid);

	return 0;

bind_process_to_device_fail:
	mutex_unlock(&p->mutex);
	return err;
}
1141

1142 1143 1144 1145 1146 1147 1148 1149 1150
static int kfd_ioctl_get_tile_config(struct file *filep,
		struct kfd_process *p, void *data)
{
	struct kfd_ioctl_get_tile_config_args *args = data;
	struct kfd_dev *dev;
	struct tile_config config;
	int err = 0;

	dev = kfd_device_by_id(args->gpu_id);
1151 1152
	if (!dev)
		return -EINVAL;
1153

1154
	amdgpu_amdkfd_get_tile_config(dev->kgd, &config);
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183

	args->gb_addr_config = config.gb_addr_config;
	args->num_banks = config.num_banks;
	args->num_ranks = config.num_ranks;

	if (args->num_tile_configs > config.num_tile_configs)
		args->num_tile_configs = config.num_tile_configs;
	err = copy_to_user((void __user *)args->tile_config_ptr,
			config.tile_config_ptr,
			args->num_tile_configs * sizeof(uint32_t));
	if (err) {
		args->num_tile_configs = 0;
		return -EFAULT;
	}

	if (args->num_macro_tile_configs > config.num_macro_tile_configs)
		args->num_macro_tile_configs =
				config.num_macro_tile_configs;
	err = copy_to_user((void __user *)args->macro_tile_config_ptr,
			config.macro_tile_config_ptr,
			args->num_macro_tile_configs * sizeof(uint32_t));
	if (err) {
		args->num_macro_tile_configs = 0;
		return -EFAULT;
	}

	return 0;
}

1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
static int kfd_ioctl_acquire_vm(struct file *filep, struct kfd_process *p,
				void *data)
{
	struct kfd_ioctl_acquire_vm_args *args = data;
	struct kfd_process_device *pdd;
	struct kfd_dev *dev;
	struct file *drm_file;
	int ret;

	dev = kfd_device_by_id(args->gpu_id);
	if (!dev)
		return -EINVAL;

	drm_file = fget(args->drm_fd);
	if (!drm_file)
		return -EINVAL;

	mutex_lock(&p->mutex);

	pdd = kfd_get_process_device_data(dev, p);
	if (!pdd) {
		ret = -EINVAL;
		goto err_unlock;
	}

	if (pdd->drm_file) {
		ret = pdd->drm_file == drm_file ? 0 : -EBUSY;
		goto err_unlock;
	}

	ret = kfd_process_device_init_vm(pdd, drm_file);
	if (ret)
		goto err_unlock;
	/* On success, the PDD keeps the drm_file reference */
	mutex_unlock(&p->mutex);

	return 0;

err_unlock:
	mutex_unlock(&p->mutex);
	fput(drm_file);
	return ret;
}

1228
bool kfd_dev_is_large_bar(struct kfd_dev *dev)
1229 1230 1231
{
	struct kfd_local_mem_info mem_info;

1232 1233 1234 1235 1236
	if (debug_largebar) {
		pr_debug("Simulate large-bar allocation on non large-bar machine\n");
		return true;
	}

1237
	if (dev->use_iommu_v2)
1238 1239
		return false;

1240
	amdgpu_amdkfd_get_local_mem_info(dev->kgd, &mem_info);
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
	if (mem_info.local_mem_size_private == 0 &&
			mem_info.local_mem_size_public > 0)
		return true;
	return false;
}

static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep,
					struct kfd_process *p, void *data)
{
	struct kfd_ioctl_alloc_memory_of_gpu_args *args = data;
	struct kfd_process_device *pdd;
	void *mem;
	struct kfd_dev *dev;
	int idr_handle;
	long err;
	uint64_t offset = args->mmap_offset;
	uint32_t flags = args->flags;

	if (args->size == 0)
		return -EINVAL;

	dev = kfd_device_by_id(args->gpu_id);
	if (!dev)
		return -EINVAL;

	if ((flags & KFD_IOC_ALLOC_MEM_FLAGS_PUBLIC) &&
		(flags & KFD_IOC_ALLOC_MEM_FLAGS_VRAM) &&
		!kfd_dev_is_large_bar(dev)) {
		pr_err("Alloc host visible vram on small bar is not allowed\n");
		return -EINVAL;
	}

	mutex_lock(&p->mutex);

	pdd = kfd_bind_process_to_device(dev, p);
	if (IS_ERR(pdd)) {
		err = PTR_ERR(pdd);
		goto err_unlock;
	}

1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
	if (flags & KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL) {
		if (args->size != kfd_doorbell_process_slice(dev)) {
			err = -EINVAL;
			goto err_unlock;
		}
		offset = kfd_get_process_doorbells(pdd);
	} else if (flags & KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP) {
		if (args->size != PAGE_SIZE) {
			err = -EINVAL;
			goto err_unlock;
		}
		offset = amdgpu_amdkfd_get_mmio_remap_phys_addr(dev->kgd);
		if (!offset) {
			err = -ENOMEM;
			goto err_unlock;
		}
	}

A
Amber Lin 已提交
1299
	err = amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
1300
		dev->kgd, args->va_addr, args->size,
1301
		pdd->drm_priv, (struct kgd_mem **) &mem, &offset,
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
		flags);

	if (err)
		goto err_unlock;

	idr_handle = kfd_process_device_create_obj_handle(pdd, mem);
	if (idr_handle < 0) {
		err = -EFAULT;
		goto err_free;
	}

1313 1314 1315 1316
	/* Update the VRAM usage count */
	if (flags & KFD_IOC_ALLOC_MEM_FLAGS_VRAM)
		WRITE_ONCE(pdd->vram_usage, pdd->vram_usage + args->size);

1317 1318 1319 1320 1321
	mutex_unlock(&p->mutex);

	args->handle = MAKE_HANDLE(args->gpu_id, idr_handle);
	args->mmap_offset = offset;

O
Oak Zeng 已提交
1322 1323 1324
	/* MMIO is mapped through kfd device
	 * Generate a kfd mmap offset
	 */
1325 1326 1327
	if (flags & KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP)
		args->mmap_offset = KFD_MMAP_TYPE_MMIO
					| KFD_MMAP_GPU_ID(args->gpu_id);
O
Oak Zeng 已提交
1328

1329 1330 1331
	return 0;

err_free:
1332 1333
	amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->kgd, (struct kgd_mem *)mem,
					       pdd->drm_priv, NULL);
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
err_unlock:
	mutex_unlock(&p->mutex);
	return err;
}

static int kfd_ioctl_free_memory_of_gpu(struct file *filep,
					struct kfd_process *p, void *data)
{
	struct kfd_ioctl_free_memory_of_gpu_args *args = data;
	struct kfd_process_device *pdd;
	void *mem;
	struct kfd_dev *dev;
	int ret;
1347
	uint64_t size = 0;
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368

	dev = kfd_device_by_id(GET_GPU_ID(args->handle));
	if (!dev)
		return -EINVAL;

	mutex_lock(&p->mutex);

	pdd = kfd_get_process_device_data(dev, p);
	if (!pdd) {
		pr_err("Process device data doesn't exist\n");
		ret = -EINVAL;
		goto err_unlock;
	}

	mem = kfd_process_device_translate_handle(
		pdd, GET_IDR_HANDLE(args->handle));
	if (!mem) {
		ret = -EINVAL;
		goto err_unlock;
	}

A
Amber Lin 已提交
1369
	ret = amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->kgd,
1370
				(struct kgd_mem *)mem, pdd->drm_priv, &size);
1371 1372 1373 1374 1375 1376 1377 1378

	/* If freeing the buffer failed, leave the handle in place for
	 * clean-up during process tear-down.
	 */
	if (!ret)
		kfd_process_device_remove_obj_handle(
			pdd, GET_IDR_HANDLE(args->handle));

1379 1380
	WRITE_ONCE(pdd->vram_usage, pdd->vram_usage - size);

1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
err_unlock:
	mutex_unlock(&p->mutex);
	return ret;
}

static int kfd_ioctl_map_memory_to_gpu(struct file *filep,
					struct kfd_process *p, void *data)
{
	struct kfd_ioctl_map_memory_to_gpu_args *args = data;
	struct kfd_process_device *pdd, *peer_pdd;
	void *mem;
	struct kfd_dev *dev, *peer;
	long err = 0;
	int i;
	uint32_t *devices_arr = NULL;
1396
	bool table_freed = false;
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410

	dev = kfd_device_by_id(GET_GPU_ID(args->handle));
	if (!dev)
		return -EINVAL;

	if (!args->n_devices) {
		pr_debug("Device IDs array empty\n");
		return -EINVAL;
	}
	if (args->n_success > args->n_devices) {
		pr_debug("n_success exceeds n_devices\n");
		return -EINVAL;
	}

1411 1412
	devices_arr = kmalloc_array(args->n_devices, sizeof(*devices_arr),
				    GFP_KERNEL);
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
	if (!devices_arr)
		return -ENOMEM;

	err = copy_from_user(devices_arr,
			     (void __user *)args->device_ids_array_ptr,
			     args->n_devices * sizeof(*devices_arr));
	if (err != 0) {
		err = -EFAULT;
		goto copy_from_user_failed;
	}

	mutex_lock(&p->mutex);

	pdd = kfd_bind_process_to_device(dev, p);
	if (IS_ERR(pdd)) {
		err = PTR_ERR(pdd);
		goto bind_process_to_device_failed;
	}

	mem = kfd_process_device_translate_handle(pdd,
						GET_IDR_HANDLE(args->handle));
	if (!mem) {
		err = -ENOMEM;
		goto get_mem_obj_from_handle_failed;
	}

	for (i = args->n_success; i < args->n_devices; i++) {
		peer = kfd_device_by_id(devices_arr[i]);
		if (!peer) {
			pr_debug("Getting device by id failed for 0x%x\n",
				 devices_arr[i]);
			err = -EINVAL;
			goto get_mem_obj_from_handle_failed;
		}

		peer_pdd = kfd_bind_process_to_device(peer, p);
		if (IS_ERR(peer_pdd)) {
			err = PTR_ERR(peer_pdd);
			goto get_mem_obj_from_handle_failed;
		}
A
Amber Lin 已提交
1453
		err = amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
1454 1455
			peer->kgd, (struct kgd_mem *)mem,
			peer_pdd->drm_priv, &table_freed);
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
		if (err) {
			pr_err("Failed to map to gpu %d/%d\n",
			       i, args->n_devices);
			goto map_memory_to_gpu_failed;
		}
		args->n_success = i+1;
	}

	mutex_unlock(&p->mutex);

A
Amber Lin 已提交
1466
	err = amdgpu_amdkfd_gpuvm_sync_memory(dev->kgd, (struct kgd_mem *) mem, true);
1467 1468 1469 1470 1471 1472
	if (err) {
		pr_debug("Sync memory failed, wait interrupted by user signal\n");
		goto sync_memory_failed;
	}

	/* Flush TLBs after waiting for the page table updates to complete */
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
	if (table_freed) {
		for (i = 0; i < args->n_devices; i++) {
			peer = kfd_device_by_id(devices_arr[i]);
			if (WARN_ON_ONCE(!peer))
				continue;
			peer_pdd = kfd_get_process_device_data(peer, p);
			if (WARN_ON_ONCE(!peer_pdd))
				continue;
			kfd_flush_tlb(peer_pdd, TLB_FLUSH_LEGACY);
		}
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
	}
	kfree(devices_arr);

	return err;

bind_process_to_device_failed:
get_mem_obj_from_handle_failed:
map_memory_to_gpu_failed:
	mutex_unlock(&p->mutex);
copy_from_user_failed:
sync_memory_failed:
	kfree(devices_arr);

	return err;
}

static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
					struct kfd_process *p, void *data)
{
	struct kfd_ioctl_unmap_memory_from_gpu_args *args = data;
	struct kfd_process_device *pdd, *peer_pdd;
	void *mem;
	struct kfd_dev *dev, *peer;
	long err = 0;
	uint32_t *devices_arr = NULL, i;

	dev = kfd_device_by_id(GET_GPU_ID(args->handle));
	if (!dev)
		return -EINVAL;

	if (!args->n_devices) {
		pr_debug("Device IDs array empty\n");
		return -EINVAL;
	}
	if (args->n_success > args->n_devices) {
		pr_debug("n_success exceeds n_devices\n");
		return -EINVAL;
	}

1522 1523
	devices_arr = kmalloc_array(args->n_devices, sizeof(*devices_arr),
				    GFP_KERNEL);
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
	if (!devices_arr)
		return -ENOMEM;

	err = copy_from_user(devices_arr,
			     (void __user *)args->device_ids_array_ptr,
			     args->n_devices * sizeof(*devices_arr));
	if (err != 0) {
		err = -EFAULT;
		goto copy_from_user_failed;
	}

	mutex_lock(&p->mutex);

	pdd = kfd_get_process_device_data(dev, p);
	if (!pdd) {
1539
		err = -EINVAL;
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
		goto bind_process_to_device_failed;
	}

	mem = kfd_process_device_translate_handle(pdd,
						GET_IDR_HANDLE(args->handle));
	if (!mem) {
		err = -ENOMEM;
		goto get_mem_obj_from_handle_failed;
	}

	for (i = args->n_success; i < args->n_devices; i++) {
		peer = kfd_device_by_id(devices_arr[i]);
		if (!peer) {
			err = -EINVAL;
			goto get_mem_obj_from_handle_failed;
		}

		peer_pdd = kfd_get_process_device_data(peer, p);
		if (!peer_pdd) {
			err = -ENODEV;
			goto get_mem_obj_from_handle_failed;
		}
A
Amber Lin 已提交
1562
		err = amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
1563
			peer->kgd, (struct kgd_mem *)mem, peer_pdd->drm_priv);
1564 1565 1566 1567 1568
		if (err) {
			pr_err("Failed to unmap from gpu %d/%d\n",
			       i, args->n_devices);
			goto unmap_memory_from_gpu_failed;
		}
1569
		args->n_success = i+1;
1570
	}
1571 1572
	mutex_unlock(&p->mutex);

1573 1574 1575 1576 1577 1578 1579
	if (dev->device_info->asic_family == CHIP_ALDEBARAN) {
		err = amdgpu_amdkfd_gpuvm_sync_memory(dev->kgd,
				(struct kgd_mem *) mem, true);
		if (err) {
			pr_debug("Sync memory failed, wait interrupted by user signal\n");
			goto sync_memory_failed;
		}
1580

1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
		/* Flush TLBs after waiting for the page table updates to complete */
		for (i = 0; i < args->n_devices; i++) {
			peer = kfd_device_by_id(devices_arr[i]);
			if (WARN_ON_ONCE(!peer))
				continue;
			peer_pdd = kfd_get_process_device_data(peer, p);
			if (WARN_ON_ONCE(!peer_pdd))
				continue;
			kfd_flush_tlb(peer_pdd, TLB_FLUSH_HEAVYWEIGHT);
		}
1591 1592 1593
	}
	kfree(devices_arr);

1594 1595 1596 1597 1598 1599 1600
	return 0;

bind_process_to_device_failed:
get_mem_obj_from_handle_failed:
unmap_memory_from_gpu_failed:
	mutex_unlock(&p->mutex);
copy_from_user_failed:
1601
sync_memory_failed:
1602 1603 1604 1605
	kfree(devices_arr);
	return err;
}

1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
static int kfd_ioctl_alloc_queue_gws(struct file *filep,
		struct kfd_process *p, void *data)
{
	int retval;
	struct kfd_ioctl_alloc_queue_gws_args *args = data;
	struct queue *q;
	struct kfd_dev *dev;

	mutex_lock(&p->mutex);
	q = pqm_get_user_queue(&p->pqm, args->queue_id);

	if (q) {
		dev = q->device;
	} else {
		retval = -EINVAL;
		goto out_unlock;
	}

1624 1625 1626 1627 1628
	if (!dev->gws) {
		retval = -ENODEV;
		goto out_unlock;
	}

1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
	if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
		retval = -ENODEV;
		goto out_unlock;
	}

	retval = pqm_set_gws(&p->pqm, args->queue_id, args->num_gws ? dev->gws : NULL);
	mutex_unlock(&p->mutex);

	args->first_gws = 0;
	return retval;

out_unlock:
	mutex_unlock(&p->mutex);
	return retval;
}

1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
static int kfd_ioctl_get_dmabuf_info(struct file *filep,
		struct kfd_process *p, void *data)
{
	struct kfd_ioctl_get_dmabuf_info_args *args = data;
	struct kfd_dev *dev = NULL;
	struct kgd_dev *dma_buf_kgd;
	void *metadata_buffer = NULL;
	uint32_t flags;
	unsigned int i;
	int r;

	/* Find a KFD GPU device that supports the get_dmabuf_info query */
	for (i = 0; kfd_topology_enum_kfd_devices(i, &dev) == 0; i++)
		if (dev)
			break;
	if (!dev)
		return -EINVAL;

	if (args->metadata_ptr) {
		metadata_buffer = kzalloc(args->metadata_size, GFP_KERNEL);
		if (!metadata_buffer)
			return -ENOMEM;
	}

	/* Get dmabuf info from KGD */
	r = amdgpu_amdkfd_get_dmabuf_info(dev->kgd, args->dmabuf_fd,
					  &dma_buf_kgd, &args->size,
					  metadata_buffer, args->metadata_size,
					  &args->metadata_size, &flags);
	if (r)
		goto exit;

	/* Reverse-lookup gpu_id from kgd pointer */
	dev = kfd_device_by_kgd(dma_buf_kgd);
	if (!dev) {
		r = -EINVAL;
		goto exit;
	}
	args->gpu_id = dev->id;
	args->flags = flags;

	/* Copy metadata buffer to user mode */
	if (metadata_buffer) {
		r = copy_to_user((void __user *)args->metadata_ptr,
				 metadata_buffer, args->metadata_size);
		if (r != 0)
			r = -EFAULT;
	}

exit:
	kfree(metadata_buffer);

	return r;
}

static int kfd_ioctl_import_dmabuf(struct file *filep,
				   struct kfd_process *p, void *data)
{
	struct kfd_ioctl_import_dmabuf_args *args = data;
	struct kfd_process_device *pdd;
	struct dma_buf *dmabuf;
	struct kfd_dev *dev;
	int idr_handle;
	uint64_t size;
	void *mem;
	int r;

	dev = kfd_device_by_id(args->gpu_id);
	if (!dev)
		return -EINVAL;

	dmabuf = dma_buf_get(args->dmabuf_fd);
1717 1718
	if (IS_ERR(dmabuf))
		return PTR_ERR(dmabuf);
1719 1720 1721 1722 1723 1724 1725 1726 1727 1728

	mutex_lock(&p->mutex);

	pdd = kfd_bind_process_to_device(dev, p);
	if (IS_ERR(pdd)) {
		r = PTR_ERR(pdd);
		goto err_unlock;
	}

	r = amdgpu_amdkfd_gpuvm_import_dmabuf(dev->kgd, dmabuf,
1729
					      args->va_addr, pdd->drm_priv,
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
					      (struct kgd_mem **)&mem, &size,
					      NULL);
	if (r)
		goto err_unlock;

	idr_handle = kfd_process_device_create_obj_handle(pdd, mem);
	if (idr_handle < 0) {
		r = -EFAULT;
		goto err_free;
	}

	mutex_unlock(&p->mutex);
1742
	dma_buf_put(dmabuf);
1743 1744 1745 1746 1747 1748

	args->handle = MAKE_HANDLE(args->gpu_id, idr_handle);

	return 0;

err_free:
1749 1750
	amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->kgd, (struct kgd_mem *)mem,
					       pdd->drm_priv, NULL);
1751 1752
err_unlock:
	mutex_unlock(&p->mutex);
1753
	dma_buf_put(dmabuf);
1754 1755 1756
	return r;
}

A
Amber Lin 已提交
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
/* Handle requests for watching SMI events */
static int kfd_ioctl_smi_events(struct file *filep,
				struct kfd_process *p, void *data)
{
	struct kfd_ioctl_smi_events_args *args = data;
	struct kfd_dev *dev;

	dev = kfd_device_by_id(args->gpuid);
	if (!dev)
		return -EINVAL;

	return kfd_smi_event_open(dev, &args->anon_fd);
}

1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
static int kfd_ioctl_set_xnack_mode(struct file *filep,
				    struct kfd_process *p, void *data)
{
	struct kfd_ioctl_set_xnack_mode_args *args = data;
	int r = 0;

	mutex_lock(&p->mutex);
	if (args->xnack_enabled >= 0) {
		if (!list_empty(&p->pqm.queues)) {
			pr_debug("Process has user queues running\n");
			mutex_unlock(&p->mutex);
			return -EBUSY;
		}
		if (args->xnack_enabled && !kfd_process_xnack_mode(p, true))
			r = -EPERM;
		else
			p->xnack_enabled = args->xnack_enabled;
	} else {
		args->xnack_enabled = p->xnack_enabled;
	}
	mutex_unlock(&p->mutex);

	return r;
}

1796
#if IS_ENABLED(CONFIG_HSA_AMD_SVM)
P
Philip Yang 已提交
1797 1798
static int kfd_ioctl_svm(struct file *filep, struct kfd_process *p, void *data)
{
P
Philip Yang 已提交
1799
	struct kfd_ioctl_svm_args *args = data;
P
Philip Yang 已提交
1800 1801
	int r = 0;

P
Philip Yang 已提交
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
	pr_debug("start 0x%llx size 0x%llx op 0x%x nattr 0x%x\n",
		 args->start_addr, args->size, args->op, args->nattr);

	if ((args->start_addr & ~PAGE_MASK) || (args->size & ~PAGE_MASK))
		return -EINVAL;
	if (!args->start_addr || !args->size)
		return -EINVAL;

	mutex_lock(&p->mutex);

	r = svm_ioctl(p, args->op, args->start_addr, args->size, args->nattr,
		      args->attrs);

	mutex_unlock(&p->mutex);

P
Philip Yang 已提交
1817 1818
	return r;
}
1819 1820 1821 1822 1823 1824
#else
static int kfd_ioctl_svm(struct file *filep, struct kfd_process *p, void *data)
{
	return -EPERM;
}
#endif
P
Philip Yang 已提交
1825

1826
#define AMDKFD_IOCTL_DEF(ioctl, _func, _flags) \
1827 1828
	[_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func, .flags = _flags, \
			    .cmd_drv = 0, .name = #ioctl}
1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851

/** Ioctl table */
static const struct amdkfd_ioctl_desc amdkfd_ioctls[] = {
	AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_VERSION,
			kfd_ioctl_get_version, 0),

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_QUEUE,
			kfd_ioctl_create_queue, 0),

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_QUEUE,
			kfd_ioctl_destroy_queue, 0),

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_MEMORY_POLICY,
			kfd_ioctl_set_memory_policy, 0),

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_CLOCK_COUNTERS,
			kfd_ioctl_get_clock_counters, 0),

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_PROCESS_APERTURES,
			kfd_ioctl_get_process_apertures, 0),

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_UPDATE_QUEUE,
			kfd_ioctl_update_queue, 0),
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_EVENT,
			kfd_ioctl_create_event, 0),

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_EVENT,
			kfd_ioctl_destroy_event, 0),

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_EVENT,
			kfd_ioctl_set_event, 0),

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_RESET_EVENT,
			kfd_ioctl_reset_event, 0),

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_WAIT_EVENTS,
			kfd_ioctl_wait_events, 0),
1867 1868 1869 1870 1871

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_REGISTER,
			kfd_ioctl_dbg_register, 0),

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_UNREGISTER,
1872
			kfd_ioctl_dbg_unregister, 0),
1873 1874 1875 1876 1877 1878

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_ADDRESS_WATCH,
			kfd_ioctl_dbg_address_watch, 0),

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_WAVE_CONTROL,
			kfd_ioctl_dbg_wave_control, 0),
1879 1880 1881

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_SCRATCH_BACKING_VA,
			kfd_ioctl_set_scratch_backing_va, 0),
1882 1883

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_TILE_CONFIG,
1884 1885 1886 1887
			kfd_ioctl_get_tile_config, 0),

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_TRAP_HANDLER,
			kfd_ioctl_set_trap_handler, 0),
1888 1889 1890

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_PROCESS_APERTURES_NEW,
			kfd_ioctl_get_process_apertures_new, 0),
1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_ACQUIRE_VM,
			kfd_ioctl_acquire_vm, 0),

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_ALLOC_MEMORY_OF_GPU,
			kfd_ioctl_alloc_memory_of_gpu, 0),

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_FREE_MEMORY_OF_GPU,
			kfd_ioctl_free_memory_of_gpu, 0),

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_MAP_MEMORY_TO_GPU,
			kfd_ioctl_map_memory_to_gpu, 0),

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_UNMAP_MEMORY_FROM_GPU,
			kfd_ioctl_unmap_memory_from_gpu, 0),

1907 1908 1909
	AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_CU_MASK,
			kfd_ioctl_set_cu_mask, 0),

1910
	AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_QUEUE_WAVE_STATE,
1911 1912 1913 1914 1915 1916 1917
			kfd_ioctl_get_queue_wave_state, 0),

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_DMABUF_INFO,
				kfd_ioctl_get_dmabuf_info, 0),

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_IMPORT_DMABUF,
				kfd_ioctl_import_dmabuf, 0),
1918

1919 1920
	AMDKFD_IOCTL_DEF(AMDKFD_IOC_ALLOC_QUEUE_GWS,
			kfd_ioctl_alloc_queue_gws, 0),
A
Amber Lin 已提交
1921 1922 1923

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_SMI_EVENTS,
			kfd_ioctl_smi_events, 0),
P
Philip Yang 已提交
1924 1925

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_SVM, kfd_ioctl_svm, 0),
1926 1927 1928

	AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_XNACK_MODE,
			kfd_ioctl_set_xnack_mode, 0),
1929 1930 1931 1932
};

#define AMDKFD_CORE_IOCTL_COUNT	ARRAY_SIZE(amdkfd_ioctls)

O
Oded Gabbay 已提交
1933 1934 1935
static long kfd_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
{
	struct kfd_process *process;
1936 1937 1938
	amdkfd_ioctl_t *func;
	const struct amdkfd_ioctl_desc *ioctl = NULL;
	unsigned int nr = _IOC_NR(cmd);
1939 1940 1941 1942
	char stack_kdata[128];
	char *kdata = NULL;
	unsigned int usize, asize;
	int retcode = -EINVAL;
O
Oded Gabbay 已提交
1943

1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
	if (nr >= AMDKFD_CORE_IOCTL_COUNT)
		goto err_i1;

	if ((nr >= AMDKFD_COMMAND_START) && (nr < AMDKFD_COMMAND_END)) {
		u32 amdkfd_size;

		ioctl = &amdkfd_ioctls[nr];

		amdkfd_size = _IOC_SIZE(ioctl->cmd);
		usize = asize = _IOC_SIZE(cmd);
		if (amdkfd_size > asize)
			asize = amdkfd_size;

		cmd = ioctl->cmd;
	} else
		goto err_i1;

Y
Yong Zhao 已提交
1961
	dev_dbg(kfd_device, "ioctl cmd 0x%x (#0x%x), arg 0x%lx\n", cmd, nr, arg);
O
Oded Gabbay 已提交
1962

1963 1964 1965 1966 1967 1968 1969 1970
	/* Get the process struct from the filep. Only the process
	 * that opened /dev/kfd can use the file descriptor. Child
	 * processes need to create their own KFD device context.
	 */
	process = filep->private_data;
	if (process->lead_thread != current->group_leader) {
		dev_dbg(kfd_device, "Using KFD FD in wrong process\n");
		retcode = -EBADF;
1971 1972
		goto err_i1;
	}
O
Oded Gabbay 已提交
1973

1974 1975 1976 1977 1978 1979 1980
	/* Do not trust userspace, use our own definition */
	func = ioctl->func;

	if (unlikely(!func)) {
		dev_dbg(kfd_device, "no function\n");
		retcode = -EINVAL;
		goto err_i1;
O
Oded Gabbay 已提交
1981 1982
	}

1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
	if (cmd & (IOC_IN | IOC_OUT)) {
		if (asize <= sizeof(stack_kdata)) {
			kdata = stack_kdata;
		} else {
			kdata = kmalloc(asize, GFP_KERNEL);
			if (!kdata) {
				retcode = -ENOMEM;
				goto err_i1;
			}
		}
		if (asize > usize)
			memset(kdata + usize, 0, asize - usize);
	}
O
Oded Gabbay 已提交
1996

1997 1998 1999 2000 2001 2002 2003 2004 2005
	if (cmd & IOC_IN) {
		if (copy_from_user(kdata, (void __user *)arg, usize) != 0) {
			retcode = -EFAULT;
			goto err_i1;
		}
	} else if (cmd & IOC_OUT) {
		memset(kdata, 0, usize);
	}

2006
	retcode = func(filep, process, kdata);
O
Oded Gabbay 已提交
2007

2008 2009 2010
	if (cmd & IOC_OUT)
		if (copy_to_user((void __user *)arg, kdata, usize) != 0)
			retcode = -EFAULT;
O
Oded Gabbay 已提交
2011

2012
err_i1:
2013 2014 2015 2016
	if (!ioctl)
		dev_dbg(kfd_device, "invalid ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n",
			  task_pid_nr(current), cmd, nr);

2017 2018 2019 2020
	if (kdata != stack_kdata)
		kfree(kdata);

	if (retcode)
Y
Yong Zhao 已提交
2021 2022
		dev_dbg(kfd_device, "ioctl cmd (#0x%x), arg 0x%lx, ret = %d\n",
				nr, arg, retcode);
2023 2024

	return retcode;
O
Oded Gabbay 已提交
2025
}
2026

O
Oak Zeng 已提交
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042
static int kfd_mmio_mmap(struct kfd_dev *dev, struct kfd_process *process,
		      struct vm_area_struct *vma)
{
	phys_addr_t address;
	int ret;

	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
		return -EINVAL;

	address = amdgpu_amdkfd_get_mmio_remap_phys_addr(dev->kgd);

	vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_NORESERVE |
				VM_DONTDUMP | VM_PFNMAP;

	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);

2043
	pr_debug("pasid 0x%x mapping mmio page\n"
O
Oak Zeng 已提交
2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059
		 "     target user address == 0x%08llX\n"
		 "     physical address    == 0x%08llX\n"
		 "     vm_flags            == 0x%04lX\n"
		 "     size                == 0x%04lX\n",
		 process->pasid, (unsigned long long) vma->vm_start,
		 address, vma->vm_flags, PAGE_SIZE);

	ret = io_remap_pfn_range(vma,
				vma->vm_start,
				address >> PAGE_SHIFT,
				PAGE_SIZE,
				vma->vm_page_prot);
	return ret;
}


2060 2061 2062
static int kfd_mmap(struct file *filp, struct vm_area_struct *vma)
{
	struct kfd_process *process;
2063
	struct kfd_dev *dev = NULL;
2064
	unsigned long mmap_offset;
2065
	unsigned int gpu_id;
2066 2067 2068 2069 2070

	process = kfd_get_process(current);
	if (IS_ERR(process))
		return PTR_ERR(process);

2071 2072
	mmap_offset = vma->vm_pgoff << PAGE_SHIFT;
	gpu_id = KFD_MMAP_GET_GPU_ID(mmap_offset);
2073 2074 2075
	if (gpu_id)
		dev = kfd_device_by_id(gpu_id);

2076
	switch (mmap_offset & KFD_MMAP_TYPE_MASK) {
2077 2078 2079 2080 2081 2082
	case KFD_MMAP_TYPE_DOORBELL:
		if (!dev)
			return -ENODEV;
		return kfd_doorbell_mmap(dev, process, vma);

	case KFD_MMAP_TYPE_EVENTS:
2083
		return kfd_event_mmap(process, vma);
2084 2085 2086 2087 2088

	case KFD_MMAP_TYPE_RESERVED_MEM:
		if (!dev)
			return -ENODEV;
		return kfd_reserved_mem_mmap(dev, process, vma);
O
Oak Zeng 已提交
2089 2090 2091 2092
	case KFD_MMAP_TYPE_MMIO:
		if (!dev)
			return -ENODEV;
		return kfd_mmio_mmap(dev, process, vma);
2093 2094 2095
	}

	return -EFAULT;
2096
}