<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p><br>
</p>
<div class="moz-cite-prefix">On 12/11/19 11:04 PM, Ma, Le wrote:<br>
</div>
<blockquote type="cite" cite="mid:MN2PR12MB4285F713BEE4E4BC2E3F3F39F6550@MN2PR12MB4285.namprd12.prod.outlook.com">
<meta name="Generator" content="Microsoft Word 15 (filtered
medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:DengXian;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:"\@DengXian";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
p.MsoPlainText, li.MsoPlainText, div.MsoPlainText
{mso-style-priority:99;
mso-style-link:"Plain Text Char";
margin:0in;
margin-bottom:.0001pt;
font-size:14.0pt;
font-family:"Calibri",sans-serif;}
p.msonormal0, li.msonormal0, div.msonormal0
{mso-style-name:msonormal;
mso-margin-top-alt:auto;
margin-right:0in;
mso-margin-bottom-alt:auto;
margin-left:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
span.PlainTextChar
{mso-style-name:"Plain Text Char";
mso-style-priority:99;
mso-style-link:"Plain Text";
font-family:"Calibri",sans-serif;}
p.msipheadera92e061b, li.msipheadera92e061b, div.msipheadera92e061b
{mso-style-name:msipheadera92e061b;
mso-margin-top-alt:auto;
margin-right:0in;
mso-margin-bottom-alt:auto;
margin-left:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
span.EmailStyle21
{mso-style-type:personal-compose;
font-family:"Arial",sans-serif;
color:#0078D7;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
<div class="WordSection1">
<p class="msipheadera92e061b" style="margin:0in;margin-bottom:.0001pt"><span style="font-size:10.0pt;font-family:"Arial",sans-serif;color:#0078D7">[AMD
Official Use Only - Internal Distribution Only]</span><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoPlainText"><o:p> </o:p></p>
<p class="MsoPlainText"><o:p> </o:p></p>
<p class="MsoPlainText">-----Original Message-----<br>
From: Andrey Grodzovsky <a class="moz-txt-link-rfc2396E" href="mailto:andrey.grodzovsky@amd.com"><andrey.grodzovsky@amd.com></a> <br>
Sent: Thursday, December 12, 2019 4:39 AM<br>
To: <a class="moz-txt-link-abbreviated" href="mailto:dri-devel@lists.freedesktop.org">dri-devel@lists.freedesktop.org</a>;
<a class="moz-txt-link-abbreviated" href="mailto:amd-gfx@lists.freedesktop.org">amd-gfx@lists.freedesktop.org</a><br>
Cc: Deucher, Alexander <a class="moz-txt-link-rfc2396E" href="mailto:Alexander.Deucher@amd.com"><Alexander.Deucher@amd.com></a>; Ma,
Le <a class="moz-txt-link-rfc2396E" href="mailto:Le.Ma@amd.com"><Le.Ma@amd.com></a>; Zhang, Hawking
<a class="moz-txt-link-rfc2396E" href="mailto:Hawking.Zhang@amd.com"><Hawking.Zhang@amd.com></a>; Quan, Evan
<a class="moz-txt-link-rfc2396E" href="mailto:Evan.Quan@amd.com"><Evan.Quan@amd.com></a>; Grodzovsky, Andrey
<a class="moz-txt-link-rfc2396E" href="mailto:Andrey.Grodzovsky@amd.com"><Andrey.Grodzovsky@amd.com></a><br>
Subject: [RESEND PATCH 2/5] drm: Add Reusable task barrier.<o:p></o:p></p>
<p class="MsoPlainText"><o:p> </o:p></p>
<p class="MsoPlainText">It is used to synchronize N threads at a
rendevouz point before execution of critical code that has to
be started by all the threads at approximatly the same time.<o:p></o:p></p>
<p class="MsoPlainText"><o:p> </o:p></p>
<p class="MsoPlainText">Signed-off-by: Andrey Grodzovsky <<a href="mailto:andrey.grodzovsky@amd.com" moz-do-not-send="true"><span style="color:windowtext;text-decoration:none">andrey.grodzovsky@amd.com</span></a>><o:p></o:p></p>
<p class="MsoPlainText">---<o:p></o:p></p>
<p class="MsoPlainText">include/drm/task_barrier.h | 106
+++++++++++++++++++++++++++++++++++++++++++++<o:p></o:p></p>
<p class="MsoPlainText">1 file changed, 106 insertions(+)<o:p></o:p></p>
<p class="MsoPlainText">create mode 100644
include/drm/task_barrier.h<o:p></o:p></p>
<p class="MsoPlainText"><o:p> </o:p></p>
<p class="MsoPlainText">diff --git a/include/drm/task_barrier.h
b/include/drm/task_barrier.h new file mode 100644 index
0000000..81fb0f7<o:p></o:p></p>
<p class="MsoPlainText">--- /dev/null<o:p></o:p></p>
<p class="MsoPlainText">+++ b/include/drm/task_barrier.h<o:p></o:p></p>
<p class="MsoPlainText">@@ -0,0 +1,106 @@<o:p></o:p></p>
<p class="MsoPlainText">+/*<o:p></o:p></p>
<p class="MsoPlainText">+ * Copyright 2019 Advanced Micro
Devices, Inc.<o:p></o:p></p>
<p class="MsoPlainText">+ *<o:p></o:p></p>
<p class="MsoPlainText">+ * Permission is hereby granted, free
of charge, to any person
<o:p></o:p></p>
<p class="MsoPlainText">+obtaining a<o:p></o:p></p>
<p class="MsoPlainText">+ * copy of this software and associated
documentation files (the
<o:p></o:p></p>
<p class="MsoPlainText">+"Software"),<o:p></o:p></p>
<p class="MsoPlainText">+ * to deal in the Software without
restriction, including without
<o:p></o:p></p>
<p class="MsoPlainText">+limitation<o:p></o:p></p>
<p class="MsoPlainText">+ * the rights to use, copy, modify,
merge, publish, distribute,
<o:p></o:p></p>
<p class="MsoPlainText">+sublicense,<o:p></o:p></p>
<p class="MsoPlainText">+ * and/or sell copies of the Software,
and to permit persons to whom
<o:p></o:p></p>
<p class="MsoPlainText">+the<o:p></o:p></p>
<p class="MsoPlainText">+ * Software is furnished to do so,
subject to the following conditions:<o:p></o:p></p>
<p class="MsoPlainText">+ *<o:p></o:p></p>
<p class="MsoPlainText">+ * The above copyright notice and this
permission notice shall be
<o:p></o:p></p>
<p class="MsoPlainText">+included in<o:p></o:p></p>
<p class="MsoPlainText">+ * all copies or substantial portions
of the Software.<o:p></o:p></p>
<p class="MsoPlainText">+ *<o:p></o:p></p>
<p class="MsoPlainText">+ * THE SOFTWARE IS PROVIDED "AS IS",
WITHOUT WARRANTY OF ANY KIND,
<o:p></o:p></p>
<p class="MsoPlainText">+EXPRESS OR<o:p></o:p></p>
<p class="MsoPlainText">+ * IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF
<o:p></o:p></p>
<p class="MsoPlainText">+MERCHANTABILITY,<o:p></o:p></p>
<p class="MsoPlainText">+ * FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT
<o:p></o:p></p>
<p class="MsoPlainText">+SHALL<o:p></o:p></p>
<p class="MsoPlainText">+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S)
BE LIABLE FOR ANY CLAIM,
<o:p></o:p></p>
<p class="MsoPlainText">+DAMAGES OR<o:p></o:p></p>
<p class="MsoPlainText">+ * OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR
<o:p></o:p></p>
<p class="MsoPlainText">+OTHERWISE,<o:p></o:p></p>
<p class="MsoPlainText">+ * ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE
<o:p></o:p></p>
<p class="MsoPlainText">+OR<o:p></o:p></p>
<p class="MsoPlainText">+ * OTHER DEALINGS IN THE SOFTWARE.<o:p></o:p></p>
<p class="MsoPlainText">+ *<o:p></o:p></p>
<p class="MsoPlainText">+ */<o:p></o:p></p>
<p class="MsoPlainText">+#include <linux/semaphore.h><o:p></o:p></p>
<p class="MsoPlainText">+#include <linux/atomic.h><o:p></o:p></p>
<p class="MsoPlainText">+<o:p></o:p></p>
<p class="MsoPlainText">+/*<o:p></o:p></p>
<p class="MsoPlainText">+ * Reusable 2 PHASE task barrier
(randevouz point) implementation for N tasks.<o:p></o:p></p>
<p class="MsoPlainText">+ * Based on the Little book of
sempahores - <o:p></o:p></p>
<p class="MsoPlainText">+https://greenteapress.com/wp/semaphores/<o:p></o:p></p>
<p class="MsoPlainText">+ */<o:p></o:p></p>
<p class="MsoPlainText">+<o:p></o:p></p>
<p class="MsoPlainText">+<o:p></o:p></p>
<p class="MsoPlainText">+<o:p></o:p></p>
<p class="MsoPlainText">+#ifndef DRM_TASK_BARRIER_H_<o:p></o:p></p>
<p class="MsoPlainText">+#define DRM_TASK_BARRIER_H_<o:p></o:p></p>
<p class="MsoPlainText">+<o:p></o:p></p>
<p class="MsoPlainText"><span style="color:black"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span style="color:#203864">[Le]: It
might be better to prefix “drm_” to the functions and
structure below, even this header file name.
</span></p>
</div>
</blockquote>
<p><br>
</p>
<p>I am not sure about this - see the example of spsc_queue we added
for GPU scheduler use. I just followed it as an example of where
to place the structure. There is nothing DRM specific about
spsc_queue or task_barrier, they are generic constructs that we
place in DRM subsystem for common use.</p>
<p> <br>
</p>
<blockquote type="cite" cite="mid:MN2PR12MB4285F713BEE4E4BC2E3F3F39F6550@MN2PR12MB4285.namprd12.prod.outlook.com">
<div class="WordSection1">
<p class="MsoPlainText"><span style="color:#203864"><o:p></o:p></span></p>
<p class="MsoPlainText"><span style="color:black"><o:p> </o:p></span></p>
<p class="MsoPlainText">+/*<o:p></o:p></p>
<p class="MsoPlainText">+ * Represents an instance of a task
barrier.<o:p></o:p></p>
<p class="MsoPlainText">+ */<o:p></o:p></p>
<p class="MsoPlainText">+struct task_barrier {<o:p></o:p></p>
<p class="MsoPlainText">+ unsigned int n;<o:p></o:p></p>
<p class="MsoPlainText"><span style="color:#203864">[Le]: We can
define it as signed type here for more common use.</span></p>
</div>
</blockquote>
<p><br>
</p>
<p>This is a counter of number of tasks/threads to synchronize in
the barrier it cannot go bellow 0</p>
<p>Andrey</p>
<p><br>
</p>
<blockquote type="cite" cite="mid:MN2PR12MB4285F713BEE4E4BC2E3F3F39F6550@MN2PR12MB4285.namprd12.prod.outlook.com">
<div class="WordSection1">
<p class="MsoPlainText"><span style="color:#203864"><o:p></o:p></span></p>
<p class="MsoPlainText">+ atomic_t count;<o:p></o:p></p>
<p class="MsoPlainText">+ struct semaphore
enter_turnstile;<o:p></o:p></p>
<p class="MsoPlainText">+ struct semaphore
exit_turnstile;<o:p></o:p></p>
<p class="MsoPlainText">+};<o:p></o:p></p>
<p class="MsoPlainText">+<o:p></o:p></p>
<p class="MsoPlainText">+static inline void
task_barrier_signal_turnstile(struct semaphore *turnstile,<o:p></o:p></p>
<p class="MsoPlainText">+
unsigned int n)<o:p></o:p></p>
<p class="MsoPlainText">+{<o:p></o:p></p>
<p class="MsoPlainText">+ int i;<o:p></o:p></p>
<p class="MsoPlainText">+<o:p></o:p></p>
<p class="MsoPlainText">+ for (i = 0 ; i < n; i++)<o:p></o:p></p>
<p class="MsoPlainText">+ up(turnstile);<o:p></o:p></p>
<p class="MsoPlainText">+}<o:p></o:p></p>
<p class="MsoPlainText">+<o:p></o:p></p>
<p class="MsoPlainText">+static inline void
task_barrier_init(struct task_barrier *tb) {<o:p></o:p></p>
<p class="MsoPlainText">+ tb->n = 0;<o:p></o:p></p>
<p class="MsoPlainText">+ atomic_set(&tb->count,
0);<o:p></o:p></p>
<p class="MsoPlainText">+
sema_init(&tb->enter_turnstile, 0);<o:p></o:p></p>
<p class="MsoPlainText">+
sema_init(&tb->exit_turnstile, 0);<o:p></o:p></p>
<p class="MsoPlainText">+}<o:p></o:p></p>
<p class="MsoPlainText">+<o:p></o:p></p>
<p class="MsoPlainText">+static inline void
task_barrier_add_task(struct task_barrier *tb) {<o:p></o:p></p>
<p class="MsoPlainText">+ tb->n++;<o:p></o:p></p>
<p class="MsoPlainText">+}<o:p></o:p></p>
<p class="MsoPlainText">+<o:p></o:p></p>
<p class="MsoPlainText">+static inline void
task_barrier_rem_task(struct task_barrier *tb) {<o:p></o:p></p>
<p class="MsoPlainText">+ tb->n--;<o:p></o:p></p>
<p class="MsoPlainText">+}<o:p></o:p></p>
<p class="MsoPlainText">+<o:p></o:p></p>
<p class="MsoPlainText">+/*<o:p></o:p></p>
<p class="MsoPlainText">+ * Lines up all the threads BEFORE the
critical point.<o:p></o:p></p>
<p class="MsoPlainText">+ *<o:p></o:p></p>
<p class="MsoPlainText">+ * When all thread passed this code the
entry barrier is back to locked state.<o:p></o:p></p>
<p class="MsoPlainText">+ */<o:p></o:p></p>
<p class="MsoPlainText">+static inline void
task_barrier_enter(struct task_barrier *tb) {<o:p></o:p></p>
<p class="MsoPlainText">+ if
(atomic_inc_return(&tb->count) == tb->n)<o:p></o:p></p>
<p class="MsoPlainText">+
task_barrier_signal_turnstile(&tb->enter_turnstile,
tb->n);<o:p></o:p></p>
<p class="MsoPlainText">+<o:p></o:p></p>
<p class="MsoPlainText">+
down(&tb->enter_turnstile);<o:p></o:p></p>
<p class="MsoPlainText">+}<o:p></o:p></p>
<p class="MsoPlainText">+<o:p></o:p></p>
<p class="MsoPlainText">+/*<o:p></o:p></p>
<p class="MsoPlainText">+ * Lines up all the threads AFTER the
critical point.<o:p></o:p></p>
<p class="MsoPlainText">+ *<o:p></o:p></p>
<p class="MsoPlainText">+ * This function is used to avoid any
one thread running ahead of the
<o:p></o:p></p>
<p class="MsoPlainText">+reset if<o:p></o:p></p>
<p class="MsoPlainText"><span style="color:#203864">[Le]: No
need to mention “reset” here.<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="color:#203864"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span style="color:#203864">With the
above addressed, Acked-by: Le Ma
<a href="mailto:Le.Ma@amd.com" moz-do-not-send="true"><span style="color:#033160">Le.Ma@amd.com</span></a><o:p></o:p></span></p>
<p class="MsoPlainText"><span style="color:#203864"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span style="color:#203864">Regards,<o:p></o:p></span></p>
<p class="MsoPlainText"><span style="color:#203864">Ma Le<o:p></o:p></span></p>
<p class="MsoPlainText">+ * the barrier is used in a loop
(repeatedly) .<o:p></o:p></p>
<p class="MsoPlainText">+ */<o:p></o:p></p>
<p class="MsoPlainText">+static inline void
task_barrier_exit(struct task_barrier *tb) {<o:p></o:p></p>
<p class="MsoPlainText">+ if
(atomic_dec_return(&tb->count) == 0)<o:p></o:p></p>
<p class="MsoPlainText">+
task_barrier_signal_turnstile(&tb->exit_turnstile,
tb->n);<o:p></o:p></p>
<p class="MsoPlainText">+<o:p></o:p></p>
<p class="MsoPlainText">+
down(&tb->exit_turnstile);<o:p></o:p></p>
<p class="MsoPlainText">+}<o:p></o:p></p>
<p class="MsoPlainText">+<o:p></o:p></p>
<p class="MsoPlainText">+static inline void
task_barrier_full(struct task_barrier *tb) {<o:p></o:p></p>
<p class="MsoPlainText">+ task_barrier_enter(tb);<o:p></o:p></p>
<p class="MsoPlainText">+ task_barrier_exit(tb);<o:p></o:p></p>
<p class="MsoPlainText">+}<o:p></o:p></p>
<p class="MsoPlainText">+<o:p></o:p></p>
<p class="MsoPlainText">+#endif<o:p></o:p></p>
<p class="MsoPlainText">--<o:p></o:p></p>
<p class="MsoPlainText">2.7.4<o:p></o:p></p>
<p class="MsoPlainText"><o:p> </o:p></p>
</div>
</blockquote>
</body>
</html>