summaryrefslogtreecommitdiff
path: root/zephyr/shim/include/zephyr_hooks_shim.h
blob: 154dbd9679c1a69f28a1a7649a00af3acf3e940d (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
/* 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.
 */

#if !defined(__CROS_EC_HOOKS_H) || defined(__CROS_EC_ZEPHYR_HOOKS_SHIM_H)
#error "This file must only be included from hooks.h. Include hooks.h directly."
#endif
#define __CROS_EC_ZEPHYR_HOOKS_SHIM_H

#include <init.h>
#include <kernel.h>
#include <zephyr.h>

#include "common.h"

/**
 * The internal data structure stored for a deferred function.
 */
struct deferred_data {
	void (*routine)(void);
	struct k_delayed_work delayed_work;
};

/**
 * See include/hooks.h for documentation.
 */
int hook_call_deferred(const struct deferred_data *data, int us);

/**
 * Runtime helper to setup deferred data.
 *
 * @param data		The struct deferred_data.
 */
void zephyr_shim_setup_deferred(const struct deferred_data *data);

/**
 * See include/hooks.h for documentation.
 *
 * Typically Zephyr would put const data in the rodata section but that is
 * write-protected with native_posix. So force it into .data when building for
 * ARCH_POSIX and put it in .rodata in all other cases so that it does not take
 * extra RAM space.
 */
#ifdef CONFIG_ARCH_POSIX
#define DEFERRED_DATA_SECTION ".data.hooks"
#else
#define DEFERRED_DATA_SECTION ".rodata.hooks"
#endif

#define DECLARE_DEFERRED(routine) _DECLARE_DEFERRED(routine)
#define _DECLARE_DEFERRED(_routine)                                        \
	__maybe_unused const struct deferred_data _routine##_data          \
		__attribute__((section(DEFERRED_DATA_SECTION))) = {        \
		.routine = _routine,                                       \
	};                                                                 \
	static int _setup_deferred_##_routine(const struct device *unused) \
	{                                                                  \
		ARG_UNUSED(unused);                                        \
		zephyr_shim_setup_deferred(&_routine##_data);              \
		return 0;                                                  \
	}                                                                  \
	SYS_INIT(_setup_deferred_##_routine, APPLICATION, 1)

/**
 * Internal linked-list structure used to store hook lists.
 */
struct zephyr_shim_hook_list {
	void (*routine)(void);
	int priority;
	struct zephyr_shim_hook_list *next;
};

/**
 * Runtime helper for DECLARE_HOOK setup data.
 *
 * @param type		The type of hook.
 * @param routine	The handler for the hook.
 * @param priority	The priority (smaller values are executed first).
 * @param entry		A statically allocated list entry.
 */
void zephyr_shim_setup_hook(enum hook_type type, void (*routine)(void),
			    int priority, struct zephyr_shim_hook_list *entry);

/**
 * See include/hooks.h for documentation.
 */
#define DECLARE_HOOK(hooktype, routine, priority) \
	_DECLARE_HOOK_1(hooktype, routine, priority, __LINE__)
#define _DECLARE_HOOK_1(hooktype, routine, priority, line) \
	_DECLARE_HOOK_2(hooktype, routine, priority, line)
#define _DECLARE_HOOK_2(hooktype, routine, priority, line)                 \
	static int _setup_hook_##line(const struct device *unused)         \
	{                                                                  \
		ARG_UNUSED(unused);                                        \
		static struct zephyr_shim_hook_list lst;                   \
		zephyr_shim_setup_hook(hooktype, routine, priority, &lst); \
		return 0;                                                  \
	}                                                                  \
	SYS_INIT(_setup_hook_##line, APPLICATION, 1)