zephyr/drivers/i3c/i3c_ibi_workq.c
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

236 lines
5.1 KiB
C

/*
* Copyright (c) 2016 Wind River Systems, Inc.
* Copyright (c) 2016,2022 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/init.h>
#include <zephyr/drivers/i3c.h>
#include <zephyr/logging/log.h>
LOG_MODULE_DECLARE(i3c, CONFIG_I3C_LOG_LEVEL);
/* Statically allocated array of IBI work item nodes */
static struct i3c_ibi_work i3c_ibi_work_nodes[CONFIG_I3C_IBI_WORKQUEUE_LENGTH];
static K_KERNEL_STACK_DEFINE(i3c_ibi_work_q_stack,
CONFIG_I3C_IBI_WORKQUEUE_STACK_SIZE);
/* IBI workqueue */
static struct k_work_q i3c_ibi_work_q;
static sys_slist_t i3c_ibi_work_nodes_free;
static inline int ibi_work_submit(struct i3c_ibi_work *ibi_node)
{
return k_work_submit_to_queue(&i3c_ibi_work_q, &ibi_node->work);
}
int i3c_ibi_work_enqueue(struct i3c_ibi_work *ibi_work)
{
sys_snode_t *node;
struct i3c_ibi_work *ibi_node;
int ret;
node = sys_slist_get(&i3c_ibi_work_nodes_free);
if (node == NULL) {
ret = -ENOMEM;
goto out;
}
ibi_node = (struct i3c_ibi_work *)node;
(void)memcpy(ibi_node, ibi_work, sizeof(*ibi_node));
ret = ibi_work_submit(ibi_node);
if (ret >= 0) {
ret = 0;
}
out:
return ret;
}
int i3c_ibi_work_enqueue_target_irq(struct i3c_device_desc *target,
uint8_t *payload, size_t payload_len)
{
sys_snode_t *node;
struct i3c_ibi_work *ibi_node;
int ret;
node = sys_slist_get(&i3c_ibi_work_nodes_free);
if (node == NULL) {
ret = -ENOMEM;
goto out;
}
ibi_node = (struct i3c_ibi_work *)node;
ibi_node->type = I3C_IBI_TARGET_INTR;
ibi_node->target = target;
ibi_node->payload.payload_len = payload_len;
if ((payload != NULL) && (payload_len > 0U)) {
(void)memcpy(&ibi_node->payload.payload[0],
payload, payload_len);
}
ret = ibi_work_submit(ibi_node);
if (ret >= 0) {
ret = 0;
}
out:
return ret;
}
int i3c_ibi_work_enqueue_hotjoin(const struct device *dev)
{
sys_snode_t *node;
struct i3c_ibi_work *ibi_node;
int ret;
node = sys_slist_get(&i3c_ibi_work_nodes_free);
if (node == NULL) {
ret = -ENOMEM;
goto out;
}
ibi_node = (struct i3c_ibi_work *)node;
ibi_node->type = I3C_IBI_HOTJOIN;
ibi_node->controller = dev;
ibi_node->payload.payload_len = 0;
ret = ibi_work_submit(ibi_node);
if (ret >= 0) {
ret = 0;
}
out:
return ret;
}
int i3c_ibi_work_enqueue_cb(const struct device *dev,
k_work_handler_t work_cb)
{
sys_snode_t *node;
struct i3c_ibi_work *ibi_node;
int ret;
node = sys_slist_get(&i3c_ibi_work_nodes_free);
if (node == NULL) {
ret = -ENOMEM;
goto out;
}
ibi_node = (struct i3c_ibi_work *)node;
ibi_node->type = I3C_IBI_WORKQUEUE_CB;
ibi_node->controller = dev;
ibi_node->work_cb = work_cb;
ret = ibi_work_submit(ibi_node);
if (ret >= 0) {
ret = 0;
}
out:
return ret;
}
static void i3c_ibi_work_handler(struct k_work *work)
{
struct i3c_ibi_work *ibi_node = CONTAINER_OF(work, struct i3c_ibi_work, work);
struct i3c_ibi_payload *payload;
int ret = 0;
if (IS_ENABLED(CONFIG_I3C_IBI_WORKQUEUE_VERBOSE_DEBUG) &&
((uint32_t)ibi_node->type <= I3C_IBI_TYPE_MAX)) {
LOG_DBG("Processing IBI work %p (type %d, len %u)",
ibi_node, (int)ibi_node->type,
ibi_node->payload.payload_len);
if (ibi_node->payload.payload_len > 0U) {
LOG_HEXDUMP_DBG(&ibi_node->payload.payload[0],
ibi_node->payload.payload_len, "IBI Payload");
}
}
switch (ibi_node->type) {
case I3C_IBI_TARGET_INTR:
if (ibi_node->payload.payload_len != 0U) {
payload = &ibi_node->payload;
} else {
payload = NULL;
}
ret = ibi_node->target->ibi_cb(ibi_node->target, payload);
if ((ret != 0) && (ret != -EBUSY)) {
LOG_ERR("IBI work %p cb returns %d", ibi_node, ret);
}
break;
case I3C_IBI_HOTJOIN:
ret = i3c_do_daa(ibi_node->controller);
if ((ret != 0) && (ret != -EBUSY)) {
LOG_ERR("i3c_do_daa returns %d", ret);
}
break;
case I3C_IBI_WORKQUEUE_CB:
if (ibi_node->work_cb != NULL) {
ibi_node->work_cb(work);
}
break;
case I3C_IBI_CONTROLLER_ROLE_REQUEST:
/* TODO: Add support for controller role request */
__fallthrough;
default:
/* Unknown IBI type: do nothing */
LOG_DBG("Cannot process IBI type %d", (int)ibi_node->type);
break;
}
if (ret == -EBUSY) {
/* Retry if bus is busy. */
if (ibi_work_submit(ibi_node) < 0) {
LOG_ERR("Error re-adding IBI work %p", ibi_node);
}
} else {
/* Add the now processed node back to the free list */
sys_slist_append(&i3c_ibi_work_nodes_free, (sys_snode_t *)ibi_node);
}
}
static int i3c_ibi_work_q_init(void)
{
struct k_work_queue_config cfg = {
.name = "i3c_ibi_workq",
.no_yield = true,
};
/* Init the linked list of work item nodes */
sys_slist_init(&i3c_ibi_work_nodes_free);
for (int i = 0; i < ARRAY_SIZE(i3c_ibi_work_nodes); i++) {
i3c_ibi_work_nodes[i].work.handler = i3c_ibi_work_handler;
sys_slist_append(&i3c_ibi_work_nodes_free,
(sys_snode_t *)&i3c_ibi_work_nodes[i]);
}
/* Start the workqueue */
k_work_queue_start(&i3c_ibi_work_q, i3c_ibi_work_q_stack,
K_KERNEL_STACK_SIZEOF(i3c_ibi_work_q_stack),
CONFIG_I3C_IBI_WORKQUEUE_PRIORITY, &cfg);
return 0;
}
SYS_INIT(i3c_ibi_work_q_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);