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.
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;
57 static void nhi_mask_interrupt(struct tb_nhi *nhi, int mask, int ring)
59 if (nhi->quirks & QUIRK_AUTO_CLEAR_INT)
61 iowrite32(mask, nhi->iobase + REG_RING_INTERRUPT_MASK_CLEAR_BASE + ring);
64 static void nhi_clear_interrupt(struct tb_nhi *nhi, int ring)
66 if (nhi->quirks & QUIRK_AUTO_CLEAR_INT)
67 ioread32(nhi->iobase + REG_RING_NOTIFY_BASE + ring);
69 iowrite32(~0, nhi->iobase + REG_RING_INT_CLEAR + ring);
73 * ring_interrupt_active() - activate/deactivate interrupts for a single ring
75 * ring->nhi->lock must be held.
77 static void ring_interrupt_active(struct tb_ring *ring, bool active)
79 int index = ring_interrupt_index(ring) / 32 * 4;
80 int reg = REG_RING_INTERRUPT_BASE + index;
81 int interrupt_bit = ring_interrupt_index(ring) & 31;
82 int mask = 1 << interrupt_bit;
86 u32 step, shift, ivr, misc;
87 void __iomem *ivr_base;
94 index = ring->hop + ring->nhi->hop_count;
97 * Intel routers support a bit that isn't part of
98 * the USB4 spec to ask the hardware to clear
99 * interrupt status bits automatically since
100 * we already know which interrupt was triggered.
102 * Other routers explicitly disable auto-clear
103 * to prevent conditions that may occur where two
104 * MSIX interrupts are simultaneously active and
105 * reading the register clears both of them.
107 misc = ioread32(ring->nhi->iobase + REG_DMA_MISC);
108 if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT)
109 auto_clear_bit = REG_DMA_MISC_INT_AUTO_CLEAR;
111 auto_clear_bit = REG_DMA_MISC_DISABLE_AUTO_CLEAR;
112 if (!(misc & auto_clear_bit))
113 iowrite32(misc | auto_clear_bit,
114 ring->nhi->iobase + REG_DMA_MISC);
116 ivr_base = ring->nhi->iobase + REG_INT_VEC_ALLOC_BASE;
117 step = index / REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
118 shift = index % REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
119 ivr = ioread32(ivr_base + step);
120 ivr &= ~(REG_INT_VEC_ALLOC_MASK << shift);
122 ivr |= ring->vector << shift;
123 iowrite32(ivr, ivr_base + step);
126 old = ioread32(ring->nhi->iobase + reg);
132 dev_dbg(&ring->nhi->pdev->dev,
133 "%s interrupt at register %#x bit %d (%#x -> %#x)\n",
134 active ? "enabling" : "disabling", reg, interrupt_bit, old, new);
137 dev_WARN(&ring->nhi->pdev->dev,
138 "interrupt for %s %d is already %s\n",
139 RING_TYPE(ring), ring->hop,
140 active ? "enabled" : "disabled");
143 iowrite32(new, ring->nhi->iobase + reg);
145 nhi_mask_interrupt(ring->nhi, mask, index);
149 * nhi_disable_interrupts() - disable interrupts for all rings
151 * Use only during init and shutdown.
153 static void nhi_disable_interrupts(struct tb_nhi *nhi)
156 /* disable interrupts */
157 for (i = 0; i < RING_INTERRUPT_REG_COUNT(nhi); i++)
158 nhi_mask_interrupt(nhi, ~0, 4 * i);
160 /* clear interrupt status bits */
161 for (i = 0; i < RING_NOTIFY_REG_COUNT(nhi); i++)
162 nhi_clear_interrupt(nhi, 4 * i);
165 /* ring helper methods */
167 static void __iomem *ring_desc_base(struct tb_ring *ring)
169 void __iomem *io = ring->nhi->iobase;
170 io += ring->is_tx ? REG_TX_RING_BASE : REG_RX_RING_BASE;
171 io += ring->hop * 16;
175 static void __iomem *ring_options_base(struct tb_ring *ring)
177 void __iomem *io = ring->nhi->iobase;
178 io += ring->is_tx ? REG_TX_OPTIONS_BASE : REG_RX_OPTIONS_BASE;
179 io += ring->hop * 32;
183 static void ring_iowrite_cons(struct tb_ring *ring, u16 cons)
186 * The other 16-bits in the register is read-only and writes to it
187 * are ignored by the hardware so we can save one ioread32() by
188 * filling the read-only bits with zeroes.
190 iowrite32(cons, ring_desc_base(ring) + 8);
193 static void ring_iowrite_prod(struct tb_ring *ring, u16 prod)
195 /* See ring_iowrite_cons() above for explanation */
196 iowrite32(prod << 16, ring_desc_base(ring) + 8);
199 static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset)
201 iowrite32(value, ring_desc_base(ring) + offset);
204 static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset)
206 iowrite32(value, ring_desc_base(ring) + offset);
207 iowrite32(value >> 32, ring_desc_base(ring) + offset + 4);
210 static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset)
212 iowrite32(value, ring_options_base(ring) + offset);
215 static bool ring_full(struct tb_ring *ring)
217 return ((ring->head + 1) % ring->size) == ring->tail;
220 static bool ring_empty(struct tb_ring *ring)
222 return ring->head == ring->tail;
226 * ring_write_descriptors() - post frames from ring->queue to the controller
228 * ring->lock is held.
230 static void ring_write_descriptors(struct tb_ring *ring)
232 struct ring_frame *frame, *n;
233 struct ring_desc *descriptor;
234 list_for_each_entry_safe(frame, n, &ring->queue, list) {
237 list_move_tail(&frame->list, &ring->in_flight);
238 descriptor = &ring->descriptors[ring->head];
239 descriptor->phys = frame->buffer_phy;
240 descriptor->time = 0;
241 descriptor->flags = RING_DESC_POSTED | RING_DESC_INTERRUPT;
243 descriptor->length = frame->size;
244 descriptor->eof = frame->eof;
245 descriptor->sof = frame->sof;
247 ring->head = (ring->head + 1) % ring->size;
249 ring_iowrite_prod(ring, ring->head);
251 ring_iowrite_cons(ring, ring->head);
256 * ring_work() - progress completed frames
258 * If the ring is shutting down then all frames are marked as canceled and
259 * their callbacks are invoked.
261 * Otherwise we collect all completed frame from the ring buffer, write new
262 * frame to the ring buffer and invoke the callbacks for the completed frames.
264 static void ring_work(struct work_struct *work)
266 struct tb_ring *ring = container_of(work, typeof(*ring), work);
267 struct ring_frame *frame;
268 bool canceled = false;
272 spin_lock_irqsave(&ring->lock, flags);
274 if (!ring->running) {
275 /* Move all frames to done and mark them as canceled. */
276 list_splice_tail_init(&ring->in_flight, &done);
277 list_splice_tail_init(&ring->queue, &done);
279 goto invoke_callback;
282 while (!ring_empty(ring)) {
283 if (!(ring->descriptors[ring->tail].flags
284 & RING_DESC_COMPLETED))
286 frame = list_first_entry(&ring->in_flight, typeof(*frame),
288 list_move_tail(&frame->list, &done);
290 frame->size = ring->descriptors[ring->tail].length;
291 frame->eof = ring->descriptors[ring->tail].eof;
292 frame->sof = ring->descriptors[ring->tail].sof;
293 frame->flags = ring->descriptors[ring->tail].flags;
295 ring->tail = (ring->tail + 1) % ring->size;
297 ring_write_descriptors(ring);
300 /* allow callbacks to schedule new work */
301 spin_unlock_irqrestore(&ring->lock, flags);
302 while (!list_empty(&done)) {
303 frame = list_first_entry(&done, typeof(*frame), list);
305 * The callback may reenqueue or delete frame.
306 * Do not hold on to it.
308 list_del_init(&frame->list);
310 frame->callback(ring, frame, canceled);
314 int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame)
319 spin_lock_irqsave(&ring->lock, flags);
321 list_add_tail(&frame->list, &ring->queue);
322 ring_write_descriptors(ring);
326 spin_unlock_irqrestore(&ring->lock, flags);
329 EXPORT_SYMBOL_GPL(__tb_ring_enqueue);
332 * tb_ring_poll() - Poll one completed frame from the ring
333 * @ring: Ring to poll
335 * This function can be called when @start_poll callback of the @ring
336 * has been called. It will read one completed frame from the ring and
337 * return it to the caller. Returns %NULL if there is no more completed
340 struct ring_frame *tb_ring_poll(struct tb_ring *ring)
342 struct ring_frame *frame = NULL;
345 spin_lock_irqsave(&ring->lock, flags);
348 if (ring_empty(ring))
351 if (ring->descriptors[ring->tail].flags & RING_DESC_COMPLETED) {
352 frame = list_first_entry(&ring->in_flight, typeof(*frame),
354 list_del_init(&frame->list);
357 frame->size = ring->descriptors[ring->tail].length;
358 frame->eof = ring->descriptors[ring->tail].eof;
359 frame->sof = ring->descriptors[ring->tail].sof;
360 frame->flags = ring->descriptors[ring->tail].flags;
363 ring->tail = (ring->tail + 1) % ring->size;
367 spin_unlock_irqrestore(&ring->lock, flags);
370 EXPORT_SYMBOL_GPL(tb_ring_poll);
372 static void __ring_interrupt_mask(struct tb_ring *ring, bool mask)
374 int idx = ring_interrupt_index(ring);
375 int reg = REG_RING_INTERRUPT_BASE + idx / 32 * 4;
379 val = ioread32(ring->nhi->iobase + reg);
384 iowrite32(val, ring->nhi->iobase + reg);
387 /* Both @nhi->lock and @ring->lock should be held */
388 static void __ring_interrupt(struct tb_ring *ring)
393 if (ring->start_poll) {
394 __ring_interrupt_mask(ring, true);
395 ring->start_poll(ring->poll_data);
397 schedule_work(&ring->work);
402 * tb_ring_poll_complete() - Re-start interrupt for the ring
403 * @ring: Ring to re-start the interrupt
405 * This will re-start (unmask) the ring interrupt once the user is done
408 void tb_ring_poll_complete(struct tb_ring *ring)
412 spin_lock_irqsave(&ring->nhi->lock, flags);
413 spin_lock(&ring->lock);
414 if (ring->start_poll)
415 __ring_interrupt_mask(ring, false);
416 spin_unlock(&ring->lock);
417 spin_unlock_irqrestore(&ring->nhi->lock, flags);
419 EXPORT_SYMBOL_GPL(tb_ring_poll_complete);
421 static void ring_clear_msix(const struct tb_ring *ring)
425 if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT)
428 bit = ring_interrupt_index(ring) & 31;
430 iowrite32(BIT(bit), ring->nhi->iobase + REG_RING_INT_CLEAR);
432 iowrite32(BIT(bit), ring->nhi->iobase + REG_RING_INT_CLEAR +
433 4 * (ring->nhi->hop_count / 32));
436 static irqreturn_t ring_msix(int irq, void *data)
438 struct tb_ring *ring = data;
440 spin_lock(&ring->nhi->lock);
441 ring_clear_msix(ring);
442 spin_lock(&ring->lock);
443 __ring_interrupt(ring);
444 spin_unlock(&ring->lock);
445 spin_unlock(&ring->nhi->lock);
450 static int ring_request_msix(struct tb_ring *ring, bool no_suspend)
452 struct tb_nhi *nhi = ring->nhi;
453 unsigned long irqflags;
456 if (!nhi->pdev->msix_enabled)
459 ret = ida_simple_get(&nhi->msix_ida, 0, MSIX_MAX_VECS, GFP_KERNEL);
465 ret = pci_irq_vector(ring->nhi->pdev, ring->vector);
471 irqflags = no_suspend ? IRQF_NO_SUSPEND : 0;
472 ret = request_irq(ring->irq, ring_msix, irqflags, "thunderbolt", ring);
479 ida_simple_remove(&nhi->msix_ida, ring->vector);
484 static void ring_release_msix(struct tb_ring *ring)
489 free_irq(ring->irq, ring);
490 ida_simple_remove(&ring->nhi->msix_ida, ring->vector);
495 static int nhi_alloc_hop(struct tb_nhi *nhi, struct tb_ring *ring)
497 unsigned int start_hop = RING_FIRST_USABLE_HOPID;
500 if (nhi->quirks & QUIRK_E2E) {
501 start_hop = RING_FIRST_USABLE_HOPID + 1;
502 if (ring->flags & RING_FLAG_E2E && !ring->is_tx) {
503 dev_dbg(&nhi->pdev->dev, "quirking E2E TX HopID %u -> %u\n",
504 ring->e2e_tx_hop, RING_E2E_RESERVED_HOPID);
505 ring->e2e_tx_hop = RING_E2E_RESERVED_HOPID;
509 spin_lock_irq(&nhi->lock);
515 * Automatically allocate HopID from the non-reserved
516 * range 1 .. hop_count - 1.
518 for (i = start_hop; i < nhi->hop_count; i++) {
520 if (!nhi->tx_rings[i]) {
525 if (!nhi->rx_rings[i]) {
533 if (ring->hop > 0 && ring->hop < start_hop) {
534 dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop);
538 if (ring->hop < 0 || ring->hop >= nhi->hop_count) {
539 dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop);
543 if (ring->is_tx && nhi->tx_rings[ring->hop]) {
544 dev_warn(&nhi->pdev->dev, "TX hop %d already allocated\n",
549 if (!ring->is_tx && nhi->rx_rings[ring->hop]) {
550 dev_warn(&nhi->pdev->dev, "RX hop %d already allocated\n",
557 nhi->tx_rings[ring->hop] = ring;
559 nhi->rx_rings[ring->hop] = ring;
562 spin_unlock_irq(&nhi->lock);
567 static struct tb_ring *tb_ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
568 bool transmit, unsigned int flags,
569 int e2e_tx_hop, u16 sof_mask, u16 eof_mask,
570 void (*start_poll)(void *),
573 struct tb_ring *ring = NULL;
575 dev_dbg(&nhi->pdev->dev, "allocating %s ring %d of size %d\n",
576 transmit ? "TX" : "RX", hop, size);
578 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
582 spin_lock_init(&ring->lock);
583 INIT_LIST_HEAD(&ring->queue);
584 INIT_LIST_HEAD(&ring->in_flight);
585 INIT_WORK(&ring->work, ring_work);
589 ring->is_tx = transmit;
592 ring->e2e_tx_hop = e2e_tx_hop;
593 ring->sof_mask = sof_mask;
594 ring->eof_mask = eof_mask;
597 ring->running = false;
598 ring->start_poll = start_poll;
599 ring->poll_data = poll_data;
601 ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev,
602 size * sizeof(*ring->descriptors),
603 &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO);
604 if (!ring->descriptors)
607 if (ring_request_msix(ring, flags & RING_FLAG_NO_SUSPEND))
610 if (nhi_alloc_hop(nhi, ring))
611 goto err_release_msix;
616 ring_release_msix(ring);
618 dma_free_coherent(&ring->nhi->pdev->dev,
619 ring->size * sizeof(*ring->descriptors),
620 ring->descriptors, ring->descriptors_dma);
628 * tb_ring_alloc_tx() - Allocate DMA ring for transmit
629 * @nhi: Pointer to the NHI the ring is to be allocated
630 * @hop: HopID (ring) to allocate
631 * @size: Number of entries in the ring
632 * @flags: Flags for the ring
634 struct tb_ring *tb_ring_alloc_tx(struct tb_nhi *nhi, int hop, int size,
637 return tb_ring_alloc(nhi, hop, size, true, flags, 0, 0, 0, NULL, NULL);
639 EXPORT_SYMBOL_GPL(tb_ring_alloc_tx);
642 * tb_ring_alloc_rx() - Allocate DMA ring for receive
643 * @nhi: Pointer to the NHI the ring is to be allocated
644 * @hop: HopID (ring) to allocate. Pass %-1 for automatic allocation.
645 * @size: Number of entries in the ring
646 * @flags: Flags for the ring
647 * @e2e_tx_hop: Transmit HopID when E2E is enabled in @flags
648 * @sof_mask: Mask of PDF values that start a frame
649 * @eof_mask: Mask of PDF values that end a frame
650 * @start_poll: If not %NULL the ring will call this function when an
651 * interrupt is triggered and masked, instead of callback
653 * @poll_data: Optional data passed to @start_poll
655 struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size,
656 unsigned int flags, int e2e_tx_hop,
657 u16 sof_mask, u16 eof_mask,
658 void (*start_poll)(void *), void *poll_data)
660 return tb_ring_alloc(nhi, hop, size, false, flags, e2e_tx_hop, sof_mask, eof_mask,
661 start_poll, poll_data);
663 EXPORT_SYMBOL_GPL(tb_ring_alloc_rx);
666 * tb_ring_start() - enable a ring
667 * @ring: Ring to start
669 * Must not be invoked in parallel with tb_ring_stop().
671 void tb_ring_start(struct tb_ring *ring)
676 spin_lock_irq(&ring->nhi->lock);
677 spin_lock(&ring->lock);
678 if (ring->nhi->going_away)
681 dev_WARN(&ring->nhi->pdev->dev, "ring already started\n");
684 dev_dbg(&ring->nhi->pdev->dev, "starting %s %d\n",
685 RING_TYPE(ring), ring->hop);
687 if (ring->flags & RING_FLAG_FRAME) {
690 flags = RING_FLAG_ENABLE;
692 frame_size = TB_FRAME_SIZE;
693 flags = RING_FLAG_ENABLE | RING_FLAG_RAW;
696 ring_iowrite64desc(ring, ring->descriptors_dma, 0);
698 ring_iowrite32desc(ring, ring->size, 12);
699 ring_iowrite32options(ring, 0, 4); /* time releated ? */
700 ring_iowrite32options(ring, flags, 0);
702 u32 sof_eof_mask = ring->sof_mask << 16 | ring->eof_mask;
704 ring_iowrite32desc(ring, (frame_size << 16) | ring->size, 12);
705 ring_iowrite32options(ring, sof_eof_mask, 4);
706 ring_iowrite32options(ring, flags, 0);
710 * Now that the ring valid bit is set we can configure E2E if
711 * enabled for the ring.
713 if (ring->flags & RING_FLAG_E2E) {
717 hop = ring->e2e_tx_hop << REG_RX_OPTIONS_E2E_HOP_SHIFT;
718 hop &= REG_RX_OPTIONS_E2E_HOP_MASK;
721 dev_dbg(&ring->nhi->pdev->dev,
722 "enabling E2E for %s %d with TX HopID %d\n",
723 RING_TYPE(ring), ring->hop, ring->e2e_tx_hop);
725 dev_dbg(&ring->nhi->pdev->dev, "enabling E2E for %s %d\n",
726 RING_TYPE(ring), ring->hop);
729 flags |= RING_FLAG_E2E_FLOW_CONTROL;
730 ring_iowrite32options(ring, flags, 0);
733 ring_interrupt_active(ring, true);
734 ring->running = true;
736 spin_unlock(&ring->lock);
737 spin_unlock_irq(&ring->nhi->lock);
739 EXPORT_SYMBOL_GPL(tb_ring_start);
742 * tb_ring_stop() - shutdown a ring
743 * @ring: Ring to stop
745 * Must not be invoked from a callback.
747 * This method will disable the ring. Further calls to
748 * tb_ring_tx/tb_ring_rx will return -ESHUTDOWN until ring_stop has been
751 * All enqueued frames will be canceled and their callbacks will be executed
752 * with frame->canceled set to true (on the callback thread). This method
753 * returns only after all callback invocations have finished.
755 void tb_ring_stop(struct tb_ring *ring)
757 spin_lock_irq(&ring->nhi->lock);
758 spin_lock(&ring->lock);
759 dev_dbg(&ring->nhi->pdev->dev, "stopping %s %d\n",
760 RING_TYPE(ring), ring->hop);
761 if (ring->nhi->going_away)
763 if (!ring->running) {
764 dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n",
765 RING_TYPE(ring), ring->hop);
768 ring_interrupt_active(ring, false);
770 ring_iowrite32options(ring, 0, 0);
771 ring_iowrite64desc(ring, 0, 0);
772 ring_iowrite32desc(ring, 0, 8);
773 ring_iowrite32desc(ring, 0, 12);
776 ring->running = false;
779 spin_unlock(&ring->lock);
780 spin_unlock_irq(&ring->nhi->lock);
783 * schedule ring->work to invoke callbacks on all remaining frames.
785 schedule_work(&ring->work);
786 flush_work(&ring->work);
788 EXPORT_SYMBOL_GPL(tb_ring_stop);
791 * tb_ring_free() - free ring
793 * When this method returns all invocations of ring->callback will have
796 * Ring must be stopped.
798 * Must NOT be called from ring_frame->callback!
800 void tb_ring_free(struct tb_ring *ring)
802 spin_lock_irq(&ring->nhi->lock);
804 * Dissociate the ring from the NHI. This also ensures that
805 * nhi_interrupt_work cannot reschedule ring->work.
808 ring->nhi->tx_rings[ring->hop] = NULL;
810 ring->nhi->rx_rings[ring->hop] = NULL;
813 dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n",
814 RING_TYPE(ring), ring->hop);
816 spin_unlock_irq(&ring->nhi->lock);
818 ring_release_msix(ring);
820 dma_free_coherent(&ring->nhi->pdev->dev,
821 ring->size * sizeof(*ring->descriptors),
822 ring->descriptors, ring->descriptors_dma);
824 ring->descriptors = NULL;
825 ring->descriptors_dma = 0;
828 dev_dbg(&ring->nhi->pdev->dev, "freeing %s %d\n", RING_TYPE(ring),
832 * ring->work can no longer be scheduled (it is scheduled only
833 * by nhi_interrupt_work, ring_stop and ring_msix). Wait for it
834 * to finish before freeing the ring.
836 flush_work(&ring->work);
839 EXPORT_SYMBOL_GPL(tb_ring_free);
842 * nhi_mailbox_cmd() - Send a command through NHI mailbox
843 * @nhi: Pointer to the NHI structure
844 * @cmd: Command to send
845 * @data: Data to be send with the command
847 * Sends mailbox command to the firmware running on NHI. Returns %0 in
848 * case of success and negative errno in case of failure.
850 int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data)
855 iowrite32(data, nhi->iobase + REG_INMAIL_DATA);
857 val = ioread32(nhi->iobase + REG_INMAIL_CMD);
858 val &= ~(REG_INMAIL_CMD_MASK | REG_INMAIL_ERROR);
859 val |= REG_INMAIL_OP_REQUEST | cmd;
860 iowrite32(val, nhi->iobase + REG_INMAIL_CMD);
862 timeout = ktime_add_ms(ktime_get(), NHI_MAILBOX_TIMEOUT);
864 val = ioread32(nhi->iobase + REG_INMAIL_CMD);
865 if (!(val & REG_INMAIL_OP_REQUEST))
867 usleep_range(10, 20);
868 } while (ktime_before(ktime_get(), timeout));
870 if (val & REG_INMAIL_OP_REQUEST)
872 if (val & REG_INMAIL_ERROR)
879 * nhi_mailbox_mode() - Return current firmware operation mode
880 * @nhi: Pointer to the NHI structure
882 * The function reads current firmware operation mode using NHI mailbox
883 * registers and returns it to the caller.
885 enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi)
889 val = ioread32(nhi->iobase + REG_OUTMAIL_CMD);
890 val &= REG_OUTMAIL_CMD_OPMODE_MASK;
891 val >>= REG_OUTMAIL_CMD_OPMODE_SHIFT;
893 return (enum nhi_fw_mode)val;
896 static void nhi_interrupt_work(struct work_struct *work)
898 struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work);
899 int value = 0; /* Suppress uninitialized usage warning. */
902 int type = 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */
903 struct tb_ring *ring;
905 spin_lock_irq(&nhi->lock);
908 * Starting at REG_RING_NOTIFY_BASE there are three status bitfields
909 * (TX, RX, RX overflow). We iterate over the bits and read a new
910 * dwords as required. The registers are cleared on read.
912 for (bit = 0; bit < 3 * nhi->hop_count; bit++) {
914 value = ioread32(nhi->iobase
915 + REG_RING_NOTIFY_BASE
917 if (++hop == nhi->hop_count) {
921 if ((value & (1 << (bit % 32))) == 0)
924 dev_warn(&nhi->pdev->dev,
925 "RX overflow for ring %d\n",
930 ring = nhi->tx_rings[hop];
932 ring = nhi->rx_rings[hop];
934 dev_warn(&nhi->pdev->dev,
935 "got interrupt for inactive %s ring %d\n",
941 spin_lock(&ring->lock);
942 __ring_interrupt(ring);
943 spin_unlock(&ring->lock);
945 spin_unlock_irq(&nhi->lock);
948 static irqreturn_t nhi_msi(int irq, void *data)
950 struct tb_nhi *nhi = data;
951 schedule_work(&nhi->interrupt_work);
955 static int __nhi_suspend_noirq(struct device *dev, bool wakeup)
957 struct pci_dev *pdev = to_pci_dev(dev);
958 struct tb *tb = pci_get_drvdata(pdev);
959 struct tb_nhi *nhi = tb->nhi;
962 ret = tb_domain_suspend_noirq(tb);
966 if (nhi->ops && nhi->ops->suspend_noirq) {
967 ret = nhi->ops->suspend_noirq(tb->nhi, wakeup);
975 static int nhi_suspend_noirq(struct device *dev)
977 return __nhi_suspend_noirq(dev, device_may_wakeup(dev));
980 static int nhi_freeze_noirq(struct device *dev)
982 struct pci_dev *pdev = to_pci_dev(dev);
983 struct tb *tb = pci_get_drvdata(pdev);
985 return tb_domain_freeze_noirq(tb);
988 static int nhi_thaw_noirq(struct device *dev)
990 struct pci_dev *pdev = to_pci_dev(dev);
991 struct tb *tb = pci_get_drvdata(pdev);
993 return tb_domain_thaw_noirq(tb);
996 static bool nhi_wake_supported(struct pci_dev *pdev)
1001 * If power rails are sustainable for wakeup from S4 this
1002 * property is set by the BIOS.
1004 if (device_property_read_u8(&pdev->dev, "WAKE_SUPPORTED", &val))
1010 static int nhi_poweroff_noirq(struct device *dev)
1012 struct pci_dev *pdev = to_pci_dev(dev);
1015 wakeup = device_may_wakeup(dev) && nhi_wake_supported(pdev);
1016 return __nhi_suspend_noirq(dev, wakeup);
1019 static void nhi_enable_int_throttling(struct tb_nhi *nhi)
1021 /* Throttling is specified in 256ns increments */
1022 u32 throttle = DIV_ROUND_UP(128 * NSEC_PER_USEC, 256);
1026 * Configure interrupt throttling for all vectors even if we
1029 for (i = 0; i < MSIX_MAX_VECS; i++) {
1030 u32 reg = REG_INT_THROTTLING_RATE + i * 4;
1031 iowrite32(throttle, nhi->iobase + reg);
1035 static int nhi_resume_noirq(struct device *dev)
1037 struct pci_dev *pdev = to_pci_dev(dev);
1038 struct tb *tb = pci_get_drvdata(pdev);
1039 struct tb_nhi *nhi = tb->nhi;
1043 * Check that the device is still there. It may be that the user
1044 * unplugged last device which causes the host controller to go
1047 if (!pci_device_is_present(pdev)) {
1048 nhi->going_away = true;
1050 if (nhi->ops && nhi->ops->resume_noirq) {
1051 ret = nhi->ops->resume_noirq(nhi);
1055 nhi_enable_int_throttling(tb->nhi);
1058 return tb_domain_resume_noirq(tb);
1061 static int nhi_suspend(struct device *dev)
1063 struct pci_dev *pdev = to_pci_dev(dev);
1064 struct tb *tb = pci_get_drvdata(pdev);
1066 return tb_domain_suspend(tb);
1069 static void nhi_complete(struct device *dev)
1071 struct pci_dev *pdev = to_pci_dev(dev);
1072 struct tb *tb = pci_get_drvdata(pdev);
1075 * If we were runtime suspended when system suspend started,
1076 * schedule runtime resume now. It should bring the domain back
1077 * to functional state.
1079 if (pm_runtime_suspended(&pdev->dev))
1080 pm_runtime_resume(&pdev->dev);
1082 tb_domain_complete(tb);
1085 static int nhi_runtime_suspend(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 ret = tb_domain_runtime_suspend(tb);
1096 if (nhi->ops && nhi->ops->runtime_suspend) {
1097 ret = nhi->ops->runtime_suspend(tb->nhi);
1104 static int nhi_runtime_resume(struct device *dev)
1106 struct pci_dev *pdev = to_pci_dev(dev);
1107 struct tb *tb = pci_get_drvdata(pdev);
1108 struct tb_nhi *nhi = tb->nhi;
1111 if (nhi->ops && nhi->ops->runtime_resume) {
1112 ret = nhi->ops->runtime_resume(nhi);
1117 nhi_enable_int_throttling(nhi);
1118 return tb_domain_runtime_resume(tb);
1121 static void nhi_shutdown(struct tb_nhi *nhi)
1125 dev_dbg(&nhi->pdev->dev, "shutdown\n");
1127 for (i = 0; i < nhi->hop_count; i++) {
1128 if (nhi->tx_rings[i])
1129 dev_WARN(&nhi->pdev->dev,
1130 "TX ring %d is still active\n", i);
1131 if (nhi->rx_rings[i])
1132 dev_WARN(&nhi->pdev->dev,
1133 "RX ring %d is still active\n", i);
1135 nhi_disable_interrupts(nhi);
1137 * We have to release the irq before calling flush_work. Otherwise an
1138 * already executing IRQ handler could call schedule_work again.
1140 if (!nhi->pdev->msix_enabled) {
1141 devm_free_irq(&nhi->pdev->dev, nhi->pdev->irq, nhi);
1142 flush_work(&nhi->interrupt_work);
1144 ida_destroy(&nhi->msix_ida);
1146 if (nhi->ops && nhi->ops->shutdown)
1147 nhi->ops->shutdown(nhi);
1150 static void nhi_check_quirks(struct tb_nhi *nhi)
1152 if (nhi->pdev->vendor == PCI_VENDOR_ID_INTEL) {
1154 * Intel hardware supports auto clear of the interrupt
1155 * status register right after interrupt is being
1158 nhi->quirks |= QUIRK_AUTO_CLEAR_INT;
1160 switch (nhi->pdev->device) {
1161 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
1162 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
1164 * Falcon Ridge controller needs the end-to-end
1165 * flow control workaround to avoid losing Rx
1166 * packets when RING_FLAG_E2E is set.
1168 nhi->quirks |= QUIRK_E2E;
1174 static int nhi_check_iommu_pdev(struct pci_dev *pdev, void *data)
1176 if (!pdev->external_facing ||
1177 !device_iommu_capable(&pdev->dev, IOMMU_CAP_PRE_BOOT_PROTECTION))
1179 *(bool *)data = true;
1180 return 1; /* Stop walking */
1183 static void nhi_check_iommu(struct tb_nhi *nhi)
1185 struct pci_bus *bus = nhi->pdev->bus;
1186 bool port_ok = false;
1189 * Ideally what we'd do here is grab every PCI device that
1190 * represents a tunnelling adapter for this NHI and check their
1191 * status directly, but unfortunately USB4 seems to make it
1192 * obnoxiously difficult to reliably make any correlation.
1194 * So for now we'll have to bodge it... Hoping that the system
1195 * is at least sane enough that an adapter is in the same PCI
1196 * segment as its NHI, if we can find *something* on that segment
1197 * which meets the requirements for Kernel DMA Protection, we'll
1198 * take that to imply that firmware is aware and has (hopefully)
1199 * done the right thing in general. We need to know that the PCI
1200 * layer has seen the ExternalFacingPort property which will then
1201 * inform the IOMMU layer to enforce the complete "untrusted DMA"
1202 * flow, but also that the IOMMU driver itself can be trusted not
1203 * to have been subverted by a pre-boot DMA attack.
1208 pci_walk_bus(bus, nhi_check_iommu_pdev, &port_ok);
1210 nhi->iommu_dma_protection = port_ok;
1211 dev_dbg(&nhi->pdev->dev, "IOMMU DMA protection is %s\n",
1212 str_enabled_disabled(port_ok));
1215 static int nhi_init_msi(struct tb_nhi *nhi)
1217 struct pci_dev *pdev = nhi->pdev;
1218 struct device *dev = &pdev->dev;
1221 /* In case someone left them on. */
1222 nhi_disable_interrupts(nhi);
1224 nhi_enable_int_throttling(nhi);
1226 ida_init(&nhi->msix_ida);
1229 * The NHI has 16 MSI-X vectors or a single MSI. We first try to
1230 * get all MSI-X vectors and if we succeed, each ring will have
1231 * one MSI-X. If for some reason that does not work out, we
1232 * fallback to a single MSI.
1234 nvec = pci_alloc_irq_vectors(pdev, MSIX_MIN_VECS, MSIX_MAX_VECS,
1237 nvec = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
1241 INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
1243 irq = pci_irq_vector(nhi->pdev, 0);
1247 res = devm_request_irq(&pdev->dev, irq, nhi_msi,
1248 IRQF_NO_SUSPEND, "thunderbolt", nhi);
1250 return dev_err_probe(dev, res, "request_irq failed, aborting\n");
1256 static bool nhi_imr_valid(struct pci_dev *pdev)
1260 if (!device_property_read_u8(&pdev->dev, "IMR_VALID", &val))
1266 static struct tb *nhi_select_cm(struct tb_nhi *nhi)
1271 * USB4 case is simple. If we got control of any of the
1272 * capabilities, we use software CM.
1274 if (tb_acpi_is_native())
1275 return tb_probe(nhi);
1278 * Either firmware based CM is running (we did not get control
1279 * from the firmware) or this is pre-USB4 PC so try first
1280 * firmware CM and then fallback to software CM.
1282 tb = icm_probe(nhi);
1289 static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1291 struct device *dev = &pdev->dev;
1296 if (!nhi_imr_valid(pdev))
1297 return dev_err_probe(dev, -ENODEV, "firmware image not valid, aborting\n");
1299 res = pcim_enable_device(pdev);
1301 return dev_err_probe(dev, res, "cannot enable PCI device, aborting\n");
1303 res = pcim_iomap_regions(pdev, 1 << 0, "thunderbolt");
1305 return dev_err_probe(dev, res, "cannot obtain PCI resources, aborting\n");
1307 nhi = devm_kzalloc(&pdev->dev, sizeof(*nhi), GFP_KERNEL);
1312 nhi->ops = (const struct tb_nhi_ops *)id->driver_data;
1313 /* cannot fail - table is allocated in pcim_iomap_regions */
1314 nhi->iobase = pcim_iomap_table(pdev)[0];
1315 nhi->hop_count = ioread32(nhi->iobase + REG_HOP_COUNT) & 0x3ff;
1316 dev_dbg(dev, "total paths: %d\n", nhi->hop_count);
1318 nhi->tx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
1319 sizeof(*nhi->tx_rings), GFP_KERNEL);
1320 nhi->rx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
1321 sizeof(*nhi->rx_rings), GFP_KERNEL);
1322 if (!nhi->tx_rings || !nhi->rx_rings)
1325 nhi_check_quirks(nhi);
1326 nhi_check_iommu(nhi);
1328 res = nhi_init_msi(nhi);
1330 return dev_err_probe(dev, res, "cannot enable MSI, aborting\n");
1332 spin_lock_init(&nhi->lock);
1334 res = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1336 return dev_err_probe(dev, res, "failed to set DMA mask\n");
1338 pci_set_master(pdev);
1340 if (nhi->ops && nhi->ops->init) {
1341 res = nhi->ops->init(nhi);
1346 tb = nhi_select_cm(nhi);
1348 return dev_err_probe(dev, -ENODEV,
1349 "failed to determine connection manager, aborting\n");
1351 dev_dbg(dev, "NHI initialized, starting thunderbolt\n");
1353 res = tb_domain_add(tb);
1356 * At this point the RX/TX rings might already have been
1357 * activated. Do a proper shutdown.
1363 pci_set_drvdata(pdev, tb);
1365 device_wakeup_enable(&pdev->dev);
1367 pm_runtime_allow(&pdev->dev);
1368 pm_runtime_set_autosuspend_delay(&pdev->dev, TB_AUTOSUSPEND_DELAY);
1369 pm_runtime_use_autosuspend(&pdev->dev);
1370 pm_runtime_put_autosuspend(&pdev->dev);
1375 static void nhi_remove(struct pci_dev *pdev)
1377 struct tb *tb = pci_get_drvdata(pdev);
1378 struct tb_nhi *nhi = tb->nhi;
1380 pm_runtime_get_sync(&pdev->dev);
1381 pm_runtime_dont_use_autosuspend(&pdev->dev);
1382 pm_runtime_forbid(&pdev->dev);
1384 tb_domain_remove(tb);
1389 * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable
1390 * the tunnels asap. A corresponding pci quirk blocks the downstream bridges
1391 * resume_noirq until we are done.
1393 static const struct dev_pm_ops nhi_pm_ops = {
1394 .suspend_noirq = nhi_suspend_noirq,
1395 .resume_noirq = nhi_resume_noirq,
1396 .freeze_noirq = nhi_freeze_noirq, /*
1397 * we just disable hotplug, the
1398 * pci-tunnels stay alive.
1400 .thaw_noirq = nhi_thaw_noirq,
1401 .restore_noirq = nhi_resume_noirq,
1402 .suspend = nhi_suspend,
1403 .poweroff_noirq = nhi_poweroff_noirq,
1404 .poweroff = nhi_suspend,
1405 .complete = nhi_complete,
1406 .runtime_suspend = nhi_runtime_suspend,
1407 .runtime_resume = nhi_runtime_resume,
1410 static struct pci_device_id nhi_ids[] = {
1412 * We have to specify class, the TB bridges use the same device and
1413 * vendor (sub)id on gen 1 and gen 2 controllers.
1416 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1417 .vendor = PCI_VENDOR_ID_INTEL,
1418 .device = PCI_DEVICE_ID_INTEL_LIGHT_RIDGE,
1419 .subvendor = 0x2222, .subdevice = 0x1111,
1422 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1423 .vendor = PCI_VENDOR_ID_INTEL,
1424 .device = PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C,
1425 .subvendor = 0x2222, .subdevice = 0x1111,
1428 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1429 .vendor = PCI_VENDOR_ID_INTEL,
1430 .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI,
1431 .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
1434 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1435 .vendor = PCI_VENDOR_ID_INTEL,
1436 .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI,
1437 .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
1441 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI) },
1442 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI) },
1443 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_USBONLY_NHI) },
1444 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI) },
1445 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_USBONLY_NHI) },
1446 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI) },
1447 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI) },
1448 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_USBONLY_NHI) },
1449 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI) },
1450 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI) },
1451 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICL_NHI0),
1452 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1453 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICL_NHI1),
1454 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1456 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_NHI0),
1457 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1458 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_NHI1),
1459 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1460 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_H_NHI0),
1461 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1462 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_H_NHI1),
1463 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1464 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL_NHI0),
1465 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1466 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL_NHI1),
1467 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1468 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_RPL_NHI0),
1469 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1470 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_RPL_NHI1),
1471 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1472 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTL_M_NHI0),
1473 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1474 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTL_P_NHI0),
1475 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1476 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTL_P_NHI1),
1477 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1479 /* Any USB4 compliant host */
1480 { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_USB4, ~0) },
1485 MODULE_DEVICE_TABLE(pci, nhi_ids);
1486 MODULE_LICENSE("GPL");
1488 static struct pci_driver nhi_driver = {
1489 .name = "thunderbolt",
1490 .id_table = nhi_ids,
1492 .remove = nhi_remove,
1493 .shutdown = nhi_remove,
1494 .driver.pm = &nhi_pm_ops,
1497 static int __init nhi_init(void)
1501 ret = tb_domain_init();
1504 ret = pci_register_driver(&nhi_driver);
1510 static void __exit nhi_unload(void)
1512 pci_unregister_driver(&nhi_driver);
1516 rootfs_initcall(nhi_init);
1517 module_exit(nhi_unload);