Skip to content

Commit 046bee1

Browse files
westerigregkh
authored andcommitted
thunderbolt: Add MSI-X support
Intel Thunderbolt controllers support up to 16 MSI-X vectors. Using MSI-X is preferred over MSI or legacy interrupt and may bring additional performance because there is no need to check the status registers which interrupt was triggered. While there we convert comments in structs tb_ring and tb_nhi to follow kernel-doc format more closely. This code is based on the work done by Amir Levy and Michael Jamet. Signed-off-by: Michael Jamet <michael.jamet@intel.com> Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com> Reviewed-by: Yehezkel Bernat <yehezkel.bernat@intel.com> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Signed-off-by: Andreas Noever <andreas.noever@gmail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent b246635 commit 046bee1

4 files changed

Lines changed: 198 additions & 36 deletions

File tree

‎drivers/thunderbolt/ctl.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,11 +488,11 @@ struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, hotplug_cb cb, void *cb_data)
488488
if (!ctl->frame_pool)
489489
goto err;
490490

491-
ctl->tx = ring_alloc_tx(nhi, 0, 10);
491+
ctl->tx = ring_alloc_tx(nhi, 0, 10, RING_FLAG_NO_SUSPEND);
492492
if (!ctl->tx)
493493
goto err;
494494

495-
ctl->rx = ring_alloc_rx(nhi, 0, 10);
495+
ctl->rx = ring_alloc_rx(nhi, 0, 10, RING_FLAG_NO_SUSPEND);
496496
if (!ctl->rx)
497497
goto err;
498498

‎drivers/thunderbolt/nhi.c‎

Lines changed: 143 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121

2222
#define RING_TYPE(ring) ((ring)->is_tx ? "TX ring" : "RX ring")
2323

24+
/*
25+
* Minimal number of vectors when we use MSI-X. Two for control channel
26+
* Rx/Tx and the rest four are for cross domain DMA paths.
27+
*/
28+
#define MSIX_MIN_VECS 6
29+
#define MSIX_MAX_VECS 16
2430

2531
static int ring_interrupt_index(struct tb_ring *ring)
2632
{
@@ -42,6 +48,37 @@ static void ring_interrupt_active(struct tb_ring *ring, bool active)
4248
int bit = ring_interrupt_index(ring) & 31;
4349
int mask = 1 << bit;
4450
u32 old, new;
51+
52+
if (ring->irq > 0) {
53+
u32 step, shift, ivr, misc;
54+
void __iomem *ivr_base;
55+
int index;
56+
57+
if (ring->is_tx)
58+
index = ring->hop;
59+
else
60+
index = ring->hop + ring->nhi->hop_count;
61+
62+
/*
63+
* Ask the hardware to clear interrupt status bits automatically
64+
* since we already know which interrupt was triggered.
65+
*/
66+
misc = ioread32(ring->nhi->iobase + REG_DMA_MISC);
67+
if (!(misc & REG_DMA_MISC_INT_AUTO_CLEAR)) {
68+
misc |= REG_DMA_MISC_INT_AUTO_CLEAR;
69+
iowrite32(misc, ring->nhi->iobase + REG_DMA_MISC);
70+
}
71+
72+
ivr_base = ring->nhi->iobase + REG_INT_VEC_ALLOC_BASE;
73+
step = index / REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
74+
shift = index % REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
75+
ivr = ioread32(ivr_base + step);
76+
ivr &= ~(REG_INT_VEC_ALLOC_MASK << shift);
77+
if (active)
78+
ivr |= ring->vector << shift;
79+
iowrite32(ivr, ivr_base + step);
80+
}
81+
4582
old = ioread32(ring->nhi->iobase + reg);
4683
if (active)
4784
new = old | mask;
@@ -239,8 +276,50 @@ int __ring_enqueue(struct tb_ring *ring, struct ring_frame *frame)
239276
return ret;
240277
}
241278

