xref: /openbmc/linux/drivers/firmware/tegra/ivc.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
12025cf9eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ca791d7fSThierry Reding /*
3ca791d7fSThierry Reding  * Copyright (c) 2014-2016, NVIDIA CORPORATION.  All rights reserved.
4ca791d7fSThierry Reding  */
5ca791d7fSThierry Reding 
6ca791d7fSThierry Reding #include <soc/tegra/ivc.h>
7ca791d7fSThierry Reding 
8ca791d7fSThierry Reding #define TEGRA_IVC_ALIGN 64
9ca791d7fSThierry Reding 
10ca791d7fSThierry Reding /*
11ca791d7fSThierry Reding  * IVC channel reset protocol.
12ca791d7fSThierry Reding  *
13ca791d7fSThierry Reding  * Each end uses its tx_channel.state to indicate its synchronization state.
14ca791d7fSThierry Reding  */
15ca791d7fSThierry Reding enum tegra_ivc_state {
16ca791d7fSThierry Reding 	/*
17ca791d7fSThierry Reding 	 * This value is zero for backwards compatibility with services that
18ca791d7fSThierry Reding 	 * assume channels to be initially zeroed. Such channels are in an
19ca791d7fSThierry Reding 	 * initially valid state, but cannot be asynchronously reset, and must
20ca791d7fSThierry Reding 	 * maintain a valid state at all times.
21ca791d7fSThierry Reding 	 *
22ca791d7fSThierry Reding 	 * The transmitting end can enter the established state from the sync or
23ca791d7fSThierry Reding 	 * ack state when it observes the receiving endpoint in the ack or
24ca791d7fSThierry Reding 	 * established state, indicating that has cleared the counters in our
25ca791d7fSThierry Reding 	 * rx_channel.
26ca791d7fSThierry Reding 	 */
27ca791d7fSThierry Reding 	TEGRA_IVC_STATE_ESTABLISHED = 0,
28ca791d7fSThierry Reding 
29ca791d7fSThierry Reding 	/*
30ca791d7fSThierry Reding 	 * If an endpoint is observed in the sync state, the remote endpoint is
31ca791d7fSThierry Reding 	 * allowed to clear the counters it owns asynchronously with respect to
32ca791d7fSThierry Reding 	 * the current endpoint. Therefore, the current endpoint is no longer
33ca791d7fSThierry Reding 	 * allowed to communicate.
34ca791d7fSThierry Reding 	 */
35ca791d7fSThierry Reding 	TEGRA_IVC_STATE_SYNC,
36ca791d7fSThierry Reding 
37ca791d7fSThierry Reding 	/*
38ca791d7fSThierry Reding 	 * When the transmitting end observes the receiving end in the sync
39ca791d7fSThierry Reding 	 * state, it can clear the w_count and r_count and transition to the ack
40ca791d7fSThierry Reding 	 * state. If the remote endpoint observes us in the ack state, it can
41ca791d7fSThierry Reding 	 * return to the established state once it has cleared its counters.
42ca791d7fSThierry Reding 	 */
43ca791d7fSThierry Reding 	TEGRA_IVC_STATE_ACK
44ca791d7fSThierry Reding };
45ca791d7fSThierry Reding 
46ca791d7fSThierry Reding /*
47ca791d7fSThierry Reding  * This structure is divided into two-cache aligned parts, the first is only
48ca791d7fSThierry Reding  * written through the tx.channel pointer, while the second is only written
49ca791d7fSThierry Reding  * through the rx.channel pointer. This delineates ownership of the cache
50ca791d7fSThierry Reding  * lines, which is critical to performance and necessary in non-cache coherent
51ca791d7fSThierry Reding  * implementations.
52ca791d7fSThierry Reding  */
53ca791d7fSThierry Reding struct tegra_ivc_header {
54ca791d7fSThierry Reding 	union {
55ca791d7fSThierry Reding 		struct {
56ca791d7fSThierry Reding 			/* fields owned by the transmitting end */
57ca791d7fSThierry Reding 			u32 count;
58ca791d7fSThierry Reding 			u32 state;
59ca791d7fSThierry Reding 		};
60ca791d7fSThierry Reding 
61ca791d7fSThierry Reding 		u8 pad[TEGRA_IVC_ALIGN];
62ca791d7fSThierry Reding 	} tx;
63ca791d7fSThierry Reding 
64ca791d7fSThierry Reding 	union {
65ca791d7fSThierry Reding 		/* fields owned by the receiving end */
66ca791d7fSThierry Reding 		u32 count;
67ca791d7fSThierry Reding 		u8 pad[TEGRA_IVC_ALIGN];
68ca791d7fSThierry Reding 	} rx;
69ca791d7fSThierry Reding };
70ca791d7fSThierry Reding 
71*4c1e0a97SThierry Reding #define tegra_ivc_header_read_field(hdr, field) \
72*4c1e0a97SThierry Reding 	iosys_map_rd_field(hdr, 0, struct tegra_ivc_header, field)
73*4c1e0a97SThierry Reding 
74*4c1e0a97SThierry Reding #define tegra_ivc_header_write_field(hdr, field, value) \
75*4c1e0a97SThierry Reding 	iosys_map_wr_field(hdr, 0, struct tegra_ivc_header, field, value)
76*4c1e0a97SThierry Reding 
tegra_ivc_invalidate(struct tegra_ivc * ivc,dma_addr_t phys)77ca791d7fSThierry Reding static inline void tegra_ivc_invalidate(struct tegra_ivc *ivc, dma_addr_t phys)
78ca791d7fSThierry Reding {
79ca791d7fSThierry Reding 	if (!ivc->peer)
80ca791d7fSThierry Reding 		return;
81ca791d7fSThierry Reding 
82ca791d7fSThierry Reding 	dma_sync_single_for_cpu(ivc->peer, phys, TEGRA_IVC_ALIGN,
83ca791d7fSThierry Reding 				DMA_FROM_DEVICE);
84ca791d7fSThierry Reding }
85ca791d7fSThierry Reding 
tegra_ivc_flush(struct tegra_ivc * ivc,dma_addr_t phys)86ca791d7fSThierry Reding static inline void tegra_ivc_flush(struct tegra_ivc *ivc, dma_addr_t phys)
87ca791d7fSThierry Reding {
88ca791d7fSThierry Reding 	if (!ivc->peer)
89ca791d7fSThierry Reding 		return;
90ca791d7fSThierry Reding 
91ca791d7fSThierry Reding 	dma_sync_single_for_device(ivc->peer, phys, TEGRA_IVC_ALIGN,
92ca791d7fSThierry Reding 				   DMA_TO_DEVICE);
93ca791d7fSThierry Reding }
94ca791d7fSThierry Reding 
tegra_ivc_empty(struct tegra_ivc * ivc,struct iosys_map * map)95*4c1e0a97SThierry Reding static inline bool tegra_ivc_empty(struct tegra_ivc *ivc, struct iosys_map *map)
96ca791d7fSThierry Reding {
97ca791d7fSThierry Reding 	/*
98ca791d7fSThierry Reding 	 * This function performs multiple checks on the same values with
99eeafcc5aSMark Rutland 	 * security implications, so create snapshots with READ_ONCE() to
100ca791d7fSThierry Reding 	 * ensure that these checks use the same values.
101ca791d7fSThierry Reding 	 */
102*4c1e0a97SThierry Reding 	u32 tx = tegra_ivc_header_read_field(map, tx.count);
103*4c1e0a97SThierry Reding 	u32 rx = tegra_ivc_header_read_field(map, rx.count);
104ca791d7fSThierry Reding 
105ca791d7fSThierry Reding 	/*
106ca791d7fSThierry Reding 	 * Perform an over-full check to prevent denial of service attacks
107ca791d7fSThierry Reding 	 * where a server could be easily fooled into believing that there's
108ca791d7fSThierry Reding 	 * an extremely large number of frames ready, since receivers are not
109ca791d7fSThierry Reding 	 * expected to check for full or over-full conditions.
110ca791d7fSThierry Reding 	 *
111ca791d7fSThierry Reding 	 * Although the channel isn't empty, this is an invalid case caused by
112ca791d7fSThierry Reding 	 * a potentially malicious peer, so returning empty is safer, because
113ca791d7fSThierry Reding 	 * it gives the impression that the channel has gone silent.
114ca791d7fSThierry Reding 	 */
115ca791d7fSThierry Reding 	if (tx - rx > ivc->num_frames)
116ca791d7fSThierry Reding 		return true;
117ca791d7fSThierry Reding 
118ca791d7fSThierry Reding 	return tx == rx;
119ca791d7fSThierry Reding }
120ca791d7fSThierry Reding 
tegra_ivc_full(struct tegra_ivc * ivc,struct iosys_map * map)121*4c1e0a97SThierry Reding static inline bool tegra_ivc_full(struct tegra_ivc *ivc, struct iosys_map *map)
122ca791d7fSThierry Reding {
123*4c1e0a97SThierry Reding 	u32 tx = tegra_ivc_header_read_field(map, tx.count);
124*4c1e0a97SThierry Reding 	u32 rx = tegra_ivc_header_read_field(map, rx.count);
125ca791d7fSThierry Reding 
126ca791d7fSThierry Reding 	/*
127ca791d7fSThierry Reding 	 * Invalid cases where the counters indicate that the queue is over
128ca791d7fSThierry Reding 	 * capacity also appear full.
129ca791d7fSThierry Reding 	 */
130ca791d7fSThierry Reding 	return tx - rx >= ivc->num_frames;
131ca791d7fSThierry Reding }
132ca791d7fSThierry Reding 
tegra_ivc_available(struct tegra_ivc * ivc,struct iosys_map * map)133*4c1e0a97SThierry Reding static inline u32 tegra_ivc_available(struct tegra_ivc *ivc, struct iosys_map *map)
134ca791d7fSThierry Reding {
135*4c1e0a97SThierry Reding 	u32 tx = tegra_ivc_header_read_field(map, tx.count);
136*4c1e0a97SThierry Reding 	u32 rx = tegra_ivc_header_read_field(map, rx.count);
137ca791d7fSThierry Reding 
138ca791d7fSThierry Reding 	/*
139ca791d7fSThierry Reding 	 * This function isn't expected to be used in scenarios where an
140ca791d7fSThierry Reding 	 * over-full situation can lead to denial of service attacks. See the
141ca791d7fSThierry Reding 	 * comment in tegra_ivc_empty() for an explanation about special
142ca791d7fSThierry Reding 	 * over-full considerations.
143ca791d7fSThierry Reding 	 */
144ca791d7fSThierry Reding 	return tx - rx;
145ca791d7fSThierry Reding }
146ca791d7fSThierry Reding 
tegra_ivc_advance_tx(struct tegra_ivc * ivc)147ca791d7fSThierry Reding static inline void tegra_ivc_advance_tx(struct tegra_ivc *ivc)
148ca791d7fSThierry Reding {
149*4c1e0a97SThierry Reding 	unsigned int count = tegra_ivc_header_read_field(&ivc->tx.map, tx.count);
150*4c1e0a97SThierry Reding 
151*4c1e0a97SThierry Reding 	tegra_ivc_header_write_field(&ivc->tx.map, tx.count, count + 1);
152ca791d7fSThierry Reding 
153ca791d7fSThierry Reding 	if (ivc->tx.position == ivc->num_frames - 1)
154ca791d7fSThierry Reding 		ivc->tx.position = 0;
155ca791d7fSThierry Reding 	else
156ca791d7fSThierry Reding 		ivc->tx.position++;
157ca791d7fSThierry Reding }
158ca791d7fSThierry Reding 
tegra_ivc_advance_rx(struct tegra_ivc * ivc)159ca791d7fSThierry Reding static inline void tegra_ivc_advance_rx(struct tegra_ivc *ivc)
160ca791d7fSThierry Reding {
161*4c1e0a97SThierry Reding 	unsigned int count = tegra_ivc_header_read_field(&ivc->rx.map, rx.count);
162*4c1e0a97SThierry Reding 
163*4c1e0a97SThierry Reding 	tegra_ivc_header_write_field(&ivc->rx.map, rx.count, count + 1);
164ca791d7fSThierry Reding 
165ca791d7fSThierry Reding 	if (ivc->rx.position == ivc->num_frames - 1)
166ca791d7fSThierry Reding 		ivc->rx.position = 0;
167ca791d7fSThierry Reding 	else
168ca791d7fSThierry Reding 		ivc->rx.position++;
169ca791d7fSThierry Reding }
170ca791d7fSThierry Reding 
tegra_ivc_check_read(struct tegra_ivc * ivc)171ca791d7fSThierry Reding static inline int tegra_ivc_check_read(struct tegra_ivc *ivc)
172ca791d7fSThierry Reding {
173ca791d7fSThierry Reding 	unsigned int offset = offsetof(struct tegra_ivc_header, tx.count);
174*4c1e0a97SThierry Reding 	unsigned int state;
175ca791d7fSThierry Reding 
176ca791d7fSThierry Reding 	/*
177ca791d7fSThierry Reding 	 * tx.channel->state is set locally, so it is not synchronized with
178ca791d7fSThierry Reding 	 * state from the remote peer. The remote peer cannot reset its
179ca791d7fSThierry Reding 	 * transmit counters until we've acknowledged its synchronization
180ca791d7fSThierry Reding 	 * request, so no additional synchronization is required because an
181ca791d7fSThierry Reding 	 * asynchronous transition of rx.channel->state to
182ca791d7fSThierry Reding 	 * TEGRA_IVC_STATE_ACK is not allowed.
183ca791d7fSThierry Reding 	 */
184*4c1e0a97SThierry Reding 	state = tegra_ivc_header_read_field(&ivc->tx.map, tx.state);
185*4c1e0a97SThierry Reding 	if (state != TEGRA_IVC_STATE_ESTABLISHED)
186ca791d7fSThierry Reding 		return -ECONNRESET;
187ca791d7fSThierry Reding 
188ca791d7fSThierry Reding 	/*
189ca791d7fSThierry Reding 	 * Avoid unnecessary invalidations when performing repeated accesses
190ca791d7fSThierry Reding 	 * to an IVC channel by checking the old queue pointers first.
191ca791d7fSThierry Reding 	 *
192ca791d7fSThierry Reding 	 * Synchronization is only necessary when these pointers indicate
193ca791d7fSThierry Reding 	 * empty or full.
194ca791d7fSThierry Reding 	 */
195*4c1e0a97SThierry Reding 	if (!tegra_ivc_empty(ivc, &ivc->rx.map))
196ca791d7fSThierry Reding 		return 0;
197ca791d7fSThierry Reding 
198ca791d7fSThierry Reding 	tegra_ivc_invalidate(ivc, ivc->rx.phys + offset);
199ca791d7fSThierry Reding 
200*4c1e0a97SThierry Reding 	if (tegra_ivc_empty(ivc, &ivc->rx.map))
201ca791d7fSThierry Reding 		return -ENOSPC;
202ca791d7fSThierry Reding 
203ca791d7fSThierry Reding 	return 0;
204ca791d7fSThierry Reding }
205ca791d7fSThierry Reding 
tegra_ivc_check_write(struct tegra_ivc * ivc)206ca791d7fSThierry Reding static inline int tegra_ivc_check_write(struct tegra_ivc *ivc)
207ca791d7fSThierry Reding {
208ca791d7fSThierry Reding 	unsigned int offset = offsetof(struct tegra_ivc_header, rx.count);
209*4c1e0a97SThierry Reding 	unsigned int state;
210ca791d7fSThierry Reding 
211*4c1e0a97SThierry Reding 	state = tegra_ivc_header_read_field(&ivc->tx.map, tx.state);
212*4c1e0a97SThierry Reding 	if (state != TEGRA_IVC_STATE_ESTABLISHED)
213ca791d7fSThierry Reding 		return -ECONNRESET;
214ca791d7fSThierry Reding 
215*4c1e0a97SThierry Reding 	if (!tegra_ivc_full(ivc, &ivc->tx.map))
216ca791d7fSThierry Reding 		return 0;
217ca791d7fSThierry Reding 
218ca791d7fSThierry Reding 	tegra_ivc_invalidate(ivc, ivc->tx.phys + offset);
219ca791d7fSThierry Reding 
220*4c1e0a97SThierry Reding 	if (tegra_ivc_full(ivc, &ivc->tx.map))
221ca791d7fSThierry Reding 		return -ENOSPC;
222ca791d7fSThierry Reding 
223ca791d7fSThierry Reding 	return 0;
224ca791d7fSThierry Reding }
225ca791d7fSThierry Reding 
tegra_ivc_frame_virt(struct tegra_ivc * ivc,const struct iosys_map * header,unsigned int frame,struct iosys_map * map)226*4c1e0a97SThierry Reding static int tegra_ivc_frame_virt(struct tegra_ivc *ivc, const struct iosys_map *header,
227*4c1e0a97SThierry Reding 				unsigned int frame, struct iosys_map *map)
228ca791d7fSThierry Reding {
229*4c1e0a97SThierry Reding 	size_t offset = sizeof(struct tegra_ivc_header) + ivc->frame_size * frame;
230ca791d7fSThierry Reding 
231*4c1e0a97SThierry Reding 	if (WARN_ON(frame >= ivc->num_frames))
232*4c1e0a97SThierry Reding 		return -EINVAL;
233*4c1e0a97SThierry Reding 
234*4c1e0a97SThierry Reding 	*map = IOSYS_MAP_INIT_OFFSET(header, offset);
235*4c1e0a97SThierry Reding 
236*4c1e0a97SThierry Reding 	return 0;
237ca791d7fSThierry Reding }
238ca791d7fSThierry Reding 
tegra_ivc_frame_phys(struct tegra_ivc * ivc,dma_addr_t phys,unsigned int frame)239ca791d7fSThierry Reding static inline dma_addr_t tegra_ivc_frame_phys(struct tegra_ivc *ivc,
240ca791d7fSThierry Reding 					      dma_addr_t phys,
241ca791d7fSThierry Reding 					      unsigned int frame)
242ca791d7fSThierry Reding {
243ca791d7fSThierry Reding 	unsigned long offset;
244ca791d7fSThierry Reding 
245ca791d7fSThierry Reding 	offset = sizeof(struct tegra_ivc_header) + ivc->frame_size * frame;
246ca791d7fSThierry Reding 
247ca791d7fSThierry Reding 	return phys + offset;
248ca791d7fSThierry Reding }
249ca791d7fSThierry Reding 
tegra_ivc_invalidate_frame(struct tegra_ivc * ivc,dma_addr_t phys,unsigned int frame,unsigned int offset,size_t size)250ca791d7fSThierry Reding static inline void tegra_ivc_invalidate_frame(struct tegra_ivc *ivc,
251ca791d7fSThierry Reding 					      dma_addr_t phys,
252ca791d7fSThierry Reding 					      unsigned int frame,
253ca791d7fSThierry Reding 					      unsigned int offset,
254ca791d7fSThierry Reding 					      size_t size)
255ca791d7fSThierry Reding {
256ca791d7fSThierry Reding 	if (!ivc->peer || WARN_ON(frame >= ivc->num_frames))
257ca791d7fSThierry Reding 		return;
258ca791d7fSThierry Reding 
259ca791d7fSThierry Reding 	phys = tegra_ivc_frame_phys(ivc, phys, frame) + offset;
260ca791d7fSThierry Reding 
261ca791d7fSThierry Reding 	dma_sync_single_for_cpu(ivc->peer, phys, size, DMA_FROM_DEVICE);
262ca791d7fSThierry Reding }
263ca791d7fSThierry Reding 
tegra_ivc_flush_frame(struct tegra_ivc * ivc,dma_addr_t phys,unsigned int frame,unsigned int offset,size_t size)264ca791d7fSThierry Reding static inline void tegra_ivc_flush_frame(struct tegra_ivc *ivc,
265ca791d7fSThierry Reding 					 dma_addr_t phys,
266ca791d7fSThierry Reding 					 unsigned int frame,
267ca791d7fSThierry Reding 					 unsigned int offset,
268ca791d7fSThierry Reding 					 size_t size)
269ca791d7fSThierry Reding {
270ca791d7fSThierry Reding 	if (!ivc->peer || WARN_ON(frame >= ivc->num_frames))
271ca791d7fSThierry Reding 		return;
272ca791d7fSThierry Reding 
273ca791d7fSThierry Reding 	phys = tegra_ivc_frame_phys(ivc, phys, frame) + offset;
274ca791d7fSThierry Reding 
275ca791d7fSThierry Reding 	dma_sync_single_for_device(ivc->peer, phys, size, DMA_TO_DEVICE);
276ca791d7fSThierry Reding }
277ca791d7fSThierry Reding 
278ca791d7fSThierry Reding /* directly peek at the next frame rx'ed */
tegra_ivc_read_get_next_frame(struct tegra_ivc * ivc,struct iosys_map * map)279*4c1e0a97SThierry Reding int tegra_ivc_read_get_next_frame(struct tegra_ivc *ivc, struct iosys_map *map)
280ca791d7fSThierry Reding {
281ca791d7fSThierry Reding 	int err;
282ca791d7fSThierry Reding 
283ca791d7fSThierry Reding 	if (WARN_ON(ivc == NULL))
284*4c1e0a97SThierry Reding 		return -EINVAL;
285ca791d7fSThierry Reding 
286ca791d7fSThierry Reding 	err = tegra_ivc_check_read(ivc);
287ca791d7fSThierry Reding 	if (err < 0)
288*4c1e0a97SThierry Reding 		return err;
289ca791d7fSThierry Reding 
290ca791d7fSThierry Reding 	/*
291ca791d7fSThierry Reding 	 * Order observation of ivc->rx.position potentially indicating new
292ca791d7fSThierry Reding 	 * data before data read.
293ca791d7fSThierry Reding 	 */
294ca791d7fSThierry Reding 	smp_rmb();
295ca791d7fSThierry Reding 
296ca791d7fSThierry Reding 	tegra_ivc_invalidate_frame(ivc, ivc->rx.phys, ivc->rx.position, 0,
297ca791d7fSThierry Reding 				   ivc->frame_size);
298ca791d7fSThierry Reding 
299*4c1e0a97SThierry Reding 	return tegra_ivc_frame_virt(ivc, &ivc->rx.map, ivc->rx.position, map);
300ca791d7fSThierry Reding }
301ca791d7fSThierry Reding EXPORT_SYMBOL(tegra_ivc_read_get_next_frame);
302ca791d7fSThierry Reding 
tegra_ivc_read_advance(struct tegra_ivc * ivc)303ca791d7fSThierry Reding int tegra_ivc_read_advance(struct tegra_ivc *ivc)
304ca791d7fSThierry Reding {
305ca791d7fSThierry Reding 	unsigned int rx = offsetof(struct tegra_ivc_header, rx.count);
306ca791d7fSThierry Reding 	unsigned int tx = offsetof(struct tegra_ivc_header, tx.count);
307ca791d7fSThierry Reding 	int err;
308ca791d7fSThierry Reding 
309ca791d7fSThierry Reding 	/*
310ca791d7fSThierry Reding 	 * No read barriers or synchronization here: the caller is expected to
311ca791d7fSThierry Reding 	 * have already observed the channel non-empty. This check is just to
312ca791d7fSThierry Reding 	 * catch programming errors.
313ca791d7fSThierry Reding 	 */
314ca791d7fSThierry Reding 	err = tegra_ivc_check_read(ivc);
315ca791d7fSThierry Reding 	if (err < 0)
316ca791d7fSThierry Reding 		return err;
317ca791d7fSThierry Reding 
318ca791d7fSThierry Reding 	tegra_ivc_advance_rx(ivc);
319ca791d7fSThierry Reding 
320ca791d7fSThierry Reding 	tegra_ivc_flush(ivc, ivc->rx.phys + rx);
321ca791d7fSThierry Reding 
322ca791d7fSThierry Reding 	/*
323ca791d7fSThierry Reding 	 * Ensure our write to ivc->rx.position occurs before our read from
324ca791d7fSThierry Reding 	 * ivc->tx.position.
325ca791d7fSThierry Reding 	 */
326ca791d7fSThierry Reding 	smp_mb();
327ca791d7fSThierry Reding 
328ca791d7fSThierry Reding 	/*
329ca791d7fSThierry Reding 	 * Notify only upon transition from full to non-full. The available
330ca791d7fSThierry Reding 	 * count can only asynchronously increase, so the worst possible
331ca791d7fSThierry Reding 	 * side-effect will be a spurious notification.
332ca791d7fSThierry Reding 	 */
333ca791d7fSThierry Reding 	tegra_ivc_invalidate(ivc, ivc->rx.phys + tx);
334ca791d7fSThierry Reding 
335*4c1e0a97SThierry Reding 	if (tegra_ivc_available(ivc, &ivc->rx.map) == ivc->num_frames - 1)
336ca791d7fSThierry Reding 		ivc->notify(ivc, ivc->notify_data);
337ca791d7fSThierry Reding 
338ca791d7fSThierry Reding 	return 0;
339ca791d7fSThierry Reding }
340ca791d7fSThierry Reding EXPORT_SYMBOL(tegra_ivc_read_advance);
341ca791d7fSThierry Reding 
342ca791d7fSThierry Reding /* directly poke at the next frame to be tx'ed */
tegra_ivc_write_get_next_frame(struct tegra_ivc * ivc,struct iosys_map * map)343*4c1e0a97SThierry Reding int tegra_ivc_write_get_next_frame(struct tegra_ivc *ivc, struct iosys_map *map)
344ca791d7fSThierry Reding {
345ca791d7fSThierry Reding 	int err;
346ca791d7fSThierry Reding 
347ca791d7fSThierry Reding 	err = tegra_ivc_check_write(ivc);
348ca791d7fSThierry Reding 	if (err < 0)
349*4c1e0a97SThierry Reding 		return err;
350ca791d7fSThierry Reding 
351*4c1e0a97SThierry Reding 	return tegra_ivc_frame_virt(ivc, &ivc->tx.map, ivc->tx.position, map);
352ca791d7fSThierry Reding }
353ca791d7fSThierry Reding EXPORT_SYMBOL(tegra_ivc_write_get_next_frame);
354ca791d7fSThierry Reding 
355ca791d7fSThierry Reding /* advance the tx buffer */
tegra_ivc_write_advance(struct tegra_ivc * ivc)356ca791d7fSThierry Reding int tegra_ivc_write_advance(struct tegra_ivc *ivc)
357ca791d7fSThierry Reding {
358ca791d7fSThierry Reding 	unsigned int tx = offsetof(struct tegra_ivc_header, tx.count);
359ca791d7fSThierry Reding 	unsigned int rx = offsetof(struct tegra_ivc_header, rx.count);
360ca791d7fSThierry Reding 	int err;
361ca791d7fSThierry Reding 
362ca791d7fSThierry Reding 	err = tegra_ivc_check_write(ivc);
363ca791d7fSThierry Reding 	if (err < 0)
364ca791d7fSThierry Reding 		return err;
365ca791d7fSThierry Reding 
366ca791d7fSThierry Reding 	tegra_ivc_flush_frame(ivc, ivc->tx.phys, ivc->tx.position, 0,
367ca791d7fSThierry Reding 			      ivc->frame_size);
368ca791d7fSThierry Reding 
369ca791d7fSThierry Reding 	/*
370ca791d7fSThierry Reding 	 * Order any possible stores to the frame before update of
371ca791d7fSThierry Reding 	 * ivc->tx.position.
372ca791d7fSThierry Reding 	 */
373ca791d7fSThierry Reding 	smp_wmb();
374ca791d7fSThierry Reding 
375ca791d7fSThierry Reding 	tegra_ivc_advance_tx(ivc);
376ca791d7fSThierry Reding 	tegra_ivc_flush(ivc, ivc->tx.phys + tx);
377ca791d7fSThierry Reding 
378ca791d7fSThierry Reding 	/*
379ca791d7fSThierry Reding 	 * Ensure our write to ivc->tx.position occurs before our read from
380ca791d7fSThierry Reding 	 * ivc->rx.position.
381ca791d7fSThierry Reding 	 */
382ca791d7fSThierry Reding 	smp_mb();
383ca791d7fSThierry Reding 
384ca791d7fSThierry Reding 	/*
385ca791d7fSThierry Reding 	 * Notify only upon transition from empty to non-empty. The available
386ca791d7fSThierry Reding 	 * count can only asynchronously decrease, so the worst possible
387ca791d7fSThierry Reding 	 * side-effect will be a spurious notification.
388ca791d7fSThierry Reding 	 */
389ca791d7fSThierry Reding 	tegra_ivc_invalidate(ivc, ivc->tx.phys + rx);
390ca791d7fSThierry Reding 
391*4c1e0a97SThierry Reding 	if (tegra_ivc_available(ivc, &ivc->tx.map) == 1)
392ca791d7fSThierry Reding 		ivc->notify(ivc, ivc->notify_data);
393ca791d7fSThierry Reding 
394ca791d7fSThierry Reding 	return 0;
395ca791d7fSThierry Reding }
396ca791d7fSThierry Reding EXPORT_SYMBOL(tegra_ivc_write_advance);
397ca791d7fSThierry Reding 
tegra_ivc_reset(struct tegra_ivc * ivc)398ca791d7fSThierry Reding void tegra_ivc_reset(struct tegra_ivc *ivc)
399ca791d7fSThierry Reding {
400ca791d7fSThierry Reding 	unsigned int offset = offsetof(struct tegra_ivc_header, tx.count);
401ca791d7fSThierry Reding 
402*4c1e0a97SThierry Reding 	tegra_ivc_header_write_field(&ivc->tx.map, tx.state, TEGRA_IVC_STATE_SYNC);
403ca791d7fSThierry Reding 	tegra_ivc_flush(ivc, ivc->tx.phys + offset);
404ca791d7fSThierry Reding 	ivc->notify(ivc, ivc->notify_data);
405ca791d7fSThierry Reding }
406ca791d7fSThierry Reding EXPORT_SYMBOL(tegra_ivc_reset);
407ca791d7fSThierry Reding 
408ca791d7fSThierry Reding /*
409ca791d7fSThierry Reding  * =======================================================
410ca791d7fSThierry Reding  *  IVC State Transition Table - see tegra_ivc_notified()
411ca791d7fSThierry Reding  * =======================================================
412ca791d7fSThierry Reding  *
413ca791d7fSThierry Reding  *	local	remote	action
414ca791d7fSThierry Reding  *	-----	------	-----------------------------------
415ca791d7fSThierry Reding  *	SYNC	EST	<none>
416ca791d7fSThierry Reding  *	SYNC	ACK	reset counters; move to EST; notify
417ca791d7fSThierry Reding  *	SYNC	SYNC	reset counters; move to ACK; notify
418ca791d7fSThierry Reding  *	ACK	EST	move to EST; notify
419ca791d7fSThierry Reding  *	ACK	ACK	move to EST; notify
420ca791d7fSThierry Reding  *	ACK	SYNC	reset counters; move to ACK; notify
421ca791d7fSThierry Reding  *	EST	EST	<none>
422ca791d7fSThierry Reding  *	EST	ACK	<none>
423ca791d7fSThierry Reding  *	EST	SYNC	reset counters; move to ACK; notify
424ca791d7fSThierry Reding  *
425ca791d7fSThierry Reding  * ===============================================================
426ca791d7fSThierry Reding  */
427ca791d7fSThierry Reding 
tegra_ivc_notified(struct tegra_ivc * ivc)428ca791d7fSThierry Reding int tegra_ivc_notified(struct tegra_ivc *ivc)
429ca791d7fSThierry Reding {
430ca791d7fSThierry Reding 	unsigned int offset = offsetof(struct tegra_ivc_header, tx.count);
431*4c1e0a97SThierry Reding 	enum tegra_ivc_state rx_state, tx_state;
432ca791d7fSThierry Reding 
433ca791d7fSThierry Reding 	/* Copy the receiver's state out of shared memory. */
434ca791d7fSThierry Reding 	tegra_ivc_invalidate(ivc, ivc->rx.phys + offset);
435*4c1e0a97SThierry Reding 	rx_state = tegra_ivc_header_read_field(&ivc->rx.map, tx.state);
436*4c1e0a97SThierry Reding 	tx_state = tegra_ivc_header_read_field(&ivc->tx.map, tx.state);
437ca791d7fSThierry Reding 
438*4c1e0a97SThierry Reding 	if (rx_state == TEGRA_IVC_STATE_SYNC) {
439ca791d7fSThierry Reding 		offset = offsetof(struct tegra_ivc_header, tx.count);
440ca791d7fSThierry Reding 
441ca791d7fSThierry Reding 		/*
442ca791d7fSThierry Reding 		 * Order observation of TEGRA_IVC_STATE_SYNC before stores
443ca791d7fSThierry Reding 		 * clearing tx.channel.
444ca791d7fSThierry Reding 		 */
445ca791d7fSThierry Reding 		smp_rmb();
446ca791d7fSThierry Reding 
447ca791d7fSThierry Reding 		/*
448ca791d7fSThierry Reding 		 * Reset tx.channel counters. The remote end is in the SYNC
449ca791d7fSThierry Reding 		 * state and won't make progress until we change our state,
450ca791d7fSThierry Reding 		 * so the counters are not in use at this time.
451ca791d7fSThierry Reding 		 */
452*4c1e0a97SThierry Reding 		tegra_ivc_header_write_field(&ivc->tx.map, tx.count, 0);
453*4c1e0a97SThierry Reding 		tegra_ivc_header_write_field(&ivc->rx.map, rx.count, 0);
454ca791d7fSThierry Reding 
455ca791d7fSThierry Reding 		ivc->tx.position = 0;
456ca791d7fSThierry Reding 		ivc->rx.position = 0;
457ca791d7fSThierry Reding 
458ca791d7fSThierry Reding 		/*
459ca791d7fSThierry Reding 		 * Ensure that counters appear cleared before new state can be
460ca791d7fSThierry Reding 		 * observed.
461ca791d7fSThierry Reding 		 */
462ca791d7fSThierry Reding 		smp_wmb();
463ca791d7fSThierry Reding 
464ca791d7fSThierry Reding 		/*
465ca791d7fSThierry Reding 		 * Move to ACK state. We have just cleared our counters, so it
466ca791d7fSThierry Reding 		 * is now safe for the remote end to start using these values.
467ca791d7fSThierry Reding 		 */
468*4c1e0a97SThierry Reding 		tegra_ivc_header_write_field(&ivc->tx.map, tx.state, TEGRA_IVC_STATE_ACK);
469ca791d7fSThierry Reding 		tegra_ivc_flush(ivc, ivc->tx.phys + offset);
470ca791d7fSThierry Reding 
471ca791d7fSThierry Reding 		/*
472ca791d7fSThierry Reding 		 * Notify remote end to observe state transition.
473ca791d7fSThierry Reding 		 */
474ca791d7fSThierry Reding 		ivc->notify(ivc, ivc->notify_data);
475ca791d7fSThierry Reding 
476*4c1e0a97SThierry Reding 	} else if (tx_state == TEGRA_IVC_STATE_SYNC &&
477*4c1e0a97SThierry Reding 		   rx_state == TEGRA_IVC_STATE_ACK) {
478ca791d7fSThierry Reding 		offset = offsetof(struct tegra_ivc_header, tx.count);
479ca791d7fSThierry Reding 
480ca791d7fSThierry Reding 		/*
481ca791d7fSThierry Reding 		 * Order observation of ivc_state_sync before stores clearing
482ca791d7fSThierry Reding 		 * tx_channel.
483ca791d7fSThierry Reding 		 */
484ca791d7fSThierry Reding 		smp_rmb();
485ca791d7fSThierry Reding 
486ca791d7fSThierry Reding 		/*
487ca791d7fSThierry Reding 		 * Reset tx.channel counters. The remote end is in the ACK
488ca791d7fSThierry Reding 		 * state and won't make progress until we change our state,
489ca791d7fSThierry Reding 		 * so the counters are not in use at this time.
490ca791d7fSThierry Reding 		 */
491*4c1e0a97SThierry Reding 		tegra_ivc_header_write_field(&ivc->tx.map, tx.count, 0);
492*4c1e0a97SThierry Reding 		tegra_ivc_header_write_field(&ivc->rx.map, rx.count, 0);
493ca791d7fSThierry Reding 
494ca791d7fSThierry Reding 		ivc->tx.position = 0;
495ca791d7fSThierry Reding 		ivc->rx.position = 0;
496ca791d7fSThierry Reding 
497ca791d7fSThierry Reding 		/*
498ca791d7fSThierry Reding 		 * Ensure that counters appear cleared before new state can be
499ca791d7fSThierry Reding 		 * observed.
500ca791d7fSThierry Reding 		 */
501ca791d7fSThierry Reding 		smp_wmb();
502ca791d7fSThierry Reding 
503ca791d7fSThierry Reding 		/*
504ca791d7fSThierry Reding 		 * Move to ESTABLISHED state. We know that the remote end has
505ca791d7fSThierry Reding 		 * already cleared its counters, so it is safe to start
506ca791d7fSThierry Reding 		 * writing/reading on this channel.
507ca791d7fSThierry Reding 		 */
508*4c1e0a97SThierry Reding 		tegra_ivc_header_write_field(&ivc->tx.map, tx.state, TEGRA_IVC_STATE_ESTABLISHED);
509ca791d7fSThierry Reding 		tegra_ivc_flush(ivc, ivc->tx.phys + offset);
510ca791d7fSThierry Reding 
511ca791d7fSThierry Reding 		/*
512ca791d7fSThierry Reding 		 * Notify remote end to observe state transition.
513ca791d7fSThierry Reding 		 */
514ca791d7fSThierry Reding 		ivc->notify(ivc, ivc->notify_data);
515ca791d7fSThierry Reding 
516*4c1e0a97SThierry Reding 	} else if (tx_state == TEGRA_IVC_STATE_ACK) {
517ca791d7fSThierry Reding 		offset = offsetof(struct tegra_ivc_header, tx.count);
518ca791d7fSThierry Reding 
519ca791d7fSThierry Reding 		/*
520ca791d7fSThierry Reding 		 * At this point, we have observed the peer to be in either
521ca791d7fSThierry Reding 		 * the ACK or ESTABLISHED state. Next, order observation of
522ca791d7fSThierry Reding 		 * peer state before storing to tx.channel.
523ca791d7fSThierry Reding 		 */
524ca791d7fSThierry Reding 		smp_rmb();
525ca791d7fSThierry Reding 
526ca791d7fSThierry Reding 		/*
527ca791d7fSThierry Reding 		 * Move to ESTABLISHED state. We know that we have previously
528ca791d7fSThierry Reding 		 * cleared our counters, and we know that the remote end has
529ca791d7fSThierry Reding 		 * cleared its counters, so it is safe to start writing/reading
530ca791d7fSThierry Reding 		 * on this channel.
531ca791d7fSThierry Reding 		 */
532*4c1e0a97SThierry Reding 		tegra_ivc_header_write_field(&ivc->tx.map, tx.state, TEGRA_IVC_STATE_ESTABLISHED);
533ca791d7fSThierry Reding 		tegra_ivc_flush(ivc, ivc->tx.phys + offset);
534ca791d7fSThierry Reding 
535ca791d7fSThierry Reding 		/*
536ca791d7fSThierry Reding 		 * Notify remote end to observe state transition.
537ca791d7fSThierry Reding 		 */
538ca791d7fSThierry Reding 		ivc->notify(ivc, ivc->notify_data);
539ca791d7fSThierry Reding 
540ca791d7fSThierry Reding 	} else {
541ca791d7fSThierry Reding 		/*
542ca791d7fSThierry Reding 		 * There is no need to handle any further action. Either the
543ca791d7fSThierry Reding 		 * channel is already fully established, or we are waiting for
544ca791d7fSThierry Reding 		 * the remote end to catch up with our current state. Refer
545ca791d7fSThierry Reding 		 * to the diagram in "IVC State Transition Table" above.
546ca791d7fSThierry Reding 		 */
547ca791d7fSThierry Reding 	}
548ca791d7fSThierry Reding 
549*4c1e0a97SThierry Reding 	if (tx_state != TEGRA_IVC_STATE_ESTABLISHED)
550ca791d7fSThierry Reding 		return -EAGAIN;
551ca791d7fSThierry Reding 
552ca791d7fSThierry Reding 	return 0;
553ca791d7fSThierry Reding }
554ca791d7fSThierry Reding EXPORT_SYMBOL(tegra_ivc_notified);
555ca791d7fSThierry Reding 
tegra_ivc_align(size_t size)556ca791d7fSThierry Reding size_t tegra_ivc_align(size_t size)
557ca791d7fSThierry Reding {
558ca791d7fSThierry Reding 	return ALIGN(size, TEGRA_IVC_ALIGN);
559ca791d7fSThierry Reding }
560ca791d7fSThierry Reding EXPORT_SYMBOL(tegra_ivc_align);
561ca791d7fSThierry Reding 
tegra_ivc_total_queue_size(unsigned queue_size)562ca791d7fSThierry Reding unsigned tegra_ivc_total_queue_size(unsigned queue_size)
563ca791d7fSThierry Reding {
564ca791d7fSThierry Reding 	if (!IS_ALIGNED(queue_size, TEGRA_IVC_ALIGN)) {
565ca791d7fSThierry Reding 		pr_err("%s: queue_size (%u) must be %u-byte aligned\n",
566ca791d7fSThierry Reding 		       __func__, queue_size, TEGRA_IVC_ALIGN);
567ca791d7fSThierry Reding 		return 0;
568ca791d7fSThierry Reding 	}
569ca791d7fSThierry Reding 
570ca791d7fSThierry Reding 	return queue_size + sizeof(struct tegra_ivc_header);
571ca791d7fSThierry Reding }
572ca791d7fSThierry Reding EXPORT_SYMBOL(tegra_ivc_total_queue_size);
573ca791d7fSThierry Reding 
tegra_ivc_check_params(unsigned long rx,unsigned long tx,unsigned int num_frames,size_t frame_size)574ca791d7fSThierry Reding static int tegra_ivc_check_params(unsigned long rx, unsigned long tx,
575ca791d7fSThierry Reding 				  unsigned int num_frames, size_t frame_size)
576ca791d7fSThierry Reding {
577ca791d7fSThierry Reding 	BUILD_BUG_ON(!IS_ALIGNED(offsetof(struct tegra_ivc_header, tx.count),
578ca791d7fSThierry Reding 				 TEGRA_IVC_ALIGN));
579ca791d7fSThierry Reding 	BUILD_BUG_ON(!IS_ALIGNED(offsetof(struct tegra_ivc_header, rx.count),
580ca791d7fSThierry Reding 				 TEGRA_IVC_ALIGN));
581ca791d7fSThierry Reding 	BUILD_BUG_ON(!IS_ALIGNED(sizeof(struct tegra_ivc_header),
582ca791d7fSThierry Reding 				 TEGRA_IVC_ALIGN));
583ca791d7fSThierry Reding 
584ca791d7fSThierry Reding 	if ((uint64_t)num_frames * (uint64_t)frame_size >= 0x100000000UL) {
585ca791d7fSThierry Reding 		pr_err("num_frames * frame_size overflows\n");
586ca791d7fSThierry Reding 		return -EINVAL;
587ca791d7fSThierry Reding 	}
588ca791d7fSThierry Reding 
589ca791d7fSThierry Reding 	if (!IS_ALIGNED(frame_size, TEGRA_IVC_ALIGN)) {
590ca791d7fSThierry Reding 		pr_err("frame size not adequately aligned: %zu\n", frame_size);
591ca791d7fSThierry Reding 		return -EINVAL;
592ca791d7fSThierry Reding 	}
593ca791d7fSThierry Reding 
594ca791d7fSThierry Reding 	/*
595ca791d7fSThierry Reding 	 * The headers must at least be aligned enough for counters
596ca791d7fSThierry Reding 	 * to be accessed atomically.
597ca791d7fSThierry Reding 	 */
598ca791d7fSThierry Reding 	if (!IS_ALIGNED(rx, TEGRA_IVC_ALIGN)) {
599ca791d7fSThierry Reding 		pr_err("IVC channel start not aligned: %#lx\n", rx);
600ca791d7fSThierry Reding 		return -EINVAL;
601ca791d7fSThierry Reding 	}
602ca791d7fSThierry Reding 
603ca791d7fSThierry Reding 	if (!IS_ALIGNED(tx, TEGRA_IVC_ALIGN)) {
604ca791d7fSThierry Reding 		pr_err("IVC channel start not aligned: %#lx\n", tx);
605ca791d7fSThierry Reding 		return -EINVAL;
606ca791d7fSThierry Reding 	}
607ca791d7fSThierry Reding 
608ca791d7fSThierry Reding 	if (rx < tx) {
609ca791d7fSThierry Reding 		if (rx + frame_size * num_frames > tx) {
610ca791d7fSThierry Reding 			pr_err("queue regions overlap: %#lx + %zx > %#lx\n",
611ca791d7fSThierry Reding 			       rx, frame_size * num_frames, tx);
612ca791d7fSThierry Reding 			return -EINVAL;
613ca791d7fSThierry Reding 		}
614ca791d7fSThierry Reding 	} else {
615ca791d7fSThierry Reding 		if (tx + frame_size * num_frames > rx) {
616ca791d7fSThierry Reding 			pr_err("queue regions overlap: %#lx + %zx > %#lx\n",
617ca791d7fSThierry Reding 			       tx, frame_size * num_frames, rx);
618ca791d7fSThierry Reding 			return -EINVAL;
619ca791d7fSThierry Reding 		}
620ca791d7fSThierry Reding 	}
621ca791d7fSThierry Reding 
622ca791d7fSThierry Reding 	return 0;
623ca791d7fSThierry Reding }
624ca791d7fSThierry Reding 
iosys_map_copy(struct iosys_map * dst,const struct iosys_map * src)625*4c1e0a97SThierry Reding static inline void iosys_map_copy(struct iosys_map *dst, const struct iosys_map *src)
626*4c1e0a97SThierry Reding {
627*4c1e0a97SThierry Reding 	*dst = *src;
628*4c1e0a97SThierry Reding }
629*4c1e0a97SThierry Reding 
iosys_map_get_address(const struct iosys_map * map)630*4c1e0a97SThierry Reding static inline unsigned long iosys_map_get_address(const struct iosys_map *map)
631*4c1e0a97SThierry Reding {
632*4c1e0a97SThierry Reding 	if (map->is_iomem)
633*4c1e0a97SThierry Reding 		return (unsigned long)map->vaddr_iomem;
634*4c1e0a97SThierry Reding 
635*4c1e0a97SThierry Reding 	return (unsigned long)map->vaddr;
636*4c1e0a97SThierry Reding }
637*4c1e0a97SThierry Reding 
iosys_map_get_vaddr(const struct iosys_map * map)638*4c1e0a97SThierry Reding static inline void *iosys_map_get_vaddr(const struct iosys_map *map)
639*4c1e0a97SThierry Reding {
640*4c1e0a97SThierry Reding 	if (WARN_ON(map->is_iomem))
641*4c1e0a97SThierry Reding 		return NULL;
642*4c1e0a97SThierry Reding 
643*4c1e0a97SThierry Reding 	return map->vaddr;
644*4c1e0a97SThierry Reding }
645*4c1e0a97SThierry Reding 
tegra_ivc_init(struct tegra_ivc * ivc,struct device * peer,const struct iosys_map * rx,dma_addr_t rx_phys,const struct iosys_map * tx,dma_addr_t tx_phys,unsigned int num_frames,size_t frame_size,void (* notify)(struct tegra_ivc * ivc,void * data),void * data)646*4c1e0a97SThierry Reding int tegra_ivc_init(struct tegra_ivc *ivc, struct device *peer, const struct iosys_map *rx,
647*4c1e0a97SThierry Reding 		   dma_addr_t rx_phys, const struct iosys_map *tx, dma_addr_t tx_phys,
648ca791d7fSThierry Reding 		   unsigned int num_frames, size_t frame_size,
649ca791d7fSThierry Reding 		   void (*notify)(struct tegra_ivc *ivc, void *data),
650ca791d7fSThierry Reding 		   void *data)
651ca791d7fSThierry Reding {
652ca791d7fSThierry Reding 	size_t queue_size;
653ca791d7fSThierry Reding 	int err;
654ca791d7fSThierry Reding 
655ca791d7fSThierry Reding 	if (WARN_ON(!ivc || !notify))
656ca791d7fSThierry Reding 		return -EINVAL;
657ca791d7fSThierry Reding 
658ca791d7fSThierry Reding 	/*
659ca791d7fSThierry Reding 	 * All sizes that can be returned by communication functions should
660ca791d7fSThierry Reding 	 * fit in an int.
661ca791d7fSThierry Reding 	 */
662ca791d7fSThierry Reding 	if (frame_size > INT_MAX)
663ca791d7fSThierry Reding 		return -E2BIG;
664ca791d7fSThierry Reding 
665*4c1e0a97SThierry Reding 	err = tegra_ivc_check_params(iosys_map_get_address(rx), iosys_map_get_address(tx),
666ca791d7fSThierry Reding 				     num_frames, frame_size);
667ca791d7fSThierry Reding 	if (err < 0)
668ca791d7fSThierry Reding 		return err;
669ca791d7fSThierry Reding 
670ca791d7fSThierry Reding 	queue_size = tegra_ivc_total_queue_size(num_frames * frame_size);
671ca791d7fSThierry Reding 
672ca791d7fSThierry Reding 	if (peer) {
673*4c1e0a97SThierry Reding 		ivc->rx.phys = dma_map_single(peer, iosys_map_get_vaddr(rx), queue_size,
674ca791d7fSThierry Reding 					      DMA_BIDIRECTIONAL);
67554ed8121SChristoph Hellwig 		if (dma_mapping_error(peer, ivc->rx.phys))
676ca791d7fSThierry Reding 			return -ENOMEM;
677ca791d7fSThierry Reding 
678*4c1e0a97SThierry Reding 		ivc->tx.phys = dma_map_single(peer, iosys_map_get_vaddr(tx), queue_size,
679ca791d7fSThierry Reding 					      DMA_BIDIRECTIONAL);
68054ed8121SChristoph Hellwig 		if (dma_mapping_error(peer, ivc->tx.phys)) {
681ca791d7fSThierry Reding 			dma_unmap_single(peer, ivc->rx.phys, queue_size,
682ca791d7fSThierry Reding 					 DMA_BIDIRECTIONAL);
683ca791d7fSThierry Reding 			return -ENOMEM;
684ca791d7fSThierry Reding 		}
685ca791d7fSThierry Reding 	} else {
686ca791d7fSThierry Reding 		ivc->rx.phys = rx_phys;
687ca791d7fSThierry Reding 		ivc->tx.phys = tx_phys;
688ca791d7fSThierry Reding 	}
689ca791d7fSThierry Reding 
690*4c1e0a97SThierry Reding 	iosys_map_copy(&ivc->rx.map, rx);
691*4c1e0a97SThierry Reding 	iosys_map_copy(&ivc->tx.map, tx);
692ca791d7fSThierry Reding 	ivc->peer = peer;
693ca791d7fSThierry Reding 	ivc->notify = notify;
694ca791d7fSThierry Reding 	ivc->notify_data = data;
695ca791d7fSThierry Reding 	ivc->frame_size = frame_size;
696ca791d7fSThierry Reding 	ivc->num_frames = num_frames;
697ca791d7fSThierry Reding 
698ca791d7fSThierry Reding 	/*
699ca791d7fSThierry Reding 	 * These values aren't necessarily correct until the channel has been
700ca791d7fSThierry Reding 	 * reset.
701ca791d7fSThierry Reding 	 */
702ca791d7fSThierry Reding 	ivc->tx.position = 0;
703ca791d7fSThierry Reding 	ivc->rx.position = 0;
704ca791d7fSThierry Reding 
705ca791d7fSThierry Reding 	return 0;
706ca791d7fSThierry Reding }
707ca791d7fSThierry Reding EXPORT_SYMBOL(tegra_ivc_init);
708ca791d7fSThierry Reding 
tegra_ivc_cleanup(struct tegra_ivc * ivc)709ca791d7fSThierry Reding void tegra_ivc_cleanup(struct tegra_ivc *ivc)
710ca791d7fSThierry Reding {
711ca791d7fSThierry Reding 	if (ivc->peer) {
712ca791d7fSThierry Reding 		size_t size = tegra_ivc_total_queue_size(ivc->num_frames *
713ca791d7fSThierry Reding 							 ivc->frame_size);
714ca791d7fSThierry Reding 
715ca791d7fSThierry Reding 		dma_unmap_single(ivc->peer, ivc->rx.phys, size,
716ca791d7fSThierry Reding 				 DMA_BIDIRECTIONAL);
717ca791d7fSThierry Reding 		dma_unmap_single(ivc->peer, ivc->tx.phys, size,
718ca791d7fSThierry Reding 				 DMA_BIDIRECTIONAL);
719ca791d7fSThierry Reding 	}
720ca791d7fSThierry Reding }
721ca791d7fSThierry Reding EXPORT_SYMBOL(tegra_ivc_cleanup);
722