Linux 4.4.50
[linux/fpc-iii.git] / drivers / thunderbolt / nhi.c
blob20a41f7de76f687d39329c2f4ea58ae30dc8fc6d
1 /*
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>
8 */
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>
18 #include "nhi.h"
19 #include "nhi_regs.h"
20 #include "tb.h"
22 #define RING_TYPE(ring) ((ring)->is_tx ? "TX ring" : "RX ring")
25 static int ring_interrupt_index(struct tb_ring *ring)
27 int bit = ring->hop;
28 if (!ring->is_tx)
29 bit += ring->nhi->hop_count;
30 return bit;
33 /**
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;
42 int mask = 1 << bit;
43 u32 old, new;
44 old = ioread32(ring->nhi->iobase + reg);
45 if (active)
46 new = old | mask;
47 else
48 new = old & ~mask;
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);
54 if (new == old)
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);
62 /**
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)
69 int i = 0;
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;
85 io += ring->hop * 16;
86 return io;
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;
93 io += ring->hop * 32;
94 return io;
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) {
138 if (ring_full(ring))
139 break;
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;
145 if (ring->is_tx) {
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;
169 LIST_HEAD(done);
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);
176 canceled = true;
177 goto invoke_callback;
180 while (!ring_empty(ring)) {
181 if (!(ring->descriptors[ring->tail].flags
182 & RING_DESC_COMPLETED))
183 break;
184 frame = list_first_entry(&ring->in_flight, typeof(*frame),
185 list);
186 list_move_tail(&frame->list, &done);
187 if (!ring->is_tx) {
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;
192 if (frame->sof != 0)
193 dev_WARN(&ring->nhi->pdev->dev,
194 "%s %d got unexpected SOF: %#x\n",
195 RING_TYPE(ring), ring->hop,
196 frame->sof);
198 * known flags:
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,
208 frame->flags);
210 ring->tail = (ring->tail + 1) % ring->size;
212 ring_write_descriptors(ring);
214 invoke_callback:
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)
229 int ret = 0;
230 mutex_lock(&ring->lock);
231 if (ring->running) {
232 list_add_tail(&frame->list, &ring->queue);
233 ring_write_descriptors(ring);
234 } else {
235 ret = -ESHUTDOWN;
237 mutex_unlock(&ring->lock);
238 return ret;
241 static struct tb_ring *ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
242 bool transmit)
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);
251 goto err;
253 if (transmit && nhi->tx_rings[hop]) {
254 dev_WARN(&nhi->pdev->dev, "TX hop %d already allocated\n", hop);
255 goto err;
256 } else if (!transmit && nhi->rx_rings[hop]) {
257 dev_WARN(&nhi->pdev->dev, "RX hop %d already allocated\n", hop);
258 goto err;
260 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
261 if (!ring)
262 goto err;
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);
269 ring->nhi = nhi;
270 ring->hop = hop;
271 ring->is_tx = transmit;
272 ring->size = size;
273 ring->head = 0;
274 ring->tail = 0;
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)
280 goto err;
282 if (transmit)
283 nhi->tx_rings[hop] = ring;
284 else
285 nhi->rx_rings[hop] = ring;
286 mutex_unlock(&nhi->lock);
287 return ring;
289 err:
290 if (ring)
291 mutex_destroy(&ring->lock);
292 kfree(ring);
293 mutex_unlock(&nhi->lock);
294 return NULL;
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);
316 if (ring->running) {
317 dev_WARN(&ring->nhi->pdev->dev, "ring already started\n");
318 goto err;
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);
324 if (ring->is_tx) {
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);
329 } else {
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;
338 err:
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);
365 goto err;
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);
373 ring->head = 0;
374 ring->tail = 0;
375 ring->running = false;
377 err:
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
392 * finished.
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.
405 if (ring->is_tx)
406 ring->nhi->tx_rings[ring->hop] = NULL;
407 else
408 ring->nhi->rx_rings[ring->hop] = NULL;
410 if (ring->running) {
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,
424 "freeing %s %d\n",
425 RING_TYPE(ring),
426 ring->hop);
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
432 * freeing the ring.
434 flush_work(&ring->work);
435 mutex_destroy(&ring->lock);
436 kfree(ring);
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. */
443 int bit;
444 int hop = -1;
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++) {
456 if (bit % 32 == 0)
457 value = ioread32(nhi->iobase
458 + REG_RING_NOTIFY_BASE
459 + 4 * (bit / 32));
460 if (++hop == nhi->hop_count) {
461 hop = 0;
462 type++;
464 if ((value & (1 << (bit % 32))) == 0)
465 continue;
466 if (type == 2) {
467 dev_warn(&nhi->pdev->dev,
468 "RX overflow for ring %d\n",
469 hop);
470 continue;
472 if (type == 0)
473 ring = nhi->tx_rings[hop];
474 else
475 ring = nhi->rx_rings[hop];
476 if (ring == NULL) {
477 dev_warn(&nhi->pdev->dev,
478 "got interrupt for inactive %s ring %d\n",
479 type ? "RX" : "TX",
480 hop);
481 continue;
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);
493 return IRQ_HANDLED;
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);
501 return 0;
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);
509 return 0;
512 static void nhi_shutdown(struct tb_nhi *nhi)
514 int i;
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)
537 struct tb_nhi *nhi;
538 struct tb *tb;
539 int res;
541 res = pcim_enable_device(pdev);
542 if (res) {
543 dev_err(&pdev->dev, "cannot enable PCI device, aborting\n");
544 return res;
547 res = pci_enable_msi(pdev);
548 if (res) {
549 dev_err(&pdev->dev, "cannot enable MSI, aborting\n");
550 return res;
553 res = pcim_iomap_regions(pdev, 1 << 0, "thunderbolt");
554 if (res) {
555 dev_err(&pdev->dev, "cannot obtain PCI resources, aborting\n");
556 return res;
559 nhi = devm_kzalloc(&pdev->dev, sizeof(*nhi), GFP_KERNEL);
560 if (!nhi)
561 return -ENOMEM;
563 nhi->pdev = pdev;
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",
569 nhi->hop_count);
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)
577 return -ENOMEM;
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 */
582 "thunderbolt", nhi);
583 if (res) {
584 dev_err(&pdev->dev, "request_irq failed, aborting\n");
585 return res;
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);
597 if (!tb) {
599 * At this point the RX/TX rings might already have been
600 * activated. Do a proper shutdown.
602 nhi_shutdown(nhi);
603 return -EIO;
605 pci_set_drvdata(pdev, tb);
607 return 0;
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);
615 nhi_shutdown(nhi);
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
636 * vendor (sub)id.
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,
648 { 0,}
651 MODULE_DEVICE_TABLE(pci, nhi_ids);
652 MODULE_LICENSE("GPL");
654 static struct pci_driver nhi_driver = {
655 .name = "thunderbolt",
656 .id_table = nhi_ids,
657 .probe = nhi_probe,
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."))
665 return -ENOSYS;
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);