Merge branch 'drm-sti-next-add-dvo' of git://git.linaro.org/people/benjamin.gaignard...
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / amd / amdkfd / kfd_chardev.c
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>
34 #include <linux/uaccess.h>
35 #include <uapi/asm-generic/mman-common.h>
36 #include <asm/processor.h>
37 #include "kfd_priv.h"
38 #include "kfd_device_queue_manager.h"
39
40 static long kfd_ioctl(struct file *, unsigned int, unsigned long);
41 static int kfd_open(struct inode *, struct file *);
42 static int kfd_mmap(struct file *, struct vm_area_struct *);
43
44 static const char kfd_dev_name[] = "kfd";
45
46 static const struct file_operations kfd_fops = {
47         .owner = THIS_MODULE,
48         .unlocked_ioctl = kfd_ioctl,
49         .compat_ioctl = kfd_ioctl,
50         .open = kfd_open,
51         .mmap = kfd_mmap,
52 };
53
54 static int kfd_char_dev_major = -1;
55 static struct class *kfd_class;
56 struct device *kfd_device;
57
58 int kfd_chardev_init(void)
59 {
60         int err = 0;
61
62         kfd_char_dev_major = register_chrdev(0, kfd_dev_name, &kfd_fops);
63         err = kfd_char_dev_major;
64         if (err < 0)
65                 goto err_register_chrdev;
66
67         kfd_class = class_create(THIS_MODULE, kfd_dev_name);
68         err = PTR_ERR(kfd_class);
69         if (IS_ERR(kfd_class))
70                 goto err_class_create;
71
72         kfd_device = device_create(kfd_class, NULL,
73                                         MKDEV(kfd_char_dev_major, 0),
74                                         NULL, kfd_dev_name);
75         err = PTR_ERR(kfd_device);
76         if (IS_ERR(kfd_device))
77                 goto err_device_create;
78
79         return 0;
80
81 err_device_create:
82         class_destroy(kfd_class);
83 err_class_create:
84         unregister_chrdev(kfd_char_dev_major, kfd_dev_name);
85 err_register_chrdev:
86         return err;
87 }
88
89 void kfd_chardev_exit(void)
90 {
91         device_destroy(kfd_class, MKDEV(kfd_char_dev_major, 0));
92         class_destroy(kfd_class);
93         unregister_chrdev(kfd_char_dev_major, kfd_dev_name);
94 }
95
96 struct device *kfd_chardev(void)
97 {
98         return kfd_device;
99 }
100
101
102 static int kfd_open(struct inode *inode, struct file *filep)
103 {
104         struct kfd_process *process;
105         bool is_32bit_user_mode;
106
107         if (iminor(inode) != 0)
108                 return -ENODEV;
109
110         is_32bit_user_mode = is_compat_task();
111
112         if (is_32bit_user_mode == true) {
113                 dev_warn(kfd_device,
114                         "Process %d (32-bit) failed to open /dev/kfd\n"
115                         "32-bit processes are not supported by amdkfd\n",
116                         current->pid);
117                 return -EPERM;
118         }
119
120         process = kfd_create_process(current);
121         if (IS_ERR(process))
122                 return PTR_ERR(process);
123
124         dev_dbg(kfd_device, "process %d opened, compat mode (32 bit) - %d\n",
125                 process->pasid, process->is_32bit_user_mode);
126
127         return 0;
128 }
129
130 static long kfd_ioctl_get_version(struct file *filep, struct kfd_process *p,
131                                         void __user *arg)
132 {
133         struct kfd_ioctl_get_version_args args;
134         int err = 0;
135
136         args.major_version = KFD_IOCTL_MAJOR_VERSION;
137         args.minor_version = KFD_IOCTL_MINOR_VERSION;
138
139         if (copy_to_user(arg, &args, sizeof(args)))
140                 err = -EFAULT;
141
142         return err;
143 }
144
145 static int set_queue_properties_from_user(struct queue_properties *q_properties,
146                                 struct kfd_ioctl_create_queue_args *args)
147 {
148         void *tmp;
149
150         if (args->queue_percentage > KFD_MAX_QUEUE_PERCENTAGE) {
151                 pr_err("kfd: queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
152                 return -EINVAL;
153         }
154
155         if (args->queue_priority > KFD_MAX_QUEUE_PRIORITY) {
156                 pr_err("kfd: queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
157                 return -EINVAL;
158         }
159
160         if ((args->ring_base_address) &&
161                 (!access_ok(VERIFY_WRITE,
162                         (const void __user *) args->ring_base_address,
163                         sizeof(uint64_t)))) {
164                 pr_err("kfd: can't access ring base address\n");
165                 return -EFAULT;
166         }
167
168         if (!is_power_of_2(args->ring_size) && (args->ring_size != 0)) {
169                 pr_err("kfd: ring size must be a power of 2 or 0\n");
170                 return -EINVAL;
171         }
172
173         if (!access_ok(VERIFY_WRITE,
174                         (const void __user *) args->read_pointer_address,
175                         sizeof(uint32_t))) {
176                 pr_err("kfd: can't access read pointer\n");
177                 return -EFAULT;
178         }
179
180         if (!access_ok(VERIFY_WRITE,
181                         (const void __user *) args->write_pointer_address,
182                         sizeof(uint32_t))) {
183                 pr_err("kfd: can't access write pointer\n");
184                 return -EFAULT;
185         }
186
187         tmp = (void *)(uintptr_t)args->eop_buffer_address;
188         if (tmp != NULL &&
189                 !access_ok(VERIFY_WRITE, tmp, sizeof(uint32_t))) {
190                 pr_debug("kfd: can't access eop buffer");
191                 return -EFAULT;
192         }
193
194         tmp = (void *)(uintptr_t)args->ctx_save_restore_address;
195         if (tmp != NULL &&
196                 !access_ok(VERIFY_WRITE, tmp, sizeof(uint32_t))) {
197                 pr_debug("kfd: can't access ctx save restore buffer");
198                 return -EFAULT;
199         }
200
201         q_properties->is_interop = false;
202         q_properties->queue_percent = args->queue_percentage;
203         q_properties->priority = args->queue_priority;
204         q_properties->queue_address = args->ring_base_address;
205         q_properties->queue_size = args->ring_size;
206         q_properties->read_ptr = (uint32_t *) args->read_pointer_address;
207         q_properties->write_ptr = (uint32_t *) args->write_pointer_address;
208         q_properties->eop_ring_buffer_address = args->eop_buffer_address;
209         q_properties->eop_ring_buffer_size = args->eop_buffer_size;
210         q_properties->ctx_save_restore_area_address =
211                         args->ctx_save_restore_address;
212         q_properties->ctx_save_restore_area_size = args->ctx_save_restore_size;
213         if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE ||
214                 args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL)
215                 q_properties->type = KFD_QUEUE_TYPE_COMPUTE;
216         else if (args->queue_type == KFD_IOC_QUEUE_TYPE_SDMA)
217                 q_properties->type = KFD_QUEUE_TYPE_SDMA;
218         else
219                 return -ENOTSUPP;
220
221         if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL)
222                 q_properties->format = KFD_QUEUE_FORMAT_AQL;
223         else
224                 q_properties->format = KFD_QUEUE_FORMAT_PM4;
225
226         pr_debug("Queue Percentage (%d, %d)\n",
227                         q_properties->queue_percent, args->queue_percentage);
228
229         pr_debug("Queue Priority (%d, %d)\n",
230                         q_properties->priority, args->queue_priority);
231
232         pr_debug("Queue Address (0x%llX, 0x%llX)\n",
233                         q_properties->queue_address, args->ring_base_address);
234
235         pr_debug("Queue Size (0x%llX, %u)\n",
236                         q_properties->queue_size, args->ring_size);
237
238         pr_debug("Queue r/w Pointers (0x%llX, 0x%llX)\n",
239                         (uint64_t) q_properties->read_ptr,
240                         (uint64_t) q_properties->write_ptr);
241
242         pr_debug("Queue Format (%d)\n", q_properties->format);
243
244         pr_debug("Queue EOP (0x%llX)\n", q_properties->eop_ring_buffer_address);
245
246         pr_debug("Queue CTX save arex (0x%llX)\n",
247                         q_properties->ctx_save_restore_area_address);
248
249         return 0;
250 }
251
252 static long kfd_ioctl_create_queue(struct file *filep, struct kfd_process *p,
253                                         void __user *arg)
254 {
255         struct kfd_ioctl_create_queue_args args;
256         struct kfd_dev *dev;
257         int err = 0;
258         unsigned int queue_id;
259         struct kfd_process_device *pdd;
260         struct queue_properties q_properties;
261
262         memset(&q_properties, 0, sizeof(struct queue_properties));
263
264         if (copy_from_user(&args, arg, sizeof(args)))
265                 return -EFAULT;
266
267         pr_debug("kfd: creating queue ioctl\n");
268
269         err = set_queue_properties_from_user(&q_properties, &args);
270         if (err)
271                 return err;
272
273         pr_debug("kfd: looking for gpu id 0x%x\n", args.gpu_id);
274         dev = kfd_device_by_id(args.gpu_id);
275         if (dev == NULL) {
276                 pr_debug("kfd: gpu id 0x%x was not found\n", args.gpu_id);
277                 return -EINVAL;
278         }
279
280         mutex_lock(&p->mutex);
281
282         pdd = kfd_bind_process_to_device(dev, p);
283         if (IS_ERR(pdd)) {
284                 err = PTR_ERR(pdd);
285                 goto err_bind_process;
286         }
287
288         pr_debug("kfd: creating queue for PASID %d on GPU 0x%x\n",
289                         p->pasid,
290                         dev->id);
291
292         err = pqm_create_queue(&p->pqm, dev, filep, &q_properties,
293                                 0, q_properties.type, &queue_id);
294         if (err != 0)
295                 goto err_create_queue;
296
297         args.queue_id = queue_id;
298
299         /* Return gpu_id as doorbell offset for mmap usage */
300         args.doorbell_offset = args.gpu_id << PAGE_SHIFT;
301
302         if (copy_to_user(arg, &args, sizeof(args))) {
303                 err = -EFAULT;
304                 goto err_copy_args_out;
305         }
306
307         mutex_unlock(&p->mutex);
308
309         pr_debug("kfd: queue id %d was created successfully\n", args.queue_id);
310
311         pr_debug("ring buffer address == 0x%016llX\n",
312                         args.ring_base_address);
313
314         pr_debug("read ptr address    == 0x%016llX\n",
315                         args.read_pointer_address);
316
317         pr_debug("write ptr address   == 0x%016llX\n",
318                         args.write_pointer_address);
319
320         return 0;
321
322 err_copy_args_out:
323         pqm_destroy_queue(&p->pqm, queue_id);
324 err_create_queue:
325 err_bind_process:
326         mutex_unlock(&p->mutex);
327         return err;
328 }
329
330 static int kfd_ioctl_destroy_queue(struct file *filp, struct kfd_process *p,
331                                         void __user *arg)
332 {
333         int retval;
334         struct kfd_ioctl_destroy_queue_args args;
335
336         if (copy_from_user(&args, arg, sizeof(args)))
337                 return -EFAULT;
338
339         pr_debug("kfd: destroying queue id %d for PASID %d\n",
340                                 args.queue_id,
341                                 p->pasid);
342
343         mutex_lock(&p->mutex);
344
345         retval = pqm_destroy_queue(&p->pqm, args.queue_id);
346
347         mutex_unlock(&p->mutex);
348         return retval;
349 }
350
351 static int kfd_ioctl_update_queue(struct file *filp, struct kfd_process *p,
352                                         void __user *arg)
353 {
354         int retval;
355         struct kfd_ioctl_update_queue_args args;
356         struct queue_properties properties;
357
358         if (copy_from_user(&args, arg, sizeof(args)))
359                 return -EFAULT;
360
361         if (args.queue_percentage > KFD_MAX_QUEUE_PERCENTAGE) {
362                 pr_err("kfd: queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
363                 return -EINVAL;
364         }
365
366         if (args.queue_priority > KFD_MAX_QUEUE_PRIORITY) {
367                 pr_err("kfd: queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
368                 return -EINVAL;
369         }
370
371         if ((args.ring_base_address) &&
372                 (!access_ok(VERIFY_WRITE,
373                         (const void __user *) args.ring_base_address,
374                         sizeof(uint64_t)))) {
375                 pr_err("kfd: can't access ring base address\n");
376                 return -EFAULT;
377         }
378
379         if (!is_power_of_2(args.ring_size) && (args.ring_size != 0)) {
380                 pr_err("kfd: ring size must be a power of 2 or 0\n");
381                 return -EINVAL;
382         }
383
384         properties.queue_address = args.ring_base_address;
385         properties.queue_size = args.ring_size;
386         properties.queue_percent = args.queue_percentage;
387         properties.priority = args.queue_priority;
388
389         pr_debug("kfd: updating queue id %d for PASID %d\n",
390                         args.queue_id, p->pasid);
391
392         mutex_lock(&p->mutex);
393
394         retval = pqm_update_queue(&p->pqm, args.queue_id, &properties);
395
396         mutex_unlock(&p->mutex);
397
398         return retval;
399 }
400
401 static long kfd_ioctl_set_memory_policy(struct file *filep,
402                                 struct kfd_process *p, void __user *arg)
403 {
404         struct kfd_ioctl_set_memory_policy_args args;
405         struct kfd_dev *dev;
406         int err = 0;
407         struct kfd_process_device *pdd;
408         enum cache_policy default_policy, alternate_policy;
409
410         if (copy_from_user(&args, arg, sizeof(args)))
411                 return -EFAULT;
412
413         if (args.default_policy != KFD_IOC_CACHE_POLICY_COHERENT
414             && args.default_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) {
415                 return -EINVAL;
416         }
417
418         if (args.alternate_policy != KFD_IOC_CACHE_POLICY_COHERENT
419             && args.alternate_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) {
420                 return -EINVAL;
421         }
422
423         dev = kfd_device_by_id(args.gpu_id);
424         if (dev == NULL)
425                 return -EINVAL;
426
427         mutex_lock(&p->mutex);
428
429         pdd = kfd_bind_process_to_device(dev, p);
430         if (IS_ERR(pdd)) {
431                 err = PTR_ERR(pdd);
432                 goto out;
433         }
434
435         default_policy = (args.default_policy == KFD_IOC_CACHE_POLICY_COHERENT)
436                          ? cache_policy_coherent : cache_policy_noncoherent;
437
438         alternate_policy =
439                 (args.alternate_policy == KFD_IOC_CACHE_POLICY_COHERENT)
440                    ? cache_policy_coherent : cache_policy_noncoherent;
441
442         if (!dev->dqm->ops.set_cache_memory_policy(dev->dqm,
443                                 &pdd->qpd,
444                                 default_policy,
445                                 alternate_policy,
446                                 (void __user *)args.alternate_aperture_base,
447                                 args.alternate_aperture_size))
448                 err = -EINVAL;
449
450 out:
451         mutex_unlock(&p->mutex);
452
453         return err;
454 }
455
456 static long kfd_ioctl_get_clock_counters(struct file *filep,
457                                 struct kfd_process *p, void __user *arg)
458 {
459         struct kfd_ioctl_get_clock_counters_args args;
460         struct kfd_dev *dev;
461         struct timespec time;
462
463         if (copy_from_user(&args, arg, sizeof(args)))
464                 return -EFAULT;
465
466         dev = kfd_device_by_id(args.gpu_id);
467         if (dev == NULL)
468                 return -EINVAL;
469
470         /* Reading GPU clock counter from KGD */
471         args.gpu_clock_counter = kfd2kgd->get_gpu_clock_counter(dev->kgd);
472
473         /* No access to rdtsc. Using raw monotonic time */
474         getrawmonotonic(&time);
475         args.cpu_clock_counter = (uint64_t)timespec_to_ns(&time);
476
477         get_monotonic_boottime(&time);
478         args.system_clock_counter = (uint64_t)timespec_to_ns(&time);
479
480         /* Since the counter is in nano-seconds we use 1GHz frequency */
481         args.system_clock_freq = 1000000000;
482
483         if (copy_to_user(arg, &args, sizeof(args)))
484                 return -EFAULT;
485
486         return 0;
487 }
488
489
490 static int kfd_ioctl_get_process_apertures(struct file *filp,
491                                 struct kfd_process *p, void __user *arg)
492 {
493         struct kfd_ioctl_get_process_apertures_args args;
494         struct kfd_process_device_apertures *pAperture;
495         struct kfd_process_device *pdd;
496
497         dev_dbg(kfd_device, "get apertures for PASID %d", p->pasid);
498
499         if (copy_from_user(&args, arg, sizeof(args)))
500                 return -EFAULT;
501
502         args.num_of_nodes = 0;
503
504         mutex_lock(&p->mutex);
505
506         /*if the process-device list isn't empty*/
507         if (kfd_has_process_device_data(p)) {
508                 /* Run over all pdd of the process */
509                 pdd = kfd_get_first_process_device_data(p);
510                 do {
511                         pAperture = &args.process_apertures[args.num_of_nodes];
512                         pAperture->gpu_id = pdd->dev->id;
513                         pAperture->lds_base = pdd->lds_base;
514                         pAperture->lds_limit = pdd->lds_limit;
515                         pAperture->gpuvm_base = pdd->gpuvm_base;
516                         pAperture->gpuvm_limit = pdd->gpuvm_limit;
517                         pAperture->scratch_base = pdd->scratch_base;
518                         pAperture->scratch_limit = pdd->scratch_limit;
519
520                         dev_dbg(kfd_device,
521                                 "node id %u\n", args.num_of_nodes);
522                         dev_dbg(kfd_device,
523                                 "gpu id %u\n", pdd->dev->id);
524                         dev_dbg(kfd_device,
525                                 "lds_base %llX\n", pdd->lds_base);
526                         dev_dbg(kfd_device,
527                                 "lds_limit %llX\n", pdd->lds_limit);
528                         dev_dbg(kfd_device,
529                                 "gpuvm_base %llX\n", pdd->gpuvm_base);
530                         dev_dbg(kfd_device,
531                                 "gpuvm_limit %llX\n", pdd->gpuvm_limit);
532                         dev_dbg(kfd_device,
533                                 "scratch_base %llX\n", pdd->scratch_base);
534                         dev_dbg(kfd_device,
535                                 "scratch_limit %llX\n", pdd->scratch_limit);
536
537                         args.num_of_nodes++;
538                 } while ((pdd = kfd_get_next_process_device_data(p, pdd)) != NULL &&
539                                 (args.num_of_nodes < NUM_OF_SUPPORTED_GPUS));
540         }
541
542         mutex_unlock(&p->mutex);
543
544         if (copy_to_user(arg, &args, sizeof(args)))
545                 return -EFAULT;
546
547         return 0;
548 }
549
550 static long kfd_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
551 {
552         struct kfd_process *process;
553         long err = -EINVAL;
554
555         dev_dbg(kfd_device,
556                 "ioctl cmd 0x%x (#%d), arg 0x%lx\n",
557                 cmd, _IOC_NR(cmd), arg);
558
559         process = kfd_get_process(current);
560         if (IS_ERR(process))
561                 return PTR_ERR(process);
562
563         switch (cmd) {
564         case KFD_IOC_GET_VERSION:
565                 err = kfd_ioctl_get_version(filep, process, (void __user *)arg);
566                 break;
567         case KFD_IOC_CREATE_QUEUE:
568                 err = kfd_ioctl_create_queue(filep, process,
569                                                 (void __user *)arg);
570                 break;
571
572         case KFD_IOC_DESTROY_QUEUE:
573                 err = kfd_ioctl_destroy_queue(filep, process,
574                                                 (void __user *)arg);
575                 break;
576
577         case KFD_IOC_SET_MEMORY_POLICY:
578                 err = kfd_ioctl_set_memory_policy(filep, process,
579                                                 (void __user *)arg);
580                 break;
581
582         case KFD_IOC_GET_CLOCK_COUNTERS:
583                 err = kfd_ioctl_get_clock_counters(filep, process,
584                                                 (void __user *)arg);
585                 break;
586
587         case KFD_IOC_GET_PROCESS_APERTURES:
588                 err = kfd_ioctl_get_process_apertures(filep, process,
589                                                 (void __user *)arg);
590                 break;
591
592         case KFD_IOC_UPDATE_QUEUE:
593                 err = kfd_ioctl_update_queue(filep, process,
594                                                 (void __user *)arg);
595                 break;
596
597         default:
598                 dev_err(kfd_device,
599                         "unknown ioctl cmd 0x%x, arg 0x%lx)\n",
600                         cmd, arg);
601                 err = -EINVAL;
602                 break;
603         }
604
605         if (err < 0)
606                 dev_err(kfd_device,
607                         "ioctl error %ld for ioctl cmd 0x%x (#%d)\n",
608                         err, cmd, _IOC_NR(cmd));
609
610         return err;
611 }
612
613 static int kfd_mmap(struct file *filp, struct vm_area_struct *vma)
614 {
615         struct kfd_process *process;
616
617         process = kfd_get_process(current);
618         if (IS_ERR(process))
619                 return PTR_ERR(process);
620
621         return kfd_doorbell_mmap(process, vma);
622 }