279+
static irqreturn_t ring_msix(int irq, void *data)
280+
{
281+
struct tb_ring *ring = data;
282+
283+
schedule_work(&ring->work);
284+
return IRQ_HANDLED;
285+
}
286+
287+
static int ring_request_msix(struct tb_ring *ring, bool no_suspend)
288+
{
289+
struct tb_nhi *nhi = ring->nhi;
290+
unsigned long irqflags;
291+
int ret;
292+
293+
if (!nhi->pdev->msix_enabled)
294+
return 0;
295+
296+
ret = ida_simple_get(&nhi->msix_ida, 0, MSIX_MAX_VECS, GFP_KERNEL);
297+
if (ret < 0)
298+
return ret;
299+
300+
ring->vector = ret;
301+
302+
ring->irq = pci_irq_vector(ring->nhi->pdev, ring->vector);
303+
if (ring->irq < 0)
304+
return ring->irq;
305+
306+
irqflags = no_suspend ? IRQF_NO_SUSPEND : 0;
307+
return request_irq(ring->irq, ring_msix, irqflags, "thunderbolt", ring);
308+
}
309+
310+
static void ring_release_msix(struct tb_ring *ring)
311+
{
312+
if (ring->irq <= 0)
313+
return;
314+
315+
free_irq(ring->irq, ring);
316+
ida_simple_remove(&ring->nhi->msix_ida, ring->vector);
317+
ring->vector = 0;
318+
ring->irq = 0;
319+
}
320+
242321
static struct tb_ring *ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
243-
bool transmit)
322+
bool transmit, unsigned int flags)
244323
{
245324
struct tb_ring *ring = NULL;
246325
dev_info(&nhi->pdev->dev, "allocating %s ring %d of size %d\n",
@@ -271,9 +350,14 @@ static struct tb_ring *ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
271350
ring->hop = hop;
272351
ring->is_tx = transmit;
273352
ring->size = size;
353+
ring->flags = flags;
274354
ring->head = 0;
275355
ring->tail = 0;
276356
ring->running = false;
357+
358+
if (ring_request_msix(ring, flags & RING_FLAG_NO_SUSPEND))
359+
goto err;
360+
277361
ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev,
278362
size * sizeof(*ring->descriptors),
279363
&ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO);
@@ -295,14 +379,16 @@ static struct tb_ring *ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
295379
return NULL;
296380
}
297381

298-
struct tb_ring *ring_alloc_tx(struct tb_nhi *nhi, int hop, int size)
382+
struct tb_ring *ring_alloc_tx(struct tb_nhi *nhi, int hop, int size,
383+
unsigned int flags)
299384
{
300-
return ring_alloc(nhi, hop, size, true);
385+
return ring_alloc(nhi, hop, size, true, flags);
301386
}
302387

303-
struct tb_ring *ring_alloc_rx(struct tb_nhi *nhi, int hop, int size)
388+
struct tb_ring *ring_alloc_rx(struct tb_nhi *nhi, int hop, int size,
389+
unsigned int flags)
304390
{
305-
return ring_alloc(nhi, hop, size, false);
391+
return ring_alloc(nhi, hop, size, false, flags);
306392
}
307393

308394
/**
@@ -413,6 +499,8 @@ void ring_free(struct tb_ring *ring)
413499
RING_TYPE(ring), ring->hop);
414500
}
415501

502+
ring_release_msix(ring);
503+
416504
dma_free_coherent(&ring->nhi->pdev->dev,
417505
ring->size * sizeof(*ring->descriptors),
418506
ring->descriptors, ring->descriptors_dma);
@@ -428,9 +516,9 @@ void ring_free(struct tb_ring *ring)
428516

429517
mutex_unlock(&ring->nhi->lock);
430518
/**
431-
* ring->work can no longer be scheduled (it is scheduled only by
432-
* nhi_interrupt_work and ring_stop). Wait for it to finish before
433-
* freeing the ring.
519+
* ring->work can no longer be scheduled (it is scheduled only
520+
* by nhi_interrupt_work, ring_stop and ring_msix). Wait for it
521+
* to finish before freeing the ring.
434522
*/
435523
flush_work(&ring->work);
436524
mutex_destroy(&ring->lock);
@@ -528,9 +616,52 @@ static void nhi_shutdown(struct tb_nhi *nhi)
528616
* We have to release the irq before calling flush_work. Otherwise an
529617
* already executing IRQ handler could call schedule_work again.
530618
*/
531-
devm_free_irq(&nhi->pdev->dev, nhi->pdev->irq, nhi);
532-
flush_work(&nhi->interrupt_work);
619+
if (!nhi->pdev->msix_enabled) {
620+
devm_free_irq(&nhi->pdev->dev, nhi->pdev->irq, nhi);
621+
flush_work(&nhi->interrupt_work);
622+
}
533623
mutex_destroy(&nhi->lock);
624+
ida_destroy(&nhi->msix_ida);
625+
}
626+
627+
static int nhi_init_msi(struct tb_nhi *nhi)
628+
{
629+
struct pci_dev *pdev = nhi->pdev;
630+
int res, irq, nvec;
631+
632+
/* In case someone left them on. */
633+
nhi_disable_interrupts(nhi);
634+
635+
ida_init(&nhi->msix_ida);
636+
637+
/*
638+
* The NHI has 16 MSI-X vectors or a single MSI. We first try to
639+
* get all MSI-X vectors and if we succeed, each ring will have
640+
* one MSI-X. If for some reason that does not work out, we
641+
* fallback to a single MSI.
642+
*/
643+
nvec = pci_alloc_irq_vectors(pdev, MSIX_MIN_VECS, MSIX_MAX_VECS,
644+
PCI_IRQ_MSIX);
645+
if (nvec < 0) {
646+
nvec = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
647+
if (nvec < 0)
648+
return nvec;
649+
650+
INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
651+
652+
irq = pci_irq_vector(nhi->pdev, 0);
653+
if (irq < 0)
654+
return irq;
655+
656+
res = devm_request_irq(&pdev->dev, irq, nhi_msi,
657+
IRQF_NO_SUSPEND, "thunderbolt", nhi);
658+
if (res) {
659+
dev_err(&pdev->dev, "request_irq failed, aborting\n");
660+
return res;
661+
}
662+
}
663+
664+
return 0;
534665
}
535666

