Commit graph

92 commits

Author SHA1 Message Date
Martí Bolívar 15c9d3711c kernel: add CONFIG_DEVICE_DT_METADATA and helpers
This option allows you to look up a struct device from any of the
node labels that were attached to the devicetree node used to create
the device, etc.

This is helpful because node labels are a much more human-friendly set
of unique identifiers than the node names we are currently relying on
for use with device_get_binding(). Adding this infrastructure in the
device core allows anyone to make use of it without having to
replicate node label storage and search functions in various places in
the tree. The main use case, however, is for looking up devices by
node label in the shell.

Since there is a footprint penalty associated with storing the node
label metadata, leave this option disabled by default.

Signed-off-by: Martí Bolívar <mbolivar@amperecomputing.com>
2024-06-12 18:49:54 +02:00
Flavio Ceolin 73b755d817 pm: Add a symbol for device power state constraints
Add a symbol to enable device power state constraints this
saves resources when this feature is not needed.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2024-06-07 19:06:23 -04:00
Flavio Ceolin 02a14d75fc pm: Declare pm state constraints for a device
Declare power state constraints for a device in devicetree.
It allows a map between device instances and power states that disable
their power. This information is used by a new API
(pm_policy_device_power_lock_put/get) that automically set/release
pm state constraints.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2024-06-07 19:06:23 -04:00
Flavio Ceolin 65fc5b7f17 device: Remove z_device_is_ready
This duplicates the functionality of device_is_ready.

Calls for z_device_is_ready are being done in kernel mode, so it is
safe to call its implementation directly.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2024-05-31 08:06:44 +02:00
Yong Cong Sin bbe5e1e6eb build: namespace the generated headers with zephyr/
Namespaced the generated headers with `zephyr` to prevent
potential conflict with other headers.

Introduce a temporary Kconfig `LEGACY_GENERATED_INCLUDE_PATH`
that is enabled by default. This allows the developers to
continue the use of the old include paths for the time being
until it is deprecated and eventually removed. The Kconfig will
generate a build-time warning message, similar to the
`CONFIG_TIMER_RANDOM_GENERATOR`.

Updated the includes path of in-tree sources accordingly.

Most of the changes here are scripted, check the PR for more
info.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-05-28 22:03:55 +02:00
Marc Herbert bfa5d02125 device.h: fix unbalanced initializer: ({ .pm_base = (pm_),)}
Close parentheses and braces in the reverse order they are opened.

