1 1.1 riastrad /* $NetBSD: kfd_chardev.c,v 1.3 2021/12/18 23:44:59 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright 2014 Advanced Micro Devices, Inc. 5 1.1 riastrad * 6 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 7 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 8 1.1 riastrad * to deal in the Software without restriction, including without limitation 9 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 11 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 12 1.1 riastrad * 13 1.1 riastrad * The above copyright notice and this permission notice shall be included in 14 1.1 riastrad * all copies or substantial portions of the Software. 15 1.1 riastrad * 16 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 1.1 riastrad * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE. 23 1.1 riastrad */ 24 1.1 riastrad 25 1.1 riastrad #include <sys/cdefs.h> 26 1.1 riastrad __KERNEL_RCSID(0, "$NetBSD: kfd_chardev.c,v 1.3 2021/12/18 23:44:59 riastradh Exp $"); 27 1.1 riastrad 28 1.1 riastrad #include <linux/device.h> 29 1.1 riastrad #include <linux/export.h> 30 1.1 riastrad #include <linux/err.h> 31 1.1 riastrad #include <linux/fs.h> 32 1.3 riastrad #include <linux/file.h> 33 1.1 riastrad #include <linux/sched.h> 34 1.1 riastrad #include <linux/slab.h> 35 1.1 riastrad #include <linux/uaccess.h> 36 1.1 riastrad #include <linux/compat.h> 37 1.1 riastrad #include <uapi/linux/kfd_ioctl.h> 38 1.1 riastrad #include <linux/time.h> 39 1.1 riastrad #include <linux/mm.h> 40 1.1 riastrad #include <linux/mman.h> 41 1.3 riastrad #include <linux/dma-buf.h> 42 1.1 riastrad #include <asm/processor.h> 43 1.1 riastrad #include "kfd_priv.h" 44 1.1 riastrad #include "kfd_device_queue_manager.h" 45 1.1 riastrad #include "kfd_dbgmgr.h" 46 1.3 riastrad #include "amdgpu_amdkfd.h" 47 1.1 riastrad 48 1.1 riastrad static long kfd_ioctl(struct file *, unsigned int, unsigned long); 49 1.1 riastrad static int kfd_open(struct inode *, struct file *); 50 1.3 riastrad static int kfd_release(struct inode *, struct file *); 51 1.1 riastrad static int kfd_mmap(struct file *, struct vm_area_struct *); 52 1.1 riastrad 53 1.1 riastrad static const char kfd_dev_name[] = "kfd"; 54 1.1 riastrad 55 1.1 riastrad static const struct file_operations kfd_fops = { 56 1.1 riastrad .owner = THIS_MODULE, 57 1.1 riastrad .unlocked_ioctl = kfd_ioctl, 58 1.3 riastrad .compat_ioctl = compat_ptr_ioctl, 59 1.1 riastrad .open = kfd_open, 60 1.3 riastrad .release = kfd_release, 61 1.1 riastrad .mmap = kfd_mmap, 62 1.1 riastrad }; 63 1.1 riastrad 64 1.1 riastrad static int kfd_char_dev_major = -1; 65 1.1 riastrad static struct class *kfd_class; 66 1.1 riastrad struct device *kfd_device; 67 1.1 riastrad 68 1.1 riastrad int kfd_chardev_init(void) 69 1.1 riastrad { 70 1.1 riastrad int err = 0; 71 1.1 riastrad 72 1.1 riastrad kfd_char_dev_major = register_chrdev(0, kfd_dev_name, &kfd_fops); 73 1.1 riastrad err = kfd_char_dev_major; 74 1.1 riastrad if (err < 0) 75 1.1 riastrad goto err_register_chrdev; 76 1.1 riastrad 77 1.1 riastrad kfd_class = class_create(THIS_MODULE, kfd_dev_name); 78 1.1 riastrad err = PTR_ERR(kfd_class); 79 1.1 riastrad if (IS_ERR(kfd_class)) 80 1.1 riastrad goto err_class_create; 81 1.1 riastrad 82 1.1 riastrad kfd_device = device_create(kfd_class, NULL, 83 1.1 riastrad MKDEV(kfd_char_dev_major, 0), 84 1.1 riastrad NULL, kfd_dev_name); 85 1.1 riastrad err = PTR_ERR(kfd_device); 86 1.1 riastrad if (IS_ERR(kfd_device)) 87 1.1 riastrad goto err_device_create; 88 1.1 riastrad 89 1.1 riastrad return 0; 90 1.1 riastrad 91 1.1 riastrad err_device_create: 92 1.1 riastrad class_destroy(kfd_class); 93 1.1 riastrad err_class_create: 94 1.1 riastrad unregister_chrdev(kfd_char_dev_major, kfd_dev_name); 95 1.1 riastrad err_register_chrdev: 96 1.1 riastrad return err; 97 1.1 riastrad } 98 1.1 riastrad 99 1.1 riastrad void kfd_chardev_exit(void) 100 1.1 riastrad { 101 1.1 riastrad device_destroy(kfd_class, MKDEV(kfd_char_dev_major, 0)); 102 1.1 riastrad class_destroy(kfd_class); 103 1.1 riastrad unregister_chrdev(kfd_char_dev_major, kfd_dev_name); 104 1.1 riastrad } 105 1.1 riastrad 106 1.1 riastrad struct device *kfd_chardev(void) 107 1.1 riastrad { 108 1.1 riastrad return kfd_device; 109 1.1 riastrad } 110 1.1 riastrad 111 1.1 riastrad 112 1.1 riastrad static int kfd_open(struct inode *inode, struct file *filep) 113 1.1 riastrad { 114 1.1 riastrad struct kfd_process *process; 115 1.1 riastrad bool is_32bit_user_mode; 116 1.1 riastrad 117 1.1 riastrad if (iminor(inode) != 0) 118 1.1 riastrad return -ENODEV; 119 1.1 riastrad 120 1.3 riastrad is_32bit_user_mode = in_compat_syscall(); 121 1.1 riastrad 122 1.3 riastrad if (is_32bit_user_mode) { 123 1.1 riastrad dev_warn(kfd_device, 124 1.1 riastrad "Process %d (32-bit) failed to open /dev/kfd\n" 125 1.1 riastrad "32-bit processes are not supported by amdkfd\n", 126 1.1 riastrad current->pid); 127 1.1 riastrad return -EPERM; 128 1.1 riastrad } 129 1.1 riastrad 130 1.3 riastrad process = kfd_create_process(filep); 131 1.1 riastrad if (IS_ERR(process)) 132 1.1 riastrad return PTR_ERR(process); 133 1.1 riastrad 134 1.3 riastrad if (kfd_is_locked()) { 135 1.3 riastrad kfd_unref_process(process); 136 1.3 riastrad return -EAGAIN; 137 1.3 riastrad } 138 1.3 riastrad 139 1.3 riastrad /* filep now owns the reference returned by kfd_create_process */ 140 1.3 riastrad filep->private_data = process; 141 1.3 riastrad 142 1.1 riastrad dev_dbg(kfd_device, "process %d opened, compat mode (32 bit) - %d\n", 143 1.1 riastrad process->pasid, process->is_32bit_user_mode); 144 1.1 riastrad 145 1.1 riastrad return 0; 146 1.1 riastrad } 147 1.1 riastrad 148 1.3 riastrad static int kfd_release(struct inode *inode, struct file *filep) 149 1.3 riastrad { 150 1.3 riastrad struct kfd_process *process = filep->private_data; 151 1.3 riastrad 152 1.3 riastrad if (process) 153 1.3 riastrad kfd_unref_process(process); 154 1.3 riastrad 155 1.3 riastrad return 0; 156 1.3 riastrad } 157 1.3 riastrad 158 1.1 riastrad static int kfd_ioctl_get_version(struct file *filep, struct kfd_process *p, 159 1.1 riastrad void *data) 160 1.1 riastrad { 161 1.1 riastrad struct kfd_ioctl_get_version_args *args = data; 162 1.1 riastrad 163 1.1 riastrad args->major_version = KFD_IOCTL_MAJOR_VERSION; 164 1.1 riastrad args->minor_version = KFD_IOCTL_MINOR_VERSION; 165 1.1 riastrad 166 1.3 riastrad return 0; 167 1.1 riastrad } 168 1.1 riastrad 169 1.1 riastrad static int set_queue_properties_from_user(struct queue_properties *q_properties, 170 1.1 riastrad struct kfd_ioctl_create_queue_args *args) 171 1.1 riastrad { 172 1.1 riastrad if (args->queue_percentage > KFD_MAX_QUEUE_PERCENTAGE) { 173 1.3 riastrad pr_err("Queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n"); 174 1.1 riastrad return -EINVAL; 175 1.1 riastrad } 176 1.1 riastrad 177 1.1 riastrad if (args->queue_priority > KFD_MAX_QUEUE_PRIORITY) { 178 1.3 riastrad pr_err("Queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n"); 179 1.1 riastrad return -EINVAL; 180 1.1 riastrad } 181 1.1 riastrad 182 1.1 riastrad if ((args->ring_base_address) && 183 1.3 riastrad (!access_ok((const void __user *) args->ring_base_address, 184 1.1 riastrad sizeof(uint64_t)))) { 185 1.3 riastrad pr_err("Can't access ring base address\n"); 186 1.1 riastrad return -EFAULT; 187 1.1 riastrad } 188 1.1 riastrad 189 1.1 riastrad if (!is_power_of_2(args->ring_size) && (args->ring_size != 0)) { 190 1.3 riastrad pr_err("Ring size must be a power of 2 or 0\n"); 191 1.1 riastrad return -EINVAL; 192 1.1 riastrad } 193 1.1 riastrad 194 1.3 riastrad if (!access_ok((const void __user *) args->read_pointer_address, 195 1.1 riastrad sizeof(uint32_t))) { 196 1.3 riastrad pr_err("Can't access read pointer\n"); 197 1.1 riastrad return -EFAULT; 198 1.1 riastrad } 199 1.1 riastrad 200 1.3 riastrad if (!access_ok((const void __user *) args->write_pointer_address, 201 1.1 riastrad sizeof(uint32_t))) { 202 1.3 riastrad pr_err("Can't access write pointer\n"); 203 1.1 riastrad return -EFAULT; 204 1.1 riastrad } 205 1.1 riastrad 206 1.1 riastrad if (args->eop_buffer_address && 207 1.3 riastrad !access_ok((const void __user *) args->eop_buffer_address, 208 1.1 riastrad sizeof(uint32_t))) { 209 1.3 riastrad pr_debug("Can't access eop buffer"); 210 1.1 riastrad return -EFAULT; 211 1.1 riastrad } 212 1.1 riastrad 213 1.1 riastrad if (args->ctx_save_restore_address && 214 1.3 riastrad !access_ok((const void __user *) args->ctx_save_restore_address, 215 1.1 riastrad sizeof(uint32_t))) { 216 1.3 riastrad pr_debug("Can't access ctx save restore buffer"); 217 1.1 riastrad return -EFAULT; 218 1.1 riastrad } 219 1.1 riastrad 220 1.1 riastrad q_properties->is_interop = false; 221 1.1 riastrad q_properties->queue_percent = args->queue_percentage; 222 1.1 riastrad q_properties->priority = args->queue_priority; 223 1.1 riastrad q_properties->queue_address = args->ring_base_address; 224 1.1 riastrad q_properties->queue_size = args->ring_size; 225 1.1 riastrad q_properties->read_ptr = (uint32_t *) args->read_pointer_address; 226 1.1 riastrad q_properties->write_ptr = (uint32_t *) args->write_pointer_address; 227 1.1 riastrad q_properties->eop_ring_buffer_address = args->eop_buffer_address; 228 1.1 riastrad q_properties->eop_ring_buffer_size = args->eop_buffer_size; 229 1.1 riastrad q_properties->ctx_save_restore_area_address = 230 1.1 riastrad args->ctx_save_restore_address; 231 1.1 riastrad q_properties->ctx_save_restore_area_size = args->ctx_save_restore_size; 232 1.3 riastrad q_properties->ctl_stack_size = args->ctl_stack_size; 233 1.1 riastrad if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE || 234 1.1 riastrad args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL) 235 1.1 riastrad q_properties->type = KFD_QUEUE_TYPE_COMPUTE; 236 1.1 riastrad else if (args->queue_type == KFD_IOC_QUEUE_TYPE_SDMA) 237 1.1 riastrad q_properties->type = KFD_QUEUE_TYPE_SDMA; 238 1.3 riastrad else if (args->queue_type == KFD_IOC_QUEUE_TYPE_SDMA_XGMI) 239 1.3 riastrad q_properties->type = KFD_QUEUE_TYPE_SDMA_XGMI; 240 1.1 riastrad else 241 1.1 riastrad return -ENOTSUPP; 242 1.1 riastrad 243 1.1 riastrad if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL) 244 1.1 riastrad q_properties->format = KFD_QUEUE_FORMAT_AQL; 245 1.1 riastrad else 246 1.1 riastrad q_properties->format = KFD_QUEUE_FORMAT_PM4; 247 1.1 riastrad 248 1.3 riastrad pr_debug("Queue Percentage: %d, %d\n", 249 1.1 riastrad q_properties->queue_percent, args->queue_percentage); 250 1.1 riastrad 251 1.3 riastrad pr_debug("Queue Priority: %d, %d\n", 252 1.1 riastrad q_properties->priority, args->queue_priority); 253 1.1 riastrad 254 1.3 riastrad pr_debug("Queue Address: 0x%llX, 0x%llX\n", 255 1.1 riastrad q_properties->queue_address, args->ring_base_address); 256 1.1 riastrad 257 1.3 riastrad pr_debug("Queue Size: 0x%llX, %u\n", 258 1.1 riastrad q_properties->queue_size, args->ring_size); 259 1.1 riastrad 260 1.3 riastrad pr_debug("Queue r/w Pointers: %px, %px\n", 261 1.3 riastrad q_properties->read_ptr, 262 1.3 riastrad q_properties->write_ptr); 263 1.1 riastrad 264 1.3 riastrad pr_debug("Queue Format: %d\n", q_properties->format); 265 1.1 riastrad 266 1.3 riastrad pr_debug("Queue EOP: 0x%llX\n", q_properties->eop_ring_buffer_address); 267 1.1 riastrad 268 1.3 riastrad pr_debug("Queue CTX save area: 0x%llX\n", 269 1.1 riastrad q_properties->ctx_save_restore_area_address); 270 1.1 riastrad 271 1.1 riastrad return 0; 272 1.1 riastrad } 273 1.1 riastrad 274 1.1 riastrad static int kfd_ioctl_create_queue(struct file *filep, struct kfd_process *p, 275 1.1 riastrad void *data) 276 1.1 riastrad { 277 1.1 riastrad struct kfd_ioctl_create_queue_args *args = data; 278 1.1 riastrad struct kfd_dev *dev; 279 1.1 riastrad int err = 0; 280 1.1 riastrad unsigned int queue_id; 281 1.1 riastrad struct kfd_process_device *pdd; 282 1.1 riastrad struct queue_properties q_properties; 283 1.3 riastrad uint32_t doorbell_offset_in_process = 0; 284 1.1 riastrad 285 1.1 riastrad memset(&q_properties, 0, sizeof(struct queue_properties)); 286 1.1 riastrad 287 1.3 riastrad pr_debug("Creating queue ioctl\n"); 288 1.1 riastrad 289 1.1 riastrad err = set_queue_properties_from_user(&q_properties, args); 290 1.1 riastrad if (err) 291 1.1 riastrad return err; 292 1.1 riastrad 293 1.3 riastrad pr_debug("Looking for gpu id 0x%x\n", args->gpu_id); 294 1.1 riastrad dev = kfd_device_by_id(args->gpu_id); 295 1.3 riastrad if (!dev) { 296 1.3 riastrad pr_debug("Could not find gpu id 0x%x\n", args->gpu_id); 297 1.1 riastrad return -EINVAL; 298 1.1 riastrad } 299 1.1 riastrad 300 1.1 riastrad mutex_lock(&p->mutex); 301 1.1 riastrad 302 1.1 riastrad pdd = kfd_bind_process_to_device(dev, p); 303 1.1 riastrad if (IS_ERR(pdd)) { 304 1.1 riastrad err = -ESRCH; 305 1.1 riastrad goto err_bind_process; 306 1.1 riastrad } 307 1.1 riastrad 308 1.3 riastrad pr_debug("Creating queue for PASID 0x%x on gpu 0x%x\n", 309 1.1 riastrad p->pasid, 310 1.1 riastrad dev->id); 311 1.1 riastrad 312 1.3 riastrad err = pqm_create_queue(&p->pqm, dev, filep, &q_properties, &queue_id, 313 1.3 riastrad &doorbell_offset_in_process); 314 1.1 riastrad if (err != 0) 315 1.1 riastrad goto err_create_queue; 316 1.1 riastrad 317 1.1 riastrad args->queue_id = queue_id; 318 1.1 riastrad 319 1.1 riastrad 320 1.1 riastrad /* Return gpu_id as doorbell offset for mmap usage */ 321 1.3 riastrad args->doorbell_offset = KFD_MMAP_TYPE_DOORBELL; 322 1.3 riastrad args->doorbell_offset |= KFD_MMAP_GPU_ID(args->gpu_id); 323 1.3 riastrad if (KFD_IS_SOC15(dev->device_info->asic_family)) 324 1.3 riastrad /* On SOC15 ASICs, include the doorbell offset within the 325 1.3 riastrad * process doorbell frame, which is 2 pages. 326 1.3 riastrad */ 327 1.3 riastrad args->doorbell_offset |= doorbell_offset_in_process; 328 1.1 riastrad 329 1.1 riastrad mutex_unlock(&p->mutex); 330 1.1 riastrad 331 1.3 riastrad pr_debug("Queue id %d was created successfully\n", args->queue_id); 332 1.1 riastrad 333 1.3 riastrad pr_debug("Ring buffer address == 0x%016llX\n", 334 1.1 riastrad args->ring_base_address); 335 1.1 riastrad 336 1.3 riastrad pr_debug("Read ptr address == 0x%016llX\n", 337 1.1 riastrad args->read_pointer_address); 338 1.1 riastrad 339 1.3 riastrad pr_debug("Write ptr address == 0x%016llX\n", 340 1.1 riastrad args->write_pointer_address); 341 1.1 riastrad 342 1.1 riastrad return 0; 343 1.1 riastrad 344 1.1 riastrad err_create_queue: 345 1.1 riastrad err_bind_process: 346 1.1 riastrad mutex_unlock(&p->mutex); 347 1.1 riastrad return err; 348 1.1 riastrad } 349 1.1 riastrad 350 1.1 riastrad static int kfd_ioctl_destroy_queue(struct file *filp, struct kfd_process *p, 351 1.1 riastrad void *data) 352 1.1 riastrad { 353 1.1 riastrad int retval; 354 1.1 riastrad struct kfd_ioctl_destroy_queue_args *args = data; 355 1.1 riastrad 356 1.3 riastrad pr_debug("Destroying queue id %d for pasid 0x%x\n", 357 1.1 riastrad args->queue_id, 358 1.1 riastrad p->pasid); 359 1.1 riastrad 360 1.1 riastrad mutex_lock(&p->mutex); 361 1.1 riastrad 362 1.1 riastrad retval = pqm_destroy_queue(&p->pqm, args->queue_id); 363 1.1 riastrad 364 1.1 riastrad mutex_unlock(&p->mutex); 365 1.1 riastrad return retval; 366 1.1 riastrad } 367 1.1 riastrad 368 1.1 riastrad static int kfd_ioctl_update_queue(struct file *filp, struct kfd_process *p, 369 1.1 riastrad void *data) 370 1.1 riastrad { 371 1.1 riastrad int retval; 372 1.1 riastrad struct kfd_ioctl_update_queue_args *args = data; 373 1.1 riastrad struct queue_properties properties; 374 1.1 riastrad 375 1.1 riastrad if (args->queue_percentage > KFD_MAX_QUEUE_PERCENTAGE) { 376 1.3 riastrad pr_err("Queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n"); 377 1.1 riastrad return -EINVAL; 378 1.1 riastrad } 379 1.1 riastrad 380 1.1 riastrad if (args->queue_priority > KFD_MAX_QUEUE_PRIORITY) { 381 1.3 riastrad pr_err("Queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n"); 382 1.1 riastrad return -EINVAL; 383 1.1 riastrad } 384 1.1 riastrad 385 1.1 riastrad if ((args->ring_base_address) && 386 1.3 riastrad (!access_ok((const void __user *) args->ring_base_address, 387 1.1 riastrad sizeof(uint64_t)))) { 388 1.3 riastrad pr_err("Can't access ring base address\n"); 389 1.1 riastrad return -EFAULT; 390 1.1 riastrad } 391 1.1 riastrad 392 1.1 riastrad if (!is_power_of_2(args->ring_size) && (args->ring_size != 0)) { 393 1.3 riastrad pr_err("Ring size must be a power of 2 or 0\n"); 394 1.1 riastrad return -EINVAL; 395 1.1 riastrad } 396 1.1 riastrad 397 1.1 riastrad properties.queue_address = args->ring_base_address; 398 1.1 riastrad properties.queue_size = args->ring_size; 399 1.1 riastrad properties.queue_percent = args->queue_percentage; 400 1.1 riastrad properties.priority = args->queue_priority; 401 1.1 riastrad 402 1.3 riastrad pr_debug("Updating queue id %d for pasid 0x%x\n", 403 1.1 riastrad args->queue_id, p->pasid); 404 1.1 riastrad 405 1.1 riastrad mutex_lock(&p->mutex); 406 1.1 riastrad 407 1.1 riastrad retval = pqm_update_queue(&p->pqm, args->queue_id, &properties); 408 1.1 riastrad 409 1.1 riastrad mutex_unlock(&p->mutex); 410 1.1 riastrad 411 1.1 riastrad return retval; 412 1.1 riastrad } 413 1.1 riastrad 414 1.3 riastrad static int kfd_ioctl_set_cu_mask(struct file *filp, struct kfd_process *p, 415 1.3 riastrad void *data) 416 1.3 riastrad { 417 1.3 riastrad int retval; 418 1.3 riastrad const int max_num_cus = 1024; 419 1.3 riastrad struct kfd_ioctl_set_cu_mask_args *args = data; 420 1.3 riastrad struct queue_properties properties; 421 1.3 riastrad uint32_t __user *cu_mask_ptr = (uint32_t __user *)args->cu_mask_ptr; 422 1.3 riastrad size_t cu_mask_size = sizeof(uint32_t) * (args->num_cu_mask / 32); 423 1.3 riastrad 424 1.3 riastrad if ((args->num_cu_mask % 32) != 0) { 425 1.3 riastrad pr_debug("num_cu_mask 0x%x must be a multiple of 32", 426 1.3 riastrad args->num_cu_mask); 427 1.3 riastrad return -EINVAL; 428 1.3 riastrad } 429 1.3 riastrad 430 1.3 riastrad properties.cu_mask_count = args->num_cu_mask; 431 1.3 riastrad if (properties.cu_mask_count == 0) { 432 1.3 riastrad pr_debug("CU mask cannot be 0"); 433 1.3 riastrad return -EINVAL; 434 1.3 riastrad } 435 1.3 riastrad 436 1.3 riastrad /* To prevent an unreasonably large CU mask size, set an arbitrary 437 1.3 riastrad * limit of max_num_cus bits. We can then just drop any CU mask bits 438 1.3 riastrad * past max_num_cus bits and just use the first max_num_cus bits. 439 1.3 riastrad */ 440 1.3 riastrad if (properties.cu_mask_count > max_num_cus) { 441 1.3 riastrad pr_debug("CU mask cannot be greater than 1024 bits"); 442 1.3 riastrad properties.cu_mask_count = max_num_cus; 443 1.3 riastrad cu_mask_size = sizeof(uint32_t) * (max_num_cus/32); 444 1.3 riastrad } 445 1.3 riastrad 446 1.3 riastrad properties.cu_mask = kzalloc(cu_mask_size, GFP_KERNEL); 447 1.3 riastrad if (!properties.cu_mask) 448 1.3 riastrad return -ENOMEM; 449 1.3 riastrad 450 1.3 riastrad retval = copy_from_user(properties.cu_mask, cu_mask_ptr, cu_mask_size); 451 1.3 riastrad if (retval) { 452 1.3 riastrad pr_debug("Could not copy CU mask from userspace"); 453 1.3 riastrad kfree(properties.cu_mask); 454 1.3 riastrad return -EFAULT; 455 1.3 riastrad } 456 1.3 riastrad 457 1.3 riastrad mutex_lock(&p->mutex); 458 1.3 riastrad 459 1.3 riastrad retval = pqm_set_cu_mask(&p->pqm, args->queue_id, &properties); 460 1.3 riastrad 461 1.3 riastrad mutex_unlock(&p->mutex); 462 1.3 riastrad 463 1.3 riastrad if (retval) 464 1.3 riastrad kfree(properties.cu_mask); 465 1.3 riastrad 466 1.3 riastrad return retval; 467 1.3 riastrad } 468 1.3 riastrad 469 1.3 riastrad static int kfd_ioctl_get_queue_wave_state(struct file *filep, 470 1.3 riastrad struct kfd_process *p, void *data) 471 1.3 riastrad { 472 1.3 riastrad struct kfd_ioctl_get_queue_wave_state_args *args = data; 473 1.3 riastrad int r; 474 1.3 riastrad 475 1.3 riastrad mutex_lock(&p->mutex); 476 1.3 riastrad 477 1.3 riastrad r = pqm_get_wave_state(&p->pqm, args->queue_id, 478 1.3 riastrad (void __user *)args->ctl_stack_address, 479 1.3 riastrad &args->ctl_stack_used_size, 480 1.3 riastrad &args->save_area_used_size); 481 1.3 riastrad 482 1.3 riastrad mutex_unlock(&p->mutex); 483 1.3 riastrad 484 1.3 riastrad return r; 485 1.3 riastrad } 486 1.3 riastrad 487 1.1 riastrad static int kfd_ioctl_set_memory_policy(struct file *filep, 488 1.1 riastrad struct kfd_process *p, void *data) 489 1.1 riastrad { 490 1.1 riastrad struct kfd_ioctl_set_memory_policy_args *args = data; 491 1.1 riastrad struct kfd_dev *dev; 492 1.1 riastrad int err = 0; 493 1.1 riastrad struct kfd_process_device *pdd; 494 1.1 riastrad enum cache_policy default_policy, alternate_policy; 495 1.1 riastrad 496 1.1 riastrad if (args->default_policy != KFD_IOC_CACHE_POLICY_COHERENT 497 1.1 riastrad && args->default_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) { 498 1.1 riastrad return -EINVAL; 499 1.1 riastrad } 500 1.1 riastrad 501 1.1 riastrad if (args->alternate_policy != KFD_IOC_CACHE_POLICY_COHERENT 502 1.1 riastrad && args->alternate_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) { 503 1.1 riastrad return -EINVAL; 504 1.1 riastrad } 505 1.1 riastrad 506 1.1 riastrad dev = kfd_device_by_id(args->gpu_id); 507 1.3 riastrad if (!dev) 508 1.1 riastrad return -EINVAL; 509 1.1 riastrad 510 1.1 riastrad mutex_lock(&p->mutex); 511 1.1 riastrad 512 1.1 riastrad pdd = kfd_bind_process_to_device(dev, p); 513 1.1 riastrad if (IS_ERR(pdd)) { 514 1.1 riastrad err = -ESRCH; 515 1.1 riastrad goto out; 516 1.1 riastrad } 517 1.1 riastrad 518 1.1 riastrad default_policy = (args->default_policy == KFD_IOC_CACHE_POLICY_COHERENT) 519 1.1 riastrad ? cache_policy_coherent : cache_policy_noncoherent; 520 1.1 riastrad 521 1.1 riastrad alternate_policy = 522 1.1 riastrad (args->alternate_policy == KFD_IOC_CACHE_POLICY_COHERENT) 523 1.1 riastrad ? cache_policy_coherent : cache_policy_noncoherent; 524 1.1 riastrad 525 1.1 riastrad if (!dev->dqm->ops.set_cache_memory_policy(dev->dqm, 526 1.1 riastrad &pdd->qpd, 527 1.1 riastrad default_policy, 528 1.1 riastrad alternate_policy, 529 1.1 riastrad (void __user *)args->alternate_aperture_base, 530 1.1 riastrad args->alternate_aperture_size)) 531 1.1 riastrad err = -EINVAL; 532 1.1 riastrad 533 1.1 riastrad out: 534 1.1 riastrad mutex_unlock(&p->mutex); 535 1.1 riastrad 536 1.1 riastrad return err; 537 1.1 riastrad } 538 1.1 riastrad 539 1.3 riastrad static int kfd_ioctl_set_trap_handler(struct file *filep, 540 1.3 riastrad struct kfd_process *p, void *data) 541 1.3 riastrad { 542 1.3 riastrad struct kfd_ioctl_set_trap_handler_args *args = data; 543 1.3 riastrad struct kfd_dev *dev; 544 1.3 riastrad int err = 0; 545 1.3 riastrad struct kfd_process_device *pdd; 546 1.3 riastrad 547 1.3 riastrad dev = kfd_device_by_id(args->gpu_id); 548 1.3 riastrad if (!dev) 549 1.3 riastrad return -EINVAL; 550 1.3 riastrad 551 1.3 riastrad mutex_lock(&p->mutex); 552 1.3 riastrad 553 1.3 riastrad pdd = kfd_bind_process_to_device(dev, p); 554 1.3 riastrad if (IS_ERR(pdd)) { 555 1.3 riastrad err = -ESRCH; 556 1.3 riastrad goto out; 557 1.3 riastrad } 558 1.3 riastrad 559 1.3 riastrad if (dev->dqm->ops.set_trap_handler(dev->dqm, 560 1.3 riastrad &pdd->qpd, 561 1.3 riastrad args->tba_addr, 562 1.3 riastrad args->tma_addr)) 563 1.3 riastrad err = -EINVAL; 564 1.3 riastrad 565 1.3 riastrad out: 566 1.3 riastrad mutex_unlock(&p->mutex); 567 1.3 riastrad 568 1.3 riastrad return err; 569 1.3 riastrad } 570 1.3 riastrad 571 1.1 riastrad static int kfd_ioctl_dbg_register(struct file *filep, 572 1.1 riastrad struct kfd_process *p, void *data) 573 1.1 riastrad { 574 1.1 riastrad struct kfd_ioctl_dbg_register_args *args = data; 575 1.1 riastrad struct kfd_dev *dev; 576 1.1 riastrad struct kfd_dbgmgr *dbgmgr_ptr; 577 1.1 riastrad struct kfd_process_device *pdd; 578 1.1 riastrad bool create_ok; 579 1.1 riastrad long status = 0; 580 1.1 riastrad 581 1.1 riastrad dev = kfd_device_by_id(args->gpu_id); 582 1.3 riastrad if (!dev) 583 1.1 riastrad return -EINVAL; 584 1.1 riastrad 585 1.1 riastrad if (dev->device_info->asic_family == CHIP_CARRIZO) { 586 1.1 riastrad pr_debug("kfd_ioctl_dbg_register not supported on CZ\n"); 587 1.1 riastrad return -EINVAL; 588 1.1 riastrad } 589 1.1 riastrad 590 1.3 riastrad mutex_lock(&p->mutex); 591 1.1 riastrad mutex_lock(kfd_get_dbgmgr_mutex()); 592 1.1 riastrad 593 1.1 riastrad /* 594 1.1 riastrad * make sure that we have pdd, if this the first queue created for 595 1.1 riastrad * this process 596 1.1 riastrad */ 597 1.1 riastrad pdd = kfd_bind_process_to_device(dev, p); 598 1.1 riastrad if (IS_ERR(pdd)) { 599 1.3 riastrad status = PTR_ERR(pdd); 600 1.3 riastrad goto out; 601 1.1 riastrad } 602 1.1 riastrad 603 1.3 riastrad if (!dev->dbgmgr) { 604 1.1 riastrad /* In case of a legal call, we have no dbgmgr yet */ 605 1.1 riastrad create_ok = kfd_dbgmgr_create(&dbgmgr_ptr, dev); 606 1.1 riastrad if (create_ok) { 607 1.1 riastrad status = kfd_dbgmgr_register(dbgmgr_ptr, p); 608 1.1 riastrad if (status != 0) 609 1.1 riastrad kfd_dbgmgr_destroy(dbgmgr_ptr); 610 1.1 riastrad else 611 1.1 riastrad dev->dbgmgr = dbgmgr_ptr; 612 1.1 riastrad } 613 1.1 riastrad } else { 614 1.1 riastrad pr_debug("debugger already registered\n"); 615 1.1 riastrad status = -EINVAL; 616 1.1 riastrad } 617 1.1 riastrad 618 1.3 riastrad out: 619 1.3 riastrad mutex_unlock(kfd_get_dbgmgr_mutex()); 620 1.1 riastrad mutex_unlock(&p->mutex); 621 1.1 riastrad 622 1.1 riastrad return status; 623 1.1 riastrad } 624 1.1 riastrad 625 1.3 riastrad static int kfd_ioctl_dbg_unregister(struct file *filep, 626 1.1 riastrad struct kfd_process *p, void *data) 627 1.1 riastrad { 628 1.1 riastrad struct kfd_ioctl_dbg_unregister_args *args = data; 629 1.1 riastrad struct kfd_dev *dev; 630 1.1 riastrad long status; 631 1.1 riastrad 632 1.1 riastrad dev = kfd_device_by_id(args->gpu_id); 633 1.3 riastrad if (!dev || !dev->dbgmgr) 634 1.1 riastrad return -EINVAL; 635 1.1 riastrad 636 1.1 riastrad if (dev->device_info->asic_family == CHIP_CARRIZO) { 637 1.3 riastrad pr_debug("kfd_ioctl_dbg_unregister not supported on CZ\n"); 638 1.1 riastrad return -EINVAL; 639 1.1 riastrad } 640 1.1 riastrad 641 1.1 riastrad mutex_lock(kfd_get_dbgmgr_mutex()); 642 1.1 riastrad 643 1.1 riastrad status = kfd_dbgmgr_unregister(dev->dbgmgr, p); 644 1.3 riastrad if (!status) { 645 1.1 riastrad kfd_dbgmgr_destroy(dev->dbgmgr); 646 1.1 riastrad dev->dbgmgr = NULL; 647 1.1 riastrad } 648 1.1 riastrad 649 1.1 riastrad mutex_unlock(kfd_get_dbgmgr_mutex()); 650 1.1 riastrad 651 1.1 riastrad return status; 652 1.1 riastrad } 653 1.1 riastrad 654 1.1 riastrad /* 655 1.1 riastrad * Parse and generate variable size data structure for address watch. 656 1.1 riastrad * Total size of the buffer and # watch points is limited in order 657 1.1 riastrad * to prevent kernel abuse. (no bearing to the much smaller HW limitation 658 1.1 riastrad * which is enforced by dbgdev module) 659 1.1 riastrad * please also note that the watch address itself are not "copied from user", 660 1.1 riastrad * since it be set into the HW in user mode values. 661 1.1 riastrad * 662 1.1 riastrad */ 663 1.1 riastrad static int kfd_ioctl_dbg_address_watch(struct file *filep, 664 1.1 riastrad struct kfd_process *p, void *data) 665 1.1 riastrad { 666 1.1 riastrad struct kfd_ioctl_dbg_address_watch_args *args = data; 667 1.1 riastrad struct kfd_dev *dev; 668 1.1 riastrad struct dbg_address_watch_info aw_info; 669 1.1 riastrad unsigned char *args_buff; 670 1.1 riastrad long status; 671 1.1 riastrad void __user *cmd_from_user; 672 1.1 riastrad uint64_t watch_mask_value = 0; 673 1.1 riastrad unsigned int args_idx = 0; 674 1.1 riastrad 675 1.1 riastrad memset((void *) &aw_info, 0, sizeof(struct dbg_address_watch_info)); 676 1.1 riastrad 677 1.1 riastrad dev = kfd_device_by_id(args->gpu_id); 678 1.3 riastrad if (!dev) 679 1.1 riastrad return -EINVAL; 680 1.1 riastrad 681 1.1 riastrad if (dev->device_info->asic_family == CHIP_CARRIZO) { 682 1.1 riastrad pr_debug("kfd_ioctl_dbg_wave_control not supported on CZ\n"); 683 1.1 riastrad return -EINVAL; 684 1.1 riastrad } 685 1.1 riastrad 686 1.1 riastrad cmd_from_user = (void __user *) args->content_ptr; 687 1.1 riastrad 688 1.1 riastrad /* Validate arguments */ 689 1.1 riastrad 690 1.1 riastrad if ((args->buf_size_in_bytes > MAX_ALLOWED_AW_BUFF_SIZE) || 691 1.1 riastrad (args->buf_size_in_bytes <= sizeof(*args) + sizeof(int) * 2) || 692 1.1 riastrad (cmd_from_user == NULL)) 693 1.1 riastrad return -EINVAL; 694 1.1 riastrad 695 1.1 riastrad /* this is the actual buffer to work with */ 696 1.3 riastrad args_buff = memdup_user(cmd_from_user, 697 1.1 riastrad args->buf_size_in_bytes - sizeof(*args)); 698 1.3 riastrad if (IS_ERR(args_buff)) 699 1.3 riastrad return PTR_ERR(args_buff); 700 1.1 riastrad 701 1.1 riastrad aw_info.process = p; 702 1.1 riastrad 703 1.1 riastrad aw_info.num_watch_points = *((uint32_t *)(&args_buff[args_idx])); 704 1.1 riastrad args_idx += sizeof(aw_info.num_watch_points); 705 1.1 riastrad 706 1.1 riastrad aw_info.watch_mode = (enum HSA_DBG_WATCH_MODE *) &args_buff[args_idx]; 707 1.1 riastrad args_idx += sizeof(enum HSA_DBG_WATCH_MODE) * aw_info.num_watch_points; 708 1.1 riastrad 709 1.1 riastrad /* 710 1.1 riastrad * set watch address base pointer to point on the array base 711 1.1 riastrad * within args_buff 712 1.1 riastrad */ 713 1.1 riastrad aw_info.watch_address = (uint64_t *) &args_buff[args_idx]; 714 1.1 riastrad 715 1.1 riastrad /* skip over the addresses buffer */ 716 1.1 riastrad args_idx += sizeof(aw_info.watch_address) * aw_info.num_watch_points; 717 1.1 riastrad 718 1.1 riastrad if (args_idx >= args->buf_size_in_bytes - sizeof(*args)) { 719 1.3 riastrad status = -EINVAL; 720 1.3 riastrad goto out; 721 1.1 riastrad } 722 1.1 riastrad 723 1.1 riastrad watch_mask_value = (uint64_t) args_buff[args_idx]; 724 1.1 riastrad 725 1.1 riastrad if (watch_mask_value > 0) { 726 1.1 riastrad /* 727 1.1 riastrad * There is an array of masks. 728 1.1 riastrad * set watch mask base pointer to point on the array base 729 1.1 riastrad * within args_buff 730 1.1 riastrad */ 731 1.1 riastrad aw_info.watch_mask = (uint64_t *) &args_buff[args_idx]; 732 1.1 riastrad 733 1.1 riastrad /* skip over the masks buffer */ 734 1.1 riastrad args_idx += sizeof(aw_info.watch_mask) * 735 1.1 riastrad aw_info.num_watch_points; 736 1.1 riastrad } else { 737 1.1 riastrad /* just the NULL mask, set to NULL and skip over it */ 738 1.1 riastrad aw_info.watch_mask = NULL; 739 1.1 riastrad args_idx += sizeof(aw_info.watch_mask); 740 1.1 riastrad } 741 1.1 riastrad 742 1.1 riastrad if (args_idx >= args->buf_size_in_bytes - sizeof(args)) { 743 1.3 riastrad status = -EINVAL; 744 1.3 riastrad goto out; 745 1.1 riastrad } 746 1.1 riastrad 747 1.1 riastrad /* Currently HSA Event is not supported for DBG */ 748 1.1 riastrad aw_info.watch_event = NULL; 749 1.1 riastrad 750 1.1 riastrad mutex_lock(kfd_get_dbgmgr_mutex()); 751 1.1 riastrad 752 1.1 riastrad status = kfd_dbgmgr_address_watch(dev->dbgmgr, &aw_info); 753 1.1 riastrad 754 1.1 riastrad mutex_unlock(kfd_get_dbgmgr_mutex()); 755 1.1 riastrad 756 1.3 riastrad out: 757 1.1 riastrad kfree(args_buff); 758 1.1 riastrad 759 1.1 riastrad return status; 760 1.1 riastrad } 761 1.1 riastrad 762 1.1 riastrad /* Parse and generate fixed size data structure for wave control */ 763 1.1 riastrad static int kfd_ioctl_dbg_wave_control(struct file *filep, 764 1.1 riastrad struct kfd_process *p, void *data) 765 1.1 riastrad { 766 1.1 riastrad struct kfd_ioctl_dbg_wave_control_args *args = data; 767 1.1 riastrad struct kfd_dev *dev; 768 1.1 riastrad struct dbg_wave_control_info wac_info; 769 1.1 riastrad unsigned char *args_buff; 770 1.1 riastrad uint32_t computed_buff_size; 771 1.1 riastrad long status; 772 1.1 riastrad void __user *cmd_from_user; 773 1.1 riastrad unsigned int args_idx = 0; 774 1.1 riastrad 775 1.1 riastrad memset((void *) &wac_info, 0, sizeof(struct dbg_wave_control_info)); 776 1.1 riastrad 777 1.1 riastrad /* we use compact form, independent of the packing attribute value */ 778 1.1 riastrad computed_buff_size = sizeof(*args) + 779 1.1 riastrad sizeof(wac_info.mode) + 780 1.1 riastrad sizeof(wac_info.operand) + 781 1.1 riastrad sizeof(wac_info.dbgWave_msg.DbgWaveMsg) + 782 1.1 riastrad sizeof(wac_info.dbgWave_msg.MemoryVA) + 783 1.1 riastrad sizeof(wac_info.trapId); 784 1.1 riastrad 785 1.1 riastrad dev = kfd_device_by_id(args->gpu_id); 786 1.3 riastrad if (!dev) 787 1.1 riastrad return -EINVAL; 788 1.1 riastrad 789 1.1 riastrad if (dev->device_info->asic_family == CHIP_CARRIZO) { 790 1.1 riastrad pr_debug("kfd_ioctl_dbg_wave_control not supported on CZ\n"); 791 1.1 riastrad return -EINVAL; 792 1.1 riastrad } 793 1.1 riastrad 794 1.1 riastrad /* input size must match the computed "compact" size */ 795 1.1 riastrad if (args->buf_size_in_bytes != computed_buff_size) { 796 1.1 riastrad pr_debug("size mismatch, computed : actual %u : %u\n", 797 1.1 riastrad args->buf_size_in_bytes, computed_buff_size); 798 1.1 riastrad return -EINVAL; 799 1.1 riastrad } 800 1.1 riastrad 801 1.1 riastrad cmd_from_user = (void __user *) args->content_ptr; 802 1.1 riastrad 803 1.1 riastrad if (cmd_from_user == NULL) 804 1.1 riastrad return -EINVAL; 805 1.1 riastrad 806 1.3 riastrad /* copy the entire buffer from user */ 807 1.1 riastrad 808 1.3 riastrad args_buff = memdup_user(cmd_from_user, 809 1.1 riastrad args->buf_size_in_bytes - sizeof(*args)); 810 1.3 riastrad if (IS_ERR(args_buff)) 811 1.3 riastrad return PTR_ERR(args_buff); 812 1.1 riastrad 813 1.1 riastrad /* move ptr to the start of the "pay-load" area */ 814 1.1 riastrad wac_info.process = p; 815 1.1 riastrad 816 1.1 riastrad wac_info.operand = *((enum HSA_DBG_WAVEOP *)(&args_buff[args_idx])); 817 1.1 riastrad args_idx += sizeof(wac_info.operand); 818 1.1 riastrad 819 1.1 riastrad wac_info.mode = *((enum HSA_DBG_WAVEMODE *)(&args_buff[args_idx])); 820 1.1 riastrad args_idx += sizeof(wac_info.mode); 821 1.1 riastrad 822 1.1 riastrad wac_info.trapId = *((uint32_t *)(&args_buff[args_idx])); 823 1.1 riastrad args_idx += sizeof(wac_info.trapId); 824 1.1 riastrad 825 1.1 riastrad wac_info.dbgWave_msg.DbgWaveMsg.WaveMsgInfoGen2.Value = 826 1.1 riastrad *((uint32_t *)(&args_buff[args_idx])); 827 1.1 riastrad wac_info.dbgWave_msg.MemoryVA = NULL; 828 1.1 riastrad 829 1.1 riastrad mutex_lock(kfd_get_dbgmgr_mutex()); 830 1.1 riastrad 831 1.1 riastrad pr_debug("Calling dbg manager process %p, operand %u, mode %u, trapId %u, message %u\n", 832 1.1 riastrad wac_info.process, wac_info.operand, 833 1.1 riastrad wac_info.mode, wac_info.trapId, 834 1.1 riastrad wac_info.dbgWave_msg.DbgWaveMsg.WaveMsgInfoGen2.Value); 835 1.1 riastrad 836 1.1 riastrad status = kfd_dbgmgr_wave_control(dev->dbgmgr, &wac_info); 837 1.1 riastrad 838 1.1 riastrad pr_debug("Returned status of dbg manager is %ld\n", status); 839 1.1 riastrad 840 1.1 riastrad mutex_unlock(kfd_get_dbgmgr_mutex()); 841 1.1 riastrad 842 1.1 riastrad kfree(args_buff); 843 1.1 riastrad 844 1.1 riastrad return status; 845 1.1 riastrad } 846 1.1 riastrad 847 1.1 riastrad static int kfd_ioctl_get_clock_counters(struct file *filep, 848 1.1 riastrad struct kfd_process *p, void *data) 849 1.1 riastrad { 850 1.1 riastrad struct kfd_ioctl_get_clock_counters_args *args = data; 851 1.1 riastrad struct kfd_dev *dev; 852 1.1 riastrad 853 1.1 riastrad dev = kfd_device_by_id(args->gpu_id); 854 1.3 riastrad if (dev) 855 1.3 riastrad /* Reading GPU clock counter from KGD */ 856 1.3 riastrad args->gpu_clock_counter = amdgpu_amdkfd_get_gpu_clock_counter(dev->kgd); 857 1.3 riastrad else 858 1.3 riastrad /* Node without GPU resource */ 859 1.3 riastrad args->gpu_clock_counter = 0; 860 1.1 riastrad 861 1.1 riastrad /* No access to rdtsc. Using raw monotonic time */ 862 1.3 riastrad args->cpu_clock_counter = ktime_get_raw_ns(); 863 1.3 riastrad args->system_clock_counter = ktime_get_boottime_ns(); 864 1.1 riastrad 865 1.1 riastrad /* Since the counter is in nano-seconds we use 1GHz frequency */ 866 1.1 riastrad args->system_clock_freq = 1000000000; 867 1.1 riastrad 868 1.1 riastrad return 0; 869 1.1 riastrad } 870 1.1 riastrad 871 1.1 riastrad 872 1.1 riastrad static int kfd_ioctl_get_process_apertures(struct file *filp, 873 1.1 riastrad struct kfd_process *p, void *data) 874 1.1 riastrad { 875 1.1 riastrad struct kfd_ioctl_get_process_apertures_args *args = data; 876 1.1 riastrad struct kfd_process_device_apertures *pAperture; 877 1.1 riastrad struct kfd_process_device *pdd; 878 1.1 riastrad 879 1.3 riastrad dev_dbg(kfd_device, "get apertures for PASID 0x%x", p->pasid); 880 1.1 riastrad 881 1.1 riastrad args->num_of_nodes = 0; 882 1.1 riastrad 883 1.1 riastrad mutex_lock(&p->mutex); 884 1.1 riastrad 885 1.1 riastrad /*if the process-device list isn't empty*/ 886 1.1 riastrad if (kfd_has_process_device_data(p)) { 887 1.1 riastrad /* Run over all pdd of the process */ 888 1.1 riastrad pdd = kfd_get_first_process_device_data(p); 889 1.1 riastrad do { 890 1.1 riastrad pAperture = 891 1.1 riastrad &args->process_apertures[args->num_of_nodes]; 892 1.1 riastrad pAperture->gpu_id = pdd->dev->id; 893 1.1 riastrad pAperture->lds_base = pdd->lds_base; 894 1.1 riastrad pAperture->lds_limit = pdd->lds_limit; 895 1.1 riastrad pAperture->gpuvm_base = pdd->gpuvm_base; 896 1.1 riastrad pAperture->gpuvm_limit = pdd->gpuvm_limit; 897 1.1 riastrad pAperture->scratch_base = pdd->scratch_base; 898 1.1 riastrad pAperture->scratch_limit = pdd->scratch_limit; 899 1.1 riastrad 900 1.1 riastrad dev_dbg(kfd_device, 901 1.1 riastrad "node id %u\n", args->num_of_nodes); 902 1.1 riastrad dev_dbg(kfd_device, 903 1.1 riastrad "gpu id %u\n", pdd->dev->id); 904 1.1 riastrad dev_dbg(kfd_device, 905 1.1 riastrad "lds_base %llX\n", pdd->lds_base); 906 1.1 riastrad dev_dbg(kfd_device, 907 1.1 riastrad "lds_limit %llX\n", pdd->lds_limit); 908 1.1 riastrad dev_dbg(kfd_device, 909 1.1 riastrad "gpuvm_base %llX\n", pdd->gpuvm_base); 910 1.1 riastrad dev_dbg(kfd_device, 911 1.1 riastrad "gpuvm_limit %llX\n", pdd->gpuvm_limit); 912 1.1 riastrad dev_dbg(kfd_device, 913 1.1 riastrad "scratch_base %llX\n", pdd->scratch_base); 914 1.1 riastrad dev_dbg(kfd_device, 915 1.1 riastrad "scratch_limit %llX\n", pdd->scratch_limit); 916 1.1 riastrad 917 1.1 riastrad args->num_of_nodes++; 918 1.3 riastrad 919 1.3 riastrad pdd = kfd_get_next_process_device_data(p, pdd); 920 1.3 riastrad } while (pdd && (args->num_of_nodes < NUM_OF_SUPPORTED_GPUS)); 921 1.3 riastrad } 922 1.3 riastrad 923 1.3 riastrad mutex_unlock(&p->mutex); 924 1.3 riastrad 925 1.3 riastrad return 0; 926 1.3 riastrad } 927 1.3 riastrad 928 1.3 riastrad static int kfd_ioctl_get_process_apertures_new(struct file *filp, 929 1.3 riastrad struct kfd_process *p, void *data) 930 1.3 riastrad { 931 1.3 riastrad struct kfd_ioctl_get_process_apertures_new_args *args = data; 932 1.3 riastrad struct kfd_process_device_apertures *pa; 933 1.3 riastrad struct kfd_process_device *pdd; 934 1.3 riastrad uint32_t nodes = 0; 935 1.3 riastrad int ret; 936 1.3 riastrad 937 1.3 riastrad dev_dbg(kfd_device, "get apertures for PASID 0x%x", p->pasid); 938 1.3 riastrad 939 1.3 riastrad if (args->num_of_nodes == 0) { 940 1.3 riastrad /* Return number of nodes, so that user space can alloacate 941 1.3 riastrad * sufficient memory 942 1.3 riastrad */ 943 1.3 riastrad mutex_lock(&p->mutex); 944 1.3 riastrad 945 1.3 riastrad if (!kfd_has_process_device_data(p)) 946 1.3 riastrad goto out_unlock; 947 1.3 riastrad 948 1.3 riastrad /* Run over all pdd of the process */ 949 1.3 riastrad pdd = kfd_get_first_process_device_data(p); 950 1.3 riastrad do { 951 1.3 riastrad args->num_of_nodes++; 952 1.3 riastrad pdd = kfd_get_next_process_device_data(p, pdd); 953 1.3 riastrad } while (pdd); 954 1.3 riastrad 955 1.3 riastrad goto out_unlock; 956 1.1 riastrad } 957 1.1 riastrad 958 1.3 riastrad /* Fill in process-aperture information for all available 959 1.3 riastrad * nodes, but not more than args->num_of_nodes as that is 960 1.3 riastrad * the amount of memory allocated by user 961 1.3 riastrad */ 962 1.3 riastrad pa = kzalloc((sizeof(struct kfd_process_device_apertures) * 963 1.3 riastrad args->num_of_nodes), GFP_KERNEL); 964 1.3 riastrad if (!pa) 965 1.3 riastrad return -ENOMEM; 966 1.3 riastrad 967 1.3 riastrad mutex_lock(&p->mutex); 968 1.3 riastrad 969 1.3 riastrad if (!kfd_has_process_device_data(p)) { 970 1.3 riastrad args->num_of_nodes = 0; 971 1.3 riastrad kfree(pa); 972 1.3 riastrad goto out_unlock; 973 1.3 riastrad } 974 1.3 riastrad 975 1.3 riastrad /* Run over all pdd of the process */ 976 1.3 riastrad pdd = kfd_get_first_process_device_data(p); 977 1.3 riastrad do { 978 1.3 riastrad pa[nodes].gpu_id = pdd->dev->id; 979 1.3 riastrad pa[nodes].lds_base = pdd->lds_base; 980 1.3 riastrad pa[nodes].lds_limit = pdd->lds_limit; 981 1.3 riastrad pa[nodes].gpuvm_base = pdd->gpuvm_base; 982 1.3 riastrad pa[nodes].gpuvm_limit = pdd->gpuvm_limit; 983 1.3 riastrad pa[nodes].scratch_base = pdd->scratch_base; 984 1.3 riastrad pa[nodes].scratch_limit = pdd->scratch_limit; 985 1.3 riastrad 986 1.3 riastrad dev_dbg(kfd_device, 987 1.3 riastrad "gpu id %u\n", pdd->dev->id); 988 1.3 riastrad dev_dbg(kfd_device, 989 1.3 riastrad "lds_base %llX\n", pdd->lds_base); 990 1.3 riastrad dev_dbg(kfd_device, 991 1.3 riastrad "lds_limit %llX\n", pdd->lds_limit); 992 1.3 riastrad dev_dbg(kfd_device, 993 1.3 riastrad "gpuvm_base %llX\n", pdd->gpuvm_base); 994 1.3 riastrad dev_dbg(kfd_device, 995 1.3 riastrad "gpuvm_limit %llX\n", pdd->gpuvm_limit); 996 1.3 riastrad dev_dbg(kfd_device, 997 1.3 riastrad "scratch_base %llX\n", pdd->scratch_base); 998 1.3 riastrad dev_dbg(kfd_device, 999 1.3 riastrad "scratch_limit %llX\n", pdd->scratch_limit); 1000 1.3 riastrad nodes++; 1001 1.3 riastrad 1002 1.3 riastrad pdd = kfd_get_next_process_device_data(p, pdd); 1003 1.3 riastrad } while (pdd && (nodes < args->num_of_nodes)); 1004 1.1 riastrad mutex_unlock(&p->mutex); 1005 1.1 riastrad 1006 1.3 riastrad args->num_of_nodes = nodes; 1007 1.3 riastrad ret = copy_to_user( 1008 1.3 riastrad (void __user *)args->kfd_process_device_apertures_ptr, 1009 1.3 riastrad pa, 1010 1.3 riastrad (nodes * sizeof(struct kfd_process_device_apertures))); 1011 1.3 riastrad kfree(pa); 1012 1.3 riastrad return ret ? -EFAULT : 0; 1013 1.3 riastrad 1014 1.3 riastrad out_unlock: 1015 1.3 riastrad mutex_unlock(&p->mutex); 1016 1.1 riastrad return 0; 1017 1.1 riastrad } 1018 1.1 riastrad 1019 1.1 riastrad static int kfd_ioctl_create_event(struct file *filp, struct kfd_process *p, 1020 1.1 riastrad void *data) 1021 1.1 riastrad { 1022 1.1 riastrad struct kfd_ioctl_create_event_args *args = data; 1023 1.1 riastrad int err; 1024 1.1 riastrad 1025 1.3 riastrad /* For dGPUs the event page is allocated in user mode. The 1026 1.3 riastrad * handle is passed to KFD with the first call to this IOCTL 1027 1.3 riastrad * through the event_page_offset field. 1028 1.3 riastrad */ 1029 1.3 riastrad if (args->event_page_offset) { 1030 1.3 riastrad struct kfd_dev *kfd; 1031 1.3 riastrad struct kfd_process_device *pdd; 1032 1.3 riastrad void *mem, *kern_addr; 1033 1.3 riastrad uint64_t size; 1034 1.3 riastrad 1035 1.3 riastrad if (p->signal_page) { 1036 1.3 riastrad pr_err("Event page is already set\n"); 1037 1.3 riastrad return -EINVAL; 1038 1.3 riastrad } 1039 1.3 riastrad 1040 1.3 riastrad kfd = kfd_device_by_id(GET_GPU_ID(args->event_page_offset)); 1041 1.3 riastrad if (!kfd) { 1042 1.3 riastrad pr_err("Getting device by id failed in %s\n", __func__); 1043 1.3 riastrad return -EINVAL; 1044 1.3 riastrad } 1045 1.3 riastrad 1046 1.3 riastrad mutex_lock(&p->mutex); 1047 1.3 riastrad pdd = kfd_bind_process_to_device(kfd, p); 1048 1.3 riastrad if (IS_ERR(pdd)) { 1049 1.3 riastrad err = PTR_ERR(pdd); 1050 1.3 riastrad goto out_unlock; 1051 1.3 riastrad } 1052 1.3 riastrad 1053 1.3 riastrad mem = kfd_process_device_translate_handle(pdd, 1054 1.3 riastrad GET_IDR_HANDLE(args->event_page_offset)); 1055 1.3 riastrad if (!mem) { 1056 1.3 riastrad pr_err("Can't find BO, offset is 0x%llx\n", 1057 1.3 riastrad args->event_page_offset); 1058 1.3 riastrad err = -EINVAL; 1059 1.3 riastrad goto out_unlock; 1060 1.3 riastrad } 1061 1.3 riastrad mutex_unlock(&p->mutex); 1062 1.3 riastrad 1063 1.3 riastrad err = amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(kfd->kgd, 1064 1.3 riastrad mem, &kern_addr, &size); 1065 1.3 riastrad if (err) { 1066 1.3 riastrad pr_err("Failed to map event page to kernel\n"); 1067 1.3 riastrad return err; 1068 1.3 riastrad } 1069 1.3 riastrad 1070 1.3 riastrad err = kfd_event_page_set(p, kern_addr, size); 1071 1.3 riastrad if (err) { 1072 1.3 riastrad pr_err("Failed to set event page\n"); 1073 1.3 riastrad return err; 1074 1.3 riastrad } 1075 1.3 riastrad } 1076 1.3 riastrad 1077 1.1 riastrad err = kfd_event_create(filp, p, args->event_type, 1078 1.1 riastrad args->auto_reset != 0, args->node_id, 1079 1.1 riastrad &args->event_id, &args->event_trigger_data, 1080 1.1 riastrad &args->event_page_offset, 1081 1.1 riastrad &args->event_slot_index); 1082 1.1 riastrad 1083 1.1 riastrad return err; 1084 1.3 riastrad 1085 1.3 riastrad out_unlock: 1086 1.3 riastrad mutex_unlock(&p->mutex); 1087 1.3 riastrad return err; 1088 1.1 riastrad } 1089 1.1 riastrad 1090 1.1 riastrad static int kfd_ioctl_destroy_event(struct file *filp, struct kfd_process *p, 1091 1.1 riastrad void *data) 1092 1.1 riastrad { 1093 1.1 riastrad struct kfd_ioctl_destroy_event_args *args = data; 1094 1.1 riastrad 1095 1.1 riastrad return kfd_event_destroy(p, args->event_id); 1096 1.1 riastrad } 1097 1.1 riastrad 1098 1.1 riastrad static int kfd_ioctl_set_event(struct file *filp, struct kfd_process *p, 1099 1.1 riastrad void *data) 1100 1.1 riastrad { 1101 1.1 riastrad struct kfd_ioctl_set_event_args *args = data; 1102 1.1 riastrad 1103 1.1 riastrad return kfd_set_event(p, args->event_id); 1104 1.1 riastrad } 1105 1.1 riastrad 1106 1.1 riastrad static int kfd_ioctl_reset_event(struct file *filp, struct kfd_process *p, 1107 1.1 riastrad void *data) 1108 1.1 riastrad { 1109 1.1 riastrad struct kfd_ioctl_reset_event_args *args = data; 1110 1.1 riastrad 1111 1.1 riastrad return kfd_reset_event(p, args->event_id); 1112 1.1 riastrad } 1113 1.1 riastrad 1114 1.1 riastrad static int kfd_ioctl_wait_events(struct file *filp, struct kfd_process *p, 1115 1.1 riastrad void *data) 1116 1.1 riastrad { 1117 1.1 riastrad struct kfd_ioctl_wait_events_args *args = data; 1118 1.1 riastrad int err; 1119 1.1 riastrad 1120 1.1 riastrad err = kfd_wait_on_events(p, args->num_events, 1121 1.1 riastrad (void __user *)args->events_ptr, 1122 1.1 riastrad (args->wait_for_all != 0), 1123 1.3 riastrad args->timeout, &args->wait_result); 1124 1.3 riastrad 1125 1.3 riastrad return err; 1126 1.3 riastrad } 1127 1.3 riastrad static int kfd_ioctl_set_scratch_backing_va(struct file *filep, 1128 1.3 riastrad struct kfd_process *p, void *data) 1129 1.3 riastrad { 1130 1.3 riastrad struct kfd_ioctl_set_scratch_backing_va_args *args = data; 1131 1.3 riastrad struct kfd_process_device *pdd; 1132 1.3 riastrad struct kfd_dev *dev; 1133 1.3 riastrad long err; 1134 1.3 riastrad 1135 1.3 riastrad dev = kfd_device_by_id(args->gpu_id); 1136 1.3 riastrad if (!dev) 1137 1.3 riastrad return -EINVAL; 1138 1.3 riastrad 1139 1.3 riastrad mutex_lock(&p->mutex); 1140 1.3 riastrad 1141 1.3 riastrad pdd = kfd_bind_process_to_device(dev, p); 1142 1.3 riastrad if (IS_ERR(pdd)) { 1143 1.3 riastrad err = PTR_ERR(pdd); 1144 1.3 riastrad goto bind_process_to_device_fail; 1145 1.3 riastrad } 1146 1.3 riastrad 1147 1.3 riastrad pdd->qpd.sh_hidden_private_base = args->va_addr; 1148 1.3 riastrad 1149 1.3 riastrad mutex_unlock(&p->mutex); 1150 1.3 riastrad 1151 1.3 riastrad if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS && 1152 1.3 riastrad pdd->qpd.vmid != 0 && dev->kfd2kgd->set_scratch_backing_va) 1153 1.3 riastrad dev->kfd2kgd->set_scratch_backing_va( 1154 1.3 riastrad dev->kgd, args->va_addr, pdd->qpd.vmid); 1155 1.1 riastrad 1156 1.3 riastrad return 0; 1157 1.1 riastrad 1158 1.3 riastrad bind_process_to_device_fail: 1159 1.3 riastrad mutex_unlock(&p->mutex); 1160 1.1 riastrad return err; 1161 1.1 riastrad } 1162 1.1 riastrad 1163 1.3 riastrad static int kfd_ioctl_get_tile_config(struct file *filep, 1164 1.3 riastrad struct kfd_process *p, void *data) 1165 1.3 riastrad { 1166 1.3 riastrad struct kfd_ioctl_get_tile_config_args *args = data; 1167 1.3 riastrad struct kfd_dev *dev; 1168 1.3 riastrad struct tile_config config; 1169 1.3 riastrad int err = 0; 1170 1.3 riastrad 1171 1.3 riastrad dev = kfd_device_by_id(args->gpu_id); 1172 1.3 riastrad if (!dev) 1173 1.3 riastrad return -EINVAL; 1174 1.3 riastrad 1175 1.3 riastrad dev->kfd2kgd->get_tile_config(dev->kgd, &config); 1176 1.3 riastrad 1177 1.3 riastrad args->gb_addr_config = config.gb_addr_config; 1178 1.3 riastrad args->num_banks = config.num_banks; 1179 1.3 riastrad args->num_ranks = config.num_ranks; 1180 1.3 riastrad 1181 1.3 riastrad if (args->num_tile_configs > config.num_tile_configs) 1182 1.3 riastrad args->num_tile_configs = config.num_tile_configs; 1183 1.3 riastrad err = copy_to_user((void __user *)args->tile_config_ptr, 1184 1.3 riastrad config.tile_config_ptr, 1185 1.3 riastrad args->num_tile_configs * sizeof(uint32_t)); 1186 1.3 riastrad if (err) { 1187 1.3 riastrad args->num_tile_configs = 0; 1188 1.3 riastrad return -EFAULT; 1189 1.3 riastrad } 1190 1.3 riastrad 1191 1.3 riastrad if (args->num_macro_tile_configs > config.num_macro_tile_configs) 1192 1.3 riastrad args->num_macro_tile_configs = 1193 1.3 riastrad config.num_macro_tile_configs; 1194 1.3 riastrad err = copy_to_user((void __user *)args->macro_tile_config_ptr, 1195 1.3 riastrad config.macro_tile_config_ptr, 1196 1.3 riastrad args->num_macro_tile_configs * sizeof(uint32_t)); 1197 1.3 riastrad if (err) { 1198 1.3 riastrad args->num_macro_tile_configs = 0; 1199 1.3 riastrad return -EFAULT; 1200 1.3 riastrad } 1201 1.3 riastrad 1202 1.3 riastrad return 0; 1203 1.3 riastrad } 1204 1.3 riastrad 1205 1.3 riastrad static int kfd_ioctl_acquire_vm(struct file *filep, struct kfd_process *p, 1206 1.3 riastrad void *data) 1207 1.3 riastrad { 1208 1.3 riastrad struct kfd_ioctl_acquire_vm_args *args = data; 1209 1.3 riastrad struct kfd_process_device *pdd; 1210 1.3 riastrad struct kfd_dev *dev; 1211 1.3 riastrad struct file *drm_file; 1212 1.3 riastrad int ret; 1213 1.3 riastrad 1214 1.3 riastrad dev = kfd_device_by_id(args->gpu_id); 1215 1.3 riastrad if (!dev) 1216 1.3 riastrad return -EINVAL; 1217 1.3 riastrad 1218 1.3 riastrad drm_file = fget(args->drm_fd); 1219 1.3 riastrad if (!drm_file) 1220 1.3 riastrad return -EINVAL; 1221 1.3 riastrad 1222 1.3 riastrad mutex_lock(&p->mutex); 1223 1.3 riastrad 1224 1.3 riastrad pdd = kfd_get_process_device_data(dev, p); 1225 1.3 riastrad if (!pdd) { 1226 1.3 riastrad ret = -EINVAL; 1227 1.3 riastrad goto err_unlock; 1228 1.3 riastrad } 1229 1.3 riastrad 1230 1.3 riastrad if (pdd->drm_file) { 1231 1.3 riastrad ret = pdd->drm_file == drm_file ? 0 : -EBUSY; 1232 1.3 riastrad goto err_unlock; 1233 1.3 riastrad } 1234 1.3 riastrad 1235 1.3 riastrad ret = kfd_process_device_init_vm(pdd, drm_file); 1236 1.3 riastrad if (ret) 1237 1.3 riastrad goto err_unlock; 1238 1.3 riastrad /* On success, the PDD keeps the drm_file reference */ 1239 1.3 riastrad mutex_unlock(&p->mutex); 1240 1.3 riastrad 1241 1.3 riastrad return 0; 1242 1.3 riastrad 1243 1.3 riastrad err_unlock: 1244 1.3 riastrad mutex_unlock(&p->mutex); 1245 1.3 riastrad fput(drm_file); 1246 1.3 riastrad return ret; 1247 1.3 riastrad } 1248 1.3 riastrad 1249 1.3 riastrad bool kfd_dev_is_large_bar(struct kfd_dev *dev) 1250 1.3 riastrad { 1251 1.3 riastrad struct kfd_local_mem_info mem_info; 1252 1.3 riastrad 1253 1.3 riastrad if (debug_largebar) { 1254 1.3 riastrad pr_debug("Simulate large-bar allocation on non large-bar machine\n"); 1255 1.3 riastrad return true; 1256 1.3 riastrad } 1257 1.3 riastrad 1258 1.3 riastrad if (dev->device_info->needs_iommu_device) 1259 1.3 riastrad return false; 1260 1.3 riastrad 1261 1.3 riastrad amdgpu_amdkfd_get_local_mem_info(dev->kgd, &mem_info); 1262 1.3 riastrad if (mem_info.local_mem_size_private == 0 && 1263 1.3 riastrad mem_info.local_mem_size_public > 0) 1264 1.3 riastrad return true; 1265 1.3 riastrad return false; 1266 1.3 riastrad } 1267 1.3 riastrad 1268 1.3 riastrad static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep, 1269 1.3 riastrad struct kfd_process *p, void *data) 1270 1.3 riastrad { 1271 1.3 riastrad struct kfd_ioctl_alloc_memory_of_gpu_args *args = data; 1272 1.3 riastrad struct kfd_process_device *pdd; 1273 1.3 riastrad void *mem; 1274 1.3 riastrad struct kfd_dev *dev; 1275 1.3 riastrad int idr_handle; 1276 1.3 riastrad long err; 1277 1.3 riastrad uint64_t offset = args->mmap_offset; 1278 1.3 riastrad uint32_t flags = args->flags; 1279 1.3 riastrad 1280 1.3 riastrad if (args->size == 0) 1281 1.3 riastrad return -EINVAL; 1282 1.3 riastrad 1283 1.3 riastrad dev = kfd_device_by_id(args->gpu_id); 1284 1.3 riastrad if (!dev) 1285 1.3 riastrad return -EINVAL; 1286 1.3 riastrad 1287 1.3 riastrad if ((flags & KFD_IOC_ALLOC_MEM_FLAGS_PUBLIC) && 1288 1.3 riastrad (flags & KFD_IOC_ALLOC_MEM_FLAGS_VRAM) && 1289 1.3 riastrad !kfd_dev_is_large_bar(dev)) { 1290 1.3 riastrad pr_err("Alloc host visible vram on small bar is not allowed\n"); 1291 1.3 riastrad return -EINVAL; 1292 1.3 riastrad } 1293 1.3 riastrad 1294 1.3 riastrad if (flags & KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL) { 1295 1.3 riastrad if (args->size != kfd_doorbell_process_slice(dev)) 1296 1.3 riastrad return -EINVAL; 1297 1.3 riastrad offset = kfd_get_process_doorbells(dev, p); 1298 1.3 riastrad } else if (flags & KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP) { 1299 1.3 riastrad if (args->size != PAGE_SIZE) 1300 1.3 riastrad return -EINVAL; 1301 1.3 riastrad offset = amdgpu_amdkfd_get_mmio_remap_phys_addr(dev->kgd); 1302 1.3 riastrad if (!offset) 1303 1.3 riastrad return -ENOMEM; 1304 1.3 riastrad } 1305 1.3 riastrad 1306 1.3 riastrad mutex_lock(&p->mutex); 1307 1.3 riastrad 1308 1.3 riastrad pdd = kfd_bind_process_to_device(dev, p); 1309 1.3 riastrad if (IS_ERR(pdd)) { 1310 1.3 riastrad err = PTR_ERR(pdd); 1311 1.3 riastrad goto err_unlock; 1312 1.3 riastrad } 1313 1.3 riastrad 1314 1.3 riastrad err = amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( 1315 1.3 riastrad dev->kgd, args->va_addr, args->size, 1316 1.3 riastrad pdd->vm, (struct kgd_mem **) &mem, &offset, 1317 1.3 riastrad flags); 1318 1.3 riastrad 1319 1.3 riastrad if (err) 1320 1.3 riastrad goto err_unlock; 1321 1.3 riastrad 1322 1.3 riastrad idr_handle = kfd_process_device_create_obj_handle(pdd, mem); 1323 1.3 riastrad if (idr_handle < 0) { 1324 1.3 riastrad err = -EFAULT; 1325 1.3 riastrad goto err_free; 1326 1.3 riastrad } 1327 1.3 riastrad 1328 1.3 riastrad mutex_unlock(&p->mutex); 1329 1.3 riastrad 1330 1.3 riastrad args->handle = MAKE_HANDLE(args->gpu_id, idr_handle); 1331 1.3 riastrad args->mmap_offset = offset; 1332 1.3 riastrad 1333 1.3 riastrad /* MMIO is mapped through kfd device 1334 1.3 riastrad * Generate a kfd mmap offset 1335 1.3 riastrad */ 1336 1.3 riastrad if (flags & KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP) 1337 1.3 riastrad args->mmap_offset = KFD_MMAP_TYPE_MMIO 1338 1.3 riastrad | KFD_MMAP_GPU_ID(args->gpu_id); 1339 1.3 riastrad 1340 1.3 riastrad return 0; 1341 1.3 riastrad 1342 1.3 riastrad err_free: 1343 1.3 riastrad amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->kgd, (struct kgd_mem *)mem); 1344 1.3 riastrad err_unlock: 1345 1.3 riastrad mutex_unlock(&p->mutex); 1346 1.3 riastrad return err; 1347 1.3 riastrad } 1348 1.3 riastrad 1349 1.3 riastrad static int kfd_ioctl_free_memory_of_gpu(struct file *filep, 1350 1.3 riastrad struct kfd_process *p, void *data) 1351 1.3 riastrad { 1352 1.3 riastrad struct kfd_ioctl_free_memory_of_gpu_args *args = data; 1353 1.3 riastrad struct kfd_process_device *pdd; 1354 1.3 riastrad void *mem; 1355 1.3 riastrad struct kfd_dev *dev; 1356 1.3 riastrad int ret; 1357 1.3 riastrad 1358 1.3 riastrad dev = kfd_device_by_id(GET_GPU_ID(args->handle)); 1359 1.3 riastrad if (!dev) 1360 1.3 riastrad return -EINVAL; 1361 1.3 riastrad 1362 1.3 riastrad mutex_lock(&p->mutex); 1363 1.3 riastrad 1364 1.3 riastrad pdd = kfd_get_process_device_data(dev, p); 1365 1.3 riastrad if (!pdd) { 1366 1.3 riastrad pr_err("Process device data doesn't exist\n"); 1367 1.3 riastrad ret = -EINVAL; 1368 1.3 riastrad goto err_unlock; 1369 1.3 riastrad } 1370 1.3 riastrad 1371 1.3 riastrad mem = kfd_process_device_translate_handle( 1372 1.3 riastrad pdd, GET_IDR_HANDLE(args->handle)); 1373 1.3 riastrad if (!mem) { 1374 1.3 riastrad ret = -EINVAL; 1375 1.3 riastrad goto err_unlock; 1376 1.3 riastrad } 1377 1.3 riastrad 1378 1.3 riastrad ret = amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->kgd, 1379 1.3 riastrad (struct kgd_mem *)mem); 1380 1.3 riastrad 1381 1.3 riastrad /* If freeing the buffer failed, leave the handle in place for 1382 1.3 riastrad * clean-up during process tear-down. 1383 1.3 riastrad */ 1384 1.3 riastrad if (!ret) 1385 1.3 riastrad kfd_process_device_remove_obj_handle( 1386 1.3 riastrad pdd, GET_IDR_HANDLE(args->handle)); 1387 1.3 riastrad 1388 1.3 riastrad err_unlock: 1389 1.3 riastrad mutex_unlock(&p->mutex); 1390 1.3 riastrad return ret; 1391 1.3 riastrad } 1392 1.3 riastrad 1393 1.3 riastrad static int kfd_ioctl_map_memory_to_gpu(struct file *filep, 1394 1.3 riastrad struct kfd_process *p, void *data) 1395 1.3 riastrad { 1396 1.3 riastrad struct kfd_ioctl_map_memory_to_gpu_args *args = data; 1397 1.3 riastrad struct kfd_process_device *pdd, *peer_pdd; 1398 1.3 riastrad void *mem; 1399 1.3 riastrad struct kfd_dev *dev, *peer; 1400 1.3 riastrad long err = 0; 1401 1.3 riastrad int i; 1402 1.3 riastrad uint32_t *devices_arr = NULL; 1403 1.3 riastrad 1404 1.3 riastrad dev = kfd_device_by_id(GET_GPU_ID(args->handle)); 1405 1.3 riastrad if (!dev) 1406 1.3 riastrad return -EINVAL; 1407 1.3 riastrad 1408 1.3 riastrad if (!args->n_devices) { 1409 1.3 riastrad pr_debug("Device IDs array empty\n"); 1410 1.3 riastrad return -EINVAL; 1411 1.3 riastrad } 1412 1.3 riastrad if (args->n_success > args->n_devices) { 1413 1.3 riastrad pr_debug("n_success exceeds n_devices\n"); 1414 1.3 riastrad return -EINVAL; 1415 1.3 riastrad } 1416 1.3 riastrad 1417 1.3 riastrad devices_arr = kmalloc_array(args->n_devices, sizeof(*devices_arr), 1418 1.3 riastrad GFP_KERNEL); 1419 1.3 riastrad if (!devices_arr) 1420 1.3 riastrad return -ENOMEM; 1421 1.3 riastrad 1422 1.3 riastrad err = copy_from_user(devices_arr, 1423 1.3 riastrad (void __user *)args->device_ids_array_ptr, 1424 1.3 riastrad args->n_devices * sizeof(*devices_arr)); 1425 1.3 riastrad if (err != 0) { 1426 1.3 riastrad err = -EFAULT; 1427 1.3 riastrad goto copy_from_user_failed; 1428 1.3 riastrad } 1429 1.3 riastrad 1430 1.3 riastrad mutex_lock(&p->mutex); 1431 1.3 riastrad 1432 1.3 riastrad pdd = kfd_bind_process_to_device(dev, p); 1433 1.3 riastrad if (IS_ERR(pdd)) { 1434 1.3 riastrad err = PTR_ERR(pdd); 1435 1.3 riastrad goto bind_process_to_device_failed; 1436 1.3 riastrad } 1437 1.3 riastrad 1438 1.3 riastrad mem = kfd_process_device_translate_handle(pdd, 1439 1.3 riastrad GET_IDR_HANDLE(args->handle)); 1440 1.3 riastrad if (!mem) { 1441 1.3 riastrad err = -ENOMEM; 1442 1.3 riastrad goto get_mem_obj_from_handle_failed; 1443 1.3 riastrad } 1444 1.3 riastrad 1445 1.3 riastrad for (i = args->n_success; i < args->n_devices; i++) { 1446 1.3 riastrad peer = kfd_device_by_id(devices_arr[i]); 1447 1.3 riastrad if (!peer) { 1448 1.3 riastrad pr_debug("Getting device by id failed for 0x%x\n", 1449 1.3 riastrad devices_arr[i]); 1450 1.3 riastrad err = -EINVAL; 1451 1.3 riastrad goto get_mem_obj_from_handle_failed; 1452 1.3 riastrad } 1453 1.3 riastrad 1454 1.3 riastrad peer_pdd = kfd_bind_process_to_device(peer, p); 1455 1.3 riastrad if (IS_ERR(peer_pdd)) { 1456 1.3 riastrad err = PTR_ERR(peer_pdd); 1457 1.3 riastrad goto get_mem_obj_from_handle_failed; 1458 1.3 riastrad } 1459 1.3 riastrad err = amdgpu_amdkfd_gpuvm_map_memory_to_gpu( 1460 1.3 riastrad peer->kgd, (struct kgd_mem *)mem, peer_pdd->vm); 1461 1.3 riastrad if (err) { 1462 1.3 riastrad pr_err("Failed to map to gpu %d/%d\n", 1463 1.3 riastrad i, args->n_devices); 1464 1.3 riastrad goto map_memory_to_gpu_failed; 1465 1.3 riastrad } 1466 1.3 riastrad args->n_success = i+1; 1467 1.3 riastrad } 1468 1.3 riastrad 1469 1.3 riastrad mutex_unlock(&p->mutex); 1470 1.3 riastrad 1471 1.3 riastrad err = amdgpu_amdkfd_gpuvm_sync_memory(dev->kgd, (struct kgd_mem *) mem, true); 1472 1.3 riastrad if (err) { 1473 1.3 riastrad pr_debug("Sync memory failed, wait interrupted by user signal\n"); 1474 1.3 riastrad goto sync_memory_failed; 1475 1.3 riastrad } 1476 1.3 riastrad 1477 1.3 riastrad /* Flush TLBs after waiting for the page table updates to complete */ 1478 1.3 riastrad for (i = 0; i < args->n_devices; i++) { 1479 1.3 riastrad peer = kfd_device_by_id(devices_arr[i]); 1480 1.3 riastrad if (WARN_ON_ONCE(!peer)) 1481 1.3 riastrad continue; 1482 1.3 riastrad peer_pdd = kfd_get_process_device_data(peer, p); 1483 1.3 riastrad if (WARN_ON_ONCE(!peer_pdd)) 1484 1.3 riastrad continue; 1485 1.3 riastrad kfd_flush_tlb(peer_pdd); 1486 1.3 riastrad } 1487 1.3 riastrad 1488 1.3 riastrad kfree(devices_arr); 1489 1.3 riastrad 1490 1.3 riastrad return err; 1491 1.3 riastrad 1492 1.3 riastrad bind_process_to_device_failed: 1493 1.3 riastrad get_mem_obj_from_handle_failed: 1494 1.3 riastrad map_memory_to_gpu_failed: 1495 1.3 riastrad mutex_unlock(&p->mutex); 1496 1.3 riastrad copy_from_user_failed: 1497 1.3 riastrad sync_memory_failed: 1498 1.3 riastrad kfree(devices_arr); 1499 1.3 riastrad 1500 1.3 riastrad return err; 1501 1.3 riastrad } 1502 1.3 riastrad 1503 1.3 riastrad static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep, 1504 1.3 riastrad struct kfd_process *p, void *data) 1505 1.3 riastrad { 1506 1.3 riastrad struct kfd_ioctl_unmap_memory_from_gpu_args *args = data; 1507 1.3 riastrad struct kfd_process_device *pdd, *peer_pdd; 1508 1.3 riastrad void *mem; 1509 1.3 riastrad struct kfd_dev *dev, *peer; 1510 1.3 riastrad long err = 0; 1511 1.3 riastrad uint32_t *devices_arr = NULL, i; 1512 1.3 riastrad 1513 1.3 riastrad dev = kfd_device_by_id(GET_GPU_ID(args->handle)); 1514 1.3 riastrad if (!dev) 1515 1.3 riastrad return -EINVAL; 1516 1.3 riastrad 1517 1.3 riastrad if (!args->n_devices) { 1518 1.3 riastrad pr_debug("Device IDs array empty\n"); 1519 1.3 riastrad return -EINVAL; 1520 1.3 riastrad } 1521 1.3 riastrad if (args->n_success > args->n_devices) { 1522 1.3 riastrad pr_debug("n_success exceeds n_devices\n"); 1523 1.3 riastrad return -EINVAL; 1524 1.3 riastrad } 1525 1.3 riastrad 1526 1.3 riastrad devices_arr = kmalloc_array(args->n_devices, sizeof(*devices_arr), 1527 1.3 riastrad GFP_KERNEL); 1528 1.3 riastrad if (!devices_arr) 1529 1.3 riastrad return -ENOMEM; 1530 1.3 riastrad 1531 1.3 riastrad err = copy_from_user(devices_arr, 1532 1.3 riastrad (void __user *)args->device_ids_array_ptr, 1533 1.3 riastrad args->n_devices * sizeof(*devices_arr)); 1534 1.3 riastrad if (err != 0) { 1535 1.3 riastrad err = -EFAULT; 1536 1.3 riastrad goto copy_from_user_failed; 1537 1.3 riastrad } 1538 1.3 riastrad 1539 1.3 riastrad mutex_lock(&p->mutex); 1540 1.3 riastrad 1541 1.3 riastrad pdd = kfd_get_process_device_data(dev, p); 1542 1.3 riastrad if (!pdd) { 1543 1.3 riastrad err = -EINVAL; 1544 1.3 riastrad goto bind_process_to_device_failed; 1545 1.3 riastrad } 1546 1.3 riastrad 1547 1.3 riastrad mem = kfd_process_device_translate_handle(pdd, 1548 1.3 riastrad GET_IDR_HANDLE(args->handle)); 1549 1.3 riastrad if (!mem) { 1550 1.3 riastrad err = -ENOMEM; 1551 1.3 riastrad goto get_mem_obj_from_handle_failed; 1552 1.3 riastrad } 1553 1.3 riastrad 1554 1.3 riastrad for (i = args->n_success; i < args->n_devices; i++) { 1555 1.3 riastrad peer = kfd_device_by_id(devices_arr[i]); 1556 1.3 riastrad if (!peer) { 1557 1.3 riastrad err = -EINVAL; 1558 1.3 riastrad goto get_mem_obj_from_handle_failed; 1559 1.3 riastrad } 1560 1.3 riastrad 1561 1.3 riastrad peer_pdd = kfd_get_process_device_data(peer, p); 1562 1.3 riastrad if (!peer_pdd) { 1563 1.3 riastrad err = -ENODEV; 1564 1.3 riastrad goto get_mem_obj_from_handle_failed; 1565 1.3 riastrad } 1566 1.3 riastrad err = amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu( 1567 1.3 riastrad peer->kgd, (struct kgd_mem *)mem, peer_pdd->vm); 1568 1.3 riastrad if (err) { 1569 1.3 riastrad pr_err("Failed to unmap from gpu %d/%d\n", 1570 1.3 riastrad i, args->n_devices); 1571 1.3 riastrad goto unmap_memory_from_gpu_failed; 1572 1.3 riastrad } 1573 1.3 riastrad args->n_success = i+1; 1574 1.3 riastrad } 1575 1.3 riastrad kfree(devices_arr); 1576 1.3 riastrad 1577 1.3 riastrad mutex_unlock(&p->mutex); 1578 1.3 riastrad 1579 1.3 riastrad return 0; 1580 1.3 riastrad 1581 1.3 riastrad bind_process_to_device_failed: 1582 1.3 riastrad get_mem_obj_from_handle_failed: 1583 1.3 riastrad unmap_memory_from_gpu_failed: 1584 1.3 riastrad mutex_unlock(&p->mutex); 1585 1.3 riastrad copy_from_user_failed: 1586 1.3 riastrad kfree(devices_arr); 1587 1.3 riastrad return err; 1588 1.3 riastrad } 1589 1.3 riastrad 1590 1.3 riastrad static int kfd_ioctl_get_dmabuf_info(struct file *filep, 1591 1.3 riastrad struct kfd_process *p, void *data) 1592 1.3 riastrad { 1593 1.3 riastrad struct kfd_ioctl_get_dmabuf_info_args *args = data; 1594 1.3 riastrad struct kfd_dev *dev = NULL; 1595 1.3 riastrad struct kgd_dev *dma_buf_kgd; 1596 1.3 riastrad void *metadata_buffer = NULL; 1597 1.3 riastrad uint32_t flags; 1598 1.3 riastrad unsigned int i; 1599 1.3 riastrad int r; 1600 1.3 riastrad 1601 1.3 riastrad /* Find a KFD GPU device that supports the get_dmabuf_info query */ 1602 1.3 riastrad for (i = 0; kfd_topology_enum_kfd_devices(i, &dev) == 0; i++) 1603 1.3 riastrad if (dev) 1604 1.3 riastrad break; 1605 1.3 riastrad if (!dev) 1606 1.3 riastrad return -EINVAL; 1607 1.3 riastrad 1608 1.3 riastrad if (args->metadata_ptr) { 1609 1.3 riastrad metadata_buffer = kzalloc(args->metadata_size, GFP_KERNEL); 1610 1.3 riastrad if (!metadata_buffer) 1611 1.3 riastrad return -ENOMEM; 1612 1.3 riastrad } 1613 1.3 riastrad 1614 1.3 riastrad /* Get dmabuf info from KGD */ 1615 1.3 riastrad r = amdgpu_amdkfd_get_dmabuf_info(dev->kgd, args->dmabuf_fd, 1616 1.3 riastrad &dma_buf_kgd, &args->size, 1617 1.3 riastrad metadata_buffer, args->metadata_size, 1618 1.3 riastrad &args->metadata_size, &flags); 1619 1.3 riastrad if (r) 1620 1.3 riastrad goto exit; 1621 1.3 riastrad 1622 1.3 riastrad /* Reverse-lookup gpu_id from kgd pointer */ 1623 1.3 riastrad dev = kfd_device_by_kgd(dma_buf_kgd); 1624 1.3 riastrad if (!dev) { 1625 1.3 riastrad r = -EINVAL; 1626 1.3 riastrad goto exit; 1627 1.3 riastrad } 1628 1.3 riastrad args->gpu_id = dev->id; 1629 1.3 riastrad args->flags = flags; 1630 1.3 riastrad 1631 1.3 riastrad /* Copy metadata buffer to user mode */ 1632 1.3 riastrad if (metadata_buffer) { 1633 1.3 riastrad r = copy_to_user((void __user *)args->metadata_ptr, 1634 1.3 riastrad metadata_buffer, args->metadata_size); 1635 1.3 riastrad if (r != 0) 1636 1.3 riastrad r = -EFAULT; 1637 1.3 riastrad } 1638 1.3 riastrad 1639 1.3 riastrad exit: 1640 1.3 riastrad kfree(metadata_buffer); 1641 1.3 riastrad 1642 1.3 riastrad return r; 1643 1.3 riastrad } 1644 1.3 riastrad 1645 1.3 riastrad static int kfd_ioctl_import_dmabuf(struct file *filep, 1646 1.3 riastrad struct kfd_process *p, void *data) 1647 1.3 riastrad { 1648 1.3 riastrad struct kfd_ioctl_import_dmabuf_args *args = data; 1649 1.3 riastrad struct kfd_process_device *pdd; 1650 1.3 riastrad struct dma_buf *dmabuf; 1651 1.3 riastrad struct kfd_dev *dev; 1652 1.3 riastrad int idr_handle; 1653 1.3 riastrad uint64_t size; 1654 1.3 riastrad void *mem; 1655 1.3 riastrad int r; 1656 1.3 riastrad 1657 1.3 riastrad dev = kfd_device_by_id(args->gpu_id); 1658 1.3 riastrad if (!dev) 1659 1.3 riastrad return -EINVAL; 1660 1.3 riastrad 1661 1.3 riastrad dmabuf = dma_buf_get(args->dmabuf_fd); 1662 1.3 riastrad if (IS_ERR(dmabuf)) 1663 1.3 riastrad return PTR_ERR(dmabuf); 1664 1.3 riastrad 1665 1.3 riastrad mutex_lock(&p->mutex); 1666 1.3 riastrad 1667 1.3 riastrad pdd = kfd_bind_process_to_device(dev, p); 1668 1.3 riastrad if (IS_ERR(pdd)) { 1669 1.3 riastrad r = PTR_ERR(pdd); 1670 1.3 riastrad goto err_unlock; 1671 1.3 riastrad } 1672 1.3 riastrad 1673 1.3 riastrad r = amdgpu_amdkfd_gpuvm_import_dmabuf(dev->kgd, dmabuf, 1674 1.3 riastrad args->va_addr, pdd->vm, 1675 1.3 riastrad (struct kgd_mem **)&mem, &size, 1676 1.3 riastrad NULL); 1677 1.3 riastrad if (r) 1678 1.3 riastrad goto err_unlock; 1679 1.3 riastrad 1680 1.3 riastrad idr_handle = kfd_process_device_create_obj_handle(pdd, mem); 1681 1.3 riastrad if (idr_handle < 0) { 1682 1.3 riastrad r = -EFAULT; 1683 1.3 riastrad goto err_free; 1684 1.3 riastrad } 1685 1.3 riastrad 1686 1.3 riastrad mutex_unlock(&p->mutex); 1687 1.3 riastrad 1688 1.3 riastrad args->handle = MAKE_HANDLE(args->gpu_id, idr_handle); 1689 1.3 riastrad 1690 1.3 riastrad return 0; 1691 1.3 riastrad 1692 1.3 riastrad err_free: 1693 1.3 riastrad amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->kgd, (struct kgd_mem *)mem); 1694 1.3 riastrad err_unlock: 1695 1.3 riastrad mutex_unlock(&p->mutex); 1696 1.3 riastrad return r; 1697 1.3 riastrad } 1698 1.3 riastrad 1699 1.1 riastrad #define AMDKFD_IOCTL_DEF(ioctl, _func, _flags) \ 1700 1.3 riastrad [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func, .flags = _flags, \ 1701 1.3 riastrad .cmd_drv = 0, .name = #ioctl} 1702 1.1 riastrad 1703 1.1 riastrad /** Ioctl table */ 1704 1.1 riastrad static const struct amdkfd_ioctl_desc amdkfd_ioctls[] = { 1705 1.1 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_VERSION, 1706 1.1 riastrad kfd_ioctl_get_version, 0), 1707 1.1 riastrad 1708 1.1 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_QUEUE, 1709 1.1 riastrad kfd_ioctl_create_queue, 0), 1710 1.1 riastrad 1711 1.1 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_QUEUE, 1712 1.1 riastrad kfd_ioctl_destroy_queue, 0), 1713 1.1 riastrad 1714 1.1 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_MEMORY_POLICY, 1715 1.1 riastrad kfd_ioctl_set_memory_policy, 0), 1716 1.1 riastrad 1717 1.1 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_CLOCK_COUNTERS, 1718 1.1 riastrad kfd_ioctl_get_clock_counters, 0), 1719 1.1 riastrad 1720 1.1 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_PROCESS_APERTURES, 1721 1.1 riastrad kfd_ioctl_get_process_apertures, 0), 1722 1.1 riastrad 1723 1.1 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_UPDATE_QUEUE, 1724 1.1 riastrad kfd_ioctl_update_queue, 0), 1725 1.1 riastrad 1726 1.1 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_EVENT, 1727 1.1 riastrad kfd_ioctl_create_event, 0), 1728 1.1 riastrad 1729 1.1 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_EVENT, 1730 1.1 riastrad kfd_ioctl_destroy_event, 0), 1731 1.1 riastrad 1732 1.1 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_EVENT, 1733 1.1 riastrad kfd_ioctl_set_event, 0), 1734 1.1 riastrad 1735 1.1 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_RESET_EVENT, 1736 1.1 riastrad kfd_ioctl_reset_event, 0), 1737 1.1 riastrad 1738 1.1 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_WAIT_EVENTS, 1739 1.1 riastrad kfd_ioctl_wait_events, 0), 1740 1.1 riastrad 1741 1.1 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_REGISTER, 1742 1.1 riastrad kfd_ioctl_dbg_register, 0), 1743 1.1 riastrad 1744 1.1 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_UNREGISTER, 1745 1.3 riastrad kfd_ioctl_dbg_unregister, 0), 1746 1.1 riastrad 1747 1.1 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_ADDRESS_WATCH, 1748 1.1 riastrad kfd_ioctl_dbg_address_watch, 0), 1749 1.1 riastrad 1750 1.1 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_WAVE_CONTROL, 1751 1.1 riastrad kfd_ioctl_dbg_wave_control, 0), 1752 1.3 riastrad 1753 1.3 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_SCRATCH_BACKING_VA, 1754 1.3 riastrad kfd_ioctl_set_scratch_backing_va, 0), 1755 1.3 riastrad 1756 1.3 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_TILE_CONFIG, 1757 1.3 riastrad kfd_ioctl_get_tile_config, 0), 1758 1.3 riastrad 1759 1.3 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_TRAP_HANDLER, 1760 1.3 riastrad kfd_ioctl_set_trap_handler, 0), 1761 1.3 riastrad 1762 1.3 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_PROCESS_APERTURES_NEW, 1763 1.3 riastrad kfd_ioctl_get_process_apertures_new, 0), 1764 1.3 riastrad 1765 1.3 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_ACQUIRE_VM, 1766 1.3 riastrad kfd_ioctl_acquire_vm, 0), 1767 1.3 riastrad 1768 1.3 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_ALLOC_MEMORY_OF_GPU, 1769 1.3 riastrad kfd_ioctl_alloc_memory_of_gpu, 0), 1770 1.3 riastrad 1771 1.3 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_FREE_MEMORY_OF_GPU, 1772 1.3 riastrad kfd_ioctl_free_memory_of_gpu, 0), 1773 1.3 riastrad 1774 1.3 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_MAP_MEMORY_TO_GPU, 1775 1.3 riastrad kfd_ioctl_map_memory_to_gpu, 0), 1776 1.3 riastrad 1777 1.3 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_UNMAP_MEMORY_FROM_GPU, 1778 1.3 riastrad kfd_ioctl_unmap_memory_from_gpu, 0), 1779 1.3 riastrad 1780 1.3 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_CU_MASK, 1781 1.3 riastrad kfd_ioctl_set_cu_mask, 0), 1782 1.3 riastrad 1783 1.3 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_QUEUE_WAVE_STATE, 1784 1.3 riastrad kfd_ioctl_get_queue_wave_state, 0), 1785 1.3 riastrad 1786 1.3 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_DMABUF_INFO, 1787 1.3 riastrad kfd_ioctl_get_dmabuf_info, 0), 1788 1.3 riastrad 1789 1.3 riastrad AMDKFD_IOCTL_DEF(AMDKFD_IOC_IMPORT_DMABUF, 1790 1.3 riastrad kfd_ioctl_import_dmabuf, 0), 1791 1.3 riastrad 1792 1.1 riastrad }; 1793 1.1 riastrad 1794 1.1 riastrad #define AMDKFD_CORE_IOCTL_COUNT ARRAY_SIZE(amdkfd_ioctls) 1795 1.1 riastrad 1796 1.1 riastrad static long kfd_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) 1797 1.1 riastrad { 1798 1.1 riastrad struct kfd_process *process; 1799 1.1 riastrad amdkfd_ioctl_t *func; 1800 1.1 riastrad const struct amdkfd_ioctl_desc *ioctl = NULL; 1801 1.1 riastrad unsigned int nr = _IOC_NR(cmd); 1802 1.1 riastrad char stack_kdata[128]; 1803 1.1 riastrad char *kdata = NULL; 1804 1.1 riastrad unsigned int usize, asize; 1805 1.1 riastrad int retcode = -EINVAL; 1806 1.1 riastrad 1807 1.1 riastrad if (nr >= AMDKFD_CORE_IOCTL_COUNT) 1808 1.1 riastrad goto err_i1; 1809 1.1 riastrad 1810 1.1 riastrad if ((nr >= AMDKFD_COMMAND_START) && (nr < AMDKFD_COMMAND_END)) { 1811 1.1 riastrad u32 amdkfd_size; 1812 1.1 riastrad 1813 1.1 riastrad ioctl = &amdkfd_ioctls[nr]; 1814 1.1 riastrad 1815 1.1 riastrad amdkfd_size = _IOC_SIZE(ioctl->cmd); 1816 1.1 riastrad usize = asize = _IOC_SIZE(cmd); 1817 1.1 riastrad if (amdkfd_size > asize) 1818 1.1 riastrad asize = amdkfd_size; 1819 1.1 riastrad 1820 1.1 riastrad cmd = ioctl->cmd; 1821 1.1 riastrad } else 1822 1.1 riastrad goto err_i1; 1823 1.1 riastrad 1824 1.3 riastrad dev_dbg(kfd_device, "ioctl cmd 0x%x (#0x%x), arg 0x%lx\n", cmd, nr, arg); 1825 1.1 riastrad 1826 1.3 riastrad /* Get the process struct from the filep. Only the process 1827 1.3 riastrad * that opened /dev/kfd can use the file descriptor. Child 1828 1.3 riastrad * processes need to create their own KFD device context. 1829 1.3 riastrad */ 1830 1.3 riastrad process = filep->private_data; 1831 1.3 riastrad if (process->lead_thread != current->group_leader) { 1832 1.3 riastrad dev_dbg(kfd_device, "Using KFD FD in wrong process\n"); 1833 1.3 riastrad retcode = -EBADF; 1834 1.1 riastrad goto err_i1; 1835 1.1 riastrad } 1836 1.1 riastrad 1837 1.1 riastrad /* Do not trust userspace, use our own definition */ 1838 1.1 riastrad func = ioctl->func; 1839 1.1 riastrad 1840 1.1 riastrad if (unlikely(!func)) { 1841 1.1 riastrad dev_dbg(kfd_device, "no function\n"); 1842 1.1 riastrad retcode = -EINVAL; 1843 1.1 riastrad goto err_i1; 1844 1.1 riastrad } 1845 1.1 riastrad 1846 1.1 riastrad if (cmd & (IOC_IN | IOC_OUT)) { 1847 1.1 riastrad if (asize <= sizeof(stack_kdata)) { 1848 1.1 riastrad kdata = stack_kdata; 1849 1.1 riastrad } else { 1850 1.1 riastrad kdata = kmalloc(asize, GFP_KERNEL); 1851 1.1 riastrad if (!kdata) { 1852 1.1 riastrad retcode = -ENOMEM; 1853 1.1 riastrad goto err_i1; 1854 1.1 riastrad } 1855 1.1 riastrad } 1856 1.1 riastrad if (asize > usize) 1857 1.1 riastrad memset(kdata + usize, 0, asize - usize); 1858 1.1 riastrad } 1859 1.1 riastrad 1860 1.1 riastrad if (cmd & IOC_IN) { 1861 1.1 riastrad if (copy_from_user(kdata, (void __user *)arg, usize) != 0) { 1862 1.1 riastrad retcode = -EFAULT; 1863 1.1 riastrad goto err_i1; 1864 1.1 riastrad } 1865 1.1 riastrad } else if (cmd & IOC_OUT) { 1866 1.1 riastrad memset(kdata, 0, usize); 1867 1.1 riastrad } 1868 1.1 riastrad 1869 1.1 riastrad retcode = func(filep, process, kdata); 1870 1.1 riastrad 1871 1.1 riastrad if (cmd & IOC_OUT) 1872 1.1 riastrad if (copy_to_user((void __user *)arg, kdata, usize) != 0) 1873 1.1 riastrad retcode = -EFAULT; 1874 1.1 riastrad 1875 1.1 riastrad err_i1: 1876 1.1 riastrad if (!ioctl) 1877 1.1 riastrad dev_dbg(kfd_device, "invalid ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n", 1878 1.1 riastrad task_pid_nr(current), cmd, nr); 1879 1.1 riastrad 1880 1.1 riastrad if (kdata != stack_kdata) 1881 1.1 riastrad kfree(kdata); 1882 1.1 riastrad 1883 1.1 riastrad if (retcode) 1884 1.3 riastrad dev_dbg(kfd_device, "ioctl cmd (#0x%x), arg 0x%lx, ret = %d\n", 1885 1.3 riastrad nr, arg, retcode); 1886 1.1 riastrad 1887 1.1 riastrad return retcode; 1888 1.1 riastrad } 1889 1.1 riastrad 1890 1.3 riastrad static int kfd_mmio_mmap(struct kfd_dev *dev, struct kfd_process *process, 1891 1.3 riastrad struct vm_area_struct *vma) 1892 1.3 riastrad { 1893 1.3 riastrad phys_addr_t address; 1894 1.3 riastrad int ret; 1895 1.3 riastrad 1896 1.3 riastrad if (vma->vm_end - vma->vm_start != PAGE_SIZE) 1897 1.3 riastrad return -EINVAL; 1898 1.3 riastrad 1899 1.3 riastrad address = amdgpu_amdkfd_get_mmio_remap_phys_addr(dev->kgd); 1900 1.3 riastrad 1901 1.3 riastrad vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_NORESERVE | 1902 1.3 riastrad VM_DONTDUMP | VM_PFNMAP; 1903 1.3 riastrad 1904 1.3 riastrad vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 1905 1.3 riastrad 1906 1.3 riastrad pr_debug("pasid 0x%x mapping mmio page\n" 1907 1.3 riastrad " target user address == 0x%08llX\n" 1908 1.3 riastrad " physical address == 0x%08llX\n" 1909 1.3 riastrad " vm_flags == 0x%04lX\n" 1910 1.3 riastrad " size == 0x%04lX\n", 1911 1.3 riastrad process->pasid, (unsigned long long) vma->vm_start, 1912 1.3 riastrad address, vma->vm_flags, PAGE_SIZE); 1913 1.3 riastrad 1914 1.3 riastrad ret = io_remap_pfn_range(vma, 1915 1.3 riastrad vma->vm_start, 1916 1.3 riastrad address >> PAGE_SHIFT, 1917 1.3 riastrad PAGE_SIZE, 1918 1.3 riastrad vma->vm_page_prot); 1919 1.3 riastrad return ret; 1920 1.3 riastrad } 1921 1.3 riastrad 1922 1.3 riastrad 1923 1.1 riastrad static int kfd_mmap(struct file *filp, struct vm_area_struct *vma) 1924 1.1 riastrad { 1925 1.1 riastrad struct kfd_process *process; 1926 1.3 riastrad struct kfd_dev *dev = NULL; 1927 1.3 riastrad unsigned long mmap_offset; 1928 1.3 riastrad unsigned int gpu_id; 1929 1.1 riastrad 1930 1.1 riastrad process = kfd_get_process(current); 1931 1.1 riastrad if (IS_ERR(process)) 1932 1.1 riastrad return PTR_ERR(process); 1933 1.1 riastrad 1934 1.3 riastrad mmap_offset = vma->vm_pgoff << PAGE_SHIFT; 1935 1.3 riastrad gpu_id = KFD_MMAP_GET_GPU_ID(mmap_offset); 1936 1.3 riastrad if (gpu_id) 1937 1.3 riastrad dev = kfd_device_by_id(gpu_id); 1938 1.3 riastrad 1939 1.3 riastrad switch (mmap_offset & KFD_MMAP_TYPE_MASK) { 1940 1.3 riastrad case KFD_MMAP_TYPE_DOORBELL: 1941 1.3 riastrad if (!dev) 1942 1.3 riastrad return -ENODEV; 1943 1.3 riastrad return kfd_doorbell_mmap(dev, process, vma); 1944 1.3 riastrad 1945 1.3 riastrad case KFD_MMAP_TYPE_EVENTS: 1946 1.1 riastrad return kfd_event_mmap(process, vma); 1947 1.3 riastrad 1948 1.3 riastrad case KFD_MMAP_TYPE_RESERVED_MEM: 1949 1.3 riastrad if (!dev) 1950 1.3 riastrad return -ENODEV; 1951 1.3 riastrad return kfd_reserved_mem_mmap(dev, process, vma); 1952 1.3 riastrad case KFD_MMAP_TYPE_MMIO: 1953 1.3 riastrad if (!dev) 1954 1.3 riastrad return -ENODEV; 1955 1.3 riastrad return kfd_mmio_mmap(dev, process, vma); 1956 1.1 riastrad } 1957 1.1 riastrad 1958 1.1 riastrad return -EFAULT; 1959 1.1 riastrad } 1960