1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright(c) 2019 Intel Corporation. All rights rsvd. */ 3 #include <linux/init.h> 4 #include <linux/kernel.h> 5 #include <linux/module.h> 6 #include <linux/pci.h> 7 #include <linux/io-64-nonatomic-lo-hi.h> 8 #include <linux/dmaengine.h> 9 #include <linux/delay.h> 10 #include <linux/iommu.h> 11 #include <linux/sched/mm.h> 12 #include <uapi/linux/idxd.h> 13 #include "../dmaengine.h" 14 #include "idxd.h" 15 #include "registers.h" 16 17 enum irq_work_type { 18 IRQ_WORK_NORMAL = 0, 19 IRQ_WORK_PROCESS_FAULT, 20 }; 21 22 struct idxd_resubmit { 23 struct work_struct work; 24 struct idxd_desc *desc; 25 }; 26 27 struct idxd_int_handle_revoke { 28 struct work_struct work; 29 struct idxd_device *idxd; 30 }; 31 32 static void idxd_device_reinit(struct work_struct *work) 33 { 34 struct idxd_device *idxd = container_of(work, struct idxd_device, work); 35 struct device *dev = &idxd->pdev->dev; 36 int rc, i; 37 38 idxd_device_reset(idxd); 39 rc = idxd_device_config(idxd); 40 if (rc < 0) 41 goto out; 42 43 rc = idxd_device_enable(idxd); 44 if (rc < 0) 45 goto out; 46 47 for (i = 0; i < idxd->max_wqs; i++) { 48 if (test_bit(i, idxd->wq_enable_map)) { 49 struct idxd_wq *wq = idxd->wqs[i]; 50 51 rc = idxd_wq_enable(wq); 52 if (rc < 0) { 53 clear_bit(i, idxd->wq_enable_map); 54 dev_warn(dev, "Unable to re-enable wq %s\n", 55 dev_name(wq_confdev(wq))); 56 } 57 } 58 } 59 60 return; 61 62 out: 63 idxd_device_clear_state(idxd); 64 } 65 66 /* 67 * The function sends a drain descriptor for the interrupt handle. The drain ensures 68 * all descriptors with this interrupt handle is flushed and the interrupt 69 * will allow the cleanup of the outstanding descriptors. 70 */ 71 static void idxd_int_handle_revoke_drain(struct idxd_irq_entry *ie) 72 { 73 struct idxd_wq *wq = ie_to_wq(ie); 74 struct idxd_device *idxd = wq->idxd; 75 struct device *dev = &idxd->pdev->dev; 76 struct dsa_hw_desc desc = {}; 77 void __iomem *portal; 78 int rc; 79 80 /* Issue a simple drain operation with interrupt but no completion record */ 81 desc.flags = IDXD_OP_FLAG_RCI; 82 desc.opcode = DSA_OPCODE_DRAIN; 83 desc.priv = 1; 84 85 if (ie->pasid != INVALID_IOASID) 86 desc.pasid = ie->pasid; 87 desc.int_handle = ie->int_handle; 88 portal = idxd_wq_portal_addr(wq); 89 90 /* 91 * The wmb() makes sure that the descriptor is all there before we 92 * issue. 93 */ 94 wmb(); 95 if (wq_dedicated(wq)) { 96 iosubmit_cmds512(portal, &desc, 1); 97 } else { 98 rc = idxd_enqcmds(wq, portal, &desc); 99 /* This should not fail unless hardware failed. */ 100 if (rc < 0) 101 dev_warn(dev, "Failed to submit drain desc on wq %d\n", wq->id); 102 } 103 } 104 105 static void idxd_abort_invalid_int_handle_descs(struct idxd_irq_entry *ie) 106 { 107 LIST_HEAD(flist); 108 struct idxd_desc *d, *t; 109 struct llist_node *head; 110 111 spin_lock(&ie->list_lock); 112 head = llist_del_all(&ie->pending_llist); 113 if (head) { 114 llist_for_each_entry_safe(d, t, head, llnode) 115 list_add_tail(&d->list, &ie->work_list); 116 } 117 118 list_for_each_entry_safe(d, t, &ie->work_list, list) { 119 if (d->completion->status == DSA_COMP_INT_HANDLE_INVAL) 120 list_move_tail(&d->list, &flist); 121 } 122 spin_unlock(&ie->list_lock); 123 124 list_for_each_entry_safe(d, t, &flist, list) { 125 list_del(&d->list); 126 idxd_dma_complete_txd(d, IDXD_COMPLETE_ABORT, true); 127 } 128 } 129 130 static void idxd_int_handle_revoke(struct work_struct *work) 131 { 132 struct idxd_int_handle_revoke *revoke = 133 container_of(work, struct idxd_int_handle_revoke, work); 134 struct idxd_device *idxd = revoke->idxd; 135 struct pci_dev *pdev = idxd->pdev; 136 struct device *dev = &pdev->dev; 137 int i, new_handle, rc; 138 139 if (!idxd->request_int_handles) { 140 kfree(revoke); 141 dev_warn(dev, "Unexpected int handle refresh interrupt.\n"); 142 return; 143 } 144 145 /* 146 * The loop attempts to acquire new interrupt handle for all interrupt 147 * vectors that supports a handle. If a new interrupt handle is acquired and the 148 * wq is kernel type, the driver will kill the percpu_ref to pause all 149 * ongoing descriptor submissions. The interrupt handle is then changed. 150 * After change, the percpu_ref is revived and all the pending submissions 151 * are woken to try again. A drain is sent to for the interrupt handle 152 * at the end to make sure all invalid int handle descriptors are processed. 153 */ 154 for (i = 1; i < idxd->irq_cnt; i++) { 155 struct idxd_irq_entry *ie = idxd_get_ie(idxd, i); 156 struct idxd_wq *wq = ie_to_wq(ie); 157 158 if (ie->int_handle == INVALID_INT_HANDLE) 159 continue; 160 161 rc = idxd_device_request_int_handle(idxd, i, &new_handle, IDXD_IRQ_MSIX); 162 if (rc < 0) { 163 dev_warn(dev, "get int handle %d failed: %d\n", i, rc); 164 /* 165 * Failed to acquire new interrupt handle. Kill the WQ 166 * and release all the pending submitters. The submitters will 167 * get error return code and handle appropriately. 168 */ 169 ie->int_handle = INVALID_INT_HANDLE; 170 idxd_wq_quiesce(wq); 171 idxd_abort_invalid_int_handle_descs(ie); 172 continue; 173 } 174 175 /* No change in interrupt handle, nothing needs to be done */ 176 if (ie->int_handle == new_handle) 177 continue; 178 179 if (wq->state != IDXD_WQ_ENABLED || wq->type != IDXD_WQT_KERNEL) { 180 /* 181 * All the MSIX interrupts are allocated at once during probe. 182 * Therefore we need to update all interrupts even if the WQ 183 * isn't supporting interrupt operations. 184 */ 185 ie->int_handle = new_handle; 186 continue; 187 } 188 189 mutex_lock(&wq->wq_lock); 190 reinit_completion(&wq->wq_resurrect); 191 192 /* Kill percpu_ref to pause additional descriptor submissions */ 193 percpu_ref_kill(&wq->wq_active); 194 195 /* Wait for all submitters quiesce before we change interrupt handle */ 196 wait_for_completion(&wq->wq_dead); 197 198 ie->int_handle = new_handle; 199 200 /* Revive percpu ref and wake up all the waiting submitters */ 201 percpu_ref_reinit(&wq->wq_active); 202 complete_all(&wq->wq_resurrect); 203 mutex_unlock(&wq->wq_lock); 204 205 /* 206 * The delay here is to wait for all possible MOVDIR64B that 207 * are issued before percpu_ref_kill() has happened to have 208 * reached the PCIe domain before the drain is issued. The driver 209 * needs to ensure that the drain descriptor issued does not pass 210 * all the other issued descriptors that contain the invalid 211 * interrupt handle in order to ensure that the drain descriptor 212 * interrupt will allow the cleanup of all the descriptors with 213 * invalid interrupt handle. 214 */ 215 if (wq_dedicated(wq)) 216 udelay(100); 217 idxd_int_handle_revoke_drain(ie); 218 } 219 kfree(revoke); 220 } 221 222 static void idxd_evl_fault_work(struct work_struct *work) 223 { 224 struct idxd_evl_fault *fault = container_of(work, struct idxd_evl_fault, work); 225 struct idxd_wq *wq = fault->wq; 226 struct idxd_device *idxd = wq->idxd; 227 struct device *dev = &idxd->pdev->dev; 228 struct __evl_entry *entry_head = fault->entry; 229 void *cr = (void *)entry_head + idxd->data->evl_cr_off; 230 int cr_size = idxd->data->compl_size, copied; 231 232 switch (fault->status) { 233 case DSA_COMP_CRA_XLAT: 234 case DSA_COMP_DRAIN_EVL: 235 /* 236 * Copy completion record to fault_addr in user address space 237 * that is found by wq and PASID. 238 */ 239 copied = idxd_copy_cr(wq, entry_head->pasid, 240 entry_head->fault_addr, 241 cr, cr_size); 242 /* 243 * The task that triggered the page fault is unknown currently 244 * because multiple threads may share the user address 245 * space or the task exits already before this fault. 246 * So if the copy fails, SIGSEGV can not be sent to the task. 247 * Just print an error for the failure. The user application 248 * waiting for the completion record will time out on this 249 * failure. 250 */ 251 if (copied != cr_size) { 252 dev_dbg_ratelimited(dev, "Failed to write to completion record. (%d:%d)\n", 253 cr_size, copied); 254 } 255 break; 256 default: 257 dev_dbg_ratelimited(dev, "Unrecognized error code: %#x\n", 258 DSA_COMP_STATUS(entry_head->error)); 259 break; 260 } 261 262 kmem_cache_free(idxd->evl_cache, fault); 263 } 264 265 static void process_evl_entry(struct idxd_device *idxd, 266 struct __evl_entry *entry_head, unsigned int index) 267 { 268 struct device *dev = &idxd->pdev->dev; 269 struct idxd_evl *evl = idxd->evl; 270 u8 status; 271 272 if (test_bit(index, evl->bmap)) { 273 clear_bit(index, evl->bmap); 274 } else { 275 status = DSA_COMP_STATUS(entry_head->error); 276 277 if (status == DSA_COMP_CRA_XLAT || status == DSA_COMP_DRAIN_EVL) { 278 struct idxd_evl_fault *fault; 279 int ent_size = evl_ent_size(idxd); 280 281 if (entry_head->rci) 282 dev_dbg(dev, "Completion Int Req set, ignoring!\n"); 283 284 if (!entry_head->rcr && status == DSA_COMP_DRAIN_EVL) 285 return; 286 287 fault = kmem_cache_alloc(idxd->evl_cache, GFP_ATOMIC); 288 if (fault) { 289 struct idxd_wq *wq = idxd->wqs[entry_head->wq_idx]; 290 291 fault->wq = wq; 292 fault->status = status; 293 memcpy(&fault->entry, entry_head, ent_size); 294 INIT_WORK(&fault->work, idxd_evl_fault_work); 295 queue_work(wq->wq, &fault->work); 296 } else { 297 dev_warn(dev, "Failed to service fault work.\n"); 298 } 299 } else { 300 dev_warn_ratelimited(dev, "Device error %#x operation: %#x fault addr: %#llx\n", 301 status, entry_head->operation, 302 entry_head->fault_addr); 303 } 304 } 305 } 306 307 static void process_evl_entries(struct idxd_device *idxd) 308 { 309 union evl_status_reg evl_status; 310 unsigned int h, t; 311 struct idxd_evl *evl = idxd->evl; 312 struct __evl_entry *entry_head; 313 unsigned int ent_size = evl_ent_size(idxd); 314 u32 size; 315 316 evl_status.bits = 0; 317 evl_status.int_pending = 1; 318 319 spin_lock(&evl->lock); 320 /* Clear interrupt pending bit */ 321 iowrite32(evl_status.bits_upper32, 322 idxd->reg_base + IDXD_EVLSTATUS_OFFSET + sizeof(u32)); 323 h = evl->head; 324 evl_status.bits = ioread64(idxd->reg_base + IDXD_EVLSTATUS_OFFSET); 325 t = evl_status.tail; 326 size = idxd->evl->size; 327 328 while (h != t) { 329 entry_head = (struct __evl_entry *)(evl->log + (h * ent_size)); 330 process_evl_entry(idxd, entry_head, h); 331 h = (h + 1) % size; 332 } 333 334 evl->head = h; 335 evl_status.head = h; 336 iowrite32(evl_status.bits_lower32, idxd->reg_base + IDXD_EVLSTATUS_OFFSET); 337 spin_unlock(&evl->lock); 338 } 339 340 irqreturn_t idxd_misc_thread(int vec, void *data) 341 { 342 struct idxd_irq_entry *irq_entry = data; 343 struct idxd_device *idxd = ie_to_idxd(irq_entry); 344 struct device *dev = &idxd->pdev->dev; 345 union gensts_reg gensts; 346 u32 val = 0; 347 int i; 348 bool err = false; 349 u32 cause; 350 351 cause = ioread32(idxd->reg_base + IDXD_INTCAUSE_OFFSET); 352 if (!cause) 353 return IRQ_NONE; 354 355 iowrite32(cause, idxd->reg_base + IDXD_INTCAUSE_OFFSET); 356 357 if (cause & IDXD_INTC_HALT_STATE) 358 goto halt; 359 360 if (cause & IDXD_INTC_ERR) { 361 spin_lock(&idxd->dev_lock); 362 for (i = 0; i < 4; i++) 363 idxd->sw_err.bits[i] = ioread64(idxd->reg_base + 364 IDXD_SWERR_OFFSET + i * sizeof(u64)); 365 366 iowrite64(idxd->sw_err.bits[0] & IDXD_SWERR_ACK, 367 idxd->reg_base + IDXD_SWERR_OFFSET); 368 369 if (idxd->sw_err.valid && idxd->sw_err.wq_idx_valid) { 370 int id = idxd->sw_err.wq_idx; 371 struct idxd_wq *wq = idxd->wqs[id]; 372 373 if (wq->type == IDXD_WQT_USER) 374 wake_up_interruptible(&wq->err_queue); 375 } else { 376 int i; 377 378 for (i = 0; i < idxd->max_wqs; i++) { 379 struct idxd_wq *wq = idxd->wqs[i]; 380 381 if (wq->type == IDXD_WQT_USER) 382 wake_up_interruptible(&wq->err_queue); 383 } 384 } 385 386 spin_unlock(&idxd->dev_lock); 387 val |= IDXD_INTC_ERR; 388 389 for (i = 0; i < 4; i++) 390 dev_warn(dev, "err[%d]: %#16.16llx\n", 391 i, idxd->sw_err.bits[i]); 392 err = true; 393 } 394 395 if (cause & IDXD_INTC_INT_HANDLE_REVOKED) { 396 struct idxd_int_handle_revoke *revoke; 397 398 val |= IDXD_INTC_INT_HANDLE_REVOKED; 399 400 revoke = kzalloc(sizeof(*revoke), GFP_ATOMIC); 401 if (revoke) { 402 revoke->idxd = idxd; 403 INIT_WORK(&revoke->work, idxd_int_handle_revoke); 404 queue_work(idxd->wq, &revoke->work); 405 406 } else { 407 dev_err(dev, "Failed to allocate work for int handle revoke\n"); 408 idxd_wqs_quiesce(idxd); 409 } 410 } 411 412 if (cause & IDXD_INTC_CMD) { 413 val |= IDXD_INTC_CMD; 414 complete(idxd->cmd_done); 415 } 416 417 if (cause & IDXD_INTC_OCCUPY) { 418 /* Driver does not utilize occupancy interrupt */ 419 val |= IDXD_INTC_OCCUPY; 420 } 421 422 if (cause & IDXD_INTC_PERFMON_OVFL) { 423 val |= IDXD_INTC_PERFMON_OVFL; 424 perfmon_counter_overflow(idxd); 425 } 426 427 if (cause & IDXD_INTC_EVL) { 428 val |= IDXD_INTC_EVL; 429 process_evl_entries(idxd); 430 } 431 432 val ^= cause; 433 if (val) 434 dev_warn_once(dev, "Unexpected interrupt cause bits set: %#x\n", 435 val); 436 437 if (!err) 438 goto out; 439 440 halt: 441 gensts.bits = ioread32(idxd->reg_base + IDXD_GENSTATS_OFFSET); 442 if (gensts.state == IDXD_DEVICE_STATE_HALT) { 443 idxd->state = IDXD_DEV_HALTED; 444 if (gensts.reset_type == IDXD_DEVICE_RESET_SOFTWARE) { 445 /* 446 * If we need a software reset, we will throw the work 447 * on a system workqueue in order to allow interrupts 448 * for the device command completions. 449 */ 450 INIT_WORK(&idxd->work, idxd_device_reinit); 451 queue_work(idxd->wq, &idxd->work); 452 } else { 453 idxd->state = IDXD_DEV_HALTED; 454 idxd_wqs_quiesce(idxd); 455 idxd_wqs_unmap_portal(idxd); 456 idxd_device_clear_state(idxd); 457 dev_err(&idxd->pdev->dev, 458 "idxd halted, need %s.\n", 459 gensts.reset_type == IDXD_DEVICE_RESET_FLR ? 460 "FLR" : "system reset"); 461 } 462 } 463 464 out: 465 return IRQ_HANDLED; 466 } 467 468 static void idxd_int_handle_resubmit_work(struct work_struct *work) 469 { 470 struct idxd_resubmit *irw = container_of(work, struct idxd_resubmit, work); 471 struct idxd_desc *desc = irw->desc; 472 struct idxd_wq *wq = desc->wq; 473 int rc; 474 475 desc->completion->status = 0; 476 rc = idxd_submit_desc(wq, desc); 477 if (rc < 0) { 478 dev_dbg(&wq->idxd->pdev->dev, "Failed to resubmit desc %d to wq %d.\n", 479 desc->id, wq->id); 480 /* 481 * If the error is not -EAGAIN, it means the submission failed due to wq 482 * has been killed instead of ENQCMDS failure. Here the driver needs to 483 * notify the submitter of the failure by reporting abort status. 484 * 485 * -EAGAIN comes from ENQCMDS failure. idxd_submit_desc() will handle the 486 * abort. 487 */ 488 if (rc != -EAGAIN) { 489 desc->completion->status = IDXD_COMP_DESC_ABORT; 490 idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT, false); 491 } 492 idxd_free_desc(wq, desc); 493 } 494 kfree(irw); 495 } 496 497 bool idxd_queue_int_handle_resubmit(struct idxd_desc *desc) 498 { 499 struct idxd_wq *wq = desc->wq; 500 struct idxd_device *idxd = wq->idxd; 501 struct idxd_resubmit *irw; 502 503 irw = kzalloc(sizeof(*irw), GFP_KERNEL); 504 if (!irw) 505 return false; 506 507 irw->desc = desc; 508 INIT_WORK(&irw->work, idxd_int_handle_resubmit_work); 509 queue_work(idxd->wq, &irw->work); 510 return true; 511 } 512 513 static void irq_process_pending_llist(struct idxd_irq_entry *irq_entry) 514 { 515 struct idxd_desc *desc, *t; 516 struct llist_node *head; 517 518 head = llist_del_all(&irq_entry->pending_llist); 519 if (!head) 520 return; 521 522 llist_for_each_entry_safe(desc, t, head, llnode) { 523 u8 status = desc->completion->status & DSA_COMP_STATUS_MASK; 524 525 if (status) { 526 /* 527 * Check against the original status as ABORT is software defined 528 * and 0xff, which DSA_COMP_STATUS_MASK can mask out. 529 */ 530 if (unlikely(desc->completion->status == IDXD_COMP_DESC_ABORT)) { 531 idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT, true); 532 continue; 533 } 534 535 idxd_dma_complete_txd(desc, IDXD_COMPLETE_NORMAL, true); 536 } else { 537 spin_lock(&irq_entry->list_lock); 538 list_add_tail(&desc->list, 539 &irq_entry->work_list); 540 spin_unlock(&irq_entry->list_lock); 541 } 542 } 543 } 544 545 static void irq_process_work_list(struct idxd_irq_entry *irq_entry) 546 { 547 LIST_HEAD(flist); 548 struct idxd_desc *desc, *n; 549 550 /* 551 * This lock protects list corruption from access of list outside of the irq handler 552 * thread. 553 */ 554 spin_lock(&irq_entry->list_lock); 555 if (list_empty(&irq_entry->work_list)) { 556 spin_unlock(&irq_entry->list_lock); 557 return; 558 } 559 560 list_for_each_entry_safe(desc, n, &irq_entry->work_list, list) { 561 if (desc->completion->status) { 562 list_move_tail(&desc->list, &flist); 563 } 564 } 565 566 spin_unlock(&irq_entry->list_lock); 567 568 list_for_each_entry(desc, &flist, list) { 569 /* 570 * Check against the original status as ABORT is software defined 571 * and 0xff, which DSA_COMP_STATUS_MASK can mask out. 572 */ 573 if (unlikely(desc->completion->status == IDXD_COMP_DESC_ABORT)) { 574 idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT, true); 575 continue; 576 } 577 578 idxd_dma_complete_txd(desc, IDXD_COMPLETE_NORMAL, true); 579 } 580 } 581 582 irqreturn_t idxd_wq_thread(int irq, void *data) 583 { 584 struct idxd_irq_entry *irq_entry = data; 585 586 /* 587 * There are two lists we are processing. The pending_llist is where 588 * submmiter adds all the submitted descriptor after sending it to 589 * the workqueue. It's a lockless singly linked list. The work_list 590 * is the common linux double linked list. We are in a scenario of 591 * multiple producers and a single consumer. The producers are all 592 * the kernel submitters of descriptors, and the consumer is the 593 * kernel irq handler thread for the msix vector when using threaded 594 * irq. To work with the restrictions of llist to remain lockless, 595 * we are doing the following steps: 596 * 1. Iterate through the work_list and process any completed 597 * descriptor. Delete the completed entries during iteration. 598 * 2. llist_del_all() from the pending list. 599 * 3. Iterate through the llist that was deleted from the pending list 600 * and process the completed entries. 601 * 4. If the entry is still waiting on hardware, list_add_tail() to 602 * the work_list. 603 */ 604 irq_process_work_list(irq_entry); 605 irq_process_pending_llist(irq_entry); 606 607 return IRQ_HANDLED; 608 } 609