1c942fddfSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2b285192aSMauro Carvalho Chehab /*
3b285192aSMauro Carvalho Chehab  *  cx18 buffer queues
4b285192aSMauro Carvalho Chehab  *
5b285192aSMauro Carvalho Chehab  *  Derived from ivtv-queue.h
6b285192aSMauro Carvalho Chehab  *
7b285192aSMauro Carvalho Chehab  *  Copyright (C) 2007  Hans Verkuil <hverkuil@xs4all.nl>
8b285192aSMauro Carvalho Chehab  *  Copyright (C) 2008  Andy Walls <awalls@md.metrocast.net>
9b285192aSMauro Carvalho Chehab  */
10b285192aSMauro Carvalho Chehab 
11b285192aSMauro Carvalho Chehab #define CX18_DMA_UNMAPPED	((u32) -1)
12b285192aSMauro Carvalho Chehab 
13b285192aSMauro Carvalho Chehab /* cx18_buffer utility functions */
14b285192aSMauro Carvalho Chehab 
cx18_buf_sync_for_cpu(struct cx18_stream * s,struct cx18_buffer * buf)15b285192aSMauro Carvalho Chehab static inline void cx18_buf_sync_for_cpu(struct cx18_stream *s,
16b285192aSMauro Carvalho Chehab 	struct cx18_buffer *buf)
17b285192aSMauro Carvalho Chehab {
18*4e541b06SChristophe JAILLET 	dma_sync_single_for_cpu(&s->cx->pci_dev->dev, buf->dma_handle,
19b285192aSMauro Carvalho Chehab 				s->buf_size, s->dma);
20b285192aSMauro Carvalho Chehab }
21b285192aSMauro Carvalho Chehab 
cx18_buf_sync_for_device(struct cx18_stream * s,struct cx18_buffer * buf)22b285192aSMauro Carvalho Chehab static inline void cx18_buf_sync_for_device(struct cx18_stream *s,
23b285192aSMauro Carvalho Chehab 	struct cx18_buffer *buf)
24b285192aSMauro Carvalho Chehab {
25*4e541b06SChristophe JAILLET 	dma_sync_single_for_device(&s->cx->pci_dev->dev, buf->dma_handle,
26b285192aSMauro Carvalho Chehab 				   s->buf_size, s->dma);
27b285192aSMauro Carvalho Chehab }
28b285192aSMauro Carvalho Chehab 
29b285192aSMauro Carvalho Chehab void _cx18_mdl_sync_for_device(struct cx18_stream *s, struct cx18_mdl *mdl);
30b285192aSMauro Carvalho Chehab 
cx18_mdl_sync_for_device(struct cx18_stream * s,struct cx18_mdl * mdl)31b285192aSMauro Carvalho Chehab static inline void cx18_mdl_sync_for_device(struct cx18_stream *s,
32b285192aSMauro Carvalho Chehab 					    struct cx18_mdl *mdl)
33b285192aSMauro Carvalho Chehab {
34b285192aSMauro Carvalho Chehab 	if (list_is_singular(&mdl->buf_list))
35b285192aSMauro Carvalho Chehab 		cx18_buf_sync_for_device(s, list_first_entry(&mdl->buf_list,
36b285192aSMauro Carvalho Chehab 							     struct cx18_buffer,
37b285192aSMauro Carvalho Chehab 							     list));
38b285192aSMauro Carvalho Chehab 	else
39b285192aSMauro Carvalho Chehab 		_cx18_mdl_sync_for_device(s, mdl);
40b285192aSMauro Carvalho Chehab }
41b285192aSMauro Carvalho Chehab 
42b285192aSMauro Carvalho Chehab void cx18_buf_swap(struct cx18_buffer *buf);
43b285192aSMauro Carvalho Chehab void _cx18_mdl_swap(struct cx18_mdl *mdl);
44b285192aSMauro Carvalho Chehab 
cx18_mdl_swap(struct cx18_mdl * mdl)45b285192aSMauro Carvalho Chehab static inline void cx18_mdl_swap(struct cx18_mdl *mdl)
46b285192aSMauro Carvalho Chehab {
47b285192aSMauro Carvalho Chehab 	if (list_is_singular(&mdl->buf_list))
48b285192aSMauro Carvalho Chehab 		cx18_buf_swap(list_first_entry(&mdl->buf_list,
49b285192aSMauro Carvalho Chehab 					       struct cx18_buffer, list));
50b285192aSMauro Carvalho Chehab 	else
51b285192aSMauro Carvalho Chehab 		_cx18_mdl_swap(mdl);
52b285192aSMauro Carvalho Chehab }
53b285192aSMauro Carvalho Chehab 
54b285192aSMauro Carvalho Chehab /* cx18_queue utility functions */
55b285192aSMauro Carvalho Chehab struct cx18_queue *_cx18_enqueue(struct cx18_stream *s, struct cx18_mdl *mdl,
56b285192aSMauro Carvalho Chehab 				 struct cx18_queue *q, int to_front);
57b285192aSMauro Carvalho Chehab 
58b285192aSMauro Carvalho Chehab static inline
cx18_enqueue(struct cx18_stream * s,struct cx18_mdl * mdl,struct cx18_queue * q)59b285192aSMauro Carvalho Chehab struct cx18_queue *cx18_enqueue(struct cx18_stream *s, struct cx18_mdl *mdl,
60b285192aSMauro Carvalho Chehab 				struct cx18_queue *q)
61b285192aSMauro Carvalho Chehab {
62b285192aSMauro Carvalho Chehab 	return _cx18_enqueue(s, mdl, q, 0); /* FIFO */
63b285192aSMauro Carvalho Chehab }
64b285192aSMauro Carvalho Chehab 
65b285192aSMauro Carvalho Chehab static inline
cx18_push(struct cx18_stream * s,struct cx18_mdl * mdl,struct cx18_queue * q)66b285192aSMauro Carvalho Chehab struct cx18_queue *cx18_push(struct cx18_stream *s, struct cx18_mdl *mdl,
67b285192aSMauro Carvalho Chehab 			     struct cx18_queue *q)
68b285192aSMauro Carvalho Chehab {
69b285192aSMauro Carvalho Chehab 	return _cx18_enqueue(s, mdl, q, 1); /* LIFO */
70b285192aSMauro Carvalho Chehab }
71b285192aSMauro Carvalho Chehab 
72b285192aSMauro Carvalho Chehab void cx18_queue_init(struct cx18_queue *q);
73b285192aSMauro Carvalho Chehab struct cx18_mdl *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q);
74b285192aSMauro Carvalho Chehab struct cx18_mdl *cx18_queue_get_mdl(struct cx18_stream *s, u32 id,
75b285192aSMauro Carvalho Chehab 	u32 bytesused);
76b285192aSMauro Carvalho Chehab void cx18_flush_queues(struct cx18_stream *s);
77b285192aSMauro Carvalho Chehab 
78b285192aSMauro Carvalho Chehab /* queue MDL reconfiguration helpers */
79b285192aSMauro Carvalho Chehab void cx18_unload_queues(struct cx18_stream *s);
80b285192aSMauro Carvalho Chehab void cx18_load_queues(struct cx18_stream *s);
81b285192aSMauro Carvalho Chehab 
82b285192aSMauro Carvalho Chehab /* cx18_stream utility functions */
83b285192aSMauro Carvalho Chehab int cx18_stream_alloc(struct cx18_stream *s);
84b285192aSMauro Carvalho Chehab void cx18_stream_free(struct cx18_stream *s);
85