drm/amdkfd: add events IOCTL set definitions
[linux-2.6-block.git] / drivers / gpu / drm / amd / amdkfd / kfd_chardev.c
CommitLineData
4a488a7a
OG
1/*
2 * Copyright 2014 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23#include <linux/device.h>
24#include <linux/export.h>
25#include <linux/err.h>
26#include <linux/fs.h>
27#include <linux/sched.h>
28#include <linux/slab.h>
29#include <linux/uaccess.h>
30#include <linux/compat.h>
31#include <uapi/linux/kfd_ioctl.h>
32#include <linux/time.h>
33#include <linux/mm.h>
4a488a7a
OG
34#include <uapi/asm-generic/mman-common.h>
35#include <asm/processor.h>
36#include "kfd_priv.h"
41a286fa 37#include "kfd_device_queue_manager.h"
4a488a7a
OG
38
39static long kfd_ioctl(struct file *, unsigned int, unsigned long);
40static int kfd_open(struct inode *, struct file *);
19f6d2a6 41static int kfd_mmap(struct file *, struct vm_area_struct *);
4a488a7a
OG
42
43static const char kfd_dev_name[] = "kfd";
44
45static const struct file_operations kfd_fops = {
46 .owner = THIS_MODULE,
47 .unlocked_ioctl = kfd_ioctl,
48 .compat_ioctl = kfd_ioctl,
49 .open = kfd_open,
19f6d2a6 50 .mmap = kfd_mmap,
4a488a7a
OG
51};
52
53static int kfd_char_dev_major = -1;
54static struct class *kfd_class;
55struct device *kfd_device;
56
57int kfd_chardev_init(void)
58{
59 int err = 0;
60
61 kfd_char_dev_major = register_chrdev(0, kfd_dev_name, &kfd_fops);
62 err = kfd_char_dev_major;
63 if (err < 0)
64 goto err_register_chrdev;
65
66 kfd_class = class_create(THIS_MODULE, kfd_dev_name);
67 err = PTR_ERR(kfd_class);
68 if (IS_ERR(kfd_class))
69 goto err_class_create;
70
71 kfd_device = device_create(kfd_class, NULL,
72 MKDEV(kfd_char_dev_major, 0),
73 NULL, kfd_dev_name);
74 err = PTR_ERR(kfd_device);
75 if (IS_ERR(kfd_device))
76 goto err_device_create;
77
78 return 0;
79
80err_device_create:
81 class_destroy(kfd_class);
82err_class_create:
83 unregister_chrdev(kfd_char_dev_major, kfd_dev_name);
84err_register_chrdev:
85 return err;
86}
87
88void kfd_chardev_exit(void)
89{
90 device_destroy(kfd_class, MKDEV(kfd_char_dev_major, 0));
91 class_destroy(kfd_class);
92 unregister_chrdev(kfd_char_dev_major, kfd_dev_name);
93}
94
95struct device *kfd_chardev(void)
96{
97 return kfd_device;
98}
99
100
101static int kfd_open(struct inode *inode, struct file *filep)
102{
19f6d2a6 103 struct kfd_process *process;
a18069c1 104 bool is_32bit_user_mode;
19f6d2a6 105
4a488a7a
OG
106 if (iminor(inode) != 0)
107 return -ENODEV;
108
a18069c1
OG
109 is_32bit_user_mode = is_compat_task();
110
111 if (is_32bit_user_mode == true) {
112 dev_warn(kfd_device,
113 "Process %d (32-bit) failed to open /dev/kfd\n"
114 "32-bit processes are not supported by amdkfd\n",
115 current->pid);
116 return -EPERM;
117 }
118
19f6d2a6
OG
119 process = kfd_create_process(current);
120 if (IS_ERR(process))
121 return PTR_ERR(process);
122
19f6d2a6
OG
123 dev_dbg(kfd_device, "process %d opened, compat mode (32 bit) - %d\n",
124 process->pasid, process->is_32bit_user_mode);
125
4a488a7a
OG
126 return 0;
127}
128
524a6404
OG
129static int kfd_ioctl_get_version(struct file *filep, struct kfd_process *p,
130 void *data)
4a488a7a 131{
524a6404 132 struct kfd_ioctl_get_version_args *args = data;
ecd5c982
OG
133 int err = 0;
134
524a6404
OG
135 args->major_version = KFD_IOCTL_MAJOR_VERSION;
136 args->minor_version = KFD_IOCTL_MINOR_VERSION;
ecd5c982
OG
137
138 return err;
4a488a7a
OG
139}
140
39b027d9
OG
141static int set_queue_properties_from_user(struct queue_properties *q_properties,
142 struct kfd_ioctl_create_queue_args *args)
143{
144 if (args->queue_percentage > KFD_MAX_QUEUE_PERCENTAGE) {
145 pr_err("kfd: queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
146 return -EINVAL;
147 }
148
149 if (args->queue_priority > KFD_MAX_QUEUE_PRIORITY) {
150 pr_err("kfd: queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
151 return -EINVAL;
152 }
153
154 if ((args->ring_base_address) &&
4307d8f6
OG
155 (!access_ok(VERIFY_WRITE,
156 (const void __user *) args->ring_base_address,
157 sizeof(uint64_t)))) {
39b027d9
OG
158 pr_err("kfd: can't access ring base address\n");
159 return -EFAULT;
160 }
161
162 if (!is_power_of_2(args->ring_size) && (args->ring_size != 0)) {
163 pr_err("kfd: ring size must be a power of 2 or 0\n");
164 return -EINVAL;
165 }
166
4307d8f6
OG
167 if (!access_ok(VERIFY_WRITE,
168 (const void __user *) args->read_pointer_address,
169 sizeof(uint32_t))) {
39b027d9
OG
170 pr_err("kfd: can't access read pointer\n");
171 return -EFAULT;
172 }
173
4307d8f6
OG
174 if (!access_ok(VERIFY_WRITE,
175 (const void __user *) args->write_pointer_address,
176 sizeof(uint32_t))) {
39b027d9
OG
177 pr_err("kfd: can't access write pointer\n");
178 return -EFAULT;
179 }
180
0b3674ae
OG
181 if (args->eop_buffer_address &&
182 !access_ok(VERIFY_WRITE,
183 (const void __user *) args->eop_buffer_address,
184 sizeof(uint32_t))) {
ff3d04a1
BG
185 pr_debug("kfd: can't access eop buffer");
186 return -EFAULT;
187 }
188
0b3674ae
OG
189 if (args->ctx_save_restore_address &&
190 !access_ok(VERIFY_WRITE,
191 (const void __user *) args->ctx_save_restore_address,
192 sizeof(uint32_t))) {
ff3d04a1
BG
193 pr_debug("kfd: can't access ctx save restore buffer");
194 return -EFAULT;
195 }
196
39b027d9
OG
197 q_properties->is_interop = false;
198 q_properties->queue_percent = args->queue_percentage;
199 q_properties->priority = args->queue_priority;
200 q_properties->queue_address = args->ring_base_address;
201 q_properties->queue_size = args->ring_size;
202 q_properties->read_ptr = (uint32_t *) args->read_pointer_address;
203 q_properties->write_ptr = (uint32_t *) args->write_pointer_address;
ff3d04a1
BG
204 q_properties->eop_ring_buffer_address = args->eop_buffer_address;
205 q_properties->eop_ring_buffer_size = args->eop_buffer_size;
206 q_properties->ctx_save_restore_area_address =
207 args->ctx_save_restore_address;
208 q_properties->ctx_save_restore_area_size = args->ctx_save_restore_size;
39b027d9
OG
209 if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE ||
210 args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL)
211 q_properties->type = KFD_QUEUE_TYPE_COMPUTE;
3385f9dd
BG
212 else if (args->queue_type == KFD_IOC_QUEUE_TYPE_SDMA)
213 q_properties->type = KFD_QUEUE_TYPE_SDMA;
39b027d9
OG
214 else
215 return -ENOTSUPP;
216
217 if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL)
218 q_properties->format = KFD_QUEUE_FORMAT_AQL;
219 else
220 q_properties->format = KFD_QUEUE_FORMAT_PM4;
221
222 pr_debug("Queue Percentage (%d, %d)\n",
223 q_properties->queue_percent, args->queue_percentage);
224
225 pr_debug("Queue Priority (%d, %d)\n",
226 q_properties->priority, args->queue_priority);
227
228 pr_debug("Queue Address (0x%llX, 0x%llX)\n",
229 q_properties->queue_address, args->ring_base_address);
230
231 pr_debug("Queue Size (0x%llX, %u)\n",
232 q_properties->queue_size, args->ring_size);
233
234 pr_debug("Queue r/w Pointers (0x%llX, 0x%llX)\n",
235 (uint64_t) q_properties->read_ptr,
236 (uint64_t) q_properties->write_ptr);
237
238 pr_debug("Queue Format (%d)\n", q_properties->format);
239
ff3d04a1
BG
240 pr_debug("Queue EOP (0x%llX)\n", q_properties->eop_ring_buffer_address);
241
242 pr_debug("Queue CTX save arex (0x%llX)\n",
243 q_properties->ctx_save_restore_area_address);
244
39b027d9
OG
245 return 0;
246}
247
524a6404
OG
248static int kfd_ioctl_create_queue(struct file *filep, struct kfd_process *p,
249 void *data)
4a488a7a 250{
524a6404 251 struct kfd_ioctl_create_queue_args *args = data;
39b027d9
OG
252 struct kfd_dev *dev;
253 int err = 0;
254 unsigned int queue_id;
255 struct kfd_process_device *pdd;
256 struct queue_properties q_properties;
257
258 memset(&q_properties, 0, sizeof(struct queue_properties));
259
39b027d9
OG
260 pr_debug("kfd: creating queue ioctl\n");
261
524a6404 262 err = set_queue_properties_from_user(&q_properties, args);
39b027d9
OG
263 if (err)
264 return err;
265
281d1bbd 266 pr_debug("kfd: looking for gpu id 0x%x\n", args->gpu_id);
524a6404 267 dev = kfd_device_by_id(args->gpu_id);
ff3d04a1 268 if (dev == NULL) {
281d1bbd 269 pr_debug("kfd: gpu id 0x%x was not found\n", args->gpu_id);
39b027d9 270 return -EINVAL;
ff3d04a1 271 }
39b027d9
OG
272
273 mutex_lock(&p->mutex);
274
275 pdd = kfd_bind_process_to_device(dev, p);
66333cb3 276 if (IS_ERR(pdd)) {
524a6404 277 err = -ESRCH;
39b027d9
OG
278 goto err_bind_process;
279 }
280
281 pr_debug("kfd: creating queue for PASID %d on GPU 0x%x\n",
282 p->pasid,
283 dev->id);
284
85dfaef3
BG
285 err = pqm_create_queue(&p->pqm, dev, filep, &q_properties,
286 0, q_properties.type, &queue_id);
39b027d9
OG
287 if (err != 0)
288 goto err_create_queue;
289
524a6404 290 args->queue_id = queue_id;
39b027d9
OG
291
292 /* Return gpu_id as doorbell offset for mmap usage */
524a6404 293 args->doorbell_offset = args->gpu_id << PAGE_SHIFT;
39b027d9
OG
294
295 mutex_unlock(&p->mutex);
296
524a6404 297 pr_debug("kfd: queue id %d was created successfully\n", args->queue_id);
39b027d9
OG
298
299 pr_debug("ring buffer address == 0x%016llX\n",
524a6404 300 args->ring_base_address);
39b027d9
OG
301
302 pr_debug("read ptr address == 0x%016llX\n",
524a6404 303 args->read_pointer_address);
39b027d9
OG
304
305 pr_debug("write ptr address == 0x%016llX\n",
524a6404 306 args->write_pointer_address);
39b027d9
OG
307
308 return 0;
309
39b027d9
OG
310err_create_queue:
311err_bind_process:
312 mutex_unlock(&p->mutex);
313 return err;
4a488a7a
OG
314}
315
316static int kfd_ioctl_destroy_queue(struct file *filp, struct kfd_process *p,
524a6404 317 void *data)
4a488a7a 318{
39b027d9 319 int retval;
524a6404 320 struct kfd_ioctl_destroy_queue_args *args = data;
39b027d9
OG
321
322 pr_debug("kfd: destroying queue id %d for PASID %d\n",
524a6404 323 args->queue_id,
39b027d9
OG
324 p->pasid);
325
326 mutex_lock(&p->mutex);
327
524a6404 328 retval = pqm_destroy_queue(&p->pqm, args->queue_id);
39b027d9
OG
329
330 mutex_unlock(&p->mutex);
331 return retval;
4a488a7a
OG
332}
333
334static int kfd_ioctl_update_queue(struct file *filp, struct kfd_process *p,
524a6404 335 void *data)
4a488a7a 336{
39b027d9 337 int retval;
524a6404 338 struct kfd_ioctl_update_queue_args *args = data;
39b027d9
OG
339 struct queue_properties properties;
340
524a6404 341 if (args->queue_percentage > KFD_MAX_QUEUE_PERCENTAGE) {
39b027d9
OG
342 pr_err("kfd: queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
343 return -EINVAL;
344 }
345
524a6404 346 if (args->queue_priority > KFD_MAX_QUEUE_PRIORITY) {
39b027d9
OG
347 pr_err("kfd: queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
348 return -EINVAL;
349 }
350
524a6404 351 if ((args->ring_base_address) &&
4307d8f6 352 (!access_ok(VERIFY_WRITE,
524a6404 353 (const void __user *) args->ring_base_address,
4307d8f6 354 sizeof(uint64_t)))) {
39b027d9
OG
355 pr_err("kfd: can't access ring base address\n");
356 return -EFAULT;
357 }
358
524a6404 359 if (!is_power_of_2(args->ring_size) && (args->ring_size != 0)) {
39b027d9
OG
360 pr_err("kfd: ring size must be a power of 2 or 0\n");
361 return -EINVAL;
362 }
363
524a6404
OG
364 properties.queue_address = args->ring_base_address;
365 properties.queue_size = args->ring_size;
366 properties.queue_percent = args->queue_percentage;
367 properties.priority = args->queue_priority;
39b027d9
OG
368
369 pr_debug("kfd: updating queue id %d for PASID %d\n",
524a6404 370 args->queue_id, p->pasid);
39b027d9
OG
371
372 mutex_lock(&p->mutex);
373
524a6404 374 retval = pqm_update_queue(&p->pqm, args->queue_id, &properties);
39b027d9
OG
375
376 mutex_unlock(&p->mutex);
377
378 return retval;
4a488a7a
OG
379}
380
524a6404
OG
381static int kfd_ioctl_set_memory_policy(struct file *filep,
382 struct kfd_process *p, void *data)
4a488a7a 383{
524a6404 384 struct kfd_ioctl_set_memory_policy_args *args = data;
41a286fa
AL
385 struct kfd_dev *dev;
386 int err = 0;
387 struct kfd_process_device *pdd;
388 enum cache_policy default_policy, alternate_policy;
389
524a6404
OG
390 if (args->default_policy != KFD_IOC_CACHE_POLICY_COHERENT
391 && args->default_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) {
41a286fa
AL
392 return -EINVAL;
393 }
394
524a6404
OG
395 if (args->alternate_policy != KFD_IOC_CACHE_POLICY_COHERENT
396 && args->alternate_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) {
41a286fa
AL
397 return -EINVAL;
398 }
399
524a6404 400 dev = kfd_device_by_id(args->gpu_id);
41a286fa
AL
401 if (dev == NULL)
402 return -EINVAL;
403
404 mutex_lock(&p->mutex);
405
406 pdd = kfd_bind_process_to_device(dev, p);
66333cb3 407 if (IS_ERR(pdd)) {
524a6404 408 err = -ESRCH;
41a286fa
AL
409 goto out;
410 }
411
524a6404 412 default_policy = (args->default_policy == KFD_IOC_CACHE_POLICY_COHERENT)
41a286fa
AL
413 ? cache_policy_coherent : cache_policy_noncoherent;
414
415 alternate_policy =
524a6404 416 (args->alternate_policy == KFD_IOC_CACHE_POLICY_COHERENT)
41a286fa
AL
417 ? cache_policy_coherent : cache_policy_noncoherent;
418
45c9a5e4 419 if (!dev->dqm->ops.set_cache_memory_policy(dev->dqm,
41a286fa
AL
420 &pdd->qpd,
421 default_policy,
422 alternate_policy,
524a6404
OG
423 (void __user *)args->alternate_aperture_base,
424 args->alternate_aperture_size))
41a286fa
AL
425 err = -EINVAL;
426
427out:
428 mutex_unlock(&p->mutex);
429
430 return err;
4a488a7a
OG
431}
432
524a6404
OG
433static int kfd_ioctl_get_clock_counters(struct file *filep,
434 struct kfd_process *p, void *data)
4a488a7a 435{
524a6404 436 struct kfd_ioctl_get_clock_counters_args *args = data;
4fac47c8 437 struct kfd_dev *dev;
affa7d86 438 struct timespec64 time;
4fac47c8 439
524a6404 440 dev = kfd_device_by_id(args->gpu_id);
4fac47c8
EP
441 if (dev == NULL)
442 return -EINVAL;
443
444 /* Reading GPU clock counter from KGD */
cea405b1
XZ
445 args->gpu_clock_counter =
446 dev->kfd2kgd->get_gpu_clock_counter(dev->kgd);
4fac47c8
EP
447
448 /* No access to rdtsc. Using raw monotonic time */
affa7d86
JS
449 getrawmonotonic64(&time);
450 args->cpu_clock_counter = (uint64_t)timespec64_to_ns(&time);
4fac47c8 451
affa7d86
JS
452 get_monotonic_boottime64(&time);
453 args->system_clock_counter = (uint64_t)timespec64_to_ns(&time);
4fac47c8
EP
454
455 /* Since the counter is in nano-seconds we use 1GHz frequency */
524a6404 456 args->system_clock_freq = 1000000000;
4fac47c8
EP
457
458 return 0;
4a488a7a
OG
459}
460
461
462static int kfd_ioctl_get_process_apertures(struct file *filp,
524a6404 463 struct kfd_process *p, void *data)
4a488a7a 464{
524a6404 465 struct kfd_ioctl_get_process_apertures_args *args = data;
775921ed
AS
466 struct kfd_process_device_apertures *pAperture;
467 struct kfd_process_device *pdd;
468
469 dev_dbg(kfd_device, "get apertures for PASID %d", p->pasid);
470
524a6404 471 args->num_of_nodes = 0;
775921ed
AS
472
473 mutex_lock(&p->mutex);
474
475 /*if the process-device list isn't empty*/
476 if (kfd_has_process_device_data(p)) {
477 /* Run over all pdd of the process */
478 pdd = kfd_get_first_process_device_data(p);
479 do {
524a6404
OG
480 pAperture =
481 &args->process_apertures[args->num_of_nodes];
775921ed
AS
482 pAperture->gpu_id = pdd->dev->id;
483 pAperture->lds_base = pdd->lds_base;
484 pAperture->lds_limit = pdd->lds_limit;
485 pAperture->gpuvm_base = pdd->gpuvm_base;
486 pAperture->gpuvm_limit = pdd->gpuvm_limit;
487 pAperture->scratch_base = pdd->scratch_base;
488 pAperture->scratch_limit = pdd->scratch_limit;
489
490 dev_dbg(kfd_device,
524a6404 491 "node id %u\n", args->num_of_nodes);
775921ed
AS
492 dev_dbg(kfd_device,
493 "gpu id %u\n", pdd->dev->id);
494 dev_dbg(kfd_device,
495 "lds_base %llX\n", pdd->lds_base);
496 dev_dbg(kfd_device,
497 "lds_limit %llX\n", pdd->lds_limit);
498 dev_dbg(kfd_device,
499 "gpuvm_base %llX\n", pdd->gpuvm_base);
500 dev_dbg(kfd_device,
501 "gpuvm_limit %llX\n", pdd->gpuvm_limit);
502 dev_dbg(kfd_device,
503 "scratch_base %llX\n", pdd->scratch_base);
504 dev_dbg(kfd_device,
505 "scratch_limit %llX\n", pdd->scratch_limit);
506
524a6404 507 args->num_of_nodes++;
775921ed 508 } while ((pdd = kfd_get_next_process_device_data(p, pdd)) != NULL &&
524a6404 509 (args->num_of_nodes < NUM_OF_SUPPORTED_GPUS));
775921ed
AS
510 }
511
512 mutex_unlock(&p->mutex);
513
775921ed 514 return 0;
4a488a7a
OG
515}
516
29a5d3eb
AL
517static int kfd_ioctl_create_event(struct file *filp, struct kfd_process *p,
518 void *data)
519{
520 return -ENODEV;
521}
522
523static int kfd_ioctl_destroy_event(struct file *filp, struct kfd_process *p,
524 void *data)
525{
526 return -ENODEV;
527}
528
529static int kfd_ioctl_set_event(struct file *filp, struct kfd_process *p,
530 void *data)
531{
532 return -ENODEV;
533}
534
535static int kfd_ioctl_reset_event(struct file *filp, struct kfd_process *p,
536 void *data)
537{
538 return -ENODEV;
539}
540
541static int kfd_ioctl_wait_events(struct file *filp, struct kfd_process *p,
542 void *data)
543{
544 return -ENODEV;
545}
546
76baee6c
OG
547#define AMDKFD_IOCTL_DEF(ioctl, _func, _flags) \
548 [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func, .flags = _flags, .cmd_drv = 0, .name = #ioctl}
549
550/** Ioctl table */
551static const struct amdkfd_ioctl_desc amdkfd_ioctls[] = {
552 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_VERSION,
553 kfd_ioctl_get_version, 0),
554
555 AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_QUEUE,
556 kfd_ioctl_create_queue, 0),
557
558 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_QUEUE,
559 kfd_ioctl_destroy_queue, 0),
560
561 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_MEMORY_POLICY,
562 kfd_ioctl_set_memory_policy, 0),
563
564 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_CLOCK_COUNTERS,
565 kfd_ioctl_get_clock_counters, 0),
566
567 AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_PROCESS_APERTURES,
568 kfd_ioctl_get_process_apertures, 0),
569
570 AMDKFD_IOCTL_DEF(AMDKFD_IOC_UPDATE_QUEUE,
571 kfd_ioctl_update_queue, 0),
29a5d3eb
AL
572
573 AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_EVENT,
574 kfd_ioctl_create_event, 0),
575
576 AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_EVENT,
577 kfd_ioctl_destroy_event, 0),
578
579 AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_EVENT,
580 kfd_ioctl_set_event, 0),
581
582 AMDKFD_IOCTL_DEF(AMDKFD_IOC_RESET_EVENT,
583 kfd_ioctl_reset_event, 0),
584
585 AMDKFD_IOCTL_DEF(AMDKFD_IOC_WAIT_EVENTS,
586 kfd_ioctl_wait_events, 0),
76baee6c
OG
587};
588
589#define AMDKFD_CORE_IOCTL_COUNT ARRAY_SIZE(amdkfd_ioctls)
590
4a488a7a
OG
591static long kfd_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
592{
593 struct kfd_process *process;
76baee6c
OG
594 amdkfd_ioctl_t *func;
595 const struct amdkfd_ioctl_desc *ioctl = NULL;
596 unsigned int nr = _IOC_NR(cmd);
524a6404
OG
597 char stack_kdata[128];
598 char *kdata = NULL;
599 unsigned int usize, asize;
600 int retcode = -EINVAL;
4a488a7a 601
76baee6c
OG
602 if (nr >= AMDKFD_CORE_IOCTL_COUNT)
603 goto err_i1;
604
605 if ((nr >= AMDKFD_COMMAND_START) && (nr < AMDKFD_COMMAND_END)) {
606 u32 amdkfd_size;
607
608 ioctl = &amdkfd_ioctls[nr];
609
610 amdkfd_size = _IOC_SIZE(ioctl->cmd);
611 usize = asize = _IOC_SIZE(cmd);
612 if (amdkfd_size > asize)
613 asize = amdkfd_size;
614
615 cmd = ioctl->cmd;
616 } else
617 goto err_i1;
618
619 dev_dbg(kfd_device, "ioctl cmd 0x%x (#%d), arg 0x%lx\n", cmd, nr, arg);
4a488a7a 620
19f6d2a6 621 process = kfd_get_process(current);
76baee6c
OG
622 if (IS_ERR(process)) {
623 dev_dbg(kfd_device, "no process\n");
624 goto err_i1;
625 }
4a488a7a 626
76baee6c
OG
627 /* Do not trust userspace, use our own definition */
628 func = ioctl->func;
629
630 if (unlikely(!func)) {
631 dev_dbg(kfd_device, "no function\n");
632 retcode = -EINVAL;
633 goto err_i1;
4a488a7a
OG
634 }
635
524a6404
OG
636 if (cmd & (IOC_IN | IOC_OUT)) {
637 if (asize <= sizeof(stack_kdata)) {
638 kdata = stack_kdata;
639 } else {
640 kdata = kmalloc(asize, GFP_KERNEL);
641 if (!kdata) {
642 retcode = -ENOMEM;
643 goto err_i1;
644 }
645 }
646 if (asize > usize)
647 memset(kdata + usize, 0, asize - usize);
648 }
4a488a7a 649
524a6404
OG
650 if (cmd & IOC_IN) {
651 if (copy_from_user(kdata, (void __user *)arg, usize) != 0) {
652 retcode = -EFAULT;
653 goto err_i1;
654 }
655 } else if (cmd & IOC_OUT) {
656 memset(kdata, 0, usize);
657 }
658
76baee6c 659 retcode = func(filep, process, kdata);
4a488a7a 660
524a6404
OG
661 if (cmd & IOC_OUT)
662 if (copy_to_user((void __user *)arg, kdata, usize) != 0)
663 retcode = -EFAULT;
4a488a7a 664
524a6404 665err_i1:
76baee6c
OG
666 if (!ioctl)
667 dev_dbg(kfd_device, "invalid ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n",
668 task_pid_nr(current), cmd, nr);
669
524a6404
OG
670 if (kdata != stack_kdata)
671 kfree(kdata);
672
673 if (retcode)
674 dev_dbg(kfd_device, "ret = %d\n", retcode);
675
676 return retcode;
4a488a7a 677}
19f6d2a6
OG
678
679static int kfd_mmap(struct file *filp, struct vm_area_struct *vma)
680{
681 struct kfd_process *process;
682
683 process = kfd_get_process(current);
684 if (IS_ERR(process))
685 return PTR_ERR(process);
686
687 return kfd_doorbell_mmap(process, vma);
688}