1 1.1 riastrad /* $NetBSD: spsc_queue.h,v 1.2 2021/12/18 23:45:46 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright 2017 Advanced Micro Devices, Inc. 5 1.1 riastrad * 6 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 7 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 8 1.1 riastrad * to deal in the Software without restriction, including without limitation 9 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 11 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 12 1.1 riastrad * 13 1.1 riastrad * The above copyright notice and this permission notice shall be included in 14 1.1 riastrad * all copies or substantial portions of the Software. 15 1.1 riastrad * 16 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 1.1 riastrad * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE. 23 1.1 riastrad * 24 1.1 riastrad */ 25 1.1 riastrad 26 1.1 riastrad #ifndef DRM_SCHEDULER_SPSC_QUEUE_H_ 27 1.1 riastrad #define DRM_SCHEDULER_SPSC_QUEUE_H_ 28 1.1 riastrad 29 1.1 riastrad #include <linux/atomic.h> 30 1.1 riastrad #include <linux/preempt.h> 31 1.1 riastrad 32 1.1 riastrad /** SPSC lockless queue */ 33 1.1 riastrad 34 1.1 riastrad struct spsc_node { 35 1.1 riastrad 36 1.1 riastrad /* Stores spsc_node* */ 37 1.1 riastrad struct spsc_node *next; 38 1.1 riastrad }; 39 1.1 riastrad 40 1.1 riastrad struct spsc_queue { 41 1.1 riastrad 42 1.1 riastrad struct spsc_node *head; 43 1.1 riastrad 44 1.1 riastrad /* atomic pointer to struct spsc_node* */ 45 1.1 riastrad atomic_long_t tail; 46 1.1 riastrad 47 1.1 riastrad atomic_t job_count; 48 1.1 riastrad }; 49 1.1 riastrad 50 1.1 riastrad static inline void spsc_queue_init(struct spsc_queue *queue) 51 1.1 riastrad { 52 1.1 riastrad queue->head = NULL; 53 1.1 riastrad atomic_long_set(&queue->tail, (long)&queue->head); 54 1.1 riastrad atomic_set(&queue->job_count, 0); 55 1.1 riastrad } 56 1.1 riastrad 57 1.1 riastrad static inline struct spsc_node *spsc_queue_peek(struct spsc_queue *queue) 58 1.1 riastrad { 59 1.1 riastrad return queue->head; 60 1.1 riastrad } 61 1.1 riastrad 62 1.1 riastrad static inline int spsc_queue_count(struct spsc_queue *queue) 63 1.1 riastrad { 64 1.1 riastrad return atomic_read(&queue->job_count); 65 1.1 riastrad } 66 1.1 riastrad 67 1.1 riastrad static inline bool spsc_queue_push(struct spsc_queue *queue, struct spsc_node *node) 68 1.1 riastrad { 69 1.1 riastrad struct spsc_node **tail; 70 1.1 riastrad 71 1.1 riastrad node->next = NULL; 72 1.1 riastrad 73 1.1 riastrad preempt_disable(); 74 1.1 riastrad 75 1.1 riastrad tail = (struct spsc_node **)atomic_long_xchg(&queue->tail, (long)&node->next); 76 1.1 riastrad WRITE_ONCE(*tail, node); 77 1.1 riastrad atomic_inc(&queue->job_count); 78 1.1 riastrad 79 1.1 riastrad /* 80 1.1 riastrad * In case of first element verify new node will be visible to the consumer 81 1.1 riastrad * thread when we ping the kernel thread that there is new work to do. 82 1.1 riastrad */ 83 1.1 riastrad smp_wmb(); 84 1.1 riastrad 85 1.1 riastrad preempt_enable(); 86 1.1 riastrad 87 1.1 riastrad return tail == &queue->head; 88 1.1 riastrad } 89 1.1 riastrad 90 1.1 riastrad 91 1.1 riastrad static inline struct spsc_node *spsc_queue_pop(struct spsc_queue *queue) 92 1.1 riastrad { 93 1.1 riastrad struct spsc_node *next, *node; 94 1.1 riastrad 95 1.1 riastrad /* Verify reading from memory and not the cache */ 96 1.1 riastrad smp_rmb(); 97 1.1 riastrad 98 1.1 riastrad node = READ_ONCE(queue->head); 99 1.1 riastrad 100 1.1 riastrad if (!node) 101 1.1 riastrad return NULL; 102 1.1 riastrad 103 1.1 riastrad next = READ_ONCE(node->next); 104 1.1 riastrad WRITE_ONCE(queue->head, next); 105 1.1 riastrad 106 1.1 riastrad if (unlikely(!next)) { 107 1.1 riastrad /* slowpath for the last element in the queue */ 108 1.1 riastrad 109 1.1 riastrad if (atomic_long_cmpxchg(&queue->tail, 110 1.1 riastrad (long)&node->next, (long) &queue->head) != (long)&node->next) { 111 1.1 riastrad /* Updating tail failed wait for new next to appear */ 112 1.1 riastrad do { 113 1.1 riastrad smp_rmb(); 114 1.1 riastrad } while (unlikely(!(queue->head = READ_ONCE(node->next)))); 115 1.1 riastrad } 116 1.1 riastrad } 117 1.1 riastrad 118 1.1 riastrad atomic_dec(&queue->job_count); 119 1.1 riastrad return node; 120 1.1 riastrad } 121 1.1 riastrad 122 1.1 riastrad 123 1.1 riastrad 124 1.1 riastrad #endif /* DRM_SCHEDULER_SPSC_QUEUE_H_ */ 125