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 <linux/uaccess.h>
35 #include <uapi/asm-generic/mman-common.h>
36 #include <asm/processor.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;
105 if (iminor(inode) != 0)
108 process = kfd_create_process(current);
110 return PTR_ERR(process);
112 process->is_32bit_user_mode = is_compat_task();
114 dev_dbg(kfd_device, "process %d opened, compat mode (32 bit) - %d\n",
115 process->pasid, process->is_32bit_user_mode);
117 kfd_init_apertures(process);
122 static long kfd_ioctl_get_version(struct file *filep, struct kfd_process *p,
128 static int set_queue_properties_from_user(struct queue_properties *q_properties,
129 struct kfd_ioctl_create_queue_args *args)
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");
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");
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");
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");
152 if (!access_ok(VERIFY_WRITE, args->read_pointer_address, sizeof(uint32_t))) {
153 pr_err("kfd: can't access read pointer\n");
157 if (!access_ok(VERIFY_WRITE, args->write_pointer_address, sizeof(uint32_t))) {
158 pr_err("kfd: can't access write pointer\n");
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;
175 if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL)
176 q_properties->format = KFD_QUEUE_FORMAT_AQL;
178 q_properties->format = KFD_QUEUE_FORMAT_PM4;
180 pr_debug("Queue Percentage (%d, %d)\n",
181 q_properties->queue_percent, args->queue_percentage);
183 pr_debug("Queue Priority (%d, %d)\n",
184 q_properties->priority, args->queue_priority);
186 pr_debug("Queue Address (0x%llX, 0x%llX)\n",
187 q_properties->queue_address, args->ring_base_address);
189 pr_debug("Queue Size (0x%llX, %u)\n",
190 q_properties->queue_size, args->ring_size);
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);
196 pr_debug("Queue Format (%d)\n", q_properties->format);
201 static long kfd_ioctl_create_queue(struct file *filep, struct kfd_process *p,
204 struct kfd_ioctl_create_queue_args args;
207 unsigned int queue_id;
208 struct kfd_process_device *pdd;
209 struct queue_properties q_properties;
211 memset(&q_properties, 0, sizeof(struct queue_properties));
213 if (copy_from_user(&args, arg, sizeof(args)))
216 pr_debug("kfd: creating queue ioctl\n");
218 err = set_queue_properties_from_user(&q_properties, &args);
222 dev = kfd_device_by_id(args.gpu_id);
226 mutex_lock(&p->mutex);
228 pdd = kfd_bind_process_to_device(dev, p);
229 if (IS_ERR(pdd) < 0) {
231 goto err_bind_process;
234 pr_debug("kfd: creating queue for PASID %d on GPU 0x%x\n",
238 err = pqm_create_queue(&p->pqm, dev, filep, &q_properties, 0,
239 KFD_QUEUE_TYPE_COMPUTE, &queue_id);
241 goto err_create_queue;
243 args.queue_id = queue_id;
245 /* Return gpu_id as doorbell offset for mmap usage */
246 args.doorbell_offset = args.gpu_id << PAGE_SHIFT;
248 if (copy_to_user(arg, &args, sizeof(args))) {
250 goto err_copy_args_out;
253 mutex_unlock(&p->mutex);
255 pr_debug("kfd: queue id %d was created successfully\n", args.queue_id);
257 pr_debug("ring buffer address == 0x%016llX\n",
258 args.ring_base_address);
260 pr_debug("read ptr address == 0x%016llX\n",
261 args.read_pointer_address);
263 pr_debug("write ptr address == 0x%016llX\n",
264 args.write_pointer_address);
269 pqm_destroy_queue(&p->pqm, queue_id);
272 mutex_unlock(&p->mutex);
276 static int kfd_ioctl_destroy_queue(struct file *filp, struct kfd_process *p,
280 struct kfd_ioctl_destroy_queue_args args;
282 if (copy_from_user(&args, arg, sizeof(args)))
285 pr_debug("kfd: destroying queue id %d for PASID %d\n",
289 mutex_lock(&p->mutex);
291 retval = pqm_destroy_queue(&p->pqm, args.queue_id);
293 mutex_unlock(&p->mutex);
297 static int kfd_ioctl_update_queue(struct file *filp, struct kfd_process *p,
301 struct kfd_ioctl_update_queue_args args;
302 struct queue_properties properties;
304 if (copy_from_user(&args, arg, sizeof(args)))
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");
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");
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");
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");
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;
333 pr_debug("kfd: updating queue id %d for PASID %d\n",
334 args.queue_id, p->pasid);
336 mutex_lock(&p->mutex);
338 retval = pqm_update_queue(&p->pqm, args.queue_id, &properties);
340 mutex_unlock(&p->mutex);
345 static long kfd_ioctl_set_memory_policy(struct file *filep,
346 struct kfd_process *p, void __user *arg)
351 static long kfd_ioctl_get_clock_counters(struct file *filep,
352 struct kfd_process *p, void __user *arg)
358 static int kfd_ioctl_get_process_apertures(struct file *filp,
359 struct kfd_process *p, void __user *arg)
364 static long kfd_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
366 struct kfd_process *process;
370 "ioctl cmd 0x%x (#%d), arg 0x%lx\n",
371 cmd, _IOC_NR(cmd), arg);
373 process = kfd_get_process(current);
375 return PTR_ERR(process);
378 case KFD_IOC_GET_VERSION:
379 err = kfd_ioctl_get_version(filep, process, (void __user *)arg);
381 case KFD_IOC_CREATE_QUEUE:
382 err = kfd_ioctl_create_queue(filep, process,
386 case KFD_IOC_DESTROY_QUEUE:
387 err = kfd_ioctl_destroy_queue(filep, process,
391 case KFD_IOC_SET_MEMORY_POLICY:
392 err = kfd_ioctl_set_memory_policy(filep, process,
396 case KFD_IOC_GET_CLOCK_COUNTERS:
397 err = kfd_ioctl_get_clock_counters(filep, process,
401 case KFD_IOC_GET_PROCESS_APERTURES:
402 err = kfd_ioctl_get_process_apertures(filep, process,
406 case KFD_IOC_UPDATE_QUEUE:
407 err = kfd_ioctl_update_queue(filep, process,
413 "unknown ioctl cmd 0x%x, arg 0x%lx)\n",
421 "ioctl error %ld for ioctl cmd 0x%x (#%d)\n",
422 err, cmd, _IOC_NR(cmd));
427 static int kfd_mmap(struct file *filp, struct vm_area_struct *vma)
429 struct kfd_process *process;
431 process = kfd_get_process(current);
433 return PTR_ERR(process);
435 return kfd_doorbell_mmap(process, vma);