Fixes commit f91d4731af ("pm: use braces for .pm_base inside anonymous
union initializer")

I have no idea how no compiler caught this!?

I tested very carefully with two different gcc versions and one clang
version and generated binaries are strictly identical before and after
this commit. Fix the code anyway because C macros are painful enough to
debug already, so no need to add more confusion.

Signed-off-by: Marc Herbert <marc.herbert@intel.com>
2024-05-03 07:29:26 -04:00
Ederson de Souza eeebb4d911 kernel: Device deferred initialization
Currently, all devices are initialized at boot time (following their
level and priority order). This patch introduces deferred
initialization: by setting the property `zephyr,deferred-init` on a
device on the devicetree, Zephyr will not initialized the device.

To initialize such devices, one has to call `device_init()`.

Deferred initialization is done by grouping all deferred devices on a
different ELF section. In this way, there's no need to consume more
memory to keep track of deferred devices. When `device_init()` is
called, Zephyr will scan the deferred devices section and call the
initialization function for the matching device. As this scanning is
done only during deferred device initialization, its cost should be
bearable.

Signed-off-by: Ederson de Souza <ederson.desouza@intel.com>
2024-04-11 15:50:44 -04:00
Ederson de Souza 28d08ada28 include/zephyr: Add 'version' and 'since' tag to groups
Based on information from doc/develop/api/overview.rst, add current
version for some groups representing APIs, following the following
table:

  - Experimental:   0.1.0
  - Unstable:       0.8.0
  - Stable:         1.0.0

Also based on doc/develop/api/overview.rst, add 'since' tag to the
groups.

Signed-off-by: Ederson de Souza <ederson.desouza@intel.com>
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2024-03-19 13:13:45 +01:00
Marc Herbert f91d4731af pm: use braces for .pm_base inside anonymous union initializer
Fixes commit 25173f71cd ("pm: device_runtime: Extend with synchronous
runtime PM")

Fixes compilation with gcc 4.2-based toolchain used by SOF for TGL
generation products. As seen in the error message below, that gcc
version requires braces for initialization of anonymous unions:

```
zephyr/soc/xtensa/intel_adsp/common/mem_window.c:62:
   error: unknown field ‘pm_base’ specified in initializer
   warning: missing braces around initializer
   warning: (near initialization for ‘__device_dts_ord_66.<anonymous>’)
```

This is a well-known and recurring issue, see past example(s) in #68118

As the Zephyr build is deterministic, I could easily verify that this
commit makes zero .obj difference (when using the Zephyr SDK).

Signed-off-by: Marc Herbert <marc.herbert@intel.com>
2024-02-21 16:57:56 +01:00
Krzysztof Chruściński 25173f71cd pm: device_runtime: Extend with synchronous runtime PM
In many cases suspending or resuming of a device is limited to
just a few register writes. Current solution assumes that those
operations may be blocking, asynchronous and take a lot of time.
Due to this assumption runtime PM API cannot be effectively used
from the interrupt context. Zephyr has few driver APIs which
can be used from an interrupt context and now use of runtime PM
is limited in those cases.

Patch introduces a new type of PM device - synchronous PM. If
device is specified as capable of synchronous PM operations then
device runtime getting and putting is executed in the critical
section. In that case, runtime API can be used from an interrupt
context. Additionally, this approach reduces RAM needed for
PM device (104 -> 20 bytes of RAM on ARM Cortex-M).

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
2024-02-01 15:03:42 +01:00
Fabio Baltieri 153f38a412 device: add braces around dev/dev_rw initializer
The init_entry struct got modified to add a union with a non const dev
pointer in afc59112a9. Some old compiler (such as GCC 4) seems to
require a pair of brackets to correctly initialize the field in the
union. Add those brackets to the initializers in device.h and init.h to
maintain compatibility.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
2023-11-29 14:44:23 +00:00
Christopher Friedt afc59112a9 device: support for mutable devices
Add support for mutable devices. Mutable devices are those which
can be modified after declaration, in-place, in kernel mode.

In order for a device to be mutable, the following must be true

  * `CONFIG_DEVICE_MUTABLE` must be y-selected
  * the Devicetree bindings for the device must include
    `mutable.yaml`
  * the Devicetree node must include the `zephyr,mutable` property

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-28 15:35:39 +01:00
Gerard Marull-Paretas 915cb05bb6 dts: drop HAS_DTS
HAS_DTS has become a redundant option. All Zephyr architectures now
select this option, meaning devicetree has become a de-facto
requirement.  In fact, if any board does not provide a devicetree
source, the build system uses an empty stub, meaning the devicetree
machinery always runs.

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
2023-10-20 12:18:17 -07:00
Gerard Marull-Paretas beec8b05a8 device: only allow PRE_KERNEL_1/2 and POST_KERNEL levels
Because devices share the same underlying infrastructure with SYS_INIT,
the same levels have always been available. However, not all of them are
needed, and some are not even usable. For example, `EARLY` can't be used
because when initialized `z_device_state_init` has not been called yet.
`SMP` has never been used by device drivers, and it is now in question,
likely to be moved to SMP specific hooks. Finally, `APPLICATION` does
not make much sense in the context of Kernel devices. Note that of the 3
levels just mentioned, only one was actively tested (`APPLICATION`) by
Kernel tests, meaning others were likely never considered in the context
of devices.

This patch leaves `PRE_KERNEL_1`, `PRE_KERNEL_2` and `POST_KERNEL`
available to devices. Others have been deprecated, and will generate a
compiler warning if used.

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
2023-09-21 15:02:45 +02:00
Jordan Yates 2790547cbd init: add sub-priority to internal ordering
Add a sub-priority field to `Z_INIT_ENTRY_SECTION`, which is used to
ensure that multiple drivers running at the same init priority still
run in an order that respects their devicetree dependencies.

This is primarily useful when multiple instances of the same device are
instantiated that can depend on each other, for example power domains
and i2c muxes.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2023-07-25 15:58:06 +00:00
Jordan Yates 109fc87737 Revert "init: add sub-priority to internal ordering"
This reverts commit bb590b5b6e.
2023-07-25 14:17:11 +02:00
Jordan Yates bb590b5b6e init: add sub-priority to internal ordering
Add a sub-priority field to `Z_INIT_ENTRY_SECTION`, which is used to
ensure that multiple drivers running at the same init priority still
run in an order that respects their devicetree dependencies.

This is primarily useful when multiple instances of the same device are
instantiated that can depend on each other, for example power domains
and i2c muxes.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2023-07-25 09:13:16 +02:00
ferar alashkar 8c32950de9 device: add explicit macro/variable unsigned suffix
add explicit unsigned suffix to 'Z_DEVICE_MAX_NAME_LEN' macro, matching
it to return type of sizeof (size_t), thus the comparison operator '<='
has the same essential types, complying with required [misra-c2012-10.4]
rule which states; Both operands of an operator in which the usual
arithmetic conversions are performed shall have the same essential type
category.

Found as a coding guideline violation (Rule 10.4) by static code
scanning tool.

Note: Tested on STM32L5 Nucleo-144 board (stm32l552xx).

Signed-off-by: ferar alashkar <ferar.alashkar@gmail.com>
2023-07-13 16:19:12 -04:00
Gerard Marull-Paretas 48b201cc53 device: make device dependencies optional
Device dependencies are not always required, so make them optional via
CONFIG_DEVICE_DEPS. When enabled, the gen_device_deps script will run so
that dependencies are collected and part of the final image. Related
APIs will be also made available. Since device dependencies are used in
just a few places (power domains), disable the feature by default. When
not enabled, a second linking pass will not be required.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-06-21 09:32:05 +02:00
Gerard Marull-Paretas 319fbe57e1 device: s/HAS_DYNAMIC_DEVICE_HANDLES/DEVICE_DEPS_DYMAMIC
Rename the Kconfig option to be in line with recent renamings in device
handles/dependencies.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-06-21 09:32:05 +02:00
Gerard Marull-Paretas d93586f831 device: s/__devicehdl_/__devicedeps_/
Rename the device dependencies array variable to use the "deps" name, in
line with latest renamings in device.h

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-06-21 09:32:05 +02:00
Gerard Marull-Paretas 5982d83e2a device: s/struct device.handles/struct device.deps
Rename struct device `handles` member to `deps`, in line with previous
renamings in the device API.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-06-21 09:32:05 +02:00
Gerard Marull-Paretas 4eb406e093 device: s/DEVICE_HANDLE/DEVICE_DEPS/
Rename multiple internal device macros to use the DEVICE_DEPS naming, so
that it is clear they belong to the device dependencies APIs.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-06-21 09:32:05 +02:00
Gerard Marull-Paretas e5f82cb5dc device: hide DEVICE_HANDLE_SEP/ENDS
These macros are used internally by the device dependencies functions.
There's no need to expose them publicly, so prefix them with Z_ and add
them under INTERNAL_HIDDEN docs section.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-06-21 09:32:05 +02:00
Gerard Marull-Paretas 8bee39edff device: s/device_handles/device_deps/ in linker scripts
Use the "device_deps" naming scheme to emphasize we are storing device
dependencies. The fact we are using device handles to store them is an
implementation detail.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-06-21 09:32:05 +02:00
Gerard Marull-Paretas 92707866d1 device: s/gen_handles/gen_device_deps
Rename the gen_handles script and all of its references/associated files
to gen_device_deps. The new new makes things more clear, because the
script just take care of generating, for each device, an array of device
dependencies. While device handles are used internally to store this
information, it is in reality an implementation detail.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-06-21 09:32:05 +02:00
Carles Cufi 5b308c0fe1 device: Fix doc syntax for DEVICE_DT_GET_ONE
Add the missing verb.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
2023-05-23 11:58:44 -05:00
Gerard Marull-Paretas dacb3dbfeb iterable_sections: move to specific header
Until now iterable sections APIs have been part of the toolchain
(common) headers. They are not strictly related to a toolchain, they
just rely on linker providing support for sections. Most files relied on
indirect includes to access the API, now, it is included as needed.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-05-22 10:42:30 +02:00
Gerard Marull-Paretas d0e58ad0a6 device: use iterable sections
Use iterable sections to handle devices list. This simplifies devices
implementation by using standard APIs.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-05-12 12:01:10 +02:00
Victor Chavez b8e1ee1042 device: Fixed CPP compatiblity for Z_DEVICE_HANDLES_DEFINE
By using the __weak attribute with the macro `Z_DEVICE_HANDLES_DEFINE`
C++ throws the error "weak declaration must be public". This change
adds the keyword extern if the macro is included in a C++ source
file.

Signed-off-by: Victor Chavez <chavez-bermudez@fh-aachen.de>
2023-04-19 10:02:12 +02:00
Victor Chavez bd59170402 device: Fixed CPP compatiblity for Z_DEVICE_INIT
The macro `Z_DEVICE_INIT` is not compatible with C++ due to
initialization order of the struct device. This commit fixes
the initialization for compatiblity with C++.

Signed-off-by: Victor Chavez <chavez-bermudez@fh-aachen.de>
2023-04-19 10:02:12 +02:00
Gerard Marull-Paretas 4d06166623 device: allow NULL init function
Some devices do not need to perform any initialization, so allow the
init function to be NULL. In this case, the initialization code will
just mark the device as initialized, i.e. ready.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-04-19 10:00:25 +02:00
Gerard Marull-Paretas 6d192e50cf init/device: add missing __noasan
__noasan attribute was accidentally removed by
a5fd0d184a. Add it back as it is causing
some address sanitizer issues when using the LLVM toolchain.

Details on why this is needed can be found in
74cc534758.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-04-13 13:39:54 +02:00
Gerard Marull-Paretas a5fd0d184a init: remove the need for a dummy device pointer in SYS_INIT functions
The init infrastructure, found in `init.h`, is currently used by:

- `SYS_INIT`: to call functions before `main`
- `DEVICE_*`: to initialize devices

They are all sorted according to an initialization level + a priority.
`SYS_INIT` calls are really orthogonal to devices, however, the required
function signature requires a `const struct device *dev` as a first
argument. The only reason for that is because the same init machinery is
used by devices, so we have something like:

```c
struct init_entry {
	int (*init)(const struct device *dev);
	/* only set by DEVICE_*, otherwise NULL */
	const struct device *dev;
}
```

As a result, we end up with such weird/ugly pattern:

```c
static int my_init(const struct device *dev)
{
	/* always NULL! add ARG_UNUSED to avoid compiler warning */
	ARG_UNUSED(dev);
	...
}
```

This is really a result of poor internals isolation. This patch proposes
a to make init entries more flexible so that they can accept sytem
initialization calls like this:

```c
static int my_init(void)
{
	...
}
```

This is achieved using a union:

```c
union init_function {
	/* for SYS_INIT, used when init_entry.dev == NULL */
	int (*sys)(void);
	/* for DEVICE*, used when init_entry.dev != NULL */
	int (*dev)(const struct device *dev);
};

struct init_entry {
	/* stores init function (either for SYS_INIT or DEVICE*)
	union init_function init_fn;
	/* stores device pointer for DEVICE*, NULL for SYS_INIT. Allows
	 * to know which union entry to call.
	 */
	const struct device *dev;
}
```

This solution **does not increase ROM usage**, and allows to offer clean
public APIs for both SYS_INIT and DEVICE*. Note that however, init
machinery keeps a coupling with devices.

**NOTE**: This is a breaking change! All `SYS_INIT` functions will need
to be converted to the new signature. See the script offered in the
following commit.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

init: convert SYS_INIT functions to the new signature

Conversion scripted using scripts/utils/migrate_sys_init.py.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

manifest: update projects for SYS_INIT changes

Update modules with updated SYS_INIT calls:

- hal_ti
- lvgl
- sof
- TraceRecorderSource

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

tests: devicetree: devices: adjust test

Adjust test according to the recently introduced SYS_INIT
infrastructure.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

tests: kernel: threads: adjust SYS_INIT call

Adjust to the new signature: int (*init_fn)(void);

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-04-12 14:28:07 +00:00
Jordan Yates 4b2da59de5 device: fix counting supported devices
With the addition of #42105, `DEVICE_HANDLE_NULL` can exist in the
supported devices handle array. Stop counting supported devices if
this value is reached, as there are no more valid devices.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2023-03-28 17:36:16 -04:00
Jordan Yates 34251808ac device: fixup doxygen parameter names
Fix several parameter references with incorrect names.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2023-01-25 14:58:52 +00:00
Gerard Marull-Paretas cad9a7eb5b Revert "device: restore init_res bit field"
This reverts commit c109491f1a.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-19 16:27:06 +02:00
Gerard Marull-Paretas 8f03cae8ba device: align device state
Device state (struct device_state) was not aligned, required since all
states are gathered together in the z_devstate section. This was causing
boot failures on certain platforms depending on the layout of the struct
device_state.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-19 16:27:06 +02:00
Gerard Marull-Paretas c109491f1a device: restore init_res bit field
After b5f3cf8006 some platforms
(qemu_riscv32_xip) are not able to boot. Re-introduce the bit field for
init_res, using `unsigned int` (valids are signed/unsigned int, and
bool).

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-17 15:12:16 +02:00
Gerard Marull-Paretas b5f3cf8006 device: use uint8_t for init_res
Init result is stored as an 8-bit unsigned integer, so let's use
uint8_t. Docs have been clarified as well.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-17 10:13:37 +02:00
Gerard Marull-Paretas 05dc794445 device: format file
Formatting in the file was inconsistent, results obtained using
clang-format (80 cols), with some minor tweaks.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-17 10:13:37 +02:00
Gerard Marull-Paretas c83ba64a56 device: some Doxygen fixes/improvements
- Align to 80cols
- Fix some incorrect `@p` usages
- Use `@ref foo` to let Doxygen generate references to structs instead
  of using <tt>...</tt>
- Use `@ref struct.field` to reference struct fields
- Use Markdown notation for literals
- use func_name()/MACRO_NAME() to let Doxygen generate references
  automatically without `@ref`
- Fix some minor typos/formatting issues

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-17 10:13:37 +02:00
Gerard Marull-Paretas fce90c0fb9 device: expose device.pm field in the documentation
struct device.pm was not exposed in the docs, make it visible to Doxygen
and document it is conditional to CONFIG_PM_DEVICE=y.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-17 10:13:37 +02:00
Gerard Marull-Paretas 6dccedb172 device: s/name/dev_id in DEVICE_*_GET/DEVICE_DECLARE
The input parameter is a device identifier, not a name. Some macros are
used for all type of devices, DT and non-DT, so adjusted documentation
to be more precise where appropriated.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-17 10:13:37 +02:00
Gerard Marull-Paretas d39e2946dd device: remove device_model_api group
The device model API can be part of the device_model group.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-17 10:13:37 +02:00
Gerard Marull-Paretas e42888bf7c device: move independent groups to groups.dox
Some Doxygen groups do not belong to a specific header, they are used to
group multiple other sub-groups defined in other headers. For this
purpose we have the groups.dox file, there's no need to use device.h for
that.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-17 10:13:37 +02:00
Gerard Marull-Paretas 882162357c scripts: build: gen_handles: use Z_DECL_ALIGN
Instead of hardcoding alignment size for pass 2 device handles, use
Z_DECL_ALIGN. This makes sure gen_handles.py is always in sync with the
type defined in device.h. The build assert in device.h can be removed as
a result, since we do not hardcode handles size anywhere else.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-11 18:11:47 +02:00
Gerard Marull-Paretas 129a093785 device: only use dev_id in Z_DEVICE_HANDLES_NAME
The dev_id is already unique for DT devices (dts_ord_XX), there's no
need to rely on the full node identifier.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-04 10:13:34 +02:00
Gerard Marull-Paretas 80e0b1e2a3 device: format Z_DEVICE_HANDLES_DEFINE
Format according to our guidelines.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-04 10:13:34 +02:00
Gerard Marull-Paretas 6fc42a0e4e device: s/Z_DEVICE_HANDLE_NAME/Z_DEVICE_HANDLES_NAME
Use Z_DEVICE_HANDLES_NAME to be consistent with the definition macro.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-04 10:13:34 +02:00