From a9e72bb58d622fcf1eb874caba17d483bd393cbb Mon Sep 17 00:00:00 2001 From: David Sidrane Date: Mon, 19 Aug 2019 20:42:52 +0000 Subject: [PATCH] Merged in david_s5/nuttx/master_h7 (pull request #1007) stm32h7:Port DTCM from F7 Approved-by: Gregory Nutt --- arch/arm/src/stm32h7/Kconfig | 38 ++- arch/arm/src/stm32h7/Make.defs | 7 + arch/arm/src/stm32h7/stm32_dtcm.c | 61 ++++ arch/arm/src/stm32h7/stm32_dtcm.h | 144 ++++++++++ arch/arm/src/stm32h7/stm32_procfs_dtcm.c | 349 +++++++++++++++++++++++ 5 files changed, 589 insertions(+), 10 deletions(-) create mode 100644 arch/arm/src/stm32h7/stm32_dtcm.c create mode 100644 arch/arm/src/stm32h7/stm32_dtcm.h create mode 100644 arch/arm/src/stm32h7/stm32_procfs_dtcm.c diff --git a/arch/arm/src/stm32h7/Kconfig b/arch/arm/src/stm32h7/Kconfig index 45dc258d98..abdc6c4a49 100644 --- a/arch/arm/src/stm32h7/Kconfig +++ b/arch/arm/src/stm32h7/Kconfig @@ -516,16 +516,6 @@ config STM32H7_SPI_DMA endmenu # "SPI Configuration" -config STM32H7_DMACAPABLE - bool "Workaround non-DMA capable memory" - depends on ARCH_DMA - default n - ---help--- - This option enables the DMA interface stm32_dmacapable that can be - used to check if it is possible to do DMA from the selected address. - Drivers then may use this information to determine if they should - attempt the DMA or fall back to a different transfer method. - menu "U[S]ART Configuration" depends on STM32H7_USART @@ -1089,6 +1079,34 @@ config STM32H7_CUSTOM_CLOCKCONFIG ---help--- Enables special, board-specific STM32 clock configuration. +config STM32H7_DTCMEXCLUDE + bool "Exclude DTCM SRAM from the heap" + default y if ELF + depends on ARMV7M_HAVE_DTCM + ---help--- + Exclude DTCM SRAM from the HEAP because it appears to be impossible + to execute ELF modules from DTCM RAM (REVISIT!). + +config STM32H7_DTCM_PROCFS + bool "DTCM SRAM PROCFS support" + default n + depends on ARMV7M_DTCM && FS_PROCFS + ---help--- + Select to build in support for /proc/dtcm. Reading from /proc/dtcm + will provide statistics about DTCM memory use similar to what you + would get from mallinfo() for the user heap. + +config STM32H7_DMACAPABLE + bool "Workaround non-DMA capable memory" + depends on ARCH_DMA + default n + ---help--- + This option enables the DMA interface stm32_dmacapable that can be + used to check if it is possible to do DMA from the selected address. + Drivers then may use this information to determine if they should + attempt the DMA or fall back to a different transfer method. + + menu "Timer Configuration" config STM32H7_PWM_LL_OPS diff --git a/arch/arm/src/stm32h7/Make.defs b/arch/arm/src/stm32h7/Make.defs index 0030c487a8..d7f11e5262 100644 --- a/arch/arm/src/stm32h7/Make.defs +++ b/arch/arm/src/stm32h7/Make.defs @@ -108,6 +108,13 @@ ifeq ($(CONFIG_BUILD_PROTECTED),y) CHIP_CSRCS += stm32_userspace.c stm32_mpuinit.c endif +ifeq ($(CONFIG_ARMV7M_DTCM),y) +CHIP_CSRCS += stm32_dtcm.c +ifeq ($(CONFIG_STM32H7_DTCM_PROCFS),y) +CHIP_CSRCS += stm32_procfs_dtcm.c +endif +endif + ifeq ($(CONFIG_STM32H7_ADC),y) CHIP_CSRCS += stm32_adc.c endif diff --git a/arch/arm/src/stm32h7/stm32_dtcm.c b/arch/arm/src/stm32h7/stm32_dtcm.c new file mode 100644 index 0000000000..eae75eff01 --- /dev/null +++ b/arch/arm/src/stm32h7/stm32_dtcm.c @@ -0,0 +1,61 @@ +/**************************************************************************** + * arch/arm/src/stm32h7/stm32_dtcm.c + * + * Copyright (C) 2015, 2019 Gregory Nutt. All rights reserved. + * Authors: Gregory Nutt + * David Sidrane + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include "stm32_dtcm.h" + +#ifdef HAVE_DTCM_HEAP + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +struct mm_heap_s g_dtcm_heap; + +#endif /* HAVE_DTCM_HEAP */ diff --git a/arch/arm/src/stm32h7/stm32_dtcm.h b/arch/arm/src/stm32h7/stm32_dtcm.h new file mode 100644 index 0000000000..bb61deca63 --- /dev/null +++ b/arch/arm/src/stm32h7/stm32_dtcm.h @@ -0,0 +1,144 @@ +/**************************************************************************** + * arch/arm/src/stm32h7/stm32_dtcm.h + * + * Copyright (C) 2015-2017, 2019 Gregory Nutt. All rights reserved. + * Authors: Gregory Nutt + * David Sidrane + * Bob Feretich + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#ifndef __ARCH_ARM_SRC_STM32H7_STM32_DTCM_H +#define __ARCH_ARM_SRC_STM32H7_STM32_DTCM_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Configuration ************************************************************/ + +/* Assume that we can support the DTCM heap */ + +#define HAVE_DTCM_HEAP 1 + +/* The STM32 H7 have DTCM memory */ + +#define DTCM_START 0x20000000 +#define DTCM_END 0x20020000 + +/* In order to use the DTCM heap, it had to have been excluded from the main + * heap. + */ + +#ifndef CONFIG_STM32H7_DTCMEXCLUDE +# undef HAVE_DTCM_HEAP +#endif + +/* Can we support the DTCM heap? */ + +#ifdef HAVE_DTCM_HEAP + +/* dtcm_initialize must be called early in initialization in order to + * initialize the DTCM heap. + */ + +#define dtcm_initialize() \ + mm_initialize(&g_dtcm_heap, (FAR void *)DTCM_START, DTCM_END-DTCM_START) + +/* The dtcm_addregion interface could be used if, for example, you want to + * add some other memory region to the DTCM heap. I don't really know why + * you might want to do that, but the functionality is essentially free. + */ + +#define dtcm_addregion(b,s) mm_addregion(&g_dtcm_heap, b, s); + +/* Then, once g_dtcm_heap has been setup by dtcm_initialize(), these memory + * allocators can be used just like the standard memory allocators. + */ + +#define dtcm_malloc(s) mm_malloc(&g_dtcm_heap, s) +#define dtcm_zalloc(s) mm_zalloc(&g_dtcm_heap, s) +#define dtcm_calloc(n,s) mm_calloc(&g_dtcm_heap, n,s) +#define dtcm_free(p) mm_free(&g_dtcm_heap, p) +#define dtcm_realloc(p,s) mm_realloc(&g_dtcm_heap, p, s) +#define dtcm_memalign(a,s) mm_memalign(&g_dtcm_heap, a, s) + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +#ifndef __ASSEMBLY__ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +EXTERN struct mm_heap_s g_dtcm_heap; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +/**************************************************************************** + * Name: dtcm_procfs_register + * + * Description: + * Register the DTCM procfs file system entry + * + ****************************************************************************/ + +#ifdef CONFIG_STM32H7_DTCM_PROCFS +int dtcm_procfs_register(void); +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* HAVE_DTCM_HEAP */ +#endif /* __ARCH_ARM_SRC_STM32H7_STM32_DTCM_H */ diff --git a/arch/arm/src/stm32h7/stm32_procfs_dtcm.c b/arch/arm/src/stm32h7/stm32_procfs_dtcm.c new file mode 100644 index 0000000000..4ee4d1a9b4 --- /dev/null +++ b/arch/arm/src/stm32h7/stm32_procfs_dtcm.c @@ -0,0 +1,349 @@ +/**************************************************************************** + * arch/arm/src/stm32h7/stm32_procfs_dtcm.c + * + * Copyright (C) 2015, 2019 Gregory Nutt. All rights reserved. + * Authors: Gregory Nutt + * David Sidrane + * + * Based on STM32 CCM code contributed by Pelle Windestam: + * + * Copyright (C) 2014 Pelle Windestam. All rights reserved. + * Author: Pelle Windestam (pelle@windestam.se) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +#include "stm32_dtcm.h" + +#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS) && \ + defined(CONFIG_FS_PROCFS_REGISTER) && defined(CONFIG_STM32H7_DTCM_PROCFS) + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define DTCM_LINELEN 64 + +/**************************************************************************** + * Private Types + ****************************************************************************/ +/* This enumeration identifies all of the thread attributes that can be + * accessed via the procfs file system. + */ + +/* This structure describes one open "file" */ + +struct dtcm_file_s +{ + struct procfs_file_s base; /* Base open file structure */ + unsigned int linesize; /* Number of valid characters in line[] */ + char line[DTCM_LINELEN]; /* Pre-allocated buffer for formatted lines */ +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* File system methods */ + +static int dtcm_open(FAR struct file *filep, FAR const char *relpath, + int oflags, mode_t mode); +static int dtcm_close(FAR struct file *filep); +static ssize_t dtcm_read(FAR struct file *filep, FAR char *buffer, + size_t buflen); +static int dtcm_dup(FAR const struct file *oldp, + FAR struct file *newp); +static int dtcm_stat(FAR const char *relpath, FAR struct stat *buf); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/* See include/nutts/fs/procfs.h + * We use the old-fashioned kind of initializers so that this will compile + * with any compiler. + */ + +static const struct procfs_operations dtcm_procfsoperations = +{ + dtcm_open, /* open */ + dtcm_close, /* close */ + dtcm_read, /* read */ + NULL, /* write */ + dtcm_dup, /* dup */ + NULL, /* opendir */ + NULL, /* closedir */ + NULL, /* readdir */ + NULL, /* rewinddir */ + dtcm_stat /* stat */ +}; + +static const struct procfs_entry_s g_procfs_dtcm = +{ + "dtcm", + &dtcm_procfsoperations +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: dtcm_open + ****************************************************************************/ + +static int dtcm_open(FAR struct file *filep, FAR const char *relpath, + int oflags, mode_t mode) +{ + FAR struct dtcm_file_s *priv; + + finfo("Open '%s'\n", relpath); + + /* PROCFS is read-only. Any attempt to open with any kind of write + * access is not permitted. + * + * REVISIT: Write-able proc files could be quite useful. + */ + + if ((oflags & O_WRONLY) != 0 || (oflags & O_RDONLY) == 0) + { + ferr("ERROR: Only O_RDONLY supported\n"); + return -EACCES; + } + + /* "cpuload" is the only acceptable value for the relpath */ + + if (strcmp(relpath, "dtcm") != 0) + { + ferr("ERROR: relpath is '%s'\n", relpath); + return -ENOENT; + } + + /* Allocate a container to hold the task and attribute selection */ + + priv = (FAR struct dtcm_file_s *)kmm_zalloc(sizeof(struct dtcm_file_s)); + if (!priv) + { + ferr("ERROR: Failed to allocate file attributes\n"); + return -ENOMEM; + } + + /* Save the index as the open-specific state in filep->f_priv */ + + filep->f_priv = (FAR void *)priv; + return OK; +} + +/**************************************************************************** + * Name: dtcm_close + ****************************************************************************/ + +static int dtcm_close(FAR struct file *filep) +{ + FAR struct dtcm_file_s *priv; + + /* Recover our private data from the struct file instance */ + + priv = (FAR struct dtcm_file_s *)filep->f_priv; + DEBUGASSERT(priv); + + /* Release the file attributes structure */ + + kmm_free(priv); + filep->f_priv = NULL; + return OK; +} + +/**************************************************************************** + * Name: dtcm_read + ****************************************************************************/ + +static ssize_t dtcm_read(FAR struct file *filep, FAR char *buffer, + size_t buflen) +{ + FAR struct dtcm_file_s *priv; + size_t linesize; + size_t copysize; + size_t remaining; + size_t totalsize; + struct mallinfo mem; + off_t offset = filep->f_pos; + + finfo("buffer=%p buflen=%d\n", buffer, (int)buflen); + + /* Recover our private data from the struct file instance */ + + priv = (FAR struct dtcm_file_s *)filep->f_priv; + DEBUGASSERT(priv); + + mm_mallinfo(&g_dtcm_heap, &mem); + + remaining = buflen; + totalsize = 0; + + linesize = snprintf(priv->line, + DTCM_LINELEN, + " total used free largest\n"); + copysize = procfs_memcpy(priv->line, linesize, buffer, remaining, &offset); + totalsize += copysize; + buffer += copysize; + remaining -= copysize; + + if (totalsize >= buflen) + { + return totalsize; + } + + linesize = snprintf(priv->line, + DTCM_LINELEN, + "Mem: %11d%11d%11d%11d\n", + mem.arena, + mem.uordblks, + mem.fordblks, + mem.mxordblk); + copysize = procfs_memcpy(priv->line, linesize, buffer, remaining, &offset); + totalsize += copysize; + + /* Update the file offset */ + + if (totalsize > 0) + { + filep->f_pos += totalsize; + } + + return totalsize; +} + +/**************************************************************************** + * Name: dtcm_dup + * + * Description: + * Duplicate open file data in the new file structure. + * + ****************************************************************************/ + +static int dtcm_dup(FAR const struct file *oldp, FAR struct file *newp) +{ + FAR struct dtcm_file_s *oldpriv; + FAR struct dtcm_file_s *newpriv; + + finfo("Dup %p->%p\n", oldp, newp); + + /* Recover our private data from the old struct file instance */ + + oldpriv = (FAR struct dtcm_file_s *)oldp->f_priv; + DEBUGASSERT(oldpriv); + + /* Allocate a new container to hold the task and attribute selection */ + + newpriv = (FAR struct dtcm_file_s *)kmm_zalloc(sizeof(struct dtcm_file_s)); + if (!newpriv) + { + ferr("ERROR: Failed to allocate file attributes\n"); + return -ENOMEM; + } + + /* The copy the file attributes from the old attributes to the new */ + + memcpy(newpriv, oldpriv, sizeof(struct dtcm_file_s)); + + /* Save the new attributes in the new file structure */ + + newp->f_priv = (FAR void *)newpriv; + return OK; +} + +static int dtcm_stat(const char *relpath, struct stat *buf) +{ + if (strcmp(relpath, "dtcm") != 0) + { + ferr("ERROR: relpath is '%s'\n", relpath); + return -ENOENT; + } + + buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR; + buf->st_size = 0; + buf->st_blksize = 0; + buf->st_blocks = 0; + + return OK; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: dtcm_procfs_register + * + * Description: + * Register the DTCM procfs file system entry + * + ****************************************************************************/ + +int dtcm_procfs_register(void) +{ + return procfs_register(&g_procfs_dtcm); +} + +#endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_PROCFS && + * CONFIG_FS_PROCFS_REGISTER && CONFIG_STM32H7_DTCM_PROCFS */