diff --git a/drivers/note/CMakeLists.txt b/drivers/note/CMakeLists.txt index 554dbd1636209..cd2c4e09902e1 100644 --- a/drivers/note/CMakeLists.txt +++ b/drivers/note/CMakeLists.txt @@ -26,7 +26,17 @@ if(CONFIG_DRIVERS_NOTE) list(APPEND SRCS note_initialize.c) endif() +if(CONFIG_DRIVERS_NOTEFILE OR CONFIG_DRIVERS_NOTELOWEROUT) + list(APPEND SRCS notestream_driver.c) +endif() + if(CONFIG_DRIVERS_NOTERAM) + if(NOT "${CONFIG_DRIVERS_NOTERAM_SECTION}" STREQUAL "") + target_compile_definitions( + drivers + PRIVATE -DDRIVERS_NOTERAM_SECTION="${CONFIG_DRIVERS_NOTERAM_SECTION}") + endif() + list(APPEND SRCS noteram_driver.c) endif() diff --git a/drivers/note/Kconfig b/drivers/note/Kconfig index 904ad6b9cf3b4..7a79d441f3fc1 100644 --- a/drivers/note/Kconfig +++ b/drivers/note/Kconfig @@ -229,14 +229,14 @@ config DRIVERS_NOTERPMSG_SERVER bool "Enable RPMSG server for NOTE" select DRIVERS_NOTE default n - depends on RPTUN + depends on RPMSG ---help--- Use rpmsg to receive message from remote proc. config DRIVERS_NOTERPMSG bool "Note to RPMSG" select DRIVERS_NOTE - depends on RPTUN + depends on RPMSG depends on SCHED_WORKQUEUE default n ---help--- diff --git a/drivers/note/note_driver.c b/drivers/note/note_driver.c index c978e9387c0d4..6263cd24fd878 100644 --- a/drivers/note/note_driver.c +++ b/drivers/note/note_driver.c @@ -107,6 +107,10 @@ #define note_vprintf(drv, ip, fmt, va) \ ((drv)->ops->vprintf && ((drv)->ops->vprintf(drv, ip, fmt, va), true)) +#define BUFFER_SIZE 256 +static_assert(BUFFER_SIZE >= sizeof(struct note_event_s), + "Buffer size too small"); + /**************************************************************************** * Private Types ****************************************************************************/ @@ -607,7 +611,8 @@ void sched_note_add(FAR const void *data, size_t len) * claims more than is left would wrap len and walk off the packet. */ - if (notelen < sizeof(struct note_common_s) || notelen > len) + if (notelen < sizeof(struct note_common_s) || + NOTE_ALIGN(notelen) > len) { break; } @@ -624,8 +629,12 @@ void sched_note_add(FAR const void *data, size_t len) note_add(*driver, note, notelen); } - data = (FAR void *)((uintptr_t)data + notelen); - len -= notelen; + /* The note data from note rpmsg is aligned. When parsing + * the data, it is necessary to align the notelen. + */ + + data = (FAR void *)((uintptr_t)data + NOTE_ALIGN(notelen)); + len -= NOTE_ALIGN(notelen); } } #endif @@ -749,7 +758,7 @@ void sched_note_stop(FAR struct tcb_s *tcb) /* Add the note to circular buffer */ - note_add(*driver, ¬e, sizeof(struct note_stop_s)); + note_add(*driver, ¬e, note.nsp_cmn.nc_length); } } @@ -788,7 +797,7 @@ void sched_note_suspend(FAR struct tcb_s *tcb) /* Add the note to circular buffer */ - note_add(*driver, ¬e, sizeof(struct note_suspend_s)); + note_add(*driver, ¬e, note.nsu_cmn.nc_length); } } @@ -826,7 +835,7 @@ void sched_note_resume(FAR struct tcb_s *tcb) /* Add the note to circular buffer */ - note_add(*driver, ¬e, sizeof(struct note_resume_s)); + note_add(*driver, ¬e, note.nre_cmn.nc_length); } } @@ -866,7 +875,7 @@ void sched_note_cpu_start(FAR struct tcb_s *tcb, int cpu) /* Add the note to circular buffer */ - note_add(*driver, ¬e, sizeof(struct note_cpu_start_s)); + note_add(*driver, ¬e, note.ncs_cmn.nc_length); } } @@ -904,7 +913,7 @@ void sched_note_cpu_started(FAR struct tcb_s *tcb) /* Add the note to circular buffer */ - note_add(*driver, ¬e, sizeof(struct note_cpu_started_s)); + note_add(*driver, ¬e, note.ncs_cmn.nc_length); } } @@ -943,7 +952,7 @@ void sched_note_cpu_pause(FAR struct tcb_s *tcb, int cpu) /* Add the note to circular buffer */ - note_add(*driver, ¬e, sizeof(struct note_cpu_pause_s)); + note_add(*driver, ¬e, note.ncp_cmn.nc_length); } } @@ -981,7 +990,7 @@ void sched_note_cpu_paused(FAR struct tcb_s *tcb) /* Add the note to circular buffer */ - note_add(*driver, ¬e, sizeof(struct note_cpu_paused_s)); + note_add(*driver, ¬e, note.ncp_cmn.nc_length); } } @@ -1020,7 +1029,7 @@ void sched_note_cpu_resume(FAR struct tcb_s *tcb, int cpu) /* Add the note to circular buffer */ - note_add(*driver, ¬e, sizeof(struct note_cpu_resume_s)); + note_add(*driver, ¬e, note.ncr_cmn.nc_length); } } @@ -1058,7 +1067,7 @@ void sched_note_cpu_resumed(FAR struct tcb_s *tcb) /* Add the note to circular buffer */ - note_add(*driver, ¬e, sizeof(struct note_cpu_resumed_s)); + note_add(*driver, ¬e, note.ncr_cmn.nc_length); } } #endif /* CONFIG_SMP */ @@ -1102,7 +1111,7 @@ void sched_note_preemption(FAR struct tcb_s *tcb, bool locked) /* Add the note to circular buffer */ - note_add(*driver, ¬e, sizeof(struct note_preempt_s)); + note_add(*driver, ¬e, note.npr_cmn.nc_length); } spin_unlock_irqrestore_notrace(&g_note_lock, flags); @@ -1147,7 +1156,7 @@ void sched_note_csection(FAR struct tcb_s *tcb, bool enter) /* Add the note to circular buffer */ - note_add(*driver, ¬e, sizeof(struct note_csection_s)); + note_add(*driver, ¬e, note.ncs_cmn.nc_length); } } #endif @@ -1206,7 +1215,7 @@ void sched_note_spinlock(FAR struct tcb_s *tcb, /* Add the note to circular buffer */ - note_add(*driver, ¬e, sizeof(struct note_spinlock_s)); + note_add(*driver, ¬e, note.nsp_cmn.nc_length); } } @@ -1362,7 +1371,7 @@ void sched_note_syscall_leave(int nr, uintptr_t result) /* Add the note to circular buffer */ - note_add(*driver, ¬e, sizeof(struct note_syscall_leave_s)); + note_add(*driver, ¬e, note.nsc_cmn.nc_length); } } #endif @@ -1404,7 +1413,7 @@ void sched_note_irqhandler(int irq, FAR void *handler, bool enter) /* Add the note to circular buffer */ - note_add(*driver, ¬e, sizeof(struct note_irqhandler_s)); + note_add(*driver, ¬e, note.nih_cmn.nc_length); } } #endif @@ -1441,7 +1450,7 @@ void sched_note_wdog(uint8_t event, FAR void *handler, FAR const void *arg) /* Add the note to circular buffer */ - note_add(*driver, ¬e, sizeof(note)); + note_add(*driver, ¬e, note.nwd_cmn.nc_length); } leave_critical_section_notrace(flags); @@ -1486,7 +1495,7 @@ void sched_note_heap(uint8_t event, FAR void *heap, FAR void *mem, /* Add the note to circular buffer */ - note_add(*driver, ¬e, sizeof(note)); + note_add(*driver, ¬e, note.nhp_cmn.nc_length); } } #endif @@ -1498,7 +1507,7 @@ void sched_note_event_ip(uint32_t tag, uintptr_t ip, uint8_t event, FAR struct note_event_s *note; FAR struct note_driver_s **driver; bool formatted = false; - char data[256]; + char data[BUFFER_SIZE]; unsigned int length; FAR struct tcb_s *tcb = running_task(); @@ -1552,7 +1561,7 @@ void sched_note_vprintf_ip(uint32_t tag, uintptr_t ip, FAR const char *fmt, FAR struct note_printf_s *note; FAR struct note_driver_s **driver; bool formatted = false; - uint8_t data[256]; + uint8_t data[BUFFER_SIZE]; size_t length = 0; FAR struct tcb_s *tcb = running_task(); diff --git a/drivers/note/notelog_driver.c b/drivers/note/notelog_driver.c index b8be1501449a9..543bf8ba90e94 100644 --- a/drivers/note/notelog_driver.c +++ b/drivers/note/notelog_driver.c @@ -69,10 +69,10 @@ static void notelog_csection(FAR struct note_driver_s *drv, FAR struct tcb_s *tcb, bool enter); #endif #ifdef CONFIG_SCHED_INSTRUMENTATION_SPINLOCKS -static void note_spinlock(FAR struct note_driver_s *drv, - FAR struct tcb_s *tcb, - FAR volatile void *spinlock, - int type); +static void notelog_spinlock(FAR struct note_driver_s *drv, + FAR struct tcb_s *tcb, + FAR volatile void *spinlock, + int type); #endif #ifdef CONFIG_SCHED_INSTRUMENTATION_IRQHANDLER static void notelog_irqhandler(FAR struct note_driver_s *drv, int irq, @@ -109,7 +109,7 @@ static const struct note_driver_ops_s g_notelog_ops = notelog_csection, /* csection */ #endif #ifdef CONFIG_SCHED_INSTRUMENTATION_SPINLOCKS - note_spinlock, /* spinlock */ + notelog_spinlock, /* spinlock */ #endif #ifdef CONFIG_SCHED_INSTRUMENTATION_SYSCALL NULL, /* syscall_enter */ @@ -283,10 +283,10 @@ static void notelog_csection(FAR struct note_driver_s *drv, #endif #ifdef CONFIG_SCHED_INSTRUMENTATION_SPINLOCKS -static void note_spinlock(FAR struct note_driver_s *drv, - FAR struct tcb_s *tcb, - FAR volatile void *spinlock, - int type) +static void notelog_spinlock(FAR struct note_driver_s *drv, + FAR struct tcb_s *tcb, + FAR volatile void *spinlock, + int type) { FAR static const char * const tmp[] = { diff --git a/drivers/note/noteram_driver.c b/drivers/note/noteram_driver.c index ed12808508aac..068042d50a028 100644 --- a/drivers/note/noteram_driver.c +++ b/drivers/note/noteram_driver.c @@ -403,10 +403,6 @@ static ssize_t noteram_get(FAR struct noteram_driver_s *drv, if (buflen < notelen) { - /* Skip the large note so that we do not get constipated. */ - - drv->ni_read = noteram_next(drv, read, NOTE_ALIGN(notelen)); - /* and return an error */ return -EFBIG; @@ -496,9 +492,23 @@ static ssize_t noteram_read(FAR struct file *filep, FAR char *buffer, if (ctx->mode == NOTERAM_MODE_READ_BINARY) #endif { + size_t nread = 0; + flags = spin_lock_irqsave_notrace(&drv->lock); - ret = noteram_get(drv, (FAR uint8_t *)buffer, buflen); + while (nread < buflen) + { + ret = noteram_get(drv, (FAR uint8_t *)buffer + nread, + buflen - nread); + if (ret <= 0) + { + break; + } + + nread += ret; + } + spin_unlock_irqrestore_notrace(&drv->lock, flags); + return nread; } #ifdef CONFIG_DRIVERS_NOTERAM_TEXT_DUMP else @@ -509,7 +519,7 @@ static ssize_t noteram_read(FAR struct file *filep, FAR char *buffer, do { - uint8_t note[256]; + aligned_data(sizeof(uintptr_t)) uint8_t note[256]; /* Get the next note (removing it from the buffer) */ diff --git a/drivers/note/noterpmsg_driver.c b/drivers/note/noterpmsg_driver.c index 414ebfa3e5dce..196558d7cb267 100644 --- a/drivers/note/noterpmsg_driver.c +++ b/drivers/note/noterpmsg_driver.c @@ -45,9 +45,9 @@ struct noterpmsg_driver_s { struct note_driver_s driver; + FAR uint8_t *buffer; volatile size_t head; volatile size_t tail; - uint8_t buffer[CONFIG_DRIVERS_NOTERPMSG_BUFSIZE]; struct work_s work; struct rpmsg_endpoint ept; spinlock_t lock; @@ -64,6 +64,8 @@ static void noterpmsg_add(FAR struct note_driver_s *driver, * Private Data ****************************************************************************/ +static uint8_t g_noterpmsg_buffer[CONFIG_DRIVERS_NOTERPMSG_BUFSIZE]; + static const struct note_driver_ops_s g_noterpmsg_ops = { noterpmsg_add @@ -85,6 +87,7 @@ struct noterpmsg_driver_s g_noterpmsg_driver = #endif &g_noterpmsg_ops }, + g_noterpmsg_buffer }; /**************************************************************************** @@ -122,7 +125,7 @@ static inline void noterpmsg_remove(FAR struct noterpmsg_driver_s *drv) uint8_t notelen = drv->buffer[tail]; DEBUGASSERT(notelen <= noterpmsg_length(drv)); - drv->tail = noterpmsg_next(drv, tail, notelen); + drv->tail = noterpmsg_next(drv, tail, NOTE_ALIGN(notelen)); } static bool noterpmsg_transfer(FAR struct noterpmsg_driver_s *drv, @@ -131,13 +134,13 @@ static bool noterpmsg_transfer(FAR struct noterpmsg_driver_s *drv, for (; ; ) { FAR uint8_t *buffer; + irqstate_t flags; uint32_t space; size_t len; - len = noterpmsg_length(drv); - if (len == 0) + if (!is_rpmsg_ept_ready(&drv->ept)) { - return true; + return false; } buffer = rpmsg_get_tx_payload_buffer(&drv->ept, &space, wait); @@ -146,6 +149,15 @@ static bool noterpmsg_transfer(FAR struct noterpmsg_driver_s *drv, return false; } + flags = spin_lock_irqsave_notrace(&drv->lock); + len = noterpmsg_length(drv); + if (len == 0) + { + spin_unlock_irqrestore_notrace(&drv->lock, flags); + rpmsg_release_tx_buffer(&drv->ept, buffer); + return true; + } + if (space < len) { /* Find the len of large entire note data */ @@ -154,10 +166,10 @@ static bool noterpmsg_transfer(FAR struct noterpmsg_driver_s *drv, uint8_t notelen = drv->buffer[pos]; len = 0; - while (len + notelen <= space) + while (len + NOTE_ALIGN(notelen) <= space) { - pos = noterpmsg_next(drv, pos, notelen); - len += notelen; + pos = noterpmsg_next(drv, pos, NOTE_ALIGN(notelen)); + len += NOTE_ALIGN(notelen); notelen = drv->buffer[pos]; } } @@ -173,22 +185,20 @@ static bool noterpmsg_transfer(FAR struct noterpmsg_driver_s *drv, rpmsg_release_tx_buffer(&drv->ept, buffer); } - drv->tail = noterpmsg_next(drv, drv->tail, len); + drv->tail = noterpmsg_next(drv, drv->tail, NOTE_ALIGN(len)); + spin_unlock_irqrestore_notrace(&drv->lock, flags); } } static void noterpmsg_work(FAR void *priv) { FAR struct noterpmsg_driver_s *drv = priv; - irqstate_t flags = spin_lock_irqsave_notrace(&drv->lock); if (!noterpmsg_transfer(drv, false)) { work_queue(HPWORK, &drv->work, noterpmsg_work, drv, NOTE_RPMSG_WORK_DELAY); } - - spin_unlock_irqrestore_notrace(&drv->lock, flags); } static void noterpmsg_add(FAR struct note_driver_s *driver, @@ -196,19 +206,23 @@ static void noterpmsg_add(FAR struct note_driver_s *driver, { FAR struct noterpmsg_driver_s *drv = (FAR struct noterpmsg_driver_s *)driver; + bool sent = false; irqstate_t flags; size_t space; flags = spin_lock_irqsave_notrace(&drv->lock); space = CONFIG_DRIVERS_NOTERPMSG_BUFSIZE - noterpmsg_length(drv); - if (space < notelen) + if (space < NOTE_ALIGN(notelen)) { if (!up_interrupt_context() && !sched_idletask()) { - noterpmsg_transfer(drv, true); + spin_unlock_irqrestore_notrace(&drv->lock, flags); + sent = noterpmsg_transfer(drv, true); + flags = spin_lock_irqsave_notrace(&drv->lock); } - else + + if (!sent) { /* Overwrite */ @@ -218,7 +232,7 @@ static void noterpmsg_add(FAR struct note_driver_s *driver, space = CONFIG_DRIVERS_NOTERPMSG_BUFSIZE - noterpmsg_length(drv); } - while (space < notelen); + while (space < NOTE_ALIGN(notelen)); } } @@ -228,15 +242,14 @@ static void noterpmsg_add(FAR struct note_driver_s *driver, memcpy(drv->buffer + drv->head, note, space); memcpy(drv->buffer, note + space, notelen - space); - drv->head = noterpmsg_next(drv, drv->head, notelen); + drv->head = noterpmsg_next(drv, drv->head, NOTE_ALIGN(notelen)); + spin_unlock_irqrestore_notrace(&drv->lock, flags); if (work_available(&drv->work)) { work_queue(HPWORK, &drv->work, noterpmsg_work, drv, NOTE_RPMSG_WORK_DELAY); } - - spin_unlock_irqrestore_notrace(&drv->lock, flags); } static int noterpmsg_ept_cb(FAR struct rpmsg_endpoint *ept,