536667
static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
@@ -545,12 +676,6 @@ static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
545676
return res;
546677
}
547678

548-
res = pci_enable_msi(pdev);
549-
if (res) {
550-
dev_err(&pdev->dev, "cannot enable MSI, aborting\n");
551-
return res;
552-
}
553-
554679
res = pcim_iomap_regions(pdev, 1 << 0, "thunderbolt");
555680
if (res) {
556681
dev_err(&pdev->dev, "cannot obtain PCI resources, aborting\n");
@@ -568,7 +693,6 @@ static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
568693
if (nhi->hop_count != 12 && nhi->hop_count != 32)
569694
dev_warn(&pdev->dev, "unexpected hop count: %d\n",
570695
nhi->hop_count);
571-
INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
572696

573697
nhi->tx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
574698
sizeof(*nhi->tx_rings), GFP_KERNEL);
@@ -577,12 +701,9 @@ static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
577701
if (!nhi->tx_rings || !nhi->rx_rings)
578702
return -ENOMEM;
579703

580-
nhi_disable_interrupts(nhi); /* In case someone left them on. */
581-
res = devm_request_irq(&pdev->dev, pdev->irq, nhi_msi,
582-
IRQF_NO_SUSPEND, /* must work during _noirq */
583-
"thunderbolt", nhi);
704+
res = nhi_init_msi(nhi);
584705
if (res) {
585-
dev_err(&pdev->dev, "request_irq failed, aborting\n");
706+
dev_err(&pdev->dev, "cannot enable MSI, aborting\n");
586707
return res;
587708
}
588709

‎drivers/thunderbolt/nhi.h‎

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,75 @@
77
#ifndef DSL3510_H_
88
#define DSL3510_H_
99

10+
#include <linux/idr.h>
1011
#include <linux/mutex.h>
1112
#include <linux/workqueue.h>
1213

1314
/**
1415
* struct tb_nhi - thunderbolt native host interface
16+
* @lock: Must be held during ring creation/destruction. Is acquired by
17+
* interrupt_work when dispatching interrupts to individual rings.
18+
* @pdev: Pointer to the PCI device
19+
* @iobase: MMIO space of the NHI
20+
* @tx_rings: All Tx rings available on this host controller
21+
* @rx_rings: All Rx rings available on this host controller
22+
* @msix_ida: Used to allocate MSI-X vectors for rings
23+
* @interrupt_work: Work scheduled to handle ring interrupt when no
24+
* MSI-X is used.
25+
* @hop_count: Number of rings (end point hops) supported by NHI.
1526
*/
1627
struct tb_nhi {
17-
struct mutex lock; /*
18-
* Must be held during ring creation/destruction.
19-
* Is acquired by interrupt_work when dispatching
20-
* interrupts to individual rings.
21-
**/
28+
struct mutex lock;
2229
struct pci_dev *pdev;
2330
void __iomem *iobase;
2431
struct tb_ring **tx_rings;
2532
struct tb_ring **rx_rings;
33+
struct ida msix_ida;
2634
struct work_struct interrupt_work;
27-
u32 hop_count; /* Number of rings (end point hops) supported by NHI. */
35+
u32 hop_count;
2836
};
2937

