// SPDX-License-Identifier: GPL-2.0-only /* * Copyright (c) 2014-2016, NVIDIA CORPORATION. All rights reserved.
*/
#include <soc/tegra/ivc.h>
#define TEGRA_IVC_ALIGN 64
/* * IVC channel reset protocol. * * Each end uses its tx_channel.state to indicate its synchronization state.
*/ enum tegra_ivc_state { /* * This value is zero for backwards compatibility with services that * assume channels to be initially zeroed. Such channels are in an * initially valid state, but cannot be asynchronously reset, and must * maintain a valid state at all times. * * The transmitting end can enter the established state from the sync or * ack state when it observes the receiving endpoint in the ack or * established state, indicating that has cleared the counters in our * rx_channel.
*/
TEGRA_IVC_STATE_ESTABLISHED = 0,
/* * If an endpoint is observed in the sync state, the remote endpoint is * allowed to clear the counters it owns asynchronously with respect to * the current endpoint. Therefore, the current endpoint is no longer * allowed to communicate.
*/
TEGRA_IVC_STATE_SYNC,
/* * When the transmitting end observes the receiving end in the sync * state, it can clear the w_count and r_count and transition to the ack * state. If the remote endpoint observes us in the ack state, it can * return to the established state once it has cleared its counters.
*/
TEGRA_IVC_STATE_ACK
};
/* * This structure is divided into two-cache aligned parts, the first is only * written through the tx.channel pointer, while the second is only written * through the rx.channel pointer. This delineates ownership of the cache * lines, which is critical to performance and necessary in non-cache coherent * implementations.
*/ struct tegra_ivc_header { union { struct { /* fields owned by the transmitting end */
u32 count;
u32 state;
};
u8 pad[TEGRA_IVC_ALIGN];
} tx;
union { /* fields owned by the receiving end */
u32 count;
u8 pad[TEGRA_IVC_ALIGN];
} rx;
};
staticinlinebool tegra_ivc_empty(struct tegra_ivc *ivc, struct iosys_map *map)
{ /* * This function performs multiple checks on the same values with * security implications, so create snapshots with READ_ONCE() to * ensure that these checks use the same values.
*/
u32 tx = tegra_ivc_header_read_field(map, tx.count);
u32 rx = tegra_ivc_header_read_field(map, rx.count);
/* * Perform an over-full check to prevent denial of service attacks * where a server could be easily fooled into believing that there's * an extremely large number of frames ready, since receivers are not * expected to check for full or over-full conditions. * * Although the channel isn't empty, this is an invalid case caused by * a potentially malicious peer, so returning empty is safer, because * it gives the impression that the channel has gone silent.
*/ if (tx - rx > ivc->num_frames) returntrue;
/* * This function isn't expected to be used in scenarios where an * over-full situation can lead to denial of service attacks. See the * comment in tegra_ivc_empty() for an explanation about special * over-full considerations.
*/ return tx - rx;
}
/* * tx.channel->state is set locally, so it is not synchronized with * state from the remote peer. The remote peer cannot reset its * transmit counters until we've acknowledged its synchronization * request, so no additional synchronization is required because an * asynchronous transition of rx.channel->state to * TEGRA_IVC_STATE_ACK is not allowed.
*/
state = tegra_ivc_header_read_field(&ivc->tx.map, tx.state); if (state != TEGRA_IVC_STATE_ESTABLISHED) return -ECONNRESET;
/* * Avoid unnecessary invalidations when performing repeated accesses * to an IVC channel by checking the old queue pointers first. * * Synchronization is only necessary when these pointers indicate * empty or full.
*/ if (!tegra_ivc_empty(ivc, &ivc->rx.map)) return 0;
tegra_ivc_invalidate(ivc, ivc->rx.phys + offset);
if (tegra_ivc_empty(ivc, &ivc->rx.map)) return -ENOSPC;
int tegra_ivc_read_advance(struct tegra_ivc *ivc)
{ unsignedint rx = offsetof(struct tegra_ivc_header, rx.count); unsignedint tx = offsetof(struct tegra_ivc_header, tx.count); int err;
/* * No read barriers or synchronization here: the caller is expected to * have already observed the channel non-empty. This check is just to * catch programming errors.
*/
err = tegra_ivc_check_read(ivc); if (err < 0) return err;
tegra_ivc_advance_rx(ivc);
tegra_ivc_flush(ivc, ivc->rx.phys + rx);
/* * Ensure our write to ivc->rx.position occurs before our read from * ivc->tx.position.
*/
smp_mb();
/* * Notify only upon transition from full to non-full. The available * count can only asynchronously increase, so the worst possible * side-effect will be a spurious notification.
*/
tegra_ivc_invalidate(ivc, ivc->rx.phys + tx);
if (tegra_ivc_available(ivc, &ivc->rx.map) == ivc->num_frames - 1)
ivc->notify(ivc, ivc->notify_data);
/* * Ensure our write to ivc->tx.position occurs before our read from * ivc->rx.position.
*/
smp_mb();
/* * Notify only upon transition from empty to non-empty. The available * count can only asynchronously decrease, so the worst possible * side-effect will be a spurious notification.
*/
tegra_ivc_invalidate(ivc, ivc->tx.phys + rx);
if (tegra_ivc_available(ivc, &ivc->tx.map) == 1)
ivc->notify(ivc, ivc->notify_data);
/* * ======================================================= * IVC State Transition Table - see tegra_ivc_notified() * ======================================================= * * local remote action * ----- ------ ----------------------------------- * SYNC EST <none> * SYNC ACK reset counters; move to EST; notify * SYNC SYNC reset counters; move to ACK; notify * ACK EST move to EST; notify * ACK ACK move to EST; notify * ACK SYNC reset counters; move to ACK; notify * EST EST <none> * EST ACK <none> * EST SYNC reset counters; move to ACK; notify * * ===============================================================
*/
/* Copy the receiver's state out of shared memory. */
tegra_ivc_invalidate(ivc, ivc->rx.phys + offset);
rx_state = tegra_ivc_header_read_field(&ivc->rx.map, tx.state);
tx_state = tegra_ivc_header_read_field(&ivc->tx.map, tx.state);
if (rx_state == TEGRA_IVC_STATE_SYNC) {
offset = offsetof(struct tegra_ivc_header, tx.count);
/* * Order observation of TEGRA_IVC_STATE_SYNC before stores * clearing tx.channel.
*/
smp_rmb();
/* * Reset tx.channel counters. The remote end is in the SYNC * state and won't make progress until we change our state, * so the counters are not in use at this time.
*/
tegra_ivc_header_write_field(&ivc->tx.map, tx.count, 0);
tegra_ivc_header_write_field(&ivc->rx.map, rx.count, 0);
ivc->tx.position = 0;
ivc->rx.position = 0;
/* * Ensure that counters appear cleared before new state can be * observed.
*/
smp_wmb();
/* * Move to ACK state. We have just cleared our counters, so it * is now safe for the remote end to start using these values.
*/
tegra_ivc_header_write_field(&ivc->tx.map, tx.state, TEGRA_IVC_STATE_ACK);
tegra_ivc_flush(ivc, ivc->tx.phys + offset);
/* * Notify remote end to observe state transition.
*/
ivc->notify(ivc, ivc->notify_data);
/* * Order observation of ivc_state_sync before stores clearing * tx_channel.
*/
smp_rmb();
/* * Reset tx.channel counters. The remote end is in the ACK * state and won't make progress until we change our state, * so the counters are not in use at this time.
*/
tegra_ivc_header_write_field(&ivc->tx.map, tx.count, 0);
tegra_ivc_header_write_field(&ivc->rx.map, rx.count, 0);
ivc->tx.position = 0;
ivc->rx.position = 0;
/* * Ensure that counters appear cleared before new state can be * observed.
*/
smp_wmb();
/* * Move to ESTABLISHED state. We know that the remote end has * already cleared its counters, so it is safe to start * writing/reading on this channel.
*/
tegra_ivc_header_write_field(&ivc->tx.map, tx.state, TEGRA_IVC_STATE_ESTABLISHED);
tegra_ivc_flush(ivc, ivc->tx.phys + offset);
/* * Notify remote end to observe state transition.
*/
ivc->notify(ivc, ivc->notify_data);
/* * At this point, we have observed the peer to be in either * the ACK or ESTABLISHED state. Next, order observation of * peer state before storing to tx.channel.
*/
smp_rmb();
/* * Move to ESTABLISHED state. We know that we have previously * cleared our counters, and we know that the remote end has * cleared its counters, so it is safe to start writing/reading * on this channel.
*/
tegra_ivc_header_write_field(&ivc->tx.map, tx.state, TEGRA_IVC_STATE_ESTABLISHED);
tegra_ivc_flush(ivc, ivc->tx.phys + offset);
/* * Notify remote end to observe state transition.
*/
ivc->notify(ivc, ivc->notify_data);
} else { /* * There is no need to handle any further action. Either the * channel is already fully established, or we are waiting for * the remote end to catch up with our current state. Refer * to the diagram in "IVC State Transition Table" above.
*/
}
if (tx_state != TEGRA_IVC_STATE_ESTABLISHED) return -EAGAIN;
if (!IS_ALIGNED(frame_size, TEGRA_IVC_ALIGN)) {
pr_err("frame size not adequately aligned: %zu\n", frame_size); return -EINVAL;
}
/* * The headers must at least be aligned enough for counters * to be accessed atomically.
*/ if (!IS_ALIGNED(rx, TEGRA_IVC_ALIGN)) {
pr_err("IVC channel start not aligned: %#lx\n", rx); return -EINVAL;
}
if (!IS_ALIGNED(tx, TEGRA_IVC_ALIGN)) {
pr_err("IVC channel start not aligned: %#lx\n", tx); return -EINVAL;
}
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.