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_iowrite_cons(struct tb_ring *ring, u16 cons)
100 * The other 16-bits in the register is read-only and writes to it
101 * are ignored by the hardware so we can save one ioread32() by
102 * filling the read-only bits with zeroes.
104 iowrite32(cons, ring_desc_base(ring) + 8);
107 static void ring_iowrite_prod(struct tb_ring *ring, u16 prod)
109 /* See ring_iowrite_cons() above for explanation */
110 iowrite32(prod << 16, ring_desc_base(ring) + 8);
113 static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset)
115 iowrite32(value, ring_desc_base(ring) + offset);
118 static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset)
120 iowrite32(value, ring_desc_base(ring) + offset);
121 iowrite32(value >> 32, ring_desc_base(ring) + offset + 4);
124 static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset)
126 iowrite32(value, ring_options_base(ring) + offset);
129 static bool ring_full(struct tb_ring *ring)
131 return ((ring->head + 1) % ring->size) == ring->tail;
134 static bool ring_empty(struct tb_ring *ring)
136 return ring->head == ring->tail;
140 * ring_write_descriptors() - post frames from ring->queue to the controller
142 * ring->lock is held.
144 static void ring_write_descriptors(struct tb_ring *ring)
146 struct ring_frame *frame, *n;
147 struct ring_desc *descriptor;
148 list_for_each_entry_safe(frame, n, &ring->queue, list) {
151 list_move_tail(&frame->list, &ring->in_flight);
152 descriptor = &ring->descriptors[ring->head];
153 descriptor->phys = frame->buffer_phy;
154 descriptor->time = 0;
155 descriptor->flags = RING_DESC_POSTED | RING_DESC_INTERRUPT;
157 descriptor->length = frame->size;
158 descriptor->eof = frame->eof;
159 descriptor->sof = frame->sof;
161 ring->head = (ring->head + 1) % ring->size;
163 ring_iowrite_prod(ring, ring->head);
165 ring_iowrite_cons(ring, ring->head);
170 * ring_work() - progress completed frames
172 * If the ring is shutting down then all frames are marked as canceled and
173 * their callbacks are invoked.
175 * Otherwise we collect all completed frame from the ring buffer, write new
176 * frame to the ring buffer and invoke the callbacks for the completed frames.
178 static void ring_work(struct work_struct *work)
180 struct tb_ring *ring = container_of(work, typeof(*ring), work);
181 struct ring_frame *frame;
182 bool canceled = false;
184 mutex_lock(&ring->lock);
186 if (!ring->running) {
187 /* Move all frames to done and mark them as canceled. */
188 list_splice_tail_init(&ring->in_flight, &done);
189 list_splice_tail_init(&ring->queue, &done);
191 goto invoke_callback;
194 while (!ring_empty(ring)) {
195 if (!(ring->descriptors[ring->tail].flags
196 & RING_DESC_COMPLETED))
198 frame = list_first_entry(&ring->in_flight, typeof(*frame),
200 list_move_tail(&frame->list, &done);
202 frame->size = ring->descriptors[ring->tail].length;
203 frame->eof = ring->descriptors[ring->tail].eof;
204 frame->sof = ring->descriptors[ring->tail].sof;
205 frame->flags = ring->descriptors[ring->tail].flags;
207 dev_WARN(&ring->nhi->pdev->dev,
208 "%s %d got unexpected SOF: %#x\n",
209 RING_TYPE(ring), ring->hop,
213 * raw not enabled, interupt not set: 0x2=0010
214 * raw enabled: 0xa=1010
215 * raw not enabled: 0xb=1011
216 * partial frame (>MAX_FRAME_SIZE): 0xe=1110
218 if (frame->flags != 0xa)
219 dev_WARN(&ring->nhi->pdev->dev,
220 "%s %d got unexpected flags: %#x\n",
221 RING_TYPE(ring), ring->hop,
224 ring->tail = (ring->tail + 1) % ring->size;
226 ring_write_descriptors(ring);
229 mutex_unlock(&ring->lock); /* allow callbacks to schedule new work */
230 while (!list_empty(&done)) {
231 frame = list_first_entry(&done, typeof(*frame), list);
233 * The callback may reenqueue or delete frame.
234 * Do not hold on to it.
236 list_del_init(&frame->list);
237 frame->callback(ring, frame, canceled);
241 int __ring_enqueue(struct tb_ring *ring, struct ring_frame *frame)
244 mutex_lock(&ring->lock);
246 list_add_tail(&frame->list, &ring->queue);
247 ring_write_descriptors(ring);
251 mutex_unlock(&ring->lock);
255 static struct tb_ring *ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
258 struct tb_ring *ring = NULL;
259 dev_info(&nhi->pdev->dev, "allocating %s ring %d of size %d\n",
260 transmit ? "TX" : "RX", hop, size);
262 mutex_lock(&nhi->lock);
263 if (hop >= nhi->hop_count) {
264 dev_WARN(&nhi->pdev->dev, "invalid hop: %d\n", hop);
267 if (transmit && nhi->tx_rings[hop]) {
268 dev_WARN(&nhi->pdev->dev, "TX hop %d already allocated\n", hop);
270 } else if (!transmit && nhi->rx_rings[hop]) {
271 dev_WARN(&nhi->pdev->dev, "RX hop %d already allocated\n", hop);
274 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
278 mutex_init(&ring->lock);
279 INIT_LIST_HEAD(&ring->queue);
280 INIT_LIST_HEAD(&ring->in_flight);
281 INIT_WORK(&ring->work, ring_work);
285 ring->is_tx = transmit;
289 ring->running = false;
290 ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev,
291 size * sizeof(*ring->descriptors),
292 &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO);
293 if (!ring->descriptors)
297 nhi->tx_rings[hop] = ring;
299 nhi->rx_rings[hop] = ring;
300 mutex_unlock(&nhi->lock);
305 mutex_destroy(&ring->lock);
307 mutex_unlock(&nhi->lock);
311 struct tb_ring *ring_alloc_tx(struct tb_nhi *nhi, int hop, int size)
313 return ring_alloc(nhi, hop, size, true);
316 struct tb_ring *ring_alloc_rx(struct tb_nhi *nhi, int hop, int size)
318 return ring_alloc(nhi, hop, size, false);
322 * ring_start() - enable a ring
324 * Must not be invoked in parallel with ring_stop().
326 void ring_start(struct tb_ring *ring)
328 mutex_lock(&ring->nhi->lock);
329 mutex_lock(&ring->lock);
331 dev_WARN(&ring->nhi->pdev->dev, "ring already started\n");
334 dev_info(&ring->nhi->pdev->dev, "starting %s %d\n",
335 RING_TYPE(ring), ring->hop);
337 ring_iowrite64desc(ring, ring->descriptors_dma, 0);
339 ring_iowrite32desc(ring, ring->size, 12);
340 ring_iowrite32options(ring, 0, 4); /* time releated ? */
341 ring_iowrite32options(ring,
342 RING_FLAG_ENABLE | RING_FLAG_RAW, 0);
344 ring_iowrite32desc(ring,
345 (TB_FRAME_SIZE << 16) | ring->size, 12);
346 ring_iowrite32options(ring, 0xffffffff, 4); /* SOF EOF mask */
347 ring_iowrite32options(ring,
348 RING_FLAG_ENABLE | RING_FLAG_RAW, 0);
350 ring_interrupt_active(ring, true);
351 ring->running = true;
353 mutex_unlock(&ring->lock);
354 mutex_unlock(&ring->nhi->lock);
359 * ring_stop() - shutdown a ring
361 * Must not be invoked from a callback.
363 * This method will disable the ring. Further calls to ring_tx/ring_rx will
364 * return -ESHUTDOWN until ring_stop has been called.
366 * All enqueued frames will be canceled and their callbacks will be executed
367 * with frame->canceled set to true (on the callback thread). This method
368 * returns only after all callback invocations have finished.
370 void ring_stop(struct tb_ring *ring)
372 mutex_lock(&ring->nhi->lock);
373 mutex_lock(&ring->lock);
374 dev_info(&ring->nhi->pdev->dev, "stopping %s %d\n",
375 RING_TYPE(ring), ring->hop);
376 if (!ring->running) {
377 dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n",
378 RING_TYPE(ring), ring->hop);
381 ring_interrupt_active(ring, false);
383 ring_iowrite32options(ring, 0, 0);
384 ring_iowrite64desc(ring, 0, 0);
385 ring_iowrite32desc(ring, 0, 8);
386 ring_iowrite32desc(ring, 0, 12);
389 ring->running = false;
392 mutex_unlock(&ring->lock);
393 mutex_unlock(&ring->nhi->lock);
396 * schedule ring->work to invoke callbacks on all remaining frames.
398 schedule_work(&ring->work);
399 flush_work(&ring->work);
403 * ring_free() - free ring
405 * When this method returns all invocations of ring->callback will have
408 * Ring must be stopped.
410 * Must NOT be called from ring_frame->callback!
412 void ring_free(struct tb_ring *ring)
414 mutex_lock(&ring->nhi->lock);
416 * Dissociate the ring from the NHI. This also ensures that
417 * nhi_interrupt_work cannot reschedule ring->work.
420 ring->nhi->tx_rings[ring->hop] = NULL;
422 ring->nhi->rx_rings[ring->hop] = NULL;
425 dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n",
426 RING_TYPE(ring), ring->hop);
429 dma_free_coherent(&ring->nhi->pdev->dev,
430 ring->size * sizeof(*ring->descriptors),
431 ring->descriptors, ring->descriptors_dma);
433 ring->descriptors = NULL;
434 ring->descriptors_dma = 0;
437 dev_info(&ring->nhi->pdev->dev,
442 mutex_unlock(&ring->nhi->lock);
444 * ring->work can no longer be scheduled (it is scheduled only by
445 * nhi_interrupt_work and ring_stop). Wait for it to finish before
448 flush_work(&ring->work);
449 mutex_destroy(&ring->lock);
453 static void nhi_interrupt_work(struct work_struct *work)
455 struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work);
456 int value = 0; /* Suppress uninitialized usage warning. */
459 int type = 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */
460 struct tb_ring *ring;
462 mutex_lock(&nhi->lock);
465 * Starting at REG_RING_NOTIFY_BASE there are three status bitfields
466 * (TX, RX, RX overflow). We iterate over the bits and read a new
467 * dwords as required. The registers are cleared on read.
469 for (bit = 0; bit < 3 * nhi->hop_count; bit++) {
471 value = ioread32(nhi->iobase
472 + REG_RING_NOTIFY_BASE
474 if (++hop == nhi->hop_count) {
478 if ((value & (1 << (bit % 32))) == 0)
481 dev_warn(&nhi->pdev->dev,
482 "RX overflow for ring %d\n",
487 ring = nhi->tx_rings[hop];
489 ring = nhi->rx_rings[hop];
491 dev_warn(&nhi->pdev->dev,
492 "got interrupt for inactive %s ring %d\n",
497 /* we do not check ring->running, this is done in ring->work */
498 schedule_work(&ring->work);
500 mutex_unlock(&nhi->lock);
503 static irqreturn_t nhi_msi(int irq, void *data)
505 struct tb_nhi *nhi = data;
506 schedule_work(&nhi->interrupt_work);
510 static int nhi_suspend_noirq(struct device *dev)
512 struct pci_dev *pdev = to_pci_dev(dev);
513 struct tb *tb = pci_get_drvdata(pdev);
514 thunderbolt_suspend(tb);
518 static int nhi_resume_noirq(struct device *dev)
520 struct pci_dev *pdev = to_pci_dev(dev);
521 struct tb *tb = pci_get_drvdata(pdev);
522 thunderbolt_resume(tb);
526 static void nhi_shutdown(struct tb_nhi *nhi)
529 dev_info(&nhi->pdev->dev, "shutdown\n");
531 for (i = 0; i < nhi->hop_count; i++) {
532 if (nhi->tx_rings[i])
533 dev_WARN(&nhi->pdev->dev,
534 "TX ring %d is still active\n", i);
535 if (nhi->rx_rings[i])
536 dev_WARN(&nhi->pdev->dev,
537 "RX ring %d is still active\n", i);
539 nhi_disable_interrupts(nhi);
541 * We have to release the irq before calling flush_work. Otherwise an
542 * already executing IRQ handler could call schedule_work again.
544 devm_free_irq(&nhi->pdev->dev, nhi->pdev->irq, nhi);
545 flush_work(&nhi->interrupt_work);
546 mutex_destroy(&nhi->lock);
549 static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
555 res = pcim_enable_device(pdev);
557 dev_err(&pdev->dev, "cannot enable PCI device, aborting\n");
561 res = pci_enable_msi(pdev);
563 dev_err(&pdev->dev, "cannot enable MSI, aborting\n");
567 res = pcim_iomap_regions(pdev, 1 << 0, "thunderbolt");
569 dev_err(&pdev->dev, "cannot obtain PCI resources, aborting\n");
573 nhi = devm_kzalloc(&pdev->dev, sizeof(*nhi), GFP_KERNEL);
578 /* cannot fail - table is allocated bin pcim_iomap_regions */
579 nhi->iobase = pcim_iomap_table(pdev)[0];
580 nhi->hop_count = ioread32(nhi->iobase + REG_HOP_COUNT) & 0x3ff;
581 if (nhi->hop_count != 12)
582 dev_warn(&pdev->dev, "unexpected hop count: %d\n",
584 INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
586 nhi->tx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
587 sizeof(*nhi->tx_rings), GFP_KERNEL);
588 nhi->rx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
589 sizeof(*nhi->rx_rings), GFP_KERNEL);
590 if (!nhi->tx_rings || !nhi->rx_rings)
593 nhi_disable_interrupts(nhi); /* In case someone left them on. */
594 res = devm_request_irq(&pdev->dev, pdev->irq, nhi_msi,
595 IRQF_NO_SUSPEND, /* must work during _noirq */
598 dev_err(&pdev->dev, "request_irq failed, aborting\n");
602 mutex_init(&nhi->lock);
604 pci_set_master(pdev);
606 /* magic value - clock related? */
607 iowrite32(3906250 / 10000, nhi->iobase + 0x38c00);
609 dev_info(&nhi->pdev->dev, "NHI initialized, starting thunderbolt\n");
610 tb = thunderbolt_alloc_and_start(nhi);
613 * At this point the RX/TX rings might already have been
614 * activated. Do a proper shutdown.
619 pci_set_drvdata(pdev, tb);
624 static void nhi_remove(struct pci_dev *pdev)
626 struct tb *tb = pci_get_drvdata(pdev);
627 struct tb_nhi *nhi = tb->nhi;
628 thunderbolt_shutdown_and_free(tb);
633 * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable
634 * the tunnels asap. A corresponding pci quirk blocks the downstream bridges
635 * resume_noirq until we are done.
637 static const struct dev_pm_ops nhi_pm_ops = {
638 .suspend_noirq = nhi_suspend_noirq,
639 .resume_noirq = nhi_resume_noirq,
640 .freeze_noirq = nhi_suspend_noirq, /*
641 * we just disable hotplug, the
642 * pci-tunnels stay alive.
644 .thaw_noirq = nhi_resume_noirq,
645 .restore_noirq = nhi_resume_noirq,
648 static struct pci_device_id nhi_ids[] = {
650 * We have to specify class, the TB bridges use the same device and
654 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
655 .vendor = PCI_VENDOR_ID_INTEL, .device = 0x1547,
656 .subvendor = 0x2222, .subdevice = 0x1111,
659 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
660 .vendor = PCI_VENDOR_ID_INTEL, .device = 0x156c,
661 .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
666 MODULE_DEVICE_TABLE(pci, nhi_ids);
667 MODULE_LICENSE("GPL");
669 static struct pci_driver nhi_driver = {
670 .name = "thunderbolt",
673 .remove = nhi_remove,
674 .driver.pm = &nhi_pm_ops,
677 static int __init nhi_init(void)
679 if (!dmi_match(DMI_BOARD_VENDOR, "Apple Inc."))
681 return pci_register_driver(&nhi_driver);
684 static void __exit nhi_unload(void)
686 pci_unregister_driver(&nhi_driver);
689 module_init(nhi_init);
690 module_exit(nhi_unload);