Lots of RISC-V improvements.
[openocd.git] / src / target / riscv / batch.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include "batch.h"
6 #include "debug_defines.h"
7 #include "riscv.h"
8
9 #define get_field(reg, mask) (((reg) & (mask)) / ((mask) & ~((mask) << 1)))
10 #define set_field(reg, mask, val) (((reg) & ~(mask)) | (((val) * ((mask) & ~((mask) << 1))) & (mask)))
11
12 static void dump_field(int idle, const struct scan_field *field);
13
14 struct riscv_batch *riscv_batch_alloc(struct target *target, size_t scans, size_t idle)
15 {
16 scans += 4;
17 struct riscv_batch *out = calloc(1, sizeof(*out));
18 out->target = target;
19 out->allocated_scans = scans;
20 out->idle_count = idle;
21 out->data_out = malloc(sizeof(*out->data_out) * (scans) * sizeof(uint64_t));
22 out->data_in = malloc(sizeof(*out->data_in) * (scans) * sizeof(uint64_t));
23 out->fields = malloc(sizeof(*out->fields) * (scans));
24 out->last_scan = RISCV_SCAN_TYPE_INVALID;
25 out->read_keys = malloc(sizeof(*out->read_keys) * (scans));
26 return out;
27 }
28
29 void riscv_batch_free(struct riscv_batch *batch)
30 {
31 free(batch->data_in);
32 free(batch->data_out);
33 free(batch->fields);
34 free(batch);
35 }
36
37 bool riscv_batch_full(struct riscv_batch *batch)
38 {
39 return batch->used_scans > (batch->allocated_scans - 4);
40 }
41
42 int riscv_batch_run(struct riscv_batch *batch)
43 {
44 if (batch->used_scans == 0) {
45 LOG_DEBUG("Ignoring empty batch.");
46 return ERROR_OK;
47 }
48
49 keep_alive();
50
51 riscv_batch_add_nop(batch);
52
53 for (size_t i = 0; i < batch->used_scans; ++i) {
54 jtag_add_dr_scan(batch->target->tap, 1, batch->fields + i, TAP_IDLE);
55 if (batch->idle_count > 0)
56 jtag_add_runtest(batch->idle_count, TAP_IDLE);
57 }
58
59 if (jtag_execute_queue() != ERROR_OK) {
60 LOG_ERROR("Unable to execute JTAG queue");
61 return ERROR_FAIL;
62 }
63
64 for (size_t i = 0; i < batch->used_scans; ++i)
65 dump_field(batch->idle_count, batch->fields + i);
66
67 return ERROR_OK;
68 }
69
70 void riscv_batch_add_dmi_write(struct riscv_batch *batch, unsigned address, uint64_t data)
71 {
72 assert(batch->used_scans < batch->allocated_scans);
73 struct scan_field *field = batch->fields + batch->used_scans;
74 field->num_bits = riscv_dmi_write_u64_bits(batch->target);
75 field->out_value = (void *)(batch->data_out + batch->used_scans * sizeof(uint64_t));
76 field->in_value = (void *)(batch->data_in + batch->used_scans * sizeof(uint64_t));
77 riscv_fill_dmi_write_u64(batch->target, (char *)field->out_value, address, data);
78 riscv_fill_dmi_nop_u64(batch->target, (char *)field->in_value);
79 batch->last_scan = RISCV_SCAN_TYPE_WRITE;
80 batch->used_scans++;
81 }
82
83 size_t riscv_batch_add_dmi_read(struct riscv_batch *batch, unsigned address)
84 {
85 assert(batch->used_scans < batch->allocated_scans);
86 struct scan_field *field = batch->fields + batch->used_scans;
87 field->num_bits = riscv_dmi_write_u64_bits(batch->target);
88 field->out_value = (void *)(batch->data_out + batch->used_scans * sizeof(uint64_t));
89 field->in_value = (void *)(batch->data_in + batch->used_scans * sizeof(uint64_t));
90 riscv_fill_dmi_read_u64(batch->target, (char *)field->out_value, address);
91 riscv_fill_dmi_nop_u64(batch->target, (char *)field->in_value);
92 batch->last_scan = RISCV_SCAN_TYPE_READ;
93 batch->used_scans++;
94
95 /* FIXME We get the read response back on the next scan. For now I'm
96 * just sticking a NOP in there, but this should be coalesced away. */
97 riscv_batch_add_nop(batch);
98
99 batch->read_keys[batch->read_keys_used] = batch->used_scans - 1;
100 return batch->read_keys_used++;
101 }
102
103 uint64_t riscv_batch_get_dmi_read(struct riscv_batch *batch, size_t key)
104 {
105 assert(key < batch->read_keys_used);
106 size_t index = batch->read_keys[key];
107 assert(index <= batch->used_scans);
108 uint8_t *base = batch->data_in + 8 * index;
109 return base[0] |
110 ((uint64_t) base[1]) << 8 |
111 ((uint64_t) base[2]) << 16 |
112 ((uint64_t) base[3]) << 24 |
113 ((uint64_t) base[4]) << 32 |
114 ((uint64_t) base[5]) << 40 |
115 ((uint64_t) base[6]) << 48 |
116 ((uint64_t) base[7]) << 56;
117 }
118
119 void riscv_batch_add_nop(struct riscv_batch *batch)
120 {
121 assert(batch->used_scans < batch->allocated_scans);
122 struct scan_field *field = batch->fields + batch->used_scans;
123 field->num_bits = riscv_dmi_write_u64_bits(batch->target);
124 field->out_value = (void *)(batch->data_out + batch->used_scans * sizeof(uint64_t));
125 field->in_value = (void *)(batch->data_in + batch->used_scans * sizeof(uint64_t));
126 riscv_fill_dmi_nop_u64(batch->target, (char *)field->out_value);
127 riscv_fill_dmi_nop_u64(batch->target, (char *)field->in_value);
128 batch->last_scan = RISCV_SCAN_TYPE_NOP;
129 batch->used_scans++;
130 }
131
132 void dump_field(int idle, const struct scan_field *field)
133 {
134 static const char * const op_string[] = {"-", "r", "w", "?"};
135 static const char * const status_string[] = {"+", "?", "F", "b"};
136
137 if (debug_level < LOG_LVL_DEBUG)
138 return;
139
140 assert(field->out_value != NULL);
141 uint64_t out = buf_get_u64(field->out_value, 0, field->num_bits);
142 unsigned int out_op = get_field(out, DTM_DMI_OP);
143 unsigned int out_data = get_field(out, DTM_DMI_DATA);
144 unsigned int out_address = out >> DTM_DMI_ADDRESS_OFFSET;
145
146 if (field->in_value) {
147 uint64_t in = buf_get_u64(field->in_value, 0, field->num_bits);
148 unsigned int in_op = get_field(in, DTM_DMI_OP);
149 unsigned int in_data = get_field(in, DTM_DMI_DATA);
150 unsigned int in_address = in >> DTM_DMI_ADDRESS_OFFSET;
151
152 log_printf_lf(LOG_LVL_DEBUG,
153 __FILE__, __LINE__, __PRETTY_FUNCTION__,
154 "%db %di %s %08x @%02x -> %s %08x @%02x",
155 field->num_bits, idle,
156 op_string[out_op], out_data, out_address,
157 status_string[in_op], in_data, in_address);
158 } else {
159 log_printf_lf(LOG_LVL_DEBUG,
160 __FILE__, __LINE__, __PRETTY_FUNCTION__, "%db %di %s %08x @%02x -> ?",
161 field->num_bits, idle, op_string[out_op], out_data, out_address);
162 }
163 }

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)