This is an automated email from Gerrit.

"liangzhen <[email protected]>" just uploaded a new patch set to Gerrit, 
which you can find at https://review.openocd.org/c/openocd/+/9214

-- gerrit

commit ee2d88122165ca20c83a2c83c523afbc7d8868a6
Author: liangzhen <[email protected]>
Date:   Wed Oct 29 18:53:24 2025 +0800

    target: cstrace: Introduce `target` access interface
    
    RISC-V trace allows sending RISC-V trace to Arm CoreSight
    infrastructure by The ATB Bridge. Introduce `target` access
    interface, Arm CoreSight components can be accessed through
    RISC-V debug module.
    
    Change-Id: If2ee57aaa6d5fbdc17d3a446da1857e2e0fc332d
    Signed-off-by: liangzhen <[email protected]>

diff --git a/doc/openocd.texi b/doc/openocd.texi
index a0f2134c76..91949d4089 100644
--- a/doc/openocd.texi
+++ b/doc/openocd.texi
@@ -10324,7 +10324,7 @@ This name is also used to create the coresight 
component object command, referre
 $component_name, and in other places the component needs to be identified.
 @item @emph{type} ... specifies the coresight component type.
 @item @emph{configparams} ... all parameters accepted by $component_name 
configure are permitted.
-You must set the @option{-dap} @emph{dap_name} @option{-ap-num} @emph{apn} 
here.
+You must set the @option{-dap} @emph{dap_name} @option{-ap-num} @emph{apn} or 
@option{-target} @emph{target_name} here.
 @end itemize
 @end deffn
 
