Skip to content

Commit

Permalink
extmod/openamp: Support building Open-AMP for the device side.
Browse files Browse the repository at this point in the history
Note changes in this commit were sent upstream.

Signed-off-by: iabdalkader <[email protected]>
  • Loading branch information
iabdalkader committed Jul 13, 2024
1 parent 6f4277b commit 9e92921
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 20 deletions.
9 changes: 8 additions & 1 deletion extmod/extmod.mk
Original file line number Diff line number Diff line change
Expand Up @@ -536,12 +536,19 @@ ifeq ($(MICROPY_PY_OPENAMP_REMOTEPROC),1)
CFLAGS += -DMICROPY_PY_OPENAMP_REMOTEPROC=1
endif

ifeq ($(MICROPY_PY_OPENAMP_DEVICE),1)
CFLAGS += -DMICROPY_PY_OPENAMP_DEVICE=1
CFLAGS_THIRDPARTY += -DVIRTIO_DEVICE_ONLY
else
CFLAGS += -DMICROPY_PY_OPENAMP_HOST=1
CFLAGS_THIRDPARTY += -DVIRTIO_DRIVER_ONLY
endif

CFLAGS_THIRDPARTY += \
-I$(BUILD)/openamp \
-I$(TOP)/$(OPENAMP_DIR) \
-I$(TOP)/$(OPENAMP_DIR)/lib/include/ \
-DMETAL_INTERNAL \
-DVIRTIO_DRIVER_ONLY \
-DNO_ATOMIC_64_SUPPORT \
-DRPMSG_BUFFER_SIZE=512 \

Expand Down
1 change: 0 additions & 1 deletion extmod/libmetal/libmetal.mk
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ SRC_LIBMETAL_C := $(addprefix $(BUILD)/openamp/metal/,\
shmem.c \
softirq.c \
version.c \
device.c \
system/micropython/condition.c \
system/micropython/device.c \
system/micropython/io.c \
Expand Down
44 changes: 31 additions & 13 deletions extmod/modopenamp.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,20 @@

#if MICROPY_PY_OPENAMP_RSC_TABLE_ENABLE
#define VIRTIO_DEV_ID 0xFF
#if MICROPY_PY_OPENAMP_HOST
#define VIRTIO_DEV_ROLE RPMSG_HOST
#else
#define VIRTIO_DEV_ROLE RPMSG_REMOTE
#endif // MICROPY_PY_OPENAMP_HOST
#define VIRTIO_DEV_FEATURES (1 << VIRTIO_RPMSG_F_NS)

#define VRING0_ID 0 // VRING0 ID (host to remote) fixed to 0 for linux compatibility
#define VRING1_ID 1 // VRING1 ID (remote to host) fixed to 1 for linux compatibility
#if MICROPY_PY_OPENAMP_HOST
#define VRING_NOTIFY_ID VRING0_ID
#else
#define VRING_NOTIFY_ID VRING1_ID
#endif // MICROPY_PY_OPENAMP_HOST

#define VRING_COUNT 2
#define VRING_ALIGNMENT 32
Expand All @@ -71,13 +80,15 @@
#define VRING_BUFF_ADDR (METAL_SHM_ADDR + 0x2000)
#define VRING_BUFF_SIZE (METAL_SHM_SIZE - 0x2000)

#if MICROPY_PY_OPENAMP_HOST
static const char openamp_trace_buf[128];
#define MICROPY_PY_OPENAMP_TRACE_BUF ((uint32_t)openamp_trace_buf)
#define MICROPY_PY_OPENAMP_TRACE_BUF_LEN sizeof(MICROPY_PY_OPENAMP_TRACE_BUF)
#endif // MICROPY_PY_OPENAMP_HOST

#endif // MICROPY_PY_OPENAMP_RSC_TABLE_ENABLE

#if MICROPY_PY_OPENAMP_REMOTEPROC
#if MICROPY_PY_OPENAMP_HOST && MICROPY_PY_OPENAMP_REMOTEPROC
extern mp_obj_type_t openamp_remoteproc_type;
#endif

Expand Down Expand Up @@ -210,7 +221,7 @@ static mp_obj_t endpoint_make_new(const mp_obj_type_t *type, size_t n_args, size
enum { ARG_name, ARG_callback, ARG_src, ARG_dest };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_name, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_rom_obj = MP_ROM_NONE } },
{ MP_QSTR_callback, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_rom_obj = MP_ROM_NONE } },
{ MP_QSTR_callback, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE } },
{ MP_QSTR_src, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = RPMSG_ADDR_ANY } },
{ MP_QSTR_dest, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = RPMSG_ADDR_ANY } },
};
Expand Down Expand Up @@ -257,6 +268,7 @@ void openamp_remoteproc_notified(mp_sched_node_t *node) {
rproc_virtio_notified(MP_STATE_PORT(virtio_device)->rvdev.vdev, VRING_NOTIFY_ID);
}

