2020-04-04 01:01:03 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Intel Corporation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2022-05-06 17:04:23 +08:00
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include <zephyr/init.h>
|
|
|
|
#include <zephyr/linker/linker-defs.h>
|
2023-05-15 21:50:28 +08:00
|
|
|
#include <zephyr/sys/iterable_sections.h>
|
2023-08-30 01:03:12 +08:00
|
|
|
/* private kernel APIs */
|
|
|
|
#include <ksched.h>
|
|
|
|
#include <wait_q.h>
|
2020-04-04 01:01:03 +08:00
|
|
|
|
2024-02-28 21:15:15 +08:00
|
|
|
void k_heap_init(struct k_heap *heap, void *mem, size_t bytes)
|
2020-04-04 01:01:03 +08:00
|
|
|
{
|
2024-02-28 21:15:15 +08:00
|
|
|
z_waitq_init(&heap->wait_q);
|
|
|
|
sys_heap_init(&heap->heap, mem, bytes);
|
2021-03-26 20:42:25 +08:00
|
|
|
|
2024-02-28 21:15:15 +08:00
|
|
|
SYS_PORT_TRACING_OBJ_INIT(k_heap, heap);
|
2020-04-04 01:01:03 +08:00
|
|
|
}
|
|
|
|
|
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>
2022-10-19 15:33:44 +08:00
|
|
|
static int statics_init(void)
|
2020-04-04 01:01:03 +08:00
|
|
|
{
|
2024-02-28 21:15:15 +08:00
|
|
|
STRUCT_SECTION_FOREACH(k_heap, heap) {
|
2021-08-04 04:59:36 +08:00
|
|
|
#if defined(CONFIG_DEMAND_PAGING) && !defined(CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT)
|
|
|
|
/* Some heaps may not present at boot, so we need to wait for
|
|
|
|
* paging mechanism to be initialized before we can initialize
|
|
|
|
* each heap.
|
|
|
|
*/
|
|
|
|
extern bool z_sys_post_kernel;
|
|
|
|
bool do_clear = z_sys_post_kernel;
|
|
|
|
|
|
|
|
/* During pre-kernel init, z_sys_post_kernel == false,
|
|
|
|
* initialize if within pinned region. Otherwise skip.
|
|
|
|
* In post-kernel init, z_sys_post_kernel == true, skip those in
|
|
|
|
* pinned region as they have already been initialized and
|
|
|
|
* possibly already in use. Otherwise initialize.
|
|
|
|
*/
|
2024-02-28 21:15:15 +08:00
|
|
|
if (lnkr_is_pinned((uint8_t *)heap) &&
|
|
|
|
lnkr_is_pinned((uint8_t *)&heap->wait_q) &&
|
|
|
|
lnkr_is_region_pinned((uint8_t *)heap->heap.init_mem,
|
|
|
|
heap->heap.init_bytes)) {
|
2021-08-04 04:59:36 +08:00
|
|
|
do_clear = !do_clear;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (do_clear)
|
|
|
|
#endif /* CONFIG_DEMAND_PAGING && !CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT */
|
|
|
|
{
|
2024-02-28 21:15:15 +08:00
|
|
|
k_heap_init(heap, heap->heap.init_mem, heap->heap.init_bytes);
|
2021-08-04 04:59:36 +08:00
|
|
|
}
|
2020-04-04 01:01:03 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-02 10:06:55 +08:00
|
|
|
SYS_INIT_NAMED(statics_init_pre, statics_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
|
2020-04-04 01:01:03 +08:00
|
|
|
|
2021-08-04 04:59:36 +08:00
|
|
|
#if defined(CONFIG_DEMAND_PAGING) && !defined(CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT)
|
|
|
|
/* Need to wait for paging mechanism to be initialized before
|
|
|
|
* heaps that are not in pinned sections can be initialized.
|
|
|
|
*/
|
2022-07-02 10:06:55 +08:00
|
|
|
SYS_INIT_NAMED(statics_init_post, statics_init, POST_KERNEL, 0);
|
2021-08-04 04:59:36 +08:00
|
|
|
#endif /* CONFIG_DEMAND_PAGING && !CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT */
|
|
|
|
|
2024-02-28 21:15:15 +08:00
|
|
|
void *k_heap_aligned_alloc(struct k_heap *heap, size_t align, size_t bytes,
|
2020-11-13 22:12:31 +08:00
|
|
|
k_timeout_t timeout)
|
2020-04-04 01:01:03 +08:00
|
|
|
{
|
2023-07-07 02:58:03 +08:00
|
|
|
k_timepoint_t end = sys_timepoint_calc(timeout);
|
2020-04-04 01:01:03 +08:00
|
|
|
void *ret = NULL;
|
2022-09-29 07:59:45 +08:00
|
|
|
|
2024-02-28 21:15:15 +08:00
|
|
|
k_spinlock_key_t key = k_spin_lock(&heap->lock);
|
2020-04-04 01:01:03 +08:00
|
|
|
|
2024-02-28 21:15:15 +08:00
|
|
|
SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_heap, aligned_alloc, heap, timeout);
|
2021-03-26 20:42:25 +08:00
|
|
|
|
2020-04-04 01:01:03 +08:00
|
|
|
__ASSERT(!arch_is_in_isr() || K_TIMEOUT_EQ(timeout, K_NO_WAIT), "");
|
|
|
|
|
2021-03-26 20:42:25 +08:00
|
|
|
bool blocked_alloc = false;
|
|
|
|
|
2020-04-04 01:01:03 +08:00
|
|
|
while (ret == NULL) {
|
2024-02-28 21:15:15 +08:00
|
|
|
ret = sys_heap_aligned_alloc(&heap->heap, align, bytes);
|
2020-04-04 01:01:03 +08:00
|
|
|
|
2021-04-19 16:52:34 +08:00
|
|
|
if (!IS_ENABLED(CONFIG_MULTITHREADING) ||
|
2023-07-07 02:58:03 +08:00
|
|
|
(ret != NULL) || K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
|
2020-04-04 01:01:03 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-03-26 20:42:25 +08:00
|
|
|
if (!blocked_alloc) {
|
|
|
|
blocked_alloc = true;
|
|
|
|
|
2024-02-28 21:15:15 +08:00
|
|
|
SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_heap, aligned_alloc, heap, timeout);
|
2021-03-26 20:42:25 +08:00
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* @todo Trace attempt to avoid empty trace segments
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2023-07-07 02:58:03 +08:00
|
|
|
timeout = sys_timepoint_timeout(end);
|
2024-02-28 21:15:15 +08:00
|
|
|
(void) z_pend_curr(&heap->lock, key, &heap->wait_q, timeout);
|
|
|
|
key = k_spin_lock(&heap->lock);
|
2020-04-04 01:01:03 +08:00
|
|
|
}
|
|
|
|
|
2024-02-28 21:15:15 +08:00
|
|
|
SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_heap, aligned_alloc, heap, timeout, ret);
|
2021-03-26 20:42:25 +08:00
|
|
|
|
2024-02-28 21:15:15 +08:00
|
|
|
k_spin_unlock(&heap->lock, key);
|
2020-04-04 01:01:03 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-02-28 21:15:15 +08:00
|
|
|
void *k_heap_alloc(struct k_heap *heap, size_t bytes, k_timeout_t timeout)
|
2021-03-26 20:42:25 +08:00
|
|
|
{
|
2024-02-28 21:15:15 +08:00
|
|
|
SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_heap, alloc, heap, timeout);
|
2021-03-26 20:42:25 +08:00
|
|
|
|
2024-02-28 21:15:15 +08:00
|
|
|
void *ret = k_heap_aligned_alloc(heap, sizeof(void *), bytes, timeout);
|
2021-03-26 20:42:25 +08:00
|
|
|
|
2024-02-28 21:15:15 +08:00
|
|
|
SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_heap, alloc, heap, timeout, ret);
|
2021-03-26 20:42:25 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-02-28 21:15:15 +08:00
|
|
|
void k_heap_free(struct k_heap *heap, void *mem)
|
2020-04-04 01:01:03 +08:00
|
|
|
{
|
2024-02-28 21:15:15 +08:00
|
|
|
k_spinlock_key_t key = k_spin_lock(&heap->lock);
|
2020-04-04 01:01:03 +08:00
|
|
|
|
2024-02-28 21:15:15 +08:00
|
|
|
sys_heap_free(&heap->heap, mem);
|
2020-04-04 06:39:25 +08:00
|
|
|
|
2024-02-28 21:15:15 +08:00
|
|
|
SYS_PORT_TRACING_OBJ_FUNC(k_heap, free, heap);
|
|
|
|
if (IS_ENABLED(CONFIG_MULTITHREADING) && z_unpend_all(&heap->wait_q) != 0) {
|
|
|
|
z_reschedule(&heap->lock, key);
|
2020-04-04 06:39:25 +08:00
|
|
|
} else {
|
2024-02-28 21:15:15 +08:00
|
|
|
k_spin_unlock(&heap->lock, key);
|
2020-04-04 06:39:25 +08:00
|
|
|
}
|
|
|
|
}
|