From 8309288831c532e7f69c6ec5f164eb5a73d66792 Mon Sep 17 00:00:00 2001
From: Evgeniy Naydanov <evgeniy.naydanov@syntacore.com>
Date: Tue, 24 Dec 2024 21:00:55 +0300
Subject: [PATCH] target/riscv: pass `jtag_tap` instead of `target`

For some functions, passing `target` is excessive. The corresponding
`tap` provides all the necessary data.

Change-Id: Ie5836024a15222bda7c2b727f5dbaac38f459b3c
Signed-off-by: Evgeniy Naydanov <evgeniy.naydanov@syntacore.com>
---
 src/target/riscv/batch.c     |  3 ++-
 src/target/riscv/riscv-011.c |  4 ++--
 src/target/riscv/riscv-013.c | 35 ++++++++++++++--------------
 src/target/riscv/riscv.c     | 44 +++++++++++++++++++-----------------
 src/target/riscv/riscv.h     |  6 ++---
 5 files changed, 47 insertions(+), 45 deletions(-)

diff --git a/src/target/riscv/batch.c b/src/target/riscv/batch.c
index 4de81a0de..04cf73c03 100644
--- a/src/target/riscv/batch.c
+++ b/src/target/riscv/batch.c
@@ -288,7 +288,8 @@ int riscv_batch_run_from(struct riscv_batch *batch, size_t start_idx,
 	unsigned int delay = 0 /* to silence maybe-uninitialized */;
 	for (size_t i = start_idx; i < batch->used_scans; ++i) {
 		if (bscan_tunnel_ir_width != 0)
-			riscv_add_bscan_tunneled_scan(batch->target, batch->fields + i, batch->bscan_ctxt + i);
+			riscv_add_bscan_tunneled_scan(batch->target->tap, batch->fields + i,
+							batch->bscan_ctxt + i);
 		else
 			jtag_add_dr_scan(batch->target->tap, 1, batch->fields + i, TAP_IDLE);
 
diff --git a/src/target/riscv/riscv-011.c b/src/target/riscv/riscv-011.c
index 1b89aabfa..3a3c2ec92 100644
--- a/src/target/riscv/riscv-011.c
+++ b/src/target/riscv/riscv-011.c
@@ -308,7 +308,7 @@ static void increase_dbus_busy_delay(struct target *target)
 			info->dtmcontrol_idle, info->dbus_busy_delay,
 			info->interrupt_high_delay);
 
-	dtmcontrol_scan(target, DTMCONTROL_DBUS_RESET, NULL /* discard value */);
+	dtmcs_scan(target->tap, DTMCONTROL_DBUS_RESET, NULL /* discard value */);
 }
 
 static void increase_interrupt_high_delay(struct target *target)
