amdkfd: Implement the create/destroy/update queue IOCTLs
[linux-2.6-block.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
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
105         if (iminor(inode) != 0)
106                 return -ENODEV;
107
108         process = kfd_create_process(current);
109         if (IS_ERR(process))
110                 return PTR_ERR(process);
111
112         process->is_32bit_user_mode = is_compat_task();
113
114         dev_dbg(kfd_device, "process %d opened, compat mode (32 bit) - %d\n",
115                 process->pasid, process->is_32bit_user_mode);
116
117         kfd_init_apertures(process);
118
119         return 0;
120 }
121
122 static long kfd_ioctl_get_version(struct file *filep, struct kfd_process *p,
123                                         void __user *arg)
124 {
125         return -ENODEV;
126 }
127
128 static int set_queue_properties_from_user(struct queue_properties *q_properties,
129                                 struct kfd_ioctl_create_queue_args *args)
130 {
131         if (args->queue_percentage > KFD_MAX_QUEUE_PERCENTAGE) {
132                 pr_err("kfd: queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
133                 return -EINVAL;
134         }
135
136         if (args->queue_priority > KFD_MAX_QUEUE_PRIORITY) {
137                 pr_err("kfd: queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
138                 return -EINVAL;
139         }
140
141         if ((args->ring_base_address) &&
142                 (!access_ok(VERIFY_WRITE, args->ring_base_address, sizeof(uint64_t)))) {
143                 pr_err("kfd: can't access ring base address\n");
144                 return -EFAULT;
145         }
146
147         if (!is_power_of_2(args->ring_size) && (args->ring_size != 0)) {
148                 pr_err("kfd: ring size must be a power of 2 or 0\n");
149                 return -EINVAL;
150         }
151
152         if (!access_ok(VERIFY_WRITE, args->read_pointer_address, sizeof(uint32_t))) {
153                 pr_err("kfd: can't access read pointer\n");
154                 return -EFAULT;
155         }
156
157         if (!access_ok(VERIFY_WRITE, args->write_pointer_address, sizeof(uint32_t))) {
158                 pr_err("kfd: can't access write pointer\n");
159                 return -EFAULT;
160         }
161
162         q_properties->is_interop = false;
163         q_properties->queue_percent = args->queue_percentage;
164         q_properties->priority = args->queue_priority;
165         q_properties->queue_address = args->ring_base_address;
166         q_properties->queue_size = args->ring_size;
167         q_properties->read_ptr = (uint32_t *) args->read_pointer_address;
168         q_properties->write_ptr = (uint32_t *) args->write_pointer_address;
169         if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE ||
170                 args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL)
171                 q_properties->type = KFD_QUEUE_TYPE_COMPUTE;
172         else
173                 return -ENOTSUPP;
174
175         if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL)
176                 q_properties->format = KFD_QUEUE_FORMAT_AQL;
177         else
178                 q_properties->format = KFD_QUEUE_FORMAT_PM4;
179
180         pr_debug("Queue Percentage (%d, %d)\n",
181                         q_properties->queue_percent, args->queue_percentage);
182
183         pr_debug("Queue Priority (%d, %d)\n",
184                         q_properties->priority, args->queue_priority);
185
186         pr_debug("Queue Address (0x%llX, 0x%llX)\n",
187                         q_properties->queue_address, args->ring_base_address);
188
189         pr_debug("Queue Size (0x%llX, %u)\n",
190                         q_properties->queue_size, args->ring_size);
191
192         pr_debug("Queue r/w Pointers (0x%llX, 0x%llX)\n",
193                         (uint64_t) q_properties->read_ptr,
194                         (uint64_t) q_properties->write_ptr);
195
196         pr_debug("Queue Format (%d)\n", q_properties->format);
197
198         return 0;
199 }
200
201 static long kfd_ioctl_create_queue(struct file *filep, struct kfd_process *p,
202                                         void __user *arg)
203 {
204         struct kfd_ioctl_create_queue_args args;
205         struct kfd_dev *dev;
206         int err = 0;
207         unsigned int queue_id;
208         struct kfd_process_device *pdd;
209         struct queue_properties q_properties;
210
211         memset(&q_properties, 0, sizeof(struct queue_properties));
212
213         if (copy_from_user(&args, arg, sizeof(args)))
214                 return -EFAULT;
215
216         pr_debug("kfd: creating queue ioctl\n");
217
218         err = set_queue_properties_from_user(&q_properties, &args);
219         if (err)
220                 return err;
221
222         dev = kfd_device_by_id(args.gpu_id);
223         if (dev == NULL)
224                 return -EINVAL;
225
226         mutex_lock(&p->mutex);
227
228         pdd = kfd_bind_process_to_device(dev, p);
229         if (IS_ERR(pdd) < 0) {
230                 err = PTR_ERR(pdd);
231                 goto err_bind_process;
232         }
233
234         pr_debug("kfd: creating queue for PASID %d on GPU 0x%x\n",
235                         p->pasid,
236                         dev->id);
237
238         err = pqm_create_queue(&p->pqm, dev, filep, &q_properties, 0,
239                                 KFD_QUEUE_TYPE_COMPUTE, &queue_id);
240         if (err != 0)
241                 goto err_create_queue;
242
243         args.queue_id = queue_id;
244
245         /* Return gpu_id as doorbell offset for mmap usage */
246         args.doorbell_offset = args.gpu_id << PAGE_SHIFT;
247
248         if (copy_to_user(arg, &args, sizeof(args))) {
249                 err = -EFAULT;
250                 goto err_copy_args_out;
251         }
252
253         mutex_unlock(&p->mutex);
254
255         pr_debug("kfd: queue id %d was created successfully\n", args.queue_id);
256
257         pr_debug("ring buffer address == 0x%016llX\n",
258                         args.ring_base_address);
259
260         pr_debug("read ptr address    == 0x%016llX\n",
261                         args.read_pointer_address);
262
263         pr_debug("write ptr address   == 0x%016llX\n",
264                         args.write_pointer_address);
265
266         return 0;
267
268 err_copy_args_out:
269         pqm_destroy_queue(&p->pqm, queue_id);
270 err_create_queue:
271 err_bind_process:
272         mutex_unlock(&p->mutex);
273         return err;
274 }
275
276 static int kfd_ioctl_destroy_queue(struct file *filp, struct kfd_process *p,
277                                         void __user *arg)
278 {
279         int retval;
280         struct kfd_ioctl_destroy_queue_args args;
281
282         if (copy_from_user(&args, arg, sizeof(args)))
283                 return -EFAULT;
284
285         pr_debug("kfd: destroying queue id %d for PASID %d\n",
286                                 args.queue_id,
287                                 p->pasid);
288
289         mutex_lock(&p->mutex);
290
291         retval = pqm_destroy_queue(&p->pqm, args.queue_id);
292
293         mutex_unlock(&p->mutex);
294         return retval;
295 }
296
297 static int kfd_ioctl_update_queue(struct file *filp, struct kfd_process *p,
298                                         void __user *arg)
299 {
300         int retval;
301         struct kfd_ioctl_update_queue_args args;
302         struct queue_properties properties;
303
304         if (copy_from_user(&args, arg, sizeof(args)))
305                 return -EFAULT;
306
307         if (args.queue_percentage > KFD_MAX_QUEUE_PERCENTAGE) {
308                 pr_err("kfd: queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
309                 return -EINVAL;
310         }
311
312         if (args.queue_priority > KFD_MAX_QUEUE_PRIORITY) {
313                 pr_err("kfd: queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
314                 return -EINVAL;
315         }
316
317         if ((args.ring_base_address) &&
318                 (!access_ok(VERIFY_WRITE, args.ring_base_address, sizeof(uint64_t)))) {
319                 pr_err("kfd: can't access ring base address\n");
320                 return -EFAULT;
321         }
322
323         if (!is_power_of_2(args.ring_size) && (args.ring_size != 0)) {
324                 pr_err("kfd: ring size must be a power of 2 or 0\n");
325                 return -EINVAL;
326         }
327
328         properties.queue_address = args.ring_base_address;
329         properties.queue_size = args.ring_size;
330         properties.queue_percent = args.queue_percentage;
331         properties.priority = args.queue_priority;
332
333         pr_debug("kfd: updating queue id %d for PASID %d\n",
334                         args.queue_id, p->pasid);
335
336         mutex_lock(&p->mutex);
337
338         retval = pqm_update_queue(&p->pqm, args.queue_id, &properties);
339
340         mutex_unlock(&p->mutex);
341
342         return retval;
343 }
344
345 static long kfd_ioctl_set_memory_policy(struct file *filep,
346                                 struct kfd_process *p, void __user *arg)
347 {
348         return -ENODEV;
349 }
350
351 static long kfd_ioctl_get_clock_counters(struct file *filep,
352                                 struct kfd_process *p, void __user *arg)
353 {
354         return -ENODEV;
355 }
356
357
358 static int kfd_ioctl_get_process_apertures(struct file *filp,
359                                 struct kfd_process *p, void __user *arg)
360 {
361         return -ENODEV;
362 }
363
364 static long kfd_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
365 {
366         struct kfd_process *process;
367         long err = -EINVAL;
368
369         dev_dbg(kfd_device,
370                 "ioctl cmd 0x%x (#%d), arg 0x%lx\n",
371                 cmd, _IOC_NR(cmd), arg);
372
373         process = kfd_get_process(current);
374         if (IS_ERR(process))
375                 return PTR_ERR(process);
376
377         switch (cmd) {
378         case KFD_IOC_GET_VERSION:
379                 err = kfd_ioctl_get_version(filep, process, (void __user *)arg);
380                 break;
381         case KFD_IOC_CREATE_QUEUE:
382                 err = kfd_ioctl_create_queue(filep, process,
383                                                 (void __user *)arg);
384                 break;
385
386         case KFD_IOC_DESTROY_QUEUE:
387                 err = kfd_ioctl_destroy_queue(filep, process,
388                                                 (void __user *)arg);
389                 break;
390
391         case KFD_IOC_SET_MEMORY_POLICY:
392                 err = kfd_ioctl_set_memory_policy(filep, process,
393                                                 (void __user *)arg);
394                 break;
395
396         case KFD_IOC_GET_CLOCK_COUNTERS:
397                 err = kfd_ioctl_get_clock_counters(filep, process,
398                                                 (void __user *)arg);
399                 break;
400
401         case KFD_IOC_GET_PROCESS_APERTURES:
402                 err = kfd_ioctl_get_process_apertures(filep, process,
403                                                 (void __user *)arg);
404                 break;
405
406         case KFD_IOC_UPDATE_QUEUE:
407                 err = kfd_ioctl_update_queue(filep, process,
408                                                 (void __user *)arg);
409                 break;
410
411         default:
412                 dev_err(kfd_device,
413                         "unknown ioctl cmd 0x%x, arg 0x%lx)\n",
414                         cmd, arg);
415                 err = -EINVAL;
416                 break;
417         }
418
419         if (err < 0)
420                 dev_err(kfd_device,
421                         "ioctl error %ld for ioctl cmd 0x%x (#%d)\n",
422                         err, cmd, _IOC_NR(cmd));
423
424         return err;
425 }
426
427 static int kfd_mmap(struct file *filp, struct vm_area_struct *vma)
428 {
429         struct kfd_process *process;
430
431         process = kfd_get_process(current);
432         if (IS_ERR(process))
433                 return PTR_ERR(process);
434
435         return kfd_doorbell_mmap(process, vma);
436 }