arm_adi_v5: separate ROM table parsing from command output [3/3]
[openocd.git] / src / target / arm_adi_v5.c
index 19af45a94cb4b7c5872d4d8a32f9240958306f6d..60bd5466694f0e1a045a6eba77eba3594faee863 100644 (file)
@@ -1094,11 +1094,14 @@ int dap_lookup_cs_component(struct adiv5_ap *ap,
        return ERROR_OK;
 }
 
-/** Holds registers of a CoreSight component */
+/** Holds registers and coordinates of a CoreSight component */
 struct cs_component_vals {
+       struct adiv5_ap *ap;
+       target_addr_t component_base;
        uint64_t pid;
        uint32_t cid;
        uint32_t devarch;
+       uint32_t devid;
        uint32_t devtype_memtype;
 };
 
@@ -1121,16 +1124,22 @@ static int rtp_read_cs_regs(struct adiv5_ap *ap, target_addr_t component_base,
        uint32_t pid0, pid1, pid2, pid3, pid4;
        int retval = ERROR_OK;
 
+       v->ap = ap;
+       v->component_base = component_base;
+
        /* sort by offset to gain speed */
 
        /*
-        * Registers DEVARCH and DEVTYPE are valid on Class 0x9 devices
+        * Registers DEVARCH, DEVID and DEVTYPE are valid on Class 0x9 devices
         * only, but are at offset above 0xf00, so can be read on any device
         * without triggering error. Read them for eventual use on Class 0x9.
         */
        if (retval == ERROR_OK)
                retval = mem_ap_read_u32(ap, component_base + ARM_CS_C9_DEVARCH, &v->devarch);
 
+       if (retval == ERROR_OK)
+               retval = mem_ap_read_u32(ap, component_base + ARM_CS_C9_DEVID, &v->devid);
+
        /* Same address as ARM_CS_C1_MEMTYPE */
        if (retval == ERROR_OK)
                retval = mem_ap_read_u32(ap, component_base + ARM_CS_C9_DEVTYPE, &v->devtype_memtype);
@@ -1473,116 +1482,100 @@ static int dap_devtype_display(struct command_invocation *cmd, uint32_t devtype)
        return ERROR_OK;
 }
 
