You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2024/09/13 01:09:25 UTC

(nuttx) branch master updated (512a496467 -> d8d7f3c38e)

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


    from 512a496467 riscv_mtimer: modify riscv_mtimer_current to reduce precision lost
     new 24050ba59f coresight: add coresight_core
     new 8cc0c24318 coresight:add common api for coresight system
     new 5324f69b58 coresight:add link coresight device support
     new c0e40fc19f coresight:add tpiu device support
     new e3e23f7eaf coresight:add stm device
     new 71e4267a7a coresight: add etm3 device support
     new 79af05c4ae coresight:add etb device support
     new df33c392ae coresight:add tmc device support
     new 8a57dafdcf coresight: add clk control for coresight system
     new 6f770fb4b1 coresight: disable device before unregister
     new d04e1ebf4e coresight: return -EEXIST when register a device which has been registered
     new b98d68599e coresight: add document for coresight
     new d8d7f3c38e bits.h: replace all inline defination of __set_bit/__clear_bit

The 13 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 Documentation/guides/coresight.rst                 |  78 ++
 Documentation/guides/index.rst                     |   1 +
 Kconfig                                            |  33 +
 drivers/Kconfig                                    |   1 +
 drivers/Makefile                                   |   1 +
 drivers/coresight/CMakeLists.txt                   |  54 ++
 drivers/coresight/Kconfig                          |  62 ++
 drivers/coresight/Make.defs                        |  59 ++
 drivers/coresight/coresight_common.c               | 312 +++++++
 drivers/coresight/coresight_common.h               | 203 +++++
 drivers/coresight/coresight_core.c                 | 957 +++++++++++++++++++++
 drivers/coresight/coresight_etb.c                  | 500 +++++++++++
 drivers/coresight/coresight_etm3.c                 | 715 +++++++++++++++
 drivers/coresight/coresight_funnel.c               | 274 ++++++
 drivers/coresight/coresight_replicator.c           | 298 +++++++
 drivers/coresight/coresight_stm.c                  | 516 +++++++++++
 drivers/coresight/coresight_tmc_core.c             | 204 +++++
 drivers/coresight/coresight_tmc_core.h             | 189 ++++
 drivers/coresight/coresight_tmc_etf.c              | 573 ++++++++++++
 drivers/coresight/coresight_tmc_etr.c              | 512 +++++++++++
 drivers/coresight/coresight_tpiu.c                 | 206 +++++
 drivers/lcd/max7219.c                              |  33 +-
 drivers/lcd/memlcd.c                               |  33 +-
 include/debug.h                                    |  18 +
 include/nuttx/bits.h                               |  23 +
 include/nuttx/coresight/coresight.h                | 279 ++++++
 .../nuttx/{bits.h => coresight/coresight_etb.h}    |  97 +--
 include/nuttx/coresight/coresight_etm.h            | 138 +++
 .../nuttx/{bits.h => coresight/coresight_funnel.h} | 108 ++-
 .../{bits.h => coresight/coresight_replicator.h}   |  92 +-
 include/nuttx/coresight/coresight_stm.h            | 179 ++++
 include/nuttx/coresight/coresight_tmc.h            | 108 +++
 .../nuttx/{bits.h => coresight/coresight_tpiu.h}   |  91 +-
 33 files changed, 6652 insertions(+), 295 deletions(-)
 create mode 100644 Documentation/guides/coresight.rst
 create mode 100644 drivers/coresight/CMakeLists.txt
 create mode 100644 drivers/coresight/Kconfig
 create mode 100644 drivers/coresight/Make.defs
 create mode 100644 drivers/coresight/coresight_common.c
 create mode 100644 drivers/coresight/coresight_common.h
 create mode 100644 drivers/coresight/coresight_core.c
 create mode 100644 drivers/coresight/coresight_etb.c
 create mode 100644 drivers/coresight/coresight_etm3.c
 create mode 100644 drivers/coresight/coresight_funnel.c
 create mode 100644 drivers/coresight/coresight_replicator.c
 create mode 100644 drivers/coresight/coresight_stm.c
 create mode 100644 drivers/coresight/coresight_tmc_core.c
 create mode 100644 drivers/coresight/coresight_tmc_core.h
 create mode 100644 drivers/coresight/coresight_tmc_etf.c
 create mode 100644 drivers/coresight/coresight_tmc_etr.c
 create mode 100644 drivers/coresight/coresight_tpiu.c
 create mode 100644 include/nuttx/coresight/coresight.h
 copy include/nuttx/{bits.h => coresight/coresight_etb.h} (52%)
 create mode 100644 include/nuttx/coresight/coresight_etm.h
 copy include/nuttx/{bits.h => coresight/coresight_funnel.h} (52%)
 copy include/nuttx/{bits.h => coresight/coresight_replicator.h} (52%)
 create mode 100644 include/nuttx/coresight/coresight_stm.h
 create mode 100644 include/nuttx/coresight/coresight_tmc.h
 copy include/nuttx/{bits.h => coresight/coresight_tpiu.h} (52%)


(nuttx) 13/13: bits.h: replace all inline defination of __set_bit/__clear_bit

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit d8d7f3c38e7844fe1b331db21109e058b710c2a9
Author: liaoao <li...@xiaomi.com>
AuthorDate: Fri Jan 26 17:23:37 2024 +0800

    bits.h: replace all inline defination of __set_bit/__clear_bit
    
    Signed-off-by: liaoao <li...@xiaomi.com>
---
 drivers/lcd/max7219.c | 33 +--------------------------------
 drivers/lcd/memlcd.c  | 33 +--------------------------------
 2 files changed, 2 insertions(+), 64 deletions(-)

