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",
530 if (!ring->is_tx && nhi->rx_rings[ring->hop]) {
531 dev_warn(&nhi->pdev->dev, "RX hop %d already allocated\n",
538 nhi->tx_rings[ring->hop] = ring;
540 nhi->rx_rings[ring->hop] = ring;
543 spin_unlock_irq(&nhi->lock);
548 static struct tb_ring *tb_ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
549 bool transmit, unsigned int flags,
550 int e2e_tx_hop, u16 sof_mask, u16 eof_mask,
551 void (*start_poll)(void *),
554 struct tb_ring *ring = NULL;
556 dev_dbg(&nhi->pdev->dev, "allocating %s ring %d of size %d\n",
557 transmit ? "TX" : "RX", hop, size);
559 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
563 spin_lock_init(&ring->lock);
564 INIT_LIST_HEAD(&ring->queue);
565 INIT_LIST_HEAD(&ring->in_flight);
566 INIT_WORK(&ring->work, ring_work);
570 ring->is_tx = transmit;
573 ring->e2e_tx_hop = e2e_tx_hop;
574 ring->sof_mask = sof_mask;
575 ring->eof_mask = eof_mask;
578 ring->running = false;
579 ring->start_poll = start_poll;
580 ring->poll_data = poll_data;
582 ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev,
583 size * sizeof(*ring->descriptors),
584 &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO);
585 if (!ring->descriptors)
588 if (ring_request_msix(ring, flags & RING_FLAG_NO_SUSPEND))
591 if (nhi_alloc_hop(nhi, ring))
592 goto err_release_msix;
597 ring_release_msix(ring);
599 dma_free_coherent(&ring->nhi->pdev->dev,
600 ring->size * sizeof(*ring->descriptors),
601 ring->descriptors, ring->descriptors_dma);
609 * tb_ring_alloc_tx() - Allocate DMA ring for transmit
610 * @nhi: Pointer to the NHI the ring is to be allocated
611 * @hop: HopID (ring) to allocate
612 * @size: Number of entries in the ring
613 * @flags: Flags for the ring
615 struct tb_ring *tb_ring_alloc_tx(struct tb_nhi *nhi, int hop, int size,
618 return tb_ring_alloc(nhi, hop, size, true, flags, 0, 0, 0, NULL, NULL);
620 EXPORT_SYMBOL_GPL(tb_ring_alloc_tx);
623 * tb_ring_alloc_rx() - Allocate DMA ring for receive
624 * @nhi: Pointer to the NHI the ring is to be allocated
625 * @hop: HopID (ring) to allocate. Pass %-1 for automatic allocation.
626 * @size: Number of entries in the ring
627 * @flags: Flags for the ring
628 * @e2e_tx_hop: Transmit HopID when E2E is enabled in @flags
629 * @sof_mask: Mask of PDF values that start a frame
630 * @eof_mask: Mask of PDF values that end a frame
631 * @start_poll: If not %NULL the ring will call this function when an
632 * interrupt is triggered and masked, instead of callback
634 * @poll_data: Optional data passed to @start_poll
636 struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size,
637 unsigned int flags, int e2e_tx_hop,
638 u16 sof_mask, u16 eof_mask,
639 void (*start_poll)(void *), void *poll_data)
641 return tb_ring_alloc(nhi, hop, size, false, flags, e2e_tx_hop, sof_mask, eof_mask,
642 start_poll, poll_data);
644 EXPORT_SYMBOL_GPL(tb_ring_alloc_rx);
647 * tb_ring_start() - enable a ring
648 * @ring: Ring to start
650 * Must not be invoked in parallel with tb_ring_stop().
652 void tb_ring_start(struct tb_ring *ring)
657 spin_lock_irq(&ring->nhi->lock);
658 spin_lock(&ring->lock);
659 if (ring->nhi->going_away)
662 dev_WARN(&ring->nhi->pdev->dev, "ring already started\n");
665 dev_dbg(&ring->nhi->pdev->dev, "starting %s %d\n",
666 RING_TYPE(ring), ring->hop);
668 if (ring->flags & RING_FLAG_FRAME) {
671 flags = RING_FLAG_ENABLE;
673 frame_size = TB_FRAME_SIZE;
674 flags = RING_FLAG_ENABLE | RING_FLAG_RAW;
677 ring_iowrite64desc(ring, ring->descriptors_dma, 0);
679 ring_iowrite32desc(ring, ring->size, 12);
680 ring_iowrite32options(ring, 0, 4); /* time releated ? */
681 ring_iowrite32options(ring, flags, 0);
683 u32 sof_eof_mask = ring->sof_mask << 16 | ring->eof_mask;
685 ring_iowrite32desc(ring, (frame_size << 16) | ring->size, 12);
686 ring_iowrite32options(ring, sof_eof_mask, 4);
687 ring_iowrite32options(ring, flags, 0);
691 * Now that the ring valid bit is set we can configure E2E if
692 * enabled for the ring.
694 if (ring->flags & RING_FLAG_E2E) {
698 hop = ring->e2e_tx_hop << REG_RX_OPTIONS_E2E_HOP_SHIFT;
699 hop &= REG_RX_OPTIONS_E2E_HOP_MASK;
702 dev_dbg(&ring->nhi->pdev->dev,
703 "enabling E2E for %s %d with TX HopID %d\n",
704 RING_TYPE(ring), ring->hop, ring->e2e_tx_hop);
706 dev_dbg(&ring->nhi->pdev->dev, "enabling E2E for %s %d\n",
707 RING_TYPE(ring), ring->hop);
710 flags |= RING_FLAG_E2E_FLOW_CONTROL;
711 ring_iowrite32options(ring, flags, 0);
714 ring_interrupt_active(ring, true);
715 ring->running = true;
717 spin_unlock(&ring->lock);
718 spin_unlock_irq(&ring->nhi->lock);
720 EXPORT_SYMBOL_GPL(tb_ring_start);
723 * tb_ring_stop() - shutdown a ring
724 * @ring: Ring to stop
726 * Must not be invoked from a callback.
728 * This method will disable the ring. Further calls to
729 * tb_ring_tx/tb_ring_rx will return -ESHUTDOWN until ring_stop has been
732 * All enqueued frames will be canceled and their callbacks will be executed
733 * with frame->canceled set to true (on the callback thread). This method
734 * returns only after all callback invocations have finished.
736 void tb_ring_stop(struct tb_ring *ring)
738 spin_lock_irq(&ring->nhi->lock);
739 spin_lock(&ring->lock);
740 dev_dbg(&ring->nhi->pdev->dev, "stopping %s %d\n",
741 RING_TYPE(ring), ring->hop);
742 if (ring->nhi->going_away)
744 if (!ring->running) {
745 dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n",
746 RING_TYPE(ring), ring->hop);
749 ring_interrupt_active(ring, false);
751 ring_iowrite32options(ring, 0, 0);
752 ring_iowrite64desc(ring, 0, 0);
753 ring_iowrite32desc(ring, 0, 8);
754 ring_iowrite32desc(ring, 0, 12);
757 ring->running = false;
760 spin_unlock(&ring->lock);
761 spin_unlock_irq(&ring->nhi->lock);
764 * schedule ring->work to invoke callbacks on all remaining frames.
766 schedule_work(&ring->work);
767 flush_work(&ring->work);
769 EXPORT_SYMBOL_GPL(tb_ring_stop);
772 * tb_ring_free() - free ring
774 * When this method returns all invocations of ring->callback will have
777 * Ring must be stopped.
779 * Must NOT be called from ring_frame->callback!
781 void tb_ring_free(struct tb_ring *ring)
783 spin_lock_irq(&ring->nhi->lock);
785 * Dissociate the ring from the NHI. This also ensures that
786 * nhi_interrupt_work cannot reschedule ring->work.
789 ring->nhi->tx_rings[ring->hop] = NULL;
791 ring->nhi->rx_rings[ring->hop] = NULL;
794 dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n",
795 RING_TYPE(ring), ring->hop);
797 spin_unlock_irq(&ring->nhi->lock);
799 ring_release_msix(ring);
801 dma_free_coherent(&ring->nhi->pdev->dev,
802 ring->size * sizeof(*ring->descriptors),
803 ring->descriptors, ring->descriptors_dma);
805 ring->descriptors = NULL;
806 ring->descriptors_dma = 0;
809 dev_dbg(&ring->nhi->pdev->dev, "freeing %s %d\n", RING_TYPE(ring),
813 * ring->work can no longer be scheduled (it is scheduled only
814 * by nhi_interrupt_work, ring_stop and ring_msix). Wait for it
815 * to finish before freeing the ring.
817 flush_work(&ring->work);
820 EXPORT_SYMBOL_GPL(tb_ring_free);
823 * nhi_mailbox_cmd() - Send a command through NHI mailbox
824 * @nhi: Pointer to the NHI structure
825 * @cmd: Command to send
826 * @data: Data to be send with the command
828 * Sends mailbox command to the firmware running on NHI. Returns %0 in
829 * case of success and negative errno in case of failure.
831 int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data)
836 iowrite32(data, nhi->iobase + REG_INMAIL_DATA);
838 val = ioread32(nhi->iobase + REG_INMAIL_CMD);
839 val &= ~(REG_INMAIL_CMD_MASK | REG_INMAIL_ERROR);
840 val |= REG_INMAIL_OP_REQUEST | cmd;
841 iowrite32(val, nhi->iobase + REG_INMAIL_CMD);
843 timeout = ktime_add_ms(ktime_get(), NHI_MAILBOX_TIMEOUT);
845 val = ioread32(nhi->iobase + REG_INMAIL_CMD);
846 if (!(val & REG_INMAIL_OP_REQUEST))
848 usleep_range(10, 20);
849 } while (ktime_before(ktime_get(), timeout));
851 if (val & REG_INMAIL_OP_REQUEST)
853 if (val & REG_INMAIL_ERROR)
860 * nhi_mailbox_mode() - Return current firmware operation mode
861 * @nhi: Pointer to the NHI structure
863 * The function reads current firmware operation mode using NHI mailbox
864 * registers and returns it to the caller.
866 enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi)
870 val = ioread32(nhi->iobase + REG_OUTMAIL_CMD);
871 val &= REG_OUTMAIL_CMD_OPMODE_MASK;
872 val >>= REG_OUTMAIL_CMD_OPMODE_SHIFT;
874 return (enum nhi_fw_mode)val;
877 static void nhi_interrupt_work(struct work_struct *work)
879 struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work);
880 int value = 0; /* Suppress uninitialized usage warning. */
883 int type = 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */
884 struct tb_ring *ring;
886 spin_lock_irq(&nhi->lock);
889 * Starting at REG_RING_NOTIFY_BASE there are three status bitfields
890 * (TX, RX, RX overflow). We iterate over the bits and read a new
891 * dwords as required. The registers are cleared on read.
893 for (bit = 0; bit < 3 * nhi->hop_count; bit++) {
895 value = ioread32(nhi->iobase
896 + REG_RING_NOTIFY_BASE
898 if (++hop == nhi->hop_count) {
902 if ((value & (1 << (bit % 32))) == 0)
905 dev_warn(&nhi->pdev->dev,
906 "RX overflow for ring %d\n",
911 ring = nhi->tx_rings[hop];
913 ring = nhi->rx_rings[hop];
915 dev_warn(&nhi->pdev->dev,
916 "got interrupt for inactive %s ring %d\n",
922 spin_lock(&ring->lock);
923 __ring_interrupt(ring);
924 spin_unlock(&ring->lock);
926 spin_unlock_irq(&nhi->lock);
929 static irqreturn_t nhi_msi(int irq, void *data)
931 struct tb_nhi *nhi = data;
932 schedule_work(&nhi->interrupt_work);
936 static int __nhi_suspend_noirq(struct device *dev, bool wakeup)
938 struct pci_dev *pdev = to_pci_dev(dev);
939 struct tb *tb = pci_get_drvdata(pdev);
940 struct tb_nhi *nhi = tb->nhi;
943 ret = tb_domain_suspend_noirq(tb);
947 if (nhi->ops && nhi->ops->suspend_noirq) {
948 ret = nhi->ops->suspend_noirq(tb->nhi, wakeup);
956 static int nhi_suspend_noirq(struct device *dev)
958 return __nhi_suspend_noirq(dev, device_may_wakeup(dev));
961 static int nhi_freeze_noirq(struct device *dev)
963 struct pci_dev *pdev = to_pci_dev(dev);
964 struct tb *tb = pci_get_drvdata(pdev);
966 return tb_domain_freeze_noirq(tb);
969 static int nhi_thaw_noirq(struct device *dev)
971 struct pci_dev *pdev = to_pci_dev(dev);
972 struct tb *tb = pci_get_drvdata(pdev);
974 return tb_domain_thaw_noirq(tb);
977 static bool nhi_wake_supported(struct pci_dev *pdev)
982 * If power rails are sustainable for wakeup from S4 this
983 * property is set by the BIOS.
985 if (device_property_read_u8(&pdev->dev, "WAKE_SUPPORTED", &val))
991 static int nhi_poweroff_noirq(struct device *dev)
993 struct pci_dev *pdev = to_pci_dev(dev);
996 wakeup = device_may_wakeup(dev) && nhi_wake_supported(pdev);
997 return __nhi_suspend_noirq(dev, wakeup);
1000 static void nhi_enable_int_throttling(struct tb_nhi *nhi)
1002 /* Throttling is specified in 256ns increments */
1003 u32 throttle = DIV_ROUND_UP(128 * NSEC_PER_USEC, 256);
1007 * Configure interrupt throttling for all vectors even if we
1010 for (i = 0; i < MSIX_MAX_VECS; i++) {
1011 u32 reg = REG_INT_THROTTLING_RATE + i * 4;
1012 iowrite32(throttle, nhi->iobase + reg);
1016 static int nhi_resume_noirq(struct device *dev)
1018 struct pci_dev *pdev = to_pci_dev(dev);
1019 struct tb *tb = pci_get_drvdata(pdev);
1020 struct tb_nhi *nhi = tb->nhi;
1024 * Check that the device is still there. It may be that the user
1025 * unplugged last device which causes the host controller to go
1028 if (!pci_device_is_present(pdev)) {
1029 nhi->going_away = true;
1031 if (nhi->ops && nhi->ops->resume_noirq) {
1032 ret = nhi->ops->resume_noirq(nhi);
1036 nhi_enable_int_throttling(tb->nhi);
1039 return tb_domain_resume_noirq(tb);
1042 static int nhi_suspend(struct device *dev)
1044 struct pci_dev *pdev = to_pci_dev(dev);
1045 struct tb *tb = pci_get_drvdata(pdev);
1047 return tb_domain_suspend(tb);
1050 static void nhi_complete(struct device *dev)
1052 struct pci_dev *pdev = to_pci_dev(dev);
1053 struct tb *tb = pci_get_drvdata(pdev);
1056 * If we were runtime suspended when system suspend started,
1057 * schedule runtime resume now. It should bring the domain back
1058 * to functional state.
1060 if (pm_runtime_suspended(&pdev->dev))
1061 pm_runtime_resume(&pdev->dev);
1063 tb_domain_complete(tb);
1066 static int nhi_runtime_suspend(struct device *dev)
1068 struct pci_dev *pdev = to_pci_dev(dev);
1069 struct tb *tb = pci_get_drvdata(pdev);
1070 struct tb_nhi *nhi = tb->nhi;
1073 ret = tb_domain_runtime_suspend(tb);
1077 if (nhi->ops && nhi->ops->runtime_suspend) {
1078 ret = nhi->ops->runtime_suspend(tb->nhi);
1085 static int nhi_runtime_resume(struct device *dev)
1087 struct pci_dev *pdev = to_pci_dev(dev);
1088 struct tb *tb = pci_get_drvdata(pdev);
1089 struct tb_nhi *nhi = tb->nhi;
1092 if (nhi->ops && nhi->ops->runtime_resume) {
1093 ret = nhi->ops->runtime_resume(nhi);
1098 nhi_enable_int_throttling(nhi);
1099 return tb_domain_runtime_resume(tb);
1102 static void nhi_shutdown(struct tb_nhi *nhi)
1106 dev_dbg(&nhi->pdev->dev, "shutdown\n");
1108 for (i = 0; i < nhi->hop_count; i++) {
1109 if (nhi->tx_rings[i])
1110 dev_WARN(&nhi->pdev->dev,
1111 "TX ring %d is still active\n", i);
1112 if (nhi->rx_rings[i])
1113 dev_WARN(&nhi->pdev->dev,
1114 "RX ring %d is still active\n", i);
1116 nhi_disable_interrupts(nhi);
1118 * We have to release the irq before calling flush_work. Otherwise an
1119 * already executing IRQ handler could call schedule_work again.
1121 if (!nhi->pdev->msix_enabled) {
1122 devm_free_irq(&nhi->pdev->dev, nhi->pdev->irq, nhi);
1123 flush_work(&nhi->interrupt_work);
1125 ida_destroy(&nhi->msix_ida);
1127 if (nhi->ops && nhi->ops->shutdown)
1128 nhi->ops->shutdown(nhi);
1131 static void nhi_check_quirks(struct tb_nhi *nhi)
1133 if (nhi->pdev->vendor == PCI_VENDOR_ID_INTEL) {
1135 * Intel hardware supports auto clear of the interrupt
1136 * status register right after interrupt is being
1139 nhi->quirks |= QUIRK_AUTO_CLEAR_INT;
1141 switch (nhi->pdev->device) {
1142 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
1143 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
1145 * Falcon Ridge controller needs the end-to-end
1146 * flow control workaround to avoid losing Rx
1147 * packets when RING_FLAG_E2E is set.
1149 nhi->quirks |= QUIRK_E2E;
1155 static int nhi_check_iommu_pdev(struct pci_dev *pdev, void *data)
1157 if (!pdev->external_facing ||
1158 !device_iommu_capable(&pdev->dev, IOMMU_CAP_PRE_BOOT_PROTECTION))
1160 *(bool *)data = true;
1161 return 1; /* Stop walking */
1164 static void nhi_check_iommu(struct tb_nhi *nhi)
1166 struct pci_bus *bus = nhi->pdev->bus;
1167 bool port_ok = false;
1170 * Ideally what we'd do here is grab every PCI device that
1171 * represents a tunnelling adapter for this NHI and check their
1172 * status directly, but unfortunately USB4 seems to make it
1173 * obnoxiously difficult to reliably make any correlation.
1175 * So for now we'll have to bodge it... Hoping that the system
1176 * is at least sane enough that an adapter is in the same PCI
1177 * segment as its NHI, if we can find *something* on that segment
1178 * which meets the requirements for Kernel DMA Protection, we'll
1179 * take that to imply that firmware is aware and has (hopefully)
1180 * done the right thing in general. We need to know that the PCI
1181 * layer has seen the ExternalFacingPort property which will then
1182 * inform the IOMMU layer to enforce the complete "untrusted DMA"
1183 * flow, but also that the IOMMU driver itself can be trusted not
1184 * to have been subverted by a pre-boot DMA attack.
1189 pci_walk_bus(bus, nhi_check_iommu_pdev, &port_ok);
1191 nhi->iommu_dma_protection = port_ok;
1192 dev_dbg(&nhi->pdev->dev, "IOMMU DMA protection is %s\n",
1193 str_enabled_disabled(port_ok));
1196 static int nhi_init_msi(struct tb_nhi *nhi)
1198 struct pci_dev *pdev = nhi->pdev;
1199 struct device *dev = &pdev->dev;
1202 /* In case someone left them on. */
1203 nhi_disable_interrupts(nhi);
1205 nhi_enable_int_throttling(nhi);
1207 ida_init(&nhi->msix_ida);
1210 * The NHI has 16 MSI-X vectors or a single MSI. We first try to
1211 * get all MSI-X vectors and if we succeed, each ring will have
1212 * one MSI-X. If for some reason that does not work out, we
1213 * fallback to a single MSI.
1215 nvec = pci_alloc_irq_vectors(pdev, MSIX_MIN_VECS, MSIX_MAX_VECS,
1218 nvec = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
1222 INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
1224 irq = pci_irq_vector(nhi->pdev, 0);
1228 res = devm_request_irq(&pdev->dev, irq, nhi_msi,
1229 IRQF_NO_SUSPEND, "thunderbolt", nhi);
1231 return dev_err_probe(dev, res, "request_irq failed, aborting\n");
1237 static bool nhi_imr_valid(struct pci_dev *pdev)
1241 if (!device_property_read_u8(&pdev->dev, "IMR_VALID", &val))
1247 static struct tb *nhi_select_cm(struct tb_nhi *nhi)
1252 * USB4 case is simple. If we got control of any of the
1253 * capabilities, we use software CM.
1255 if (tb_acpi_is_native())
1256 return tb_probe(nhi);
1259 * Either firmware based CM is running (we did not get control
1260 * from the firmware) or this is pre-USB4 PC so try first
1261 * firmware CM and then fallback to software CM.
1263 tb = icm_probe(nhi);
1270 static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1272 struct device *dev = &pdev->dev;
1277 if (!nhi_imr_valid(pdev))
1278 return dev_err_probe(dev, -ENODEV, "firmware image not valid, aborting\n");
1280 res = pcim_enable_device(pdev);
1282 return dev_err_probe(dev, res, "cannot enable PCI device, aborting\n");
1284 res = pcim_iomap_regions(pdev, 1 << 0, "thunderbolt");
1286 return dev_err_probe(dev, res, "cannot obtain PCI resources, aborting\n");
1288 nhi = devm_kzalloc(&pdev->dev, sizeof(*nhi), GFP_KERNEL);
1293 nhi->ops = (const struct tb_nhi_ops *)id->driver_data;
1294 /* cannot fail - table is allocated in pcim_iomap_regions */
1295 nhi->iobase = pcim_iomap_table(pdev)[0];
1296 nhi->hop_count = ioread32(nhi->iobase + REG_HOP_COUNT) & 0x3ff;
1297 dev_dbg(dev, "total paths: %d\n", nhi->hop_count);
1299 nhi->tx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
1300 sizeof(*nhi->tx_rings), GFP_KERNEL);
1301 nhi->rx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
1302 sizeof(*nhi->rx_rings), GFP_KERNEL);
1303 if (!nhi->tx_rings || !nhi->rx_rings)
1306 nhi_check_quirks(nhi);
1307 nhi_check_iommu(nhi);
1309 res = nhi_init_msi(nhi);
1311 return dev_err_probe(dev, res, "cannot enable MSI, aborting\n");
1313 spin_lock_init(&nhi->lock);
1315 res = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1317 return dev_err_probe(dev, res, "failed to set DMA mask\n");
1319 pci_set_master(pdev);
1321 if (nhi->ops && nhi->ops->init) {
1322 res = nhi->ops->init(nhi);
1327 tb = nhi_select_cm(nhi);
1329 return dev_err_probe(dev, -ENODEV,
1330 "failed to determine connection manager, aborting\n");
1332 dev_dbg(dev, "NHI initialized, starting thunderbolt\n");
1334 res = tb_domain_add(tb);
1337 * At this point the RX/TX rings might already have been
1338 * activated. Do a proper shutdown.
1344 pci_set_drvdata(pdev, tb);
1346 device_wakeup_enable(&pdev->dev);
1348 pm_runtime_allow(&pdev->dev);
1349 pm_runtime_set_autosuspend_delay(&pdev->dev, TB_AUTOSUSPEND_DELAY);
1350 pm_runtime_use_autosuspend(&pdev->dev);
1351 pm_runtime_put_autosuspend(&pdev->dev);
1356 static void nhi_remove(struct pci_dev *pdev)
1358 struct tb *tb = pci_get_drvdata(pdev);
1359 struct tb_nhi *nhi = tb->nhi;
1361 pm_runtime_get_sync(&pdev->dev);
1362 pm_runtime_dont_use_autosuspend(&pdev->dev);
1363 pm_runtime_forbid(&pdev->dev);
1365 tb_domain_remove(tb);
1370 * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable
1371 * the tunnels asap. A corresponding pci quirk blocks the downstream bridges
1372 * resume_noirq until we are done.
1374 static const struct dev_pm_ops nhi_pm_ops = {
1375 .suspend_noirq = nhi_suspend_noirq,
1376 .resume_noirq = nhi_resume_noirq,
1377 .freeze_noirq = nhi_freeze_noirq, /*
1378 * we just disable hotplug, the
1379 * pci-tunnels stay alive.
1381 .thaw_noirq = nhi_thaw_noirq,
1382 .restore_noirq = nhi_resume_noirq,
1383 .suspend = nhi_suspend,
1384 .poweroff_noirq = nhi_poweroff_noirq,
1385 .poweroff = nhi_suspend,
1386 .complete = nhi_complete,
1387 .runtime_suspend = nhi_runtime_suspend,
1388 .runtime_resume = nhi_runtime_resume,
1391 static struct pci_device_id nhi_ids[] = {
1393 * We have to specify class, the TB bridges use the same device and
1394 * vendor (sub)id on gen 1 and gen 2 controllers.
1397 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1398 .vendor = PCI_VENDOR_ID_INTEL,
1399 .device = PCI_DEVICE_ID_INTEL_LIGHT_RIDGE,
1400 .subvendor = 0x2222, .subdevice = 0x1111,
1403 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1404 .vendor = PCI_VENDOR_ID_INTEL,
1405 .device = PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C,
1406 .subvendor = 0x2222, .subdevice = 0x1111,
1409 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1410 .vendor = PCI_VENDOR_ID_INTEL,
1411 .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI,
1412 .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
1415 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1416 .vendor = PCI_VENDOR_ID_INTEL,
1417 .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI,
1418 .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
1422 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI) },
1423 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI) },
1424 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_USBONLY_NHI) },
1425 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI) },
1426 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_USBONLY_NHI) },
1427 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI) },
1428 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI) },
1429 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_USBONLY_NHI) },
1430 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI) },
1431 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI) },
1432 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICL_NHI0),
1433 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1434 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICL_NHI1),
1435 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1437 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_NHI0),
1438 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1439 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_NHI1),
1440 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1441 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_H_NHI0),
1442 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1443 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_H_NHI1),
1444 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1445 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL_NHI0),
1446 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1447 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL_NHI1),
1448 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1449 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_RPL_NHI0),
1450 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1451 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_RPL_NHI1),
1452 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1453 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTL_M_NHI0),
1454 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1455 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTL_P_NHI0),
1456 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1457 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTL_P_NHI1),
1458 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1460 /* Any USB4 compliant host */
1461 { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_USB4, ~0) },
1466 MODULE_DEVICE_TABLE(pci, nhi_ids);
1467 MODULE_LICENSE("GPL");
1469 static struct pci_driver nhi_driver = {
1470 .name = "thunderbolt",
1471 .id_table = nhi_ids,
1473 .remove = nhi_remove,
1474 .shutdown = nhi_remove,
1475 .driver.pm = &nhi_pm_ops,
1478 static int __init nhi_init(void)
1482 ret = tb_domain_init();
1485 ret = pci_register_driver(&nhi_driver);
1491 static void __exit nhi_unload(void)
1493 pci_unregister_driver(&nhi_driver);
1497 rootfs_initcall(nhi_init);
1498 module_exit(nhi_unload);