@@ -10349,6 +10349,7 @@ These values can later be queried one at a time by 
using the @command{$component
 @item @option{-ap-num} @emph{apn} - set DAP access port for the coresight 
component.
 On ADIv5 DAP ap number is the numeric index of the DAP AP the component is 
connected to.
 On ADIv6 DAP ap number is the base address of the DAP AP the component is 
connected to.
+@item @option{-target} @emph{target_name} - names the target used to access 
this coresight component.
 @item @option{-baseaddr} @emph{base_address} - base address of the component 
on the respective MEM-AP.
 @end itemize
 @end deffn
diff --git a/src/target/coresight_trace.c b/src/target/coresight_trace.c
index e0e6360cb6..bb031b7336 100644
--- a/src/target/coresight_trace.c
+++ b/src/target/coresight_trace.c
@@ -18,6 +18,7 @@
 #include <helper/command.h>
 #include <helper/nvp.h>
 #include <helper/jim-nvp.h>
+#include "target.h"
 #include "coresight_trace.h"
 
 static OOCD_LIST_HEAD(all_cstrace_component);
@@ -30,7 +31,10 @@ static struct coresight_type *coresight_type[] = {
 
 int coresight_write_reg(const struct cstrace_component *comp, unsigned int 
reg, uint32_t value)
 {
-       return mem_ap_write_atomic_u32(comp->ap, comp->spot.base + reg, value);
+       if (comp->cstrace_ap->type == CSTRACE_AP_TYPE_TARGET)
+               return target_write_u32(comp->cstrace_ap->ap, comp->spot.base + 
reg, value);
+
+       return mem_ap_write_atomic_u32(comp->cstrace_ap->ap, comp->spot.base + 
reg, value);
 }
 
 int coresight_read_reg(const struct cstrace_component *comp, unsigned int reg, 
uint32_t *value)
@@ -38,13 +42,19 @@ int coresight_read_reg(const struct cstrace_component 
*comp, unsigned int reg, u
        if (!value)
                return ERROR_COMMAND_ARGUMENT_INVALID;
 
-       return mem_ap_read_atomic_u32(comp->ap, comp->spot.base + reg, value);
+       if (comp->cstrace_ap->type == CSTRACE_AP_TYPE_TARGET)
+               return target_read_u32(comp->cstrace_ap->ap, comp->spot.base + 
reg, value);
+
+       return mem_ap_read_atomic_u32(comp->cstrace_ap->ap, comp->spot.base + 
reg, value);
 }
 
 int coresight_read_buffer(const struct cstrace_component *comp, target_addr_t 
address,
                uint32_t size, uint8_t *buffer)
 {
-       return mem_ap_read_buf(comp->ap, buffer, 4, size / 4, address);
+       if (comp->cstrace_ap->type == CSTRACE_AP_TYPE_TARGET)
+               return target_read_buffer(comp->cstrace_ap->ap, address, size, 
buffer);
+
+       return mem_ap_read_buf(comp->cstrace_ap->ap, buffer, 4, size / 4, 
address);
 }
 
 int coresight_lock(const struct cstrace_component *comp)
@@ -90,6 +100,7 @@ int cstrace_cleanup_all(void)
        list_for_each_entry_safe(obj, tmp, &all_cstrace_component, lh) {
                free(obj->component_info);
                free(obj->private_config);
+               free(obj->cstrace_ap);
                free(obj->name);
                free(obj->type);
                free(obj);
@@ -213,12 +224,14 @@ COMMAND_HANDLER(handle_cstrace_enable)
 
 enum coresight_cfg_opts {
        CORESIGHT_CFG_TYPE,
+       CORESIGHT_CFG_TARGET,
        CORESIGHT_CFG_INVALID = -1
 };
 
 static struct nvp nvp_config_opts[] = {
        { .name = "-type", .value = CORESIGHT_CFG_TYPE },
-       { .name = NULL, .value = CORESIGHT_CFG_INVALID }
+       { .name = "-target", .value = CORESIGHT_CFG_TARGET },
+       { .name = NULL, .value = CORESIGHT_CFG_INVALID },
 };
 
 static COMMAND_HELPER(cstrace_component_configure, struct cstrace_component 
*comp, unsigned int index, bool is_configure)
@@ -280,6 +293,32 @@ static COMMAND_HELPER(cstrace_component_configure, struct 
cstrace_component *com
                                return ERROR_COMMAND_SYNTAX_ERROR;
                        command_print(CMD, "%s", comp->type->name);
                        break;
+               case CORESIGHT_CFG_TARGET:
+                       if (is_configure) {
+                               if (index == CMD_ARGC) {
+                                       command_print(CMD, "missing argument to 
%s", CMD_ARGV[index - 1]);
+                                       return ERROR_COMMAND_ARGUMENT_INVALID;
+                               }
+
+                               struct target *target = 
get_target(CMD_ARGV[index]);
+                               if (!target) {
+                                       command_print(CMD, "Target '%s' could 
not be found", CMD_ARGV[index]);
+                                       return ERROR_COMMAND_ARGUMENT_INVALID;
+                               }
+                               comp->cstrace_ap->type = CSTRACE_AP_TYPE_TARGET;
+                               comp->cstrace_ap->ap = target;
+                               index++;
+                       } else {
+                               if (index != CMD_ARGC)
+                                       return ERROR_COMMAND_SYNTAX_ERROR;
+                               if (comp->cstrace_ap->type == 
CSTRACE_AP_TYPE_TARGET) {
+                                       struct target *target = 
comp->cstrace_ap->ap;
+                                       command_print(CMD, "%s", 
target_name(target));
+                               } else {
+                                       command_print(CMD, "not target");
+                               }
+                       }
+                       break;
                }
        }
 
@@ -420,17 +459,27 @@ COMMAND_HANDLER(handle_cstrace_create)
 
        adiv5_mem_ap_spot_init(&comp->spot);
 
+       comp->cstrace_ap = calloc(1, sizeof(struct cstrace_ap));
+       if (!comp->cstrace_ap) {
+               LOG_ERROR("Out of memory");
+               free(comp->name);
+               free(comp->type);
+               free(comp);
+               return ERROR_FAIL;
+       }
+
        /* Do the rest as "configure" options */
        bool is_configure = true;
        retval = CALL_COMMAND_HANDLER(cstrace_component_configure, comp, 2, 
is_configure);
        if (retval == ERROR_OK) {
-               if (!comp->spot.dap) {
-                       command_print(CMD, "-dap required when creating 
coresight trace component");
+               if (!comp->cstrace_ap->ap && !comp->spot.dap) {
+                       command_print(CMD, "-target or -dap required when 
creating coresight trace component");
                        retval = ERROR_COMMAND_ARGUMENT_INVALID;
                }
        }
 
        if (retval != ERROR_OK) {
+               free(comp->cstrace_ap);
                free(comp->name);
                free(comp->type);
                free(comp);
@@ -443,6 +492,7 @@ COMMAND_HANDLER(handle_cstrace_create)
                        LOG_DEBUG("coresight_create failed");
                        free(comp->private_config);
                        free(comp->component_info);
+                       free(comp->cstrace_ap);
                        free(comp->name);
                        free(comp->type);
                        free(comp);
@@ -474,17 +524,23 @@ COMMAND_HANDLER(handle_cstrace_create)
        if (retval != ERROR_OK) {
                free(comp->private_config);
                free(comp->component_info);
+               free(comp->cstrace_ap);
                free(comp->name);
                free(comp->type);
                free(comp);
                return retval;
        }
 
-       comp->ap = dap_get_ap(comp->spot.dap, comp->spot.ap_num);
-       if (!comp->ap) {
+       if (!comp->cstrace_ap->ap) {
+               comp->cstrace_ap->ap = dap_get_ap(comp->spot.dap, 
comp->spot.ap_num);
+               comp->cstrace_ap->type = CSTRACE_AP_TYPE_MEM_AP;
+       }
+
+       if (!comp->cstrace_ap->ap) {
                LOG_ERROR("coresight trace cannot get AP");
                free(comp->private_config);
                free(comp->component_info);
+               free(comp->cstrace_ap);
                free(comp->name);
                free(comp->type);
                free(comp);
diff --git a/src/target/coresight_trace.h b/src/target/coresight_trace.h
index c4bcb465ab..8b09b13d0f 100644
--- a/src/target/coresight_trace.h
+++ b/src/target/coresight_trace.h
@@ -24,13 +24,23 @@
 
 #define CORESIGHT_TIMEOUT      10
 
+enum cstrace_ap_type {
+       CSTRACE_AP_TYPE_MEM_AP,
+       CSTRACE_AP_TYPE_TARGET,
+};
+
+struct cstrace_ap {
+       enum cstrace_ap_type type;
+       void *ap;
+};
+
 struct cstrace_component {
        struct list_head lh;
        target_addr_t base;
        char *name;
        struct coresight_type *type;
        struct adiv5_mem_ap_spot spot;
-       struct adiv5_ap *ap;
+       struct cstrace_ap *cstrace_ap;
        void *component_info;
        void *private_config;
 };

-- 

Reply via email to