-static int dap_rom_display(struct command_invocation *cmd,
-                               struct adiv5_ap *ap, target_addr_t dbgbase, int depth)
+/* Broken ROM tables can have circular references. Stop after a while */
+#define ROM_TABLE_MAX_DEPTH (16)
+
+/* TODO: these prototypes will be removed in a following patch */
+static int dap_info_mem_ap_header(struct command_invocation *cmd,
+               int retval, struct adiv5_ap *ap,
+               target_addr_t dbgbase, uint32_t apid);
+static int dap_info_cs_component(struct command_invocation *cmd,
+               int retval, struct cs_component_vals *v, int depth);
+static int dap_info_rom_table_entry(struct command_invocation *cmd,
+               int retval, int depth,
+               unsigned int offset, uint32_t romentry);
+
+static int rtp_cs_component(struct command_invocation *cmd,
+               struct adiv5_ap *ap, target_addr_t dbgbase, int depth);
+
+static int rtp_rom_loop(struct command_invocation *cmd,
+               struct adiv5_ap *ap, target_addr_t base_address, int depth,
+               unsigned int max_entries)
 {
-       struct cs_component_vals v;
-       unsigned int rom_num_entries;
-       int retval;
-       char tabs[16] = "";
+       assert(IS_ALIGNED(base_address, ARM_CS_ALIGN));
 
-       if (depth > 16) {
-               command_print(cmd, "\tTables too deep");
-               return ERROR_FAIL;
-       }
+       unsigned int offset = 0;
+       while (max_entries--) {
+               uint32_t romentry;
+               unsigned int saved_offset = offset;
 
-       if (depth)
-               snprintf(tabs, sizeof(tabs), "[L%02d] ", depth);
+               int retval = mem_ap_read_atomic_u32(ap, base_address + offset, &romentry);
+               offset += 4;
+               if (retval != ERROR_OK)
+                       LOG_DEBUG("Failed read ROM table entry");
 
-       target_addr_t base_addr = dbgbase & 0xFFFFFFFFFFFFF000ull;
-       command_print(cmd, "\t\tComponent base address " TARGET_ADDR_FMT, base_addr);
+               retval = dap_info_rom_table_entry(cmd, retval, depth, saved_offset, romentry);
+               if (retval != ERROR_OK)
+                       return retval;
 
-       retval = rtp_read_cs_regs(ap, base_addr, &v);
-       if (retval != ERROR_OK) {
-               command_print(cmd, "\t\tCan't read component, the corresponding core might be turned off");
-               return ERROR_OK; /* Don't abort recursion */
-       }
+               if (romentry == 0) {
+                       /* End of ROM table */
+                       break;
+               }
 
-       if (!is_valid_arm_cs_cidr(v.cid)) {
-               command_print(cmd, "\t\tInvalid CID 0x%08" PRIx32, v.cid);
-               return ERROR_OK; /* Don't abort recursion */
+               if (!(romentry & ARM_CS_ROMENTRY_PRESENT))
+                       continue;
+
+               /* Recurse. "romentry" is signed */
+               target_addr_t component_base = base_address + (int32_t)(romentry & ARM_CS_ROMENTRY_OFFSET_MASK);
+               retval = rtp_cs_component(cmd, ap, component_base, depth + 1);
+               if (retval != ERROR_OK) {
+                       /* TODO: do we need to send an ABORT before continuing? */
+                       LOG_DEBUG("Ignore error parsing CoreSight component");
+                       continue;
+               }
        }
 
-       /* component may take multiple 4K pages */
-       uint32_t size = ARM_CS_PIDR_SIZE(v.pid);
-       if (size > 0)
-               command_print(cmd, "\t\tStart address " TARGET_ADDR_FMT, base_addr - 0x1000 * size);
+       return ERROR_OK;
+}
 
-       command_print(cmd, "\t\tPeripheral ID 0x%010" PRIx64, v.pid);
+static int rtp_cs_component(struct command_invocation *cmd,
+               struct adiv5_ap *ap, target_addr_t base_address, int depth)
+{
+       struct cs_component_vals v;
+       int retval;
 
-       const unsigned int class = ARM_CS_CIDR_CLASS(v.cid);
-       const unsigned int part_num = ARM_CS_PIDR_PART(v.pid);
-       unsigned int designer_id = ARM_CS_PIDR_DESIGNER(v.pid);
+       assert(IS_ALIGNED(base_address, ARM_CS_ALIGN));
 
-       if (v.pid & ARM_CS_PIDR_JEDEC) {
-               /* JEP106 code */
-               command_print(cmd, "\t\tDesigner is 0x%03x, %s",
-                               designer_id, jep106_manufacturer(designer_id));
-       } else {
-               /* Legacy ASCII ID, clear invalid bits */
-               designer_id &= 0x7f;
-               command_print(cmd, "\t\tDesigner ASCII code 0x%02x, %s",
-                               designer_id, designer_id == 0x41 ? "ARM" : "<unknown>");
-       }
+       if (depth > ROM_TABLE_MAX_DEPTH)
+               retval = ERROR_FAIL;
+       else
+               retval = rtp_read_cs_regs(ap, base_address, &v);
 
-       const struct dap_part_nums *partnum = pidr_to_part_num(designer_id, part_num);
-       command_print(cmd, "\t\tPart is 0x%03x, %s %s", part_num, partnum->type, partnum->full);
-       command_print(cmd, "\t\tComponent class is 0x%x, %s", class, class_description[class]);
+       retval = dap_info_cs_component(cmd, retval, &v, depth);
+       if (retval != ERROR_OK)
+               return ERROR_OK; /* Don't abort recursion */
 
-       if (class == ARM_CS_CLASS_0X1_ROM_TABLE) {
-               if (v.devtype_memtype & ARM_CS_C1_MEMTYPE_SYSMEM_MASK)
-                       command_print(cmd, "\t\tMEMTYPE system memory present on bus");
-               else
-                       command_print(cmd, "\t\tMEMTYPE system memory not present: dedicated debug bus");
+       if (!is_valid_arm_cs_cidr(v.cid))
+               return ERROR_OK; /* Don't abort recursion */
 
-               rom_num_entries = 960;
-       } else if (class == ARM_CS_CLASS_0X9_CS_COMPONENT) {
-               retval = dap_devtype_display(cmd, v.devtype_memtype);
-               if (retval != ERROR_OK)
-                       return retval;
+       const unsigned int class = ARM_CS_CIDR_CLASS(v.cid);
 
-               /* REVISIT also show ARM_CS_C9_DEVID */
+       if (class == ARM_CS_CLASS_0X1_ROM_TABLE)
+               return rtp_rom_loop(cmd, ap, base_address, depth, 960);
 
+       if (class == ARM_CS_CLASS_0X9_CS_COMPONENT) {
                if ((v.devarch & ARM_CS_C9_DEVARCH_PRESENT) == 0)
                        return ERROR_OK;
 
-               unsigned int architect_id = (v.devarch & ARM_CS_C9_DEVARCH_ARCHITECT_MASK) >> ARM_CS_C9_DEVARCH_ARCHITECT_SHIFT;
-               unsigned int revision = (v.devarch & ARM_CS_C9_DEVARCH_REVISION_MASK) >> ARM_CS_C9_DEVARCH_REVISION_SHIFT;
-               command_print(cmd, "\t\tDev Arch is 0x%08" PRIx32 ", %s \"%s\" rev.%u", v.devarch,
-                               jep106_manufacturer(architect_id), class0x9_devarch_description(v.devarch),
-                               revision);
                /* quit if not ROM table */
                if ((v.devarch & DEVARCH_ID_MASK) != DEVARCH_ROM_C_0X9)
                        return ERROR_OK;
 
-               rom_num_entries = 512;
-       } else {
-               /* Class other than 0x1 and 0x9 */
-               return ERROR_OK;
-       }
-
-       /* Read ROM table entries from base address until we get 0x00000000 or reach the reserved area */
-       for (unsigned int entry_offset = 0; entry_offset < 4 * rom_num_entries; entry_offset += 4) {
-               uint32_t romentry;
-               retval = mem_ap_read_atomic_u32(ap, base_addr + entry_offset, &romentry);
-               if (retval != ERROR_OK)
-                       return retval;
-               command_print(cmd, "\t%sROMTABLE[0x%x] = 0x%08" PRIx32 "",
-                               tabs, entry_offset, romentry);
-               if (romentry & ARM_CS_ROMENTRY_PRESENT) {
-                       /* Recurse. "romentry" is signed */
-                       retval = dap_rom_display(cmd, ap, base_addr + (int32_t)(romentry & ARM_CS_ROMENTRY_OFFSET_MASK),
-                                                                        depth + 1);
-                       if (retval != ERROR_OK)
-                               return retval;
-               } else if (romentry != 0) {
-                       command_print(cmd, "\t\tComponent not present");
-               } else {
-                       command_print(cmd, "\t%s\tEnd of ROM table", tabs);
-                       break;
-               }
+               return rtp_rom_loop(cmd, ap, base_address, depth, 512);
        }
 
+       /* Class other than 0x1 and 0x9 */
        return ERROR_OK;
 }
 
@@ -1592,13 +1585,48 @@ int dap_info_command(struct command_invocation *cmd,
        int retval;
        uint32_t apid;
        target_addr_t dbgbase;
-       target_addr_t dbgaddr;
+       target_addr_t invalid_entry;
 
        /* Now we read ROM table ID registers, ref. ARM IHI 0029B sec  */
        retval = dap_get_debugbase(ap, &dbgbase, &apid);
+       retval = dap_info_mem_ap_header(cmd, retval, ap, dbgbase, apid);
        if (retval != ERROR_OK)
                return retval;
 
+       if (apid == 0)
+               return ERROR_FAIL;
+
+       /* NOTE: a MEM-AP may have a single CoreSight component that's
+        * not a ROM table ... or have no such components at all.
+        */
+       const unsigned int class = (apid & AP_REG_IDR_CLASS_MASK) >> AP_REG_IDR_CLASS_SHIFT;
+
+       if (class == AP_REG_IDR_CLASS_MEM_AP) {
+               if (is_64bit_ap(ap))
+                       invalid_entry = 0xFFFFFFFFFFFFFFFFull;
+               else
+                       invalid_entry = 0xFFFFFFFFul;
+
+               if (dbgbase != invalid_entry && (dbgbase & 0x3) != 0x2)
+                       rtp_cs_component(cmd, ap, dbgbase & 0xFFFFFFFFFFFFF000ull, 0);
+       }
+
+       return ERROR_OK;
+}
+
+/* Actions for command "dap info" */
+
+static int dap_info_mem_ap_header(struct command_invocation *cmd,
+               int retval, struct adiv5_ap *ap,
+               target_addr_t dbgbase, uint32_t apid)
+{
+       target_addr_t invalid_entry;
+
+       if (retval != ERROR_OK) {
+               command_print(cmd, "\t\tCan't read MEM-AP, the corresponding core might be turned off");
+               return retval;
+       }
+
        command_print(cmd, "AP ID register 0x%8.8" PRIx32, apid);
        if (apid == 0) {
                command_print(cmd, "No AP found at this ap 0x%x", ap->ap_num);
@@ -1614,22 +1642,136 @@ int dap_info_command(struct command_invocation *cmd,
 
        if (class == AP_REG_IDR_CLASS_MEM_AP) {
                if (is_64bit_ap(ap))
-                       dbgaddr = 0xFFFFFFFFFFFFFFFFull;
+                       invalid_entry = 0xFFFFFFFFFFFFFFFFull;
                else
-                       dbgaddr = 0xFFFFFFFFul;
+                       invalid_entry = 0xFFFFFFFFul;
 
                command_print(cmd, "MEM-AP BASE " TARGET_ADDR_FMT, dbgbase);
 
-               if (dbgbase == dbgaddr || (dbgbase & 0x3) == 0x2) {
+               if (dbgbase == invalid_entry || (dbgbase & 0x3) == 0x2) {
                        command_print(cmd, "\tNo ROM table present");
                } else {
                        if (dbgbase & 0x01)
                                command_print(cmd, "\tValid ROM table present");
                        else
                                command_print(cmd, "\tROM table in legacy format");
+               }
+       }
+
+       return ERROR_OK;
+}
+
+static int dap_info_cs_component(struct command_invocation *cmd,
+               int retval, struct cs_component_vals *v, int depth)
+{
+       if (depth > ROM_TABLE_MAX_DEPTH) {
+               command_print(cmd, "\tTables too deep");
+               return ERROR_FAIL;
+       }
+
+       command_print(cmd, "\t\tComponent base address " TARGET_ADDR_FMT, v->component_base);
+
+       if (retval != ERROR_OK) {
+               command_print(cmd, "\t\tCan't read component, the corresponding core might be turned off");
+               return retval;
+       }
+
+       if (!is_valid_arm_cs_cidr(v->cid)) {
+               command_print(cmd, "\t\tInvalid CID 0x%08" PRIx32, v->cid);
+               return ERROR_OK; /* Don't abort recursion */
+       }
+
+       /* component may take multiple 4K pages */
+       uint32_t size = ARM_CS_PIDR_SIZE(v->pid);
+       if (size > 0)
+               command_print(cmd, "\t\tStart address " TARGET_ADDR_FMT, v->component_base - 0x1000 * size);
+
+       command_print(cmd, "\t\tPeripheral ID 0x%010" PRIx64, v->pid);
+
+       const unsigned int part_num = ARM_CS_PIDR_PART(v->pid);
+       unsigned int designer_id = ARM_CS_PIDR_DESIGNER(v->pid);
+
+       if (v->pid & ARM_CS_PIDR_JEDEC) {
+               /* JEP106 code */
+               command_print(cmd, "\t\tDesigner is 0x%03x, %s",
+                               designer_id, jep106_manufacturer(designer_id));
+       } else {
+               /* Legacy ASCII ID, clear invalid bits */
+               designer_id &= 0x7f;
+               command_print(cmd, "\t\tDesigner ASCII code 0x%02x, %s",
+                               designer_id, designer_id == 0x41 ? "ARM" : "<unknown>");
+       }
+
+       const struct dap_part_nums *partnum = pidr_to_part_num(designer_id, part_num);
+       command_print(cmd, "\t\tPart is 0x%03x, %s %s", part_num, partnum->type, partnum->full);
+
+       const unsigned int class = ARM_CS_CIDR_CLASS(v->cid);
+       command_print(cmd, "\t\tComponent class is 0x%x, %s", class, class_description[class]);
+
+       if (class == ARM_CS_CLASS_0X1_ROM_TABLE) {
+               if (v->devtype_memtype & ARM_CS_C1_MEMTYPE_SYSMEM_MASK)
+                       command_print(cmd, "\t\tMEMTYPE system memory present on bus");
+               else
+                       command_print(cmd, "\t\tMEMTYPE system memory not present: dedicated debug bus");
+               return ERROR_OK;
+       }
+
+       if (class == ARM_CS_CLASS_0X9_CS_COMPONENT) {
+               dap_devtype_display(cmd, v->devtype_memtype);
 
-                       dap_rom_display(cmd, ap, dbgbase & 0xFFFFFFFFFFFFF000ull, 0);
+               /* REVISIT also show ARM_CS_C9_DEVID */
+
+               if ((v->devarch & ARM_CS_C9_DEVARCH_PRESENT) == 0)
+                       return ERROR_OK;
+
+               unsigned int architect_id = ARM_CS_C9_DEVARCH_ARCHITECT(v->devarch);
+               unsigned int revision = ARM_CS_C9_DEVARCH_REVISION(v->devarch);
+               command_print(cmd, "\t\tDev Arch is 0x%08" PRIx32 ", %s \"%s\" rev.%u", v->devarch,
+                               jep106_manufacturer(architect_id), class0x9_devarch_description(v->devarch),
+                               revision);
+
+               if ((v->devarch & DEVARCH_ID_MASK) == DEVARCH_ROM_C_0X9) {
+                       command_print(cmd, "\t\tType is ROM table");
+
+                       if (v->devid & ARM_CS_C9_DEVID_SYSMEM_MASK)
+                               command_print(cmd, "\t\tMEMTYPE system memory present on bus");
+                       else
+                               command_print(cmd, "\t\tMEMTYPE system memory not present: dedicated debug bus");
                }
+               return ERROR_OK;
+       }
+
+       /* Class other than 0x1 and 0x9 */
+       return ERROR_OK;
+}
+
+static int dap_info_rom_table_entry(struct command_invocation *cmd,
+               int retval, int depth,
+               unsigned int offset, uint32_t romentry)
+{
+       char tabs[16] = "";
+
+       if (depth)
+               snprintf(tabs, sizeof(tabs), "[L%02d] ", depth);
+
+       if (retval != ERROR_OK) {
+               command_print(cmd, "\t%sROMTABLE[0x%x] Read error", tabs, offset);
+               command_print(cmd, "\t\tUnable to continue");
+               command_print(cmd, "\t%s\tStop parsing of ROM table", tabs);
+               return retval;
+       }
+
+       command_print(cmd, "\t%sROMTABLE[0x%x] = 0x%08" PRIx32,
+                       tabs, offset, romentry);
+
+       if (romentry == 0) {
+               command_print(cmd, "\t%s\tEnd of ROM table", tabs);
+               return ERROR_OK;
+       }
+
+       if (!(romentry & ARM_CS_ROMENTRY_PRESENT)) {
+               command_print(cmd, "\t\tComponent not present");
+               return ERROR_OK;
        }
 
        return ERROR_OK;

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)