1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Thunderbolt driver - NHI driver 4 * 5 * The NHI (native host interface) is the device that allows us to send and 6 * receive frames from the thunderbolt bus. 7 * 8 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com> 9 * Copyright (C) 2018, Intel Corporation 10 */ 11 12 #include <linux/pm_runtime.h> 13 #include <linux/slab.h> 14 #include <linux/errno.h> 15 #include <linux/dma-mapping.h> 16 #include <linux/interrupt.h> 17 #include <linux/iommu.h> 18 #include <linux/module.h> 19 #include <linux/delay.h> 20 #include <linux/property.h> 21 #include <linux/string_choices.h> 22 #include <linux/string_helpers.h> 23 24 #include "nhi.h" 25 #include "nhi_regs.h" 26 #include "tb.h" 27 28 #define RING_TYPE(ring) ((ring)->is_tx ? "TX ring" : "RX ring") 29 30 #define RING_FIRST_USABLE_HOPID 1 31 /* 32 * Used with QUIRK_E2E to specify an unused HopID the Rx credits are 33 * transferred. 34 */ 35 #define RING_E2E_RESERVED_HOPID RING_FIRST_USABLE_HOPID 36 37 #define NHI_MAILBOX_TIMEOUT 500 /* ms */ 38 39 static bool host_reset = true; 40 module_param(host_reset, bool, 0444); 41 MODULE_PARM_DESC(host_reset, "reset USB4 host router (default: true)"); 42 43 static int ring_interrupt_index(const struct tb_ring *ring) 44 { 45 int bit = ring->hop; 46 if (!ring->is_tx) 47 bit += ring->nhi->hop_count; 48 return bit; 49 } 50 51 static void nhi_mask_interrupt(struct tb_nhi *nhi, int mask, int ring) 52 { 53 if (nhi->quirks & QUIRK_AUTO_CLEAR_INT) { 54 u32 val; 55 56 val = ioread32(nhi->iobase + REG_RING_INTERRUPT_BASE + ring); 57 iowrite32(val & ~mask, nhi->iobase + REG_RING_INTERRUPT_BASE + ring); 58 } else { 59 iowrite32(mask, nhi->iobase + REG_RING_INTERRUPT_MASK_CLEAR_BASE + ring); 60 } 61 } 62 63 static void nhi_clear_interrupt(struct tb_nhi *nhi, int ring) 64 { 65 if (nhi->quirks & QUIRK_AUTO_CLEAR_INT) 66 ioread32(nhi->iobase + REG_RING_NOTIFY_BASE + ring); 67 else 68 iowrite32(~0, nhi->iobase + REG_RING_INT_CLEAR + ring); 69 } 70 71 /* 72 * ring_interrupt_active() - activate/deactivate interrupts for a single ring 73 * 74 * ring->nhi->lock must be held. 75 */ 76 static void ring_interrupt_active(struct tb_ring *ring, bool active) 77 { 78 int index = ring_interrupt_index(ring) / 32 * 4; 79 int reg = REG_RING_INTERRUPT_BASE + index; 80 int interrupt_bit = ring_interrupt_index(ring) & 31; 81 int mask = 1 << interrupt_bit; 82 u32 old, new; 83 84 if (ring->irq > 0) { 85 u32 step, shift, ivr, misc, itr; 86 void __iomem *ivr_base; 87 int auto_clear_bit; 88 int index; 89 90 if (ring->is_tx) 91 index = ring->hop; 92 else 93 index = ring->hop + ring->nhi->hop_count; 94 95 /* 96 * Intel routers support a bit that isn't part of 97 * the USB4 spec to ask the hardware to clear 98 * interrupt status bits automatically since 99 * we already know which interrupt was triggered. 100 * 101 * Other routers explicitly disable auto-clear 102 * to prevent conditions that may occur where two 103 * MSIX interrupts are simultaneously active and 104 * reading the register clears both of them. 105 */ 106 misc = ioread32(ring->nhi->iobase + REG_DMA_MISC); 107 if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT) 108 auto_clear_bit = REG_DMA_MISC_INT_AUTO_CLEAR; 109 else 110 auto_clear_bit = REG_DMA_MISC_DISABLE_AUTO_CLEAR; 111 if (!(misc & auto_clear_bit)) 112 iowrite32(misc | auto_clear_bit, 113 ring->nhi->iobase + REG_DMA_MISC); 114 115 ivr_base = ring->nhi->iobase + REG_INT_VEC_ALLOC_BASE; 116 step = index / REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS; 117 shift = index % REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS; 118 ivr = ioread32(ivr_base + step); 119 ivr &= ~(REG_INT_VEC_ALLOC_MASK << shift); 120 if (active) 121 ivr |= ring->vector << shift; 122 iowrite32(ivr, ivr_base + step); 123 124 /* Throttling is specified in 256ns increments */ 125 itr = DIV_ROUND_UP(ring->interval_nsec, 256); 126 itr &= REG_INT_THROTTLING_RATE_INTERVAL_MASK; 127 iowrite32(itr, ring->nhi->iobase + REG_INT_THROTTLING_RATE + 128 ring->vector * 4); 129 } 130 131 old = ioread32(ring->nhi->iobase + reg); 132 if (active) 133 new = old | mask; 134 else 135 new = old & ~mask; 136 137 dev_dbg(ring->nhi->dev, 138 "%s interrupt at register %#x bit %d (%#x -> %#x)\n", 139 active ? "enabling" : "disabling", reg, interrupt_bit, old, new); 140 141 if (new == old) 142 dev_WARN(ring->nhi->dev, "interrupt for %s %d is already %s\n", 143 RING_TYPE(ring), ring->hop, 144 str_enabled_disabled(active)); 145 146 if (active) 147 iowrite32(new, ring->nhi->iobase + reg); 148 else 149 nhi_mask_interrupt(ring->nhi, mask, index); 150 } 151 152 /* 153 * nhi_disable_interrupts() - disable interrupts for all rings 154 * 155 * Use only during init and shutdown. 156 */ 157 void nhi_disable_interrupts(struct tb_nhi *nhi) 158 { 159 int i = 0; 160 /* disable interrupts */ 161 for (i = 0; i < RING_INTERRUPT_REG_COUNT(nhi); i++) 162 nhi_mask_interrupt(nhi, ~0, 4 * i); 163 164 /* clear interrupt status bits */ 165 for (i = 0; i < RING_NOTIFY_REG_COUNT(nhi); i++) 166 nhi_clear_interrupt(nhi, 4 * i); 167 } 168 169 /* ring helper methods */ 170 171 static void __iomem *ring_desc_base(struct tb_ring *ring) 172 { 173 void __iomem *io = ring->nhi->iobase; 174 io += ring->is_tx ? REG_TX_RING_BASE : REG_RX_RING_BASE; 175 io += ring->hop * 16; 176 return io; 177 } 178 179 static void __iomem *ring_options_base(struct tb_ring *ring) 180 { 181 void __iomem *io = ring->nhi->iobase; 182 io += ring->is_tx ? REG_TX_OPTIONS_BASE : REG_RX_OPTIONS_BASE; 183 io += ring->hop * 32; 184 return io; 185 } 186 187 static void ring_iowrite_cons(struct tb_ring *ring, u16 cons) 188 { 189 /* 190 * The other 16-bits in the register is read-only and writes to it 191 * are ignored by the hardware so we can save one ioread32() by 192 * filling the read-only bits with zeroes. 193 */ 194 iowrite32(cons, ring_desc_base(ring) + 8); 195 } 196 197 static void ring_iowrite_prod(struct tb_ring *ring, u16 prod) 198 { 199 /* See ring_iowrite_cons() above for explanation */ 200 iowrite32(prod << 16, ring_desc_base(ring) + 8); 201 } 202 203 static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset) 204 { 205 iowrite32(value, ring_desc_base(ring) + offset); 206 } 207 208 static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset) 209 { 210 iowrite32(value, ring_desc_base(ring) + offset); 211 iowrite32(value >> 32, ring_desc_base(ring) + offset + 4); 212 } 213 214 static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset) 215 { 216 iowrite32(value, ring_options_base(ring) + offset); 217 } 218 219 static bool ring_full(struct tb_ring *ring) 220 { 221 return ((ring->head + 1) % ring->size) == ring->tail; 222 } 223 224 static bool ring_empty(struct tb_ring *ring) 225 { 226 return ring->head == ring->tail; 227 } 228 229 /* 230 * ring_write_descriptors() - post frames from ring->queue to the controller 231 * 232 * ring->lock is held. 233 */ 234 static void ring_write_descriptors(struct tb_ring *ring) 235 { 236 struct ring_frame *frame, *n; 237 struct ring_desc *descriptor; 238 u32 flags; 239 240 flags = RING_DESC_POSTED; 241 if (!(ring->flags & RING_FLAG_NO_INTERRUPT)) 242 flags |= RING_DESC_INTERRUPT; 243 244 list_for_each_entry_safe(frame, n, &ring->queue, list) { 245 if (ring_full(ring)) 246 break; 247 list_move_tail(&frame->list, &ring->in_flight); 248 descriptor = &ring->descriptors[ring->head]; 249 descriptor->phys = frame->buffer_phy; 250 descriptor->time = 0; 251 descriptor->flags = flags; 252 if (ring->is_tx) { 253 descriptor->length = frame->size; 254 descriptor->eof = frame->eof; 255 descriptor->sof = frame->sof; 256 } 257 ring->head = (ring->head + 1) % ring->size; 258 if (ring->is_tx) 259 ring_iowrite_prod(ring, ring->head); 260 else 261 ring_iowrite_cons(ring, ring->head); 262 } 263 } 264 265 /* 266 * ring_work() - progress completed frames 267 * 268 * If the ring is shutting down then all frames are marked as canceled and 269 * their callbacks are invoked. 270 * 271 * Otherwise we collect all completed frame from the ring buffer, write new 272 * frame to the ring buffer and invoke the callbacks for the completed frames. 273 */ 274 static void ring_work(struct work_struct *work) 275 { 276 struct tb_ring *ring = container_of(work, typeof(*ring), work); 277 struct ring_frame *frame; 278 bool canceled = false; 279 unsigned long flags; 280 LIST_HEAD(done); 281 282 spin_lock_irqsave(&ring->lock, flags); 283 284 if (!ring->running) { 285 /* Move all frames to done and mark them as canceled. */ 286 list_splice_tail_init(&ring->in_flight, &done); 287 list_splice_tail_init(&ring->queue, &done); 288 canceled = true; 289 goto invoke_callback; 290 } 291 292 while (!ring_empty(ring)) { 293 if (!(ring->descriptors[ring->tail].flags 294 & RING_DESC_COMPLETED)) 295 break; 296 frame = list_first_entry(&ring->in_flight, typeof(*frame), 297 list); 298 list_move_tail(&frame->list, &done); 299 if (!ring->is_tx) { 300 frame->size = ring->descriptors[ring->tail].length; 301 frame->eof = ring->descriptors[ring->tail].eof; 302 frame->sof = ring->descriptors[ring->tail].sof; 303 frame->flags = ring->descriptors[ring->tail].flags; 304 } 305 ring->tail = (ring->tail + 1) % ring->size; 306 } 307 ring_write_descriptors(ring); 308 309 invoke_callback: 310 /* allow callbacks to schedule new work */ 311 spin_unlock_irqrestore(&ring->lock, flags); 312 while (!list_empty(&done)) { 313 frame = list_first_entry(&done, typeof(*frame), list); 314 /* 315 * The callback may reenqueue or delete frame. 316 * Do not hold on to it. 317 */ 318 list_del_init(&frame->list); 319 if (frame->callback) 320 frame->callback(ring, frame, canceled); 321 } 322 323 wake_up(&ring->wait); 324 } 325 326 int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame) 327 { 328 unsigned long flags; 329 int ret = 0; 330 331 spin_lock_irqsave(&ring->lock, flags); 332 if (ring->running) { 333 list_add_tail(&frame->list, &ring->queue); 334 ring_write_descriptors(ring); 335 } else { 336 ret = -ESHUTDOWN; 337 } 338 spin_unlock_irqrestore(&ring->lock, flags); 339 return ret; 340 } 341 EXPORT_SYMBOL_GPL(__tb_ring_enqueue); 342 343 /** 344 * tb_ring_poll() - Poll one completed frame from the ring 345 * @ring: Ring to poll 346 * 347 * This function can be called when @start_poll callback of the @ring 348 * has been called or the ring is created with %RING_FLAG_NO_INTERRUPT. 349 * It will read one completed frame from the ring and return it to the 350 * caller. 351 * 352 * Return: Pointer to &struct ring_frame, %NULL if there is no more 353 * completed frames. 354 */ 355 struct ring_frame *tb_ring_poll(struct tb_ring *ring) 356 { 357 struct ring_frame *frame = NULL; 358 unsigned long flags; 359 360 spin_lock_irqsave(&ring->lock, flags); 361 if (!ring->running) 362 goto unlock; 363 if (ring_empty(ring)) 364 goto unlock; 365 366 if (ring->descriptors[ring->tail].flags & RING_DESC_COMPLETED) { 367 frame = list_first_entry(&ring->in_flight, typeof(*frame), 368 list); 369 list_del_init(&frame->list); 370 371 if (!ring->is_tx) { 372 frame->size = ring->descriptors[ring->tail].length; 373 frame->eof = ring->descriptors[ring->tail].eof; 374 frame->sof = ring->descriptors[ring->tail].sof; 375 frame->flags = ring->descriptors[ring->tail].flags; 376 } 377 378 ring->tail = (ring->tail + 1) % ring->size; 379 } 380 381 unlock: 382 spin_unlock_irqrestore(&ring->lock, flags); 383 return frame; 384 } 385 EXPORT_SYMBOL_GPL(tb_ring_poll); 386 387 static void __ring_interrupt_mask(struct tb_ring *ring, bool mask) 388 { 389 int idx = ring_interrupt_index(ring); 390 int reg = REG_RING_INTERRUPT_BASE + idx / 32 * 4; 391 int bit = idx % 32; 392 u32 val; 393 394 val = ioread32(ring->nhi->iobase + reg); 395 if (mask) 396 val &= ~BIT(bit); 397 else 398 val |= BIT(bit); 399 iowrite32(val, ring->nhi->iobase + reg); 400 } 401 402 /* Both @nhi->lock and @ring->lock should be held */ 403 static void __ring_interrupt(struct tb_ring *ring) 404 { 405 if (!ring->running) 406 return; 407 408 if (ring->start_poll) { 409 __ring_interrupt_mask(ring, true); 410 ring->start_poll(ring->poll_data); 411 } else { 412 schedule_work(&ring->work); 413 } 414 } 415 416 /** 417 * tb_ring_poll_complete() - Re-start interrupt for the ring 418 * @ring: Ring to re-start the interrupt 419 * 420 * This will re-start (unmask) the ring interrupt once the user is done 421 * with polling. 422 */ 423 void tb_ring_poll_complete(struct tb_ring *ring) 424 { 425 unsigned long flags; 426 427 spin_lock_irqsave(&ring->nhi->lock, flags); 428 spin_lock(&ring->lock); 429 if (ring->start_poll) 430 __ring_interrupt_mask(ring, false); 431 spin_unlock(&ring->lock); 432 spin_unlock_irqrestore(&ring->nhi->lock, flags); 433 } 434 EXPORT_SYMBOL_GPL(tb_ring_poll_complete); 435 436 static void ring_clear_msix(const struct tb_ring *ring) 437 { 438 int bit; 439 440 if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT) 441 return; 442 443 bit = ring_interrupt_index(ring) & 31; 444 if (ring->is_tx) 445 iowrite32(BIT(bit), ring->nhi->iobase + REG_RING_INT_CLEAR); 446 else 447 iowrite32(BIT(bit), ring->nhi->iobase + REG_RING_INT_CLEAR + 448 4 * (ring->nhi->hop_count / 32)); 449 } 450 451 irqreturn_t ring_msix(int irq, void *data) 452 { 453 struct tb_ring *ring = data; 454 455 spin_lock(&ring->nhi->lock); 456 ring_clear_msix(ring); 457 spin_lock(&ring->lock); 458 __ring_interrupt(ring); 459 spin_unlock(&ring->lock); 460 spin_unlock(&ring->nhi->lock); 461 462 return IRQ_HANDLED; 463 } 464 465 static int nhi_alloc_hop(struct tb_nhi *nhi, struct tb_ring *ring) 466 { 467 unsigned int start_hop = RING_FIRST_USABLE_HOPID; 468 int ret = 0; 469 470 if (nhi->quirks & QUIRK_E2E) { 471 start_hop = RING_FIRST_USABLE_HOPID + 1; 472 if (ring->flags & RING_FLAG_E2E && !ring->is_tx) { 473 dev_dbg(nhi->dev, "quirking E2E TX HopID %u -> %u\n", 474 ring->e2e_tx_hop, RING_E2E_RESERVED_HOPID); 475 ring->e2e_tx_hop = RING_E2E_RESERVED_HOPID; 476 } 477 } 478 479 spin_lock_irq(&nhi->lock); 480 481 if (ring->hop < 0) { 482 unsigned int i; 483 484 /* 485 * Automatically allocate HopID from the non-reserved 486 * range 1 .. hop_count - 1. 487 */ 488 for (i = start_hop; i < nhi->hop_count; i++) { 489 if (ring->is_tx) { 490 if (!nhi->tx_rings[i]) { 491 ring->hop = i; 492 break; 493 } 494 } else { 495 if (!nhi->rx_rings[i]) { 496 ring->hop = i; 497 break; 498 } 499 } 500 } 501 } 502 503 if (ring->hop > 0 && ring->hop < start_hop) { 504 dev_warn(nhi->dev, "invalid hop: %d\n", ring->hop); 505 ret = -EINVAL; 506 goto err_unlock; 507 } 508 if (ring->hop < 0 || ring->hop >= nhi->hop_count) { 509 dev_warn(nhi->dev, "invalid hop: %d\n", ring->hop); 510 ret = -EINVAL; 511 goto err_unlock; 512 } 513 if (ring->is_tx && nhi->tx_rings[ring->hop]) { 514 dev_warn(nhi->dev, "TX hop %d already allocated\n", 515 ring->hop); 516 ret = -EBUSY; 517 goto err_unlock; 518 } 519 if (!ring->is_tx && nhi->rx_rings[ring->hop]) { 520 dev_warn(nhi->dev, "RX hop %d already allocated\n", 521 ring->hop); 522 ret = -EBUSY; 523 goto err_unlock; 524 } 525 526 if (ring->is_tx) 527 nhi->tx_rings[ring->hop] = ring; 528 else 529 nhi->rx_rings[ring->hop] = ring; 530 531 err_unlock: 532 spin_unlock_irq(&nhi->lock); 533 534 return ret; 535 } 536 537 static struct tb_ring *tb_ring_alloc(struct tb_nhi *nhi, u32 hop, int size, 538 bool transmit, unsigned int flags, 539 int e2e_tx_hop, u16 sof_mask, u16 eof_mask, 540 void (*start_poll)(void *), 541 void *poll_data) 542 { 543 struct tb_ring *ring = NULL; 544 545 dev_dbg(nhi->dev, "allocating %s ring %d of size %d\n", 546 transmit ? "TX" : "RX", hop, size); 547 548 if ((flags & RING_FLAG_NO_INTERRUPT) && start_poll) { 549 dev_WARN(nhi->dev, 550 "start_poll() and NO_INTERRUPT cannot be used at the same time\n"); 551 return NULL; 552 } 553 554 ring = kzalloc_obj(*ring); 555 if (!ring) 556 return NULL; 557 558 spin_lock_init(&ring->lock); 559 INIT_LIST_HEAD(&ring->queue); 560 INIT_LIST_HEAD(&ring->in_flight); 561 INIT_WORK(&ring->work, ring_work); 562 init_waitqueue_head(&ring->wait); 563 564 ring->nhi = nhi; 565 ring->hop = hop; 566 ring->is_tx = transmit; 567 ring->size = size; 568 ring->flags = flags; 569 ring->e2e_tx_hop = e2e_tx_hop; 570 ring->sof_mask = sof_mask; 571 ring->eof_mask = eof_mask; 572 ring->head = 0; 573 ring->tail = 0; 574 ring->running = false; 575 ring->start_poll = start_poll; 576 ring->poll_data = poll_data; 577 578 ring->descriptors = dma_alloc_coherent(ring->nhi->dev, 579 size * sizeof(*ring->descriptors), 580 &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO); 581 if (!ring->descriptors) 582 goto err_free_ring; 583 584 if (!(flags & RING_FLAG_NO_INTERRUPT) && nhi->ops->request_ring_irq) { 585 if (nhi->ops->request_ring_irq(ring, flags & RING_FLAG_NO_SUSPEND)) 586 goto err_free_descs; 587 } 588 589 if (nhi_alloc_hop(nhi, ring)) 590 goto err_release_msix; 591 592 return ring; 593 594 err_release_msix: 595 if (nhi->ops->release_ring_irq) 596 nhi->ops->release_ring_irq(ring); 597 err_free_descs: 598 dma_free_coherent(ring->nhi->dev, 599 ring->size * sizeof(*ring->descriptors), 600 ring->descriptors, ring->descriptors_dma); 601 err_free_ring: 602 kfree(ring); 603 604 return NULL; 605 } 606 607 /** 608 * tb_ring_alloc_tx() - Allocate DMA ring for transmit 609 * @nhi: Pointer to the NHI the ring is to be allocated 610 * @hop: HopID (ring) to allocate 611 * @size: Number of entries in the ring 612 * @flags: Flags for the ring 613 * 614 * Return: Pointer to &struct tb_ring, %NULL otherwise. 615 */ 616 struct tb_ring *tb_ring_alloc_tx(struct tb_nhi *nhi, int hop, int size, 617 unsigned int flags) 618 { 619 return tb_ring_alloc(nhi, hop, size, true, flags, 0, 0, 0, NULL, NULL); 620 } 621 EXPORT_SYMBOL_GPL(tb_ring_alloc_tx); 622 623 /** 624 * tb_ring_alloc_rx() - Allocate DMA ring for receive 625 * @nhi: Pointer to the NHI the ring is to be allocated 626 * @hop: HopID (ring) to allocate. Pass %-1 for automatic allocation. 627 * @size: Number of entries in the ring 628 * @flags: Flags for the ring 629 * @e2e_tx_hop: Transmit HopID when E2E is enabled in @flags 630 * @sof_mask: Mask of PDF values that start a frame 631 * @eof_mask: Mask of PDF values that end a frame 632 * @start_poll: If not %NULL the ring will call this function when an 633 * interrupt is triggered and masked, instead of callback 634 * in each Rx frame. 635 * @poll_data: Optional data passed to @start_poll 636 * 637 * Return: Pointer to &struct tb_ring, %NULL otherwise. 638 */ 639 struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size, 640 unsigned int flags, int e2e_tx_hop, 641 u16 sof_mask, u16 eof_mask, 642 void (*start_poll)(void *), void *poll_data) 643 { 644 return tb_ring_alloc(nhi, hop, size, false, flags, e2e_tx_hop, sof_mask, eof_mask, 645 start_poll, poll_data); 646 } 647 EXPORT_SYMBOL_GPL(tb_ring_alloc_rx); 648 649 /** 650 * tb_ring_start() - enable a ring 651 * @ring: Ring to start 652 * 653 * Must not be invoked in parallel with tb_ring_stop(). 654 */ 655 void tb_ring_start(struct tb_ring *ring) 656 { 657 u16 frame_size; 658 u32 flags; 659 660 spin_lock_irq(&ring->nhi->lock); 661 spin_lock(&ring->lock); 662 if (ring->nhi->going_away) 663 goto err; 664 if (ring->running) { 665 dev_WARN(ring->nhi->dev, "ring already started\n"); 666 goto err; 667 } 668 dev_dbg(ring->nhi->dev, "starting %s %d\n", 669 RING_TYPE(ring), ring->hop); 670 671 if (ring->flags & RING_FLAG_FRAME) { 672 /* Means 4096 */ 673 frame_size = 0; 674 flags = RING_FLAG_ENABLE; 675 } else { 676 frame_size = TB_FRAME_SIZE; 677 flags = RING_FLAG_ENABLE | RING_FLAG_RAW; 678 } 679 680 ring_iowrite64desc(ring, ring->descriptors_dma, 0); 681 if (ring->is_tx) { 682 ring_iowrite32desc(ring, ring->size, 12); 683 ring_iowrite32options(ring, 0, 4); 684 ring_iowrite32options(ring, flags, 0); 685 } else { 686 u32 sof_eof_mask = ring->sof_mask << 16 | ring->eof_mask; 687 688 ring_iowrite32desc(ring, (frame_size << 16) | ring->size, 12); 689 ring_iowrite32options(ring, sof_eof_mask, 4); 690 ring_iowrite32options(ring, flags, 0); 691 } 692 693 /* 694 * Now that the ring valid bit is set we can configure E2E if 695 * enabled for the ring. 696 */ 697 if (ring->flags & RING_FLAG_E2E) { 698 if (!ring->is_tx) { 699 u32 hop; 700 701 hop = ring->e2e_tx_hop << REG_RX_OPTIONS_E2E_HOP_SHIFT; 702 hop &= REG_RX_OPTIONS_E2E_HOP_MASK; 703 flags |= hop; 704 705 dev_dbg(ring->nhi->dev, 706 "enabling E2E for %s %d with TX HopID %d\n", 707 RING_TYPE(ring), ring->hop, ring->e2e_tx_hop); 708 } else { 709 dev_dbg(ring->nhi->dev, "enabling E2E for %s %d\n", 710 RING_TYPE(ring), ring->hop); 711 } 712 713 flags |= RING_FLAG_E2E_FLOW_CONTROL; 714 ring_iowrite32options(ring, flags, 0); 715 } 716 717 if (!(ring->flags & RING_FLAG_NO_INTERRUPT)) 718 ring_interrupt_active(ring, true); 719 ring->running = true; 720 err: 721 spin_unlock(&ring->lock); 722 spin_unlock_irq(&ring->nhi->lock); 723 } 724 EXPORT_SYMBOL_GPL(tb_ring_start); 725 726 static bool tb_ring_empty(struct tb_ring *ring) 727 { 728 guard(spinlock_irqsave)(&ring->lock); 729 return list_empty(&ring->in_flight); 730 } 731 732 /** 733 * tb_ring_flush() - Waits for a ring to be empty 734 * @ring: Ring to wait 735 * @timeout_msec: Timeout in ms how long to wait. 736 * 737 * This can be called before stopping a ring to make sure all the frames 738 * submitted prior have been completed. 739 * 740 * Return: %true if the ring is empty now, %false otherwise. 741 */ 742 bool tb_ring_flush(struct tb_ring *ring, unsigned int timeout_msec) 743 { 744 if (!wait_event_timeout(ring->wait, tb_ring_empty(ring), 745 msecs_to_jiffies(timeout_msec))) 746 return false; 747 return tb_ring_empty(ring); 748 } 749 EXPORT_SYMBOL_GPL(tb_ring_flush); 750 751 /** 752 * tb_ring_stop() - shutdown a ring 753 * @ring: Ring to stop 754 * 755 * Must not be invoked from a callback. 756 * 757 * This method will disable the ring. Further calls to 758 * tb_ring_tx/tb_ring_rx will return -ESHUTDOWN until ring_stop has been 759 * called. 760 * 761 * All enqueued frames will be canceled and their callbacks will be executed 762 * with frame->canceled set to true (on the callback thread). This method 763 * returns only after all callback invocations have finished. 764 */ 765 void tb_ring_stop(struct tb_ring *ring) 766 { 767 spin_lock_irq(&ring->nhi->lock); 768 spin_lock(&ring->lock); 769 dev_dbg(ring->nhi->dev, "stopping %s %d\n", 770 RING_TYPE(ring), ring->hop); 771 if (ring->nhi->going_away) 772 goto err; 773 if (!ring->running) { 774 dev_WARN(ring->nhi->dev, "%s %d already stopped\n", 775 RING_TYPE(ring), ring->hop); 776 goto err; 777 } 778 if (!(ring->flags & RING_FLAG_NO_INTERRUPT)) 779 ring_interrupt_active(ring, false); 780 781 ring_iowrite32options(ring, 0, 0); 782 ring_iowrite64desc(ring, 0, 0); 783 ring_iowrite32desc(ring, 0, 8); 784 ring_iowrite32desc(ring, 0, 12); 785 ring->head = 0; 786 ring->tail = 0; 787 ring->running = false; 788 789 err: 790 spin_unlock(&ring->lock); 791 spin_unlock_irq(&ring->nhi->lock); 792 793 /* 794 * schedule ring->work to invoke callbacks on all remaining frames. 795 */ 796 schedule_work(&ring->work); 797 flush_work(&ring->work); 798 } 799 EXPORT_SYMBOL_GPL(tb_ring_stop); 800 801 /* 802 * tb_ring_free() - free ring 803 * 804 * When this method returns all invocations of ring->callback will have 805 * finished. 806 * 807 * Ring must be stopped. 808 * 809 * Must NOT be called from ring_frame->callback! 810 */ 811 void tb_ring_free(struct tb_ring *ring) 812 { 813 struct tb_nhi *nhi = ring->nhi; 814 815 spin_lock_irq(&ring->nhi->lock); 816 /* 817 * Dissociate the ring from the NHI. This also ensures that 818 * nhi_interrupt_work cannot reschedule ring->work. 819 */ 820 if (ring->is_tx) 821 ring->nhi->tx_rings[ring->hop] = NULL; 822 else 823 ring->nhi->rx_rings[ring->hop] = NULL; 824 825 if (ring->running) { 826 dev_WARN(ring->nhi->dev, "%s %d still running\n", 827 RING_TYPE(ring), ring->hop); 828 } 829 spin_unlock_irq(&ring->nhi->lock); 830 831 if (nhi->ops->release_ring_irq) 832 nhi->ops->release_ring_irq(ring); 833 834 dma_free_coherent(ring->nhi->dev, 835 ring->size * sizeof(*ring->descriptors), 836 ring->descriptors, ring->descriptors_dma); 837 838 ring->descriptors = NULL; 839 ring->descriptors_dma = 0; 840 841 842 dev_dbg(ring->nhi->dev, "freeing %s %d\n", RING_TYPE(ring), 843 ring->hop); 844 845 /* 846 * ring->work can no longer be scheduled (it is scheduled only 847 * by nhi_interrupt_work, ring_stop and ring_msix). Wait for it 848 * to finish before freeing the ring. 849 */ 850 flush_work(&ring->work); 851 kfree(ring); 852 } 853 EXPORT_SYMBOL_GPL(tb_ring_free); 854 855 /** 856 * tb_ring_throttling() - Configure throttling for ring interrupt 857 * @ring: Ring to configure 858 * @interval_nsec: Interval counter for moderation (in ns), %0 disables 859 * 860 * Enables or disables ring interrupt throttling. The ring must be 861 * stopped for this to be called. Granularity is 256 ns. 862 * 863 * Return: %0 on success, negative errno otherwise. 864 */ 865 int tb_ring_throttling(struct tb_ring *ring, unsigned int interval_nsec) 866 { 867 guard(spinlock_irqsave)(&ring->lock); 868 if (WARN_ON_ONCE(ring->running)) 869 return -EBUSY; 870 ring->interval_nsec = interval_nsec; 871 return 0; 872 } 873 EXPORT_SYMBOL_GPL(tb_ring_throttling); 874 875 /** 876 * nhi_mailbox_cmd() - Send a command through NHI mailbox 877 * @nhi: Pointer to the NHI structure 878 * @cmd: Command to send 879 * @data: Data to be send with the command 880 * 881 * Sends mailbox command to the firmware running on NHI. 882 * 883 * Return: %0 on success, negative errno otherwise. 884 */ 885 int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data) 886 { 887 ktime_t timeout; 888 u32 val; 889 890 iowrite32(data, nhi->iobase + REG_INMAIL_DATA); 891 892 val = ioread32(nhi->iobase + REG_INMAIL_CMD); 893 val &= ~(REG_INMAIL_CMD_MASK | REG_INMAIL_ERROR); 894 val |= REG_INMAIL_OP_REQUEST | cmd; 895 iowrite32(val, nhi->iobase + REG_INMAIL_CMD); 896 897 timeout = ktime_add_ms(ktime_get(), NHI_MAILBOX_TIMEOUT); 898 do { 899 val = ioread32(nhi->iobase + REG_INMAIL_CMD); 900 if (!(val & REG_INMAIL_OP_REQUEST)) 901 break; 902 usleep_range(10, 20); 903 } while (ktime_before(ktime_get(), timeout)); 904 905 if (val & REG_INMAIL_OP_REQUEST) 906 return -ETIMEDOUT; 907 if (val & REG_INMAIL_ERROR) 908 return -EIO; 909 910 return 0; 911 } 912 913 /** 914 * nhi_mailbox_mode() - Return current firmware operation mode 915 * @nhi: Pointer to the NHI structure 916 * 917 * The function reads current firmware operation mode using NHI mailbox 918 * registers and returns it to the caller. 919 * 920 * Return: &enum nhi_fw_mode. 921 */ 922 enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi) 923 { 924 u32 val; 925 926 val = ioread32(nhi->iobase + REG_OUTMAIL_CMD); 927 val &= REG_OUTMAIL_CMD_OPMODE_MASK; 928 val >>= REG_OUTMAIL_CMD_OPMODE_SHIFT; 929 930 return (enum nhi_fw_mode)val; 931 } 932 933 void nhi_interrupt_work(struct work_struct *work) 934 { 935 struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work); 936 int value = 0; /* Suppress uninitialized usage warning. */ 937 int bit; 938 int hop = -1; 939 int type = 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */ 940 struct tb_ring *ring; 941 942 spin_lock_irq(&nhi->lock); 943 944 /* 945 * Starting at REG_RING_NOTIFY_BASE there are three status bitfields 946 * (TX, RX, RX overflow). We iterate over the bits and read a new 947 * dwords as required. The registers are cleared on read. 948 */ 949 for (bit = 0; bit < 3 * nhi->hop_count; bit++) { 950 if (bit % 32 == 0) 951 value = ioread32(nhi->iobase 952 + REG_RING_NOTIFY_BASE 953 + 4 * (bit / 32)); 954 if (++hop == nhi->hop_count) { 955 hop = 0; 956 type++; 957 } 958 if ((value & (1 << (bit % 32))) == 0) 959 continue; 960 if (type == 2) { 961 dev_warn(nhi->dev, "RX overflow for ring %d\n", hop); 962 continue; 963 } 964 if (type == 0) 965 ring = nhi->tx_rings[hop]; 966 else 967 ring = nhi->rx_rings[hop]; 968 if (ring == NULL) { 969 dev_warn(nhi->dev, 970 "got interrupt for inactive %s ring %d\n", 971 type ? "RX" : "TX", 972 hop); 973 continue; 974 } 975 976 spin_lock(&ring->lock); 977 __ring_interrupt(ring); 978 spin_unlock(&ring->lock); 979 } 980 spin_unlock_irq(&nhi->lock); 981 } 982 983 irqreturn_t nhi_msi(int irq, void *data) 984 { 985 struct tb_nhi *nhi = data; 986 schedule_work(&nhi->interrupt_work); 987 return IRQ_HANDLED; 988 } 989 990 static int __nhi_suspend_noirq(struct device *dev, bool wakeup) 991 { 992 struct tb *tb = dev_get_drvdata(dev); 993 struct tb_nhi *nhi = tb->nhi; 994 int ret; 995 996 ret = tb_domain_suspend_noirq(tb); 997 if (ret) 998 return ret; 999 1000 if (nhi->ops->suspend_noirq) { 1001 ret = nhi->ops->suspend_noirq(tb->nhi, wakeup); 1002 if (ret) 1003 return ret; 1004 } 1005 1006 return 0; 1007 } 1008 1009 static int nhi_suspend_noirq(struct device *dev) 1010 { 1011 return __nhi_suspend_noirq(dev, device_may_wakeup(dev)); 1012 } 1013 1014 static int nhi_freeze_noirq(struct device *dev) 1015 { 1016 struct tb *tb = dev_get_drvdata(dev); 1017 1018 return tb_domain_freeze_noirq(tb); 1019 } 1020 1021 static int nhi_thaw_noirq(struct device *dev) 1022 { 1023 struct tb *tb = dev_get_drvdata(dev); 1024 1025 return tb_domain_thaw_noirq(tb); 1026 } 1027 1028 static bool nhi_wake_supported(struct device *dev) 1029 { 1030 u8 val; 1031 1032 /* 1033 * If power rails are sustainable for wakeup from S4 this 1034 * property is set by the BIOS. 1035 */ 1036 if (!device_property_read_u8(dev, "WAKE_SUPPORTED", &val)) 1037 return !!val; 1038 1039 return true; 1040 } 1041 1042 static int nhi_poweroff_noirq(struct device *dev) 1043 { 1044 bool wakeup; 1045 1046 wakeup = device_may_wakeup(dev) && nhi_wake_supported(dev); 1047 return __nhi_suspend_noirq(dev, wakeup); 1048 } 1049 1050 static int nhi_resume_noirq(struct device *dev) 1051 { 1052 struct tb *tb = dev_get_drvdata(dev); 1053 struct tb_nhi *nhi = tb->nhi; 1054 int ret; 1055 1056 /* 1057 * Check that the device is still there. It may be that the user 1058 * unplugged last device which causes the host controller to go 1059 * away on PCs. 1060 */ 1061 if ((nhi->ops->is_present && !nhi->ops->is_present(nhi))) { 1062 nhi->going_away = true; 1063 } else if (nhi->ops->resume_noirq) { 1064 ret = nhi->ops->resume_noirq(nhi); 1065 if (ret) 1066 return ret; 1067 } 1068 1069 return tb_domain_resume_noirq(tb); 1070 } 1071 1072 static int nhi_suspend(struct device *dev) 1073 { 1074 struct tb *tb = dev_get_drvdata(dev); 1075 1076 return tb_domain_suspend(tb); 1077 } 1078 1079 static void nhi_complete(struct device *dev) 1080 { 1081 struct tb *tb = dev_get_drvdata(dev); 1082 1083 /* 1084 * If we were runtime suspended when system suspend started, 1085 * schedule runtime resume now. It should bring the domain back 1086 * to functional state. 1087 */ 1088 if (pm_runtime_suspended(dev)) 1089 pm_runtime_resume(dev); 1090 else 1091 tb_domain_complete(tb); 1092 } 1093 1094 static int nhi_runtime_suspend(struct device *dev) 1095 { 1096 struct tb *tb = dev_get_drvdata(dev); 1097 struct tb_nhi *nhi = tb->nhi; 1098 int ret; 1099 1100 ret = tb_domain_runtime_suspend(tb); 1101 if (ret) 1102 return ret; 1103 1104 if (nhi->ops->runtime_suspend) { 1105 ret = nhi->ops->runtime_suspend(tb->nhi); 1106 if (ret) 1107 return ret; 1108 } 1109 return 0; 1110 } 1111 1112 static int nhi_runtime_resume(struct device *dev) 1113 { 1114 struct tb *tb = dev_get_drvdata(dev); 1115 struct tb_nhi *nhi = tb->nhi; 1116 int ret; 1117 1118 if (nhi->ops->runtime_resume) { 1119 ret = nhi->ops->runtime_resume(nhi); 1120 if (ret) 1121 return ret; 1122 } 1123 1124 return tb_domain_runtime_resume(tb); 1125 } 1126 1127 void nhi_shutdown(struct tb_nhi *nhi) 1128 { 1129 int i; 1130 1131 dev_dbg(nhi->dev, "shutdown\n"); 1132 1133 for (i = 0; i < nhi->hop_count; i++) { 1134 if (nhi->tx_rings[i]) 1135 dev_WARN(nhi->dev, 1136 "TX ring %d is still active\n", i); 1137 if (nhi->rx_rings[i]) 1138 dev_WARN(nhi->dev, 1139 "RX ring %d is still active\n", i); 1140 } 1141 nhi_disable_interrupts(nhi); 1142 1143 if (nhi->ops->shutdown) 1144 nhi->ops->shutdown(nhi); 1145 } 1146 1147 static void nhi_reset(struct tb_nhi *nhi) 1148 { 1149 ktime_t timeout; 1150 u32 val; 1151 1152 val = ioread32(nhi->iobase + REG_CAPS); 1153 /* Reset only v2 and later routers */ 1154 if (FIELD_GET(REG_CAPS_VERSION_MASK, val) < REG_CAPS_VERSION_2) 1155 return; 1156 1157 if (!host_reset) { 1158 dev_dbg(nhi->dev, "skipping host router reset\n"); 1159 return; 1160 } 1161 1162 iowrite32(REG_RESET_HRR, nhi->iobase + REG_RESET); 1163 msleep(100); 1164 1165 timeout = ktime_add_ms(ktime_get(), 500); 1166 do { 1167 val = ioread32(nhi->iobase + REG_RESET); 1168 if (!(val & REG_RESET_HRR)) { 1169 dev_warn(nhi->dev, "host router reset successful\n"); 1170 return; 1171 } 1172 usleep_range(10, 20); 1173 } while (ktime_before(ktime_get(), timeout)); 1174 1175 dev_warn(nhi->dev, "timeout resetting host router\n"); 1176 } 1177 1178 /** 1179 * nhi_reset_interface() - Reset the host interface 1180 * @nhi: Host interface to reset 1181 * 1182 * Brings the registers in the memory BAR back to their default state and 1183 * clears the End-to-End Flow Control state. The caller is responsible for 1184 * stopping the control channel over the reset because it clears the ring 1185 * state as well. 1186 */ 1187 void nhi_reset_interface(struct tb_nhi *nhi) 1188 { 1189 u32 val; 1190 1191 val = ioread32(nhi->iobase + REG_CAPS); 1192 /* Only v1 host interfaces implement the reset */ 1193 if (FIELD_GET(REG_CAPS_VERSION_MASK, val) >= REG_CAPS_VERSION_2) 1194 return; 1195 1196 dev_dbg(nhi->dev, "issuing host interface reset\n"); 1197 1198 iowrite32(REG_HOST_INTERFACE_RESET_RST, 1199 nhi->iobase + REG_HOST_INTERFACE_RESET); 1200 /* Wait for tHIReset (10 ms) to complete */ 1201 usleep_range(10000, 20000); 1202 } 1203 1204 static struct tb *nhi_select_cm(struct tb_nhi *nhi) 1205 { 1206 struct tb *tb; 1207 1208 /* 1209 * USB4 case is simple. If we got control of any of the 1210 * capabilities, we use software CM. 1211 */ 1212 if (tb_acpi_is_native()) 1213 return tb_probe(nhi); 1214 1215 /* 1216 * Either firmware based CM is running (we did not get control 1217 * from the firmware) or this is pre-USB4 PC so try first 1218 * firmware CM and then fallback to software CM. 1219 */ 1220 tb = icm_probe(nhi); 1221 if (!tb) 1222 tb = tb_probe(nhi); 1223 1224 return tb; 1225 } 1226 1227 int nhi_probe(struct tb_nhi *nhi) 1228 { 1229 struct device *dev = nhi->dev; 1230 struct tb *tb; 1231 int res; 1232 1233 if (!nhi->ops) 1234 return dev_err_probe(dev, -EINVAL, "NHI ops not set\n"); 1235 1236 if (!nhi->ops->init_interrupts) 1237 return dev_err_probe(dev, -EINVAL, "missing required NHI ops\n"); 1238 1239 nhi->hop_count = ioread32(nhi->iobase + REG_CAPS) & 0x3ff; 1240 dev_dbg(dev, "total paths: %d\n", nhi->hop_count); 1241 1242 nhi->tx_rings = devm_kcalloc(dev, nhi->hop_count, 1243 sizeof(*nhi->tx_rings), GFP_KERNEL); 1244 nhi->rx_rings = devm_kcalloc(dev, nhi->hop_count, 1245 sizeof(*nhi->rx_rings), GFP_KERNEL); 1246 if (!nhi->tx_rings || !nhi->rx_rings) 1247 return -ENOMEM; 1248 1249 nhi_reset(nhi); 1250 1251 /* In case someone left them on. */ 1252 nhi_disable_interrupts(nhi); 1253 1254 res = nhi->ops->init_interrupts(nhi); 1255 if (res) 1256 return dev_err_probe(dev, res, "cannot enable interrupts, aborting\n"); 1257 1258 spin_lock_init(&nhi->lock); 1259 1260 res = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)); 1261 if (res) 1262 return dev_err_probe(dev, res, "failed to set DMA mask\n"); 1263 1264 if (nhi->ops->init) { 1265 res = nhi->ops->init(nhi); 1266 if (res) 1267 return dev_err_probe(dev, res, "NHI specific init failed\n"); 1268 } 1269 1270 init_completion(&nhi->domain_released); 1271 1272 tb = nhi_select_cm(nhi); 1273 if (!tb) 1274 return dev_err_probe(dev, -ENODEV, 1275 "failed to determine connection manager, aborting\n"); 1276 1277 dev_dbg(dev, "NHI initialized, starting thunderbolt\n"); 1278 1279 nhi->host_reset = host_reset; 1280 1281 res = tb_domain_add(tb, host_reset); 1282 if (res) { 1283 /* 1284 * At this point the RX/TX rings might already have been 1285 * activated. Do a proper shutdown. 1286 */ 1287 tb_domain_put(tb); 1288 wait_for_completion(&nhi->domain_released); 1289 nhi_shutdown(nhi); 1290 return dev_err_probe(dev, res, "failed to add domain\n"); 1291 } 1292 dev_set_drvdata(dev, tb); 1293 1294 device_wakeup_enable(dev); 1295 1296 pm_runtime_allow(dev); 1297 pm_runtime_set_autosuspend_delay(dev, TB_AUTOSUSPEND_DELAY); 1298 pm_runtime_use_autosuspend(dev); 1299 pm_runtime_put_autosuspend(dev); 1300 1301 return 0; 1302 } 1303 1304 /* 1305 * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable 1306 * the tunnels asap. A corresponding pci quirk blocks the downstream bridges 1307 * resume_noirq until we are done. 1308 */ 1309 const struct dev_pm_ops nhi_pm_ops = { 1310 .suspend_noirq = nhi_suspend_noirq, 1311 .resume_noirq = nhi_resume_noirq, 1312 .freeze_noirq = nhi_freeze_noirq, /* 1313 * we just disable hotplug, the 1314 * pci-tunnels stay alive. 1315 */ 1316 .thaw_noirq = nhi_thaw_noirq, 1317 .restore_noirq = nhi_resume_noirq, 1318 .suspend = nhi_suspend, 1319 .poweroff_noirq = nhi_poweroff_noirq, 1320 .poweroff = nhi_suspend, 1321 .complete = nhi_complete, 1322 .runtime_suspend = nhi_runtime_suspend, 1323 .runtime_resume = nhi_runtime_resume, 1324 }; 1325