diff options
-rw-r--r-- | include/hooks.h | 65 | ||||
-rw-r--r-- | zephyr/app/ec/ec_app_main.c | 5 | ||||
-rw-r--r-- | zephyr/shim/include/zephyr_hooks_shim.h | 32 | ||||
-rw-r--r-- | zephyr/test/ec_app/src/main.c | 33 |
4 files changed, 93 insertions, 42 deletions
diff --git a/include/hooks.h b/include/hooks.h index e629ffbd9b..3a5b05b39c 100644 --- a/include/hooks.h +++ b/include/hooks.h @@ -10,6 +10,7 @@ #include "common.h" +#ifndef CONFIG_ZEPHYR enum hook_priority { /* Generic values across all hooks */ HOOK_PRIO_FIRST = 1, /* Highest priority */ @@ -67,6 +68,70 @@ enum hook_priority { /* After all sensors have been polled */ HOOK_PRIO_TEMP_SENSOR_DONE = HOOK_PRIO_TEMP_SENSOR + 1, }; +#else +/* + * On Zephyr SYS_INIT priorities may only be 0-99. The priority level must + * be a decimal integer literal. See the definition of SYS_INIT() in + * the upstream zephyr/init.h file. + * + * The priorities below match the relative priorities defined above. + * + * TODO(b/226434387): Convert HOOK_INIT calls in shim layer to SYS_INT + */ + /* Generic values across all hooks */ +#define HOOK_PRIO_FIRST 1 /* Highest priority */ +#define HOOK_PRIO_POST_FIRST 2 +#define HOOK_PRIO_PRE_DEFAULT 49 /* Priority just before default */ +#define HOOK_PRIO_DEFAULT 50 /* Default priority */ +#define HOOK_PRIO_POST_DEFAULT 51 /* Priority just after default */ +#define HOOK_PRIO_LAST 99 /* Lowest priority */ + + /* LPC inits before modules which need memory-mapped I/O */ +#define HOOK_PRIO_INIT_LPC 2 + /* + * I2C dependents (battery, sensors, etc), everything but the + * controllers. I2C controller is now initialized in main.c + * TODO(b/138384267): Split this hook up and name the resulting + * ones more semantically. + */ +#define HOOK_PRIO_PRE_I2C 2 +#define HOOK_PRIO_INIT_I2C 3 +#define HOOK_PRIO_POST_I2C 4 + /* Chipset inits before modules which need to know its initial state. */ +#define HOOK_PRIO_INIT_CHIPSET 4 +#define HOOK_PRIO_POST_CHIPSET 5 + /* Lid switch inits before power button */ +#define HOOK_PRIO_INIT_LID 5 +#define HOOK_PRIO_POST_LID 6 + /* Power button inits before chipset and switch */ +#define HOOK_PRIO_INIT_POWER_BUTTON 6 +#define HOOK_PRIO_POST_POWER_BUTTON 7 + /* Init switch states after power button / lid */ +#define HOOK_PRIO_INIT_SWITCH 7 + /* Init fan before PWM */ +#define HOOK_PRIO_INIT_FAN 8 + /* PWM inits before modules which might use it (LEDs) */ +#define HOOK_PRIO_INIT_PWM 9 +#define HOOK_PRIO_POST_PWM 10 + /* Extpower inits before modules which might use it (battery, LEDs) */ +#define HOOK_PRIO_INIT_EXTPOWER 11 +#define HOOK_PRIO_POST_EXTPOWER 12 + /* Init VBOOT hash later, since it depends on deferred functions */ +#define HOOK_PRIO_INIT_VBOOT_HASH 13 + /* Init charge manager before usage in board init */ +#define HOOK_PRIO_INIT_CHARGE_MANAGER 13 +#define HOOK_PRIO_POST_CHARGE_MANAGER 14 + +/* + * For Zephyr, the non-init related hook priorities must still be defined. + */ +enum hook_priority { + /* Specific values to lump temperature-related hooks together */ + HOOK_PRIO_TEMP_SENSOR = 60, + /* After all sensors have been polled */ + HOOK_PRIO_TEMP_SENSOR_DONE = HOOK_PRIO_TEMP_SENSOR + 1, +}; +#endif enum hook_type { /* diff --git a/zephyr/app/ec/ec_app_main.c b/zephyr/app/ec/ec_app_main.c index db4289e442..32ad941f6c 100644 --- a/zephyr/app/ec/ec_app_main.c +++ b/zephyr/app/ec/ec_app_main.c @@ -81,11 +81,6 @@ void ec_app_main(void) */ k_thread_priority_set(&k_sys_work_q.thread, LOWEST_THREAD_PRIORITY); - /* Call init hooks before main tasks start */ - if (IS_ENABLED(CONFIG_PLATFORM_EC_HOOKS)) { - hook_notify(HOOK_INIT); - } - /* * Print the init time. Not completely accurate because it can't take * into account the time before timer_init(), but it'll at least catch diff --git a/zephyr/shim/include/zephyr_hooks_shim.h b/zephyr/shim/include/zephyr_hooks_shim.h index 7b56487076..8d24fd6cb4 100644 --- a/zephyr/shim/include/zephyr_hooks_shim.h +++ b/zephyr/shim/include/zephyr_hooks_shim.h @@ -31,7 +31,7 @@ int hook_call_deferred(const struct deferred_data *data, int us); K_WORK_DELAYABLE_DEFINE(routine##_work_data, \ (void (*)(struct k_work *))routine); \ __maybe_unused const struct deferred_data routine##_data = { \ - .work = &routine##_work_data, \ + .work = &routine##_work_data, \ } /** @@ -52,13 +52,37 @@ struct zephyr_shim_hook_list { const struct zephyr_shim_hook_info *end; }; -/** - * See include/hooks.h for documentation. +#define Z_CONST_HOOK_INIT 1 + +/* + * Convert the DECLARE_HOOK(HOOK_INIT, ...) to an equivalent SYS_INIT(...) + * call in Zephyr. The cros-ec initialization uses a void function, while + * the Zephyr SYS_INIT requires a function with this prototype: + * int module_init(const struct device *dev); + * + * The device instance is not used with SYS_INIT, and we can always return 0. + * Create a wrapper routine for each initialization. */ -#define DECLARE_HOOK(_hooktype, _routine, _priority) \ +#define DEFINE_SYS_INIT(_routine, _priority) \ + static int sys_init_##_routine(const struct device *unused) \ + { \ + _routine(); \ + return 0; \ + } \ + SYS_INIT(sys_init_##_routine, APPLICATION, _priority) + +#define DEFINE_LEGACY_HOOK(_hooktype, _routine, _priority) \ STRUCT_SECTION_ITERABLE_ALTERNATE( \ zephyr_shim_hook_##_hooktype, zephyr_shim_hook_info, \ _cros_hook_##_hooktype##_##_routine) = { \ .routine = _routine, \ .priority = _priority, \ } + +/** + * See include/hooks.h for documentation. + */ +#define DECLARE_HOOK(_hooktype, _routine, _priority) \ + COND_CODE_1(Z_CONST_##_hooktype, \ + (DEFINE_SYS_INIT(_routine, _priority)), \ + (DEFINE_LEGACY_HOOK(_hooktype, _routine, _priority))) diff --git a/zephyr/test/ec_app/src/main.c b/zephyr/test/ec_app/src/main.c index 47aecc7eca..5974b65485 100644 --- a/zephyr/test/ec_app/src/main.c +++ b/zephyr/test/ec_app/src/main.c @@ -70,38 +70,6 @@ static void test_vboot_main(void) #endif } -#ifdef CONFIG_PLATFORM_EC_HOOKS -static int sample_init_hook_count; -/** - * Just a sample hook. - */ -static void sample_init_hook(void) -{ - printk("Running hook.\n"); - sample_init_hook_count++; -} -DECLARE_HOOK(HOOK_INIT, sample_init_hook, HOOK_PRIO_DEFAULT); - -/** - * @brief Test EC App main runs hooks of type HOOK_INIT. - * - * This test installs a hook, runs main and verifies that the hook ran. - * - */ -static void test_hook_notify_init(void) -{ - sample_init_hook_count = 0; - ec_app_main(); - zassert_equal(1, sample_init_hook_count, - "Expected sample_init_hook to run once."); -} -#else -static void test_hook_notify_init(void) -{ - ztest_test_skip(); -} -#endif - static void test_start_ec_tasks(void) { #ifdef CONFIG_SHIMMED_TASKS @@ -120,7 +88,6 @@ void test_main(void) ztest_unit_test(test_setup_espi), ztest_unit_test(test_watchdog_init), ztest_unit_test(test_vboot_main), - ztest_unit_test(test_hook_notify_init), ztest_unit_test(test_start_ec_tasks)); ztest_run_test_suite(ec_app_tests); |