1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright(c) 2016 Intel Corporation. All rights reserved.
4  *
5  * Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
6  */
7 
8 #ifndef __SOF_SCHEDULE_TASK_H__
9 #define __SOF_SCHEDULE_TASK_H__
10 
11 #include <arch/schedule/task.h>
12 #include <sof/debug/panic.h>
13 #include <sof/list.h>
14 #include <stdbool.h>
15 #include <stdint.h>
16 
17 #ifdef __ZEPHYR__
18 #include <kernel.h>
19 #endif
20 
21 struct comp_dev;
22 struct sof;
23 
24 /** \brief Predefined LL task priorities. */
25 #define SOF_TASK_PRI_HIGH	0	/* priority level 0 - high */
26 #define SOF_TASK_PRI_MED	4	/* priority level 4 - medium */
27 #define SOF_TASK_PRI_LOW	9	/* priority level 9 - low */
28 
29 /** \brief Predefined EDF task deadlines. */
30 #define SOF_TASK_DEADLINE_IDLE		UINT64_MAX
31 #define SOF_TASK_DEADLINE_ALMOST_IDLE	(SOF_TASK_DEADLINE_IDLE - 1)
32 #define SOF_TASK_DEADLINE_NOW		0
33 
34 /** \brief Task counter initial value. */
35 #define SOF_TASK_SKIP_COUNT		0xFFFFu
36 
37 /** \brief Task states. */
38 enum task_state {
39 	SOF_TASK_STATE_INIT = 0,
40 	SOF_TASK_STATE_QUEUED,
41 	SOF_TASK_STATE_PENDING,
42 	SOF_TASK_STATE_RUNNING,
43 	SOF_TASK_STATE_PREEMPTED,
44 	SOF_TASK_STATE_COMPLETED,
45 	SOF_TASK_STATE_FREE,
46 	SOF_TASK_STATE_CANCEL,
47 	SOF_TASK_STATE_RESCHEDULE,
48 };
49 
50 /** \brief Task operations. */
51 struct task_ops {
52 	enum task_state (*run)(void *data);	/**< task's main operation */
53 	void (*complete)(void *data);		/**< executed on completion */
54 	uint64_t (*get_deadline)(void *data);	/**< returns current deadline */
55 };
56 
57 /** \brief Task used by schedulers. */
58 struct task {
59 	uint64_t start;		/**< start time in [ms] since now (LL only) */
60 	const struct sof_uuid_entry *uid; /**< Uuid */
61 	uint16_t type;		/**< type of the task (LL or EDF) */
62 	uint16_t priority;	/**< priority of the task (used by LL) */
63 	uint16_t core;		/**< execution core */
64 	uint16_t flags;		/**< custom flags */
65 	enum task_state state;	/**< current state */
66 	void *data;		/**< custom data passed to all ops */
67 	struct list_item list;	/**< used by schedulers to hold tasks */
68 	void *priv_data;	/**< task private data */
69 	struct task_ops ops;	/**< task operations */
70 #ifdef __ZEPHYR__
71 	struct k_work_delayable z_delayed_work;
72 	uint32_t cycles_sum;
73 	uint32_t cycles_max;
74 	uint32_t cycles_cnt;
75 #endif
76 };
77 
78 /** \brief Task type registered by pipelines. */
79 struct pipeline_task {
80 	struct task task;		/**< parent structure */
81 	bool registrable;		/**< should task be registered on irq */
82 	struct comp_dev *sched_comp;	/**< pipeline scheduling component */
83 };
84 
85 #define pipeline_task_get(t) container_of(t, struct pipeline_task, task)
86 
task_run(struct task * task)87 static inline enum task_state task_run(struct task *task)
88 {
89 	assert(task->ops.run);
90 
91 	return task->ops.run(task->data);
92 }
93 
task_complete(struct task * task)94 static inline void task_complete(struct task *task)
95 {
96 	if (task->ops.complete)
97 		task->ops.complete(task->data);
98 }
99 
task_get_deadline(struct task * task)100 static inline uint64_t task_get_deadline(struct task *task)
101 {
102 	assert(task->ops.get_deadline);
103 
104 	return task->ops.get_deadline(task->data);
105 }
106 
107 enum task_state task_main_primary_core(void *data);
108 
109 enum task_state task_main_secondary_core(void *data);
110 
111 void task_main_init(void);
112 
113 void task_main_free(void);
114 
115 int task_main_start(struct sof *sof);
116 
117 #endif /* __SOF_SCHEDULE_TASK_H__ */
118