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