diff --git a/drivers/lcd/max7219.c b/drivers/lcd/max7219.c
index 1aecff8ecb..a6157aa5ad 100644
--- a/drivers/lcd/max7219.c
+++ b/drivers/lcd/max7219.c
@@ -271,37 +271,6 @@ static struct max7219_dev_s g_max7219dev =
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * __set_bit - Set a bit in memory
- *
- *   nr   - The bit to set
- *   addr - The address to start counting from
- *
- * Unlike set_bit(), this function is non-atomic and may be reordered.
- * If it's called on the same region of memory simultaneously, the effect
- * may be that only one operation succeeds.
- *
- ****************************************************************************/
-
-static inline void __set_bit(int nr, uint8_t *addr)
-{
-  uint8_t mask = BIT_BYTE_MASK(nr);
-  uint8_t *p = ((FAR uint8_t *)addr) + BIT_BYTE(nr);
-  *p |= mask;
-}
-
-static inline void __clear_bit(int nr, uint8_t *addr)
-{
-  uint8_t mask = BIT_BYTE_MASK(nr);
-  uint8_t *p = ((FAR uint8_t *)addr) + BIT_BYTE(nr);
-  *p &= ~mask;
-}
-
-static inline int __test_bit(int nr, const volatile uint8_t *addr)
-{
-  return 1 & (addr[BIT_BYTE(nr)] >> (nr & (BITS_PER_BYTE - 1)));
-}
-
 /****************************************************************************
  * Name:  max7219_powerstring
  *
@@ -575,7 +544,7 @@ static int max7219_getrun(FAR struct lcd_dev_s *dev, fb_coord_t row,
 
   for (i = 0; i < pixlen; i++)
     {
-      if (__test_bit(col % 8 + i, ptr))
+      if (test_bit(col % 8 + i, ptr))
         {
           *buffer |= usrmask;
         }
diff --git a/drivers/lcd/memlcd.c b/drivers/lcd/memlcd.c
index e308632a8e..979e71f23a 100644
--- a/drivers/lcd/memlcd.c
+++ b/drivers/lcd/memlcd.c
@@ -232,37 +232,6 @@ static struct memlcd_dev_s g_memlcddev =
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * __set_bit - Set a bit in memory
- *
- * @nr: the bit to set
- * @addr: the address to start counting from
- *
- * This function is not atomic and may be reordered.  If it's called on the
- * same region of memory simultaneously, the effect may be that only one
- * operation succeeds.
- *
- ****************************************************************************/
-
-static inline void __set_bit(int nr, uint8_t *addr)
-{
-  uint8_t mask = BIT_BYTE_MASK(nr);
-  uint8_t *p = ((FAR uint8_t *)addr) + BIT_BYTE(nr);
-  *p |= mask;
-}
-
-static inline void __clear_bit(int nr, uint8_t *addr)
-{
-  uint8_t mask = BIT_BYTE_MASK(nr);
-  uint8_t *p = ((FAR uint8_t *)addr) + BIT_BYTE(nr);
-  *p &= ~mask;
-}
-
-static inline int __test_bit(int nr, FAR const volatile uint8_t *addr)
-{
-  return 1 & (addr[BIT_BYTE(nr)] >> (nr & (BITS_PER_BYTE - 1)));
-}
-
 /****************************************************************************
  * Name: memlcd_worker
  *
@@ -557,7 +526,7 @@ static int memlcd_getrun(FAR struct lcd_dev_s *dev,
   p = pfb + (col >> 3);
   for (i = 0; i < npixels; i++)
     {
-      if (__test_bit(col % 8 + i, p))
+      if (test_bit(col % 8 + i, p))
         {
           *buffer |= usrmask;
         }


(nuttx) 05/13: coresight:add stm device

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit e3e23f7eaf6a607473ccc33d56fe20790bff3557
Author: liaoao <li...@xiaomi.com>
AuthorDate: Thu Sep 21 10:32:42 2023 +0800

    coresight:add stm device
    
    Signed-off-by: liaoao <li...@xiaomi.com>
---
 drivers/coresight/CMakeLists.txt        |   4 +
 drivers/coresight/Kconfig               |   4 +
 drivers/coresight/Make.defs             |   4 +
 drivers/coresight/coresight_common.h    |   6 +
 drivers/coresight/coresight_stm.c       | 533 ++++++++++++++++++++++++++++++++
 include/nuttx/coresight/coresight.h     |   7 +
 include/nuttx/coresight/coresight_stm.h | 180 +++++++++++
 7 files changed, 738 insertions(+)

diff --git a/drivers/coresight/CMakeLists.txt b/drivers/coresight/CMakeLists.txt
index 9c3ca5a7a3..b2add74a8a 100644
--- a/drivers/coresight/CMakeLists.txt
+++ b/drivers/coresight/CMakeLists.txt
@@ -29,6 +29,10 @@ if(CONFIG_CORESIGHT)
     list(APPEND SRCS coresight_replicator.c)
   endif()
 
+  if(CONFIG_CORESIGHT_STM)
+    list(APPEND SRCS coresight_stm.c)
+  endif()
+
   if(CONFIG_CORESIGHT_TPIU)
     list(APPEND SRCS coresight_tpiu.c)
   endif()
diff --git a/drivers/coresight/Kconfig b/drivers/coresight/Kconfig
index be00792361..6dbff6c83c 100644
--- a/drivers/coresight/Kconfig
+++ b/drivers/coresight/Kconfig
@@ -27,6 +27,10 @@ config CORESIGHT_REPLICATOR
 	bool "Replicator coresight device support"
 	default n
 
+config CORESIGHT_STM
+	bool "STM coresight device support"
+	default n
+
 config CORESIGHT_TPIU
 	bool "TPIU coresight device support"
 	default n
diff --git a/drivers/coresight/Make.defs b/drivers/coresight/Make.defs
index a71e0ac72e..71723d5e6e 100644
--- a/drivers/coresight/Make.defs
+++ b/drivers/coresight/Make.defs
@@ -32,6 +32,10 @@ ifeq ($(CONFIG_CORESIGHT_REPLICATOR),y)
 CSRCS += coresight_replicator.c
 endif
 
+ifeq ($(CONFIG_CORESIGHT_STM),y)
+CSRCS += coresight_stm.c
+endif
+
 ifeq ($(CONFIG_CORESIGHT_TPIU),y)
 CSRCS += coresight_tpiu.c
 endif
diff --git a/drivers/coresight/coresight_common.h b/drivers/coresight/coresight_common.h
index 928c3a2b4b..e11eea5767 100644
--- a/drivers/coresight/coresight_common.h
+++ b/drivers/coresight/coresight_common.h
@@ -56,8 +56,14 @@
         (*(FAR volatile uint16_t *)(addr) = (val))
 #define coresight_put32(val, addr) \
         (*(FAR volatile uint32_t *)(addr) = (val))
+#define coresight_put64(val, addr) \
+        (*(FAR volatile uint64_t *)(addr) = (val))
+
 #define coresight_get32(addr) \
         (*(FAR volatile uint32_t *)(addr))
+#define coresight_modify32(val, mask, addr) \
+        coresight_put32((coresight_get32(addr) & ~(mask)) | \
+        ((val) & (mask)), (addr))
 
 /****************************************************************************
  * Public Function Prototypes
diff --git a/drivers/coresight/coresight_stm.c b/drivers/coresight/coresight_stm.c
new file mode 100644
index 0000000000..308d02ae08
--- /dev/null
+++ b/drivers/coresight/coresight_stm.c
@@ -0,0 +1,533 @@
+/****************************************************************************
+ * drivers/coresight/coresight_stm.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <errno.h>
+#include <debug.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/list.h>
+#include <nuttx/bits.h>
+
+#include <nuttx/coresight/coresight_stm.h>
+
+#include "coresight_common.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* STM registers */
+
+#define STM_DMASTARTR           0xc04
+#define STM_DMASTOPR            0xc08
+#define STM_DMASTATR            0xc0c
+#define STM_DMACTLR             0xc10
+#define STM_DMAIDR              0xcfc
+#define STM_HEER                0xd00
+#define STM_HETER               0xd20
+#define STM_HEBSR               0xd60
+#define STM_HEMCR               0xd64
+#define STM_HEMASTR             0xdf4
+#define STM_HEFEAT1R            0xdf8
+#define STM_HEIDR               0xdfc
+#define STM_SPER                0xe00
+#define STM_SPTER               0xe20
+#define STM_PRIVMASKR           0xe40
+#define STM_SPSCR               0xe60
+#define STM_SPMSCR              0xe64
+#define STM_SPOVERRIDER         0xe68
+#define STM_SPMOVERRIDER        0xe6c
+#define STM_SPTRIGCSR           0xe70
+#define STM_TCSR                0xe80
+#define STM_TSSTIMR             0xe84
+#define STM_TSFREQR             0xe8c
+#define STM_SYNCR               0xe90
+#define STM_AUXCR               0xe94
+#define STM_SPFEAT1R            0xea0
+#define STM_SPFEAT2R            0xea4
+#define STM_SPFEAT3R            0xea8
+#define STM_ITTRIGGER           0xee8
+#define STM_ITATBDATA0          0xeec
+#define STM_ITATBCTR2           0xef0
+#define STM_ITATBID             0xef4
+#define STM_ITATBCTR0           0xef8
+
+#define STM_DEFAULT_CHANNELS    32
+#define STM_BYTES_PER_CHANNEL   256
+
+/* Register bit definition */
+
+#define STM_EN                  BIT(0)
+#define STM_TIMESTAMPED_EN      BIT(1)
+#define STM_TCSR_BUSY           BIT(23)
+
+#define STM_ATBTRIGEN_DIR       BIT(4)
+
+#define STM_HE_EN               BIT(0)
+#define STM_HE_ERRDETECT_EN     BIT(2)
+
+/* Channel offset bit definition */
+
+#define STM_NO_TIMESTAMPED      BIT(3)
+#define STM_NO_MARKED           BIT(4)
+#define STM_NO_GUARANTEED       BIT(7)
+
+/* Bit operation */
+
+#define rounddown_pow_of_two(n) (1 << (fls(n) - 1))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* Address offset of different data type. */
+
+enum stm_pkt_type_e
+{
+  STM_PKT_TYPE_DATA = 0x00,
+  STM_PKT_TYPE_FLAG = 0x60,
+  STM_PKT_TYPE_TRIG = 0x70,
+};
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+static int stm_enable(FAR struct coresight_dev_s *csdev);
+static void stm_disable(FAR struct coresight_dev_s *csdev);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct coresight_source_ops_s g_stm_source_ops =
+{
+  .enable  = stm_enable,
+  .disable = stm_disable,
+};
+
+static const struct coresight_ops_s g_stm_ops =
+{
+  .source_ops = &g_stm_source_ops,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: stm_hw_disable
+ ****************************************************************************/
+
+static void stm_hw_disable(FAR struct coresight_stm_dev_s *stmdev)
+{
+  coresight_unlock(stmdev->csdev.addr);
+  coresight_modify32(0x0, STM_EN, stmdev->csdev.addr + STM_TCSR);
+  coresight_put32(0x0, stmdev->csdev.addr + STM_SPER);
+  coresight_put32(0x0, stmdev->csdev.addr + STM_SPTRIGCSR);
+
+  coresight_put32(0x0, stmdev->csdev.addr + STM_HEMCR);
+  coresight_put32(0x0, stmdev->csdev.addr + STM_HEER);
+  coresight_put32(0x0, stmdev->csdev.addr + STM_HETER);
+
+  if (coresight_timeout(stmdev->csdev.addr, STM_TCSR,
+                        STM_TCSR_BUSY, 0) < 0)
+    {
+      cserr("timeout waiting for STM stopped\n");
+    }
+
+  coresight_lock(stmdev->csdev.addr);
+  coresight_disclaim_device(stmdev->csdev.addr);
+}
+
+/****************************************************************************
+ * Name: stm_hw_enable
+ ****************************************************************************/
+
+static int stm_hw_enable(FAR struct coresight_stm_dev_s *stmdev)
+{
+  int ret;
+
+  ret = coresight_claim_device(stmdev->csdev.addr);
+  if (ret < 0)
+    {
+      cserr("stm %s claim failed\n", stmdev->csdev.name);
+      return ret;
+    }
+
+  coresight_unlock(stmdev->csdev.addr);
+  if (stmdev->stmheer != 0)
+    {
+      coresight_put32(stmdev->stmhebsr, stmdev->csdev.addr + STM_HEBSR);
+      coresight_put32(stmdev->stmheter, stmdev->csdev.addr + STM_HETER);
+      coresight_put32(stmdev->stmheer, stmdev->csdev.addr + STM_HEER);
+      coresight_put32(STM_HE_EN | STM_HE_ERRDETECT_EN,
+                      stmdev->csdev.addr + STM_HEMCR);
+    }
+
+  coresight_put32(STM_ATBTRIGEN_DIR, stmdev->csdev.addr + STM_SPTRIGCSR);
+  coresight_put32(stmdev->stmspscr, stmdev->csdev.addr + STM_SPSCR);
+  coresight_put32(stmdev->stmsper, stmdev->csdev.addr + STM_SPER);
+
+  /* 4096 byte between synchronisation packets */
+
+  coresight_put32(0xfff, stmdev->csdev.addr + STM_SYNCR);
+  coresight_put32((stmdev->traceid << 16) | STM_TIMESTAMPED_EN | STM_EN,
+                  stmdev->csdev.addr + STM_TCSR);
+
+  coresight_lock(stmdev->csdev.addr);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: stm_hw_disable
+ ****************************************************************************/
+
+static void stm_disable(FAR struct coresight_dev_s *csdev)
+{
+  FAR struct coresight_stm_dev_s *stmdev =
+    (FAR struct coresight_stm_dev_s *)csdev;
+
+  if (--stmdev->refcnt == 0)
+    {
+      stm_hw_disable(stmdev);
+      csinfo("%s disabled\n", csdev->name);
+    }
+}
+
+/****************************************************************************
+ * Name: stm_enable
+ ****************************************************************************/
+
+static int stm_enable(FAR struct coresight_dev_s *csdev)
+{
+  FAR struct coresight_stm_dev_s *stmdev =
+    (FAR struct coresight_stm_dev_s *)csdev;
+  int ret = 0;
+
+  if (stmdev->refcnt++ == 0)
+    {
+      ret = stm_hw_enable(stmdev);
+      if (ret < 0)
+        {
+          stmdev->refcnt--;
+          cserr("%s enabled failed\n", csdev->name);
+        }
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: stm_get_stimulus_port_num
+ ****************************************************************************/
+
+static uint32_t stm_get_stimulus_port_num(uintptr_t addr)
+{
+  uint32_t numsp;
+
+  coresight_unlock(addr);
+  numsp = coresight_get32(addr + CORESIGHT_DEVID);
+  if ((numsp & 0x1ffff) == 0)
+    {
+      numsp = STM_DEFAULT_CHANNELS;
+    }
+
+  coresight_lock(addr);
+  return numsp;
+}
+
+/****************************************************************************
+ * Name: stm_get_fundamental_data_size
+ ****************************************************************************/
+
+static uint8_t stm_get_fundamental_data_size(uintptr_t addr)
+{
+  uint32_t stmspfeat2r;
+
+  if (sizeof(uintptr_t) == 4)
+    {
+      return 4;
+    }
+
+  coresight_unlock(addr);
+  stmspfeat2r = coresight_get32(addr + STM_SPFEAT2R);
+  coresight_lock(addr);
+
+  return BMVAL(stmspfeat2r, 12, 15) ? 8 : 4;
+}
+
+/****************************************************************************
+ * Name: stm_init_default_data
+ ****************************************************************************/
+
+static void stm_init_default_data(FAR struct coresight_stm_dev_s *stmdev)
+{
+  /* Do not use port selection, and enable all channels. */
+
+  stmdev->stmspscr = 0x0;
+  stmdev->stmsper = ~0x0;
+
+  /* Disable hardware event tracing. */
+
+  stmdev->stmheer = 0x0;
+
+  /* Set invariant transaction timing on all channels. */
+
+  memset(stmdev->guaranteed, 0,
+         sizeof(unsigned long) * BITS_TO_LONGS(stmdev->numsp));
+}
+
+/****************************************************************************
+ * Name: stm_send
+ ****************************************************************************/
+
+static void stm_send(uintptr_t addr, const void *data,
+                     uint32_t size, uint8_t write_bytes)
+{
+  uint64_t payload[1];
+
+  /* Check data address whether write_bytes aligned. */
+
+  if ((unsigned long)data & (write_bytes - 1))
+    {
+      memcpy(payload, data, size);
+      data = payload;
+    }
+
+  switch (size)
+    {
+      case 8:
+        DEBUGASSERT(sizeof(uintptr_t) == 8);
+
+        coresight_put64(*(uint64_t *)data, addr);
+        break;
+
+      case 4:
+        coresight_put32(*(uint32_t *)data, addr);
+        break;
+
+      case 2:
+        coresight_put16(*(uint16_t *)data, addr);
+        break;
+
+      case 1:
+        coresight_put8(*(uint8_t *)data, addr);
+        break;
+
+      default:
+        break;
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: stm_set_channel_options
+ *
+ * Description:
+ *   Set a channel's trace mode.
+ *
+ * Input Parameters:
+ *   stmdev  - Pointer to STM device.
+ *   channel - Channels to configure.
+ *   options - If this channel's trace mode is guaranteed(blocking)
+ *             or invariant(noblocking).
+ *
+ * Returned Value:
+ *   Zero on success; a negative value on failure.
+ *
+ ****************************************************************************/
+
+int stm_set_channel_options(FAR struct coresight_stm_dev_s *stmdev,
+                            uint32_t channel, uint32_t options)
+{
+  if (channel >= stmdev->numsp)
+    {
+      return -EINVAL;
+    }
+
+  switch (options)
+    {
+      case STM_OPTION_GUARANTEED:
+        __set_bit(channel, stmdev->guaranteed);
+        break;
+
+      case STM_OPTION_INVARIANT:
+        __clear_bit(channel, stmdev->guaranteed);
+        break;
+
+      default:
+       return -EINVAL;
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: stm_sendpacket
+ *
+ * Description:
+ *   Write data to STM device.
+ *
+ * Input Parameters:
+ *   stmdev  - Pointer to STM device.
+ *   type    - Data type.
+ *   flag    - Data flags (or attributes).
+ *   channel - Channels this data from.
+ *   data    - Pointer to the data buffer.
+ *   size    - Data size.
+ *
+ * Returned Value:
+ *   Size of data written to STM device; a negative value on failure.
+ *
+ ****************************************************************************/
+
+ssize_t stm_sendpacket(FAR struct coresight_stm_dev_s *stmdev,
+                       enum stp_packet_type_e type,
+                       enum stp_packet_flags_e flag, uint32_t channel,
+                       FAR const void *data, size_t size)
+{
+  uintptr_t chaddr;
+  uint32_t offset;
+
+  if (channel >= stmdev->numsp)
+    {
+      return -EINVAL;
+    }
+
+  /* Aligned to STM fundamental data size. */
+
+  if (size > stmdev->write_bytes)
+    {
+      size = stmdev->write_bytes;
+    }
+  else
+    {
+      size = rounddown_pow_of_two(size);
+    }
+
+  /* Write data to corresponding channel offset according to data flags. */
+
+  chaddr = stmdev->stimulus_port_addr + channel * STM_BYTES_PER_CHANNEL;
+  offset = (flag & STP_PACKET_TIMESTAMPED) ? 0 : STM_NO_TIMESTAMPED;
+  offset |= test_bit(channel, stmdev->guaranteed) ? 0 : STM_NO_GUARANTEED;
+
+  switch (type)
+    {
+      case STP_PACKET_FLAG:
+        chaddr += offset | STM_PKT_TYPE_FLAG;
+        stm_send(chaddr, data, 1, stmdev->write_bytes);
+        size = 1;
+        break;
+
+      case STP_PACKET_DATA:
+        offset |= (flag & STP_PACKET_MARKED) ? 0 : STM_NO_MARKED;
+        chaddr += offset | STM_PKT_TYPE_DATA;
+        stm_send(chaddr, data, size, stmdev->write_bytes);
+        break;
+
+      default:
+        return -EOPNOTSUPP;
+    }
+
+  return size;
+}
+
+/****************************************************************************
+ * Name: stm_register
+ *
+ * Description:
+ *   Register a STM devices.
+ *
+ * Input Parameters:
+ *   desc  - A description of this coresight device.
+ *
+ * Returned Value:
+ *   Pointer to a STM device on success; NULL on failure.
+ *
+ ****************************************************************************/
+
+FAR struct coresight_stm_dev_s *
+stm_register(FAR const struct coresight_desc_s *desc)
+{
+  FAR struct coresight_stm_dev_s *stmdev;
+  FAR struct coresight_dev_s *csdev;
+  uint32_t numsp = stm_get_stimulus_port_num(desc->addr);
+  int ret;
+
+  stmdev = kmm_zalloc(sizeof(struct coresight_stm_dev_s) +
+                      sizeof(unsigned long) * BITS_TO_LONGS(numsp));
+  if (stmdev == NULL)
+    {
+      cserr("%s:malloc failed!\n", desc->name);
+      return NULL;
+    }
+
+  stmdev->numsp = numsp;
+  stmdev->write_bytes = stm_get_fundamental_data_size(desc->addr);
+  stmdev->stimulus_port_addr = desc->stimulus_port_addr;
+
+  stmdev->traceid = coresight_get_system_trace_id();
+  if (stmdev->traceid < 0)
+    {
+      kmm_free(stmdev);
+      cserr("%s:get unique traceid failed!\n", desc->name);
+      return NULL;
+    }
+
+  stm_init_default_data(stmdev);
+
+  csdev = &stmdev->csdev;
+  csdev->ops = &g_stm_ops;
+  ret = coresight_register(csdev, desc);
+  if (ret < 0)
+    {
+      coresight_put_system_trace_id(stmdev->traceid);
+      kmm_free(stmdev);
+      cserr("%s:register failed\n", desc->name);
+      return NULL;
+    }
+
+  return stmdev;
+}
+
+/****************************************************************************
+ * Name: stm_unregister
+ *
+ * Description:
+ *   Unregister a STM devices.
+ *
+ * Input Parameters:
+ *   stmdev  - Pointer to the STM device.
+ *
+ ****************************************************************************/
+
+void stm_unregister(FAR struct coresight_stm_dev_s *stmdev)
+{
+  coresight_unregister(&stmdev->csdev);
+  coresight_put_system_trace_id(stmdev->traceid);
+  kmm_free(stmdev);
+}
diff --git a/include/nuttx/coresight/coresight.h b/include/nuttx/coresight/coresight.h
index 987145d58f..f34fe5449b 100644
--- a/include/nuttx/coresight/coresight.h
+++ b/include/nuttx/coresight/coresight.h
@@ -126,6 +126,13 @@ struct coresight_desc_s
 
   int inport_num;
 
+  /* Used in STM device: start address of extend stimulus port, this memory
+   * should be reserved for stm use.
+   * Size of this prealloced memory equals to (256 x number of ports).
+   */
+
+  uintptr_t stimulus_port_addr;
+
   /* Description of outports of current device. */
 
   int outport_num;
diff --git a/include/nuttx/coresight/coresight_stm.h b/include/nuttx/coresight/coresight_stm.h
new file mode 100644
index 0000000000..0848b2468a
--- /dev/null
+++ b/include/nuttx/coresight/coresight_stm.h
@@ -0,0 +1,180 @@
+/****************************************************************************
+ * include/nuttx/coresight/coresight_stm.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_CORESIGHT_CORESIGHT_STM_H
+#define __INCLUDE_NUTTX_CORESIGHT_CORESIGHT_STM_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/coresight/coresight.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+enum stm_trans_type_e
+{
+  STM_OPTION_GUARANTEED = 0,  /* Blocking, may stall the bus when fifio is full. */
+  STM_OPTION_INVARIANT,       /* No-blocking, may lost trace data when fifo is full. */
+};
+
+/* enum stp_packet_type - STP packets that an STM driver sends. */
+
+enum stp_packet_type_e
+{
+  STP_PACKET_DATA = 0,
+  STP_PACKET_FLAG,
+  STP_PACKET_USER,
+  STP_PACKET_MERR,
+  STP_PACKET_GERR,
+  STP_PACKET_TRIG,
+  STP_PACKET_XSYNC,
+};
+
+enum stp_packet_flags_e
+{
+  STP_PACKET_MARKED = 0x1,
+  STP_PACKET_TIMESTAMPED  = 0x2,
+};
+
+struct coresight_stm_dev_s
+{
+  struct coresight_dev_s csdev;
+  uint8_t refcnt;
+  int traceid;                       /* Trace id. */
+
+  /* Used in STM device: start address of extend stimulus port, this memory
+   * should be reserved for stm use.
+   * Size of this reserved memory equals to (256 x number of ports).
+   */
+
+  uintptr_t stimulus_port_addr;
+
+  /* The total number of stimulus port support by this STM. */
+
+  uint32_t numsp;
+
+  /* Maximus bytes this STM can write at a time. */
+
+  uint8_t write_bytes;
+
+  /* Port enable and select register. */
+
+  uint32_t stmsper;
+  uint32_t stmspscr;
+
+  /* Hardware Event Tracing. */
+
+  uint32_t stmheer;
+  uint32_t stmheter;
+  uint32_t stmhebsr;
+
+  /* Bitmap used to indicate whether corresponding stimulus port is
+   * guaranteed transactions or invariant timing transactions.
+   */
+
+  unsigned long guaranteed[0];
+};
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: stm_set_channel_options
+ *
+ * Description:
+ *   Set a channel's trace mode.
+ *
+ * Input Parameters:
+ *   stmdev  - Pointer to STM device.
+ *   channel - Channels to configure.
+ *   options - If this channel's trace mode is guaranteed(blocking)
+ *             or invariant(noblocking).
+ *
+ * Returned Value:
+ *   Zero on success; a negative value on failure.
+ *
+ ****************************************************************************/
+
+int stm_set_channel_options(FAR struct coresight_stm_dev_s *stmdev,
+                            uint32_t channel, uint32_t options);
+
+/****************************************************************************
+ * Name: stm_sendpacket
+ *
+ * Description:
+ *   Write data to STM device.
+ *
+ * Input Parameters:
+ *   stmdev  - Pointer to STM device.
+ *   type    - Data type.
+ *   flag    - Data flags (or attributes).
+ *   channel - Channels this data from.
+ *   data    - Pointer to the data buffer.
+ *   size    - Data size.
+ *
+ * Returned Value:
+ *   Size of data written to STM device; a negative value on failure.
+ *
+ ****************************************************************************/
+
+ssize_t stm_sendpacket(FAR struct coresight_stm_dev_s *stmdev,
+                       enum stp_packet_type_e type,
+                       enum stp_packet_flags_e flag, uint32_t channel,
+                       FAR const void *data, size_t size);
+
+/****************************************************************************
+ * Name: stm_register
+ *
+ * Description:
+ *   Register a STM devices.
+ *
+ * Input Parameters:
+ *   desc  - A description of this coresight device.
+ *
+ * Returned Value:
+ *   Pointer to a STM device on success; NULL on failure.
+ *
+ ****************************************************************************/
+
+FAR struct coresight_stm_dev_s *
+stm_register(FAR const struct coresight_desc_s *desc);
+
+/****************************************************************************
+ * Name: stm_unregister
+ *
+ * Description:
+ *   Unregister a STM devices.
+ *
+ * Input Parameters:
+ *   stmdev  - Pointer to the STM device.
+ *
+ ****************************************************************************/
+
+void stm_unregister(FAR struct coresight_stm_dev_s *stmdev);
+
+#endif  //__INCLUDE_NUTTX_CORESIGHT_CORESIGHT_STM_H


(nuttx) 09/13: coresight: add clk control for coresight system

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 8a57dafdcf1a66cd83b6c350db40b1b8c556c9f4
Author: liaoao <li...@xiaomi.com>
AuthorDate: Wed Dec 27 21:42:51 2023 +0800

    coresight: add clk control for coresight system
    
    Signed-off-by: liaoao <li...@xiaomi.com>
---
 drivers/coresight/coresight_core.c       | 247 ++++++++++++++++++++++++++++---
 drivers/coresight/coresight_etb.c        |  28 ++--
 drivers/coresight/coresight_etm3.c       |  36 ++---
 drivers/coresight/coresight_stm.c        |  33 +----
 drivers/coresight/coresight_tmc_etf.c    |  70 ++++-----
 drivers/coresight/coresight_tmc_etr.c    |  38 ++---
 drivers/coresight/coresight_tpiu.c       |  33 +----
 include/nuttx/coresight/coresight.h      |  15 ++
 include/nuttx/coresight/coresight_etb.h  |   1 -
 include/nuttx/coresight/coresight_etm.h  |   1 -
 include/nuttx/coresight/coresight_stm.h  |   1 -
 include/nuttx/coresight/coresight_tmc.h  |   1 -
 include/nuttx/coresight/coresight_tpiu.h |   1 -
 13 files changed, 312 insertions(+), 193 deletions(-)

diff --git a/drivers/coresight/coresight_core.c b/drivers/coresight/coresight_core.c
index 54e6f3e7c8..5cd889937d 100644
--- a/drivers/coresight/coresight_core.c
+++ b/drivers/coresight/coresight_core.c
@@ -56,20 +56,151 @@ static struct list_node g_csdev_list = LIST_INITIAL_VALUE(g_csdev_list);
  * Private Functions
  ****************************************************************************/
 
+#ifdef CONFIG_PM
+
+/****************************************************************************
+ * Name: coresight_notify_pm
+ ****************************************************************************/
+
+static void coresight_notify_pm(struct pm_callback_s *cb, int domain,
+                                enum pm_state_e pmstate)
+{
+  FAR struct coresight_dev_s *csdev =
+    container_of(cb, struct coresight_dev_s, pmcb);
+  enum pm_state_e oldstate;
+
+  if (csdev->refcnt == 0 || domain != PM_IDLE_DOMAIN)
+    {
+      return;
+    }
+
+  oldstate = pm_querystate(PM_IDLE_DOMAIN);
+  switch (oldstate)
+    {
+      case PM_NORMAL:
+      case PM_IDLE:
+      case PM_STANDBY:
+        if (pmstate == PM_SLEEP)
+          {
+            clk_disable(csdev->clk);
+          }
+        break;
+
+      case PM_SLEEP:
+        if (pmstate == PM_NORMAL || pmstate == PM_IDLE ||
+            pmstate == PM_STANDBY)
+          {
+            if (clk_enable(csdev->clk) <= 0)
+              {
+                cserr("clk enable failed when pm state change\n");
+              }
+          }
+        break;
+
+      default:
+        break;
+    }
+}
+
+#endif
+
+#ifdef CONFIG_CLK
+
+/****************************************************************************
+ * Name: coresight_enable_clk
+ ****************************************************************************/
+
+static int coresight_enable_clk(FAR struct coresight_dev_s *csdev)
+{
+  int ret;
+
+  if (csdev->clk == NULL)
+    {
+      return 0;
+    }
+
+  ret = clk_enable(csdev->clk);
+  if (ret < 0)
+    {
+      cserr("%s clk enable failed\n", csdev->name);
+      return ret;
+    }
+
+#ifdef CONFIG_PM
+  if (csdev->pmcb.notify == NULL)
+    {
+      csdev->pmcb.notify = coresight_notify_pm;
+    }
+
+  ret = pm_register(&csdev->pmcb);
+  if (ret < 0)
+    {
+      clk_disable(csdev->clk);
+      cserr("%s register pm failed\n", csdev->name);
+      return ret;
+    }
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: coresight_disable_clk
+ ****************************************************************************/
+
+static void coresight_disable_clk(FAR struct coresight_dev_s *csdev)
+{
+  if (csdev->clk == NULL)
+    {
+      return;
+    }
+
+#ifdef CONFIG_PM
+  pm_unregister(&csdev->pmcb);
+#endif
+  clk_disable(csdev->clk);
+}
+
+#else
+#  define coresight_enable_clk(csdev) (0)
+#  define coresight_disable_clk(csdev)
+#endif
+
 /****************************************************************************
  * Name: coresight_enable_sink
  ****************************************************************************/
 
 static int coresight_enable_sink(FAR struct coresight_dev_s *csdev)
 {
-  if (csdev->ops->sink_ops->enable != NULL)
+  int ret;
+
+  if (csdev->ops->sink_ops->enable == NULL)
     {
-      return csdev->ops->sink_ops->enable(csdev);
+      return -EINVAL;
     }
-  else
+
+  if (csdev->refcnt++ != 0)
     {
-      return -EINVAL;
+      return 0;
     }
+
+  ret = coresight_enable_clk(csdev);
+  if (ret < 0)
+    {
+      csdev->refcnt--;
+      return ret;
+    }
+
+  ret = csdev->ops->sink_ops->enable(csdev);
+  if (ret >= 0)
+    {
+      return ret;
+    }
+
+  csdev->refcnt--;
+  coresight_disable_clk(csdev);
+  cserr("%s enable failed\n", csdev->name);
+  return ret;
 }
 
 /****************************************************************************
@@ -78,10 +209,18 @@ static int coresight_enable_sink(FAR struct coresight_dev_s *csdev)
 
 static void coresight_disable_sink(FAR struct coresight_dev_s *csdev)
 {
-  if (csdev->ops->sink_ops->disable != NULL)
+  if (csdev->ops->sink_ops->disable == NULL)
+    {
+      return;
+    }
+
+  if (--csdev->refcnt != 0)
     {
-      csdev->ops->sink_ops->disable(csdev);
+      return;
     }
+
+  csdev->ops->sink_ops->disable(csdev);
+  coresight_disable_clk(csdev);
 }
 
 /****************************************************************************
@@ -138,6 +277,7 @@ static int coresight_enable_link(FAR struct coresight_dev_s *csdev,
 {
   int inport = 0;
   int outport = 0;
+  int ret;
 
   if (csdev->ops->link_ops->enable == NULL)
     {
@@ -162,7 +302,28 @@ static int coresight_enable_link(FAR struct coresight_dev_s *csdev,
         }
     }
 
-  return csdev->ops->link_ops->enable(csdev, inport, outport);
+  if (csdev->refcnt++ == 0)
+    {
+      ret = coresight_enable_clk(csdev);
+      if (ret < 0)
+        {
+          csdev->refcnt--;
+          return ret;
+        }
+    }
+
+  ret = csdev->ops->link_ops->enable(csdev, inport, outport);
+  if (ret < 0)
+    {
+      if (--csdev->refcnt == 0)
+        {
+          coresight_disable_clk(csdev);
+        }
+
+      return ret;
+    }
+
+  return ret;
 }
 
 /****************************************************************************
@@ -173,11 +334,21 @@ static void coresight_disable_link(FAR struct coresight_dev_s *csdev,
                                    FAR struct coresight_dev_s *prev,
                                    FAR struct coresight_dev_s *next)
 {
-  if (csdev->ops->sink_ops->disable != NULL)
+  int inport;
+  int outport;
+
+  if (csdev->ops->sink_ops->disable == NULL)
+    {
+      return;
+    }
+
+  inport = coresight_find_link_inport(csdev, prev);
+  outport = coresight_find_link_outport(csdev, next);
+  csdev->ops->link_ops->disable(csdev, inport, outport);
+
+  if (--csdev->refcnt == 0)
     {
-      int inport = coresight_find_link_inport(csdev, prev);
-      int outport = coresight_find_link_outport(csdev, next);
-      csdev->ops->link_ops->disable(csdev, inport, outport);
+      coresight_disable_clk(csdev);
     }
 }
 
@@ -187,14 +358,35 @@ static void coresight_disable_link(FAR struct coresight_dev_s *csdev,
 
 static int coresight_enable_source(FAR struct coresight_dev_s *csdev)
 {
-  if (csdev->ops->source_ops->enable != NULL)
+  int ret;
+
+  if (csdev->ops->source_ops->enable == NULL)
+    {
+      return -EINVAL;
+    }
+
+  if (csdev->refcnt++ != 0)
     {
-      return csdev->ops->source_ops->enable(csdev);
+      return 0;
     }
-  else
+
+  ret = coresight_enable_clk(csdev);
+  if (ret < 0)
     {
-      return -EINVAL;
+      csdev->refcnt--;
+      return ret;
     }
+
+  ret = csdev->ops->source_ops->enable(csdev);
+  if (ret >= 0)
+    {
+      return ret;
+    }
+
+  csdev->refcnt--;
+  coresight_disable_clk(csdev);
+  cserr("%s enable failed\n", csdev->name);
+  return ret;
 }
 
 /****************************************************************************
@@ -203,10 +395,18 @@ static int coresight_enable_source(FAR struct coresight_dev_s *csdev)
 
 static void coresight_disable_source(FAR struct coresight_dev_s *csdev)
 {
-  if (csdev->ops->source_ops->disable != NULL)
+  if (csdev->ops->source_ops->disable == NULL)
+    {
+      return;
+    }
+
+  if (--csdev->refcnt != 0)
     {
-      csdev->ops->source_ops->disable(csdev);
+      return;
     }
+
+  csdev->ops->source_ops->disable(csdev);
+  coresight_disable_clk(csdev);
 }
 
 /****************************************************************************
@@ -494,6 +694,18 @@ int coresight_register(FAR struct coresight_dev_s *csdev,
   csdev->outport_num = desc->outport_num;
   list_initialize(&csdev->path);
 
+#ifdef CONFIG_CLK
+  if (desc->clkname != NULL)
+    {
+      csdev->clk = clk_get(desc->clkname);
+      if (csdev->clk == NULL)
+        {
+          cserr("get device clk failed\n");
+          return -ENODEV;
+        }
+    }
+#endif
+
   if (csdev->outport_num > 0)
     {
       csdev->outconns =
@@ -684,4 +896,3 @@ void coresight_disable(FAR struct coresight_dev_s *srcdev)
 
   leave_critical_section(flags);
 }
-
diff --git a/drivers/coresight/coresight_etb.c b/drivers/coresight/coresight_etb.c
index 859d84a4d2..360161b6f1 100644
--- a/drivers/coresight/coresight_etb.c
+++ b/drivers/coresight/coresight_etb.c
@@ -272,21 +272,15 @@ static int etb_enable(FAR struct coresight_dev_s *csdev)
 {
   FAR struct coresight_etb_dev_s *etbdev =
     (FAR struct coresight_etb_dev_s *)csdev;
-  int ret = 0;
+  int ret;
 
-  if (etbdev->refcnt++ == 0)
+  ret = coresight_claim_device(etbdev->csdev.addr);
+  if (ret < 0)
     {
-      ret = coresight_claim_device(etbdev->csdev.addr);
-      if (ret < 0)
-        {
-          etbdev->refcnt--;
-          cserr("%s enable failed\n", csdev->name);
-          return ret;
-        }
-
-      etb_hw_enable(etbdev);
+      return ret;
     }
 
+  etb_hw_enable(etbdev);
   return ret;
 }
 
@@ -299,12 +293,8 @@ static void etb_disable(FAR struct coresight_dev_s *csdev)
   FAR struct coresight_etb_dev_s *etbdev =
     (FAR struct coresight_etb_dev_s *)csdev;
 
-  if (--etbdev->refcnt == 0)
-    {
-      etb_hw_disable(etbdev);
-      coresight_disclaim_device(etbdev->csdev.addr);
-      csinfo("%s disabled\n", csdev->name);
-    }
+  etb_hw_disable(etbdev);
+  coresight_disclaim_device(etbdev->csdev.addr);
 }
 
 /****************************************************************************
@@ -342,13 +332,13 @@ static int etb_open(FAR struct file *filep)
           irqstate_t flags;
 
           flags = enter_critical_section();
-          if (etbdev->refcnt > 0)
+          if (etbdev->csdev.refcnt > 0)
             {
               etb_hw_disable(etbdev);
             }
 
           etb_hw_read(etbdev);
-          if (etbdev->refcnt > 0)
+          if (etbdev->csdev.refcnt > 0)
             {
               etb_hw_enable(etbdev);
             }
diff --git a/drivers/coresight/coresight_etm3.c b/drivers/coresight/coresight_etm3.c
index 03bf424fbc..db72d04876 100644
--- a/drivers/coresight/coresight_etm3.c
+++ b/drivers/coresight/coresight_etm3.c
@@ -436,7 +436,7 @@ static bool etm_arch_supported(uint8_t arch)
  *
  ****************************************************************************/
 
-void etm_set_default(struct etm_config_s *config)
+static void etm_set_default(struct etm_config_s *config)
 {
   int i;
 
@@ -470,19 +470,12 @@ void etm_set_default(struct etm_config_s *config)
  * Name: etm_hw_enable
  ****************************************************************************/
 
-static int etm_hw_enable(FAR struct coresight_etm_dev_s *etmdev)
+static void etm_hw_enable(FAR struct coresight_etm_dev_s *etmdev)
 {
   FAR struct etm_config_s *config = &etmdev->cfg;
   uint32_t etmcr;
-  int ret;
   int i;
 
-  ret = coresight_claim_device(etmdev->csdev.addr);
-  if (ret < 0)
-    {
-      return ret;
-    }
-
   coresight_unlock(etmdev->csdev.addr);
   etm_os_unlock(etmdev);
   etm_set_pwrup(etmdev);
@@ -549,8 +542,6 @@ static int etm_hw_enable(FAR struct coresight_etm_dev_s *etmdev)
 
   etm_clr_program(etmdev);
   coresight_lock(etmdev->csdev.addr);
-
-  return ret;
 }
 
 /****************************************************************************
@@ -575,8 +566,6 @@ static void etm_hw_disable(FAR struct coresight_etm_dev_s *etmdev)
 
   etm_set_pwrdwn(etmdev);
   coresight_lock(etmdev->csdev.addr);
-
-  coresight_disclaim_device(etmdev->csdev.addr);
 }
 
 /****************************************************************************
@@ -587,18 +576,15 @@ static int etm_enable(FAR struct coresight_dev_s *csdev)
 {
   FAR struct coresight_etm_dev_s *etmdev =
     (FAR struct coresight_etm_dev_s *)csdev;
-  int ret = 0;
+  int ret;
 
-  if (etmdev->refcnt++ == 0)
+  ret = coresight_claim_device(etmdev->csdev.addr);
+  if (ret < 0)
     {
-      ret = etm_hw_enable(etmdev);
-      if (ret < 0)
-        {
-          etmdev->refcnt--;
-          cserr("%s enabled\n", csdev->name);
-        }
+      return ret;
     }
 
+  etm_hw_enable(etmdev);
   return ret;
 }
 
@@ -611,11 +597,8 @@ static void etm_disable(FAR struct coresight_dev_s *csdev)
   FAR struct coresight_etm_dev_s *etmdev =
     (FAR struct coresight_etm_dev_s *)csdev;
 
-  if (--etmdev->refcnt == 0)
-    {
-      etm_hw_disable(etmdev);
-      csinfo("etm %s disabled\n", csdev->name);
-    }
+  etm_hw_disable(etmdev);
+  coresight_disclaim_device(etmdev->csdev.addr);
 }
 
 /****************************************************************************
@@ -692,7 +675,6 @@ etm_register(FAR const struct coresight_desc_s *desc)
 
   etmdev->cpu = desc->cpu;
   etmdev->csdev.addr = desc->addr;
-
   etm_init_arch_data(etmdev);
 
   if (!etm_arch_supported(etmdev->arch))
diff --git a/drivers/coresight/coresight_stm.c b/drivers/coresight/coresight_stm.c
index c64eaf048d..4bb586b212 100644
--- a/drivers/coresight/coresight_stm.c
+++ b/drivers/coresight/coresight_stm.c
@@ -157,24 +157,14 @@ static void stm_hw_disable(FAR struct coresight_stm_dev_s *stmdev)
     }
 
   coresight_lock(stmdev->csdev.addr);
-  coresight_disclaim_device(stmdev->csdev.addr);
 }
 
 /****************************************************************************
  * Name: stm_hw_enable
  ****************************************************************************/
 
-static int stm_hw_enable(FAR struct coresight_stm_dev_s *stmdev)
+static void stm_hw_enable(FAR struct coresight_stm_dev_s *stmdev)
 {
-  int ret;
-
-  ret = coresight_claim_device(stmdev->csdev.addr);
-  if (ret < 0)
-    {
-      cserr("stm %s claim failed\n", stmdev->csdev.name);
-      return ret;
-    }
-
   coresight_unlock(stmdev->csdev.addr);
   if (stmdev->stmheer != 0)
     {
@@ -196,7 +186,6 @@ static int stm_hw_enable(FAR struct coresight_stm_dev_s *stmdev)
                   stmdev->csdev.addr + STM_TCSR);
 
   coresight_lock(stmdev->csdev.addr);
-  return ret;
 }
 
 /****************************************************************************
@@ -208,11 +197,8 @@ static void stm_disable(FAR struct coresight_dev_s *csdev)
   FAR struct coresight_stm_dev_s *stmdev =
     (FAR struct coresight_stm_dev_s *)csdev;
 
-  if (--stmdev->refcnt == 0)
-    {
-      stm_hw_disable(stmdev);
-      csinfo("%s disabled\n", csdev->name);
-    }
+  stm_hw_disable(stmdev);
+  coresight_disclaim_device(stmdev->csdev.addr);
 }
 
 /****************************************************************************
@@ -223,18 +209,15 @@ static int stm_enable(FAR struct coresight_dev_s *csdev)
 {
   FAR struct coresight_stm_dev_s *stmdev =
     (FAR struct coresight_stm_dev_s *)csdev;
-  int ret = 0;
+  int ret;
 
-  if (stmdev->refcnt++ == 0)
+  ret = coresight_claim_device(stmdev->csdev.addr);
+  if (ret < 0)
     {
-      ret = stm_hw_enable(stmdev);
-      if (ret < 0)
-        {
-          stmdev->refcnt--;
-          cserr("%s enabled failed\n", csdev->name);
-        }
+      return ret;
     }
 
+  stm_hw_enable(stmdev);
   return ret;
 }
 
diff --git a/drivers/coresight/coresight_tmc_etf.c b/drivers/coresight/coresight_tmc_etf.c
index 1a68559835..c494c2f34d 100644
--- a/drivers/coresight/coresight_tmc_etf.c
+++ b/drivers/coresight/coresight_tmc_etf.c
@@ -277,25 +277,19 @@ static int tmc_etf_sink_enable(FAR struct coresight_dev_s *csdev)
 {
   FAR struct coresight_tmc_dev_s *tmcdev =
     (FAR struct coresight_tmc_dev_s *)csdev;
-  int ret = 0;
+  int ret;
 
-  if (tmcdev->refcnt++ == 0)
+  ret = coresight_claim_device(tmcdev->csdev.addr);
+  if (ret < 0)
     {
-      ret = coresight_claim_device(tmcdev->csdev.addr);
-      if (ret < 0)
-        {
-          tmcdev->refcnt--;
-          cserr("%s claimed failed\n", csdev->name);
-          return ret;
-        }
+      cserr("%s claimed failed\n", csdev->name);
+      return ret;
+    }
 
-      ret = tmc_etf_sink_hw_enable(tmcdev);
-      if (ret < 0)
-        {
-          tmcdev->refcnt--;
-          coresight_disclaim_device(tmcdev->csdev.addr);
-          cserr("%s enable failed\n", csdev->name);
-        }
+  ret = tmc_etf_sink_hw_enable(tmcdev);
+  if (ret < 0)
+    {
+      coresight_disclaim_device(tmcdev->csdev.addr);
     }
 
   return ret;
@@ -310,12 +304,8 @@ static void tmc_etf_sink_disable(FAR struct coresight_dev_s *csdev)
   FAR struct coresight_tmc_dev_s *tmcdev =
     (FAR struct coresight_tmc_dev_s *)csdev;
 
-  if (--tmcdev->refcnt == 0)
-    {
-      tmc_etf_hw_disable(tmcdev);
-      coresight_disclaim_device(tmcdev->csdev.addr);
-      csinfo("%s disabled\n", csdev->name);
-    }
+  tmc_etf_hw_disable(tmcdev);
+  coresight_disclaim_device(tmcdev->csdev.addr);
 }
 
 /****************************************************************************
@@ -327,25 +317,19 @@ static int tmc_etf_link_enable(FAR struct coresight_dev_s *csdev,
 {
   FAR struct coresight_tmc_dev_s *tmcdev =
     (FAR struct coresight_tmc_dev_s *)csdev;
-  int ret = 0;
+  int ret;
 
-  if (tmcdev->refcnt++ == 0)
+  ret = coresight_claim_device(tmcdev->csdev.addr);
+  if (ret < 0)
     {
-      ret = coresight_claim_device(tmcdev->csdev.addr);
-      if (ret < 0)
-        {
-          tmcdev->refcnt--;
-          cserr("%s claimed failed\n", csdev->name);
-          return ret;
-        }
+      cserr("%s claimed failed\n", csdev->name);
+      return ret;
+    }
 
-      ret = tmc_etf_link_hw_enable(tmcdev);
-      if (ret < 0)
-        {
-          tmcdev->refcnt--;
-          coresight_disclaim_device(tmcdev->csdev.addr);
-          cserr("%s enable failed\n", csdev->name);
-        }
+  ret = tmc_etf_link_hw_enable(tmcdev);
+  if (ret < 0)
+    {
+      coresight_disclaim_device(tmcdev->csdev.addr);
     }
 
   return ret;
@@ -361,12 +345,8 @@ static void tmc_etf_link_disable(FAR struct coresight_dev_s *csdev,
   FAR struct coresight_tmc_dev_s *tmcdev =
     (FAR struct coresight_tmc_dev_s *)csdev;
 
-  if (--tmcdev->refcnt == 0)
-    {
-      tmc_etf_hw_disable(tmcdev);
-      coresight_disclaim_device(tmcdev->csdev.addr);
-      csinfo("%s disabled\n", csdev->name);
-    }
+  tmc_etf_hw_disable(tmcdev);
+  coresight_disclaim_device(tmcdev->csdev.addr);
 }
 
 /****************************************************************************
@@ -402,7 +382,7 @@ static int tmc_etf_open(FAR struct file *filep)
           irqstate_t flags;
 
           flags = enter_critical_section();
-          if (tmcdev->refcnt > 0)
+          if (tmcdev->csdev.refcnt > 0)
             {
               tmc_etf_hw_disable_and_read(tmcdev);
               tmc_etf_sink_hw_enable(tmcdev);
diff --git a/drivers/coresight/coresight_tmc_etr.c b/drivers/coresight/coresight_tmc_etr.c
index 37ea6d23df..2643621684 100644
--- a/drivers/coresight/coresight_tmc_etr.c
+++ b/drivers/coresight/coresight_tmc_etr.c
@@ -270,25 +270,19 @@ static int tmc_etr_enable(FAR struct coresight_dev_s *csdev)
 {
   FAR struct coresight_tmc_dev_s *tmcdev =
     (FAR struct coresight_tmc_dev_s *)csdev;
-  int ret = 0;
+  int ret;
 
-  if (tmcdev->refcnt++ == 0)
+  ret = coresight_claim_device(tmcdev->csdev.addr);
+  if (ret < 0)
     {
-      ret = coresight_claim_device(tmcdev->csdev.addr);
-      if (ret < 0)
-        {
-          tmcdev->refcnt--;
-          cserr("%s claimed failed\n", csdev->name);
-          return ret;
-        }
+      cserr("%s claimed failed\n", csdev->name);
+      return ret;
+    }
 
-      ret = tmc_etr_hw_enable(tmcdev);
-      if (ret < 0)
-        {
-          tmcdev->refcnt--;
-          coresight_disclaim_device(tmcdev->csdev.addr);
-          cserr("%s enabled failed\n", csdev->name);
-        }
+  ret = tmc_etr_hw_enable(tmcdev);
+  if (ret < 0)
+    {
+      coresight_disclaim_device(tmcdev->csdev.addr);
     }
 
   return ret;
@@ -303,12 +297,8 @@ static void tmc_etr_disable(FAR struct coresight_dev_s *csdev)
   FAR struct coresight_tmc_dev_s *tmcdev =
     (FAR struct coresight_tmc_dev_s *)csdev;
 
-  if (--tmcdev->refcnt == 0)
-    {
-      tmc_etr_hw_disable(tmcdev);
-      coresight_disclaim_device(tmcdev->csdev.addr);
-      csinfo("%s disabled\n", csdev->name);
-    }
+  tmc_etr_hw_disable(tmcdev);
+  coresight_disclaim_device(tmcdev->csdev.addr);
 }
 
 /****************************************************************************
@@ -340,7 +330,7 @@ static int tmc_etr_open(FAR struct file *filep)
       irqstate_t flags;
 
       flags = enter_critical_section();
-      if (tmcdev->refcnt > 0)
+      if (tmcdev->csdev.refcnt > 0)
         {
           tmc_etr_hw_disable_and_read(tmcdev);
         }
@@ -387,7 +377,7 @@ static int tmc_etr_close(FAR struct file *filep)
       irqstate_t flags;
 
       flags = enter_critical_section();
-      if (tmcdev->refcnt > 0)
+      if (tmcdev->csdev.refcnt > 0)
         {
           if (tmc_etr_hw_enable(tmcdev) < 0)
             {
diff --git a/drivers/coresight/coresight_tpiu.c b/drivers/coresight/coresight_tpiu.c
index 907cfb0958..2c688e8c7a 100644
--- a/drivers/coresight/coresight_tpiu.c
+++ b/drivers/coresight/coresight_tpiu.c
@@ -92,15 +92,6 @@ static const struct coresight_ops_s g_tpiu_ops =
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: tpiu_hw_enable
- ****************************************************************************/
-
-static int tpiu_hw_enable(FAR struct coresight_tpiu_dev_s *tpiudev)
-{
-  return coresight_claim_device(tpiudev->csdev.addr);
-}
-
 /****************************************************************************
  * Name: tpiu_hw_disable
  ****************************************************************************/
@@ -128,7 +119,6 @@ static void tpiu_hw_disable(FAR struct coresight_tpiu_dev_s *tpiudev)
     }
 
   coresight_lock(tpiudev->csdev.addr);
-  coresight_disclaim_device(tpiudev->csdev.addr);
 }
 
 /****************************************************************************
@@ -137,21 +127,7 @@ static void tpiu_hw_disable(FAR struct coresight_tpiu_dev_s *tpiudev)
 
 static int tpiu_enable(FAR struct coresight_dev_s *csdev)
 {
-  FAR struct coresight_tpiu_dev_s *tpiudev =
-    (FAR struct coresight_tpiu_dev_s *)csdev;
-  int ret = 0;
-
-  if (tpiudev->refcnt++ == 0)
-    {
-      ret = tpiu_hw_enable(tpiudev);
-      if (ret < 0)
-        {
-          tpiudev->refcnt--;
-          cserr("tpiu %s enabled failed\n", csdev->name);
-        }
-    }
-
-  return ret;
+  return coresight_claim_device(csdev->addr);
 }
 
 /****************************************************************************
@@ -163,11 +139,8 @@ static void tpiu_disable(FAR struct coresight_dev_s *csdev)
   FAR struct coresight_tpiu_dev_s *tpiudev =
     (FAR struct coresight_tpiu_dev_s *)csdev;
 
-  if (--tpiudev->refcnt == 0)
-    {
-      tpiu_hw_disable(tpiudev);
-      csinfo("tpiu %s disabled\n", csdev->name);
-    }
+  tpiu_hw_disable(tpiudev);
+  coresight_disclaim_device(tpiudev->csdev.addr);
 }
 
 /****************************************************************************
diff --git a/include/nuttx/coresight/coresight.h b/include/nuttx/coresight/coresight.h
index 9261cafd57..d0e14ddb76 100644
--- a/include/nuttx/coresight/coresight.h
+++ b/include/nuttx/coresight/coresight.h
@@ -26,7 +26,9 @@
  ****************************************************************************/
 
 #include <stdint.h>
+#include <nuttx/clk/clk.h>
 #include <nuttx/list.h>
+#include <nuttx/power/pm.h>
 
 /****************************************************************************
  * Pre-processor Definitions
@@ -121,6 +123,9 @@ struct coresight_portdesc_s
 struct coresight_desc_s
 {
   FAR const char *name;
+#ifdef CONFIG_CLK
+  FAR const char *clkname;
+#endif
   uintptr_t addr;
   enum coresight_dev_type_e type;
   union coresight_dev_subtype_u subtype;
@@ -168,6 +173,16 @@ struct coresight_connect_s
 struct coresight_dev_s
 {
   FAR const char *name;
+#ifdef CONFIG_CLK
+  FAR struct clk_s *clk;
+#endif
+#ifdef CONFIG_PM
+  struct pm_callback_s pmcb;
+#endif
+
+  /* Coresight device's enable count. */
+
+  uint8_t refcnt;
 
   /* Memory-mapped base address of current coresight device. */
 
diff --git a/include/nuttx/coresight/coresight_etb.h b/include/nuttx/coresight/coresight_etb.h
index 21c15f7dcb..a6ed913b54 100644
--- a/include/nuttx/coresight/coresight_etb.h
+++ b/include/nuttx/coresight/coresight_etb.h
@@ -39,7 +39,6 @@ struct coresight_etb_dev_s
   uint32_t buffer_depth;             /* ETB buffer depth. */
   FAR uint32_t *bufptr;              /* Buffer that ETB content sends to. */
   mutex_t lock;                      /* Mutex for driver's open/close. */
-  uint8_t refcnt;                    /* ETB coresight device's enable count. */
   uint8_t opencnt;                   /* ETB device's open count. */
 };
 
diff --git a/include/nuttx/coresight/coresight_etm.h b/include/nuttx/coresight/coresight_etm.h
index a76d27ddbb..33822ea906 100644
--- a/include/nuttx/coresight/coresight_etm.h
+++ b/include/nuttx/coresight/coresight_etm.h
@@ -75,7 +75,6 @@ struct coresight_etm_dev_s
 {
   struct coresight_dev_s csdev;
   struct etm_config_s cfg;
-  uint8_t refcnt;
   int cpu;                       /* The cpu this component is affined to */
   int port_size;                 /* Out port size */
   int traceid;                   /* Trace id */
diff --git a/include/nuttx/coresight/coresight_stm.h b/include/nuttx/coresight/coresight_stm.h
index 0848b2468a..83fcff99f0 100644
--- a/include/nuttx/coresight/coresight_stm.h
+++ b/include/nuttx/coresight/coresight_stm.h
@@ -63,7 +63,6 @@ enum stp_packet_flags_e
 struct coresight_stm_dev_s
 {
   struct coresight_dev_s csdev;
-  uint8_t refcnt;
   int traceid;                       /* Trace id. */
 
   /* Used in STM device: start address of extend stimulus port, this memory
diff --git a/include/nuttx/coresight/coresight_tmc.h b/include/nuttx/coresight/coresight_tmc.h
index 64fc2d6d41..99afb02c21 100644
--- a/include/nuttx/coresight/coresight_tmc.h
+++ b/include/nuttx/coresight/coresight_tmc.h
@@ -68,7 +68,6 @@ struct coresight_tmc_dev_s
   uint32_t caps;                        /* Capalilities current etr device has. */
   enum tmc_etr_mode_e mode;             /* ETR buffer mode. */
   uint32_t offset;                      /* Data offset in ETR buffer. */
-  uint8_t refcnt;                       /* TMC coresight device's enable count */
   uint8_t opencnt;                      /* TMC device's open count. */
 };
 
diff --git a/include/nuttx/coresight/coresight_tpiu.h b/include/nuttx/coresight/coresight_tpiu.h
index 9a947aedae..d967dcd449 100644
--- a/include/nuttx/coresight/coresight_tpiu.h
+++ b/include/nuttx/coresight/coresight_tpiu.h
@@ -34,7 +34,6 @@
 struct coresight_tpiu_dev_s
 {
   struct coresight_dev_s csdev;
-  uint8_t refcnt;
 };
 
 /****************************************************************************


(nuttx) 01/13: coresight: add coresight_core

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 24050ba59ff556000a878e8d02af34f9465111a8
Author: liaoao <li...@xiaomi.com>
AuthorDate: Fri Sep 15 19:33:41 2023 +0800

    coresight: add coresight_core
    
    Signed-off-by: liaoao <li...@xiaomi.com>
---
 Kconfig                             |  33 ++
 drivers/Kconfig                     |   1 +
 drivers/Makefile                    |   1 +
 drivers/coresight/CMakeLists.txt    |  25 ++
 drivers/coresight/Kconfig           |  18 +
 drivers/coresight/Make.defs         |  31 ++
 drivers/coresight/coresight_core.c  | 687 ++++++++++++++++++++++++++++++++++++
 include/debug.h                     |  18 +
 include/nuttx/coresight/coresight.h | 241 +++++++++++++
 9 files changed, 1055 insertions(+)

diff --git a/Kconfig b/Kconfig
index 397ad247c4..f700eeeb4c 100644
--- a/Kconfig
+++ b/Kconfig
@@ -2284,6 +2284,39 @@ config DEBUG_RPMSG_INFO
 		Enable RPMSG driver informational output to SYSLOG.
 
 endif # DEBUG_RPMSG
+
+config DEBUG_CORESIGHT
+	bool "Coresight Debug Features"
+	default n
+	depends on CORESIGHT
+	---help---
+		Enable coresight debug features.
+
+if DEBUG_CORESIGHT
+
+config DEBUG_CORESIGHT_ERROR
+	bool "Coresight Error Output"
+	default n
+	depends on DEBUG_ERROR
+	---help---
+		Enable coresight driver error output to SYSLOG.
+
+config DEBUG_CORESIGHT_WARN
+	bool "Coresight Warnings Output"
+	default n
+	depends on DEBUG_WARN
+	---help---
+		Enable coresight warning output to SYSLOG.
+
+config DEBUG_CORESIGHT_INFO
+	bool "Coresight Informational Output"
+	default n
+	depends on DEBUG_INFO
+	---help---
+		Enable coresight driver informational output to SYSLOG.
+
+endif # DEBUG_CORESIGHT
+
 endif # DEBUG_FEATURES
 
 config ARCH_HAVE_STACKCHECK
diff --git a/drivers/Kconfig b/drivers/Kconfig
index fe95fa7600..46ddf14cf1 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -60,3 +60,4 @@ source "drivers/dma/Kconfig"
 source "drivers/devicetree/Kconfig"
 source "drivers/reset/Kconfig"
 source "drivers/pci/Kconfig"
+source "drivers/coresight/Kconfig"
diff --git a/drivers/Makefile b/drivers/Makefile
index e5344abc07..1461e7f223 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -78,6 +78,7 @@ include segger/Make.defs
 include usrsock/Make.defs
 include reset/Make.defs
 include pci/Make.defs
+include coresight/Make.defs
 
 ifeq ($(CONFIG_SPECIFIC_DRIVERS),y)
 -include platform/Make.defs
diff --git a/drivers/coresight/CMakeLists.txt b/drivers/coresight/CMakeLists.txt
new file mode 100644
index 0000000000..6205709727
--- /dev/null
+++ b/drivers/coresight/CMakeLists.txt
@@ -0,0 +1,25 @@
+# ##############################################################################
+# drivers/coresight/CMakeLists.txt
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more contributor
+# license agreements.  See the NOTICE file distributed with this work for
+# additional information regarding copyright ownership.  The ASF licenses this
+# file to you under the Apache License, Version 2.0 (the "License"); you may not
+# use this file except in compliance with the License.  You may obtain a copy of
+# the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations under
+# the License.
+#
+# ##############################################################################
+
+if(CONFIG_CORESIGHT)
+  set(SRCS coresight_core.c)
+
+  target_sources(drivers PRIVATE ${SRCS})
+endif()
diff --git a/drivers/coresight/Kconfig b/drivers/coresight/Kconfig
new file mode 100644
index 0000000000..73274a0682
--- /dev/null
+++ b/drivers/coresight/Kconfig
@@ -0,0 +1,18 @@
+#
+# For a description of the syntax of this configuration file,
+# see the file kconfig-language.txt in the NuttX tools repository.
+#
+
+menuconfig CORESIGHT
+	bool "Coresight Driver Support"
+	default n
+	---help---
+		Coresight drivers support.
+
+if CORESIGHT
+
+config CORESIGHT_MAX_OUTPORT_NUM
+	int "Max outport number of coresight device"
+	default 2
+
+endif # CORESIGHT
diff --git a/drivers/coresight/Make.defs b/drivers/coresight/Make.defs
new file mode 100644
index 0000000000..9152b10cf0
--- /dev/null
+++ b/drivers/coresight/Make.defs
@@ -0,0 +1,31 @@
+############################################################################
+# drivers/coresight/Make.defs
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+############################################################################
+
+# Include coresight driver build surrport
+
+ifeq ($(CONFIG_CORESIGHT),y)
+
+CSRCS += coresight_core.c
+
+DEPPATH += --dep-path coresight
+VPATH += :coresight
+CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)drivers$(DELIM)coresight
+
+endif # CONFIG_CORESIGHT
diff --git a/drivers/coresight/coresight_core.c b/drivers/coresight/coresight_core.c
new file mode 100644
index 0000000000..54e6f3e7c8
--- /dev/null
+++ b/drivers/coresight/coresight_core.c
@@ -0,0 +1,687 @@
+/****************************************************************************
+ * drivers/coresight/coresight_core.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <errno.h>
+#include <debug.h>
+#include <stdbool.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/irq.h>
+
+#include <nuttx/coresight/coresight.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* Used for build path */
+
+struct coresight_node_s
+{
+  FAR struct coresight_dev_s *csdev;
+  struct list_node link;
+};
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct list_node g_csdev_list = LIST_INITIAL_VALUE(g_csdev_list);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: coresight_enable_sink
+ ****************************************************************************/
+
+static int coresight_enable_sink(FAR struct coresight_dev_s *csdev)
+{
+  if (csdev->ops->sink_ops->enable != NULL)
+    {
+      return csdev->ops->sink_ops->enable(csdev);
+    }
+  else
+    {
+      return -EINVAL;
+    }
+}
+
+/****************************************************************************
+ * Name: coresight_disable_sink
+ ****************************************************************************/
+
+static void coresight_disable_sink(FAR struct coresight_dev_s *csdev)
+{
+  if (csdev->ops->sink_ops->disable != NULL)
+    {
+      csdev->ops->sink_ops->disable(csdev);
+    }
+}
+
+/****************************************************************************
+ * Name: coresight_find_link_inport
+ ****************************************************************************/
+
+static int coresight_find_link_inport(FAR struct coresight_dev_s *csdev,
+                                      FAR struct coresight_dev_s *prev)
+{
+  FAR struct coresight_connect_s *conn;
+  int i;
+
+  for (i = 0; i < prev->outport_num; i++)
+    {
+      conn = &prev->outconns[i];
+      if (conn->destdev == csdev)
+        {
+          return conn->destport;
+        }
+    }
+
+  return -ENODEV;
+}
+
+/****************************************************************************
+ * Name: coresight_find_link_outport
+ ****************************************************************************/
+
+static int coresight_find_link_outport(FAR struct coresight_dev_s *csdev,
+                                       FAR struct coresight_dev_s *next)
+{
+  FAR struct coresight_connect_s *conn;
+  int i;
+
+  for (i = 0; i < csdev->outport_num; i++)
+    {
+      conn = &csdev->outconns[i];
+      if (conn->destdev == next)
+        {
+          return conn->srcport;
+        }
+    }
+
+  return -ENODEV;
+}
+
+/****************************************************************************
+ * Name: coresight_enable_link
+ ****************************************************************************/
+
+static int coresight_enable_link(FAR struct coresight_dev_s *csdev,
+                                 FAR struct coresight_dev_s *prev,
+                                 FAR struct coresight_dev_s *next)
+{
+  int inport = 0;
+  int outport = 0;
+
+  if (csdev->ops->link_ops->enable == NULL)
+    {
+      return -EINVAL;
+    }
+
+  if (csdev->subtype.link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
+    {
+      inport = coresight_find_link_inport(csdev, prev);
+      if (inport < 0)
+        {
+          return inport;
+        }
+    }
+
+  if (csdev->subtype.link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
+    {
+      outport = coresight_find_link_outport(csdev, next);
+      if (outport < 0)
+        {
+          return outport;
+        }
+    }
+
+  return csdev->ops->link_ops->enable(csdev, inport, outport);
+}
+
+/****************************************************************************
+ * Name: coresight_disable_link
+ ****************************************************************************/
+
+static void coresight_disable_link(FAR struct coresight_dev_s *csdev,
+                                   FAR struct coresight_dev_s *prev,
+                                   FAR struct coresight_dev_s *next)
+{
+  if (csdev->ops->sink_ops->disable != NULL)
+    {
+      int inport = coresight_find_link_inport(csdev, prev);
+      int outport = coresight_find_link_outport(csdev, next);
+      csdev->ops->link_ops->disable(csdev, inport, outport);
+    }
+}
+
+/****************************************************************************
+ * Name: coresight_enable_source
+ ****************************************************************************/
+
+static int coresight_enable_source(FAR struct coresight_dev_s *csdev)
+{
+  if (csdev->ops->source_ops->enable != NULL)
+    {
+      return csdev->ops->source_ops->enable(csdev);
+    }
+  else
+    {
+      return -EINVAL;
+    }
+}
+
+/****************************************************************************
+ * Name: coresight_disable_source
+ ****************************************************************************/
+
+static void coresight_disable_source(FAR struct coresight_dev_s *csdev)
+{
+  if (csdev->ops->source_ops->disable != NULL)
+    {
+      csdev->ops->source_ops->disable(csdev);
+    }
+}
+
+/****************************************************************************
+ * Name: coresight_validate_source
+ *
+ * Description:
+ *   Indicate if this coresight device is a valid source device.
+ *
+ ****************************************************************************/
+
+static int coresight_validate_source(FAR struct coresight_dev_s *csdev)
+{
+  uint8_t type = csdev->type;
+  uint8_t subtype = csdev->subtype.source_subtype;
+
+  if (type != CORESIGHT_DEV_TYPE_SOURCE)
+    {
+      cserr("not a source coresight device\n");
+      return -EINVAL;
+    }
+
+  if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
+      subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
+    {
+      cserr("not a supported subtype of source device\n");
+      return -EINVAL;
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: coresight_build_path
+ *
+ * Description:
+ *   Build path from srcdev to destdev.
+ *
+ * Input Parameters:
+ *   srcdev  - Pointer to the source device.
+ *   destdev - Pointer to the destination device.
+ *   path    - Pointer to the path which will save all the coresight devices
+ *             through source device to destination device.
+ *
+ * Returned Value:
+ *   Zero on success; a negative value on failure.
+ *
+ ****************************************************************************/
+
+static int coresight_build_path(FAR struct coresight_dev_s *srcdev,
+                                FAR struct coresight_dev_s *destdev,
+                                FAR struct list_node *path)
+{
+  FAR struct coresight_node_s *node;
+  bool found = false;
+  int i;
+
+  if (srcdev == destdev)
+    {
+      goto out;
+    }
+
+  for (i = 0; i < srcdev->outport_num; i++)
+    {
+      struct coresight_dev_s *csdev = srcdev->outconns[i].destdev;
+      if (csdev && coresight_build_path(csdev, destdev, path) == 0)
+        {
+          found = true;
+          break;
+        }
+    }
+
+  if (!found)
+    {
+      return -ENODEV;
+    }
+
+out:
+  node = kmm_malloc(sizeof(struct coresight_node_s));
+  if (node == NULL)
+    {
+      return -ENOMEM;
+    }
+
+  node->csdev = srcdev;
+  list_add_head(path, &node->link);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: coresight_release_path
+ *
+ * Description:
+ *   Release memory malloced through the path.
+ *
+ ****************************************************************************/
+
+static void coresight_release_path(FAR struct list_node *path)
+{
+  FAR struct coresight_node_s *node;
+  FAR struct coresight_node_s *next;
+
+  list_for_every_entry_safe(path, node, next, struct coresight_node_s, link)
+    {
+      list_delete(&node->link);
+      kmm_free(node);
+    }
+}
+
+/****************************************************************************
+ * Name: coresight_disable_dev
+ ****************************************************************************/
+
+static void coresight_disable_dev(FAR struct coresight_node_s *node)
+{
+  switch (node->csdev->type)
+    {
+      case CORESIGHT_DEV_TYPE_SINK:
+        coresight_disable_sink(node->csdev);
+        break;
+
+      case CORESIGHT_DEV_TYPE_LINK:
+        {
+          FAR struct coresight_node_s *prev =
+            list_prev_entry(node, struct coresight_node_s, link);
+          FAR struct coresight_node_s *next =
+            list_next_entry(node, struct coresight_node_s, link);
+          coresight_disable_link(node->csdev, prev->csdev, next->csdev);
+        }
+        break;
+
+      /* We skip the first node in the path assuming that it is the source,
+       * and it will be disabled in coresight_disable. So we don't expect a
+       * source device in the middle of a path.
+       */
+
+      default:
+        DEBUGASSERT(0);
+        break;
+    }
+}
+
+/****************************************************************************
+ * Name: coresight_disable_path_from
+ *
+ * Description:
+ *   Disable coresight devices from specific node.
+ *
+ * Input Parameters:
+ *   path  - Head of the path.
+ *   node  - Start position to search, it begins from next of this node to
+ *           disable.
+ *
+ ****************************************************************************/
+
+static void coresight_disable_path_from(FAR struct list_node *path,
+                                        FAR struct coresight_node_s *node)
+{
+  list_for_every_entry_continue(node, path, struct coresight_node_s, link)
+    {
+      coresight_disable_dev(node);
+    }
+}
+
+/****************************************************************************
+ * Name: coresight_disable_path
+ *
+ * Description:
+ *   Disable all the coresight devices through the path except source device.
+ *   Source device will be disabled by coresight_disable or perf end
+ *   function.
+ *
+ ****************************************************************************/
+
+static void coresight_disable_path(FAR struct list_node *path)
+{
+  coresight_disable_path_from(path,
+    container_of(path->next, struct coresight_node_s, link));
+}
+
+/****************************************************************************
+ * Name: coresight_enable_path
+ *
+ * Description:
+ *   Enable all coresight devices through the path in reverse order.
+ *
+ * Input Parameters:
+ *   path  - path from source device to sink device.
+ *
+ * Returned Value:
+ *   Zero on success; a negative value on failure.
+ *
+ ****************************************************************************/
+
+static int coresight_enable_path(FAR struct list_node *path)
+{
+  FAR struct coresight_node_s *node;
+  int ret = 0;
+
+  list_for_every_entry_reverse(path, node, struct coresight_node_s, link)
+    {
+      switch (node->csdev->type)
+        {
+          /* Sink device is the first device to be enable. No need to disable
+           * other coresight device in the path if it enabled failed.
+           */
+
+          case CORESIGHT_DEV_TYPE_SINK:
+            ret = coresight_enable_sink(node->csdev);
+            if (ret < 0)
+              {
+                cserr("enalbe sink: %s failed ret: %d\n",
+                      node->csdev->name, ret);
+                return ret;
+              }
+            break;
+
+          case CORESIGHT_DEV_TYPE_LINK:
+            {
+              FAR struct coresight_node_s *prev =
+                list_prev_entry(node, struct coresight_node_s, link);
+              FAR struct coresight_node_s *next =
+                list_next_entry(node, struct coresight_node_s, link);
+              ret = coresight_enable_link(node->csdev, prev->csdev,
+                                          next->csdev);
+              if (ret < 0)
+                {
+                  cserr("enalbe link: %s failed ret: %d\n",
+                        node->csdev->name, ret);
+                  goto err;
+                }
+            }
+            break;
+
+          /* Source device will be enabled in coresight_enable or
+           * perf start function.
+           */
+
+          case CORESIGHT_DEV_TYPE_SOURCE:
+            break;
+
+          default:
+            cserr("invalid coresight device type through the path\n");
+            DEBUGASSERT(0);
+            goto err;
+      }
+    }
+
+  return ret;
+
+err:
+  coresight_disable_path_from(path, node);
+  return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: coresight_register
+ *
+ * Description:
+ *   Register a coresight device to the coresight bus.
+ *
+ * Input Parameters:
+ *   csdev  - Pointer to the coresight device that needs to be registered.
+ *   desc   - Pointer to the attribute description of this coresight device.
+ *
+ * Returned Value:
+ *   Zero on success; a negative value on failure.
+ *
+ ****************************************************************************/
+
+int coresight_register(FAR struct coresight_dev_s *csdev,
+                       FAR const struct coresight_desc_s *desc)
+{
+  FAR struct coresight_dev_s *tempdev;
+  irqstate_t flags;
+  int i;
+
+  csdev->name = desc->name;
+  csdev->addr = desc->addr;
+  csdev->type = desc->type;
+  csdev->subtype = desc->subtype;
+  csdev->outport_num = desc->outport_num;
+  list_initialize(&csdev->path);
+
+  if (csdev->outport_num > 0)
+    {
+      csdev->outconns =
+        kmm_zalloc(sizeof(struct coresight_connect_s) * desc->outport_num);
+      if (csdev->outconns == NULL)
+        {
+          return -ENOMEM;
+        }
+
+      for (i = 0; i < csdev->outport_num; i++)
+        {
+          FAR struct coresight_connect_s *conn = &csdev->outconns[i];
+          FAR const struct coresight_portdesc_s *portdesc =
+            &desc->outports[i];
+
+          conn->srcport = i;
+          conn->destport = portdesc->port;
+          conn->destname = portdesc->remote;
+          conn->srcdev = csdev;
+        }
+    }
+
+  flags = enter_critical_section();
+  list_for_every_entry(&g_csdev_list, tempdev, struct coresight_dev_s, node)
+    {
+      for (i = 0; i < tempdev->outport_num; i++)
+        {
+          FAR struct coresight_connect_s *conn = &tempdev->outconns[i];
+          if (strcmp(conn->destname, csdev->name) == 0)
+            {
+              conn->destdev = csdev;
+            }
+        }
+
+      for (i = 0; i < csdev->outport_num; i++)
+        {
+          FAR struct coresight_connect_s *conn = &csdev->outconns[i];
+          if (strcmp(conn->destname, tempdev->name) == 0)
+            {
+              conn->destdev = tempdev;
+            }
+        }
+    }
+
+  list_add_tail(&g_csdev_list, &csdev->node);
+  leave_critical_section(flags);
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: coresight_unregister
+ *
+ * Description:
+ *   Unregister a coresight device from coresight bus.
+ *
+ * Input Parameters:
+ *   csdev  - Pointer to the coresight device that needs to be unregistered.
+ *
+ ****************************************************************************/
+
+void coresight_unregister(FAR struct coresight_dev_s *csdev)
+{
+  FAR struct coresight_dev_s *tempdev;
+  irqstate_t flags;
+  int i;
+
+  flags = enter_critical_section();
+  list_for_every_entry(&g_csdev_list, tempdev, struct coresight_dev_s, node)
+    {
+      if (csdev == tempdev)
+        {
+          continue;
+        }
+
+      for (i = 0; i < tempdev->outport_num; i++)
+        {
+          FAR struct coresight_connect_s *conn = &tempdev->outconns[i];
+          if (conn->destdev == csdev)
+            {
+              conn->destdev = NULL;
+            }
+        }
+    }
+
+  list_delete(&csdev->node);
+  leave_critical_section(flags);
+
+  if (csdev->outport_num > 0)
+    {
+      kmm_free(csdev->outconns);
+      csdev->outconns = NULL;
+    }
+}
+
+/****************************************************************************
+ * Name: coresight_enable
+ *
+ * Description:
+ *   Enable trace start from srcdev to destdev.
+ *
+ * Input Parameters:
+ *   srcdev  - Source device that generates trace data.
+ *   destdev - Sink device that finally accepts the trace data.
+ *
+ * Returned Value:
+ *   Zero on success; a negative value on failure.
+ *
+ ****************************************************************************/
+
+int coresight_enable(FAR struct coresight_dev_s *srcdev,
+                     FAR struct coresight_dev_s *destdev)
+{
+  FAR struct coresight_node_s *node;
+  irqstate_t flags;
+  int ret;
+
+  ret = coresight_validate_source(srcdev);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  flags = enter_critical_section();
+
+  if (list_is_empty(&srcdev->path))
+    {
+      ret = coresight_build_path(srcdev, destdev, &srcdev->path);
+      if (ret < 0)
+        {
+          cserr("build path failed from %s ret: %d\n", srcdev->name, ret);
+          goto err_path;
+        }
+    }
+
+  ret = coresight_enable_path(&srcdev->path);
+  if (ret < 0)
+    {
+      cserr("enable path failed from %s ret: %d\n", srcdev->name, ret);
+      goto err_path;
+    }
+
+  ret = coresight_enable_source(srcdev);
+  if (ret < 0)
+    {
+      cserr("enable source failed %s ret: %d\n", srcdev->name, ret);
+      goto err_source;
+    }
+
+  csinfo("trace enabled success while devices are:");
+  list_for_every_entry(&srcdev->path, node, struct coresight_node_s, link)
+    {
+      csinfo("-> %s", node->csdev->name);
+    }
+
+out:
+  leave_critical_section(flags);
+  return ret;
+
+err_source:
+  coresight_disable_path(&srcdev->path);
+
+err_path:
+  coresight_release_path(&srcdev->path);
+  goto out;
+}
+
+/****************************************************************************
+ * Name: coresight_disable
+ *
+ * Description:
+ *   Disable the trace start from srcdev to destdev.
+ *
+ * Input Parameters:
+ *   srcdev  - Source device that generates trace data.
+ *
+ ****************************************************************************/
+
+void coresight_disable(FAR struct coresight_dev_s *srcdev)
+{
+  irqstate_t flags;
+
+  flags = enter_critical_section();
+
+  coresight_disable_source(srcdev);
+  coresight_disable_path(&srcdev->path);
+  coresight_release_path(&srcdev->path);
+
+  leave_critical_section(flags);
+}
+
diff --git a/include/debug.h b/include/debug.h
index 2567b01a73..418ecc6fb6 100644
--- a/include/debug.h
+++ b/include/debug.h
@@ -956,6 +956,24 @@
 #  define rpmsginfo     _none
 #endif
 
+#ifdef CONFIG_DEBUG_CORESIGHT_ERROR
+#  define cserr       _err
+#else
+#  define cserr      _none
+#endif
+
+#ifdef CONFIG_DEBUG_CORESIGHT_WARN
+#  define cswarn     _warn
+#else
+#  define cswarn     _none
+#endif
+
+#ifdef CONFIG_DEBUG_CORESIGHT_INFO
+#  define csinfo     _info
+#else
+#  define csinfo     _none
+#endif
+
 /* Buffer dumping macros do not depend on varargs */
 
 #ifdef CONFIG_DEBUG_ERROR
diff --git a/include/nuttx/coresight/coresight.h b/include/nuttx/coresight/coresight.h
new file mode 100644
index 0000000000..2630e81633
--- /dev/null
+++ b/include/nuttx/coresight/coresight.h
@@ -0,0 +1,241 @@
+/****************************************************************************
+ * include/nuttx/coresight/coresight.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_CORESIGHT_CORESIGHT_H
+#define __INCLUDE_NUTTX_CORESIGHT_CORESIGHT_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+#include <nuttx/list.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+enum coresight_dev_type_e
+{
+  CORESIGHT_DEV_TYPE_SOURCE,
+  CORESIGHT_DEV_TYPE_LINK,
+  CORESIGHT_DEV_TYPE_SINK,
+  CORESIGHT_DEV_TYPE_MAX
+};
+
+enum coresight_dev_subtype_source_e
+{
+  CORESIGHT_DEV_SUBTYPE_SOURCE_PROC,       /* ETM */
+  CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE,   /* STM */
+};
+
+enum coresight_dev_subtype_link_e
+{
+  CORESIGHT_DEV_SUBTYPE_LINK_MERG,         /* Funnel */
+  CORESIGHT_DEV_SUBTYPE_LINK_SPLIT,        /* Replocator */
+  CORESIGHT_DEV_SUBTYPE_LINK_FIFO,         /* TMC ETR */
+};
+
+enum coresight_dev_subtype_sink_e
+{
+  CORESIGHT_DEV_SUBTYPE_SINK_PORT,         /* TPIU */
+  CORESIGHT_DEV_SUBTYPE_SINK_BUFFER,       /* ETB */
+};
+
+/* This structure is used to unify different subtype of devices. */
+
+union coresight_dev_subtype_u
+{
+  enum coresight_dev_subtype_source_e source_subtype;
+  enum coresight_dev_subtype_link_e link_subtype;
+  enum coresight_dev_subtype_sink_e sink_subtype;
+};
+
+struct coresight_dev_s;
+
+struct coresight_sink_ops_s
+{
+  int (*enable)(FAR struct coresight_dev_s *csdev);
+  void (*disable)(FAR struct coresight_dev_s *csdev);
+};
+
+struct coresight_link_ops_s
+{
+  int (*enable)(FAR struct coresight_dev_s *csdev, int iport, int oport);
+  void (*disable)(FAR struct coresight_dev_s *csdev, int iport, int oport);
+};
+
+struct coresight_source_ops_s
+{
+  int (*enable)(FAR struct coresight_dev_s *csdev);
+  void (*disable)(FAR struct coresight_dev_s *csdev);
+};
+
+/* This structure is used to unify different operations of devices. */
+
+struct coresight_ops_s
+{
+  union
+    {
+      FAR const struct coresight_sink_ops_s *sink_ops;
+      FAR const struct coresight_link_ops_s *link_ops;
+      FAR const struct coresight_source_ops_s *source_ops;
+    };
+};
+
+struct coresight_portdesc_s
+{
+  /* Coresight device's name this port connects to. */
+
+  FAR const char *remote;
+
+  /* Port connects to. */
+
+  int port;
+};
+
+struct coresight_desc_s
+{
+  FAR const char *name;
+  uintptr_t addr;
+  enum coresight_dev_type_e type;
+  union coresight_dev_subtype_u subtype;
+
+  /* Description of outports of current device. */
+
+  int outport_num;
+  struct coresight_portdesc_s outports[CONFIG_CORESIGHT_MAX_OUTPORT_NUM];
+};
+
+/* Use to build the trace path. */
+
+struct coresight_connect_s
+{
+  int srcport;
+  int destport;
+
+  /* Used to find the dest device when build the trace path. */
+
+  FAR const char *destname;
+  FAR struct coresight_dev_s *srcdev;
+  FAR struct coresight_dev_s *destdev;
+};
+
+struct coresight_dev_s
+{
+  FAR const char *name;
+
+  /* Memory-mapped base address of current coresight device. */
+
+  uintptr_t addr;
+  enum coresight_dev_type_e type;
+  union coresight_dev_subtype_u subtype;
+  FAR const struct coresight_ops_s *ops;
+
+  /* Used to connect all the coresight device register to coresight bus. */
+
+  struct list_node node;
+
+  /* Used in source coresight device as trace path's list head. */
+
+  struct list_node path;
+
+  /* Out port number current coresight device have. */
+
+  int outport_num;
+
+  /* Pointer to an array of connections, array size is equal
+   * to the outport number.
+   */
+
+  FAR struct coresight_connect_s *outconns;
+};
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: coresight_register
+ *
+ * Description:
+ *   Register a coresight device to the coresight bus.
+ *
+ * Input Parameters:
+ *   csdev  - Pointer to the coresight device that needs to be registered.
+ *   desc   - Pointer to the attribute description of this coresight device.
+ *
+ * Returned Value:
+ *   Zero on success; a negative value on failure.
+ *
+ ****************************************************************************/
+
+int coresight_register(FAR struct coresight_dev_s *csdev,
+                       FAR const struct coresight_desc_s *desc);
+
+/****************************************************************************
+ * Name: coresight_unregister
+ *
+ * Description:
+ *   Unregister a coresight device from coresight bus.
+ *
+ * Input Parameters:
+ *   csdev  - Pointer to the coresight device that needs to be unregistered.
+ *
+ ****************************************************************************/
+
+void coresight_unregister(FAR struct coresight_dev_s *csdev);
+
+/****************************************************************************
+ * Name: coresight_enable
+ *
+ * Description:
+ *   Enable trace start from srcdev to destdev.
+ *
+ * Input Parameters:
+ *   srcdev  - Source device that generates trace data.
+ *   destdev - Sink device that finally accepts the trace data.
+ *
+ * Returned Value:
+ *   Zero on success; a negative value on failure.
+ *
+ ****************************************************************************/
+
+int coresight_enable(FAR struct coresight_dev_s *srcdev,
+                     FAR struct coresight_dev_s *destdev);
+
+/****************************************************************************
+ * Name: coresight_disable
+ *
+ * Description:
+ *   Disable the trace start from srcdev to destdev.
+ *
+ * Input Parameters:
+ *   srcdev  - Source device that generates trace data.
+ *
+ ****************************************************************************/
+
+void coresight_disable(FAR struct coresight_dev_s *srcdev);
+
+#endif  /* __INCLUDE_NUTTX_CORESIGHT_CORESIGHT_H */


(nuttx) 10/13: coresight: disable device before unregister

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 6f770fb4b176ef2c58b61b7a369a8c129cd3fb0f
Author: liaoao <li...@xiaomi.com>
AuthorDate: Wed Jan 10 18:12:10 2024 +0800

    coresight: disable device before unregister
    
    coresight_claim_device will fail when unregister device
    which has been enabled. device should be the init state when
    it is unregistered.
    
    Signed-off-by: liaoao <li...@xiaomi.com>
---
 drivers/coresight/coresight_core.c         | 30 ++++++++++++++++++++++++++++++
 drivers/coresight/coresight_funnel.c       | 20 ++++++++++++++++++++
 drivers/coresight/coresight_replicator.c   | 19 +++++++++++++++++++
 drivers/coresight/coresight_tmc_etf.c      | 23 +++++++++++++++++++++++
 include/nuttx/coresight/coresight_funnel.h |  1 +
 5 files changed, 93 insertions(+)

diff --git a/drivers/coresight/coresight_core.c b/drivers/coresight/coresight_core.c
index 5cd889937d..56e056af0e 100644
--- a/drivers/coresight/coresight_core.c
+++ b/drivers/coresight/coresight_core.c
@@ -791,6 +791,36 @@ void coresight_unregister(FAR struct coresight_dev_s *csdev)
         }
     }
 
+  if (csdev->refcnt > 0)
+    {
+      switch (csdev->type)
+        {
+          case CORESIGHT_DEV_TYPE_SINK:
+            if (csdev->ops->sink_ops->disable != NULL)
+              {
+                csdev->ops->sink_ops->disable(csdev);
+              }
+            break;
+
+          case CORESIGHT_DEV_TYPE_SOURCE:
+            if (csdev->ops->source_ops->disable != NULL)
+              {
+                csdev->ops->source_ops->disable(csdev);
+              }
+            break;
+
+          /* Link devices may have multiple inport or outport, it can
+           * not be distinguished here which one of them has been enabled.
+           * so disable inport/outports in its own unregister function.
+           */
+
+          default:
+            break;
+        }
+
+      coresight_disable_clk(csdev);
+    }
+
   list_delete(&csdev->node);
   leave_critical_section(flags);
 
diff --git a/drivers/coresight/coresight_funnel.c b/drivers/coresight/coresight_funnel.c
index 4228847aeb..8fe281f19e 100644
--- a/drivers/coresight/coresight_funnel.c
+++ b/drivers/coresight/coresight_funnel.c
@@ -25,6 +25,7 @@
 #include <errno.h>
 #include <debug.h>
 #include <nuttx/kmalloc.h>
+#include <nuttx/irq.h>
 
 #include <nuttx/coresight/coresight_funnel.h>
 
@@ -223,6 +224,7 @@ funnel_register(FAR const struct coresight_desc_s *desc)
       return NULL;
     }
 
+  fundev->port_num = desc->inport_num;
   csdev = &fundev->csdev;
   csdev->ops = &g_funnel_ops;
   ret = coresight_register(csdev, desc);
@@ -249,6 +251,24 @@ funnel_register(FAR const struct coresight_desc_s *desc)
 
 void funnel_unregister(FAR struct coresight_funnel_dev_s *fundev)
 {
+  irqstate_t flags;
+
+  flags = enter_critical_section();
+  if (fundev->csdev.refcnt > 0)
+    {
+      int i;
+
+      for (i = 0; i < fundev->port_num; i++)
+        {
+          if (fundev->port_refcnt[i] > 0)
+            {
+              funnel_hw_disable(fundev, i);
+            }
+        }
+    }
+
+  leave_critical_section(flags);
   coresight_unregister(&fundev->csdev);
+
   kmm_free(fundev);
 }
diff --git a/drivers/coresight/coresight_replicator.c b/drivers/coresight/coresight_replicator.c
index aa1c354bf8..cdba6f5110 100644
--- a/drivers/coresight/coresight_replicator.c
+++ b/drivers/coresight/coresight_replicator.c
@@ -25,6 +25,7 @@
 #include <errno.h>
 #include <debug.h>
 #include <nuttx/kmalloc.h>
+#include <nuttx/irq.h>
 
 #include <nuttx/coresight/coresight_replicator.h>
 
@@ -274,6 +275,24 @@ replicator_register(FAR const struct coresight_desc_s *desc)
 
 void replicator_unregister(FAR struct coresight_replicator_dev_s *repdev)
 {
+  irqstate_t flags;
+
+  flags = enter_critical_section();
+  if (repdev->csdev.refcnt > 0)
+    {
+      int i;
+
+      for (i = 0; i < repdev->csdev.outport_num; i++)
+        {
+          if (repdev->port_refcnt[i] > 0)
+            {
+              replicator_hw_disable(repdev, i);
+            }
+        }
+    }
+
+  leave_critical_section(flags);
   coresight_unregister(&repdev->csdev);
+
   kmm_free(repdev);
 }
diff --git a/drivers/coresight/coresight_tmc_etf.c b/drivers/coresight/coresight_tmc_etf.c
index c494c2f34d..67cb659c58 100644
--- a/drivers/coresight/coresight_tmc_etf.c
+++ b/drivers/coresight/coresight_tmc_etf.c
@@ -319,6 +319,11 @@ static int tmc_etf_link_enable(FAR struct coresight_dev_s *csdev,
     (FAR struct coresight_tmc_dev_s *)csdev;
   int ret;
 
+  if (csdev->refcnt != 1)
+    {
+      return 0;
+    }
+
   ret = coresight_claim_device(tmcdev->csdev.addr);
   if (ret < 0)
     {
@@ -345,6 +350,11 @@ static void tmc_etf_link_disable(FAR struct coresight_dev_s *csdev,
   FAR struct coresight_tmc_dev_s *tmcdev =
     (FAR struct coresight_tmc_dev_s *)csdev;
 
+  if (csdev->refcnt != 1)
+    {
+      return;
+    }
+
   tmc_etf_hw_disable(tmcdev);
   coresight_disclaim_device(tmcdev->csdev.addr);
 }
@@ -545,6 +555,19 @@ void tmc_etf_unregister(FAR struct coresight_tmc_dev_s * tmcdev)
       snprintf(pathname, sizeof(pathname), "/dev/%s", tmcdev->csdev.name);
       unregister_driver(pathname);
     }
+  else if (tmcdev->csdev.refcnt > 0)
+    {
+      irqstate_t flags;
+
+      /* Link device should disable hw here, and sink device will disable
+       * hw in coresight_core.c
+       */
+
+      flags = enter_critical_section();
+      tmc_etf_hw_disable(tmcdev);
+      coresight_disclaim_device(tmcdev->csdev.addr);
+      leave_critical_section(flags);
+    }
 
   coresight_unregister(&tmcdev->csdev);
 }
diff --git a/include/nuttx/coresight/coresight_funnel.h b/include/nuttx/coresight/coresight_funnel.h
index 37d53016a6..43e02fc306 100644
--- a/include/nuttx/coresight/coresight_funnel.h
+++ b/include/nuttx/coresight/coresight_funnel.h
@@ -35,6 +35,7 @@ struct coresight_funnel_dev_s
 {
   struct coresight_dev_s csdev;
   uint32_t priority;                 /* Port selection order. */
+  uint8_t port_num;                  /* Port numbre. */
   uint8_t port_refcnt[0];            /* Port refcnt. */
 };
 


(nuttx) 06/13: coresight: add etm3 device support

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 71e4267a7aaf0902665c0835aa25615ce0ec2dc7
Author: liaoao <li...@xiaomi.com>
AuthorDate: Fri Sep 15 21:33:00 2023 +0800

    coresight: add etm3 device support
    
    Signed-off-by: liaoao <li...@xiaomi.com>
---
 drivers/coresight/CMakeLists.txt        |   4 +
 drivers/coresight/Kconfig               |  16 +
 drivers/coresight/Make.defs             |   4 +
 drivers/coresight/coresight_common.c    |   7 +-
 drivers/coresight/coresight_common.h    |   3 +-
 drivers/coresight/coresight_etm3.c      | 733 ++++++++++++++++++++++++++++++++
 drivers/coresight/coresight_stm.c       |   4 +-
 drivers/coresight/coresight_tpiu.c      |  19 +-
 include/nuttx/coresight/coresight.h     |   4 +
 include/nuttx/coresight/coresight_etm.h | 139 ++++++
 10 files changed, 914 insertions(+), 19 deletions(-)

diff --git a/drivers/coresight/CMakeLists.txt b/drivers/coresight/CMakeLists.txt
index b2add74a8a..e12b869f5b 100644
--- a/drivers/coresight/CMakeLists.txt
+++ b/drivers/coresight/CMakeLists.txt
@@ -21,6 +21,10 @@
 if(CONFIG_CORESIGHT)
   set(SRCS coresight_core.c coresight_common.c)
 
+  if(CONFIG_CORESIGHT_ETM_VERSION STREQUAL "v3")
+    list(APPEND SRCS coresight_etm3.c)
+  endif()
+
   if(CONFIG_CORESIGHT_FUNNEL)
     list(APPEND SRCS coresight_funnel.c)
   endif()
diff --git a/drivers/coresight/Kconfig b/drivers/coresight/Kconfig
index 6dbff6c83c..ad0d7e50b6 100644
--- a/drivers/coresight/Kconfig
+++ b/drivers/coresight/Kconfig
@@ -19,6 +19,22 @@ config CORESIGHT_TIMEOUT
 	int "Timeout us for waiting register state change"
 	default 100
 
+config CORESIGHT_ETM
+	bool "ETM coresight device support"
+	default n
+
+if CORESIGHT_ETM
+
+config CORESIGHT_ETM_VERSION
+	string "Coresight ETM version"
+	default "v3"
+
+config CORESIGHT_ETM_USE_COPROCESSOR
+	bool "Whether use coprocessor to access ETM registers"
+	default n
+
+endif # CORESIGHT_ETM
+
 config CORESIGHT_FUNNEL
 	bool "Funnel coresight device support"
 	default n
diff --git a/drivers/coresight/Make.defs b/drivers/coresight/Make.defs
index 71723d5e6e..fa8fc3e366 100644
--- a/drivers/coresight/Make.defs
+++ b/drivers/coresight/Make.defs
@@ -24,6 +24,10 @@ ifeq ($(CONFIG_CORESIGHT),y)
 
 CSRCS += coresight_core.c coresight_common.c
 
+ifeq ($(CONFIG_CORESIGHT_ETM_VERSION),"v3")
+CSRCS += coresight_etm3.c
+endif
+
 ifeq ($(CONFIG_CORESIGHT_FUNNEL),y)
 CSRCS += coresight_funnel.c
 endif
diff --git a/drivers/coresight/coresight_common.c b/drivers/coresight/coresight_common.c
index bd7f3172a8..10af662402 100644
--- a/drivers/coresight/coresight_common.c
+++ b/drivers/coresight/coresight_common.c
@@ -272,15 +272,14 @@ void coresight_put_system_trace_id(int traceid)
  *
  ****************************************************************************/
 
-int coresight_timeout(uintptr_t addr, uint32_t off,
-                      uint32_t bitmask, uint32_t val)
+int coresight_timeout(uint32_t val, uint32_t mask, uintptr_t addr)
 {
   int i;
 
   for (i = CONFIG_CORESIGHT_TIMEOUT; i > 0; i--)
     {
-      uint32_t value = coresight_get32(addr + off);
-      if ((value & bitmask) == val)
+      uint32_t value = coresight_get32(addr);
+      if ((value & mask) == val)
         {
           return 0;
         }
diff --git a/drivers/coresight/coresight_common.h b/drivers/coresight/coresight_common.h
index e11eea5767..164d7d2183 100644
--- a/drivers/coresight/coresight_common.h
+++ b/drivers/coresight/coresight_common.h
@@ -183,8 +183,7 @@ void coresight_put_system_trace_id(int traceid);
  *
  ****************************************************************************/
 
-int coresight_timeout(uintptr_t addr, uint32_t off,
-                      uint32_t bitmask, uint32_t val);
+int coresight_timeout(uint32_t val, uint32_t mask, uintptr_t addr);
 
 /****************************************************************************
  * Name: coresight_insert_barrier_packet
diff --git a/drivers/coresight/coresight_etm3.c b/drivers/coresight/coresight_etm3.c
new file mode 100644
index 0000000000..03bf424fbc
--- /dev/null
+++ b/drivers/coresight/coresight_etm3.c
@@ -0,0 +1,733 @@
+/****************************************************************************
+ * drivers/coresight/coresight_etm3.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <errno.h>
+#include <debug.h>
+#include <nuttx/bits.h>
+#include <nuttx/kmalloc.h>
+
+#include <nuttx/coresight/coresight_etm.h>
+
+#include "coresight_common.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Device registers:
+ * 0x000 - 0x2FC: Trace         registers
+ * 0x300 - 0x314: Management    registers
+ * 0x318 - 0xEFC: Trace         registers
+ *
+ * Coresight registers
+ * 0xF00 - 0xF9C: Management    registers
+ * 0xFA0 - 0xFA4: Management    registers in PFTv1.0
+ *                Trace         registers in PFTv1.1
+ * 0xFA8 - 0xFFC: Management    registers
+ */
+
+#define ETM_CR                   0x000
+#define ETM_CCR                  0x004
+#define ETM_TRIGGER              0x008
+#define ETM_SR                   0x010
+#define ETM_SCR                  0x014
+#define ETM_TSSCR                0x018
+#define ETM_TECR2                0x01c
+#define ETM_TEEVR                0x020
+#define ETM_TECR1                0x024
+#define ETM_FFLR                 0x02c
+#define ETM_ACVR(n)              (0x040 + (n * 4))
+#define ETM_ACTR(n)              (0x080 + (n * 4))
+#define ETM_CNTRLDVR(n)          (0x140 + (n * 4))
+#define ETM_CNTENR(n)            (0x150 + (n * 4))
+#define ETM_CNTRLDEVR(n)         (0x160 + (n * 4))
+#define ETM_CNTVR(n)             (0x170 + (n * 4))
+#define ETM_SQ12EVR              0x180
+#define ETM_SQ21EVR              0x184
+#define ETM_SQ23EVR              0x188
+#define ETM_SQ31EVR              0x18c
+#define ETM_SQ32EVR              0x190
+#define ETM_SQ13EVR              0x194
+#define ETM_SQR                  0x19c
+#define ETM_EXTOUTEVR(n)         (0x1a0 + (n * 4))
+#define ETM_CIDCVR(n)            (0x1b0 + (n * 4))
+#define ETM_CIDCMR               0x1bc
+#define ETM_IMPSPEC0             0x1c0
+#define ETM_IMPSPEC1             0x1c4
+#define ETM_IMPSPEC2             0x1c8
+#define ETM_IMPSPEC3             0x1cc
+#define ETM_IMPSPEC4             0x1d0
+#define ETM_IMPSPEC5             0x1d4
+#define ETM_IMPSPEC6             0x1d8
+#define ETM_IMPSPEC7             0x1dc
+#define ETM_SYNCFR               0x1e0
+#define ETM_IDR                  0x1e4
+#define ETM_CCER                 0x1e8
+#define ETM_EXTINSELR            0x1ec
+#define ETM_TESSEICR             0x1f0
+#define ETM_EIBCR                0x1f4
+#define ETM_TSEVR                0x1f8
+#define ETM_AUXCR                0x1fc
+#define ETM_TRACEIDR             0x200
+#define ETM_VMIDCVR              0x240
+
+/* Management registers (0x300-0x314) */
+
+#define ETM_OSLAR                0x300
+#define ETM_OSLSR                0x304
+#define ETM_OSSRR                0x308
+#define ETM_PDCR                 0x310
+#define ETM_PDSR                 0x314
+
+/* Register definition */
+
+/* ETMCR - 0x00 */
+
+#define ETM_CR_PWD_DWN           BIT(0)
+#define ETM_CR_STALL_MODE        BIT(7)
+#define ETM_CR_BRANCH_BROADCAST  BIT(8)
+#define ETM_CR_ETM_PRG           BIT(10)
+#define ETM_CR_ETM_EN            BIT(11)
+#define ETM_CR_CYC_ACC           BIT(12)
+#define ETM_CR_CTXID_SIZE        (BIT(14) | BIT(15))
+#define ETM_CR_TIMESTAMP_EN      BIT(28)
+
+/* ETM_CR_RETURN_STACK is supported by PTM, ETM not support it */
+
+#define ETM_CR_RETURN_STACK      BIT(29)
+
+/* ETMCCR - 0x04 */
+
+#define ETM_CCR_FIFOFULL         BIT(23)
+
+/* ETMPDCR - 0x310 */
+
+#define ETM_PDCR_PWD_UP          BIT(3)
+
+/* ETMTECR1 - 0x024 */
+
+#define ETM_TECR1_ADDR_COMP_1    BIT(0)
+#define ETM_TECR1_INC_EXC        BIT(24)
+#define ETM_TECR1_START_STOP     BIT(25)
+
+/* ETMCCER - 0x1E8 */
+
+#define ETM_CCER_TIMESTAMP       BIT(22)
+#define ETM_CCER_RETSTACK        BIT(23)
+
+#define ETM_MODE_EXCLUDE         BIT(0)
+#define ETM_MODE_CYCACC          BIT(1)
+#define ETM_MODE_STALL           BIT(2)
+#define ETM_MODE_TIMESTAMP       BIT(3)
+#define ETM_MODE_CTXID           BIT(4)
+#define ETM_MODE_BBROAD          BIT(5)
+#define ETM_MODE_RET_STACK       BIT(6)
+#define ETM_MODE_EXCL_KERN       BIT(30)
+#define ETM_MODE_EXCL_USER       BIT(31)
+#define ETM_MODE_ALL                                                    \
+        (ETM_MODE_EXCLUDE | ETM_MODE_CYCACC | ETM_MODE_STALL |          \
+         ETM_MODE_TIMESTAMP | ETM_MODE_CTXID | ETM_MODE_BBROAD |        \
+         ETM_MODE_RET_STACK | ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER)
+
+#define ETM_SQR_MASK             0x3
+#define ETM_TRACEID_MASK         0x3f
+#define ETM_EVENT_MASK           0x1ffff
+#define ETM_SYNC_MASK            0xfff
+#define ETM_ALL_MASK             0xffffffff
+
+#define ETM_SR_PROGRAM           BIT(1)
+#define ETM_SEQ_STATE_MAX_VAL    0x2
+#define ETM_PORT_SIZE_MASK       (GENMASK(21, 21) | GENMASK(6, 4))
+
+#define ETM_ARCH_V3_3            0x23
+#define ETM_ARCH_V3_5            0x25
+#define PFT_ARCH_V1_0            0x30
+#define PFT_ARCH_V1_1            0x31
+
+/* Hard wired, always true */
+
+#define ETM_HARD_WIRE_RES_A      ((0x0f << 0) | (0x06 << 4))
+
+/* Single addr comparator 1 */
+
+#define ETM_ADD_COMP_0           ((0x00 << 7) | (0x00 << 11))
+
+/* NOT(A) */
+
+#define ETM_EVENT_NOT_A          BIT(14)
+
+#define ETM_DEFAULT_EVENT_VAL \
+        (ETM_HARD_WIRE_RES_A | ETM_ADD_COMP_0 | ETM_EVENT_NOT_A)
+
+#define ETM3X_SUPPORTED_OPTIONS \
+        (ETM_CR_CYC_ACC | ETM_CR_TIMESTAMP_EN | ETM_CR_RETURN_STACK)
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+static int etm_enable(FAR struct coresight_dev_s *csdev);
+static void etm_disable(FAR struct coresight_dev_s *csdev);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct coresight_source_ops_s g_etm_source_ops =
+{
+  .enable  = etm_enable,
+  .disable = etm_disable,
+};
+
+static const struct coresight_ops_s g_etm_ops =
+{
+  .source_ops = &g_etm_source_ops,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+#ifndef CONFIG_CORESIGHT_ETM_USE_COPROCESSOR
+static inline void etm_write_reg(FAR struct coresight_etm_dev_s *etmdev,
+                                 uint32_t val, uint32_t off)
+{
+  coresight_put32(val, etmdev->csdev.addr + off);
+}
+
+static inline uint32_t etm_read_reg(FAR struct coresight_etm_dev_s *etmdev,
+                                    uint32_t off)
+{
+  return coresight_get32(etmdev->csdev.addr + off);
+}
+#endif
+
+/****************************************************************************
+ * Name: etm_modify_reg32
+ *
+ * Description:
+ *   Set a bitmask of register to specific value.
+ *
+ ****************************************************************************/
+
+static void etm_modify_reg32(FAR struct coresight_etm_dev_s *etmdev,
+                             uint32_t val, uint32_t mask, uint32_t off)
+{
+  uint32_t temp = etm_read_reg(etmdev, off);
+
+  etm_write_reg(etmdev, (temp & ~mask) | (val & mask), off);
+}
+
+/****************************************************************************
+ * Name: etm_os_unlock
+ *
+ * Description:
+ *   When the ETM trace registers are locked, any attempt to access the
+ *   locked registers returns a slave-generated error response.In ETMv3.5,
+ *   the OS Lock is always set from an ETM reset. ARM recommends that, after
+ *   programming the ETM registers, you always execute an ISB instruction to
+ *   ensure that all updates are committed to the ETM before you restart
+ *   normal code execution.
+ *
+ ****************************************************************************/
+
+static void etm_os_unlock(FAR struct coresight_etm_dev_s *etmdev)
+{
+  etm_write_reg(etmdev, 0x0, ETM_OSLAR);
+}
+
+/****************************************************************************
+ * Name: etm_clr_pwrdwn
+ *
+ * Description:
+ *   When pwrdown bit is set to 1, the ETM must be powered down and disabled,
+ *   and then operated in a low power mode with all clocks stopped. When this
+ *   bit is set to 1, writes to some registers and fields might be ignored.
+ *   ARM recommends that you use a read-modify-write procedure when modifying
+ *   the ETMCR.
+ *
+ ****************************************************************************/
+
+static void etm_clr_pwrdwn(FAR struct coresight_etm_dev_s *etmdev)
+{
+  etm_modify_reg32(etmdev, 0, ETM_CR_PWD_DWN, ETM_CR);
+}
+
+/****************************************************************************
+ * Name: etm_set_pwrdwn
+ ****************************************************************************/
+
+static void etm_set_pwrdwn(FAR struct coresight_etm_dev_s *etmdev)
+{
+  etm_modify_reg32(etmdev, ETM_CR_PWD_DWN, ETM_CR_PWD_DWN, ETM_CR);
+}
+
+/****************************************************************************
+ * Name: etm_set_pwrup
+ *
+ * Description:
+ *   Power is provided to the ETM trace registers. This register can only be
+ *   accessed using a memory-mapped interface or from an external debugger.
+ *
+ ****************************************************************************/
+
+static void etm_set_pwrup(FAR struct coresight_etm_dev_s *etmdev)
+{
+  coresight_modify32(ETM_PDCR_PWD_UP, ETM_PDCR_PWD_UP,
+                     etmdev->csdev.addr + ETM_PDCR);
+}
+
+/****************************************************************************
+ * Name: etm_clr_pwrup
+ ****************************************************************************/
+
+static void etm_clr_pwrup(FAR struct coresight_etm_dev_s *etmdev)
+{
+  coresight_modify32(0, ETM_PDCR_PWD_UP, etmdev->csdev.addr + ETM_PDCR);
+}
+
+/****************************************************************************
+ * Name: etm_timeout
+ *
+ * Description:
+ *   Loop until a bitmask of register has changed to a specific value.
+ *
+ ****************************************************************************/
+
+static int etm_timeout(FAR struct coresight_etm_dev_s *etmdev,
+                       uint32_t val, uint32_t mask, uint32_t off)
+{
+  int i;
+
+  for (i = CONFIG_CORESIGHT_TIMEOUT; i > 0; i--)
+    {
+      uint32_t value = etm_read_reg(etmdev, off);
+      if ((value & mask) == val)
+        {
+          return 0;
+        }
+
+      up_udelay(1);
+    }
+
+  return -EAGAIN;
+}
+
+/****************************************************************************
+ * Name: etm_set_program
+ *
+ * Description:
+ *   Set ETM Programming bit to disable all operations during programming.
+ *   When programming the ETM registers you must enable all the changes at
+ *   the same time.
+ *
+ ****************************************************************************/
+
+static void etm_set_program(FAR struct coresight_etm_dev_s *etmdev)
+{
+  etm_modify_reg32(etmdev, ETM_CR_ETM_PRG, ETM_CR_ETM_PRG, ETM_CR);
+
+  if (etm_timeout(etmdev, ETM_SR_PROGRAM, ETM_SR_PROGRAM, ETM_SR) < 0)
+    {
+      cserr("timeout observed at setting ETM_SR_PROGRAM\n");
+    }
+}
+
+/****************************************************************************
+ * Name: etm_clr_program
+ ****************************************************************************/
+
+static void etm_clr_program(FAR struct coresight_etm_dev_s *etmdev)
+{
+  etm_modify_reg32(etmdev, 0, ETM_CR_ETM_PRG, ETM_CR);
+
+  if (etm_timeout(etmdev, 0, ETM_SR_PROGRAM, ETM_SR) < 0)
+    {
+      cserr("timeout observed at clearing ETM_SR_PROGRAM\n");
+    }
+}
+
+/****************************************************************************
+ * Name: etm_init_arch_data
+ *
+ * Description:
+ *   Get capabilities of current ETM architecture version.
+ *
+ ****************************************************************************/
+
+static void etm_init_arch_data(FAR struct coresight_etm_dev_s *etmdev)
+{
+  uint32_t etmccr;
+
+  coresight_unlock(etmdev->csdev.addr);
+  etm_os_unlock(etmdev);
+  etm_clr_pwrdwn(etmdev);
+  etm_set_pwrup(etmdev);
+  etm_set_program(etmdev);
+
+  etmdev->arch = BMVAL(etm_read_reg(etmdev, ETM_IDR), 4, 11);
+  etmdev->port_size = etm_read_reg(etmdev, ETM_CR) & ETM_PORT_SIZE_MASK;
+
+  etmccr = etm_read_reg(etmdev, ETM_CCR);
+  etmdev->nr_addr_cmp = BMVAL(etmccr, 0, 3) * 2;
+  etmdev->nr_cntr = BMVAL(etmccr, 13, 15);
+  etmdev->nr_ext_inp = BMVAL(etmccr, 17, 19);
+  etmdev->nr_ext_out = BMVAL(etmccr, 20, 22);
+  etmdev->nr_ctxid_cmp = BMVAL(etmccr, 24, 25);
+
+  etm_clr_program(etmdev);
+  etm_clr_pwrup(etmdev);
+  etm_set_pwrdwn(etmdev);
+  coresight_lock(etmdev->csdev.addr);
+}
+
+/****************************************************************************
+ * Name: etm_arch_supported
+ *
+ * Description:
+ *   If ETM/PTM implement is supported by current driver.
+ *
+ ****************************************************************************/
+
+static bool etm_arch_supported(uint8_t arch)
+{
+  switch (arch)
+    {
+      case ETM_ARCH_V3_3:
+      case ETM_ARCH_V3_5:
+      case PFT_ARCH_V1_0:
+      case PFT_ARCH_V1_1:
+        return true;
+      default:
+        return false;
+    }
+}
+
+/****************************************************************************
+ * Name: etm_set_default
+ *
+ * Description:
+ *   Setup default ETM config.
+ *
+ ****************************************************************************/
+
+void etm_set_default(struct etm_config_s *config)
+{
+  int i;
+
+  /* Trace all memory, set according to spec */
+
+  config->enable_ctrl1 = BIT(24);
+  config->enable_ctrl2 = 0x0;
+  config->enable_event = ETM_HARD_WIRE_RES_A;
+
+  /* Disable all other event */
+
+  config->trigger_event = ETM_DEFAULT_EVENT_VAL;
+  config->seq_12_event = ETM_DEFAULT_EVENT_VAL;
+  config->seq_21_event = ETM_DEFAULT_EVENT_VAL;
+  config->seq_23_event = ETM_DEFAULT_EVENT_VAL;
+  config->seq_31_event = ETM_DEFAULT_EVENT_VAL;
+  config->seq_32_event = ETM_DEFAULT_EVENT_VAL;
+  config->seq_13_event = ETM_DEFAULT_EVENT_VAL;
+  config->timestamp_event = ETM_DEFAULT_EVENT_VAL;
+
+  for (i = 0; i < ETM_MAX_CNTR; i++)
+    {
+      config->cntr_event[i] = ETM_DEFAULT_EVENT_VAL;
+      config->cntr_rld_event[i] = ETM_DEFAULT_EVENT_VAL;
+    }
+
+  config->sync_freq = 0x400;
+}
+
+/****************************************************************************
+ * Name: etm_hw_enable
+ ****************************************************************************/
+
+static int etm_hw_enable(FAR struct coresight_etm_dev_s *etmdev)
+{
+  FAR struct etm_config_s *config = &etmdev->cfg;
+  uint32_t etmcr;
+  int ret;
+  int i;
+
+  ret = coresight_claim_device(etmdev->csdev.addr);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  coresight_unlock(etmdev->csdev.addr);
+  etm_os_unlock(etmdev);
+  etm_set_pwrup(etmdev);
+  etm_clr_pwrdwn(etmdev);
+  etm_set_program(etmdev);
+
+  etmcr = etm_read_reg(etmdev, ETM_CR);
+  etmcr &= ~ETM3X_SUPPORTED_OPTIONS;
+  etmcr |= ETM_CR_ETM_EN;
+  etm_write_reg(etmdev, config->ctrl | etmcr, ETM_CR);
+  etm_write_reg(etmdev, config->trigger_event, ETM_TRIGGER);
+  etm_write_reg(etmdev, config->startstop_ctrl, ETM_TSSCR);
+  etm_write_reg(etmdev, config->enable_event, ETM_TEEVR);
+  etm_write_reg(etmdev, config->enable_ctrl1, ETM_TECR1);
+  etm_write_reg(etmdev, config->fifofull_level, ETM_FFLR);
+  for (i = 0; i < etmdev->nr_addr_cmp; i++)
+    {
+      etm_write_reg(etmdev, config->addr_val[i], ETM_ACVR(i));
+      etm_write_reg(etmdev, config->addr_acctype[i], ETM_ACTR(i));
+    }
+
+  for (i = 0; i < etmdev->nr_cntr; i++)
+    {
+      etm_write_reg(etmdev, config->cntr_rld_val[i], ETM_CNTRLDVR(i));
+      etm_write_reg(etmdev, config->cntr_event[i], ETM_CNTENR(i));
+      etm_write_reg(etmdev, config->cntr_rld_event[i], ETM_CNTRLDEVR(i));
+      etm_write_reg(etmdev, config->cntr_val[i], ETM_CNTVR(i));
+    }
+
+  etm_write_reg(etmdev, config->seq_12_event, ETM_SQ12EVR);
+  etm_write_reg(etmdev, config->seq_21_event, ETM_SQ21EVR);
+  etm_write_reg(etmdev, config->seq_23_event, ETM_SQ23EVR);
+  etm_write_reg(etmdev, config->seq_31_event, ETM_SQ31EVR);
+  etm_write_reg(etmdev, config->seq_32_event, ETM_SQ32EVR);
+  etm_write_reg(etmdev, config->seq_13_event, ETM_SQ13EVR);
+  etm_write_reg(etmdev, config->seq_curr_state, ETM_SQR);
+  for (i = 0; i < etmdev->nr_ext_out; i++)
+    {
+      etm_write_reg(etmdev, ETM_DEFAULT_EVENT_VAL, ETM_EXTOUTEVR(i));
+    }
+
+  for (i = 0; i < etmdev->nr_ctxid_cmp; i++)
+    {
+      etm_write_reg(etmdev, config->ctxid_pid[i], ETM_CIDCVR(i));
+    }
+
+  etm_write_reg(etmdev, config->ctxid_mask, ETM_CIDCMR);
+  etm_write_reg(etmdev, config->sync_freq, ETM_SYNCFR);
+
+  /* No external input selected */
+
+  etm_write_reg(etmdev, 0x0, ETM_EXTINSELR);
+  etm_write_reg(etmdev, config->timestamp_event, ETM_TSEVR);
+
+  /* No auxiliary control selected */
+
+  etm_write_reg(etmdev, 0x0, ETM_AUXCR);
+
+  etm_write_reg(etmdev, etmdev->traceid, ETM_TRACEIDR);
+
+  /* No VMID comparator value selected */
+
+  etm_write_reg(etmdev, 0x0, ETM_VMIDCVR);
+
+  etm_clr_program(etmdev);
+  coresight_lock(etmdev->csdev.addr);
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: etm_hw_disable
+ ****************************************************************************/
+
+static void etm_hw_disable(FAR struct coresight_etm_dev_s *etmdev)
+{
+  FAR struct etm_config_s *config = &etmdev->cfg;
+  int i;
+
+  coresight_unlock(etmdev->csdev.addr);
+  etm_set_program(etmdev);
+
+  /* Read back sequencer and counters for post trace analysis */
+
+  config->seq_curr_state = etm_read_reg(etmdev, ETM_SQR) & ETM_SQR_MASK;
+  for (i = 0; i < etmdev->nr_cntr; i++)
+    {
+      config->cntr_val[i] = etm_read_reg(etmdev, ETM_CNTVR(i));
+    }
+
+  etm_set_pwrdwn(etmdev);
+  coresight_lock(etmdev->csdev.addr);
+
+  coresight_disclaim_device(etmdev->csdev.addr);
+}
+
+/****************************************************************************
+ * Name: etm_enable
+ ****************************************************************************/
+
+static int etm_enable(FAR struct coresight_dev_s *csdev)
+{
+  FAR struct coresight_etm_dev_s *etmdev =
+    (FAR struct coresight_etm_dev_s *)csdev;
+  int ret = 0;
+
+  if (etmdev->refcnt++ == 0)
+    {
+      ret = etm_hw_enable(etmdev);
+      if (ret < 0)
+        {
+          etmdev->refcnt--;
+          cserr("%s enabled\n", csdev->name);
+        }
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: etm_enable
+ ****************************************************************************/
+
+static void etm_disable(FAR struct coresight_dev_s *csdev)
+{
+  FAR struct coresight_etm_dev_s *etmdev =
+    (FAR struct coresight_etm_dev_s *)csdev;
+
+  if (--etmdev->refcnt == 0)
+    {
+      etm_hw_disable(etmdev);
+      csinfo("etm %s disabled\n", csdev->name);
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: etm_config
+ *
+ * Description:
+ *   Configure the etm device.
+ *
+ * Input Parameters:
+ *   etmdev  - Pointer to the ETM device to config.
+ *   config  - Configuration need to be set to ETM device.
+ *
+ * Returned Value:
+ *   Zero on success; a negative value on failure.
+ *
+ ****************************************************************************/
+
+int etm_config(FAR struct coresight_etm_dev_s *etmdev,
+               FAR const struct etm_config_s *config)
+{
+  coresight_unlock(etmdev->csdev.addr);
+  etm_os_unlock(etmdev);
+  etm_clr_pwrdwn(etmdev);
+  etm_set_pwrup(etmdev);
+  etm_set_program(etmdev);
+
+  memcpy(&etmdev->cfg, config, sizeof(struct etm_config_s));
+
+  if ((etmdev->cfg.ctrl & ETM_CR_RETURN_STACK) &&
+      !(etm_read_reg(etmdev, ETM_CCER) & ETM_CCER_RETSTACK))
+    {
+      etmdev->cfg.ctrl &= ~ETM_CR_RETURN_STACK;
+    }
+
+  etm_clr_program(etmdev);
+  etm_clr_pwrup(etmdev);
+  etm_set_pwrdwn(etmdev);
+  coresight_lock(etmdev->csdev.addr);
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: etm_register
+ *
+ * Description:
+ *   Register an ETM/PTM devices.
+ *
+ * Input Parameters:
+ *   desc  - A description of this coresight device.
+ *
+ * Returned Value:
+ *   Pointer to an ETM device on success; NULL on failure.
+ *
+ ****************************************************************************/
+
+FAR struct coresight_etm_dev_s *
+etm_register(FAR const struct coresight_desc_s *desc)
+{
+  FAR struct coresight_etm_dev_s *etmdev;
+  FAR struct coresight_dev_s *csdev;
+  int ret;
+
+  etmdev = kmm_zalloc(sizeof(struct coresight_etm_dev_s));
+  if (etmdev == NULL)
+    {
+      cserr("%s:malloc failed!\n", desc->name);
+      return NULL;
+    }
+
+  etmdev->cpu = desc->cpu;
+  etmdev->csdev.addr = desc->addr;
+
+  etm_init_arch_data(etmdev);
+
+  if (!etm_arch_supported(etmdev->arch))
+    {
+      kmm_free(etmdev);
+      cserr("%s:current implement version is not supported\n", desc->name);
+      return NULL;
+    }
+
+  etmdev->traceid = coresight_get_cpu_trace_id(etmdev->cpu);
+  etm_set_default(&etmdev->cfg);
+
+  csdev = &etmdev->csdev;
+  csdev->ops = &g_etm_ops;
+  ret = coresight_register(csdev, desc);
+  if (ret < 0)
+    {
+      kmm_free(etmdev);
+      cserr("%s:register failed\n", desc->name);
+      return NULL;
+    }
+
+  return etmdev;
+}
+
+/****************************************************************************
+ * Name: etm_unregister
+ *
+ * Description:
+ *   Unregister an EMT/PTM device.
+ *
+ ****************************************************************************/
+
+void etm_unregister(FAR struct coresight_etm_dev_s *etmdev)
+{
+  coresight_unregister(&etmdev->csdev);
+  kmm_free(etmdev);
+}
diff --git a/drivers/coresight/coresight_stm.c b/drivers/coresight/coresight_stm.c
index 308d02ae08..c64eaf048d 100644
--- a/drivers/coresight/coresight_stm.c
+++ b/drivers/coresight/coresight_stm.c
@@ -150,8 +150,8 @@ static void stm_hw_disable(FAR struct coresight_stm_dev_s *stmdev)
   coresight_put32(0x0, stmdev->csdev.addr + STM_HEER);
   coresight_put32(0x0, stmdev->csdev.addr + STM_HETER);
 
-  if (coresight_timeout(stmdev->csdev.addr, STM_TCSR,
-                        STM_TCSR_BUSY, 0) < 0)
+  if (coresight_timeout(0x0, STM_TCSR_BUSY,
+                        stmdev->csdev.addr + STM_TCSR) < 0)
     {
       cserr("timeout waiting for STM stopped\n");
     }
diff --git a/drivers/coresight/coresight_tpiu.c b/drivers/coresight/coresight_tpiu.c
index 6685d2a006..907cfb0958 100644
--- a/drivers/coresight/coresight_tpiu.c
+++ b/drivers/coresight/coresight_tpiu.c
@@ -107,25 +107,22 @@ static int tpiu_hw_enable(FAR struct coresight_tpiu_dev_s *tpiudev)
 
 static void tpiu_hw_disable(FAR struct coresight_tpiu_dev_s *tpiudev)
 {
-  uint32_t ffcr;
-
   coresight_unlock(tpiudev->csdev.addr);
 
   /* Trigger a formatter stop event. */
 
-  ffcr = coresight_get32(tpiudev->csdev.addr + TPIU_FFCR);
-  ffcr |= TPIU_FFCR_STOP_FI;
-  coresight_put32(ffcr, tpiudev->csdev.addr + TPIU_FFCR);
-  ffcr |= TPIU_FFCR_FON_MAN;
-  coresight_put32(ffcr, tpiudev->csdev.addr + TPIU_FFCR);
-  if (coresight_timeout(tpiudev->csdev.addr, TPIU_FFCR,
-                        TPIU_FFCR_FON_MAN, 0) < 0)
+  coresight_modify32(TPIU_FFCR_STOP_FI, TPIU_FFCR_STOP_FI,
+                     tpiudev->csdev.addr + TPIU_FFCR);
+  coresight_modify32(TPIU_FFCR_FON_MAN, TPIU_FFCR_FON_MAN,
+                     tpiudev->csdev.addr + TPIU_FFCR);
+  if (coresight_timeout(0, TPIU_FFCR_FON_MAN,
+                        tpiudev->csdev.addr + TPIU_FFCR) < 0)
     {
       cserr("timeout while waiting for completion of Manual Flush\n");
     }
 
-  if (coresight_timeout(tpiudev->csdev.addr, TPIU_FFSR,
-                        TPIU_FFSR_FT_STOPPED, TPIU_FFSR_FT_STOPPED) < 0)
+  if (coresight_timeout(TPIU_FFSR_FT_STOPPED, TPIU_FFSR_FT_STOPPED,
+                        tpiudev->csdev.addr + TPIU_FFSR) < 0)
     {
       cserr("timeout while waiting for Formatter to Stop\n");
     }
diff --git a/include/nuttx/coresight/coresight.h b/include/nuttx/coresight/coresight.h
index f34fe5449b..178a332304 100644
--- a/include/nuttx/coresight/coresight.h
+++ b/include/nuttx/coresight/coresight.h
@@ -122,6 +122,10 @@ struct coresight_desc_s
   enum coresight_dev_type_e type;
   union coresight_dev_subtype_u subtype;
 
+  /* Used in ETM device. */
+
+  uint8_t cpu;
+
   /* Used in funnel devices. */
 
   int inport_num;
diff --git a/include/nuttx/coresight/coresight_etm.h b/include/nuttx/coresight/coresight_etm.h
new file mode 100644
index 0000000000..a76d27ddbb
--- /dev/null
+++ b/include/nuttx/coresight/coresight_etm.h
@@ -0,0 +1,139 @@
+/****************************************************************************
+ * include/nuttx/coresight/coresight_etm.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_CORESIGHT_CORESIGHT_ETM_H
+#define __INCLUDE_NUTTX_CORESIGHT_CORESIGHT_ETM_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/coresight/coresight.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define ETM_MAX_ADDR_CMP         16
+#define ETM_MAX_CNTR             4
+#define ETM_MAX_CTXID_CMP        3
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct etm_config_s
+{
+  uint32_t ctrl;                            /* ETMCR */
+  uint32_t trigger_event;                   /* ETMTRIGGER */
+  uint32_t startstop_ctrl;                  /* ETMTSSCR */
+  uint32_t enable_event;                    /* ETMTEEVR */
+  uint32_t enable_ctrl1;                    /* ETMTECR1 */
+  uint32_t enable_ctrl2;                    /* ETMTECR2 */
+  uint32_t fifofull_level;                  /* ETMFFLR */
+  uint8_t  addr_idx;                        /* Index for the address comparator selection. */
+  uint32_t addr_val[ETM_MAX_ADDR_CMP];      /* Value for address comparator register. */
+  uint32_t addr_acctype[ETM_MAX_ADDR_CMP];  /* Access type for address comparator register. */
+  uint32_t addr_type[ETM_MAX_ADDR_CMP];     /* Current status of the comparator register. */
+  uint8_t  cntr_idx;                        /* Index for the counter register selection */
+  uint32_t cntr_rld_val[ETM_MAX_CNTR];      /* Reload value of a counter register. */
+  uint32_t cntr_event[ETM_MAX_CNTR];        /* Control for counter enable register. */
+  uint32_t cntr_rld_event[ETM_MAX_CNTR];    /* Value for counter reload event register. */
+  uint32_t cntr_val[ETM_MAX_CNTR];          /* Counter value register. */
+  uint32_t seq_12_event;                    /* Event causing the transition from 1 to 2 */
+  uint32_t seq_21_event;                    /* Event causing the transition from 2 to 1 */
+  uint32_t seq_23_event;                    /* Event causing the transition from 2 to 3 */
+  uint32_t seq_31_event;                    /* Event causing the transition from 3 to 1 */
+  uint32_t seq_32_event;                    /* Event causing the transition from 3 to 2 */
+  uint32_t seq_13_event;                    /* Event causing the transition from 1 to 3 */
+  uint32_t seq_curr_state;                  /* Current value of the sequencer register. */
+  uint8_t  ctxid_idx;                       /* Index for the context ID registers. */
+  uint32_t ctxid_pid[ETM_MAX_CTXID_CMP];    /* Value for the context ID to trigger on */
+  uint32_t ctxid_mask;                      /* Mask applicable to all the context IDs. */
+  uint32_t sync_freq;                       /* Synchronisation frequency. */
+  uint32_t timestamp_event;                 /* ETMTSEVR */
+};
+
+struct coresight_etm_dev_s
+{
+  struct coresight_dev_s csdev;
+  struct etm_config_s cfg;
+  uint8_t refcnt;
+  int cpu;                       /* The cpu this component is affined to */
+  int port_size;                 /* Out port size */
+  int traceid;                   /* Trace id */
+  uint8_t arch;                  /* ETM/PTM version number */
+  uint8_t nr_addr_cmp;           /* Number of pairs of address comparators */
+  uint8_t nr_cntr;               /* Number of counters */
+  uint8_t nr_ext_inp;            /* Number of external input */
+  uint8_t nr_ext_out;            /* Number of external output */
+  uint8_t nr_ctxid_cmp;          /* Number of contextID comparators */
+};
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: etm_register
+ *
+ * Description:
+ *   Register an ETM/PTM devices.
+ *
+ * Input Parameters:
+ *   desc  - A description of this coresight device.
+ *
+ * Returned Value:
+ *   Pointer to an ETM device on success; NULL on failure.
+ *
+ ****************************************************************************/
+
+FAR struct coresight_etm_dev_s *
+etm_register(FAR const struct coresight_desc_s *desc);
+
+/****************************************************************************
+ * Name: etm_unregister
+ *
+ * Description:
+ *   Unregister an EMT/PTM device.
+ *
+ ****************************************************************************/
+
+void etm_unregister(FAR struct coresight_etm_dev_s *etmdev);
+
+/****************************************************************************
+ * Name: etm_config
+ *
+ * Description:
+ *   Configure the etm device.
+ *
+ * Input Parameters:
+ *   etmdev  - Pointer to the ETM device to config.
+ *   config  - Configuration need to be set to ETM device.
+ *
+ * Returned Value:
+ *   Zero on success; a negative value on failure.
+ *
+ ****************************************************************************/
+
+int etm_config(FAR struct coresight_etm_dev_s *etmdev,
+               FAR const struct etm_config_s *config);
+
+#endif  //__INCLUDE_NUTTX_CORESIGHT_CORESIGHT_ETM_H


(nuttx) 12/13: coresight: add document for coresight

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit b98d68599e74e68e13f843a3d1626762fa0ff3d9
Author: liaoao <li...@xiaomi.com>
AuthorDate: Thu Sep 12 17:08:42 2024 +0800

    coresight: add document for coresight
    
    Signed-off-by: liaoao <li...@xiaomi.com>
---
 Documentation/guides/coresight.rst | 78 ++++++++++++++++++++++++++++++++++++++
 Documentation/guides/index.rst     |  1 +
 2 files changed, 79 insertions(+)

diff --git a/Documentation/guides/coresight.rst b/Documentation/guides/coresight.rst
new file mode 100644
index 0000000000..66899fee31
--- /dev/null
+++ b/Documentation/guides/coresight.rst
@@ -0,0 +1,78 @@
+======================================
+Coresight - HW Assisted Tracing on ARM
+======================================
+
+Overview
+--------
+
+Coresight is an umbrella of technologies allowing for the debugging of ARM
+based SoC.  It includes solutions for JTAG and HW assisted tracing.  This
+document is concerned with the latter.
+
+HW assisted tracing is becoming increasingly useful when dealing with systems
+that have many SoCs and other components like GPU and DMA engines. Developers
+can monitor the behavior of their software as it runs on the device, view
+real-time data about its execution, and identify and debug issues quickly.
+
+Coresight omponents are generally categorised as source, link and sinks.
+The source devices generats a compressed stream representing the processor
+instruction path based on tracing scenarios. The link devices are responsible
+for transferring the stream from the source device to the sink device. The sink
+devices serve as as endpoints to the coresight implementation, either storing
+the compressed stream in a memory buffer or creating an interface to the
+outside world where data can be transferred to a host without fear of filling
+up the onboard coresight memory buffer.
+
+refer to the following document for more details:
+https://developer.arm.com/documentation/102520/latest/
+
+
+Acronyms and Classification
+---------------------------
+
+Acronyms:
+
+PTM:
+    Program Trace Macrocell
+ETM:
+    Embedded Trace Macrocell
+STM:
+    System trace Macrocell
+ETB:
+    Embedded Trace Buffer
+ITM:
+    Instrumentation Trace Macrocell
+TPIU:
+     Trace Port Interface Unit
+TMC-ETR:
+        Trace Memory Controller, configured as Embedded Trace Router
+TMC-ETF:
+        Trace Memory Controller, configured as Embedded Trace FIFO
+
+Classification:
+
+Source:
+   ETM, STM, ITM
+Link:
+   Funnel, replicator, TMC-ETF
+Sinks:
+   ETB, TPIU, TMC-ETR
+
+Framework and implementation
+----------------------------
+
+The coresight framework provides a central point to represent, configure and
+manage coresight devices on a platform.  Any coresight compliant device can
+register with the framework for as long as they use the right APIs:
+
+.. c:function:: int coresight_register(FAR struct coresight_dev_s *csdev, FAR const struct coresight_desc_s *desc);
+.. c:function:: void coresight_unregister(FAR struct coresight_dev_s *csdev);
+
+``struct coresight_desc *desc`` describes the type of current coresight device
+and where it connects to. When all the coresight devices are registered,
+devices throught the tracing stream path can be enablea by calling:
+
+.. c:function:: int coresight_enable(FAR struct coresight_dev_s *srcdev, FAR struct coresight_dev_s *destdev);
+
+The ``coresight_enable`` function will build the path through srcdev and
+destdev according the ``struct coresight_desc *desc``.
diff --git a/Documentation/guides/index.rst b/Documentation/guides/index.rst
index c25644a14e..11e43cc421 100644
--- a/Documentation/guides/index.rst
+++ b/Documentation/guides/index.rst
@@ -22,6 +22,7 @@ Guides
   nestedinterrupts.rst
   cortexmhardfaults.rst
   coredump.rst
+  coresight.rst
   gdbserver.rst
   gdbwithpython.rst
   ofloader.rst


(nuttx) 08/13: coresight:add tmc device support

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit df33c392aeeae89ed29da42657daed593a0fdb4f
Author: liaoao <li...@xiaomi.com>
AuthorDate: Fri Sep 22 16:13:47 2023 +0800

    coresight:add tmc device support
    
    Signed-off-by: liaoao <li...@xiaomi.com>
---
 drivers/coresight/CMakeLists.txt        |   5 +
 drivers/coresight/Kconfig               |   4 +
 drivers/coresight/Make.defs             |   4 +
 drivers/coresight/coresight_tmc_core.c  | 204 ++++++++++++
 drivers/coresight/coresight_tmc_core.h  | 189 +++++++++++
 drivers/coresight/coresight_tmc_etf.c   | 570 ++++++++++++++++++++++++++++++++
 drivers/coresight/coresight_tmc_etr.c   | 522 +++++++++++++++++++++++++++++
 include/nuttx/coresight/coresight.h     |  10 +-
 include/nuttx/coresight/coresight_tmc.h | 109 ++++++
 9 files changed, 1616 insertions(+), 1 deletion(-)

diff --git a/drivers/coresight/CMakeLists.txt b/drivers/coresight/CMakeLists.txt
index 29bda14e2d..5885df1aef 100644
--- a/drivers/coresight/CMakeLists.txt
+++ b/drivers/coresight/CMakeLists.txt
@@ -41,6 +41,11 @@ if(CONFIG_CORESIGHT)
     list(APPEND SRCS coresight_stm.c)
   endif()
 
+  if(CONFIG_CORESIGHT_TMC)
+    list(APPEND SRCS coresight_tmc_core.c coresight_tmc_etf.c
+         coresight_tmc_etr.c)
+  endif()
+
   if(CONFIG_CORESIGHT_TPIU)
     list(APPEND SRCS coresight_tpiu.c)
   endif()
diff --git a/drivers/coresight/Kconfig b/drivers/coresight/Kconfig
index 3ea611a806..71fa025bc1 100644
--- a/drivers/coresight/Kconfig
+++ b/drivers/coresight/Kconfig
@@ -51,6 +51,10 @@ config CORESIGHT_STM
 	bool "STM coresight device support"
 	default n
 
+config CORESIGHT_TMC
+	bool "TMC coresight device support"
+	default n
+
 config CORESIGHT_TPIU
 	bool "TPIU coresight device support"
 	default n
diff --git a/drivers/coresight/Make.defs b/drivers/coresight/Make.defs
index b3852b0c57..b2219e53db 100644
--- a/drivers/coresight/Make.defs
+++ b/drivers/coresight/Make.defs
@@ -44,6 +44,10 @@ ifeq ($(CONFIG_CORESIGHT_STM),y)
 CSRCS += coresight_stm.c
 endif
 
+ifeq ($(CONFIG_CORESIGHT_TMC),y)
+CSRCS += coresight_tmc_core.c coresight_tmc_etf.c coresight_tmc_etr.c
+endif
+
 ifeq ($(CONFIG_CORESIGHT_TPIU),y)
 CSRCS += coresight_tpiu.c
 endif
diff --git a/drivers/coresight/coresight_tmc_core.c b/drivers/coresight/coresight_tmc_core.c
new file mode 100644
index 0000000000..da5d4fb25c
--- /dev/null
+++ b/drivers/coresight/coresight_tmc_core.c
@@ -0,0 +1,204 @@
+/****************************************************************************
+ * drivers/coresight/coresight_tmc_core.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <errno.h>
+#include <debug.h>
+#include <nuttx/bits.h>
+#include <nuttx/kmalloc.h>
+
+#include <nuttx/coresight/coresight_tmc.h>
+
+#include "coresight_common.h"
+#include "coresight_tmc_core.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tmc_etf_get_memwidth
+ ****************************************************************************/
+
+static enum tmc_mem_intf_width_e tmc_etf_get_memwidth(uint32_t devid)
+{
+  /* Indicate the minimum alignemnt for RRR/RURP/RWP/DBA etc registers. */
+
+  switch (BMVAL(devid, 8, 10))
+  {
+    case 0x2:
+      return TMC_MEM_INTF_WIDTH_32BITS;
+    case 0x3:
+      return TMC_MEM_INTF_WIDTH_64BITS;
+    case 0x4:
+      return TMC_MEM_INTF_WIDTH_128BITS;
+    case 0x5:
+      return TMC_MEM_INTF_WIDTH_256BITS;
+    default:
+      return 0;
+  }
+}
+
+/****************************************************************************
+ * Name: tmc_etr_get_memwidth
+ ****************************************************************************/
+
+static enum tmc_mem_intf_width_e tmc_etr_get_memwidth(uint32_t devid)
+{
+  uint32_t val = (BMVAL(devid, 14, 15) << 3) | BMVAL(devid, 8, 10);
+
+  /* Indicate the minimum alignemnt for RRR/RURP/RWP/DBA etc registers. */
+
+  switch (val)
+  {
+    case 0x5:
+      return TMC_MEM_INTF_WIDTH_32BITS;
+    case 0x6:
+      return TMC_MEM_INTF_WIDTH_64BITS;
+    case 0x7:
+      return TMC_MEM_INTF_WIDTH_128BITS;
+    case 0x8:
+      return TMC_MEM_INTF_WIDTH_256BITS;
+    default:
+      return 0;
+  }
+}
+
+/****************************************************************************
+ * Name: tmc_init_arch_data
+ ****************************************************************************/
+
+static void tmc_init_arch_data(FAR struct coresight_tmc_dev_s *tmcdev,
+                               FAR const struct coresight_desc_s *desc)
+{
+  uint32_t devid;
+
+  coresight_unlock(desc->addr);
+  devid = coresight_get32(desc->addr + CORESIGHT_DEVID);
+  tmcdev->config_type = BMVAL(devid, 6, 7);
+  if (tmcdev->config_type == TMC_CONFIG_TYPE_ETR)
+    {
+      tmcdev->size = desc->buffer_size;
+      tmcdev->burst_size = desc->burst_size;
+      tmcdev->mmwidth = tmc_etr_get_memwidth(devid);
+    }
+  else
+    {
+      tmcdev->size = coresight_get32(desc->addr + TMC_RSZ) * 4;
+      tmcdev->mmwidth = tmc_etf_get_memwidth(devid);
+    }
+
+  coresight_lock(desc->addr);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tmc_register
+ *
+ * Description:
+ *   Register a TMC devices.
+ *
+ * Input Parameters:
+ *   desc  - A description of this coresight device.
+ *
+ * Returned Value:
+ *   Pointer to a TMC device on success; NULL on failure.
+ *
+ ****************************************************************************/
+
+FAR struct coresight_tmc_dev_s *
+tmc_register(FAR const struct coresight_desc_s *desc)
+{
+  FAR struct coresight_tmc_dev_s *tmcdev;
+  int ret = -EINVAL;
+
+  tmcdev = kmm_zalloc(sizeof(struct coresight_tmc_dev_s));
+  if (tmcdev == NULL)
+    {
+      cserr("%s:malloc failed!\n", desc->name);
+      return NULL;
+    }
+
+  tmc_init_arch_data(tmcdev, desc);
+
+  switch (tmcdev->config_type)
+    {
+      case TMC_CONFIG_TYPE_ETB:
+      case TMC_CONFIG_TYPE_ETF:
+        ret = tmc_etf_register(tmcdev, desc);
+        break;
+
+      case TMC_CONFIG_TYPE_ETR:
+        ret = tmc_etr_register(tmcdev, desc);
+        break;
+
+      default:
+        cserr("config type error\n");
+        break;
+    }
+
+  if (ret < 0)
+    {
+      kmm_free(tmcdev);
+      return NULL;
+    }
+
+  nxmutex_init(&tmcdev->lock);
+  return tmcdev;
+}
+
+/****************************************************************************
+ * Name: tmc_unregister
+ *
+ * Description:
+ *   Unregister a TMC devices.
+ *
+ * Input Parameters:
+ *   tmcdev  - Pointer to the TMC device.
+ *
+ ****************************************************************************/
+
+void tmc_unregister(FAR struct coresight_tmc_dev_s *tmcdev)
+{
+  switch (tmcdev->config_type)
+    {
+      case TMC_CONFIG_TYPE_ETB:
+      case TMC_CONFIG_TYPE_ETF:
+        tmc_etf_unregister(tmcdev);
+        break;
+
+      case TMC_CONFIG_TYPE_ETR:
+        tmc_etr_unregister(tmcdev);
+        break;
+
+      default:
+        cserr("wrong config type\n");
+        break;
+    }
+
+  nxmutex_destroy(&tmcdev->lock);
+  kmm_free(tmcdev);
+}
diff --git a/drivers/coresight/coresight_tmc_core.h b/drivers/coresight/coresight_tmc_core.h
new file mode 100644
index 0000000000..7de33133b8
--- /dev/null
+++ b/drivers/coresight/coresight_tmc_core.h
@@ -0,0 +1,189 @@
+/****************************************************************************
+ * drivers/coresight/coresight_tmc_core.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __DRIVERS_CORESIGHT_CORESIGHT_TMC_CORE_H
+#define __DRIVERS_CORESIGHT_CORESIGHT_TMC_CORE_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/bits.h>
+#include <nuttx/coresight/coresight_tmc.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define TMC_RSZ               0x004
+#define TMC_STS               0x00c
+#define TMC_RRD               0x010
+#define TMC_RRP               0x014
+#define TMC_RWP               0x018
+#define TMC_TRG               0x01c
+#define TMC_CTL               0x020
+#define TMC_RWD               0x024
+#define TMC_MODE              0x028
+#define TMC_LBUFLEVEL         0x02c
+#define TMC_CBUFLEVEL         0x030
+#define TMC_BUFWM             0x034
+#define TMC_RRPHI             0x038
+#define TMC_RWPHI             0x03c
+#define TMC_AXICTL            0x110
+#define TMC_DBALO             0x118
+#define TMC_DBAHI             0x11c
+#define TMC_FFSR              0x300
+#define TMC_FFCR              0x304
+#define TMC_PSCR              0x308
+#define TMC_ITMISCOP0         0xee0
+#define TMC_ITTRFLIN          0xee8
+#define TMC_ITATBDATA0        0xeec
+#define TMC_ITATBCTR2         0xef0
+#define TMC_ITATBCTR1         0xef4
+#define TMC_ITATBCTR0         0xef8
+#define TMC_AUTHSTATUS        0xfb8
+
+/* TMC_AUTHSTATUS - 0xfb8 */
+
+#define TMC_AUTH_NSID_MASK    GENMASK(1, 0)
+#define TMC_NSID_EN           0x03
+
+/* TMC_CTL - 0x020 */
+
+#define TMC_CTL_CAPT_EN       BIT(0)
+
+/* TMC_STS - 0x00C */
+
+#define TMC_STS_FULL          BIT(0)
+#define TMC_STS_TRIGGERED     BIT(1)
+#define TMC_STS_TMCREADY      BIT(2)
+#define TMC_STS_MEMERR        BIT(5)
+
+/* TMC_AXICTL - 0x110
+ *
+ * TMC AXICTL format for SoC-400
+ *  Bits [0-1]  : ProtCtrlBit0-1
+ *  Bits [2-5]  : CacheCtrlBits 0-3 (AXCACHE)
+ *  Bit  6      : Reserved
+ *  Bit  7      : ScatterGatherMode
+ *  Bits [8-11] : WrBurstLen
+ *  Bits [12-31]  : Reserved.
+ * TMC AXICTL format for SoC-600, as above except:
+ *  Bits [2-5     : AXI WCACHE
+ *  Bits [16-19]  : AXI RCACHE
+ *  Bits [20-31]  : Reserved
+ */
+
+#define TMC_AXICTL_CLEAR_MASK   0xfbf
+#define TMC_AXICTL_ARCACHE_MASK (0xf << 16)
+
+#define TMC_AXICTL_PROT_CTL_B0  BIT(0)
+#define TMC_AXICTL_PROT_CTL_B1  BIT(1)
+#define TMC_AXICTL_SCT_GAT_MODE BIT(7)
+#define TMC_AXICTL_WR_BURST(v)  (((v) & 0xf) << 8)
+#define TMC_AXICTL_WR_BURST_16  0xf
+
+/* Write-back Read and Write-allocate */
+
+#define TMC_AXICTL_AXCACHE_OS   (0xf << 2)
+#define TMC_AXICTL_ARCACHE_OS   (0xf << 16)
+
+/* TMC_FFCR - 0x304 */
+
+#define TMC_FFCR_EN_FMT         BIT(0)
+#define TMC_FFCR_EN_TI          BIT(1)
+#define TMC_FFCR_FON_FLIN       BIT(4)
+#define TMC_FFCR_FON_TRIG_EVT   BIT(5)
+#define TMC_FFCR_FON_MAN        BIT(6)
+#define TMC_FFCR_TRIGON_TRIGIN  BIT(8)
+#define TMC_FFCR_STOP_ON_FLUSH  BIT(12)
+
+#define TMC_DEVID_AXIAW_VALID   BIT(16)
+#define TMC_DEVID_NOSCAT        BIT(24)
+#define TMC_DEVID_AXIAW_SHIFT   17
+#define TMC_DEVID_AXIAW_MASK    0x7f
+
+/* TMC ETR Capability bit definitions. These need to be set by software. */
+
+#define TMC_ETR_SG              (0x1U << 0)
+
+/* ETR has separate read/write cache encodings. */
+
+#define TMC_ETR_AXI_ARCACHE     (0x1U << 1)
+
+/* TMC_ETR_SAVE_RESTORE - Values of RRP/RWP/STS.Full are
+ * retained when TMC leaves Disabled state, allowing us to continue
+ * the tracing from a point where we stopped. This also implies that
+ * the RRP/RWP/STS.Full should always be programmed to the correct
+ * value. Unfortunately this is not advertised by the hardware,
+ * so we have to rely on PID of the IP to detect the functionality.
+ */
+
+#define TMC_ETR_SAVE_RESTORE    (0x1U << 2)
+
+/* Coresight SoC-600 TMC-ETR unadvertised capabilities */
+
+#define TMC_600_ETR_CAPS  \
+  (TMC_ETR_SAVE_RESTORE | TMC_ETR_AXI_ARCACHE)
+
+#define TMC_MAX_NAME_LEN        32
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+enum tmc_mode_e
+{
+  TMC_MODE_CIRCULAR_BUFFER,
+  TMC_MODE_SOFTWARE_FIFO,
+  TMC_MODE_HARDWARE_FIFO,
+};
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tmc_etf_register
+ ****************************************************************************/
+
+int tmc_etf_register(FAR struct coresight_tmc_dev_s *tmcdev,
+                     FAR const struct coresight_desc_s *desc);
+
+/****************************************************************************
+ * Name: tmc_etr_register
+ ****************************************************************************/
+
+int tmc_etr_register(FAR struct coresight_tmc_dev_s *tmcdev,
+                     FAR const struct coresight_desc_s *desc);
+
+/****************************************************************************
+ * Name: tmc_etf_unregister
+ ****************************************************************************/
+
+void tmc_etf_unregister(FAR struct coresight_tmc_dev_s * tmcdev);
+
+/****************************************************************************
+ * Name: tmc_etr_unregister
+ ****************************************************************************/
+
+void tmc_etr_unregister(FAR struct coresight_tmc_dev_s * tmcdev);
+
+#endif  //__DRIVERS_CORESIGHT_CORESIGHT_TMC_CORE_H
diff --git a/drivers/coresight/coresight_tmc_etf.c b/drivers/coresight/coresight_tmc_etf.c
new file mode 100644
index 0000000000..1a68559835
--- /dev/null
+++ b/drivers/coresight/coresight_tmc_etf.c
@@ -0,0 +1,570 @@
+/****************************************************************************
+ * drivers/coresight/coresight_tmc_etf.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <errno.h>
+#include <debug.h>
+#include <stdio.h>
+#include <string.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/irq.h>
+
+#include <nuttx/coresight/coresight_tmc.h>
+
+#include "coresight_common.h"
+#include "coresight_tmc_core.h"
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+static int tmc_etf_sink_enable(FAR struct coresight_dev_s *csdev);
+static void tmc_etf_sink_disable(FAR struct coresight_dev_s *csdev);
+static int tmc_etf_link_enable(FAR struct coresight_dev_s *csdev,
+                               int iport, int oport);
+static void tmc_etf_link_disable(FAR struct coresight_dev_s *csdev,
+                                 int iport, int oport);
+
+static int tmc_etf_open(FAR struct file *filep);
+static int tmc_etf_close(FAR struct file *filep);
+static ssize_t tmc_etf_read(FAR struct file *filep, FAR char *buffer,
+                            size_t buflen);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* TMC-ETB and TMC-ETF sink device operations. */
+
+static const struct coresight_sink_ops_s g_tmc_etf_sink_ops =
+{
+  .enable  = tmc_etf_sink_enable,
+  .disable = tmc_etf_sink_disable,
+};
+
+static const struct coresight_ops_s g_tmc_sink_ops =
+{
+  .sink_ops = &g_tmc_etf_sink_ops,
+};
+
+/* TMC-ETF link device operations. */
+
+static const struct coresight_link_ops_s g_tmc_etf_link_ops =
+{
+  .enable  = tmc_etf_link_enable,
+  .disable = tmc_etf_link_disable,
+};
+
+static const struct coresight_ops_s g_tmc_link_ops =
+{
+  .link_ops = &g_tmc_etf_link_ops,
+};
+
+static const struct file_operations g_tmc_fops =
+{
+  tmc_etf_open,  /* open */
+  tmc_etf_close, /* close */
+  tmc_etf_read,  /* read */
+  NULL,          /* write */
+  NULL,          /* seek */
+  NULL,          /* ioctl */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tmc_etf_sink_hw_enable
+ ****************************************************************************/
+
+static int tmc_etf_sink_hw_enable(FAR struct coresight_tmc_dev_s *tmcdev)
+{
+  coresight_unlock(tmcdev->csdev.addr);
+
+  /* Wait for TMCSReady bit to be set. */
+
+  if (coresight_timeout(TMC_STS_TMCREADY, TMC_STS_TMCREADY,
+                        tmcdev->csdev.addr + TMC_STS) < 0)
+    {
+      cserr("tmc device is not ready\n");
+      coresight_lock(tmcdev->csdev.addr);
+      return -EAGAIN;
+    }
+
+  /* TMC-ETB and TMC-ETF sink device use cirular buffer mode. */
+
+  coresight_put32(TMC_MODE_CIRCULAR_BUFFER, tmcdev->csdev.addr + TMC_MODE);
+  coresight_put32(TMC_FFCR_EN_FMT | TMC_FFCR_EN_TI | TMC_FFCR_FON_FLIN |
+                  TMC_FFCR_FON_TRIG_EVT | TMC_FFCR_TRIGON_TRIGIN,
+                  tmcdev->csdev.addr + TMC_FFCR);
+  coresight_put32(tmcdev->trigger_cntr, tmcdev->csdev.addr + TMC_TRG);
+
+  /* Enable capture. */
+
+  coresight_put32(TMC_CTL_CAPT_EN, tmcdev->csdev.addr + TMC_CTL);
+
+  coresight_lock(tmcdev->csdev.addr);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: tmc_etf_link_hw_enable
+ ****************************************************************************/
+
+static int tmc_etf_link_hw_enable(FAR struct coresight_tmc_dev_s *tmcdev)
+{
+  coresight_unlock(tmcdev->csdev.addr);
+
+  /* Wait for TMCSReady bit to be set. */
+
+  if (coresight_timeout(TMC_STS_TMCREADY, TMC_STS_TMCREADY,
+                        tmcdev->csdev.addr + TMC_STS) < 0)
+    {
+      cserr("tmc device is not ready\n");
+      coresight_lock(tmcdev->csdev.addr);
+      return -EAGAIN;
+    }
+
+  /* TMC-ETF link device use Hardware FIFO buffer mode. */
+
+  coresight_put32(TMC_MODE_HARDWARE_FIFO, tmcdev->csdev.addr + TMC_MODE);
+  coresight_put32(TMC_FFCR_EN_FMT | TMC_FFCR_EN_TI,
+                  tmcdev->csdev.addr + TMC_FFCR);
+  coresight_put32(0x0, tmcdev->csdev.addr + TMC_BUFWM);
+
+  /* Enable capture. */
+
+  coresight_put32(TMC_CTL_CAPT_EN, tmcdev->csdev.addr + TMC_CTL);
+
+  coresight_lock(tmcdev->csdev.addr);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: tmc_flush_and_stop
+ ****************************************************************************/
+
+static void tmc_flush_and_stop(FAR struct coresight_tmc_dev_s *tmcdev)
+{
+  coresight_modify32(TMC_FFCR_STOP_ON_FLUSH, TMC_FFCR_STOP_ON_FLUSH,
+                     tmcdev->csdev.addr + TMC_FFCR);
+  coresight_modify32(TMC_FFCR_FON_MAN, TMC_FFCR_FON_MAN,
+                     tmcdev->csdev.addr + TMC_FFCR);
+  if (coresight_timeout(0x0, TMC_FFCR_FON_MAN,
+                        tmcdev->csdev.addr + TMC_FFCR) < 0)
+    {
+      cserr("timeout while waiting for completion of Manual Flush\n");
+    }
+
+  if (coresight_timeout(TMC_STS_TMCREADY, TMC_STS_TMCREADY,
+                        tmcdev->csdev.addr + TMC_STS) < 0)
+    {
+      cserr("timeout while waiting for TMC to be Ready\n");
+    }
+}
+
+/****************************************************************************
+ * Name: tmc_etf_hw_read
+ *
+ * Description:
+ *   Dump ETB RAM buffer to device's buffer for usrspace's read. It just need
+ *   to performing successive reads to the RRD Register, until the value
+ *   0xFFFFFFFF is returned whick is kind different from coresight ETB
+ *   device's reading process. refers to TRM.
+ *
+ ****************************************************************************/
+
+static void tmc_etf_hw_read(FAR struct coresight_tmc_dev_s *tmcdev)
+{
+  FAR uint32_t *bufptr;
+  uint32_t read_data;
+
+  bufptr = tmcdev->buf;
+  tmcdev->len = 0;
+  for (; ; )
+    {
+      read_data = coresight_get32(tmcdev->csdev.addr + TMC_RRD);
+      if (read_data == 0xffffffff)
+        {
+          break;
+        }
+
+      memcpy(bufptr, &read_data, 4);
+      bufptr += 1;
+      tmcdev->len += 4;
+    }
+
+  if ((coresight_get32(tmcdev->csdev.addr + TMC_STS) & TMC_STS_FULL) == 1)
+    {
+      coresight_insert_barrier_packet(tmcdev->buf);
+    }
+}
+
+/****************************************************************************
+ * Name: tmc_etf_hw_disable_and_read
+ *
+ * Description:
+ *   Used for ETF sink devices to dump trace buffer. Do not dump trace buffer
+ *   in tmc_etf_hw_disable to avoid trace buffer's data confusion when a
+ *   process is reading trace buffer and anther process calles tmc_disable.
+ *
+ ****************************************************************************/
+
+static void
+tmc_etf_hw_disable_and_read(FAR struct coresight_tmc_dev_s *tmcdev)
+{
+  coresight_unlock(tmcdev->csdev.addr);
+  tmc_flush_and_stop(tmcdev);
+
+  /* This is kind different from ETB coresight device, it should read data
+   * from ram buffer in stopped state rather then disabled state.
+   */
+
+  tmc_etf_hw_read(tmcdev);
+
+  /* Disable capture enable bit. */
+
+  coresight_put32(0x0, tmcdev->csdev.addr + TMC_CTL);
+  coresight_lock(tmcdev->csdev.addr);
+}
+
+/****************************************************************************
+ * Name: tmc_etf_hw_disable
+ *
+ * Description:
+ *   ETF link devices and ETF sink devices share same disable flow.
+ *
+ ****************************************************************************/
+
+static void tmc_etf_hw_disable(FAR struct coresight_tmc_dev_s *tmcdev)
+{
+  coresight_unlock(tmcdev->csdev.addr);
+  tmc_flush_and_stop(tmcdev);
+
+  /* Disable capture enable bit. */
+
+  coresight_put32(0x0, tmcdev->csdev.addr + TMC_CTL);
+  coresight_lock(tmcdev->csdev.addr);
+}
+
+/****************************************************************************
+ * Name: tmc_etf_sink_enable
+ ****************************************************************************/
+
+static int tmc_etf_sink_enable(FAR struct coresight_dev_s *csdev)
+{
+  FAR struct coresight_tmc_dev_s *tmcdev =
+    (FAR struct coresight_tmc_dev_s *)csdev;
+  int ret = 0;
+
+  if (tmcdev->refcnt++ == 0)
+    {
+      ret = coresight_claim_device(tmcdev->csdev.addr);
+      if (ret < 0)
+        {
+          tmcdev->refcnt--;
+          cserr("%s claimed failed\n", csdev->name);
+          return ret;
+        }
+
+      ret = tmc_etf_sink_hw_enable(tmcdev);
+      if (ret < 0)
+        {
+          tmcdev->refcnt--;
+          coresight_disclaim_device(tmcdev->csdev.addr);
+          cserr("%s enable failed\n", csdev->name);
+        }
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: tmc_etf_sink_disable
+ ****************************************************************************/
+
+static void tmc_etf_sink_disable(FAR struct coresight_dev_s *csdev)
+{
+  FAR struct coresight_tmc_dev_s *tmcdev =
+    (FAR struct coresight_tmc_dev_s *)csdev;
+
+  if (--tmcdev->refcnt == 0)
+    {
+      tmc_etf_hw_disable(tmcdev);
+      coresight_disclaim_device(tmcdev->csdev.addr);
+      csinfo("%s disabled\n", csdev->name);
+    }
+}
+
+/****************************************************************************
+ * Name: tmc_etf_link_enable
+ ****************************************************************************/
+
+static int tmc_etf_link_enable(FAR struct coresight_dev_s *csdev,
+                               int iport, int oport)
+{
+  FAR struct coresight_tmc_dev_s *tmcdev =
+    (FAR struct coresight_tmc_dev_s *)csdev;
+  int ret = 0;
+
+  if (tmcdev->refcnt++ == 0)
+    {
+      ret = coresight_claim_device(tmcdev->csdev.addr);
+      if (ret < 0)
+        {
+          tmcdev->refcnt--;
+          cserr("%s claimed failed\n", csdev->name);
+          return ret;
+        }
+
+      ret = tmc_etf_link_hw_enable(tmcdev);
+      if (ret < 0)
+        {
+          tmcdev->refcnt--;
+          coresight_disclaim_device(tmcdev->csdev.addr);
+          cserr("%s enable failed\n", csdev->name);
+        }
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: tmc_etf_link_disable
+ ****************************************************************************/
+
+static void tmc_etf_link_disable(FAR struct coresight_dev_s *csdev,
+                                 int iport, int oport)
+{
+  FAR struct coresight_tmc_dev_s *tmcdev =
+    (FAR struct coresight_tmc_dev_s *)csdev;
+
+  if (--tmcdev->refcnt == 0)
+    {
+      tmc_etf_hw_disable(tmcdev);
+      coresight_disclaim_device(tmcdev->csdev.addr);
+      csinfo("%s disabled\n", csdev->name);
+    }
+}
+
+/****************************************************************************
+ * Name: tmc_etf_open
+ ****************************************************************************/
+
+static int tmc_etf_open(FAR struct file *filep)
+{
+  FAR struct inode *inode = filep->f_inode;
+  FAR struct coresight_tmc_dev_s *tmcdev;
+  int ret;
+
+  DEBUGASSERT(inode->i_private);
+  tmcdev = (FAR struct coresight_tmc_dev_s *)inode->i_private;
+
+  ret = nxmutex_lock(&tmcdev->lock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  if (tmcdev->opencnt++ == 0)
+    {
+      tmcdev->buf = kmm_zalloc(tmcdev->size);
+      if (tmcdev->buf == NULL)
+        {
+          cserr("malloc buffer failed\n");
+          tmcdev->opencnt--;
+          ret = -ENOMEM;
+        }
+      else
+        {
+          irqstate_t flags;
+
+          flags = enter_critical_section();
+          if (tmcdev->refcnt > 0)
+            {
+              tmc_etf_hw_disable_and_read(tmcdev);
+              tmc_etf_sink_hw_enable(tmcdev);
+            }
+          else
+            {
+              /* ETF devices can not read buffer directly when it is not
+               * enabled like etb device does. Its buffer needs to be
+               * captured in stopped state which is transferred after it
+               * hase been enabled.
+               */
+
+              kmm_free(tmcdev->buf);
+              tmcdev->opencnt--;
+              ret = -EACCES;
+            }
+
+          leave_critical_section(flags);
+        }
+    }
+
+  nxmutex_unlock(&tmcdev->lock);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: tmc_etf_close
+ ****************************************************************************/
+
+static int tmc_etf_close(FAR struct file *filep)
+{
+  FAR struct inode *inode = filep->f_inode;
+  FAR struct coresight_tmc_dev_s *tmcdev;
+  int ret;
+
+  DEBUGASSERT(inode->i_private);
+  tmcdev = (FAR struct coresight_tmc_dev_s *)inode->i_private;
+
+  ret = nxmutex_lock(&tmcdev->lock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  if (--tmcdev->opencnt == 0)
+    {
+      kmm_free(tmcdev->buf);
+    }
+
+  nxmutex_unlock(&tmcdev->lock);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: tmc_etf_read
+ ****************************************************************************/
+
+static ssize_t tmc_etf_read(FAR struct file *filep, FAR char *buffer,
+                            size_t buflen)
+{
+  FAR struct inode *inode = filep->f_inode;
+  FAR struct coresight_tmc_dev_s *tmcdev;
+
+  DEBUGASSERT(inode->i_private);
+  tmcdev = (FAR struct coresight_tmc_dev_s *)inode->i_private;
+
+  if (filep->f_pos > tmcdev->len)
+    {
+      return 0;
+    }
+
+  if (filep->f_pos + buflen > tmcdev->len)
+    {
+      buflen = tmcdev->len - filep->f_pos;
+    }
+
+  memcpy(buffer, (FAR char *)tmcdev->buf + filep->f_pos, buflen);
+  filep->f_pos += buflen;
+
+  return buflen;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tmc_etf_register
+ ****************************************************************************/
+
+int tmc_etf_register(FAR struct coresight_tmc_dev_s * tmcdev,
+                     FAR const struct coresight_desc_s *desc)
+{
+  int ret;
+
+  if (desc->type == CORESIGHT_DEV_TYPE_SINK)
+    {
+      enum coresight_dev_subtype_sink_e subtype = desc->subtype.sink_subtype;
+      char pathname[TMC_MAX_NAME_LEN];
+
+      if ((subtype != CORESIGHT_DEV_SUBTYPE_SINK_TMC_BUFFER) &&
+          (subtype != CORESIGHT_DEV_SUBTYPE_SINK_TMC_ETF))
+        {
+          cserr("unsupported tmc device type\n");
+          return -EPERM;
+        }
+
+      tmcdev->csdev.ops = &g_tmc_sink_ops;
+      ret = coresight_register(&tmcdev->csdev, desc);
+      if (ret < 0)
+        {
+          cserr("%s:coresight register failed\n", desc->name);
+          return ret;
+        }
+
+      snprintf(pathname, sizeof(pathname), "/dev/%s", desc->name);
+      ret = register_driver(pathname, &g_tmc_fops, 0444, tmcdev);
+      if (ret < 0)
+        {
+          cserr("%s:driver register failed\n", desc->name);
+          coresight_unregister(&tmcdev->csdev);
+          return ret;
+        }
+    }
+  else if (desc->type == CORESIGHT_DEV_TYPE_LINK)
+    {
+      if (desc->subtype.link_subtype != CORESIGHT_DEV_SUBTYPE_LINK_FIFO)
+        {
+          cserr("unsupported tmc link device type\n");
+          return -EPERM;
+        }
+
+      tmcdev->csdev.ops = &g_tmc_link_ops;
+      ret = coresight_register(&tmcdev->csdev, desc);
+      if (ret < 0)
+        {
+          cserr("%s:coresight register failed\n", desc->name);
+          return ret;
+        }
+    }
+  else
+    {
+      cserr("unsupported tmc device type\n");
+      return -EPERM;
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: tmc_etf_unregister
+ ****************************************************************************/
+
+void tmc_etf_unregister(FAR struct coresight_tmc_dev_s * tmcdev)
+{
+  if (tmcdev->csdev.type == CORESIGHT_DEV_TYPE_SINK)
+    {
+      char pathname[TMC_MAX_NAME_LEN];
+
+      snprintf(pathname, sizeof(pathname), "/dev/%s", tmcdev->csdev.name);
+      unregister_driver(pathname);
+    }
+
+  coresight_unregister(&tmcdev->csdev);
+}
diff --git a/drivers/coresight/coresight_tmc_etr.c b/drivers/coresight/coresight_tmc_etr.c
new file mode 100644
index 0000000000..37ea6d23df
--- /dev/null
+++ b/drivers/coresight/coresight_tmc_etr.c
@@ -0,0 +1,522 @@
+/****************************************************************************
+ * drivers/coresight/coresight_tmc_etr.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <errno.h>
+#include <debug.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/cache.h>
+
+#include <nuttx/coresight/coresight_tmc.h>
+
+#include "coresight_common.h"
+#include "coresight_tmc_core.h"
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+static int tmc_etr_enable(FAR struct coresight_dev_s *csdev);
+static void tmc_etr_disable(FAR struct coresight_dev_s *csdev);
+
+static int tmc_etr_open(FAR struct file *filep);
+static int tmc_etr_close(FAR struct file *filep);
+static ssize_t tmc_etr_read(FAR struct file *filep, FAR char *buffer,
+                            size_t buflen);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct coresight_sink_ops_s g_tmc_etr_sink_ops =
+{
+  .enable  = tmc_etr_enable,
+  .disable = tmc_etr_disable,
+};
+
+static const struct coresight_ops_s g_tmc_sink_ops =
+{
+  .sink_ops = &g_tmc_etr_sink_ops,
+};
+
+static const struct file_operations g_tmc_fops =
+{
+  tmc_etr_open,  /* open */
+  tmc_etr_close, /* close */
+  tmc_etr_read,  /* read */
+  NULL,          /* write */
+  NULL,          /* seek */
+  NULL,          /* ioctl */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tmc_etr_hw_enable
+ ****************************************************************************/
+
+static int tmc_etr_hw_enable(FAR struct coresight_tmc_dev_s *tmcdev)
+{
+  uint32_t axictl;
+
+  coresight_unlock(tmcdev->csdev.addr);
+
+  /* Wait for TMCSReady bit to be set. */
+
+  if (coresight_timeout(TMC_STS_TMCREADY, TMC_STS_TMCREADY,
+                        tmcdev->csdev.addr + TMC_STS) < 0)
+    {
+      cserr("tmc device is not ready\n");
+      coresight_lock(tmcdev->csdev.addr);
+      return -EAGAIN;
+    }
+
+  coresight_put32(tmcdev->size / 4, tmcdev->csdev.addr + TMC_RSZ);
+  coresight_put32(TMC_MODE_CIRCULAR_BUFFER, tmcdev->csdev.addr + TMC_MODE);
+
+  /* Set AXICTL. */
+
+  axictl = coresight_get32(tmcdev->csdev.addr + TMC_AXICTL);
+  axictl &= ~TMC_AXICTL_CLEAR_MASK;
+  axictl |= TMC_AXICTL_PROT_CTL_B1;
+  axictl |= TMC_AXICTL_WR_BURST(tmcdev->burst_size);
+  axictl |= TMC_AXICTL_AXCACHE_OS;
+  if (tmcdev->caps & TMC_ETR_AXI_ARCACHE)
+    {
+      axictl &= ~TMC_AXICTL_ARCACHE_MASK;
+      axictl |= TMC_AXICTL_ARCACHE_OS;
+    }
+
+  if (tmcdev->mode == TMC_ETR_MODE_ETR_SG)
+    {
+      axictl |= TMC_AXICTL_SCT_GAT_MODE;
+    }
+
+  coresight_put32(axictl, tmcdev->csdev.addr + TMC_AXICTL);
+  coresight_put32((uintptr_t)tmcdev->buf, tmcdev->csdev.addr + TMC_DBALO);
+  coresight_put32(((uint64_t)(uintptr_t)tmcdev->buf) >> 32,
+                  tmcdev->csdev.addr + TMC_DBAHI);
+
+  if (tmcdev->caps & TMC_ETR_SAVE_RESTORE)
+    {
+      coresight_put32((uint32_t)tmcdev->buf, tmcdev->csdev.addr + TMC_RRP);
+      coresight_put32(((uint64_t)(uintptr_t)tmcdev->buf >> 32),
+                      tmcdev->csdev.addr + TMC_RRPHI);
+
+      coresight_put32((uint32_t)tmcdev->buf, tmcdev->csdev.addr + TMC_RWP);
+      coresight_put32(((uint64_t)(uintptr_t)tmcdev->buf >> 32),
+                      tmcdev->csdev.addr + TMC_RWPHI);
+
+      coresight_modify32(0x0, TMC_STS_FULL, tmcdev->csdev.addr + TMC_STS);
+    }
+
+  coresight_put32(TMC_FFCR_EN_FMT | TMC_FFCR_EN_TI | TMC_FFCR_FON_FLIN |
+                  TMC_FFCR_FON_TRIG_EVT | TMC_FFCR_TRIGON_TRIGIN,
+                  tmcdev->csdev.addr + TMC_FFCR);
+  coresight_put32(tmcdev->trigger_cntr, tmcdev->csdev.addr + TMC_TRG);
+
+  /* Enable capture. */
+
+  coresight_put32(TMC_CTL_CAPT_EN, tmcdev->csdev.addr + TMC_CTL);
+
+  coresight_lock(tmcdev->csdev.addr);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: tmc_flush_and_stop
+ ****************************************************************************/
+
+static void tmc_flush_and_stop(FAR struct coresight_tmc_dev_s *tmcdev)
+{
+  coresight_modify32(TMC_FFCR_STOP_ON_FLUSH, TMC_FFCR_STOP_ON_FLUSH,
+                     tmcdev->csdev.addr + TMC_FFCR);
+  coresight_modify32(TMC_FFCR_FON_MAN, TMC_FFCR_FON_MAN,
+                     tmcdev->csdev.addr + TMC_FFCR);
+  if (coresight_timeout(0x0, TMC_FFCR_FON_MAN,
+                        tmcdev->csdev.addr + TMC_FFCR) < 0)
+    {
+      cserr("timeout while waiting for completion of Manual Flush\n");
+    }
+
+  if (coresight_timeout(TMC_STS_TMCREADY, TMC_STS_TMCREADY,
+                        tmcdev->csdev.addr + TMC_STS) < 0)
+    {
+      cserr("timeout while waiting for TMC to be Ready\n");
+    }
+}
+
+/****************************************************************************
+ * Name: tmc_etr_hw_read
+ ****************************************************************************/
+
+static void tmc_etr_hw_read(FAR struct coresight_tmc_dev_s *tmcdev)
+{
+  uintptr_t rrp;
+  uintptr_t rwp;
+  uint32_t status;
+  bool lost = false;
+
+  rrp = (uint64_t)coresight_get32(tmcdev->csdev.addr + TMC_RRPHI) << 32 |
+        coresight_get32(tmcdev->csdev.addr + TMC_RRP);
+  rwp = (uint64_t)coresight_get32(tmcdev->csdev.addr + TMC_RWPHI) << 32 |
+        coresight_get32(tmcdev->csdev.addr + TMC_RWP);
+  status = coresight_get32(tmcdev->csdev.addr + TMC_STS);
+
+  /* If there were memory errors in the session, truncate the buffer. */
+
+  if (status & TMC_STS_MEMERR)
+    {
+      cserr("tmc memory error detected, truncating buffer\n");
+      tmcdev->len = 0;
+      return;
+    }
+
+  if ((status & TMC_STS_FULL) == 1)
+    {
+      lost = true;
+    }
+
+  tmcdev->offset = rrp - (uintptr_t)tmcdev->buf;
+  if (lost == true)
+    {
+      tmcdev->len = tmcdev->size;
+    }
+  else
+    {
+      tmcdev->len = (uint32_t)(rwp - rrp);
+    }
+
+  if (tmcdev->offset + tmcdev->len > tmcdev->size)
+    {
+      up_invalidate_dcache((uintptr_t)tmcdev->buf, tmcdev->size);
+    }
+  else
+    {
+      up_invalidate_dcache((uintptr_t)((FAR char *)tmcdev->buf +
+                                       tmcdev->offset), tmcdev->len);
+    }
+
+  if (lost == true)
+    {
+      coresight_insert_barrier_packet((FAR char *)tmcdev->buf +
+                                      tmcdev->offset);
+    }
+}
+
+/****************************************************************************
+ * Name: tmc_etr_hw_disable_and_read
+ ****************************************************************************/
+
+static void
+tmc_etr_hw_disable_and_read(FAR struct coresight_tmc_dev_s *tmcdev)
+{
+  coresight_unlock(tmcdev->csdev.addr);
+  tmc_flush_and_stop(tmcdev);
+  tmc_etr_hw_read(tmcdev);
+
+  /* Disable capture enable bit. */
+
+  coresight_put32(0x0, tmcdev->csdev.addr + TMC_CTL);
+  coresight_lock(tmcdev->csdev.addr);
+}
+
+/****************************************************************************
+ * Name: tmc_etr_hw_disable
+ ****************************************************************************/
+
+static void tmc_etr_hw_disable(FAR struct coresight_tmc_dev_s *tmcdev)
+{
+  coresight_unlock(tmcdev->csdev.addr);
+  tmc_flush_and_stop(tmcdev);
+
+  /* Disable capture enable bit. */
+
+  coresight_put32(0x0, tmcdev->csdev.addr + TMC_CTL);
+  coresight_lock(tmcdev->csdev.addr);
+}
+
+/****************************************************************************
+ * Name: tmc_etr_enable
+ ****************************************************************************/
+
+static int tmc_etr_enable(FAR struct coresight_dev_s *csdev)
+{
+  FAR struct coresight_tmc_dev_s *tmcdev =
+    (FAR struct coresight_tmc_dev_s *)csdev;
+  int ret = 0;
+
+  if (tmcdev->refcnt++ == 0)
+    {
+      ret = coresight_claim_device(tmcdev->csdev.addr);
+      if (ret < 0)
+        {
+          tmcdev->refcnt--;
+          cserr("%s claimed failed\n", csdev->name);
+          return ret;
+        }
+
+      ret = tmc_etr_hw_enable(tmcdev);
+      if (ret < 0)
+        {
+          tmcdev->refcnt--;
+          coresight_disclaim_device(tmcdev->csdev.addr);
+          cserr("%s enabled failed\n", csdev->name);
+        }
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: tmc_etr_disable
+ ****************************************************************************/
+
+static void tmc_etr_disable(FAR struct coresight_dev_s *csdev)
+{
+  FAR struct coresight_tmc_dev_s *tmcdev =
+    (FAR struct coresight_tmc_dev_s *)csdev;
+
+  if (--tmcdev->refcnt == 0)
+    {
+      tmc_etr_hw_disable(tmcdev);
+      coresight_disclaim_device(tmcdev->csdev.addr);
+      csinfo("%s disabled\n", csdev->name);
+    }
+}
+
+/****************************************************************************
+ * Name: tmc_etr_open
+ *
+ * Description:
+ *   TMC etr devices write data directly to system memory, it can not enable
+ *   after disable like tmc_etf_open does, which may cause data confusion.
+ *
+ ****************************************************************************/
+
+static int tmc_etr_open(FAR struct file *filep)
+{
+  FAR struct inode *inode = filep->f_inode;
+  FAR struct coresight_tmc_dev_s *tmcdev;
+  int ret;
+
+  DEBUGASSERT(inode->i_private);
+  tmcdev = (FAR struct coresight_tmc_dev_s *)inode->i_private;
+
+  ret = nxmutex_lock(&tmcdev->lock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  if (tmcdev->opencnt++ == 0)
+    {
+      irqstate_t flags;
+
+      flags = enter_critical_section();
+      if (tmcdev->refcnt > 0)
+        {
+          tmc_etr_hw_disable_and_read(tmcdev);
+        }
+      else
+        {
+          /* ETR devices can not read buffer directly when it is not
+           * enabled like etb device does. Its buffer needs to be
+           * captured in stopped state which is transferred after it
+           * hase been enabled.
+           */
+
+          tmcdev->opencnt--;
+          ret = -EACCES;
+        }
+
+      leave_critical_section(flags);
+    }
+
+  nxmutex_unlock(&tmcdev->lock);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: tmc_etr_close
+ ****************************************************************************/
+
+static int tmc_etr_close(FAR struct file *filep)
+{
+  FAR struct inode *inode = filep->f_inode;
+  FAR struct coresight_tmc_dev_s *tmcdev;
+  int ret;
+
+  DEBUGASSERT(inode->i_private);
+  tmcdev = (FAR struct coresight_tmc_dev_s *)inode->i_private;
+
+  ret = nxmutex_lock(&tmcdev->lock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  if (--tmcdev->opencnt == 0)
+    {
+      irqstate_t flags;
+
+      flags = enter_critical_section();
+      if (tmcdev->refcnt > 0)
+        {
+          if (tmc_etr_hw_enable(tmcdev) < 0)
+            {
+              cserr("%s enabled failed after read\n", tmcdev->csdev.name);
+            }
+        }
+
+      leave_critical_section(flags);
+    }
+
+  nxmutex_unlock(&tmcdev->lock);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: tmc_etr_read
+ ****************************************************************************/
+
+static ssize_t tmc_etr_read(FAR struct file *filep, FAR char *buffer,
+                            size_t buflen)
+{
+  FAR struct inode *inode = filep->f_inode;
+  FAR struct coresight_tmc_dev_s *tmcdev;
+  off_t off;
+
+  DEBUGASSERT(inode->i_private);
+  tmcdev = (FAR struct coresight_tmc_dev_s *)inode->i_private;
+
+  if (filep->f_pos > tmcdev->len)
+    {
+      return 0;
+    }
+
+  if (filep->f_pos + buflen > tmcdev->len)
+    {
+      buflen = tmcdev->len - filep->f_pos;
+    }
+
+  /* Compute the offset from which we read the data. */
+
+  off = tmcdev->offset + filep->f_pos;
+  if (off >= tmcdev->size)
+    {
+      off -= tmcdev->size;
+    }
+
+  /* Adjust the length to limit this transaction to end of buffer. */
+
+  if (buflen > tmcdev->size - off)
+    {
+      buflen = tmcdev->size - off;
+    }
+
+  memcpy(buffer, (FAR char *)tmcdev->buf + off, buflen);
+  filep->f_pos += buflen;
+
+  return buflen;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tmc_etr_register
+ ****************************************************************************/
+
+int tmc_etr_register(FAR struct coresight_tmc_dev_s *tmcdev,
+                     FAR const struct coresight_desc_s *desc)
+{
+  char pathname[TMC_MAX_NAME_LEN];
+  int ret;
+
+  /* Check for AXI access. */
+
+  if ((coresight_get32(desc->addr + TMC_AUTHSTATUS) &
+       TMC_AUTH_NSID_MASK) != TMC_NSID_EN)
+    {
+      return -EACCES;
+    }
+
+  if (desc->subtype.sink_subtype != CORESIGHT_DEV_SUBTYPE_SINK_TMC_SYSMEM)
+    {
+      cserr("unsupported tmc device type\n");
+      return -EPERM;
+    }
+
+  tmcdev->mode = TMC_ETR_MODE_FLAT;
+  tmcdev->buf = kmm_zalloc(tmcdev->size);
+  if (tmcdev->buf == NULL)
+    {
+      return -ENOMEM;
+    }
+
+  tmcdev->csdev.ops = &g_tmc_sink_ops;
+  ret = coresight_register(&tmcdev->csdev, desc);
+  if (ret < 0)
+    {
+      cserr("%s:coresight register failed\n", desc->name);
+      goto cs_err;
+    }
+
+  snprintf(pathname, sizeof(pathname), "/dev/%s", desc->name);
+  ret = register_driver(pathname, &g_tmc_fops, 0444, tmcdev);
+  if (ret < 0)
+    {
+      cserr("%s:driver register failed\n", desc->name);
+      goto drv_err;
+    }
+
+  return ret;
+
+drv_err:
+  coresight_unregister(&tmcdev->csdev);
+cs_err:
+  kmm_free(tmcdev->buf);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: tmc_etr_unregister
+ ****************************************************************************/
+
+void tmc_etr_unregister(FAR struct coresight_tmc_dev_s * tmcdev)
+{
+  char pathname[TMC_MAX_NAME_LEN];
+
+  snprintf(pathname, sizeof(pathname), "/dev/%s", tmcdev->csdev.name);
+  unregister_driver(pathname);
+  coresight_unregister(&tmcdev->csdev);
+  kmm_free(tmcdev->buf);
+}
diff --git a/include/nuttx/coresight/coresight.h b/include/nuttx/coresight/coresight.h
index 178a332304..9261cafd57 100644
--- a/include/nuttx/coresight/coresight.h
+++ b/include/nuttx/coresight/coresight.h
@@ -54,13 +54,16 @@ enum coresight_dev_subtype_link_e
 {
   CORESIGHT_DEV_SUBTYPE_LINK_MERG,         /* Funnel */
   CORESIGHT_DEV_SUBTYPE_LINK_SPLIT,        /* Replocator */
-  CORESIGHT_DEV_SUBTYPE_LINK_FIFO,         /* TMC ETR */
+  CORESIGHT_DEV_SUBTYPE_LINK_FIFO,         /* TMC ETF */
 };
 
 enum coresight_dev_subtype_sink_e
 {
   CORESIGHT_DEV_SUBTYPE_SINK_PORT,         /* TPIU */
   CORESIGHT_DEV_SUBTYPE_SINK_BUFFER,       /* ETB */
+  CORESIGHT_DEV_SUBTYPE_SINK_TMC_BUFFER,   /* TMC ETB */
+  CORESIGHT_DEV_SUBTYPE_SINK_TMC_SYSMEM,   /* TMC ETR */
+  CORESIGHT_DEV_SUBTYPE_SINK_TMC_ETF,      /* TMC ETF */
 };
 
 /* This structure is used to unify different subtype of devices. */
@@ -137,6 +140,11 @@ struct coresight_desc_s
 
   uintptr_t stimulus_port_addr;
 
+  /* Used in TMC-ETR device. */
+
+  uint32_t buffer_size;
+  uint32_t burst_size;
+
   /* Description of outports of current device. */
 
   int outport_num;
diff --git a/include/nuttx/coresight/coresight_tmc.h b/include/nuttx/coresight/coresight_tmc.h
new file mode 100644
index 0000000000..64fc2d6d41
--- /dev/null
+++ b/include/nuttx/coresight/coresight_tmc.h
@@ -0,0 +1,109 @@
+/****************************************************************************
+ * include/nuttx/coresight/coresight_tmc.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_CORESIGHT_CORESIGHT_TMC_H
+#define __INCLUDE_NUTTX_CORESIGHT_CORESIGHT_TMC_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/mutex.h>
+#include <nuttx/coresight/coresight.h>
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+enum tmc_config_type_e
+{
+  TMC_CONFIG_TYPE_ETB,
+  TMC_CONFIG_TYPE_ETR,
+  TMC_CONFIG_TYPE_ETF,
+};
+
+enum tmc_mem_intf_width_e
+{
+  TMC_MEM_INTF_WIDTH_32BITS = 1,
+  TMC_MEM_INTF_WIDTH_64BITS = 2,
+  TMC_MEM_INTF_WIDTH_128BITS = 4,
+  TMC_MEM_INTF_WIDTH_256BITS = 8,
+};
+
+enum tmc_etr_mode_e
+{
+  TMC_ETR_MODE_FLAT,                    /* Uses contiguous flat buffer. */
+  TMC_ETR_MODE_ETR_SG,                  /* Uses in-built TMC ETR SG mechanism. */
+  TMC_ETR_MODE_CATU,                    /* Use SG mechanism in CATU. */
+};
+
+struct coresight_tmc_dev_s
+{
+  struct coresight_dev_s csdev;
+  enum tmc_config_type_e config_type;   /* Device type: ETB/ETR/ETF. */
+  enum tmc_mem_intf_width_e mmwidth;    /* Width of the memory interface databus, in bytes. */
+  uint32_t trigger_cntr;                /* Amount of words to store after a trigger. */
+  uint32_t size;                        /* RAM buffer size. */
+  uint32_t burst_size;                  /* Max burst size used in ETR devices. */
+  FAR uint32_t *buf;                    /* Pointer to the RAM buf. */
+  uint32_t len;                         /* Valid data len in RAM buffer. */
+  mutex_t lock;                         /* Mutex for driver's open/close. */
+  uint32_t caps;                        /* Capalilities current etr device has. */
+  enum tmc_etr_mode_e mode;             /* ETR buffer mode. */
+  uint32_t offset;                      /* Data offset in ETR buffer. */
+  uint8_t refcnt;                       /* TMC coresight device's enable count */
+  uint8_t opencnt;                      /* TMC device's open count. */
+};
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tmc_register
+ *
+ * Description:
+ *   Register a TMC devices.
+ *
+ * Input Parameters:
+ *   desc  - A description of this coresight device.
+ *
+ * Returned Value:
+ *   Pointer to a TMC device on success; NULL on failure.
+ *
+ ****************************************************************************/
+
+FAR struct coresight_tmc_dev_s *
+tmc_register(FAR const struct coresight_desc_s *desc);
+
+/****************************************************************************
+ * Name: tmc_unregister
+ *
+ * Description:
+ *   Unregister a TMC devices.
+ *
+ * Input Parameters:
+ *   tmcdev  - Pointer to the TMC device.
+ *
+ ****************************************************************************/
+
+void tmc_unregister(FAR struct coresight_tmc_dev_s *tmcdev);
+
+#endif  //__INCLUDE_NUTTX_CORESIGHT_CORESIGHT_TMC_H


(nuttx) 03/13: coresight:add link coresight device support

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 5324f69b58d21d0c573e1471dcb8261617766c1a
Author: liaoao <li...@xiaomi.com>
AuthorDate: Mon Sep 18 19:16:03 2023 +0800

    coresight:add link coresight device support
    
    Signed-off-by: liaoao <li...@xiaomi.com>
---
 drivers/coresight/CMakeLists.txt               |   8 +
 drivers/coresight/Kconfig                      |   8 +
 drivers/coresight/Make.defs                    |   8 +
 drivers/coresight/coresight_funnel.c           | 254 ++++++++++++++++++++++
 drivers/coresight/coresight_replicator.c       | 279 +++++++++++++++++++++++++
 include/nuttx/coresight/coresight.h            |   4 +
 include/nuttx/coresight/coresight_funnel.h     |  91 ++++++++
 include/nuttx/coresight/coresight_replicator.h |  74 +++++++
 8 files changed, 726 insertions(+)

diff --git a/drivers/coresight/CMakeLists.txt b/drivers/coresight/CMakeLists.txt
index 42915d216a..531b0ce795 100644
--- a/drivers/coresight/CMakeLists.txt
+++ b/drivers/coresight/CMakeLists.txt
@@ -21,5 +21,13 @@
 if(CONFIG_CORESIGHT)
   set(SRCS coresight_core.c coresight_common.c)
 
+  if(CONFIG_CORESIGHT_FUNNEL)
+    list(APPEND SRCS coresight_funnel.c)
+  endif()
+
+  if(CONFIG_CORESIGHT_REPLICATOR)
+    list(APPEND SRCS coresight_replicator.c)
+  endif()
+
   target_sources(drivers PRIVATE ${SRCS})
 endif()
diff --git a/drivers/coresight/Kconfig b/drivers/coresight/Kconfig
index a8fb6d4972..775b8964e1 100644
--- a/drivers/coresight/Kconfig
+++ b/drivers/coresight/Kconfig
@@ -19,4 +19,12 @@ config CORESIGHT_TIMEOUT
 	int "Timeout us for waiting register state change"
 	default 100
 
+config CORESIGHT_FUNNEL
+	bool "Funnel coresight device support"
+	default n
+
+config CORESIGHT_REPLICATOR
+	bool "Replicator coresight device support"
+	default n
+
 endif # CORESIGHT
diff --git a/drivers/coresight/Make.defs b/drivers/coresight/Make.defs
index c805893033..5bc0b0a64a 100644
--- a/drivers/coresight/Make.defs
+++ b/drivers/coresight/Make.defs
@@ -24,6 +24,14 @@ ifeq ($(CONFIG_CORESIGHT),y)
 
 CSRCS += coresight_core.c coresight_common.c
 
+ifeq ($(CONFIG_CORESIGHT_FUNNEL),y)
+CSRCS += coresight_funnel.c
+endif
+
+ifeq ($(CONFIG_CORESIGHT_REPLICATOR),y)
+CSRCS += coresight_replicator.c
+endif
+
 DEPPATH += --dep-path coresight
 VPATH += :coresight
 CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)drivers$(DELIM)coresight
diff --git a/drivers/coresight/coresight_funnel.c b/drivers/coresight/coresight_funnel.c
new file mode 100644
index 0000000000..4228847aeb
--- /dev/null
+++ b/drivers/coresight/coresight_funnel.c
@@ -0,0 +1,254 @@
+/****************************************************************************
+ * drivers/coresight/coresight_funnel.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <errno.h>
+#include <debug.h>
+#include <nuttx/kmalloc.h>
+
+#include <nuttx/coresight/coresight_funnel.h>
+
+#include "coresight_common.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Funnel registers */
+
+#define FUNNEL_FUNCTL           0x000
+#define FUNNEL_PRICTL           0x004
+
+#define FUNNEL_HOLDTIME_MASK    0xf00
+#define FUNNEL_HOLDTIME_SHFT    0x8
+#define FUNNEL_HOLDTIME         (0x7 << FUNNEL_HOLDTIME_SHFT)
+#define FUNNEL_ENS_MASK         0xff
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+static int funnel_enable(FAR struct coresight_dev_s *csdev,
+                         int iport, int oport);
+static void funnel_disable(FAR struct coresight_dev_s *csdev,
+                           int iport, int oport);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct coresight_link_ops_s g_funnel_link_ops =
+{
+  .enable  = funnel_enable,
+  .disable = funnel_disable,
+};
+
+static const struct coresight_ops_s g_funnel_ops =
+{
+  .link_ops = &g_funnel_link_ops,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: funnel_hw_enable
+ ****************************************************************************/
+
+static int funnel_hw_enable(FAR struct coresight_funnel_dev_s *fundev,
+                            int port)
+{
+  uint32_t functl;
+  int ret = 0;
+
+  coresight_unlock(fundev->csdev.addr);
+  functl = coresight_get32(fundev->csdev.addr + FUNNEL_FUNCTL);
+  coresight_lock(fundev->csdev.addr);
+
+  /* Only claim the device when the first slave port is enabled */
+
+  if (!(functl & FUNNEL_ENS_MASK))
+    {
+      ret = coresight_claim_device(fundev->csdev.addr);
+      if (ret < 0)
+        {
+          cserr("%s claim failed\n", fundev->csdev.name);
+          return ret;
+        }
+    }
+
+  coresight_unlock(fundev->csdev.addr);
+  functl &= ~FUNNEL_HOLDTIME_MASK;
+  functl |= FUNNEL_HOLDTIME;
+  functl |= 1 << port;
+  coresight_put32(functl, fundev->csdev.addr + FUNNEL_FUNCTL);
+  coresight_put32(fundev->priority, fundev->csdev.addr + FUNNEL_PRICTL);
+  coresight_lock(fundev->csdev.addr);
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: funnel_hw_disable
+ ****************************************************************************/
+
+static void funnel_hw_disable(FAR struct coresight_funnel_dev_s *fundev,
+                              int port)
+{
+  uint32_t functl;
+
+  coresight_unlock(fundev->csdev.addr);
+  functl = coresight_get32(fundev->csdev.addr + FUNNEL_FUNCTL);
+  functl &= ~(1 << port);
+  coresight_put32(functl, fundev->csdev.addr + FUNNEL_FUNCTL);
+  coresight_lock(fundev->csdev.addr);
+
+  if (!(functl & FUNNEL_ENS_MASK))
+    {
+      coresight_disclaim_device(fundev->csdev.addr);
+    }
+}
+
+/****************************************************************************
+ * Name: funnel_enable
+ ****************************************************************************/
+
+static int funnel_enable(FAR struct coresight_dev_s *csdev,
+                         int iport, int oport)
+{
+  FAR struct coresight_funnel_dev_s *fundev =
+    (FAR struct coresight_funnel_dev_s *)csdev;
+  int ret = 0;
+
+  if (fundev->port_refcnt[iport]++ == 0)
+    {
+      ret = funnel_hw_enable(fundev, iport);
+      if (ret < 0)
+        {
+          fundev->port_refcnt[iport]--;
+          cserr("%s inport %d enabled failed\n", csdev->name, iport);
+        }
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: funnel_disable
+ ****************************************************************************/
+
+static void funnel_disable(FAR struct coresight_dev_s *csdev,
+                           int iport, int oport)
+{
+  FAR struct coresight_funnel_dev_s *fundev =
+    (FAR struct coresight_funnel_dev_s *)csdev;
+
+  if (--fundev->port_refcnt[iport] == 0)
+    {
+      funnel_hw_disable(fundev, iport);
+      csinfo("%s inport %d disabled\n", csdev->name, iport);
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: set_funnel_priority
+ *
+ * Description:
+ *   Set funnel ports priority. It should to be called when port has not been
+ *   enabled.
+ *
+ * Input Parameters:
+ *   fundev  - Pointer to the funnel coresight device.
+ *   priority- Priority to set.
+ *
+ ****************************************************************************/
+
+void set_funnel_priority(FAR struct coresight_funnel_dev_s *fundev,
+                         uint32_t priority)
+{
+  fundev->priority = priority;
+}
+
+/****************************************************************************
+ * Name: funnel_register
+ *
+ * Description:
+ *   Register a funnel devices.
+ *
+ * Input Parameters:
+ *   desc  - A description of this coresight device.
+ *
+ * Returned Value:
+ *   Pointer to a funnel device on success; NULL on failure.
+ *
+ ****************************************************************************/
+
+FAR struct coresight_funnel_dev_s *
+funnel_register(FAR const struct coresight_desc_s *desc)
+{
+  FAR struct coresight_funnel_dev_s *fundev;
+  FAR struct coresight_dev_s *csdev;
+  int ret;
+
+  fundev = kmm_zalloc(sizeof(struct coresight_funnel_dev_s) +
+                      sizeof(uint8_t) * desc->inport_num);
+  if (fundev == NULL)
+    {
+      cserr("%s:malloc failed!\n", desc->name);
+      return NULL;
+    }
+
+  csdev = &fundev->csdev;
+  csdev->ops = &g_funnel_ops;
+  ret = coresight_register(csdev, desc);
+  if (ret < 0)
+    {
+      kmm_free(fundev);
+      cserr("%s: register failed\n", desc->name);
+      return NULL;
+    }
+
+  return fundev;
+}
+
+/****************************************************************************
+ * Name: funnel_unregister
+ *
+ * Description:
+ *   Unregister a funnel devices.
+ *
+ * Input Parameters:
+ *   fundev  - Pointer to the funnel device.
+ *
+ ****************************************************************************/
+
+void funnel_unregister(FAR struct coresight_funnel_dev_s *fundev)
+{
+  coresight_unregister(&fundev->csdev);
+  kmm_free(fundev);
+}
diff --git a/drivers/coresight/coresight_replicator.c b/drivers/coresight/coresight_replicator.c
new file mode 100644
index 0000000000..aa1c354bf8
--- /dev/null
+++ b/drivers/coresight/coresight_replicator.c
@@ -0,0 +1,279 @@
+/****************************************************************************
+ * drivers/coresight/coresight_replicator.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <errno.h>
+#include <debug.h>
+#include <nuttx/kmalloc.h>
+
+#include <nuttx/coresight/coresight_replicator.h>
+
+#include "coresight_common.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Replicator registers */
+
+#define REPLICATOR_IDFILTER0      0x000
+#define REPLICATOR_IDFILTER1      0x004
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+static int replicator_enable(FAR struct coresight_dev_s *csdev,
+                             int iport, int oport);
+static void replicator_disable(FAR struct coresight_dev_s *csdev,
+                               int iport, int oport);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct coresight_link_ops_s g_replicator_link_ops =
+{
+  .enable  = replicator_enable,
+  .disable = replicator_disable,
+};
+
+static const struct coresight_ops_s g_replicator_ops =
+{
+  .link_ops = &g_replicator_link_ops,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: replicator_hw_enable
+ ****************************************************************************/
+
+static int
+replicator_hw_enable(FAR struct coresight_replicator_dev_s *repdev,
+                     int port)
+{
+  uint32_t id0val;
+  uint32_t id1val;
+  int ret;
+
+  if (port != 0 && port != 1)
+    {
+      return -EINVAL;
+    }
+
+  coresight_unlock(repdev->csdev.addr);
+  id0val = coresight_get32(repdev->csdev.addr + REPLICATOR_IDFILTER0);
+  id1val = coresight_get32(repdev->csdev.addr + REPLICATOR_IDFILTER1);
+
+  /* Only claim the device when the first slave port is enabled */
+
+  if (id0val == 0xff && id1val == 0xff)
+    {
+      ret = coresight_claim_device(repdev->csdev.addr);
+      if (ret < 0)
+        {
+          cserr("%s claim failed\n", repdev->csdev.name);
+          coresight_lock(repdev->csdev.addr);
+          return ret;
+        }
+    }
+
+  switch (port)
+    {
+      case 0:
+        coresight_put32(0x00, repdev->csdev.addr + REPLICATOR_IDFILTER0);
+        break;
+
+      case 1:
+        coresight_put32(0x00, repdev->csdev.addr + REPLICATOR_IDFILTER1);
+        break;
+
+      default:
+        break;
+    }
+
+  coresight_lock(repdev->csdev.addr);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: replicator_hw_disable
+ ****************************************************************************/
+
+static void
+replicator_hw_disable(FAR struct coresight_replicator_dev_s *repdev,
+                      int port)
+{
+  uint32_t id0val;
+  uint32_t id1val;
+  uint32_t off;
+
+  switch (port)
+    {
+      case 0:
+        off = REPLICATOR_IDFILTER0;
+        break;
+
+      case 1:
+        off = REPLICATOR_IDFILTER1;
+        break;
+
+      default:
+        return;
+    }
+
+  coresight_unlock(repdev->csdev.addr);
+  coresight_put32(0xff, repdev->csdev.addr + off);
+  id0val = coresight_get32(repdev->csdev.addr + REPLICATOR_IDFILTER0);
+  id1val = coresight_get32(repdev->csdev.addr + REPLICATOR_IDFILTER1);
+  coresight_lock(repdev->csdev.addr);
+
+  if (id0val == 0xff && id1val == 0xff)
+    {
+      coresight_disclaim_device(repdev->csdev.addr);
+    }
+}
+
+/****************************************************************************
+ * Name: replicator_enable
+ ****************************************************************************/
+
+static int replicator_enable(FAR struct coresight_dev_s *csdev,
+                             int iport, int oport)
+{
+  FAR struct coresight_replicator_dev_s *repdev =
+    (FAR struct coresight_replicator_dev_s *)csdev;
+  int ret = 0;
+
+  if (repdev->port_refcnt[oport]++ == 0)
+    {
+      ret = replicator_hw_enable(repdev, oport);
+      if (ret < 0)
+        {
+          repdev->port_refcnt[oport]--;
+          cserr("%s inport %d enabled failed\n", csdev->name, oport);
+        }
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: replicator_disable
+ ****************************************************************************/
+
+static void replicator_disable(FAR struct coresight_dev_s *csdev,
+                               int iport, int oport)
+{
+  FAR struct coresight_replicator_dev_s *repdev =
+    (FAR struct coresight_replicator_dev_s *)csdev;
+
+  if (--repdev->port_refcnt[oport] == 0)
+    {
+      replicator_hw_disable(repdev, oport);
+      csinfo("%s inport %d disabled\n", csdev->name, oport);
+    }
+}
+
+/****************************************************************************
+ * Name: replicator_reset
+ ****************************************************************************/
+
+static void replicator_reset(FAR struct coresight_replicator_dev_s *repdev)
+{
+  if (coresight_claim_device(repdev->csdev.addr) == 0)
+    {
+      coresight_unlock(repdev->csdev.addr);
+      coresight_put32(0xff, repdev->csdev.addr + REPLICATOR_IDFILTER0);
+      coresight_put32(0xff, repdev->csdev.addr + REPLICATOR_IDFILTER1);
+      coresight_lock(repdev->csdev.addr);
+      coresight_disclaim_device(repdev->csdev.addr);
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: replicator_register
+ *
+ * Description:
+ *   Register a replicator devices.
+ *
+ * Input Parameters:
+ *   desc  - A description of this coresight device.
+ *
+ * Returned Value:
+ *   Pointer to a replicator device on success; NULL on failure.
+ *
+ ****************************************************************************/
+
+FAR struct coresight_replicator_dev_s *
+replicator_register(FAR const struct coresight_desc_s *desc)
+{
+  FAR struct coresight_replicator_dev_s *repdev;
+  FAR struct coresight_dev_s *csdev;
+  int ret;
+
+  repdev = kmm_zalloc(sizeof(struct coresight_replicator_dev_s) +
+                      sizeof(uint8_t) * desc->outport_num);
+  if (repdev == NULL)
+    {
+      cserr("%s:malloc failed!\n", desc->name);
+      return NULL;
+    }
+
+  csdev = &repdev->csdev;
+  csdev->ops = &g_replicator_ops;
+  ret = coresight_register(csdev, desc);
+  if (ret < 0)
+    {
+      kmm_free(repdev);
+      cserr("%s: register failed\n", desc->name);
+      return NULL;
+    }
+
+  replicator_reset(repdev);
+  return repdev;
+}
+
+/****************************************************************************
+ * Name: replicator_unregister
+ *
+ * Description:
+ *   Unregister a replicator devices.
+ *
+ * Input Parameters:
+ *   fundev  - Pointer to the replicator device.
+ *
+ ****************************************************************************/
+
+void replicator_unregister(FAR struct coresight_replicator_dev_s *repdev)
+{
+  coresight_unregister(&repdev->csdev);
+  kmm_free(repdev);
+}
diff --git a/include/nuttx/coresight/coresight.h b/include/nuttx/coresight/coresight.h
index 2630e81633..987145d58f 100644
--- a/include/nuttx/coresight/coresight.h
+++ b/include/nuttx/coresight/coresight.h
@@ -122,6 +122,10 @@ struct coresight_desc_s
   enum coresight_dev_type_e type;
   union coresight_dev_subtype_u subtype;
 
+  /* Used in funnel devices. */
+
+  int inport_num;
+
   /* Description of outports of current device. */
 
   int outport_num;
diff --git a/include/nuttx/coresight/coresight_funnel.h b/include/nuttx/coresight/coresight_funnel.h
new file mode 100644
index 0000000000..37d53016a6
--- /dev/null
+++ b/include/nuttx/coresight/coresight_funnel.h
@@ -0,0 +1,91 @@
+/****************************************************************************
+ * include/nuttx/coresight/coresight_funnel.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_CORESIGHT_CORESIGHT_FUNNEL_H
+#define __INCLUDE_NUTTX_CORESIGHT_CORESIGHT_FUNNEL_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/coresight/coresight.h>
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct coresight_funnel_dev_s
+{
+  struct coresight_dev_s csdev;
+  uint32_t priority;                 /* Port selection order. */
+  uint8_t port_refcnt[0];            /* Port refcnt. */
+};
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: set_funnel_priority
+ *
+ * Description:
+ *   Set funnel ports priority. It should to be called when port has not been
+ *   enabled.
+ *
+ * Input Parameters:
+ *   fundev  - Pointer to the funnel coresight device.
+ *   priority- Priority to set.
+ *
+ ****************************************************************************/
+
+void set_funnel_priority(FAR struct coresight_funnel_dev_s *fundev,
+                         uint32_t priority);
+
+/****************************************************************************
+ * Name: funnel_register
+ *
+ * Description:
+ *   Register an funnel devices.
+ *
+ * Input Parameters:
+ *   desc  - A description of this coresight device.
+ *
+ * Returned Value:
+ *   Pointer to a funnel device on success; NULL on failure.
+ *
+ ****************************************************************************/
+
+FAR struct coresight_funnel_dev_s *
+funnel_register(FAR const struct coresight_desc_s *desc);
+
+/****************************************************************************
+ * Name: funnel_unregister
+ *
+ * Description:
+ *   Unregister a funnel devices.
+ *
+ * Input Parameters:
+ *   fundev  - Pointer to the funnel device.
+ *
+ ****************************************************************************/
+
+void funnel_unregister(FAR struct coresight_funnel_dev_s *fundev);
+
+#endif  //__INCLUDE_NUTTX_CORESIGHT_CORESIGHT_FUNNEL_H
diff --git a/include/nuttx/coresight/coresight_replicator.h b/include/nuttx/coresight/coresight_replicator.h
new file mode 100644
index 0000000000..09e643a1a4
--- /dev/null
+++ b/include/nuttx/coresight/coresight_replicator.h
@@ -0,0 +1,74 @@
+/****************************************************************************
+ * include/nuttx/coresight/coresight_replicator.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_CORESIGHT_CORESIGHT_REPLICATOR_H
+#define __INCLUDE_NUTTX_CORESIGHT_CORESIGHT_REPLICATOR_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/coresight/coresight.h>
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct coresight_replicator_dev_s
+{
+  struct coresight_dev_s csdev;
+  uint8_t port_refcnt[0];            /* Port refcnt. */
+};
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: replicator_register
+ *
+ * Description:
+ *   Register an replicator devices.
+ *
+ * Input Parameters:
+ *   desc  - A description of this coresight device.
+ *
+ * Returned Value:
+ *   Pointer to a replicator device on success; NULL on failure.
+ *
+ ****************************************************************************/
+
+FAR struct coresight_replicator_dev_s *
+replicator_register(FAR const struct coresight_desc_s *desc);
+
+/****************************************************************************
+ * Name: replicator_unregister
+ *
+ * Description:
+ *   Unregister a replicator devices.
+ *
+ * Input Parameters:
+ *   fundev  - Pointer to the replicator device.
+ *
+ ****************************************************************************/
+
+void replicator_unregister(FAR struct coresight_replicator_dev_s *repdev);
+
+#endif  //__INCLUDE_NUTTX_CORESIGHT_CORESIGHT_REPLICATOR_H


(nuttx) 04/13: coresight:add tpiu device support

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit c0e40fc19f71b13b07108c92a9001a44ee0fee55
Author: liaoao <li...@xiaomi.com>
AuthorDate: Tue Sep 19 21:24:00 2023 +0800

    coresight:add tpiu device support
    
    Signed-off-by: liaoao <li...@xiaomi.com>
---
 drivers/coresight/CMakeLists.txt         |   4 +
 drivers/coresight/Kconfig                |   4 +
 drivers/coresight/Make.defs              |   4 +
 drivers/coresight/coresight_tpiu.c       | 236 +++++++++++++++++++++++++++++++
 include/nuttx/coresight/coresight_tpiu.h |  74 ++++++++++
 5 files changed, 322 insertions(+)

diff --git a/drivers/coresight/CMakeLists.txt b/drivers/coresight/CMakeLists.txt
index 531b0ce795..9c3ca5a7a3 100644
--- a/drivers/coresight/CMakeLists.txt
+++ b/drivers/coresight/CMakeLists.txt
@@ -29,5 +29,9 @@ if(CONFIG_CORESIGHT)
     list(APPEND SRCS coresight_replicator.c)
   endif()
 
+  if(CONFIG_CORESIGHT_TPIU)
+    list(APPEND SRCS coresight_tpiu.c)
+  endif()
+
   target_sources(drivers PRIVATE ${SRCS})
 endif()
diff --git a/drivers/coresight/Kconfig b/drivers/coresight/Kconfig
index 775b8964e1..be00792361 100644
--- a/drivers/coresight/Kconfig
+++ b/drivers/coresight/Kconfig
@@ -27,4 +27,8 @@ config CORESIGHT_REPLICATOR
 	bool "Replicator coresight device support"
 	default n
 
+config CORESIGHT_TPIU
+	bool "TPIU coresight device support"
+	default n
+
 endif # CORESIGHT
diff --git a/drivers/coresight/Make.defs b/drivers/coresight/Make.defs
index 5bc0b0a64a..a71e0ac72e 100644
--- a/drivers/coresight/Make.defs
+++ b/drivers/coresight/Make.defs
@@ -32,6 +32,10 @@ ifeq ($(CONFIG_CORESIGHT_REPLICATOR),y)
 CSRCS += coresight_replicator.c
 endif
 
+ifeq ($(CONFIG_CORESIGHT_TPIU),y)
+CSRCS += coresight_tpiu.c
+endif
+
 DEPPATH += --dep-path coresight
 VPATH += :coresight
 CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)drivers$(DELIM)coresight
diff --git a/drivers/coresight/coresight_tpiu.c b/drivers/coresight/coresight_tpiu.c
new file mode 100644
index 0000000000..6685d2a006
--- /dev/null
+++ b/drivers/coresight/coresight_tpiu.c
@@ -0,0 +1,236 @@
+/****************************************************************************
+ * drivers/coresight/coresight_tpiu.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <errno.h>
+#include <debug.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/bits.h>
+
+#include <nuttx/coresight/coresight_tpiu.h>
+
+#include "coresight_common.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* TPIU registers */
+
+#define TPIU_SUPP_PORTSZ           0x000
+#define TPIU_CURR_PORTSZ           0x004
+#define TPIU_SUPP_TRIGMODES        0x100
+#define TPIU_TRIG_CNTRVAL          0x104
+#define TPIU_TRIG_MULT             0x108
+#define TPIU_SUPP_TESTPATM         0x200
+#define TPIU_CURR_TESTPATM         0x204
+#define TPIU_TEST_PATREPCNTR       0x208
+#define TPIU_FFSR                  0x300
+#define TPIU_FFCR                  0x304
+#define TPIU_FSYNC_CNTR            0x308
+#define TPIU_EXTCTL_INPORT         0x400
+#define TPIU_EXTCTL_OUTPORT        0x404
+#define TPIU_ITTRFLINACK           0xee4
+#define TPIU_ITTRFLIN              0xee8
+#define TPIU_ITATBDATA0            0xeec
+#define TPIU_ITATBCTR2             0xef0
+#define TPIU_ITATBCTR1             0xef4
+#define TPIU_ITATBCTR0             0xef8
+
+/* FFSR - 0x300 */
+
+#define TPIU_FFSR_FT_STOPPED       BIT(1)
+
+/* FFCR - 0x304 */
+
+#define TPIU_FFCR_FON_MAN          BIT(6)
+#define TPIU_FFCR_STOP_FI          BIT(12)
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+static int tpiu_enable(FAR struct coresight_dev_s *csdev);
+static void tpiu_disable(FAR struct coresight_dev_s *csdev);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct coresight_sink_ops_s g_tpiu_sink_ops =
+{
+  .enable  = tpiu_enable,
+  .disable = tpiu_disable,
+};
+
+static const struct coresight_ops_s g_tpiu_ops =
+{
+  .sink_ops = &g_tpiu_sink_ops,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tpiu_hw_enable
+ ****************************************************************************/
+
+static int tpiu_hw_enable(FAR struct coresight_tpiu_dev_s *tpiudev)
+{
+  return coresight_claim_device(tpiudev->csdev.addr);
+}
+
+/****************************************************************************
+ * Name: tpiu_hw_disable
+ ****************************************************************************/
+
+static void tpiu_hw_disable(FAR struct coresight_tpiu_dev_s *tpiudev)
+{
+  uint32_t ffcr;
+
+  coresight_unlock(tpiudev->csdev.addr);
+
+  /* Trigger a formatter stop event. */
+
+  ffcr = coresight_get32(tpiudev->csdev.addr + TPIU_FFCR);
+  ffcr |= TPIU_FFCR_STOP_FI;
+  coresight_put32(ffcr, tpiudev->csdev.addr + TPIU_FFCR);
+  ffcr |= TPIU_FFCR_FON_MAN;
+  coresight_put32(ffcr, tpiudev->csdev.addr + TPIU_FFCR);
+  if (coresight_timeout(tpiudev->csdev.addr, TPIU_FFCR,
+                        TPIU_FFCR_FON_MAN, 0) < 0)
+    {
+      cserr("timeout while waiting for completion of Manual Flush\n");
+    }
+
+  if (coresight_timeout(tpiudev->csdev.addr, TPIU_FFSR,
+                        TPIU_FFSR_FT_STOPPED, TPIU_FFSR_FT_STOPPED) < 0)
+    {
+      cserr("timeout while waiting for Formatter to Stop\n");
+    }
+
+  coresight_lock(tpiudev->csdev.addr);
+  coresight_disclaim_device(tpiudev->csdev.addr);
+}
+
+/****************************************************************************
+ * Name: tpiu_enable
+ ****************************************************************************/
+
+static int tpiu_enable(FAR struct coresight_dev_s *csdev)
+{
+  FAR struct coresight_tpiu_dev_s *tpiudev =
+    (FAR struct coresight_tpiu_dev_s *)csdev;
+  int ret = 0;
+
+  if (tpiudev->refcnt++ == 0)
+    {
+      ret = tpiu_hw_enable(tpiudev);
+      if (ret < 0)
+        {
+          tpiudev->refcnt--;
+          cserr("tpiu %s enabled failed\n", csdev->name);
+        }
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: tpiu_disable
+ ****************************************************************************/
+
+static void tpiu_disable(FAR struct coresight_dev_s *csdev)
+{
+  FAR struct coresight_tpiu_dev_s *tpiudev =
+    (FAR struct coresight_tpiu_dev_s *)csdev;
+
+  if (--tpiudev->refcnt == 0)
+    {
+      tpiu_hw_disable(tpiudev);
+      csinfo("tpiu %s disabled\n", csdev->name);
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tpiu_register
+ *
+ * Description:
+ *   Register a tpiu devices.
+ *
+ * Input Parameters:
+ *   desc  - A description of this coresight device.
+ *
+ * Returned Value:
+ *   Pointer to a tpiu device on success; NULL on failure.
+ *
+ ****************************************************************************/
+
+FAR struct coresight_tpiu_dev_s *
+tpiu_register(FAR const struct coresight_desc_s *desc)
+{
+  FAR struct coresight_tpiu_dev_s *tpiudev;
+  FAR struct coresight_dev_s *csdev;
+  int ret;
+
+  tpiudev = kmm_zalloc(sizeof(struct coresight_tpiu_dev_s));
+  if (tpiudev == NULL)
+    {
+      cserr("%s:malloc failed!\n", desc->name);
+      return NULL;
+    }
+
+  csdev = &tpiudev->csdev;
+  csdev->ops = &g_tpiu_ops;
+  ret = coresight_register(csdev, desc);
+  if (ret < 0)
+    {
+      kmm_free(tpiudev);
+      cserr("%s:register failed\n", desc->name);
+      return NULL;
+    }
+
+  return tpiudev;
+}
+
+/****************************************************************************
+ * Name: tpiu_unregister
+ *
+ * Description:
+ *   Unregister a tpiu devices.
+ *
+ * Input Parameters:
+ *   fundev  - Pointer to the tpiu device.
+ *
+ ****************************************************************************/
+
+void tpiu_unregister(FAR struct coresight_tpiu_dev_s *tpiudev)
+{
+  coresight_unregister(&tpiudev->csdev);
+  kmm_free(tpiudev);
+}
diff --git a/include/nuttx/coresight/coresight_tpiu.h b/include/nuttx/coresight/coresight_tpiu.h
new file mode 100644
index 0000000000..9a947aedae
--- /dev/null
+++ b/include/nuttx/coresight/coresight_tpiu.h
@@ -0,0 +1,74 @@
+/****************************************************************************
+ * include/nuttx/coresight/coresight_tpiu.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_CORESIGHT_CORESIGHT_TPIU_H
+#define __INCLUDE_NUTTX_CORESIGHT_CORESIGHT_TPIU_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/coresight/coresight.h>
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct coresight_tpiu_dev_s
+{
+  struct coresight_dev_s csdev;
+  uint8_t refcnt;
+};
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tpiu_register
+ *
+ * Description:
+ *   Register a tpiu devices.
+ *
+ * Input Parameters:
+ *   desc  - A description of this coresight device.
+ *
+ * Returned Value:
+ *   Pointer to a tpiu device on success; NULL on failure.
+ *
+ ****************************************************************************/
+
+FAR struct coresight_tpiu_dev_s *
+tpiu_register(FAR const struct coresight_desc_s *desc);
+
+/****************************************************************************
+ * Name: tpiu_unregister
+ *
+ * Description:
+ *   Unregister a tpiu devices.
+ *
+ * Input Parameters:
+ *   tpiudev  - Pointer to the tpiu device.
+ *
+ ****************************************************************************/
+
+void tpiu_unregister(FAR struct coresight_tpiu_dev_s *tpiudev);
+
+#endif  //__INCLUDE_NUTTX_CORESIGHT_CORESIGHT_TPIU_H


(nuttx) 02/13: coresight:add common api for coresight system

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 8cc0c243185ca8dd88b932810df615ac106d86cf
Author: liaoao <li...@xiaomi.com>
AuthorDate: Thu Nov 2 19:05:34 2023 +0800

    coresight:add common api for coresight system
    
    Signed-off-by: liaoao <li...@xiaomi.com>
---
 drivers/coresight/CMakeLists.txt     |   2 +-
 drivers/coresight/Kconfig            |   4 +
 drivers/coresight/Make.defs          |   2 +-
 drivers/coresight/coresight_common.c | 313 +++++++++++++++++++++++++++++++++++
 drivers/coresight/coresight_common.h | 198 ++++++++++++++++++++++
 include/nuttx/bits.h                 |  23 +++
 6 files changed, 540 insertions(+), 2 deletions(-)

diff --git a/drivers/coresight/CMakeLists.txt b/drivers/coresight/CMakeLists.txt
index 6205709727..42915d216a 100644
--- a/drivers/coresight/CMakeLists.txt
+++ b/drivers/coresight/CMakeLists.txt
@@ -19,7 +19,7 @@
 # ##############################################################################
 
 if(CONFIG_CORESIGHT)
-  set(SRCS coresight_core.c)
+  set(SRCS coresight_core.c coresight_common.c)
 
   target_sources(drivers PRIVATE ${SRCS})
 endif()
diff --git a/drivers/coresight/Kconfig b/drivers/coresight/Kconfig
index 73274a0682..a8fb6d4972 100644
--- a/drivers/coresight/Kconfig
+++ b/drivers/coresight/Kconfig
@@ -15,4 +15,8 @@ config CORESIGHT_MAX_OUTPORT_NUM
 	int "Max outport number of coresight device"
 	default 2
 
+config CORESIGHT_TIMEOUT
+	int "Timeout us for waiting register state change"
+	default 100
+
 endif # CORESIGHT
diff --git a/drivers/coresight/Make.defs b/drivers/coresight/Make.defs
index 9152b10cf0..c805893033 100644
--- a/drivers/coresight/Make.defs
+++ b/drivers/coresight/Make.defs
@@ -22,7 +22,7 @@
 
 ifeq ($(CONFIG_CORESIGHT),y)
 
-CSRCS += coresight_core.c
+CSRCS += coresight_core.c coresight_common.c
 
 DEPPATH += --dep-path coresight
 VPATH += :coresight
diff --git a/drivers/coresight/coresight_common.c b/drivers/coresight/coresight_common.c
new file mode 100644
index 0000000000..bd7f3172a8
--- /dev/null
+++ b/drivers/coresight/coresight_common.c
@@ -0,0 +1,313 @@
+/****************************************************************************
+ * drivers/coresight/coresight_common.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <errno.h>
+#include <debug.h>
+#include <string.h>
+#include <nuttx/spinlock.h>
+#include <nuttx/bits.h>
+#include <nuttx/arch.h>
+
+#include "coresight_common.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define CORESIGHT_UNLOCK               0xc5acce55
+#define CORESIGHT_LOCK                 0x0
+
+#define CORESIGHT_CLAIM_SELF_HOSTED    BIT(1)
+
+#define CORESIGHT_SOURCE_BITMAP_SIZE   32
+
+#define CORESIGHT_ETM_PMU_SEED         0x10
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static DECLARE_BITMAP(g_coresight_trace_id_bitmap,
+                      CORESIGHT_SOURCE_BITMAP_SIZE);
+static spinlock_t g_coresight_trace_id_lock = SP_UNLOCKED;
+
+static const uint32_t g_coresight_barrier_pkt[4] =
+{
+  0x7fffffff,
+  0x7fffffff,
+  0x7fffffff,
+  0x7fffffff
+};
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: coresight_lock
+ *
+ * Description:
+ *   To ensure that the software being debugged can never access an unlocked
+ *   CoreSight component, a software monitor that accesses debug registers
+ *   must unlock the component before accessing any registers, and lock the
+ *   component again before exiting the monitor.
+ *
+ * Input Parameters:
+ *   addr  - Base addr of the coresight device.
+ *
+ ****************************************************************************/
+
+void coresight_lock(uintptr_t addr)
+{
+  coresight_put32(CORESIGHT_LOCK, addr + CORESIGHT_LAR);
+}
+
+/****************************************************************************
+ * Name: coresight_unlock
+ ****************************************************************************/
+
+void coresight_unlock(uintptr_t addr)
+{
+  coresight_put32(CORESIGHT_UNLOCK, addr + CORESIGHT_LAR);
+}
+
+/****************************************************************************
+ * Name: coresight_claim_device
+ *
+ * Description:
+ *   Claim the device for self-hosted usage to prevent an external tool from
+ *   touching this device.Use Protocol 3: Set private bit and check for race.
+ *
+ * Input Parameters:
+ *   addr  - Base addr of the coresight device.
+ *
+ * Returned Value:
+ *   Zero on success; a negative value on failure.
+ *
+ ****************************************************************************/
+
+int coresight_claim_device(uintptr_t addr)
+{
+  int ret = -EBUSY;
+
+  coresight_unlock(addr);
+  if (coresight_get32(addr + CORESIGHT_CLAIMCLR) != 0)
+    {
+      goto out;
+    }
+
+  coresight_put32(CORESIGHT_CLAIM_SELF_HOSTED, addr + CORESIGHT_CLAIMSET);
+  if (coresight_get32(addr + CORESIGHT_CLAIMCLR) ==
+        CORESIGHT_CLAIM_SELF_HOSTED)
+    {
+      ret = 0;
+    }
+  else
+    {
+      /* There was a race setting the tags, clean up and fail */
+
+      coresight_put32(CORESIGHT_CLAIM_SELF_HOSTED,
+                      addr + CORESIGHT_CLAIMCLR);
+    }
+
+out:
+  coresight_lock(addr);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: coresight_disclaim_device
+ *
+ * Description:
+ *   Disclaim the device, then an external tool can touch the device.
+ *
+ * Input Parameters:
+ *   addr  - Base addr of the coresight device.
+ *
+ ****************************************************************************/
+
+void coresight_disclaim_device(uintptr_t addr)
+{
+  coresight_unlock(addr);
+  if (coresight_get32(addr + CORESIGHT_CLAIMCLR) ==
+        CORESIGHT_CLAIM_SELF_HOSTED)
+    {
+      coresight_put32(CORESIGHT_CLAIM_SELF_HOSTED,
+                      addr + CORESIGHT_CLAIMCLR);
+    }
+  else
+    {
+      cserr("current device is not claimed or something wrong happend\n");
+    }
+
+  coresight_lock(addr);
+}
+
+/****************************************************************************
+ * Name: coresight_get_cpu_trace_id
+ *
+ * Description:
+ *   Used to get an unique trace id associated with cpu id of an ETM
+ *   coresight device.
+ *
+ * Input Parameters:
+ *   cpu  - CPU index to generate an unique trace id.
+ *
+ * Returned Value:
+ *   Unique trace id on success; a negative value on failure.
+ *
+ ****************************************************************************/
+
+int coresight_get_cpu_trace_id(int cpu)
+{
+  int traceid = CORESIGHT_ETM_PMU_SEED + cpu * 2;
+
+  if (traceid >= CORESIGHT_SOURCE_BITMAP_SIZE)
+    {
+      cserr("trace id is out of bounds\n");
+      return -EINVAL;
+    }
+
+  return traceid;
+}
+
+/****************************************************************************
+ * Name: coresight_get_system_trace_id
+ *
+ * Description:
+ *   Used to get an unique trace id of a STM coresight device. The trace ID
+ *   value for *ETM* tracers start at CPU_ID * 2 + 0x10, and Trace ID 0x00
+ *   and anything equal to or higher than 0x70 is reserved.
+ *
+ * Returned Value:
+ *   Unique trace id on success; a negative value on failure.
+ *
+ ****************************************************************************/
+
+int coresight_get_system_trace_id(void)
+{
+  int max = CORESIGHT_SOURCE_BITMAP_SIZE / 2;
+  irqstate_t flags;
+  int traceid = 0;
+  int i;
+
+  flags = spin_lock_irqsave(&g_coresight_trace_id_lock);
+  for (i = 0; i < max - 1; i++)
+    {
+      if (test_bit(1 + 2 * i, g_coresight_trace_id_bitmap) == 0)
+        {
+          traceid = 1 + 2 * i;
+          break;
+        }
+    }
+
+  if (traceid == 0)
+    {
+      cserr("get system trace id failed\n");
+      spin_unlock_irqrestore(&g_coresight_trace_id_lock, flags);
+      return -EINVAL;
+    }
+
+  __set_bit(traceid, g_coresight_trace_id_bitmap);
+  spin_unlock_irqrestore(&g_coresight_trace_id_lock, flags);
+
+  return traceid;
+}
+
+/****************************************************************************
+ * Name: coresight_put_system_trace_id
+ *
+ * Description:
+ *    Release an allocated system trace ID.
+ *
+ * Input Parameters:
+ *   traceid  - Traceid to be released.
+ *
+ ****************************************************************************/
+
+void coresight_put_system_trace_id(int traceid)
+{
+  irqstate_t flags;
+
+  flags = spin_lock_irqsave(&g_coresight_trace_id_lock);
+  __clear_bit(traceid, g_coresight_trace_id_bitmap);
+  spin_unlock_irqrestore(&g_coresight_trace_id_lock, flags);
+}
+
+/****************************************************************************
+ * Name: coresight_timeout
+ *
+ * Description:
+ *   Loop until a bitmask of register has changed to a specific value.
+ *
+ * Input Parameters:
+ *   addr    - Base addr of the coresight device.
+ *   off     - Register offset of the coresight device.
+ *   bitmask - Bitmask to be checked.
+ *   val     - Value to be matched.
+ *
+ * Returned Value:
+ *   Zero on success; a negative value on failure.
+ *
+ ****************************************************************************/
+
+int coresight_timeout(uintptr_t addr, uint32_t off,
+                      uint32_t bitmask, uint32_t val)
+{
+  int i;
+
+  for (i = CONFIG_CORESIGHT_TIMEOUT; i > 0; i--)
+    {
+      uint32_t value = coresight_get32(addr + off);
+      if ((value & bitmask) == val)
+        {
+          return 0;
+        }
+
+      up_udelay(1);
+    }
+
+  return -EAGAIN;
+}
+
+/****************************************************************************
+ * Name: coresight_insert_barrier_packet
+ *
+ * Description:
+ *   When losing synchronisation a new barrier packet needs to be inserted at
+ *   the beginning of the data collected in a buffer.  That way the decoder
+ *   knows that it needs to look for another sync sequence.
+ *
+ * Input Parameters:
+ *   buf  - buffer that a new barrier packet inserts to.
+ *
+ ****************************************************************************/
+
+void coresight_insert_barrier_packet(FAR void *buf)
+{
+  if (buf != NULL)
+    {
+      memcpy(buf, g_coresight_barrier_pkt, sizeof(g_coresight_barrier_pkt));
+    }
+}
diff --git a/drivers/coresight/coresight_common.h b/drivers/coresight/coresight_common.h
new file mode 100644
index 0000000000..928c3a2b4b
--- /dev/null
+++ b/drivers/coresight/coresight_common.h
@@ -0,0 +1,198 @@
+/****************************************************************************
+ * drivers/coresight/coresight_common.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __DRIVERS_CORESIGHT_CORESIGHT_COMMON_H
+#define __DRIVERS_CORESIGHT_CORESIGHT_COMMON_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Coresight registers
+ * 0xF00 - 0xF9C: Management    registers
+ * 0xFA0 - 0xFA4: Management    registers in PFTv1.0
+ *                Trace         registers in PFTv1.1
+ * 0xFA8 - 0xFFC: Management    registers
+ */
+
+#define CORESIGHT_ITCTRL               0xf00
+#define CORESIGHT_CLAIMSET             0xfa0
+#define CORESIGHT_CLAIMCLR             0xfa4
+#define CORESIGHT_LAR                  0xfb0
+#define CORESIGHT_LSR                  0xfb4
+#define CORESIGHT_DEVARCH              0xfbc
+#define CORESIGHT_AUTHSTATUS           0xfb8
+#define CORESIGHT_DEVID                0xfc8
+#define CORESIGHT_DEVTYPE              0xfcc
+
+/* Register operations */
+
+#define coresight_put8(val, addr) \
+        (*(FAR volatile uint8_t *)(addr) = (val))
+#define coresight_put16(val, addr) \
+        (*(FAR volatile uint16_t *)(addr) = (val))
+#define coresight_put32(val, addr) \
+        (*(FAR volatile uint32_t *)(addr) = (val))
+#define coresight_get32(addr) \
+        (*(FAR volatile uint32_t *)(addr))
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: coresight_lock
+ *
+ * Description:
+ *   To ensure that the software being debugged can never access an unlocked
+ *   CoreSight component, a software monitor that accesses debug registers
+ *   must unlock the component before accessing any registers, and lock the
+ *   component again before exiting the monitor.
+ *
+ * Input Parameters:
+ *   addr  - Base addr of the coresight device.
+ *
+ ****************************************************************************/
+
+void coresight_lock(uintptr_t addr);
+
+/****************************************************************************
+ * Name: coresight_unlock
+ ****************************************************************************/
+
+void coresight_unlock(uintptr_t addr);
+
+/****************************************************************************
+ * Name: coresight_claim_device
+ *
+ * Description:
+ *   Claim the device for self-hosted usage to prevent an external tool from
+ *   touching this device.Use Protocol 3: Set private bit and check for race.
+ *
+ * Input Parameters:
+ *   addr  - Base addr of the coresight device.
+ *
+ * Returned Value:
+ *   Zero on success; a negative value on failure.
+ *
+ ****************************************************************************/
+
+int coresight_claim_device(uintptr_t addr);
+
+/****************************************************************************
+ * Name: coresight_disclaim_device
+ *
+ * Description:
+ *   Disclaim the device, then an external tool can touch the device.
+ *
+ * Input Parameters:
+ *   addr  - Base addr of the coresight device.
+ *
+ ****************************************************************************/
+
+void coresight_disclaim_device(uintptr_t addr);
+
+/****************************************************************************
+ * Name: coresight_get_cpu_trace_id
+ *
+ * Description:
+ *   Used to get an unique trace id associated with cpu id of an ETM
+ *   coresight device.
+ *
+ * Input Parameters:
+ *   cpu  - CPU index to generate an unique trace id.
+ *
+ * Returned Value:
+ *   Unique trace id on success; a negative value on failure.
+ *
+ ****************************************************************************/
+
+int coresight_get_cpu_trace_id(int cpu);
+
+/****************************************************************************
+ * Name: coresight_get_system_trace_id
+ *
+ * Description:
+ *   Used to get an unique trace id of a STM coresight device. The trace ID
+ *   value for *ETM* tracers start at CPU_ID * 2 + 0x10, and Trace ID 0x00
+ *   and anything equal to or higher than 0x70 is reserved.
+ *
+ * Returned Value:
+ *   Unique trace id on success; a negative value on failure.
+ *
+ ****************************************************************************/
+
+int coresight_get_system_trace_id(void);
+
+/****************************************************************************
+ * Name: coresight_put_system_trace_id
+ *
+ * Description:
+ *    Release an allocated system trace ID.
+ *
+ * Input Parameters:
+ *   traceid  - Traceid to be released.
+ *
+ ****************************************************************************/
+
+void coresight_put_system_trace_id(int traceid);
+
+/****************************************************************************
+ * Name: coresight_timeout
+ *
+ * Description:
+ *   Loop until a bitmask of register has changed to a specific value.
+ *
+ * Input Parameters:
+ *   addr    - Base addr of the coresight device.
+ *   off     - Register offset of the coresight device.
+ *   bitmask - Bitmask to be checked.
+ *   val     - Value to be matched.
+ *
+ * Returned Value:
+ *   Zero on success; a negative value on failure.
+ *
+ ****************************************************************************/
+
+int coresight_timeout(uintptr_t addr, uint32_t off,
+                      uint32_t bitmask, uint32_t val);
+
+/****************************************************************************
+ * Name: coresight_insert_barrier_packet
+ *
+ * Description:
+ *   When losing synchronisation a new barrier packet needs to be inserted at
+ *   the beginning of the data collected in a buffer.  That way the decoder
+ *   knows that it needs to look for another sync sequence.
+ *
+ * Input Parameters:
+ *   buf  - buffer that a new barrier packet inserts to.
+ *
+ ****************************************************************************/
+
+void coresight_insert_barrier_packet(FAR void *buf);
+
+#endif  /* __DRIVERS_CORESIGHT_CORESIGHT_COMMON_H */
diff --git a/include/nuttx/bits.h b/include/nuttx/bits.h
index 371934ea25..eb0c2c9126 100644
--- a/include/nuttx/bits.h
+++ b/include/nuttx/bits.h
@@ -40,6 +40,10 @@
 # define BITS_PER_LONG (sizeof(unsigned long) * BITS_PER_BYTE)
 #endif
 
+#ifndef BITS_TO_LONGS
+#  define BITS_TO_LONGS(nr) (((nr) + BITS_PER_LONG - 1) / BITS_PER_LONG)
+#endif
+
 #ifndef BITS_PER_LONG_LONG
 # define BITS_PER_LONG_LONG (sizeof(unsigned long long) * BITS_PER_BYTE)
 #endif
@@ -70,6 +74,25 @@
 #define GENMASK_ULL(h, l) \
         (BUILD_BUG_ON_ZERO((l) > (h)) + __GENMASK_ULL(h, l))
 
+#define BMVAL(val, lsb, msb)      (((val) & GENMASK(msb, lsb)) >> (lsb))
+
+/* Bitmap operations */
+
+#define DECLARE_BITMAP(name, bits) \
+        unsigned long name[BITS_TO_LONGS(bits)]
+
+#define __set_bit(nr, addr) \
+        (*(((FAR unsigned long *)(addr)) + BIT_WORD(nr)) |= \
+        BIT_WORD_MASK(nr))
+
+#define __clear_bit(nr, addr) \
+        (*(((FAR unsigned long *)(addr)) + BIT_WORD(nr)) &= \
+        ~BIT_WORD_MASK(nr))
+
+#define test_bit(nr, addr) \
+        (*(((FAR unsigned long *)(addr)) + BIT_WORD(nr)) & \
+        BIT_WORD_MASK(nr))
+
 /****************************************************************************
  * Type Definitions
  ****************************************************************************/


(nuttx) 07/13: coresight:add etb device support

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 79af05c4ae27f8dce5433619299571b732b24992
Author: liaoao <li...@xiaomi.com>
AuthorDate: Mon Sep 18 21:05:06 2023 +0800

    coresight:add etb device support
    
    Signed-off-by: liaoao <li...@xiaomi.com>
---
 drivers/coresight/CMakeLists.txt        |   4 +
 drivers/coresight/Kconfig               |   4 +
 drivers/coresight/Make.defs             |   4 +
 drivers/coresight/coresight_etb.c       | 510 ++++++++++++++++++++++++++++++++
 include/nuttx/coresight/coresight_etb.h |  80 +++++
 5 files changed, 602 insertions(+)

diff --git a/drivers/coresight/CMakeLists.txt b/drivers/coresight/CMakeLists.txt
index e12b869f5b..29bda14e2d 100644
--- a/drivers/coresight/CMakeLists.txt
+++ b/drivers/coresight/CMakeLists.txt
@@ -21,6 +21,10 @@
 if(CONFIG_CORESIGHT)
   set(SRCS coresight_core.c coresight_common.c)
 
+  if(CONFIG_CORESIGHT_ETB)
+    list(APPEND SRCS coresight_etb.c)
+  endif()
+
   if(CONFIG_CORESIGHT_ETM_VERSION STREQUAL "v3")
     list(APPEND SRCS coresight_etm3.c)
   endif()
diff --git a/drivers/coresight/Kconfig b/drivers/coresight/Kconfig
index ad0d7e50b6..3ea611a806 100644
--- a/drivers/coresight/Kconfig
+++ b/drivers/coresight/Kconfig
@@ -19,6 +19,10 @@ config CORESIGHT_TIMEOUT
 	int "Timeout us for waiting register state change"
 	default 100
 
+config CORESIGHT_ETB
+	bool "ETB coresight device support"
+	default n
+
 config CORESIGHT_ETM
 	bool "ETM coresight device support"
 	default n
diff --git a/drivers/coresight/Make.defs b/drivers/coresight/Make.defs
index fa8fc3e366..b3852b0c57 100644
--- a/drivers/coresight/Make.defs
+++ b/drivers/coresight/Make.defs
@@ -24,6 +24,10 @@ ifeq ($(CONFIG_CORESIGHT),y)
 
 CSRCS += coresight_core.c coresight_common.c
 
+ifeq ($(CONFIG_CORESIGHT_ETB),y)
+CSRCS += coresight_etb.c
+endif
+
 ifeq ($(CONFIG_CORESIGHT_ETM_VERSION),"v3")
 CSRCS += coresight_etm3.c
 endif
diff --git a/drivers/coresight/coresight_etb.c b/drivers/coresight/coresight_etb.c
new file mode 100644
index 0000000000..859d84a4d2
--- /dev/null
+++ b/drivers/coresight/coresight_etb.c
@@ -0,0 +1,510 @@
+/****************************************************************************
+ * drivers/coresight/coresight_etb.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <errno.h>
+#include <debug.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <nuttx/bits.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/fs/fs.h>
+
+#include <nuttx/coresight/coresight_etb.h>
+
+#include "coresight_common.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* ETB registers */
+
+#define ETB_RAM_DEPTH_REG     0x004
+#define ETB_STATUS_REG        0x00c
+#define ETB_RAM_READ_DATA_REG 0x010
+#define ETB_RAM_READ_POINTER  0x014
+#define ETB_RAM_WRITE_POINTER 0x018
+#define ETB_TRG               0x01c
+#define ETB_CTL_REG           0x020
+#define ETB_RWD_REG           0x024
+#define ETB_FFSR              0x300
+#define ETB_FFCR              0x304
+#define ETB_ITMISCOP0         0xee0
+#define ETB_ITTRFLINACK       0xee4
+#define ETB_ITTRFLIN          0xee8
+#define ETB_ITATBDATA0        0xeeC
+#define ETB_ITATBCTR2         0xef0
+#define ETB_ITATBCTR1         0xef4
+#define ETB_ITATBCTR0         0xef8
+
+/* STS - 0x00C */
+
+#define ETB_STATUS_RAM_FULL   BIT(0)
+
+/* CTL - 0x020 */
+
+#define ETB_CTL_CAPT_EN       BIT(0)
+
+/* FFCR - 0x304 */
+
+#define ETB_FFCR_EN_FTC       BIT(0)
+#define ETB_FFCR_FON_MAN      BIT(6)
+#define ETB_FFCR_STOP_FI      BIT(12)
+#define ETB_FFCR_STOP_TRIGGER BIT(13)
+
+#define ETB_FFSR_FT_STOPPED   BIT(1)
+
+#define ETB_FRAME_SIZE_WORDS  4
+
+#define ETB_MAX_NAME_LEN      32
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+static int etb_enable(FAR struct coresight_dev_s *csdev);
+static void etb_disable(FAR struct coresight_dev_s *csdev);
+static int etb_open(FAR struct file *filep);
+static int etb_close(FAR struct file *filep);
+static ssize_t etb_read(FAR struct file *filep, FAR char *buffer,
+                        size_t buflen);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct coresight_sink_ops_s g_etb_sink_ops =
+{
+  .enable  = etb_enable,
+  .disable = etb_disable,
+};
+
+static const struct coresight_ops_s g_etb_ops =
+{
+  .sink_ops = &g_etb_sink_ops,
+};
+
+static const struct file_operations g_etb_fops =
+{
+  etb_open,               /* open */
+  etb_close,              /* close */
+  etb_read,               /* read */
+  NULL,                   /* write */
+  NULL,                   /* seek */
+  NULL,                   /* ioctl */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: etb_hw_enable
+ ****************************************************************************/
+
+static void etb_hw_enable(FAR struct coresight_etb_dev_s *etbdev)
+{
+  uint32_t i;
+
+  coresight_unlock(etbdev->csdev.addr);
+
+  /* Clear entire RAM buffer. */
+
+  coresight_put32(0x0, etbdev->csdev.addr + ETB_RAM_WRITE_POINTER);
+  for (i = 0; i < etbdev->buffer_depth; i++)
+    {
+      coresight_put32(0x0, etbdev->csdev.addr + ETB_RWD_REG);
+    }
+
+  /* Reset read/write RAM pointer. */
+
+  coresight_put32(0x0, etbdev->csdev.addr + ETB_RAM_WRITE_POINTER);
+  coresight_put32(0x0, etbdev->csdev.addr + ETB_RAM_READ_POINTER);
+
+  /* Set trigger event. */
+
+  coresight_put32(etbdev->trigger_cntr, etbdev->csdev.addr + ETB_TRG);
+  coresight_put32(ETB_FFCR_EN_FTC | ETB_FFCR_STOP_TRIGGER,
+                  etbdev->csdev.addr + ETB_FFCR);
+
+  /* Enable trace capture. */
+
+  coresight_put32(ETB_CTL_CAPT_EN, etbdev->csdev.addr + ETB_CTL_REG);
+  coresight_lock(etbdev->csdev.addr);
+}
+
+/****************************************************************************
+ * Name: etb_hw_read
+ *
+ * Description:
+ *   Dump ETB RAM buffer to device's buffer for usrspace's read.
+ *
+ * Input Parameters:
+ *   etbdev  - Pointer to the ETB device.
+ *
+ ****************************************************************************/
+
+static void etb_hw_read(FAR struct coresight_etb_dev_s *etbdev)
+{
+  uint32_t readptr;
+  uint32_t writeptr;
+  uint32_t frameoff;
+  FAR uint32_t *bufptr;
+  bool lost = false;
+  uint32_t i;
+
+  coresight_unlock(etbdev->csdev.addr);
+
+  /* ARM recommends that addresses are 128-bit aligned. Read from 0 when ETB
+   * buffer is not full, otherwise, read from writepointer and there are some
+   * trace data has been overwriten and lost.
+   */
+
+  readptr = coresight_get32(etbdev->csdev.addr + ETB_RAM_READ_POINTER);
+  writeptr = coresight_get32(etbdev->csdev.addr + ETB_RAM_WRITE_POINTER);
+
+  frameoff = writeptr % ETB_FRAME_SIZE_WORDS;
+  if (frameoff != 0)
+    {
+      cserr("writeptr: 0x%" PRIx32 " not aligned to formatter frame size\n",
+            writeptr);
+      writeptr += ETB_FRAME_SIZE_WORDS - frameoff;
+    }
+
+  if ((coresight_get32(etbdev->csdev.addr + ETB_STATUS_REG) &
+       ETB_STATUS_RAM_FULL) == 0)
+    {
+      coresight_put32(0x0, etbdev->csdev.addr + ETB_RAM_READ_POINTER);
+    }
+  else
+    {
+      coresight_put32(writeptr, etbdev->csdev.addr + ETB_RAM_READ_POINTER);
+      lost = true;
+    }
+
+  bufptr = etbdev->bufptr;
+  for (i = 0; i < etbdev->buffer_depth; i++)
+    {
+      *bufptr = coresight_get32(etbdev->csdev.addr + ETB_RAM_READ_DATA_REG);
+      bufptr += 1;
+    }
+
+  if (lost == true)
+    {
+      coresight_insert_barrier_packet(etbdev->bufptr);
+    }
+
+  /* Clear content between frameoff and ETB_FRAME_SIZE_WORDS - frameoff. */
+
+  if (frameoff)
+    {
+      bufptr -= ETB_FRAME_SIZE_WORDS - frameoff;
+      for (i = 0; i < ETB_FRAME_SIZE_WORDS - frameoff; i++)
+        {
+          *bufptr = 0x0;
+          bufptr += 1;
+        }
+    }
+
+  coresight_put32(readptr, etbdev->csdev.addr + ETB_RAM_READ_POINTER);
+  coresight_lock(etbdev->csdev.addr);
+}
+
+/****************************************************************************
+ * Name: etb_hw_disable
+ ****************************************************************************/
+
+static void etb_hw_disable(FAR struct coresight_etb_dev_s *etbdev)
+{
+  coresight_unlock(etbdev->csdev.addr);
+
+  /* Trigger a formatter stop event. */
+
+  coresight_modify32(ETB_FFCR_STOP_FI, ETB_FFCR_STOP_FI,
+                     etbdev->csdev.addr + ETB_FFCR);
+  coresight_modify32(ETB_FFCR_FON_MAN, ETB_FFCR_FON_MAN,
+                     etbdev->csdev.addr + ETB_FFCR);
+  if (coresight_timeout(0x0, ETB_FFCR_FON_MAN,
+                        etbdev->csdev.addr + ETB_FFCR) < 0)
+    {
+      cserr("timeout while waiting for completion of Manual Flush\n");
+    }
+
+  /* Disable trace capture. */
+
+  coresight_put32(0x0, etbdev->csdev.addr + ETB_CTL_REG);
+  if (coresight_timeout(ETB_FFSR_FT_STOPPED, ETB_FFSR_FT_STOPPED,
+                        etbdev->csdev.addr + ETB_FFSR) < 0)
+    {
+      cserr("timeout while waiting for Formatter to Stop\n");
+    }
+
+  coresight_lock(etbdev->csdev.addr);
+}
+
+/****************************************************************************
+ * Name: etb_enable
+ ****************************************************************************/
+
+static int etb_enable(FAR struct coresight_dev_s *csdev)
+{
+  FAR struct coresight_etb_dev_s *etbdev =
+    (FAR struct coresight_etb_dev_s *)csdev;
+  int ret = 0;
+
+  if (etbdev->refcnt++ == 0)
+    {
+      ret = coresight_claim_device(etbdev->csdev.addr);
+      if (ret < 0)
+        {
+          etbdev->refcnt--;
+          cserr("%s enable failed\n", csdev->name);
+          return ret;
+        }
+
+      etb_hw_enable(etbdev);
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: etb_disable
+ ****************************************************************************/
+
+static void etb_disable(FAR struct coresight_dev_s *csdev)
+{
+  FAR struct coresight_etb_dev_s *etbdev =
+    (FAR struct coresight_etb_dev_s *)csdev;
+
+  if (--etbdev->refcnt == 0)
+    {
+      etb_hw_disable(etbdev);
+      coresight_disclaim_device(etbdev->csdev.addr);
+      csinfo("%s disabled\n", csdev->name);
+    }
+}
+
+/****************************************************************************
+ * Name: etb_open
+ ****************************************************************************/
+
+static int etb_open(FAR struct file *filep)
+{
+  FAR struct inode *inode = filep->f_inode;
+  FAR struct coresight_etb_dev_s *etbdev;
+  int ret;
+
+  DEBUGASSERT(inode->i_private);
+  etbdev = (FAR struct coresight_etb_dev_s *)inode->i_private;
+
+  ret = nxmutex_lock(&etbdev->lock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  if (etbdev->opencnt++ == 0)
+    {
+      /* Each buffer line is 32bit size. */
+
+      etbdev->bufptr = kmm_zalloc(etbdev->buffer_depth * 4);
+      if (etbdev->bufptr == NULL)
+        {
+          cserr("malloc buffer failed\n");
+          etbdev->opencnt--;
+          ret = -ENOMEM;
+        }
+      else
+        {
+          irqstate_t flags;
+
+          flags = enter_critical_section();
+          if (etbdev->refcnt > 0)
+            {
+              etb_hw_disable(etbdev);
+            }
+
+          etb_hw_read(etbdev);
+          if (etbdev->refcnt > 0)
+            {
+              etb_hw_enable(etbdev);
+            }
+
+          leave_critical_section(flags);
+        }
+    }
+
+  nxmutex_unlock(&etbdev->lock);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: etb_close
+ ****************************************************************************/
+
+static int etb_close(FAR struct file *filep)
+{
+  FAR struct inode *inode = filep->f_inode;
+  FAR struct coresight_etb_dev_s *etbdev;
+  int ret;
+
+  DEBUGASSERT(inode->i_private);
+  etbdev = (FAR struct coresight_etb_dev_s *)inode->i_private;
+
+  ret = nxmutex_lock(&etbdev->lock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  if (--etbdev->opencnt == 0)
+    {
+      kmm_free(etbdev->bufptr);
+    }
+
+  nxmutex_unlock(&etbdev->lock);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: etb_read
+ ****************************************************************************/
+
+static ssize_t etb_read(FAR struct file *filep, FAR char *buffer,
+                        size_t buflen)
+{
+  FAR struct inode *inode = filep->f_inode;
+  FAR struct coresight_etb_dev_s *etbdev;
+
+  DEBUGASSERT(inode->i_private);
+  etbdev = (FAR struct coresight_etb_dev_s *)inode->i_private;
+
+  if (filep->f_pos > etbdev->buffer_depth * 4)
+    {
+      return 0;
+    }
+
+  if (filep->f_pos + buflen > etbdev->buffer_depth * 4)
+    {
+      buflen = etbdev->buffer_depth * 4 - filep->f_pos;
+    }
+
+  memcpy(buffer, (char *)etbdev->bufptr + filep->f_pos, buflen);
+  filep->f_pos += buflen;
+
+  return buflen;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: etb_register
+ *
+ * Description:
+ *   Register an etb devices.
+ *
+ * Input Parameters:
+ *   desc  - A description of this coresight device.
+ *
+ * Returned Value:
+ *   Pointer to an etb device on success; NULL on failure.
+ *
+ ****************************************************************************/
+
+FAR struct coresight_etb_dev_s *
+etb_register(FAR const struct coresight_desc_s *desc)
+{
+  FAR struct coresight_etb_dev_s *etbdev;
+  FAR struct coresight_dev_s *csdev;
+  char pathname[ETB_MAX_NAME_LEN];
+  int ret;
+
+  etbdev = kmm_zalloc(sizeof(struct coresight_etb_dev_s));
+  if (etbdev == NULL)
+    {
+      cserr("%s:malloc failed!\n", desc->name);
+      return NULL;
+    }
+
+  coresight_unlock(desc->addr);
+  etbdev->buffer_depth = coresight_get32(desc->addr + ETB_RAM_DEPTH_REG);
+  coresight_lock(desc->addr);
+  if (etbdev->buffer_depth & 0x80000000)
+    {
+      cserr("%s:etb buffer depth is invalid\n", desc->name);
+      goto buf_err;
+    }
+
+  csdev = &etbdev->csdev;
+  csdev->ops = &g_etb_ops;
+  ret = coresight_register(csdev, desc);
+  if (ret < 0)
+    {
+      cserr("%s:coresight register failed\n", desc->name);
+      goto buf_err;
+    }
+
+  snprintf(pathname, sizeof(pathname), "/dev/%s", desc->name);
+  ret = register_driver(pathname, &g_etb_fops, 0444, etbdev);
+  if (ret < 0)
+    {
+      cserr("%s:driver register failed\n", desc->name);
+      goto drv_err;
+    }
+
+  nxmutex_init(&etbdev->lock);
+  return etbdev;
+
+drv_err:
+  coresight_unregister(csdev);
+buf_err:
+  kmm_free(etbdev);
+  return NULL;
+}
+
+/****************************************************************************
+ * Name: etb_unregister
+ *
+ * Description:
+ *   Unregister an etb devices.
+ *
+ * Input Parameters:
+ *   etbdev  - Pointer to the etb device.
+ *
+ ****************************************************************************/
+
+void etb_unregister(FAR struct coresight_etb_dev_s *etbdev)
+{
+  char pathname[ETB_MAX_NAME_LEN];
+
+  snprintf(pathname, sizeof(pathname), "/dev/%s", etbdev->csdev.name);
+  unregister_driver(pathname);
+  coresight_unregister(&etbdev->csdev);
+  nxmutex_destroy(&etbdev->lock);
+  kmm_free(etbdev);
+}
diff --git a/include/nuttx/coresight/coresight_etb.h b/include/nuttx/coresight/coresight_etb.h
new file mode 100644
index 0000000000..21c15f7dcb
--- /dev/null
+++ b/include/nuttx/coresight/coresight_etb.h
@@ -0,0 +1,80 @@
+/****************************************************************************
+ * include/nuttx/coresight/coresight_etb.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_CORESIGHT_CORESIGHT_ETB_H
+#define __INCLUDE_NUTTX_CORESIGHT_CORESIGHT_ETB_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/mutex.h>
+#include <nuttx/coresight/coresight.h>
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct coresight_etb_dev_s
+{
+  struct coresight_dev_s csdev;
+  uint32_t trigger_cntr;             /* Amount of words to store after a trigger */
+  uint32_t buffer_depth;             /* ETB buffer depth. */
+  FAR uint32_t *bufptr;              /* Buffer that ETB content sends to. */
+  mutex_t lock;                      /* Mutex for driver's open/close. */
+  uint8_t refcnt;                    /* ETB coresight device's enable count. */
+  uint8_t opencnt;                   /* ETB device's open count. */
+};
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: etb_register
+ *
+ * Description:
+ *   Register an etb devices.
+ *
+ * Input Parameters:
+ *   desc  - A description of this coresight device.
+ *
+ * Returned Value:
+ *   Pointer to an etb device on success; NULL on failure.
+ *
+ ****************************************************************************/
+
+FAR struct coresight_etb_dev_s *
+etb_register(FAR const struct coresight_desc_s *desc);
+
+/****************************************************************************
+ * Name: etb_unregister
+ *
+ * Description:
+ *   Unregister an etb devices.
+ *
+ * Input Parameters:
+ *   etbdev  - Pointer to the etb device.
+ *
+ ****************************************************************************/
+
+void etb_unregister(FAR struct coresight_etb_dev_s *etbdev);
+
+#endif  //__INCLUDE_NUTTX_CORESIGHT_CORESIGHT_ETB_H


(nuttx) 11/13: coresight: return -EEXIST when register a device which has been registered

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit d04e1ebf4ec2805b78b10c4db5d3020f63e098e4
Author: liaoao <li...@xiaomi.com>
AuthorDate: Thu Jan 11 17:01:56 2024 +0800

    coresight: return -EEXIST when register a device which has been registered
    
    Signed-off-by: liaoao <li...@xiaomi.com>
---
 drivers/coresight/coresight_core.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/drivers/coresight/coresight_core.c b/drivers/coresight/coresight_core.c
index 56e056af0e..9b113123b9 100644
--- a/drivers/coresight/coresight_core.c
+++ b/drivers/coresight/coresight_core.c
@@ -661,6 +661,29 @@ err:
   return ret;
 }
 
+/****************************************************************************
+ * Name: coresight_find_dev
+ ****************************************************************************/
+
+static FAR struct coresight_dev_s *coresight_find_dev(FAR const char *name)
+{
+  FAR struct coresight_dev_s *tempdev;
+  irqstate_t flags;
+
+  flags = enter_critical_section();
+  list_for_every_entry(&g_csdev_list, tempdev, struct coresight_dev_s, node)
+    {
+      if (strcmp(tempdev->name, name) == 0)
+        {
+          leave_critical_section(flags);
+          return tempdev;
+        }
+    }
+
+  leave_critical_section(flags);
+  return NULL;
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -687,6 +710,12 @@ int coresight_register(FAR struct coresight_dev_s *csdev,
   irqstate_t flags;
   int i;
 
+  if (coresight_find_dev(desc->name) != NULL)
+    {
+      cserr("device has been registered!\n");
+      return -EEXIST;
+    }
+
   csdev->name = desc->name;
   csdev->addr = desc->addr;
   csdev->type = desc->type;