1*83d290c5STom Rini /* SPDX-License-Identifier: GPL-2.0 */
249626ea8SStephen Warren /*
349626ea8SStephen Warren  * Copyright (c) 2016, NVIDIA CORPORATION.
449626ea8SStephen Warren  */
549626ea8SStephen Warren 
649626ea8SStephen Warren #ifndef _ASM_ARCH_TEGRA_IVC_H
749626ea8SStephen Warren #define _ASM_ARCH_TEGRA_IVC_H
849626ea8SStephen Warren 
949626ea8SStephen Warren #include <common.h>
1049626ea8SStephen Warren 
1149626ea8SStephen Warren /*
1249626ea8SStephen Warren  * Tegra IVC is a communication protocol that transfers fixed-size frames
1349626ea8SStephen Warren  * bi-directionally and in-order between the local CPU and some remote entity.
1449626ea8SStephen Warren  * Communication is via a statically sized and allocated buffer in shared
1549626ea8SStephen Warren  * memory and a notification mechanism.
1649626ea8SStephen Warren  *
1749626ea8SStephen Warren  * This API handles all aspects of the shared memory buffer's metadata, and
1849626ea8SStephen Warren  * leaves all aspects of the frame content to the calling code; frames
1949626ea8SStephen Warren  * typically contain some higher-level protocol. The notification mechanism is
2049626ea8SStephen Warren  * also handled externally to this API, since it can vary from instance to
2149626ea8SStephen Warren  * instance.
2249626ea8SStephen Warren  *
2349626ea8SStephen Warren  * The client model is to first find some free (for TX) or filled (for RX)
2449626ea8SStephen Warren  * frame, process that frame's memory buffer (fill or read it), and then
2549626ea8SStephen Warren  * inform the protocol that the frame has been filled/read, i.e. advance the
2649626ea8SStephen Warren  * write/read pointer. If the channel is full, there may be no available frames
2749626ea8SStephen Warren  * to fill/read. In this case, client code may either poll for an available
2849626ea8SStephen Warren  * frame, or wait for the remote entity to send a notification to the local
2949626ea8SStephen Warren  * CPU.
3049626ea8SStephen Warren  */
3149626ea8SStephen Warren 
3249626ea8SStephen Warren /**
3349626ea8SStephen Warren  * struct tegra_ivc - In-memory shared memory layout.
3449626ea8SStephen Warren  *
3549626ea8SStephen Warren  * This is described in detail in ivc.c.
3649626ea8SStephen Warren  */
3749626ea8SStephen Warren struct tegra_ivc_channel_header;
3849626ea8SStephen Warren 
3949626ea8SStephen Warren /**
4049626ea8SStephen Warren  * struct tegra_ivc - Software state of an IVC channel.
4149626ea8SStephen Warren  *
4249626ea8SStephen Warren  * This state is internal to the IVC code and should not be accessed directly
4349626ea8SStephen Warren  * by clients. It is public solely so clients can allocate storage for the
4449626ea8SStephen Warren  * structure.
4549626ea8SStephen Warren  */
4649626ea8SStephen Warren struct tegra_ivc {
4749626ea8SStephen Warren 	/**
4849626ea8SStephen Warren 	 * rx_channel - Pointer to the shared memory region used to receive
4949626ea8SStephen Warren 	 * messages from the remote entity.
5049626ea8SStephen Warren 	 */
5149626ea8SStephen Warren 	struct tegra_ivc_channel_header *rx_channel;
5249626ea8SStephen Warren 	/**
5349626ea8SStephen Warren 	 * tx_channel - Pointer to the shared memory region used to send
5449626ea8SStephen Warren 	 * messages to the remote entity.
5549626ea8SStephen Warren 	 */
5649626ea8SStephen Warren 	struct tegra_ivc_channel_header *tx_channel;
5749626ea8SStephen Warren 	/**
5849626ea8SStephen Warren 	 * r_pos - The position in list of frames in rx_channel that we are
5949626ea8SStephen Warren 	 * reading from.
6049626ea8SStephen Warren 	 */
6149626ea8SStephen Warren 	uint32_t r_pos;
6249626ea8SStephen Warren 	/**
6349626ea8SStephen Warren 	 * w_pos - The position in list of frames in tx_channel that we are
6449626ea8SStephen Warren 	 * writing to.
6549626ea8SStephen Warren 	 */
6649626ea8SStephen Warren 	uint32_t w_pos;
6749626ea8SStephen Warren 	/**
6849626ea8SStephen Warren 	 * nframes - The number of frames allocated (in each direction) in
6949626ea8SStephen Warren 	 * shared memory.
7049626ea8SStephen Warren 	 */
7149626ea8SStephen Warren 	uint32_t nframes;
7249626ea8SStephen Warren 	/**
7349626ea8SStephen Warren 	 * frame_size - The size of each frame in shared memory.
7449626ea8SStephen Warren 	 */
7549626ea8SStephen Warren 	uint32_t frame_size;
7649626ea8SStephen Warren 	/**
7749626ea8SStephen Warren 	 * notify - Function to call to notify the remote processor of a
7849626ea8SStephen Warren 	 * change in channel state.
7949626ea8SStephen Warren 	 */
8049626ea8SStephen Warren 	void (*notify)(struct tegra_ivc *);
8149626ea8SStephen Warren };
8249626ea8SStephen Warren 
8349626ea8SStephen Warren /**
8449626ea8SStephen Warren  * tegra_ivc_read_get_next_frame - Locate the next frame to receive.
8549626ea8SStephen Warren  *
8649626ea8SStephen Warren  * Locate the next frame to be received/processed, return the address of the
8749626ea8SStephen Warren  * frame, and do not remove it from the queue. Repeated calls to this function
8849626ea8SStephen Warren  * will return the same address until tegra_ivc_read_advance() is called.
8949626ea8SStephen Warren  *
9049626ea8SStephen Warren  * @ivc		The IVC channel.
9149626ea8SStephen Warren  * @frame	Pointer to be filled with the address of the frame to receive.
9249626ea8SStephen Warren  *
9349626ea8SStephen Warren  * @return 0 if a frame is available, else a negative error code.
9449626ea8SStephen Warren  */
9549626ea8SStephen Warren int tegra_ivc_read_get_next_frame(struct tegra_ivc *ivc, void **frame);
9649626ea8SStephen Warren 
9749626ea8SStephen Warren /**
9849626ea8SStephen Warren  * tegra_ivc_read_advance - Advance the read queue.
9949626ea8SStephen Warren  *
10049626ea8SStephen Warren  * Inform the protocol and remote entity that the frame returned by
10149626ea8SStephen Warren  * tegra_ivc_read_get_next_frame() has been processed. The remote end may then
10249626ea8SStephen Warren  * re-use it to transmit further data. Subsequent to this function returning,
10349626ea8SStephen Warren  * tegra_ivc_read_get_next_frame() will return a different frame.
10449626ea8SStephen Warren  *
10549626ea8SStephen Warren  * @ivc		The IVC channel.
10649626ea8SStephen Warren  *
10749626ea8SStephen Warren  * @return 0 if OK, else a negative error code.
10849626ea8SStephen Warren  */
10949626ea8SStephen Warren int tegra_ivc_read_advance(struct tegra_ivc *ivc);
11049626ea8SStephen Warren 
11149626ea8SStephen Warren /**
11249626ea8SStephen Warren  * tegra_ivc_write_get_next_frame - Locate the next frame to fill for transmit.
11349626ea8SStephen Warren  *
11449626ea8SStephen Warren  * Locate the next frame to be filled for transmit, return the address of the
11549626ea8SStephen Warren  * frame, and do not add it to the queue. Repeated calls to this function
11649626ea8SStephen Warren  * will return the same address until tegra_ivc_read_advance() is called.
11749626ea8SStephen Warren  *
11849626ea8SStephen Warren  * @ivc		The IVC channel.
11949626ea8SStephen Warren  * @frame	Pointer to be filled with the address of the frame to fill.
12049626ea8SStephen Warren  *
12149626ea8SStephen Warren  * @return 0 if a frame is available, else a negative error code.
12249626ea8SStephen Warren  */
12349626ea8SStephen Warren int tegra_ivc_write_get_next_frame(struct tegra_ivc *ivc, void **frame);
12449626ea8SStephen Warren 
12549626ea8SStephen Warren /**
12649626ea8SStephen Warren  * tegra_ivc_write_advance - Advance the write queue.
12749626ea8SStephen Warren  *
12849626ea8SStephen Warren  * Inform the protocol and remote entity that the frame returned by
12949626ea8SStephen Warren  * tegra_ivc_write_get_next_frame() has been filled and should be transmitted.
13049626ea8SStephen Warren  * The remote end may then read data from it. Subsequent to this function
13149626ea8SStephen Warren  * returning, tegra_ivc_write_get_next_frame() will return a different frame.
13249626ea8SStephen Warren  *
13349626ea8SStephen Warren  * @ivc		The IVC channel.
13449626ea8SStephen Warren  *
13549626ea8SStephen Warren  * @return 0 if OK, else a negative error code.
13649626ea8SStephen Warren  */
13749626ea8SStephen Warren int tegra_ivc_write_advance(struct tegra_ivc *ivc);
13849626ea8SStephen Warren 
13949626ea8SStephen Warren /**
14049626ea8SStephen Warren  * tegra_ivc_channel_notified - handle internal messages
14149626ea8SStephen Warren  *
14249626ea8SStephen Warren  * This function must be called following every notification.
14349626ea8SStephen Warren  *
14449626ea8SStephen Warren  * @ivc		The IVC channel.
14549626ea8SStephen Warren  *
14649626ea8SStephen Warren  * @return 0 if the channel is ready for communication, or -EAGAIN if a
14749626ea8SStephen Warren  * channel reset is in progress.
14849626ea8SStephen Warren  */
14949626ea8SStephen Warren int tegra_ivc_channel_notified(struct tegra_ivc *ivc);
15049626ea8SStephen Warren 
15149626ea8SStephen Warren /**
15249626ea8SStephen Warren  * tegra_ivc_channel_reset - initiates a reset of the shared memory state
15349626ea8SStephen Warren  *
15449626ea8SStephen Warren  * This function must be called after a channel is initialized but before it
15549626ea8SStephen Warren  * is used for communication. The channel will be ready for use when a
15649626ea8SStephen Warren  * subsequent call to notify the remote of the channel reset indicates the
15749626ea8SStephen Warren  * reset operation is complete.
15849626ea8SStephen Warren  *
15949626ea8SStephen Warren  * @ivc		The IVC channel.
16049626ea8SStephen Warren  */
16149626ea8SStephen Warren void tegra_ivc_channel_reset(struct tegra_ivc *ivc);
16249626ea8SStephen Warren 
16349626ea8SStephen Warren /**
16449626ea8SStephen Warren  * tegra_ivc_init - Initialize a channel's software state.
16549626ea8SStephen Warren  *
16649626ea8SStephen Warren  * @ivc		The IVC channel.
16749626ea8SStephen Warren  * @rx_base	Address of the the RX shared memory buffer.
16849626ea8SStephen Warren  * @tx_base	Address of the the TX shared memory buffer.
16949626ea8SStephen Warren  * @nframes	Number of frames in each shared memory buffer.
17049626ea8SStephen Warren  * @frame_size	Size of each frame.
17149626ea8SStephen Warren  *
17249626ea8SStephen Warren  * @return 0 if OK, else a negative error code.
17349626ea8SStephen Warren  */
17449626ea8SStephen Warren int tegra_ivc_init(struct tegra_ivc *ivc, ulong rx_base, ulong tx_base,
17549626ea8SStephen Warren 		   uint32_t nframes, uint32_t frame_size,
17649626ea8SStephen Warren 		   void (*notify)(struct tegra_ivc *));
17749626ea8SStephen Warren 
17849626ea8SStephen Warren #endif
179