@@ -1458,7 +1458,7 @@ static int examine(struct target *target)
 {
 	/* Don't need to select dbus, since the first thing we do is read dtmcontrol. */
 	uint32_t dtmcontrol;
-	if (dtmcontrol_scan(target, 0, &dtmcontrol) != ERROR_OK || dtmcontrol == 0) {
+	if (dtmcs_scan(target->tap, 0, &dtmcontrol) != ERROR_OK || dtmcontrol == 0) {
 		LOG_ERROR("Could not scan dtmcontrol. Check JTAG connectivity/board power.");
 		return ERROR_FAIL;
 	}
diff --git a/src/target/riscv/riscv-013.c b/src/target/riscv/riscv-013.c
index 91f8d330a..93ab31822 100644
--- a/src/target/riscv/riscv-013.c
+++ b/src/target/riscv/riscv-013.c
@@ -353,29 +353,28 @@ static uint32_t set_dmcontrol_hartsel(uint32_t initial, int hart_index)
 
 /*** Utility functions. ***/
 
-static void select_dmi(struct target *target)
+static void select_dmi(struct jtag_tap *tap)
 {
 	if (bscan_tunnel_ir_width != 0) {
-		select_dmi_via_bscan(target);
+		select_dmi_via_bscan(tap);
 		return;
 	}
-	if (!target->tap->enabled)
-		LOG_TARGET_ERROR(target, "BUG: Target's TAP '%s' is disabled!",
-				jtag_tap_name(target->tap));
+	if (!tap->enabled)
+		LOG_ERROR("BUG: Target's TAP '%s' is disabled!", jtag_tap_name(tap));
 
 	bool need_ir_scan = false;
 	/* FIXME: make "tap" a const pointer. */
-	for (struct jtag_tap *tap = jtag_tap_next_enabled(NULL);
-			tap; tap = jtag_tap_next_enabled(tap)) {
-		if (tap != target->tap) {
+	for (struct jtag_tap *other_tap = jtag_tap_next_enabled(NULL);
+			other_tap; other_tap = jtag_tap_next_enabled(other_tap)) {
+		if (other_tap != tap) {
 			/* Different TAP than ours - check if it is in bypass */
-			if (!tap->bypass) {
+			if (!other_tap->bypass) {
 				need_ir_scan = true;
 				break;
 			}
 		} else {
 			/* Our TAP - check if the correct instruction is already loaded */
-			if (!buf_eq(target->tap->cur_instr, select_dbus.out_value, target->tap->ir_length)) {
+			if (!buf_eq(tap->cur_instr, select_dbus.out_value, tap->ir_length)) {
 				need_ir_scan = true;
 				break;
 			}
@@ -383,14 +382,14 @@ static void select_dmi(struct target *target)
 	}
 
 	if (need_ir_scan)
-		jtag_add_ir_scan(target->tap, &select_dbus, TAP_IDLE);
+		jtag_add_ir_scan(tap, &select_dbus, TAP_IDLE);
 }
 
 static int increase_dmi_busy_delay(struct target *target)
 {
 	RISCV013_INFO(info);
 
-	int res = dtmcontrol_scan(target, DTM_DTMCS_DMIRESET,
+	int res = dtmcs_scan(target->tap, DTM_DTMCS_DMIRESET,
 			NULL /* discard result */);
 	if (res != ERROR_OK)
 		return res;
@@ -1921,7 +1920,7 @@ static int examine(struct target *target)
 	LOG_TARGET_DEBUG(target, "dbgbase=0x%x", target->dbgbase);
 
 	uint32_t dtmcontrol;
-	if (dtmcontrol_scan(target, 0, &dtmcontrol) != ERROR_OK || dtmcontrol == 0) {
+	if (dtmcs_scan(target->tap, 0, &dtmcontrol) != ERROR_OK || dtmcontrol == 0) {
 		LOG_TARGET_ERROR(target, "Could not scan dtmcontrol. Check JTAG connectivity/board power.");
 		return ERROR_FAIL;
 	}
@@ -2413,7 +2412,7 @@ static int batch_run(struct target *target, struct riscv_batch *batch)
 {
 	RISCV_INFO(r);
 	RISCV013_INFO(info);
-	select_dmi(target);
+	select_dmi(target->tap);
 	riscv_batch_add_nop(batch);
 	const int result = riscv_batch_run_from(batch, 0, &info->learned_delays,
 			/*resets_delays*/  r->reset_delays_wait >= 0,
@@ -2436,7 +2435,7 @@ static int batch_run(struct target *target, struct riscv_batch *batch)
 static int batch_run_timeout(struct target *target, struct riscv_batch *batch)
 {
 	RISCV013_INFO(info);
-	select_dmi(target);
+	select_dmi(target->tap);
 	riscv_batch_add_nop(batch);
 
 	size_t finished_scans = 0;
@@ -2819,7 +2818,7 @@ static int assert_reset(struct target *target)
 	RISCV013_INFO(info);
 	int result;
 
-	select_dmi(target);
+	select_dmi(target->tap);
 
 	if (target_has_event_action(target, TARGET_EVENT_RESET_ASSERT)) {
 		/* Run the user-supplied script if there is one. */
@@ -2873,7 +2872,7 @@ static int deassert_reset(struct target *target)
 		return ERROR_FAIL;
 	int result;
 
-	select_dmi(target);
+	select_dmi(target->tap);
 	/* Clear the reset, but make sure haltreq is still set */
 	uint32_t control = 0;
 	control = set_field(control, DM_DMCONTROL_DMACTIVE, 1);
@@ -4359,7 +4358,7 @@ read_memory_progbuf(struct target *target, const riscv_mem_access_args_t args)
 	if (dm013_select_target(target) != ERROR_OK)
 		return MEM_ACCESS_SKIPPED_TARGET_SELECT_FAILED;
 
-	select_dmi(target);
+	select_dmi(target->tap);
 
 	memset(args.read_buffer, 0, args.count * args.size);
 
diff --git a/src/target/riscv/riscv.c b/src/target/riscv/riscv.c
index f2aa02888..4004b3e57 100644
--- a/src/target/riscv/riscv.c
+++ b/src/target/riscv/riscv.c
@@ -310,18 +310,18 @@ static void riscv_sample_buf_maybe_add_timestamp(struct target *target, bool bef
 
 static int riscv_resume_go_all_harts(struct target *target);
 
-void select_dmi_via_bscan(struct target *target)
+void select_dmi_via_bscan(struct jtag_tap *tap)
 {
-	jtag_add_ir_scan(target->tap, &select_user4, TAP_IDLE);
+	jtag_add_ir_scan(tap, &select_user4, TAP_IDLE);
 	if (bscan_tunnel_type == BSCAN_TUNNEL_DATA_REGISTER)
-		jtag_add_dr_scan(target->tap, bscan_tunnel_data_register_select_dmi_num_fields,
+		jtag_add_dr_scan(tap, bscan_tunnel_data_register_select_dmi_num_fields,
 										bscan_tunnel_data_register_select_dmi, TAP_IDLE);
 	else /* BSCAN_TUNNEL_NESTED_TAP */
-		jtag_add_dr_scan(target->tap, bscan_tunnel_nested_tap_select_dmi_num_fields,
+		jtag_add_dr_scan(tap, bscan_tunnel_nested_tap_select_dmi_num_fields,
 										bscan_tunnel_nested_tap_select_dmi, TAP_IDLE);
 }
 
-int dtmcontrol_scan_via_bscan(struct target *target, uint32_t out, uint32_t *in_ptr)
+static int dtmcs_scan_via_bscan(struct jtag_tap *tap, uint32_t out, uint32_t *in_ptr)
 {
 	/* On BSCAN TAP: Select IR=USER4, issue tunneled IR scan via BSCAN TAP's DR */
 	uint8_t tunneled_dr_width[4] = {32};
@@ -386,10 +386,10 @@ int dtmcontrol_scan_via_bscan(struct target *target, uint32_t out, uint32_t *in_
 		tunneled_dr[0].out_value = bscan_one;
 		tunneled_dr[0].in_value = NULL;
 	}
-	jtag_add_ir_scan(target->tap, &select_user4, TAP_IDLE);
-	jtag_add_dr_scan(target->tap, ARRAY_SIZE(tunneled_ir), tunneled_ir, TAP_IDLE);
-	jtag_add_dr_scan(target->tap, ARRAY_SIZE(tunneled_dr), tunneled_dr, TAP_IDLE);
-	select_dmi_via_bscan(target);
+	jtag_add_ir_scan(tap, &select_user4, TAP_IDLE);
+	jtag_add_dr_scan(tap, ARRAY_SIZE(tunneled_ir), tunneled_ir, TAP_IDLE);
+	jtag_add_dr_scan(tap, ARRAY_SIZE(tunneled_dr), tunneled_dr, TAP_IDLE);
+	select_dmi_via_bscan(tap);
 
 	int retval = jtag_execute_queue();
 	if (retval != ERROR_OK) {
@@ -407,40 +407,42 @@ int dtmcontrol_scan_via_bscan(struct target *target, uint32_t out, uint32_t *in_
 }
 
 /* TODO: rename "dtmcontrol"-> "dtmcs" */
-int dtmcontrol_scan(struct target *target, uint32_t out, uint32_t *in_ptr)
+int dtmcs_scan(struct jtag_tap *tap, uint32_t out, uint32_t *in_ptr)
 {
 	uint8_t value[4];
 
 	if (bscan_tunnel_ir_width != 0)
-		return dtmcontrol_scan_via_bscan(target, out, in_ptr);
+		return dtmcs_scan_via_bscan(tap, out, in_ptr);
 
 	buf_set_u32(value, 0, 32, out);
 
-	jtag_add_ir_scan(target->tap, &select_dtmcontrol, TAP_IDLE);
+	jtag_add_ir_scan(tap, &select_dtmcontrol, TAP_IDLE);
 
 	struct scan_field field = {
 		.num_bits = 32,
 		.out_value = value,
 		.in_value = in_ptr ? value : NULL
 	};
-	jtag_add_dr_scan(target->tap, 1, &field, TAP_IDLE);
+	jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
 
 	/* Always return to dbus. */
-	jtag_add_ir_scan(target->tap, &select_dbus, TAP_IDLE);
+	jtag_add_ir_scan(tap, &select_dbus, TAP_IDLE);
 
 	int retval = jtag_execute_queue();
 	if (retval != ERROR_OK) {
-		LOG_TARGET_ERROR(target, "dtmcs scan failed, error code = %d", retval);
+		LOG_ERROR("'dtmcs' scan failed on TAP %s, error code = %d",
+				jtag_tap_name(tap), retval);
 		return retval;
 	}
 
 	if (in_ptr) {
 		assert(field.in_value);
 		uint32_t in = buf_get_u32(field.in_value, 0, 32);
-		LOG_TARGET_DEBUG(target, "DTMCS: 0x%" PRIx32 " -> 0x%" PRIx32, out, in);
+		LOG_DEBUG("TAP %s: DTMCS: 0x%" PRIx32 " -> 0x%" PRIx32,
+				jtag_tap_name(tap), out, in);
 		*in_ptr = in;
 	} else {
-		LOG_TARGET_DEBUG(target, "DTMCS: 0x%" PRIx32 " -> ?", out);
+		LOG_DEBUG("TAP %s: DTMCS: 0x%" PRIx32 " -> ?", jtag_tap_name(tap), out);
 	}
 	return ERROR_OK;
 }
@@ -2469,7 +2471,7 @@ static int riscv_examine(struct target *target)
 
 	RISCV_INFO(info);
 	uint32_t dtmcontrol;
-	if (dtmcontrol_scan(target, 0, &dtmcontrol) != ERROR_OK || dtmcontrol == 0) {
+	if (dtmcs_scan(target->tap, 0, &dtmcontrol) != ERROR_OK || dtmcontrol == 0) {
 		LOG_TARGET_ERROR(target, "Could not read dtmcontrol. Check JTAG connectivity/board power.");
 		return ERROR_FAIL;
 	}
@@ -6255,10 +6257,10 @@ int riscv_enumerate_triggers(struct target *target)
 	return ERROR_OK;
 }
 
-void riscv_add_bscan_tunneled_scan(struct target *target, const struct scan_field *field,
+void riscv_add_bscan_tunneled_scan(struct jtag_tap *tap, const struct scan_field *field,
 					riscv_bscan_tunneled_scan_context_t *ctxt)
 {
-	jtag_add_ir_scan(target->tap, &select_user4, TAP_IDLE);
+	jtag_add_ir_scan(tap, &select_user4, TAP_IDLE);
 
 	memset(ctxt->tunneled_dr, 0, sizeof(ctxt->tunneled_dr));
 	if (bscan_tunnel_type == BSCAN_TUNNEL_DATA_REGISTER) {
@@ -6291,5 +6293,5 @@ void riscv_add_bscan_tunneled_scan(struct target *target, const struct scan_fiel
 		ctxt->tunneled_dr[3].num_bits = 3;
 		ctxt->tunneled_dr[3].out_value = bscan_zero;
 	}
-	jtag_add_dr_scan(target->tap, ARRAY_SIZE(ctxt->tunneled_dr), ctxt->tunneled_dr, TAP_IDLE);
+	jtag_add_dr_scan(tap, ARRAY_SIZE(ctxt->tunneled_dr), ctxt->tunneled_dr, TAP_IDLE);
 }
diff --git a/src/target/riscv/riscv.h b/src/target/riscv/riscv.h
index ee547a604..fae94da64 100644
--- a/src/target/riscv/riscv.h
+++ b/src/target/riscv/riscv.h
@@ -432,14 +432,14 @@ extern struct scan_field select_dtmcontrol;
 extern struct scan_field select_dbus;
 extern struct scan_field select_idcode;
 
-int dtmcontrol_scan(struct target *target, uint32_t out, uint32_t *in_ptr);
+int dtmcs_scan(struct jtag_tap *tap, uint32_t out, uint32_t *in_ptr);
 
 extern struct scan_field *bscan_tunneled_select_dmi;
 extern uint32_t bscan_tunneled_select_dmi_num_fields;
 typedef enum { BSCAN_TUNNEL_NESTED_TAP, BSCAN_TUNNEL_DATA_REGISTER } bscan_tunnel_type_t;
 extern uint8_t bscan_tunnel_ir_width;
 
-void select_dmi_via_bscan(struct target *target);
+void select_dmi_via_bscan(struct jtag_tap *tap);
 
 /*** OpenOCD Interface */
 int riscv_openocd_poll(struct target *target);
@@ -495,7 +495,7 @@ void riscv_semihosting_init(struct target *target);
 
 enum semihosting_result riscv_semihosting(struct target *target, int *retval);
 
-void riscv_add_bscan_tunneled_scan(struct target *target, const struct scan_field *field,
+void riscv_add_bscan_tunneled_scan(struct jtag_tap *tap, const struct scan_field *field,
 		riscv_bscan_tunneled_scan_context_t *ctxt);
 
 int riscv_read_by_any_size(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer);