2 * Copyright 2014 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
23 #include <linux/device.h>
24 #include <linux/export.h>
25 #include <linux/err.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>
34 #include <uapi/asm-generic/mman-common.h>
35 #include <asm/processor.h>
37 #include "kfd_device_queue_manager.h"
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 *);
43 static const char kfd_dev_name[] = "kfd";
45 static const struct file_operations kfd_fops = {
47 .unlocked_ioctl = kfd_ioctl,
48 .compat_ioctl = kfd_ioctl,
53 static int kfd_char_dev_major = -1;
54 static struct class *kfd_class;
55 struct device *kfd_device;
57 int kfd_chardev_init(void)
61 kfd_char_dev_major = register_chrdev(0, kfd_dev_name, &kfd_fops);
62 err = kfd_char_dev_major;
64 goto err_register_chrdev;
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;
71 kfd_device = device_create(kfd_class, NULL,
72 MKDEV(kfd_char_dev_major, 0),
74 err = PTR_ERR(kfd_device);
75 if (IS_ERR(kfd_device))
76 goto err_device_create;
81 class_destroy(kfd_class);
83 unregister_chrdev(kfd_char_dev_major, kfd_dev_name);
88 void kfd_chardev_exit(void)
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);
95 struct device *kfd_chardev(void)
101 static int kfd_open(struct inode *inode, struct file *filep)
103 struct kfd_process *process;
104 bool is_32bit_user_mode;
106 if (iminor(inode) != 0)
109 is_32bit_user_mode = is_compat_task();
111 if (is_32bit_user_mode == true) {
113 "Process %d (32-bit) failed to open /dev/kfd\n"
114 "32-bit processes are not supported by amdkfd\n",
119 process = kfd_create_process(current);
121 return PTR_ERR(process);
123 dev_dbg(kfd_device, "process %d opened, compat mode (32 bit) - %d\n",
124 process->pasid, process->is_32bit_user_mode);
129 static long kfd_ioctl_get_version(struct file *filep, struct kfd_process *p,
132 struct kfd_ioctl_get_version_args args;
135 args.major_version = KFD_IOCTL_MAJOR_VERSION;
136 args.minor_version = KFD_IOCTL_MINOR_VERSION;
138 if (copy_to_user(arg, &args, sizeof(args)))
144 static int set_queue_properties_from_user(struct queue_properties *q_properties,
145 struct kfd_ioctl_create_queue_args *args)
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");
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");
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");
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");
170 if (!access_ok(VERIFY_WRITE,
171 (const void __user *) args->read_pointer_address,
173 pr_err("kfd: can't access read pointer\n");
177 if (!access_ok(VERIFY_WRITE,
178 (const void __user *) args->write_pointer_address,
180 pr_err("kfd: can't access write pointer\n");
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;
197 if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL)
198 q_properties->format = KFD_QUEUE_FORMAT_AQL;
200 q_properties->format = KFD_QUEUE_FORMAT_PM4;
202 pr_debug("Queue Percentage (%d, %d)\n",
203 q_properties->queue_percent, args->queue_percentage);
205 pr_debug("Queue Priority (%d, %d)\n",
206 q_properties->priority, args->queue_priority);
208 pr_debug("Queue Address (0x%llX, 0x%llX)\n",
209 q_properties->queue_address, args->ring_base_address);
211 pr_debug("Queue Size (0x%llX, %u)\n",
212 q_properties->queue_size, args->ring_size);
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);
218 pr_debug("Queue Format (%d)\n", q_properties->format);
223 static long kfd_ioctl_create_queue(struct file *filep, struct kfd_process *p,
226 struct kfd_ioctl_create_queue_args args;
229 unsigned int queue_id;
230 struct kfd_process_device *pdd;
231 struct queue_properties q_properties;
233 memset(&q_properties, 0, sizeof(struct queue_properties));
235 if (copy_from_user(&args, arg, sizeof(args)))
238 pr_debug("kfd: creating queue ioctl\n");
240 err = set_queue_properties_from_user(&q_properties, &args);
244 dev = kfd_device_by_id(args.gpu_id);
248 mutex_lock(&p->mutex);
250 pdd = kfd_bind_process_to_device(dev, p);
253 goto err_bind_process;
256 pr_debug("kfd: creating queue for PASID %d on GPU 0x%x\n",
260 err = pqm_create_queue(&p->pqm, dev, filep, &q_properties, 0,
261 KFD_QUEUE_TYPE_COMPUTE, &queue_id);
263 goto err_create_queue;
265 args.queue_id = queue_id;
267 /* Return gpu_id as doorbell offset for mmap usage */
268 args.doorbell_offset = args.gpu_id << PAGE_SHIFT;
270 if (copy_to_user(arg, &args, sizeof(args))) {
272 goto err_copy_args_out;
275 mutex_unlock(&p->mutex);
277 pr_debug("kfd: queue id %d was created successfully\n", args.queue_id);
279 pr_debug("ring buffer address == 0x%016llX\n",
280 args.ring_base_address);
282 pr_debug("read ptr address == 0x%016llX\n",
283 args.read_pointer_address);
285 pr_debug("write ptr address == 0x%016llX\n",
286 args.write_pointer_address);
291 pqm_destroy_queue(&p->pqm, queue_id);
294 mutex_unlock(&p->mutex);
298 static int kfd_ioctl_destroy_queue(struct file *filp, struct kfd_process *p,
302 struct kfd_ioctl_destroy_queue_args args;
304 if (copy_from_user(&args, arg, sizeof(args)))
307 pr_debug("kfd: destroying queue id %d for PASID %d\n",
311 mutex_lock(&p->mutex);
313 retval = pqm_destroy_queue(&p->pqm, args.queue_id);
315 mutex_unlock(&p->mutex);
319 static int kfd_ioctl_update_queue(struct file *filp, struct kfd_process *p,
323 struct kfd_ioctl_update_queue_args args;
324 struct queue_properties properties;
326 if (copy_from_user(&args, arg, sizeof(args)))
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");
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");
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");
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");
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;
357 pr_debug("kfd: updating queue id %d for PASID %d\n",
358 args.queue_id, p->pasid);
360 mutex_lock(&p->mutex);
362 retval = pqm_update_queue(&p->pqm, args.queue_id, &properties);
364 mutex_unlock(&p->mutex);
369 static long kfd_ioctl_set_memory_policy(struct file *filep,
370 struct kfd_process *p, void __user *arg)
372 struct kfd_ioctl_set_memory_policy_args args;
375 struct kfd_process_device *pdd;
376 enum cache_policy default_policy, alternate_policy;
378 if (copy_from_user(&args, arg, sizeof(args)))
381 if (args.default_policy != KFD_IOC_CACHE_POLICY_COHERENT
382 && args.default_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) {
386 if (args.alternate_policy != KFD_IOC_CACHE_POLICY_COHERENT
387 && args.alternate_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) {
391 dev = kfd_device_by_id(args.gpu_id);
395 mutex_lock(&p->mutex);
397 pdd = kfd_bind_process_to_device(dev, p);
403 default_policy = (args.default_policy == KFD_IOC_CACHE_POLICY_COHERENT)
404 ? cache_policy_coherent : cache_policy_noncoherent;
407 (args.alternate_policy == KFD_IOC_CACHE_POLICY_COHERENT)
408 ? cache_policy_coherent : cache_policy_noncoherent;
410 if (!dev->dqm->set_cache_memory_policy(dev->dqm,
414 (void __user *)args.alternate_aperture_base,
415 args.alternate_aperture_size))
419 mutex_unlock(&p->mutex);
424 static long kfd_ioctl_get_clock_counters(struct file *filep,
425 struct kfd_process *p, void __user *arg)
427 struct kfd_ioctl_get_clock_counters_args args;
429 struct timespec time;
431 if (copy_from_user(&args, arg, sizeof(args)))
434 dev = kfd_device_by_id(args.gpu_id);
438 /* Reading GPU clock counter from KGD */
439 args.gpu_clock_counter = kfd2kgd->get_gpu_clock_counter(dev->kgd);
441 /* No access to rdtsc. Using raw monotonic time */
442 getrawmonotonic(&time);
443 args.cpu_clock_counter = (uint64_t)timespec_to_ns(&time);
445 get_monotonic_boottime(&time);
446 args.system_clock_counter = (uint64_t)timespec_to_ns(&time);
448 /* Since the counter is in nano-seconds we use 1GHz frequency */
449 args.system_clock_freq = 1000000000;
451 if (copy_to_user(arg, &args, sizeof(args)))
458 static int kfd_ioctl_get_process_apertures(struct file *filp,
459 struct kfd_process *p, void __user *arg)
461 struct kfd_ioctl_get_process_apertures_args args;
462 struct kfd_process_device_apertures *pAperture;
463 struct kfd_process_device *pdd;
465 dev_dbg(kfd_device, "get apertures for PASID %d", p->pasid);
467 if (copy_from_user(&args, arg, sizeof(args)))
470 args.num_of_nodes = 0;
472 mutex_lock(&p->mutex);
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);
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;
489 "node id %u\n", args.num_of_nodes);
491 "gpu id %u\n", pdd->dev->id);
493 "lds_base %llX\n", pdd->lds_base);
495 "lds_limit %llX\n", pdd->lds_limit);
497 "gpuvm_base %llX\n", pdd->gpuvm_base);
499 "gpuvm_limit %llX\n", pdd->gpuvm_limit);
501 "scratch_base %llX\n", pdd->scratch_base);
503 "scratch_limit %llX\n", pdd->scratch_limit);
506 } while ((pdd = kfd_get_next_process_device_data(p, pdd)) != NULL &&
507 (args.num_of_nodes < NUM_OF_SUPPORTED_GPUS));
510 mutex_unlock(&p->mutex);
512 if (copy_to_user(arg, &args, sizeof(args)))
518 static long kfd_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
520 struct kfd_process *process;
524 "ioctl cmd 0x%x (#%d), arg 0x%lx\n",
525 cmd, _IOC_NR(cmd), arg);
527 process = kfd_get_process(current);
529 return PTR_ERR(process);
532 case KFD_IOC_GET_VERSION:
533 err = kfd_ioctl_get_version(filep, process, (void __user *)arg);
535 case KFD_IOC_CREATE_QUEUE:
536 err = kfd_ioctl_create_queue(filep, process,
540 case KFD_IOC_DESTROY_QUEUE:
541 err = kfd_ioctl_destroy_queue(filep, process,
545 case KFD_IOC_SET_MEMORY_POLICY:
546 err = kfd_ioctl_set_memory_policy(filep, process,
550 case KFD_IOC_GET_CLOCK_COUNTERS:
551 err = kfd_ioctl_get_clock_counters(filep, process,
555 case KFD_IOC_GET_PROCESS_APERTURES:
556 err = kfd_ioctl_get_process_apertures(filep, process,
560 case KFD_IOC_UPDATE_QUEUE:
561 err = kfd_ioctl_update_queue(filep, process,
567 "unknown ioctl cmd 0x%x, arg 0x%lx)\n",
575 "ioctl error %ld for ioctl cmd 0x%x (#%d)\n",
576 err, cmd, _IOC_NR(cmd));
581 static int kfd_mmap(struct file *filp, struct vm_area_struct *vma)
583 struct kfd_process *process;
585 process = kfd_get_process(current);
587 return PTR_ERR(process);
589 return kfd_doorbell_mmap(process, vma);