2 * Thunderbolt Cactus Ridge driver - NHI driver
4 * The NHI (native host interface) is the pci device that allows us to send and
5 * receive frames from the thunderbolt bus.
7 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
10 #include <linux/pm_runtime.h>
11 #include <linux/slab.h>
12 #include <linux/errno.h>
13 #include <linux/pci.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/dmi.h>
22 #define RING_TYPE(ring) ((ring)->is_tx ? "TX ring" : "RX ring")
25 static int ring_interrupt_index(struct tb_ring
*ring
)
29 bit
+= ring
->nhi
->hop_count
;
34 * ring_interrupt_active() - activate/deactivate interrupts for a single ring
36 * ring->nhi->lock must be held.
38 static void ring_interrupt_active(struct tb_ring
*ring
, bool active
)
40 int reg
= REG_RING_INTERRUPT_BASE
+ ring_interrupt_index(ring
) / 32;
41 int bit
= ring_interrupt_index(ring
) & 31;
44 old
= ioread32(ring
->nhi
->iobase
+ reg
);
50 dev_info(&ring
->nhi
->pdev
->dev
,
51 "%s interrupt at register %#x bit %d (%#x -> %#x)\n",
52 active
? "enabling" : "disabling", reg
, bit
, old
, new);
55 dev_WARN(&ring
->nhi
->pdev
->dev
,
56 "interrupt for %s %d is already %s\n",
57 RING_TYPE(ring
), ring
->hop
,
58 active
? "enabled" : "disabled");
59 iowrite32(new, ring
->nhi
->iobase
+ reg
);
63 * nhi_disable_interrupts() - disable interrupts for all rings
65 * Use only during init and shutdown.
67 static void nhi_disable_interrupts(struct tb_nhi
*nhi
)
70 /* disable interrupts */
71 for (i
= 0; i
< RING_INTERRUPT_REG_COUNT(nhi
); i
++)
72 iowrite32(0, nhi
->iobase
+ REG_RING_INTERRUPT_BASE
+ 4 * i
);
74 /* clear interrupt status bits */
75 for (i
= 0; i
< RING_NOTIFY_REG_COUNT(nhi
); i
++)
76 ioread32(nhi
->iobase
+ REG_RING_NOTIFY_BASE
+ 4 * i
);
79 /* ring helper methods */
81 static void __iomem
*ring_desc_base(struct tb_ring
*ring
)
83 void __iomem
*io
= ring
->nhi
->iobase
;
84 io
+= ring
->is_tx
? REG_TX_RING_BASE
: REG_RX_RING_BASE
;
89 static void __iomem
*ring_options_base(struct tb_ring
*ring
)
91 void __iomem
*io
= ring
->nhi
->iobase
;
92 io
+= ring
->is_tx
? REG_TX_OPTIONS_BASE
: REG_RX_OPTIONS_BASE
;
97 static void ring_iowrite16desc(struct tb_ring
*ring
, u32 value
, u32 offset
)
99 iowrite16(value
, ring_desc_base(ring
) + offset
);
102 static void ring_iowrite32desc(struct tb_ring
*ring
, u32 value
, u32 offset
)
104 iowrite32(value
, ring_desc_base(ring
) + offset
);
107 static void ring_iowrite64desc(struct tb_ring
*ring
, u64 value
, u32 offset
)
109 iowrite32(value
, ring_desc_base(ring
) + offset
);
110 iowrite32(value
>> 32, ring_desc_base(ring
) + offset
+ 4);
113 static void ring_iowrite32options(struct tb_ring
*ring
, u32 value
, u32 offset
)
115 iowrite32(value
, ring_options_base(ring
) + offset
);
118 static bool ring_full(struct tb_ring
*ring
)
120 return ((ring
->head
+ 1) % ring
->size
) == ring
->tail
;
123 static bool ring_empty(struct tb_ring
*ring
)
125 return ring
->head
== ring
->tail
;
129 * ring_write_descriptors() - post frames from ring->queue to the controller
131 * ring->lock is held.
133 static void ring_write_descriptors(struct tb_ring
*ring
)
135 struct ring_frame
*frame
, *n
;
136 struct ring_desc
*descriptor
;
137 list_for_each_entry_safe(frame
, n
, &ring
->queue
, list
) {
140 list_move_tail(&frame
->list
, &ring
->in_flight
);
141 descriptor
= &ring
->descriptors
[ring
->head
];
142 descriptor
->phys
= frame
->buffer_phy
;
143 descriptor
->time
= 0;
144 descriptor
->flags
= RING_DESC_POSTED
| RING_DESC_INTERRUPT
;
146 descriptor
->length
= frame
->size
;
147 descriptor
->eof
= frame
->eof
;
148 descriptor
->sof
= frame
->sof
;
150 ring
->head
= (ring
->head
+ 1) % ring
->size
;
151 ring_iowrite16desc(ring
, ring
->head
, ring
->is_tx
? 10 : 8);
156 * ring_work() - progress completed frames
158 * If the ring is shutting down then all frames are marked as canceled and
159 * their callbacks are invoked.
161 * Otherwise we collect all completed frame from the ring buffer, write new
162 * frame to the ring buffer and invoke the callbacks for the completed frames.
164 static void ring_work(struct work_struct
*work
)
166 struct tb_ring
*ring
= container_of(work
, typeof(*ring
), work
);
167 struct ring_frame
*frame
;
168 bool canceled
= false;
170 mutex_lock(&ring
->lock
);
172 if (!ring
->running
) {
173 /* Move all frames to done and mark them as canceled. */
174 list_splice_tail_init(&ring
->in_flight
, &done
);
175 list_splice_tail_init(&ring
->queue
, &done
);
177 goto invoke_callback
;
180 while (!ring_empty(ring
)) {
181 if (!(ring
->descriptors
[ring
->tail
].flags
182 & RING_DESC_COMPLETED
))
184 frame
= list_first_entry(&ring
->in_flight
, typeof(*frame
),
186 list_move_tail(&frame
->list
, &done
);
188 frame
->size
= ring
->descriptors
[ring
->tail
].length
;
189 frame
->eof
= ring
->descriptors
[ring
->tail
].eof
;
190 frame
->sof
= ring
->descriptors
[ring
->tail
].sof
;
191 frame
->flags
= ring
->descriptors
[ring
->tail
].flags
;
193 dev_WARN(&ring
->nhi
->pdev
->dev
,
194 "%s %d got unexpected SOF: %#x\n",
195 RING_TYPE(ring
), ring
->hop
,
199 * raw not enabled, interupt not set: 0x2=0010
200 * raw enabled: 0xa=1010
201 * raw not enabled: 0xb=1011
202 * partial frame (>MAX_FRAME_SIZE): 0xe=1110
204 if (frame
->flags
!= 0xa)
205 dev_WARN(&ring
->nhi
->pdev
->dev
,
206 "%s %d got unexpected flags: %#x\n",
207 RING_TYPE(ring
), ring
->hop
,
210 ring
->tail
= (ring
->tail
+ 1) % ring
->size
;
212 ring_write_descriptors(ring
);
215 mutex_unlock(&ring
->lock
); /* allow callbacks to schedule new work */
216 while (!list_empty(&done
)) {
217 frame
= list_first_entry(&done
, typeof(*frame
), list
);
219 * The callback may reenqueue or delete frame.
220 * Do not hold on to it.
222 list_del_init(&frame
->list
);
223 frame
->callback(ring
, frame
, canceled
);
227 int __ring_enqueue(struct tb_ring
*ring
, struct ring_frame
*frame
)
230 mutex_lock(&ring
->lock
);
232 list_add_tail(&frame
->list
, &ring
->queue
);
233 ring_write_descriptors(ring
);
237 mutex_unlock(&ring
->lock
);
241 static struct tb_ring
*ring_alloc(struct tb_nhi
*nhi
, u32 hop
, int size
,
244 struct tb_ring
*ring
= NULL
;
245 dev_info(&nhi
->pdev
->dev
, "allocating %s ring %d of size %d\n",
246 transmit
? "TX" : "RX", hop
, size
);
248 mutex_lock(&nhi
->lock
);
249 if (hop
>= nhi
->hop_count
) {
250 dev_WARN(&nhi
->pdev
->dev
, "invalid hop: %d\n", hop
);
253 if (transmit
&& nhi
->tx_rings
[hop
]) {
254 dev_WARN(&nhi
->pdev
->dev
, "TX hop %d already allocated\n", hop
);
256 } else if (!transmit
&& nhi
->rx_rings
[hop
]) {
257 dev_WARN(&nhi
->pdev
->dev
, "RX hop %d already allocated\n", hop
);
260 ring
= kzalloc(sizeof(*ring
), GFP_KERNEL
);
264 mutex_init(&ring
->lock
);
265 INIT_LIST_HEAD(&ring
->queue
);
266 INIT_LIST_HEAD(&ring
->in_flight
);
267 INIT_WORK(&ring
->work
, ring_work
);
271 ring
->is_tx
= transmit
;
275 ring
->running
= false;
276 ring
->descriptors
= dma_alloc_coherent(&ring
->nhi
->pdev
->dev
,
277 size
* sizeof(*ring
->descriptors
),
278 &ring
->descriptors_dma
, GFP_KERNEL
| __GFP_ZERO
);
279 if (!ring
->descriptors
)
283 nhi
->tx_rings
[hop
] = ring
;
285 nhi
->rx_rings
[hop
] = ring
;
286 mutex_unlock(&nhi
->lock
);
291 mutex_destroy(&ring
->lock
);
293 mutex_unlock(&nhi
->lock
);
297 struct tb_ring
*ring_alloc_tx(struct tb_nhi
*nhi
, int hop
, int size
)
299 return ring_alloc(nhi
, hop
, size
, true);
302 struct tb_ring
*ring_alloc_rx(struct tb_nhi
*nhi
, int hop
, int size
)
304 return ring_alloc(nhi
, hop
, size
, false);
308 * ring_start() - enable a ring
310 * Must not be invoked in parallel with ring_stop().
312 void ring_start(struct tb_ring
*ring
)
314 mutex_lock(&ring
->nhi
->lock
);
315 mutex_lock(&ring
->lock
);
317 dev_WARN(&ring
->nhi
->pdev
->dev
, "ring already started\n");
320 dev_info(&ring
->nhi
->pdev
->dev
, "starting %s %d\n",
321 RING_TYPE(ring
), ring
->hop
);
323 ring_iowrite64desc(ring
, ring
->descriptors_dma
, 0);
325 ring_iowrite32desc(ring
, ring
->size
, 12);
326 ring_iowrite32options(ring
, 0, 4); /* time releated ? */
327 ring_iowrite32options(ring
,
328 RING_FLAG_ENABLE
| RING_FLAG_RAW
, 0);
330 ring_iowrite32desc(ring
,
331 (TB_FRAME_SIZE
<< 16) | ring
->size
, 12);
332 ring_iowrite32options(ring
, 0xffffffff, 4); /* SOF EOF mask */
333 ring_iowrite32options(ring
,
334 RING_FLAG_ENABLE
| RING_FLAG_RAW
, 0);
336 ring_interrupt_active(ring
, true);
337 ring
->running
= true;
339 mutex_unlock(&ring
->lock
);
340 mutex_unlock(&ring
->nhi
->lock
);
345 * ring_stop() - shutdown a ring
347 * Must not be invoked from a callback.
349 * This method will disable the ring. Further calls to ring_tx/ring_rx will
350 * return -ESHUTDOWN until ring_stop has been called.
352 * All enqueued frames will be canceled and their callbacks will be executed
353 * with frame->canceled set to true (on the callback thread). This method
354 * returns only after all callback invocations have finished.
356 void ring_stop(struct tb_ring
*ring
)
358 mutex_lock(&ring
->nhi
->lock
);
359 mutex_lock(&ring
->lock
);
360 dev_info(&ring
->nhi
->pdev
->dev
, "stopping %s %d\n",
361 RING_TYPE(ring
), ring
->hop
);
362 if (!ring
->running
) {
363 dev_WARN(&ring
->nhi
->pdev
->dev
, "%s %d already stopped\n",
364 RING_TYPE(ring
), ring
->hop
);
367 ring_interrupt_active(ring
, false);
369 ring_iowrite32options(ring
, 0, 0);
370 ring_iowrite64desc(ring
, 0, 0);
371 ring_iowrite16desc(ring
, 0, ring
->is_tx
? 10 : 8);
372 ring_iowrite32desc(ring
, 0, 12);
375 ring
->running
= false;
378 mutex_unlock(&ring
->lock
);
379 mutex_unlock(&ring
->nhi
->lock
);
382 * schedule ring->work to invoke callbacks on all remaining frames.
384 schedule_work(&ring
->work
);
385 flush_work(&ring
->work
);
389 * ring_free() - free ring
391 * When this method returns all invocations of ring->callback will have
394 * Ring must be stopped.
396 * Must NOT be called from ring_frame->callback!
398 void ring_free(struct tb_ring
*ring
)
400 mutex_lock(&ring
->nhi
->lock
);
402 * Dissociate the ring from the NHI. This also ensures that
403 * nhi_interrupt_work cannot reschedule ring->work.
406 ring
->nhi
->tx_rings
[ring
->hop
] = NULL
;
408 ring
->nhi
->rx_rings
[ring
->hop
] = NULL
;
411 dev_WARN(&ring
->nhi
->pdev
->dev
, "%s %d still running\n",
412 RING_TYPE(ring
), ring
->hop
);
415 dma_free_coherent(&ring
->nhi
->pdev
->dev
,
416 ring
->size
* sizeof(*ring
->descriptors
),
417 ring
->descriptors
, ring
->descriptors_dma
);
419 ring
->descriptors
= NULL
;
420 ring
->descriptors_dma
= 0;
423 dev_info(&ring
->nhi
->pdev
->dev
,
428 mutex_unlock(&ring
->nhi
->lock
);
430 * ring->work can no longer be scheduled (it is scheduled only by
431 * nhi_interrupt_work and ring_stop). Wait for it to finish before
434 flush_work(&ring
->work
);
435 mutex_destroy(&ring
->lock
);
439 static void nhi_interrupt_work(struct work_struct
*work
)
441 struct tb_nhi
*nhi
= container_of(work
, typeof(*nhi
), interrupt_work
);
442 int value
= 0; /* Suppress uninitialized usage warning. */
445 int type
= 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */
446 struct tb_ring
*ring
;
448 mutex_lock(&nhi
->lock
);
451 * Starting at REG_RING_NOTIFY_BASE there are three status bitfields
452 * (TX, RX, RX overflow). We iterate over the bits and read a new
453 * dwords as required. The registers are cleared on read.
455 for (bit
= 0; bit
< 3 * nhi
->hop_count
; bit
++) {
457 value
= ioread32(nhi
->iobase
458 + REG_RING_NOTIFY_BASE
460 if (++hop
== nhi
->hop_count
) {
464 if ((value
& (1 << (bit
% 32))) == 0)
467 dev_warn(&nhi
->pdev
->dev
,
468 "RX overflow for ring %d\n",
473 ring
= nhi
->tx_rings
[hop
];
475 ring
= nhi
->rx_rings
[hop
];
477 dev_warn(&nhi
->pdev
->dev
,
478 "got interrupt for inactive %s ring %d\n",
483 /* we do not check ring->running, this is done in ring->work */
484 schedule_work(&ring
->work
);
486 mutex_unlock(&nhi
->lock
);
489 static irqreturn_t
nhi_msi(int irq
, void *data
)
491 struct tb_nhi
*nhi
= data
;
492 schedule_work(&nhi
->interrupt_work
);
496 static int nhi_suspend_noirq(struct device
*dev
)
498 struct pci_dev
*pdev
= to_pci_dev(dev
);
499 struct tb
*tb
= pci_get_drvdata(pdev
);
500 thunderbolt_suspend(tb
);
504 static int nhi_resume_noirq(struct device
*dev
)
506 struct pci_dev
*pdev
= to_pci_dev(dev
);
507 struct tb
*tb
= pci_get_drvdata(pdev
);
508 thunderbolt_resume(tb
);
512 static void nhi_shutdown(struct tb_nhi
*nhi
)
515 dev_info(&nhi
->pdev
->dev
, "shutdown\n");
517 for (i
= 0; i
< nhi
->hop_count
; i
++) {
518 if (nhi
->tx_rings
[i
])
519 dev_WARN(&nhi
->pdev
->dev
,
520 "TX ring %d is still active\n", i
);
521 if (nhi
->rx_rings
[i
])
522 dev_WARN(&nhi
->pdev
->dev
,
523 "RX ring %d is still active\n", i
);
525 nhi_disable_interrupts(nhi
);
527 * We have to release the irq before calling flush_work. Otherwise an
528 * already executing IRQ handler could call schedule_work again.
530 devm_free_irq(&nhi
->pdev
->dev
, nhi
->pdev
->irq
, nhi
);
531 flush_work(&nhi
->interrupt_work
);
532 mutex_destroy(&nhi
->lock
);
535 static int nhi_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
541 res
= pcim_enable_device(pdev
);
543 dev_err(&pdev
->dev
, "cannot enable PCI device, aborting\n");
547 res
= pci_enable_msi(pdev
);
549 dev_err(&pdev
->dev
, "cannot enable MSI, aborting\n");
553 res
= pcim_iomap_regions(pdev
, 1 << 0, "thunderbolt");
555 dev_err(&pdev
->dev
, "cannot obtain PCI resources, aborting\n");
559 nhi
= devm_kzalloc(&pdev
->dev
, sizeof(*nhi
), GFP_KERNEL
);
564 /* cannot fail - table is allocated bin pcim_iomap_regions */
565 nhi
->iobase
= pcim_iomap_table(pdev
)[0];
566 nhi
->hop_count
= ioread32(nhi
->iobase
+ REG_HOP_COUNT
) & 0x3ff;
567 if (nhi
->hop_count
!= 12)
568 dev_warn(&pdev
->dev
, "unexpected hop count: %d\n",
570 INIT_WORK(&nhi
->interrupt_work
, nhi_interrupt_work
);
572 nhi
->tx_rings
= devm_kcalloc(&pdev
->dev
, nhi
->hop_count
,
573 sizeof(*nhi
->tx_rings
), GFP_KERNEL
);
574 nhi
->rx_rings
= devm_kcalloc(&pdev
->dev
, nhi
->hop_count
,
575 sizeof(*nhi
->rx_rings
), GFP_KERNEL
);
576 if (!nhi
->tx_rings
|| !nhi
->rx_rings
)
579 nhi_disable_interrupts(nhi
); /* In case someone left them on. */
580 res
= devm_request_irq(&pdev
->dev
, pdev
->irq
, nhi_msi
,
581 IRQF_NO_SUSPEND
, /* must work during _noirq */
584 dev_err(&pdev
->dev
, "request_irq failed, aborting\n");
588 mutex_init(&nhi
->lock
);
590 pci_set_master(pdev
);
592 /* magic value - clock related? */
593 iowrite32(3906250 / 10000, nhi
->iobase
+ 0x38c00);
595 dev_info(&nhi
->pdev
->dev
, "NHI initialized, starting thunderbolt\n");
596 tb
= thunderbolt_alloc_and_start(nhi
);
599 * At this point the RX/TX rings might already have been
600 * activated. Do a proper shutdown.
605 pci_set_drvdata(pdev
, tb
);
610 static void nhi_remove(struct pci_dev
*pdev
)
612 struct tb
*tb
= pci_get_drvdata(pdev
);
613 struct tb_nhi
*nhi
= tb
->nhi
;
614 thunderbolt_shutdown_and_free(tb
);
619 * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable
620 * the tunnels asap. A corresponding pci quirk blocks the downstream bridges
621 * resume_noirq until we are done.
623 static const struct dev_pm_ops nhi_pm_ops
= {
624 .suspend_noirq
= nhi_suspend_noirq
,
625 .resume_noirq
= nhi_resume_noirq
,
626 .freeze_noirq
= nhi_suspend_noirq
, /*
627 * we just disable hotplug, the
628 * pci-tunnels stay alive.
630 .restore_noirq
= nhi_resume_noirq
,
633 static struct pci_device_id nhi_ids
[] = {
635 * We have to specify class, the TB bridges use the same device and
639 .class = PCI_CLASS_SYSTEM_OTHER
<< 8, .class_mask
= ~0,
640 .vendor
= PCI_VENDOR_ID_INTEL
, .device
= 0x1547,
641 .subvendor
= 0x2222, .subdevice
= 0x1111,
644 .class = PCI_CLASS_SYSTEM_OTHER
<< 8, .class_mask
= ~0,
645 .vendor
= PCI_VENDOR_ID_INTEL
, .device
= 0x156c,
646 .subvendor
= PCI_ANY_ID
, .subdevice
= PCI_ANY_ID
,
651 MODULE_DEVICE_TABLE(pci
, nhi_ids
);
652 MODULE_LICENSE("GPL");
654 static struct pci_driver nhi_driver
= {
655 .name
= "thunderbolt",
658 .remove
= nhi_remove
,
659 .driver
.pm
= &nhi_pm_ops
,
662 static int __init
nhi_init(void)
664 if (!dmi_match(DMI_BOARD_VENDOR
, "Apple Inc."))
666 return pci_register_driver(&nhi_driver
);
669 static void __exit
nhi_unload(void)
671 pci_unregister_driver(&nhi_driver
);
674 module_init(nhi_init
);
675 module_exit(nhi_unload
);