1 /*
2  * Copyright (c) 2020 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <zephyr/logging/log.h>
7 #include <zephyr/sys/p4wq.h>
8 #include <zephyr/kernel.h>
9 #include <zephyr/init.h>
10 #include <zephyr/sys/iterable_sections.h>
11 /* private kernel APIs */
12 #include <ksched.h>
13 #include <wait_q.h>
14 
15 LOG_MODULE_REGISTER(p4wq, CONFIG_LOG_DEFAULT_LEVEL);
16 
17 struct device;
18 
set_prio(struct k_thread * th,struct k_p4wq_work * item)19 static void set_prio(struct k_thread *th, struct k_p4wq_work *item)
20 {
21 	__ASSERT_NO_MSG(!IS_ENABLED(CONFIG_SMP) || !z_is_thread_queued(th));
22 	th->base.prio = item->priority;
23 	th->base.prio_deadline = item->deadline;
24 }
25 
rb_lessthan(struct rbnode * a,struct rbnode * b)26 static bool rb_lessthan(struct rbnode *a, struct rbnode *b)
27 {
28 	struct k_p4wq_work *aw = CONTAINER_OF(a, struct k_p4wq_work, rbnode);
29 	struct k_p4wq_work *bw = CONTAINER_OF(b, struct k_p4wq_work, rbnode);
30 
31 	if (aw->priority != bw->priority) {
32 		return aw->priority > bw->priority;
33 	}
34 
35 	if (aw->deadline != bw->deadline) {
36 		return aw->deadline - bw->deadline > 0;
37 	}
38 
39 	return (uintptr_t)a < (uintptr_t)b;
40 }
41 
thread_set_requeued(struct k_thread * th)42 static void thread_set_requeued(struct k_thread *th)
43 {
44 	th->base.user_options |= K_CALLBACK_STATE;
45 }
46 
thread_clear_requeued(struct k_thread * th)47 static void thread_clear_requeued(struct k_thread *th)
48 {
49 	th->base.user_options &= ~K_CALLBACK_STATE;
50 }
51 
thread_was_requeued(struct k_thread * th)52 static bool thread_was_requeued(struct k_thread *th)
53 {
54 	return !!(th->base.user_options & K_CALLBACK_STATE);
55 }
56 
57 /* Slightly different semantics: rb_lessthan must be perfectly
58  * symmetric (to produce a single tree structure) and will use the
59  * pointer value to break ties where priorities are equal, here we
60  * tolerate equality as meaning "not lessthan"
61  */
item_lessthan(struct k_p4wq_work * a,struct k_p4wq_work * b)62 static inline bool item_lessthan(struct k_p4wq_work *a, struct k_p4wq_work *b)
63 {
64 	if (a->priority > b->priority) {
65 		return true;
66 	} else if ((a->priority == b->priority) &&
67 		   (a->deadline != b->deadline)) {
68 		return a->deadline - b->deadline > 0;
69 	} else {
70 		;
71 	}
72 	return false;
73 }
74 
p4wq_loop(void * p0,void * p1,void * p2)75 static FUNC_NORETURN void p4wq_loop(void *p0, void *p1, void *p2)
76 {
77 	ARG_UNUSED(p1);
78 	ARG_UNUSED(p2);
79 	struct k_p4wq *queue = p0;
80 	k_spinlock_key_t k = k_spin_lock(&queue->lock);
81 
82 	while (true) {
83 		struct rbnode *r = rb_get_max(&queue->queue);
84 
85 		if (r) {
86 			struct k_p4wq_work *w
87 				= CONTAINER_OF(r, struct k_p4wq_work, rbnode);
88 
89 			rb_remove(&queue->queue, r);
90 			w->thread = arch_current_thread();
91 			sys_dlist_append(&queue->active, &w->dlnode);
92 			set_prio(arch_current_thread(), w);
93 			thread_clear_requeued(arch_current_thread());
94 
95 			k_spin_unlock(&queue->lock, k);
96 
97 			w->handler(w);
98 
99 			k = k_spin_lock(&queue->lock);
100 
101 			/* Remove from the active list only if it
102 			 * wasn't resubmitted already
103 			 */
104 			if (!thread_was_requeued(arch_current_thread())) {
105 				sys_dlist_remove(&w->dlnode);
106 				w->thread = NULL;
107 				k_sem_give(&w->done_sem);
108 			}
109 		} else {
110 			z_pend_curr(&queue->lock, k, &queue->waitq, K_FOREVER);
111 			k = k_spin_lock(&queue->lock);
112 		}
113 	}
114 }
115 
116 /* Must be called to regain ownership of the work item */
k_p4wq_wait(struct k_p4wq_work * work,k_timeout_t timeout)117 int k_p4wq_wait(struct k_p4wq_work *work, k_timeout_t timeout)
118 {
119 	if (work->sync) {
120 		return k_sem_take(&work->done_sem, timeout);
121 	}
122 
123 	return k_sem_count_get(&work->done_sem) ? 0 : -EBUSY;
124 }
125 
k_p4wq_init(struct k_p4wq * queue)126 void k_p4wq_init(struct k_p4wq *queue)
127 {
128 	memset(queue, 0, sizeof(*queue));
129 	z_waitq_init(&queue->waitq);
130 	queue->queue.lessthan_fn = rb_lessthan;
131 	sys_dlist_init(&queue->active);
132 }
133 
k_p4wq_add_thread(struct k_p4wq * queue,struct k_thread * thread,k_thread_stack_t * stack,size_t stack_size)134 void k_p4wq_add_thread(struct k_p4wq *queue, struct k_thread *thread,
135 			k_thread_stack_t *stack,
136 			size_t stack_size)
137 {
138 	k_thread_create(thread, stack, stack_size,
139 			p4wq_loop, queue, NULL, NULL,
140 			K_HIGHEST_THREAD_PRIO, 0,
141 			queue->flags & K_P4WQ_DELAYED_START ? K_FOREVER : K_NO_WAIT);
142 }
143 
static_init(void)144 static int static_init(void)
145 {
146 
147 	STRUCT_SECTION_FOREACH(k_p4wq_initparam, pp) {
148 		for (int i = 0; i < pp->num; i++) {
149 			uintptr_t ssz = K_THREAD_STACK_LEN(pp->stack_size);
150 			struct k_p4wq *q = pp->flags & K_P4WQ_QUEUE_PER_THREAD ?
151 				pp->queue + i : pp->queue;
152 
153 			if (!i || (pp->flags & K_P4WQ_QUEUE_PER_THREAD)) {
154 				k_p4wq_init(q);
155 			}
156 
157 			q->flags = pp->flags;
158 
159 			/*
160 			 * If the user wants to specify CPU affinity, we have to
161 			 * delay starting threads until that has been done
162 			 */
163 			if (q->flags & K_P4WQ_USER_CPU_MASK) {
164 				q->flags |= K_P4WQ_DELAYED_START;
165 			}
166 
167 			k_p4wq_add_thread(q, &pp->threads[i],
168 					  &pp->stacks[ssz * i],
169 					  pp->stack_size);
170 
171 #ifdef CONFIG_SCHED_CPU_MASK
172 			if (pp->flags & K_P4WQ_USER_CPU_MASK) {
173 				int ret = k_thread_cpu_mask_clear(&pp->threads[i]);
174 
175 				if (ret < 0) {
176 					LOG_ERR("Couldn't clear CPU mask: %d", ret);
177 				}
178 			}
179 #endif
180 		}
181 	}
182 
183 	return 0;
184 }
185 
k_p4wq_enable_static_thread(struct k_p4wq * queue,struct k_thread * thread,uint32_t cpu_mask)186 void k_p4wq_enable_static_thread(struct k_p4wq *queue, struct k_thread *thread,
187 				 uint32_t cpu_mask)
188 {
189 #ifdef CONFIG_SCHED_CPU_MASK
190 	if (queue->flags & K_P4WQ_USER_CPU_MASK) {
191 		unsigned int i;
192 
193 		while ((i = find_lsb_set(cpu_mask))) {
194 			int ret = k_thread_cpu_mask_enable(thread, i - 1);
195 
196 			if (ret < 0) {
197 				LOG_ERR("Couldn't set CPU mask for %u: %d", i, ret);
198 			}
199 			cpu_mask &= ~BIT(i - 1);
200 		}
201 	}
202 #endif
203 
204 	if (queue->flags & K_P4WQ_DELAYED_START) {
205 		k_thread_start(thread);
206 	}
207 }
208 
209 /* We spawn a bunch of high priority threads, use the "SMP" initlevel
210  * so they can initialize in parallel instead of serially on the main
211  * CPU.
212  */
213 SYS_INIT(static_init, APPLICATION, 99);
214 
k_p4wq_submit(struct k_p4wq * queue,struct k_p4wq_work * item)215 void k_p4wq_submit(struct k_p4wq *queue, struct k_p4wq_work *item)
216 {
217 	k_spinlock_key_t k = k_spin_lock(&queue->lock);
218 
219 	/* Input is a delta time from now (to match
220 	 * k_thread_deadline_set()), but we store and use the absolute
221 	 * cycle count.
222 	 */
223 	item->deadline += k_cycle_get_32();
224 
225 	/* Resubmission from within handler?  Remove from active list */
226 	if (item->thread == arch_current_thread()) {
227 		sys_dlist_remove(&item->dlnode);
228 		thread_set_requeued(arch_current_thread());
229 		item->thread = NULL;
230 	} else {
231 		k_sem_init(&item->done_sem, 0, 1);
232 	}
233 	__ASSERT_NO_MSG(item->thread == NULL);
234 
235 	rb_insert(&queue->queue, &item->rbnode);
236 	item->queue = queue;
237 
238 	/* If there were other items already ahead of it in the queue,
239 	 * then we don't need to revisit active thread state and can
240 	 * return.
241 	 */
242 	if (rb_get_max(&queue->queue) != &item->rbnode) {
243 		goto out;
244 	}
245 
246 	/* Check the list of active (running or preempted) items, if
247 	 * there are at least an "active target" of those that are
248 	 * higher priority than the new item, then no one needs to be
249 	 * preempted and we can return.
250 	 */
251 	struct k_p4wq_work *wi;
252 	uint32_t n_beaten_by = 0, active_target = arch_num_cpus();
253 
254 	SYS_DLIST_FOR_EACH_CONTAINER(&queue->active, wi, dlnode) {
255 		/*
256 		 * item_lessthan(a, b) == true means a has lower priority than b
257 		 * !item_lessthan(a, b) counts all work items with higher or
258 		 * equal priority
259 		 */
260 		if (!item_lessthan(wi, item)) {
261 			n_beaten_by++;
262 		}
263 	}
264 
265 	if (n_beaten_by >= active_target) {
266 		/* Too many already have higher priority, not preempting */
267 		goto out;
268 	}
269 
270 	/* Grab a thread, set its priority and queue it.  If there are
271 	 * no threads available to unpend, this is a soft runtime
272 	 * error: we are breaking our promise about run order.
273 	 * Complain.
274 	 */
275 	struct k_thread *th = z_unpend_first_thread(&queue->waitq);
276 
277 	if (th == NULL) {
278 		LOG_WRN("Out of worker threads, priority guarantee violated");
279 		goto out;
280 	}
281 
282 	set_prio(th, item);
283 	z_ready_thread(th);
284 	z_reschedule(&queue->lock, k);
285 
286 	return;
287 
288 out:
289 	k_spin_unlock(&queue->lock, k);
290 }
291 
k_p4wq_cancel(struct k_p4wq * queue,struct k_p4wq_work * item)292 bool k_p4wq_cancel(struct k_p4wq *queue, struct k_p4wq_work *item)
293 {
294 	k_spinlock_key_t k = k_spin_lock(&queue->lock);
295 	bool ret = rb_contains(&queue->queue, &item->rbnode);
296 
297 	if (ret) {
298 		rb_remove(&queue->queue, &item->rbnode);
299 		k_sem_give(&item->done_sem);
300 	}
301 
302 	k_spin_unlock(&queue->lock, k);
303 	return ret;
304 }
305