Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 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> |
Christoph Hellwig | 2497ee7 | 2015-08-28 09:27:16 +0200 | [diff] [blame] | 34 | #include <linux/mman.h> |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 35 | #include <asm/processor.h> |
| 36 | #include "kfd_priv.h" |
Andrew Lewycky | 41a286f | 2014-07-17 01:46:17 +0300 | [diff] [blame] | 37 | #include "kfd_device_queue_manager.h" |
Yair Shachar | 037ed9a | 2015-05-20 14:08:55 +0300 | [diff] [blame] | 38 | #include "kfd_dbgmgr.h" |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 39 | |
| 40 | static long kfd_ioctl(struct file *, unsigned int, unsigned long); |
| 41 | static int kfd_open(struct inode *, struct file *); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 42 | static int kfd_mmap(struct file *, struct vm_area_struct *); |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 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, |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 51 | .mmap = kfd_mmap, |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 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 | { |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 104 | struct kfd_process *process; |
Oded Gabbay | a18069c | 2014-12-05 10:40:34 +0200 | [diff] [blame] | 105 | bool is_32bit_user_mode; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 106 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 107 | if (iminor(inode) != 0) |
| 108 | return -ENODEV; |
| 109 | |
Andy Lutomirski | 10f1685 | 2016-03-22 14:25:19 -0700 | [diff] [blame] | 110 | is_32bit_user_mode = in_compat_syscall(); |
Oded Gabbay | a18069c | 2014-12-05 10:40:34 +0200 | [diff] [blame] | 111 | |
Edward O'Callaghan | 991ca8e | 2016-05-01 00:06:27 +1000 | [diff] [blame] | 112 | if (is_32bit_user_mode) { |
Oded Gabbay | a18069c | 2014-12-05 10:40:34 +0200 | [diff] [blame] | 113 | dev_warn(kfd_device, |
| 114 | "Process %d (32-bit) failed to open /dev/kfd\n" |
| 115 | "32-bit processes are not supported by amdkfd\n", |
| 116 | current->pid); |
| 117 | return -EPERM; |
| 118 | } |
| 119 | |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 120 | process = kfd_create_process(filep); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 121 | if (IS_ERR(process)) |
| 122 | return PTR_ERR(process); |
| 123 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 124 | dev_dbg(kfd_device, "process %d opened, compat mode (32 bit) - %d\n", |
| 125 | process->pasid, process->is_32bit_user_mode); |
| 126 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 127 | return 0; |
| 128 | } |
| 129 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 130 | static int kfd_ioctl_get_version(struct file *filep, struct kfd_process *p, |
| 131 | void *data) |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 132 | { |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 133 | struct kfd_ioctl_get_version_args *args = data; |
Oded Gabbay | ecd5c98 | 2014-11-02 12:18:29 +0200 | [diff] [blame] | 134 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 135 | args->major_version = KFD_IOCTL_MAJOR_VERSION; |
| 136 | args->minor_version = KFD_IOCTL_MINOR_VERSION; |
Oded Gabbay | ecd5c98 | 2014-11-02 12:18:29 +0200 | [diff] [blame] | 137 | |
Edward O'Callaghan | eb02602 | 2016-05-01 00:06:29 +1000 | [diff] [blame] | 138 | return 0; |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 139 | } |
| 140 | |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 141 | static int set_queue_properties_from_user(struct queue_properties *q_properties, |
| 142 | struct kfd_ioctl_create_queue_args *args) |
| 143 | { |
| 144 | if (args->queue_percentage > KFD_MAX_QUEUE_PERCENTAGE) { |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 145 | pr_err("Queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n"); |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 146 | return -EINVAL; |
| 147 | } |
| 148 | |
| 149 | if (args->queue_priority > KFD_MAX_QUEUE_PRIORITY) { |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 150 | pr_err("Queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n"); |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 151 | return -EINVAL; |
| 152 | } |
| 153 | |
| 154 | if ((args->ring_base_address) && |
Oded Gabbay | 4307d8f | 2014-11-20 15:37:13 +0200 | [diff] [blame] | 155 | (!access_ok(VERIFY_WRITE, |
| 156 | (const void __user *) args->ring_base_address, |
| 157 | sizeof(uint64_t)))) { |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 158 | pr_err("Can't access ring base address\n"); |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 159 | return -EFAULT; |
| 160 | } |
| 161 | |
| 162 | if (!is_power_of_2(args->ring_size) && (args->ring_size != 0)) { |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 163 | pr_err("Ring size must be a power of 2 or 0\n"); |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 164 | return -EINVAL; |
| 165 | } |
| 166 | |
Oded Gabbay | 4307d8f | 2014-11-20 15:37:13 +0200 | [diff] [blame] | 167 | if (!access_ok(VERIFY_WRITE, |
| 168 | (const void __user *) args->read_pointer_address, |
| 169 | sizeof(uint32_t))) { |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 170 | pr_err("Can't access read pointer\n"); |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 171 | return -EFAULT; |
| 172 | } |
| 173 | |
Oded Gabbay | 4307d8f | 2014-11-20 15:37:13 +0200 | [diff] [blame] | 174 | if (!access_ok(VERIFY_WRITE, |
| 175 | (const void __user *) args->write_pointer_address, |
| 176 | sizeof(uint32_t))) { |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 177 | pr_err("Can't access write pointer\n"); |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 178 | return -EFAULT; |
| 179 | } |
| 180 | |
Oded Gabbay | 0b3674a | 2015-01-22 13:42:28 +0200 | [diff] [blame] | 181 | if (args->eop_buffer_address && |
| 182 | !access_ok(VERIFY_WRITE, |
| 183 | (const void __user *) args->eop_buffer_address, |
| 184 | sizeof(uint32_t))) { |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 185 | pr_debug("Can't access eop buffer"); |
Ben Goz | ff3d04a | 2015-01-04 10:37:18 +0200 | [diff] [blame] | 186 | return -EFAULT; |
| 187 | } |
| 188 | |
Oded Gabbay | 0b3674a | 2015-01-22 13:42:28 +0200 | [diff] [blame] | 189 | if (args->ctx_save_restore_address && |
| 190 | !access_ok(VERIFY_WRITE, |
| 191 | (const void __user *) args->ctx_save_restore_address, |
| 192 | sizeof(uint32_t))) { |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 193 | pr_debug("Can't access ctx save restore buffer"); |
Ben Goz | ff3d04a | 2015-01-04 10:37:18 +0200 | [diff] [blame] | 194 | return -EFAULT; |
| 195 | } |
| 196 | |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 197 | q_properties->is_interop = false; |
| 198 | q_properties->queue_percent = args->queue_percentage; |
| 199 | q_properties->priority = args->queue_priority; |
| 200 | q_properties->queue_address = args->ring_base_address; |
| 201 | q_properties->queue_size = args->ring_size; |
| 202 | q_properties->read_ptr = (uint32_t *) args->read_pointer_address; |
| 203 | q_properties->write_ptr = (uint32_t *) args->write_pointer_address; |
Ben Goz | ff3d04a | 2015-01-04 10:37:18 +0200 | [diff] [blame] | 204 | q_properties->eop_ring_buffer_address = args->eop_buffer_address; |
| 205 | q_properties->eop_ring_buffer_size = args->eop_buffer_size; |
| 206 | q_properties->ctx_save_restore_area_address = |
| 207 | args->ctx_save_restore_address; |
| 208 | q_properties->ctx_save_restore_area_size = args->ctx_save_restore_size; |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 209 | q_properties->ctl_stack_size = args->ctl_stack_size; |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 210 | if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE || |
| 211 | args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL) |
| 212 | q_properties->type = KFD_QUEUE_TYPE_COMPUTE; |
Ben Goz | 3385f9d | 2015-01-03 22:12:33 +0200 | [diff] [blame] | 213 | else if (args->queue_type == KFD_IOC_QUEUE_TYPE_SDMA) |
| 214 | q_properties->type = KFD_QUEUE_TYPE_SDMA; |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 215 | else |
| 216 | return -ENOTSUPP; |
| 217 | |
| 218 | if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL) |
| 219 | q_properties->format = KFD_QUEUE_FORMAT_AQL; |
| 220 | else |
| 221 | q_properties->format = KFD_QUEUE_FORMAT_PM4; |
| 222 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 223 | pr_debug("Queue Percentage: %d, %d\n", |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 224 | q_properties->queue_percent, args->queue_percentage); |
| 225 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 226 | pr_debug("Queue Priority: %d, %d\n", |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 227 | q_properties->priority, args->queue_priority); |
| 228 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 229 | pr_debug("Queue Address: 0x%llX, 0x%llX\n", |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 230 | q_properties->queue_address, args->ring_base_address); |
| 231 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 232 | pr_debug("Queue Size: 0x%llX, %u\n", |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 233 | q_properties->queue_size, args->ring_size); |
| 234 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 235 | pr_debug("Queue r/w Pointers: %p, %p\n", |
| 236 | q_properties->read_ptr, |
| 237 | q_properties->write_ptr); |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 238 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 239 | pr_debug("Queue Format: %d\n", q_properties->format); |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 240 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 241 | pr_debug("Queue EOP: 0x%llX\n", q_properties->eop_ring_buffer_address); |
Ben Goz | ff3d04a | 2015-01-04 10:37:18 +0200 | [diff] [blame] | 242 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 243 | pr_debug("Queue CTX save area: 0x%llX\n", |
Ben Goz | ff3d04a | 2015-01-04 10:37:18 +0200 | [diff] [blame] | 244 | q_properties->ctx_save_restore_area_address); |
| 245 | |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 249 | static int kfd_ioctl_create_queue(struct file *filep, struct kfd_process *p, |
| 250 | void *data) |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 251 | { |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 252 | struct kfd_ioctl_create_queue_args *args = data; |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 253 | struct kfd_dev *dev; |
| 254 | int err = 0; |
| 255 | unsigned int queue_id; |
| 256 | struct kfd_process_device *pdd; |
| 257 | struct queue_properties q_properties; |
| 258 | |
| 259 | memset(&q_properties, 0, sizeof(struct queue_properties)); |
| 260 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 261 | pr_debug("Creating queue ioctl\n"); |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 262 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 263 | err = set_queue_properties_from_user(&q_properties, args); |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 264 | if (err) |
| 265 | return err; |
| 266 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 267 | pr_debug("Looking for gpu id 0x%x\n", args->gpu_id); |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 268 | dev = kfd_device_by_id(args->gpu_id); |
Kent Russell | 4eacc26b | 2017-08-15 23:00:06 -0400 | [diff] [blame] | 269 | if (!dev) { |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 270 | pr_debug("Could not find gpu id 0x%x\n", args->gpu_id); |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 271 | return -EINVAL; |
Ben Goz | ff3d04a | 2015-01-04 10:37:18 +0200 | [diff] [blame] | 272 | } |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 273 | |
| 274 | mutex_lock(&p->mutex); |
| 275 | |
| 276 | pdd = kfd_bind_process_to_device(dev, p); |
Dan Carpenter | 66333cb | 2014-11-25 13:21:30 +0300 | [diff] [blame] | 277 | if (IS_ERR(pdd)) { |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 278 | err = -ESRCH; |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 279 | goto err_bind_process; |
| 280 | } |
| 281 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 282 | pr_debug("Creating queue for PASID %d on gpu 0x%x\n", |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 283 | p->pasid, |
| 284 | dev->id); |
| 285 | |
Yong Zhao | e6f791b | 2017-09-27 00:09:53 -0400 | [diff] [blame] | 286 | err = pqm_create_queue(&p->pqm, dev, filep, &q_properties, &queue_id); |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 287 | if (err != 0) |
| 288 | goto err_create_queue; |
| 289 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 290 | args->queue_id = queue_id; |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 291 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 292 | |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 293 | /* Return gpu_id as doorbell offset for mmap usage */ |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 294 | args->doorbell_offset = (KFD_MMAP_DOORBELL_MASK | args->gpu_id); |
| 295 | args->doorbell_offset <<= PAGE_SHIFT; |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 296 | |
| 297 | mutex_unlock(&p->mutex); |
| 298 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 299 | pr_debug("Queue id %d was created successfully\n", args->queue_id); |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 300 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 301 | pr_debug("Ring buffer address == 0x%016llX\n", |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 302 | args->ring_base_address); |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 303 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 304 | pr_debug("Read ptr address == 0x%016llX\n", |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 305 | args->read_pointer_address); |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 306 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 307 | pr_debug("Write ptr address == 0x%016llX\n", |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 308 | args->write_pointer_address); |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 309 | |
| 310 | return 0; |
| 311 | |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 312 | err_create_queue: |
| 313 | err_bind_process: |
| 314 | mutex_unlock(&p->mutex); |
| 315 | return err; |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static int kfd_ioctl_destroy_queue(struct file *filp, struct kfd_process *p, |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 319 | void *data) |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 320 | { |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 321 | int retval; |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 322 | struct kfd_ioctl_destroy_queue_args *args = data; |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 323 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 324 | pr_debug("Destroying queue id %d for pasid %d\n", |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 325 | args->queue_id, |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 326 | p->pasid); |
| 327 | |
| 328 | mutex_lock(&p->mutex); |
| 329 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 330 | retval = pqm_destroy_queue(&p->pqm, args->queue_id); |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 331 | |
| 332 | mutex_unlock(&p->mutex); |
| 333 | return retval; |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | static int kfd_ioctl_update_queue(struct file *filp, struct kfd_process *p, |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 337 | void *data) |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 338 | { |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 339 | int retval; |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 340 | struct kfd_ioctl_update_queue_args *args = data; |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 341 | struct queue_properties properties; |
| 342 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 343 | if (args->queue_percentage > KFD_MAX_QUEUE_PERCENTAGE) { |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 344 | pr_err("Queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n"); |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 345 | return -EINVAL; |
| 346 | } |
| 347 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 348 | if (args->queue_priority > KFD_MAX_QUEUE_PRIORITY) { |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 349 | pr_err("Queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n"); |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 350 | return -EINVAL; |
| 351 | } |
| 352 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 353 | if ((args->ring_base_address) && |
Oded Gabbay | 4307d8f | 2014-11-20 15:37:13 +0200 | [diff] [blame] | 354 | (!access_ok(VERIFY_WRITE, |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 355 | (const void __user *) args->ring_base_address, |
Oded Gabbay | 4307d8f | 2014-11-20 15:37:13 +0200 | [diff] [blame] | 356 | sizeof(uint64_t)))) { |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 357 | pr_err("Can't access ring base address\n"); |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 358 | return -EFAULT; |
| 359 | } |
| 360 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 361 | if (!is_power_of_2(args->ring_size) && (args->ring_size != 0)) { |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 362 | pr_err("Ring size must be a power of 2 or 0\n"); |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 363 | return -EINVAL; |
| 364 | } |
| 365 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 366 | properties.queue_address = args->ring_base_address; |
| 367 | properties.queue_size = args->ring_size; |
| 368 | properties.queue_percent = args->queue_percentage; |
| 369 | properties.priority = args->queue_priority; |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 370 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 371 | pr_debug("Updating queue id %d for pasid %d\n", |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 372 | args->queue_id, p->pasid); |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 373 | |
| 374 | mutex_lock(&p->mutex); |
| 375 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 376 | retval = pqm_update_queue(&p->pqm, args->queue_id, &properties); |
Oded Gabbay | 39b027d | 2014-10-19 23:46:40 +0300 | [diff] [blame] | 377 | |
| 378 | mutex_unlock(&p->mutex); |
| 379 | |
| 380 | return retval; |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 381 | } |
| 382 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 383 | static int kfd_ioctl_set_memory_policy(struct file *filep, |
| 384 | struct kfd_process *p, void *data) |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 385 | { |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 386 | struct kfd_ioctl_set_memory_policy_args *args = data; |
Andrew Lewycky | 41a286f | 2014-07-17 01:46:17 +0300 | [diff] [blame] | 387 | struct kfd_dev *dev; |
| 388 | int err = 0; |
| 389 | struct kfd_process_device *pdd; |
| 390 | enum cache_policy default_policy, alternate_policy; |
| 391 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 392 | if (args->default_policy != KFD_IOC_CACHE_POLICY_COHERENT |
| 393 | && args->default_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) { |
Andrew Lewycky | 41a286f | 2014-07-17 01:46:17 +0300 | [diff] [blame] | 394 | return -EINVAL; |
| 395 | } |
| 396 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 397 | if (args->alternate_policy != KFD_IOC_CACHE_POLICY_COHERENT |
| 398 | && args->alternate_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) { |
Andrew Lewycky | 41a286f | 2014-07-17 01:46:17 +0300 | [diff] [blame] | 399 | return -EINVAL; |
| 400 | } |
| 401 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 402 | dev = kfd_device_by_id(args->gpu_id); |
Kent Russell | 4eacc26b | 2017-08-15 23:00:06 -0400 | [diff] [blame] | 403 | if (!dev) |
Andrew Lewycky | 41a286f | 2014-07-17 01:46:17 +0300 | [diff] [blame] | 404 | return -EINVAL; |
| 405 | |
| 406 | mutex_lock(&p->mutex); |
| 407 | |
| 408 | pdd = kfd_bind_process_to_device(dev, p); |
Dan Carpenter | 66333cb | 2014-11-25 13:21:30 +0300 | [diff] [blame] | 409 | if (IS_ERR(pdd)) { |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 410 | err = -ESRCH; |
Andrew Lewycky | 41a286f | 2014-07-17 01:46:17 +0300 | [diff] [blame] | 411 | goto out; |
| 412 | } |
| 413 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 414 | default_policy = (args->default_policy == KFD_IOC_CACHE_POLICY_COHERENT) |
Andrew Lewycky | 41a286f | 2014-07-17 01:46:17 +0300 | [diff] [blame] | 415 | ? cache_policy_coherent : cache_policy_noncoherent; |
| 416 | |
| 417 | alternate_policy = |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 418 | (args->alternate_policy == KFD_IOC_CACHE_POLICY_COHERENT) |
Andrew Lewycky | 41a286f | 2014-07-17 01:46:17 +0300 | [diff] [blame] | 419 | ? cache_policy_coherent : cache_policy_noncoherent; |
| 420 | |
Oded Gabbay | 45c9a5e | 2015-01-12 14:26:10 +0200 | [diff] [blame] | 421 | if (!dev->dqm->ops.set_cache_memory_policy(dev->dqm, |
Andrew Lewycky | 41a286f | 2014-07-17 01:46:17 +0300 | [diff] [blame] | 422 | &pdd->qpd, |
| 423 | default_policy, |
| 424 | alternate_policy, |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 425 | (void __user *)args->alternate_aperture_base, |
| 426 | args->alternate_aperture_size)) |
Andrew Lewycky | 41a286f | 2014-07-17 01:46:17 +0300 | [diff] [blame] | 427 | err = -EINVAL; |
| 428 | |
| 429 | out: |
| 430 | mutex_unlock(&p->mutex); |
| 431 | |
| 432 | return err; |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 433 | } |
| 434 | |
Felix Kuehling | d7b9bd2 | 2017-11-14 16:41:20 -0500 | [diff] [blame^] | 435 | static int kfd_ioctl_set_trap_handler(struct file *filep, |
| 436 | struct kfd_process *p, void *data) |
| 437 | { |
| 438 | struct kfd_ioctl_set_trap_handler_args *args = data; |
| 439 | struct kfd_dev *dev; |
| 440 | int err = 0; |
| 441 | struct kfd_process_device *pdd; |
| 442 | |
| 443 | dev = kfd_device_by_id(args->gpu_id); |
| 444 | if (dev == NULL) |
| 445 | return -EINVAL; |
| 446 | |
| 447 | mutex_lock(&p->mutex); |
| 448 | |
| 449 | pdd = kfd_bind_process_to_device(dev, p); |
| 450 | if (IS_ERR(pdd)) { |
| 451 | err = -ESRCH; |
| 452 | goto out; |
| 453 | } |
| 454 | |
| 455 | if (dev->dqm->ops.set_trap_handler(dev->dqm, |
| 456 | &pdd->qpd, |
| 457 | args->tba_addr, |
| 458 | args->tma_addr)) |
| 459 | err = -EINVAL; |
| 460 | |
| 461 | out: |
| 462 | mutex_unlock(&p->mutex); |
| 463 | |
| 464 | return err; |
| 465 | } |
| 466 | |
Yair Shachar | aef1100 | 2014-12-07 17:05:22 +0200 | [diff] [blame] | 467 | static int kfd_ioctl_dbg_register(struct file *filep, |
| 468 | struct kfd_process *p, void *data) |
| 469 | { |
Yair Shachar | 037ed9a | 2015-05-20 14:08:55 +0300 | [diff] [blame] | 470 | struct kfd_ioctl_dbg_register_args *args = data; |
| 471 | struct kfd_dev *dev; |
| 472 | struct kfd_dbgmgr *dbgmgr_ptr; |
| 473 | struct kfd_process_device *pdd; |
| 474 | bool create_ok; |
| 475 | long status = 0; |
| 476 | |
| 477 | dev = kfd_device_by_id(args->gpu_id); |
Kent Russell | 4eacc26b | 2017-08-15 23:00:06 -0400 | [diff] [blame] | 478 | if (!dev) |
Yair Shachar | 037ed9a | 2015-05-20 14:08:55 +0300 | [diff] [blame] | 479 | return -EINVAL; |
| 480 | |
| 481 | if (dev->device_info->asic_family == CHIP_CARRIZO) { |
| 482 | pr_debug("kfd_ioctl_dbg_register not supported on CZ\n"); |
| 483 | return -EINVAL; |
| 484 | } |
| 485 | |
Yair Shachar | 037ed9a | 2015-05-20 14:08:55 +0300 | [diff] [blame] | 486 | mutex_lock(&p->mutex); |
Yair Shachar | 062c567 | 2017-11-01 19:21:29 -0400 | [diff] [blame] | 487 | mutex_lock(kfd_get_dbgmgr_mutex()); |
Yair Shachar | 037ed9a | 2015-05-20 14:08:55 +0300 | [diff] [blame] | 488 | |
| 489 | /* |
| 490 | * make sure that we have pdd, if this the first queue created for |
| 491 | * this process |
| 492 | */ |
| 493 | pdd = kfd_bind_process_to_device(dev, p); |
| 494 | if (IS_ERR(pdd)) { |
Kent Russell | ab7c164 | 2017-08-15 23:00:07 -0400 | [diff] [blame] | 495 | status = PTR_ERR(pdd); |
| 496 | goto out; |
Yair Shachar | 037ed9a | 2015-05-20 14:08:55 +0300 | [diff] [blame] | 497 | } |
| 498 | |
Kent Russell | 4eacc26b | 2017-08-15 23:00:06 -0400 | [diff] [blame] | 499 | if (!dev->dbgmgr) { |
Yair Shachar | 037ed9a | 2015-05-20 14:08:55 +0300 | [diff] [blame] | 500 | /* In case of a legal call, we have no dbgmgr yet */ |
| 501 | create_ok = kfd_dbgmgr_create(&dbgmgr_ptr, dev); |
| 502 | if (create_ok) { |
| 503 | status = kfd_dbgmgr_register(dbgmgr_ptr, p); |
| 504 | if (status != 0) |
| 505 | kfd_dbgmgr_destroy(dbgmgr_ptr); |
| 506 | else |
| 507 | dev->dbgmgr = dbgmgr_ptr; |
| 508 | } |
| 509 | } else { |
| 510 | pr_debug("debugger already registered\n"); |
| 511 | status = -EINVAL; |
| 512 | } |
| 513 | |
Kent Russell | ab7c164 | 2017-08-15 23:00:07 -0400 | [diff] [blame] | 514 | out: |
Yair Shachar | 037ed9a | 2015-05-20 14:08:55 +0300 | [diff] [blame] | 515 | mutex_unlock(kfd_get_dbgmgr_mutex()); |
Yair Shachar | 062c567 | 2017-11-01 19:21:29 -0400 | [diff] [blame] | 516 | mutex_unlock(&p->mutex); |
Yair Shachar | aef1100 | 2014-12-07 17:05:22 +0200 | [diff] [blame] | 517 | |
| 518 | return status; |
| 519 | } |
| 520 | |
Colin Ian King | a7522cd | 2016-11-12 17:33:29 +0000 | [diff] [blame] | 521 | static int kfd_ioctl_dbg_unregister(struct file *filep, |
Yair Shachar | aef1100 | 2014-12-07 17:05:22 +0200 | [diff] [blame] | 522 | struct kfd_process *p, void *data) |
| 523 | { |
Yair Shachar | 037ed9a | 2015-05-20 14:08:55 +0300 | [diff] [blame] | 524 | struct kfd_ioctl_dbg_unregister_args *args = data; |
| 525 | struct kfd_dev *dev; |
| 526 | long status; |
| 527 | |
| 528 | dev = kfd_device_by_id(args->gpu_id); |
Kent Russell | 4eacc26b | 2017-08-15 23:00:06 -0400 | [diff] [blame] | 529 | if (!dev) |
Yair Shachar | 037ed9a | 2015-05-20 14:08:55 +0300 | [diff] [blame] | 530 | return -EINVAL; |
| 531 | |
| 532 | if (dev->device_info->asic_family == CHIP_CARRIZO) { |
Colin Ian King | a7522cd | 2016-11-12 17:33:29 +0000 | [diff] [blame] | 533 | pr_debug("kfd_ioctl_dbg_unregister not supported on CZ\n"); |
Yair Shachar | 037ed9a | 2015-05-20 14:08:55 +0300 | [diff] [blame] | 534 | return -EINVAL; |
| 535 | } |
| 536 | |
| 537 | mutex_lock(kfd_get_dbgmgr_mutex()); |
| 538 | |
| 539 | status = kfd_dbgmgr_unregister(dev->dbgmgr, p); |
Kent Russell | 4eacc26b | 2017-08-15 23:00:06 -0400 | [diff] [blame] | 540 | if (!status) { |
Yair Shachar | 037ed9a | 2015-05-20 14:08:55 +0300 | [diff] [blame] | 541 | kfd_dbgmgr_destroy(dev->dbgmgr); |
| 542 | dev->dbgmgr = NULL; |
| 543 | } |
| 544 | |
| 545 | mutex_unlock(kfd_get_dbgmgr_mutex()); |
Yair Shachar | aef1100 | 2014-12-07 17:05:22 +0200 | [diff] [blame] | 546 | |
| 547 | return status; |
| 548 | } |
| 549 | |
| 550 | /* |
| 551 | * Parse and generate variable size data structure for address watch. |
| 552 | * Total size of the buffer and # watch points is limited in order |
| 553 | * to prevent kernel abuse. (no bearing to the much smaller HW limitation |
| 554 | * which is enforced by dbgdev module) |
| 555 | * please also note that the watch address itself are not "copied from user", |
| 556 | * since it be set into the HW in user mode values. |
| 557 | * |
| 558 | */ |
| 559 | static int kfd_ioctl_dbg_address_watch(struct file *filep, |
| 560 | struct kfd_process *p, void *data) |
| 561 | { |
Yair Shachar | f8bd133 | 2015-05-20 14:09:39 +0300 | [diff] [blame] | 562 | struct kfd_ioctl_dbg_address_watch_args *args = data; |
| 563 | struct kfd_dev *dev; |
| 564 | struct dbg_address_watch_info aw_info; |
| 565 | unsigned char *args_buff; |
| 566 | long status; |
| 567 | void __user *cmd_from_user; |
| 568 | uint64_t watch_mask_value = 0; |
| 569 | unsigned int args_idx = 0; |
| 570 | |
| 571 | memset((void *) &aw_info, 0, sizeof(struct dbg_address_watch_info)); |
| 572 | |
| 573 | dev = kfd_device_by_id(args->gpu_id); |
Kent Russell | 4eacc26b | 2017-08-15 23:00:06 -0400 | [diff] [blame] | 574 | if (!dev) |
Yair Shachar | f8bd133 | 2015-05-20 14:09:39 +0300 | [diff] [blame] | 575 | return -EINVAL; |
| 576 | |
| 577 | if (dev->device_info->asic_family == CHIP_CARRIZO) { |
| 578 | pr_debug("kfd_ioctl_dbg_wave_control not supported on CZ\n"); |
| 579 | return -EINVAL; |
| 580 | } |
| 581 | |
| 582 | cmd_from_user = (void __user *) args->content_ptr; |
| 583 | |
| 584 | /* Validate arguments */ |
| 585 | |
| 586 | if ((args->buf_size_in_bytes > MAX_ALLOWED_AW_BUFF_SIZE) || |
Dan Carpenter | 7861c7a | 2015-06-16 13:49:32 +0300 | [diff] [blame] | 587 | (args->buf_size_in_bytes <= sizeof(*args) + sizeof(int) * 2) || |
Yair Shachar | f8bd133 | 2015-05-20 14:09:39 +0300 | [diff] [blame] | 588 | (cmd_from_user == NULL)) |
| 589 | return -EINVAL; |
| 590 | |
| 591 | /* this is the actual buffer to work with */ |
Borislav Petkov | 39c01bf | 2016-01-15 19:26:44 +0100 | [diff] [blame] | 592 | args_buff = memdup_user(cmd_from_user, |
Yair Shachar | f8bd133 | 2015-05-20 14:09:39 +0300 | [diff] [blame] | 593 | args->buf_size_in_bytes - sizeof(*args)); |
Al Viro | 8f1d57c | 2016-01-02 15:06:19 -0500 | [diff] [blame] | 594 | if (IS_ERR(args_buff)) |
| 595 | return PTR_ERR(args_buff); |
Yair Shachar | f8bd133 | 2015-05-20 14:09:39 +0300 | [diff] [blame] | 596 | |
| 597 | aw_info.process = p; |
| 598 | |
| 599 | aw_info.num_watch_points = *((uint32_t *)(&args_buff[args_idx])); |
| 600 | args_idx += sizeof(aw_info.num_watch_points); |
| 601 | |
| 602 | aw_info.watch_mode = (enum HSA_DBG_WATCH_MODE *) &args_buff[args_idx]; |
| 603 | args_idx += sizeof(enum HSA_DBG_WATCH_MODE) * aw_info.num_watch_points; |
| 604 | |
| 605 | /* |
| 606 | * set watch address base pointer to point on the array base |
| 607 | * within args_buff |
| 608 | */ |
| 609 | aw_info.watch_address = (uint64_t *) &args_buff[args_idx]; |
| 610 | |
| 611 | /* skip over the addresses buffer */ |
| 612 | args_idx += sizeof(aw_info.watch_address) * aw_info.num_watch_points; |
| 613 | |
Dan Carpenter | 7861c7a | 2015-06-16 13:49:32 +0300 | [diff] [blame] | 614 | if (args_idx >= args->buf_size_in_bytes - sizeof(*args)) { |
Kent Russell | ab7c164 | 2017-08-15 23:00:07 -0400 | [diff] [blame] | 615 | status = -EINVAL; |
| 616 | goto out; |
Yair Shachar | f8bd133 | 2015-05-20 14:09:39 +0300 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | watch_mask_value = (uint64_t) args_buff[args_idx]; |
| 620 | |
| 621 | if (watch_mask_value > 0) { |
| 622 | /* |
| 623 | * There is an array of masks. |
| 624 | * set watch mask base pointer to point on the array base |
| 625 | * within args_buff |
| 626 | */ |
| 627 | aw_info.watch_mask = (uint64_t *) &args_buff[args_idx]; |
| 628 | |
| 629 | /* skip over the masks buffer */ |
| 630 | args_idx += sizeof(aw_info.watch_mask) * |
| 631 | aw_info.num_watch_points; |
| 632 | } else { |
| 633 | /* just the NULL mask, set to NULL and skip over it */ |
| 634 | aw_info.watch_mask = NULL; |
| 635 | args_idx += sizeof(aw_info.watch_mask); |
| 636 | } |
| 637 | |
Dan Carpenter | 7861c7a | 2015-06-16 13:49:32 +0300 | [diff] [blame] | 638 | if (args_idx >= args->buf_size_in_bytes - sizeof(args)) { |
Kent Russell | ab7c164 | 2017-08-15 23:00:07 -0400 | [diff] [blame] | 639 | status = -EINVAL; |
| 640 | goto out; |
Yair Shachar | f8bd133 | 2015-05-20 14:09:39 +0300 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /* Currently HSA Event is not supported for DBG */ |
| 644 | aw_info.watch_event = NULL; |
| 645 | |
| 646 | mutex_lock(kfd_get_dbgmgr_mutex()); |
| 647 | |
| 648 | status = kfd_dbgmgr_address_watch(dev->dbgmgr, &aw_info); |
| 649 | |
| 650 | mutex_unlock(kfd_get_dbgmgr_mutex()); |
| 651 | |
Kent Russell | ab7c164 | 2017-08-15 23:00:07 -0400 | [diff] [blame] | 652 | out: |
Yair Shachar | f8bd133 | 2015-05-20 14:09:39 +0300 | [diff] [blame] | 653 | kfree(args_buff); |
Yair Shachar | aef1100 | 2014-12-07 17:05:22 +0200 | [diff] [blame] | 654 | |
| 655 | return status; |
| 656 | } |
| 657 | |
| 658 | /* Parse and generate fixed size data structure for wave control */ |
| 659 | static int kfd_ioctl_dbg_wave_control(struct file *filep, |
| 660 | struct kfd_process *p, void *data) |
| 661 | { |
Yair Shachar | 9448458 | 2015-05-20 14:09:24 +0300 | [diff] [blame] | 662 | struct kfd_ioctl_dbg_wave_control_args *args = data; |
| 663 | struct kfd_dev *dev; |
| 664 | struct dbg_wave_control_info wac_info; |
| 665 | unsigned char *args_buff; |
| 666 | uint32_t computed_buff_size; |
| 667 | long status; |
| 668 | void __user *cmd_from_user; |
| 669 | unsigned int args_idx = 0; |
| 670 | |
| 671 | memset((void *) &wac_info, 0, sizeof(struct dbg_wave_control_info)); |
| 672 | |
| 673 | /* we use compact form, independent of the packing attribute value */ |
| 674 | computed_buff_size = sizeof(*args) + |
| 675 | sizeof(wac_info.mode) + |
| 676 | sizeof(wac_info.operand) + |
| 677 | sizeof(wac_info.dbgWave_msg.DbgWaveMsg) + |
| 678 | sizeof(wac_info.dbgWave_msg.MemoryVA) + |
| 679 | sizeof(wac_info.trapId); |
| 680 | |
| 681 | dev = kfd_device_by_id(args->gpu_id); |
Kent Russell | 4eacc26b | 2017-08-15 23:00:06 -0400 | [diff] [blame] | 682 | if (!dev) |
Yair Shachar | 9448458 | 2015-05-20 14:09:24 +0300 | [diff] [blame] | 683 | return -EINVAL; |
| 684 | |
| 685 | if (dev->device_info->asic_family == CHIP_CARRIZO) { |
| 686 | pr_debug("kfd_ioctl_dbg_wave_control not supported on CZ\n"); |
| 687 | return -EINVAL; |
| 688 | } |
| 689 | |
| 690 | /* input size must match the computed "compact" size */ |
| 691 | if (args->buf_size_in_bytes != computed_buff_size) { |
| 692 | pr_debug("size mismatch, computed : actual %u : %u\n", |
| 693 | args->buf_size_in_bytes, computed_buff_size); |
| 694 | return -EINVAL; |
| 695 | } |
| 696 | |
| 697 | cmd_from_user = (void __user *) args->content_ptr; |
| 698 | |
| 699 | if (cmd_from_user == NULL) |
| 700 | return -EINVAL; |
| 701 | |
Al Viro | 8f1d57c | 2016-01-02 15:06:19 -0500 | [diff] [blame] | 702 | /* copy the entire buffer from user */ |
Yair Shachar | 9448458 | 2015-05-20 14:09:24 +0300 | [diff] [blame] | 703 | |
Al Viro | 8f1d57c | 2016-01-02 15:06:19 -0500 | [diff] [blame] | 704 | args_buff = memdup_user(cmd_from_user, |
Yair Shachar | 9448458 | 2015-05-20 14:09:24 +0300 | [diff] [blame] | 705 | args->buf_size_in_bytes - sizeof(*args)); |
Al Viro | 8f1d57c | 2016-01-02 15:06:19 -0500 | [diff] [blame] | 706 | if (IS_ERR(args_buff)) |
| 707 | return PTR_ERR(args_buff); |
Yair Shachar | 9448458 | 2015-05-20 14:09:24 +0300 | [diff] [blame] | 708 | |
| 709 | /* move ptr to the start of the "pay-load" area */ |
| 710 | wac_info.process = p; |
| 711 | |
| 712 | wac_info.operand = *((enum HSA_DBG_WAVEOP *)(&args_buff[args_idx])); |
| 713 | args_idx += sizeof(wac_info.operand); |
| 714 | |
| 715 | wac_info.mode = *((enum HSA_DBG_WAVEMODE *)(&args_buff[args_idx])); |
| 716 | args_idx += sizeof(wac_info.mode); |
| 717 | |
| 718 | wac_info.trapId = *((uint32_t *)(&args_buff[args_idx])); |
| 719 | args_idx += sizeof(wac_info.trapId); |
| 720 | |
| 721 | wac_info.dbgWave_msg.DbgWaveMsg.WaveMsgInfoGen2.Value = |
| 722 | *((uint32_t *)(&args_buff[args_idx])); |
| 723 | wac_info.dbgWave_msg.MemoryVA = NULL; |
| 724 | |
| 725 | mutex_lock(kfd_get_dbgmgr_mutex()); |
| 726 | |
| 727 | pr_debug("Calling dbg manager process %p, operand %u, mode %u, trapId %u, message %u\n", |
| 728 | wac_info.process, wac_info.operand, |
| 729 | wac_info.mode, wac_info.trapId, |
| 730 | wac_info.dbgWave_msg.DbgWaveMsg.WaveMsgInfoGen2.Value); |
| 731 | |
| 732 | status = kfd_dbgmgr_wave_control(dev->dbgmgr, &wac_info); |
| 733 | |
| 734 | pr_debug("Returned status of dbg manager is %ld\n", status); |
| 735 | |
| 736 | mutex_unlock(kfd_get_dbgmgr_mutex()); |
| 737 | |
| 738 | kfree(args_buff); |
Yair Shachar | aef1100 | 2014-12-07 17:05:22 +0200 | [diff] [blame] | 739 | |
| 740 | return status; |
| 741 | } |
| 742 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 743 | static int kfd_ioctl_get_clock_counters(struct file *filep, |
| 744 | struct kfd_process *p, void *data) |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 745 | { |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 746 | struct kfd_ioctl_get_clock_counters_args *args = data; |
Evgeny Pinchuk | 4fac47c | 2014-07-17 01:47:58 +0300 | [diff] [blame] | 747 | struct kfd_dev *dev; |
John Stultz | affa7d8 | 2015-03-12 10:23:40 -0700 | [diff] [blame] | 748 | struct timespec64 time; |
Evgeny Pinchuk | 4fac47c | 2014-07-17 01:47:58 +0300 | [diff] [blame] | 749 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 750 | dev = kfd_device_by_id(args->gpu_id); |
Evgeny Pinchuk | 4fac47c | 2014-07-17 01:47:58 +0300 | [diff] [blame] | 751 | if (dev == NULL) |
| 752 | return -EINVAL; |
| 753 | |
| 754 | /* Reading GPU clock counter from KGD */ |
Xihan Zhang | cea405b | 2015-03-17 19:32:53 +0800 | [diff] [blame] | 755 | args->gpu_clock_counter = |
| 756 | dev->kfd2kgd->get_gpu_clock_counter(dev->kgd); |
Evgeny Pinchuk | 4fac47c | 2014-07-17 01:47:58 +0300 | [diff] [blame] | 757 | |
| 758 | /* No access to rdtsc. Using raw monotonic time */ |
John Stultz | affa7d8 | 2015-03-12 10:23:40 -0700 | [diff] [blame] | 759 | getrawmonotonic64(&time); |
| 760 | args->cpu_clock_counter = (uint64_t)timespec64_to_ns(&time); |
Evgeny Pinchuk | 4fac47c | 2014-07-17 01:47:58 +0300 | [diff] [blame] | 761 | |
John Stultz | affa7d8 | 2015-03-12 10:23:40 -0700 | [diff] [blame] | 762 | get_monotonic_boottime64(&time); |
| 763 | args->system_clock_counter = (uint64_t)timespec64_to_ns(&time); |
Evgeny Pinchuk | 4fac47c | 2014-07-17 01:47:58 +0300 | [diff] [blame] | 764 | |
| 765 | /* Since the counter is in nano-seconds we use 1GHz frequency */ |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 766 | args->system_clock_freq = 1000000000; |
Evgeny Pinchuk | 4fac47c | 2014-07-17 01:47:58 +0300 | [diff] [blame] | 767 | |
| 768 | return 0; |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | |
| 772 | static int kfd_ioctl_get_process_apertures(struct file *filp, |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 773 | struct kfd_process *p, void *data) |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 774 | { |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 775 | struct kfd_ioctl_get_process_apertures_args *args = data; |
Alexey Skidanov | 775921e | 2014-07-17 01:49:36 +0300 | [diff] [blame] | 776 | struct kfd_process_device_apertures *pAperture; |
| 777 | struct kfd_process_device *pdd; |
| 778 | |
| 779 | dev_dbg(kfd_device, "get apertures for PASID %d", p->pasid); |
| 780 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 781 | args->num_of_nodes = 0; |
Alexey Skidanov | 775921e | 2014-07-17 01:49:36 +0300 | [diff] [blame] | 782 | |
| 783 | mutex_lock(&p->mutex); |
| 784 | |
| 785 | /*if the process-device list isn't empty*/ |
| 786 | if (kfd_has_process_device_data(p)) { |
| 787 | /* Run over all pdd of the process */ |
| 788 | pdd = kfd_get_first_process_device_data(p); |
| 789 | do { |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 790 | pAperture = |
| 791 | &args->process_apertures[args->num_of_nodes]; |
Alexey Skidanov | 775921e | 2014-07-17 01:49:36 +0300 | [diff] [blame] | 792 | pAperture->gpu_id = pdd->dev->id; |
| 793 | pAperture->lds_base = pdd->lds_base; |
| 794 | pAperture->lds_limit = pdd->lds_limit; |
| 795 | pAperture->gpuvm_base = pdd->gpuvm_base; |
| 796 | pAperture->gpuvm_limit = pdd->gpuvm_limit; |
| 797 | pAperture->scratch_base = pdd->scratch_base; |
| 798 | pAperture->scratch_limit = pdd->scratch_limit; |
| 799 | |
| 800 | dev_dbg(kfd_device, |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 801 | "node id %u\n", args->num_of_nodes); |
Alexey Skidanov | 775921e | 2014-07-17 01:49:36 +0300 | [diff] [blame] | 802 | dev_dbg(kfd_device, |
| 803 | "gpu id %u\n", pdd->dev->id); |
| 804 | dev_dbg(kfd_device, |
| 805 | "lds_base %llX\n", pdd->lds_base); |
| 806 | dev_dbg(kfd_device, |
| 807 | "lds_limit %llX\n", pdd->lds_limit); |
| 808 | dev_dbg(kfd_device, |
| 809 | "gpuvm_base %llX\n", pdd->gpuvm_base); |
| 810 | dev_dbg(kfd_device, |
| 811 | "gpuvm_limit %llX\n", pdd->gpuvm_limit); |
| 812 | dev_dbg(kfd_device, |
| 813 | "scratch_base %llX\n", pdd->scratch_base); |
| 814 | dev_dbg(kfd_device, |
| 815 | "scratch_limit %llX\n", pdd->scratch_limit); |
| 816 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 817 | args->num_of_nodes++; |
Kent Russell | 4eacc26b | 2017-08-15 23:00:06 -0400 | [diff] [blame] | 818 | |
| 819 | pdd = kfd_get_next_process_device_data(p, pdd); |
| 820 | } while (pdd && (args->num_of_nodes < NUM_OF_SUPPORTED_GPUS)); |
Alexey Skidanov | 775921e | 2014-07-17 01:49:36 +0300 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | mutex_unlock(&p->mutex); |
| 824 | |
Alexey Skidanov | 775921e | 2014-07-17 01:49:36 +0300 | [diff] [blame] | 825 | return 0; |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 826 | } |
| 827 | |
Andrew Lewycky | 29a5d3e | 2014-12-07 17:05:11 +0200 | [diff] [blame] | 828 | static int kfd_ioctl_create_event(struct file *filp, struct kfd_process *p, |
| 829 | void *data) |
| 830 | { |
Andrew Lewycky | 8377396 | 2014-09-09 15:22:05 +0300 | [diff] [blame] | 831 | struct kfd_ioctl_create_event_args *args = data; |
| 832 | int err; |
| 833 | |
| 834 | err = kfd_event_create(filp, p, args->event_type, |
| 835 | args->auto_reset != 0, args->node_id, |
| 836 | &args->event_id, &args->event_trigger_data, |
| 837 | &args->event_page_offset, |
| 838 | &args->event_slot_index); |
| 839 | |
| 840 | return err; |
Andrew Lewycky | 29a5d3e | 2014-12-07 17:05:11 +0200 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | static int kfd_ioctl_destroy_event(struct file *filp, struct kfd_process *p, |
| 844 | void *data) |
| 845 | { |
Andrew Lewycky | 8377396 | 2014-09-09 15:22:05 +0300 | [diff] [blame] | 846 | struct kfd_ioctl_destroy_event_args *args = data; |
| 847 | |
| 848 | return kfd_event_destroy(p, args->event_id); |
Andrew Lewycky | 29a5d3e | 2014-12-07 17:05:11 +0200 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | static int kfd_ioctl_set_event(struct file *filp, struct kfd_process *p, |
| 852 | void *data) |
| 853 | { |
Andrew Lewycky | 8377396 | 2014-09-09 15:22:05 +0300 | [diff] [blame] | 854 | struct kfd_ioctl_set_event_args *args = data; |
| 855 | |
| 856 | return kfd_set_event(p, args->event_id); |
Andrew Lewycky | 29a5d3e | 2014-12-07 17:05:11 +0200 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | static int kfd_ioctl_reset_event(struct file *filp, struct kfd_process *p, |
| 860 | void *data) |
| 861 | { |
Andrew Lewycky | 8377396 | 2014-09-09 15:22:05 +0300 | [diff] [blame] | 862 | struct kfd_ioctl_reset_event_args *args = data; |
| 863 | |
| 864 | return kfd_reset_event(p, args->event_id); |
Andrew Lewycky | 29a5d3e | 2014-12-07 17:05:11 +0200 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | static int kfd_ioctl_wait_events(struct file *filp, struct kfd_process *p, |
| 868 | void *data) |
| 869 | { |
Andrew Lewycky | 8377396 | 2014-09-09 15:22:05 +0300 | [diff] [blame] | 870 | struct kfd_ioctl_wait_events_args *args = data; |
Andrew Lewycky | 8377396 | 2014-09-09 15:22:05 +0300 | [diff] [blame] | 871 | int err; |
| 872 | |
| 873 | err = kfd_wait_on_events(p, args->num_events, |
| 874 | (void __user *)args->events_ptr, |
| 875 | (args->wait_for_all != 0), |
Felix Kuehling | fdf0c83 | 2017-10-27 19:35:22 -0400 | [diff] [blame] | 876 | args->timeout, &args->wait_result); |
Andrew Lewycky | 8377396 | 2014-09-09 15:22:05 +0300 | [diff] [blame] | 877 | |
| 878 | return err; |
Andrew Lewycky | 29a5d3e | 2014-12-07 17:05:11 +0200 | [diff] [blame] | 879 | } |
Moses Reuben | 6a1c951 | 2017-08-15 23:00:20 -0400 | [diff] [blame] | 880 | static int kfd_ioctl_set_scratch_backing_va(struct file *filep, |
| 881 | struct kfd_process *p, void *data) |
| 882 | { |
| 883 | struct kfd_ioctl_set_scratch_backing_va_args *args = data; |
| 884 | struct kfd_process_device *pdd; |
| 885 | struct kfd_dev *dev; |
| 886 | long err; |
| 887 | |
| 888 | dev = kfd_device_by_id(args->gpu_id); |
| 889 | if (!dev) |
| 890 | return -EINVAL; |
| 891 | |
| 892 | mutex_lock(&p->mutex); |
| 893 | |
| 894 | pdd = kfd_bind_process_to_device(dev, p); |
| 895 | if (IS_ERR(pdd)) { |
| 896 | err = PTR_ERR(pdd); |
| 897 | goto bind_process_to_device_fail; |
| 898 | } |
| 899 | |
| 900 | pdd->qpd.sh_hidden_private_base = args->va_addr; |
| 901 | |
| 902 | mutex_unlock(&p->mutex); |
| 903 | |
| 904 | if (sched_policy == KFD_SCHED_POLICY_NO_HWS && pdd->qpd.vmid != 0) |
| 905 | dev->kfd2kgd->set_scratch_backing_va( |
| 906 | dev->kgd, args->va_addr, pdd->qpd.vmid); |
| 907 | |
| 908 | return 0; |
| 909 | |
| 910 | bind_process_to_device_fail: |
| 911 | mutex_unlock(&p->mutex); |
| 912 | return err; |
| 913 | } |
Andrew Lewycky | 29a5d3e | 2014-12-07 17:05:11 +0200 | [diff] [blame] | 914 | |
Yong Zhao | 5d71dbc | 2017-08-15 23:00:22 -0400 | [diff] [blame] | 915 | static int kfd_ioctl_get_tile_config(struct file *filep, |
| 916 | struct kfd_process *p, void *data) |
| 917 | { |
| 918 | struct kfd_ioctl_get_tile_config_args *args = data; |
| 919 | struct kfd_dev *dev; |
| 920 | struct tile_config config; |
| 921 | int err = 0; |
| 922 | |
| 923 | dev = kfd_device_by_id(args->gpu_id); |
Colin Ian King | bfaa1ce | 2017-09-08 15:13:33 +0100 | [diff] [blame] | 924 | if (!dev) |
| 925 | return -EINVAL; |
Yong Zhao | 5d71dbc | 2017-08-15 23:00:22 -0400 | [diff] [blame] | 926 | |
| 927 | dev->kfd2kgd->get_tile_config(dev->kgd, &config); |
| 928 | |
| 929 | args->gb_addr_config = config.gb_addr_config; |
| 930 | args->num_banks = config.num_banks; |
| 931 | args->num_ranks = config.num_ranks; |
| 932 | |
| 933 | if (args->num_tile_configs > config.num_tile_configs) |
| 934 | args->num_tile_configs = config.num_tile_configs; |
| 935 | err = copy_to_user((void __user *)args->tile_config_ptr, |
| 936 | config.tile_config_ptr, |
| 937 | args->num_tile_configs * sizeof(uint32_t)); |
| 938 | if (err) { |
| 939 | args->num_tile_configs = 0; |
| 940 | return -EFAULT; |
| 941 | } |
| 942 | |
| 943 | if (args->num_macro_tile_configs > config.num_macro_tile_configs) |
| 944 | args->num_macro_tile_configs = |
| 945 | config.num_macro_tile_configs; |
| 946 | err = copy_to_user((void __user *)args->macro_tile_config_ptr, |
| 947 | config.macro_tile_config_ptr, |
| 948 | args->num_macro_tile_configs * sizeof(uint32_t)); |
| 949 | if (err) { |
| 950 | args->num_macro_tile_configs = 0; |
| 951 | return -EFAULT; |
| 952 | } |
| 953 | |
| 954 | return 0; |
| 955 | } |
| 956 | |
Oded Gabbay | 76baee6 | 2014-12-29 14:20:05 +0200 | [diff] [blame] | 957 | #define AMDKFD_IOCTL_DEF(ioctl, _func, _flags) \ |
Kent Russell | 8eabaf5 | 2017-08-15 23:00:04 -0400 | [diff] [blame] | 958 | [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func, .flags = _flags, \ |
| 959 | .cmd_drv = 0, .name = #ioctl} |
Oded Gabbay | 76baee6 | 2014-12-29 14:20:05 +0200 | [diff] [blame] | 960 | |
| 961 | /** Ioctl table */ |
| 962 | static const struct amdkfd_ioctl_desc amdkfd_ioctls[] = { |
| 963 | AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_VERSION, |
| 964 | kfd_ioctl_get_version, 0), |
| 965 | |
| 966 | AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_QUEUE, |
| 967 | kfd_ioctl_create_queue, 0), |
| 968 | |
| 969 | AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_QUEUE, |
| 970 | kfd_ioctl_destroy_queue, 0), |
| 971 | |
| 972 | AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_MEMORY_POLICY, |
| 973 | kfd_ioctl_set_memory_policy, 0), |
| 974 | |
| 975 | AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_CLOCK_COUNTERS, |
| 976 | kfd_ioctl_get_clock_counters, 0), |
| 977 | |
| 978 | AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_PROCESS_APERTURES, |
| 979 | kfd_ioctl_get_process_apertures, 0), |
| 980 | |
| 981 | AMDKFD_IOCTL_DEF(AMDKFD_IOC_UPDATE_QUEUE, |
| 982 | kfd_ioctl_update_queue, 0), |
Andrew Lewycky | 29a5d3e | 2014-12-07 17:05:11 +0200 | [diff] [blame] | 983 | |
| 984 | AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_EVENT, |
| 985 | kfd_ioctl_create_event, 0), |
| 986 | |
| 987 | AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_EVENT, |
| 988 | kfd_ioctl_destroy_event, 0), |
| 989 | |
| 990 | AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_EVENT, |
| 991 | kfd_ioctl_set_event, 0), |
| 992 | |
| 993 | AMDKFD_IOCTL_DEF(AMDKFD_IOC_RESET_EVENT, |
| 994 | kfd_ioctl_reset_event, 0), |
| 995 | |
| 996 | AMDKFD_IOCTL_DEF(AMDKFD_IOC_WAIT_EVENTS, |
| 997 | kfd_ioctl_wait_events, 0), |
Yair Shachar | aef1100 | 2014-12-07 17:05:22 +0200 | [diff] [blame] | 998 | |
| 999 | AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_REGISTER, |
| 1000 | kfd_ioctl_dbg_register, 0), |
| 1001 | |
| 1002 | AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_UNREGISTER, |
Colin Ian King | a7522cd | 2016-11-12 17:33:29 +0000 | [diff] [blame] | 1003 | kfd_ioctl_dbg_unregister, 0), |
Yair Shachar | aef1100 | 2014-12-07 17:05:22 +0200 | [diff] [blame] | 1004 | |
| 1005 | AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_ADDRESS_WATCH, |
| 1006 | kfd_ioctl_dbg_address_watch, 0), |
| 1007 | |
| 1008 | AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_WAVE_CONTROL, |
| 1009 | kfd_ioctl_dbg_wave_control, 0), |
Moses Reuben | 6a1c951 | 2017-08-15 23:00:20 -0400 | [diff] [blame] | 1010 | |
| 1011 | AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_SCRATCH_BACKING_VA, |
| 1012 | kfd_ioctl_set_scratch_backing_va, 0), |
Yong Zhao | 5d71dbc | 2017-08-15 23:00:22 -0400 | [diff] [blame] | 1013 | |
| 1014 | AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_TILE_CONFIG, |
Felix Kuehling | d7b9bd2 | 2017-11-14 16:41:20 -0500 | [diff] [blame^] | 1015 | kfd_ioctl_get_tile_config, 0), |
| 1016 | |
| 1017 | AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_TRAP_HANDLER, |
| 1018 | kfd_ioctl_set_trap_handler, 0), |
Oded Gabbay | 76baee6 | 2014-12-29 14:20:05 +0200 | [diff] [blame] | 1019 | }; |
| 1020 | |
| 1021 | #define AMDKFD_CORE_IOCTL_COUNT ARRAY_SIZE(amdkfd_ioctls) |
| 1022 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 1023 | static long kfd_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) |
| 1024 | { |
| 1025 | struct kfd_process *process; |
Oded Gabbay | 76baee6 | 2014-12-29 14:20:05 +0200 | [diff] [blame] | 1026 | amdkfd_ioctl_t *func; |
| 1027 | const struct amdkfd_ioctl_desc *ioctl = NULL; |
| 1028 | unsigned int nr = _IOC_NR(cmd); |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 1029 | char stack_kdata[128]; |
| 1030 | char *kdata = NULL; |
| 1031 | unsigned int usize, asize; |
| 1032 | int retcode = -EINVAL; |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 1033 | |
Oded Gabbay | 76baee6 | 2014-12-29 14:20:05 +0200 | [diff] [blame] | 1034 | if (nr >= AMDKFD_CORE_IOCTL_COUNT) |
| 1035 | goto err_i1; |
| 1036 | |
| 1037 | if ((nr >= AMDKFD_COMMAND_START) && (nr < AMDKFD_COMMAND_END)) { |
| 1038 | u32 amdkfd_size; |
| 1039 | |
| 1040 | ioctl = &amdkfd_ioctls[nr]; |
| 1041 | |
| 1042 | amdkfd_size = _IOC_SIZE(ioctl->cmd); |
| 1043 | usize = asize = _IOC_SIZE(cmd); |
| 1044 | if (amdkfd_size > asize) |
| 1045 | asize = amdkfd_size; |
| 1046 | |
| 1047 | cmd = ioctl->cmd; |
| 1048 | } else |
| 1049 | goto err_i1; |
| 1050 | |
| 1051 | dev_dbg(kfd_device, "ioctl cmd 0x%x (#%d), arg 0x%lx\n", cmd, nr, arg); |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 1052 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 1053 | process = kfd_get_process(current); |
Oded Gabbay | 76baee6 | 2014-12-29 14:20:05 +0200 | [diff] [blame] | 1054 | if (IS_ERR(process)) { |
| 1055 | dev_dbg(kfd_device, "no process\n"); |
| 1056 | goto err_i1; |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 1057 | } |
| 1058 | |
Oded Gabbay | 76baee6 | 2014-12-29 14:20:05 +0200 | [diff] [blame] | 1059 | /* Do not trust userspace, use our own definition */ |
| 1060 | func = ioctl->func; |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 1061 | |
Oded Gabbay | 76baee6 | 2014-12-29 14:20:05 +0200 | [diff] [blame] | 1062 | if (unlikely(!func)) { |
| 1063 | dev_dbg(kfd_device, "no function\n"); |
| 1064 | retcode = -EINVAL; |
| 1065 | goto err_i1; |
| 1066 | } |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 1067 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 1068 | if (cmd & (IOC_IN | IOC_OUT)) { |
| 1069 | if (asize <= sizeof(stack_kdata)) { |
| 1070 | kdata = stack_kdata; |
| 1071 | } else { |
| 1072 | kdata = kmalloc(asize, GFP_KERNEL); |
| 1073 | if (!kdata) { |
| 1074 | retcode = -ENOMEM; |
| 1075 | goto err_i1; |
| 1076 | } |
| 1077 | } |
| 1078 | if (asize > usize) |
| 1079 | memset(kdata + usize, 0, asize - usize); |
| 1080 | } |
| 1081 | |
| 1082 | if (cmd & IOC_IN) { |
| 1083 | if (copy_from_user(kdata, (void __user *)arg, usize) != 0) { |
| 1084 | retcode = -EFAULT; |
| 1085 | goto err_i1; |
| 1086 | } |
| 1087 | } else if (cmd & IOC_OUT) { |
| 1088 | memset(kdata, 0, usize); |
| 1089 | } |
| 1090 | |
Oded Gabbay | 76baee6 | 2014-12-29 14:20:05 +0200 | [diff] [blame] | 1091 | retcode = func(filep, process, kdata); |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 1092 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 1093 | if (cmd & IOC_OUT) |
| 1094 | if (copy_to_user((void __user *)arg, kdata, usize) != 0) |
| 1095 | retcode = -EFAULT; |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 1096 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 1097 | err_i1: |
Oded Gabbay | 76baee6 | 2014-12-29 14:20:05 +0200 | [diff] [blame] | 1098 | if (!ioctl) |
| 1099 | dev_dbg(kfd_device, "invalid ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n", |
| 1100 | task_pid_nr(current), cmd, nr); |
| 1101 | |
Oded Gabbay | 524a640 | 2014-12-29 13:52:22 +0200 | [diff] [blame] | 1102 | if (kdata != stack_kdata) |
| 1103 | kfree(kdata); |
| 1104 | |
| 1105 | if (retcode) |
| 1106 | dev_dbg(kfd_device, "ret = %d\n", retcode); |
| 1107 | |
| 1108 | return retcode; |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 1109 | } |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 1110 | |
| 1111 | static int kfd_mmap(struct file *filp, struct vm_area_struct *vma) |
| 1112 | { |
| 1113 | struct kfd_process *process; |
| 1114 | |
| 1115 | process = kfd_get_process(current); |
| 1116 | if (IS_ERR(process)) |
| 1117 | return PTR_ERR(process); |
| 1118 | |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 1119 | if ((vma->vm_pgoff & KFD_MMAP_DOORBELL_MASK) == |
| 1120 | KFD_MMAP_DOORBELL_MASK) { |
| 1121 | vma->vm_pgoff = vma->vm_pgoff ^ KFD_MMAP_DOORBELL_MASK; |
| 1122 | return kfd_doorbell_mmap(process, vma); |
| 1123 | } else if ((vma->vm_pgoff & KFD_MMAP_EVENTS_MASK) == |
| 1124 | KFD_MMAP_EVENTS_MASK) { |
| 1125 | vma->vm_pgoff = vma->vm_pgoff ^ KFD_MMAP_EVENTS_MASK; |
| 1126 | return kfd_event_mmap(process, vma); |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 1127 | } else if ((vma->vm_pgoff & KFD_MMAP_RESERVED_MEM_MASK) == |
| 1128 | KFD_MMAP_RESERVED_MEM_MASK) { |
| 1129 | vma->vm_pgoff = vma->vm_pgoff ^ KFD_MMAP_RESERVED_MEM_MASK; |
| 1130 | return kfd_reserved_mem_mmap(process, vma); |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | return -EFAULT; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 1134 | } |