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/interrupt.h>
17 #include <linux/module.h>
18 #include <linux/delay.h>
19 #include <linux/property.h>
25 #define RING_TYPE(ring) ((ring)->is_tx ? "TX ring" : "RX ring")
27 #define RING_FIRST_USABLE_HOPID 1
30 * Minimal number of vectors when we use MSI-X. Two for control channel
31 * Rx/Tx and the rest four are for cross domain DMA paths.
33 #define MSIX_MIN_VECS 6
34 #define MSIX_MAX_VECS 16
36 #define NHI_MAILBOX_TIMEOUT 500 /* ms */
38 #define QUIRK_AUTO_CLEAR_INT BIT(0)
40 static int ring_interrupt_index(struct tb_ring *ring)
44 bit += ring->nhi->hop_count;
49 * ring_interrupt_active() - activate/deactivate interrupts for a single ring
51 * ring->nhi->lock must be held.
53 static void ring_interrupt_active(struct tb_ring *ring, bool active)
55 int reg = REG_RING_INTERRUPT_BASE +
56 ring_interrupt_index(ring) / 32 * 4;
57 int bit = ring_interrupt_index(ring) & 31;
62 u32 step, shift, ivr, misc;
63 void __iomem *ivr_base;
69 index = ring->hop + ring->nhi->hop_count;
71 if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT) {
73 * Ask the hardware to clear interrupt status
74 * bits automatically since we already know
75 * which interrupt was triggered.
77 misc = ioread32(ring->nhi->iobase + REG_DMA_MISC);
78 if (!(misc & REG_DMA_MISC_INT_AUTO_CLEAR)) {
79 misc |= REG_DMA_MISC_INT_AUTO_CLEAR;
80 iowrite32(misc, ring->nhi->iobase + REG_DMA_MISC);
84 ivr_base = ring->nhi->iobase + REG_INT_VEC_ALLOC_BASE;
85 step = index / REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
86 shift = index % REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
87 ivr = ioread32(ivr_base + step);
88 ivr &= ~(REG_INT_VEC_ALLOC_MASK << shift);
90 ivr |= ring->vector << shift;
91 iowrite32(ivr, ivr_base + step);
94 old = ioread32(ring->nhi->iobase + reg);
100 dev_dbg(&ring->nhi->pdev->dev,
101 "%s interrupt at register %#x bit %d (%#x -> %#x)\n",
102 active ? "enabling" : "disabling", reg, bit, old, new);
105 dev_WARN(&ring->nhi->pdev->dev,
106 "interrupt for %s %d is already %s\n",
107 RING_TYPE(ring), ring->hop,
108 active ? "enabled" : "disabled");
109 iowrite32(new, ring->nhi->iobase + reg);
113 * nhi_disable_interrupts() - disable interrupts for all rings
115 * Use only during init and shutdown.
117 static void nhi_disable_interrupts(struct tb_nhi *nhi)
120 /* disable interrupts */
121 for (i = 0; i < RING_INTERRUPT_REG_COUNT(nhi); i++)
122 iowrite32(0, nhi->iobase + REG_RING_INTERRUPT_BASE + 4 * i);
124 /* clear interrupt status bits */
125 for (i = 0; i < RING_NOTIFY_REG_COUNT(nhi); i++)
126 ioread32(nhi->iobase + REG_RING_NOTIFY_BASE + 4 * i);
129 /* ring helper methods */
131 static void __iomem *ring_desc_base(struct tb_ring *ring)
133 void __iomem *io = ring->nhi->iobase;
134 io += ring->is_tx ? REG_TX_RING_BASE : REG_RX_RING_BASE;
135 io += ring->hop * 16;
139 static void __iomem *ring_options_base(struct tb_ring *ring)
141 void __iomem *io = ring->nhi->iobase;
142 io += ring->is_tx ? REG_TX_OPTIONS_BASE : REG_RX_OPTIONS_BASE;
143 io += ring->hop * 32;
147 static void ring_iowrite_cons(struct tb_ring *ring, u16 cons)
150 * The other 16-bits in the register is read-only and writes to it
151 * are ignored by the hardware so we can save one ioread32() by
152 * filling the read-only bits with zeroes.
154 iowrite32(cons, ring_desc_base(ring) + 8);
157 static void ring_iowrite_prod(struct tb_ring *ring, u16 prod)
159 /* See ring_iowrite_cons() above for explanation */
160 iowrite32(prod << 16, ring_desc_base(ring) + 8);
163 static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset)
165 iowrite32(value, ring_desc_base(ring) + offset);
168 static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset)
170 iowrite32(value, ring_desc_base(ring) + offset);
171 iowrite32(value >> 32, ring_desc_base(ring) + offset + 4);
174 static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset)
176 iowrite32(value, ring_options_base(ring) + offset);
179 static bool ring_full(struct tb_ring *ring)
181 return ((ring->head + 1) % ring->size) == ring->tail;
184 static bool ring_empty(struct tb_ring *ring)
186 return ring->head == ring->tail;
190 * ring_write_descriptors() - post frames from ring->queue to the controller
192 * ring->lock is held.
194 static void ring_write_descriptors(struct tb_ring *ring)
196 struct ring_frame *frame, *n;
197 struct ring_desc *descriptor;
198 list_for_each_entry_safe(frame, n, &ring->queue, list) {
201 list_move_tail(&frame->list, &ring->in_flight);
202 descriptor = &ring->descriptors[ring->head];
203 descriptor->phys = frame->buffer_phy;
204 descriptor->time = 0;
205 descriptor->flags = RING_DESC_POSTED | RING_DESC_INTERRUPT;
207 descriptor->length = frame->size;
208 descriptor->eof = frame->eof;
209 descriptor->sof = frame->sof;
211 ring->head = (ring->head + 1) % ring->size;
213 ring_iowrite_prod(ring, ring->head);
215 ring_iowrite_cons(ring, ring->head);
220 * ring_work() - progress completed frames
222 * If the ring is shutting down then all frames are marked as canceled and
223 * their callbacks are invoked.
225 * Otherwise we collect all completed frame from the ring buffer, write new
226 * frame to the ring buffer and invoke the callbacks for the completed frames.
228 static void ring_work(struct work_struct *work)
230 struct tb_ring *ring = container_of(work, typeof(*ring), work);
231 struct ring_frame *frame;
232 bool canceled = false;
236 spin_lock_irqsave(&ring->lock, flags);
238 if (!ring->running) {
239 /* Move all frames to done and mark them as canceled. */
240 list_splice_tail_init(&ring->in_flight, &done);
241 list_splice_tail_init(&ring->queue, &done);
243 goto invoke_callback;
246 while (!ring_empty(ring)) {
247 if (!(ring->descriptors[ring->tail].flags
248 & RING_DESC_COMPLETED))
250 frame = list_first_entry(&ring->in_flight, typeof(*frame),
252 list_move_tail(&frame->list, &done);
254 frame->size = ring->descriptors[ring->tail].length;
255 frame->eof = ring->descriptors[ring->tail].eof;
256 frame->sof = ring->descriptors[ring->tail].sof;
257 frame->flags = ring->descriptors[ring->tail].flags;
259 ring->tail = (ring->tail + 1) % ring->size;
261 ring_write_descriptors(ring);
264 /* allow callbacks to schedule new work */
265 spin_unlock_irqrestore(&ring->lock, flags);
266 while (!list_empty(&done)) {
267 frame = list_first_entry(&done, typeof(*frame), list);
269 * The callback may reenqueue or delete frame.
270 * Do not hold on to it.
272 list_del_init(&frame->list);
274 frame->callback(ring, frame, canceled);
278 int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame)
283 spin_lock_irqsave(&ring->lock, flags);
285 list_add_tail(&frame->list, &ring->queue);
286 ring_write_descriptors(ring);
290 spin_unlock_irqrestore(&ring->lock, flags);
293 EXPORT_SYMBOL_GPL(__tb_ring_enqueue);
296 * tb_ring_poll() - Poll one completed frame from the ring
297 * @ring: Ring to poll
299 * This function can be called when @start_poll callback of the @ring
300 * has been called. It will read one completed frame from the ring and
301 * return it to the caller. Returns %NULL if there is no more completed
304 struct ring_frame *tb_ring_poll(struct tb_ring *ring)
306 struct ring_frame *frame = NULL;
309 spin_lock_irqsave(&ring->lock, flags);
312 if (ring_empty(ring))
315 if (ring->descriptors[ring->tail].flags & RING_DESC_COMPLETED) {
316 frame = list_first_entry(&ring->in_flight, typeof(*frame),
318 list_del_init(&frame->list);
321 frame->size = ring->descriptors[ring->tail].length;
322 frame->eof = ring->descriptors[ring->tail].eof;
323 frame->sof = ring->descriptors[ring->tail].sof;
324 frame->flags = ring->descriptors[ring->tail].flags;
327 ring->tail = (ring->tail + 1) % ring->size;
331 spin_unlock_irqrestore(&ring->lock, flags);
334 EXPORT_SYMBOL_GPL(tb_ring_poll);
336 static void __ring_interrupt_mask(struct tb_ring *ring, bool mask)
338 int idx = ring_interrupt_index(ring);
339 int reg = REG_RING_INTERRUPT_BASE + idx / 32 * 4;
343 val = ioread32(ring->nhi->iobase + reg);
348 iowrite32(val, ring->nhi->iobase + reg);
351 /* Both @nhi->lock and @ring->lock should be held */
352 static void __ring_interrupt(struct tb_ring *ring)
357 if (ring->start_poll) {
358 __ring_interrupt_mask(ring, true);
359 ring->start_poll(ring->poll_data);
361 schedule_work(&ring->work);
366 * tb_ring_poll_complete() - Re-start interrupt for the ring
367 * @ring: Ring to re-start the interrupt
369 * This will re-start (unmask) the ring interrupt once the user is done
372 void tb_ring_poll_complete(struct tb_ring *ring)
376 spin_lock_irqsave(&ring->nhi->lock, flags);
377 spin_lock(&ring->lock);
378 if (ring->start_poll)
379 __ring_interrupt_mask(ring, false);
380 spin_unlock(&ring->lock);
381 spin_unlock_irqrestore(&ring->nhi->lock, flags);
383 EXPORT_SYMBOL_GPL(tb_ring_poll_complete);
385 static void ring_clear_msix(const struct tb_ring *ring)
387 if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT)
391 ioread32(ring->nhi->iobase + REG_RING_NOTIFY_BASE);
393 ioread32(ring->nhi->iobase + REG_RING_NOTIFY_BASE +
394 4 * (ring->nhi->hop_count / 32));
397 static irqreturn_t ring_msix(int irq, void *data)
399 struct tb_ring *ring = data;
401 spin_lock(&ring->nhi->lock);
402 ring_clear_msix(ring);
403 spin_lock(&ring->lock);
404 __ring_interrupt(ring);
405 spin_unlock(&ring->lock);
406 spin_unlock(&ring->nhi->lock);
411 static int ring_request_msix(struct tb_ring *ring, bool no_suspend)
413 struct tb_nhi *nhi = ring->nhi;
414 unsigned long irqflags;
417 if (!nhi->pdev->msix_enabled)
420 ret = ida_simple_get(&nhi->msix_ida, 0, MSIX_MAX_VECS, GFP_KERNEL);
426 ret = pci_irq_vector(ring->nhi->pdev, ring->vector);
432 irqflags = no_suspend ? IRQF_NO_SUSPEND : 0;
433 ret = request_irq(ring->irq, ring_msix, irqflags, "thunderbolt", ring);
440 ida_simple_remove(&nhi->msix_ida, ring->vector);
445 static void ring_release_msix(struct tb_ring *ring)
450 free_irq(ring->irq, ring);
451 ida_simple_remove(&ring->nhi->msix_ida, ring->vector);
456 static int nhi_alloc_hop(struct tb_nhi *nhi, struct tb_ring *ring)
460 spin_lock_irq(&nhi->lock);
466 * Automatically allocate HopID from the non-reserved
467 * range 1 .. hop_count - 1.
469 for (i = RING_FIRST_USABLE_HOPID; i < nhi->hop_count; i++) {
471 if (!nhi->tx_rings[i]) {
476 if (!nhi->rx_rings[i]) {
484 if (ring->hop < 0 || ring->hop >= nhi->hop_count) {
485 dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop);
489 if (ring->is_tx && nhi->tx_rings[ring->hop]) {
490 dev_warn(&nhi->pdev->dev, "TX hop %d already allocated\n",
494 } else if (!ring->is_tx && nhi->rx_rings[ring->hop]) {
495 dev_warn(&nhi->pdev->dev, "RX hop %d already allocated\n",
502 nhi->tx_rings[ring->hop] = ring;
504 nhi->rx_rings[ring->hop] = ring;
507 spin_unlock_irq(&nhi->lock);
512 static struct tb_ring *tb_ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
513 bool transmit, unsigned int flags,
514 int e2e_tx_hop, u16 sof_mask, u16 eof_mask,
515 void (*start_poll)(void *),
518 struct tb_ring *ring = NULL;
520 dev_dbg(&nhi->pdev->dev, "allocating %s ring %d of size %d\n",
521 transmit ? "TX" : "RX", hop, size);
523 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
527 spin_lock_init(&ring->lock);
528 INIT_LIST_HEAD(&ring->queue);
529 INIT_LIST_HEAD(&ring->in_flight);
530 INIT_WORK(&ring->work, ring_work);
534 ring->is_tx = transmit;
537 ring->e2e_tx_hop = e2e_tx_hop;
538 ring->sof_mask = sof_mask;
539 ring->eof_mask = eof_mask;
542 ring->running = false;
543 ring->start_poll = start_poll;
544 ring->poll_data = poll_data;
546 ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev,
547 size * sizeof(*ring->descriptors),
548 &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO);
549 if (!ring->descriptors)
552 if (ring_request_msix(ring, flags & RING_FLAG_NO_SUSPEND))
555 if (nhi_alloc_hop(nhi, ring))
556 goto err_release_msix;
561 ring_release_msix(ring);
563 dma_free_coherent(&ring->nhi->pdev->dev,
564 ring->size * sizeof(*ring->descriptors),
565 ring->descriptors, ring->descriptors_dma);
573 * tb_ring_alloc_tx() - Allocate DMA ring for transmit
574 * @nhi: Pointer to the NHI the ring is to be allocated
575 * @hop: HopID (ring) to allocate
576 * @size: Number of entries in the ring
577 * @flags: Flags for the ring
579 struct tb_ring *tb_ring_alloc_tx(struct tb_nhi *nhi, int hop, int size,
582 return tb_ring_alloc(nhi, hop, size, true, flags, 0, 0, 0, NULL, NULL);
584 EXPORT_SYMBOL_GPL(tb_ring_alloc_tx);
587 * tb_ring_alloc_rx() - Allocate DMA ring for receive
588 * @nhi: Pointer to the NHI the ring is to be allocated
589 * @hop: HopID (ring) to allocate. Pass %-1 for automatic allocation.
590 * @size: Number of entries in the ring
591 * @flags: Flags for the ring
592 * @e2e_tx_hop: Transmit HopID when E2E is enabled in @flags
593 * @sof_mask: Mask of PDF values that start a frame
594 * @eof_mask: Mask of PDF values that end a frame
595 * @start_poll: If not %NULL the ring will call this function when an
596 * interrupt is triggered and masked, instead of callback
598 * @poll_data: Optional data passed to @start_poll
600 struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size,
601 unsigned int flags, int e2e_tx_hop,
602 u16 sof_mask, u16 eof_mask,
603 void (*start_poll)(void *), void *poll_data)
605 return tb_ring_alloc(nhi, hop, size, false, flags, e2e_tx_hop, sof_mask, eof_mask,
606 start_poll, poll_data);
608 EXPORT_SYMBOL_GPL(tb_ring_alloc_rx);
611 * tb_ring_start() - enable a ring
612 * @ring: Ring to start
614 * Must not be invoked in parallel with tb_ring_stop().
616 void tb_ring_start(struct tb_ring *ring)
621 spin_lock_irq(&ring->nhi->lock);
622 spin_lock(&ring->lock);
623 if (ring->nhi->going_away)
626 dev_WARN(&ring->nhi->pdev->dev, "ring already started\n");
629 dev_dbg(&ring->nhi->pdev->dev, "starting %s %d\n",
630 RING_TYPE(ring), ring->hop);
632 if (ring->flags & RING_FLAG_FRAME) {
635 flags = RING_FLAG_ENABLE;
637 frame_size = TB_FRAME_SIZE;
638 flags = RING_FLAG_ENABLE | RING_FLAG_RAW;
641 ring_iowrite64desc(ring, ring->descriptors_dma, 0);
643 ring_iowrite32desc(ring, ring->size, 12);
644 ring_iowrite32options(ring, 0, 4); /* time releated ? */
645 ring_iowrite32options(ring, flags, 0);
647 u32 sof_eof_mask = ring->sof_mask << 16 | ring->eof_mask;
649 ring_iowrite32desc(ring, (frame_size << 16) | ring->size, 12);
650 ring_iowrite32options(ring, sof_eof_mask, 4);
651 ring_iowrite32options(ring, flags, 0);
655 * Now that the ring valid bit is set we can configure E2E if
656 * enabled for the ring.
658 if (ring->flags & RING_FLAG_E2E) {
662 hop = ring->e2e_tx_hop << REG_RX_OPTIONS_E2E_HOP_SHIFT;
663 hop &= REG_RX_OPTIONS_E2E_HOP_MASK;
666 dev_dbg(&ring->nhi->pdev->dev,
667 "enabling E2E for %s %d with TX HopID %d\n",
668 RING_TYPE(ring), ring->hop, ring->e2e_tx_hop);
670 dev_dbg(&ring->nhi->pdev->dev, "enabling E2E for %s %d\n",
671 RING_TYPE(ring), ring->hop);
674 flags |= RING_FLAG_E2E_FLOW_CONTROL;
675 ring_iowrite32options(ring, flags, 0);
678 ring_interrupt_active(ring, true);
679 ring->running = true;
681 spin_unlock(&ring->lock);
682 spin_unlock_irq(&ring->nhi->lock);
684 EXPORT_SYMBOL_GPL(tb_ring_start);
687 * tb_ring_stop() - shutdown a ring
688 * @ring: Ring to stop
690 * Must not be invoked from a callback.
692 * This method will disable the ring. Further calls to
693 * tb_ring_tx/tb_ring_rx will return -ESHUTDOWN until ring_stop has been
696 * All enqueued frames will be canceled and their callbacks will be executed
697 * with frame->canceled set to true (on the callback thread). This method
698 * returns only after all callback invocations have finished.
700 void tb_ring_stop(struct tb_ring *ring)
702 spin_lock_irq(&ring->nhi->lock);
703 spin_lock(&ring->lock);
704 dev_dbg(&ring->nhi->pdev->dev, "stopping %s %d\n",
705 RING_TYPE(ring), ring->hop);
706 if (ring->nhi->going_away)
708 if (!ring->running) {
709 dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n",
710 RING_TYPE(ring), ring->hop);
713 ring_interrupt_active(ring, false);
715 ring_iowrite32options(ring, 0, 0);
716 ring_iowrite64desc(ring, 0, 0);
717 ring_iowrite32desc(ring, 0, 8);
718 ring_iowrite32desc(ring, 0, 12);
721 ring->running = false;
724 spin_unlock(&ring->lock);
725 spin_unlock_irq(&ring->nhi->lock);
728 * schedule ring->work to invoke callbacks on all remaining frames.
730 schedule_work(&ring->work);
731 flush_work(&ring->work);
733 EXPORT_SYMBOL_GPL(tb_ring_stop);
736 * tb_ring_free() - free ring
738 * When this method returns all invocations of ring->callback will have
741 * Ring must be stopped.
743 * Must NOT be called from ring_frame->callback!
745 void tb_ring_free(struct tb_ring *ring)
747 spin_lock_irq(&ring->nhi->lock);
749 * Dissociate the ring from the NHI. This also ensures that
750 * nhi_interrupt_work cannot reschedule ring->work.
753 ring->nhi->tx_rings[ring->hop] = NULL;
755 ring->nhi->rx_rings[ring->hop] = NULL;
758 dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n",
759 RING_TYPE(ring), ring->hop);
761 spin_unlock_irq(&ring->nhi->lock);
763 ring_release_msix(ring);
765 dma_free_coherent(&ring->nhi->pdev->dev,
766 ring->size * sizeof(*ring->descriptors),
767 ring->descriptors, ring->descriptors_dma);
769 ring->descriptors = NULL;
770 ring->descriptors_dma = 0;
773 dev_dbg(&ring->nhi->pdev->dev, "freeing %s %d\n", RING_TYPE(ring),
777 * ring->work can no longer be scheduled (it is scheduled only
778 * by nhi_interrupt_work, ring_stop and ring_msix). Wait for it
779 * to finish before freeing the ring.
781 flush_work(&ring->work);
784 EXPORT_SYMBOL_GPL(tb_ring_free);
787 * nhi_mailbox_cmd() - Send a command through NHI mailbox
788 * @nhi: Pointer to the NHI structure
789 * @cmd: Command to send
790 * @data: Data to be send with the command
792 * Sends mailbox command to the firmware running on NHI. Returns %0 in
793 * case of success and negative errno in case of failure.
795 int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data)
800 iowrite32(data, nhi->iobase + REG_INMAIL_DATA);
802 val = ioread32(nhi->iobase + REG_INMAIL_CMD);
803 val &= ~(REG_INMAIL_CMD_MASK | REG_INMAIL_ERROR);
804 val |= REG_INMAIL_OP_REQUEST | cmd;
805 iowrite32(val, nhi->iobase + REG_INMAIL_CMD);
807 timeout = ktime_add_ms(ktime_get(), NHI_MAILBOX_TIMEOUT);
809 val = ioread32(nhi->iobase + REG_INMAIL_CMD);
810 if (!(val & REG_INMAIL_OP_REQUEST))
812 usleep_range(10, 20);
813 } while (ktime_before(ktime_get(), timeout));
815 if (val & REG_INMAIL_OP_REQUEST)
817 if (val & REG_INMAIL_ERROR)
824 * nhi_mailbox_mode() - Return current firmware operation mode
825 * @nhi: Pointer to the NHI structure
827 * The function reads current firmware operation mode using NHI mailbox
828 * registers and returns it to the caller.
830 enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi)
834 val = ioread32(nhi->iobase + REG_OUTMAIL_CMD);
835 val &= REG_OUTMAIL_CMD_OPMODE_MASK;
836 val >>= REG_OUTMAIL_CMD_OPMODE_SHIFT;
838 return (enum nhi_fw_mode)val;
841 static void nhi_interrupt_work(struct work_struct *work)
843 struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work);
844 int value = 0; /* Suppress uninitialized usage warning. */
847 int type = 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */
848 struct tb_ring *ring;
850 spin_lock_irq(&nhi->lock);
853 * Starting at REG_RING_NOTIFY_BASE there are three status bitfields
854 * (TX, RX, RX overflow). We iterate over the bits and read a new
855 * dwords as required. The registers are cleared on read.
857 for (bit = 0; bit < 3 * nhi->hop_count; bit++) {
859 value = ioread32(nhi->iobase
860 + REG_RING_NOTIFY_BASE
862 if (++hop == nhi->hop_count) {
866 if ((value & (1 << (bit % 32))) == 0)
869 dev_warn(&nhi->pdev->dev,
870 "RX overflow for ring %d\n",
875 ring = nhi->tx_rings[hop];
877 ring = nhi->rx_rings[hop];
879 dev_warn(&nhi->pdev->dev,
880 "got interrupt for inactive %s ring %d\n",
886 spin_lock(&ring->lock);
887 __ring_interrupt(ring);
888 spin_unlock(&ring->lock);
890 spin_unlock_irq(&nhi->lock);
893 static irqreturn_t nhi_msi(int irq, void *data)
895 struct tb_nhi *nhi = data;
896 schedule_work(&nhi->interrupt_work);
900 static int __nhi_suspend_noirq(struct device *dev, bool wakeup)
902 struct pci_dev *pdev = to_pci_dev(dev);
903 struct tb *tb = pci_get_drvdata(pdev);
904 struct tb_nhi *nhi = tb->nhi;
907 ret = tb_domain_suspend_noirq(tb);
911 if (nhi->ops && nhi->ops->suspend_noirq) {
912 ret = nhi->ops->suspend_noirq(tb->nhi, wakeup);
920 static int nhi_suspend_noirq(struct device *dev)
922 return __nhi_suspend_noirq(dev, device_may_wakeup(dev));
925 static int nhi_freeze_noirq(struct device *dev)
927 struct pci_dev *pdev = to_pci_dev(dev);
928 struct tb *tb = pci_get_drvdata(pdev);
930 return tb_domain_freeze_noirq(tb);
933 static int nhi_thaw_noirq(struct device *dev)
935 struct pci_dev *pdev = to_pci_dev(dev);
936 struct tb *tb = pci_get_drvdata(pdev);
938 return tb_domain_thaw_noirq(tb);
941 static bool nhi_wake_supported(struct pci_dev *pdev)
946 * If power rails are sustainable for wakeup from S4 this
947 * property is set by the BIOS.
949 if (device_property_read_u8(&pdev->dev, "WAKE_SUPPORTED", &val))
955 static int nhi_poweroff_noirq(struct device *dev)
957 struct pci_dev *pdev = to_pci_dev(dev);
960 wakeup = device_may_wakeup(dev) && nhi_wake_supported(pdev);
961 return __nhi_suspend_noirq(dev, wakeup);
964 static void nhi_enable_int_throttling(struct tb_nhi *nhi)
966 /* Throttling is specified in 256ns increments */
967 u32 throttle = DIV_ROUND_UP(128 * NSEC_PER_USEC, 256);
971 * Configure interrupt throttling for all vectors even if we
974 for (i = 0; i < MSIX_MAX_VECS; i++) {
975 u32 reg = REG_INT_THROTTLING_RATE + i * 4;
976 iowrite32(throttle, nhi->iobase + reg);
980 static int nhi_resume_noirq(struct device *dev)
982 struct pci_dev *pdev = to_pci_dev(dev);
983 struct tb *tb = pci_get_drvdata(pdev);
984 struct tb_nhi *nhi = tb->nhi;
988 * Check that the device is still there. It may be that the user
989 * unplugged last device which causes the host controller to go
992 if (!pci_device_is_present(pdev)) {
993 nhi->going_away = true;
995 if (nhi->ops && nhi->ops->resume_noirq) {
996 ret = nhi->ops->resume_noirq(nhi);
1000 nhi_enable_int_throttling(tb->nhi);
1003 return tb_domain_resume_noirq(tb);
1006 static int nhi_suspend(struct device *dev)
1008 struct pci_dev *pdev = to_pci_dev(dev);
1009 struct tb *tb = pci_get_drvdata(pdev);
1011 return tb_domain_suspend(tb);
1014 static void nhi_complete(struct device *dev)
1016 struct pci_dev *pdev = to_pci_dev(dev);
1017 struct tb *tb = pci_get_drvdata(pdev);
1020 * If we were runtime suspended when system suspend started,
1021 * schedule runtime resume now. It should bring the domain back
1022 * to functional state.
1024 if (pm_runtime_suspended(&pdev->dev))
1025 pm_runtime_resume(&pdev->dev);
1027 tb_domain_complete(tb);
1030 static int nhi_runtime_suspend(struct device *dev)
1032 struct pci_dev *pdev = to_pci_dev(dev);
1033 struct tb *tb = pci_get_drvdata(pdev);
1034 struct tb_nhi *nhi = tb->nhi;
1037 ret = tb_domain_runtime_suspend(tb);
1041 if (nhi->ops && nhi->ops->runtime_suspend) {
1042 ret = nhi->ops->runtime_suspend(tb->nhi);
1049 static int nhi_runtime_resume(struct device *dev)
1051 struct pci_dev *pdev = to_pci_dev(dev);
1052 struct tb *tb = pci_get_drvdata(pdev);
1053 struct tb_nhi *nhi = tb->nhi;
1056 if (nhi->ops && nhi->ops->runtime_resume) {
1057 ret = nhi->ops->runtime_resume(nhi);
1062 nhi_enable_int_throttling(nhi);
1063 return tb_domain_runtime_resume(tb);
1066 static void nhi_shutdown(struct tb_nhi *nhi)
1070 dev_dbg(&nhi->pdev->dev, "shutdown\n");
1072 for (i = 0; i < nhi->hop_count; i++) {
1073 if (nhi->tx_rings[i])
1074 dev_WARN(&nhi->pdev->dev,
1075 "TX ring %d is still active\n", i);
1076 if (nhi->rx_rings[i])
1077 dev_WARN(&nhi->pdev->dev,
1078 "RX ring %d is still active\n", i);
1080 nhi_disable_interrupts(nhi);
1082 * We have to release the irq before calling flush_work. Otherwise an
1083 * already executing IRQ handler could call schedule_work again.
1085 if (!nhi->pdev->msix_enabled) {
1086 devm_free_irq(&nhi->pdev->dev, nhi->pdev->irq, nhi);
1087 flush_work(&nhi->interrupt_work);
1089 ida_destroy(&nhi->msix_ida);
1091 if (nhi->ops && nhi->ops->shutdown)
1092 nhi->ops->shutdown(nhi);
1095 static void nhi_check_quirks(struct tb_nhi *nhi)
1098 * Intel hardware supports auto clear of the interrupt status
1099 * reqister right after interrupt is being issued.
1101 if (nhi->pdev->vendor == PCI_VENDOR_ID_INTEL)
1102 nhi->quirks |= QUIRK_AUTO_CLEAR_INT;
1105 static int nhi_init_msi(struct tb_nhi *nhi)
1107 struct pci_dev *pdev = nhi->pdev;
1110 /* In case someone left them on. */
1111 nhi_disable_interrupts(nhi);
1113 nhi_enable_int_throttling(nhi);
1115 ida_init(&nhi->msix_ida);
1118 * The NHI has 16 MSI-X vectors or a single MSI. We first try to
1119 * get all MSI-X vectors and if we succeed, each ring will have
1120 * one MSI-X. If for some reason that does not work out, we
1121 * fallback to a single MSI.
1123 nvec = pci_alloc_irq_vectors(pdev, MSIX_MIN_VECS, MSIX_MAX_VECS,
1126 nvec = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
1130 INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
1132 irq = pci_irq_vector(nhi->pdev, 0);
1136 res = devm_request_irq(&pdev->dev, irq, nhi_msi,
1137 IRQF_NO_SUSPEND, "thunderbolt", nhi);
1139 dev_err(&pdev->dev, "request_irq failed, aborting\n");
1147 static bool nhi_imr_valid(struct pci_dev *pdev)
1151 if (!device_property_read_u8(&pdev->dev, "IMR_VALID", &val))
1157 static struct tb *nhi_select_cm(struct tb_nhi *nhi)
1162 * USB4 case is simple. If we got control of any of the
1163 * capabilities, we use software CM.
1165 if (tb_acpi_is_native())
1166 return tb_probe(nhi);
1169 * Either firmware based CM is running (we did not get control
1170 * from the firmware) or this is pre-USB4 PC so try first
1171 * firmware CM and then fallback to software CM.
1173 tb = icm_probe(nhi);
1180 static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1186 if (!nhi_imr_valid(pdev)) {
1187 dev_warn(&pdev->dev, "firmware image not valid, aborting\n");
1191 res = pcim_enable_device(pdev);
1193 dev_err(&pdev->dev, "cannot enable PCI device, aborting\n");
1197 res = pcim_iomap_regions(pdev, 1 << 0, "thunderbolt");
1199 dev_err(&pdev->dev, "cannot obtain PCI resources, aborting\n");
1203 nhi = devm_kzalloc(&pdev->dev, sizeof(*nhi), GFP_KERNEL);
1208 nhi->ops = (const struct tb_nhi_ops *)id->driver_data;
1209 /* cannot fail - table is allocated bin pcim_iomap_regions */
1210 nhi->iobase = pcim_iomap_table(pdev)[0];
1211 nhi->hop_count = ioread32(nhi->iobase + REG_HOP_COUNT) & 0x3ff;
1212 dev_dbg(&pdev->dev, "total paths: %d\n", nhi->hop_count);
1214 nhi->tx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
1215 sizeof(*nhi->tx_rings), GFP_KERNEL);
1216 nhi->rx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
1217 sizeof(*nhi->rx_rings), GFP_KERNEL);
1218 if (!nhi->tx_rings || !nhi->rx_rings)
1221 nhi_check_quirks(nhi);
1223 res = nhi_init_msi(nhi);
1225 dev_err(&pdev->dev, "cannot enable MSI, aborting\n");
1229 spin_lock_init(&nhi->lock);
1231 res = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1233 res = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1235 dev_err(&pdev->dev, "failed to set DMA mask\n");
1239 pci_set_master(pdev);
1241 if (nhi->ops && nhi->ops->init) {
1242 res = nhi->ops->init(nhi);
1247 tb = nhi_select_cm(nhi);
1249 dev_err(&nhi->pdev->dev,
1250 "failed to determine connection manager, aborting\n");
1254 dev_dbg(&nhi->pdev->dev, "NHI initialized, starting thunderbolt\n");
1256 res = tb_domain_add(tb);
1259 * At this point the RX/TX rings might already have been
1260 * activated. Do a proper shutdown.
1266 pci_set_drvdata(pdev, tb);
1268 device_wakeup_enable(&pdev->dev);
1270 pm_runtime_allow(&pdev->dev);
1271 pm_runtime_set_autosuspend_delay(&pdev->dev, TB_AUTOSUSPEND_DELAY);
1272 pm_runtime_use_autosuspend(&pdev->dev);
1273 pm_runtime_put_autosuspend(&pdev->dev);
1278 static void nhi_remove(struct pci_dev *pdev)
1280 struct tb *tb = pci_get_drvdata(pdev);
1281 struct tb_nhi *nhi = tb->nhi;
1283 pm_runtime_get_sync(&pdev->dev);
1284 pm_runtime_dont_use_autosuspend(&pdev->dev);
1285 pm_runtime_forbid(&pdev->dev);
1287 tb_domain_remove(tb);
1292 * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable
1293 * the tunnels asap. A corresponding pci quirk blocks the downstream bridges
1294 * resume_noirq until we are done.
1296 static const struct dev_pm_ops nhi_pm_ops = {
1297 .suspend_noirq = nhi_suspend_noirq,
1298 .resume_noirq = nhi_resume_noirq,
1299 .freeze_noirq = nhi_freeze_noirq, /*
1300 * we just disable hotplug, the
1301 * pci-tunnels stay alive.
1303 .thaw_noirq = nhi_thaw_noirq,
1304 .restore_noirq = nhi_resume_noirq,
1305 .suspend = nhi_suspend,
1306 .poweroff_noirq = nhi_poweroff_noirq,
1307 .poweroff = nhi_suspend,
1308 .complete = nhi_complete,
1309 .runtime_suspend = nhi_runtime_suspend,
1310 .runtime_resume = nhi_runtime_resume,
1313 static struct pci_device_id nhi_ids[] = {
1315 * We have to specify class, the TB bridges use the same device and
1316 * vendor (sub)id on gen 1 and gen 2 controllers.
1319 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1320 .vendor = PCI_VENDOR_ID_INTEL,
1321 .device = PCI_DEVICE_ID_INTEL_LIGHT_RIDGE,
1322 .subvendor = 0x2222, .subdevice = 0x1111,
1325 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1326 .vendor = PCI_VENDOR_ID_INTEL,
1327 .device = PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C,
1328 .subvendor = 0x2222, .subdevice = 0x1111,
1331 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1332 .vendor = PCI_VENDOR_ID_INTEL,
1333 .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI,
1334 .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
1337 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
1338 .vendor = PCI_VENDOR_ID_INTEL,
1339 .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI,
1340 .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
1344 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI) },
1345 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI) },
1346 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_USBONLY_NHI) },
1347 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI) },
1348 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_USBONLY_NHI) },
1349 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI) },
1350 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI) },
1351 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_USBONLY_NHI) },
1352 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI) },
1353 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI) },
1354 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICL_NHI0),
1355 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1356 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICL_NHI1),
1357 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1358 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_NHI0),
1359 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1360 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_NHI1),
1361 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1362 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_H_NHI0),
1363 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1364 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_H_NHI1),
1365 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1366 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL_NHI0),
1367 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1368 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL_NHI1),
1369 .driver_data = (kernel_ulong_t)&icl_nhi_ops },
1371 /* Any USB4 compliant host */
1372 { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_USB4, ~0) },
1377 MODULE_DEVICE_TABLE(pci, nhi_ids);
1378 MODULE_LICENSE("GPL");
1380 static struct pci_driver nhi_driver = {
1381 .name = "thunderbolt",
1382 .id_table = nhi_ids,
1384 .remove = nhi_remove,
1385 .shutdown = nhi_remove,
1386 .driver.pm = &nhi_pm_ops,
1389 static int __init nhi_init(void)
1393 ret = tb_domain_init();
1396 ret = pci_register_driver(&nhi_driver);
1402 static void __exit nhi_unload(void)
1404 pci_unregister_driver(&nhi_driver);
1408 rootfs_initcall(nhi_init);
1409 module_exit(nhi_unload);