#if MICROPY_PY_OPENAMP_HOST
static void openamp_ns_callback(struct rpmsg_device *rdev, const char *name, uint32_t dest) {
metal_log(METAL_LOG_DEBUG, "rpmsg_new_service_callback() new service request name: %s dest %lu\n", name, dest);
// The remote processor advertises its presence to the host by sending
Expand All @@ -266,13 +278,13 @@ static void openamp_ns_callback(struct rpmsg_device *rdev, const char *name, uin
mp_call_function_2(virtio_device->ns_callback, mp_obj_new_int(dest), mp_obj_new_str(name, strlen(name)));
}
}
#endif // MICROPY_PY_OPENAMP_HOST

#if MICROPY_PY_OPENAMP_RSC_TABLE_ENABLE
#if MICROPY_PY_OPENAMP_HOST && MICROPY_PY_OPENAMP_RSC_TABLE_ENABLE
// The shared resource table must be initialized manually by the host here,
// because it's not located in the data region, so the startup code doesn't
// know about it.
static void openamp_rsc_table_init(openamp_rsc_table_t **rsc_table_out) {
openamp_rsc_table_t *rsc_table = METAL_RSC_ADDR;
static void openamp_rsc_table_init(openamp_rsc_table_t *rsc_table) {
memset(rsc_table, 0, METAL_RSC_SIZE);

rsc_table->version = 1;
Expand All @@ -299,9 +311,8 @@ static void openamp_rsc_table_init(openamp_rsc_table_t **rsc_table_out) {
// Flush resource table.
metal_cache_flush((uint32_t *)rsc_table, sizeof(openamp_rsc_table_t));
#endif
*rsc_table_out = rsc_table;
}
#endif // MICROPY_PY_OPENAMP_RSC_TABLE_ENABLE
#endif // MICROPY_PY_OPENAMP_HOST && MICROPY_PY_OPENAMP_RSC_TABLE_ENABLE

static mp_obj_t openamp_new_service_callback(mp_obj_t ns_callback) {
if (MP_STATE_PORT(virtio_device) == NULL) {
Expand Down Expand Up @@ -342,8 +353,10 @@ void openamp_init(void) {
metal_init(&metal_params);

// Initialize the shared resource table.
openamp_rsc_table_t *rsc_table;
openamp_rsc_table_init(&rsc_table);
openamp_rsc_table_t *rsc_table = METAL_RSC_ADDR;
#if MICROPY_PY_OPENAMP_HOST
openamp_rsc_table_init(rsc_table);
#endif // MICROPY_PY_OPENAMP_HOST

if (metal_register_generic_device(&shm_device) != 0) {
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("Failed to register metal device"));
Expand All @@ -368,7 +381,7 @@ void openamp_init(void) {
}

// Create virtio device.
struct virtio_device *vdev = rproc_virtio_create_vdev(RPMSG_HOST, VIRTIO_DEV_ID,
struct virtio_device *vdev = rproc_virtio_create_vdev(VIRTIO_DEV_ROLE, VIRTIO_DEV_ID,
&rsc_table->vdev, rsc_io, NULL, metal_rproc_notify, NULL);
if (vdev == NULL) {
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("Failed to create virtio device"));
Expand All @@ -389,8 +402,13 @@ void openamp_init(void) {
// The remote processor detects that the virtio device is ready by polling
// the status field in the resource table.
rpmsg_virtio_init_shm_pool(&virtio_device->shm_pool, (void *)VRING_BUFF_ADDR, (size_t)VRING_BUFF_SIZE);
rpmsg_init_vdev(&virtio_device->rvdev, vdev, openamp_ns_callback, shm_io, &virtio_device->shm_pool);

rpmsg_ns_bind_cb ns_callback = NULL;
#if MICROPY_PY_OPENAMP_HOST
ns_callback = openamp_ns_callback;
#endif // MICROPY_PY_OPENAMP_HOST

rpmsg_init_vdev(&virtio_device->rvdev, vdev, ns_callback, shm_io, &virtio_device->shm_pool);
MP_STATE_PORT(virtio_device) = virtio_device;
}

Expand All @@ -399,7 +417,7 @@ static const mp_rom_map_elem_t globals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_ENDPOINT_ADDR_ANY), MP_ROM_INT(RPMSG_ADDR_ANY) },
{ MP_ROM_QSTR(MP_QSTR_new_service_callback), MP_ROM_PTR(&openamp_new_service_callback_obj) },
{ MP_ROM_QSTR(MP_QSTR_Endpoint), MP_ROM_PTR(&endpoint_type) },
#if MICROPY_PY_OPENAMP_REMOTEPROC
#if MICROPY_PY_OPENAMP_HOST && MICROPY_PY_OPENAMP_REMOTEPROC
{ MP_ROM_QSTR(MP_QSTR_RemoteProc), MP_ROM_PTR(&openamp_remoteproc_type) },
#endif
};
Expand All @@ -411,6 +429,6 @@ const mp_obj_module_t openamp_module = {
};

MP_REGISTER_ROOT_POINTER(struct _virtio_dev_obj_t *virtio_device);
MP_REGISTER_MODULE(MP_QSTR_openamp, openamp_module);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_openamp, openamp_module);

#endif // MICROPY_PY_OPENAMP
6 changes: 3 additions & 3 deletions extmod/modopenamp_remoteproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* OpenAMP's remoteproc class.
*/

#if MICROPY_PY_OPENAMP_REMOTEPROC
#if MICROPY_PY_OPENAMP_HOST && MICROPY_PY_OPENAMP_REMOTEPROC

#include "py/obj.h"
#include "py/nlr.h"
Expand Down Expand Up @@ -137,7 +137,7 @@ mp_obj_t openamp_remoteproc_make_new(const mp_obj_type_t *type, size_t n_args, s
}

if (mp_obj_is_int(args[ARG_entry].u_obj)) {
self->rproc.bootaddr = mp_obj_get_int(args[ARG_entry].u_obj);
self->rproc.bootaddr = mp_obj_int_get_truncated(args[ARG_entry].u_obj);
} else {
#if MICROPY_PY_OPENAMP_REMOTEPROC_ELFLD_ENABLE
// Load firmware.
Expand Down Expand Up @@ -170,4 +170,4 @@ MP_DEFINE_CONST_OBJ_TYPE(
locals_dict, &openamp_remoteproc_dict
);

#endif // MICROPY_PY_OPENAMP_REMOTEPROC
#endif // MICROPY_PY_OPENAMP_HOST && MICROPY_PY_OPENAMP_REMOTEPROC
4 changes: 2 additions & 2 deletions extmod/modopenamp_remoteproc_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* OpenAMP's remoteproc store.
*/

#if MICROPY_PY_OPENAMP_REMOTEPROC
#if MICROPY_PY_OPENAMP_HOST && MICROPY_PY_OPENAMP_REMOTEPROC

#include "py/obj.h"
#include "py/nlr.h"
Expand Down Expand Up @@ -141,4 +141,4 @@ const struct image_store_ops openamp_remoteproc_store_ops = {

#endif // MICROPY_PY_OPENAMP_REMOTEPROC_STORE_ENABLE

#endif // MICROPY_PY_OPENAMP_REMOTEPROC
#endif // MICROPY_PY_OPENAMP_HOST && MICROPY_PY_OPENAMP_REMOTEPROC

0 comments on commit 9e92921

Please sign in to comment.