Merge tag 'sound-3.19-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai...
[cascardo/linux.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         if (args->queue_percentage > KFD_MAX_QUEUE_PERCENTAGE) {
149                 pr_err("kfd: queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
150                 return -EINVAL;
151         }
152
153         if (args->queue_priority > KFD_MAX_QUEUE_PRIORITY) {
154                 pr_err("kfd: queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
155                 return -EINVAL;
156         }
157
158         if ((args->ring_base_address) &&
159                 (!access_ok(VERIFY_WRITE,
160                         (const void __user *) args->ring_base_address,
161                         sizeof(uint64_t)))) {
162                 pr_err("kfd: can't access ring base address\n");
163                 return -EFAULT;
164         }
165
166         if (!is_power_of_2(args->ring_size) && (args->ring_size != 0)) {
167                 pr_err("kfd: ring size must be a power of 2 or 0\n");
168                 return -EINVAL;
169         }
170
171         if (!access_ok(VERIFY_WRITE,
172                         (const void __user *) args->read_pointer_address,
173                         sizeof(uint32_t))) {
174                 pr_err("kfd: can't access read pointer\n");
175                 return -EFAULT;
176         }
177
178         if (!access_ok(VERIFY_WRITE,
179                         (const void __user *) args->write_pointer_address,
180                         sizeof(uint32_t))) {
181                 pr_err("kfd: can't access write pointer\n");
182                 return -EFAULT;
183         }
184
185         q_properties->is_interop = false;
186         q_properties->queue_percent = args->queue_percentage;
187         q_properties->priority = args->queue_priority;
188         q_properties->queue_address = args->ring_base_address;
189         q_properties->queue_size = args->ring_size;
190         q_properties->read_ptr = (uint32_t *) args->read_pointer_address;
191         q_properties->write_ptr = (uint32_t *) args->write_pointer_address;
192         if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE ||
193                 args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL)
194                 q_properties->type = KFD_QUEUE_TYPE_COMPUTE;
195         else
196                 return -ENOTSUPP;
197
198         if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL)
199                 q_properties->format = KFD_QUEUE_FORMAT_AQL;
200         else
201                 q_properties->format = KFD_QUEUE_FORMAT_PM4;
202
203         pr_debug("Queue Percentage (%d, %d)\n",
204                         q_properties->queue_percent, args->queue_percentage);
205
206         pr_debug("Queue Priority (%d, %d)\n",
207                         q_properties->priority, args->queue_priority);
208
209         pr_debug("Queue Address (0x%llX, 0x%llX)\n",
210                         q_properties->queue_address, args->ring_base_address);
211
212         pr_debug("Queue Size (0x%llX, %u)\n",
213                         q_properties->queue_size, args->ring_size);
214
215         pr_debug("Queue r/w Pointers (0x%llX, 0x%llX)\n",
216                         (uint64_t) q_properties->read_ptr,
217                         (uint64_t) q_properties->write_ptr);
218
219         pr_debug("Queue Format (%d)\n", q_properties->format);
220
221         return 0;
222 }
223
224 static long kfd_ioctl_create_queue(struct file *filep, struct kfd_process *p,
225                                         void __user *arg)
226 {
227         struct kfd_ioctl_create_queue_args args;
228         struct kfd_dev *dev;
229         int err = 0;
230         unsigned int queue_id;
231         struct kfd_process_device *pdd;
232         struct queue_properties q_properties;
233
234         memset(&q_properties, 0, sizeof(struct queue_properties));
235
236         if (copy_from_user(&args, arg, sizeof(args)))
237                 return -EFAULT;
238
239         pr_debug("kfd: creating queue ioctl\n");
240
241         err = set_queue_properties_from_user(&q_properties, &args);
242         if (err)
243                 return err;
244
245         dev = kfd_device_by_id(args.gpu_id);
246         if (dev == NULL)
247                 return -EINVAL;
248
249         mutex_lock(&p->mutex);
250
251         pdd = kfd_bind_process_to_device(dev, p);
252         if (IS_ERR(pdd)) {
253                 err = PTR_ERR(pdd);
254                 goto err_bind_process;
255         }
256
257         pr_debug("kfd: creating queue for PASID %d on GPU 0x%x\n",
258                         p->pasid,
259                         dev->id);
260
261         err = pqm_create_queue(&p->pqm, dev, filep, &q_properties, 0,
262                                 KFD_QUEUE_TYPE_COMPUTE, &queue_id);
263         if (err != 0)
264                 goto err_create_queue;
265
266         args.queue_id = queue_id;
267
268         /* Return gpu_id as doorbell offset for mmap usage */
269         args.doorbell_offset = args.gpu_id << PAGE_SHIFT;
270
271         if (copy_to_user(arg, &args, sizeof(args))) {
272                 err = -EFAULT;
273                 goto err_copy_args_out;
274         }
275
276         mutex_unlock(&p->mutex);
277
278         pr_debug("kfd: queue id %d was created successfully\n", args.queue_id);
279
280         pr_debug("ring buffer address == 0x%016llX\n",
281                         args.ring_base_address);
282
283         pr_debug("read ptr address    == 0x%016llX\n",
284                         args.read_pointer_address);
285
286         pr_debug("write ptr address   == 0x%016llX\n",
287                         args.write_pointer_address);
288
289         return 0;
290
291 err_copy_args_out:
292         pqm_destroy_queue(&p->pqm, queue_id);
293 err_create_queue:
294 err_bind_process:
295         mutex_unlock(&p->mutex);
296         return err;
297 }
298
299 static int kfd_ioctl_destroy_queue(struct file *filp, struct kfd_process *p,
300                                         void __user *arg)
301 {
302         int retval;
303         struct kfd_ioctl_destroy_queue_args args;
304
305         if (copy_from_user(&args, arg, sizeof(args)))
306                 return -EFAULT;
307
308         pr_debug("kfd: destroying queue id %d for PASID %d\n",
309                                 args.queue_id,
310                                 p->pasid);
311
312         mutex_lock(&p->mutex);
313
314         retval = pqm_destroy_queue(&p->pqm, args.queue_id);
315
316         mutex_unlock(&p->mutex);
317         return retval;
318 }
319
320 static int kfd_ioctl_update_queue(struct file *filp, struct kfd_process *p,
321                                         void __user *arg)
322 {
323         int retval;
324         struct kfd_ioctl_update_queue_args args;
325         struct queue_properties properties;
326
327         if (copy_from_user(&args, arg, sizeof(args)))
328                 return -EFAULT;
329
330         if (args.queue_percentage > KFD_MAX_QUEUE_PERCENTAGE) {
331                 pr_err("kfd: queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
332                 return -EINVAL;
333         }
334
335         if (args.queue_priority > KFD_MAX_QUEUE_PRIORITY) {
336                 pr_err("kfd: queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
337                 return -EINVAL;
338         }
339
340         if ((args.ring_base_address) &&
341                 (!access_ok(VERIFY_WRITE,
342                         (const void __user *) args.ring_base_address,
343                         sizeof(uint64_t)))) {
344                 pr_err("kfd: can't access ring base address\n");
345                 return -EFAULT;
346         }
347
348         if (!is_power_of_2(args.ring_size) && (args.ring_size != 0)) {
349                 pr_err("kfd: ring size must be a power of 2 or 0\n");
350                 return -EINVAL;
351         }
352
353         properties.queue_address = args.ring_base_address;
354         properties.queue_size = args.ring_size;
355         properties.queue_percent = args.queue_percentage;
356         properties.priority = args.queue_priority;
357
358         pr_debug("kfd: updating queue id %d for PASID %d\n",
359                         args.queue_id, p->pasid);
360
361         mutex_lock(&p->mutex);
362
363         retval = pqm_update_queue(&p->pqm, args.queue_id, &properties);
364
365         mutex_unlock(&p->mutex);
366
367         return retval;
368 }
369
370 static long kfd_ioctl_set_memory_policy(struct file *filep,
371                                 struct kfd_process *p, void __user *arg)
372 {
373         struct kfd_ioctl_set_memory_policy_args args;
374         struct kfd_dev *dev;
375         int err = 0;
376         struct kfd_process_device *pdd;
377         enum cache_policy default_policy, alternate_policy;
378
379         if (copy_from_user(&args, arg, sizeof(args)))
380                 return -EFAULT;
381
382         if (args.default_policy != KFD_IOC_CACHE_POLICY_COHERENT
383             && args.default_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) {
384                 return -EINVAL;
385         }
386
387         if (args.alternate_policy != KFD_IOC_CACHE_POLICY_COHERENT
388             && args.alternate_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) {
389                 return -EINVAL;
390         }
391
392         dev = kfd_device_by_id(args.gpu_id);
393         if (dev == NULL)
394                 return -EINVAL;
395
396         mutex_lock(&p->mutex);
397
398         pdd = kfd_bind_process_to_device(dev, p);
399         if (IS_ERR(pdd)) {
400                 err = PTR_ERR(pdd);
401                 goto out;
402         }
403
404         default_policy = (args.default_policy == KFD_IOC_CACHE_POLICY_COHERENT)
405                          ? cache_policy_coherent : cache_policy_noncoherent;
406
407         alternate_policy =
408                 (args.alternate_policy == KFD_IOC_CACHE_POLICY_COHERENT)
409                    ? cache_policy_coherent : cache_policy_noncoherent;
410
411         if (!dev->dqm->set_cache_memory_policy(dev->dqm,
412                                 &pdd->qpd,
413                                 default_policy,
414                                 alternate_policy,
415                                 (void __user *)args.alternate_aperture_base,
416                                 args.alternate_aperture_size))
417                 err = -EINVAL;
418
419 out:
420         mutex_unlock(&p->mutex);
421
422         return err;
423 }
424
425 static long kfd_ioctl_get_clock_counters(struct file *filep,
426                                 struct kfd_process *p, void __user *arg)
427 {
428         struct kfd_ioctl_get_clock_counters_args args;
429         struct kfd_dev *dev;
430         struct timespec time;
431
432         if (copy_from_user(&args, arg, sizeof(args)))
433                 return -EFAULT;
434
435         dev = kfd_device_by_id(args.gpu_id);
436         if (dev == NULL)
437                 return -EINVAL;
438
439         /* Reading GPU clock counter from KGD */
440         args.gpu_clock_counter = kfd2kgd->get_gpu_clock_counter(dev->kgd);
441
442         /* No access to rdtsc. Using raw monotonic time */
443         getrawmonotonic(&time);
444         args.cpu_clock_counter = (uint64_t)timespec_to_ns(&time);
445
446         get_monotonic_boottime(&time);
447         args.system_clock_counter = (uint64_t)timespec_to_ns(&time);
448
449         /* Since the counter is in nano-seconds we use 1GHz frequency */
450         args.system_clock_freq = 1000000000;
451
452         if (copy_to_user(arg, &args, sizeof(args)))
453                 return -EFAULT;
454
455         return 0;
456 }
457
458
459 static int kfd_ioctl_get_process_apertures(struct file *filp,
460                                 struct kfd_process *p, void __user *arg)
461 {
462         struct kfd_ioctl_get_process_apertures_args args;
463         struct kfd_process_device_apertures *pAperture;
464         struct kfd_process_device *pdd;
465
466         dev_dbg(kfd_device, "get apertures for PASID %d", p->pasid);
467
468         if (copy_from_user(&args, arg, sizeof(args)))
469                 return -EFAULT;
470
471         args.num_of_nodes = 0;
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 {
480                         pAperture = &args.process_apertures[args.num_of_nodes];
481                         pAperture->gpu_id = pdd->dev->id;
482                         pAperture->lds_base = pdd->lds_base;
483                         pAperture->lds_limit = pdd->lds_limit;
484                         pAperture->gpuvm_base = pdd->gpuvm_base;
485                         pAperture->gpuvm_limit = pdd->gpuvm_limit;
486                         pAperture->scratch_base = pdd->scratch_base;
487                         pAperture->scratch_limit = pdd->scratch_limit;
488
489                         dev_dbg(kfd_device,
490                                 "node id %u\n", args.num_of_nodes);
491                         dev_dbg(kfd_device,
492                                 "gpu id %u\n", pdd->dev->id);
493                         dev_dbg(kfd_device,
494                                 "lds_base %llX\n", pdd->lds_base);
495                         dev_dbg(kfd_device,
496                                 "lds_limit %llX\n", pdd->lds_limit);
497                         dev_dbg(kfd_device,
498                                 "gpuvm_base %llX\n", pdd->gpuvm_base);
499                         dev_dbg(kfd_device,
500                                 "gpuvm_limit %llX\n", pdd->gpuvm_limit);
501                         dev_dbg(kfd_device,
502                                 "scratch_base %llX\n", pdd->scratch_base);
503                         dev_dbg(kfd_device,
504                                 "scratch_limit %llX\n", pdd->scratch_limit);
505
506                         args.num_of_nodes++;
507                 } while ((pdd = kfd_get_next_process_device_data(p, pdd)) != NULL &&
508                                 (args.num_of_nodes < NUM_OF_SUPPORTED_GPUS));
509         }
510
511         mutex_unlock(&p->mutex);
512
513         if (copy_to_user(arg, &args, sizeof(args)))
514                 return -EFAULT;
515
516         return 0;
517 }
518
519 static long kfd_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
520 {
521         struct kfd_process *process;
522         long err = -EINVAL;
523
524         dev_dbg(kfd_device,
525                 "ioctl cmd 0x%x (#%d), arg 0x%lx\n",
526                 cmd, _IOC_NR(cmd), arg);
527
528         process = kfd_get_process(current);
529         if (IS_ERR(process))
530                 return PTR_ERR(process);
531
532         switch (cmd) {
533         case KFD_IOC_GET_VERSION:
534                 err = kfd_ioctl_get_version(filep, process, (void __user *)arg);
535                 break;
536         case KFD_IOC_CREATE_QUEUE:
537                 err = kfd_ioctl_create_queue(filep, process,
538                                                 (void __user *)arg);
539                 break;
540
541         case KFD_IOC_DESTROY_QUEUE:
542                 err = kfd_ioctl_destroy_queue(filep, process,
543                                                 (void __user *)arg);
544                 break;
545
546         case KFD_IOC_SET_MEMORY_POLICY:
547                 err = kfd_ioctl_set_memory_policy(filep, process,
548                                                 (void __user *)arg);
549                 break;
550
551         case KFD_IOC_GET_CLOCK_COUNTERS:
552                 err = kfd_ioctl_get_clock_counters(filep, process,
553                                                 (void __user *)arg);
554                 break;
555
556         case KFD_IOC_GET_PROCESS_APERTURES:
557                 err = kfd_ioctl_get_process_apertures(filep, process,
558                                                 (void __user *)arg);
559                 break;
560
561         case KFD_IOC_UPDATE_QUEUE:
562                 err = kfd_ioctl_update_queue(filep, process,
563                                                 (void __user *)arg);
564                 break;
565
566         default:
567                 dev_err(kfd_device,
568                         "unknown ioctl cmd 0x%x, arg 0x%lx)\n",
569                         cmd, arg);
570                 err = -EINVAL;
571                 break;
572         }
573
574         if (err < 0)
575                 dev_err(kfd_device,
576                         "ioctl error %ld for ioctl cmd 0x%x (#%d)\n",
577                         err, cmd, _IOC_NR(cmd));
578
579         return err;
580 }
581
582 static int kfd_mmap(struct file *filp, struct vm_area_struct *vma)
583 {
584         struct kfd_process *process;
585
586         process = kfd_get_process(current);
587         if (IS_ERR(process))
588                 return PTR_ERR(process);
589
590         return kfd_doorbell_mmap(process, vma);
591 }