Home | History | Annotate | Line # | Download | only in drm
      1  1.1  riastrad /*	$NetBSD: task_barrier.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 2019 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 #include <linux/semaphore.h>
     26  1.1  riastrad #include <linux/atomic.h>
     27  1.1  riastrad 
     28  1.1  riastrad /*
     29  1.1  riastrad  * Reusable 2 PHASE task barrier (randevouz point) implementation for N tasks.
     30  1.1  riastrad  * Based on the Little book of sempahores - https://greenteapress.com/wp/semaphores/
     31  1.1  riastrad  */
     32  1.1  riastrad 
     33  1.1  riastrad 
     34  1.1  riastrad 
     35  1.1  riastrad #ifndef DRM_TASK_BARRIER_H_
     36  1.1  riastrad #define DRM_TASK_BARRIER_H_
     37  1.1  riastrad 
     38  1.1  riastrad /*
     39  1.1  riastrad  * Represents an instance of a task barrier.
     40  1.1  riastrad  */
     41  1.1  riastrad struct task_barrier {
     42  1.1  riastrad 	unsigned int n;
     43  1.1  riastrad 	atomic_t count;
     44  1.1  riastrad 	struct semaphore enter_turnstile;
     45  1.1  riastrad 	struct semaphore exit_turnstile;
     46  1.1  riastrad };
     47  1.1  riastrad 
     48  1.1  riastrad static inline void task_barrier_signal_turnstile(struct semaphore *turnstile,
     49  1.1  riastrad 						 unsigned int n)
     50  1.1  riastrad {
     51  1.1  riastrad 	int i;
     52  1.1  riastrad 
     53  1.1  riastrad 	for (i = 0 ; i < n; i++)
     54  1.1  riastrad 		up(turnstile);
     55  1.1  riastrad }
     56  1.1  riastrad 
     57  1.1  riastrad static inline void task_barrier_init(struct task_barrier *tb)
     58  1.1  riastrad {
     59  1.1  riastrad 	tb->n = 0;
     60  1.1  riastrad 	atomic_set(&tb->count, 0);
     61  1.1  riastrad 	sema_init(&tb->enter_turnstile, 0);
     62  1.1  riastrad 	sema_init(&tb->exit_turnstile, 0);
     63  1.1  riastrad }
     64  1.1  riastrad 
     65  1.1  riastrad static inline void task_barrier_add_task(struct task_barrier *tb)
     66  1.1  riastrad {
     67  1.1  riastrad 	tb->n++;
     68  1.1  riastrad }
     69  1.1  riastrad 
     70  1.1  riastrad static inline void task_barrier_rem_task(struct task_barrier *tb)
     71  1.1  riastrad {
     72  1.1  riastrad 	tb->n--;
     73  1.1  riastrad }
     74  1.1  riastrad 
     75  1.1  riastrad /*
     76  1.1  riastrad  * Lines up all the threads BEFORE the critical point.
     77  1.1  riastrad  *
     78  1.1  riastrad  * When all thread passed this code the entry barrier is back to locked state.
     79  1.1  riastrad  */
     80  1.1  riastrad static inline void task_barrier_enter(struct task_barrier *tb)
     81  1.1  riastrad {
     82  1.1  riastrad 	if (atomic_inc_return(&tb->count) == tb->n)
     83  1.1  riastrad 		task_barrier_signal_turnstile(&tb->enter_turnstile, tb->n);
     84  1.1  riastrad 
     85  1.1  riastrad 	down(&tb->enter_turnstile);
     86  1.1  riastrad }
     87  1.1  riastrad 
     88  1.1  riastrad /*
     89  1.1  riastrad  * Lines up all the threads AFTER the critical point.
     90  1.1  riastrad  *
     91  1.1  riastrad  * This function is used to avoid any one thread running ahead if the barrier is
     92  1.1  riastrad  *  used repeatedly .
     93  1.1  riastrad  */
     94  1.1  riastrad static inline void task_barrier_exit(struct task_barrier *tb)
     95  1.1  riastrad {
     96  1.1  riastrad 	if (atomic_dec_return(&tb->count) == 0)
     97  1.1  riastrad 		task_barrier_signal_turnstile(&tb->exit_turnstile, tb->n);
     98  1.1  riastrad 
     99  1.1  riastrad 	down(&tb->exit_turnstile);
    100  1.1  riastrad }
    101  1.1  riastrad 
    102  1.1  riastrad /* Convinieince function when nothing to be done in between entry and exit */
    103  1.1  riastrad static inline void task_barrier_full(struct task_barrier *tb)
    104  1.1  riastrad {
    105  1.1  riastrad 	task_barrier_enter(tb);
    106  1.1  riastrad 	task_barrier_exit(tb);
    107  1.1  riastrad }
    108  1.1  riastrad 
    109  1.1  riastrad #endif
    110