162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright 2019 Advanced Micro Devices, Inc.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
562306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
662306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation
762306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
862306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
962306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
1262306a36Sopenharmony_ci * all copies or substantial portions of the Software.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1562306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1662306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1762306a36Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
1862306a36Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1962306a36Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2062306a36Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
2162306a36Sopenharmony_ci *
2262306a36Sopenharmony_ci */
2362306a36Sopenharmony_ci#include <linux/semaphore.h>
2462306a36Sopenharmony_ci#include <linux/atomic.h>
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci/*
2762306a36Sopenharmony_ci * Reusable 2 PHASE task barrier (rendez-vous point) implementation for N tasks.
2862306a36Sopenharmony_ci * Based on the Little book of semaphores - https://greenteapress.com/wp/semaphores/
2962306a36Sopenharmony_ci */
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci#ifndef DRM_TASK_BARRIER_H_
3462306a36Sopenharmony_ci#define DRM_TASK_BARRIER_H_
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci/*
3762306a36Sopenharmony_ci * Represents an instance of a task barrier.
3862306a36Sopenharmony_ci */
3962306a36Sopenharmony_cistruct task_barrier {
4062306a36Sopenharmony_ci	unsigned int n;
4162306a36Sopenharmony_ci	atomic_t count;
4262306a36Sopenharmony_ci	struct semaphore enter_turnstile;
4362306a36Sopenharmony_ci	struct semaphore exit_turnstile;
4462306a36Sopenharmony_ci};
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_cistatic inline void task_barrier_signal_turnstile(struct semaphore *turnstile,
4762306a36Sopenharmony_ci						 unsigned int n)
4862306a36Sopenharmony_ci{
4962306a36Sopenharmony_ci	int i;
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci	for (i = 0 ; i < n; i++)
5262306a36Sopenharmony_ci		up(turnstile);
5362306a36Sopenharmony_ci}
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_cistatic inline void task_barrier_init(struct task_barrier *tb)
5662306a36Sopenharmony_ci{
5762306a36Sopenharmony_ci	tb->n = 0;
5862306a36Sopenharmony_ci	atomic_set(&tb->count, 0);
5962306a36Sopenharmony_ci	sema_init(&tb->enter_turnstile, 0);
6062306a36Sopenharmony_ci	sema_init(&tb->exit_turnstile, 0);
6162306a36Sopenharmony_ci}
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_cistatic inline void task_barrier_add_task(struct task_barrier *tb)
6462306a36Sopenharmony_ci{
6562306a36Sopenharmony_ci	tb->n++;
6662306a36Sopenharmony_ci}
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_cistatic inline void task_barrier_rem_task(struct task_barrier *tb)
6962306a36Sopenharmony_ci{
7062306a36Sopenharmony_ci	tb->n--;
7162306a36Sopenharmony_ci}
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci/*
7462306a36Sopenharmony_ci * Lines up all the threads BEFORE the critical point.
7562306a36Sopenharmony_ci *
7662306a36Sopenharmony_ci * When all thread passed this code the entry barrier is back to locked state.
7762306a36Sopenharmony_ci */
7862306a36Sopenharmony_cistatic inline void task_barrier_enter(struct task_barrier *tb)
7962306a36Sopenharmony_ci{
8062306a36Sopenharmony_ci	if (atomic_inc_return(&tb->count) == tb->n)
8162306a36Sopenharmony_ci		task_barrier_signal_turnstile(&tb->enter_turnstile, tb->n);
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci	down(&tb->enter_turnstile);
8462306a36Sopenharmony_ci}
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci/*
8762306a36Sopenharmony_ci * Lines up all the threads AFTER the critical point.
8862306a36Sopenharmony_ci *
8962306a36Sopenharmony_ci * This function is used to avoid any one thread running ahead if the barrier is
9062306a36Sopenharmony_ci *  used repeatedly .
9162306a36Sopenharmony_ci */
9262306a36Sopenharmony_cistatic inline void task_barrier_exit(struct task_barrier *tb)
9362306a36Sopenharmony_ci{
9462306a36Sopenharmony_ci	if (atomic_dec_return(&tb->count) == 0)
9562306a36Sopenharmony_ci		task_barrier_signal_turnstile(&tb->exit_turnstile, tb->n);
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	down(&tb->exit_turnstile);
9862306a36Sopenharmony_ci}
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci/* Convinieince function when nothing to be done in between entry and exit */
10162306a36Sopenharmony_cistatic inline void task_barrier_full(struct task_barrier *tb)
10262306a36Sopenharmony_ci{
10362306a36Sopenharmony_ci	task_barrier_enter(tb);
10462306a36Sopenharmony_ci	task_barrier_exit(tb);
10562306a36Sopenharmony_ci}
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci#endif
108