summaryrefslogtreecommitdiff
path: root/zephyr/shim/src/tasks.c
blob: f954401a45a03f39ac1cbdc1e7921d934464eda4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <kernel.h>
#include <init.h>
#include <sys/atomic.h>
#include <task.h>

#include "shimmed_tasks.h"

BUILD_ASSERT(CONFIG_NUM_PREEMPT_PRIORITIES >= TASK_ID_COUNT,
	     "Must increase number of available preempt priorities");

/* Ensure all of the manually defined HAS_TASK_ defines are present */
#define CROS_EC_TASK(name, ...) \
	BUILD_ASSERT(HAS_TASK_##name, "Must define HAS_TASK_*");
CROS_EC_TASK_LIST
#undef CROS_EC_TASK

/* Declare all task stacks here */
#define CROS_EC_TASK(name, e, p, size) \
	K_THREAD_STACK_DEFINE(name##_STACK, size);
CROS_EC_TASK_LIST
#undef CROS_EC_TASK

/* Forward declare all task entry point functions */
#define CROS_EC_TASK(name, entry, ...) void entry(void *p);
CROS_EC_TASK_LIST
#undef CROS_EC_TASK

/** Context for each CROS EC task that is run in its own zephyr thread */
struct task_ctx {
	/** Name of thread (for debugging) */
	const char *name;
	/** Zephyr thread structure that hosts EC tasks */
	struct k_thread zephyr_thread;
	/** Zephyr thread id for above thread */
	k_tid_t zephyr_tid;
	/** Address of Zephyr thread's stack */
	k_thread_stack_t *stack;
	/** Usabled size in bytes of above thread stack */
	size_t stack_size;
	/** Task (platform/ec) entry point */
	void (*entry)(void *p);
	/** The parameter that is passed into the task entry point */
	intptr_t parameter;
	/** A wait-able event that is raised when a new task event is posted */
	struct k_poll_signal new_event;
	/** The current platform/ec events set for this task/thread */
	uint32_t event_mask;
};

#define CROS_EC_TASK(_name, _entry, _parameter, _size) \
	{                                              \
		.entry = _entry,                       \
		.parameter = _parameter,               \
		.stack = _name##_STACK,                \
		.stack_size = _size,                   \
		.name = #_name,                        \
	},
static struct task_ctx shimmed_tasks[] = { CROS_EC_TASK_LIST };
#undef CROS_EC_TASK

task_id_t task_get_current(void)
{
	for (size_t i = 0; i < ARRAY_SIZE(shimmed_tasks); ++i) {
		if (shimmed_tasks[i].zephyr_tid == k_current_get()) {
			return i;
		}
	}
	__ASSERT(false, "Task index out of bound");
	return 0;
}

uint32_t task_set_event(task_id_t cros_task_id, uint32_t event, int wait)
{
	struct task_ctx *const ctx = &shimmed_tasks[cros_task_id];

	atomic_or(&ctx->event_mask, event);
	k_poll_signal_raise(&ctx->new_event, 0);

	/* TODO(b/172360521): Remove wait parameter from EC. No one uses it */
	return 0;
}

uint32_t task_wait_event(int timeout_us)
{
	struct task_ctx *const ctx = &shimmed_tasks[task_get_current()];
	const k_timeout_t timeout = (timeout_us == -1) ? K_FOREVER :
							 K_USEC(timeout_us);
	const int64_t tick_deadline =
		k_uptime_ticks() + k_us_to_ticks_near64(timeout_us);

	struct k_poll_event poll_events[1] = {
		K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_SIGNAL,
					 K_POLL_MODE_NOTIFY_ONLY,
					 &ctx->new_event),
	};

	/* Wait for signal, then clear it before reading events */
	const int rv = k_poll(poll_events, ARRAY_SIZE(poll_events), timeout);

	k_poll_signal_reset(&ctx->new_event);
	uint32_t events = atomic_set(&ctx->event_mask, 0);

	if (rv == -EAGAIN) {
		events |= TASK_EVENT_TIMER;
	}

	/* If we didn't get an event, we need to wait again. There is a very
	 * small change of us reading the event_mask one signaled event too
	 * early. In that case, just wait again for the remaining timeout
	 */
	if (events == 0) {
		const int64_t ticks_left = tick_deadline - k_uptime_ticks();

		if (ticks_left > 0) {
			return task_wait_event(
				k_ticks_to_us_near64(ticks_left));
		}

		events |= TASK_EVENT_TIMER;
	}

	return events;
}

uint32_t task_wait_event_mask(uint32_t event_mask, int timeout_us)
{
	struct task_ctx *const ctx = &shimmed_tasks[task_get_current()];
	uint32_t events = 0;
	const int64_t tick_deadline =
		k_uptime_ticks() + k_us_to_ticks_near64(timeout_us);

	/*  Need to return timeout flags if it occurs as well */
	event_mask |= TASK_EVENT_TIMER;

	while (!(event_mask & events)) {
		const int64_t ticks_left = tick_deadline - k_uptime_ticks();

		if (timeout_us != -1 && ticks_left <= 0) {
			events |= TASK_EVENT_TIMER;
			break;
		}

		struct k_poll_event poll_events[1] = {
			K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_SIGNAL,
						 K_POLL_MODE_NOTIFY_ONLY,
						 &ctx->new_event),
		};

		/* Ensure to honor the -1 timeout as FOREVER */
		k_poll(poll_events, ARRAY_SIZE(poll_events),
		       timeout_us == -1 ? K_FOREVER : K_TICKS(ticks_left));
		k_poll_signal_reset(&ctx->new_event);
		events |= atomic_set(&ctx->event_mask, 0);
	}

	/* Replace any events that weren't in the mask */
	if (events & ~event_mask) {
		atomic_or(&ctx->event_mask, events & ~event_mask);
		k_poll_signal_raise(&ctx->new_event, 0);
	}

	return events & event_mask;
}

static void task_entry(void *task_contex, void *unused1, void *unused2)
{
	ARG_UNUSED(unused1);
	ARG_UNUSED(unused2);

	struct task_ctx *const ctx = (struct task_ctx *)task_contex;
	/* Name thread for debugging */
	k_thread_name_set(ctx->zephyr_tid, ctx->name);

	/* Initialize the new_event structure */
	k_poll_signal_init(&ctx->new_event);

	/* Call into task entry point */
	ctx->entry((void *)ctx->parameter);
}

void start_ec_tasks(void)
{
	for (size_t i = 0; i < ARRAY_SIZE(shimmed_tasks); ++i) {
		struct task_ctx *const ctx = &shimmed_tasks[i];

		/*
		 * TODO(b/172361873): Add K_FP_REGS for FPU tasks. See
		 * comment in config.h for CONFIG_TASK_LIST for existing flags
		 * implementation.
		 */
		ctx->zephyr_tid = k_thread_create(
			&ctx->zephyr_thread, ctx->stack, ctx->stack_size,
			task_entry, ctx, NULL, NULL,
			K_PRIO_PREEMPT(TASK_ID_COUNT - i), 0, K_NO_WAIT);
	}
}