1 /* $NetBSD: task_barrier.h,v 1.2 2021/12/18 23:45:46 riastradh Exp $ */ 2 3 /* 4 * Copyright 2019 Advanced Micro Devices, Inc. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 * 24 */ 25 #include <linux/semaphore.h> 26 #include <linux/atomic.h> 27 28 /* 29 * Reusable 2 PHASE task barrier (randevouz point) implementation for N tasks. 30 * Based on the Little book of sempahores - https://greenteapress.com/wp/semaphores/ 31 */ 32 33 34 35 #ifndef DRM_TASK_BARRIER_H_ 36 #define DRM_TASK_BARRIER_H_ 37 38 /* 39 * Represents an instance of a task barrier. 40 */ 41 struct task_barrier { 42 unsigned int n; 43 atomic_t count; 44 struct semaphore enter_turnstile; 45 struct semaphore exit_turnstile; 46 }; 47 48 static inline void task_barrier_signal_turnstile(struct semaphore *turnstile, 49 unsigned int n) 50 { 51 int i; 52 53 for (i = 0 ; i < n; i++) 54 up(turnstile); 55 } 56 57 static inline void task_barrier_init(struct task_barrier *tb) 58 { 59 tb->n = 0; 60 atomic_set(&tb->count, 0); 61 sema_init(&tb->enter_turnstile, 0); 62 sema_init(&tb->exit_turnstile, 0); 63 } 64 65 static inline void task_barrier_add_task(struct task_barrier *tb) 66 { 67 tb->n++; 68 } 69 70 static inline void task_barrier_rem_task(struct task_barrier *tb) 71 { 72 tb->n--; 73 } 74 75 /* 76 * Lines up all the threads BEFORE the critical point. 77 * 78 * When all thread passed this code the entry barrier is back to locked state. 79 */ 80 static inline void task_barrier_enter(struct task_barrier *tb) 81 { 82 if (atomic_inc_return(&tb->count) == tb->n) 83 task_barrier_signal_turnstile(&tb->enter_turnstile, tb->n); 84 85 down(&tb->enter_turnstile); 86 } 87 88 /* 89 * Lines up all the threads AFTER the critical point. 90 * 91 * This function is used to avoid any one thread running ahead if the barrier is 92 * used repeatedly . 93 */ 94 static inline void task_barrier_exit(struct task_barrier *tb) 95 { 96 if (atomic_dec_return(&tb->count) == 0) 97 task_barrier_signal_turnstile(&tb->exit_turnstile, tb->n); 98 99 down(&tb->exit_turnstile); 100 } 101 102 /* Convinieince function when nothing to be done in between entry and exit */ 103 static inline void task_barrier_full(struct task_barrier *tb) 104 { 105 task_barrier_enter(tb); 106 task_barrier_exit(tb); 107 } 108 109 #endif 110