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 +
41 ring_interrupt_index(ring) / 32 * 4;
42 int bit = ring_interrupt_index(ring) & 31;
45 old = ioread32(ring->nhi->iobase + reg);
51 dev_info(&ring->nhi->pdev->dev,
52 "%s interrupt at register %#x bit %d (%#x -> %#x)\n",
53 active ? "enabling" : "disabling", reg, bit, old, new);
56 dev_WARN(&ring->nhi->pdev->dev,
57 "interrupt for %s %d is already %s\n",
58 RING_TYPE(ring), ring->hop,
59 active ? "enabled" : "disabled");
60 iowrite32(new, ring->nhi->iobase + reg);
64 * nhi_disable_interrupts() - disable interrupts for all rings
66 * Use only during init and shutdown.
68 static void nhi_disable_interrupts(struct tb_nhi *nhi)
71 /* disable interrupts */
72 for (i = 0; i < RING_INTERRUPT_REG_COUNT(nhi); i++)
73 iowrite32(0, nhi->iobase + REG_RING_INTERRUPT_BASE + 4 * i);
75 /* clear interrupt status bits */
76 for (i = 0; i < RING_NOTIFY_REG_COUNT(nhi); i++)
77 ioread32(nhi->iobase + REG_RING_NOTIFY_BASE + 4 * i);
80 /* ring helper methods */
82 static void __iomem *ring_desc_base(struct tb_ring *ring)
84 void __iomem *io = ring->nhi->iobase;
85 io += ring->is_tx ? REG_TX_RING_BASE : REG_RX_RING_BASE;
90 static void __iomem *ring_options_base(struct tb_ring *ring)
92 void __iomem *io = ring->nhi->iobase;
93 io += ring->is_tx ? REG_TX_OPTIONS_BASE : REG_RX_OPTIONS_BASE;
98 static void ring_iowrite_cons(struct tb_ring *ring, u16 cons)
101 * The other 16-bits in the register is read-only and writes to it
102 * are ignored by the hardware so we can save one ioread32() by
103 * filling the read-only bits with zeroes.
105 iowrite32(cons, ring_desc_base(ring) + 8);
108 static void ring_iowrite_prod(struct tb_ring *ring, u16 prod)
110 /* See ring_iowrite_cons() above for explanation */
111 iowrite32(prod << 16, ring_desc_base(ring) + 8);
114 static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset)
116 iowrite32(value, ring_desc_base(ring) + offset);
119 static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset)
121 iowrite32(value, ring_desc_base(ring) + offset);
122 iowrite32(value >> 32, ring_desc_base(ring) + offset + 4);
125 static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset)
127 iowrite32(value, ring_options_base(ring) + offset);
130 static bool ring_full(struct tb_ring *ring)
132 return ((ring->head + 1) % ring->size) == ring->tail;
135 static bool ring_empty(struct tb_ring *ring)
137 return ring->head == ring->tail;
141 * ring_write_descriptors() - post frames from ring->queue to the controller
143 * ring->lock is held.
145 static void ring_write_descriptors(struct tb_ring *ring)
147 struct ring_frame *frame, *n;
148 struct ring_desc *descriptor;
149 list_for_each_entry_safe(frame, n, &ring->queue, list) {
152 list_move_tail(&frame->list, &ring->in_flight);
153 descriptor = &ring->descriptors[ring->head];
154 descriptor->phys = frame->buffer_phy;
155 descriptor->time = 0;
156 descriptor->flags = RING_DESC_POSTED | RING_DESC_INTERRUPT;
158 descriptor->length = frame->size;
159 descriptor->eof = frame->eof;
160 descriptor->sof = frame->sof;
162 ring->head = (ring->head + 1) % ring->size;
164 ring_iowrite_prod(ring, ring->head);
166 ring_iowrite_cons(ring, ring->head);
171 * ring_work() - progress completed frames
173 * If the ring is shutting down then all frames are marked as canceled and
174 * their callbacks are invoked.
176 * Otherwise we collect all completed frame from the ring buffer, write new
177 * frame to the ring buffer and invoke the callbacks for the completed frames.
179 static void ring_work(struct work_struct *work)
181 struct tb_ring *ring = container_of(work, typeof(*ring), work);
182 struct ring_frame *frame;
183 bool canceled = false;
185 mutex_lock(&ring->lock);
187 if (!ring->running) {
188 /* Move all frames to done and mark them as canceled. */
189 list_splice_tail_init(&ring->in_flight, &done);
190 list_splice_tail_init(&ring->queue, &done);
192 goto invoke_callback;
195 while (!ring_empty(ring)) {
196 if (!(ring->descriptors[ring->tail].flags
197 & RING_DESC_COMPLETED))
199 frame = list_first_entry(&ring->in_flight, typeof(*frame),
201 list_move_tail(&frame->list, &done);
203 frame->size = ring->descriptors[ring->tail].length;
204 frame->eof = ring->descriptors[ring->tail].eof;
205 frame->sof = ring->descriptors[ring->tail].sof;
206 frame->flags = ring->descriptors[ring->tail].flags;
208 dev_WARN(&ring->nhi->pdev->dev,
209 "%s %d got unexpected SOF: %#x\n",
210 RING_TYPE(ring), ring->hop,
214 * raw not enabled, interupt not set: 0x2=0010
215 * raw enabled: 0xa=1010
216 * raw not enabled: 0xb=1011
217 * partial frame (>MAX_FRAME_SIZE): 0xe=1110
219 if (frame->flags != 0xa)
220 dev_WARN(&ring->nhi->pdev->dev,
221 "%s %d got unexpected flags: %#x\n",
222 RING_TYPE(ring), ring->hop,
225 ring->tail = (ring->tail + 1) % ring->size;
227 ring_write_descriptors(ring);
230 mutex_unlock(&ring->lock); /* allow callbacks to schedule new work */
231 while (!list_empty(&done)) {
232 frame = list_first_entry(&done, typeof(*frame), list);
234 * The callback may reenqueue or delete frame.
235 * Do not hold on to it.
237 list_del_init(&frame->list);
238 frame->callback(ring, frame, canceled);
242 int __ring_enqueue(struct tb_ring *ring, struct ring_frame *frame)
245 mutex_lock(&ring->lock);
247 list_add_tail(&frame->list, &ring->queue);
248 ring_write_descriptors(ring);
252 mutex_unlock(&ring->lock);
256 static struct tb_ring *ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
259 struct tb_ring *ring = NULL;
260 dev_info(&nhi->pdev->dev, "allocating %s ring %d of size %d\n",
261 transmit ? "TX" : "RX", hop, size);
263 mutex_lock(&nhi->lock);
264 if (hop >= nhi->hop_count) {
265 dev_WARN(&nhi->pdev->dev, "invalid hop: %d\n", hop);
268 if (transmit && nhi->tx_rings[hop]) {
269 dev_WARN(&nhi->pdev->dev, "TX hop %d already allocated\n", hop);
271 } else if (!transmit && nhi->rx_rings[hop]) {
272 dev_WARN(&nhi->pdev->dev, "RX hop %d already allocated\n", hop);
275 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
279 mutex_init(&ring->lock);
280 INIT_LIST_HEAD(&ring->queue);
281 INIT_LIST_HEAD(&ring->in_flight);
282 INIT_WORK(&ring->work, ring_work);
286 ring->is_tx = transmit;
290 ring->running = false;
291 ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev,
292 size * sizeof(*ring->descriptors),
293 &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO);
294 if (!ring->descriptors)
298 nhi->tx_rings[hop] = ring;
300 nhi->rx_rings[hop] = ring;
301 mutex_unlock(&nhi->lock);
306 mutex_destroy(&ring->lock);
308 mutex_unlock(&nhi->lock);
312 struct tb_ring *ring_alloc_tx(struct tb_nhi *nhi, int hop, int size)
314 return ring_alloc(nhi, hop, size, true);
317 struct tb_ring *ring_alloc_rx(struct tb_nhi *nhi, int hop, int size)
319 return ring_alloc(nhi, hop, size, false);
323 * ring_start() - enable a ring
325 * Must not be invoked in parallel with ring_stop().
327 void ring_start(struct tb_ring *ring)
329 mutex_lock(&ring->nhi->lock);
330 mutex_lock(&ring->lock);
332 dev_WARN(&ring->nhi->pdev->dev, "ring already started\n");
335 dev_info(&ring->nhi->pdev->dev, "starting %s %d\n",
336 RING_TYPE(ring), ring->hop);
338 ring_iowrite64desc(ring, ring->descriptors_dma, 0);
340 ring_iowrite32desc(ring, ring->size, 12);
341 ring_iowrite32options(ring, 0, 4); /* time releated ? */
342 ring_iowrite32options(ring,
343 RING_FLAG_ENABLE | RING_FLAG_RAW, 0);
345 ring_iowrite32desc(ring,
346 (TB_FRAME_SIZE << 16) | ring->size, 12);
347 ring_iowrite32options(ring, 0xffffffff, 4); /* SOF EOF mask */
348 ring_iowrite32options(ring,
349 RING_FLAG_ENABLE | RING_FLAG_RAW, 0);
351 ring_interrupt_active(ring, true);
352 ring->running = true;
354 mutex_unlock(&ring->lock);
355 mutex_unlock(&ring->nhi->lock);
360 * ring_stop() - shutdown a ring
362 * Must not be invoked from a callback.
364 * This method will disable the ring. Further calls to ring_tx/ring_rx will
365 * return -ESHUTDOWN until ring_stop has been called.
367 * All enqueued frames will be canceled and their callbacks will be executed
368 * with frame->canceled set to true (on the callback thread). This method
369 * returns only after all callback invocations have finished.
371 void ring_stop(struct tb_ring *ring)
373 mutex_lock(&ring->nhi->lock);
374 mutex_lock(&ring->lock);
375 dev_info(&ring->nhi->pdev->dev, "stopping %s %d\n",
376 RING_TYPE(ring), ring->hop);
377 if (!ring->running) {
378 dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n",
379 RING_TYPE(ring), ring->hop);
382 ring_interrupt_active(ring, false);
384 ring_iowrite32options(ring, 0, 0);
385 ring_iowrite64desc(ring, 0, 0);
386 ring_iowrite32desc(ring, 0, 8);
387 ring_iowrite32desc(ring, 0, 12);
390 ring->running = false;
393 mutex_unlock(&ring->lock);
394 mutex_unlock(&ring->nhi->lock);
397 * schedule ring->work to invoke callbacks on all remaining frames.
399 schedule_work(&ring->work);
400 flush_work(&ring->work);
404 * ring_free() - free ring
406 * When this method returns all invocations of ring->callback will have
409 * Ring must be stopped.
411 * Must NOT be called from ring_frame->callback!
413 void ring_free(struct tb_ring *ring)
415 mutex_lock(&ring->nhi->lock);
417 * Dissociate the ring from the NHI. This also ensures that
418 * nhi_interrupt_work cannot reschedule ring->work.
421 ring->nhi->tx_rings[ring->hop] = NULL;
423 ring->nhi->rx_rings[ring->hop] = NULL;
426 dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n",
427 RING_TYPE(ring), ring->hop);
430 dma_free_coherent(&ring->nhi->pdev->dev,
431 ring->size * sizeof(*ring->descriptors),
432 ring->descriptors, ring->descriptors_dma);
434 ring->descriptors = NULL;
435 ring->descriptors_dma = 0;
438 dev_info(&ring->nhi->pdev->dev,
443 mutex_unlock(&ring->nhi->lock);
445 * ring->work can no longer be scheduled (it is scheduled only by
446 * nhi_interrupt_work and ring_stop). Wait for it to finish before
449 flush_work(&ring->work);
450 mutex_destroy(&ring->lock);
454 static void nhi_interrupt_work(struct work_struct *work)
456 struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work);
457 int value = 0; /* Suppress uninitialized usage warning. */
460 int type = 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */
461 struct tb_ring *ring;
463 mutex_lock(&nhi->lock);
466 * Starting at REG_RING_NOTIFY_BASE there are three status bitfields
467 * (TX, RX, RX overflow). We iterate over the bits and read a new
468 * dwords as required. The registers are cleared on read.
470 for (bit = 0; bit < 3 * nhi->hop_count; bit++) {
472 value = ioread32(nhi->iobase
473 + REG_RING_NOTIFY_BASE
475 if (++hop == nhi->hop_count) {
479 if ((value & (1 << (bit % 32))) == 0)
482 dev_warn(&nhi->pdev->dev,
483 "RX overflow for ring %d\n",
488 ring = nhi->tx_rings[hop];
490 ring = nhi->rx_rings[hop];
492 dev_warn(&nhi->pdev->dev,
493 "got interrupt for inactive %s ring %d\n",
498 /* we do not check ring->running, this is done in ring->work */
499 schedule_work(&ring->work);
501 mutex_unlock(&nhi->lock);
504 static irqreturn_t nhi_msi(int irq, void *data)
506 struct tb_nhi *nhi = data;
507 schedule_work(&nhi->interrupt_work);
511 static int nhi_suspend_noirq(struct device *dev)
513 struct pci_dev *pdev = to_pci_dev(dev);
514 struct tb *tb = pci_get_drvdata(pdev);
515 thunderbolt_suspend(tb);
519 static int nhi_resume_noirq(struct device *dev)
521 struct pci_dev *pdev = to_pci_dev(dev);
522 struct tb *tb = pci_get_drvdata(pdev);
523 thunderbolt_resume(tb);
527 static void nhi_shutdown(struct tb_nhi *nhi)
530 dev_info(&nhi->pdev->dev, "shutdown\n");
532 for (i = 0; i < nhi->hop_count; i++) {
533 if (nhi->tx_rings[i])
534 dev_WARN(&nhi->pdev->dev,
535 "TX ring %d is still active\n", i);
536 if (nhi->rx_rings[i])
537 dev_WARN(&nhi->pdev->dev,
538 "RX ring %d is still active\n", i);
540 nhi_disable_interrupts(nhi);
542 * We have to release the irq before calling flush_work. Otherwise an
543 * already executing IRQ handler could call schedule_work again.
545 devm_free_irq(&nhi->pdev->dev, nhi->pdev->irq, nhi);
546 flush_work(&nhi->interrupt_work);
547 mutex_destroy(&nhi->lock);
550 static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
556 res = pcim_enable_device(pdev);
558 dev_err(&pdev->dev, "cannot enable PCI device, aborting\n");
562 res = pci_enable_msi(pdev);
564 dev_err(&pdev->dev, "cannot enable MSI, aborting\n");
568 res = pcim_iomap_regions(pdev, 1 << 0, "thunderbolt");
570 dev_err(&pdev->dev, "cannot obtain PCI resources, aborting\n");
574 nhi = devm_kzalloc(&pdev->dev, sizeof(*nhi), GFP_KERNEL);
579 /* cannot fail - table is allocated bin pcim_iomap_regions */
580 nhi->iobase = pcim_iomap_table(pdev)[0];
581 nhi->hop_count = ioread32(nhi->iobase + REG_HOP_COUNT) & 0x3ff;
582 if (nhi->hop_count != 12 && nhi->hop_count != 32)
583 dev_warn(&pdev->dev, "unexpected hop count: %d\n",
585 INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
587 nhi->tx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
588 sizeof(*nhi->tx_rings), GFP_KERNEL);
589 nhi->rx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
590 sizeof(*nhi->rx_rings), GFP_KERNEL);
591 if (!nhi->tx_rings || !nhi->rx_rings)
594 nhi_disable_interrupts(nhi); /* In case someone left them on. */
595 res = devm_request_irq(&pdev->dev, pdev->irq, nhi_msi,
596 IRQF_NO_SUSPEND, /* must work during _noirq */
599 dev_err(&pdev->dev, "request_irq failed, aborting\n");
603 mutex_init(&nhi->lock);
605 pci_set_master(pdev);
607 /* magic value - clock related? */
608 iowrite32(3906250 / 10000, nhi->iobase + 0x38c00);
610 dev_info(&nhi->pdev->dev, "NHI initialized, starting thunderbolt\n");
611 tb = thunderbolt_alloc_and_start(nhi);
614 * At this point the RX/TX rings might already have been
615 * activated. Do a proper shutdown.
620 pci_set_drvdata(pdev, tb);
625 static void nhi_remove(struct pci_dev *pdev)
627 struct tb *tb = pci_get_drvdata(pdev);
628 struct tb_nhi *nhi = tb->nhi;
629 thunderbolt_shutdown_and_free(tb);
634 * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable
635 * the tunnels asap. A corresponding pci quirk blocks the downstream bridges
636 * resume_noirq until we are done.
638 static const struct dev_pm_ops nhi_pm_ops = {
639 .suspend_noirq = nhi_suspend_noirq,
640 .resume_noirq = nhi_resume_noirq,
641 .freeze_noirq = nhi_suspend_noirq, /*
642 * we just disable hotplug, the
643 * pci-tunnels stay alive.
645 .thaw_noirq = nhi_resume_noirq,
646 .restore_noirq = nhi_resume_noirq,
649 static struct pci_device_id nhi_ids[] = {
651 * We have to specify class, the TB bridges use the same device and
652 * vendor (sub)id on gen 1 and gen 2 controllers.
655 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
656 .vendor = PCI_VENDOR_ID_INTEL,
657 .device = PCI_DEVICE_ID_INTEL_LIGHT_RIDGE,
658 .subvendor = 0x2222, .subdevice = 0x1111,
661 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
662 .vendor = PCI_VENDOR_ID_INTEL,
663 .device = PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C,
664 .subvendor = 0x2222, .subdevice = 0x1111,
667 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
668 .vendor = PCI_VENDOR_ID_INTEL,
669 .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI,
670 .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
673 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
674 .vendor = PCI_VENDOR_ID_INTEL,
675 .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI,
676 .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
681 MODULE_DEVICE_TABLE(pci, nhi_ids);
682 MODULE_LICENSE("GPL");
684 static struct pci_driver nhi_driver = {
685 .name = "thunderbolt",
688 .remove = nhi_remove,
689 .driver.pm = &nhi_pm_ops,
692 static int __init nhi_init(void)
694 if (!dmi_match(DMI_BOARD_VENDOR, "Apple Inc."))
696 return pci_register_driver(&nhi_driver);
699 static void __exit nhi_unload(void)
701 pci_unregister_driver(&nhi_driver);
704 module_init(nhi_init);
705 module_exit(nhi_unload);