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