18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2019 Advanced Micro Devices, Inc.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
128c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
158c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
168c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
178c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
188c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
198c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
208c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci#include <linux/semaphore.h>
248c2ecf20Sopenharmony_ci#include <linux/atomic.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/*
278c2ecf20Sopenharmony_ci * Reusable 2 PHASE task barrier (randevouz point) implementation for N tasks.
288c2ecf20Sopenharmony_ci * Based on the Little book of sempahores - https://greenteapress.com/wp/semaphores/
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#ifndef DRM_TASK_BARRIER_H_
348c2ecf20Sopenharmony_ci#define DRM_TASK_BARRIER_H_
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/*
378c2ecf20Sopenharmony_ci * Represents an instance of a task barrier.
388c2ecf20Sopenharmony_ci */
398c2ecf20Sopenharmony_cistruct task_barrier {
408c2ecf20Sopenharmony_ci	unsigned int n;
418c2ecf20Sopenharmony_ci	atomic_t count;
428c2ecf20Sopenharmony_ci	struct semaphore enter_turnstile;
438c2ecf20Sopenharmony_ci	struct semaphore exit_turnstile;
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic inline void task_barrier_signal_turnstile(struct semaphore *turnstile,
478c2ecf20Sopenharmony_ci						 unsigned int n)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	int i;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	for (i = 0 ; i < n; i++)
528c2ecf20Sopenharmony_ci		up(turnstile);
538c2ecf20Sopenharmony_ci}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistatic inline void task_barrier_init(struct task_barrier *tb)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	tb->n = 0;
588c2ecf20Sopenharmony_ci	atomic_set(&tb->count, 0);
598c2ecf20Sopenharmony_ci	sema_init(&tb->enter_turnstile, 0);
608c2ecf20Sopenharmony_ci	sema_init(&tb->exit_turnstile, 0);
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic inline void task_barrier_add_task(struct task_barrier *tb)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	tb->n++;
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic inline void task_barrier_rem_task(struct task_barrier *tb)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	tb->n--;
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci/*
748c2ecf20Sopenharmony_ci * Lines up all the threads BEFORE the critical point.
758c2ecf20Sopenharmony_ci *
768c2ecf20Sopenharmony_ci * When all thread passed this code the entry barrier is back to locked state.
778c2ecf20Sopenharmony_ci */
788c2ecf20Sopenharmony_cistatic inline void task_barrier_enter(struct task_barrier *tb)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	if (atomic_inc_return(&tb->count) == tb->n)
818c2ecf20Sopenharmony_ci		task_barrier_signal_turnstile(&tb->enter_turnstile, tb->n);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	down(&tb->enter_turnstile);
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci/*
878c2ecf20Sopenharmony_ci * Lines up all the threads AFTER the critical point.
888c2ecf20Sopenharmony_ci *
898c2ecf20Sopenharmony_ci * This function is used to avoid any one thread running ahead if the barrier is
908c2ecf20Sopenharmony_ci *  used repeatedly .
918c2ecf20Sopenharmony_ci */
928c2ecf20Sopenharmony_cistatic inline void task_barrier_exit(struct task_barrier *tb)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	if (atomic_dec_return(&tb->count) == 0)
958c2ecf20Sopenharmony_ci		task_barrier_signal_turnstile(&tb->exit_turnstile, tb->n);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	down(&tb->exit_turnstile);
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci/* Convinieince function when nothing to be done in between entry and exit */
1018c2ecf20Sopenharmony_cistatic inline void task_barrier_full(struct task_barrier *tb)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	task_barrier_enter(tb);
1048c2ecf20Sopenharmony_ci	task_barrier_exit(tb);
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci#endif
108