OpenOCD
xtensa_chip.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Xtensa Chip-level Target Support for OpenOCD *
5  * Copyright (C) 2020-2022 Cadence Design Systems, Inc. *
6  ***************************************************************************/
7 
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 
12 #include "assert.h"
13 #include <target/target.h>
14 #include <target/target_type.h>
15 #include <target/arm_adi_v5.h>
16 #include <rtos/rtos.h>
17 #include "xtensa_chip.h"
18 
19 int xtensa_chip_init_arch_info(struct target *target, void *arch_info,
20  struct xtensa_debug_module_config *dm_cfg)
21 {
22  struct xtensa_chip_common *xtensa_chip = (struct xtensa_chip_common *)arch_info;
23  int ret = xtensa_init_arch_info(target, &xtensa_chip->xtensa, dm_cfg);
24  if (ret != ERROR_OK)
25  return ret;
26  /* All xtensa target structures point back to original xtensa_chip */
27  xtensa_chip->xtensa.xtensa_chip = arch_info;
28  return ERROR_OK;
29 }
30 
32 {
33  return xtensa_target_init(cmd_ctx, target);
34 }
35 
37 {
38  return ERROR_OK;
39 }
40 
41 static int xtensa_chip_poll(struct target *target)
42 {
43  enum target_state old_state = target->state;
44  int ret = xtensa_poll(target);
45 
46  if (old_state != TARGET_HALTED && target->state == TARGET_HALTED) {
47  /*Call any event callbacks that are applicable */
48  if (old_state == TARGET_DEBUG_RUNNING)
50  else
52  }
53 
54  return ret;
55 }
56 
57 static int xtensa_chip_virt2phys(struct target *target,
58  target_addr_t virtual, target_addr_t *physical)
59 {
60  if (physical) {
61  *physical = virtual;
62  return ERROR_OK;
63  }
64  return ERROR_FAIL;
65 }
66 
67 static const struct xtensa_debug_ops xtensa_chip_dm_dbg_ops = {
69  .queue_reg_read = xtensa_dm_queue_reg_read,
70  .queue_reg_write = xtensa_dm_queue_reg_write
71 };
72 
73 static const struct xtensa_power_ops xtensa_chip_dm_pwr_ops = {
75  .queue_reg_write = xtensa_dm_queue_pwr_reg_write
76 };
77 
78 static int xtensa_chip_target_create(struct target *target, Jim_Interp *interp)
79 {
80  struct xtensa_debug_module_config xtensa_chip_dm_cfg = {
82  .pwr_ops = &xtensa_chip_dm_pwr_ops,
83  .tap = NULL,
84  .queue_tdi_idle = NULL,
85  .queue_tdi_idle_arg = NULL,
86  .dap = NULL,
87  .debug_ap = NULL,
88  .debug_apsel = DP_APSEL_INVALID,
89  .ap_offset = 0,
90  };
91 
93  if (adiv5_verify_config(pc) == ERROR_OK) {
94  xtensa_chip_dm_cfg.dap = pc->dap;
95  xtensa_chip_dm_cfg.debug_apsel = pc->ap_num;
96  xtensa_chip_dm_cfg.ap_offset = target->dbgbase;
97  LOG_DEBUG("DAP: ap_num %" PRId64 " DAP %p\n", pc->ap_num, pc->dap);
98  } else {
99  xtensa_chip_dm_cfg.tap = target->tap;
100  LOG_DEBUG("JTAG: %s:%s pos %d", target->tap->chip, target->tap->tapname,
102  }
103 
104  struct xtensa_chip_common *xtensa_chip = calloc(1, sizeof(struct xtensa_chip_common));
105  if (!xtensa_chip) {
106  LOG_ERROR("Failed to alloc chip-level memory!");
107  return ERROR_FAIL;
108  }
109 
110  int ret = xtensa_chip_init_arch_info(target, xtensa_chip, &xtensa_chip_dm_cfg);
111  if (ret != ERROR_OK) {
112  LOG_ERROR("Failed to init arch info!");
113  free(xtensa_chip);
114  return ret;
115  }
116 
117  /*Assume running target. If different, the first poll will fix this. */
120  return ERROR_OK;
121 }
122 
124 {
127  free(xtensa->xtensa_chip);
128 }
129 
130 static int xtensa_chip_examine(struct target *target)
131 {
133  int retval = xtensa_dm_examine(&xtensa->dbg_mod);
134  if (retval == ERROR_OK)
135  retval = xtensa_examine(target);
136  return retval;
137 }
138 
139 static int xtensa_chip_jim_configure(struct target *target, struct jim_getopt_info *goi)
140 {
141  static bool dap_configured;
142  int ret = adiv5_jim_configure(target, goi);
143  if (ret == JIM_OK) {
144  LOG_DEBUG("xtensa '-dap' target option found");
145  dap_configured = true;
146  }
147  if (!dap_configured) {
148  LOG_DEBUG("xtensa '-dap' target option not yet found, assuming JTAG...");
149  target->has_dap = false;
150  }
151  return ret;
152 }
153 
156  .name = "xtensa",
157 
158  .poll = xtensa_chip_poll,
159  .arch_state = xtensa_chip_arch_state,
160 
161  .halt = xtensa_halt,
162  .resume = xtensa_resume,
163  .step = xtensa_step,
164 
165  .assert_reset = xtensa_assert_reset,
166  .deassert_reset = xtensa_deassert_reset,
167  .soft_reset_halt = xtensa_soft_reset_halt,
168 
169  .virt2phys = xtensa_chip_virt2phys,
170  .mmu = xtensa_mmu_is_enabled,
171  .read_memory = xtensa_read_memory,
172  .write_memory = xtensa_write_memory,
173 
174  .read_buffer = xtensa_read_buffer,
175  .write_buffer = xtensa_write_buffer,
176 
177  .checksum_memory = xtensa_checksum_memory,
178 
179  .get_gdb_reg_list = xtensa_get_gdb_reg_list,
180 
181  .add_breakpoint = xtensa_breakpoint_add,
182  .remove_breakpoint = xtensa_breakpoint_remove,
183 
184  .add_watchpoint = xtensa_watchpoint_add,
185  .remove_watchpoint = xtensa_watchpoint_remove,
186 
187  .target_create = xtensa_chip_target_create,
188  .target_jim_configure = xtensa_chip_jim_configure,
189  .init_target = xtensa_chip_target_init,
190  .examine = xtensa_chip_examine,
191  .deinit_target = xtensa_chip_target_deinit,
192 
193  .gdb_query_custom = xtensa_gdb_query_custom,
194 
195  .commands = xtensa_command_handlers,
196 };
int adiv5_verify_config(struct adiv5_private_config *pc)
Definition: arm_adi_v5.c:2366
int adiv5_jim_configure(struct target *target, struct jim_getopt_info *goi)
Definition: arm_adi_v5.c:2330
This defines formats and data structures used to talk to ADIv5 entities.
#define DP_APSEL_INVALID
Definition: arm_adi_v5.h:106
#define ERROR_FAIL
Definition: log.h:161
#define LOG_ERROR(expr ...)
Definition: log.h:123
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:155
struct adiv5_dap * dap
Definition: arm_adi_v5.h:743
A TCL -ish GetOpt like code.
Definition: jim-nvp.h:135
int abs_chain_position
Definition: jtag.h:104
char * chip
Definition: jtag.h:101
char * tapname
Definition: jtag.h:102
This holds methods shared between all instances of a given target type.
Definition: target_type.h:26
const char * name
Name of this type of target.
Definition: target_type.h:31
Definition: target.h:120
struct jtag_tap * tap
Definition: target.h:124
enum target_debug_reason debug_reason
Definition: target.h:159
enum target_state state
Definition: target.h:162
uint32_t dbgbase
Definition: target.h:180
void * private_config
Definition: target.h:170
bool has_dap
Definition: target.h:183
struct xtensa xtensa
Definition: xtensa_chip.h:16
const struct xtensa_debug_ops * dbg_ops
int(* queue_enable)(struct xtensa_debug_module *dm)
enable operation
int(* queue_reg_read)(struct xtensa_debug_module *dm, enum xtensa_dm_pwr_reg reg, uint8_t *data, uint32_t clear)
register read.
Represents a generic Xtensa core.
Definition: xtensa.h:192
struct xtensa_debug_module dbg_mod
Definition: xtensa.h:196
struct xtensa_chip_common * xtensa_chip
Definition: xtensa.h:194
int target_call_event_callbacks(struct target *target, enum target_event event)
Definition: target.c:1833
@ DBG_REASON_NOTHALTED
Definition: target.h:78
@ TARGET_EVENT_HALTED
Definition: target.h:253
@ TARGET_EVENT_DEBUG_HALTED
Definition: target.h:272
target_state
Definition: target.h:52
@ TARGET_DEBUG_RUNNING
Definition: target.h:57
@ TARGET_HALTED
Definition: target.h:55
@ TARGET_RUNNING
Definition: target.h:54
uint64_t target_addr_t
Definition: types.h:335
#define NULL
Definition: usb.h:16
int xtensa_gdb_query_custom(struct target *target, const char *packet, char **response_p)
Definition: xtensa.c:2740
int xtensa_breakpoint_add(struct target *target, struct breakpoint *breakpoint)
Definition: xtensa.c:2254
void xtensa_target_deinit(struct target *target)
Definition: xtensa.c:2961
int xtensa_watchpoint_add(struct target *target, struct watchpoint *watchpoint)
Definition: xtensa.c:2334
int xtensa_poll(struct target *target)
Definition: xtensa.c:2022
int xtensa_halt(struct target *target)
Definition: xtensa.c:1302
int xtensa_breakpoint_remove(struct target *target, struct breakpoint *breakpoint)
Definition: xtensa.c:2298
int xtensa_read_buffer(struct target *target, target_addr_t address, uint32_t count, uint8_t *buffer)
Definition: xtensa.c:1805
int xtensa_get_gdb_reg_list(struct target *target, struct reg **reg_list[], int *reg_list_size, enum target_register_class reg_class)
Definition: xtensa.c:1225
int xtensa_step(struct target *target, int current, target_addr_t address, int handle_breakpoints)
Definition: xtensa.c:1658
int xtensa_target_init(struct command_context *cmd_ctx, struct target *target)
Definition: xtensa.c:2895
int xtensa_checksum_memory(struct target *target, target_addr_t address, uint32_t count, uint32_t *checksum)
Definition: xtensa.c:2016
const struct command_registration xtensa_command_handlers[]
Definition: xtensa.c:4020
int xtensa_examine(struct target *target)
Definition: xtensa.c:780
int xtensa_init_arch_info(struct target *target, struct xtensa *xtensa, const struct xtensa_debug_module_config *dm_cfg)
Definition: xtensa.c:2851
int xtensa_resume(struct target *target, int current, target_addr_t address, int handle_breakpoints, int debug_execution)
Definition: xtensa.c:1404
int xtensa_watchpoint_remove(struct target *target, struct watchpoint *watchpoint)
Definition: xtensa.c:2390
int xtensa_write_buffer(struct target *target, target_addr_t address, uint32_t count, const uint8_t *buffer)
Definition: xtensa.c:2010
int xtensa_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: xtensa.c:1811
int xtensa_mmu_is_enabled(struct target *target, int *enabled)
Definition: xtensa.c:1294
int xtensa_deassert_reset(struct target *target)
Definition: xtensa.c:979
int xtensa_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Definition: xtensa.c:1722
int xtensa_soft_reset_halt(struct target *target)
Definition: xtensa.c:1001
int xtensa_assert_reset(struct target *target)
Definition: xtensa.c:958
static struct xtensa * target_to_xtensa(struct target *target)
Definition: xtensa.h:239
int xtensa_chip_init_arch_info(struct target *target, void *arch_info, struct xtensa_debug_module_config *dm_cfg)
Definition: xtensa_chip.c:19
static void xtensa_chip_target_deinit(struct target *target)
Definition: xtensa_chip.c:123
int xtensa_chip_target_init(struct command_context *cmd_ctx, struct target *target)
Definition: xtensa_chip.c:31
static int xtensa_chip_jim_configure(struct target *target, struct jim_getopt_info *goi)
Definition: xtensa_chip.c:139
static int xtensa_chip_examine(struct target *target)
Definition: xtensa_chip.c:130
int xtensa_chip_arch_state(struct target *target)
Definition: xtensa_chip.c:36
static int xtensa_chip_virt2phys(struct target *target, target_addr_t virtual, target_addr_t *physical)
Definition: xtensa_chip.c:57
struct target_type xtensa_chip_target
Methods for generic example of Xtensa-based chip-level targets.
Definition: xtensa_chip.c:155
static const struct xtensa_debug_ops xtensa_chip_dm_dbg_ops
Definition: xtensa_chip.c:67
static int xtensa_chip_target_create(struct target *target, Jim_Interp *interp)
Definition: xtensa_chip.c:78
static int xtensa_chip_poll(struct target *target)
Definition: xtensa_chip.c:41
static const struct xtensa_power_ops xtensa_chip_dm_pwr_ops
Definition: xtensa_chip.c:73
int xtensa_dm_queue_reg_read(struct xtensa_debug_module *dm, enum xtensa_dm_reg reg, uint8_t *value)
int xtensa_dm_queue_pwr_reg_write(struct xtensa_debug_module *dm, enum xtensa_dm_pwr_reg reg, uint32_t data)
int xtensa_dm_queue_pwr_reg_read(struct xtensa_debug_module *dm, enum xtensa_dm_pwr_reg reg, uint8_t *data, uint32_t clear)
int xtensa_dm_queue_reg_write(struct xtensa_debug_module *dm, enum xtensa_dm_reg reg, uint32_t value)
int xtensa_dm_queue_enable(struct xtensa_debug_module *dm)
int xtensa_dm_examine(struct xtensa_debug_module *dm)