18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * The contents of this file are private to DMA engine drivers, and is not
48c2ecf20Sopenharmony_ci * part of the API to be used by DMA engine users.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci#ifndef DMAENGINE_H
78c2ecf20Sopenharmony_ci#define DMAENGINE_H
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/bug.h>
108c2ecf20Sopenharmony_ci#include <linux/dmaengine.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/**
138c2ecf20Sopenharmony_ci * dma_cookie_init - initialize the cookies for a DMA channel
148c2ecf20Sopenharmony_ci * @chan: dma channel to initialize
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_cistatic inline void dma_cookie_init(struct dma_chan *chan)
178c2ecf20Sopenharmony_ci{
188c2ecf20Sopenharmony_ci	chan->cookie = DMA_MIN_COOKIE;
198c2ecf20Sopenharmony_ci	chan->completed_cookie = DMA_MIN_COOKIE;
208c2ecf20Sopenharmony_ci}
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/**
238c2ecf20Sopenharmony_ci * dma_cookie_assign - assign a DMA engine cookie to the descriptor
248c2ecf20Sopenharmony_ci * @tx: descriptor needing cookie
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci * Assign a unique non-zero per-channel cookie to the descriptor.
278c2ecf20Sopenharmony_ci * Note: caller is expected to hold a lock to prevent concurrency.
288c2ecf20Sopenharmony_ci */
298c2ecf20Sopenharmony_cistatic inline dma_cookie_t dma_cookie_assign(struct dma_async_tx_descriptor *tx)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	struct dma_chan *chan = tx->chan;
328c2ecf20Sopenharmony_ci	dma_cookie_t cookie;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	cookie = chan->cookie + 1;
358c2ecf20Sopenharmony_ci	if (cookie < DMA_MIN_COOKIE)
368c2ecf20Sopenharmony_ci		cookie = DMA_MIN_COOKIE;
378c2ecf20Sopenharmony_ci	tx->cookie = chan->cookie = cookie;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	return cookie;
408c2ecf20Sopenharmony_ci}
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/**
438c2ecf20Sopenharmony_ci * dma_cookie_complete - complete a descriptor
448c2ecf20Sopenharmony_ci * @tx: descriptor to complete
458c2ecf20Sopenharmony_ci *
468c2ecf20Sopenharmony_ci * Mark this descriptor complete by updating the channels completed
478c2ecf20Sopenharmony_ci * cookie marker.  Zero the descriptors cookie to prevent accidental
488c2ecf20Sopenharmony_ci * repeated completions.
498c2ecf20Sopenharmony_ci *
508c2ecf20Sopenharmony_ci * Note: caller is expected to hold a lock to prevent concurrency.
518c2ecf20Sopenharmony_ci */
528c2ecf20Sopenharmony_cistatic inline void dma_cookie_complete(struct dma_async_tx_descriptor *tx)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	BUG_ON(tx->cookie < DMA_MIN_COOKIE);
558c2ecf20Sopenharmony_ci	tx->chan->completed_cookie = tx->cookie;
568c2ecf20Sopenharmony_ci	tx->cookie = 0;
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/**
608c2ecf20Sopenharmony_ci * dma_cookie_status - report cookie status
618c2ecf20Sopenharmony_ci * @chan: dma channel
628c2ecf20Sopenharmony_ci * @cookie: cookie we are interested in
638c2ecf20Sopenharmony_ci * @state: dma_tx_state structure to return last/used cookies
648c2ecf20Sopenharmony_ci *
658c2ecf20Sopenharmony_ci * Report the status of the cookie, filling in the state structure if
668c2ecf20Sopenharmony_ci * non-NULL.  No locking is required.
678c2ecf20Sopenharmony_ci */
688c2ecf20Sopenharmony_cistatic inline enum dma_status dma_cookie_status(struct dma_chan *chan,
698c2ecf20Sopenharmony_ci	dma_cookie_t cookie, struct dma_tx_state *state)
708c2ecf20Sopenharmony_ci{
718c2ecf20Sopenharmony_ci	dma_cookie_t used, complete;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	used = chan->cookie;
748c2ecf20Sopenharmony_ci	complete = chan->completed_cookie;
758c2ecf20Sopenharmony_ci	barrier();
768c2ecf20Sopenharmony_ci	if (state) {
778c2ecf20Sopenharmony_ci		state->last = complete;
788c2ecf20Sopenharmony_ci		state->used = used;
798c2ecf20Sopenharmony_ci		state->residue = 0;
808c2ecf20Sopenharmony_ci		state->in_flight_bytes = 0;
818c2ecf20Sopenharmony_ci	}
828c2ecf20Sopenharmony_ci	return dma_async_is_complete(cookie, complete, used);
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic inline void dma_set_residue(struct dma_tx_state *state, u32 residue)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	if (state)
888c2ecf20Sopenharmony_ci		state->residue = residue;
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic inline void dma_set_in_flight_bytes(struct dma_tx_state *state,
928c2ecf20Sopenharmony_ci					   u32 in_flight_bytes)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	if (state)
958c2ecf20Sopenharmony_ci		state->in_flight_bytes = in_flight_bytes;
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistruct dmaengine_desc_callback {
998c2ecf20Sopenharmony_ci	dma_async_tx_callback callback;
1008c2ecf20Sopenharmony_ci	dma_async_tx_callback_result callback_result;
1018c2ecf20Sopenharmony_ci	void *callback_param;
1028c2ecf20Sopenharmony_ci};
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci/**
1058c2ecf20Sopenharmony_ci * dmaengine_desc_get_callback - get the passed in callback function
1068c2ecf20Sopenharmony_ci * @tx: tx descriptor
1078c2ecf20Sopenharmony_ci * @cb: temp struct to hold the callback info
1088c2ecf20Sopenharmony_ci *
1098c2ecf20Sopenharmony_ci * Fill the passed in cb struct with what's available in the passed in
1108c2ecf20Sopenharmony_ci * tx descriptor struct
1118c2ecf20Sopenharmony_ci * No locking is required.
1128c2ecf20Sopenharmony_ci */
1138c2ecf20Sopenharmony_cistatic inline void
1148c2ecf20Sopenharmony_cidmaengine_desc_get_callback(struct dma_async_tx_descriptor *tx,
1158c2ecf20Sopenharmony_ci			    struct dmaengine_desc_callback *cb)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	cb->callback = tx->callback;
1188c2ecf20Sopenharmony_ci	cb->callback_result = tx->callback_result;
1198c2ecf20Sopenharmony_ci	cb->callback_param = tx->callback_param;
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci/**
1238c2ecf20Sopenharmony_ci * dmaengine_desc_callback_invoke - call the callback function in cb struct
1248c2ecf20Sopenharmony_ci * @cb: temp struct that is holding the callback info
1258c2ecf20Sopenharmony_ci * @result: transaction result
1268c2ecf20Sopenharmony_ci *
1278c2ecf20Sopenharmony_ci * Call the callback function provided in the cb struct with the parameter
1288c2ecf20Sopenharmony_ci * in the cb struct.
1298c2ecf20Sopenharmony_ci * Locking is dependent on the driver.
1308c2ecf20Sopenharmony_ci */
1318c2ecf20Sopenharmony_cistatic inline void
1328c2ecf20Sopenharmony_cidmaengine_desc_callback_invoke(struct dmaengine_desc_callback *cb,
1338c2ecf20Sopenharmony_ci			       const struct dmaengine_result *result)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	struct dmaengine_result dummy_result = {
1368c2ecf20Sopenharmony_ci		.result = DMA_TRANS_NOERROR,
1378c2ecf20Sopenharmony_ci		.residue = 0
1388c2ecf20Sopenharmony_ci	};
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	if (cb->callback_result) {
1418c2ecf20Sopenharmony_ci		if (!result)
1428c2ecf20Sopenharmony_ci			result = &dummy_result;
1438c2ecf20Sopenharmony_ci		cb->callback_result(cb->callback_param, result);
1448c2ecf20Sopenharmony_ci	} else if (cb->callback) {
1458c2ecf20Sopenharmony_ci		cb->callback(cb->callback_param);
1468c2ecf20Sopenharmony_ci	}
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci/**
1508c2ecf20Sopenharmony_ci * dmaengine_desc_get_callback_invoke - get the callback in tx descriptor and
1518c2ecf20Sopenharmony_ci * 					then immediately call the callback.
1528c2ecf20Sopenharmony_ci * @tx: dma async tx descriptor
1538c2ecf20Sopenharmony_ci * @result: transaction result
1548c2ecf20Sopenharmony_ci *
1558c2ecf20Sopenharmony_ci * Call dmaengine_desc_get_callback() and dmaengine_desc_callback_invoke()
1568c2ecf20Sopenharmony_ci * in a single function since no work is necessary in between for the driver.
1578c2ecf20Sopenharmony_ci * Locking is dependent on the driver.
1588c2ecf20Sopenharmony_ci */
1598c2ecf20Sopenharmony_cistatic inline void
1608c2ecf20Sopenharmony_cidmaengine_desc_get_callback_invoke(struct dma_async_tx_descriptor *tx,
1618c2ecf20Sopenharmony_ci				   const struct dmaengine_result *result)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	struct dmaengine_desc_callback cb;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	dmaengine_desc_get_callback(tx, &cb);
1668c2ecf20Sopenharmony_ci	dmaengine_desc_callback_invoke(&cb, result);
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci/**
1708c2ecf20Sopenharmony_ci * dmaengine_desc_callback_valid - verify the callback is valid in cb
1718c2ecf20Sopenharmony_ci * @cb: callback info struct
1728c2ecf20Sopenharmony_ci *
1738c2ecf20Sopenharmony_ci * Return a bool that verifies whether callback in cb is valid or not.
1748c2ecf20Sopenharmony_ci * No locking is required.
1758c2ecf20Sopenharmony_ci */
1768c2ecf20Sopenharmony_cistatic inline bool
1778c2ecf20Sopenharmony_cidmaengine_desc_callback_valid(struct dmaengine_desc_callback *cb)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	return cb->callback || cb->callback_result;
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistruct dma_chan *dma_get_slave_channel(struct dma_chan *chan);
1838c2ecf20Sopenharmony_cistruct dma_chan *dma_get_any_slave_channel(struct dma_device *device);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci#ifdef CONFIG_DEBUG_FS
1868c2ecf20Sopenharmony_ci#include <linux/debugfs.h>
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_cistatic inline struct dentry *
1898c2ecf20Sopenharmony_cidmaengine_get_debugfs_root(struct dma_device *dma_dev) {
1908c2ecf20Sopenharmony_ci	return dma_dev->dbg_dev_root;
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci#else
1938c2ecf20Sopenharmony_cistruct dentry;
1948c2ecf20Sopenharmony_cistatic inline struct dentry *
1958c2ecf20Sopenharmony_cidmaengine_get_debugfs_root(struct dma_device *dma_dev)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	return NULL;
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci#endif /* CONFIG_DEBUG_FS */
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci#endif
202