3038
/**
3139
* struct tb_ring - thunderbolt TX or RX ring associated with a NHI
40+
* @lock: Lock serializing actions to this ring. Must be acquired after
41+
* nhi->lock.
42+
* @nhi: Pointer to the native host controller interface
43+
* @size: Size of the ring
44+
* @hop: Hop (DMA channel) associated with this ring
45+
* @head: Head of the ring (write next descriptor here)
46+
* @tail: Tail of the ring (complete next descriptor here)
47+
* @descriptors: Allocated descriptors for this ring
48+
* @queue: Queue holding frames to be transferred over this ring
49+
* @in_flight: Queue holding frames that are currently in flight
50+
* @work: Interrupt work structure
51+
* @is_tx: Is the ring Tx or Rx
52+
* @running: Is the ring running
53+
* @irq: MSI-X irq number if the ring uses MSI-X. %0 otherwise.
54+
* @vector: MSI-X vector number the ring uses (only set if @irq is > 0)
55+
* @flags: Ring specific flags
3256
*/
3357
struct tb_ring {
34-
struct mutex lock; /* must be acquired after nhi->lock */
58+
struct mutex lock;
3559
struct tb_nhi *nhi;
3660
int size;
3761
int hop;
38-
int head; /* write next descriptor here */
39-
int tail; /* complete next descriptor here */
62+
int head;
63+
int tail;
4064
struct ring_desc *descriptors;
4165
dma_addr_t descriptors_dma;
4266
struct list_head queue;
4367
struct list_head in_flight;
4468
struct work_struct work;
45-
bool is_tx:1; /* rx otherwise */
69+
bool is_tx:1;
4670
bool running:1;
71+
int irq;
72+
u8 vector;
73+
unsigned int flags;
4774
};
4875

76+
/* Leave ring interrupt enabled on suspend */
77+
#define RING_FLAG_NO_SUSPEND BIT(0)
78+
4979
struct ring_frame;
5080
typedef void (*ring_cb)(struct tb_ring*, struct ring_frame*, bool canceled);
5181

@@ -64,8 +94,10 @@ struct ring_frame {
6494

6595
#define TB_FRAME_SIZE 0x100 /* minimum size for ring_rx */
6696

67-
struct tb_ring *ring_alloc_tx(struct tb_nhi *nhi, int hop, int size);
68-
struct tb_ring *ring_alloc_rx(struct tb_nhi *nhi, int hop, int size);
97+
struct tb_ring *ring_alloc_tx(struct tb_nhi *nhi, int hop, int size,
98+
unsigned int flags);
99+
struct tb_ring *ring_alloc_rx(struct tb_nhi *nhi, int hop, int size,
100+
unsigned int flags);
69101
void ring_start(struct tb_ring *ring);
70102
void ring_stop(struct tb_ring *ring);
71103
void ring_free(struct tb_ring *ring);

‎drivers/thunderbolt/nhi_regs.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,16 @@ struct ring_desc {
9595
#define REG_RING_INTERRUPT_BASE 0x38200
9696
#define RING_INTERRUPT_REG_COUNT(nhi) ((31 + 2 * nhi->hop_count) / 32)
9797

98+
/* Interrupt Vector Allocation */
99+
#define REG_INT_VEC_ALLOC_BASE 0x38c40
100+
#define REG_INT_VEC_ALLOC_BITS 4
101+
#define REG_INT_VEC_ALLOC_MASK GENMASK(3, 0)
102+
#define REG_INT_VEC_ALLOC_REGS (32 / REG_INT_VEC_ALLOC_BITS)
103+
98104
/* The last 11 bits contain the number of hops supported by the NHI port. */
99105
#define REG_HOP_COUNT 0x39640
100106

107+
#define REG_DMA_MISC 0x39864
108+
#define REG_DMA_MISC_INT_AUTO_CLEAR BIT(2)
109+
101110
#endif

0 commit comments

Comments
 (0)