Browse Source

extmod/modopenamp: Add support for building Open-AMP on device side.

Tested with two VMs each running on a different core.

Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
pull/15392/head
iabdalkader 4 months ago
committed by Damien George
parent
commit
7f49897ada
  1. 12
      extmod/extmod.mk
  2. 33
      extmod/modopenamp.c
  3. 4
      extmod/modopenamp_remoteproc.c
  4. 4
      extmod/modopenamp_remoteproc_store.c

12
extmod/extmod.mk

@ -527,6 +527,7 @@ ifeq ($(MICROPY_PY_OPENAMP),1)
OPENAMP_DIR = lib/open-amp
LIBMETAL_DIR = lib/libmetal
GIT_SUBMODULES += $(LIBMETAL_DIR) $(OPENAMP_DIR)
MICROPY_PY_OPENAMP_MODE ?= 0
include $(TOP)/extmod/libmetal/libmetal.mk
INC += -I$(TOP)/$(OPENAMP_DIR)
@ -536,12 +537,21 @@ ifeq ($(MICROPY_PY_OPENAMP_REMOTEPROC),1)
CFLAGS += -DMICROPY_PY_OPENAMP_REMOTEPROC=1
endif
ifeq ($(MICROPY_PY_OPENAMP_MODE),0)
CFLAGS += -DMICROPY_PY_OPENAMP_HOST=1
CFLAGS_THIRDPARTY += -DVIRTIO_DRIVER_ONLY
else ifeq ($(MICROPY_PY_OPENAMP_MODE),1)
CFLAGS += -DMICROPY_PY_OPENAMP_DEVICE=1
CFLAGS_THIRDPARTY += -DVIRTIO_DEVICE_ONLY
else
$(error Invalid Open-AMP mode specified: $(MICROPY_PY_OPENAMP_MODE))
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 \

33
extmod/modopenamp.c

@ -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
@ -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
@ -267,12 +278,11 @@ static void openamp_ns_callback(struct rpmsg_device *rdev, const char *name, uin
}
}
#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;
@ -299,9 +309,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) {
@ -342,8 +351,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"));
@ -368,7 +379,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"));
@ -389,8 +400,8 @@ 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_init_vdev(&virtio_device->rvdev, vdev, openamp_ns_callback, shm_io, &virtio_device->shm_pool);
MP_STATE_PORT(virtio_device) = virtio_device;
}
@ -399,7 +410,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
};

4
extmod/modopenamp_remoteproc.c

@ -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"
@ -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
extmod/modopenamp_remoteproc_store.c

@ -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"
@ -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

Loading…
Cancel
Save