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
33 * Minimal number of vectors when we use MSI-X. Two for control channel
34 * Rx/Tx and the rest four are for cross domain DMA paths.
36 #define MSIX_MIN_VECS 6
37 #define MSIX_MAX_VECS 16
39 #define NHI_MAILBOX_TIMEOUT 500 /* ms */
41 #define QUIRK_AUTO_CLEAR_INT BIT(0)
43 static int ring_interrupt_index(struct tb_ring *ring)
47 bit += ring->nhi->hop_count;
52 * ring_interrupt_active() - activate/deactivate interrupts for a single ring
54 * ring->nhi->lock must be held.
56 static void ring_interrupt_active(struct tb_ring *ring, bool active)
58 int reg = REG_RING_INTERRUPT_BASE +
59 ring_interrupt_index(ring) / 32 * 4;
60 int bit = ring_interrupt_index(ring) & 31;
65 u32 step, shift, ivr, misc;
66 void __iomem *ivr_base;
72 index = ring->hop + ring->nhi->hop_count;
74 if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT) {
76 * Ask the hardware to clear interrupt status
77 * bits automatically since we already know
78 * which interrupt was triggered.
80 misc = ioread32(ring->nhi->iobase + REG_DMA_MISC);
81 if (!(misc & REG_DMA_MISC_INT_AUTO_CLEAR)) {
82 misc |= REG_DMA_MISC_INT_AUTO_CLEAR;
83 iowrite32(misc, ring->nhi->iobase + REG_DMA_MISC);
87 ivr_base = ring->nhi->iobase + REG_INT_VEC_ALLOC_BASE;
88 step = index / REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
89 shift = index % REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
90 ivr = ioread32(ivr_base + step);
91 ivr &= ~(REG_INT_VEC_ALLOC_MASK << shift);
93 ivr |= ring->vector << shift;
94 iowrite32(ivr, ivr_base + step);
97 old = ioread32(ring->nhi->iobase + reg);
103 dev_dbg(&ring->nhi->pdev->dev,
104 "%s interrupt at register %#x bit %d (%#x -> %#x)\n",
105 active ? "enabling" : "disabling", reg, bit, old, new);
108 dev_WARN(&ring->nhi->pdev->dev,
109 "interrupt for %s %d is already %s\n",
110 RING_TYPE(ring), ring->hop,
111 active ? "enabled" : "disabled");
112 iowrite32(new, ring->nhi->iobase + reg);
116 * nhi_disable_interrupts() - disable interrupts for all rings
118 * Use only during init and shutdown.
120 static void nhi_disable_interrupts(struct tb_nhi *nhi)
123 /* disable interrupts */
124 for (i = 0; i < RING_INTERRUPT_REG_COUNT(nhi); i++)
125 iowrite32(0, nhi->iobase + REG_RING_INTERRUPT_BASE + 4 * i);
127 /* clear interrupt status bits */
128 for (i = 0; i < RING_NOTIFY_REG_COUNT(nhi); i++)
129 ioread32(nhi->iobase + REG_RING_NOTIFY_BASE + 4 * i);
132 /* ring helper methods */
134 static void __iomem *ring_desc_base(struct tb_ring *ring)
136 void __iomem *io = ring->nhi->iobase;
137 io += ring->is_tx ? REG_TX_RING_BASE : REG_RX_RING_BASE;
138 io += ring->hop * 16;
142 static void __iomem *ring_options_base(struct tb_ring *ring)
144 void __iomem *io = ring->nhi->iobase;
145 io += ring->is_tx ? REG_TX_OPTIONS_BASE : REG_RX_OPTIONS_BASE;
146 io += ring->hop * 32;
150 static void ring_iowrite_cons(struct tb_ring *ring, u16 cons)
153 * The other 16-bits in the register is read-only and writes to it
154 * are ignored by the hardware so we can save one ioread32() by
155 * filling the read-only bits with zeroes.
157 iowrite32(cons, ring_desc_base(ring) + 8);
160 static void ring_iowrite_prod(struct tb_ring *ring, u16 prod)
162 /* See ring_iowrite_cons() above for explanation */
163 iowrite32(prod << 16, ring_desc_base(ring) + 8);
166 static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset)
168 iowrite32(value, ring_desc_base(ring) + offset);
171 static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset)
173 iowrite32(value, ring_desc_base(ring) + offset);
174 iowrite32(value >> 32, ring_desc_base(ring) + offset + 4);
177 static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset)
179 iowrite32(value, ring_options_base(ring) + offset);
182 static bool ring_full(struct tb_ring *ring)
184 return ((ring->head + 1) % ring->size) == ring->tail;
187 static bool ring_empty(struct tb_ring *ring)
189 return ring->head == ring->tail;
193 * ring_write_descriptors() - post frames from ring->queue to the controller
195 * ring->lock is held.
197 static void ring_write_descriptors(struct tb_ring *ring)
199 struct ring_frame *frame, *n;
200 struct ring_desc *descriptor;
201 list_for_each_entry_safe(frame, n, &ring->queue, list) {
204 list_move_tail(&frame->list, &ring->in_flight);
205 descriptor = &ring->descriptors[ring->head];
206 descriptor->phys = frame->buffer_phy;
207 descriptor->time = 0;
208 descriptor->flags = RING_DESC_POSTED | RING_DESC_INTERRUPT;
210 descriptor->length = frame->size;
211 descriptor->eof = frame->eof;
212 descriptor->sof = frame->sof;
214 ring->head = (ring->head + 1) % ring->size;
216 ring_iowrite_prod(ring, ring->head);
218 ring_iowrite_cons(ring, ring->head);
223 * ring_work() - progress completed frames
225 * If the ring is shutting down then all frames are marked as canceled and
226 * their callbacks are invoked.
228 * Otherwise we collect all completed frame from the ring buffer, write new
229 * frame to the ring buffer and invoke the callbacks for the completed frames.
231 static void ring_work(struct work_struct *work)
233 struct tb_ring *ring = container_of(work, typeof(*ring), work);
234 struct ring_frame *frame;
235 bool canceled = false;
239 spin_lock_irqsave(&ring->lock, flags);
241 if (!ring->running) {
242 /* Move all frames to done and mark them as canceled. */
243 list_splice_tail_init(&ring->in_flight, &done);
244 list_splice_tail_init(&ring->queue, &done);
246 goto invoke_callback;
249 while (!ring_empty(ring)) {
250 if (!(ring->descriptors[ring->tail].flags
251 & RING_DESC_COMPLETED))
253 frame = list_first_entry(&ring->in_flight, typeof(*frame),
255 list_move_tail(&frame->list, &done);
257 frame->size = ring->descriptors[ring->tail].length;
258 frame->eof = ring->descriptors[ring->tail].eof;
259 frame->sof = ring->descriptors[ring->tail].sof;
260 frame->flags = ring->descriptors[ring->tail].flags;
262 ring->tail = (ring->tail + 1) % ring->size;
264 ring_write_descriptors(ring);
267 /* allow callbacks to schedule new work */
268 spin_unlock_irqrestore(&ring->lock, flags);
269 while (!list_empty(&done)) {
270 frame = list_first_entry(&done, typeof(*frame), list);
272 * The callback may reenqueue or delete frame.
273 * Do not hold on to it.
275 list_del_init(&frame->list);
277 frame->callback(ring, frame, canceled);
281 int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame)
286 spin_lock_irqsave(&ring->lock, flags);
288 list_add_tail(&frame->list, &ring->queue);
289 ring_write_descriptors(ring);
293 spin_unlock_irqrestore(&ring->lock, flags);
296 EXPORT_SYMBOL_GPL(__tb_ring_enqueue);
299 * tb_ring_poll() - Poll one completed frame from the ring
300 * @ring: Ring to poll
302 * This function can be called when @start_poll callback of the @ring
303 * has been called. It will read one completed frame from the ring and
304 * return it to the caller. Returns %NULL if there is no more completed
307 struct ring_frame *tb_ring_poll(struct tb_ring *ring)
309 struct ring_frame *frame = NULL;
312 spin_lock_irqsave(&ring->lock, flags);
315 if (ring_empty(ring))
318 if (ring->descriptors[ring->tail].flags & RING_DESC_COMPLETED) {
319 frame = list_first_entry(&ring->in_flight, typeof(*frame),
321 list_del_init(&frame->list);
324 frame->size = ring->descriptors[ring->tail].length;
325 frame->eof = ring->descriptors[ring->tail].eof;
326 frame->sof = ring->descriptors[ring->tail].sof;
327 frame->flags = ring->descriptors[ring->tail].flags;
330 ring->tail = (ring->tail + 1) % ring->size;
334 spin_unlock_irqrestore(&ring->lock, flags);
337 EXPORT_SYMBOL_GPL(tb_ring_poll);
339 static void __ring_interrupt_mask(struct tb_ring *ring, bool mask)
341 int idx = ring_interrupt_index(ring);
342 int reg = REG_RING_INTERRUPT_BASE + idx / 32 * 4;
346 val = ioread32(ring->nhi->iobase + reg);
351 iowrite32(val, ring->nhi->iobase + reg);
354 /* Both @nhi->lock and @ring->lock should be held */
355 static void __ring_interrupt(struct tb_ring *ring)
360 if (ring->start_poll) {
361 __ring_interrupt_mask(ring, true);
362 ring->start_poll(ring->poll_data);
364 schedule_work(&ring->work);
369 * tb_ring_poll_complete() - Re-start interrupt for the ring
370 * @ring: Ring to re-start the interrupt
372 * This will re-start (unmask) the ring interrupt once the user is done
375 void tb_ring_poll_complete(struct tb_ring *ring)
379 spin_lock_irqsave(&ring->nhi->lock, flags);
380 spin_lock(&ring->lock);
381 if (ring->start_poll)
382 __ring_interrupt_mask(ring, false);
383 spin_unlock(&ring->lock);
384 spin_unlock_irqrestore(&ring->nhi->lock, flags);
386 EXPORT_SYMBOL_GPL(tb_ring_poll_complete);
388 static void ring_clear_msix(const struct tb_ring *ring)
390 if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT)
394 ioread32(ring->nhi->iobase + REG_RING_NOTIFY_BASE);
396 ioread32(ring->nhi->iobase + REG_RING_NOTIFY_BASE +
397 4 * (ring->nhi->hop_count / 32));
400 static irqreturn_t ring_msix(int irq, void *data)
402 struct tb_ring *ring = data;
404 spin_lock(&ring->nhi->lock);
405 ring_clear_msix(ring);
406 spin_lock(&ring->lock);
407 __ring_interrupt(ring);
408 spin_unlock(&ring->lock);
409 spin_unlock(&ring->nhi->lock);
414 static int ring_request_msix(struct tb_ring *ring, bool no_suspend)
416 struct tb_nhi *nhi = ring->nhi;
417 unsigned long irqflags;
420 if (!nhi->pdev->msix_enabled)
423 ret = ida_simple_get(&nhi->msix_ida, 0, MSIX_MAX_VECS, GFP_KERNEL);
429 ret = pci_irq_vector(ring->nhi->pdev, ring->vector);
435 irqflags = no_suspend ? IRQF_NO_SUSPEND : 0;
436 ret = request_irq(ring->irq, ring_msix, irqflags, "thunderbolt", ring);
443 ida_simple_remove(&nhi->msix_ida, ring->vector);
448 static void ring_release_msix(struct tb_ring *ring)
453 free_irq(ring->irq, ring);
454 ida_simple_remove(&ring->nhi->msix_ida, ring->vector);
459 static int nhi_alloc_hop(struct tb_nhi *nhi, struct tb_ring *ring)
463 spin_lock_irq(&nhi->lock);
469 * Automatically allocate HopID from the non-reserved
470 * range 1 .. hop_count - 1.
472 for (i = RING_FIRST_USABLE_HOPID; i < nhi->hop_count; i++) {
474 if (!nhi->tx_rings[i]) {
479 if (!nhi->rx_rings[i]) {
487 if (ring->hop < 0 || ring->hop >= nhi->hop_count) {
488 dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop);
492 if (ring->is_tx && nhi->tx_rings[ring->hop]) {
493 dev_warn(&nhi->pdev->dev, "TX hop %d already allocated\n",
497 } else if (!ring->is_tx && nhi->rx_rings[ring->hop]) {
498 dev_warn(&nhi->pdev->dev, "RX hop %d already allocated\n",
505 nhi->tx_rings[ring->hop] = ring;
507 nhi->rx_rings[ring->hop] = ring;
510 spin_unlock_irq(&nhi->lock);
515 static struct tb_ring *tb_ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
516 bool transmit, unsigned int flags,
517 int e2e_tx_hop, u16 sof_mask, u16 eof_mask,
518 void (*start_poll)(void *),
521 struct tb_ring *ring = NULL;
523 dev_dbg(&nhi->pdev->dev, "allocating %s ring %d of size %d\n",
524 transmit ? "TX" : "RX", hop, size);
526 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
530 spin_lock_init(&ring->lock);
531 INIT_LIST_HEAD(&ring->queue);
532 INIT_LIST_HEAD(&ring->in_flight);
533 INIT_WORK(&ring->work, ring_work);
537 ring->is_tx = transmit;
540 ring->e2e_tx_hop = e2e_tx_hop;
541 ring->sof_mask = sof_mask;
542 ring->eof_mask = eof_mask;
545 ring->running = false;
546 ring->start_poll = start_poll;
547 ring->poll_data = poll_data;
549 ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev,
550 size * sizeof(*ring->descriptors),
551 &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO);
552 if (!ring->descriptors)
555 if (ring_request_msix(ring, flags & RING_FLAG_NO_SUSPEND))
558 if (nhi_alloc_hop(nhi, ring))
559 goto err_release_msix;
564 ring_release_msix(ring);
566 dma_free_coherent(&ring->nhi->pdev->dev,
567 ring->size * sizeof(*ring->descriptors),
568 ring->descriptors, ring->descriptors_dma);
576 * tb_ring_alloc_tx() - Allocate DMA ring for transmit
577 * @nhi: Pointer to the NHI the ring is to be allocated
578 * @hop: HopID (ring) to allocate
579 * @size: Number of entries in the ring
580 * @flags: Flags for the ring
582 struct tb_ring *tb_ring_alloc_tx(struct tb_nhi *nhi, int hop, int size,
585 return tb_ring_alloc(nhi, hop, size, true, flags, 0, 0, 0, NULL, NULL);
587 EXPORT_SYMBOL_GPL(tb_ring_alloc_tx);
590 * tb_ring_alloc_rx() - Allocate DMA ring for receive
591 * @nhi: Pointer to the NHI the ring is to be allocated
592 * @hop: HopID (ring) to allocate. Pass %-1 for automatic allocation.
593 * @size: Number of entries in the ring
594 * @flags: Flags for the ring
595 * @e2e_tx_hop: Transmit HopID when E2E is enabled in @flags
596 * @sof_mask: Mask of PDF values that start a frame
597 * @eof_mask: Mask of PDF values that end a frame
598 * @start_poll: If not %NULL the ring will call this function when an
599 * interrupt is triggered and masked, instead of callback
601 * @poll_data: Optional data passed to @start_poll
603 struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size,
604 unsigned int flags, int e2e_tx_hop,
605 u16 sof_mask, u16 eof_mask,
606 void (*start_poll)(void *), void *poll_data)
608 return tb_ring_alloc(nhi, hop, size, false, flags, e2e_tx_hop, sof_mask, eof_mask,
609 start_poll, poll_data);
611 EXPORT_SYMBOL_GPL(tb_ring_alloc_rx);
614 * tb_ring_start() - enable a ring
615 * @ring: Ring to start
617 * Must not be invoked in parallel with tb_ring_stop().
619 void tb_ring_start(struct tb_ring *ring)
624 spin_lock_irq(&ring->nhi->lock);
625 spin_lock(&ring->lock);
626 if (ring->nhi->going_away)
629 dev_WARN(&ring->nhi->pdev->dev, "ring already started\n");
632 dev_dbg(&ring->nhi->pdev->dev, "starting %s %d\n",
633 RING_TYPE(ring), ring->hop);
635 if (ring->flags & RING_FLAG_FRAME) {
638 flags = RING_FLAG_ENABLE;
640 frame_size = TB_FRAME_SIZE;
641 flags = RING_FLAG_ENABLE | RING_FLAG_RAW;
644 ring_iowrite64desc(ring, ring->descriptors_dma, 0);
646 ring_iowrite32desc(ring, ring->size, 12);
647 ring_iowrite32options(ring, 0, 4); /* time releated ? */
648 ring_iowrite32options(ring, flags, 0);
650 u32 sof_eof_mask = ring->sof_mask << 16 | ring->eof_mask;
652 ring_iowrite32desc(ring, (frame_size << 16) | ring->size, 12);
653 ring_iowrite32options(ring, sof_eof_mask, 4);
654 ring_iowrite32options(ring, flags, 0);
658 * Now that the ring valid bit is set we can configure E2E if
659 * enabled for the ring.
661 if (ring->flags & RING_FLAG_E2E) {
665 hop = ring->e2e_tx_hop << REG_RX_OPTIONS_E2E_HOP_SHIFT;
666 hop &= REG_RX_OPTIONS_E2E_HOP_MASK;
669 dev_dbg(&ring->nhi->pdev->dev,
670 "enabling E2E for %s %d with TX HopID %d\n",
671 RING_TYPE(ring), ring->hop, ring->e2e_tx_hop);
673 dev_dbg(&ring->nhi->pdev->dev, "enabling E2E for %s %d\n",
674 RING_TYPE(ring), ring->hop);
677 flags |= RING_FLAG_E2E_FLOW_CONTROL;
678 ring_iowrite32options(ring, flags, 0);
681 ring_interrupt_active(ring, true);
682 ring->running = true;
684 spin_unlock(&ring->lock);
685 spin_unlock_irq(&ring->nhi->lock);
687 EXPORT_SYMBOL_GPL(tb_ring_start);
690 * tb_ring_stop() - shutdown a ring
691 * @ring: Ring to stop
693 * Must not be invoked from a callback.
695 * This method will disable the ring. Further calls to
696 * tb_ring_tx/tb_ring_rx will return -ESHUTDOWN until ring_stop has been
699 * All enqueued frames will be canceled and their callbacks will be executed
700 * with frame->canceled set to true (on the callback thread). This method
701 * returns only after all callback invocations have finished.
703 void tb_ring_stop(struct tb_ring *ring)
705 spin_lock_irq(&ring->nhi->lock);
706 spin_lock(&ring->lock);
707 dev_dbg(&ring->nhi->pdev->dev, "stopping %s %d\n",
708 RING_TYPE(ring), ring->hop);
709 if (ring->nhi->going_away)
711 if (!ring->running) {
712 dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n",
713 RING_TYPE(ring), ring->hop);
716 ring_interrupt_active(ring, false);
718 ring_iowrite32options(ring, 0, 0);
719 ring_iowrite64desc(ring, 0, 0);
720 ring_iowrite32desc(ring, 0, 8);
721 ring_iowrite32desc(ring, 0, 12);
724 ring->running = false;
727 spin_unlock(&ring->lock);
728 spin_unlock_irq(&ring->nhi->lock);
731 * schedule ring->work to invoke callbacks on all remaining frames.
733 schedule_work(&ring->work);
734 flush_work(&ring->work);
736 EXPORT_SYMBOL_GPL(tb_ring_stop);
739 * tb_ring_free() - free ring
741 * When this method returns all invocations of ring->callback will have
744 * Ring must be stopped.
746 * Must NOT be called from ring_frame->callback!
748 void tb_ring_free(struct tb_ring *ring)
750 spin_lock_irq(&ring->nhi->lock);
752 * Dissociate the ring from the NHI. This also ensures that
753 * nhi_interrupt_work cannot reschedule ring->work.
756 ring->nhi->tx_rings[ring->hop] = NULL;
758 ring->nhi->rx_rings[ring->hop] = NULL;
761 dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n",
762 RING_TYPE(ring), ring->hop);
764 spin_unlock_irq(&ring->nhi->lock);
766 ring_release_msix(ring);
768 dma_free_coherent(&ring->nhi->pdev->dev,
769 ring->size * sizeof(*ring->descriptors),
770 ring->descriptors, ring->descriptors_dma);
772 ring->descriptors = NULL;
773 ring->descriptors_dma = 0;
776 dev_dbg(&ring->nhi->pdev->dev, "freeing %s %d\n", RING_TYPE(ring),
780 * ring->work can no longer be scheduled (it is scheduled only
781 * by nhi_interrupt_work, ring_stop and ring_msix). Wait for it
782 * to finish before freeing the ring.
784 flush_work(&ring->work);
787 EXPORT_SYMBOL_GPL(tb_ring_free);
790 * nhi_mailbox_cmd() - Send a command through NHI mailbox
791 * @nhi: Pointer to the NHI structure
792 * @cmd: Command to send
793 * @data: Data to be send with the command
795 * Sends mailbox command to the firmware running on NHI. Returns %0 in
796 * case of success and negative errno in case of failure.
798 int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data)
803 iowrite32(data, nhi->iobase + REG_INMAIL_DATA);
805 val = ioread32(nhi->iobase + REG_INMAIL_CMD);
806 val &= ~(REG_INMAIL_CMD_MASK | REG_INMAIL_ERROR);
807 val |= REG_INMAIL_OP_REQUEST | cmd;
808 iowrite32(val, nhi->iobase + REG_INMAIL_CMD);
810 timeout = ktime_add_ms(ktime_get(), NHI_MAILBOX_TIMEOUT);
812 val = ioread32(nhi->iobase + REG_INMAIL_CMD);
813 if (!(val & REG_INMAIL_OP_REQUEST))
815 usleep_range(10, 20);
816 } while (ktime_before(ktime_get(), timeout));
818 if (val & REG_INMAIL_OP_REQUEST)
820 if (val & REG_INMAIL_ERROR)
827 * nhi_mailbox_mode() - Return current firmware operation mode
828 * @nhi: Pointer to the NHI structure
830 * The function reads current firmware operation mode using NHI mailbox
831 * registers and returns it to the caller.
833 enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi)
837 val = ioread32(nhi->iobase + REG_OUTMAIL_CMD);
838 val &= REG_OUTMAIL_CMD_OPMODE_MASK;
839 val >>= REG_OUTMAIL_CMD_OPMODE_SHIFT;
841 return (enum nhi_fw_mode)val;
844 static void nhi_interrupt_work(struct work_struct *work)
846 struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work);
847 int value = 0; /* Suppress uninitialized usage warning. */
850 int type = 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */
851 struct tb_ring *ring;
853 spin_lock_irq(&nhi->lock);
856 * Starting at REG_RING_NOTIFY_BASE there are three status bitfields
857 * (TX, RX, RX overflow). We iterate over the bits and read a new
858 * dwords as required. The registers are cleared on read.
860 for (bit = 0; bit < 3 * nhi->hop_count; bit++) {
862 value = ioread32(nhi->iobase
863 + REG_RING_NOTIFY_BASE
865 if (++hop == nhi->hop_count) {
869 if ((value & (1 << (bit % 32))) == 0)
872 dev_warn(&nhi->pdev->dev,
873 "RX overflow for ring %d\n",
878 ring = nhi->tx_rings[hop];
880 ring = nhi->rx_rings[hop];
882 dev_warn(&nhi->pdev->dev,
883 "got interrupt for inactive %s ring %d\n",
889 spin_lock(&ring->lock);
890 __ring_interrupt(ring);
891 spin_unlock(&ring->lock);
893 spin_unlock_irq(&nhi->lock);
896 static irqreturn_t nhi_msi(int irq, void *data)
898 struct tb_nhi *nhi = data;
899 schedule_work(&nhi->interrupt_work);
903 static int __nhi_suspend_noirq(struct device *dev, bool wakeup)
905 struct pci_dev *pdev = to_pci_dev(dev);
906 struct tb *tb = pci_get_drvdata(pdev);
907 struct tb_nhi *nhi = tb->nhi;
910 ret = tb_domain_suspend_noirq(tb);
914 if (nhi->ops && nhi->ops->suspend_noirq) {
915 ret = nhi->ops->suspend_noirq(tb->nhi, wakeup);
923 static int nhi_suspend_noirq(struct device *dev)
925 return __nhi_suspend_noirq(dev, device_may_wakeup(dev));
928 static int nhi_freeze_noirq(struct device *dev)
930 struct pci_dev *pdev = to_pci_dev(dev);
931 struct tb *tb = pci_get_drvdata(pdev);
933 return tb_domain_freeze_noirq(tb);
936 static int nhi_thaw_noirq(struct device *dev)
938 struct pci_dev *pdev = to_pci_dev(dev);
939 struct tb *tb = pci_get_drvdata(pdev);
941 return tb_domain_thaw_noirq(tb);
944 static bool nhi_wake_supported(struct pci_dev *pdev)
949 * If power rails are sustainable for wakeup from S4 this
950 * property is set by the BIOS.
952 if (device_property_read_u8(&pdev->dev, "WAKE_SUPPORTED", &val))
958 static int nhi_poweroff_noirq(struct device *dev)
960 struct pci_dev *pdev = to_pci_dev(dev);
963 wakeup = device_may_wakeup(dev) && nhi_wake_supported(pdev);
964 return __nhi_suspend_noirq(dev, wakeup);
967 static void nhi_enable_int_throttling(struct tb_nhi *nhi)
969 /* Throttling is specified in 256ns increments */
970 u32 throttle = DIV_ROUND_UP(128 * NSEC_PER_USEC, 256);
974 * Configure interrupt throttling for all vectors even if we
977 for (i = 0; i < MSIX_MAX_VECS; i++) {
978 u32 reg = REG_INT_THROTTLING_RATE + i * 4;
979 iowrite32(throttle, nhi->iobase + reg);
983 static int nhi_resume_noirq(struct device *dev)
985 struct pci_dev *pdev = to_pci_dev(dev);
986 struct tb *tb = pci_get_drvdata(pdev);
987 struct tb_nhi *nhi = tb->nhi;
991 * Check that the device is still there. It may be that the user
992 * unplugged last device which causes the host controller to go
995 if (!pci_device_is_present(pdev)) {
996 nhi->going_away = true;
998 if (nhi->ops && nhi->ops->resume_noirq) {
999 ret = nhi->ops->resume_noirq(nhi);
1003 nhi_enable_int_throttling(tb->nhi);
1006 return tb_domain_resume_noirq(tb);
1009 static int nhi_suspend(struct device *dev)
1011 struct pci_dev *pdev = to_pci_dev(dev);
1012 struct tb *tb = pci_get_drvdata(pdev);
1014 return tb_domain_suspend(tb);
1017 static void nhi_complete(struct device *dev)
1019 struct pci_dev *pdev = to_pci_dev(dev);
1020 struct tb *tb = pci_get_drvdata(pdev);
1023 * If we were runtime suspended when system suspend started,
1024 * schedule runtime resume now. It should bring the domain back
1025 * to functional state.
1027 if (pm_runtime_suspended(&pdev->dev))
1028 pm_runtime_resume(&pdev->dev);
1030 tb_domain_complete(tb);
1033 static int nhi_runtime_suspend(struct device *dev)
1035 struct pci_dev *pdev = to_pci_dev(dev);
1036 struct tb *tb = pci_get_drvdata(pdev);
1037 struct tb_nhi *nhi = tb->nhi;
1040 ret = tb_domain_runtime_suspend(tb);
1044 if (nhi->ops && nhi->ops->runtime_suspend) {
1045 ret = nhi->ops->runtime_suspend(tb->nhi);
1052 static int nhi_runtime_resume(struct device *dev)
1054 struct pci_dev *pdev = to_pci_dev(dev);
1055 struct tb *tb = pci_get_drvdata(pdev);
1056 struct tb_nhi *nhi = tb->nhi;
1059 if (nhi->ops && nhi->ops->runtime_resume) {
1060 ret = nhi->ops->runtime_resume(nhi);
1065 nhi_enable_int_throttling(nhi);
1066 return tb_domain_runtime_resume(tb);
1069 static void nhi_shutdown(struct tb_nhi *nhi)
1073 dev_dbg(&nhi->pdev->dev, "shutdown\n");
1075 for (i = 0; i < nhi->hop_count; i++) {
1076 if (nhi->tx_rings[i])
1077 dev_WARN(&nhi->pdev->dev,
1078 "TX ring %d is still active\n", i);
1079 if (nhi->rx_rings[i])
1080 dev_WARN(&nhi->pdev->dev,
1081 "RX ring %d is still active\n", i);
1083 nhi_disable_interrupts(nhi);
1085 * We have to release the irq before calling flush_work. Otherwise an
1086 * already executing IRQ handler could call schedule_work again.
1088 if (!nhi->pdev->msix_enabled) {
1089 devm_free_irq(&nhi->pdev->dev, nhi->pdev->irq, nhi);
1090 flush_work(&nhi->interrupt_work);
1092 ida_destroy(&nhi->msix_ida);
1094 if (nhi->ops && nhi->ops->shutdown)
1095 nhi->ops->shutdown(nhi);
1098 static void nhi_check_quirks(struct tb_nhi *nhi)
1101 * Intel hardware supports auto clear of the interrupt status
1102 * reqister right after interrupt is being issued.
1104 if (nhi->pdev->vendor == PCI_VENDOR_ID_INTEL)
1105 nhi->quirks |= QUIRK_AUTO_CLEAR_INT;
1108 static int nhi_check_iommu_pdev(struct pci_dev *pdev, void *data)
1110 if (!pdev->external_facing ||
1111 !device_iommu_capable(&pdev->dev, IOMMU_CAP_PRE_BOOT_PROTECTION))
1113 *(bool *)data = true;
1114 return 1; /* Stop walking */
1117 static void nhi_check_iommu(struct tb_nhi *nhi)
1119 struct pci_bus *bus = nhi->pdev->bus;
1120 bool port_ok = false;
1123 * Ideally what we'd do here is grab every PCI device that
1124 * represents a tunnelling adapter for this NHI and check their
1125 * status directly, but unfortunately USB4 seems to make it
1126 * obnoxiously difficult to reliably make any correlation.
1128 * So for now we'll have to bodge it... Hoping that the system
1129 * is at least sane enough that an adapter is in the same PCI
1130 * segment as its NHI, if we can find *something* on that segment
1131 * which meets the requirements for Kernel DMA Protection, we'll
1132 * take that to imply that firmware is aware and has (hopefully)
1133 * done the right thing in general. We need to know that the PCI
1134 * layer has seen the ExternalFacingPort property which will then
1135 * inform the IOMMU layer to enforce the complete "untrusted DMA"
1136 * flow, but also that the IOMMU driver itself can be trusted not
1137 * to have been subverted by a pre-boot DMA attack.
1142 pci_walk_bus(bus, nhi_check_iommu_pdev, &port_ok);
1144 nhi->iommu_dma_protection = port_ok;
1145 dev_dbg(&nhi->pdev->dev, "IOMMU DMA protection is %s\n",
1146 str_enabled_disabled(port_ok));
1149 static int nhi_init_msi(struct tb_nhi *nhi)
1151 struct pci_dev *pdev = nhi->pdev;
1154 /* In case someone left them on. */
1155 nhi_disable_interrupts(nhi);
1157 nhi_enable_int_throttling(nhi);
1159 ida_init(&nhi->msix_ida);
1162 * The NHI has 16 MSI-X vectors or a single MSI. We first try to
1163 * get all MSI-X vectors and if we succeed, each ring will have
1164 * one MSI-X. If for some reason that does not work out, we
1165 * fallback to a single MSI.
1167 nvec = pci_alloc_irq_vectors(pdev, MSIX_MIN_VECS, MSIX_MAX_VECS,
1170 nvec = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
1174 INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
1176 irq = pci_irq_vector(nhi->pdev, 0);
1180 res = devm_request_irq(&pdev->dev, irq, nhi_msi,
1181 IRQF_NO_SUSPEND, "thunderbolt", nhi);
1183 dev_err(&pdev->dev, "request_irq failed, aborting\n");
1191 static bool nhi_imr_valid(struct pci_dev *pdev)
1195 if (!device_property_read_u8(&pdev->dev, "IMR_VALID", &val))
1201 static struct tb *nhi_select_cm(struct tb_nhi *nhi)
1206 * USB4 case is simple. If we got control of any of the
1207 * capabilities, we use software CM.
1209 if (tb_acpi_is_native())
1210 return tb_probe(nhi);
1213 * Either firmware based CM is running (we did not get control
1214 * from the firmware) or this is pre-USB4 PC so try first
1215 * firmware CM and then fallback to software CM.
1217 tb = icm_probe(nhi);
1224 static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1230 if (!nhi_imr_valid(pdev)) {
1231 dev_warn(&pdev->dev, "firmware image not valid, aborting\n");
1235 res = pcim_enable_device(pdev);
1237 dev_err(&pdev->dev, "cannot enable PCI device, aborting\n");
1241 res = pcim_iomap_regions(pdev, 1 << 0, "thunderbolt");
1243 dev_err(&pdev->dev, "cannot obtain PCI resources, aborting\n");
1247 nhi = devm_kzalloc(&pdev->dev, sizeof(*nhi), GFP_KERNEL);
1252 nhi->ops = (const struct tb_nhi_ops *)id->driver_data;
1253 /* cannot fail - table is allocated in pcim_iomap_regions */
1254 nhi->iobase = pcim_iomap_table(pdev)[0];
1255 nhi->hop_count = ioread32(nhi->iobase + REG_HOP_COUNT) & 0x3ff;
1256 dev_dbg(&pdev->dev, "total paths: %d\n", nhi->hop_count);
1258 nhi->tx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
1259 sizeof(*nhi->tx_rings), GFP_KERNEL);
1260 nhi->rx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
1261 sizeof(*nhi->rx_rings), GFP_KERNEL);
1262 if (!nhi->tx_rings || !nhi->rx_rings)
1265 nhi_check_quirks(nhi);
1266 nhi_check_iommu(nhi);
1268 res = nhi_init_msi(nhi);
1270 dev_err(&pdev->dev, "cannot enable MSI, aborting\n");
1274 spin_lock_init(&nhi->lock);
1276 res = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1278 dev_err(&pdev->dev, "failed to set DMA mask\n");
1282 pci_set_master(pdev);
1284 if (nhi->ops && nhi->ops->init) {
1285 res = nhi->ops->init(nhi);
1290 tb = nhi_select_cm(nhi);
1292 dev_err(&nhi->pdev->dev,
1293 "failed to determine connection manager, aborting\n");
1297 dev_dbg(&nhi->pdev->dev, "NHI initialized, starting thunderbolt\n");
1299 res = tb_domain_add(tb);
1302 * At this point the RX/TX rings might already have been
1303 * activated. Do a proper shutdown.
1309 pci_set_drvdata(pdev, tb);
1311 device_wakeup_enable(&pdev->dev);
1313 pm_runtime_allow(&pdev->dev);
1314 pm_runtime_set_autosuspend_delay(&pdev->dev, TB_AUTOSUSPEND_DELAY);
1315 pm_runtime_use_autosuspend(&pdev->dev);
1316 pm_runtime_put_autosuspend(&pdev->dev);
1321 static void nhi_remove(struct pci_dev *pdev)
1323 struct tb *tb = pci_get_drvdata(pdev);
1324 struct tb_nhi *nhi = tb->nhi;
1326 pm_runtime_get_sync(&pdev->dev);
1327 pm_runtime_dont_use_autosuspend(&pdev->dev);
1328 pm_runtime_forbid(&pdev->dev);
1330 tb_domain_remove(tb);
1335 * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable
1336 * the tunnels asap. A corresponding pci quirk blocks the downstream bridges
1337 * resume_noirq until we are done.
1339 static const struct dev_pm_ops nhi_pm_ops = {
1340 .suspend_noirq = nhi_suspend_noirq,
1341 .resume_noirq = nhi_resume_noirq,
1342 .freeze_noirq = nhi_freeze_noirq, /*
1343 * we just disable hotplug, the
1344 * pci-tunnels stay alive.
1346 .thaw_noirq = nhi_thaw_noirq,
1347 .restore_noirq = nhi_resume_noirq,
1348 .suspend = nhi_suspend,
1349 .poweroff_noirq = nhi_poweroff_noirq,
1350 .poweroff = nhi_suspend,
1351 .complete = nhi_complete,
1352 .runtime_suspend = nhi_runtime_suspend,
1353 .runtime_resume = nhi_runtime_resume,
1356 static struct pci_device_id nhi_ids[] = {
1358 * We have to specify class, the TB bridges use the same device and
1359 * vendor (sub)id on gen 1 and gen 2 controllers.
1362 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1363 .vendor = PCI_VENDOR_ID_INTEL,
1364 .device = PCI_DEVICE_ID_INTEL_LIGHT_RIDGE,
1365 .subvendor = 0x2222, .subdevice = 0x1111,
1368 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1369 .vendor = PCI_VENDOR_ID_INTEL,
1370 .device = PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C,
1371 .subvendor = 0x2222, .subdevice = 0x1111,
1374 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1375 .vendor = PCI_VENDOR_ID_INTEL,
1376 .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI,
1377 .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
1380 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1381 .vendor = PCI_VENDOR_ID_INTEL,
1382 .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI,
1383 .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
1387 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI) },
1388 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI) },
1389 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_USBONLY_NHI) },
1390 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI) },
1391 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_USBONLY_NHI) },
1392 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI) },
1393 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI) },
1394 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_USBONLY_NHI) },
1395 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI) },
1396 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI) },
1397 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICL_NHI0),
1398 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1399 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICL_NHI1),
1400 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1401 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_NHI0),
1402 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1403 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_NHI1),
1404 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1405 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_H_NHI0),
1406 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1407 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_H_NHI1),
1408 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1409 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL_NHI0),
1410 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1411 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL_NHI1),
1412 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1414 /* Any USB4 compliant host */
1415 { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_USB4, ~0) },
1420 MODULE_DEVICE_TABLE(pci, nhi_ids);
1421 MODULE_LICENSE("GPL");
1423 static struct pci_driver nhi_driver = {
1424 .name = "thunderbolt",
1425 .id_table = nhi_ids,
1427 .remove = nhi_remove,
1428 .shutdown = nhi_remove,
1429 .driver.pm = &nhi_pm_ops,
1432 static int __init nhi_init(void)
1436 ret = tb_domain_init();
1439 ret = pci_register_driver(&nhi_driver);
1445 static void __exit nhi_unload(void)
1447 pci_unregister_driver(&nhi_driver);
1451 rootfs_initcall(nhi_init);
1452 module_exit(nhi_unload);