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 24 #include <linux/slab.h> 25 #include <linux/list.h> 26 #include "kfd_device_queue_manager.h" 27 #include "kfd_priv.h" 28 #include "kfd_kernel_queue.h" 29 #include "amdgpu_amdkfd.h" 30 31 static inline struct process_queue_node *get_queue_by_qid( 32 struct process_queue_manager *pqm, unsigned int qid) 33 { 34 struct process_queue_node *pqn; 35 36 list_for_each_entry(pqn, &pqm->queues, process_queue_list) { 37 if ((pqn->q && pqn->q->properties.queue_id == qid) || 38 (pqn->kq && pqn->kq->queue->properties.queue_id == qid)) 39 return pqn; 40 } 41 42 return NULL; 43 } 44 45 static int find_available_queue_slot(struct process_queue_manager *pqm, 46 unsigned int *qid) 47 { 48 unsigned long found; 49 50 found = find_first_zero_bit(pqm->queue_slot_bitmap, 51 KFD_MAX_NUM_OF_QUEUES_PER_PROCESS); 52 53 pr_debug("The new slot id %lu\n", found); 54 55 if (found >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS) { 56 pr_info("Cannot open more queues for process with pasid %d\n", 57 pqm->process->pasid); 58 return -ENOMEM; 59 } 60 61 set_bit(found, pqm->queue_slot_bitmap); 62 *qid = found; 63 64 return 0; 65 } 66 67 void kfd_process_dequeue_from_device(struct kfd_process_device *pdd) 68 { 69 struct kfd_dev *dev = pdd->dev; 70 71 if (pdd->already_dequeued) 72 return; 73 74 dev->dqm->ops.process_termination(dev->dqm, &pdd->qpd); 75 pdd->already_dequeued = true; 76 } 77 78 int pqm_set_gws(struct process_queue_manager *pqm, unsigned int qid, 79 void *gws) 80 { 81 struct kfd_dev *dev = NULL; 82 struct process_queue_node *pqn; 83 struct kfd_process_device *pdd; 84 struct kgd_mem *mem = NULL; 85 int ret; 86 87 pqn = get_queue_by_qid(pqm, qid); 88 if (!pqn) { 89 pr_err("Queue id does not match any known queue\n"); 90 return -EINVAL; 91 } 92 93 if (pqn->q) 94 dev = pqn->q->device; 95 if (WARN_ON(!dev)) 96 return -ENODEV; 97 98 pdd = kfd_get_process_device_data(dev, pqm->process); 99 if (!pdd) { 100 pr_err("Process device data doesn't exist\n"); 101 return -EINVAL; 102 } 103 104 /* Only allow one queue per process can have GWS assigned */ 105 if (gws && pdd->qpd.num_gws) 106 return -EBUSY; 107 108 if (!gws && pdd->qpd.num_gws == 0) 109 return -EINVAL; 110 111 if (gws) 112 ret = amdgpu_amdkfd_add_gws_to_process(pdd->process->kgd_process_info, 113 gws, &mem); 114 else 115 ret = amdgpu_amdkfd_remove_gws_from_process(pdd->process->kgd_process_info, 116 pqn->q->gws); 117 if (unlikely(ret)) 118 return ret; 119 120 pqn->q->gws = mem; 121 pdd->qpd.num_gws = gws ? amdgpu_amdkfd_get_num_gws(dev->kgd) : 0; 122 123 return pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm, 124 pqn->q); 125 } 126 127 void kfd_process_dequeue_from_all_devices(struct kfd_process *p) 128 { 129 struct kfd_process_device *pdd; 130 131 list_for_each_entry(pdd, &p->per_device_data, per_device_list) 132 kfd_process_dequeue_from_device(pdd); 133 } 134 135 int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p) 136 { 137 INIT_LIST_HEAD(&pqm->queues); 138 pqm->queue_slot_bitmap = 139 kzalloc(DIV_ROUND_UP(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS, 140 BITS_PER_BYTE), GFP_KERNEL); 141 if (!pqm->queue_slot_bitmap) 142 return -ENOMEM; 143 pqm->process = p; 144 145 return 0; 146 } 147 148 void pqm_uninit(struct process_queue_manager *pqm) 149 { 150 struct process_queue_node *pqn, *next; 151 152 list_for_each_entry_safe(pqn, next, &pqm->queues, process_queue_list) { 153 uninit_queue(pqn->q); 154 list_del(&pqn->process_queue_list); 155 kfree(pqn); 156 } 157 158 kfree(pqm->queue_slot_bitmap); 159 pqm->queue_slot_bitmap = NULL; 160 } 161 162 static int create_cp_queue(struct process_queue_manager *pqm, 163 struct kfd_dev *dev, struct queue **q, 164 struct queue_properties *q_properties, 165 struct file *f, unsigned int qid) 166 { 167 int retval; 168 169 /* Doorbell initialized in user space*/ 170 q_properties->doorbell_ptr = NULL; 171 172 /* let DQM handle it*/ 173 q_properties->vmid = 0; 174 q_properties->queue_id = qid; 175 176 retval = init_queue(q, q_properties); 177 if (retval != 0) 178 return retval; 179 180 (*q)->device = dev; 181 (*q)->process = pqm->process; 182 183 pr_debug("PQM After init queue"); 184 185 return retval; 186 } 187 188 int pqm_create_queue(struct process_queue_manager *pqm, 189 struct kfd_dev *dev, 190 struct file *f, 191 struct queue_properties *properties, 192 unsigned int *qid) 193 { 194 int retval; 195 struct kfd_process_device *pdd; 196 struct queue *q; 197 struct process_queue_node *pqn; 198 struct kernel_queue *kq; 199 enum kfd_queue_type type = properties->type; 200 unsigned int max_queues = 127; /* HWS limit */ 201 202 q = NULL; 203 kq = NULL; 204 205 pdd = kfd_get_process_device_data(dev, pqm->process); 206 if (!pdd) { 207 pr_err("Process device data doesn't exist\n"); 208 return -1; 209 } 210 211 /* 212 * for debug process, verify that it is within the static queues limit 213 * currently limit is set to half of the total avail HQD slots 214 * If we are just about to create DIQ, the is_debug flag is not set yet 215 * Hence we also check the type as well 216 */ 217 if ((pdd->qpd.is_debug) || (type == KFD_QUEUE_TYPE_DIQ)) 218 max_queues = dev->device_info->max_no_of_hqd/2; 219 220 if (pdd->qpd.queue_count >= max_queues) 221 return -ENOSPC; 222 223 retval = find_available_queue_slot(pqm, qid); 224 if (retval != 0) 225 return retval; 226 227 if (list_empty(&pdd->qpd.queues_list) && 228 list_empty(&pdd->qpd.priv_queue_list)) 229 dev->dqm->ops.register_process(dev->dqm, &pdd->qpd); 230 231 pqn = kzalloc(sizeof(*pqn), GFP_KERNEL); 232 if (!pqn) { 233 retval = -ENOMEM; 234 goto err_allocate_pqn; 235 } 236 237 switch (type) { 238 case KFD_QUEUE_TYPE_SDMA: 239 case KFD_QUEUE_TYPE_SDMA_XGMI: 240 if ((type == KFD_QUEUE_TYPE_SDMA && dev->dqm->sdma_queue_count 241 >= get_num_sdma_queues(dev->dqm)) || 242 (type == KFD_QUEUE_TYPE_SDMA_XGMI && 243 dev->dqm->xgmi_sdma_queue_count 244 >= get_num_xgmi_sdma_queues(dev->dqm))) { 245 pr_debug("Over-subscription is not allowed for SDMA.\n"); 246 retval = -EPERM; 247 goto err_create_queue; 248 } 249 250 retval = create_cp_queue(pqm, dev, &q, properties, f, *qid); 251 if (retval != 0) 252 goto err_create_queue; 253 pqn->q = q; 254 pqn->kq = NULL; 255 retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd); 256 pr_debug("DQM returned %d for create_queue\n", retval); 257 print_queue(q); 258 break; 259 260 case KFD_QUEUE_TYPE_COMPUTE: 261 /* check if there is over subscription */ 262 if ((dev->dqm->sched_policy == 263 KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION) && 264 ((dev->dqm->processes_count >= dev->vm_info.vmid_num_kfd) || 265 (dev->dqm->queue_count >= get_queues_num(dev->dqm)))) { 266 pr_debug("Over-subscription is not allowed when amdkfd.sched_policy == 1\n"); 267 retval = -EPERM; 268 goto err_create_queue; 269 } 270 271 retval = create_cp_queue(pqm, dev, &q, properties, f, *qid); 272 if (retval != 0) 273 goto err_create_queue; 274 pqn->q = q; 275 pqn->kq = NULL; 276 retval = dev->dqm->ops.create_queue(dev->dqm, q, &pdd->qpd); 277 pr_debug("DQM returned %d for create_queue\n", retval); 278 print_queue(q); 279 break; 280 case KFD_QUEUE_TYPE_DIQ: 281 kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_DIQ); 282 if (!kq) { 283 retval = -ENOMEM; 284 goto err_create_queue; 285 } 286 kq->queue->properties.queue_id = *qid; 287 pqn->kq = kq; 288 pqn->q = NULL; 289 retval = dev->dqm->ops.create_kernel_queue(dev->dqm, 290 kq, &pdd->qpd); 291 break; 292 default: 293 WARN(1, "Invalid queue type %d", type); 294 retval = -EINVAL; 295 } 296 297 if (retval != 0) { 298 pr_err("Pasid %d DQM create queue %d failed. ret %d\n", 299 pqm->process->pasid, type, retval); 300 goto err_create_queue; 301 } 302 303 if (q) 304 /* Return the doorbell offset within the doorbell page 305 * to the caller so it can be passed up to user mode 306 * (in bytes). 307 */ 308 properties->doorbell_off = 309 (q->properties.doorbell_off * sizeof(uint32_t)) & 310 (kfd_doorbell_process_slice(dev) - 1); 311 312 pr_debug("PQM After DQM create queue\n"); 313 314 list_add(&pqn->process_queue_list, &pqm->queues); 315 316 if (q) { 317 pr_debug("PQM done creating queue\n"); 318 print_queue_properties(&q->properties); 319 } 320 321 return retval; 322 323 err_create_queue: 324 kfree(pqn); 325 err_allocate_pqn: 326 /* check if queues list is empty unregister process from device */ 327 clear_bit(*qid, pqm->queue_slot_bitmap); 328 if (list_empty(&pdd->qpd.queues_list) && 329 list_empty(&pdd->qpd.priv_queue_list)) 330 dev->dqm->ops.unregister_process(dev->dqm, &pdd->qpd); 331 return retval; 332 } 333 334 int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid) 335 { 336 struct process_queue_node *pqn; 337 struct kfd_process_device *pdd; 338 struct device_queue_manager *dqm; 339 struct kfd_dev *dev; 340 int retval; 341 342 dqm = NULL; 343 344 retval = 0; 345 346 pqn = get_queue_by_qid(pqm, qid); 347 if (!pqn) { 348 pr_err("Queue id does not match any known queue\n"); 349 return -EINVAL; 350 } 351 352 dev = NULL; 353 if (pqn->kq) 354 dev = pqn->kq->dev; 355 if (pqn->q) 356 dev = pqn->q->device; 357 if (WARN_ON(!dev)) 358 return -ENODEV; 359 360 pdd = kfd_get_process_device_data(dev, pqm->process); 361 if (!pdd) { 362 pr_err("Process device data doesn't exist\n"); 363 return -1; 364 } 365 366 if (pqn->kq) { 367 /* destroy kernel queue (DIQ) */ 368 dqm = pqn->kq->dev->dqm; 369 dqm->ops.destroy_kernel_queue(dqm, pqn->kq, &pdd->qpd); 370 kernel_queue_uninit(pqn->kq); 371 } 372 373 if (pqn->q) { 374 dqm = pqn->q->device->dqm; 375 retval = dqm->ops.destroy_queue(dqm, &pdd->qpd, pqn->q); 376 if (retval) { 377 pr_err("Pasid %d destroy queue %d failed, ret %d\n", 378 pqm->process->pasid, 379 pqn->q->properties.queue_id, retval); 380 if (retval != -ETIME) 381 goto err_destroy_queue; 382 } 383 384 if (pqn->q->gws) { 385 amdgpu_amdkfd_remove_gws_from_process(pqm->process->kgd_process_info, 386 pqn->q->gws); 387 pdd->qpd.num_gws = 0; 388 } 389 390 kfree(pqn->q->properties.cu_mask); 391 pqn->q->properties.cu_mask = NULL; 392 uninit_queue(pqn->q); 393 } 394 395 list_del(&pqn->process_queue_list); 396 kfree(pqn); 397 clear_bit(qid, pqm->queue_slot_bitmap); 398 399 if (list_empty(&pdd->qpd.queues_list) && 400 list_empty(&pdd->qpd.priv_queue_list)) 401 dqm->ops.unregister_process(dqm, &pdd->qpd); 402 403 err_destroy_queue: 404 return retval; 405 } 406 407 int pqm_update_queue(struct process_queue_manager *pqm, unsigned int qid, 408 struct queue_properties *p) 409 { 410 int retval; 411 struct process_queue_node *pqn; 412 413 pqn = get_queue_by_qid(pqm, qid); 414 if (!pqn) { 415 pr_debug("No queue %d exists for update operation\n", qid); 416 return -EFAULT; 417 } 418 419 pqn->q->properties.queue_address = p->queue_address; 420 pqn->q->properties.queue_size = p->queue_size; 421 pqn->q->properties.queue_percent = p->queue_percent; 422 pqn->q->properties.priority = p->priority; 423 424 retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm, 425 pqn->q); 426 if (retval != 0) 427 return retval; 428 429 return 0; 430 } 431 432 int pqm_set_cu_mask(struct process_queue_manager *pqm, unsigned int qid, 433 struct queue_properties *p) 434 { 435 int retval; 436 struct process_queue_node *pqn; 437 438 pqn = get_queue_by_qid(pqm, qid); 439 if (!pqn) { 440 pr_debug("No queue %d exists for update operation\n", qid); 441 return -EFAULT; 442 } 443 444 /* Free the old CU mask memory if it is already allocated, then 445 * allocate memory for the new CU mask. 446 */ 447 kfree(pqn->q->properties.cu_mask); 448 449 pqn->q->properties.cu_mask_count = p->cu_mask_count; 450 pqn->q->properties.cu_mask = p->cu_mask; 451 452 retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm, 453 pqn->q); 454 if (retval != 0) 455 return retval; 456 457 return 0; 458 } 459 460 struct kernel_queue *pqm_get_kernel_queue( 461 struct process_queue_manager *pqm, 462 unsigned int qid) 463 { 464 struct process_queue_node *pqn; 465 466 pqn = get_queue_by_qid(pqm, qid); 467 if (pqn && pqn->kq) 468 return pqn->kq; 469 470 return NULL; 471 } 472 473 int pqm_get_wave_state(struct process_queue_manager *pqm, 474 unsigned int qid, 475 void __user *ctl_stack, 476 u32 *ctl_stack_used_size, 477 u32 *save_area_used_size) 478 { 479 struct process_queue_node *pqn; 480 481 pqn = get_queue_by_qid(pqm, qid); 482 if (!pqn) { 483 pr_debug("amdkfd: No queue %d exists for operation\n", 484 qid); 485 return -EFAULT; 486 } 487 488 return pqn->q->device->dqm->ops.get_wave_state(pqn->q->device->dqm, 489 pqn->q, 490 ctl_stack, 491 ctl_stack_used_size, 492 save_area_used_size); 493 } 494 495 #if defined(CONFIG_DEBUG_FS) 496 497 int pqm_debugfs_mqds(struct seq_file *m, void *data) 498 { 499 struct process_queue_manager *pqm = data; 500 struct process_queue_node *pqn; 501 struct queue *q; 502 enum KFD_MQD_TYPE mqd_type; 503 struct mqd_manager *mqd_mgr; 504 int r = 0; 505 506 list_for_each_entry(pqn, &pqm->queues, process_queue_list) { 507 if (pqn->q) { 508 q = pqn->q; 509 switch (q->properties.type) { 510 case KFD_QUEUE_TYPE_SDMA: 511 case KFD_QUEUE_TYPE_SDMA_XGMI: 512 seq_printf(m, " SDMA queue on device %x\n", 513 q->device->id); 514 mqd_type = KFD_MQD_TYPE_SDMA; 515 break; 516 case KFD_QUEUE_TYPE_COMPUTE: 517 seq_printf(m, " Compute queue on device %x\n", 518 q->device->id); 519 mqd_type = KFD_MQD_TYPE_CP; 520 break; 521 default: 522 seq_printf(m, 523 " Bad user queue type %d on device %x\n", 524 q->properties.type, q->device->id); 525 continue; 526 } 527 mqd_mgr = q->device->dqm->mqd_mgrs[mqd_type]; 528 } else if (pqn->kq) { 529 q = pqn->kq->queue; 530 mqd_mgr = pqn->kq->mqd_mgr; 531 switch (q->properties.type) { 532 case KFD_QUEUE_TYPE_DIQ: 533 seq_printf(m, " DIQ on device %x\n", 534 pqn->kq->dev->id); 535 break; 536 default: 537 seq_printf(m, 538 " Bad kernel queue type %d on device %x\n", 539 q->properties.type, 540 pqn->kq->dev->id); 541 continue; 542 } 543 } else { 544 seq_printf(m, 545 " Weird: Queue node with neither kernel nor user queue\n"); 546 continue; 547 } 548 549 r = mqd_mgr->debugfs_show_mqd(m, q->mqd); 550 if (r != 0) 551 break; 552 } 553 554 return r; 555 } 556 557 #endif 558