// SPDX-License-Identifier: GPL-2.0-only /* * Thunderbolt driver - NHI driver * * The NHI (native host interface) is the pci device that allows us to send and * receive frames from the thunderbolt bus. * * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com> * Copyright (C) 2018, Intel Corporation
*/
#define RING_FIRST_USABLE_HOPID 1 /* * Used with QUIRK_E2E to specify an unused HopID the Rx credits are * transferred.
*/ #define RING_E2E_RESERVED_HOPID RING_FIRST_USABLE_HOPID /* * Minimal number of vectors when we use MSI-X. Two for control channel * Rx/Tx and the rest four are for cross domain DMA paths.
*/ #define MSIX_MIN_VECS 6 #define MSIX_MAX_VECS 16
/* * ring_interrupt_active() - activate/deactivate interrupts for a single ring * * ring->nhi->lock must be held.
*/ staticvoid ring_interrupt_active(struct tb_ring *ring, bool active)
{ int index = ring_interrupt_index(ring) / 32 * 4; int reg = REG_RING_INTERRUPT_BASE + index; int interrupt_bit = ring_interrupt_index(ring) & 31; int mask = 1 << interrupt_bit;
u32 old, new;
if (ring->irq > 0) {
u32 step, shift, ivr, misc; void __iomem *ivr_base; int auto_clear_bit; int index;
if (ring->is_tx)
index = ring->hop; else
index = ring->hop + ring->nhi->hop_count;
/* * Intel routers support a bit that isn't part of * the USB4 spec to ask the hardware to clear * interrupt status bits automatically since * we already know which interrupt was triggered. * * Other routers explicitly disable auto-clear * to prevent conditions that may occur where two * MSIX interrupts are simultaneously active and * reading the register clears both of them.
*/
misc = ioread32(ring->nhi->iobase + REG_DMA_MISC); if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT)
auto_clear_bit = REG_DMA_MISC_INT_AUTO_CLEAR; else
auto_clear_bit = REG_DMA_MISC_DISABLE_AUTO_CLEAR; if (!(misc & auto_clear_bit))
iowrite32(misc | auto_clear_bit,
ring->nhi->iobase + REG_DMA_MISC);
/* * nhi_disable_interrupts() - disable interrupts for all rings * * Use only during init and shutdown.
*/ staticvoid nhi_disable_interrupts(struct tb_nhi *nhi)
{ int i = 0; /* disable interrupts */ for (i = 0; i < RING_INTERRUPT_REG_COUNT(nhi); i++)
nhi_mask_interrupt(nhi, ~0, 4 * i);
/* clear interrupt status bits */ for (i = 0; i < RING_NOTIFY_REG_COUNT(nhi); i++)
nhi_clear_interrupt(nhi, 4 * i);
}
staticvoid ring_iowrite_cons(struct tb_ring *ring, u16 cons)
{ /* * The other 16-bits in the register is read-only and writes to it * are ignored by the hardware so we can save one ioread32() by * filling the read-only bits with zeroes.
*/
iowrite32(cons, ring_desc_base(ring) + 8);
}
/* * ring_write_descriptors() - post frames from ring->queue to the controller * * ring->lock is held.
*/ staticvoid ring_write_descriptors(struct tb_ring *ring)
{ struct ring_frame *frame, *n; struct ring_desc *descriptor;
list_for_each_entry_safe(frame, n, &ring->queue, list) { if (ring_full(ring)) break;
list_move_tail(&frame->list, &ring->in_flight);
descriptor = &ring->descriptors[ring->head];
descriptor->phys = frame->buffer_phy;
descriptor->time = 0;
descriptor->flags = RING_DESC_POSTED | RING_DESC_INTERRUPT; if (ring->is_tx) {
descriptor->length = frame->size;
descriptor->eof = frame->eof;
descriptor->sof = frame->sof;
}
ring->head = (ring->head + 1) % ring->size; if (ring->is_tx)
ring_iowrite_prod(ring, ring->head); else
ring_iowrite_cons(ring, ring->head);
}
}
/* * ring_work() - progress completed frames * * If the ring is shutting down then all frames are marked as canceled and * their callbacks are invoked. * * Otherwise we collect all completed frame from the ring buffer, write new * frame to the ring buffer and invoke the callbacks for the completed frames.
*/ staticvoid ring_work(struct work_struct *work)
{ struct tb_ring *ring = container_of(work, typeof(*ring), work); struct ring_frame *frame; bool canceled = false; unsignedlong flags;
LIST_HEAD(done);
spin_lock_irqsave(&ring->lock, flags);
if (!ring->running) { /* Move all frames to done and mark them as canceled. */
list_splice_tail_init(&ring->in_flight, &done);
list_splice_tail_init(&ring->queue, &done);
canceled = true; goto invoke_callback;
}
invoke_callback: /* allow callbacks to schedule new work */
spin_unlock_irqrestore(&ring->lock, flags); while (!list_empty(&done)) {
frame = list_first_entry(&done, typeof(*frame), list); /* * The callback may reenqueue or delete frame. * Do not hold on to it.
*/
list_del_init(&frame->list); if (frame->callback)
frame->callback(ring, frame, canceled);
}
}
int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame)
{ unsignedlong flags; int ret = 0;
/** * tb_ring_poll() - Poll one completed frame from the ring * @ring: Ring to poll * * This function can be called when @start_poll callback of the @ring * has been called. It will read one completed frame from the ring and * return it to the caller. Returns %NULL if there is no more completed * frames.
*/ struct ring_frame *tb_ring_poll(struct tb_ring *ring)
{ struct ring_frame *frame = NULL; unsignedlong flags;
spin_lock_irqsave(&ring->lock, flags); if (!ring->running) goto unlock; if (ring_empty(ring)) goto unlock;
/** * tb_ring_poll_complete() - Re-start interrupt for the ring * @ring: Ring to re-start the interrupt * * This will re-start (unmask) the ring interrupt once the user is done * with polling.
*/ void tb_ring_poll_complete(struct tb_ring *ring)
{ unsignedlong flags;
/** * tb_ring_alloc_tx() - Allocate DMA ring for transmit * @nhi: Pointer to the NHI the ring is to be allocated * @hop: HopID (ring) to allocate * @size: Number of entries in the ring * @flags: Flags for the ring
*/ struct tb_ring *tb_ring_alloc_tx(struct tb_nhi *nhi, int hop, int size, unsignedint flags)
{ return tb_ring_alloc(nhi, hop, size, true, flags, 0, 0, 0, NULL, NULL);
}
EXPORT_SYMBOL_GPL(tb_ring_alloc_tx);
/** * tb_ring_alloc_rx() - Allocate DMA ring for receive * @nhi: Pointer to the NHI the ring is to be allocated * @hop: HopID (ring) to allocate. Pass %-1 for automatic allocation. * @size: Number of entries in the ring * @flags: Flags for the ring * @e2e_tx_hop: Transmit HopID when E2E is enabled in @flags * @sof_mask: Mask of PDF values that start a frame * @eof_mask: Mask of PDF values that end a frame * @start_poll: If not %NULL the ring will call this function when an * interrupt is triggered and masked, instead of callback * in each Rx frame. * @poll_data: Optional data passed to @start_poll
*/ struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size, unsignedint flags, int e2e_tx_hop,
u16 sof_mask, u16 eof_mask, void (*start_poll)(void *), void *poll_data)
{ return tb_ring_alloc(nhi, hop, size, false, flags, e2e_tx_hop, sof_mask, eof_mask,
start_poll, poll_data);
}
EXPORT_SYMBOL_GPL(tb_ring_alloc_rx);
/** * tb_ring_start() - enable a ring * @ring: Ring to start * * Must not be invoked in parallel with tb_ring_stop().
*/ void tb_ring_start(struct tb_ring *ring)
{
u16 frame_size;
u32 flags;
/* * Now that the ring valid bit is set we can configure E2E if * enabled for the ring.
*/ if (ring->flags & RING_FLAG_E2E) { if (!ring->is_tx) {
u32 hop;
hop = ring->e2e_tx_hop << REG_RX_OPTIONS_E2E_HOP_SHIFT;
hop &= REG_RX_OPTIONS_E2E_HOP_MASK;
flags |= hop;
dev_dbg(&ring->nhi->pdev->dev, "enabling E2E for %s %d with TX HopID %d\n",
RING_TYPE(ring), ring->hop, ring->e2e_tx_hop);
} else {
dev_dbg(&ring->nhi->pdev->dev, "enabling E2E for %s %d\n",
RING_TYPE(ring), ring->hop);
}
/** * tb_ring_stop() - shutdown a ring * @ring: Ring to stop * * Must not be invoked from a callback. * * This method will disable the ring. Further calls to * tb_ring_tx/tb_ring_rx will return -ESHUTDOWN until ring_stop has been * called. * * All enqueued frames will be canceled and their callbacks will be executed * with frame->canceled set to true (on the callback thread). This method * returns only after all callback invocations have finished.
*/ void tb_ring_stop(struct tb_ring *ring)
{
spin_lock_irq(&ring->nhi->lock);
spin_lock(&ring->lock);
dev_dbg(&ring->nhi->pdev->dev, "stopping %s %d\n",
RING_TYPE(ring), ring->hop); if (ring->nhi->going_away) goto err; if (!ring->running) {
dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n",
RING_TYPE(ring), ring->hop); goto err;
}
ring_interrupt_active(ring, false);
/* * schedule ring->work to invoke callbacks on all remaining frames.
*/
schedule_work(&ring->work);
flush_work(&ring->work);
}
EXPORT_SYMBOL_GPL(tb_ring_stop);
/* * tb_ring_free() - free ring * * When this method returns all invocations of ring->callback will have * finished. * * Ring must be stopped. * * Must NOT be called from ring_frame->callback!
*/ void tb_ring_free(struct tb_ring *ring)
{
spin_lock_irq(&ring->nhi->lock); /* * Dissociate the ring from the NHI. This also ensures that * nhi_interrupt_work cannot reschedule ring->work.
*/ if (ring->is_tx)
ring->nhi->tx_rings[ring->hop] = NULL; else
ring->nhi->rx_rings[ring->hop] = NULL;
if (ring->running) {
dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n",
RING_TYPE(ring), ring->hop);
}
spin_unlock_irq(&ring->nhi->lock);
/* * ring->work can no longer be scheduled (it is scheduled only * by nhi_interrupt_work, ring_stop and ring_msix). Wait for it * to finish before freeing the ring.
*/
flush_work(&ring->work);
kfree(ring);
}
EXPORT_SYMBOL_GPL(tb_ring_free);
/** * nhi_mailbox_cmd() - Send a command through NHI mailbox * @nhi: Pointer to the NHI structure * @cmd: Command to send * @data: Data to be send with the command * * Sends mailbox command to the firmware running on NHI. Returns %0 in * case of success and negative errno in case of failure.
*/ int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data)
{
ktime_t timeout;
u32 val;
iowrite32(data, nhi->iobase + REG_INMAIL_DATA);
val = ioread32(nhi->iobase + REG_INMAIL_CMD);
val &= ~(REG_INMAIL_CMD_MASK | REG_INMAIL_ERROR);
val |= REG_INMAIL_OP_REQUEST | cmd;
iowrite32(val, nhi->iobase + REG_INMAIL_CMD);
timeout = ktime_add_ms(ktime_get(), NHI_MAILBOX_TIMEOUT); do {
val = ioread32(nhi->iobase + REG_INMAIL_CMD); if (!(val & REG_INMAIL_OP_REQUEST)) break;
usleep_range(10, 20);
} while (ktime_before(ktime_get(), timeout));
if (val & REG_INMAIL_OP_REQUEST) return -ETIMEDOUT; if (val & REG_INMAIL_ERROR) return -EIO;
return 0;
}
/** * nhi_mailbox_mode() - Return current firmware operation mode * @nhi: Pointer to the NHI structure * * The function reads current firmware operation mode using NHI mailbox * registers and returns it to the caller.
*/ enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi)
{
u32 val;
val = ioread32(nhi->iobase + REG_OUTMAIL_CMD);
val &= REG_OUTMAIL_CMD_OPMODE_MASK;
val >>= REG_OUTMAIL_CMD_OPMODE_SHIFT;
return (enum nhi_fw_mode)val;
}
staticvoid nhi_interrupt_work(struct work_struct *work)
{ struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work); int value = 0; /* Suppress uninitialized usage warning. */ int bit; int hop = -1; int type = 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */ struct tb_ring *ring;
spin_lock_irq(&nhi->lock);
/* * Starting at REG_RING_NOTIFY_BASE there are three status bitfields * (TX, RX, RX overflow). We iterate over the bits and read a new * dwords as required. The registers are cleared on read.
*/ for (bit = 0; bit < 3 * nhi->hop_count; bit++) { if (bit % 32 == 0)
value = ioread32(nhi->iobase
+ REG_RING_NOTIFY_BASE
+ 4 * (bit / 32)); if (++hop == nhi->hop_count) {
hop = 0;
type++;
} if ((value & (1 << (bit % 32))) == 0) continue; if (type == 2) {
dev_warn(&nhi->pdev->dev, "RX overflow for ring %d\n",
hop); continue;
} if (type == 0)
ring = nhi->tx_rings[hop]; else
ring = nhi->rx_rings[hop]; if (ring == NULL) {
dev_warn(&nhi->pdev->dev, "got interrupt for inactive %s ring %d\n",
type ? "RX" : "TX",
hop); continue;
}
/* * If power rails are sustainable for wakeup from S4 this * property is set by the BIOS.
*/ if (device_property_read_u8(&pdev->dev, "WAKE_SUPPORTED", &val)) return !!val;
/* * Configure interrupt throttling for all vectors even if we * only use few.
*/ for (i = 0; i < MSIX_MAX_VECS; i++) {
u32 reg = REG_INT_THROTTLING_RATE + i * 4;
iowrite32(throttle, nhi->iobase + reg);
}
}
/* * Check that the device is still there. It may be that the user * unplugged last device which causes the host controller to go * away on PCs.
*/ if (!pci_device_is_present(pdev)) {
nhi->going_away = true;
} else { if (nhi->ops && nhi->ops->resume_noirq) {
ret = nhi->ops->resume_noirq(nhi); if (ret) return ret;
}
nhi_enable_int_throttling(tb->nhi);
}
/* * If we were runtime suspended when system suspend started, * schedule runtime resume now. It should bring the domain back * to functional state.
*/ if (pm_runtime_suspended(&pdev->dev))
pm_runtime_resume(&pdev->dev); else
tb_domain_complete(tb);
}
staticvoid nhi_shutdown(struct tb_nhi *nhi)
{ int i;
dev_dbg(&nhi->pdev->dev, "shutdown\n");
for (i = 0; i < nhi->hop_count; i++) { if (nhi->tx_rings[i])
dev_WARN(&nhi->pdev->dev, "TX ring %d is still active\n", i); if (nhi->rx_rings[i])
dev_WARN(&nhi->pdev->dev, "RX ring %d is still active\n", i);
}
nhi_disable_interrupts(nhi); /* * We have to release the irq before calling flush_work. Otherwise an * already executing IRQ handler could call schedule_work again.
*/ if (!nhi->pdev->msix_enabled) {
devm_free_irq(&nhi->pdev->dev, nhi->pdev->irq, nhi);
flush_work(&nhi->interrupt_work);
}
ida_destroy(&nhi->msix_ida);
if (nhi->ops && nhi->ops->shutdown)
nhi->ops->shutdown(nhi);
}
staticvoid nhi_check_quirks(struct tb_nhi *nhi)
{ if (nhi->pdev->vendor == PCI_VENDOR_ID_INTEL) { /* * Intel hardware supports auto clear of the interrupt * status register right after interrupt is being * issued.
*/
nhi->quirks |= QUIRK_AUTO_CLEAR_INT;
switch (nhi->pdev->device) { case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI: case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI: /* * Falcon Ridge controller needs the end-to-end * flow control workaround to avoid losing Rx * packets when RING_FLAG_E2E is set.
*/
nhi->quirks |= QUIRK_E2E; break;
}
}
}
/* * Ideally what we'd do here is grab every PCI device that * represents a tunnelling adapter for this NHI and check their * status directly, but unfortunately USB4 seems to make it * obnoxiously difficult to reliably make any correlation. * * So for now we'll have to bodge it... Hoping that the system * is at least sane enough that an adapter is in the same PCI * segment as its NHI, if we can find *something* on that segment * which meets the requirements for Kernel DMA Protection, we'll * take that to imply that firmware is aware and has (hopefully) * done the right thing in general. We need to know that the PCI * layer has seen the ExternalFacingPort property which will then * inform the IOMMU layer to enforce the complete "untrusted DMA" * flow, but also that the IOMMU driver itself can be trusted not * to have been subverted by a pre-boot DMA attack.
*/ while (bus->parent)
bus = bus->parent;
/* In case someone left them on. */
nhi_disable_interrupts(nhi);
nhi_enable_int_throttling(nhi);
ida_init(&nhi->msix_ida);
/* * The NHI has 16 MSI-X vectors or a single MSI. We first try to * get all MSI-X vectors and if we succeed, each ring will have * one MSI-X. If for some reason that does not work out, we * fallback to a single MSI.
*/
nvec = pci_alloc_irq_vectors(pdev, MSIX_MIN_VECS, MSIX_MAX_VECS,
PCI_IRQ_MSIX); if (nvec < 0) {
nvec = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI); if (nvec < 0) return nvec;
/* * USB4 case is simple. If we got control of any of the * capabilities, we use software CM.
*/ if (tb_acpi_is_native()) return tb_probe(nhi);
/* * Either firmware based CM is running (we did not get control * from the firmware) or this is pre-USB4 PC so try first * firmware CM and then fallback to software CM.
*/
tb = icm_probe(nhi); if (!tb)
tb = tb_probe(nhi);
res = tb_domain_add(tb, host_reset); if (res) { /* * At this point the RX/TX rings might already have been * activated. Do a proper shutdown.
*/
tb_domain_put(tb);
nhi_shutdown(nhi); return res;
}
pci_set_drvdata(pdev, tb);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.