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