1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016, NVIDIA CORPORATION.
9 #include <asm/arch-tegra/ivc.h>
10 #include <linux/bug.h>
12 #define TEGRA_IVC_ALIGN 64
15 * IVC channel reset protocol.
17 * Each end uses its tx_channel.state to indicate its synchronization state.
21 * This value is zero for backwards compatibility with services that
22 * assume channels to be initially zeroed. Such channels are in an
23 * initially valid state, but cannot be asynchronously reset, and must
24 * maintain a valid state at all times.
26 * The transmitting end can enter the established state from the sync or
27 * ack state when it observes the receiving endpoint in the ack or
28 * established state, indicating that has cleared the counters in our
31 ivc_state_established = 0,
34 * If an endpoint is observed in the sync state, the remote endpoint is
35 * allowed to clear the counters it owns asynchronously with respect to
36 * the current endpoint. Therefore, the current endpoint is no longer
37 * allowed to communicate.
42 * When the transmitting end observes the receiving end in the sync
43 * state, it can clear the w_count and r_count and transition to the ack
44 * state. If the remote endpoint observes us in the ack state, it can
45 * return to the established state once it has cleared its counters.
51 * This structure is divided into two-cache aligned parts, the first is only
52 * written through the tx_channel pointer, while the second is only written
53 * through the rx_channel pointer. This delineates ownership of the cache lines,
54 * which is critical to performance and necessary in non-cache coherent
57 struct tegra_ivc_channel_header {
59 /* fields owned by the transmitting end */
64 uint8_t w_align[TEGRA_IVC_ALIGN];
67 /* fields owned by the receiving end */
69 uint8_t r_align[TEGRA_IVC_ALIGN];
73 static inline void tegra_ivc_invalidate_counter(struct tegra_ivc *ivc,
74 struct tegra_ivc_channel_header *h,
77 ulong base = ((ulong)h) + offset;
78 invalidate_dcache_range(base, base + TEGRA_IVC_ALIGN);
81 static inline void tegra_ivc_flush_counter(struct tegra_ivc *ivc,
82 struct tegra_ivc_channel_header *h,
85 ulong base = ((ulong)h) + offset;
86 flush_dcache_range(base, base + TEGRA_IVC_ALIGN);
89 static inline ulong tegra_ivc_frame_addr(struct tegra_ivc *ivc,
90 struct tegra_ivc_channel_header *h,
93 BUG_ON(frame >= ivc->nframes);
95 return ((ulong)h) + sizeof(struct tegra_ivc_channel_header) +
96 (ivc->frame_size * frame);
99 static inline void *tegra_ivc_frame_pointer(struct tegra_ivc *ivc,
100 struct tegra_ivc_channel_header *ch,
103 return (void *)tegra_ivc_frame_addr(ivc, ch, frame);
106 static inline void tegra_ivc_invalidate_frame(struct tegra_ivc *ivc,
107 struct tegra_ivc_channel_header *h,
110 ulong base = tegra_ivc_frame_addr(ivc, h, frame);
111 invalidate_dcache_range(base, base + ivc->frame_size);
114 static inline void tegra_ivc_flush_frame(struct tegra_ivc *ivc,
115 struct tegra_ivc_channel_header *h,
118 ulong base = tegra_ivc_frame_addr(ivc, h, frame);
119 flush_dcache_range(base, base + ivc->frame_size);
122 static inline int tegra_ivc_channel_empty(struct tegra_ivc *ivc,
123 struct tegra_ivc_channel_header *ch)
126 * This function performs multiple checks on the same values with
127 * security implications, so create snapshots with READ_ONCE() to
128 * ensure that these checks use the same values.
130 uint32_t w_count = READ_ONCE(ch->w_count);
131 uint32_t r_count = READ_ONCE(ch->r_count);
134 * Perform an over-full check to prevent denial of service attacks where
135 * a server could be easily fooled into believing that there's an
136 * extremely large number of frames ready, since receivers are not
137 * expected to check for full or over-full conditions.
139 * Although the channel isn't empty, this is an invalid case caused by
140 * a potentially malicious peer, so returning empty is safer, because it
141 * gives the impression that the channel has gone silent.
143 if (w_count - r_count > ivc->nframes)
146 return w_count == r_count;
149 static inline int tegra_ivc_channel_full(struct tegra_ivc *ivc,
150 struct tegra_ivc_channel_header *ch)
153 * Invalid cases where the counters indicate that the queue is over
154 * capacity also appear full.
156 return (READ_ONCE(ch->w_count) - READ_ONCE(ch->r_count)) >=
160 static inline void tegra_ivc_advance_rx(struct tegra_ivc *ivc)
162 WRITE_ONCE(ivc->rx_channel->r_count,
163 READ_ONCE(ivc->rx_channel->r_count) + 1);
165 if (ivc->r_pos == ivc->nframes - 1)
171 static inline void tegra_ivc_advance_tx(struct tegra_ivc *ivc)
173 WRITE_ONCE(ivc->tx_channel->w_count,
174 READ_ONCE(ivc->tx_channel->w_count) + 1);
176 if (ivc->w_pos == ivc->nframes - 1)
182 static inline int tegra_ivc_check_read(struct tegra_ivc *ivc)
187 * tx_channel->state is set locally, so it is not synchronized with
188 * state from the remote peer. The remote peer cannot reset its
189 * transmit counters until we've acknowledged its synchronization
190 * request, so no additional synchronization is required because an
191 * asynchronous transition of rx_channel->state to ivc_state_ack is not
194 if (ivc->tx_channel->state != ivc_state_established)
198 * Avoid unnecessary invalidations when performing repeated accesses to
199 * an IVC channel by checking the old queue pointers first.
200 * Synchronization is only necessary when these pointers indicate empty
203 if (!tegra_ivc_channel_empty(ivc, ivc->rx_channel))
206 offset = offsetof(struct tegra_ivc_channel_header, w_count);
207 tegra_ivc_invalidate_counter(ivc, ivc->rx_channel, offset);
208 return tegra_ivc_channel_empty(ivc, ivc->rx_channel) ? -ENOMEM : 0;
211 static inline int tegra_ivc_check_write(struct tegra_ivc *ivc)
215 if (ivc->tx_channel->state != ivc_state_established)
218 if (!tegra_ivc_channel_full(ivc, ivc->tx_channel))
221 offset = offsetof(struct tegra_ivc_channel_header, r_count);
222 tegra_ivc_invalidate_counter(ivc, ivc->tx_channel, offset);
223 return tegra_ivc_channel_full(ivc, ivc->tx_channel) ? -ENOMEM : 0;
226 static inline uint32_t tegra_ivc_channel_avail_count(struct tegra_ivc *ivc,
227 struct tegra_ivc_channel_header *ch)
230 * This function isn't expected to be used in scenarios where an
231 * over-full situation can lead to denial of service attacks. See the
232 * comment in tegra_ivc_channel_empty() for an explanation about
233 * special over-full considerations.
235 return READ_ONCE(ch->w_count) - READ_ONCE(ch->r_count);
238 int tegra_ivc_read_get_next_frame(struct tegra_ivc *ivc, void **frame)
240 int result = tegra_ivc_check_read(ivc);
245 * Order observation of w_pos potentially indicating new data before
250 tegra_ivc_invalidate_frame(ivc, ivc->rx_channel, ivc->r_pos);
251 *frame = tegra_ivc_frame_pointer(ivc, ivc->rx_channel, ivc->r_pos);
256 int tegra_ivc_read_advance(struct tegra_ivc *ivc)
262 * No read barriers or synchronization here: the caller is expected to
263 * have already observed the channel non-empty. This check is just to
264 * catch programming errors.
266 result = tegra_ivc_check_read(ivc);
270 tegra_ivc_advance_rx(ivc);
271 offset = offsetof(struct tegra_ivc_channel_header, r_count);
272 tegra_ivc_flush_counter(ivc, ivc->rx_channel, offset);
275 * Ensure our write to r_pos occurs before our read from w_pos.
279 offset = offsetof(struct tegra_ivc_channel_header, w_count);
280 tegra_ivc_invalidate_counter(ivc, ivc->rx_channel, offset);
282 if (tegra_ivc_channel_avail_count(ivc, ivc->rx_channel) ==
289 int tegra_ivc_write_get_next_frame(struct tegra_ivc *ivc, void **frame)
291 int result = tegra_ivc_check_write(ivc);
295 *frame = tegra_ivc_frame_pointer(ivc, ivc->tx_channel, ivc->w_pos);
300 int tegra_ivc_write_advance(struct tegra_ivc *ivc)
305 result = tegra_ivc_check_write(ivc);
309 tegra_ivc_flush_frame(ivc, ivc->tx_channel, ivc->w_pos);
312 * Order any possible stores to the frame before update of w_pos.
316 tegra_ivc_advance_tx(ivc);
317 offset = offsetof(struct tegra_ivc_channel_header, w_count);
318 tegra_ivc_flush_counter(ivc, ivc->tx_channel, offset);
321 * Ensure our write to w_pos occurs before our read from r_pos.
325 offset = offsetof(struct tegra_ivc_channel_header, r_count);
326 tegra_ivc_invalidate_counter(ivc, ivc->tx_channel, offset);
328 if (tegra_ivc_channel_avail_count(ivc, ivc->tx_channel) == 1)
335 * ===============================================================
336 * IVC State Transition Table - see tegra_ivc_channel_notified()
337 * ===============================================================
339 * local remote action
340 * ----- ------ -----------------------------------
342 * SYNC ACK reset counters; move to EST; notify
343 * SYNC SYNC reset counters; move to ACK; notify
344 * ACK EST move to EST; notify
345 * ACK ACK move to EST; notify
346 * ACK SYNC reset counters; move to ACK; notify
349 * EST SYNC reset counters; move to ACK; notify
351 * ===============================================================
353 int tegra_ivc_channel_notified(struct tegra_ivc *ivc)
356 enum ivc_state peer_state;
358 /* Copy the receiver's state out of shared memory. */
359 offset = offsetof(struct tegra_ivc_channel_header, w_count);
360 tegra_ivc_invalidate_counter(ivc, ivc->rx_channel, offset);
361 peer_state = READ_ONCE(ivc->rx_channel->state);
363 if (peer_state == ivc_state_sync) {
365 * Order observation of ivc_state_sync before stores clearing
371 * Reset tx_channel counters. The remote end is in the SYNC
372 * state and won't make progress until we change our state,
373 * so the counters are not in use at this time.
375 ivc->tx_channel->w_count = 0;
376 ivc->rx_channel->r_count = 0;
382 * Ensure that counters appear cleared before new state can be
388 * Move to ACK state. We have just cleared our counters, so it
389 * is now safe for the remote end to start using these values.
391 ivc->tx_channel->state = ivc_state_ack;
392 offset = offsetof(struct tegra_ivc_channel_header, w_count);
393 tegra_ivc_flush_counter(ivc, ivc->tx_channel, offset);
396 * Notify remote end to observe state transition.
399 } else if (ivc->tx_channel->state == ivc_state_sync &&
400 peer_state == ivc_state_ack) {
402 * Order observation of ivc_state_sync before stores clearing
408 * Reset tx_channel counters. The remote end is in the ACK
409 * state and won't make progress until we change our state,
410 * so the counters are not in use at this time.
412 ivc->tx_channel->w_count = 0;
413 ivc->rx_channel->r_count = 0;
419 * Ensure that counters appear cleared before new state can be
425 * Move to ESTABLISHED state. We know that the remote end has
426 * already cleared its counters, so it is safe to start
427 * writing/reading on this channel.
429 ivc->tx_channel->state = ivc_state_established;
430 offset = offsetof(struct tegra_ivc_channel_header, w_count);
431 tegra_ivc_flush_counter(ivc, ivc->tx_channel, offset);
434 * Notify remote end to observe state transition.
437 } else if (ivc->tx_channel->state == ivc_state_ack) {
439 * At this point, we have observed the peer to be in either
440 * the ACK or ESTABLISHED state. Next, order observation of
441 * peer state before storing to tx_channel.
446 * Move to ESTABLISHED state. We know that we have previously
447 * cleared our counters, and we know that the remote end has
448 * cleared its counters, so it is safe to start writing/reading
451 ivc->tx_channel->state = ivc_state_established;
452 offset = offsetof(struct tegra_ivc_channel_header, w_count);
453 tegra_ivc_flush_counter(ivc, ivc->tx_channel, offset);
456 * Notify remote end to observe state transition.
461 * There is no need to handle any further action. Either the
462 * channel is already fully established, or we are waiting for
463 * the remote end to catch up with our current state. Refer
464 * to the diagram in "IVC State Transition Table" above.
468 if (ivc->tx_channel->state != ivc_state_established)
474 void tegra_ivc_channel_reset(struct tegra_ivc *ivc)
478 ivc->tx_channel->state = ivc_state_sync;
479 offset = offsetof(struct tegra_ivc_channel_header, w_count);
480 tegra_ivc_flush_counter(ivc, ivc->tx_channel, offset);
484 static int check_ivc_params(ulong qbase1, ulong qbase2, uint32_t nframes,
489 BUG_ON(offsetof(struct tegra_ivc_channel_header, w_count) &
490 (TEGRA_IVC_ALIGN - 1));
491 BUG_ON(offsetof(struct tegra_ivc_channel_header, r_count) &
492 (TEGRA_IVC_ALIGN - 1));
493 BUG_ON(sizeof(struct tegra_ivc_channel_header) &
494 (TEGRA_IVC_ALIGN - 1));
496 if ((uint64_t)nframes * (uint64_t)frame_size >= 0x100000000) {
497 pr_err("tegra_ivc: nframes * frame_size overflows\n");
502 * The headers must at least be aligned enough for counters
503 * to be accessed atomically.
505 if ((qbase1 & (TEGRA_IVC_ALIGN - 1)) ||
506 (qbase2 & (TEGRA_IVC_ALIGN - 1))) {
507 pr_err("tegra_ivc: channel start not aligned\n");
511 if (frame_size & (TEGRA_IVC_ALIGN - 1)) {
512 pr_err("tegra_ivc: frame size not adequately aligned\n");
516 if (qbase1 < qbase2) {
517 if (qbase1 + frame_size * nframes > qbase2)
520 if (qbase2 + frame_size * nframes > qbase1)
525 pr_err("tegra_ivc: queue regions overlap\n");
532 int tegra_ivc_init(struct tegra_ivc *ivc, ulong rx_base, ulong tx_base,
533 uint32_t nframes, uint32_t frame_size,
534 void (*notify)(struct tegra_ivc *))
541 ret = check_ivc_params(rx_base, tx_base, nframes, frame_size);
545 ivc->rx_channel = (struct tegra_ivc_channel_header *)rx_base;
546 ivc->tx_channel = (struct tegra_ivc_channel_header *)tx_base;
549 ivc->nframes = nframes;
550 ivc->frame_size = frame_size;
551 ivc->notify = notify;