1 /*******************************************************************************
2 * Copyright (C) 2018 Cadence Design Systems, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to use this Software with Cadence processor cores only and
7 * not with any other processors and platforms, subject to
8 * the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included
11 * in all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
18 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 
21 ******************************************************************************/
22 
23 /*******************************************************************************
24  * xf-sched.h
25  *
26  * Non-preemptive earliest-deadline-first scheduler
27  *
28  ******************************************************************************/
29 
30 #ifndef __XF_SCHED_H
31 #define __XF_SCHED_H
32 
33 /*******************************************************************************
34  * Types definitions
35  ******************************************************************************/
36 
37 /* ...scheduler data */
38 typedef rb_tree_t   xf_sched_t;
39 
40 /* ...scheduling item */
41 typedef rb_node_t   xf_task_t;
42 
43 /*******************************************************************************
44  * Helpers
45  ******************************************************************************/
46 
47 /* ...retrieve timestamp from task handle */
xf_task_timestamp(xf_task_t * t)48 static inline u32 xf_task_timestamp(xf_task_t *t)
49 {
50     /* ...wipe out last bit of "color" */
51     return (((rb_node_t *)t)->color & ~1);
52 }
53 
54 /* ...set task decoding timestamp */
xf_task_timestamp_set(xf_task_t * t,u32 ts)55 static inline u32 xf_task_timestamp_set(xf_task_t *t, u32 ts)
56 {
57     /* ...technically, wiping out last bit of timestamp is not needed */
58     return (((rb_node_t *)t)->color = ts);
59 }
60 
61 /* ...compare two timestamps with respect to wrap-around */
xf_timestamp_before(u32 t0,u32 t1)62 static inline int xf_timestamp_before(u32 t0, u32 t1)
63 {
64     /* ...distance between active items is never high */
65     return ((s32)(t0 - t1) < 0);
66 }
67 
68 /* ...current scheduler timestamp */
xf_sched_timestamp(xf_sched_t * sched)69 static inline u32 xf_sched_timestamp(xf_sched_t *sched)
70 {
71     /* ...don't quite care about last bit */
72     return ((rb_tree_t *)sched)->root.color;
73 }
74 
75 /* ...set scheduler timestamp */
xf_sched_timestamp_set(xf_sched_t * sched,u32 ts)76 static inline u32 xf_sched_timestamp_set(xf_sched_t *sched, u32 ts)
77 {
78     /* ...wipe out last bit (black color is 0) */
79     return (((rb_tree_t *)sched)->root.color = ts & ~0x1);
80 }
81 
82 /*******************************************************************************
83  * Entry points
84  ******************************************************************************/
85 
86 /* ...place message into scheduler queue */
87 extern void xf_sched_put(xf_sched_t *sched, xf_task_t *t, u32 ts);
88 
89 /* ...get first item from the scheduler */
90 extern xf_task_t * xf_sched_get(xf_sched_t *sched);
91 
92 /* ...cancel task execution */
93 extern void xf_sched_cancel(xf_sched_t *sched, xf_task_t *t);
94 
95 /* ...initialize scheduler */
96 extern void xf_sched_init(xf_sched_t *sched);
97 
98 #endif  /* __XF_SCHED_H */
99