xref: /netbsd-src/sys/external/bsd/drm2/dist/include/drm/spsc_queue.h (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1*41ec0267Sriastradh /*	$NetBSD: spsc_queue.h,v 1.2 2021/12/18 23:45:46 riastradh Exp $	*/
24e390cabSriastradh 
34e390cabSriastradh /*
44e390cabSriastradh  * Copyright 2017 Advanced Micro Devices, Inc.
54e390cabSriastradh  *
64e390cabSriastradh  * Permission is hereby granted, free of charge, to any person obtaining a
74e390cabSriastradh  * copy of this software and associated documentation files (the "Software"),
84e390cabSriastradh  * to deal in the Software without restriction, including without limitation
94e390cabSriastradh  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
104e390cabSriastradh  * and/or sell copies of the Software, and to permit persons to whom the
114e390cabSriastradh  * Software is furnished to do so, subject to the following conditions:
124e390cabSriastradh  *
134e390cabSriastradh  * The above copyright notice and this permission notice shall be included in
144e390cabSriastradh  * all copies or substantial portions of the Software.
154e390cabSriastradh  *
164e390cabSriastradh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
174e390cabSriastradh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
184e390cabSriastradh  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
194e390cabSriastradh  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
204e390cabSriastradh  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
214e390cabSriastradh  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
224e390cabSriastradh  * OTHER DEALINGS IN THE SOFTWARE.
234e390cabSriastradh  *
244e390cabSriastradh  */
254e390cabSriastradh 
264e390cabSriastradh #ifndef DRM_SCHEDULER_SPSC_QUEUE_H_
274e390cabSriastradh #define DRM_SCHEDULER_SPSC_QUEUE_H_
284e390cabSriastradh 
294e390cabSriastradh #include <linux/atomic.h>
304e390cabSriastradh #include <linux/preempt.h>
314e390cabSriastradh 
324e390cabSriastradh /** SPSC lockless queue */
334e390cabSriastradh 
344e390cabSriastradh struct spsc_node {
354e390cabSriastradh 
364e390cabSriastradh 	/* Stores spsc_node* */
374e390cabSriastradh 	struct spsc_node *next;
384e390cabSriastradh };
394e390cabSriastradh 
404e390cabSriastradh struct spsc_queue {
414e390cabSriastradh 
424e390cabSriastradh 	 struct spsc_node *head;
434e390cabSriastradh 
444e390cabSriastradh 	/* atomic pointer to struct spsc_node* */
454e390cabSriastradh 	atomic_long_t tail;
464e390cabSriastradh 
474e390cabSriastradh 	atomic_t job_count;
484e390cabSriastradh };
494e390cabSriastradh 
spsc_queue_init(struct spsc_queue * queue)504e390cabSriastradh static inline void spsc_queue_init(struct spsc_queue *queue)
514e390cabSriastradh {
524e390cabSriastradh 	queue->head = NULL;
534e390cabSriastradh 	atomic_long_set(&queue->tail, (long)&queue->head);
544e390cabSriastradh 	atomic_set(&queue->job_count, 0);
554e390cabSriastradh }
564e390cabSriastradh 
spsc_queue_peek(struct spsc_queue * queue)574e390cabSriastradh static inline struct spsc_node *spsc_queue_peek(struct spsc_queue *queue)
584e390cabSriastradh {
594e390cabSriastradh 	return queue->head;
604e390cabSriastradh }
614e390cabSriastradh 
spsc_queue_count(struct spsc_queue * queue)624e390cabSriastradh static inline int spsc_queue_count(struct spsc_queue *queue)
634e390cabSriastradh {
644e390cabSriastradh 	return atomic_read(&queue->job_count);
654e390cabSriastradh }
664e390cabSriastradh 
spsc_queue_push(struct spsc_queue * queue,struct spsc_node * node)674e390cabSriastradh static inline bool spsc_queue_push(struct spsc_queue *queue, struct spsc_node *node)
684e390cabSriastradh {
694e390cabSriastradh 	struct spsc_node **tail;
704e390cabSriastradh 
714e390cabSriastradh 	node->next = NULL;
724e390cabSriastradh 
734e390cabSriastradh 	preempt_disable();
744e390cabSriastradh 
754e390cabSriastradh 	tail = (struct spsc_node **)atomic_long_xchg(&queue->tail, (long)&node->next);
764e390cabSriastradh 	WRITE_ONCE(*tail, node);
774e390cabSriastradh 	atomic_inc(&queue->job_count);
784e390cabSriastradh 
794e390cabSriastradh 	/*
804e390cabSriastradh 	 * In case of first element verify new node will be visible to the consumer
814e390cabSriastradh 	 * thread when we ping the kernel thread that there is new work to do.
824e390cabSriastradh 	 */
834e390cabSriastradh 	smp_wmb();
844e390cabSriastradh 
854e390cabSriastradh 	preempt_enable();
864e390cabSriastradh 
874e390cabSriastradh 	return tail == &queue->head;
884e390cabSriastradh }
894e390cabSriastradh 
904e390cabSriastradh 
spsc_queue_pop(struct spsc_queue * queue)914e390cabSriastradh static inline struct spsc_node *spsc_queue_pop(struct spsc_queue *queue)
924e390cabSriastradh {
934e390cabSriastradh 	struct spsc_node *next, *node;
944e390cabSriastradh 
954e390cabSriastradh 	/* Verify reading from memory and not the cache */
964e390cabSriastradh 	smp_rmb();
974e390cabSriastradh 
984e390cabSriastradh 	node = READ_ONCE(queue->head);
994e390cabSriastradh 
1004e390cabSriastradh 	if (!node)
1014e390cabSriastradh 		return NULL;
1024e390cabSriastradh 
1034e390cabSriastradh 	next = READ_ONCE(node->next);
1044e390cabSriastradh 	WRITE_ONCE(queue->head, next);
1054e390cabSriastradh 
1064e390cabSriastradh 	if (unlikely(!next)) {
1074e390cabSriastradh 		/* slowpath for the last element in the queue */
1084e390cabSriastradh 
1094e390cabSriastradh 		if (atomic_long_cmpxchg(&queue->tail,
1104e390cabSriastradh 				(long)&node->next, (long) &queue->head) != (long)&node->next) {
1114e390cabSriastradh 			/* Updating tail failed wait for new next to appear */
1124e390cabSriastradh 			do {
1134e390cabSriastradh 				smp_rmb();
1144e390cabSriastradh 			} while (unlikely(!(queue->head = READ_ONCE(node->next))));
1154e390cabSriastradh 		}
1164e390cabSriastradh 	}
1174e390cabSriastradh 
1184e390cabSriastradh 	atomic_dec(&queue->job_count);
1194e390cabSriastradh 	return node;
1204e390cabSriastradh }
1214e390cabSriastradh 
1224e390cabSriastradh 
1234e390cabSriastradh 
1244e390cabSriastradh #endif /* DRM_SCHEDULER_SPSC_QUEUE_H_ */
125