1 // SPDX-License-Identifier: GPL-2.0-only
3 * Thunderbolt driver - NHI driver
5 * The NHI (native host interface) is the pci device that allows us to send and
6 * receive frames from the thunderbolt bus.
8 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
9 * Copyright (C) 2018, Intel Corporation
12 #include <linux/pm_runtime.h>
13 #include <linux/slab.h>
14 #include <linux/errno.h>
15 #include <linux/pci.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/interrupt.h>
18 #include <linux/iommu.h>
19 #include <linux/module.h>
20 #include <linux/delay.h>
21 #include <linux/property.h>
22 #include <linux/string_helpers.h>
28 #define RING_TYPE(ring) ((ring)->is_tx ? "TX ring" : "RX ring")
30 #define RING_FIRST_USABLE_HOPID 1
32 * Used with QUIRK_E2E to specify an unused HopID the Rx credits are
35 #define RING_E2E_RESERVED_HOPID RING_FIRST_USABLE_HOPID
37 * Minimal number of vectors when we use MSI-X. Two for control channel
38 * Rx/Tx and the rest four are for cross domain DMA paths.
40 #define MSIX_MIN_VECS 6
41 #define MSIX_MAX_VECS 16
43 #define NHI_MAILBOX_TIMEOUT 500 /* ms */
45 /* Host interface quirks */
46 #define QUIRK_AUTO_CLEAR_INT BIT(0)
47 #define QUIRK_E2E BIT(1)
49 static int ring_interrupt_index(const struct tb_ring *ring)
53 bit += ring->nhi->hop_count;
58 * ring_interrupt_active() - activate/deactivate interrupts for a single ring
60 * ring->nhi->lock must be held.
62 static void ring_interrupt_active(struct tb_ring *ring, bool active)
64 int reg = REG_RING_INTERRUPT_BASE +
65 ring_interrupt_index(ring) / 32 * 4;
66 int interrupt_bit = ring_interrupt_index(ring) & 31;
67 int mask = 1 << interrupt_bit;
71 u32 step, shift, ivr, misc;
72 void __iomem *ivr_base;
79 index = ring->hop + ring->nhi->hop_count;
82 * Intel routers support a bit that isn't part of
83 * the USB4 spec to ask the hardware to clear
84 * interrupt status bits automatically since
85 * we already know which interrupt was triggered.
87 * Other routers explicitly disable auto-clear
88 * to prevent conditions that may occur where two
89 * MSIX interrupts are simultaneously active and
90 * reading the register clears both of them.
92 misc = ioread32(ring->nhi->iobase + REG_DMA_MISC);
93 if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT)
94 auto_clear_bit = REG_DMA_MISC_INT_AUTO_CLEAR;
96 auto_clear_bit = REG_DMA_MISC_DISABLE_AUTO_CLEAR;
97 if (!(misc & auto_clear_bit))
98 iowrite32(misc | auto_clear_bit,
99 ring->nhi->iobase + REG_DMA_MISC);
101 ivr_base = ring->nhi->iobase + REG_INT_VEC_ALLOC_BASE;
102 step = index / REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
103 shift = index % REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
104 ivr = ioread32(ivr_base + step);
105 ivr &= ~(REG_INT_VEC_ALLOC_MASK << shift);
107 ivr |= ring->vector << shift;
108 iowrite32(ivr, ivr_base + step);
111 old = ioread32(ring->nhi->iobase + reg);
117 dev_dbg(&ring->nhi->pdev->dev,
118 "%s interrupt at register %#x bit %d (%#x -> %#x)\n",
119 active ? "enabling" : "disabling", reg, interrupt_bit, old, new);
122 dev_WARN(&ring->nhi->pdev->dev,
123 "interrupt for %s %d is already %s\n",
124 RING_TYPE(ring), ring->hop,
125 active ? "enabled" : "disabled");
126 iowrite32(new, ring->nhi->iobase + reg);
130 * nhi_disable_interrupts() - disable interrupts for all rings
132 * Use only during init and shutdown.
134 static void nhi_disable_interrupts(struct tb_nhi *nhi)
137 /* disable interrupts */
138 for (i = 0; i < RING_INTERRUPT_REG_COUNT(nhi); i++)
139 iowrite32(0, nhi->iobase + REG_RING_INTERRUPT_BASE + 4 * i);
141 /* clear interrupt status bits */
142 for (i = 0; i < RING_NOTIFY_REG_COUNT(nhi); i++)
143 ioread32(nhi->iobase + REG_RING_NOTIFY_BASE + 4 * i);
146 /* ring helper methods */
148 static void __iomem *ring_desc_base(struct tb_ring *ring)
150 void __iomem *io = ring->nhi->iobase;
151 io += ring->is_tx ? REG_TX_RING_BASE : REG_RX_RING_BASE;
152 io += ring->hop * 16;
156 static void __iomem *ring_options_base(struct tb_ring *ring)
158 void __iomem *io = ring->nhi->iobase;
159 io += ring->is_tx ? REG_TX_OPTIONS_BASE : REG_RX_OPTIONS_BASE;
160 io += ring->hop * 32;
164 static void ring_iowrite_cons(struct tb_ring *ring, u16 cons)
167 * The other 16-bits in the register is read-only and writes to it
168 * are ignored by the hardware so we can save one ioread32() by
169 * filling the read-only bits with zeroes.
171 iowrite32(cons, ring_desc_base(ring) + 8);
174 static void ring_iowrite_prod(struct tb_ring *ring, u16 prod)
176 /* See ring_iowrite_cons() above for explanation */
177 iowrite32(prod << 16, ring_desc_base(ring) + 8);
180 static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset)
182 iowrite32(value, ring_desc_base(ring) + offset);
185 static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset)
187 iowrite32(value, ring_desc_base(ring) + offset);
188 iowrite32(value >> 32, ring_desc_base(ring) + offset + 4);
191 static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset)
193 iowrite32(value, ring_options_base(ring) + offset);
196 static bool ring_full(struct tb_ring *ring)
198 return ((ring->head + 1) % ring->size) == ring->tail;
201 static bool ring_empty(struct tb_ring *ring)
203 return ring->head == ring->tail;
207 * ring_write_descriptors() - post frames from ring->queue to the controller
209 * ring->lock is held.
211 static void ring_write_descriptors(struct tb_ring *ring)
213 struct ring_frame *frame, *n;
214 struct ring_desc *descriptor;
215 list_for_each_entry_safe(frame, n, &ring->queue, list) {
218 list_move_tail(&frame->list, &ring->in_flight);
219 descriptor = &ring->descriptors[ring->head];
220 descriptor->phys = frame->buffer_phy;
221 descriptor->time = 0;
222 descriptor->flags = RING_DESC_POSTED | RING_DESC_INTERRUPT;
224 descriptor->length = frame->size;
225 descriptor->eof = frame->eof;
226 descriptor->sof = frame->sof;
228 ring->head = (ring->head + 1) % ring->size;
230 ring_iowrite_prod(ring, ring->head);
232 ring_iowrite_cons(ring, ring->head);
237 * ring_work() - progress completed frames
239 * If the ring is shutting down then all frames are marked as canceled and
240 * their callbacks are invoked.
242 * Otherwise we collect all completed frame from the ring buffer, write new
243 * frame to the ring buffer and invoke the callbacks for the completed frames.
245 static void ring_work(struct work_struct *work)
247 struct tb_ring *ring = container_of(work, typeof(*ring), work);
248 struct ring_frame *frame;
249 bool canceled = false;
253 spin_lock_irqsave(&ring->lock, flags);
255 if (!ring->running) {
256 /* Move all frames to done and mark them as canceled. */
257 list_splice_tail_init(&ring->in_flight, &done);
258 list_splice_tail_init(&ring->queue, &done);
260 goto invoke_callback;
263 while (!ring_empty(ring)) {
264 if (!(ring->descriptors[ring->tail].flags
265 & RING_DESC_COMPLETED))
267 frame = list_first_entry(&ring->in_flight, typeof(*frame),
269 list_move_tail(&frame->list, &done);
271 frame->size = ring->descriptors[ring->tail].length;
272 frame->eof = ring->descriptors[ring->tail].eof;
273 frame->sof = ring->descriptors[ring->tail].sof;
274 frame->flags = ring->descriptors[ring->tail].flags;
276 ring->tail = (ring->tail + 1) % ring->size;
278 ring_write_descriptors(ring);
281 /* allow callbacks to schedule new work */
282 spin_unlock_irqrestore(&ring->lock, flags);
283 while (!list_empty(&done)) {
284 frame = list_first_entry(&done, typeof(*frame), list);
286 * The callback may reenqueue or delete frame.
287 * Do not hold on to it.
289 list_del_init(&frame->list);
291 frame->callback(ring, frame, canceled);
295 int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame)
300 spin_lock_irqsave(&ring->lock, flags);
302 list_add_tail(&frame->list, &ring->queue);
303 ring_write_descriptors(ring);
307 spin_unlock_irqrestore(&ring->lock, flags);
310 EXPORT_SYMBOL_GPL(__tb_ring_enqueue);
313 * tb_ring_poll() - Poll one completed frame from the ring
314 * @ring: Ring to poll
316 * This function can be called when @start_poll callback of the @ring
317 * has been called. It will read one completed frame from the ring and
318 * return it to the caller. Returns %NULL if there is no more completed
321 struct ring_frame *tb_ring_poll(struct tb_ring *ring)
323 struct ring_frame *frame = NULL;
326 spin_lock_irqsave(&ring->lock, flags);
329 if (ring_empty(ring))
332 if (ring->descriptors[ring->tail].flags & RING_DESC_COMPLETED) {
333 frame = list_first_entry(&ring->in_flight, typeof(*frame),
335 list_del_init(&frame->list);
338 frame->size = ring->descriptors[ring->tail].length;
339 frame->eof = ring->descriptors[ring->tail].eof;
340 frame->sof = ring->descriptors[ring->tail].sof;
341 frame->flags = ring->descriptors[ring->tail].flags;
344 ring->tail = (ring->tail + 1) % ring->size;
348 spin_unlock_irqrestore(&ring->lock, flags);
351 EXPORT_SYMBOL_GPL(tb_ring_poll);
353 static void __ring_interrupt_mask(struct tb_ring *ring, bool mask)
355 int idx = ring_interrupt_index(ring);
356 int reg = REG_RING_INTERRUPT_BASE + idx / 32 * 4;
360 val = ioread32(ring->nhi->iobase + reg);
365 iowrite32(val, ring->nhi->iobase + reg);
368 /* Both @nhi->lock and @ring->lock should be held */
369 static void __ring_interrupt(struct tb_ring *ring)
374 if (ring->start_poll) {
375 __ring_interrupt_mask(ring, true);
376 ring->start_poll(ring->poll_data);
378 schedule_work(&ring->work);
383 * tb_ring_poll_complete() - Re-start interrupt for the ring
384 * @ring: Ring to re-start the interrupt
386 * This will re-start (unmask) the ring interrupt once the user is done
389 void tb_ring_poll_complete(struct tb_ring *ring)
393 spin_lock_irqsave(&ring->nhi->lock, flags);
394 spin_lock(&ring->lock);
395 if (ring->start_poll)
396 __ring_interrupt_mask(ring, false);
397 spin_unlock(&ring->lock);
398 spin_unlock_irqrestore(&ring->nhi->lock, flags);
400 EXPORT_SYMBOL_GPL(tb_ring_poll_complete);
402 static void ring_clear_msix(const struct tb_ring *ring)
406 if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT)
409 bit = ring_interrupt_index(ring) & 31;
411 iowrite32(BIT(bit), ring->nhi->iobase + REG_RING_INT_CLEAR);
413 iowrite32(BIT(bit), ring->nhi->iobase + REG_RING_INT_CLEAR +
414 4 * (ring->nhi->hop_count / 32));
417 static irqreturn_t ring_msix(int irq, void *data)
419 struct tb_ring *ring = data;
421 spin_lock(&ring->nhi->lock);
422 ring_clear_msix(ring);
423 spin_lock(&ring->lock);
424 __ring_interrupt(ring);
425 spin_unlock(&ring->lock);
426 spin_unlock(&ring->nhi->lock);
431 static int ring_request_msix(struct tb_ring *ring, bool no_suspend)
433 struct tb_nhi *nhi = ring->nhi;
434 unsigned long irqflags;
437 if (!nhi->pdev->msix_enabled)
440 ret = ida_simple_get(&nhi->msix_ida, 0, MSIX_MAX_VECS, GFP_KERNEL);
446 ret = pci_irq_vector(ring->nhi->pdev, ring->vector);
452 irqflags = no_suspend ? IRQF_NO_SUSPEND : 0;
453 ret = request_irq(ring->irq, ring_msix, irqflags, "thunderbolt", ring);
460 ida_simple_remove(&nhi->msix_ida, ring->vector);
465 static void ring_release_msix(struct tb_ring *ring)
470 free_irq(ring->irq, ring);
471 ida_simple_remove(&ring->nhi->msix_ida, ring->vector);
476 static int nhi_alloc_hop(struct tb_nhi *nhi, struct tb_ring *ring)
478 unsigned int start_hop = RING_FIRST_USABLE_HOPID;
481 if (nhi->quirks & QUIRK_E2E) {
482 start_hop = RING_FIRST_USABLE_HOPID + 1;
483 if (ring->flags & RING_FLAG_E2E && !ring->is_tx) {
484 dev_dbg(&nhi->pdev->dev, "quirking E2E TX HopID %u -> %u\n",
485 ring->e2e_tx_hop, RING_E2E_RESERVED_HOPID);
486 ring->e2e_tx_hop = RING_E2E_RESERVED_HOPID;
490 spin_lock_irq(&nhi->lock);
496 * Automatically allocate HopID from the non-reserved
497 * range 1 .. hop_count - 1.
499 for (i = start_hop; i < nhi->hop_count; i++) {
501 if (!nhi->tx_rings[i]) {
506 if (!nhi->rx_rings[i]) {
514 if (ring->hop > 0 && ring->hop < start_hop) {
515 dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop);
519 if (ring->hop < 0 || ring->hop >= nhi->hop_count) {
520 dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop);
524 if (ring->is_tx && nhi->tx_rings[ring->hop]) {
525 dev_warn(&nhi->pdev->dev, "TX hop %d already allocated\n",
529 } else if (!ring->is_tx && nhi->rx_rings[ring->hop]) {
530 dev_warn(&nhi->pdev->dev, "RX hop %d already allocated\n",
537 nhi->tx_rings[ring->hop] = ring;
539 nhi->rx_rings[ring->hop] = ring;
542 spin_unlock_irq(&nhi->lock);
547 static struct tb_ring *tb_ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
548 bool transmit, unsigned int flags,
549 int e2e_tx_hop, u16 sof_mask, u16 eof_mask,
550 void (*start_poll)(void *),
553 struct tb_ring *ring = NULL;
555 dev_dbg(&nhi->pdev->dev, "allocating %s ring %d of size %d\n",
556 transmit ? "TX" : "RX", hop, size);
558 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
562 spin_lock_init(&ring->lock);
563 INIT_LIST_HEAD(&ring->queue);
564 INIT_LIST_HEAD(&ring->in_flight);
565 INIT_WORK(&ring->work, ring_work);
569 ring->is_tx = transmit;
572 ring->e2e_tx_hop = e2e_tx_hop;
573 ring->sof_mask = sof_mask;
574 ring->eof_mask = eof_mask;
577 ring->running = false;
578 ring->start_poll = start_poll;
579 ring->poll_data = poll_data;
581 ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev,
582 size * sizeof(*ring->descriptors),
583 &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO);
584 if (!ring->descriptors)
587 if (ring_request_msix(ring, flags & RING_FLAG_NO_SUSPEND))
590 if (nhi_alloc_hop(nhi, ring))
591 goto err_release_msix;
596 ring_release_msix(ring);
598 dma_free_coherent(&ring->nhi->pdev->dev,
599 ring->size * sizeof(*ring->descriptors),
600 ring->descriptors, ring->descriptors_dma);
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
614 struct tb_ring *tb_ring_alloc_tx(struct tb_nhi *nhi, int hop, int size,
617 return tb_ring_alloc(nhi, hop, size, true, flags, 0, 0, 0, NULL, NULL);
619 EXPORT_SYMBOL_GPL(tb_ring_alloc_tx);
622 * tb_ring_alloc_rx() - Allocate DMA ring for receive
623 * @nhi: Pointer to the NHI the ring is to be allocated
624 * @hop: HopID (ring) to allocate. Pass %-1 for automatic allocation.
625 * @size: Number of entries in the ring
626 * @flags: Flags for the ring
627 * @e2e_tx_hop: Transmit HopID when E2E is enabled in @flags
628 * @sof_mask: Mask of PDF values that start a frame
629 * @eof_mask: Mask of PDF values that end a frame
630 * @start_poll: If not %NULL the ring will call this function when an
631 * interrupt is triggered and masked, instead of callback
633 * @poll_data: Optional data passed to @start_poll
635 struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size,
636 unsigned int flags, int e2e_tx_hop,
637 u16 sof_mask, u16 eof_mask,
638 void (*start_poll)(void *), void *poll_data)
640 return tb_ring_alloc(nhi, hop, size, false, flags, e2e_tx_hop, sof_mask, eof_mask,
641 start_poll, poll_data);
643 EXPORT_SYMBOL_GPL(tb_ring_alloc_rx);
646 * tb_ring_start() - enable a ring
647 * @ring: Ring to start
649 * Must not be invoked in parallel with tb_ring_stop().
651 void tb_ring_start(struct tb_ring *ring)
656 spin_lock_irq(&ring->nhi->lock);
657 spin_lock(&ring->lock);
658 if (ring->nhi->going_away)
661 dev_WARN(&ring->nhi->pdev->dev, "ring already started\n");
664 dev_dbg(&ring->nhi->pdev->dev, "starting %s %d\n",
665 RING_TYPE(ring), ring->hop);
667 if (ring->flags & RING_FLAG_FRAME) {
670 flags = RING_FLAG_ENABLE;
672 frame_size = TB_FRAME_SIZE;
673 flags = RING_FLAG_ENABLE | RING_FLAG_RAW;
676 ring_iowrite64desc(ring, ring->descriptors_dma, 0);
678 ring_iowrite32desc(ring, ring->size, 12);
679 ring_iowrite32options(ring, 0, 4); /* time releated ? */
680 ring_iowrite32options(ring, flags, 0);
682 u32 sof_eof_mask = ring->sof_mask << 16 | ring->eof_mask;
684 ring_iowrite32desc(ring, (frame_size << 16) | ring->size, 12);
685 ring_iowrite32options(ring, sof_eof_mask, 4);
686 ring_iowrite32options(ring, flags, 0);
690 * Now that the ring valid bit is set we can configure E2E if
691 * enabled for the ring.
693 if (ring->flags & RING_FLAG_E2E) {
697 hop = ring->e2e_tx_hop << REG_RX_OPTIONS_E2E_HOP_SHIFT;
698 hop &= REG_RX_OPTIONS_E2E_HOP_MASK;
701 dev_dbg(&ring->nhi->pdev->dev,
702 "enabling E2E for %s %d with TX HopID %d\n",
703 RING_TYPE(ring), ring->hop, ring->e2e_tx_hop);
705 dev_dbg(&ring->nhi->pdev->dev, "enabling E2E for %s %d\n",
706 RING_TYPE(ring), ring->hop);
709 flags |= RING_FLAG_E2E_FLOW_CONTROL;
710 ring_iowrite32options(ring, flags, 0);
713 ring_interrupt_active(ring, true);
714 ring->running = true;
716 spin_unlock(&ring->lock);
717 spin_unlock_irq(&ring->nhi->lock);
719 EXPORT_SYMBOL_GPL(tb_ring_start);
722 * tb_ring_stop() - shutdown a ring
723 * @ring: Ring to stop
725 * Must not be invoked from a callback.
727 * This method will disable the ring. Further calls to
728 * tb_ring_tx/tb_ring_rx will return -ESHUTDOWN until ring_stop has been
731 * All enqueued frames will be canceled and their callbacks will be executed
732 * with frame->canceled set to true (on the callback thread). This method
733 * returns only after all callback invocations have finished.
735 void tb_ring_stop(struct tb_ring *ring)
737 spin_lock_irq(&ring->nhi->lock);
738 spin_lock(&ring->lock);
739 dev_dbg(&ring->nhi->pdev->dev, "stopping %s %d\n",
740 RING_TYPE(ring), ring->hop);
741 if (ring->nhi->going_away)
743 if (!ring->running) {
744 dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n",
745 RING_TYPE(ring), ring->hop);
748 ring_interrupt_active(ring, false);
750 ring_iowrite32options(ring, 0, 0);
751 ring_iowrite64desc(ring, 0, 0);
752 ring_iowrite32desc(ring, 0, 8);
753 ring_iowrite32desc(ring, 0, 12);
756 ring->running = false;
759 spin_unlock(&ring->lock);
760 spin_unlock_irq(&ring->nhi->lock);
763 * schedule ring->work to invoke callbacks on all remaining frames.
765 schedule_work(&ring->work);
766 flush_work(&ring->work);
768 EXPORT_SYMBOL_GPL(tb_ring_stop);
771 * tb_ring_free() - free ring
773 * When this method returns all invocations of ring->callback will have
776 * Ring must be stopped.
778 * Must NOT be called from ring_frame->callback!
780 void tb_ring_free(struct tb_ring *ring)
782 spin_lock_irq(&ring->nhi->lock);
784 * Dissociate the ring from the NHI. This also ensures that
785 * nhi_interrupt_work cannot reschedule ring->work.
788 ring->nhi->tx_rings[ring->hop] = NULL;
790 ring->nhi->rx_rings[ring->hop] = NULL;
793 dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n",
794 RING_TYPE(ring), ring->hop);
796 spin_unlock_irq(&ring->nhi->lock);
798 ring_release_msix(ring);
800 dma_free_coherent(&ring->nhi->pdev->dev,
801 ring->size * sizeof(*ring->descriptors),
802 ring->descriptors, ring->descriptors_dma);
804 ring->descriptors = NULL;
805 ring->descriptors_dma = 0;
808 dev_dbg(&ring->nhi->pdev->dev, "freeing %s %d\n", RING_TYPE(ring),
812 * ring->work can no longer be scheduled (it is scheduled only
813 * by nhi_interrupt_work, ring_stop and ring_msix). Wait for it
814 * to finish before freeing the ring.
816 flush_work(&ring->work);
819 EXPORT_SYMBOL_GPL(tb_ring_free);
822 * nhi_mailbox_cmd() - Send a command through NHI mailbox
823 * @nhi: Pointer to the NHI structure
824 * @cmd: Command to send
825 * @data: Data to be send with the command
827 * Sends mailbox command to the firmware running on NHI. Returns %0 in
828 * case of success and negative errno in case of failure.
830 int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data)
835 iowrite32(data, nhi->iobase + REG_INMAIL_DATA);
837 val = ioread32(nhi->iobase + REG_INMAIL_CMD);
838 val &= ~(REG_INMAIL_CMD_MASK | REG_INMAIL_ERROR);
839 val |= REG_INMAIL_OP_REQUEST | cmd;
840 iowrite32(val, nhi->iobase + REG_INMAIL_CMD);
842 timeout = ktime_add_ms(ktime_get(), NHI_MAILBOX_TIMEOUT);
844 val = ioread32(nhi->iobase + REG_INMAIL_CMD);
845 if (!(val & REG_INMAIL_OP_REQUEST))
847 usleep_range(10, 20);
848 } while (ktime_before(ktime_get(), timeout));
850 if (val & REG_INMAIL_OP_REQUEST)
852 if (val & REG_INMAIL_ERROR)
859 * nhi_mailbox_mode() - Return current firmware operation mode
860 * @nhi: Pointer to the NHI structure
862 * The function reads current firmware operation mode using NHI mailbox
863 * registers and returns it to the caller.
865 enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi)
869 val = ioread32(nhi->iobase + REG_OUTMAIL_CMD);
870 val &= REG_OUTMAIL_CMD_OPMODE_MASK;
871 val >>= REG_OUTMAIL_CMD_OPMODE_SHIFT;
873 return (enum nhi_fw_mode)val;
876 static void nhi_interrupt_work(struct work_struct *work)
878 struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work);
879 int value = 0; /* Suppress uninitialized usage warning. */
882 int type = 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */
883 struct tb_ring *ring;
885 spin_lock_irq(&nhi->lock);
888 * Starting at REG_RING_NOTIFY_BASE there are three status bitfields
889 * (TX, RX, RX overflow). We iterate over the bits and read a new
890 * dwords as required. The registers are cleared on read.
892 for (bit = 0; bit < 3 * nhi->hop_count; bit++) {
894 value = ioread32(nhi->iobase
895 + REG_RING_NOTIFY_BASE
897 if (++hop == nhi->hop_count) {
901 if ((value & (1 << (bit % 32))) == 0)
904 dev_warn(&nhi->pdev->dev,
905 "RX overflow for ring %d\n",
910 ring = nhi->tx_rings[hop];
912 ring = nhi->rx_rings[hop];
914 dev_warn(&nhi->pdev->dev,
915 "got interrupt for inactive %s ring %d\n",
921 spin_lock(&ring->lock);
922 __ring_interrupt(ring);
923 spin_unlock(&ring->lock);
925 spin_unlock_irq(&nhi->lock);
928 static irqreturn_t nhi_msi(int irq, void *data)
930 struct tb_nhi *nhi = data;
931 schedule_work(&nhi->interrupt_work);
935 static int __nhi_suspend_noirq(struct device *dev, bool wakeup)
937 struct pci_dev *pdev = to_pci_dev(dev);
938 struct tb *tb = pci_get_drvdata(pdev);
939 struct tb_nhi *nhi = tb->nhi;
942 ret = tb_domain_suspend_noirq(tb);
946 if (nhi->ops && nhi->ops->suspend_noirq) {
947 ret = nhi->ops->suspend_noirq(tb->nhi, wakeup);
955 static int nhi_suspend_noirq(struct device *dev)
957 return __nhi_suspend_noirq(dev, device_may_wakeup(dev));
960 static int nhi_freeze_noirq(struct device *dev)
962 struct pci_dev *pdev = to_pci_dev(dev);
963 struct tb *tb = pci_get_drvdata(pdev);
965 return tb_domain_freeze_noirq(tb);
968 static int nhi_thaw_noirq(struct device *dev)
970 struct pci_dev *pdev = to_pci_dev(dev);
971 struct tb *tb = pci_get_drvdata(pdev);
973 return tb_domain_thaw_noirq(tb);
976 static bool nhi_wake_supported(struct pci_dev *pdev)
981 * If power rails are sustainable for wakeup from S4 this
982 * property is set by the BIOS.
984 if (device_property_read_u8(&pdev->dev, "WAKE_SUPPORTED", &val))
990 static int nhi_poweroff_noirq(struct device *dev)
992 struct pci_dev *pdev = to_pci_dev(dev);
995 wakeup = device_may_wakeup(dev) && nhi_wake_supported(pdev);
996 return __nhi_suspend_noirq(dev, wakeup);
999 static void nhi_enable_int_throttling(struct tb_nhi *nhi)
1001 /* Throttling is specified in 256ns increments */
1002 u32 throttle = DIV_ROUND_UP(128 * NSEC_PER_USEC, 256);
1006 * Configure interrupt throttling for all vectors even if we
1009 for (i = 0; i < MSIX_MAX_VECS; i++) {
1010 u32 reg = REG_INT_THROTTLING_RATE + i * 4;
1011 iowrite32(throttle, nhi->iobase + reg);
1015 static int nhi_resume_noirq(struct device *dev)
1017 struct pci_dev *pdev = to_pci_dev(dev);
1018 struct tb *tb = pci_get_drvdata(pdev);
1019 struct tb_nhi *nhi = tb->nhi;
1023 * Check that the device is still there. It may be that the user
1024 * unplugged last device which causes the host controller to go
1027 if (!pci_device_is_present(pdev)) {
1028 nhi->going_away = true;
1030 if (nhi->ops && nhi->ops->resume_noirq) {
1031 ret = nhi->ops->resume_noirq(nhi);
1035 nhi_enable_int_throttling(tb->nhi);
1038 return tb_domain_resume_noirq(tb);
1041 static int nhi_suspend(struct device *dev)
1043 struct pci_dev *pdev = to_pci_dev(dev);
1044 struct tb *tb = pci_get_drvdata(pdev);
1046 return tb_domain_suspend(tb);
1049 static void nhi_complete(struct device *dev)
1051 struct pci_dev *pdev = to_pci_dev(dev);
1052 struct tb *tb = pci_get_drvdata(pdev);
1055 * If we were runtime suspended when system suspend started,
1056 * schedule runtime resume now. It should bring the domain back
1057 * to functional state.
1059 if (pm_runtime_suspended(&pdev->dev))
1060 pm_runtime_resume(&pdev->dev);
1062 tb_domain_complete(tb);
1065 static int nhi_runtime_suspend(struct device *dev)
1067 struct pci_dev *pdev = to_pci_dev(dev);
1068 struct tb *tb = pci_get_drvdata(pdev);
1069 struct tb_nhi *nhi = tb->nhi;
1072 ret = tb_domain_runtime_suspend(tb);
1076 if (nhi->ops && nhi->ops->runtime_suspend) {
1077 ret = nhi->ops->runtime_suspend(tb->nhi);
1084 static int nhi_runtime_resume(struct device *dev)
1086 struct pci_dev *pdev = to_pci_dev(dev);
1087 struct tb *tb = pci_get_drvdata(pdev);
1088 struct tb_nhi *nhi = tb->nhi;
1091 if (nhi->ops && nhi->ops->runtime_resume) {
1092 ret = nhi->ops->runtime_resume(nhi);
1097 nhi_enable_int_throttling(nhi);
1098 return tb_domain_runtime_resume(tb);
1101 static void nhi_shutdown(struct tb_nhi *nhi)
1105 dev_dbg(&nhi->pdev->dev, "shutdown\n");
1107 for (i = 0; i < nhi->hop_count; i++) {
1108 if (nhi->tx_rings[i])
1109 dev_WARN(&nhi->pdev->dev,
1110 "TX ring %d is still active\n", i);
1111 if (nhi->rx_rings[i])
1112 dev_WARN(&nhi->pdev->dev,
1113 "RX ring %d is still active\n", i);
1115 nhi_disable_interrupts(nhi);
1117 * We have to release the irq before calling flush_work. Otherwise an
1118 * already executing IRQ handler could call schedule_work again.
1120 if (!nhi->pdev->msix_enabled) {
1121 devm_free_irq(&nhi->pdev->dev, nhi->pdev->irq, nhi);
1122 flush_work(&nhi->interrupt_work);
1124 ida_destroy(&nhi->msix_ida);
1126 if (nhi->ops && nhi->ops->shutdown)
1127 nhi->ops->shutdown(nhi);
1130 static void nhi_check_quirks(struct tb_nhi *nhi)
1132 if (nhi->pdev->vendor == PCI_VENDOR_ID_INTEL) {
1134 * Intel hardware supports auto clear of the interrupt
1135 * status register right after interrupt is being
1138 nhi->quirks |= QUIRK_AUTO_CLEAR_INT;
1140 switch (nhi->pdev->device) {
1141 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
1142 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
1144 * Falcon Ridge controller needs the end-to-end
1145 * flow control workaround to avoid losing Rx
1146 * packets when RING_FLAG_E2E is set.
1148 nhi->quirks |= QUIRK_E2E;
1154 static int nhi_check_iommu_pdev(struct pci_dev *pdev, void *data)
1156 if (!pdev->external_facing ||
1157 !device_iommu_capable(&pdev->dev, IOMMU_CAP_PRE_BOOT_PROTECTION))
1159 *(bool *)data = true;
1160 return 1; /* Stop walking */
1163 static void nhi_check_iommu(struct tb_nhi *nhi)
1165 struct pci_bus *bus = nhi->pdev->bus;
1166 bool port_ok = false;
1169 * Ideally what we'd do here is grab every PCI device that
1170 * represents a tunnelling adapter for this NHI and check their
1171 * status directly, but unfortunately USB4 seems to make it
1172 * obnoxiously difficult to reliably make any correlation.
1174 * So for now we'll have to bodge it... Hoping that the system
1175 * is at least sane enough that an adapter is in the same PCI
1176 * segment as its NHI, if we can find *something* on that segment
1177 * which meets the requirements for Kernel DMA Protection, we'll
1178 * take that to imply that firmware is aware and has (hopefully)
1179 * done the right thing in general. We need to know that the PCI
1180 * layer has seen the ExternalFacingPort property which will then
1181 * inform the IOMMU layer to enforce the complete "untrusted DMA"
1182 * flow, but also that the IOMMU driver itself can be trusted not
1183 * to have been subverted by a pre-boot DMA attack.
1188 pci_walk_bus(bus, nhi_check_iommu_pdev, &port_ok);
1190 nhi->iommu_dma_protection = port_ok;
1191 dev_dbg(&nhi->pdev->dev, "IOMMU DMA protection is %s\n",
1192 str_enabled_disabled(port_ok));
1195 static int nhi_init_msi(struct tb_nhi *nhi)
1197 struct pci_dev *pdev = nhi->pdev;
1198 struct device *dev = &pdev->dev;
1201 /* In case someone left them on. */
1202 nhi_disable_interrupts(nhi);
1204 nhi_enable_int_throttling(nhi);
1206 ida_init(&nhi->msix_ida);
1209 * The NHI has 16 MSI-X vectors or a single MSI. We first try to
1210 * get all MSI-X vectors and if we succeed, each ring will have
1211 * one MSI-X. If for some reason that does not work out, we
1212 * fallback to a single MSI.
1214 nvec = pci_alloc_irq_vectors(pdev, MSIX_MIN_VECS, MSIX_MAX_VECS,
1217 nvec = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
1221 INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
1223 irq = pci_irq_vector(nhi->pdev, 0);
1227 res = devm_request_irq(&pdev->dev, irq, nhi_msi,
1228 IRQF_NO_SUSPEND, "thunderbolt", nhi);
1230 return dev_err_probe(dev, res, "request_irq failed, aborting\n");
1236 static bool nhi_imr_valid(struct pci_dev *pdev)
1240 if (!device_property_read_u8(&pdev->dev, "IMR_VALID", &val))
1246 static struct tb *nhi_select_cm(struct tb_nhi *nhi)
1251 * USB4 case is simple. If we got control of any of the
1252 * capabilities, we use software CM.
1254 if (tb_acpi_is_native())
1255 return tb_probe(nhi);
1258 * Either firmware based CM is running (we did not get control
1259 * from the firmware) or this is pre-USB4 PC so try first
1260 * firmware CM and then fallback to software CM.
1262 tb = icm_probe(nhi);
1269 static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1271 struct device *dev = &pdev->dev;
1276 if (!nhi_imr_valid(pdev))
1277 return dev_err_probe(dev, -ENODEV, "firmware image not valid, aborting\n");
1279 res = pcim_enable_device(pdev);
1281 return dev_err_probe(dev, res, "cannot enable PCI device, aborting\n");
1283 res = pcim_iomap_regions(pdev, 1 << 0, "thunderbolt");
1285 return dev_err_probe(dev, res, "cannot obtain PCI resources, aborting\n");
1287 nhi = devm_kzalloc(&pdev->dev, sizeof(*nhi), GFP_KERNEL);
1292 nhi->ops = (const struct tb_nhi_ops *)id->driver_data;
1293 /* cannot fail - table is allocated in pcim_iomap_regions */
1294 nhi->iobase = pcim_iomap_table(pdev)[0];
1295 nhi->hop_count = ioread32(nhi->iobase + REG_HOP_COUNT) & 0x3ff;
1296 dev_dbg(dev, "total paths: %d\n", nhi->hop_count);
1298 nhi->tx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
1299 sizeof(*nhi->tx_rings), GFP_KERNEL);
1300 nhi->rx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
1301 sizeof(*nhi->rx_rings), GFP_KERNEL);
1302 if (!nhi->tx_rings || !nhi->rx_rings)
1305 nhi_check_quirks(nhi);
1306 nhi_check_iommu(nhi);
1308 res = nhi_init_msi(nhi);
1310 return dev_err_probe(dev, res, "cannot enable MSI, aborting\n");
1312 spin_lock_init(&nhi->lock);
1314 res = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1316 return dev_err_probe(dev, res, "failed to set DMA mask\n");
1318 pci_set_master(pdev);
1320 if (nhi->ops && nhi->ops->init) {
1321 res = nhi->ops->init(nhi);
1326 tb = nhi_select_cm(nhi);
1328 return dev_err_probe(dev, -ENODEV,
1329 "failed to determine connection manager, aborting\n");
1331 dev_dbg(dev, "NHI initialized, starting thunderbolt\n");
1333 res = tb_domain_add(tb);
1336 * At this point the RX/TX rings might already have been
1337 * activated. Do a proper shutdown.
1343 pci_set_drvdata(pdev, tb);
1345 device_wakeup_enable(&pdev->dev);
1347 pm_runtime_allow(&pdev->dev);
1348 pm_runtime_set_autosuspend_delay(&pdev->dev, TB_AUTOSUSPEND_DELAY);
1349 pm_runtime_use_autosuspend(&pdev->dev);
1350 pm_runtime_put_autosuspend(&pdev->dev);
1355 static void nhi_remove(struct pci_dev *pdev)
1357 struct tb *tb = pci_get_drvdata(pdev);
1358 struct tb_nhi *nhi = tb->nhi;
1360 pm_runtime_get_sync(&pdev->dev);
1361 pm_runtime_dont_use_autosuspend(&pdev->dev);
1362 pm_runtime_forbid(&pdev->dev);
1364 tb_domain_remove(tb);
1369 * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable
1370 * the tunnels asap. A corresponding pci quirk blocks the downstream bridges
1371 * resume_noirq until we are done.
1373 static const struct dev_pm_ops nhi_pm_ops = {
1374 .suspend_noirq = nhi_suspend_noirq,
1375 .resume_noirq = nhi_resume_noirq,
1376 .freeze_noirq = nhi_freeze_noirq, /*
1377 * we just disable hotplug, the
1378 * pci-tunnels stay alive.
1380 .thaw_noirq = nhi_thaw_noirq,
1381 .restore_noirq = nhi_resume_noirq,
1382 .suspend = nhi_suspend,
1383 .poweroff_noirq = nhi_poweroff_noirq,
1384 .poweroff = nhi_suspend,
1385 .complete = nhi_complete,
1386 .runtime_suspend = nhi_runtime_suspend,
1387 .runtime_resume = nhi_runtime_resume,
1390 static struct pci_device_id nhi_ids[] = {
1392 * We have to specify class, the TB bridges use the same device and
1393 * vendor (sub)id on gen 1 and gen 2 controllers.
1396 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1397 .vendor = PCI_VENDOR_ID_INTEL,
1398 .device = PCI_DEVICE_ID_INTEL_LIGHT_RIDGE,
1399 .subvendor = 0x2222, .subdevice = 0x1111,
1402 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1403 .vendor = PCI_VENDOR_ID_INTEL,
1404 .device = PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C,
1405 .subvendor = 0x2222, .subdevice = 0x1111,
1408 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1409 .vendor = PCI_VENDOR_ID_INTEL,
1410 .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI,
1411 .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
1414 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1415 .vendor = PCI_VENDOR_ID_INTEL,
1416 .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI,
1417 .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
1421 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI) },
1422 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI) },
1423 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_USBONLY_NHI) },
1424 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI) },
1425 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_USBONLY_NHI) },
1426 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI) },
1427 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI) },
1428 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_USBONLY_NHI) },
1429 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI) },
1430 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI) },
1431 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICL_NHI0),
1432 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1433 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICL_NHI1),
1434 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1436 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_NHI0),
1437 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1438 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_NHI1),
1439 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1440 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_H_NHI0),
1441 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1442 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_H_NHI1),
1443 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1444 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL_NHI0),
1445 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1446 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL_NHI1),
1447 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1448 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_RPL_NHI0),
1449 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1450 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_RPL_NHI1),
1451 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1452 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTL_M_NHI0),
1453 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1454 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTL_P_NHI0),
1455 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1456 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTL_P_NHI1),
1457 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1459 /* Any USB4 compliant host */
1460 { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_USB4, ~0) },
1465 MODULE_DEVICE_TABLE(pci, nhi_ids);
1466 MODULE_LICENSE("GPL");
1468 static struct pci_driver nhi_driver = {
1469 .name = "thunderbolt",
1470 .id_table = nhi_ids,
1472 .remove = nhi_remove,
1473 .shutdown = nhi_remove,
1474 .driver.pm = &nhi_pm_ops,
1477 static int __init nhi_init(void)
1481 ret = tb_domain_init();
1484 ret = pci_register_driver(&nhi_driver);
1490 static void __exit nhi_unload(void)
1492 pci_unregister_driver(&nhi_driver);
1496 rootfs_initcall(nhi_init);
1497 module_exit(nhi_unload);