device: Refactor device structures

When the device driver model got introduced, there were no concept of
SYS_INIT() which can be seen as software service. These were introduced
afterwards and reusing the device infrastructure for simplicity.
However, it meant to allocate a bit too much for something that only
required an initialization function to be called at right time.

Thus refactoring the devices structures relevantly:
- introducing struct init_entry which is a generic init end-point
- struct deviceconfig is removed and struct device owns everything now.
- SYS_INIT() generates only a struct init_entry via calling
  INIT_ENTRY_DEFINE()
- DEVICE_AND_API_INIT() generates a struct device and calls
  INIT_ENTRY_DEFINE()
- init objects sections are in ROM
- device objects sections are in RAM (but will end up in ROM once they
  will be 'constified')

It also generate a tiny memory gain on both ROM and RAM, which is nice.

Perhaps kernel/device.c could be renamed to something more relevant.

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
This commit is contained in:
Tomasz Bursztyka 2020-03-09 11:02:20 +01:00 committed by Carles Cufí
commit 8d7bb8ffd8
11 changed files with 304 additions and 250 deletions

View file

@ -7,8 +7,9 @@
#ifndef ZEPHYR_INCLUDE_INIT_H_
#define ZEPHYR_INCLUDE_INIT_H_
#include <device.h>
#include <toolchain.h>
#include <kernel.h>
#include <zephyr/types.h>
#ifdef __cplusplus
extern "C" {
@ -29,11 +30,85 @@ extern "C" {
#define _SYS_INIT_LEVEL_SMP 4
#endif
struct device;
/**
* @brief Static init entry structure for each device driver or services
*
* @param init init function for the init entry which will take the dev
* attribute as parameter. See below.
* @param dev pointer to a device driver instance structure. Can be NULL
* if the init entry is not used for a device driver but a service.
*/
struct init_entry {
int (*init)(struct device *dev);
struct device *dev;
};
void z_sys_init_run_level(s32_t level);
/* A counter is used to avoid issues when two or more system devices
* are declared in the same C file with the same init function.
*/
#define Z_SYS_NAME(init_fn) _CONCAT(_CONCAT(sys_init_, init_fn), __COUNTER__)
/**
* @def Z_INIT_ENTRY_DEFINE
*
* @brief Create an init entry object and set it up for boot time initialization
*
* @details This macro defines an init entry object that will be automatically
* configured by the kernel during system initialization. Note that
* init entries will not be accessible from user mode. Also this macro should
* not be used directly, use relevant macro such as SYS_INIT() or
* DEVICE_AND_API_INIT() instead.
*
* @param entry_name Init entry name. It is the name this instance exposes to
* the system.
*
* @param init_fn Address to the init function of the entry.
*
* @param device A device driver instance pointer or NULL
*
* @param level The initialization level at which configuration occurs.
* Must be one of the following symbols, which are listed in the order
* they are performed by the kernel:
* \n
* \li PRE_KERNEL_1: Used for initialization objects that have no dependencies,
* such as those that rely solely on hardware present in the processor/SOC.
* These objects cannot use any kernel services during configuration, since
* they are not yet available.
* \n
* \li PRE_KERNEL_2: Used for initialization objects that rely on objects
* initialized as part of the PRE_KERNEL_1 level. These objects cannot use any
* kernel services during configuration, since they are not yet available.
* \n
* \li POST_KERNEL: Used for initialization objects that require kernel services
* during configuration.
* \n
* \li POST_KERNEL_SMP: Used for initialization objects that require kernel
* services during configuration after SMP initialization.
* \n
* \li APPLICATION: Used for application components (i.e. non-kernel components)
* that need automatic configuration. These objects can use all services
* provided by the kernel during configuration.
*
* @param prio The initialization priority of the object, relative to
* other objects of the same initialization level. Specified as an integer
* value in the range 0 to 99; lower values indicate earlier initialization.
* Must be a decimal integer literal without leading zeroes or sign (e.g. 32),
* or an equivalent symbolic name (e.g. \#define MY_INIT_PRIO 32); symbolic
* expressions are *not* permitted
* (e.g. CONFIG_KERNEL_INIT_PRIORITY_DEFAULT + 5).
*/
#define Z_INIT_ENTRY_DEFINE(entry_name, init_fn, device, level, prio) \
static const Z_DECL_ALIGN(struct init_entry) \
_CONCAT(__init_, entry_name) __used \
__attribute__((__section__(".init_" #level STRINGIFY(prio)))) = { \
.init = (init_fn), \
.dev = (device), \
}
/**
* @def SYS_INIT
*
@ -43,35 +118,13 @@ extern "C" {
*
* @param init_fn Pointer to the boot function to run
*
* @param level The initialization level, See DEVICE_AND_API_INIT for details.
* @param level The initialization level, See Z_INIT_ENTRY_DEFINE for details.
*
* @param prio Priority within the selected initialization level. See
* DEVICE_AND_API_INIT for details.
* Z_INIT_ENTRY_DEFINE for details.
*/
#define SYS_INIT(init_fn, level, prio) \
DEVICE_AND_API_INIT(Z_SYS_NAME(init_fn), "", init_fn, \
NULL, NULL, level, prio, NULL)
/**
* @def SYS_DEVICE_DEFINE
*
* @brief Run an initialization function at boot at specified priority,
* and define device PM control function.
*
* @details This macro lets you run a function at system boot.
*
* @param drv_name Name of this system device
* @param init_fn Pointer to the boot function to run
* @param pm_control_fn Pointer to device_pm_control function.
* Can be empty function (device_pm_control_nop) if not
* implemented.
* @param level The initialization level, See DEVICE_INIT for details.
* @param prio Priority within the selected initialization level. See
* DEVICE_INIT for details.
*/
#define SYS_DEVICE_DEFINE(drv_name, init_fn, pm_control_fn, level, prio) \
DEVICE_DEFINE(Z_SYS_NAME(init_fn), drv_name, init_fn, pm_control_fn, \
NULL, NULL, level, prio, NULL)
Z_INIT_ENTRY_DEFINE(Z_SYS_NAME(init_fn), init_fn, NULL, level, prio)
#ifdef __cplusplus
}